3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 David Prentice (david.prentice [at] farming [dot] uk)
11 Copyright 2010 Peter Danneger
12 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
14 Permission to use, copy, modify, distribute, and sell this
15 software and its documentation for any purpose is hereby granted
16 without fee, provided that the above copyright notice appear in
17 all copies and that both that the copyright notice and this
18 permission notice and warranty disclaimer appear in supporting
19 documentation, and that the name of the author not be used in
20 advertising or publicity pertaining to distribution of the
21 software without specific, written prior permission.
23 The author disclaim all warranties with regard to this
24 software, including all implied warranties of merchantability
25 and fitness. In no event shall the author be liable for any
26 special, indirect or consequential damages or any damages
27 whatsoever resulting from loss of use, data or profits, whether
28 in an action of contract, negligence or other tortious action,
29 arising out of or in connection with the use or performance of
35 volatile bool srx_done
;
36 volatile uint8_t stx_count
;
37 static uint8_t srx_data
, srx_mask
, srx_tmp
, stx_data
;
39 void SoftUART_TxByte(uint8_t Byte
)
47 uint8_t SoftUART_RxByte(void)
55 void SoftUART_Init(void)
57 OCR2B
= TCNT2
+ 1; // force first compare
58 TCCR2A
= (1 << COM2B1
) | (1 << COM2B0
); // T1 mode 0
59 TCCR2B
= (1 << FOC2B
) | (1 << CS21
); // CLK/8, T1 mode 0
60 TIMSK2
= (1 << OCIE2B
); // enable tx and wait for start
61 EICRA
= (1 << ISC01
); // -ve edge
62 EIMSK
= (1 << INT0
); // enable INT0 interrupt
64 stx_count
= 0; // nothing to send
65 srx_done
= false; // nothing received
66 STXPORT
|= (1 << STX
); // TX output
67 STXDDR
|= (1 << STX
); // TX output
68 SRXPORT
|= (1 << SRX
); // pullup on INT0
71 /* ISR to detect the start of a bit being sent from the transmitter. */
74 OCR2A
= TCNT2
+ (BIT_TIME
/ 8 * 3 / 2); // scan 1.5 bits after start
76 srx_tmp
= 0; // clear bit storage
77 srx_mask
= 1; // bit mask
79 TIFR2
= (1 << OCF2A
); // clear pending interrupt
81 if (!(SRXPIN
& (1 << SRX
))) // still low
83 TIMSK2
= (1 << OCIE2A
) | (1 << OCIE2B
); // wait for first bit
84 EIMSK
&= ~(1 << INT0
);
88 /* ISR to manage the reception of bits to the transmitter. */
89 ISR(TIMER2_COMPA_vect
)
93 if (SRXPIN
& (1 << SRX
))
98 OCR2A
+= BIT_TIME
/ 8; // next bit slice
102 srx_done
= true; // mark rx data valid
103 srx_data
= srx_tmp
; // store rx data
104 TIMSK2
= (1 << OCIE2B
); // enable tx and wait for start
105 EIMSK
|= (1 << INT0
); // enable START irq
106 EIFR
= (1 << INTF0
); // clear any pending
110 /* ISR to manage the transmission of bits to the receiver. */
111 ISR(TIMER2_COMPB_vect
)
113 OCR2B
+= BIT_TIME
/ 8; // next bit slice
117 if (--stx_count
!= 9) // no start bit
119 if (!(stx_data
& 1)) // test inverted data
120 TCCR2A
= (1 << COM2B1
) | (1 << COM2B0
);
122 TCCR2A
= (1 << COM2B1
);
124 stx_data
>>= 1; // shift zero in from left
128 TCCR2A
= (1 << COM2B1
); // START bit