0591284e6a19f94a2c76b0ad4143f9230125f8d4
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 * Software UART for both data transmission and reception. This
36 * code continuously monitors the ring buffers set up by the main
37 * project source file and reads/writes data as it becomes available.
42 static uint8_t TX_BitsRemaining
, TX_Data
;
43 static uint8_t RX_BitMask
, RX_Data
;
45 void SoftUART_Init(void)
47 OCR2B
= TCNT2
+ 1; // force first compare
48 TCCR2A
= (1 << COM2B1
) | (1 << COM2B0
); // T1 mode 0
49 TCCR2B
= (1 << FOC2B
) | (1 << CS21
); // CLK/8, T1 mode 0
50 TIMSK2
= (1 << OCIE2B
); // enable tx and wait for start
51 EICRA
= (1 << ISC01
); // -ve edge
52 EIMSK
= (1 << INT0
); // enable INT0 interrupt
54 STXPORT
|= (1 << STX
); // TX output
55 STXDDR
|= (1 << STX
); // TX output
56 SRXPORT
|= (1 << SRX
); // pullup on INT0
59 /* ISR to detect the start of a bit being sent to the software UART. */
62 OCR2A
= TCNT2
+ (uint16_t)((BIT_TIME
/ 8.0f
) * 1.5f
); // scan 1.5 bits after start
64 RX_Data
= 0; // clear bit storage
65 RX_BitMask
= (1 << 0); // bit mask
67 TIFR2
= (1 << OCF2A
); // clear pending interrupt
69 if (!(SRXPIN
& (1 << SRX
))) // still low
71 TIMSK2
= (1 << OCIE2A
) | (1 << OCIE2B
); // wait for first bit
72 EIMSK
&= ~(1 << INT0
);
76 /* ISR to manage the reception of bits to the software UART. */
77 ISR(TIMER2_COMPA_vect
)
81 if (SRXPIN
& (1 << SRX
))
82 RX_Data
|= RX_BitMask
;
86 OCR2A
+= BIT_TIME
/ 8; // next bit slice
90 RingBuffer_Insert(&UARTtoUSB_Buffer
, RX_Data
);
92 TIMSK2
= (1 << OCIE2B
); // enable tx and wait for start
93 EIMSK
|= (1 << INT0
); // enable START irq
94 EIFR
= (1 << INTF0
); // clear any pending
98 /* ISR to manage the transmission of bits via the software UART. */
99 ISR(TIMER2_COMPB_vect
)
101 OCR2B
+= BIT_TIME
/ 8; // next bit slice
103 if (TX_BitsRemaining
)
105 if (--TX_BitsRemaining
!= 9) // no start bit
107 if (TX_Data
& (1 << 0)) // test inverted data
108 TCCR2A
= (1 << COM2B1
);
110 TCCR2A
= (1 << COM2B1
) | (1 << COM2B0
);
112 TX_Data
>>= 1; // shift zero in from left
116 TCCR2A
= (1 << COM2B1
); // START bit
119 else if (USBtoUART_Buffer
.Count
)
121 TX_Data
= ~RingBuffer_Remove(&USBtoUART_Buffer
);
122 TX_BitsRemaining
= 10;