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.