简体   繁体   English

STM32L011 无法在 rx 模式下设置 USART

[英]STM32L011 cannot set USART in rx mode

I am using STM32L011K4T6.我正在使用 STM32L011K4T6。 I want to receive a command from USART2 and do a specific action from that command.我想从 USART2 接收命令并从该命令执行特定操作。 Data that is not that command should be ignored.应该忽略不是该命令的数据。

My problem is that I don't receive any data on the USART.我的问题是我没有收到关于 USART 的任何数据。 The RxCallback funtion is never called. RxCallback 函数永远不会被调用。 No data is ever shown in the RDR register, so i'm thinking the HAL_UART_Receive_IT doesn't act like i think it does (set usart in rx mode with RX interrupt set). RDR 寄存器中从未显示任何数据,所以我认为 HAL_UART_Receive_IT 的行为不像我认为的那样(将 usart 设置为 rx 模式并设置 RX 中断)。

    UartHandle.Instance = USART2;
    UartHandle.Init.BaudRate = 9600;
    UartHandle.Init.WordLength = UART_WORDLENGTH_9B;
    UartHandle.Init.StopBits = UART_STOPBITS_1;
    UartHandle.Init.Parity = UART_PARITY_EVEN;
    UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    UartHandle.Init.Mode = UART_MODE_TX_RX;

    if (HAL_UART_DeInit(&UartHandle) != HAL_OK) {
        Error_Handler();
    }
    if (HAL_UART_Init(&UartHandle) != HAL_OK) {
        Error_Handler();
    }

//Set USART in RX mode
    if (HAL_UART_Receive_IT(&UartHandle, (uint8_t*) g_commandReceived, 2)
            != HAL_OK) {
        Error_Handler();
    }

while (1) {
    if (STATE == 0x01) {
    //Open LED, and go back to waiting for a command

       BSP_LED_On(LED3);
       memset(g_commandReceived, 0x00, sizeof(g_commandReceived));
       if (HAL_UART_Receive_IT(&UartHandle, (uint8_t*) g_commandReceived,2) != HAL_OK) {
        Error_Handler();}
      STATE = 0x00;}
}


   
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle) {
   if (UartHandle->Instance == USART2) {
        if ((g_commandReceived[0] == 0x31) && (g_commandReceived[1] == 0x32)) {
            STATE = 0x01;
    }
  }
}

I tried do send data, then receive it, and that strangely works as expected.我尝试发送数据,然后接收它,并且奇怪地按预期工作。

if (HAL_UART_Transmit_IT(&UartHandle, (uint8_t*) MyMessege, 0x08)
            != HAL_OK) {
        Error_Handler();
    }

while (UartReady != SET) {
}
UartReady = RESET;

/*Put UART peripheral in reception process ###########################*/

if (HAL_UART_Receive_IT(&UartHandle, (uint8_t *) aRxBuffer2, 8) != HAL_OK) {
Error_Handler();}

I usually work with a custom HAL package, so I don't really understand how this function work.我通常使用自定义的 HAL package,所以我不太明白这个 function 是如何工作的。

I assume what you're looking for is a way to raise an interrupt for any received message through USART.我假设您正在寻找的是一种为通过 USART 接收到的任何消息引发中断的方法。

To get interrupts working with STM HAL functions, this usually is the approach:要使中断与 STM HAL 函数一起工作,通常是这样的方法:

  1. Make sure the interrupt is enabled in the NVIC,确保在 NVIC 中启用中断,
  2. Enable the interrupt generation from the peripheral through the HAL_UART_Receive_IT ,通过HAL_UART_Receive_IT启用外设的中断生成,
  3. Write an Interrupt Service Routine whose name matches with the one defined in the startup assembly file.编写一个名称与启动程序集文件中定义的名称匹配的中断服务程序。 For USARTx (x=1,2,..) it would be USARTx_IRQHandler对于 USARTx (x=1,2,..),它将是USARTx_IRQHandler

If one of these steps is missing, then it most likely won't work.如果缺少其中一个步骤,则很可能无法正常工作。 Enabling the interrupt generation at the peripheral level ( HAL_UART_Receive_IT ) is not enough, because the NVIC will not jump to the ISR if that interrupt is not enabled at the NVIC level.在外设级别启用中断生成 ( HAL_UART_Receive_IT ) 是不够的,因为如果在 NVIC 级别未启用该中断,NVIC 将不会跳转到 ISR。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM