首先,
开发板使用的是
STM32l073rz-nucleo。stm32l073支持4个usart串口,1个lpsuart。当前项目需要使用4个串口。我选择了usart1、2、4、5.前三个已经能够正常的共同使用。但是usart5在使用过程中,没有任何反应。单步跟踪时,usart5的发送过程能顺利过去,但是串口助手没有任何输出。请教,这是为何呢?
配置代码如下:
stm32l0xx_it.c
void USART1_IRQHandler(void)
{
HAL_UART_IRQHandler(&huart1);
}
/* USART2的中断处理函数 */
void USART2_IRQHandler(void)
{
HAL_UART_IRQHandler(&huart2);
}
/* USART4的中断处理函数 */
void USART4_5_IRQHandler(void)
{
HAL_UART_IRQHandler(&huart4);
}
/* USART5的中断处理函数,这个中断处理函数是自己添加的,并且usart5的中断向量也是自行添加到.s文件中的。 */
void USART5_IRQHandler(void)
{
HAL_UART_IRQHandler(&huart5);
}
usart.c
/* USART init func
tion */
void MX_UART_Init(UART_HandleTypeDef* huart)
{
if (huart == &hlpuart1)
{
huart->Instance = LPUART1;
huart->Init.BaudRate = 9600;
}
else if (huart == &huart1)
{
huart->Instance = USART1;
huart->Init.BaudRate = 9600;
}
else if (huart == &huart2)
{
huart->Instance = USART2;
huart->Init.BaudRate = 9600;
}
else if (huart == &huart4)
{
huart->Instance = USART4;
huart->Init.BaudRate = 9600;
}
else if (huart == &huart5)
{
huart->Instance = USART5;
huart->Init.BaudRate = 9600;
}
huart->Init.WordLength = UART_WORDLENGTH_8B;
huart->Init.StopBits = UART_STOPBITS_1;
huart->Init.Parity = UART_PARITY_NONE;
huart->Init.Mode = UART_MODE_TX_RX;
huart->Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart->Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart->AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(huart) != HAL_OK)
{
Error_Handler();
}
}
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(uartHandle->Instance==USART1)
{
/* Peripheral clock enable */
__HAL_RCC_USART1_CLK_ENABLE();
/**USART1 GPIO Configuration
PA9 ------> USART2_TX
PA10 ------> USART2_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF4_USART1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* Peripheral interrupt init */
HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART1_IRQn);
}
else if(uartHandle->Instance==USART2)
{
/* Peripheral clock enable */
__HAL_RCC_USART2_CLK_ENABLE();
/**USART2 GPIO Configuration
PA2 ------> USART2_TX
PA3 ------> USART2_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF4_USART2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* Peripheral interrupt init */
HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART2_IRQn);
}
else if(uartHandle->Instance==USART4)
{
/* Peripheral clock enable */
__HAL_RCC_USART4_CLK_ENABLE();
/**USART4 GPIO Configuration
PC10 ------> USART4_TX
PC11 ------> USART4_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_AF6_USART4;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/* Peripheral interrupt init */
HAL_NVIC_SetPriority(USART4_5_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART4_5_IRQn);
}
else if(uartHandle->Instance==USART5)
{
/* Peripheral clock enable */
__HAL_RCC_USART5_CLK_ENABLE();
/**USART2 GPIO Configuration
:RZ LQFP64 Pin55/56
PB3 ------> USART5_TX
PB4 ------> USART5_RX
:VZ LQFP100 Pin41/42
PE10 ------> USART5_TX
PE11 ------> USART5_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF6_USART5;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* Peripheral interrupt init */
HAL_NVIC_SetPriority(USART4_5_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART4_5_IRQn);
}
}
main.c
...
MX_UART_Init(&huart1);
MX_UART_Init(&huart2);
#ifdef DEF_USART4
MX_UART_Init(&huart4);
#endif
#ifdef DEF_USART5
MX_UART_Init(&huart5);
#endif
...
while (1)
{
// 虚拟串口用的usart2,重定向
printf("
loop!
");
/* Insert 5 seconds delay */
BSP_LED_On(LED2);
HAL_Delay(1000);
/* usart1中断发送 */
if(HAL_UART_Transmit_IT(&huart1, (uint8_t*)gcTx1Buffer, TX1BUFFERSIZE)!= HAL_OK)
{
Error_Handler();
}
while (Uart1Ready != SET)
{
}
Uart1Ready = RESET;
HAL_Delay(500);
/* Checks if Buffer full indication has been set */
BSP_LED_On(LED2);
HAL_Delay(1000);
#ifdef DEF_USART4
if(HAL_UART_Transmit(&huart4, (uint8_t*)gcTx4Buffer, TX4BUFFERSIZE, 1000)!= HAL_OK)
{
Error_Handler();
}
HAL_Delay(500);
#endif
#ifdef DEF_USART5
if(HAL_UART_Transmit(&huart5, (uint8_t*)gcTx5Buffer, TX5BUFFERSIZE, 1000)!= HAL_OK)
{
Error_Handler();
}
HAL_Delay(1000);
#endif
printf("
Off!
");
//BSP_LED_Init(LED2);
//HAL_Delay(2000);
//printf("
Wake up.
");
HAL_Delay(1000);
//DisplayClkInfo();
}
....
通过查看
startup_stm32l073xx.s,
在原本保留字的位置上,增加了DCD USART5_IRQHandler,以及后文增加了:EXPORT USART5_IRQHandler [WEAK]
但是程序运行过程中,usart5应该输出的内容,从pb3、pb4连接到电脑后,串口助手没有响应。
通过关闭usart4,也没有变化。
现在我关注到usart4、5在定义中断IRQn时,定义为共用的USART4_5_IRQn(第27号)。因此,USART4的中断处理用了USART4_5_IRQn作为设置参数。而USART5_IRQHanfler也用了USART4_5_IRQn。
这里有没有矛盾?
为何不见usart5的输出,怎么解决?
谢谢指正。
如果不另外定义中断函数。4和5进同一个中断,根据什么,如何区分huart呢?
void USART4_5_IRQHandler(void)
{
HAL_UART_IRQHandler(&huart??);
}
通过分别判断2个串口的标志位来判断到底是谁触发的中断;
这个我没试过,你可以尝试一下下面写法
void USART4_5_IRQHandler(void)
{
HAL_UART_IRQHandler(&huart4);
HAL_UART_IRQHandler(&huart5);
}
一周热门 更多>