Template
refactor: 同步 actuator 项目的 RS485 支持和 ENABLE→CFG_ENABLE 重命名
- board_config.h: ENABLE/DISABLE 重命名为 CFG_ENABLE/CFG_DISABLE - board_config.h: 新增 RS485_MODE 配置块(默认 CFG_DISABLE) - uart.c: 新增 RS485 DE 引脚 AF 配置和硬件 485 模式配置 - 所有源文件: == ENABLE → == CFG_ENABLE(共 34 处) - 兼容: RS485 默认关闭,零影响现有项目
This commit is contained in:
+20
-10
@@ -1,12 +1,12 @@
|
||||
#ifndef BOARD_CONFIG_H
|
||||
#define BOARD_CONFIG_H
|
||||
|
||||
#ifndef DISABLE
|
||||
#define DISABLE 0
|
||||
#ifndef CFG_DISABLE
|
||||
#define CFG_DISABLE 0
|
||||
#endif
|
||||
|
||||
#ifndef ENABLE
|
||||
#define ENABLE 1
|
||||
#ifndef CFG_ENABLE
|
||||
#define CFG_ENABLE 1
|
||||
#endif
|
||||
|
||||
#define GD32E23XF4 0x10
|
||||
@@ -19,25 +19,25 @@
|
||||
|
||||
/* >>>>>>>>>>>>>>>>>>>>[IIC TYPE DEFINE]<<<<<<<<<<<<<<<<<<<< */
|
||||
|
||||
#define SOFTWARE_IIC DISABLE // DISABLE: Hardware IIC; ENABLE: Software IIC
|
||||
#define SOFTWARE_IIC CFG_DISABLE // CFG_DISABLE: Hardware IIC; CFG_ENABLE: Software IIC
|
||||
|
||||
/* >>>>>>>>>>>>>>>>>>>>[DEBUG MODE]<<<<<<<<<<<<<<<<<<<< */
|
||||
|
||||
#define DEBUG_MODE DISABLE // DISABLE: Release Mode; ENABLE: Debug Mode
|
||||
#define DEBUG_MODE CFG_DISABLE // CFG_DISABLE: Release Mode; CFG_ENABLE: Debug Mode
|
||||
|
||||
/* >>>>>>>>>>>>>>>>>>>>[COMMAND DEBUG]<<<<<<<<<<<<<<<<<<<< */
|
||||
|
||||
#define COM_DEBUG DISABLE // DISABLE: Command debug off; ENABLE: Command debug on
|
||||
#define COM_DEBUG CFG_DISABLE // CFG_DISABLE: Command debug off; CFG_ENABLE: Command debug on
|
||||
|
||||
/* >>>>>>>>>>>>>>>>>>>>[DEBUG ASSERTIONS DEFINE]<<<<<<<<<<<<<<<<<<<< */
|
||||
|
||||
#define DEBUG_VERBOSE DISABLE // DISABLE: Verbose debug off; ENABLE: Verbose debug on
|
||||
#define DEBUG_VERBOSE CFG_DISABLE // CFG_DISABLE: Verbose debug off; CFG_ENABLE: Verbose debug on
|
||||
|
||||
/* >>>>>>>>>>>>>>>>>>>>[SEGGER RTT DETECTION]<<<<<<<<<<<<<<<<<<<< */
|
||||
|
||||
#define SEGGER_RTT_DETECTION ENABLE // DISABLE: SEGGER RTT off; ENABLE: SEGGER RTT on
|
||||
#define SEGGER_RTT_DETECTION CFG_ENABLE // CFG_DISABLE: SEGGER RTT off; CFG_ENABLE: SEGGER RTT on
|
||||
|
||||
#if SEGGER_RTT_DETECTION == ENABLE
|
||||
#if SEGGER_RTT_DETECTION == CFG_ENABLE
|
||||
#include "SEGGER_RTT.h"
|
||||
#define RTT_printf(ch, ...) SEGGER_RTT_printf(ch, __VA_ARGS__)
|
||||
#define RTT_WriteString(ch, s) SEGGER_RTT_WriteString(ch, s)
|
||||
@@ -48,6 +48,16 @@
|
||||
#define RTT_PutChar(ch, c)
|
||||
#endif
|
||||
|
||||
/* >>>>>>>>>>>>>>>>>>>>[RS485 MODE]<<<<<<<<<<<<<<<<<<<< */
|
||||
|
||||
#define RS485_MODE CFG_DISABLE // CFG_DISABLE: RS485 off; CFG_ENABLE: RS485 on
|
||||
|
||||
#if RS485_MODE == CFG_ENABLE
|
||||
#define RS485_DE_RCU RCU_GPIOA
|
||||
#define RS485_DE_PORT GPIOA
|
||||
#define RS485_DE_PIN GPIO_PIN_1 // 根据实际硬件修改 DE 引脚
|
||||
#endif
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/* Dynamic USART Configuration Structure */
|
||||
|
||||
@@ -86,7 +86,7 @@ i2c_result_t i2c_bus_reset(void);
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
#if DEBUG_VERBOSE == CFG_ENABLE
|
||||
void i2c_scan(void);
|
||||
#endif
|
||||
|
||||
@@ -141,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);
|
||||
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
#if DEBUG_VERBOSE == CFG_ENABLE
|
||||
/*!
|
||||
\brief get status string for debugging
|
||||
\param[in] status: i2c_result_t value
|
||||
|
||||
+2
-2
@@ -101,7 +101,7 @@ void system_software_reset(void)
|
||||
}
|
||||
|
||||
/* Debug output control */
|
||||
#if COM_DEBUG == ENABLE
|
||||
#if COM_DEBUG == CFG_ENABLE
|
||||
#include <stdio.h>
|
||||
#define COMMAND_DEBUG(fmt, ...) printf("[COMMAND] " fmt "\n", ##__VA_ARGS__)
|
||||
#else
|
||||
@@ -565,7 +565,7 @@ void command_process(void) {
|
||||
// 到帧尾,进行各项校验
|
||||
bool verification_status = true;
|
||||
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
#if DEBUG_VERBOSE == CFG_ENABLE
|
||||
if (cmd_buf[0] != PROTOCOL_PACKAGE_HEADER) {
|
||||
send_response(RESP_TYPE_HEADER_ERR, s_report_status_err, sizeof(s_report_status_err));
|
||||
verification_status = false;
|
||||
|
||||
+1
-1
@@ -108,7 +108,7 @@ void USART0_IRQHandler(void) {
|
||||
g_usart_config.irq_handler(); // 通过函数指针调用对应的处理函数
|
||||
}
|
||||
// 作为调试口(第二串口)时也接收数据,便于模拟上位机命令
|
||||
#if DEBUG_MODE == ENABLE
|
||||
#if DEBUG_MODE == CFG_ENABLE
|
||||
else {
|
||||
usart0_irq_handler();
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ i2c_result_t i2c_bus_reset(void) {
|
||||
i2c_disable(I2C0);
|
||||
i2c_deinit(I2C0);
|
||||
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
#if DEBUG_VERBOSE == CFG_ENABLE
|
||||
printf("I2C bus reset\r\n");
|
||||
#endif
|
||||
|
||||
@@ -145,7 +145,7 @@ i2c_result_t i2c_bus_reset(void) {
|
||||
* 如果在某个地址上发现了设备,则会打印出该设备的地址。
|
||||
* 最后会打印出找到的设备总数。
|
||||
*/
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
#if DEBUG_VERBOSE == CFG_ENABLE
|
||||
void i2c_scan(void) {
|
||||
uint32_t timeout;
|
||||
uint8_t address;
|
||||
@@ -262,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){
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
#if DEBUG_VERBOSE == CFG_ENABLE
|
||||
const char* fname = "UNKNOWN";
|
||||
switch (flag) {
|
||||
case I2C_FLAG_SBSEND: fname = "SBSEND"; break;
|
||||
@@ -361,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;
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
#if DEBUG_VERBOSE == CFG_ENABLE
|
||||
printf("IIC write failed for Error Slave Address. \n");
|
||||
#endif
|
||||
return I2C_RESULT_NACK;
|
||||
@@ -440,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)
|
||||
{
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
#if DEBUG_VERBOSE == CFG_ENABLE
|
||||
printf("IIC write failed after %d retries\n", I2C_MAX_RETRY);
|
||||
#endif
|
||||
return I2C_RESULT_ERROR;
|
||||
@@ -614,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) {
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
#if DEBUG_VERBOSE == CFG_ENABLE
|
||||
printf("IIC read failed after %d retries\n", I2C_MAX_RETRY);
|
||||
#endif
|
||||
return I2C_RESULT_ERROR;
|
||||
@@ -654,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) {
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
#if DEBUG_VERBOSE == CFG_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;
|
||||
@@ -714,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;
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
#if DEBUG_VERBOSE == CFG_ENABLE
|
||||
printf("I2C write failed for Error Slave Address. \n");
|
||||
#endif
|
||||
return I2C_RESULT_NACK;
|
||||
@@ -789,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) {
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
#if DEBUG_VERBOSE == CFG_ENABLE
|
||||
printf("IIC write failed after %d retries\n", I2C_MAX_RETRY);
|
||||
#endif
|
||||
return I2C_RESULT_ERROR;
|
||||
@@ -834,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);
|
||||
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
#if DEBUG_VERBOSE == CFG_ENABLE
|
||||
printf("I2C read start: slave=0x%02X reg=0x%02X len=%u\r\n", slave_addr, reg_addr, length);
|
||||
#endif
|
||||
|
||||
@@ -886,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 */
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
#if DEBUG_VERBOSE == CFG_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),
|
||||
@@ -904,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);
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
#if DEBUG_VERBOSE == CFG_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);
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
#if DEBUG_VERBOSE == CFG_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);
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
#if DEBUG_VERBOSE == CFG_ENABLE
|
||||
printf("I2C READ phase: length=%u (>2), keeping ACK enabled\r\n", length);
|
||||
#endif
|
||||
}
|
||||
@@ -949,7 +949,7 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8
|
||||
break;
|
||||
}
|
||||
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
#if DEBUG_VERBOSE == CFG_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 */
|
||||
@@ -972,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;
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
#if DEBUG_VERBOSE == CFG_ENABLE
|
||||
printf("I2C addressing sent for read (slave=0x%02X)\r\n", slave_addr);
|
||||
#endif
|
||||
break;
|
||||
|
||||
case I2C_STATE_RECEIVE_DATA:
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
#if DEBUG_VERBOSE == CFG_ENABLE
|
||||
printf("I2C RECEIVE_DATA: expecting %u bytes\r\n", length);
|
||||
#endif
|
||||
if (length == 1) {
|
||||
@@ -1060,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 */
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
#if DEBUG_VERBOSE == CFG_ENABLE
|
||||
printf("I2C_STATE_ERROR: resetting bus and retrying (retry_count=%u)\r\n", retry_count);
|
||||
#endif
|
||||
i2c_bus_reset();
|
||||
@@ -1071,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) {
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
#if DEBUG_VERBOSE == CFG_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",
|
||||
@@ -1080,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
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
#if DEBUG_VERBOSE == CFG_ENABLE
|
||||
printf("I2C read failed after %d retries\n", I2C_MAX_RETRY);
|
||||
#endif
|
||||
return I2C_RESULT_ERROR;
|
||||
@@ -1101,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 */
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
#if DEBUG_VERBOSE == CFG_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),
|
||||
@@ -1269,7 +1269,7 @@ i2c_result_t i2c_read_raw(uint8_t slave_addr, uint8_t *data, uint8_t length)
|
||||
return I2C_RESULT_TIMEOUT;
|
||||
}
|
||||
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
#if DEBUG_VERBOSE == CFG_ENABLE
|
||||
/*!
|
||||
\brief get status string for debugging
|
||||
\param[in] status: i2c_status_t value
|
||||
|
||||
+3
-3
@@ -62,15 +62,15 @@ int main(void)
|
||||
|
||||
i2c_config();
|
||||
|
||||
#if DEBUG_MODE == ENABLE
|
||||
#if DEBUG_MODE == CFG_ENABLE
|
||||
printf("Hello World!\r\n");
|
||||
#endif
|
||||
|
||||
#if SEGGER_RTT_DETECTION == ENABLE
|
||||
#if SEGGER_RTT_DETECTION == CFG_ENABLE
|
||||
RTT_printf(0, "Hello World!\r\n");
|
||||
#endif
|
||||
|
||||
#if DEBUG_VERBOSE == ENABLE
|
||||
#if DEBUG_VERBOSE == CFG_ENABLE
|
||||
i2c_scan();
|
||||
|
||||
i2c_bus_reset();
|
||||
|
||||
+15
@@ -17,6 +17,14 @@ void uart_init(void) {
|
||||
gpio_mode_set(UART_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, UART_TX_PIN | UART_RX_PIN);
|
||||
gpio_output_options_set(UART_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, UART_TX_PIN | UART_RX_PIN);
|
||||
|
||||
#if RS485_MODE == CFG_ENABLE
|
||||
/* 配置 RS485 DE 引脚 */
|
||||
rcu_periph_clock_enable(RS485_DE_RCU);
|
||||
gpio_af_set(RS485_DE_PORT, GPIO_AF_1, RS485_DE_PIN);
|
||||
gpio_mode_set(RS485_DE_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, RS485_DE_PIN);
|
||||
gpio_output_options_set(RS485_DE_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, RS485_DE_PIN);
|
||||
#endif
|
||||
|
||||
/* 配置波特率、数据位、停止位等 */
|
||||
usart_deinit(UART_PHY);
|
||||
usart_word_length_set(UART_PHY, USART_WL_8BIT);
|
||||
@@ -26,6 +34,13 @@ void uart_init(void) {
|
||||
usart_receive_config(UART_PHY, USART_RECEIVE_ENABLE);
|
||||
usart_transmit_config(UART_PHY, USART_TRANSMIT_ENABLE);
|
||||
|
||||
#if RS485_MODE == CFG_ENABLE
|
||||
// 配置 RS485 模式
|
||||
usart_rs485_driver_enable(UART_PHY);
|
||||
usart_driver_assertime_config(UART_PHY, 10);
|
||||
usart_driver_deassertime_config(UART_PHY, 10);
|
||||
|
||||
#endif
|
||||
usart_enable(UART_PHY);
|
||||
|
||||
nvic_irq_enable(UART_IRQ, 0);
|
||||
|
||||
Reference in New Issue
Block a user