UART interrupt reception issue in STM32G4
Hi,
I am working with STM32G4.I am communicating with STM32G4 with other controller through UART interrupt communication. The data is transmitting and receiving between stm32g4 and other mcu. Some times I am not receiving the data properly from other controller to STM32G4,like SOF always 0x22 and EOF 0x04 always. data bytes not coming properly as expected .
Receiver buffer always should start with 0x22 and with 0x04 but whenever I didn't received the data what I observed is I am receiving the data not in order like SOF(0x22) coming in different byte of rx_bufer .so then I can't be transmit and receive the proper data.
below added code
uint8_t ocpp_rxdata[7];
uint8_t ocpp_txdata[64];
uint8_t ocppalldata_received = 0;
int main(){
//all initializations
HAL_UART_RegisterCallback(&huart5,HAL_UART_RX_COMPLETE_CB_ID,HAL_UART_RxCpltCallback);
AL_UART_RegisterCallback(&huart5,HAL_UART_TX_COMPLETE_CB_ID,HAL_UART_TxCpltCallback);
HAL_UART_Receive_IT(&huart5, ocpp_rxdata, 7); /* USER CODE END 2 */
while(1){
request();
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
if (huart->Instance == huart5.Instance) {
alldata_received = 1;
}
}
void request()
{
if (alldata_received) {
uint16_t calculatedCRC;
uint16_t receivedCRC;
if(ocpp_rxdata[0]==0x22)
{
calculatedCRC = HAL_CRC_Calculate(&hcrc,(uint32_t *)&ocpp_rxdata, 4);
receivedCRC=ocpp_rxdata[4]<<8 | ocpp_rxdata[5];
if(calculatedCRC==receivedCRC){
switch(ocpp_rxdata[2])
{
case 0x01:
//trasmit data ocpp_txdata[0] to ocpp_txdata[18]
//ocpp_txdata[16],ocpp_txdata[17] is crc
HAL_UART_Transmit_IT(&huart5, ocpp_txdata,19);
break;
case 0x02:
//trasmit data ocpp_txdata[0] to ocpp_txdata[13]
//ocpp_txdata[11],ocpp_txdata[12] is crc
HAL_UART_Transmit_IT(&huart5, ocpp_txdata,14);
break;
}
}
else{
crc_error();
}
}
alldata_received = 0;
HAL_UART_Receive_IT(&huart5, ocpp_rxdata, 7);
}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
memset(ocpp_rxdata, '\0',7); //empty the reception data buffer
}
I changed receive buffer and transmit buffer to volatile and checked I am facing same issue and I also enabled UART in register in .ioc configuration.
Can anyone suggest what wrong in my code. Any suggestion will be helpful.
Thanks
