Hardware CAN, corrupted frames

Post Reply
Phono
Posts: 92
Joined: Tue May 05, 2015 6:08 am
Location: Ermont, France

Hardware CAN, corrupted frames

Post by Phono » Tue Jul 04, 2017 7:58 pm

Hi,
for those who have tested the HardwareCAN library I submitted (see Library Request sub-forum), I would like to know whether they have experienced corrupted received frames.
It's been a couple years I use this library in a personal system which has 7 CAN nodes, and everything works well, except for occasionnal corruption of received frames.
I have spend quite a long time thinking about the possible causes, but since I use the Arduino IDE, no debugging is available to me, so I have only limited means to investigate. What i know for sure is that all the frames that the system is expected to produce are actually sent on the bus, according to the CAN bus analyzer I have connected to it. But once in a while, a frame correctly transmitted is incorrectly decoded by my code, though the code is straightforward. It just cuts the received frame in pieces and copies the received bytes to the appropriate global variables my application uses. As far as I could see, any received frame (they are about a dozen different frames for various purposes, sent at paces ranging from 100 ms to 1s) is likely to be corrupted. This is of course only a guess, as I cannot see the internal values of the variables, but I very clearly see the effect on the system of the values that are unexpected, whereas the frames that I can see directly on the bus are correct.
I tried to figure out the causes by carfully reading the code of the library, for example an interrupt fired at the wrong time or a multitasking side-effect (I use FreeRTOS), but so far I could not find any flaw.
I am very close to my wit's end, and any help would be appreciated.
Jean-Marc

zmemw16
Posts: 1490
Joined: Wed Jul 08, 2015 2:09 pm
Location: St Annes, Lancs,UK

Re: Hardware CAN, corrupted frames

Post by zmemw16 » Tue Jul 04, 2017 8:32 pm

It just cuts the received frame in pieces and copies the received bytes to the appropriate global variables
that sounds like the place it's copying from is incorrect, using pointers? the length is wrong?
does the cutting involve conditionals, case or if then else etc ?
you could perhaps preset all globals to unique values.
could you 'insert' a frame repeatedly with unique values, see where they get to?
stephen

Phono
Posts: 92
Joined: Tue May 05, 2015 6:08 am
Location: Ermont, France

Re: Hardware CAN, corrupted frames

Post by Phono » Tue Jul 04, 2017 9:24 pm

Here is an excerpt of the code where most of the problems are noticeable

Code: Select all

void Acquisition(void)
{
  int16 Pr = 0 ;
  int i ;
  
  CanMsg *r_msg;

  while ((r_msg = canBus.recv()) != NULL)
  {
    switch ( r_msg->ID )
    {
      case BMS_SOC_ID :
        TempsSilenceBMS = 0 ;              // Remise à zéro du compteur à chaque trame reçue
        CANErrorBMS = false ;
        Pr = r_msg->Data[0] << 8 ;        // les trames J1939 sont big endian
        Pr |= r_msg->Data[1] ;
        SOC = Pr / 100 ;                      // Le SOC est en 1/10000
        break ;
        
      case BMS_DISJ_ID :
       DisjoncteurFerme = ( r_msg->Data[0] & 2 ) != 0 ;
       break ;
       
    }
    
    canBus.free();    // Détruit le message lu du driver
    vTaskDelay ( 1 ) ;
  }
}
I have omitted a few other case statements that are less interesting.
In this example, the variable SOC occasionnally gets a wrong value (should be between 0 to 100%, but I sometimes get 241% !)
Please note the second case, where I never observed corruption of the variable DisjoncteurFerme.
In most cases, the identifier is not even recognized, and I get a timeout whereas I have logged the frames on the bus and none is missing when the timeout is detected! (The variables TempsSilenceBMS and CANErrorBMS are involved in the timeout mechanism).

victor_pv
Posts: 1748
Joined: Mon Apr 27, 2015 12:12 pm

Re: Hardware CAN, corrupted frames

Post by victor_pv » Wed Jul 05, 2017 3:40 pm

About debugging, you can use the eclipse plugin to compile and debug (Sloeber). I have been using it for compiling and very happy with it.
There is a thread from Pito with more information on the debugging part. I normally use Ozone for debugging though because I got used to it before learning how to use the debugger in Eclipse.
Ozone is for J-links, but the Eclipse debugger should work fine with stlink.

You can also compile with arduino, but use the .elf file with a debugger to debug. I use Ozone from Segger with my jlink, but there is also GDB. And I believe the eclipse debugger can also probably open the elf generated with the arduino ide without any problem since the compiler is the still gcc.

Post Reply