STM32空闲中断+DMA解决接收不定长数据问题

http://blog.csdn.net/shu_8708/article/details/73480939



串口的中断类型:

[cpp]view plain copy

  1. #define USART_IT_PE((uint16_t)0x0028)
  2. #define USART_IT_TXE((uint16_t)0x0727)
  3. #define USART_IT_TC((uint16_t)0x0626)
  4. #define USART_IT_RXNE((uint16_t)0x0525)
  5. #define USART_IT_IDLE((uint16_t)0x0424)
  6. #define USART_IT_LBD((uint16_t)0x0846)
  7. #define USART_IT_CTS((uint16_t)0x096A)
  8. #define USART_IT_ERR((uint16_t)0x0060)
  9. #define USART_IT_ORE((uint16_t)0x0360)
  10. #define USART_IT_NE((uint16_t)0x0260)
  11. #define USART_IT_FE((uint16_t)0x0160)
USART_IT_PE 奇偶错误中断
USART_IT_TXE发送中断
USART_IT_TC 传输完成中断
USART_IT_RXNE 接收中断
USART_IT_IDLE 空闲总线中断
USART_IT_LBD LIN中断检测中断
USART_IT_CTS CTS中断
USART_IT_ERR 错误中断


该程序中用到的就是串口的空闲中断:当总线是一个字节周期内没有收到数据时触发
串口的配置如下:

[cpp]view plain copy
  1. void uart_init(u32 bound){
  2. GPIO_InitTypeDef GPIO_InitStructure;
  3. USART_InitTypeDef USART_InitStructure;
  4. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);
  5. NVIC_InitTypeDef NVIC_InitStructure;
  6. //IO口相关配置
  7. USART_DeInit(USART1);
  8. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速率
  9. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  10. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  11. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  12. GPIO_Init(GPIOA, &GPIO_InitStructure);
  13. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  14. GPIO_Init(GPIOA, &GPIO_InitStructure);
  15. //串口中断配置
  16. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
  17. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  18. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;
  19. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  20. NVIC_Init(&NVIC_InitStructure);
  21. USART_InitStructure.USART_Parity = USART_Parity_No; //没有奇偶校验位
  22. USART_InitStructure.USART_BaudRate = bound; //波特率设置
  23. USART_InitStructure.USART_WordLength = USART_WordLength_8b; //8位数据位
  24. USART_InitStructure.USART_StopBits = USART_StopBits_1; //1位停止位
  25. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无硬件数据流配置
  26. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //串口收发模式
  27. USART_Init(USART1, &USART_InitStructure);
  28. USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);
  29. USART_Cmd(USART1, ENABLE);
  30. }



串口中断配置: void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
{
DMA_Cmd(DMA1_Channel5,DISABLE);
recok=1;
lenth=USART1->DR; //软件清空空闲中断标志位
lenth=USART1->SR;
lenth=32-DMA_GetCurrDataCounter(DMA1_Channel5); //获取当前接收的数据量
USART_RX_BUF[lenth]=0; //在buff最后加入空字符
DMA1_Channel5->CNDTR=32; //重新设置传输量为32
DMA_Cmd(DMA1_Channel5,ENABLE);
}
}


DMA配置:
void MYDMA_Config(DMA_Channel_TypeDef* DMA_CHx,u32 cpar,u32 cmar,u16 cndtr)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //使能DMA时钟

DMA_DeInit(DMA_CHx);
DMA1_MEM_LEN=cndtr;
DMA_InitStructure.DMA_PeripheralBaseAddr = cpar; //外设地址
DMA_InitStructure.DMA_MemoryBaseAddr = cmar; //内存地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //传输方向外设到内存
DMA_InitStructure.DMA_BufferSize = cndtr; //传输量
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_Normal; //普通模式
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //高优先级
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA_CHx, &DMA_InitStructure);
}









主函数中需要进行的配置: 【STM32空闲中断+DMA解决接收不定长数据问题】
[cpp]view plain copy
  1. MYDMA_Config(DMA1_Channel5,(u32)&USART1->DR,(u32)USART_RX_BUF,32); //传输方向设置为USART1->DR到USART_RX_BUF 传输大小为32字节
  2. USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE); //允许DMA请求
  3. DMA_Cmd(DMA1_Channel5,ENABLE);

当标志为recok位1时表示接受完成在主函数中等待recok位1就可以了

    推荐阅读