cancel
Showing results for 
Search instead for 
Did you mean: 

CAN Reception Data Integrity

STUser34
Associate II

I am facing the problem of data integrity, the logic is something as shown below i am taking the CAN rx data from the interrupt and processing in while loop.

 

uint8_t g_candata_u8=0;
int main(void)
{
 Initilializations();
 while()
  {
    if(CanMessageReceived == TRUE)
    {
      LCDDisplay(g_candata_u8);
    }
}

void CanRxInterrupt()
{
  g_candata_u8 = RxBuffer[0];
}

 

The problem i am facing is the g_candata_u8 is getting corrupted. The value g_candata_u8 which is acclerator pedal percentage from 0 - 100% received from vehicle shall be less than 100, but the value displayed on the LCD is sometimes shows greater than 200, most of the time it is less than 100 and as expected. 

I am suspecting the above method of code implementation is a problem. What is the best method to process the rx data?

1 ACCEPTED SOLUTION

Accepted Solutions

https://embedjournal.com/implementing-circular-buffer-embedded-c/

https://embeddedartistry.com/blog/2017/05/17/creating-a-circular-buffer-in-c-and-c/

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

View solution in original post

5 REPLIES 5
SofLit
ST Employee

Hello,

I'm sure it's not a CAN transmission issue as CRC is computed with the protocol at HW level. So if there is a data integrity issue you will get CRC error. Do you have a such error (HAL_CAN_ERROR_CRC)? 

If not check the raw data sent by the other node.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

I think i have not explained the problem clearly enough.

Suppose if i receive the CAN data as 90 from the vehicle.

The global variable g_candata_u8 is updated in the receive interrupt as 90 from the CAN rx buffer, this is not a problem.

But when i am trying to use this value in while for LCD display i have statements in the while loop in main 

strcpy(l_dest_u8a,"ACC: ");
sprintf(l_result_u8a, "%d", g_candata_u8);
strcat(l_result_u8a,"       ");
strcat(l_dest_u8a,l_result_u8a);
LCDDisplay(SECONDLINE,l_dest_u8a);
g_accpedalpercentagedata_u8 = FALSE;

I have several lines of code for updating the LCD using the variable g_candata_u8 using sprintf. So my doubt is while updating the variable suppose the Can Rx interrupt happens again then the variable g_candata_u8 will be updated with new value as this global variable. Is this possible? Because i see sometimes some random values on LCD.

Your comment doesn't deny my previous one and again this is not a data integrity of the CAN communication. 

You may have a race condition issue when the frame is received and the reading of the data to the LCD mostly when you get a burst of frames. I think you need to implement a ring buffer to receive and display the CAN frame on your LCD.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
STUser34
Associate II

Yes surely the naming is wrong it is not a data integrity of the CAN communication, it is my handling of data. Any example available for ring buffer implementation, it will help me.

 

https://embedjournal.com/implementing-circular-buffer-embedded-c/

https://embeddedartistry.com/blog/2017/05/17/creating-a-circular-buffer-in-c-and-c/

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.