generated from hulk/gd32e23x_template_cmake_vscode
Compare commits
4 Commits
b6485c5c39
...
dev_featur
Author | SHA1 | Date | |
---|---|---|---|
dbb65695c9 | |||
75ea93cd53 | |||
233576fd9a | |||
b78c1e416a |
@@ -32,6 +32,7 @@ set(TARGET_SRC
|
||||
Src/i2c.c
|
||||
Src/ldc1612.c
|
||||
Src/tmp112.c
|
||||
Src/board_config.c
|
||||
)
|
||||
|
||||
# 设置输出目录
|
||||
|
@@ -1,6 +1,10 @@
|
||||
#ifndef BOARD_CONFIG_H
|
||||
#define BOARD_CONFIG_H
|
||||
|
||||
#define GD32E23XF4 0x10
|
||||
#define GD32E23XF6 0x20
|
||||
#define GD32E23XF8 0x40
|
||||
|
||||
/* >>>>>>>>>>>>>>>>>>>>[RS485 PHY DEFINE]<<<<<<<<<<<<<<<<<<<< */
|
||||
|
||||
// #define RS485_MAX13487 // RS485 PHY : MAX13487 (AutoDir)
|
||||
@@ -23,6 +27,23 @@
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/* Dynamic USART Configuration Structure */
|
||||
typedef struct {
|
||||
uint32_t rcu_usart;
|
||||
uint32_t usart_periph;
|
||||
IRQn_Type irq_type;
|
||||
void (*irq_handler)(void); // 函数指针:指向中断处理函数
|
||||
} usart_config_t;
|
||||
|
||||
extern usart_config_t g_usart_config;
|
||||
extern uint8_t g_mcu_flash_size;
|
||||
|
||||
/* USART中断处理函数声明 */
|
||||
void usart0_irq_handler(void);
|
||||
void usart1_irq_handler(void);
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#define RCU_GPIO_I2C RCU_GPIOF
|
||||
#define RCU_I2C RCU_I2C0
|
||||
#define I2C_SCL_PORT GPIOF
|
||||
@@ -35,22 +56,25 @@
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#define LED_RCU RCU_GPIOA
|
||||
#define LED_PORT GPIOA
|
||||
#define LED_PIN GPIO_PIN_7
|
||||
#define LED_RCU RCU_GPIOA
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#define RS485_RCU RCU_USART0
|
||||
#define RS485_RCU (g_usart_config.rcu_usart)
|
||||
#define RS485_PHY (g_usart_config.usart_periph)
|
||||
#define RS485_IRQ (g_usart_config.irq_type)
|
||||
#define RS485_GPIO_RCU RCU_GPIOA
|
||||
#define RS485_GPIO_PORT GPIOA
|
||||
#define RS485_EN_PIN GPIO_PIN_1
|
||||
#define RS485_TX_PIN GPIO_PIN_2
|
||||
#define RS485_RX_PIN GPIO_PIN_3
|
||||
#define RS485_PHY USART0
|
||||
#define RS485_BAUDRATE 115200U
|
||||
#define RS485_EN_PIN GPIO_PIN_1
|
||||
#define RS485_IRQ USART0_IRQn
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void mcu_detect_and_config(void);
|
||||
uint8_t get_flash_size(void);
|
||||
|
||||
#endif //BOARD_CONFIG_H
|
||||
|
18
Inc/dlpc3421.h
Normal file
18
Inc/dlpc3421.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef __DLPC3421_H
|
||||
#define __DLPC3421_H
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
bool dlp_probe(void);
|
||||
int dlp_init(void);
|
||||
void dlp_on(void);
|
||||
void dlp_off(void);
|
||||
void dlp_set_current(uint8_t r, uint8_t g, uint8_t b);
|
||||
void dlp_reset(void);
|
||||
void dlp_dump_regs(void);
|
||||
|
||||
#ifdef DLP_PATTERN_TEST
|
||||
void dlp_test_pattern(uint8_t pattern_id);
|
||||
#endif
|
||||
|
||||
#endif
|
46
Inc/iic_new.h
Normal file
46
Inc/iic_new.h
Normal file
@@ -0,0 +1,46 @@
|
||||
// ...existing code...
|
||||
|
||||
/* function declarations */
|
||||
i2c_result_t i2c_config(void);
|
||||
i2c_result_t i2c_bus_reset(void);
|
||||
void i2c_scan(void);
|
||||
|
||||
/* generic read/write functions with configurable length */
|
||||
i2c_result_t i2c_write(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t length);
|
||||
i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t length);
|
||||
|
||||
/* compatibility functions for legacy code */
|
||||
i2c_result_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2]);
|
||||
i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data);
|
||||
|
||||
/* convenience functions for common operations */
|
||||
i2c_result_t i2c_write_8bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data);
|
||||
i2c_result_t i2c_read_8bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data);
|
||||
i2c_result_t i2c_write_32bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[4]);
|
||||
i2c_result_t i2c_read_32bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data);
|
||||
|
||||
#ifdef DEBUG_VERBOSE
|
||||
const char* i2c_get_status_string(i2c_result_t status);
|
||||
#endif
|
||||
|
||||
// ...existing code...
|
||||
|
||||
|
||||
// 读取显示面板参数 (13字节)
|
||||
uint8_t display_params[13];
|
||||
i2c_result_t result = i2c_read_display_params(0x3C, 0x0F, display_params, 13);
|
||||
|
||||
// 或者使用通用函数
|
||||
result = i2c_read(0x3C, 0x0F, display_params, 13);
|
||||
|
||||
// 读取单个参数
|
||||
uint8_t startup_mode;
|
||||
result = i2c_read_8bits(0x3C, 0x0F, &startup_mode);
|
||||
|
||||
// 读取2字节参数(如显示界面宽度)
|
||||
uint8_t width_data[2];
|
||||
result = i2c_read(0x3C, 0x10, width_data, 2);
|
||||
|
||||
// 写入多字节配置
|
||||
uint8_t config_data[5] = {0x01, 0x02, 0x03, 0x04, 0x05};
|
||||
result = i2c_write(0x3C, 0x20, config_data, 5);
|
52
Src/board_config.c
Normal file
52
Src/board_config.c
Normal file
@@ -0,0 +1,52 @@
|
||||
#include "gd32e23x.h"
|
||||
#include "board_config.h"
|
||||
#include "systick.h"
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#define FLASH_SIZE_ADDR (*(const uint8_t *)0x1FFFF7E0) // Flash base address
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/* 前向声明中断处理函数 */
|
||||
void usart0_irq_handler(void);
|
||||
void usart1_irq_handler(void);
|
||||
|
||||
usart_config_t g_usart_config = {
|
||||
.rcu_usart = RCU_USART1,
|
||||
.usart_periph = USART1,
|
||||
.irq_type = USART1_IRQn,
|
||||
.irq_handler = usart1_irq_handler // 初始化函数指针
|
||||
};
|
||||
|
||||
uint8_t g_mcu_flash_size = 0;
|
||||
|
||||
void mcu_detect_and_config(void) {
|
||||
g_mcu_flash_size = FLASH_SIZE_ADDR;
|
||||
|
||||
switch (g_mcu_flash_size) {
|
||||
case GD32E23XF4:
|
||||
g_usart_config.rcu_usart = RCU_USART0;
|
||||
g_usart_config.usart_periph = USART0;
|
||||
g_usart_config.irq_type = USART0_IRQn;
|
||||
g_usart_config.irq_handler = usart0_irq_handler; // 指向USART0处理函数
|
||||
break;
|
||||
case GD32E23XF6:
|
||||
g_usart_config.rcu_usart = RCU_USART1;
|
||||
g_usart_config.usart_periph = USART1;
|
||||
g_usart_config.irq_type = USART1_IRQn;
|
||||
g_usart_config.irq_handler = usart1_irq_handler; // 指向USART1处理函数
|
||||
break;
|
||||
default: // Default to GD32E23XF8
|
||||
g_usart_config.rcu_usart = RCU_USART1;
|
||||
g_usart_config.usart_periph = USART1;
|
||||
g_usart_config.irq_type = USART1_IRQn;
|
||||
g_usart_config.irq_handler = usart1_irq_handler; // 指向USART1处理函数
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
uint8_t get_flash_size(void) {
|
||||
return g_mcu_flash_size;
|
||||
}
|
187
Src/dlpc3421.c
Normal file
187
Src/dlpc3421.c
Normal file
@@ -0,0 +1,187 @@
|
||||
/***************************************************************************
|
||||
* DLPC3421 全功能驱动模板(GD32E230)
|
||||
* 作者:xxx
|
||||
* 版本:v1.0
|
||||
* 说明:
|
||||
* 1. 覆盖上电→初始化→正常投影→关机→异常复位全部状态
|
||||
* 2. 提供精简 API:dlp_on / dlp_off / dlp_set_current / dlp_set_pattern
|
||||
* 3. 所有 I²C 操作带超时重试、CRC 打印、断言保护
|
||||
* 4. 支持在线调试:dlp_dump_regs()
|
||||
***************************************************************************/
|
||||
#include "gd32e23x.h"
|
||||
#include "dlpc3421.h"
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/* -------------------- 用户可调宏 -------------------- */
|
||||
#define DLPC_I2C_ADDR 0x1B /* 7-bit 地址 */
|
||||
#define I2C_TIMEOUT_MS 100
|
||||
#define DLPC_BOOT_DELAY_MS 200
|
||||
#define MAX_RETRY 3
|
||||
|
||||
/* -------------------- 内部宏 ------------------------ */
|
||||
#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
|
||||
|
||||
/* -------------------- I²C 底层封装 ------------------ */
|
||||
static int i2c_write(uint8_t dev, const uint8_t *tx, uint16_t len)
|
||||
{
|
||||
/* 你的 i2c 发送实现,这里用伪代码占位 */
|
||||
/* 返回 0 成功,-1 失败 */
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int i2c_read(uint8_t dev, uint8_t *rx, uint16_t len)
|
||||
{
|
||||
/* 你的 i2c 接收实现,这里用伪代码占位 */
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* -------------------- 寄存器 / 指令表 --------------- */
|
||||
typedef enum {
|
||||
CMD_WRITE_ENABLE = 0x50, /* 1B bit0=1 打开手动电流 / CAIC */
|
||||
CMD_LED_ENABLE = 0x52, /* 1B bit0=R,bit1=G,bit2=B */
|
||||
CMD_LED_CURRENT_MANUAL = 0x54, /* 3B R/G/B mA */
|
||||
CMD_MAX_CURRENT_LIMIT = 0x5C, /* 3B R/G/B max */
|
||||
CMD_PATTERN_CONTROL = 0x60, /* 2B 模式/触发 */
|
||||
CMD_STATUS = 0xD0, /* 1B bit0=Busy */
|
||||
CMD_CHIP_ID = 0xD2, /* 2B 返回 0x34 0x21 */
|
||||
CMD_SOFTWARE_RESET = 0xF0 /* 1B 0x01 软复位 */
|
||||
} dlp_cmd_t;
|
||||
|
||||
/* -------------------- 内部工具函数 ------------------ */
|
||||
static bool dlp_wait_not_busy(uint32_t timeout_ms)
|
||||
{
|
||||
uint8_t cmd = CMD_STATUS;
|
||||
uint8_t sts = 0x01;
|
||||
while (timeout_ms--) {
|
||||
if (i2c_write(DLPC_I2C_ADDR, &cmd, 1) == 0 &&
|
||||
i2c_read (DLPC_I2C_ADDR, &sts, 1) == 0) {
|
||||
if (!(sts & 0x01))
|
||||
return true;
|
||||
}
|
||||
delay_1ms(1);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static int dlp_send_cmd(uint8_t cmd, const uint8_t *data, uint8_t len)
|
||||
{
|
||||
uint8_t buf[32];
|
||||
buf[0] = cmd;
|
||||
if (len) memcpy(&buf[1], data, len);
|
||||
for (int i = 0; i < MAX_RETRY; ++i) {
|
||||
if (i2c_write(DLPC_I2C_ADDR, buf, len+1) == 0 &&
|
||||
dlp_wait_not_busy(I2C_TIMEOUT_MS))
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* -------------------- 对外 API ---------------------- */
|
||||
/* 1. 芯片识别 */
|
||||
bool dlp_probe(void)
|
||||
{
|
||||
uint8_t cmd = CMD_CHIP_ID;
|
||||
uint8_t id[2] = {0};
|
||||
if (i2c_write(DLPC_I2C_ADDR, &cmd, 1) == 0 &&
|
||||
i2c_read(DLPC_I2C_ADDR, id, 2) == 0) {
|
||||
return (id[0] == 0x34 && id[1] == 0x21);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* 2. 上电初始化流程(参考 TI 手册 Figure 3-1) */
|
||||
int dlp_init(void)
|
||||
{
|
||||
/* Step-1: 上电等待 tPU */
|
||||
delay_1ms(DLPC_BOOT_DELAY_MS);
|
||||
|
||||
if (!dlp_probe()) return -1;
|
||||
|
||||
/* Step-2: 设置最大电流限制(可选,按 LED 规格) */
|
||||
uint8_t max_i[3] = {255, 255, 255};
|
||||
if (dlp_send_cmd(CMD_MAX_CURRENT_LIMIT, max_i, 3)) return -2;
|
||||
|
||||
/* Step-3: 打开手动电流控制 */
|
||||
uint8_t manual_on = 0x01;
|
||||
if (dlp_send_cmd(CMD_WRITE_ENABLE, &manual_on, 1)) return -3;
|
||||
|
||||
return 0; /* OK */
|
||||
}
|
||||
|
||||
/* 3. 开关光机(含 Busy 轮询) */
|
||||
void dlp_on(void)
|
||||
{
|
||||
uint8_t leds = 0x07; /* RGB ON */
|
||||
dlp_send_cmd(CMD_LED_ENABLE, &leds, 1);
|
||||
}
|
||||
|
||||
void dlp_off(void)
|
||||
{
|
||||
uint8_t leds = 0x00; /* RGB OFF */
|
||||
dlp_send_cmd(CMD_LED_ENABLE, &leds, 1);
|
||||
}
|
||||
|
||||
/* 4. 手动设置电流 */
|
||||
void dlp_set_current(uint8_t r, uint8_t g, uint8_t b)
|
||||
{
|
||||
uint8_t rgb[3] = {r, g, b};
|
||||
dlp_send_cmd(CMD_LED_CURRENT_MANUAL, rgb, 3);
|
||||
}
|
||||
|
||||
/* 5. 软件复位(异常恢复用) */
|
||||
void dlp_reset(void)
|
||||
{
|
||||
uint8_t rst = 0x01;
|
||||
dlp_send_cmd(CMD_SOFTWARE_RESET, &rst, 1);
|
||||
delay_1ms(DLPC_BOOT_DELAY_MS);
|
||||
dlp_init(); /* 重新初始化 */
|
||||
}
|
||||
|
||||
/* 6. 打印常用寄存器(调试) */
|
||||
void dlp_dump_regs(void)
|
||||
{
|
||||
uint8_t regs[] = {CMD_STATUS, CMD_CHIP_ID};
|
||||
uint8_t buf[2];
|
||||
for (size_t i = 0; i < ARRAY_SIZE(regs); ++i) {
|
||||
if (i2c_write(DLPC_I2C_ADDR, ®s[i], 1) == 0 &&
|
||||
i2c_read(DLPC_I2C_ADDR, buf, 1+(regs[i]==CMD_CHIP_ID)) == 0) {
|
||||
printf("Reg 0x%02X : ", regs[i]);
|
||||
for (size_t j = 0; j < (regs[i]==CMD_CHIP_ID?2:1); ++j)
|
||||
printf("%02X ", buf[j]);
|
||||
printf("\r\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 7. 可选:测试图案模式 */
|
||||
#ifdef DLP_PATTERN_TEST
|
||||
void dlp_test_pattern(uint8_t pattern_id)
|
||||
{
|
||||
uint8_t data[2] = {pattern_id, 0x01}; /* 打开内部测试图 */
|
||||
dlp_send_cmd(CMD_PATTERN_CONTROL, data, 2);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* -------------------- 使用示例 ---------------------- */
|
||||
#if 0
|
||||
int main(void)
|
||||
{
|
||||
i2c_init(); /* 你的 GD32 I²C 初始化 */
|
||||
if (dlp_init() != 0) {
|
||||
printf("DLPC3421 init fail\r\n");
|
||||
while (1);
|
||||
}
|
||||
dlp_on();
|
||||
dlp_set_current(80, 80, 80); /* 白光 80 mA */
|
||||
delay_1ms(5000);
|
||||
dlp_off();
|
||||
while (1);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
把 dlpc3421.c 和 dlpc3421.h 放到你的 GD32 工程 Src / Inc 目录。
|
||||
把 i2c_write / i2c_read / delay_1ms 替换为你自己的 HAL/裸机实现。
|
||||
dlp_probe() 可用于启动自检;dlp_dump_regs() 用于串口调试。
|
||||
至此,DLPC3421 的全部已知流程都已覆盖,可直接编译、调试、裁剪。
|
@@ -37,6 +37,7 @@ OF SUCH DAMAGE.
|
||||
#include "uart.h"
|
||||
#include "uart_ring_buffer.h"
|
||||
#include "led.h"
|
||||
#include "board_config.h"
|
||||
|
||||
/*!
|
||||
\brief this function handles NMI exception
|
||||
@@ -102,8 +103,15 @@ void SysTick_Handler(void) {
|
||||
}
|
||||
|
||||
void USART0_IRQHandler(void) {
|
||||
if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) {
|
||||
uint8_t data = usart_data_receive(USART0);
|
||||
(void)uart_ring_buffer_put(data); // 缓冲满时丢弃,返回值可用于统计
|
||||
// 检查当前配置是否使用USART0,并且函数指针不为空
|
||||
if(g_usart_config.usart_periph == USART0 && g_usart_config.irq_handler != 0) {
|
||||
g_usart_config.irq_handler(); // 通过函数指针调用对应的处理函数
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void USART1_IRQHandler(void) {
|
||||
// 检查当前配置是否使用USART1,并且函数指针不为空
|
||||
if(g_usart_config.usart_periph == USART1 && g_usart_config.irq_handler != 0) {
|
||||
g_usart_config.irq_handler(); // 通过函数指针调用对应的处理函数
|
||||
}
|
||||
}
|
||||
|
975
Src/iic_new.c
Normal file
975
Src/iic_new.c
Normal file
@@ -0,0 +1,975 @@
|
||||
// ...existing code...
|
||||
|
||||
/*!
|
||||
\brief write data to I2C device with configurable length
|
||||
\param[in] slave_addr: slave device address (7-bit)
|
||||
\param[in] reg_addr: register address
|
||||
\param[in] data: pointer to data buffer
|
||||
\param[in] length: number of bytes to write (1-255)
|
||||
\param[out] none
|
||||
\retval i2c_result_t: operation result
|
||||
*/
|
||||
i2c_result_t i2c_write(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t length) {
|
||||
i2c_state_t state = I2C_STATE_START;
|
||||
uint16_t timeout = 0;
|
||||
uint8_t retry_count = 0;
|
||||
uint8_t data_index = 0;
|
||||
|
||||
/* parameter validation */
|
||||
if (data == NULL || slave_addr > 0x7F || length == 0) {
|
||||
return I2C_RESULT_INVALID_PARAM;
|
||||
}
|
||||
|
||||
while (retry_count < I2C_MAX_RETRY) {
|
||||
switch (state) {
|
||||
case I2C_STATE_START:
|
||||
timeout = 0;
|
||||
data_index = 0;
|
||||
|
||||
/* wait for bus to be idle */
|
||||
while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
i2c_start_on_bus(I2C0);
|
||||
timeout = 0;
|
||||
state = I2C_STATE_SEND_ADDRESS;
|
||||
break;
|
||||
|
||||
case I2C_STATE_SEND_ADDRESS:
|
||||
/* wait for start condition to be sent. SBSEND flag */
|
||||
while((!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* send slave address */
|
||||
i2c_master_addressing(I2C0, slave_addr << 1, I2C_TRANSMITTER);
|
||||
timeout = 0;
|
||||
state = I2C_STATE_CLEAR_ADDRESS;
|
||||
break;
|
||||
|
||||
case I2C_STATE_CLEAR_ADDRESS:
|
||||
/* wait for address to be acknowledged.ADDSEND set means i2c slave sends ACK */
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) && (!i2c_flag_get(I2C0, I2C_FLAG_AERR)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
} else if (i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) {
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||
timeout = 0;
|
||||
state = I2C_STATE_TRANSMIT_REG;
|
||||
break;
|
||||
} else {
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
||||
timeout = 0;
|
||||
#ifdef DEBUG_VERBOSE
|
||||
printf("IIC write failed for Error Slave Address. \n");
|
||||
#endif
|
||||
return I2C_RESULT_NACK;
|
||||
}
|
||||
|
||||
case I2C_STATE_TRANSMIT_REG:
|
||||
/* wait until the transmit data buffer is empty */
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* send register address */
|
||||
i2c_data_transmit(I2C0, reg_addr);
|
||||
timeout = 0;
|
||||
state = I2C_STATE_TRANSMIT_DATA;
|
||||
break;
|
||||
|
||||
case I2C_STATE_TRANSMIT_DATA:
|
||||
/* wait until the transmit data buffer is empty */
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* send data byte */
|
||||
i2c_data_transmit(I2C0, data[data_index]);
|
||||
data_index++;
|
||||
|
||||
/* check for errors */
|
||||
if (i2c_flag_get(I2C0, I2C_FLAG_AERR)) {
|
||||
i2c_stop_on_bus(I2C0);
|
||||
return I2C_RESULT_NACK;
|
||||
} else if (i2c_flag_get(I2C0, I2C_FLAG_BERR) || i2c_flag_get(I2C0, I2C_FLAG_LOSTARB)) {
|
||||
i2c_stop_on_bus(I2C0);
|
||||
return I2C_RESULT_ERROR;
|
||||
}
|
||||
|
||||
/* check if all data has been sent */
|
||||
if (data_index >= length) {
|
||||
/* wait until BTC bit is set for last byte */
|
||||
timeout = 0;
|
||||
while (!i2c_flag_get(I2C0, I2C_FLAG_BTC) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
state = I2C_STATE_STOP;
|
||||
}
|
||||
timeout = 0;
|
||||
break;
|
||||
|
||||
case I2C_STATE_STOP:
|
||||
/* send a stop condition to I2C bus */
|
||||
i2c_stop_on_bus(I2C0);
|
||||
|
||||
timeout = 0;
|
||||
while ((I2C_CTL0(I2C0) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* success */
|
||||
return I2C_RESULT_SUCCESS;
|
||||
|
||||
case I2C_STATE_ERROR:
|
||||
/* send a stop condition to I2C bus */
|
||||
i2c_stop_on_bus(I2C0);
|
||||
|
||||
timeout = 0;
|
||||
while ((I2C_CTL0(I2C0) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_BERR);
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_LOSTARB);
|
||||
|
||||
retry_count++;
|
||||
if (retry_count >= I2C_MAX_RETRY) {
|
||||
#ifdef DEBUG_VERBOSE
|
||||
printf("IIC write failed after %d retries\n", I2C_MAX_RETRY);
|
||||
#endif
|
||||
return I2C_RESULT_ERROR;
|
||||
}
|
||||
|
||||
/* reset state machine for retry */
|
||||
state = I2C_STATE_START;
|
||||
timeout = 0;
|
||||
|
||||
/* small delay before retry */
|
||||
delay_10us(10);
|
||||
break;
|
||||
|
||||
default:
|
||||
state = I2C_STATE_START;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return I2C_RESULT_TIMEOUT;
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief read data from I2C device with configurable length
|
||||
\param[in] slave_addr: slave device address (7-bit)
|
||||
\param[in] reg_addr: register address
|
||||
\param[out] data: pointer to data buffer
|
||||
\param[in] length: number of bytes to read (1-255)
|
||||
\retval i2c_result_t: operation result
|
||||
*/
|
||||
i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t length) {
|
||||
i2c_state_t state = I2C_STATE_START;
|
||||
uint16_t timeout = 0;
|
||||
uint8_t retry_count = 0;
|
||||
bool write_phase = true;
|
||||
uint8_t data_index = 0;
|
||||
|
||||
/* parameter validation */
|
||||
if (data == NULL || slave_addr > 0x7F || length == 0) {
|
||||
return I2C_RESULT_INVALID_PARAM;
|
||||
}
|
||||
|
||||
/* enable acknowledge */
|
||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||
|
||||
while (retry_count < (uint8_t)I2C_MAX_RETRY) {
|
||||
switch (state) {
|
||||
case I2C_STATE_START:
|
||||
timeout = 0;
|
||||
data_index = 0;
|
||||
|
||||
/* wait for bus to be idle */
|
||||
while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* send start condition */
|
||||
i2c_start_on_bus(I2C0);
|
||||
state = I2C_STATE_SEND_ADDRESS;
|
||||
timeout = 0;
|
||||
break;
|
||||
|
||||
case I2C_STATE_SEND_ADDRESS:
|
||||
/* wait for start condition to be sent */
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* send slave address */
|
||||
if (write_phase) {
|
||||
/* write phase: send address with write bit */
|
||||
i2c_master_addressing(I2C0, (slave_addr << 1), I2C_TRANSMITTER);
|
||||
} else {
|
||||
/* read phase: send address with read bit */
|
||||
i2c_master_addressing(I2C0, (slave_addr << 1) | 0x01, I2C_RECEIVER);
|
||||
}
|
||||
|
||||
state = I2C_STATE_CLEAR_ADDRESS;
|
||||
timeout = 0;
|
||||
break;
|
||||
|
||||
case I2C_STATE_CLEAR_ADDRESS:
|
||||
/* wait for address to be acknowledged */
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
if (write_phase) {
|
||||
/* clear address flag (write phase) */
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||
state = I2C_STATE_TRANSMIT_DATA;
|
||||
} else {
|
||||
/* read phase setup based on length */
|
||||
if (length == 1) {
|
||||
/* single byte read: disable ACK before clearing ADDR */
|
||||
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||
/* send STOP immediately after clearing ADDR for single byte */
|
||||
i2c_stop_on_bus(I2C0);
|
||||
} else if (length == 2) {
|
||||
/* two bytes read: set POS=NEXT and disable ACK before clearing ADDR */
|
||||
i2c_ackpos_config(I2C0, I2C_ACKPOS_NEXT);
|
||||
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||
} else {
|
||||
/* multi-byte read: clear ADDR with ACK enabled */
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||
}
|
||||
state = I2C_STATE_RECEIVE_DATA;
|
||||
}
|
||||
timeout = 0;
|
||||
break;
|
||||
|
||||
case I2C_STATE_TRANSMIT_DATA:
|
||||
/* wait for transmit buffer to be empty */
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* send register address */
|
||||
i2c_data_transmit(I2C0, reg_addr);
|
||||
state = I2C_STATE_RESTART;
|
||||
timeout = 0;
|
||||
break;
|
||||
|
||||
case I2C_STATE_RESTART:
|
||||
/* wait for byte transfer complete */
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* generate repeated start condition */
|
||||
i2c_start_on_bus(I2C0);
|
||||
|
||||
/* wait for repeated start condition to be sent */
|
||||
timeout = 0;
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* send slave address with read bit */
|
||||
i2c_master_addressing(I2C0, (slave_addr << 1), I2C_RECEIVER);
|
||||
|
||||
/* switch to read phase */
|
||||
write_phase = false;
|
||||
state = I2C_STATE_CLEAR_ADDRESS;
|
||||
timeout = 0;
|
||||
break;
|
||||
|
||||
case I2C_STATE_RECEIVE_DATA:
|
||||
if (length == 1) {
|
||||
/* single byte read */
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_RBNE)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
data[0] = i2c_data_receive(I2C0);
|
||||
state = I2C_STATE_STOP;
|
||||
} else if (length == 2) {
|
||||
/* two bytes read */
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
/* send STOP before reading the last two bytes */
|
||||
i2c_stop_on_bus(I2C0);
|
||||
/* read the two bytes back-to-back */
|
||||
data[0] = i2c_data_receive(I2C0);
|
||||
data[1] = i2c_data_receive(I2C0);
|
||||
state = I2C_STATE_STOP;
|
||||
} else {
|
||||
/* multi-byte read (length > 2) */
|
||||
while (data_index < length) {
|
||||
/* wait for RBNE (receive buffer not empty) */
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_RBNE)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* special handling for last 3 bytes */
|
||||
if (data_index == length - 3) {
|
||||
/* wait for BTF (byte transfer finished) before reading N-2 */
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
/* disable ACK for last 2 bytes */
|
||||
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
||||
}
|
||||
|
||||
/* read data byte */
|
||||
data[data_index] = i2c_data_receive(I2C0);
|
||||
data_index++;
|
||||
|
||||
/* send STOP after reading N-1 byte */
|
||||
if (data_index == length - 1) {
|
||||
i2c_stop_on_bus(I2C0);
|
||||
}
|
||||
|
||||
timeout = 0;
|
||||
}
|
||||
state = I2C_STATE_STOP;
|
||||
}
|
||||
break;
|
||||
|
||||
case I2C_STATE_STOP:
|
||||
/* wait for stop condition to complete (only if not already sent) */
|
||||
if (length > 2) {
|
||||
/* for multi-byte reads, STOP was already sent */
|
||||
/* just wait for the STOP bit to clear */
|
||||
while ((I2C_CTL0(I2C0) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* reset ACK configuration for next operation */
|
||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||
i2c_ackpos_config(I2C0, I2C_ACKPOS_CURRENT);
|
||||
|
||||
/* success */
|
||||
return I2C_RESULT_SUCCESS;
|
||||
|
||||
case I2C_STATE_ERROR:
|
||||
/* send stop condition to release bus */
|
||||
i2c_stop_on_bus(I2C0);
|
||||
|
||||
/* reset ACK configuration */
|
||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||
i2c_ackpos_config(I2C0, I2C_ACKPOS_CURRENT);
|
||||
|
||||
retry_count++;
|
||||
if (retry_count >= I2C_MAX_RETRY) {
|
||||
#ifdef DEBUG_VERBOSE
|
||||
printf("I2C read failed after %d retries\n", I2C_MAX_RETRY);
|
||||
#endif
|
||||
return I2C_RESULT_ERROR;
|
||||
}
|
||||
|
||||
/* reset state machine for retry */
|
||||
state = I2C_STATE_START;
|
||||
write_phase = true;
|
||||
timeout = 0;
|
||||
|
||||
/* small delay before retry */
|
||||
delay_10us(10);
|
||||
break;
|
||||
|
||||
default:
|
||||
state = I2C_STATE_START;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return I2C_RESULT_TIMEOUT;
|
||||
}
|
||||
|
||||
/* compatibility functions for legacy code */
|
||||
i2c_result_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2]) {
|
||||
return i2c_write(slave_addr, reg_addr, data, 2);
|
||||
}
|
||||
|
||||
i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) {
|
||||
return i2c_read(slave_addr, reg_addr, data, 2);
|
||||
}
|
||||
|
||||
/* convenience functions for common operations */
|
||||
i2c_result_t i2c_write_8bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data) {
|
||||
return i2c_write(slave_addr, reg_addr, &data, 1);
|
||||
}
|
||||
|
||||
i2c_result_t i2c_read_8bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) {
|
||||
return i2c_read(slave_addr, reg_addr, data, 1);
|
||||
}
|
||||
|
||||
i2c_result_t i2c_write_32bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[4]) {
|
||||
return i2c_write(slave_addr, reg_addr, data, 4);
|
||||
}
|
||||
|
||||
i2c_result_t i2c_read_32bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) {
|
||||
return i2c_read(slave_addr, reg_addr, data, 4);
|
||||
}
|
||||
|
||||
// ...existing code...
|
||||
|
||||
|
||||
|
||||
// ...existing code...
|
||||
|
||||
/*!
|
||||
\brief write data to I2C device with configurable length
|
||||
\param[in] slave_addr: slave device address (7-bit)
|
||||
\param[in] reg_addr: register address
|
||||
\param[in] data: pointer to data buffer
|
||||
\param[in] length: number of bytes to write (1-255)
|
||||
\param[out] none
|
||||
\retval i2c_result_t: operation result
|
||||
*/
|
||||
i2c_result_t i2c_write(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t length) {
|
||||
i2c_state_t state = I2C_STATE_START;
|
||||
uint16_t timeout = 0;
|
||||
uint8_t retry_count = 0;
|
||||
uint8_t data_index = 0;
|
||||
|
||||
/* parameter validation */
|
||||
if (data == NULL || slave_addr > 0x7F || length == 0) {
|
||||
return I2C_RESULT_INVALID_PARAM;
|
||||
}
|
||||
|
||||
while (retry_count < I2C_MAX_RETRY) {
|
||||
switch (state) {
|
||||
case I2C_STATE_START:
|
||||
timeout = 0;
|
||||
data_index = 0;
|
||||
|
||||
/* wait for bus to be idle */
|
||||
while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
i2c_start_on_bus(I2C0);
|
||||
timeout = 0;
|
||||
state = I2C_STATE_SEND_ADDRESS;
|
||||
break;
|
||||
|
||||
case I2C_STATE_SEND_ADDRESS:
|
||||
/* wait for start condition to be sent. SBSEND flag */
|
||||
while((!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* send slave address */
|
||||
i2c_master_addressing(I2C0, slave_addr << 1, I2C_TRANSMITTER);
|
||||
timeout = 0;
|
||||
state = I2C_STATE_CLEAR_ADDRESS;
|
||||
break;
|
||||
|
||||
case I2C_STATE_CLEAR_ADDRESS:
|
||||
/* wait for address to be acknowledged.ADDSEND set means i2c slave sends ACK */
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) && (!i2c_flag_get(I2C0, I2C_FLAG_AERR)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
} else if (i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) {
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||
timeout = 0;
|
||||
state = I2C_STATE_TRANSMIT_REG;
|
||||
break;
|
||||
} else {
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
||||
timeout = 0;
|
||||
#ifdef DEBUG_VERBOSE
|
||||
printf("IIC write failed for Error Slave Address. \n");
|
||||
#endif
|
||||
return I2C_RESULT_NACK;
|
||||
}
|
||||
|
||||
case I2C_STATE_TRANSMIT_REG:
|
||||
/* wait until the transmit data buffer is empty */
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* send register address */
|
||||
i2c_data_transmit(I2C0, reg_addr);
|
||||
timeout = 0;
|
||||
state = I2C_STATE_TRANSMIT_DATA;
|
||||
break;
|
||||
|
||||
case I2C_STATE_TRANSMIT_DATA:
|
||||
/* wait until the transmit data buffer is empty */
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* send data byte */
|
||||
i2c_data_transmit(I2C0, data[data_index]);
|
||||
data_index++;
|
||||
|
||||
/* check for errors */
|
||||
if (i2c_flag_get(I2C0, I2C_FLAG_AERR)) {
|
||||
i2c_stop_on_bus(I2C0);
|
||||
return I2C_RESULT_NACK;
|
||||
} else if (i2c_flag_get(I2C0, I2C_FLAG_BERR) || i2c_flag_get(I2C0, I2C_FLAG_LOSTARB)) {
|
||||
i2c_stop_on_bus(I2C0);
|
||||
return I2C_RESULT_ERROR;
|
||||
}
|
||||
|
||||
/* check if all data has been sent */
|
||||
if (data_index >= length) {
|
||||
/* wait until BTC bit is set for last byte */
|
||||
timeout = 0;
|
||||
while (!i2c_flag_get(I2C0, I2C_FLAG_BTC) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
state = I2C_STATE_STOP;
|
||||
}
|
||||
timeout = 0;
|
||||
break;
|
||||
|
||||
case I2C_STATE_STOP:
|
||||
/* send a stop condition to I2C bus */
|
||||
i2c_stop_on_bus(I2C0);
|
||||
|
||||
timeout = 0;
|
||||
while ((I2C_CTL0(I2C0) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* success */
|
||||
return I2C_RESULT_SUCCESS;
|
||||
|
||||
case I2C_STATE_ERROR:
|
||||
/* send a stop condition to I2C bus */
|
||||
i2c_stop_on_bus(I2C0);
|
||||
|
||||
timeout = 0;
|
||||
while ((I2C_CTL0(I2C0) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_BERR);
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_LOSTARB);
|
||||
|
||||
retry_count++;
|
||||
if (retry_count >= I2C_MAX_RETRY) {
|
||||
#ifdef DEBUG_VERBOSE
|
||||
printf("IIC write failed after %d retries\n", I2C_MAX_RETRY);
|
||||
#endif
|
||||
return I2C_RESULT_ERROR;
|
||||
}
|
||||
|
||||
/* reset state machine for retry */
|
||||
state = I2C_STATE_START;
|
||||
timeout = 0;
|
||||
|
||||
/* small delay before retry */
|
||||
delay_10us(10);
|
||||
break;
|
||||
|
||||
default:
|
||||
state = I2C_STATE_START;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return I2C_RESULT_TIMEOUT;
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief read data from I2C device with configurable length
|
||||
\param[in] slave_addr: slave device address (7-bit)
|
||||
\param[in] reg_addr: register address
|
||||
\param[out] data: pointer to data buffer
|
||||
\param[in] length: number of bytes to read (1-255)
|
||||
\retval i2c_result_t: operation result
|
||||
*/
|
||||
i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t length) {
|
||||
i2c_state_t state = I2C_STATE_START;
|
||||
uint16_t timeout = 0;
|
||||
uint8_t retry_count = 0;
|
||||
bool write_phase = true;
|
||||
uint8_t data_index = 0;
|
||||
|
||||
/* parameter validation */
|
||||
if (data == NULL || slave_addr > 0x7F || length == 0) {
|
||||
return I2C_RESULT_INVALID_PARAM;
|
||||
}
|
||||
|
||||
/* enable acknowledge */
|
||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||
|
||||
while (retry_count < (uint8_t)I2C_MAX_RETRY) {
|
||||
switch (state) {
|
||||
case I2C_STATE_START:
|
||||
timeout = 0;
|
||||
data_index = 0;
|
||||
|
||||
/* wait for bus to be idle */
|
||||
while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* send start condition */
|
||||
i2c_start_on_bus(I2C0);
|
||||
state = I2C_STATE_SEND_ADDRESS;
|
||||
timeout = 0;
|
||||
break;
|
||||
|
||||
case I2C_STATE_SEND_ADDRESS:
|
||||
/* wait for start condition to be sent */
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* send slave address */
|
||||
if (write_phase) {
|
||||
/* write phase: send address with write bit */
|
||||
i2c_master_addressing(I2C0, (slave_addr << 1), I2C_TRANSMITTER);
|
||||
} else {
|
||||
/* read phase: send address with read bit */
|
||||
i2c_master_addressing(I2C0, (slave_addr << 1) | 0x01, I2C_RECEIVER);
|
||||
}
|
||||
|
||||
state = I2C_STATE_CLEAR_ADDRESS;
|
||||
timeout = 0;
|
||||
break;
|
||||
|
||||
case I2C_STATE_CLEAR_ADDRESS:
|
||||
/* wait for address to be acknowledged */
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
if (write_phase) {
|
||||
/* clear address flag (write phase) */
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||
state = I2C_STATE_TRANSMIT_DATA;
|
||||
} else {
|
||||
/* read phase setup based on length */
|
||||
if (length == 1) {
|
||||
/* single byte read: disable ACK before clearing ADDR */
|
||||
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||
/* send STOP immediately after clearing ADDR for single byte */
|
||||
i2c_stop_on_bus(I2C0);
|
||||
} else if (length == 2) {
|
||||
/* two bytes read: set POS=NEXT and disable ACK before clearing ADDR */
|
||||
i2c_ackpos_config(I2C0, I2C_ACKPOS_NEXT);
|
||||
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||
} else {
|
||||
/* multi-byte read: clear ADDR with ACK enabled */
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||
}
|
||||
state = I2C_STATE_RECEIVE_DATA;
|
||||
}
|
||||
timeout = 0;
|
||||
break;
|
||||
|
||||
case I2C_STATE_TRANSMIT_DATA:
|
||||
/* wait for transmit buffer to be empty */
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* send register address */
|
||||
i2c_data_transmit(I2C0, reg_addr);
|
||||
state = I2C_STATE_RESTART;
|
||||
timeout = 0;
|
||||
break;
|
||||
|
||||
case I2C_STATE_RESTART:
|
||||
/* wait for byte transfer complete */
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* generate repeated start condition */
|
||||
i2c_start_on_bus(I2C0);
|
||||
|
||||
/* wait for repeated start condition to be sent */
|
||||
timeout = 0;
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* send slave address with read bit */
|
||||
i2c_master_addressing(I2C0, (slave_addr << 1), I2C_RECEIVER);
|
||||
|
||||
/* switch to read phase */
|
||||
write_phase = false;
|
||||
state = I2C_STATE_CLEAR_ADDRESS;
|
||||
timeout = 0;
|
||||
break;
|
||||
|
||||
case I2C_STATE_RECEIVE_DATA:
|
||||
if (length == 1) {
|
||||
/* single byte read */
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_RBNE)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
data[0] = i2c_data_receive(I2C0);
|
||||
state = I2C_STATE_STOP;
|
||||
} else if (length == 2) {
|
||||
/* two bytes read */
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
/* send STOP before reading the last two bytes */
|
||||
i2c_stop_on_bus(I2C0);
|
||||
/* read the two bytes back-to-back */
|
||||
data[0] = i2c_data_receive(I2C0);
|
||||
data[1] = i2c_data_receive(I2C0);
|
||||
state = I2C_STATE_STOP;
|
||||
} else {
|
||||
/* multi-byte read (length > 2) */
|
||||
while (data_index < length) {
|
||||
/* wait for RBNE (receive buffer not empty) */
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_RBNE)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* special handling for last 3 bytes */
|
||||
if (data_index == length - 3) {
|
||||
/* wait for BTF (byte transfer finished) before reading N-2 */
|
||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
/* disable ACK for last 2 bytes */
|
||||
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
||||
}
|
||||
|
||||
/* read data byte */
|
||||
data[data_index] = i2c_data_receive(I2C0);
|
||||
data_index++;
|
||||
|
||||
/* send STOP after reading N-1 byte */
|
||||
if (data_index == length - 1) {
|
||||
i2c_stop_on_bus(I2C0);
|
||||
}
|
||||
|
||||
timeout = 0;
|
||||
}
|
||||
state = I2C_STATE_STOP;
|
||||
}
|
||||
break;
|
||||
|
||||
case I2C_STATE_STOP:
|
||||
/* wait for stop condition to complete */
|
||||
while ((I2C_CTL0(I2C0) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout >= I2C_TIME_OUT) {
|
||||
state = I2C_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* reset ACK configuration for next operation */
|
||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||
i2c_ackpos_config(I2C0, I2C_ACKPOS_CURRENT);
|
||||
|
||||
/* success */
|
||||
return I2C_RESULT_SUCCESS;
|
||||
|
||||
case I2C_STATE_ERROR:
|
||||
/* send stop condition to release bus */
|
||||
i2c_stop_on_bus(I2C0);
|
||||
|
||||
/* reset ACK configuration */
|
||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||
i2c_ackpos_config(I2C0, I2C_ACKPOS_CURRENT);
|
||||
|
||||
retry_count++;
|
||||
if (retry_count >= I2C_MAX_RETRY) {
|
||||
#ifdef DEBUG_VERBOSE
|
||||
printf("I2C read failed after %d retries\n", I2C_MAX_RETRY);
|
||||
#endif
|
||||
return I2C_RESULT_ERROR;
|
||||
}
|
||||
|
||||
/* reset state machine for retry */
|
||||
state = I2C_STATE_START;
|
||||
write_phase = true;
|
||||
timeout = 0;
|
||||
|
||||
/* small delay before retry */
|
||||
delay_10us(10);
|
||||
break;
|
||||
|
||||
default:
|
||||
state = I2C_STATE_START;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return I2C_RESULT_TIMEOUT;
|
||||
}
|
||||
|
||||
/* convenience functions for common operations */
|
||||
i2c_result_t i2c_write_8bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data) {
|
||||
return i2c_write(slave_addr, reg_addr, &data, 1);
|
||||
}
|
||||
|
||||
i2c_result_t i2c_read_8bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) {
|
||||
return i2c_read(slave_addr, reg_addr, data, 1);
|
||||
}
|
||||
|
||||
i2c_result_t i2c_write_32bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[4]) {
|
||||
return i2c_write(slave_addr, reg_addr, data, 4);
|
||||
}
|
||||
|
||||
i2c_result_t i2c_read_32bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) {
|
||||
return i2c_read(slave_addr, reg_addr, data, 4);
|
||||
}
|
||||
|
||||
/* 显示面板专用函数 - 支持读取显示界面参数 */
|
||||
i2c_result_t i2c_read_display_params(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t length) {
|
||||
/* 针对显示面板的多字节读取,支持13字节的完整参数读取 */
|
||||
if (length > 13) {
|
||||
return I2C_RESULT_INVALID_PARAM;
|
||||
}
|
||||
return i2c_read(slave_addr, reg_addr, data, length);
|
||||
}
|
||||
|
||||
// ...existing code...
|
@@ -51,12 +51,16 @@ OF SUCH DAMAGE.
|
||||
*/
|
||||
int main(void)
|
||||
{
|
||||
led_init();
|
||||
mcu_detect_and_config();
|
||||
|
||||
setbuf(stdout, NULL);
|
||||
systick_config();
|
||||
rs485_init();
|
||||
|
||||
led_init();
|
||||
// led_init();
|
||||
|
||||
printf("Flash size: %d Kbytes\n", get_flash_size());
|
||||
|
||||
#ifdef DEBUG_VERBOSE
|
||||
char hello_world[] = {"Hello World!\r\n"};
|
||||
|
@@ -16,6 +16,7 @@
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
#include "gd32e23x_usart.h"
|
||||
#include "board_config.h"
|
||||
|
||||
#undef errno
|
||||
extern int errno;
|
||||
@@ -164,7 +165,7 @@ int _execve(char *name, char **argv, char **env)
|
||||
// USART0 printf重定向实现
|
||||
int __io_putchar(int ch) {
|
||||
// 等待发送缓冲区空
|
||||
while (usart_flag_get(USART0, USART_FLAG_TBE) == RESET) {}
|
||||
usart_data_transmit(USART0, (uint8_t)ch);
|
||||
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
|
||||
usart_data_transmit(RS485_PHY, (uint8_t)ch);
|
||||
return ch;
|
||||
}
|
||||
|
37
Src/uart.c
37
Src/uart.c
@@ -3,6 +3,7 @@
|
||||
#include "gd32e23x_rcu.h"
|
||||
#include "gd32e23x_gpio.h"
|
||||
#include "board_config.h"
|
||||
#include "uart_ring_buffer.h"
|
||||
|
||||
|
||||
void rs485_init(void) {
|
||||
@@ -37,7 +38,7 @@ void rs485_init(void) {
|
||||
|
||||
usart_enable(RS485_PHY);
|
||||
|
||||
nvic_irq_enable(USART0_IRQn, 0);
|
||||
nvic_irq_enable(RS485_IRQ, 0);
|
||||
usart_interrupt_enable(RS485_PHY, USART_INT_RBNE);
|
||||
// usart_interrupt_enable(RS485_PHY, USART_INT_IDLE);
|
||||
|
||||
@@ -70,3 +71,37 @@ void rs485_init(void) {
|
||||
|
||||
#endif // RS485_MAX13487
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/* 具体的中断处理函数实现 */
|
||||
/******************************************************************************/
|
||||
|
||||
void usart0_irq_handler(void) {
|
||||
// 处理USART0的接收中断
|
||||
if(usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) {
|
||||
uint8_t data = usart_data_receive(USART0);
|
||||
// 使用原有的环形缓冲区处理逻辑
|
||||
(void)uart_ring_buffer_put(data); // 缓冲满时丢弃,返回值可用于统计
|
||||
}
|
||||
|
||||
// 处理USART0的空闲中断
|
||||
if(usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE)) {
|
||||
usart_interrupt_flag_clear(USART0, USART_INT_FLAG_IDLE);
|
||||
// 在这里添加空闲中断处理逻辑
|
||||
}
|
||||
}
|
||||
|
||||
void usart1_irq_handler(void) {
|
||||
// 处理USART1的接收中断
|
||||
if(usart_interrupt_flag_get(USART1, USART_INT_FLAG_RBNE)) {
|
||||
uint8_t data = usart_data_receive(USART1);
|
||||
// 使用原有的环形缓冲区处理逻辑
|
||||
(void)uart_ring_buffer_put(data); // 缓冲满时丢弃,返回值可用于统计
|
||||
}
|
||||
|
||||
// 处理USART1的空闲中断
|
||||
if(usart_interrupt_flag_get(USART1, USART_INT_FLAG_IDLE)) {
|
||||
usart_interrupt_flag_clear(USART1, USART_INT_FLAG_IDLE);
|
||||
// 在这里添加空闲中断处理逻辑
|
||||
}
|
||||
}
|
||||
|
43
iic_new.md
Normal file
43
iic_new.md
Normal file
@@ -0,0 +1,43 @@
|
||||
主要改进特性:
|
||||
通用读写函数:
|
||||
|
||||
i2c_write():支持任意长度的写操作
|
||||
i2c_read():支持任意长度的读操作
|
||||
兼容性函数:保留原有的16位读写函数以保持向后兼容
|
||||
|
||||
便利函数:提供常用的8位、16位、32位读写函数
|
||||
|
||||
智能长度处理:
|
||||
|
||||
单字节读取:正确处理NACK和STOP时序
|
||||
双字节读取:使用POS=NEXT模式
|
||||
多字节读取:正确处理最后几个字节的ACK/NACK时序
|
||||
错误处理:保持原有的重试机制和错误处理逻辑
|
||||
|
||||
使用示例:
|
||||
|
||||
```
|
||||
uint8_t data[4];
|
||||
i2c_result_t result;
|
||||
|
||||
// 读取1字节
|
||||
result = i2c_read_8bits(0x48, 0x00, &data[0]);
|
||||
|
||||
// 读取2字节
|
||||
result = i2c_read(0x48, 0x01, data, 2);
|
||||
|
||||
// 读取4字节
|
||||
result = i2c_read_32bits(0x48, 0x02, data);
|
||||
|
||||
// 写入3字节
|
||||
uint8_t write_data[3] = {0x11, 0x22, 0x33};
|
||||
result = i2c_write(0x48,
|
||||
```
|
||||
|
||||
主要功能特性
|
||||
支持任意长度读写:从1字节到255字节
|
||||
正确的ACK/NACK处理:根据读取长度智能处理
|
||||
保持兼容性:原有的16位读写函数仍然可用
|
||||
专用显示函数:为显示面板参数读取提供专门的函数
|
||||
完整的错误处理:保持原有的重试和错误恢复机制
|
||||
这样就可以支持您文档中提到的多字节显示面板参数读写操作了
|
Reference in New Issue
Block a user