31 #include <avr/interrupt.h>
54 #ifndef RX_BUFFER_SIZE
55 #define RX_BUFFER_SIZE 32
58 #ifndef TX_BUFFER_SIZE
59 #define TX_BUFFER_SIZE 16
69 #if (RX_BUFFER_SIZE < 2) || (TX_BUFFER_SIZE < 2)
70 #error SERIAL BUFFER TOO SMALL!
74 #if (RX_BUFFER_SIZE < 8) || (TX_BUFFER_SIZE < 8)
75 #error SERIAL BUFFER TOO SMALL!
79 #if ((RX_BUFFER_SIZE + TX_BUFFER_SIZE) * UART_COUNT) >= (RAMEND - 0x60)
80 #error SERIAL BUFFER TOO LARGE!
102 uint16_t
volatile rxRead[UART_COUNT];
103 uint16_t
volatile rxWrite[UART_COUNT];
104 uint16_t
volatile txRead[UART_COUNT];
105 uint16_t
volatile txWrite[UART_COUNT];
106 uint8_t
volatile shouldStartTransmission[UART_COUNT];
109 uint8_t
volatile sendThisNext[UART_COUNT];
110 uint8_t
volatile flow[UART_COUNT];
111 uint8_t
volatile rxBufferElements[UART_COUNT];
119 if (uart >= UART_COUNT)
127 shouldStartTransmission[uart] = 1;
129 sendThisNext[uart] = 0;
131 rxBufferElements[uart] = 0;
135 *serialRegisters[uart][SERIALC] = (1 << serialBits[uart][SERIALUCSZ0]) | (1 << serialBits[uart][SERIALUCSZ1]);
138 #if SERIALBAUDBIT == 8
139 *serialRegisters[uart][SERIALUBRRH] = (baud >> 8);
140 *serialRegisters[uart][SERIALUBRRL] = baud;
142 *serialBaudRegisters[uart] = baud;
145 *serialRegisters[uart][SERIALB] = (1 << serialBits[uart][SERIALRXCIE]);
146 *serialRegisters[uart][SERIALB] |= (1 << serialBits[uart][SERIALRXEN]) | (1 << serialBits[uart][SERIALTXEN]);
150 if (uart >= UART_COUNT)
156 while (*serialRegisters[uart][SERIALB] & (1 << serialBits[uart][SERIALUDRIE]));
158 *serialRegisters[uart][SERIALB] = 0;
159 *serialRegisters[uart][SERIALC] = 0;
165 if (uart >= UART_COUNT)
168 if (flow[uart] != on) {
171 while (sendThisNext[uart] != 0);
172 sendThisNext[uart] =
XON;
174 if (shouldStartTransmission[uart]) {
175 shouldStartTransmission[uart] = 0;
176 *serialRegisters[uart][SERIALB] |= (1 << serialBits[uart][SERIALUDRIE]);
177 *serialRegisters[uart][SERIALA] |= (1 << serialBits[uart][SERIALUDRE]);
181 sendThisNext[uart] =
XOFF;
183 if (shouldStartTransmission[uart]) {
184 shouldStartTransmission[uart] = 0;
185 *serialRegisters[uart][SERIALB] |= (1 << serialBits[uart][SERIALUDRIE]);
186 *serialRegisters[uart][SERIALA] |= (1 << serialBits[uart][SERIALUDRE]);
190 while (*serialRegisters[uart][SERIALB] & (1 << serialBits[uart][SERIALUDRIE]));
200 if (uart >= UART_COUNT)
203 if (rxRead[uart] != rxWrite[uart]) {
211 if (uart >= UART_COUNT)
219 if (uart >= UART_COUNT)
225 rxBufferElements[uart]--;
226 if ((flow[uart] == 0) && (rxBufferElements[uart] <=
FLOWMARK)) {
227 while (sendThisNext[uart] != 0);
228 sendThisNext[uart] =
XON;
230 if (shouldStartTransmission[uart]) {
231 shouldStartTransmission[uart] = 0;
232 *serialRegisters[uart][SERIALB] |= (1 << serialBits[uart][SERIALUDRIE]);
233 *serialRegisters[uart][SERIALA] |= (1 << serialBits[uart][SERIALUDRE]);
238 if (rxRead[uart] != rxWrite[uart]) {
239 c = rxBuffer[uart][rxRead[uart]];
240 rxBuffer[uart][rxRead[uart]] = 0;
253 if (uart >= UART_COUNT)
256 return (((rxWrite[uart] + 1) == rxRead[uart]) || ((rxRead[uart] == 0) && ((rxWrite[uart] + 1) ==
RX_BUFFER_SIZE)));
260 if (uart >= UART_COUNT)
263 if (rxRead[uart] != rxWrite[uart]) {
275 if (uart >= UART_COUNT)
278 #ifdef SERIALINJECTCR
285 txBuffer[uart][txWrite[uart]] = data;
291 if (shouldStartTransmission[uart]) {
292 shouldStartTransmission[uart] = 0;
293 *serialRegisters[uart][SERIALB] |= (1 << serialBits[uart][SERIALUDRIE]);
294 *serialRegisters[uart][SERIALA] |= (1 << serialBits[uart][SERIALUDRE]);
299 if (uart >= UART_COUNT)
305 while (*data !=
'\0') {
312 if (uart >= UART_COUNT)
315 return (((txWrite[uart] + 1) == txRead[uart]) || ((txRead[uart] == 0) && ((txWrite[uart] + 1) ==
TX_BUFFER_SIZE)));
319 if (uart >= UART_COUNT)
322 if (txRead[uart] != txWrite[uart]) {
329 void serialReceiveInterrupt(uint8_t uart) {
330 rxBuffer[uart][rxWrite[uart]] = *serialRegisters[uart][SERIALDATA];
338 rxBufferElements[uart]++;
340 sendThisNext[uart] =
XOFF;
342 if (shouldStartTransmission[uart]) {
343 shouldStartTransmission[uart] = 0;
344 *serialRegisters[uart][SERIALB] |= (1 << serialBits[uart][SERIALUDRIE]);
345 *serialRegisters[uart][SERIALA] |= (1 << serialBits[uart][SERIALUDRE]);
351 void serialTransmitInterrupt(uint8_t uart) {
353 if (sendThisNext[uart]) {
354 *serialRegisters[uart][SERIALDATA] = sendThisNext[uart];
355 sendThisNext[uart] = 0;
358 if (txRead[uart] != txWrite[uart]) {
359 *serialRegisters[uart][SERIALDATA] = txBuffer[uart][txRead[uart]];
366 shouldStartTransmission[uart] = 1;
367 *serialRegisters[uart][SERIALB] &= ~(1 << serialBits[uart][SERIALUDRIE]);
374 ISR(SERIALRECIEVEINTERRUPT) {
375 serialReceiveInterrupt(0);
378 ISR(SERIALTRANSMITINTERRUPT) {
379 serialTransmitInterrupt(0);
383 ISR(SERIALRECIEVEINTERRUPT1) {
384 serialReceiveInterrupt(1);
387 ISR(SERIALTRANSMITINTERRUPT1) {
388 serialTransmitInterrupt(1);
393 ISR(SERIALRECIEVEINTERRUPT2) {
394 serialReceiveInterrupt(2);
397 ISR(SERIALTRANSMITINTERRUPT2) {
398 serialTransmitInterrupt(2);
403 ISR(SERIALRECIEVEINTERRUPT3) {
404 serialReceiveInterrupt(3);
407 ISR(SERIALTRANSMITINTERRUPT3) {
408 serialTransmitInterrupt(3);