SoftUART.c 3.51 KB
Newer Older
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
/*
			 LUFA Library
	 Copyright (C) Dean Camera, 2010.
			  
  dean [at] fourwalledcubicle [dot] com
	  www.fourwalledcubicle.com
*/

/*
  Copyright 2010  David Prentice (david.prentice [at] farming [dot] uk)
  Copyright 2010  Peter Danneger
  Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com)

  Permission to use, copy, modify, distribute, and sell this 
  software and its documentation for any purpose is hereby granted
  without fee, provided that the above copyright notice appear in 
  all copies and that both that the copyright notice and this
  permission notice and warranty disclaimer appear in supporting 
  documentation, and that the name of the author not be used in 
  advertising or publicity pertaining to distribution of the 
  software without specific, written prior permission.

  The author disclaim all warranties with regard to this
  software, including all implied warranties of merchantability
  and fitness.  In no event shall the author be liable for any
  special, indirect or consequential damages or any damages
  whatsoever resulting from loss of use, data or profits, whether
  in an action of contract, negligence or other tortious action,
  arising out of or in connection with the use or performance of
  this software.
*/

33
34
35
36
37
38
/** \file
 *
 *  Software UART for both data transmission and reception. This
 *  code continuously monitors the ring buffers set up by the main
 *  project source file and reads/writes data as it becomes available.
 */
39

40
#include "SoftUART.h"
41

42
static uint8_t TX_BitsRemaining, TX_Data, RX_BitMask, RX_Data;
43
44
45
46
47
48
49
50
51
52

void SoftUART_Init(void)
{
	OCR2B  = TCNT2 + 1;						// force first compare
	TCCR2A = (1 << COM2B1) | (1 << COM2B0);	// T1 mode 0
	TCCR2B = (1 << FOC2B)  | (1 << CS21); 	// CLK/8, T1 mode 0
	TIMSK2 = (1 << OCIE2B);					// enable tx and wait for start
	EICRA  = (1 << ISC01);					// -ve edge
	EIMSK  = (1 << INT0);					// enable INT0 interrupt

53
	TX_BitsRemaining = 0;					// nothing to send
54
55
	STXPORT |= (1 << STX);					// TX output
	STXDDR  |= (1 << STX);					// TX output
56
57
58
59
60
61
62
63
	SRXPORT |= (1 << SRX);					// pullup on INT0
}

/* ISR to detect the start of a bit being sent from the transmitter. */
ISR(INT0_vect)
{
	OCR2A = TCNT2 + (BIT_TIME / 8 * 3 / 2);	// scan 1.5 bits after start

64
65
	RX_Data    = 0;							// clear bit storage
	RX_BitMask = (1 << 0);					// bit mask
66
67
68
69
70
71
72
73
74
75
76
77
78

	TIFR2 = (1 << OCF2A);					// clear pending interrupt

	if (!(SRXPIN & (1 << SRX)))				// still low
	{
		TIMSK2 = (1 << OCIE2A) | (1 << OCIE2B); // wait for first bit
		EIMSK &= ~(1 << INT0);
	}
}

/* ISR to manage the reception of bits to the transmitter. */
ISR(TIMER2_COMPA_vect)
{
79
	if (RX_BitMask)
80
81
	{
		if (SRXPIN & (1 << SRX))
82
		  RX_Data |= RX_BitMask;
83

84
		RX_BitMask <<= 1;
85
86
87
88
89

		OCR2A += BIT_TIME / 8;				// next bit slice
	}
	else
	{
90
91
		RingBuffer_Insert(&UARTtoUSB_Buffer, RX_Data);

92
93
94
95
96
97
98
99
100
101
102
		TIMSK2    = (1 << OCIE2B);			// enable tx and wait for start
		EIMSK    |= (1 << INT0);			// enable START irq
		EIFR      = (1 << INTF0);			// clear any pending
	}
}

/* ISR to manage the transmission of bits to the receiver. */
ISR(TIMER2_COMPB_vect)
{
	OCR2B += BIT_TIME / 8;					// next bit slice

103
	if (TX_BitsRemaining)
104
	{
105
		if (--TX_BitsRemaining != 9)		// no start bit
106
		{
107
			if (TX_Data & (1 << 0))			// test inverted data
108
			  TCCR2A = (1 << COM2B1);
109
110
			else
			  TCCR2A = (1 << COM2B1) | (1 << COM2B0);
111

112
			TX_Data >>= 1;					// shift zero in from left
113
114
115
116
117
118
		}
		else
		{
			TCCR2A = (1 << COM2B1);			// START bit
		}
	}
119
120
121
122
123
	else if (USBtoUART_Buffer.Count)
	{
		TX_Data = ~RingBuffer_Remove(&USBtoUART_Buffer);
		TX_BitsRemaining = 10;
	}
124
}