1
votes

I am using STM32L011K4T6. I want to receive a command from USART2 and do a specific action from that command. Data that is not that command should be ignored.

My problem is that I don't receive any data on the USART. The RxCallback funtion is never called. 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).

    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.

1

1 Answers

0
votes

I assume what you're looking for is a way to raise an interrupt for any received message through USART.

To get interrupts working with STM HAL functions, this usually is the approach:

  1. Make sure the interrupt is enabled in the NVIC,
  2. Enable the interrupt generation from the peripheral through the 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

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.