-
Notifications
You must be signed in to change notification settings - Fork 5
/
avrlirc.c
771 lines (682 loc) · 21.3 KB
/
avrlirc.c
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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
/*
* avrlirc.c - IR Remote control to RS323 receiver
*
* run-length encodes the ir pulse/space receive signal to
* offload PC of low level timing.
*
* AVR ATTiny2313 pinout :
* ----------
* (rst for prog) /reset |1 + 20| Vcc
* (dbg only) rxd,pd0 |2 + 19| pb7,pci7,scl (scl, for prog)
* (to pin 16) txd,pd1 |3 + 18| pb6,pci6,miso (miso, for prog)
* xtal2,pa1 |4 17| pb5,pci5,mosi (mosi, for prog)
* xtal1,pa0 |5 16| pb4,pci4,oc1b (txout, from pin 3)
* (gnd for Fox) int0,pd2 |6 15| pb3,pci3,oc1a (/txout, to DB9-2)
* (gnd for enable) pd3 |7 + 14| pb2,pci2,oc0a (gnd for UUUUUU)
* (dbg led #2) t0,pd4 |8 + 13| pb1,pci1,ain1 (acts as Vcc for IR-Recv)
* (act led #1) t1,pd5 |9 + 12| pb0,pci0,ain0 (acts as GND for IR-Recv)
* (db-9 pin 5) Gnd |10+ + 11| pd6,icp (input, IR-Recv))
* ----------
* ('+' denotes required for operation with CHIP)
*
*
* hardware setup:
*
* ATTiny2313 running from internal 8Mhz osc. pins 15/16 form a
* "software inverter" which can be used in place of a hardware
* RS232 line driver. (i.e., pin 15 will drive the RS232 RX line
* directly, at TTL levels. most consumer-grade serial ports
* will tolerate this.)
*
* for debug, if you want to send characters _to_ the avr, or if
* you want more reliable/standard RS232 interfacing, use
* MAX232/3 or equivalent to connect rxd/txd to db-9 pins 2 and
* 3. 5V supplied from USB serial device, which is also used for
* serial connection.
*
* for high-speed (115200) operation, use a crystal with a couple 22pf
* caps. at high speeds, you'll probably need to use txd (pin 3)
* directly, and connect it via an RS232 line driver (max232 or
* equiv). but note that in practice, i've had zero problems that
* could be attributed to the baud rate being too low -- i use 38400
* baud exclusively, which lets me dispense with the crystal, the caps,
* and also means that the chip's fuse settings don't need to be changed.
*
* the IR receiver should be something like the Vishay TSOP3438 (2.5V
* to 5.5V) or the Sharp GP1UX511QS (5V only).
*
* 1) bare minimum net-list, if connecting to a physical serial port:
* at 2313, connect
* pin 3 to pin 16 (connects TXD to the "software inverter")
* pin 10 to gnd
* pin 11 to the output (Vout) lead from IR detector
* pin 15 to the RX pin (DB-9 pin 2) of the RS232 serial port
* pin 20 to +5
* put a .1uf cap between pin 10 and pin 20
*
* 2) bare minimum net-list, if connecting to digital I/O lines, as on a $9
* CHIP computer:
* pin 3 to the RX input on the computer
* for optional two-way comms, connect pin 2 to computers TX output
* pin 10 to gnd
* pin 11 to the output (Vout) lead from IR detector
* pin 20 to +3.3V
* put a .1uf cap between pin 10 and pin 20
*
* 3) connect IR receiver to GND and Vcc, or to pins 12 and 13. these pins
* are programmed as "low" and "high" respectively, and the 2313
* GPIO pinssupply plenty of current for powering the IR receiver.
*
* 4) if connecting the activity LED:
* pin 9 to to cathode (short lead or flat side) of the LED
* connect anode of LED to 400ohm resistor
* connect other end of resistor to Vcc
*
* 5) at IR detector, refer to the datasheet for you specific
* part. connect a 10uf to 50uf cap between Vcc and gnd, very
* close to the detector. (though i've left it off, with no ill effects)
*
* for both the Vishay TSOP3438 and Sharp GP1UX511QS:
* +---+
* | O | (looking at the front)
* | |
* TTT
* ||| pin 1 is Vout ('o' to the left)
* 123 pin 2 is GND ('g')
* ogV pin 3 is Vcc ('V')
*
* (Note! Some IR receivers swap Vcc and GND)
*
*
* the serial ouptput stream is RS232 data at 38400,8N1. no flow
* control. (or 115200 if using crystal)
* - in binary (default) mode, we emit the lirc "udp" protocol:
* packets consist of some number of little-endian 16-bit
* words. the high bit signifies whether the received
* signal was high or low; the low 15 bits specify the
* number of 1/16384-second intervals the signal lasted,
* with a minimum value of 1. since transmit data is buffered,
* baud rates slower than the pulse arrival rate are tolerated.
* two zero bytes in a row (which can't occur otherwise) are
* reserved as an escape mechanism for someday sending other
* types of data.
* - ascii mode is a simple command/response, for debugging. requires
* max232 or equiv. line driver -- don't connect the RS232 TX
* signal directly to your AVR!!! enable the ability to run
* in ascii mode by defining DO_RECEIVE.
*
* both the LEDs are optional. Led #1 (pin 9) is somewhat useful --
* it indicates activity from the IR receiver. Led #2 is only
* used for debugging -- don't populate it unless you're sure you
* need it.
*
* to assist in verifying that the data transmit path is okay, the AVR
* will transmit an endless stream of 'U' characters if pin 14 is
* grounded while the chip comes out of reset. a stream of ascii 'U'
* characters appears as a square wave when viewed on an oscilloscope.
* the rest of the interrupts are still active, to allow for testing,
* but normal data transmission is bypassed.
*
* similarly, grounding pin 6 will cause continuous transmission of a
* "quick brown fox" message, to further help verify the data path.
*
**********
*
* Copyright 2002 Karl Bongers (karl@turbobit.com)
* Copyright 2007 Paul Fox (pgf@foxharp.boston.ma.us)
* Copyright 2017 Paul Fox (pgf@foxharp.boston.ma.us)
*
* 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 2 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, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
/*
* fuses: default for tiny2313: low 0x64 high 0xdf
* or, for external crystal, 8Mhz and up, no div by 8, change
* low to: 0xff
* high 0xdb (for brownout at 2.7v)
*
* default fuse values work fine
*/
/* personal preference macros/typedefs */
#define bit(x) _BV(x)
typedef uint16_t word;
typedef uint8_t byte;
/* the UART reception code should probably remain off -- it was really only
* needed for initial debugging. but it works, and could be used to support
* adding small bits of code -- e.g., the ability to toggle I/O pins, etc.
* enabling it as it stands now costs about 360 bytes.
*/
#define DO_RECEIVE 1
/* if connecting to the console RX/TX on a $9 CHIP computer, there's a
* risk that an IR sequence will cause data to be received on the
* computer console while it's booting, thereby halting the boot
* sequence. after booting, that data might also "fight" with the
* "login:" message. so in that use case, USE_ENABLE should be set to
* 1, and pin 7 should be pulled low by the CHIP computer after the
* system has booted, and after the getty program (which controls the
* login prompt) has been disabled off).
*/
#define USE_ENABLE 0
/*
* speed selection
*/
// #define FOSC 3686400 // STK500, ext. clock, for example
// #define FOSC 7372800 // with crystal, or int. RC w/ OSCCAL recal.
#define FOSC 8000000 // 38.4Kbaud) (internal osc)
// #define FOSC 12000000
// #define FOSC 11059200
// #define FOSC 14745600
/*
* GPIO usage
*/
#define IR_GND PB0
#define IR_VCC PB1
#define DO_DEBUG PB2 // pin 12, input: ground to force debug loop
#define TX_INVERT_OUT PB3
#define TX_INVERT_IN PB4
// #define MOSI_BITNUM PB5 // pin17, MOSI, input
// #define MISO_BITNUM PB6 // pin18, MISO (output)
// #define SCK_BITNUM PB7 // pin19, SCK, input
// #define RX_BITNUM PD0 // pin 1
// #define TX_BITNUM PD1 // pin 2
#define DO_FOX PD2 // pin 6
#define OUTPUT_ENABLE PD3 // pin 7, input: ground to enable serial output
#define LED2_BITNUM PD4 // pin 8, output (debug LED)
#define LED1_BITNUM PD5 // pin 9, output (activity LED)
#define IRREC_BITNUM PD6 // pin 11 -- input: from IR receiver
/* hardware access macros */
#define Led1_Off() { PORTD |= bit(LED1_BITNUM); }
#define Led1_On() { PORTD &= ~bit(LED1_BITNUM); }
#define Led1_Flip() { PORTD ^= bit(LED1_BITNUM); }
#define Led2_Off() { PORTD |= bit(LED2_BITNUM); }
#define Led2_On() { PORTD &= ~bit(LED2_BITNUM); }
#define Led2_Flip() { PORTD ^= bit(LED2_BITNUM); }
#define IR_is_high() (PIND & bit(IRREC_BITNUM))
#define do_fox() ((PIND & bit(DO_FOX)) == 0)
#if USE_ENABLE
# define output_enabled() ((PIND & bit(OUTPUT_ENABLE)) == 0)
#else
# define output_enabled() (1)
#endif
#define do_uuuu() ((PINB & bit(DO_DEBUG)) == 0)
// values for TCCR1B
#define CLKDIV_8 2
#define CLKDIV_64 3
#define CLKDIV_256 4
#define CLKDIV_1024 5
/* alternate version of ISR() macro, which doesn't disable all
* interrupts.
*/
#define INTERRUPTIBLE_ISR(vector) \
void vector (void) __attribute__((interrupt)); \
void vector (void)
volatile word pulse_length;
volatile byte pulse_is_high;
volatile byte had_overflow;
static const char version_s[] PROGMEM = AVRLIRC_VERSION;
static const char fox_s[] PROGMEM = "The Quick Brown Fox Jumped Over the Lazy Dog's Back\r\n";
#if DO_RECEIVE
static const char error_s[] PROGMEM = "try (h)elp";
static const char usage_s[] PROGMEM = "(a)scii (b)inary (i)r (v)ers (f)ox (m)cuusr (U)UUU";
static const char ascii_s[] PROGMEM = "ascii";
static const char binary_s[] PROGMEM = "binary";
static const char crnl_s[] PROGMEM = "\r\n";
volatile byte ascii;
#endif
#define TX_QLEN 64 // NB! power of 2
#define TX_QLEN_MASK (TX_QLEN - 1)
volatile byte tx_r, tx_w;
volatile byte tx_queue[TX_QLEN];
volatile byte mcusr_mirror;
// verify the crystal freq. config
#if FOSC != 14745600 && FOSC != 12000000 && \
FOSC != 11059200 && FOSC != 8000000 && \
FOSC != 7372800 && FOSC != 3686400
# error unsupported FOSC value
#endif
/*
* set up initial chip conditions
*/
void
hw_init(void)
{
mcusr_mirror = MCUSR;
MCUSR = 0;
wdt_disable();
// eliminate div-by-8 (no-op if 'div by 8' clock fuse not programmed)
CLKPR = bit(CLKPCE);
CLKPR = 0;
// setup outputs and pullups
// port B
DDRB = bit(TX_INVERT_OUT);
DDRB |= bit(IR_VCC);
DDRB |= bit(IR_GND);
PORTB |= bit(IR_VCC);
PORTB &= ~bit(IR_GND);
// PORTB |= bit(TX_INVERT_IN); // enable pull-up on input
PORTB |= bit(TX_INVERT_OUT); // set output high
PORTB |= bit(DO_DEBUG); // enable pull-up
// port D -- just leds are outputs
DDRD |= bit(LED1_BITNUM);
DDRD |= bit(LED2_BITNUM);
// turn off leds
PORTD |= bit(LED1_BITNUM);
PORTD |= bit(LED2_BITNUM);
// enable pullup on IR recvr, and on the debug-mode pin
PORTD |= bit(IRREC_BITNUM);
PORTD |= bit(OUTPUT_ENABLE); // turn on pullup on output enable
PORTD |= bit(DO_FOX);
// set up pin-change interrupt for doing inversion on uart TX
PCMSK = bit(PCINT4); // enable pin-change for PB4
GIMSK = bit(PCIE); // enable pin-change interrupts
// disable analog comparator -- saves power
ACSR = bit(ACD);
// these baud rate divisor values taken from datasheet
#if FOSC == 14745600
UBRRL = 7; // for 115200 baud at 14.7456Mhz
#elif FOSC == 12000000
UBRRL = 12; // for 115200 baud at 12Mhz
UCSRA |= bit(U2X); // doubletime
#elif FOSC == 11059200
UBRRL = 5; // for 115200 baud at 11.0592Mhz
#elif FOSC == 8000000
UBRRL = 12; // for 38400 baud at 8Mhz
// UBRRL = 51; // for 19200 baud at 8Mhz,
// UCSRA |= bit(U2X); // needs doubletime
#elif FOSC == 7372800
UBRRL = 3; // for 115200 baud at 7.3728Mhz, or detuned 8Mz
#elif FOSC == 3686400
UBRRL = 1; // for 115200 baud at 3.6864Mhz
#endif
// (UBRRH = 0;) // default poweron value
// enable rx and tx uart functions. tx interrupt gets enabled on demand.
UCSRB = bit(TXEN);
#if DO_RECEIVE
// enable rx interrupt.
UCSRB |= bit(RXEN) | bit(RXCIE);
#endif
// two stop bits -- only one would be faster, but the with repeated
// data streams, it's possible for the receiver to lock onto a
// stop/start transition in the middle of a byte, and then stay
// out os sync for a while.
UCSRC |= bit(USBS);
// set 8 bits, no parity, 1 stop bit
// (UCSRC = bit(UCSZ0) | bit(UCSZ1);) // default poweron value
// (TCCR1A = 0;) // default poweron value
TCCR1B = bit(ICNC1) | CLKDIV_256; // see comments at emit_pulse_data()
// timer1 overflow int enable, and input capture event int enable.
TIMSK = bit(TOIE1) | bit(OCIE1A) | bit(ICIE1);
// we use the output compare interrupt to turn off the
// "activity" LED. this value is around 1/20th of a
// second for all the "interesting" clock rates (see comments
// at emit_pulse_data(), below
OCR1A = 3000;
// (set_sleep_mode(SLEEP_MODE_IDLE);) // default poweron value
}
/*
* delay - wait a bit
*/
void
delay(word dly)
{
volatile word i;
volatile byte j;
for (i = dly; i != 0; i--)
for (j = 255; j != 0; j--)
/* nothing */;
}
/*
* wiggling light pattern, to show life at startup. very useful
* for visually detecting watchdog or crash.
*/
void
blinky(void)
{
byte i;
for (i = 0; i < 6; i++) {
delay(1000);
if (i & 1) {
Led1_Off();
} else {
Led1_On();
}
}
}
/*
* tx_char - send a serial character
*/
void
tx_char(byte t)
{
byte tmp;
if (!output_enabled())
return;
tmp = (tx_w + 1) & TX_QLEN_MASK;
#define WAIT_FOR_TX_SPACE 1 // tx_char() only called with ints enabled
#if WAIT_FOR_TX_SPACE
// if we were to call tx_char() with interrupts disabled,
// this condition will never be met. in that case, we could
// probably still make it work, but it's probably not worth
// it.
while (tmp == tx_r)
/* spin for freespace */;
#else
if (tmp == tx_r) {
return; // drop character
}
#endif
tx_queue[tmp] = t;
tx_w = tmp;
UCSRB |= bit(UDRIE);
}
#if DO_RECEIVE
/*
* tx_char_hex - send a serial nibble, in ascii hex
*/
void
tx_char_hex(byte t)
{
if (t <= 9)
tx_char(t + '0');
else
tx_char(t + 'a' - 10);
}
/*
* tx_hexword - send up to a word as hex
*/
void
tx_hexword(word v)
{
tx_char_hex((v >> 12) & 0xf);
tx_char_hex((v >> 8) & 0xf);
tx_char_hex((v >> 4) & 0xf);
tx_char_hex((v >> 0) & 0xf);
}
#endif
void
tx_str_p(const char *s)
{
char c;
while ( (c = pgm_read_byte(s++)) )
tx_char(c);
}
/*
* tx_word - send 16 bits, little-endian, optionally in ascii
*/
void
tx_word(word t)
{
#if DO_RECEIVE
if (ascii) {
tx_hexword(t);
tx_str_p(crnl_s);
return;
}
#endif
tx_char(t & 0xff);
tx_char((t >> 8) & 0xff);
}
void
UUUU_loop()
{
/* to get a uniform square wave, we change the number of stop bits
* to just 1.
*/
UCSRC &= ~bit(USBS);
for(;;) {
wdt_reset();
tx_char('U'); /* square wave */
Led2_Flip();
}
}
/*
* timer1 overflow interrupt handler.
* if we hit the overflow without getting a transition on the IR
* line, then we're certainly "between" IR packets. we save the
* overflow indication until just before the next "real" pulse --
* lirc wants to see it then (just before the "real" data),
* rather than at the end.
*/
INTERRUPTIBLE_ISR(TIMER1_OVF_vect)
{
byte tmp;
if (IR_is_high())
tmp = 0xff; // high byte of eventual dummy pulselen
else
tmp = 0x7f;
had_overflow = tmp;
}
/*
* timer1 compare match interrupt handler. this is simply a way
* of turning off the "IR message received" LED sooner.
* otherwise we could do it in the overflow handler. this has no
* affect on the timing protocol, but doing it here makes the LED
* behavior match the user's button presses a little more
* closely.
*/
INTERRUPTIBLE_ISR(TIMER1_COMPA_vect)
{
Led1_Off();
}
/*
* input capture event handler
* the "event" is a transition on the IR line. we save the
* captured count, and restart the timer from zero again.
*/
INTERRUPTIBLE_ISR(TIMER1_CAPT_vect)
{
// read the event
pulse_length = ICR1;
// and save the new state of the IR line.
pulse_is_high = IR_is_high();
// restart the timer
TCNT1 = 0;
// change detection edge, and clear interrupt flag -- it's
// set as result of detection edge change
cli();
TCCR1B ^= bit(ICES1);
TIFR &= ~bit(ICF1);
sei();
}
/*
* uart transmit interrupt handler.
*/
ISR(USART_UDRE_vect)
{
// disable uart interrupt, so we can reenable interrupts in general
UCSRB &= ~bit(UDRIE);
sei();
// if data in queue, send it, and reenable the interrupt
if (tx_r != tx_w) {
tx_r = (tx_r + 1) & TX_QLEN_MASK;
UDR = tx_queue[tx_r];
UCSRB |= bit(UDRIE);
}
}
/*
* pc_int - pin change interrupt.
* this is purely and simply an inversion function -- we want to
* present an inverted copy of the USART's TX output to the host's
* serial port. so we watch for changes on one pin, and update the other.
* since this operation needs minimal latency, we reenable interrupts
* wherever possible in the other interrupt handlers.
*/
ISR(PCINT_vect)
{
if (PINB & bit(TX_INVERT_IN))
PORTB &= ~bit(TX_INVERT_OUT);
else
PORTB |= bit(TX_INVERT_OUT);
}
#if DO_RECEIVE
/*
* uart_rx - Uart Rx interrupt handler.
* this command-line interface was used early in development.
*/
ISR(USART_RX_vect)
{
byte c;
Led2_Flip();
c = UDR;
switch (c) {
case 'h':
tx_str_p(usage_s);
break;
case 'a':
ascii = 1;
tx_str_p(ascii_s);
break;
case 'b':
ascii = 0;
tx_str_p(binary_s);
break;
case 'i':
tx_char(IR_is_high() ? '1':'0');
break;
case 'm':
tx_hexword(mcusr_mirror); /* reset reason, etc */
break;
case 'f':
tx_str_p(fox_s); /* quick brown fox */
break;
case 'U':
sei();
UUUU_loop(); /* no return! */
break;
case 'v':
tx_str_p(version_s); /* version */
break;
default:
tx_str_p(error_s);
break;
}
tx_str_p(crnl_s);
}
#endif
/*
* we want the timer overflow to be (a lot) longer than the
* longest interval we need to record using ICR1, which is
* something like .25 sec. we also need to convert from
* timer count intervals to 16384'ths of a second.
*
* 14.7456Mhz
* 14745600 counts/sec, prescaled by 256, gives 57600 counts/sec,
* or 17.36usec/count, times 65536 gives overflow at 1.14sec. good.
* want 16384'ths: scale count by 16384 / 57600. ( 4096 / 14400 )
*
* 12.0000Mhz
* 12000000 counts/sec, prescaled by 256, gives 46875 counts/sec,
* or 21.33usec/count, times 65536 gives overflow at 1.40sec. good.
* want 16384'ths: scale count by 16384 / 46875. ( 4096 / 11719 )
*
* 11.0592
* 11059200 counts/sec, prescaled by 256, gives 43200 counts/sec,
* or 23.15usec/count, times 65536 gives overflow at 1.51sec. good.
* want 16384'ths: scale count by 16384 / 43200. ( 4096 / 10800 )
*
* 8.0000Mhz
* 8000000 counts/sec, prescaled by 256, gives 31250 counts/sec,
* or 32usec/count, times 65536 gives overflow at 2.09sec. good.
* want 16384'ths: scale count by 16384 / 31250. ( 4096 / 7812 )
*
* 3.6864Mhz
* 3686400/256 --> 14400, so scale by 16384 / 14400 --> 4096 / 3600
*
*/
#define scale_denom(fosc) ((fosc / 256) / 4)
void
emit_pulse_data(void)
{
word len;
byte high;
byte overflow;
while (pulse_length) {
cli();
len = pulse_length;
high = pulse_is_high;
overflow = had_overflow;
pulse_length = had_overflow = 0;
sei();
Led1_On();
if (overflow) {
// if we had an overflow, then the current pulse_length
// is meaningless -- it's just the last remnant of a
// long gap. just send the previously recorded
// overflow value to indicate that gap. this is
// effectively the start of a "packet".
tx_word((overflow << 8) | 0xff);
} else {
uint32_t l;
/* do long arithmetic. expensive, but we have time. */
l = (uint32_t)len * 4096 / scale_denom(FOSC);
if (l > 0x7fff) // limit range.
len = 0x7fff;
else
len = l;
if (len == 0) // pulse length never zero.
len++;
if (!high) // report the state we transitioned out of
len |= 0x8000;
tx_word(len);
}
}
}
/*
* main -
*/
int
main(void)
{
hw_init();
blinky();
wdt_enable(WDTO_4S);
sei();
if (do_uuuu()) {
/* a perfect square wave is useful for debugging the TX
* inversion, and baud rate stability.
*/
UUUU_loop(); /* no return! */
}
if (do_fox()) {
/* a "quick brown fox" message is good for checking data integrity.
*/
for(;;) {
wdt_reset();
tx_str_p(version_s);
tx_str_p(fox_s);
}
/* not reached */
}
for(;;) {
wdt_reset();
cli();
if (!pulse_length) {
// only sleep if there's no pulse data to emit
// (see <sleep.h> for explanation of this snippet)
sleep_enable();
sei();
sleep_cpu();
sleep_disable();
}
sei();
emit_pulse_data();
}
/* not reached */
}