STM32串口+DMA使用
STM32有5个串口资源(USART1,USART2,USART3及UART4,UART5)。其中3个USART(通用同步/异步收/发器universalsynchronous asynchronous receiver and transmitter);2个UART(通用异步收/发器universalasynchronous receiver and transmitter);至于USART与UART的区别,如果只是拿来做串口用,没什么区别,在车载项目里,我们拿来做串口用,USART与UART在编程上并没有区别。
其中USART1,USART2,USART3,UART4支持DMA方式,UART5不支持DMA。(详见数据手册stm32f105&107_datesheet_English的P18/2.3.17)。
DMA(Derect MemoryAcess直接存储器存取),STM32有2个DMA,DMA1有7个通道,DMA2有5和通道,每个通道对应不同的外设(详见数据手册P272/13.3.7)。
1.时钟RCC配置:
串口时钟 + DMA时钟 + IO时钟
staticvoid RCC_Configuration(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE); //串口时钟
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2,ENABLE); //DMA2时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC| RCC_APB2Periph_AFIO, ENABLE);//IO时钟
}
2.GPIO配置:
UART4的TX为PC10脚,发送端配置为复用推挽输出模式(GPIO_Mode_AF_PP)
UART4的RX为PC11脚,接收端配置为浮空输入模式(GPIO_Mode_IN_FLOATING)
staticvoid GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP; //TX复用推挽输出模式
GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode &