-
Notifications
You must be signed in to change notification settings - Fork 14
/
mpx.cpp
533 lines (510 loc) · 14.1 KB
/
mpx.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
//-----------------------------------------------------------------------------
// MPX.cpp
//-----------------------------------------------------------------------------
#include "avr-misc/avr-misc.h"
#include "mpx.h"
#include "config.h"
#include "mpx_config.h"
#include "../avr-debug/debug.h"
//-----------------------------------------------------------------------------
static BYTE queue_head = 0; // queue write index
static BYTE queue_tail = 0; // queue read index
static BYTE queue[MPX_QUEUE_SIZE][MPX_MAX_PACKET_SIZE];
//-----------------------------------------------------------------------------
static struct
{
bool idle;
} bus;
//-----------------------------------------------------------------------------
static struct
{
BYTE buf[MPX_MAX_FRAME_SIZE];
BYTE size;
BYTE crc;
volatile BYTE byte;
BYTE bit;
bool busy;
BYTE same_bit_counter;
void (*callback)(BYTE size, const BYTE* buf);
} rx;
//-----------------------------------------------------------------------------
static struct
{
BYTE buf[MPX_MAX_FRAME_SIZE];
BYTE size;
BYTE pos;
BYTE shift;
volatile BYTE state;
BYTE result;
void (*callback)(BYTE result);
} tx;
//-----------------------------------------------------------------------------
static struct
{
BYTE counter;
BYTE prev;
} stuffing;
//-----------------------------------------------------------------------------
__inline void start_timer()
{
MPX_TIMER_TCNT = 0;
MPX_TIMER_TCCRB |= _bit(MPX_TIMER_CS1);
set_bit(TIFR0, OCF0A);
}
//-----------------------------------------------------------------------------
__inline void stop_timer()
{
MPX_TIMER_TCCRB &= ~_bit(MPX_TIMER_CS1);
}
//-----------------------------------------------------------------------------
__inline void half_bit_timer()
{
MPX_TIMER_OCRA = F_CPU / 8 / MPX_BAUD_RATE / 2 - 1;
}
//-----------------------------------------------------------------------------
__inline void full_bit_timer()
{
MPX_TIMER_OCRA = F_CPU / 8 / MPX_BAUD_RATE - 1;
}
//-----------------------------------------------------------------------------
__inline void resync_timer()
{
MPX_TIMER_TCNT = 0;
half_bit_timer();
MPX_TIMER_TCCRB |= _bit(MPX_TIMER_CS1);
set_bit(TIFR0, OCF0A);
}
//-----------------------------------------------------------------------------
__inline void stuffing_reset()
{
stuffing.counter = 0;
};
//-----------------------------------------------------------------------------
/**
* @brief Registers the bit and determines whether it is a stuffing bit
* @param value - bit value
* @return 1 - the bit is a stuffing bit and should be skipped in receiption
*/
__inline BYTE stuffing_push(BYTE value)
{
value = (value > 0) ? 1 : 0;
if(value == stuffing.prev)
{
stuffing.counter++;
return 0;
}
volatile register BYTE res = 0;
if(stuffing.counter == 5)
{
// stuffing bit detected
res = 1;
}
stuffing.counter = 1;
stuffing.prev = value;
return res;
}
//-----------------------------------------------------------------------------
/**
* @brief Checks whether the stuffing bit is needed after the previous push for
* transmission
* @return 1 - stuffing bit should be the next
*/
__inline BYTE stuffing_check()
{
return (stuffing.counter >= 5);
}
//-----------------------------------------------------------------------------
/**
* @brief Returns the value (polarity) of the following stuffing bit
*/
__inline BYTE stuffing_bit()
{
stuffing.counter = 1;
stuffing.prev = (stuffing.prev > 0) ? 0 : 1; // stuffing bit is an inverted bit
return stuffing.prev;
}
//-----------------------------------------------------------------------------
/**
* @brief Checks whether the driver controls the MPX bus during transmission
* @return true - The driver has lost the control of the bus, another device is
* transmitting a message with a greater priority or higher message-id
*/
__inline bool arbitration_lost()
{
return (!test_bit(MPX_TX_PORT, MPX_TX_BIT) && RX_PIN);
}
//-----------------------------------------------------------------------------
__inline void rx_reset()
{
rx.size = 0;
rx.crc = 0;
rx.byte = 0;
rx.bit = 0;
stuffing_reset();
}
//-----------------------------------------------------------------------------
__inline BYTE CRC8(const BYTE* buf, BYTE size)
{
BYTE crc = 0;
while(size--)
{
crc = CRC8_table[crc ^ *buf++];
}
return crc;
}
//-----------------------------------------------------------------------------
__inline void suspend_transmission()
{
// suspend the transmission
clr_bit(MPX_TX_PORT, MPX_TX_BIT);
tx.state = TX_QUEUED;
MPX_TX_LED_OFF();
// and continue receiption
MPX_RX_LED_ON();
rx.busy = true;
// restore skipped data
volatile register BYTE size = tx.pos;
memcpy(rx.buf, tx.buf, size);
rx.crc = CRC8(rx.buf, size);
rx.bit = tx.shift - 1;
rx.byte = tx.buf[size] >> (9 - tx.shift);
rx.size = size;
}
//-----------------------------------------------------------------------------
__inline void start_transmission()
{
if(!RX_PIN)
{
// start of frame (a start bit)
MPX_HW_DEBUG_ON();
tx.pos = 0;
tx.shift = 0;
stuffing_reset();
stuffing_push(1);
tx.state = TX_IN_PROGRESS;
set_bit(MPX_TX_PORT, MPX_TX_BIT);
full_bit_timer();
start_timer();
MPX_HW_DEBUG_OFF();
}
}
//-----------------------------------------------------------------------------
__inline void get_bit()
{
static BYTE EOM = 0;
EOM = EOM << 1 | RX_PIN;
if(EOM == 0x7E)
{
// end-of-message detected
rx.buf[rx.size++] = 0x7E;
if(!rx.crc)
{
// if received CRC and calculated CRC are equal, then
// the result CRC should be zero
if(rx.callback)
{
rx.callback(rx.size, rx.buf);
}
}
rx_reset();
return;
}
if(!rx.busy && RX_PIN)
{
// start bit detected
MPX_RX_LED_ON();
rx_reset();
rx.busy = true;
return;
}
if(stuffing_push(RX_PIN))
{
// skip stuffing-bit
return;
}
rx.byte = rx.byte << 1 | RX_PIN;
if(++rx.bit >= 8)
{
// byte reception completed
rx.bit = 0;
rx.buf[rx.size++] = rx.byte;
rx.crc = CRC8_table[rx.crc ^ rx.byte];
}
}
//-----------------------------------------------------------------------------
__inline void send_bit()
{
volatile register BYTE value = 0;
if(tx.pos < tx.size && stuffing_check())
{
if(stuffing_bit())
set_bit(MPX_TX_PORT, MPX_TX_BIT);
else
clr_bit(MPX_TX_PORT, MPX_TX_BIT);
return;
}
if(tx.pos <= tx.size)
{
// various data (from priority to CRC)
value = (tx.buf[tx.pos] << tx.shift) & 0x80;
// set bit value as soon as possible
if(value)
{
set_bit(MPX_TX_PORT, MPX_TX_BIT);
}
else
{
clr_bit(MPX_TX_PORT, MPX_TX_BIT);
}
if(++tx.shift >= 8)
{
tx.pos++;
tx.shift = 0;
}
if(value)
{
set_bit(MPX_TX_PORT, MPX_TX_BIT);
stuffing_push(1);
}
else
{
clr_bit(MPX_TX_PORT, MPX_TX_BIT);
// prepare arbitration check
half_bit_timer();
tx.state = TX_ARBITRATION;
}
}
else
{
switch(tx.shift++)
{
case 0:
// prepare to receive checksum and acknowlegement reply
resync_timer();
tx.state = TX_ACKNOWLEGEMENT;
// prevent detecting acknowlegement as a start bit
rx.busy = true;
break;
case 1:
// checksum reply
tx.result = RX_PIN << 1;
break;
case 2:
// acknowlegement reply
tx.result = (tx.result | RX_PIN);
break;
case 3:
// resync back into transmission mode
resync_timer();
if(!RX_PIN)
{
tx.result = tx.result << 1 | 1;
//** tx.result values
// "001" = 1 -- NACK
// "011" = 3 -- ACK
// "111" = 7 -- BAD CRC
}
else
{
// unexpected bus level
tx.result = MPX_ERR_UNKNOWN;
}
// transmission completed
MPX_TX_LED_OFF();
tx.state = TX_IDLE;
if(tx.callback)
{
tx.callback(tx.result);
}
rx.busy = false;
break;
}
}
}
//-----------------------------------------------------------------------------
ISR(MPX_RX_INT_VECT)
{
MPX_HW_DEBUG_ON();
// resync the timer
if(tx.state == TX_IN_PROGRESS) // do not resync while transmission is in progress
{
full_bit_timer();
start_timer();
}
else
{
resync_timer();
}
bus.idle = false;
if(tx.state == TX_IN_PROGRESS && arbitration_lost())
{
//MPX_HW_DEBUG_SYNC();
resync_timer();
suspend_transmission();
}
// continue further receiption
rx.same_bit_counter = 0;
MPX_HW_DEBUG_OFF();
}
//-----------------------------------------------------------------------------
ISR(MPX_TIMER_VECT)
{
MPX_HW_DEBUG_ON();
// change the timer interval to full-bit value
full_bit_timer();
switch(tx.state)
{
case TX_ARBITRATION:
if(arbitration_lost())
{
suspend_transmission();
get_bit();
}
else
{
// arbitration is OK, continue the transmission
stuffing_push(0);
tx.state = TX_IN_PROGRESS;
half_bit_timer();
}
break;
case TX_IN_PROGRESS:
case TX_ACKNOWLEGEMENT:
// transmission mode
send_bit();
break;
default:
// receiption mode
if(++rx.same_bit_counter > 6)
{
// idle state detected
bus.idle = true;
rx.busy = false;
MPX_RX_LED_OFF();
rx.same_bit_counter = 0;
if(tx.state == TX_QUEUED)
{
// a packet is waiting for transmission
start_transmission();
}
else
{
// go to idle mode
stop_timer();
}
}
else
{
get_bit();
}
}
MPX_HW_DEBUG_OFF();
}
//-----------------------------------------------------------------------------
void mpx_init(void (*rx_callback)(BYTE size, const BYTE* buf))
{
rx.callback = rx_callback;
set_bit(MPX_TX_DIR, MPX_TX_BIT);
bus.idle = true;
rx.busy = false;
tx.state = TX_IDLE;
stuffing.counter = 0;
stuffing.prev = 0;
#ifdef MPX_RX_LED_PORT
set_bit(MPX_RX_LED_DIR, MPX_RX_LED_BIT);
#endif
#ifdef HW_DEBUG_PORT
set_bit(HW_DEBUG_DIR, HW_DEBUG_BIT);
#endif
#ifdef HW_DEBUG_SYNC_PORT
set_bit(HW_DEBUG_SYNC_DIR, HW_DEBUG_SYNC_BIT);
#endif
MPX_TIMER_TCCRA = _bit(MPX_TIMER_WGM_CTC); // CTC (clear timer on compare) mode
set_bit(MPX_TIMER_TIMSK, MPX_TIMER_OCIEA); // enable timer compare match interrupt
// keep in sync, correcting the timer on every level change
set_bit(EICRA, MPX_RX_ISC0);
clr_bit(EICRA, MPX_RX_ISC1);
// enable receiption interrupt
set_bit(EIFR, INTF0); // reset pending interrupt call
set_bit(EIMSK, INT0);
}
//-----------------------------------------------------------------------------
BYTE mpx_send(BYTE priority, BYTE address, BYTE size, const BYTE* data, void (*callback)(BYTE result))
{
if(tx.state != TX_IDLE)
{
return MPX_ERR_BUSY;
}
if((priority > 15) || (size < 2) || (size > MPX_MAX_DATA_SIZE + 1))
{
// valid priority range: 0...15
// valid size range: 2...12 (includes message id)
return MPX_ERR_BAD_PARAMETER;
}
// priority and size
tx.buf[0] = (priority << 4) | (size + 1);
// destination address
tx.buf[1] = address;
// data
memcpy(tx.buf + 2, data, size);
// CRC
tx.buf[size + 2] = CRC8(tx.buf, size + 2);
// EOM
tx.buf[size + 3] = 0x7E;
tx.size = size + 3;
tx.callback = callback;
tx.state = TX_QUEUED;
if(bus.idle)
{
start_transmission();
}
return MPX_ERR_QUEUED;
}
//-----------------------------------------------------------------------------
#define mpx_queue_is_empty() (queue_head == queue_tail)
#define mpx_queue_is_not_empty() (queue_head != queue_tail)
//-----------------------------------------------------------------------------
void mpx_queue_check(__unused BYTE result)
{
if(mpx_queue_is_not_empty())
{
BYTE index = queue_tail;
queue_tail = (queue_tail + 1) % MPX_QUEUE_SIZE;
mpx_send(queue[index][0], queue[index][1], queue[index][2], queue[index] + 3, mpx_queue_check);
}
}
//-----------------------------------------------------------------------------
BYTE mpx_queue(BYTE priority, BYTE address, BYTE size, const BYTE* data)
{
BYTE result = 0;
if(mpx_queue_is_empty() && tx.state == TX_IDLE)
{
result = mpx_send(priority, address, size, data, mpx_queue_check);
}
else
{
disable_interrupts();
BYTE next_index = (queue_head + 1) % MPX_QUEUE_SIZE;
if(next_index == queue_tail)
{
// step on self tail
result = MPX_ERR_QUEUE_OVERFLOW;
}
else
{
queue[queue_head][0] = priority;
queue[queue_head][1] = address;
queue[queue_head][2] = size;
memcpy(queue[queue_head] + 3, data, size);
queue_head = next_index;
}
resume_interrupts();
}
return result;
}
//-----------------------------------------------------------------------------
BYTE mpx_CRC8(const BYTE* buf, BYTE size)
{
return CRC8(buf, size);
}
//-----------------------------------------------------------------------------