forked from afterhoursengineering/_CANcrusher_ARM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ccom.cpp
387 lines (326 loc) · 11.6 KB
/
ccom.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/******************************************************************************
** ccom.cpp
** Copyright (C) 2015 David Burke (david@afterhoursengineering.net)
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
/*---------------------------------------------------------------------------*/
/* System Includes */
/*---------------------------------------------------------------------------*/
#include "Arduino.h"
#include "usb_rawhid.h"
#include "usb_seremu.h"
#include "HardwareSerial.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "ccom.h"
/*---------------------------------------------------------------------------*/
/* Constant declarations */
/*---------------------------------------------------------------------------*/
#define BT Serial2
#define USB Serial
#define MAX_FRAME_SIZE 64u /* TX frames are sent in 64 byte packets */
#define MAX_BUFFER_SIZE 8u /* 8 buffers of 64 bytes */
#define RBUF_SIZE MAX_BUFFER_SIZE
#define ID00_IO_ONLY 0
#define ID00_CAN_MSG1_AND_IO 1u
#define ID00_CAN_MSG1_AND_2_AND_IO 2u
/** To send A55A as the header, need to store the data in little endian format
* since the struct variable for the header is a uint16_t and all data is
* stored (and therefore sent) in little endian format as well.
*/
#define ID00_HEADER (uint16_t)0x5AA5u
#define ID00 (uint8_t)0x00
/*---------------------------------------------------------------------------*/
/* Type declarations */
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/* Structure declarations */
/*---------------------------------------------------------------------------*/
/**
* Structure defines a typedef for a FIFO ring buffer. Structure contains the
* pointer buffer of size RBUF_SIZE, the index of the (head) and (tail), and the
* total number of 32-bit addresses currently being used in the buffer (count).
*/
typedef struct ringBufS
{
uint32_t au32Addr[RBUF_SIZE]; /* 32bit address buffer of size RBUF_SIZE */
int16_t head; /* The index of the newest data in the buffer */
int16_t tail; /* The index of the oldest data in the buffer */
int16_t count; /* The total # of addresses currently used by the buffer */
} T_RING_BUF;
/*---------------------------------------------------------------------------*/
/* Variable declarations */
/*---------------------------------------------------------------------------*/
static uint8_t au8Data[MAX_BUFFER_SIZE][MAX_FRAME_SIZE];
static T_RING_BUF ccomBuf;
/*---------------------------------------------------------------------------*/
/* Macro declarations */
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/* Function prototypes */
/*---------------------------------------------------------------------------*/
bool USB_isReady(void);
void LoadOutput(uint8_t *inBuf, uint8_t *outBuf, uint16_t len);
void Clear_ID00_Packet(T_CCOM_ID00 * id00Packet);
uint8_t Checksum_ID00_Packet(T_CCOM_ID00 * id00Packet);
uint16_t modulo_inc (const uint16_t value, const uint16_t modulus);
void rbuf_Init (T_RING_BUF *_this, uint8_t * initAddress, uint16_t u16ArrayLen);
uint8_t rbuf_empty (T_RING_BUF *_this);
uint8_t rbuf_full (T_RING_BUF *_this);
uint8_t rbuf_get (T_RING_BUF *_this, uint32_t * pu32Val);
void rbuf_put (T_RING_BUF *_this, uint32_t * pu32Val);
void rbuf_flush (T_RING_BUF *_this);
/**
* \brief Load an output buffer with the bytes of the input buffer.
* @param inBuf pointer to the input buffer
* @param outBuf pointer to the output buffer
* @param len length of the data to transfer, from 0 to (len-1)
*/
void LoadOutput(uint8_t *inBuf, uint8_t *outBuf, uint16_t len)
{
uint16_t i;
for(i=0;i<len;i++){ outBuf[i] = inBuf[i]; }
}
/**
* \brief Initialize the USB and Serial2 drivers
*/
void CCOM_Init(void)
{
uint8_t i;
BT.begin(921600u,SERIAL_8N1);
USB.begin(9600); /* Pretty sure this does nothing... USB enumerates if connected on its own */
rbuf_Init(&ccomBuf, &au8Data[0][0], (uint16_t)MAX_FRAME_SIZE);
}
/**
* \brief Returns 8-bit checksum of all bytes in the packet up to the CKSM
*
* Checksum is the summation of all bytes, inverted, subtract 1
*
* @param id00Packet
* @return
*/
uint8_t Checksum_ID00_Packet(T_CCOM_ID00 * id00Packet)
{
uint64_t u64Cksm = 0;
uint8_t i;
for(i=0;i<(MAX_FRAME_SIZE-1);i++){ u64Cksm += id00Packet->au8AllData[i]; }
return (~((uint8_t)u64Cksm))-1;
}
void Clear_ID00_Packet(T_CCOM_ID00 * id00Packet)
{
uint8_t i;
for(i=0;i<64u;i++){ id00Packet->au8AllData[i] = 0; }
}
/**
* \brief Periodic process which will send any data currently loaded for transmission
*/
void CCOM_Task(void)
{
uint32_t u32GetAddr;
uint8_t *pu8Data;
uint8_t i;
if(!rbuf_empty(&ccomBuf))
{
rbuf_get(&ccomBuf,&u32GetAddr);
pu8Data = (uint8_t *)u32GetAddr;
//if(USB_isReady()){ USB.write(pu8Data,MAX_FRAME_SIZE); }
usb_rawhid_send(pu8Data, MAX_FRAME_SIZE);
//for(i=0;i<MAX_FRAME_SIZE;i++){ printf("%02x,",*pu8Data++); } printf("\n\r");
}
//else{ printf("No Data...\n\r"); }
}
/**
* \brief Passes CAN message data and VEH_IO data to the output stream
* @param canMsg1 pointer to a packet of CAN message data
* @param u64Can1EpochTime1 Time the message arrived
* @param canMsg2 pointer to a packet of CAN message data
* @param u64CanEpochTime2 Time the message arrived
* @param u8IoDir 8-bit value representing the direction of the IO pins (Input:1, Output:0)
* @param u8IoVal 8-bit value representing the value currently on the IO pins.
* @return T if data is in a valid format and if there is space available in the output stream buffers
*/
uint8_t CCOM_TxCmd00(CAN_msg_t *canMsg1,
uint64_t u64CanEpochTime1,
CAN_msg_t *canMsg2,
uint64_t u64CanEpochTime2,
uint8_t u8IoDir,
uint8_t u8IoVal)
{
uint8_t bOk = 1;
T_CCOM_ID00 id00Packet;
uint8_t i;
uint32_t u32PutAddr;
Clear_ID00_Packet(&id00Packet);
/* Check to see if there is room in the output stream buffers */
if(rbuf_full(&ccomBuf)){ return 0; }
id00Packet.u16Header = ID00_HEADER;
id00Packet.u8Id = ID00;
id00Packet.u8IoDir = u8IoDir;
id00Packet.u8IoVal = u8IoVal;
id00Packet.u8BufSize = ccomBuf.count;
if(NULL == canMsg1)
{
id00Packet.u8Dat = ID00_IO_ONLY;
}
else if(canMsg2 != NULL)
{
id00Packet.u8Dat = ID00_CAN_MSG1_AND_2_AND_IO;
/* CAN MESSAGE 1 */
id00Packet.u32CanId1 = canMsg1->id;
id00Packet.u8CanDlc1 = canMsg1->len;
id00Packet.u8CanStat1 = canMsg1->stat;
for(i=0;i<canMsg1->len;i++){ id00Packet.au8CanData1[i] = canMsg1->buf[i]; }
id00Packet.u64CanEpochTime1 = u64CanEpochTime1;
/* CAN MESSAGE 2 */
id00Packet.u32CanId2 = canMsg2->id;
id00Packet.u8CanDlc2 = canMsg2->len;
id00Packet.u8CanStat2 = canMsg2->stat;
for(i=0;i<canMsg2->len;i++){ id00Packet.au8CanData2[i] = canMsg2->buf[i]; }
id00Packet.u64CanEpochTime2 = u64CanEpochTime2;
}
else
{
id00Packet.u8Dat = ID00_CAN_MSG1_AND_IO;
/* CAN MESSAGE 1 */
id00Packet.u32CanId1 = canMsg1->id;
id00Packet.u8CanDlc1 = canMsg1->len;
id00Packet.u8CanStat1 = canMsg1->stat;
for(i=0;i<canMsg1->len;i++){ id00Packet.au8CanData1[i] = canMsg1->buf[i]; }
id00Packet.u64CanEpochTime1 = u64CanEpochTime1;
}
id00Packet.u8Cksm = Checksum_ID00_Packet(&id00Packet);
/* Packet is put together... Now stuff it in a bucket for sending */
rbuf_put(&ccomBuf, &u32PutAddr); // which available array should we use?
LoadOutput(&id00Packet.au8AllData[0],(uint8_t *)u32PutAddr,MAX_FRAME_SIZE);
//for(i=0;i<MAX_FRAME_SIZE;i++){ printf("%02x,",au8Data[0][i]); } printf("\n\r");
return bOk;
}
/**
* \brief Initialize the Ring Buffer
*
* Initialize the ring buffer such that all elements = 0 (buf, head, tail, count),
* then populate the array with the addresses provided.
*
* @param _this
*/
void rbuf_Init(T_RING_BUF *_this, uint8_t * initAddress, uint16_t u16ArrayLen)
{
uint16_t i;
//memset (_this, 0, sizeof (*_this));
_this->count = 0;
_this->head = 0;
_this->tail = 0;
for(i=0; i<RBUF_SIZE;i++){ _this->au32Addr[i] = (uint32_t)initAddress + i * u16ArrayLen; }
}
/**
* \brief Get the oldest data in the buffer (First In).
* @param _this
* @param pu8Val --> au32Addr[tail] returned here.
* @return T if there is data in the buffer, F otherwise
*/
uint8_t rbuf_get (T_RING_BUF *_this, uint32_t * pu32Val)
{
uint8_t bOk = 1;
if (_this->count>0)
{
*pu32Val = _this->au32Addr[_this->tail];
_this->tail = modulo_inc (_this->tail, RBUF_SIZE);
--_this->count;
}
else{ bOk = 0; }
return bOk;
}
/**
* \brief Add data to one of the arrays pointed to by the address
* at au32Addr[head].
*
* If .count < RBUF_SIZE, head is incremented, count is incremented
* and the next available 32-bit address is returned.
*
* @param _this pointer to the ring buffer
* @param pu32Val 32-bit address at au32Addr[head] is returned
*/
void rbuf_put (T_RING_BUF *_this, uint32_t * pu32Val)
{
if (_this->count < RBUF_SIZE)
{
*pu32Val = _this->au32Addr[_this->head];
_this->head = modulo_inc (_this->head, RBUF_SIZE);
++_this->count;
}
}
/**
* \brief Returns whether Ring Buffer is FULL(T) or NOT_FULL(F)
*
* @param _this pointer to the ring buffer
* @return FULL-->TRUE, NOT_FULL-->FALSE
*/
uint8_t rbuf_full (T_RING_BUF *_this)
{
return (_this->count>=RBUF_SIZE);
}
/**
* \brief Flush the buffer
*
* count, head, and tail are cleared to 0.
*
* @param _this pointer to the ring buffer
*/
void rbuf_flush (T_RING_BUF *_this)
{
_this->count = 0;
_this->head = 0;
_this->tail = 0;
}
/**
* \brief Returns whether Ring Buffer is EMPTY(T) or NOT_EMPTY(F)
*
* @param _this pointer to the ring buffer
* @return EMPTY(TRUE), NOT_EMPTY(FALSE)
*/
uint8_t rbuf_empty (T_RING_BUF *_this)
{
return (0==_this->count);
}
/**
*\brief Calculate the modulus of 2 numbers
*
* If value++ >= modulus, return 0, else return value++.
*
* @param value The value compared against the provided modulus
* @param modulus The modulus
* @return value++ if value++ < modulus, else 0
*/
uint16_t modulo_inc (const uint16_t value, const uint16_t modulus)
{
uint16_t my_value = value + 1;
if (my_value >= modulus)
{
my_value = 0;
}
return (my_value);
}
/**
* \brief Returns whether USB enumerated with a computer and is connected.
* @return T:CONNECTED, F:NOT CONNECTED
*/
bool USB_isReady(void)
{
return USB; /* Returns the state of the usb_configuration value */
}
void CCOM_Debug(const char * msg, boolean bCrlf)
{
}