没有从带有中断、STM32F4、HAL 驱动程序的 UART 接收数据

No Data Receiving form UART with Interrupt, STM32F4, HAL drivers

当我通过 UART 端口从 PC(串行监视器)向 STM32F4 发现板发送请求时,将收不到信号。开发板通常应使用之前收到的相同请求(UART 镜像)进行响应。我使用中断(没有 DMA)来发送或接收消息。在中断服务程序中,已经设置了中断标志。该标志将在主循环中读取。我没有使用回调函数。

没有中断服务(主循环中只有 HAL_UART_Transmit(...) 和 HAL_UART_Receive(...))一切正常。但是我想要一个有中断的通信。

在 运行- 模式下,我在 ISR 和两个 if 语句中启用了断点。 我想知道 ISR 程序是否有问题。但是 ISR 例程可以正常工作。根据来自 PC 的请求,ISR 和 receive if 语句被调用。

问题是,接收寄存器保持为空。如果它仍然为空,控制器将不会发送请求消息。 怎么了?问题出在哪里,你能帮帮我吗? UART Port 的配置是否正确? 感谢您的帮助和支持!

volatile enum RxStates {
UART_RX, DUMMY_RX, WAIT_RX
} stateRx;

volatile enum TxStates {
    UART_TX_READY, DUMMY_TX, WAIT_TX
} stateTx;


static UART_HandleTypeDef s_UARTHandle;
GPIO_InitTypeDef GPIOC2_InitStruct;     //GPIO

uint8_t empfang[8];                     //buffer


void UART_ini(void) 
{
    __HAL_RCC_GPIOA_CLK_ENABLE();
    __USART2_CLK_ENABLE();

//PIN TX
    GPIOC2_InitStruct.Pin = GPIO_PIN_2;
    GPIOC2_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIOC2_InitStruct.Alternate = GPIO_AF7_USART2;
    GPIOC2_InitStruct.Speed = GPIO_SPEED_FAST;
    GPIOC2_InitStruct.Pull = GPIO_PULLUP;
    HAL_GPIO_Init(GPIOA, &GPIOC2_InitStruct);

//PIN RX
    GPIOC2_InitStruct.Pin = GPIO_PIN_3;
    GPIOC2_InitStruct.Mode = GPIO_MODE_AF_OD;
    HAL_GPIO_Init(GPIOA, &GPIOC2_InitStruct);

//USART2
    s_UARTHandle.Instance = USART2;
    s_UARTHandle.Init.BaudRate = 9600;
    s_UARTHandle.Init.WordLength = UART_WORDLENGTH_8B;
    s_UARTHandle.Init.StopBits = UART_STOPBITS_1;
    s_UARTHandle.Init.Parity = UART_PARITY_NONE;
    s_UARTHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    s_UARTHandle.Init.Mode = UART_MODE_TX_RX;
    HAL_UART_Init(&s_UARTHandle);

    __HAL_UART_ENABLE_IT(&s_UARTHandle, UART_IT_RXNE | UART_IT_TC);

    HAL_NVIC_SetPriority(USART2_IRQn, 15, 15);
    HAL_NVIC_EnableIRQ(USART2_IRQn);               // Enable Interrupt

}//UART


int main(int argc, char* argv[]) 
{

//initialization of the interrupt flags
    stateRx = WAIT_RX;
    stateTx = WAIT_TX;

    UART_ini(); //initialization UART

    while (1) 
    {
//receive interrupt flag
        if (stateRx == UART_RX) 
        {
            HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_SET);    //set LED
            HAL_UART_Receive(&s_UARTHandle, empfang, 2, 10000000);  //receive message

            stateRx = WAIT_RX;                                      //RESET flag
        }

//transmit interrupt flag
        if (stateTx == UART_TX_READY) 
        {
            HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_SET);    //set LED
            HAL_UART_Transmit(&s_UARTHandle, empfang, 2, 10000);    //send message

            stateTx = WAIT_TX;                                      //RESET flag
        }

//RESET LED
        if (stateTx != UART_TX_READY && stateRx != UART_RX)         
        {
            HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, RESET);           //RESET LED
        }

    }//while
}//main


void USART2_IRQHandler() 
{

    if (__HAL_UART_GET_FLAG(&s_UARTHandle, UART_FLAG_RXNE) == SET) 
    {
        __HAL_UART_CLEAR_FLAG(&s_UARTHandle, UART_FLAG_RXNE);       //clear ISR flag
        stateRx = UART_RX;                                          //set RX flag
    }

    if (__HAL_UART_GET_FLAG(&s_UARTHandle, UART_FLAG_TC) == SET) 
    {
        __HAL_UART_CLEAR_FLAG(&s_UARTHandle, UART_FLAG_TC);         //clear ISR flag
        stateTx = UART_TX_READY;                                    //set TX flag
    }
}//ISR

//EOF

查看 HAL_UART_Receive 函数,该函数等待 UART_FLAG_RXNE 标志,但您在 USART2_IRQHandler 中清除了该标志 通常,当你使用RX中断时,你必须将接收到的数据保存到用户缓冲区中,然后再解析它。