generated from hulk/gd32e23x_template_cmake_vscode
refactor: sync from template - ENABLE/DISABLE macro pattern
- board_config.h: replace #define/#undef with ENABLE/DISABLE constants - All debug switches use ENABLE/DISABLE instead of define/undef - Replace #ifdef with #if MACRO == ENABLE across all sources - Sync updated README documentation
This commit is contained in:
@@ -79,7 +79,7 @@ i2c_result_t i2c_bus_reset(void) {
|
||||
i2c_disable(I2C0);
|
||||
i2c_deinit(I2C0);
|
||||
|
||||
#ifdef DEBUG_VERBOSE
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
printf("I2C bus reset\r\n");
|
||||
#endif
|
||||
|
||||
@@ -145,6 +145,7 @@ i2c_result_t i2c_bus_reset(void) {
|
||||
* 如果在某个地址上发现了设备,则会打印出该设备的地址。
|
||||
* 最后会打印出找到的设备总数。
|
||||
*/
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
void i2c_scan(void) {
|
||||
uint32_t timeout;
|
||||
uint8_t address;
|
||||
@@ -249,6 +250,7 @@ void i2c_scan(void) {
|
||||
while (usart_flag_get(UART_PHY, USART_FLAG_TC) == RESET) {}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief 内部辅助函数:等待指定的I2C标志位被设置,带有超时。
|
||||
@@ -260,7 +262,7 @@ static i2c_result_t _i2c_wait_flag_timeout(uint32_t flag)
|
||||
uint16_t timeout = 0;
|
||||
while(!i2c_flag_get(I2C0, flag)){
|
||||
if(timeout++ > I2C_TIME_OUT){
|
||||
#ifdef DEBUG_VERBOSE
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
const char* fname = "UNKNOWN";
|
||||
switch (flag) {
|
||||
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 {
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
||||
timeout =0;
|
||||
#ifdef DEBUG_VERBOSE
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
printf("IIC write failed for Error Slave Address. \n");
|
||||
#endif
|
||||
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 ++;
|
||||
if (retry_count >= I2C_MAX_RETRY)
|
||||
{
|
||||
#ifdef DEBUG_VERBOSE
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
printf("IIC write failed after %d retries\n", I2C_MAX_RETRY);
|
||||
#endif
|
||||
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++;
|
||||
if (retry_count >= I2C_MAX_RETRY) {
|
||||
#ifdef DEBUG_VERBOSE
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
printf("IIC read failed after %d retries\n", I2C_MAX_RETRY);
|
||||
#endif
|
||||
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 */
|
||||
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);
|
||||
#endif
|
||||
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_stop_on_bus(I2C0);
|
||||
timeout = 0;
|
||||
#ifdef DEBUG_VERBOSE
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
printf("I2C write failed for Error Slave Address. \n");
|
||||
#endif
|
||||
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++;
|
||||
if (retry_count >= I2C_MAX_RETRY) {
|
||||
#ifdef DEBUG_VERBOSE
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
printf("IIC write failed after %d retries\n", I2C_MAX_RETRY);
|
||||
#endif
|
||||
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 */
|
||||
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);
|
||||
#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 */
|
||||
#ifdef DEBUG_VERBOSE
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
printf("I2C CLEAR_ADDRESS: write_phase=%d ADDSEND=%d AERR=%d\r\n",
|
||||
(int)write_phase,
|
||||
(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 */
|
||||
if (length == 1) {
|
||||
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");
|
||||
#endif
|
||||
} else if (length == 2) {
|
||||
i2c_ackpos_config(I2C0, I2C_ACKPOS_NEXT);
|
||||
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");
|
||||
#endif
|
||||
} else {
|
||||
/* length > 2: keep ACK enabled so slave will clock out data */
|
||||
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);
|
||||
#endif
|
||||
}
|
||||
@@ -947,7 +949,7 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8
|
||||
break;
|
||||
}
|
||||
|
||||
#ifdef DEBUG_VERBOSE
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
printf("I2C RESTART: after BTC, delay 5ms and issuing repeated START\r\n");
|
||||
#endif
|
||||
/* 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;
|
||||
state = I2C_STATE_CLEAR_ADDRESS;
|
||||
timeout = 0;
|
||||
#ifdef DEBUG_VERBOSE
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
printf("I2C addressing sent for read (slave=0x%02X)\r\n", slave_addr);
|
||||
#endif
|
||||
break;
|
||||
|
||||
case I2C_STATE_RECEIVE_DATA:
|
||||
#ifdef DEBUG_VERBOSE
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
printf("I2C RECEIVE_DATA: expecting %u bytes\r\n", length);
|
||||
#endif
|
||||
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:
|
||||
/* 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);
|
||||
#endif
|
||||
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++;
|
||||
if (retry_count >= I2C_MAX_RETRY) {
|
||||
#ifdef DEBUG_VERBOSE
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
/* 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(" 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_I2CBSY));
|
||||
#endif
|
||||
#ifdef DEBUG_VERBOSE
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
printf("I2C read failed after %d retries\n", I2C_MAX_RETRY);
|
||||
#endif
|
||||
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 */
|
||||
#ifdef DEBUG_VERBOSE
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
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",
|
||||
(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;
|
||||
}
|
||||
|
||||
#ifdef DEBUG_VERBOSE
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
/*!
|
||||
\brief get status string for debugging
|
||||
\param[in] status: i2c_status_t value
|
||||
|
||||
Reference in New Issue
Block a user