add usart(rs485)

This commit is contained in:
yelvlab 2024-12-30 22:59:49 +08:00
parent dbbeea4dc7
commit 88fc97d037
12 changed files with 97 additions and 69 deletions

View File

@ -18,8 +18,8 @@ set(TARGET_C_SRC
${CMAKE_SOURCE_DIR}/src/main.c ${CMAKE_SOURCE_DIR}/src/main.c
${CMAKE_SOURCE_DIR}/src/gd32e23x_it.c ${CMAKE_SOURCE_DIR}/src/gd32e23x_it.c
${CMAKE_SOURCE_DIR}/src/systick.c ${CMAKE_SOURCE_DIR}/src/systick.c
${CMAKE_SOURCE_DIR}/src/peripheral.c
${CMAKE_SOURCE_DIR}/src/led.c ${CMAKE_SOURCE_DIR}/src/led.c
${CMAKE_SOURCE_DIR}/src/usart.c
) )
add_executable(gd32e23x_template ${TARGET_C_SRC}) add_executable(gd32e23x_template ${TARGET_C_SRC})

View File

@ -15,7 +15,6 @@ set(TARGET_C_SRC
${CMAKE_SOURCE_DIR}/src/main.c ${CMAKE_SOURCE_DIR}/src/main.c
${CMAKE_SOURCE_DIR}/src/gd32e23x_it.c ${CMAKE_SOURCE_DIR}/src/gd32e23x_it.c
${CMAKE_SOURCE_DIR}/src/systick.c ${CMAKE_SOURCE_DIR}/src/systick.c
${CMAKE_SOURCE_DIR}/src/peripheral.c
) )
``` ```
## 关于链接脚本 ## 关于链接脚本

View File

@ -21,13 +21,15 @@
/******************************************************************************/ /******************************************************************************/
#define RS485_RCU RCU_USART0 #define USART_RCU RCU_USART0
#define RS485_GPIO_RCU RCU_GPIOA #define USART_GPIO_RCU RCU_GPIOA
#define RS485_GPIO_PORT GPIOA #define USART_GPIO_PORT GPIOA
#define RS485_TX_PIN GPIO_PIN_2 #define USART_GPIO_AF GPIO_AF_1
#define RS485_RX_PIN GPIO_PIN_3 #define USART_TX_PIN GPIO_PIN_2
#define RS485_PHY USART0 #define USART_RX_PIN GPIO_PIN_3
#define RS485_BAUDRATE 115200U #define USART_PHY USART0
#define USART_PHY_BAUDRATE 115200U
#define RS485_EN_PORT GPIOA
#define RS485_EN_PIN GPIO_PIN_1 #define RS485_EN_PIN GPIO_PIN_1
/******************************************************************************/ /******************************************************************************/

View File

@ -36,6 +36,9 @@ OF SUCH DAMAGE.
#define GD32E23X_IT_H #define GD32E23X_IT_H
#include "gd32e23x.h" #include "gd32e23x.h"
#include "main.h"
#include "systick.h"
#include "board_config.h"
/* function declarations */ /* function declarations */
/* this function handles NMI exception */ /* this function handles NMI exception */
@ -49,4 +52,6 @@ void PendSV_Handler(void);
/* this function handles SysTick exception */ /* this function handles SysTick exception */
void SysTick_Handler(void); void SysTick_Handler(void);
void TIMER16_IRQHandler(void);
#endif /* GD32E23X_IT_H */ #endif /* GD32E23X_IT_H */

View File

@ -35,4 +35,12 @@ OF SUCH DAMAGE.
#ifndef MAIN_H #ifndef MAIN_H
#define MAIN_H #define MAIN_H
#include <stdio.h>
#include "gd32e23x.h"
#include "systick.h"
#include "gd32e23x_libopt.h"
#include "led.h"
#include "usart.h"
#include "board_config.h"
#endif /* MAIN_H */ #endif /* MAIN_H */

View File

@ -1,11 +0,0 @@
//
// Created by yelv1 on 24-9-22.
//
#ifndef PERIPHERAL_H
#define PERIPHERAL_H
void usart_config(void);
#endif //PERIPHERAL_H

15
inc/usart.h Normal file
View File

@ -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

View File

@ -33,9 +33,6 @@ OF SUCH DAMAGE.
*/ */
#include "gd32e23x_it.h" #include "gd32e23x_it.h"
#include "main.h"
#include "systick.h"
#include "board_config.h"
/*! /*!
\brief this function handles NMI exception \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) void TIMER16_IRQHandler(void)
{ {
if (timer_interrupt_flag_get(LED_BLINK_TIMER, TIMER_INT_FLAG_UP) == SET) if (timer_interrupt_flag_get(LED_BLINK_TIMER, TIMER_INT_FLAG_UP) == SET)

View File

@ -5,12 +5,6 @@
\version 2024-02-22, V2.1.0, firmware for GD32E23x \version 2024-02-22, V2.1.0, firmware for GD32E23x
*/ */
#include "main.h" #include "main.h"
#include <stdio.h>
#include "gd32e23x.h"
#include "systick.h"
#include "gd32e23x_libopt.h"
#include "peripheral.h"
/*! /*!
\brief main function \brief main function
@ -22,7 +16,9 @@ int main(void)
{ {
/* configure systick */ /* configure systick */
systick_config(); systick_config();
usart_config(); /* configure USART */
rs485_config();
/* configure LED */
led_blink_config(); led_blink_config();
delay_ms(5000); delay_ms(5000);
@ -35,10 +31,8 @@ int main(void)
} }
/* retarget the C library printf function to the USART */ /* retarget the C library printf function to the USART */
int _write (int fd, char *pBuffer, int size) int _write(int fd, char *pBuffer, int size) {
{ for (int i = 0; i < size; i++) {
for (int i = 0; i < size; i++)
{
usart_data_transmit(USART0, (uint8_t) pBuffer[i]); usart_data_transmit(USART0, (uint8_t) pBuffer[i]);
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE)); while (RESET == usart_flag_get(USART0, USART_FLAG_TBE));
} }

View File

@ -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);
}

46
src/usart.c Normal file
View File

@ -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
}