#include "UART.h"
#include "gd32f4xx_usart.h"
#include <stdio.h>
/* 重定义printf->USART0 */
int fputc(int ch, FILE *f)
{
usart_data_transmit(USART0, (uint8_t)ch);
while (usart_flag_get(USART0, USART_FLAG_TBE) == RESET);
return ch;
}
uint8_t rx_fifo[RX_FIFO_SIZE];
uint16_t wp = 0; // 写指针
uint16_t rp = 0; // 读指针
uint8_t buf[BUF_SIZE];
uint8_t frame_ready = 0;
uint8_t frame_found = 0;
uint16_t frame_start = 0;
void usart0_init(void)
{
/* 1. 使能GPIOA和USART0时钟 */
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_USART0);
/* 2. 配置PA9为USART0_TX,PA10为USART0_RX */
gpio_af_set(GPIOA, GPIO_AF_7, GPIO_PIN_9 | GPIO_PIN_10);
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);
/* 3. 配置USART0参数:115200 8N1 */
usart_deinit(USART0);
usart_baudrate_set(USART0, 115200U);
// usart_baudrate_set(USART0, 256000U);
usart_word_length_set(USART0, USART_WL_8BIT);
usart_stop_bit_set(USART0, USART_STB_1BIT);
usart_parity_config(USART0, USART_PM_NONE);
usart_hardware_flow_rts_config(USART0, USART_RTS_DISABLE);
usart_hardware_flow_cts_config(USART0, USART_CTS_DISABLE);
usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
usart_receive_config(USART0, USART_RECEIVE_ENABLE);
/* 4. 使能接收中断 */
usart_interrupt_enable(USART0, USART_INT_RBNE); // 接收缓冲区非空中断
/* 5. 配置NVIC */
nvic_irq_enable(USART0_IRQn, 0, 0); // 设置优先级
/* 6. 使能USART */
usart_enable(USART0);
}
void usart_send_char(uint32_t usart_periph, char ch) {
usart_data_transmit(usart_periph, (uint8_t)ch);
while (RESET == usart_flag_get(usart_periph, USART_FLAG_TBE));
}
// 发送指定长度的char数组
void usart_send_array(uint32_t usart_periph, const char* data, uint16_t length) {
for (uint16_t i = 0; i < length; i++) {
usart_send_char(usart_periph, data[i]);
}
}
void usart_send_string(uint32_t usart_periph, const char* str) {
while (*str != '\0') {
usart_send_char(usart_periph, *str);
str++;
}
}
————————————————
版权声明:本文为CSDN博主「qvictory」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qvictory/article/details/150954201
|