generated from hulk/gd32e23x_template_cmake_vscode
Compare commits
1 Commits
main
...
template_p
| Author | SHA1 | Date | |
|---|---|---|---|
| 3a56d5937a |
@@ -22,7 +22,7 @@
|
||||
|
||||
// #define ULTRASONIC_BOARD
|
||||
// #define EDDY_BOARD
|
||||
#define BLDC_BOARD
|
||||
#define CERAMIXNANO_MB
|
||||
|
||||
|
||||
/******************************************************************************/
|
||||
@@ -39,7 +39,7 @@
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#if defined(ULTRASONIC_BOARD)
|
||||
#ifdef ULTRASONIC_BOARD
|
||||
|
||||
#define LED_RCU RCU_GPIOA
|
||||
#define LED_PORT GPIOA
|
||||
@@ -50,10 +50,10 @@
|
||||
#define LED_PORT GPIOA
|
||||
#define LED_PIN GPIO_PIN_7
|
||||
|
||||
#elif defined(BLDC_BOARD)
|
||||
#define LED_RCU RCU_GPIOB
|
||||
#define LED_PORT GPIOB
|
||||
#define LED_PIN GPIO_PIN_1
|
||||
#elif defined(CERAMIXNANO_MB)
|
||||
#define LED_RCU RCU_GPIOA
|
||||
#define LED_PORT GPIOA
|
||||
#define LED_PIN GPIO_PIN_7
|
||||
|
||||
#endif
|
||||
|
||||
@@ -65,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
|
||||
|
||||
43
README.md
43
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 控制在发送/接收期间正确切换以避免收发冲突。
|
||||
|
||||
如需,我可以为你生成一个包含以上三项修改的补丁并运行一次构建测试。
|
||||
|
||||
|
||||
如需进一步完善或有其他建议,欢迎随时反馈!
|
||||
|
||||
@@ -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);
|
||||
// 在这里添加空闲中断处理逻辑
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
12
Src/uart.c
12
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);
|
||||
|
||||
Reference in New Issue
Block a user