时间:2021/6/9
本人正在使用 stm32f030r8t6 做开发、未遇到如上描述的问题。我使用的是 UART1 并使用 DMA 方式、我把代码贴出来、请
- void USART1_Init(uint32_t BaudRate)
- {
- GPIO_InitTypeDef GPIO_Initstructure;
- USART_InitTypeDef USART_Initstructure;
- ///
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
- ///
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
- GPIO_Initstructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_Initstructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_Initstructure.GPIO_OType = GPIO_OType_PP;
- ///
- GPIO_Initstructure.GPIO_Speed = GPIO_Speed_50MHz;
- ///<
- GPIO_Init(GPIOA,&GPIO_Initstructure);
- ///<
- GPIO_Initstructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_Initstructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_Init(GPIOA,&GPIO_Initstructure);
-
- ///
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
- USART_Initstructure.USART_BaudRate = BaudRate;
- USART_Initstructure.USART_Parity = USART_Parity_No;
- USART_Initstructure.USART_WordLength = USART_WordLength_8b;
- USART_Initstructure.USART_StopBits = USART_StopBits_1;
- USART_Initstructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
- USART_Initstructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
-
- USART_ITConfig(USART1,USART_IT_TC,DISABLE);
- USART_ITConfig(USART1,USART_IT_IDLE,ENABLE);
- ///
- USART1->ICR |= 1<<4;
- USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
-
- USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);
- USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);
- USART_Init(USART1,&USART_Initstructure);
- USART_ClearFlag(USART1,USART_FLAG_TC);
- USART_Cmd(USART1,ENABLE);
- }
- void USART1_IRQHandler(void)
- {
- uint16_t Len = 0;
- ///<谨慎处理 ORE 中断错误.
- if (USART_GetFlagStatus(USART1, USART_FLAG_PE) != RESET)
- {
- USART_ReceiveData(USART1);
- USART_ClearFlag(USART1, USART_FLAG_PE);
- }
- if (USART_GetFlagStatus(USART1, USART_FLAG_ORE) != RESET)
- {
- USART_ReceiveData(USART1);
- USART_ClearFlag(USART1, USART_FLAG_ORE);
- }
- if (USART_GetFlagStatus(USART1, USART_FLAG_FE) != RESET)
- {
- USART_ReceiveData(USART1);
- USART_ClearFlag(USART1, USART_FLAG_FE);
- }
- if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
- {
- USART_ClearFlag(USART1, USART_FLAG_RXNE);
- USART_ClearITPendingBit(USART1, USART_IT_RXNE);
- USART_ReceiveData(USART1);
- }
-
- ///<接收到1帧数据.
- if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
- {
- USART1->ICR |= 1<<4;
- DMA_Cmd(DMA1_Channel3,DISABLE);
-
- Len = UART1_RX_SIZE_MAX - DMA_GetCurrDataCounter(DMA1_Channel3);
-
- if ((0!= Len)&&(Len < UART1_RX_SIZE_MAX))
- {
- uart1_rx_buff.dat_length = Len;
- PutData(&uart1_rx_buff);
- }
- ///<
- DMA_SetCurrDataCounter(DMA1_Channel3,UART1_RX_SIZE_MAX);
- DMA_Cmd(DMA1_Channel3,ENABLE);
- }
- }
- ----------------------------分割线-------------
- void DMA1_Init(void)
- {
- DMA_InitTypeDef DMA_InitStructure;
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
-
- ///
- ///
- ///
- ///
- ///
- ///
- ///<
- ///TDR;
- ///
- ///
- ///
- ///
- ///<
- ///
- ///<
- ///
- ///
- ///<
- ///<
- ///
- ///
- DMA_DeInit(DMA1_Channel3);
- DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART1->RDR);
- DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)uart1_rx_buff.dat_content;
-
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
- DMA_InitStructure.DMA_BufferSize = UART1_RX_SIZE_MAX;
- 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_VeryHigh;
- DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
-
- DMA_Init(DMA1_Channel3,&DMA_InitStructure);
-
- DMA_ClearITPendingBit(DMA1_IT_TC3);
- DMA_ITConfig(DMA1_Channel3,DMA_IT_TC,ENABLE);
-
- DMA_Cmd(DMA1_Channel3,ENABLE);
- }
- ///<
- void uart1_tx_dma(uint8_t *dat_tx,uint16_t dat_length)
- {
- DMA_InitTypeDef DMA_InitStructure;
- if((0!=dat_length)&&(dat_length < UART1_TX_SIZE_MAX))
- {
- ///<和校验.
- dat_tx[dat_length-1] = check_sum_add8(dat_tx,dat_length-1);
- DMA_DeInit(DMA1_Channel2); //复位DMA1_Channel2.
-
- DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART1->TDR; //外设地址.
- DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)dat_tx; //内存地址.
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; //传输方向:内存 -> 外设.
- DMA_InitStructure.DMA_BufferSize = dat_length; //传输长度.
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设递增:NO.
- DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存递增:YES.
- DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
- DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //数据宽度:Byte.
- DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //循环模式:NO.
- DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; //优先级:高.
- DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //内存-内存:NO.
- DMA_Init(DMA1_Channel2, &DMA_InitStructure);
-
- ///
-
- DMA_Cmd(DMA1_Channel2, ENABLE); //使能DMA(开始传输).
-
- while(!DMA_GetFlagStatus(DMA1_IT_TC2))
- {
- }
- DMA_ClearFlag(DMA1_IT_TC2);
- }
- }
- void DMA1_Channel2_3_IRQHandler(void)
- {
- if(DMA_GetITStatus(DMA1_IT_TC2))
- {
- DMA_ClearITPendingBit(DMA1_IT_GL2);
- }
- if(DMA_GetITStatus(DMA1_IT_TC3))
- {
- DMA_ClearITPendingBit(DMA1_IT_GL3);
- DMA_Cmd(DMA1_Channel3,ENABLE);
- ///RDR)), (uint32_t)DMA_Rx_Buf, DMA_DATA_LEN);
- }
- }
复制代码
自行检查对比。
|
|
2021-6-9 08:54:27
评论
举报
|
|
|