-
Notifications
You must be signed in to change notification settings - Fork 460
/
Serial.cpp
241 lines (222 loc) · 8.08 KB
/
Serial.cpp
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
#include "Arduino.h"
#include "config.h"
#include "def.h"
#include "Serial.h"
#include "MultiWii.h"
static volatile uint8_t serialHeadRX[UART_NUMBER],serialTailRX[UART_NUMBER];
static uint8_t serialBufferRX[RX_BUFFER_SIZE][UART_NUMBER];
static volatile uint8_t serialHeadTX[UART_NUMBER],serialTailTX[UART_NUMBER];
static uint8_t serialBufferTX[TX_BUFFER_SIZE][UART_NUMBER];
// *******************************************************
// For Teensy 2.0, these function emulate the API used for ProMicro
// it cant have the same name as in the arduino API because it wont compile for the promini (eaven if it will be not compiled)
// *******************************************************
#if defined(TEENSY20)
unsigned char T_USB_Available(){
int n = Serial.available();
if (n > 255) n = 255;
return n;
}
#endif
// *******************************************************
// Interrupt driven UART transmitter - using a ring buffer
// *******************************************************
#if defined(PROMINI) || defined(MEGA)
#if defined(PROMINI)
ISR(USART_UDRE_vect) { // Serial 0 on a PROMINI
#endif
#if defined(MEGA)
ISR(USART0_UDRE_vect) { // Serial 0 on a MEGA
#endif
uint8_t t = serialTailTX[0];
if (serialHeadTX[0] != t) {
if (++t >= TX_BUFFER_SIZE) t = 0;
UDR0 = serialBufferTX[t][0]; // Transmit next byte in the ring
serialTailTX[0] = t;
}
if (t == serialHeadTX[0]) UCSR0B &= ~(1<<UDRIE0); // Check if all data is transmitted . if yes disable transmitter UDRE interrupt
}
#endif
#if defined(MEGA) || defined(PROMICRO)
ISR(USART1_UDRE_vect) { // Serial 1 on a MEGA or on a PROMICRO
uint8_t t = serialTailTX[1];
if (serialHeadTX[1] != t) {
if (++t >= TX_BUFFER_SIZE) t = 0;
UDR1 = serialBufferTX[t][1]; // Transmit next byte in the ring
serialTailTX[1] = t;
}
if (t == serialHeadTX[1]) UCSR1B &= ~(1<<UDRIE1);
}
#endif
#if defined(MEGA)
ISR(USART2_UDRE_vect) { // Serial 2 on a MEGA
uint8_t t = serialTailTX[2];
if (serialHeadTX[2] != t) {
if (++t >= TX_BUFFER_SIZE) t = 0;
UDR2 = serialBufferTX[t][2];
serialTailTX[2] = t;
}
if (t == serialHeadTX[2]) UCSR2B &= ~(1<<UDRIE2);
}
ISR(USART3_UDRE_vect) { // Serial 3 on a MEGA
uint8_t t = serialTailTX[3];
if (serialHeadTX[3] != t) {
if (++t >= TX_BUFFER_SIZE) t = 0;
UDR3 = serialBufferTX[t][3];
serialTailTX[3] = t;
}
if (t == serialHeadTX[3]) UCSR3B &= ~(1<<UDRIE3);
}
#endif
void UartSendData(uint8_t port) {
#if defined(PROMINI)
UCSR0B |= (1<<UDRIE0);
#endif
#if defined(PROMICRO)
switch (port) {
case 0:
while(serialHeadTX[0] != serialTailTX[0]) {
if (++serialTailTX[0] >= TX_BUFFER_SIZE) serialTailTX[0] = 0;
#if !defined(TEENSY20)
USB_Send(USB_CDC_TX,serialBufferTX[serialTailTX[0]],1);
#else
Serial.write(serialBufferTX[serialTailTX[0]],1);
#endif
}
break;
case 1: UCSR1B |= (1<<UDRIE1); break;
}
#endif
#if defined(MEGA)
switch (port) {
case 0: UCSR0B |= (1<<UDRIE0); break;
case 1: UCSR1B |= (1<<UDRIE1); break;
case 2: UCSR2B |= (1<<UDRIE2); break;
case 3: UCSR3B |= (1<<UDRIE3); break;
}
#endif
}
#if defined(GPS_SERIAL)
bool SerialTXfree(uint8_t port) {
return (serialHeadTX[port] == serialTailTX[port]);
}
#endif
void SerialOpen(uint8_t port, uint32_t baud) {
uint8_t h = ((F_CPU / 4 / baud -1) / 2) >> 8;
uint8_t l = ((F_CPU / 4 / baud -1) / 2);
switch (port) {
#if defined(PROMINI)
case 0: UCSR0A = (1<<U2X0); UBRR0H = h; UBRR0L = l; UCSR0B |= (1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0); break;
#endif
#if defined(PROMICRO)
#if (ARDUINO >= 100) && !defined(TEENSY20)
case 0: UDIEN &= ~(1<<SOFE); break;// disable the USB frame interrupt of arduino (it causes strong jitter and we dont need it)
#endif
case 1: UCSR1A = (1<<U2X1); UBRR1H = h; UBRR1L = l; UCSR1B |= (1<<RXEN1)|(1<<TXEN1)|(1<<RXCIE1); break;
#endif
#if defined(MEGA)
case 0: UCSR0A = (1<<U2X0); UBRR0H = h; UBRR0L = l; UCSR0B |= (1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0); break;
case 1: UCSR1A = (1<<U2X1); UBRR1H = h; UBRR1L = l; UCSR1B |= (1<<RXEN1)|(1<<TXEN1)|(1<<RXCIE1); break;
case 2: UCSR2A = (1<<U2X2); UBRR2H = h; UBRR2L = l; UCSR2B |= (1<<RXEN2)|(1<<TXEN2)|(1<<RXCIE2); break;
case 3: UCSR3A = (1<<U2X3); UBRR3H = h; UBRR3L = l; UCSR3B |= (1<<RXEN3)|(1<<TXEN3)|(1<<RXCIE3); break;
#endif
}
}
void SerialEnd(uint8_t port) {
switch (port) {
#if defined(PROMINI)
case 0: UCSR0B &= ~((1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0)|(1<<UDRIE0)); break;
#endif
#if defined(PROMICRO)
case 1: UCSR1B &= ~((1<<RXEN1)|(1<<TXEN1)|(1<<RXCIE1)|(1<<UDRIE1)); break;
#endif
#if defined(MEGA)
case 0: UCSR0B &= ~((1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0)|(1<<UDRIE0)); break;
case 1: UCSR1B &= ~((1<<RXEN1)|(1<<TXEN1)|(1<<RXCIE1)|(1<<UDRIE1)); break;
case 2: UCSR2B &= ~((1<<RXEN2)|(1<<TXEN2)|(1<<RXCIE2)|(1<<UDRIE2)); break;
case 3: UCSR3B &= ~((1<<RXEN3)|(1<<TXEN3)|(1<<RXCIE3)|(1<<UDRIE3)); break;
#endif
}
}
// we don't care about ring buffer overflow (head->tail) to avoid a test condition : data is lost anyway if it happens
void store_uart_in_buf(uint8_t data, uint8_t portnum) {
#if defined(SERIAL_RX)
if (portnum == RX_SERIAL_PORT) {
if (!spekFrameFlags) {
sei();
uint32_t spekTimeNow = (timer0_overflow_count << 8) * (64 / clockCyclesPerMicrosecond()); //Move timer0_overflow_count into registers so we don't touch a volatile twice
uint32_t spekInterval = spekTimeNow - spekTimeLast; //timer0_overflow_count will be slightly off because of the way the Arduino core timer interrupt handler works; that is acceptable for this use. Using the core variable avoids an expensive call to millis() or micros()
spekTimeLast = spekTimeNow;
if (spekInterval > 2500) { //Potential start of a Spektrum frame, they arrive every 11 or every 22 ms. Mark it, and clear the buffer.
serialTailRX[portnum] = 0;
serialHeadRX[portnum] = 0;
spekFrameFlags = 0x01;
}
cli();
}
}
#endif
uint8_t h = serialHeadRX[portnum];
serialBufferRX[h++][portnum] = data;
if (h >= RX_BUFFER_SIZE) h = 0;
serialHeadRX[portnum] = h;
}
#if defined(PROMINI)
ISR(USART_RX_vect) { store_uart_in_buf(UDR0, 0); }
#endif
#if defined(PROMICRO)
ISR(USART1_RX_vect) { store_uart_in_buf(UDR1, 1); }
#endif
#if defined(MEGA)
ISR(USART0_RX_vect) { store_uart_in_buf(UDR0, 0); }
ISR(USART1_RX_vect) { store_uart_in_buf(UDR1, 1); }
ISR(USART2_RX_vect) { store_uart_in_buf(UDR2, 2); }
ISR(USART3_RX_vect) { store_uart_in_buf(UDR3, 3); }
#endif
uint8_t SerialRead(uint8_t port) {
#if defined(PROMICRO)
#if defined(TEENSY20)
if(port == 0) return Serial.read();
#else
#if (ARDUINO >= 100)
if(port == 0) USB_Flush(USB_CDC_TX);
#endif
if(port == 0) return USB_Recv(USB_CDC_RX);
#endif
#endif
uint8_t t = serialTailRX[port];
uint8_t c = serialBufferRX[t][port];
if (serialHeadRX[port] != t) {
if (++t >= RX_BUFFER_SIZE) t = 0;
serialTailRX[port] = t;
}
return c;
}
#if defined(SERIAL_RX)
uint8_t SerialPeek(uint8_t port) {
uint8_t c = serialBufferRX[serialTailRX[port]][port];
if ((serialHeadRX[port] != serialTailRX[port])) return c; else return 0;
}
#endif
uint8_t SerialAvailable(uint8_t port) {
#if defined(PROMICRO)
#if !defined(TEENSY20)
if(port == 0) return USB_Available(USB_CDC_RX);
#else
if(port == 0) return T_USB_Available();
#endif
#endif
return ((uint8_t)(serialHeadRX[port] - serialTailRX[port]))%RX_BUFFER_SIZE;
}
uint8_t SerialUsedTXBuff(uint8_t port) {
return ((uint8_t)(serialHeadTX[port] - serialTailTX[port]))%TX_BUFFER_SIZE;
}
void SerialSerialize(uint8_t port,uint8_t a) {
uint8_t t = serialHeadTX[port];
if (++t >= TX_BUFFER_SIZE) t = 0;
serialBufferTX[t][port] = a;
serialHeadTX[port] = t;
}
void SerialWrite(uint8_t port,uint8_t c){
SerialSerialize(port,c);UartSendData(port);
}