-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.c
More file actions
601 lines (521 loc) · 12.8 KB
/
Copy pathmain.c
File metadata and controls
601 lines (521 loc) · 12.8 KB
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
/* Operate a motorized potentiometer based on HDMI CEC volume commands. */
/*#define DEBUG 1*/
#define F_CPU 16384000UL
#ifdef DEBUG
#include <stdio.h>
#endif
#include <string.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
/* The HDMI address for this device. Respond to CEC messages sent to this
* address. */
#define ADDRESS 0x05
/* The duration to turn the motor for a single volume step. On my setup this
* value, which I found through trial and error, produces a small but audible
* change in volume, but it depends heavily on the characteristics of the
* receiver, volume potentiometer, and listener. */
#define STEP_TICKS 12000
#ifdef DEBUG
static int uart_put(unsigned char, FILE *);
static FILE uart_stdout = FDEV_SETUP_STREAM(uart_put, NULL,
_FDEV_SETUP_WRITE);
static void
uart_init(void)
{
/* Baud rate 500000 */
UBRRL = 1;
UCSRB = _BV(RXEN) | _BV(TXEN);
UCSRC = _BV(UCSZ1) | _BV(UCSZ0);
}
static int
uart_put(unsigned char c, FILE *stream)
{
if (c == '\n')
uart_put('\r', stream);
loop_until_bit_is_set(UCSRA, UDRE);
UDR = c;
return 0;
}
#endif /* DEBUG */
static void
send_ack(void)
{
/* Send a follower-initiated ACK. This must be called immediately
* after a falling edge has occured. */
uint16_t ticks_start;
uint16_t ticks;
ticks_start = TCNT1;
/* Pull the CEC line low. */
DDRD = 0x40;
for (;;) {
ticks = TCNT1;
/* optimal 1.5 ms */
if ((ticks - ticks_start) >= 96) {
/* Set the CEC line back to high-Z. */
DDRD = 0x00;
break;
}
}
}
enum edge { FALLING, RISING };
static uint16_t
wait_edge(enum edge e)
{
uint16_t ticks;
uint8_t last, cec;
last = cec = (PIND & 0x40) >> 6;
for (;;) {
ticks = TCNT1;
last = cec;
cec = (PIND & 0x40) >> 6;
if (e == RISING) {
if ((last == 0) && (cec == 1)) {
return ticks;
}
}
else {
if ((last == 1) && (cec == 0)) {
return ticks;
}
}
}
}
static uint16_t
wait_falling_edge(void)
{
return wait_edge(FALLING);
}
static uint16_t
wait_rising_edge(void)
{
return wait_edge(RISING);
}
static uint8_t
recv_data_bit(void)
{
/* Sample a bit, must be called immediately after a falling edge
* occurs. */
uint16_t ticks_start;
uint16_t ticks;
ticks_start = TCNT1;
for (;;) {
ticks = TCNT1;
/* optimal 1.05 ms */
if ((ticks - ticks_start) >= 67) {
return (PIND & 0x40) >> 6;
}
}
}
static int8_t
wait_start_bit(void)
{
uint16_t ticks_start;
uint16_t ticks;
/* A start bit consists of a falling edge followed by a rising edge
* between 3.5 ms and 3.9 ms after the falling edge, and a second
* falling edge between 4.3 and 4.7 ms after the first falling edge.
* Wait until those conditions are met, and start over at the next
* falling edge if any threshold is exceeded. */
for (;;) {
ticks_start = wait_falling_edge();
ticks = wait_rising_edge();
/* Rising edge took longer than 3.9 ms, start over */
if ((ticks - ticks_start) >= 250) {
continue;
}
/* Rising edge occured between 3.5 ms and 3.9 ms */
else if ((ticks - ticks_start) >= 224) {
ticks = wait_falling_edge();
/* Falling edge took longer than 4.7 ms, start over */
if ((ticks - ticks_start) >= 301) {
continue;
}
/* Falling edge between 4.3 ms and 4.7 ms means that
* this has been a start bit! */
else if ((ticks - ticks_start) >= 276) {
return 0;
}
/* The falling edge came too early, start over */
else {
continue;
}
}
/* The rising edge came sooner than 3.5 ms, start over */
else {
continue;
}
}
}
static int8_t
recv_frame(uint8_t *pld, uint8_t address)
{
uint16_t ticks_start;
uint16_t ticks;
uint8_t bit_count;
uint8_t pldcnt;
uint8_t eom;
wait_start_bit();
bit_count = 9;
pldcnt = 0;
pld[pldcnt] = 0;
/* Read blocks into pld until the EOM bit signals that the message is
* complete. Each block is 10 bits consisting of information bits 7-0,
* an EOM bit, and an ACK bit. The initiator sends the information
* bits and the EOM bit and expects the follower to send a '0' during
* the ACK bit to acknowledge receipt of the block. */
for (;;) {
/* At this point in the loop, a falling edge has just occured,
* either in wait_start_bit() above or wait_falling_edge() at
* the end of the loop, so it is time to sample a bit. */
ticks_start = TCNT1;
/* Only store and return the information bits. */
if (bit_count > 1) {
pld[pldcnt] <<= 1;
pld[pldcnt] |= recv_data_bit();
}
else {
eom = recv_data_bit();
}
bit_count--;
/* Wait for the starting falling edge of the next bit. */
ticks = wait_falling_edge();
/* 2.05 ms */
if ((ticks - ticks_start) < 131) {
#ifdef DEBUG
printf_P(PSTR("min\n"));
#endif /* DEBUG */
return -1;
}
ticks_start = ticks;
/* If that was the EOM bit, it's time to send an ACK and either
* return the data (if EOM was indicated by the initiator) or
* prepare to read another block. */
if (bit_count == 0) {
/* Only ACK messages addressed to us (indicated by the
* header block). */
if ((pld[0] & 0x0f) == address) {
send_ack();
}
if (eom) {
/* Don't consume the falling edge in this case
* because it could be the start of the next
* start bit! */
return pldcnt + 1;
}
else {
/* Wait for the starting falling edge of the
* next bit. */
ticks = wait_falling_edge();
/* 2.75 ms */
if ((ticks - ticks_start) >= 176) {
#ifdef DEBUG
printf_P(PSTR("max\n"));
#endif /* DEBUG */
return -1;
}
}
bit_count = 9;
pldcnt++;
pld[pldcnt] = 0;
}
}
}
static void
send_start_bit(void)
{
/* Pull the line low for 3.7 ms and then high again until the 4.5 ms
* mark. This function doesn't produce the final falling edge of the
* start bit - that is left to send_data_bit(). */
uint16_t ticks;
uint16_t ticks_start;
ticks_start = TCNT1;
/* Pull the CEC line low. */
DDRD = 0x40;
for (;;) {
ticks = TCNT1;
/* 3.7 ms */
if ((ticks - ticks_start) >= 237) {
break;
}
}
/* Set the CEC line back to high-Z. */
DDRD = 0x00;
for (;;) {
ticks = TCNT1;
/* 4.5 ms */
if ((ticks - ticks_start) >= 288) {
break;
}
}
}
static void
send_data_bit(int8_t bit)
{
/* A data bit consists of a falling edge at T=0ms, a rising edge, and
* another falling edge at T=2.4ms. The timing of the rising edge
* determines the bit value. The rising edge for an optimal logical 1
* occurs at T=0.6ms. The rising edge for an optimal logical 0 occurs
* at T=1.5ms. */
uint16_t ticks;
uint16_t ticks_start;
ticks_start = TCNT1;
/* Pull the CEC line low. */
DDRD = 0x40;
for (;;) {
ticks = TCNT1;
if (bit) {
/* 0.6 ms */
if ((ticks - ticks_start) >= 39) {
break;
}
}
else {
/* 1.5 ms */
if ((ticks - ticks_start) >= 96) {
break;
}
}
}
/* Set the CEC line back to high-Z. */
DDRD = 0x00;
for (;;) {
ticks = TCNT1;
/* 2.4 ms */
if ((ticks - ticks_start) >= 154) {
break;
}
}
}
static void
send_frame(uint8_t pldcnt, uint8_t *pld)
{
uint8_t bit_count;
uint8_t i;
send_start_bit();
for (i = 0; i < pldcnt; i++) {
bit_count = 7;
/* Information bits. */
do {
send_data_bit((pld[i] >> bit_count) & 0x01);
} while (bit_count--);
/* EOM bit. */
send_data_bit(i == (pldcnt - 1));
/* ACK bit (we will assume the block was received). */
send_data_bit(1);
}
}
static void
device_vendor_id(uint8_t initiator, uint8_t destination, uint32_t vendor_id)
{
uint8_t pld[5];
pld[0] = (initiator << 4) | destination;
pld[1] = 0x87;
pld[2] = (vendor_id >> 16) & 0x0ff;
pld[3] = (vendor_id >> 8) & 0x0ff;
pld[4] = (vendor_id >> 0) & 0x0ff;
send_frame(5, pld);
}
static void
report_power_status(uint8_t initiator, uint8_t destination, uint8_t power_status)
{
uint8_t pld[3];
pld[0] = (initiator << 4) | destination;
pld[1] = 0x90;
pld[2] = power_status;
send_frame(3, pld);
}
static void
set_system_audio_mode(uint8_t initiator, uint8_t destination, uint8_t system_audio_mode)
{
uint8_t pld[3];
pld[0] = (initiator << 4) | destination;
pld[1] = 0x72;
pld[2] = system_audio_mode;
send_frame(3, pld);
}
static void
system_audio_mode_status(uint8_t initiator, uint8_t destination, uint8_t system_audio_mode_status)
{
uint8_t pld[3];
pld[0] = (initiator << 4) | destination;
pld[1] = 0x7e;
pld[2] = system_audio_mode_status;
send_frame(3, pld);
}
static void
set_osd_name(uint8_t initiator, uint8_t destination)
{
uint8_t pld[15] = {
0, 0x47,
'P', 'i', 'o', 'n', 'e', 'e', 'r', 'S', 'X', '-', '9', '5', '0' };
pld[0] = (initiator << 4) | destination;
send_frame(15, pld);
}
static void
report_physical_address(uint8_t initiator, uint8_t destination, uint16_t physical_address, uint8_t device_type)
{
uint8_t pld[5];
pld[0] = (initiator << 4) | destination;
pld[1] = 0x84;
pld[2] = (physical_address >> 8) & 0x0ff;
pld[3] = (physical_address >> 0) & 0x0ff;
pld[4] = device_type;
send_frame(5, pld);
}
ISR (TIMER1_COMPA_vect)
{
#ifdef DEBUG
printf_P(PSTR("<int>\n"));
#endif
PORTB = 0;
wdt_reset();
}
int
main(void)
{
/* Operate a motorized potentiometer based on HDMI CEC volume commands.
* Pins 0 and 1 of port B are connected to the A inputs of an L293DNE
* half-H driver. The HDMI CEC line is connected to pin 6 of port D.
* The AVR is clocked at 16.384 MHz. */
uint8_t pld[16];
int8_t pldcnt;
uint8_t initiator, destination;
uint8_t repeat = 0;
#ifdef DEBUG
int i;
#endif /* DEBUG */
/* This needs to be executed first to make sure the motor is stopped,
* just in case we end up here by way of the watchdog timer. */
PORTB = 0;
/* The CEC line is an open-collector bus, so we leave PORTD permanently
* set to 0. This allows us to toggle its pins between low impedance
* and high impedance by setting and clearing bits in DDRD. */
PORTD = 0;
wdt_enable(WDTO_2S);
#ifdef DEBUG
uart_init();
stdout = &uart_stdout;
printf_P(PSTR("s\n"));
#endif /* DEBUG */
/* Set up timer 1 to count up at CLK / 256. This comes out to 64000
* ticks per second. Timing will be achieved by sampling TCNT1 at
* different times and comparing the difference. */
TCCR1A = 0;
TCCR1B = _BV(CS12); /* CLK / 256 */
TCNT1 = 0;
/* Also enable interrupts for timer 1. For momentary volume changes,
* the main loop will turn on the motor and set OCR1A to STEP_TICKS in
* the future. The interrupt handler will turn off the motor. The
* interrupt is also used during for long presses as an added safety
* measure. */
TIMSK = _BV(OCIE1A);
sei();
DDRB = 0x03;
for (;;) {
pldcnt = recv_frame(pld, ADDRESS);
if (pldcnt < 0) {
#ifdef DEBUG
printf_P(PSTR("error %i\n"), pldcnt);
#endif /* DEBUG */
continue;
}
initiator = (pld[0] & 0xf0) >> 4;
destination = pld[0] & 0x0f;
#ifdef DEBUG
for (i = 0; i < pldcnt - 1; i++) {
printf_P(PSTR("%02x:"), pld[i]);
}
printf_P(PSTR("%02x\n"), pld[i]);
#endif /* DEBUG */
if ((destination == ADDRESS) && (pldcnt > 0)) {
switch (pld[1]) {
case 0x8c:
#ifdef DEBUG
/* Give Device Vendor ID */
printf_P(PSTR("<vendor id>\n"));
#endif /* DEBUG */
_delay_ms(13);
device_vendor_id(ADDRESS, 0x0f, 0x000045);
break;
case 0x8f:
#ifdef DEBUG
/* Give Device Power Status */
printf_P(PSTR("<power status>\n"));
#endif /* DEBUG */
_delay_ms(13);
/* On */
report_power_status(ADDRESS, initiator, 0x00);
break;
case 0x46:
#ifdef DEBUG
/* Give OSD Name */
printf_P(PSTR("<osd name>\n"));
#endif /* DEBUG */
_delay_ms(13);
set_osd_name(ADDRESS, initiator);
break;
case 0x83:
#ifdef DEBUG
/* Give Physical Address */
printf_P(PSTR("<phys address>\n"));
#endif /* DEBUG */
_delay_ms(13);
report_physical_address(ADDRESS, 0x0f, 0x0005, 0x05);
break;
case 0x70:
#ifdef DEBUG
/* System Audio Mode Request */
printf_P(PSTR("<sys audio>\n"));
#endif /* DEBUG */
_delay_ms(13);
set_system_audio_mode(ADDRESS, 0x0f, 1);
break;
case 0x7d:
#ifdef DEBUG
/* Give System Audio Mode Status */
printf_P(PSTR("<sys audio status>\n"));
#endif /* DEBUG */
_delay_ms(13);
system_audio_mode_status(ADDRESS, 0x0f, 1);
break;
case 0x44:
if (pld[2] == 0x41) {
/* Volume Up Pressed */
#ifdef DEBUG
printf_P(PSTR("<vol up>\n"));
#endif /* DEBUG */
repeat = ((PORTB & 0x01) != 0);
OCR1A = TCNT1 + STEP_TICKS;
TIFR = _BV(OCF1A);
TIMSK = _BV(OCIE1A);
PORTB = 0x01;
}
else if (pld[2] == 0x42) {
/* Volume Down Pressed */
#ifdef DEBUG
printf_P(PSTR("<vol down>\n"));
#endif /* DEBUG */
repeat = ((PORTB & 0x02) != 0);
OCR1A = TCNT1 + STEP_TICKS;
TIFR = _BV(OCF1A);
TIMSK = _BV(OCIE1A);
PORTB = 0x02;
}
break;
case 0x45:
#ifdef DEBUG
printf_P(PSTR("<released>\n"));
#endif /* DEBUG */
if (repeat)
PORTB = 0;
break;
default:
break;
}
}
}
return 0;
}