Suppress correct warning in nightly test.
[bertos.git] / bertos / cpu / avr / drv / ser_avr.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 2000 Bernie Innocenti <bernie@codewiz.org>
31  *
32  * -->
33  *
34  * \brief AVR UART and SPI I/O driver (Implementation)
35  *
36  * \version $Id$
37  *
38  * \author Bernie Innocenti <bernie@codewiz.org>
39  * \author Stefano Fedrigo <aleph@develer.com>
40  */
41
42 #include "hw/hw_ser.h"  /* Required for bus macros overrides */
43 #include <hw/hw_cpufreq.h>  /* CPU_FREQ */
44
45 #include "cfg/cfg_ser.h"
46
47 #include <cfg/macros.h> /* DIV_ROUND */
48 #include <cfg/debug.h>
49 #include <cfg/cfg_arch.h> // ARCH_NIGHTTEST
50
51 #include <drv/ser.h>
52 #include <drv/ser_p.h>
53 #include <drv/timer.h>
54
55 #include <struct/fifobuf.h>
56
57 #include <avr/io.h>
58
59 #if defined(__AVR_LIBC_VERSION__) && (__AVR_LIBC_VERSION__ >= 10400UL)
60         #include <avr/interrupt.h>
61 #else
62         #include <avr/signal.h>
63 #endif
64
65
66 #if !CONFIG_SER_HWHANDSHAKE
67         /**
68          * \name Hardware handshake (RTS/CTS).
69          * \{
70          */
71         #define RTS_ON      do {} while (0)
72         #define RTS_OFF     do {} while (0)
73         #define IS_CTS_ON   true
74         #define EIMSKF_CTS  0 /**< Dummy value, must be overridden */
75         /*\}*/
76 #endif
77
78 #if CPU_AVR_ATMEGA1281
79         #define BIT_RXCIE0 RXCIE0
80         #define BIT_RXEN0  RXEN0
81         #define BIT_TXEN0  TXEN0
82         #define BIT_UDRIE0 UDRIE0
83
84         #define BIT_RXCIE1 RXCIE1
85         #define BIT_RXEN1  RXEN1
86         #define BIT_TXEN1  TXEN1
87         #define BIT_UDRIE1 UDRIE1
88 #else
89         #define BIT_RXCIE0 RXCIE
90         #define BIT_RXEN0  RXEN
91         #define BIT_TXEN0  TXEN
92         #define BIT_UDRIE0 UDRIE
93
94         #define BIT_RXCIE1 RXCIE
95         #define BIT_RXEN1  RXEN
96         #define BIT_TXEN1  TXEN
97         #define BIT_UDRIE1 UDRIE
98 #endif
99
100
101 /**
102  * \name Overridable serial bus hooks
103  *
104  * These can be redefined in hw.h to implement
105  * special bus policies such as half-duplex, 485, etc.
106  *
107  *
108  * \code
109  *  TXBEGIN      TXCHAR      TXEND  TXOFF
110  *    |   __________|__________ |     |
111  *    |   |   |   |   |   |   | |     |
112  *    v   v   v   v   v   v   v v     v
113  * ______  __  __  __  __  __  __  ________________
114  *       \/  \/  \/  \/  \/  \/  \/
115  * ______/\__/\__/\__/\__/\__/\__/
116  *
117  * \endcode
118  *
119  * \{
120  */
121 #ifndef SER_UART0_BUS_TXINIT
122         /**
123          * Default TXINIT macro - invoked in uart0_init()
124          *
125          * - Enable both the receiver and the transmitter
126          * - Enable only the RX complete interrupt
127          */
128         #define SER_UART0_BUS_TXINIT do { \
129                 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0); \
130         } while (0)
131 #endif
132
133 #ifndef SER_UART0_BUS_TXBEGIN
134         /**
135          * Invoked before starting a transmission
136          *
137          * - Enable both the receiver and the transmitter
138          * - Enable both the RX complete and UDR empty interrupts
139          */
140         #define SER_UART0_BUS_TXBEGIN do { \
141                 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_UDRIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0); \
142         } while (0)
143 #endif
144
145 #ifndef SER_UART0_BUS_TXCHAR
146         /**
147          * Invoked to send one character.
148          */
149         #define SER_UART0_BUS_TXCHAR(c) do { \
150                 UDR0 = (c); \
151         } while (0)
152 #endif
153
154 #ifndef SER_UART0_BUS_TXEND
155         /**
156          * Invoked as soon as the txfifo becomes empty
157          *
158          * - Keep both the receiver and the transmitter enabled
159          * - Keep the RX complete interrupt enabled
160          * - Disable the UDR empty interrupt
161          */
162         #define SER_UART0_BUS_TXEND do { \
163                 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0); \
164         } while (0)
165 #endif
166
167 #ifndef SER_UART0_BUS_TXOFF
168         /**
169          * \def SER_UART0_BUS_TXOFF
170          *
171          * Invoked after the last character has been transmitted
172          *
173          * The default is no action.
174          */
175         #ifdef __doxygen__
176         #define SER_UART0_BUS_TXOFF
177         #endif
178 #endif
179
180 #ifndef SER_UART1_BUS_TXINIT
181         /** \sa SER_UART0_BUS_TXINIT */
182         #define SER_UART1_BUS_TXINIT do { \
183                 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1); \
184         } while (0)
185 #endif
186 #ifndef SER_UART1_BUS_TXBEGIN
187         /** \sa SER_UART0_BUS_TXBEGIN */
188         #define SER_UART1_BUS_TXBEGIN do { \
189                 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_UDRIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1); \
190         } while (0)
191 #endif
192 #ifndef SER_UART1_BUS_TXCHAR
193         /** \sa SER_UART0_BUS_TXCHAR */
194         #define SER_UART1_BUS_TXCHAR(c) do { \
195                 UDR1 = (c); \
196         } while (0)
197 #endif
198 #ifndef SER_UART1_BUS_TXEND
199         /** \sa SER_UART0_BUS_TXEND */
200         #define SER_UART1_BUS_TXEND do { \
201                 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1); \
202         } while (0)
203 #endif
204 #ifndef SER_UART1_BUS_TXOFF
205         /**
206          * \def SER_UART1_BUS_TXOFF
207          *
208          * \see SER_UART0_BUS_TXOFF
209          */
210         #ifdef __doxygen__
211         #define SER_UART1_BUS_TXOFF
212         #endif
213 #endif
214 /*\}*/
215
216
217 /**
218  * \name Overridable SPI hooks
219  *
220  * These can be redefined in hw.h to implement
221  * special bus policies such as slave select pin handling, etc.
222  *
223  * \{
224  */
225 #ifndef SER_SPI_BUS_TXINIT
226         /**
227          * Default TXINIT macro - invoked in spi_init()
228          * The default is no action.
229          */
230         #define SER_SPI_BUS_TXINIT
231 #endif
232
233 #ifndef SER_SPI_BUS_TXCLOSE
234         /**
235          * Invoked after the last character has been transmitted.
236          * The default is no action.
237          */
238         #define SER_SPI_BUS_TXCLOSE
239 #endif
240 /*\}*/
241
242
243 /* SPI port and pin configuration */
244 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA103 || CPU_AVR_ATMEGA1281
245         #define SPI_PORT      PORTB
246         #define SPI_DDR       DDRB
247         #define SPI_SS_BIT    PB0
248         #define SPI_SCK_BIT   PB1
249         #define SPI_MOSI_BIT  PB2
250         #define SPI_MISO_BIT  PB3
251 #elif CPU_AVR_ATMEGA8
252         #define SPI_PORT      PORTB
253         #define SPI_DDR       DDRB
254         #define SPI_SS_BIT    PB2
255         #define SPI_SCK_BIT   PB5
256         #define SPI_MOSI_BIT  PB3
257         #define SPI_MISO_BIT  PB4
258 #else
259         #error Unknown architecture
260 #endif
261
262 /* USART register definitions */
263 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA1281
264         #define AVR_HAS_UART1 1
265 #elif CPU_AVR_ATMEGA8
266         #define AVR_HAS_UART1 0
267         #define UCSR0A UCSRA
268         #define UCSR0B UCSRB
269         #define UCSR0C UCSRC
270         #define UDR0   UDR
271         #define UBRR0L UBRRL
272         #define UBRR0H UBRRH
273         #define SIG_UART0_DATA SIG_UART_DATA
274         #define SIG_UART0_RECV SIG_UART_RECV
275         #define SIG_UART0_TRANS SIG_UART_TRANS
276 #elif CPU_AVR_ATMEGA103
277         #define AVR_HAS_UART1 0
278         #define UCSR0B UCR
279         #define UDR0   UDR
280         #define UCSR0A USR
281         #define UBRR0L UBRR
282         #define SIG_UART0_DATA SIG_UART_DATA
283         #define SIG_UART0_RECV SIG_UART_RECV
284         #define SIG_UART0_TRANS SIG_UART_TRANS
285 #else
286         #error Unknown architecture
287 #endif
288
289
290 /* From the high-level serial driver */
291 extern struct Serial *ser_handles[SER_CNT];
292
293 /* TX and RX buffers */
294 static unsigned char uart0_txbuffer[CONFIG_UART0_TXBUFSIZE];
295 static unsigned char uart0_rxbuffer[CONFIG_UART0_RXBUFSIZE];
296 #if AVR_HAS_UART1
297         static unsigned char uart1_txbuffer[CONFIG_UART1_TXBUFSIZE];
298         static unsigned char uart1_rxbuffer[CONFIG_UART1_RXBUFSIZE];
299 #endif
300 static unsigned char spi_txbuffer[CONFIG_SPI_TXBUFSIZE];
301 static unsigned char spi_rxbuffer[CONFIG_SPI_RXBUFSIZE];
302
303
304 /**
305  * Internal hardware state structure
306  *
307  * The \a sending variable is true while the transmission
308  * interrupt is retriggering itself.
309  *
310  * For the USARTs the \a sending flag is useful for taking specific
311  * actions before sending a burst of data, at the start of a trasmission
312  * but not before every char sent.
313  *
314  * For the SPI, this flag is necessary because the SPI sends and receives
315  * bytes at the same time and the SPI IRQ is unique for send/receive.
316  * The only way to start transmission is to write data in SPDR (this
317  * is done by spi_starttx()). We do this *only* if a transfer is
318  * not already started.
319  */
320 struct AvrSerial
321 {
322         struct SerialHardware hw;
323         volatile bool sending;
324 };
325
326
327
328 /*
329  * Callbacks
330  */
331 static void uart0_init(
332         UNUSED_ARG(struct SerialHardware *, _hw),
333         UNUSED_ARG(struct Serial *, ser))
334 {
335         SER_UART0_BUS_TXINIT;
336         RTS_ON;
337         SER_STROBE_INIT;
338 }
339
340 static void uart0_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
341 {
342         UCSR0B = 0;
343 }
344
345 static void uart0_enabletxirq(struct SerialHardware *_hw)
346 {
347         struct AvrSerial *hw = (struct AvrSerial *)_hw;
348
349         /*
350          * WARNING: racy code here!  The tx interrupt sets hw->sending to false
351          * when it runs with an empty fifo.  The order of statements in the
352          * if-block matters.
353          */
354         if (!hw->sending)
355         {
356                 hw->sending = true;
357                 SER_UART0_BUS_TXBEGIN;
358         }
359 }
360
361 static void uart0_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
362 {
363         /* Compute baud-rate period */
364         uint16_t period = DIV_ROUND(CPU_FREQ / 16UL, rate) - 1;
365
366 #if !CPU_AVR_ATMEGA103
367         UBRR0H = (period) >> 8;
368 #endif
369         UBRR0L = (period);
370
371         //DB(kprintf("uart0_setbaudrate(rate=%lu): period=%d\n", rate, period);)
372 }
373
374 static void uart0_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
375 {
376 #if !CPU_AVR_ATMEGA103
377         UCSR0C = (UCSR0C & ~(BV(UPM01) | BV(UPM00))) | ((parity) << UPM00);
378 #endif
379 }
380
381 #if AVR_HAS_UART1
382
383 static void uart1_init(
384         UNUSED_ARG(struct SerialHardware *, _hw),
385         UNUSED_ARG(struct Serial *, ser))
386 {
387         SER_UART1_BUS_TXINIT;
388         RTS_ON;
389         SER_STROBE_INIT;
390 }
391
392 static void uart1_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
393 {
394         UCSR1B = 0;
395 }
396
397 static void uart1_enabletxirq(struct SerialHardware *_hw)
398 {
399         struct AvrSerial *hw = (struct AvrSerial *)_hw;
400
401         /*
402          * WARNING: racy code here!  The tx interrupt
403          * sets hw->sending to false when it runs with
404          * an empty fifo.  The order of the statements
405          * in the if-block matters.
406          */
407         if (!hw->sending)
408         {
409                 hw->sending = true;
410                 SER_UART1_BUS_TXBEGIN;
411         }
412 }
413
414 static void uart1_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
415 {
416         /* Compute baud-rate period */
417         uint16_t period = DIV_ROUND(CPU_FREQ / 16UL, rate) - 1;
418
419         UBRR1H = (period) >> 8;
420         UBRR1L = (period);
421
422         //DB(kprintf("uart1_setbaudrate(rate=%ld): period=%d\n", rate, period);)
423 }
424
425 static void uart1_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
426 {
427         UCSR1C = (UCSR1C & ~(BV(UPM11) | BV(UPM10))) | ((parity) << UPM10);
428 }
429
430 #endif // AVR_HAS_UART1
431
432 static void spi_init(UNUSED_ARG(struct SerialHardware *, _hw), UNUSED_ARG(struct Serial *, ser))
433 {
434         /*
435          * Set MOSI and SCK ports out, MISO in.
436          *
437          * The ATmega64/128 datasheet explicitly states that the input/output
438          * state of the SPI pins is not significant, as when the SPI is
439          * active the I/O port are overrided.
440          * This is *blatantly FALSE*.
441          *
442          * Moreover, the MISO pin on the board_kc *must* be in high impedance
443          * state even when the SPI is off, because the line is wired together
444          * with the KBus serial RX, and the transmitter of the slave boards
445          * would be unable to drive the line.
446          */
447         ATOMIC(SPI_DDR |= (BV(SPI_MOSI_BIT) | BV(SPI_SCK_BIT)));
448
449         /*
450          * If the SPI master mode is activated and the SS pin is in input and tied low,
451          * the SPI hardware will automatically switch to slave mode!
452          * For proper communication this pins should therefore be:
453          * - as output
454          * - as input but tied high forever!
455          * This driver set the pin as output.
456          */
457         #if (ARCH & ARCH_NIGHTTEST)
458                 #warning __FILTER_NEXT_WARNING__
459         #endif
460         #warning SPI SS pin set as output for proper operation, check schematics for possible conflicts.
461         ATOMIC(SPI_DDR |= BV(SPI_SS_BIT));
462
463         ATOMIC(SPI_DDR &= ~BV(SPI_MISO_BIT));
464         /* Enable SPI, IRQ on, Master */
465         SPCR = BV(SPE) | BV(SPIE) | BV(MSTR);
466
467         /* Set data order */
468         #if CONFIG_SPI_DATA_ORDER == SER_LSB_FIRST
469                 SPCR |= BV(DORD);
470         #endif
471
472         /* Set SPI clock rate */
473         #if CONFIG_SPI_CLOCK_DIV == 128
474                 SPCR |= (BV(SPR1) | BV(SPR0));
475         #elif (CONFIG_SPI_CLOCK_DIV == 64 || CONFIG_SPI_CLOCK_DIV == 32)
476                 SPCR |= BV(SPR1);
477         #elif (CONFIG_SPI_CLOCK_DIV == 16 || CONFIG_SPI_CLOCK_DIV == 8)
478                 SPCR |= BV(SPR0);
479         #elif (CONFIG_SPI_CLOCK_DIV == 4 || CONFIG_SPI_CLOCK_DIV == 2)
480                 // SPR0 & SDPR1 both at 0
481         #else
482                 #error Unsupported SPI clock division factor.
483         #endif
484
485         /* Set SPI2X bit (spi double frequency) */
486         #if (CONFIG_SPI_CLOCK_DIV == 128 || CONFIG_SPI_CLOCK_DIV == 64 \
487           || CONFIG_SPI_CLOCK_DIV == 16 || CONFIG_SPI_CLOCK_DIV == 4)
488                 SPSR &= ~BV(SPI2X);
489         #elif (CONFIG_SPI_CLOCK_DIV == 32 || CONFIG_SPI_CLOCK_DIV == 8 || CONFIG_SPI_CLOCK_DIV == 2)
490                 SPSR |= BV(SPI2X);
491         #else
492                 #error Unsupported SPI clock division factor.
493         #endif
494
495         /* Set clock polarity */
496         #if CONFIG_SPI_CLOCK_POL == 1
497                 SPCR |= BV(CPOL);
498         #endif
499
500         /* Set clock phase */
501         #if CONFIG_SPI_CLOCK_PHASE == 1
502                 SPCR |= BV(CPHA);
503         #endif
504         SER_SPI_BUS_TXINIT;
505
506         SER_STROBE_INIT;
507 }
508
509 static void spi_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
510 {
511         SPCR = 0;
512
513         SER_SPI_BUS_TXCLOSE;
514
515         /* Set all pins as inputs */
516         ATOMIC(SPI_DDR &= ~(BV(SPI_MISO_BIT) | BV(SPI_MOSI_BIT) | BV(SPI_SCK_BIT) | BV(SPI_SS_BIT)));
517 }
518
519 static void spi_starttx(struct SerialHardware *_hw)
520 {
521         struct AvrSerial *hw = (struct AvrSerial *)_hw;
522
523         cpu_flags_t flags;
524         IRQ_SAVE_DISABLE(flags);
525
526         /* Send data only if the SPI is not already transmitting */
527         if (!hw->sending && !fifo_isempty(&ser_handles[SER_SPI]->txfifo))
528         {
529                 hw->sending = true;
530                 SPDR = fifo_pop(&ser_handles[SER_SPI]->txfifo);
531         }
532
533         IRQ_RESTORE(flags);
534 }
535
536 static void spi_setbaudrate(
537         UNUSED_ARG(struct SerialHardware *, _hw),
538         UNUSED_ARG(unsigned long, rate))
539 {
540         // nop
541 }
542
543 static void spi_setparity(UNUSED_ARG(struct SerialHardware *, _hw), UNUSED_ARG(int, parity))
544 {
545         // nop
546 }
547
548 static bool tx_sending(struct SerialHardware* _hw)
549 {
550         struct AvrSerial *hw = (struct AvrSerial *)_hw;
551         return hw->sending;
552 }
553
554
555
556 // FIXME: move into compiler.h?  Ditch?
557 #if COMPILER_C99
558         #define C99INIT(name,val) .name = val
559 #elif defined(__GNUC__)
560         #define C99INIT(name,val) name: val
561 #else
562         #warning No designated initializers, double check your code
563         #define C99INIT(name,val) (val)
564 #endif
565
566 /*
567  * High-level interface data structures
568  */
569 static const struct SerialHardwareVT UART0_VT =
570 {
571         C99INIT(init, uart0_init),
572         C99INIT(cleanup, uart0_cleanup),
573         C99INIT(setBaudrate, uart0_setbaudrate),
574         C99INIT(setParity, uart0_setparity),
575         C99INIT(txStart, uart0_enabletxirq),
576         C99INIT(txSending, tx_sending),
577 };
578
579 #if AVR_HAS_UART1
580 static const struct SerialHardwareVT UART1_VT =
581 {
582         C99INIT(init, uart1_init),
583         C99INIT(cleanup, uart1_cleanup),
584         C99INIT(setBaudrate, uart1_setbaudrate),
585         C99INIT(setParity, uart1_setparity),
586         C99INIT(txStart, uart1_enabletxirq),
587         C99INIT(txSending, tx_sending),
588 };
589 #endif // AVR_HAS_UART1
590
591 static const struct SerialHardwareVT SPI_VT =
592 {
593         C99INIT(init, spi_init),
594         C99INIT(cleanup, spi_cleanup),
595         C99INIT(setBaudrate, spi_setbaudrate),
596         C99INIT(setParity, spi_setparity),
597         C99INIT(txStart, spi_starttx),
598         C99INIT(txSending, tx_sending),
599 };
600
601 static struct AvrSerial UARTDescs[SER_CNT] =
602 {
603         {
604                 C99INIT(hw, /**/) {
605                         C99INIT(table, &UART0_VT),
606                         C99INIT(txbuffer, uart0_txbuffer),
607                         C99INIT(rxbuffer, uart0_rxbuffer),
608                         C99INIT(txbuffer_size, sizeof(uart0_txbuffer)),
609                         C99INIT(rxbuffer_size, sizeof(uart0_rxbuffer)),
610                 },
611                 C99INIT(sending, false),
612         },
613 #if AVR_HAS_UART1
614         {
615                 C99INIT(hw, /**/) {
616                         C99INIT(table, &UART1_VT),
617                         C99INIT(txbuffer, uart1_txbuffer),
618                         C99INIT(rxbuffer, uart1_rxbuffer),
619                         C99INIT(txbuffer_size, sizeof(uart1_txbuffer)),
620                         C99INIT(rxbuffer_size, sizeof(uart1_rxbuffer)),
621                 },
622                 C99INIT(sending, false),
623         },
624 #endif
625         {
626                 C99INIT(hw, /**/) {
627                         C99INIT(table, &SPI_VT),
628                         C99INIT(txbuffer, spi_txbuffer),
629                         C99INIT(rxbuffer, spi_rxbuffer),
630                         C99INIT(txbuffer_size, sizeof(spi_txbuffer)),
631                         C99INIT(rxbuffer_size, sizeof(spi_rxbuffer)),
632                 },
633                 C99INIT(sending, false),
634         }
635 };
636
637 struct SerialHardware *ser_hw_getdesc(int unit)
638 {
639         ASSERT(unit < SER_CNT);
640         return &UARTDescs[unit].hw;
641 }
642
643
644 /*
645  * Interrupt handlers
646  */
647
648 #if CONFIG_SER_HWHANDSHAKE
649
650 /// This interrupt is triggered when the CTS line goes high
651 SIGNAL(SIG_CTS)
652 {
653         // Re-enable UDR empty interrupt and TX, then disable CTS interrupt
654         UCSR0B = BV(BIT_RXCIE0) | BV(BIT_UDRIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0);
655         EIMSK &= ~EIMSKF_CTS;
656 }
657
658 #endif // CONFIG_SER_HWHANDSHAKE
659
660
661 /**
662  * Serial 0 TX interrupt handler
663  */
664 SIGNAL(USART0_UDRE_vect)
665 {
666         SER_STROBE_ON;
667
668         struct FIFOBuffer * const txfifo = &ser_handles[SER_UART0]->txfifo;
669
670         if (fifo_isempty(txfifo))
671         {
672                 SER_UART0_BUS_TXEND;
673 #ifndef SER_UART0_BUS_TXOFF
674                 UARTDescs[SER_UART0].sending = false;
675 #endif
676         }
677 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA103
678         else if (!IS_CTS_ON)
679         {
680                 // Disable rx interrupt and tx, enable CTS interrupt
681                 // UNTESTED
682                 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0);
683                 EIFR |= EIMSKF_CTS;
684                 EIMSK |= EIMSKF_CTS;
685         }
686 #endif
687         else
688         {
689                 char c = fifo_pop(txfifo);
690                 SER_UART0_BUS_TXCHAR(c);
691         }
692
693         SER_STROBE_OFF;
694 }
695
696 #ifdef SER_UART0_BUS_TXOFF
697 /**
698  * Serial port 0 TX complete interrupt handler.
699  *
700  * This IRQ is usually disabled.  The UDR-empty interrupt
701  * enables it when there's no more data to transmit.
702  * We need to wait until the last character has been
703  * transmitted before switching the 485 transceiver to
704  * receive mode.
705  *
706  * The txfifo might have been refilled by putchar() while
707  * we were waiting for the transmission complete interrupt.
708  * In this case, we must restart the UDR empty interrupt,
709  * otherwise we'd stop the serial port with some data
710  * still pending in the buffer.
711  */
712 SIGNAL(SIG_UART0_TRANS)
713 {
714         SER_STROBE_ON;
715
716         struct FIFOBuffer * const txfifo = &ser_handles[SER_UART0]->txfifo;
717         if (fifo_isempty(txfifo))
718         {
719                 SER_UART0_BUS_TXOFF;
720                 UARTDescs[SER_UART0].sending = false;
721         }
722         else
723                 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_UDRIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0);
724
725         SER_STROBE_OFF;
726 }
727 #endif /* SER_UART0_BUS_TXOFF */
728
729
730 #if AVR_HAS_UART1
731
732 /**
733  * Serial 1 TX interrupt handler
734  */
735 SIGNAL(USART1_UDRE_vect)
736 {
737         SER_STROBE_ON;
738
739         struct FIFOBuffer * const txfifo = &ser_handles[SER_UART1]->txfifo;
740
741         if (fifo_isempty(txfifo))
742         {
743                 SER_UART1_BUS_TXEND;
744 #ifndef SER_UART1_BUS_TXOFF
745                 UARTDescs[SER_UART1].sending = false;
746 #endif
747         }
748 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA103
749         else if (!IS_CTS_ON)
750         {
751                 // Disable rx interrupt and tx, enable CTS interrupt
752                 // UNTESTED
753                 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1);
754                 EIFR |= EIMSKF_CTS;
755                 EIMSK |= EIMSKF_CTS;
756         }
757 #endif
758         else
759         {
760                 char c = fifo_pop(txfifo);
761                 SER_UART1_BUS_TXCHAR(c);
762         }
763
764         SER_STROBE_OFF;
765 }
766
767 #ifdef SER_UART1_BUS_TXOFF
768 /**
769  * Serial port 1 TX complete interrupt handler.
770  *
771  * \sa port 0 TX complete handler.
772  */
773 SIGNAL(USART1_TX_vect)
774 {
775         SER_STROBE_ON;
776
777         struct FIFOBuffer * const txfifo = &ser_handles[SER_UART1]->txfifo;
778         if (fifo_isempty(txfifo))
779         {
780                 SER_UART1_BUS_TXOFF;
781                 UARTDescs[SER_UART1].sending = false;
782         }
783         else
784                 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_UDRIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1);
785
786         SER_STROBE_OFF;
787 }
788 #endif /* SER_UART1_BUS_TXOFF */
789
790 #endif // AVR_HAS_UART1
791
792
793 /**
794  * Serial 0 RX complete interrupt handler.
795  *
796  * This handler is interruptible.
797  * Interrupt are reenabled as soon as recv complete interrupt is
798  * disabled. Using INTERRUPT() is troublesome when the serial
799  * is heavily loaded, because an interrupt could be retriggered
800  * when executing the handler prologue before RXCIE is disabled.
801  *
802  * \note The code that re-enables interrupts is commented out
803  *       because in some nasty cases the interrupt is retriggered.
804  *       This is probably due to the RXC flag being set before
805  *       RXCIE is cleared.  Unfortunately the RXC flag is read-only
806  *       and can't be cleared by code.
807  */
808 SIGNAL(USART0_RX_vect)
809 {
810         SER_STROBE_ON;
811
812         /* Disable Recv complete IRQ */
813         //UCSR0B &= ~BV(RXCIE);
814         //IRQ_ENABLE;
815
816         /* Should be read before UDR */
817         ser_handles[SER_UART0]->status |= UCSR0A & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
818
819         /* To clear the RXC flag we must _always_ read the UDR even when we're
820          * not going to accept the incoming data, otherwise a new interrupt
821          * will occur once the handler terminates.
822          */
823         char c = UDR0;
824         struct FIFOBuffer * const rxfifo = &ser_handles[SER_UART0]->rxfifo;
825
826         if (fifo_isfull(rxfifo))
827                 ser_handles[SER_UART0]->status |= SERRF_RXFIFOOVERRUN;
828         else
829         {
830                 fifo_push(rxfifo, c);
831 #if CONFIG_SER_HWHANDSHAKE
832                 if (fifo_isfull(rxfifo))
833                         RTS_OFF;
834 #endif
835         }
836
837         /* Reenable receive complete int */
838         //IRQ_DISABLE;
839         //UCSR0B |= BV(RXCIE);
840
841         SER_STROBE_OFF;
842 }
843
844
845 #if AVR_HAS_UART1
846
847 /**
848  * Serial 1 RX complete interrupt handler.
849  *
850  * This handler is interruptible.
851  * Interrupt are reenabled as soon as recv complete interrupt is
852  * disabled. Using INTERRUPT() is troublesome when the serial
853  * is heavily loaded, because an interrupt could be retriggered
854  * when executing the handler prologue before RXCIE is disabled.
855  *
856  * \see SIGNAL(USART1_RX_vect)
857  */
858 SIGNAL(USART1_RX_vect)
859 {
860         SER_STROBE_ON;
861
862         /* Disable Recv complete IRQ */
863         //UCSR1B &= ~BV(RXCIE);
864         //IRQ_ENABLE;
865
866         /* Should be read before UDR */
867         ser_handles[SER_UART1]->status |= UCSR1A & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
868
869         /* To avoid an IRQ storm, we must _always_ read the UDR even when we're
870          * not going to accept the incoming data
871          */
872         char c = UDR1;
873         struct FIFOBuffer * const rxfifo = &ser_handles[SER_UART1]->rxfifo;
874         //ASSERT_VALID_FIFO(rxfifo);
875
876         if (UNLIKELY(fifo_isfull(rxfifo)))
877                 ser_handles[SER_UART1]->status |= SERRF_RXFIFOOVERRUN;
878         else
879         {
880                 fifo_push(rxfifo, c);
881 #if CONFIG_SER_HWHANDSHAKE
882                 if (fifo_isfull(rxfifo))
883                         RTS_OFF;
884 #endif
885         }
886         /* Re-enable receive complete int */
887         //IRQ_DISABLE;
888         //UCSR1B |= BV(RXCIE);
889
890         SER_STROBE_OFF;
891 }
892
893 #endif // AVR_HAS_UART1
894
895
896 /**
897  * SPI interrupt handler
898  */
899 SIGNAL(SIG_SPI)
900 {
901         SER_STROBE_ON;
902
903         /* Read incoming byte. */
904         if (!fifo_isfull(&ser_handles[SER_SPI]->rxfifo))
905                 fifo_push(&ser_handles[SER_SPI]->rxfifo, SPDR);
906         /*
907          * FIXME
908         else
909                 ser_handles[SER_SPI]->status |= SERRF_RXFIFOOVERRUN;
910         */
911
912         /* Send */
913         if (!fifo_isempty(&ser_handles[SER_SPI]->txfifo))
914                 SPDR = fifo_pop(&ser_handles[SER_SPI]->txfifo);
915         else
916                 UARTDescs[SER_SPI].sending = false;
917
918         SER_STROBE_OFF;
919 }