有个项目串口不够用,所以打算用IO口模拟一个串口进行通讯。
参考了网上的代码,接收方法大同小异,但是发送大多数人都是用阻塞式的。阻塞式优点是可以节省单片机资源,只需要一个准确的延时函数即可。但是CPU在发送期间不能做其他事情,而使用定时器中断发送可解决这个问题,缺点是需要独占定时器。
代码在GD32303E开发板上运行通过,使用串口助手收发正常。
源文件
#include "bsp_io_uart.h"
//104us 9600 buad
#define UART_IO_DELAY_COUNT 104
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;
void BspIoUartInit(void)
{
BspIoUartT3Init();
BspIoUartT4Init();
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 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_COUNT * 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);
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:
u8SendData = pu8TxBuff[u16TxPos++];
gpio_bit_reset(GPIOB, GPIO_PIN_9);
eUartStep = eTransmit;
break;
case eTransmit:
if((u8SendData >> u8SendPos) & 0x01)
{
gpio_bit_set(GPIOB, GPIO_PIN_9);
}
else
{
gpio_bit_reset(GPIOB, GPIO_PIN_9);
}
u8SendPos++;
if(u8SendPos > 7)
{
u8SendPos = 0;
eUartStep = eStop;
}
break;
case eStop:
gpio_bit_set(GPIOB, GPIO_PIN_9);
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_COUNT * 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);
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))
{
if(gpio_input_bit_get(GPIOB, GPIO_PIN_8) != RESET)
{
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
|