add usart(rs485)
This commit is contained in:
@@ -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)
|
||||
|
14
src/led.h
14
src/led.h
@@ -1,14 +0,0 @@
|
||||
//
|
||||
// Created by yelv1 on 24-12-30.
|
||||
//
|
||||
|
||||
#ifndef LED_H
|
||||
#define LED_H
|
||||
|
||||
#include "gd32e23x_it.h"
|
||||
#include "gd32e23x.h"
|
||||
#include "board_config.h"
|
||||
|
||||
void led_blink_config(void);
|
||||
|
||||
#endif //LED_H
|
20
src/main.c
20
src/main.c
@@ -5,12 +5,6 @@
|
||||
\version 2024-02-22, V2.1.0, firmware for GD32E23x
|
||||
*/
|
||||
#include "main.h"
|
||||
#include <stdio.h>
|
||||
#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;
|
||||
}
|
||||
|
@@ -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
46
src/usart.c
Normal 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
|
||||
}
|
Reference in New Issue
Block a user