What kind of data structure to hold a complete CAN frame?

The simple datatypes based on multiples of 8 bits don't map well to something like this which has a head and tail containing fields of various lengths. (I think I can ignore the stuff and ACK bits as I assume they are added and removed by the CAN controller.)

I’m not sure what you are trying to say here .
Have a look at some of the examples provided with libraries for CAN breakouts ( eg sparkfun )

A struct with bitfields will probably do.

struct CANFRAME
{
  uint64_t SOF:1;
  uint64_t arbitration: 13;
  uint64_t control: 7:
  uint64_t data: 9;
  uint64_t crc: 16;
  ...
  ...
};

In case you don't know, the numbers after the colons indicate the number of bits used by a field.

I'm not familiar with CAN bus; this is purely based on your image. Fields with stuffing bits will contain the stuffing but; you will have to do some shifting.

That's new for me.
Do bitSet() and bitRead() work OK with variables declared this way?

Declare a variable of type CANFRAME.

CANFRAME frame;
...
...
// test
if(frame.SOF == 1)

// assign value
frame.SOF =1;
frame.data = 123;

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.