//stm32f1 串口通信
// 串口通信模塊會為串口劃分256字節(jié)的uart緩存區(qū),緩存中斷接收的數(shù)據(jù),
// 處理串口接收數(shù)據(jù)的任務(wù)會每50ms按協(xié)議處理解析緩存區(qū)里的數(shù)據(jù)。當(dāng)然,
// 如果沒有處理完緩存區(qū)的數(shù)據(jù),而又接收到新的數(shù)據(jù),那新的數(shù)據(jù)將會被
// 舍棄。
// 串口的接收數(shù)據(jù)和發(fā)送數(shù)據(jù)都用到DMA處理器,其中串口接收用了串口
// 空閑中斷。串口連續(xù)接收數(shù)據(jù)后檢測到串口接收空閑,產(chǎn)生一個空閑中斷,
// 這時候?qū)⒔邮盏降臄?shù)據(jù)緩存到uart緩存區(qū)中,等待解析。
// 1. 配置串口IO口以及串口功能配置
// 這里使用的是uart1
#define FIFOLEN 256
static uint8_t dma_buf[FIFOLEN];
static uint8_t dma_rx[FIFOLEN];
static u8 fifo_buf[FIFOLEN];
static u16 fifo_in = 0;
static u16 fifo_out = 0;
static u16 fifo_used = 0;
void uart1_func_init(void)
{
GPIO_InitTypeDef io;
USART_InitTypeDef uart;
//UART1 IO口配置
io.GPIO_Pin = GPIO_Pin_9;
io.GPIO_Mode= GPIO_Mode_AF_PP;
io.GPIO_Speed= GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &io);
io.GPIO_Pin = GPIO_Pin_10;
io.GPIO_Mode= GPIO_Mode_IN_FLOATING;
io.GPIO_Speed= GPIO_Speed_50MHz;;
GPIO_Init(GPIOA, &io);
//USART1 功能配置 波特率115200,8位數(shù)據(jù)位,1位停止位,無校驗(yàn),無硬件流控制
uart.USART_BaudRate = 115200;
uart.USART_WordLength = USART_WordLength_8b;
uart.USART_StopBits = USART_StopBits_1;
uart.USART_Parity = USART_Parity_No;
uart.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
uart.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &uart);
USART_ITConfig(USART1,USART_IT_TC,DISABLE);//使能串口空閑中斷
USART_ITConfig(USART1,USART_IT_RXNE,DISABLE);//失能接收中斷
USART_ITConfig(USART1,USART_IT_IDLE,ENABLE);//使能發(fā)送中斷
USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);//使能串口發(fā)送DMA
USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);//使能串口接收DMA
USART_Cmd(USART1, ENABLE);
}
// 2. 配置DMA功能
// stm32f103手冊里可以了解到,uart1-uart4有相關(guān)的DMA通道,對應(yīng)如下
// uart1_tx-->DMA1_Channel4 uart1_rx-->DMA1_Channel5
// uart2_tx-->DMA1_Channel7 uart2_rx-->DMA1_Channel6
// uart3_tx-->DMA1_Channel2 uart3_rx-->DMA1_Channel3
// uart4_tx-->DMA2_Channel5 uart4_rx-->DMA2_Channel2
static void uart1_dma_init(void)
{
DMA_InitTypeDef dma;
//DMA 配置
dma.DMA_BufferSize = 1; //初始化為1個字節(jié)
dma.DMA_DIR = DMA_DIR_PeripheralDST; //從內(nèi)存到外設(shè)
dma.DMA_M2M = DMA_M2M_Disable; //內(nèi)存到內(nèi)存,失能
dma.DMA_MemoryBaseAddr = (uint32_t)dma_buf; //內(nèi)存地址
dma.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //數(shù)據(jù)寬度
dma.DMA_MemoryInc = DMA_MemoryInc_Enable; //使能自增
dma.DMA_Mode = DMA_Mode_Normal; //普通模式,不連續(xù)模式
dma.DMA_PeripheralBaseAddr = (uint32_t)&USART1->DR; //外設(shè)地址
dma.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //數(shù)據(jù)寬度
dma.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外設(shè)地址不自增
dma.DMA_Priority = DMA_Priority_High; //優(yōu)先級
DMA_DeInit(DMA1_Channel4);
DMA_Init(DMA1_Channel4, &dma);
DMA_Cmd(DMA1_Channel4, ENABLE);
dma.DMA_PeripheralBaseAddr = (u32)(&USART1->DR);
dma.DMA_MemoryBaseAddr = (uint32_t)dma_rx;
dma.DMA_DIR = DMA_DIR_PeripheralSRC;
dma.DMA_BufferSize = FIFOLEN;
dma.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
dma.DMA_MemoryInc = DMA_MemoryInc_Enable;
dma.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
dma.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
dma.DMA_Mode = DMA_Mode_Normal;
dma.DMA_Priority = DMA_Priority_VeryHigh;
dma.DMA_M2M = DMA_M2M_Disable;
DMA_DeInit(DMA1_Channel5);
DMA_Init(DMA1_Channel5,&dma);
DMA_Cmd(DMA1_Channel5,ENABLE);
}
void uart1_init(void)
{
uart1_dma_init();
uart1_func_init();
}
//3. uart 緩存區(qū)用fifo方式存取
u16 uart1_read(u8* data, u16 len)
{
u16 i;
u16 ret;
ret = (len < fifo_used) ? len : fifo_used;
for(i = 0; i < ret; i++)
{
data[i] = fifo_buf[fifo_out];
if(++fifo_out == FIFOLEN)
{
fifo_out = 0;
}
}
fifo_used -= ret;
return ret;
}
void uart1_write(uint8_t* data, uint16_t len)
{
uint16_t i;
while(!DMA_GetFlagStatus(DMA1_FLAG_TC4));
DMA_Cmd(DMA1_Channel4, DISABLE);
for(i=0; i<len; i++)
{
dma_buf[i] = data[i];
}
DMA_ClearFlag(DMA1_FLAG_TC4);
DMA_SetCurrDataCounter(DMA1_Channel4, len);
DMA_Cmd(DMA1_Channel4, ENABLE);
}
//打印調(diào)試信息
void uart1_puts(char* s)
{
u16 i;
for(i=0;s[i]!=0;i++)
{
while(!USART_GetFlagStatus(USART1, USART_FLAG_TXE));
USART_SendData(USART1, s[i]);
}
}
void USART1_IRQHandler(void)
{
u16 temp;
u16 i;
if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
{
temp = USART1->SR;//----清除空閑標(biāo)志位
temp = USART1->DR;
DMA_Cmd(DMA1_Channel5,DISABLE);
temp = FIFOLEN - DMA_GetCurrDataCounter(DMA1_Channel5);
for(i=0; i<temp; i++)
{
if(fifo_used<FIFOLEN)
{
fifo_buf[fifo_in] = dma_rx[i];
if(++fifo_in == FIFOLEN)
{
fifo_in = 0;
}
fifo_used++;
}
}
DMA_SetCurrDataCounter(DMA1_Channel5,FIFOLEN);
DMA_Cmd(DMA1_Channel5,ENABLE);
}
else
{
USART_ReceiveData(USART1);
USART_ClearFlag(USART1, USART_FLAG_ORE);
}
}
//------------------------------------------------------------------------------------
上面是c文件,在h文件的接口如下
void uart1_init(void);
u16 uart1_read(u8* data, u16 len);
void uart1_write(u8* data, u16 len);
void uart1_puts(char* s);
//---------------------------------------------------------------------------------------
完成了上面的配置,就可以在解析數(shù)據(jù)包的任務(wù)里調(diào)用uart1_read函數(shù)了。長度一般為緩存區(qū)長度。
這里有兩個發(fā)送函數(shù),一個是uart1_put,一個是uart1_write,前者沒有dma發(fā)送,不需要指定長度,發(fā)送完整的字符串出去,適合打印調(diào)試信息到上位機(jī)。后者是dma方式發(fā)送,可以一定程度上減輕cpu負(fù)擔(dān),需要指定發(fā)送長度。