1
- // from address 8bits (A)
2
- // to address 8 bits (B)
3
- // current part number 4 bits (C)
4
- // total part count 4 bits (D)
5
- // 3 bits message_id (E)
6
- // 1 bit require ack (F)
7
- // 1 bit is ack (G)
8
- // 1 bit is extended frame (H). (FIXED)
9
- // 1 bit RTR (Remote Transmission Request) (I) (FIXED)
10
- // 1 bit SRR (Substitute Remote Request) (J) (FIXED)
11
- // header model (32 bits)
12
- // HIJG FEEE DDDD CCCC BBBB BBBB AAAA AAAA
13
-
14
1
#include " hal/transport/CAN/driver/mcp_can.h"
15
2
#include " hal/transport/CAN/driver/mcp_can.cpp"
16
3
#include " MyTransportCAN.h"
@@ -27,6 +14,8 @@ long unsigned int rxId;
27
14
unsigned char len = 0 ;
28
15
unsigned char rxBuf[8 ];
29
16
unsigned char _nodeId;
17
+
18
+ // message id updated for every outgoing mesage
30
19
uint8_t message_id =0 ;
31
20
32
21
// buffer element
@@ -41,6 +30,7 @@ typedef struct {
41
30
bool ready;
42
31
} CAN_Packet;
43
32
33
+ // buffer
44
34
CAN_Packet packets[CAN_BUF_SIZE];
45
35
46
36
// filter incoming messages (MCP2515 feature)
@@ -131,6 +121,32 @@ uint8_t _findCanPacketSlot(long unsigned int from,long unsigned int currentPart,
131
121
return slot;
132
122
}
133
123
124
+ // from address 8bits (A)
125
+ // to address 8 bits (B)
126
+ // current part number 4 bits (C)
127
+ // total part count 4 bits (D)
128
+ // 3 bits message_id (E)
129
+ // 1 bit require ack (F)
130
+ // 1 bit is ack (G)
131
+ // 1 bit is extended frame (H). (FIXED)
132
+ // 1 bit RTR (Remote Transmission Request) (I) (FIXED)
133
+ // 1 bit SRR (Substitute Remote Request) (J) (FIXED)
134
+ // header model (32 bits)
135
+ // HIJG FEEE DDDD CCCC BBBB BBBB AAAA AAAA
136
+ long unsigned int _buildHeader (uint8_t messageId, uint8_t totalPartCount, uint8_t currentPartNumber,uint8_t toAddress,uint8_t fromAddress){
137
+ long unsigned int header=0x80 ; // set H=1 (FIXED), I=0 (FIXED), J=0 (FIXED), G=0 (To be implemented), F=0 (To be implemented)
138
+ header+=(messageId & 0x07 ); // set messageId
139
+ header=header << 4 ;
140
+ header+=(totalPartCount & 0x0F );// set total part count
141
+ header=header << 4 ;
142
+ header+=(currentPartNumber & 0x0F );// set current part number
143
+ header=header << 8 ;
144
+ header+=toAddress;// set destination address
145
+ header=header << 8 ;
146
+ header+=fromAddress;// set source address
147
+ CAN_DEBUG (PSTR (" CAN:SND:CANH=%" PRIu32 " ,ID=%" PRIu8 " ,TOTAL=%" PRIu8" ,CURR=%" PRIu8" ,TO=%" PRIu8" ,FROM=%" PRIu8" \n " ), header, messageId, totalPartCount,currentPartNumber,toAddress,fromAddress);
148
+ return header;
149
+ }
134
150
bool transportSend (const uint8_t to, const void * data, const uint8_t len, const bool noACK)
135
151
{
136
152
(void )noACK; // some ack is provided by CAN itself. TODO implement application layer ack.
@@ -140,50 +156,32 @@ bool transportSend(const uint8_t to, const void* data, const uint8_t len, const
140
156
if (len % 8 != 0 ) {
141
157
noOfFrames++;
142
158
}
143
- // set left most bit to 1 as this indicates extended frame
144
- uint8_t h1 = 0x80 ;
145
- // increment msg_id for new message.
146
- h1+=message_id;
159
+ // update message_id
147
160
message_id++;
148
- message_id=(message_id & 0x07 );
161
+ // make sure message_id isn't longer than 3 bits.
162
+ message_id = message_id & 0x07 ;
149
163
150
- // set total number of frames
151
- uint8_t h2 = noOfFrames;
152
- // shift left to create space for current frame number
153
- h2=h2 << 4 ;
154
- uint8_t i = 0 ;
155
164
CAN_DEBUG (PSTR (" CAN:SND:LN=%" PRIu8 " ,NOF=%" PRIu8 " \n " ), len, noOfFrames);
156
-
157
- for (i = 0 ; i < noOfFrames; i++) {
158
- uint32_t canId = h1;
159
- canId=canId << 8 ;
160
- // reset current frame number
161
- h2 = h2 & 0xF0 ;
162
- h2 += i;
163
- canId += h2;
164
- canId=canId << 8 ;
165
- canId += to;
166
- canId=canId << 8 ;
167
- canId += _nodeId;
165
+ uint8_t currentFrame;
166
+ for (currentFrame = 0 ; currentFrame < noOfFrames; currentFrame++) {
168
167
uint8_t partLen;
169
168
if (len<=8 ){
170
169
partLen=len;
171
- } else if (i * 8 <= len) {
170
+ } else if (currentFrame * 8 <= len) {
172
171
partLen = 8 ;
173
172
} else {
174
173
partLen = len % 8 ;
175
174
}
176
175
uint8_t buff[8 ];
177
176
uint8_t j=0 ;
178
- // memcpy(buff,datap[i*8+j],partlen );
177
+ // memcpy(buff,datap[currentFrame*8],partLen );
179
178
for (j = 0 ; j < partLen; j++) {
180
- buff[j]=datap[i *8 +j];
179
+ buff[j]=datap[currentFrame *8 +j];
181
180
}
182
- CAN_DEBUG (PSTR (" CAN:SND:LN=%" PRIu8 " ,DTA0=%" PRIu8 " ,DTA1=%" PRIu8 " ,DTA2=%" PRIu8 " ,DTA3=%" PRIu8 " ,DTA4=%" PRIu8 " ,DTA5=%" PRIu8 " ,DTA6=%" PRIu8 " ,DTA7=%" PRIu8 " \n " ), partLen, buff[0 ], buff[1 ], buff[2 ],buff[3 ],buff[4 ],buff[5 ],buff[6 ],buff[7 ]);
183
181
184
- CAN_DEBUG (PSTR (" CAN:SND:LN=%" PRIu8 " ,CANH =%" PRIu32 " \n " ), partLen, canId );
182
+ CAN_DEBUG (PSTR (" CAN:SND:LN=%" PRIu8 " ,DTA0 =%" PRIu8 " ,DTA1=% " PRIu8 " ,DTA2=% " PRIu8 " ,DTA3=% " PRIu8 " ,DTA4=% " PRIu8 " ,DTA5=% " PRIu8 " ,DTA6=% " PRIu8 " ,DTA7=% " PRIu8 " \n " ), partLen, buff[ 0 ], buff[ 1 ], buff[ 2 ],buff[ 3 ],buff[ 4 ],buff[ 5 ],buff[ 6 ],buff[ 7 ] );
185
183
186
- byte sndStat = CAN0.sendMsgBuf (canId , partLen, buff);
184
+ byte sndStat = CAN0.sendMsgBuf (_buildHeader (message_id, noOfFrames, currentFrame, to, _nodeId) , partLen, buff);
187
185
if (sndStat == CAN_OK) {
188
186
CAN_DEBUG (PSTR (" CAN:SND:OK\n " ));
189
187
return true ;
@@ -203,7 +201,7 @@ bool transportDataAvailable(void)
203
201
long unsigned int currentPart=(rxId & 0x000F0000 )>>16 ;
204
202
long unsigned int totalPartCount=(rxId & 0x00F00000 )>>20 ;
205
203
long unsigned int messageId=(rxId & 0x07000000 )>>24 ;
206
- CAN_DEBUG (PSTR (" CAN:RCV:CANH=%" PRIu32 " ,FROM =%" PRIu32 " ,TO =%" PRIu32 " ,CURR=%" PRIu32 " ,TOTAL =%" PRIu32 " ,ID =%" PRIu32 " \n " ), rxId, from, to, currentPart,totalPartCount,messageId );
204
+ CAN_DEBUG (PSTR (" CAN:RCV:CANH=%" PRIu32 " ,ID =%" PRIu32 " ,TOTAL =%" PRIu32" ,CURR=%" PRIu32" ,TO =%" PRIu32" ,FROM =%" PRIu32" \n " ), rxId, messageId, totalPartCount, currentPart,to,from );
207
205
208
206
uint8_t slot;
209
207
if (currentPart==0 ){
0 commit comments