31 #include <avr/interrupt.h>
53 #ifndef RX_BUFFER_SIZE
54 #define RX_BUFFER_SIZE 32
57 #ifndef TX_BUFFER_SIZE
58 #define TX_BUFFER_SIZE 16
68 #if (RX_BUFFER_SIZE < 2) || (TX_BUFFER_SIZE < 2)
69 #error SERIAL BUFFER TOO SMALL!
73 #if (RX_BUFFER_SIZE < 8) || (TX_BUFFER_SIZE < 8)
74 #error SERIAL BUFFER TOO SMALL!
78 #if ((RX_BUFFER_SIZE + TX_BUFFER_SIZE) * UART_COUNT) >= (RAMEND - 0x60)
79 #error SERIAL BUFFER TOO LARGE!
101 uint16_t
volatile rxRead[UART_COUNT];
102 uint16_t
volatile rxWrite[UART_COUNT];
103 uint16_t
volatile txRead[UART_COUNT];
104 uint16_t
volatile txWrite[UART_COUNT];
105 uint8_t
volatile shouldStartTransmission[UART_COUNT];
108 uint8_t
volatile sendThisNext[UART_COUNT];
109 uint8_t
volatile flow[UART_COUNT];
110 uint8_t
volatile rxBufferElements[UART_COUNT];
118 if (uart >= UART_COUNT)
126 shouldStartTransmission[uart] = 1;
128 sendThisNext[uart] = 0;
130 rxBufferElements[uart] = 0;
134 *serialRegisters[uart][SERIALC] = (1 << serialBits[uart][SERIALUCSZ0]) | (1 << serialBits[uart][SERIALUCSZ1]);
137 #if SERIALBAUDBIT == 8
138 *serialRegisters[uart][SERIALUBRRH] = (baud >> 8);
139 *serialRegisters[uart][SERIALUBRRL] = baud;
141 *serialBaudRegisters[uart] = baud;
144 *serialRegisters[uart][SERIALB] = (1 << serialBits[uart][SERIALRXCIE]);
145 *serialRegisters[uart][SERIALB] |= (1 << serialBits[uart][SERIALRXEN]) | (1 << serialBits[uart][SERIALTXEN]);
149 if (uart >= UART_COUNT)
155 while (*serialRegisters[uart][SERIALB] & (1 << serialBits[uart][SERIALUDRIE]));
157 *serialRegisters[uart][SERIALB] = 0;
158 *serialRegisters[uart][SERIALC] = 0;
164 if (uart >= UART_COUNT)
167 if (flow[uart] != on) {
170 while (sendThisNext[uart] != 0);
171 sendThisNext[uart] =
XON;
173 if (shouldStartTransmission[uart]) {
174 shouldStartTransmission[uart] = 0;
175 *serialRegisters[uart][SERIALB] |= (1 << serialBits[uart][SERIALUDRIE]);
176 *serialRegisters[uart][SERIALA] |= (1 << serialBits[uart][SERIALUDRE]);
180 sendThisNext[uart] =
XOFF;
182 if (shouldStartTransmission[uart]) {
183 shouldStartTransmission[uart] = 0;
184 *serialRegisters[uart][SERIALB] |= (1 << serialBits[uart][SERIALUDRIE]);
185 *serialRegisters[uart][SERIALA] |= (1 << serialBits[uart][SERIALUDRE]);
189 while (*serialRegisters[uart][SERIALB] & (1 << serialBits[uart][SERIALUDRIE]));
199 if (uart >= UART_COUNT)
202 if (rxRead[uart] != rxWrite[uart]) {
210 if (uart >= UART_COUNT)
218 if (uart >= UART_COUNT)
224 rxBufferElements[uart]--;
225 if ((flow[uart] == 0) && (rxBufferElements[uart] <=
FLOWMARK)) {
226 while (sendThisNext[uart] != 0);
227 sendThisNext[uart] =
XON;
229 if (shouldStartTransmission[uart]) {
230 shouldStartTransmission[uart] = 0;
231 *serialRegisters[uart][SERIALB] |= (1 << serialBits[uart][SERIALUDRIE]);
232 *serialRegisters[uart][SERIALA] |= (1 << serialBits[uart][SERIALUDRE]);
237 if (rxRead[uart] != rxWrite[uart]) {
238 c = rxBuffer[uart][rxRead[uart]];
239 rxBuffer[uart][rxRead[uart]] = 0;
252 if (uart >= UART_COUNT)
255 return (((rxWrite[uart] + 1) == rxRead[uart]) || ((rxRead[uart] == 0) && ((rxWrite[uart] + 1) ==
RX_BUFFER_SIZE)));
259 if (uart >= UART_COUNT)
262 if (rxRead[uart] != rxWrite[uart]) {
274 if (uart >= UART_COUNT)
277 #ifdef SERIALINJECTCR
284 txBuffer[uart][txWrite[uart]] = data;
290 if (shouldStartTransmission[uart]) {
291 shouldStartTransmission[uart] = 0;
292 *serialRegisters[uart][SERIALB] |= (1 << serialBits[uart][SERIALUDRIE]);
293 *serialRegisters[uart][SERIALA] |= (1 << serialBits[uart][SERIALUDRE]);
298 if (uart >= UART_COUNT)
304 while (*data !=
'\0') {
311 if (uart >= UART_COUNT)
314 return (((txWrite[uart] + 1) == txRead[uart]) || ((txRead[uart] == 0) && ((txWrite[uart] + 1) ==
TX_BUFFER_SIZE)));
318 if (uart >= UART_COUNT)
321 if (txRead[uart] != txWrite[uart]) {
328 void serialReceiveInterrupt(uint8_t uart) {
329 rxBuffer[uart][rxWrite[uart]] = *serialRegisters[uart][SERIALDATA];
337 rxBufferElements[uart]++;
339 sendThisNext[uart] =
XOFF;
341 if (shouldStartTransmission[uart]) {
342 shouldStartTransmission[uart] = 0;
343 *serialRegisters[uart][SERIALB] |= (1 << serialBits[uart][SERIALUDRIE]);
344 *serialRegisters[uart][SERIALA] |= (1 << serialBits[uart][SERIALUDRE]);
350 void serialTransmitInterrupt(uint8_t uart) {
352 if (sendThisNext[uart]) {
353 *serialRegisters[uart][SERIALDATA] = sendThisNext[uart];
354 sendThisNext[uart] = 0;
357 if (txRead[uart] != txWrite[uart]) {
358 *serialRegisters[uart][SERIALDATA] = txBuffer[uart][txRead[uart]];
365 shouldStartTransmission[uart] = 1;
366 *serialRegisters[uart][SERIALB] &= ~(1 << serialBits[uart][SERIALUDRIE]);
373 ISR(SERIALRECIEVEINTERRUPT) {
374 serialReceiveInterrupt(0);
377 ISR(SERIALTRANSMITINTERRUPT) {
378 serialTransmitInterrupt(0);
382 ISR(SERIALRECIEVEINTERRUPT1) {
383 serialReceiveInterrupt(1);
386 ISR(SERIALTRANSMITINTERRUPT1) {
387 serialTransmitInterrupt(1);
392 ISR(SERIALRECIEVEINTERRUPT2) {
393 serialReceiveInterrupt(2);
396 ISR(SERIALTRANSMITINTERRUPT2) {
397 serialTransmitInterrupt(2);
402 ISR(SERIALRECIEVEINTERRUPT3) {
403 serialReceiveInterrupt(3);
406 ISR(SERIALTRANSMITINTERRUPT3) {
407 serialTransmitInterrupt(3);