diff --git a/Inc/board_config.h b/Inc/board_config.h index 2aaa572..50007c2 100644 --- a/Inc/board_config.h +++ b/Inc/board_config.h @@ -20,8 +20,9 @@ /* >>>>>>>>>>>>>>>>>>>[BOARD TYPE CONFIG]<<<<<<<<<<<<<<<<<<<< */ -#define ULTRASONIC_BOARD +// #define ULTRASONIC_BOARD // #define EDDY_BOARD +#define CERAMIXNANO_MB /******************************************************************************/ @@ -44,7 +45,12 @@ #define LED_PORT GPIOA #define LED_PIN GPIO_PIN_9 -#elif EDDY_BOARD +#elif defined(EDDY_BOARD) +#define LED_RCU RCU_GPIOA +#define LED_PORT GPIOA +#define LED_PIN GPIO_PIN_7 + +#elif defined(CERAMIXNANO_MB) #define LED_RCU RCU_GPIOA #define LED_PORT GPIOA #define LED_PIN GPIO_PIN_7 @@ -59,7 +65,7 @@ #define RS485_IRQ USART1_IRQn #define RS485_GPIO_RCU RCU_GPIOA #define RS485_GPIO_PORT GPIOA -#define RS485_EN_PIN GPIO_PIN_1 +// #define RS485_EN_PIN GPIO_PIN_1 #define RS485_TX_PIN GPIO_PIN_2 #define RS485_RX_PIN GPIO_PIN_3 #define RS485_BAUDRATE 115200U diff --git a/README.md b/README.md index a2cadad..f74812c 100644 --- a/README.md +++ b/README.md @@ -136,4 +136,47 @@ --- +## Bootloader: RS485 与中断(重要修改说明) + +如果你在工程中使用 RS485 串口作为 bootloader 的传输通道,请按下列步骤检查并修改源码以确保接收与中断工作正常: + +1. `board_config.h`:如果板级设计使用 RS485 的发送使能引脚(DE/RE),请取消注释或定义 `RS485_EN_PIN`,例如: + + - 在 `Inc/board_config.h` 中找到 RS485 相关定义区域,确保存在并启用 `RS485_EN_PIN` 的定义(该宏在部分板卡上用于控制驱动器方向)。 + +2. `Src/uart.c`:在启用 RS485 时,需要把 EN 引脚也加入 AF 配置,以便在需要时由软件控制或保持正确的复用: + + - 修改 `gpio_af_set` 的调用,包含 `RS485_EN_PIN`: + ```c + gpio_af_set(RS485_GPIO_PORT, GPIO_AF_1, RS485_TX_PIN | RS485_RX_PIN | RS485_EN_PIN); + ``` + + - 如果你的硬件使用独立的 GPIO 控制 EN(非 AF),请根据实际电路改为 `gpio_mode_set` + `gpio_output_options_set` 并在发送前后切换引脚电平。 + +3. 中断服务函数(ISR)名称一致性: + + - 当前 `Src/gd32e23x_it.c` 中的接收中断函数为 `USART1_IRQHandler`,但 `RS485_PHY` 使用的是 `USART0`(见 `Inc/board_config.h` 中 `RS485_PHY` 定义)。请确保 NVIC 中断号与 ISR 名称匹配。两种可行的修复: + - 把 `RS485_IRQ` 设置为 `USART0_IRQn`(推荐,当 `RS485_PHY` 为 `USART0` 时),或 + - 把 ISR 更名为 `USART0_IRQHandler` 并处理 `USART0` 的中断。 + + - 示例(推荐修复一):在 `Inc/board_config.h` 中: + ```c + #define RS485_PHY USART0 + #define RS485_IRQ USART0_IRQn + ``` + + - 示例(替代修复二):在 `Src/gd32e23x_it.c` 中将函数签名改为: + ```c + void USART0_IRQHandler(void) { ... } + ``` + +注意:在做以上修改后,重新编译并烧录固件,然后用示波器或串口日志确认: + +- MCU 是否在上电/复位后输出 bootloader 的欢迎/OK 字符; +- 发送单字节(例如 ASCII '0'、'1')时是否会触发中断并被写入环形缓冲; +- 若使用 RS485,确保 DE/RE 控制在发送/接收期间正确切换以避免收发冲突。 + +如需,我可以为你生成一个包含以上三项修改的补丁并运行一次构建测试。 + + 如需进一步完善或有其他建议,欢迎随时反馈! diff --git a/Src/gd32e23x_it.c b/Src/gd32e23x_it.c index b36f5d3..4b4e57f 100644 --- a/Src/gd32e23x_it.c +++ b/Src/gd32e23x_it.c @@ -102,15 +102,15 @@ void SysTick_Handler(void) { void USART1_IRQHandler(void) { // 处理USART1的接收中断 - if(usart_interrupt_flag_get(USART1, USART_INT_FLAG_RBNE)) { - uint8_t data = usart_data_receive(USART1); + if(usart_interrupt_flag_get(RS485_PHY, USART_INT_FLAG_RBNE)) { + uint8_t data = usart_data_receive(RS485_PHY); // 使用原有的环形缓冲区处理逻辑 (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); + if(usart_interrupt_flag_get(RS485_PHY, USART_INT_FLAG_IDLE)) { + usart_interrupt_flag_clear(RS485_PHY, USART_INT_FLAG_IDLE); // 在这里添加空闲中断处理逻辑 } } diff --git a/Src/main.c b/Src/main.c index df4ef7f..f199f99 100644 --- a/Src/main.c +++ b/Src/main.c @@ -65,10 +65,10 @@ int main(void) delay_ms(100); rs485_send_str((uint8_t*)"ok", 2); - delay_ms(100); + // delay_ms(100); // wait rs485 send complete: DE high -> low - while (uart_ring_buffer_get() != 0x30); + while (uart_ring_buffer_get() != 0x31); rs485_send_str((uint8_t*)"ok", 2); while (uart_ring_buffer_get() != 0x31); diff --git a/Src/uart.c b/Src/uart.c index ed54489..1ea6bc4 100644 --- a/Src/uart.c +++ b/Src/uart.c @@ -12,13 +12,13 @@ void rs485_init(void) { rcu_periph_clock_enable(RS485_RCU); /* 配置 PA2 为 USART0_TX,PA3 为 USART0_RX */ - gpio_af_set(RS485_GPIO_PORT, GPIO_AF_1, RS485_TX_PIN | RS485_RX_PIN | RS485_EN_PIN); + gpio_af_set(RS485_GPIO_PORT, GPIO_AF_1, RS485_TX_PIN | RS485_RX_PIN ); gpio_mode_set(RS485_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, RS485_TX_PIN | RS485_RX_PIN); gpio_output_options_set(RS485_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, RS485_TX_PIN | RS485_RX_PIN); - gpio_mode_set(RS485_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, RS485_EN_PIN); - gpio_output_options_set(RS485_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, RS485_EN_PIN); + // gpio_mode_set(RS485_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, RS485_EN_PIN); + // gpio_output_options_set(RS485_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, RS485_EN_PIN); /* 配置波特率、数据位、停止位等 */ usart_deinit(RS485_PHY); @@ -29,10 +29,10 @@ void rs485_init(void) { usart_receive_config(RS485_PHY, USART_RECEIVE_ENABLE); usart_transmit_config(RS485_PHY, USART_TRANSMIT_ENABLE); - usart_driver_assertime_config(RS485_PHY, 0x01); - usart_driver_deassertime_config(RS485_PHY, 0x10); + // usart_driver_assertime_config(RS485_PHY, 0x01); + // usart_driver_deassertime_config(RS485_PHY, 0x10); - usart_rs485_driver_enable(RS485_PHY); + // usart_rs485_driver_enable(RS485_PHY); usart_interrupt_enable(RS485_PHY, USART_INT_RBNE); // usart_interrupt_enable(RS485_PHY, USART_INT_IDLE);