generated from hulk/gd32e23x_template_cmake_vscode
feature(rs485):添加RS485驱动支持
This commit is contained in:
@@ -51,6 +51,8 @@
|
|||||||
#define RTT_PutChar(ch, c)
|
#define RTT_PutChar(ch, c)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#define RS485_MODE CFG_ENABLE // CFG_DISABLE: RS485 off; CFG_ENABLE: RS485 on
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
/* Dynamic USART Configuration Structure */
|
/* Dynamic USART Configuration Structure */
|
||||||
@@ -129,6 +131,12 @@ typedef enum {
|
|||||||
#define UART_RX_PIN GPIO_PIN_3
|
#define UART_RX_PIN GPIO_PIN_3
|
||||||
#define UART_BAUDRATE 115200U
|
#define UART_BAUDRATE 115200U
|
||||||
|
|
||||||
|
#if RS485_MODE == CFG_ENABLE
|
||||||
|
#define RS485_DE_RCU RCU_GPIOA
|
||||||
|
#define RS485_DE_PORT GPIOA
|
||||||
|
#define RS485_DE_PIN GPIO_PIN_1
|
||||||
|
#endif
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#define DEBUG_UART USART0
|
#define DEBUG_UART USART0
|
||||||
|
|||||||
+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_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);
|
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_mode_set(RS485_DE_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, RS485_DE_PIN);
|
||||||
|
gpio_output_options_set(RS485_DE_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, RS485_DE_PIN);
|
||||||
|
gpio_bit_reset(RS485_DE_PORT, RS485_DE_PIN); // 初始状态为接收模式
|
||||||
|
#endif
|
||||||
|
|
||||||
/* 配置波特率、数据位、停止位等 */
|
/* 配置波特率、数据位、停止位等 */
|
||||||
usart_deinit(UART_PHY);
|
usart_deinit(UART_PHY);
|
||||||
usart_word_length_set(UART_PHY, USART_WL_8BIT);
|
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_receive_config(UART_PHY, USART_RECEIVE_ENABLE);
|
||||||
usart_transmit_config(UART_PHY, USART_TRANSMIT_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); // 设置驱动器断言时间为 10 个时钟周期
|
||||||
|
usart_driver_deassertime_config(UART_PHY, 10); // 设置驱动器去断言时间为 10 个时钟周期
|
||||||
|
|
||||||
|
#endif
|
||||||
usart_enable(UART_PHY);
|
usart_enable(UART_PHY);
|
||||||
|
|
||||||
nvic_irq_enable(UART_IRQ, 0);
|
nvic_irq_enable(UART_IRQ, 0);
|
||||||
|
|||||||
Reference in New Issue
Block a user