问题描述
问题复现平台: STM324xG-Eval
例程路径:
STM32Cube_FW_F4_V1.15.0\Projects\STM324xG_EVAL\Applications\USB_Device\CDC_Standalone
在对 CDC 例程了解后, 参考已有的 UART 应用, 新增了一路 UART。 对应初始化程序如下所示。 其中
USARTx 为例程原有部分,而 USARTy 为新增的一路 UART 初始化部分。
void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
static DMA_HandleTypeDef hdma_tx;
GPIO_InitTypeDef GPIO_InitStruct;
if(huart->Instance == USARTx )
{
/*##-1- Enable peripherals and GPIO Clocks #################################*/
/* Enable GPIO clock */
USARTx_TX_GPIO_CLK_ENABLE();
USARTx_RX_GPIO_CLK_ENABLE();
/* Enable USARTx clock */
USARTx_CLK_ENABLE();
/* Enable DMAx clock */
DMAx_CLK_ENABLE();
/*##-2- Configure peripheral GPIO ##########################################*/
/* UART TX GPIO pin configuration */
GPIO_InitStruct.Pin = USARTx_TX_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
GPIO_InitStruct.Alternate = USARTx_TX_AF;
HAL_GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStruct);
/* UART RX GPIO pin configuration */
GPIO_InitStruct.Pin = USARTx_RX_PIN;
GPIO_InitStruct.Alternate = USARTx_RX_AF;
HAL_GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStruct);
/*##-3- Configure the NVIC for UART ########################################*/
HAL_NVIC_SetPriority(USARTx_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(USARTx_IRQn);
/*##-4- Configure the DMA streams ##########################################*/
/* Configure the DMA handler for Transmission process */
hdma_tx.Instance = USARTx_TX_DMA_STREAM;
hdma_tx.Init.Channel = USARTx_TX_DMA_CHANNEL;
hdma_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_tx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_tx.Init.MemInc = DMA_MINC_ENABLE;
hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_tx.Init.Mode = DMA_NORMAL;
hdma_tx.Init.Priority = DMA_PRIORITY_LOW;
hdma_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
hdma_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
hdma_tx.Init.MemBurst = DMA_MBURST_INC4;
hdma_tx.Init.PeriphBurst = DMA_PBURST_INC4;
HAL_DMA_Init(&hdma_tx);
/* Associate the initialized DMA handle to the UART handle */
__HAL_LINKDMA(huart, hdmatx, hdma_tx);
/*##-5- Configure the NVIC for DMA #########################################*/
/* NVIC configuration for DMA transfer complete interrupt (USARTx_TX) */
HAL_NVIC_SetPriority(USARTx_DMA_TX_IRQn, 6, 0);
HAL_NVIC_EnableIRQ(USARTx_DMA_TX_IRQn);
}
if(huart->Instance == USARTy )
{
/*##-1- Enable peripherals and GPIO Clocks #################################*/
/* Enable GPIO clock */
USARTy_TX_GPIO_CLK_ENABLE();
USARTy_RX_GPIO_CLK_ENABLE();
/* Enable USARTx clock */
USARTy_CLK_ENABLE();
/* Enable DMAx clock */
DMAy_CLK_ENABLE();
/*##-2- Configure peripheral GPIO ##########################################*/
/* UART TX GPIO pin configuration */
GPIO_InitStruct.Pin = USARTy_TX_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
GPIO_InitStruct.Alternate = USARTy_TX_AF;
HAL_GPIO_Init(USARTy_TX_GPIO_PORT, &GPIO_InitStruct);
/* UART RX GPIO pin configuration */
GPIO_InitStruct.Pin = USARTy_RX_PIN;
GPIO_InitStruct.Alternate = USARTy_RX_AF;
HAL_GPIO_Init(USARTy_RX_GPIO_PORT, &GPIO_InitStruct);
/*##-3- Configure the NVIC for UART ########################################*/
HAL_NVIC_SetPriority(USARTy_IRQn, 7, 0);
HAL_NVIC_EnableIRQ(USARTy_IRQn);
/*##-4- Configure the DMA streams ##########################################*/
/* Configure the DMA handler for Transmission process */
hdma_tx.Instance = USARTy_TX_DMA_STREAM;
hdma_tx.Init.Channel = USARTy_TX_DMA_CHANNEL;
hdma_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_tx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_tx.Init.MemInc = DMA_MINC_ENABLE;
hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_tx.Init.Mode = DMA_NORMAL;
hdma_tx.Init.Priority = DMA_PRIORITY_LOW;
hdma_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
hdma_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
hdma_tx.Init.MemBurst = DMA_MBURST_INC4;
hdma_tx.Init.PeriphBurst = DMA_PBURST_INC4;
HAL_DMA_Init(&hdma_tx);
/* Associate the initialized DMA handle to the UART handle */
__HAL_LINKDMA(huart, hdmatx, hdma_tx);
/*##-5- Configure the NVIC for DMA #########################################*/
/* NVIC configuration for DMA transfer complete interrupt (USARTx_TX) */
HAL_NVIC_SetPriority(USARTy_DMA_TX_IRQn, 8, 0);
HAL_NVIC_EnableIRQ(USARTy_DMA_TX_IRQn);
} …
}
在
之前的 UART 发送和接收处, 同样新增了一路 UART 的发送和接收。 为了方便描述, 这里不对应用
层面进行描述。 而是直接在例程中, 时钟配置后执行下述语句, 复现问题。
#ifdef USART_X_INIT_ENABLE
UartHandle.Instance = USARTx;
…
if(HAL_UART_Init(&UartHandle) != HAL_OK)
…
if(HAL_UART_Receive_IT(&UartHandle, (uint8_t *)UserTxBuffer, 1) != HAL_OK)
…
#endif // USART_X_INIT_ENABLE
#ifdef USART_Y_INIT_ENABLE
UartHandley.Instance = USARTy;
…
if(HAL_UART_Init(&UartHandley) != HAL_OK)
…
if(HAL_UART_Receive_IT(&UartHandley, (uint8_t *)UserTxBuffer, 1) != HAL_OK)
…
#endif //USART_Y_INIT_ENABLE
while(1)
{
#ifdef USART_X_ENABLE
HAL_UART_Transmit_DMA(&UartHandle, “UARTx Test”, 11);
#endif //USART_X_ENABLE
#ifdef USART_Y_ENABLE
HAL_UART_Transmit_DMA(&UartHandley,”UARTy Test”, 11);
#endif // USART_Y_ENABLE
HAL_Delay(100);
}
结果如下表。
|