-
Notifications
You must be signed in to change notification settings - Fork 1
/
CEC_Electrical.cpp
673 lines (599 loc) · 15.8 KB
/
CEC_Electrical.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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
#include "CEC_Electrical.h"
CEC_Electrical::CEC_Electrical(int address)
{
MonitorMode = false;
Promiscuous = false;
_address = address & 0x0f;
_amLastTransmittor = false;
_transmitPending = false;
_xmitretry = 0;
ResetState();
}
void CEC_Electrical::Initialize()
{
_lastLineState = LineState();
_lastStateChangeTime = micros();
}
void CEC_Electrical::SetAddress(int address)
{
_address = address & 0x0f;
}
bool CEC_Electrical::Raise()
{
if (MonitorMode)
return LineState();
unsigned long time = micros();
SetLineState(1);
// Only update state if the line was actually changed (i.e. it wasn't already in its new state)
if (LineState())
{
_lastLineState = true;
_lastStateChangeTime = time;
return true;
}
return false;
}
bool CEC_Electrical::Lower()
{
if (MonitorMode)
return LineState();
unsigned long time = micros();
SetLineState(0);
// Only update state if the line was actually changed (i.e. it wasn't already in its new state)
if (!LineState())
{
if (_lastLineState)
_bitStartTime = time;
_lastLineState = false;
_lastStateChangeTime = time;
return false;
}
return true;
}
void CEC_Electrical::HasRaised(unsigned long time)
{
_lastLineState = true;
_lastStateChangeTime = time;
}
void CEC_Electrical::HasLowered(unsigned long time)
{
_lastLineState = false;
_lastStateChangeTime = time;
_bitStartTime = time;
}
bool CEC_Electrical::CheckAddress()
{
if (ReceivedBytes() == 1)
{
int address = _receiveBuffer[0] & 0x0f;
if (address != _address && address != 0x0f)
{
// It's not addressed to us and it's not a broadcast.
// Reset and wait for the next start bit
return false;
}
// It is either addressed to this device or its a broadcast
if (address == 0x0f)
_broadcast = true;
else
_follower = true;
}
return true;
}
void CEC_Electrical::ReceivedBit(bool state)
{
if (_tertiaryState == CEC_RCV_BIT_EOM)
{
//DbgPrint("%p: Received eom %d\n", this, state?1:0);
_eom = state;
}
else
{
//DbgPrint("%p: Received bit %d\n", this, state?1:0);
if (state)
PushReceiveBit(1);
else
PushReceiveBit(0);
}
}
unsigned long CEC_Electrical::LineError()
{
DbgPrint("%p: Line Error!\n", this);
if (_follower || _broadcast)
{
_secondaryState = CEC_RCV_LINEERROR;
Lower();
return micros() + 3600;
}
return ResetState() ? micros() : (unsigned long)-1;
}
///
/// CEC_Electrical::Process implements our main state machine
/// which includes all reading and writing of state including
/// acknowledgements and arbitration
///
unsigned long CEC_Electrical::Process()
{
// We are either called because of an INTerrupt in which case
// state has changed or because of a poll (during write).
bool currentLineState = LineState();
bool lastLineState = _lastLineState;
unsigned long waitTime = -1; // INFINITE; (== wait until an external event has occurred)
unsigned long time = micros();
//DbgPrint("%d %d %d\n", _primaryState, _secondaryState, _tertiaryState);
// If the state hasn't changed and we're not in a transmit
// state then this is spurrious.
if( currentLineState == lastLineState &&
!(_primaryState == CEC_TRANSMIT ||
(_primaryState == CEC_RECEIVE &&
(_secondaryState == CEC_RCV_ACK_SENT ||
_secondaryState == CEC_RCV_LINEERROR))))
return waitTime;
unsigned long lasttime = _lastStateChangeTime;
unsigned long difftime = time - _bitStartTime;
if( currentLineState != lastLineState )
{
// Line state has changed, update our internal state
if (currentLineState)
HasRaised(time);
else
HasLowered(time);
}
switch (_primaryState)
{
case CEC_IDLE:
// If a high to low transition occurs, then we must be
// beginning a start bit
if (lastLineState && !currentLineState)
{
_primaryState = CEC_RECEIVE;
_secondaryState = CEC_RCV_STARTBIT1;
_follower = false;
_broadcast = false;
_amLastTransmittor = false;
break;
}
// Nothing to do until we have a need to transmit
// or we detect the falling edge of the start bit
break;
case CEC_RECEIVE:
switch (_secondaryState)
{
case CEC_RCV_STARTBIT1:
// We're waiting for the rising edge of the start bit
if (difftime >= 3500 && difftime <= 3900)
{
// We now need to wait for the next falling edge
_secondaryState = CEC_RCV_STARTBIT2;
break;
}
// Illegal state. Go back to CEC_IDLE to wait for a valid
// start bit
//DbgPrint("1: %ld %ld\n", difftime, micros());
waitTime = ResetState() ? micros() : (unsigned long)-1;
break;
case CEC_RCV_STARTBIT2:
// This should be the falling edge of the start bit
if (difftime >= 4300 && difftime <= 4700)
{
// We've fully received the start bit. Begin receiving
// a data bit
_secondaryState = CEC_RCV_DATABIT1;
_tertiaryState = CEC_RCV_BIT0;
break;
}
// Illegal state. Go back to CEC_IDLE to wait for a valid
// start bit
//DbgPrint("2: %ld %ld\n", difftime, micros());
waitTime = ResetState() ? micros() : (unsigned long)-1;
break;
case CEC_RCV_DATABIT1:
// We've received the rising edge of the data bit
if (difftime >= 400 && difftime <= 800)
{
// We're receiving bit '1'
ReceivedBit(true);
_secondaryState = CEC_RCV_DATABIT2;
break;
}
else if (difftime >= 1300 && difftime <= 1700)
{
// We're receiving bit '0'
ReceivedBit(false);
_secondaryState = CEC_RCV_DATABIT2;
break;
}
// Illegal state. Go back to CEC_IDLE to wait for a valid
// start bit
waitTime = LineError();
break;
case CEC_RCV_DATABIT2:
// We've received the falling edge of the data bit
if (difftime >= 2050 && difftime <= 2750)
{
if (_tertiaryState == CEC_RCV_BIT_EOM)
{
_secondaryState = CEC_RCV_ACK1;
_tertiaryState = (CEC_TERTIARY_STATE)(_tertiaryState + 1);
// Check to see if the frame is addressed to us
// or if we are in promiscuous mode (in which case we'll receive everything)
if (!CheckAddress() && !Promiscuous)
{
// It's not addressed to us. Reset and wait for the next start bit
waitTime = ResetState() ? micros() : (unsigned long)-1;
break;
}
// If we're the follower, go low for a while
if (_follower)
{
Lower();
_secondaryState = CEC_RCV_ACK_SENT;
waitTime = _bitStartTime + 1500;
}
break;
}
// Receive another bit
_secondaryState = CEC_RCV_DATABIT1;
_tertiaryState = (CEC_TERTIARY_STATE)(_tertiaryState + 1);
break;
}
// Illegal state. Go back to CEC_IDLE to wait for a valid
// start bit
waitTime = LineError();
break;
case CEC_RCV_ACK_SENT:
// We're done holding the line low... release it
Raise();
if (_eom)
{
// We're not going to receive anything more from
// the initiator (EOM has been received)
// And we've sent the ACK for the most recent bit
// therefore this message is all done. Go back
// to the IDLE state and wait for another start bit.
ProcessFrame();
waitTime = ResetState() ? micros() : (unsigned long)-1;
break;
}
// We need to wait for the falling edge of the ACK
// to finish processing this ack
_secondaryState = CEC_RCV_ACK2;
_tertiaryState = CEC_ACK;
break;
// FIXME: This is dead state
// Code currently exists in CEC_RCV_DATABIT2 that checks the address
// of the frame and if it isn't addressed to this device it goes back
// to watching for a start bit state. This state, CEC_RCV_ACK1, was
// from when we didn't do that and we followed state for all frames
// regardless of addressing. However, I'm not removing this code because
// it will be needed when we support broadcast frames.
case CEC_RCV_ACK1:
{
int ack;
if (difftime >= 400 && difftime <= 800)
ack = _broadcast ? CEC_ACK : CEC_NAK;
else if (difftime >= 1300 && difftime <= 1700)
ack = _broadcast ? CEC_NAK : CEC_ACK;
else
{
// Illegal state. Go back to CEC_IDLE to wait for a valid
// start bit
waitTime = LineError();
break;
}
if (_eom && ack == CEC_ACK)
{
// We're not going to receive anything more from
// the initiator (EOM has been received)
// And we've seen the ACK for the most recent bit
// therefore this message is all done. Go back
// to the IDLE state and wait for another start bit.
ProcessFrame();
waitTime = ResetState() ? micros() : (unsigned long)-1;
break;
}
if (ack == CEC_NAK)
{
waitTime = ResetState() ? micros() : (unsigned long)-1;
break;
}
// receive the rest of the ACK (or rather the beginning of the next bit)
_secondaryState = CEC_RCV_ACK2;
break;
}
case CEC_RCV_ACK2:
// We're receiving the falling edge of the ack
if (difftime >= 2050 && difftime <= 2750)
{
_secondaryState = CEC_RCV_DATABIT1;
_tertiaryState = CEC_RCV_BIT0;
break;
}
// Illegal state (or NACK). Either way, go back to CEC_IDLE
// to wait for next start bit (maybe a retransmit)..
waitTime = LineError();
break;
case CEC_RCV_LINEERROR:
//DbgPrint("%p: Done signaling line error\n", this);
Raise();
waitTime = ResetState() ? micros() : (unsigned long)-1;
break;
}
break;
case CEC_TRANSMIT:
if (lastLineState != currentLineState)
{
// Someone else is mucking with the line. Wait for the
// line to clear before appropriately before (re)transmit
// However it is OK for a follower to ACK if we are in an
// ACK state
if (_secondaryState != CEC_XMIT_ACK &&
_secondaryState != CEC_XMIT_ACK2 &&
_secondaryState != CEC_XMIT_ACK3 &&
_secondaryState != CEC_XMIT_ACK_TEST)
{
// If a state changed TO LOW during IDLE wait, someone could be legitimately transmitting
if (_secondaryState == CEC_IDLE_WAIT)
{
if (currentLineState == false)
{
_primaryState = CEC_RECEIVE;
_secondaryState = CEC_RCV_STARTBIT1;
_transmitPending = true;
}
break;
}
else
{
// Transmit collision
ResetTransmit(true);
waitTime = 0;
break;
}
}
else
{
// This is a state change from an ACK and isn't part of our state
// tracking.
waitTime = -2;
break;
}
}
unsigned long neededIdleTime = 0;
switch (_secondaryState)
{
case CEC_IDLE_WAIT:
// We need to wait a certain amount of time before we can
// transmit..
// If the line is low, we can't do anything now. Wait
// indefinitely until a line state changes which will
// catch in the code just above
if (currentLineState == 0)
break;
// The line is high. Have we waited long enough?
neededIdleTime = 0;
switch (_tertiaryState)
{
case CEC_IDLE_RETRANSMIT_FRAME:
neededIdleTime = 3 * 2400;
break;
case CEC_IDLE_NEW_FRAME:
neededIdleTime = 5 * 2400;
break;
case CEC_IDLE_SUBSEQUENT_FRAME:
neededIdleTime = 7 * 2400;
break;
}
if (time - _lastStateChangeTime < neededIdleTime)
{
// not waited long enough, wait some more!
waitTime = lasttime + neededIdleTime;
break;
}
// we've wait long enough, begin start bit
Lower();
_amLastTransmittor = true;
_broadcast = (_transmitBuffer[0] & 0x0f) == 0x0f;
// wait 3700 microsec
waitTime = _bitStartTime + 3700;
// and transition to our next state
_secondaryState = CEC_XMIT_STARTBIT1;
break;
case CEC_XMIT_STARTBIT1:
if (!Raise())
{
//DbgPrint("%p: Received Line Error\n", this);
ResetTransmit(true);
break;
}
waitTime = _bitStartTime + 4500;
_secondaryState = CEC_XMIT_STARTBIT2;
break;
case CEC_XMIT_STARTBIT2:
case CEC_XMIT_ACK3:
Lower();
_secondaryState = CEC_XMIT_DATABIT1;
_tertiaryState = CEC_XMIT_BIT0;
if (PopTransmitBit())
{
// Sending bit '1'
//DbgPrint("%p: Sending bit 1\n", this);
waitTime = _bitStartTime + 600;
}
else
{
// Sending bit '0'
//DbgPrint("%p: Sending bit 0\n", this);
waitTime = _bitStartTime + 1500;
}
break;
case CEC_XMIT_DATABIT1:
if (!Raise())
{
//DbgPrint("%p: Received Line Error\n", this);
ResetTransmit(true);
break;
}
waitTime = _bitStartTime + 2400;
if (_tertiaryState == CEC_XMIT_BIT_EOM)
{
// We've just finished transmitting the EOM
// move on to the ACK
_secondaryState = CEC_XMIT_ACK;
}
else
_secondaryState = CEC_XMIT_DATABIT2;
break;
case CEC_XMIT_DATABIT2:
Lower();
_tertiaryState = (CEC_TERTIARY_STATE)(_tertiaryState + 1);
if (_tertiaryState == CEC_XMIT_BIT_EOM)
{
if (RemainingTransmitBytes() == 0)
{
// Sending eom '1'
//DbgPrint("%p: Sending eom 1\n", this);
waitTime = _bitStartTime + 600;
}
else
{
// Sending eom '0'
//DbgPrint("%p: Sending eom 0\n", this);
waitTime = _bitStartTime + 1500;
}
}
else
{
if (PopTransmitBit())
{
// Sending bit '1'
//DbgPrint("%p: Sending bit 1\n", this);
waitTime = _bitStartTime + 600;
}
else
{
// Sending bit '0'
//DbgPrint("%p: Sending bit 0\n", this);
waitTime = _bitStartTime + 1500;
}
}
_secondaryState = CEC_XMIT_DATABIT1;
break;
case CEC_XMIT_ACK:
Lower();
// We transmit a '1'
//DbgPrint("%p: Sending ack\n", this);
waitTime = _bitStartTime + 600;
_secondaryState = CEC_XMIT_ACK2;
break;
case CEC_XMIT_ACK2:
Raise();
// we need to sample the state in a little bit
waitTime = _bitStartTime + 1050;
_secondaryState = CEC_XMIT_ACK_TEST;
break;
case CEC_XMIT_ACK_TEST:
//DbgPrint("%p: Testing ack: %d\n", this, (currentLineState == 0) != _broadcast?1:0);
if ((currentLineState != 0) != _broadcast)
{
// not being acknowledged
// normally we retransmit. But this is NOT the case for <Polling Message> as its
// function is basically to 'ping' a logical address in which case we just want
// acknowledgement that it has succeeded or failed
if (RemainingTransmitBytes() == 0 && TransmitSize() == 1)
{
ResetState();
DbgPrint("Transmit failed, no acknowledge.\n");
OnTransmitComplete(false);
}
else
{
ResetTransmit(true);
waitTime = 0;
}
break;
}
_lastStateChangeTime = lasttime;
if (RemainingTransmitBytes() == 0)
{
// Nothing left to transmit, go back to idle
ResetState();
OnTransmitComplete(true);
break;
}
// We have more to transmit, so do so...
waitTime = _bitStartTime + 2400;
_secondaryState = CEC_XMIT_ACK3;
break;
}
}
return waitTime;
}
bool CEC_Electrical::ResetState()
{
_primaryState = CEC_IDLE;
_secondaryState = (CEC_SECONDARY_STATE)0;
_tertiaryState = (CEC_TERTIARY_STATE)0;
_eom = false;
_follower = false;
_broadcast = false;
ResetReceiveBuffer();
if (_transmitPending)
{
ResetTransmit(false);
return true;
}
return false;
}
void CEC_Electrical::ResetTransmit(bool retransmit)
{
_primaryState = CEC_TRANSMIT;
_secondaryState = CEC_IDLE_WAIT;
_tertiaryState = CEC_IDLE_NEW_FRAME;
_transmitPending = false;
if (retransmit)
{
if (++_xmitretry == CEC_MAX_RETRANSMIT)
{
// No more
ResetState();
DbgPrint("Transmit failed, max retries reached.\n");
OnTransmitComplete(false);
}
else
{
//DbgPrint("%p: Retransmitting current frame\n", this);
_tertiaryState = CEC_IDLE_RETRANSMIT_FRAME;
ResetTransmitBuffer();
}
}
else
{
_xmitretry = 0;
if (_amLastTransmittor)
{
_tertiaryState = CEC_IDLE_SUBSEQUENT_FRAME;
}
}
}
void CEC_Electrical::ProcessFrame()
{
// We've successfully received a frame in the serial line buffer
// Allow it to be processed
OnReceiveComplete(_receiveBuffer, _receiveBufferByte);
}
void CEC_Electrical::OnTransmitBegin()
{
if (!MonitorMode)
{
if (_primaryState == CEC_IDLE)
{
ResetTransmit(false);
return;
}
_transmitPending = true;
}
}
void CEC_Electrical::OnTransmitComplete(bool success)
{
}