From 88fc97d0377d6f8a6934e3759fb107673ea38b77 Mon Sep 17 00:00:00 2001 From: yelvlab Date: Mon, 30 Dec 2024 22:59:49 +0800 Subject: [PATCH] add usart(rs485) --- CMakeLists.txt | 2 +- README.md | 1 - inc/board_config.h | 16 +++++++++------- inc/gd32e23x_it.h | 5 +++++ {src => inc}/led.h | 0 inc/main.h | 8 ++++++++ inc/peripheral.h | 11 ----------- inc/usart.h | 15 +++++++++++++++ src/gd32e23x_it.c | 9 ++++++--- src/main.c | 20 +++++++------------- src/peripheral.c | 33 --------------------------------- src/usart.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 12 files changed, 97 insertions(+), 69 deletions(-) rename {src => inc}/led.h (100%) delete mode 100644 inc/peripheral.h create mode 100644 inc/usart.h delete mode 100644 src/peripheral.c create mode 100644 src/usart.c diff --git a/CMakeLists.txt b/CMakeLists.txt index c6f6b6d..11519a3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,8 +18,8 @@ set(TARGET_C_SRC ${CMAKE_SOURCE_DIR}/src/main.c ${CMAKE_SOURCE_DIR}/src/gd32e23x_it.c ${CMAKE_SOURCE_DIR}/src/systick.c - ${CMAKE_SOURCE_DIR}/src/peripheral.c ${CMAKE_SOURCE_DIR}/src/led.c + ${CMAKE_SOURCE_DIR}/src/usart.c ) add_executable(gd32e23x_template ${TARGET_C_SRC}) diff --git a/README.md b/README.md index 40b9a59..f53230e 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,6 @@ set(TARGET_C_SRC ${CMAKE_SOURCE_DIR}/src/main.c ${CMAKE_SOURCE_DIR}/src/gd32e23x_it.c ${CMAKE_SOURCE_DIR}/src/systick.c - ${CMAKE_SOURCE_DIR}/src/peripheral.c ) ``` ## 关于链接脚本 diff --git a/inc/board_config.h b/inc/board_config.h index c8a2284..de0b880 100644 --- a/inc/board_config.h +++ b/inc/board_config.h @@ -21,13 +21,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 USART_RCU RCU_USART0 +#define USART_GPIO_RCU RCU_GPIOA +#define USART_GPIO_PORT GPIOA +#define USART_GPIO_AF GPIO_AF_1 +#define USART_TX_PIN GPIO_PIN_2 +#define USART_RX_PIN GPIO_PIN_3 +#define USART_PHY USART0 +#define USART_PHY_BAUDRATE 115200U +#define RS485_EN_PORT GPIOA #define RS485_EN_PIN GPIO_PIN_1 /******************************************************************************/ diff --git a/inc/gd32e23x_it.h b/inc/gd32e23x_it.h index cdae010..1b3b1ba 100644 --- a/inc/gd32e23x_it.h +++ b/inc/gd32e23x_it.h @@ -36,6 +36,9 @@ OF SUCH DAMAGE. #define GD32E23X_IT_H #include "gd32e23x.h" +#include "main.h" +#include "systick.h" +#include "board_config.h" /* function declarations */ /* this function handles NMI exception */ @@ -49,4 +52,6 @@ void PendSV_Handler(void); /* this function handles SysTick exception */ void SysTick_Handler(void); +void TIMER16_IRQHandler(void); + #endif /* GD32E23X_IT_H */ diff --git a/src/led.h b/inc/led.h similarity index 100% rename from src/led.h rename to inc/led.h diff --git a/inc/main.h b/inc/main.h index dd037de..b2da3dd 100644 --- a/inc/main.h +++ b/inc/main.h @@ -35,4 +35,12 @@ OF SUCH DAMAGE. #ifndef MAIN_H #define MAIN_H +#include +#include "gd32e23x.h" +#include "systick.h" +#include "gd32e23x_libopt.h" +#include "led.h" +#include "usart.h" +#include "board_config.h" + #endif /* MAIN_H */ diff --git a/inc/peripheral.h b/inc/peripheral.h deleted file mode 100644 index c74f98a..0000000 --- a/inc/peripheral.h +++ /dev/null @@ -1,11 +0,0 @@ -// -// Created by yelv1 on 24-9-22. -// - -#ifndef PERIPHERAL_H -#define PERIPHERAL_H - -void usart_config(void); - - -#endif //PERIPHERAL_H diff --git a/inc/usart.h b/inc/usart.h new file mode 100644 index 0000000..2c523d0 --- /dev/null +++ b/inc/usart.h @@ -0,0 +1,15 @@ +// +// Created by yelv1 on 24-12-30. +// + +#ifndef USART_H +#define USART_H + +#include "gd32e23x.h" +#include "board_config.h" + +void usart_config(void); + +void rs485_config(void); + +#endif //USART_H diff --git a/src/gd32e23x_it.c b/src/gd32e23x_it.c index e7fcd90..4dc5453 100644 --- a/src/gd32e23x_it.c +++ b/src/gd32e23x_it.c @@ -33,9 +33,6 @@ OF SUCH DAMAGE. */ #include "gd32e23x_it.h" -#include "main.h" -#include "systick.h" -#include "board_config.h" /*! \brief this function handles NMI exception @@ -99,6 +96,12 @@ void SysTick_Handler(void) { } +/*! + \brief this function handles TIMER16 interrupt request + \param[in] none + \param[out] none + \retval none +*/ void TIMER16_IRQHandler(void) { if (timer_interrupt_flag_get(LED_BLINK_TIMER, TIMER_INT_FLAG_UP) == SET) diff --git a/src/main.c b/src/main.c index ec1f78d..3402a38 100644 --- a/src/main.c +++ b/src/main.c @@ -5,12 +5,6 @@ \version 2024-02-22, V2.1.0, firmware for GD32E23x */ #include "main.h" -#include -#include "gd32e23x.h" -#include "systick.h" -#include "gd32e23x_libopt.h" - -#include "peripheral.h" /*! \brief main function @@ -22,7 +16,9 @@ int main(void) { /* configure systick */ systick_config(); - usart_config(); + /* configure USART */ + rs485_config(); + /* configure LED */ led_blink_config(); delay_ms(5000); @@ -35,12 +31,10 @@ int main(void) } /* retarget the C library printf function to the USART */ -int _write (int fd, char *pBuffer, int size) -{ - for (int i = 0; i < size; i++) - { - usart_data_transmit(USART0, (uint8_t)pBuffer[i]); - while(RESET == usart_flag_get(USART0, USART_FLAG_TBE)); +int _write(int fd, char *pBuffer, int size) { + for (int i = 0; i < size; i++) { + usart_data_transmit(USART0, (uint8_t) pBuffer[i]); + while (RESET == usart_flag_get(USART0, USART_FLAG_TBE)); } return size; } diff --git a/src/peripheral.c b/src/peripheral.c deleted file mode 100644 index 463ec58..0000000 --- a/src/peripheral.c +++ /dev/null @@ -1,33 +0,0 @@ -// -// Created by yelv1 on 24-9-22. -// -#include "peripheral.h" -#include "gd32e23x.h" - -void usart_config(void) -{ - rcu_periph_clock_enable(RCU_GPIOA); - rcu_periph_clock_enable(RCU_USART0); - - gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_3); - gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_2); - - gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_3); - gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_3); - gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_2); - gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_2); - - usart_deinit(USART0); - usart_baudrate_set(USART0, 115200U); - usart_receive_config(USART0, USART_RECEIVE_ENABLE); - usart_transmit_config(USART0, USART_TRANSMIT_ENABLE); - - usart_enable(USART0); - - gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_4); - gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_4); - - gpio_bit_write(GPIOA, GPIO_PIN_4, SET); -} - - diff --git a/src/usart.c b/src/usart.c new file mode 100644 index 0000000..25e1a4e --- /dev/null +++ b/src/usart.c @@ -0,0 +1,46 @@ +// +// Created by yelv1 on 24-12-30. +// + +#include "usart.h" + +/** + * @brief configure the USART + * @param none + * @retval none + */ +void usart_config(void) +{ + rcu_periph_clock_enable(USART_GPIO_RCU); + rcu_periph_clock_enable(USART_RCU); + + gpio_af_set(USART_GPIO_PORT, USART_GPIO_AF, USART_RX_PIN); + gpio_af_set(USART_GPIO_PORT, USART_GPIO_AF, USART_TX_PIN); + + gpio_mode_set(USART_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, USART_RX_PIN); + gpio_output_options_set(USART_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, USART_RX_PIN); + gpio_mode_set(USART_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, USART_TX_PIN); + gpio_output_options_set(USART_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, USART_TX_PIN); + + usart_deinit(USART_PHY); + usart_baudrate_set(USART_PHY, USART_PHY_BAUDRATE); + usart_receive_config(USART_PHY, USART_RECEIVE_ENABLE); + usart_transmit_config(USART_PHY, USART_TRANSMIT_ENABLE); + + usart_enable(USART_PHY); +} + +/** + * @brief configure the RS485(MAX13487) driver + * @param none + * @retval none + */ +void rs485_config(void) +{ + usart_config(); + + gpio_mode_set(RS485_EN_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, RS485_EN_PIN); + gpio_output_options_set(RS485_EN_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, RS485_EN_PIN); + + gpio_bit_write(RS485_EN_PORT, RS485_EN_PIN, SET); //auto dircetion control +} \ No newline at end of file