有个项目串口不够用,所以打算用IO口模拟一个串口进行通讯。
参考了网上的代码,接收方法大同小异,但是发送大多数人都是用阻塞式的。阻塞式优点是可以节省单片机资源,只需要一个准确的延时函数即可。但是CPU在发送期间不能做其他事情,而使用定时器中断发送可解决这个问题,缺点是需要独占定时器。
代码在GD32303E开发板上运行通过,使用串口助手收发正常。
注意的是,如果用此代码与单片机进行通讯,需要注意单片机停止位宽度。
实测GD32设置串口停止位为1时,输出2个停止位。
串口助手实际输出停止位为1.
源文件
#include "bsp_io_uart.h"
//208us 4800 buad
//注意单片机停止位
//此程序停止位为2
#define UART_IO_DELAY_US 208
static uint8 *pu8RxBuff = NULL;
static uint16 u16RxPos = 0;
static uint16 u16RxCount = 0;
static uint8 u8RevData = 0;
static uint8 u8Pos = 0;
static uint8 *pu8TxBuff = NULL;
static uint16 u16TxPos = 0;
static uint16 u16TxCount = 0;
static uint8 u8SendData = 0;
static uint8 u8SendPos = 0;
static void BspIoUartT3Init(void);
static void BspIoUartT4Init(void);
typedef enum
{
eStart = 0,
eTransmit,
eStop,
}eBspIoUartTxStep;
static eBspIoUartTxStep eUartStep;
static void IoUartTimerInit(void);
static void IoUartGpioInit(void);
static void IoUartSetTxTimerTrigger(uint32 u32TimeUs);
static void IoUartSetRxTimerTrigger(uint32 u32TimeUs);
static void IoUartSetTxIoState(bool bState);
static bool IoUartGetRxIoState(void);
void BspIoUartInit(void)
{
IoUartTimerInit();
IoUartGpioInit();
}
static void IoUartTimerInit(void)
{
BspIoUartT3Init();
BspIoUartT4Init();
}
static void IoUartGpioInit(void)
{
rcu_periph_clock_enable(RCU_AF);
rcu_periph_clock_enable(RCU_GPIOB);
//PB8 RX
nvic_irq_enable(EXTI5_9_IRQn, 0U, 0U);
gpio_init(GPIOB, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, GPIO_PIN_8);
gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOB, GPIO_PIN_SOURCE_8);
exti_init(EXTI_8, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
exti_interrupt_flag_clear(EXTI_8);
//PB9 TX
gpio_bit_set(GPIOB, GPIO_PIN_9);
gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
}
static void IoUartSetTxTimerTrigger(uint32 u32TimeUs)
{
timer_autoreload_value_config(TIMER4, u32TimeUs * 120 - 1);
}
static void IoUartSetRxTimerTrigger(uint32 u32TimeUs)
{
timer_autoreload_value_config(TIMER3, u32TimeUs * 120 - 1);
}
static void IoUartSetTxIoState(bool bState)
{
if(bState == true)
{
gpio_bit_set(GPIOB, GPIO_PIN_9);
}
else
{
gpio_bit_reset(GPIOB, GPIO_PIN_9);
}
}
static bool IoUartGetRxIoState(void)
{
if(gpio_input_bit_get(GPIOB, GPIO_PIN_8) != RESET)
{
return true;
}
else
{
return false;
}
}
static void BspIoUartT4Init(void)
{
timer_parameter_struct timer_initpara;
nvic_irq_enable(TIMER4_IRQn, 0, 0);
rcu_periph_clock_enable(RCU_TIMER4);
timer_deinit(TIMER4);
timer_struct_para_init(&timer_initpara);
timer_initpara.prescaler = 0;
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = UART_IO_DELAY_US * 120 - 1;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_init(TIMER4, &timer_initpara);
timer_interrupt_flag_clear(TIMER4, TIMER_INT_FLAG_UP);
timer_interrupt_enable(TIMER4, TIMER_INT_UP);
timer_disable(TIMER4);
}
void BspIoUartTxData(uint8 *pu8TxData, uint16 u16Count)
{
timer_disable(TIMER4);
IoUartSetTxTimerTrigger(UART_IO_DELAY_US);
timer_counter_value_config(TIMER4,0);
timer_interrupt_flag_clear(TIMER4, TIMER_INT_FLAG_UP);
pu8TxBuff = pu8TxData;
u16TxCount = u16Count;
u16TxPos = 0;
u8SendData = 0;
u8SendPos = 0;
eUartStep = eStart;
timer_enable(TIMER4);
}
void TIMER4_IRQHandler(void)
{
if(SET == timer_interrupt_flag_get(TIMER4, TIMER_INT_FLAG_UP))
{
switch(eUartStep)
{
case eStart:
IoUartSetTxTimerTrigger(UART_IO_DELAY_US);
u8SendData = pu8TxBuff[u16TxPos++];
IoUartSetTxIoState(false);
eUartStep = eTransmit;
break;
case eTransmit:
if((u8SendData >> u8SendPos) & 0x01)
{
IoUartSetTxIoState(true);
}
else
{
IoUartSetTxIoState(false);
}
u8SendPos++;
if(u8SendPos > 7)
{
u8SendPos = 0;
eUartStep = eStop;
}
break;
case eStop:
IoUartSetTxTimerTrigger(UART_IO_DELAY_US * 2);
IoUartSetTxIoState(true);
eUartStep = eStart;
if(u16TxPos >= u16TxCount)
{
timer_disable(TIMER4);
}
break;
}
timer_interrupt_flag_clear(TIMER4, TIMER_INT_FLAG_UP);
}
}
/************************* IO UART RX ****************************/
static void BspIoUartT3Init(void)
{
timer_parameter_struct timer_initpara;
nvic_irq_enable(TIMER3_IRQn, 0, 0);
rcu_periph_clock_enable(RCU_TIMER3);
timer_deinit(TIMER3);
timer_struct_para_init(&timer_initpara);
timer_initpara.prescaler = 0;
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = UART_IO_DELAY_US * 120 - 1;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_init(TIMER3, &timer_initpara);
timer_interrupt_flag_clear(TIMER3, TIMER_INT_FLAG_UP);
timer_interrupt_enable(TIMER3, TIMER_INT_UP);
timer_disable(TIMER3);
}
void BspIoUartRxData(uint8 *pu8RxData, uint16 u16Count)
{
nvic_irq_disable(EXTI5_9_IRQn);
timer_disable(TIMER3);
timer_interrupt_flag_clear(TIMER3, TIMER_INT_FLAG_UP);
pu8RxBuff = pu8RxData;
u16RxCount = u16Count;
u16RxPos = 0;
u8RevData = 0;
u8Pos = 0;
exti_interrupt_flag_clear(EXTI_8);
nvic_irq_enable(EXTI5_9_IRQn, 0U, 0U);
}
void EXTI5_9_IRQHandler(void)
{
if(RESET != exti_interrupt_flag_get(EXTI_8))
{
nvic_irq_disable(EXTI5_9_IRQn);
IoUartSetRxTimerTrigger((uint32)(UART_IO_DELAY_US * 1.5f));
timer_counter_value_config(TIMER3,0);
timer_enable(TIMER3);
exti_interrupt_flag_clear(EXTI_8);
}
}
void TIMER3_IRQHandler(void)
{
if(SET == timer_interrupt_flag_get(TIMER3, TIMER_INT_FLAG_UP))
{
IoUartSetRxTimerTrigger(UART_IO_DELAY_US);
if(IoUartGetRxIoState() == true)
{
u8RevData |= (1 << u8Pos);
}
u8Pos++;
if(u8Pos >= 8)
{
pu8RxBuff[u16RxPos++] = u8RevData;
if(u16RxPos >= u16RxCount)
{
u16RxPos = 0;
}
u8Pos = 0;
u8RevData = 0;
exti_interrupt_flag_clear(EXTI_8);
nvic_irq_enable(EXTI5_9_IRQn, 0U, 0U);
timer_disable(TIMER3);
}
timer_interrupt_flag_clear(TIMER3, TIMER_INT_FLAG_UP);
}
}
头文件
#ifndef _BSP_IO_UART_H
#define _BSP_IO_UART_H
#include "gd32f30x.h"
#include "basic_data_type.h"
void BspIoUartInit(void);
void BspIoUartTxData(uint8 *pu8TxData, uint16 u16Count);
void BspIoUartRxData(uint8 *pu8RxData, uint16 u16Count);
#endif
版权声明:本文为CSDN博主「爱FC的捷哥」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/a3748622/article/details/121930558
暂无评论