generated from hulk/gd32e23x_template_cmake_vscode
Compare commits
2 Commits
c13a7abc84
...
c6c90800d4
Author | SHA1 | Date | |
---|---|---|---|
c6c90800d4 | |||
63d3c30607 |
@ -12,6 +12,7 @@
|
|||||||
"type": "FILEPATH",
|
"type": "FILEPATH",
|
||||||
"value": "${sourceDir}/cmake/arm-none-eabi-gcc.cmake"
|
"value": "${sourceDir}/cmake/arm-none-eabi-gcc.cmake"
|
||||||
}
|
}
|
||||||
|
,"CMAKE_EXPORT_COMPILE_COMMANDS": "ON"
|
||||||
},
|
},
|
||||||
"architecture": {
|
"architecture": {
|
||||||
"value": "unspecified",
|
"value": "unspecified",
|
||||||
|
@ -9,4 +9,15 @@
|
|||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
|
#define RS485_RCU RCU_USART0
|
||||||
|
#define RS485_GPIO_RCU RCU_GPIOA
|
||||||
|
#define RS485_GPIO_PORT GPIOA
|
||||||
|
#define RS485_TX_PIN GPIO_PIN_2
|
||||||
|
#define RS485_RX_PIN GPIO_PIN_3
|
||||||
|
#define RS485_PHY USART0
|
||||||
|
#define RS485_BAUDRATE 115200U
|
||||||
|
#define RS485_EN_PIN GPIO_PIN_1
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
#endif //BOARD_CONFIG_H
|
#endif //BOARD_CONFIG_H
|
||||||
|
@ -9,8 +9,7 @@ typedef enum {
|
|||||||
UART_PRINTF_BOTH = 2
|
UART_PRINTF_BOTH = 2
|
||||||
} uart_printf_port_t;
|
} uart_printf_port_t;
|
||||||
|
|
||||||
void uart0_init(uint32_t baudrate);
|
void uart0_init(void);
|
||||||
void uart1_init(uint32_t baudrate);
|
|
||||||
void uart_set_printf_port(uart_printf_port_t port);
|
void uart_set_printf_port(uart_printf_port_t port);
|
||||||
|
|
||||||
#endif // UART_H
|
#endif // UART_H
|
||||||
|
@ -47,7 +47,7 @@ OF SUCH DAMAGE.
|
|||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
systick_config();
|
systick_config();
|
||||||
uart0_init(115200);
|
uart0_init();
|
||||||
// uart1_init(115200); // 如需使用USART1请初始化
|
// uart1_init(115200); // 如需使用USART1请初始化
|
||||||
|
|
||||||
// printf("Hello USART0!\r\n");
|
// printf("Hello USART0!\r\n");
|
||||||
|
51
Src/uart.c
51
Src/uart.c
@ -2,39 +2,38 @@
|
|||||||
#include "gd32e23x_usart.h"
|
#include "gd32e23x_usart.h"
|
||||||
#include "gd32e23x_rcu.h"
|
#include "gd32e23x_rcu.h"
|
||||||
#include "gd32e23x_gpio.h"
|
#include "gd32e23x_gpio.h"
|
||||||
|
#include "board_config.h"
|
||||||
|
|
||||||
|
|
||||||
void uart0_init(uint32_t baudrate) {
|
void uart0_init(void) {
|
||||||
/* 使能 GPIOA 和 USART0 时钟 */
|
/* 使能 GPIOA 和 USART0 时钟 */
|
||||||
rcu_periph_clock_enable(RCU_GPIOA);
|
rcu_periph_clock_enable(RS485_GPIO_RCU);
|
||||||
rcu_periph_clock_enable(RCU_USART0);
|
rcu_periph_clock_enable(RS485_RCU);
|
||||||
|
|
||||||
/* 配置 PA9 为 USART0_TX,PA10 为 USART0_RX */
|
/* 配置 PA2 为 USART0_TX,PA3 为 USART0_RX */
|
||||||
gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_9 | GPIO_PIN_10);
|
gpio_af_set(RS485_GPIO_PORT, GPIO_AF_1, RS485_TX_PIN | RS485_RX_PIN | RS485_EN_PIN);
|
||||||
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_9 | GPIO_PIN_10);
|
|
||||||
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9 | GPIO_PIN_10);
|
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_OUTPUT, GPIO_PUPD_NONE, RS485_EN_PIN);
|
||||||
|
gpio_output_options_set(RS485_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, RS485_EN_PIN);
|
||||||
|
|
||||||
/* 配置波特率、数据位、停止位等 */
|
/* 配置波特率、数据位、停止位等 */
|
||||||
usart_deinit(USART0);
|
usart_deinit(RS485_PHY);
|
||||||
usart_baudrate_set(USART0, baudrate);
|
usart_baudrate_set(RS485_PHY, RS485_BAUDRATE);
|
||||||
usart_receive_config(USART0, USART_RECEIVE_ENABLE);
|
usart_receive_config(RS485_PHY, USART_RECEIVE_ENABLE);
|
||||||
usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
|
usart_transmit_config(RS485_PHY, USART_TRANSMIT_ENABLE);
|
||||||
usart_enable(USART0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void uart1_init(uint32_t baudrate) {
|
usart_driver_assertime_config(RS485_PHY, 0x01);
|
||||||
rcu_periph_clock_enable(RCU_GPIOA);
|
usart_driver_deassertime_config(RS485_PHY, 0x01);
|
||||||
rcu_periph_clock_enable(RCU_USART1);
|
|
||||||
// USART1 默认引脚为 PA2 (TX), PA3 (RX)
|
usart_rs485_driver_enable(RS485_PHY);
|
||||||
gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_2 | GPIO_PIN_3);
|
|
||||||
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_2 | GPIO_PIN_3);
|
usart_enable(RS485_PHY);
|
||||||
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2 | GPIO_PIN_3);
|
|
||||||
usart_deinit(USART1);
|
// TODO NVIC
|
||||||
usart_baudrate_set(USART1, baudrate);
|
}
|
||||||
usart_receive_config(USART1, USART_RECEIVE_ENABLE);
|
|
||||||
usart_transmit_config(USART1, USART_TRANSMIT_ENABLE);
|
|
||||||
usart_enable(USART1);
|
|
||||||
}
|
|
||||||
|
|
||||||
static uart_printf_port_t g_printf_port = UART_PRINTF_USART0;
|
static uart_printf_port_t g_printf_port = UART_PRINTF_USART0;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user