Skip to content
Snippets Groups Projects
Commit c4f19d8a authored by Federico Mandelli's avatar Federico Mandelli
Browse files

[can-dev] Better description of the protocol using enum

parent f7ab22eb
No related branches found
No related tags found
No related merge requests found
...@@ -31,24 +31,35 @@ ...@@ -31,24 +31,35 @@
#define NPACKET 3 // equals the number of boards in the can system #define NPACKET 3 // equals the number of boards in the can system
/** /**
* @brief Struct that contains how the canId is composed * @brief enum that contains how the canId is composed
*/ */
struct CanIDMask
{
uint32_t priority = 0x1E000000;
uint32_t type = 0x1F80000;
uint32_t source = 0x78000;
uint32_t destination = 0x7800;
uint32_t idType = 0x780;
uint32_t firstPacket = 0x40;
uint32_t leftToSend = 0x3F;
} idMask;
namespace Boardcore namespace Boardcore
{ {
namespace Canbus namespace Canbus
{ {
enum IDMask
{
priority = 0x3C0000,
shiftPriority = 18, // number of bit before priority
type = 0x3F000,
shiftType = 12,
source = 0xF00,
shiftSource = 8,
destination = 0xF0,
shiftDestination = 4,
idType = 0xF,
shiftIdType = 0
};
enum SequentialInformation
{
firstPacket = 0x40,
shiftFirstPacket = 6,
leftToSend = 0x3F,
shiftLeftToSend = 0,
shiftSequentialInfo = 7
};
/** /**
* @brief Generic struct that contains a logical can packet * @brief Generic struct that contains a logical can packet
* i.e. 1 accelerometer packet 3*4byte (acc: x,y,z)+timestamp, will be 4 * i.e. 1 accelerometer packet 3*4byte (acc: x,y,z)+timestamp, will be 4
...@@ -128,8 +139,8 @@ public: ...@@ -128,8 +139,8 @@ public:
uint8_t tempLen = toSend.len - 1; uint8_t tempLen = toSend.len - 1;
uint32_t tempId = toSend.canId; uint32_t tempId = toSend.canId;
packet.ext = true; packet.ext = true;
packet.id = (tempId << 7) | idMask.firstPacket | packet.id = (tempId << shiftSequentialInfo) | firstPacket |
(63 - (tempLen & idMask.leftToSend)); (63 - (tempLen & leftToSend));
packet.length = byteForInt(toSend.payload[0]); packet.length = byteForInt(toSend.payload[0]);
for (int k = 0; k < packet.length; k++) for (int k = 0; k < packet.length; k++)
{ {
...@@ -140,8 +151,8 @@ public: ...@@ -140,8 +151,8 @@ public:
for (int i = 1; i < toSend.len; i++) for (int i = 1; i < toSend.len; i++)
{ {
tempId = toSend.canId; tempId = toSend.canId;
packet.id = (tempId << 7) | !(idMask.firstPacket) | packet.id = (tempId << shiftSequentialInfo) | !(firstPacket) |
(63 - (tempLen & idMask.leftToSend)); (63 - (tempLen & leftToSend));
packet.length = byteForInt(toSend.payload[i]); packet.length = byteForInt(toSend.payload[i]);
for (int k = 0; k < packet.length; k++) for (int k = 0; k < packet.length; k++)
{ {
...@@ -163,7 +174,8 @@ protected: ...@@ -163,7 +174,8 @@ protected:
// order the whole packet will be lost once we receive // order the whole packet will be lost once we receive
// a new first packet without warning canhandler // a new first packet without warning canhandler
{ {
uint32_t sourceId; uint32_t idNoSeq;
uint8_t sourceId;
CanPacket packet; CanPacket packet;
// Infinite loop // Infinite loop
while (true) while (true)
...@@ -173,28 +185,28 @@ protected: ...@@ -173,28 +185,28 @@ protected:
{ {
packet = can->getRXBuffer().pop().packet; packet = can->getRXBuffer().pop().packet;
sourceId = (packet.id & idMask.source) >> 15; idNoSeq = packet.id >>
shiftSequentialInfo; // discard the sequence number
sourceId = (idNoSeq & source) >> shiftSource;
if (sourceId >= 0 && if (sourceId >= 0 &&
sourceId < NPACKET) // check for maximum size sourceId < NPACKET) // check for maximum size
{ {
if (data[sourceId].canId == -1 || if (data[sourceId].canId == -1 ||
(((data[sourceId].canId << 7) & idMask.source) >> 15) == ((data[sourceId].canId & source) >> shiftSource) ==
sourceId) sourceId)
{ {
uint32_t leftToSend = uint8_t left = 63 - (packet.id & leftToSend);
63 - (packet.id & idMask.leftToSend);
if ((packet.id & idMask.firstPacket) >> if ((packet.id & firstPacket) >>
6) // it is a first shiftFirstPacket) // it is a first
// packet of a data; // packet of a data;
{ {
data[sourceId].len = left + 1;
data[sourceId].len = leftToSend + 1; data[sourceId].canId = idNoSeq;
data[sourceId].canId =
packet.id >> 7; // discard the sequence number
} }
if ((data[sourceId].len - (data[sourceId].nRec + 1)) == if ((data[sourceId].len - (data[sourceId].nRec + 1)) ==
leftToSend) left)
{ {
uint64_t tempPayload = 0; uint64_t tempPayload = 0;
...@@ -204,13 +216,14 @@ protected: ...@@ -204,13 +216,14 @@ protected:
tempPayload = tempPayload =
tempPayload | (tempData << (f * 8)); tempPayload | (tempData << (f * 8));
} }
if (data[sourceId].len - leftToSend - 1 >= 0 &&
data[sourceId].len - leftToSend - 1 < if (data[sourceId].len - left - 1 >= 0 &&
data[sourceId].len - left - 1 <
32) // check for index 32) // check for index
{ {
data[sourceId].payload[data[sourceId].len - data[sourceId]
leftToSend - 1] = .payload[data[sourceId].len - left - 1] =
tempPayload; tempPayload;
data[sourceId].nRec++; data[sourceId].nRec++;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment