STM32 是一款根据ARM Cortex-M3内核的32位MCU,主频最高可达72M。最近我们要在车机上集成TPMS功用, 便开端着手STM32的开发作业,STM32F10x系列共有5个串口(USART1~USART5),支撑DMA方法通讯,DMA方法我们不需要CPU的参加,而是直接由DMA操控器完结串口数据的读写,因此能够很大程度的进步CPU的利用率。在运用STM32串口之前需要做一系列的初始化作业:
1.RCC(复位和时钟操控寄存器)初始化,启用GPIO、DMA、USART时钟。
2.NVIC(嵌套向量中止操控寄存器)初始化,完结各个硬件中止的装备。
3.USART初始话,装备串口,设置DMA通道等。
4.DMA初始化,完结DMA的装备。
最终是使能USART和DMA。下面是经过DMA的方法从串口USART1接纳数据,STM32作业后串口数据由DMA操控器接纳存到指定buffer,读取数据直接从DMA buffer中读取即可。发送数据选用非DMA方法,首先将待发送的数据存入到发送行列,然后在使命循环中将行列中的数据发送给USART1。实例代码如下:
[cpp] view plaincopy
//**********************************************************************************************
// STM32F10x USART Test
// compiler: Keil UV3
// 2011-03-28 , By friehood
//**********************************************************************************************
static int8u rDMABuffer[64]; // DMA buffer
static int16u rDMARear = sizeof(rDMABuffer);
static int8u USART_RevBuf[64]; // 串口接纳buffer
static int8u USART_SndBuf[64]; // 串口发送buffer
static int8u Head=0,Tail=0; // 发送buffer的头尾
// 串口使命
void Task_USART(void)
{
int16u end;
if (USART1->SR & (USART_FLAG_ORE | USART_FLAG_NE | USART_FLAG_FE))
{
USART_ReceiveData(USART1);
}
// DMA接纳
end = DMA_GetCurrDataCounter(DMA1_Channel5);
/*if((sizeof(rDMABuffer)-end)>0)
dbgprt(“DMA available datalen=%d/n”,sizeof(rDMABuffer)-end); */
while(rDMARear != end)
{
USART_receive(rDMABuffer[sizeof(rDMABuffer)-rDMARear]);
if (!(–rDMARear))
{
rDMARear = sizeof(rDMABuffer);
}
}
//发送
if(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == SET)
{
int8u chr;
// 从发送行列取出一个字符
if(PopFront(&chr))
{
USART_SendData(USART1, chr);
dbgprt(“USART_SendData:0x%02x/n”,chr);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{
}
}
}
}
// USART串口初始化
void dev_USART_init(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
/* DMA1 Channel5 (triggered by USART1 Rx event) Config */ //拜见 STM32 datasheet
DMA_DeInit(DMA1_Channel5);
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&USART1->DR;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)rDMABuffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = sizeof(rDMABuffer);
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_Low;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel5, &DMA_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
//装备IO: GPIOA9和GPIOA10别离作为串口TX、RX端。 见STM32 datasheet
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA,&GPIO_InitStructure);
/* Configure USART1 */
USART_Init(USART1, &USART_InitStructure);
/* Enable USART1 DMA Rxrequest */
USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);
/* Enable DMA1 Channel5 */
DMA_Cmd(DMA1_Channel5, ENABLE);
/* Enable the USART1 */
USART_Cmd(USART1, ENABLE);
}
// 向串口发送数据
void USART_send(const int8u *pBuf, int8u len)
{
int i;
if(pBuf == NULL)
{
return;
}
// 将数据压入到发送行列
dbgprt(“USART_PushBack:”);
for(i=0;i
PushBack(*pBuf);
dbgprt(“0x%02x “,*pBuf);
pBuf++;
}
dbgprt(“/n”);
}
// 向发送行列尾部刺进一个字节
void PushBack(int8u byte)
{
USART_SndBuf[Tail++]= byte;
if(Tail >= ARRAYSIZE(USART_SndBuf))
Tail = 0;
if(Tail == Head)
Head = Tail+1;
}
// 从发送行列头部取出一个字节
bool PopFront(int8u *byte)
{
if(Head >= ARRAYSIZE(USART_SndBuf))
Head = 0;
if(Head == Tail)
return FALSE;
*byte = USART_SndBuf[Head++];
return TRUE;
}
// 处理接纳到的串口数据
void USART_receive(int8u byte)
{
// Treate received data
// Place Code here
// …
}
// CRC校验
bool CheckCRC(const int8u *str, int8u len, const int8u *crcstr)
{
int8u checkSum;
if(str == NULL || crcstr== NULL)
return FALSE;
GetCRC(str,len,&checkSum);
if(checkSum != *crcstr)
return FALSE;
else
return TRUE;
}
// 获取CRC
bool GetCRC(const int8u *str, int8u len, int8u *crcstr)
{
int8u i;
int8u checkSum;
if(str == NULL || crcstr== NULL)
return FALSE;
checkSum = *str;
for(i=1; i
checkSum ^= *(str+i);
}
*crcstr = checkSum;
return TRUE;
}