STM32L073,多个uart,但是调试接收中断时,只开启了uart2和uart4,都进不了中断.请网友指正,我到底犯了什么高级的还是低级的错误?直接上代码吧:初始化:
void MX_UART_Init(UART_HandleTypeDef* huart, uint32_t BaudRate)
{
if (huart == &huart2)
{
huart->Instance = USART2;
__HAL_RCC_GPIOA_CLK_ENABLE();
}
else if (huart == &huart4)
{
huart->Instance = USART4;
__HAL_RCC_GPIOC_CLK_ENABLE();
}
huart->Init.BaudRate = BaudRate;
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==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);
}
}
中断服务程序:
void USART2_IRQHandler(void)
{
HAL_UART_IRQHandler(&huart2);
}
/* USART4/5的中断处理函数 */
void USART4_5_IRQHandler(void)
{
HAL_UART_IRQHandler(&huart4);
HAL_UART_IRQHandler(&huart5);
}
回调函数:
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
{
unsigned char data = 0;
/* Read Received character. RXNE flag is cleared by reading of RDR register */
data = LL_USART_ReceiveData8(UartHandle->Instance);
if (UartHandle == &huart2)
{
Uart2Ready = SET;
ProtocolRecvBuff2.Revbuff[ProtocolRecvBuff2.phead] = data;
ProtocolRecvBuff2.phead++;
if ( ProtocolRecvBuff2.phead >= PROTOCOLMAXRECVBUFF )
{
ProtocolRecvBuff2.phead = 0;
ProtocolRecvBuff2.ptail = 0;
}
while (HAL_UART_GetState(&huart2) != HAL_UART_STATE_READY);
HAL_UART_Receive_IT(&huart2, ProtocolRecvBuff2.Revbuff, 1);
}
else if (UartHandle == &huart4)
{
Uart4Ready = SET;
ProtocolRecvBuff1.Revbuff[ProtocolRecvBuff1.phead] = data;
ProtocolRecvBuff1.phead++;
if ( ProtocolRecvBuff1.phead >= PROTOCOLMAXRECVBUFF )
{
ProtocolRecvBuff1.phead = 0;
ProtocolRecvBuff1.ptail = 0;
}
/* 处理完上一次的接收中断后,需要再次打开中断。 */
while (HAL_UART_GetState(&huart4) != HAL_UART_STATE_READY);
HAL_UART_Receive_IT(&huart4, &(ProtocolRecvBuff1.Revbuff[ProtocolRecvBuff1.phead]), 1);
}
}
main()调用:
int main(void)
{
HAL_Init();
SystemClock_Config();
SystemPower_Config();
MX_UART_Init(&huart2, ComBaudRate_9600);
MX_UART_Init(&huart4, ComBaudRate_9600);
if(HAL_UART_Receive_IT(&huart2, ProtocolRecvBuff2.Revbuff, 1) != HAL_OK)
{
Error_Handler();
}
if(HAL_UART_Receive_IT(&huart4, ProtocolRecvBuff1.Revbuff, 1) != HAL_OK)
{
Error_Handler();
}
while ( 1U )
{
HAL_Delay(1000);
printf("GPRS_POWER LOOP.
");
}
}
int main(void)整个工程已经调试多天了,卡在了接收这个地方,很是郁闷。也很疲了,一直没找到这个问题的原因在哪里。。。希望
论坛里各位网友支持一下,非常感谢。
最新进展:上述的代码其实是可以进中断的。但中断不可控,上机测试在上电初始化过程中就莫名其妙进去好多次,UART_IT_FE,可能出不来。也可能不响应RX的输入信号(通过串口调试助手发送的数据)。。
谢谢这位兄弟保持关注。。按照原理来说,输入输出配成对应的方式,肯定是没有问题的。。
但是,对你的回复,今天我特意测试了几遍。
当我把GPIO都按照AF_PP配置时,程序跑起来,UART4收发是正常的。调试信息能清楚看到远端串口返回的数据。
当我TX与RX分别配置成AF_PP、INPUT时,调试信息不见任何返回。等有时间我再分析是不是卡在中断里了。
我的板子没再出现上电就进串口中断或者其他错误的原因,可能重要的一点在电源。。之前我一直电池供电,可能电压不稳,现在换成稳压电源。。
除了上面的模式配置,我把使能uart中断,写进了uart的init中和RX的callback中。
最后,我把库函数UART_Receive_IT()替换成自己的处理函数了。
一周热门 更多>