Template
更改宏定义的开启方式
This commit is contained in:
+14
-11
@@ -1,6 +1,14 @@
|
|||||||
#ifndef BOARD_CONFIG_H
|
#ifndef BOARD_CONFIG_H
|
||||||
#define BOARD_CONFIG_H
|
#define BOARD_CONFIG_H
|
||||||
|
|
||||||
|
#ifndef DISABLE
|
||||||
|
#define DISABLE 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef ENABLE
|
||||||
|
#define ENABLE 1
|
||||||
|
#endif
|
||||||
|
|
||||||
#define GD32E23XF4 0x10
|
#define GD32E23XF4 0x10
|
||||||
#define GD32E23XF6 0x20
|
#define GD32E23XF6 0x20
|
||||||
#define GD32E23XF8 0x40
|
#define GD32E23XF8 0x40
|
||||||
@@ -11,30 +19,25 @@
|
|||||||
|
|
||||||
/* >>>>>>>>>>>>>>>>>>>>[IIC TYPE DEFINE]<<<<<<<<<<<<<<<<<<<< */
|
/* >>>>>>>>>>>>>>>>>>>>[IIC TYPE DEFINE]<<<<<<<<<<<<<<<<<<<< */
|
||||||
|
|
||||||
// #define SOFTWARE_IIC // IIC Type : Software IIC
|
#define SOFTWARE_IIC DISABLE // DISABLE: Hardware IIC; ENABLE: Software IIC
|
||||||
#undef SOFTWARE_IIC // IIC Type : Hardware IIC
|
|
||||||
|
|
||||||
/* >>>>>>>>>>>>>>>>>>>>[DEBUG MODE]<<<<<<<<<<<<<<<<<<<< */
|
/* >>>>>>>>>>>>>>>>>>>>[DEBUG MODE]<<<<<<<<<<<<<<<<<<<< */
|
||||||
|
|
||||||
// #define DEBUG_MODE // Operating Mode : Debug Mode
|
#define DEBUG_MODE DISABLE // DISABLE: Release Mode; ENABLE: Debug Mode
|
||||||
#undef DEBUG_MODE // Operating Mode : Release Mode
|
|
||||||
|
|
||||||
/* >>>>>>>>>>>>>>>>>>>>[COMMAND DEBUG]<<<<<<<<<<<<<<<<<<<< */
|
/* >>>>>>>>>>>>>>>>>>>>[COMMAND DEBUG]<<<<<<<<<<<<<<<<<<<< */
|
||||||
|
|
||||||
// #define COM_DEBUG // Enable Command Debug Information
|
#define COM_DEBUG DISABLE // DISABLE: Command debug off; ENABLE: Command debug on
|
||||||
#undef COM_DEBUG // Disable Command Debug Information
|
|
||||||
|
|
||||||
/* >>>>>>>>>>>>>>>>>>>>[DEBUG ASSERTIONS DEFINE]<<<<<<<<<<<<<<<<<<<< */
|
/* >>>>>>>>>>>>>>>>>>>>[DEBUG ASSERTIONS DEFINE]<<<<<<<<<<<<<<<<<<<< */
|
||||||
|
|
||||||
// #define DEBUG_VERBOSE // Debug Assertions Status : Debug Verbose Information
|
#define DEBUG_VERBOSE DISABLE // DISABLE: Verbose debug off; ENABLE: Verbose debug on
|
||||||
#undef DEBUG_VERBOSE // Debug Assertions Status : No Debug Verbose Information
|
|
||||||
|
|
||||||
/* >>>>>>>>>>>>>>>>>>>>[SEGGER RTT DETECTION]<<<<<<<<<<<<<<<<<<<< */
|
/* >>>>>>>>>>>>>>>>>>>>[SEGGER RTT DETECTION]<<<<<<<<<<<<<<<<<<<< */
|
||||||
|
|
||||||
#define SEGGER_RTT_DETECTION // SEGGER RTT Detection : Enable
|
#define SEGGER_RTT_DETECTION ENABLE // DISABLE: SEGGER RTT off; ENABLE: SEGGER RTT on
|
||||||
// #undef SEGGER_RTT_DETECTION // SEGGER RTT Detection : Disable
|
|
||||||
|
|
||||||
#ifdef SEGGER_RTT_DETECTION
|
#if SEGGER_RTT_DETECTION == ENABLE
|
||||||
#include "SEGGER_RTT.h"
|
#include "SEGGER_RTT.h"
|
||||||
#define RTT_printf(ch, ...) SEGGER_RTT_printf(ch, __VA_ARGS__)
|
#define RTT_printf(ch, ...) SEGGER_RTT_printf(ch, __VA_ARGS__)
|
||||||
#define RTT_WriteString(ch, s) SEGGER_RTT_WriteString(ch, s)
|
#define RTT_WriteString(ch, s) SEGGER_RTT_WriteString(ch, s)
|
||||||
|
|||||||
@@ -86,7 +86,9 @@ i2c_result_t i2c_bus_reset(void);
|
|||||||
\param[out] none
|
\param[out] none
|
||||||
\retval none
|
\retval none
|
||||||
*/
|
*/
|
||||||
|
#if DEBUG_VERBOSE == ENABLE
|
||||||
void i2c_scan(void);
|
void i2c_scan(void);
|
||||||
|
#endif
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\brief write 16-bit data to I2C device
|
\brief write 16-bit data to I2C device
|
||||||
@@ -139,7 +141,7 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8
|
|||||||
*/
|
*/
|
||||||
i2c_result_t i2c_read_raw(uint8_t slave_addr, uint8_t *data, uint8_t length);
|
i2c_result_t i2c_read_raw(uint8_t slave_addr, uint8_t *data, uint8_t length);
|
||||||
|
|
||||||
#ifdef DEBUG_VERBOSE
|
#if DEBUG_VERBOSE == ENABLE
|
||||||
/*!
|
/*!
|
||||||
\brief get status string for debugging
|
\brief get status string for debugging
|
||||||
\param[in] status: i2c_result_t value
|
\param[in] status: i2c_result_t value
|
||||||
|
|||||||
@@ -110,17 +110,17 @@
|
|||||||
|
|
||||||
## 板级配置(`Inc/board_config.h`)
|
## 板级配置(`Inc/board_config.h`)
|
||||||
|
|
||||||
所有功能开关和引脚定义集中在 `Inc/board_config.h`。以下为完整的宏开关说明和推荐使用方式。
|
所有功能开关和引脚定义集中在 `Inc/board_config.h`。功能开关均为单行数值宏:将右侧的 `ENABLE` 或 `DISABLE` 改为另一值即可;其中 `ENABLE` 为 `1`,`DISABLE` 为 `0`。以下为完整的宏开关说明和推荐使用方式。
|
||||||
|
|
||||||
### 功能开关速查表
|
### 功能开关速查表
|
||||||
|
|
||||||
| 宏 | 作用 | 默认值 | Release 建议 |
|
| 宏 | 作用 | 默认值 | Release 建议 |
|
||||||
|---|------|:---:|:---:|
|
|---|------|:---:|:---:|
|
||||||
| `SOFTWARE_IIC` | I2C 实现方式 | ❌ 硬件 | 硬件 |
|
| `SOFTWARE_IIC` | I2C 实现方式 | `DISABLE`(硬件) | `DISABLE` |
|
||||||
| `DEBUG_MODE` | printf 串口输出 | ❌ 关闭 | ❌ |
|
| `DEBUG_MODE` | printf 串口输出 | `DISABLE` | `DISABLE` |
|
||||||
| `COM_DEBUG` | 命令帧调试打印 | ❌ 关闭 | ❌ |
|
| `COM_DEBUG` | 命令帧调试打印 | `DISABLE` | `DISABLE` |
|
||||||
| `DEBUG_VERBOSE` | 详细调试信息 | ❌ 关闭 | ❌ |
|
| `DEBUG_VERBOSE` | 详细调试信息 | `DISABLE` | `DISABLE` |
|
||||||
| `SEGGER_RTT_DETECTION` | SEGGER RTT 支持 | ✅ 开启 | ❌ |
|
| `SEGGER_RTT_DETECTION` | SEGGER RTT 支持 | `ENABLE` | `DISABLE` |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -128,12 +128,10 @@
|
|||||||
|
|
||||||
选择 I2C 使用硬件外设还是软件 GPIO 模拟。
|
选择 I2C 使用硬件外设还是软件 GPIO 模拟。
|
||||||
|
|
||||||
```c
|
> 当前工程尚未实现 `SOFTWARE_IIC` 的条件编译驱动选择;该宏已迁移为数值配置,但改值不会在此版本切换 I2C 实现。
|
||||||
// 使用软件 I2C(GPIO 模拟)
|
|
||||||
#define SOFTWARE_IIC
|
|
||||||
|
|
||||||
// 使用硬件 I2C(外设 I2C0) —— 默认
|
```c
|
||||||
#undef SOFTWARE_IIC
|
#define SOFTWARE_IIC DISABLE // DISABLE: 硬件 I2C(默认);ENABLE: 软件 I2C(GPIO 模拟)
|
||||||
```
|
```
|
||||||
|
|
||||||
| 选项 | 优点 | 缺点 |
|
| 选项 | 优点 | 缺点 |
|
||||||
@@ -150,11 +148,7 @@
|
|||||||
开启后 USART0 输出 printf 调试信息。**Release 固件必须关闭。**
|
开启后 USART0 输出 printf 调试信息。**Release 固件必须关闭。**
|
||||||
|
|
||||||
```c
|
```c
|
||||||
// 开启调试输出
|
#define DEBUG_MODE DISABLE // DISABLE: 关闭(默认);ENABLE: 开启调试输出
|
||||||
#define DEBUG_MODE
|
|
||||||
|
|
||||||
// 关闭 —— 默认
|
|
||||||
#undef DEBUG_MODE
|
|
||||||
```
|
```
|
||||||
|
|
||||||
**影响范围:**
|
**影响范围:**
|
||||||
@@ -169,11 +163,7 @@
|
|||||||
开启后串口命令解析过程打印每帧的详细内容(地址、长度、数据、校验)。**仅调试通信协议时开启。**
|
开启后串口命令解析过程打印每帧的详细内容(地址、长度、数据、校验)。**仅调试通信协议时开启。**
|
||||||
|
|
||||||
```c
|
```c
|
||||||
// 开启命令帧调试
|
#define COM_DEBUG DISABLE // DISABLE: 关闭(默认);ENABLE: 开启命令帧调试
|
||||||
#define COM_DEBUG
|
|
||||||
|
|
||||||
// 关闭 —— 默认
|
|
||||||
#undef COM_DEBUG
|
|
||||||
```
|
```
|
||||||
|
|
||||||
**依赖:** 需要先开启 `DEBUG_MODE`,否则输出无法外发。
|
**依赖:** 需要先开启 `DEBUG_MODE`,否则输出无法外发。
|
||||||
@@ -192,11 +182,7 @@
|
|||||||
在 `DEBUG_MODE` 基础上输出更底层的信息,如 I2C 总线扫描结果、MCU 型号识别等。
|
在 `DEBUG_MODE` 基础上输出更底层的信息,如 I2C 总线扫描结果、MCU 型号识别等。
|
||||||
|
|
||||||
```c
|
```c
|
||||||
// 开启详细调试
|
#define DEBUG_VERBOSE DISABLE // DISABLE: 关闭(默认);ENABLE: 开启详细调试
|
||||||
#define DEBUG_VERBOSE
|
|
||||||
|
|
||||||
// 关闭 —— 默认
|
|
||||||
#undef DEBUG_VERBOSE
|
|
||||||
```
|
```
|
||||||
|
|
||||||
**额外输出:**
|
**额外输出:**
|
||||||
@@ -213,11 +199,7 @@
|
|||||||
RTT(Real-Time Transfer)是 SEGGER 的调试通道技术,通过 SWD 接口传输数据,不占用串口引脚,速度远超 UART。
|
RTT(Real-Time Transfer)是 SEGGER 的调试通道技术,通过 SWD 接口传输数据,不占用串口引脚,速度远超 UART。
|
||||||
|
|
||||||
```c
|
```c
|
||||||
// 启用 RTT —— 默认
|
#define SEGGER_RTT_DETECTION ENABLE // DISABLE: 禁用 RTT;ENABLE: 启用 RTT(默认)
|
||||||
#define SEGGER_RTT_DETECTION
|
|
||||||
|
|
||||||
// 禁用
|
|
||||||
#undef SEGGER_RTT_DETECTION
|
|
||||||
```
|
```
|
||||||
|
|
||||||
**启用时:**
|
**启用时:**
|
||||||
@@ -226,8 +208,9 @@ RTT(Real-Time Transfer)是 SEGGER 的调试通道技术,通过 SWD 接口
|
|||||||
- 可用 J-Link RTT Viewer 或 VSCode + cortex-debug 查看实时日志
|
- 可用 J-Link RTT Viewer 或 VSCode + cortex-debug 查看实时日志
|
||||||
|
|
||||||
**禁用时:**
|
**禁用时:**
|
||||||
- RTT 宏展开为空操作,零开销
|
- 应用层 RTT 头文件引用与调用代码均在预处理阶段排除
|
||||||
- SEGGER_RTT 模块不参与编译
|
- RTT 宏展开为空操作;应用目标不生成 RTT 调用代码
|
||||||
|
- 为保持现有 CMake SDK 加载方式,`SDK/SEGGER_RTT/` 仍会参与构建;静态库中未被引用的对象不会被链接器提取
|
||||||
|
|
||||||
> **Release 建议关闭** — RTT 依赖调试器连接,量产固件中无意义且占用 ROM。
|
> **Release 建议关闭** — RTT 依赖调试器连接,量产固件中无意义且占用 ROM。
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -101,7 +101,7 @@ void system_software_reset(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Debug output control */
|
/* Debug output control */
|
||||||
#ifdef COM_DEBUG
|
#if COM_DEBUG == ENABLE
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#define COMMAND_DEBUG(fmt, ...) printf("[COMMAND] " fmt "\n", ##__VA_ARGS__)
|
#define COMMAND_DEBUG(fmt, ...) printf("[COMMAND] " fmt "\n", ##__VA_ARGS__)
|
||||||
#else
|
#else
|
||||||
@@ -565,7 +565,7 @@ void command_process(void) {
|
|||||||
// 到帧尾,进行各项校验
|
// 到帧尾,进行各项校验
|
||||||
bool verification_status = true;
|
bool verification_status = true;
|
||||||
|
|
||||||
#ifdef DEBUG_VERBOSE
|
#if DEBUG_VERBOSE == ENABLE
|
||||||
if (cmd_buf[0] != PROTOCOL_PACKAGE_HEADER) {
|
if (cmd_buf[0] != PROTOCOL_PACKAGE_HEADER) {
|
||||||
send_response(RESP_TYPE_HEADER_ERR, s_report_status_err, sizeof(s_report_status_err));
|
send_response(RESP_TYPE_HEADER_ERR, s_report_status_err, sizeof(s_report_status_err));
|
||||||
verification_status = false;
|
verification_status = false;
|
||||||
|
|||||||
+1
-1
@@ -108,7 +108,7 @@ void USART0_IRQHandler(void) {
|
|||||||
g_usart_config.irq_handler(); // 通过函数指针调用对应的处理函数
|
g_usart_config.irq_handler(); // 通过函数指针调用对应的处理函数
|
||||||
}
|
}
|
||||||
// 作为调试口(第二串口)时也接收数据,便于模拟上位机命令
|
// 作为调试口(第二串口)时也接收数据,便于模拟上位机命令
|
||||||
#ifdef DEBUG_MODE
|
#if DEBUG_MODE == ENABLE
|
||||||
else {
|
else {
|
||||||
usart0_irq_handler();
|
usart0_irq_handler();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ i2c_result_t i2c_bus_reset(void) {
|
|||||||
i2c_disable(I2C0);
|
i2c_disable(I2C0);
|
||||||
i2c_deinit(I2C0);
|
i2c_deinit(I2C0);
|
||||||
|
|
||||||
#ifdef DEBUG_VERBOSE
|
#if DEBUG_VERBOSE == ENABLE
|
||||||
printf("I2C bus reset\r\n");
|
printf("I2C bus reset\r\n");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -145,6 +145,7 @@ i2c_result_t i2c_bus_reset(void) {
|
|||||||
* 如果在某个地址上发现了设备,则会打印出该设备的地址。
|
* 如果在某个地址上发现了设备,则会打印出该设备的地址。
|
||||||
* 最后会打印出找到的设备总数。
|
* 最后会打印出找到的设备总数。
|
||||||
*/
|
*/
|
||||||
|
#if DEBUG_VERBOSE == ENABLE
|
||||||
void i2c_scan(void) {
|
void i2c_scan(void) {
|
||||||
uint32_t timeout;
|
uint32_t timeout;
|
||||||
uint8_t address;
|
uint8_t address;
|
||||||
@@ -249,6 +250,7 @@ void i2c_scan(void) {
|
|||||||
while (usart_flag_get(UART_PHY, USART_FLAG_TC) == RESET) {}
|
while (usart_flag_get(UART_PHY, USART_FLAG_TC) == RESET) {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 内部辅助函数:等待指定的I2C标志位被设置,带有超时。
|
* @brief 内部辅助函数:等待指定的I2C标志位被设置,带有超时。
|
||||||
@@ -260,7 +262,7 @@ static i2c_result_t _i2c_wait_flag_timeout(uint32_t flag)
|
|||||||
uint16_t timeout = 0;
|
uint16_t timeout = 0;
|
||||||
while(!i2c_flag_get(I2C0, flag)){
|
while(!i2c_flag_get(I2C0, flag)){
|
||||||
if(timeout++ > I2C_TIME_OUT){
|
if(timeout++ > I2C_TIME_OUT){
|
||||||
#ifdef DEBUG_VERBOSE
|
#if DEBUG_VERBOSE == ENABLE
|
||||||
const char* fname = "UNKNOWN";
|
const char* fname = "UNKNOWN";
|
||||||
switch (flag) {
|
switch (flag) {
|
||||||
case I2C_FLAG_SBSEND: fname = "SBSEND"; break;
|
case I2C_FLAG_SBSEND: fname = "SBSEND"; break;
|
||||||
@@ -359,7 +361,7 @@ i2c_result_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data
|
|||||||
} else {
|
} else {
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
||||||
timeout =0;
|
timeout =0;
|
||||||
#ifdef DEBUG_VERBOSE
|
#if DEBUG_VERBOSE == ENABLE
|
||||||
printf("IIC write failed for Error Slave Address. \n");
|
printf("IIC write failed for Error Slave Address. \n");
|
||||||
#endif
|
#endif
|
||||||
return I2C_RESULT_NACK;
|
return I2C_RESULT_NACK;
|
||||||
@@ -438,7 +440,7 @@ i2c_result_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data
|
|||||||
retry_count ++;
|
retry_count ++;
|
||||||
if (retry_count >= I2C_MAX_RETRY)
|
if (retry_count >= I2C_MAX_RETRY)
|
||||||
{
|
{
|
||||||
#ifdef DEBUG_VERBOSE
|
#if DEBUG_VERBOSE == ENABLE
|
||||||
printf("IIC write failed after %d retries\n", I2C_MAX_RETRY);
|
printf("IIC write failed after %d retries\n", I2C_MAX_RETRY);
|
||||||
#endif
|
#endif
|
||||||
return I2C_RESULT_ERROR;
|
return I2C_RESULT_ERROR;
|
||||||
@@ -612,7 +614,7 @@ i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data
|
|||||||
|
|
||||||
retry_count++;
|
retry_count++;
|
||||||
if (retry_count >= I2C_MAX_RETRY) {
|
if (retry_count >= I2C_MAX_RETRY) {
|
||||||
#ifdef DEBUG_VERBOSE
|
#if DEBUG_VERBOSE == ENABLE
|
||||||
printf("IIC read failed after %d retries\n", I2C_MAX_RETRY);
|
printf("IIC read failed after %d retries\n", I2C_MAX_RETRY);
|
||||||
#endif
|
#endif
|
||||||
return I2C_RESULT_ERROR;
|
return I2C_RESULT_ERROR;
|
||||||
@@ -652,7 +654,7 @@ i2c_result_t i2c_write(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint
|
|||||||
|
|
||||||
/* parameter validation */
|
/* parameter validation */
|
||||||
if (data == NULL || slave_addr > 0x7F || length == 0) {
|
if (data == NULL || slave_addr > 0x7F || length == 0) {
|
||||||
#ifdef DEBUG_VERBOSE
|
#if DEBUG_VERBOSE == ENABLE
|
||||||
printf("I2C read invalid param: slave=0x%02X, len=%u, data=%p\r\n", slave_addr, length, data);
|
printf("I2C read invalid param: slave=0x%02X, len=%u, data=%p\r\n", slave_addr, length, data);
|
||||||
#endif
|
#endif
|
||||||
return I2C_RESULT_INVALID_PARAM;
|
return I2C_RESULT_INVALID_PARAM;
|
||||||
@@ -712,7 +714,7 @@ i2c_result_t i2c_write(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint
|
|||||||
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
||||||
i2c_stop_on_bus(I2C0);
|
i2c_stop_on_bus(I2C0);
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
#ifdef DEBUG_VERBOSE
|
#if DEBUG_VERBOSE == ENABLE
|
||||||
printf("I2C write failed for Error Slave Address. \n");
|
printf("I2C write failed for Error Slave Address. \n");
|
||||||
#endif
|
#endif
|
||||||
return I2C_RESULT_NACK;
|
return I2C_RESULT_NACK;
|
||||||
@@ -787,7 +789,7 @@ i2c_result_t i2c_write(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint
|
|||||||
|
|
||||||
retry_count++;
|
retry_count++;
|
||||||
if (retry_count >= I2C_MAX_RETRY) {
|
if (retry_count >= I2C_MAX_RETRY) {
|
||||||
#ifdef DEBUG_VERBOSE
|
#if DEBUG_VERBOSE == ENABLE
|
||||||
printf("IIC write failed after %d retries\n", I2C_MAX_RETRY);
|
printf("IIC write failed after %d retries\n", I2C_MAX_RETRY);
|
||||||
#endif
|
#endif
|
||||||
return I2C_RESULT_ERROR;
|
return I2C_RESULT_ERROR;
|
||||||
@@ -832,7 +834,7 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8
|
|||||||
/* enable acknowledge */
|
/* enable acknowledge */
|
||||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||||
|
|
||||||
#ifdef DEBUG_VERBOSE
|
#if DEBUG_VERBOSE == ENABLE
|
||||||
printf("I2C read start: slave=0x%02X reg=0x%02X len=%u\r\n", slave_addr, reg_addr, length);
|
printf("I2C read start: slave=0x%02X reg=0x%02X len=%u\r\n", slave_addr, reg_addr, length);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -884,7 +886,7 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* debug: show address ack and error flags */
|
/* debug: show address ack and error flags */
|
||||||
#ifdef DEBUG_VERBOSE
|
#if DEBUG_VERBOSE == ENABLE
|
||||||
printf("I2C CLEAR_ADDRESS: write_phase=%d ADDSEND=%d AERR=%d\r\n",
|
printf("I2C CLEAR_ADDRESS: write_phase=%d ADDSEND=%d AERR=%d\r\n",
|
||||||
(int)write_phase,
|
(int)write_phase,
|
||||||
(int)i2c_flag_get(I2C0, I2C_FLAG_ADDSEND),
|
(int)i2c_flag_get(I2C0, I2C_FLAG_ADDSEND),
|
||||||
@@ -902,19 +904,19 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8
|
|||||||
- length > 2: keep ACK enabled and clear ADDR; we'll handle N-2/BTF sequence later */
|
- length > 2: keep ACK enabled and clear ADDR; we'll handle N-2/BTF sequence later */
|
||||||
if (length == 1) {
|
if (length == 1) {
|
||||||
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
||||||
#ifdef DEBUG_VERBOSE
|
#if DEBUG_VERBOSE == ENABLE
|
||||||
printf("I2C READ phase: length=1, disabling ACK before clearing ADDSEND\r\n");
|
printf("I2C READ phase: length=1, disabling ACK before clearing ADDSEND\r\n");
|
||||||
#endif
|
#endif
|
||||||
} else if (length == 2) {
|
} else if (length == 2) {
|
||||||
i2c_ackpos_config(I2C0, I2C_ACKPOS_NEXT);
|
i2c_ackpos_config(I2C0, I2C_ACKPOS_NEXT);
|
||||||
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
||||||
#ifdef DEBUG_VERBOSE
|
#if DEBUG_VERBOSE == ENABLE
|
||||||
printf("I2C READ phase: length=2, set ACKPOS_NEXT and disabling ACK before clearing ADDSEND\r\n");
|
printf("I2C READ phase: length=2, set ACKPOS_NEXT and disabling ACK before clearing ADDSEND\r\n");
|
||||||
#endif
|
#endif
|
||||||
} else {
|
} else {
|
||||||
/* length > 2: keep ACK enabled so slave will clock out data */
|
/* length > 2: keep ACK enabled so slave will clock out data */
|
||||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||||
#ifdef DEBUG_VERBOSE
|
#if DEBUG_VERBOSE == ENABLE
|
||||||
printf("I2C READ phase: length=%u (>2), keeping ACK enabled\r\n", length);
|
printf("I2C READ phase: length=%u (>2), keeping ACK enabled\r\n", length);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -947,7 +949,7 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef DEBUG_VERBOSE
|
#if DEBUG_VERBOSE == ENABLE
|
||||||
printf("I2C RESTART: after BTC, delay 5ms and issuing repeated START\r\n");
|
printf("I2C RESTART: after BTC, delay 5ms and issuing repeated START\r\n");
|
||||||
#endif
|
#endif
|
||||||
/* small delay to allow slave device to prepare multi-byte data before repeated start */
|
/* small delay to allow slave device to prepare multi-byte data before repeated start */
|
||||||
@@ -970,13 +972,13 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8
|
|||||||
write_phase = false;
|
write_phase = false;
|
||||||
state = I2C_STATE_CLEAR_ADDRESS;
|
state = I2C_STATE_CLEAR_ADDRESS;
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
#ifdef DEBUG_VERBOSE
|
#if DEBUG_VERBOSE == ENABLE
|
||||||
printf("I2C addressing sent for read (slave=0x%02X)\r\n", slave_addr);
|
printf("I2C addressing sent for read (slave=0x%02X)\r\n", slave_addr);
|
||||||
#endif
|
#endif
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case I2C_STATE_RECEIVE_DATA:
|
case I2C_STATE_RECEIVE_DATA:
|
||||||
#ifdef DEBUG_VERBOSE
|
#if DEBUG_VERBOSE == ENABLE
|
||||||
printf("I2C RECEIVE_DATA: expecting %u bytes\r\n", length);
|
printf("I2C RECEIVE_DATA: expecting %u bytes\r\n", length);
|
||||||
#endif
|
#endif
|
||||||
if (length == 1) {
|
if (length == 1) {
|
||||||
@@ -1058,7 +1060,7 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8
|
|||||||
|
|
||||||
case I2C_STATE_ERROR:
|
case I2C_STATE_ERROR:
|
||||||
/* I2C bus error, try to reset the bus and retry */
|
/* I2C bus error, try to reset the bus and retry */
|
||||||
#ifdef DEBUG_VERBOSE
|
#if DEBUG_VERBOSE == ENABLE
|
||||||
printf("I2C_STATE_ERROR: resetting bus and retrying (retry_count=%u)\r\n", retry_count);
|
printf("I2C_STATE_ERROR: resetting bus and retrying (retry_count=%u)\r\n", retry_count);
|
||||||
#endif
|
#endif
|
||||||
i2c_bus_reset();
|
i2c_bus_reset();
|
||||||
@@ -1069,7 +1071,7 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8
|
|||||||
|
|
||||||
retry_count++;
|
retry_count++;
|
||||||
if (retry_count >= I2C_MAX_RETRY) {
|
if (retry_count >= I2C_MAX_RETRY) {
|
||||||
#ifdef DEBUG_VERBOSE
|
#if DEBUG_VERBOSE == ENABLE
|
||||||
/* Print diagnostic flags to help root-cause analysis */
|
/* Print diagnostic flags to help root-cause analysis */
|
||||||
printf("I2C read final failure after %d retries, last_state=%d\r\n", I2C_MAX_RETRY, state);
|
printf("I2C read final failure after %d retries, last_state=%d\r\n", I2C_MAX_RETRY, state);
|
||||||
printf(" FLAGS: AERR=%d BERR=%d LOSTARB=%d I2CBSY=%d\r\n",
|
printf(" FLAGS: AERR=%d BERR=%d LOSTARB=%d I2CBSY=%d\r\n",
|
||||||
@@ -1078,7 +1080,7 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8
|
|||||||
(int)i2c_flag_get(I2C0, I2C_FLAG_LOSTARB),
|
(int)i2c_flag_get(I2C0, I2C_FLAG_LOSTARB),
|
||||||
(int)i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
|
(int)i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
|
||||||
#endif
|
#endif
|
||||||
#ifdef DEBUG_VERBOSE
|
#if DEBUG_VERBOSE == ENABLE
|
||||||
printf("I2C read failed after %d retries\n", I2C_MAX_RETRY);
|
printf("I2C read failed after %d retries\n", I2C_MAX_RETRY);
|
||||||
#endif
|
#endif
|
||||||
return I2C_RESULT_ERROR;
|
return I2C_RESULT_ERROR;
|
||||||
@@ -1099,7 +1101,7 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* timeout path: provide debug info */
|
/* timeout path: provide debug info */
|
||||||
#ifdef DEBUG_VERBOSE
|
#if DEBUG_VERBOSE == ENABLE
|
||||||
printf("I2C read timeout (end state). slave=0x%02X, len=%u\r\n", slave_addr, length);
|
printf("I2C read timeout (end state). slave=0x%02X, len=%u\r\n", slave_addr, length);
|
||||||
printf(" FLAGS: AERR=%d BERR=%d LOSTARB=%d I2CBSY=%d\r\n",
|
printf(" FLAGS: AERR=%d BERR=%d LOSTARB=%d I2CBSY=%d\r\n",
|
||||||
(int)i2c_flag_get(I2C0, I2C_FLAG_AERR),
|
(int)i2c_flag_get(I2C0, I2C_FLAG_AERR),
|
||||||
@@ -1267,7 +1269,7 @@ i2c_result_t i2c_read_raw(uint8_t slave_addr, uint8_t *data, uint8_t length)
|
|||||||
return I2C_RESULT_TIMEOUT;
|
return I2C_RESULT_TIMEOUT;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef DEBUG_VERBOSE
|
#if DEBUG_VERBOSE == ENABLE
|
||||||
/*!
|
/*!
|
||||||
\brief get status string for debugging
|
\brief get status string for debugging
|
||||||
\param[in] status: i2c_status_t value
|
\param[in] status: i2c_status_t value
|
||||||
|
|||||||
+3
-3
@@ -62,15 +62,15 @@ int main(void)
|
|||||||
|
|
||||||
i2c_config();
|
i2c_config();
|
||||||
|
|
||||||
#ifdef DEBUG_MODE
|
#if DEBUG_MODE == ENABLE
|
||||||
printf("Hello World!\r\n");
|
printf("Hello World!\r\n");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef SEGGER_RTT_DETECTION
|
#if SEGGER_RTT_DETECTION == ENABLE
|
||||||
RTT_printf(0, "Hello World!\r\n");
|
RTT_printf(0, "Hello World!\r\n");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef DEBUG_VERBOSE
|
#if DEBUG_VERBOSE == ENABLE
|
||||||
i2c_scan();
|
i2c_scan();
|
||||||
|
|
||||||
i2c_bus_reset();
|
i2c_bus_reset();
|
||||||
|
|||||||
Reference in New Issue
Block a user