今天在调试时,遇到个棘手的问题,分享解决方法给大家,
我使用HAL库,一个让人抓狂的烂库啊,想骂娘,一直骂都不解气的烂库。
串口+DMA,官网407的DMA例程移植过来的,不需要过多说明,上代码即可
void HAL_UART_MspInit(UART_HandleTypeDef* huart)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(huart->Instance==USART3)
{
/* USER CODE BEGIN USART3_MspInit 0 */
/* USER CODE END USART3_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_USART3_CLK_ENABLE();
/**USART3 GPIO Configurati on
PB10 ------> USART3_TX
PB11 ------> USART3_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/*##-3- Configure the DMA streams ##########################################*/
/* Configure the DMA handler for Transmission process */
hdma_usart3_tx.Instance = USARTx_TX_DMA_STREAM;
hdma_usart3_tx.Init.Channel = USARTx_TX_DMA_CHANNEL;
hdma_usart3_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_usart3_tx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_usart3_tx.Init.MemInc = DMA_MINC_ENABLE;
hdma_usart3_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_usart3_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_usart3_tx.Init.Mode = DMA_NORMAL;
hdma_usart3_tx.Init.Priority = DMA_PRIORITY_LOW;
hdma_usart3_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
hdma_usart3_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
hdma_usart3_tx.Init.MemBurst = DMA_MBURST_INC4;
hdma_usart3_tx.Init.PeriphBurst = DMA_PBURST_INC4;
HAL_DMA_Init(&hdma_usart3_tx);
/* Associate the initialized DMA handle to the UART handle */
__HAL_LINKDMA(huart, hdmatx, hdma_usart3_tx);
/* Associate the initialized DMA handle to the the UART handle */
__HAL_LINKDMA(huart, hdmarx, hdma_usart3_rx);
/*##-4- Configure the NVIC for DMA #########################################*/
/* NVIC configuration for DMA transfer complete interrupt (USARTx_TX) */
HAL_NVIC_SetPriority(DMA1_Stream3_IRQn, 0, 1);
HAL_NVIC_EnableIRQ(DMA1_Stream3_IRQn);
/* USER CODE BEGIN USART3_MspInit 1 */
HAL_NVIC_SetPriority(USART3_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART3_IRQn);
/* USER CODE END USART3_MspInit 1 */
}
}
/* USART3 init function */
static void MX_USART3_UART_Init(void)
{
huart3.Instance = USART3;
huart3.Init.BaudRate = 115200*2;
huart3.Init.WordLength = UART_WORDLENGTH_8B;
huart3.Init.StopBits = UART_STOPBITS_1;
huart3.Init.Parity = UART_PARITY_NONE;
huart3.Init.Mode = UART_MODE_TX_RX;
huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart3.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart3) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
以上是初始化代码
使用的DMA发送,中断接收,
串口是和4G模块通讯,具体哪个厂家的就不说了,只能说很low。
在配置模块时,出现了如图所示,
再次上电没有错误了,就接上f4的串口,开始调试,顿时来个下马威啊,一直在反复进入串口中断,单步跟踪,先是 Framing error,后来一直是 Noise detected flag,Overrun error这2个错误,相当于进入死循环了,急得我直骂娘啊,百度一下,基本没有收获,能知道是硬件问题,就上了示波器,果然是波形出问题了,f4的串口是使用串口助手调试了N遍的,肯定没问题,只能把4G模块的波特率降低一个档次,因为之前使用115200(出厂默认值)一直没出问题,果然是没问题,又再次把波特率升到230400,直到在调试软件上没有任何错误,反复测试都OK,才放心大胆的使用,果然,一切正常了,权当发个水贴,给大家分享一下经验了。
此内容由EEWORLD论坛网友star_66666 原创,如需转载或用于商业用途需征得作者同意并注明出处