11a6c65c024c7d628f5041056d556d238eeab21e
[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, 2010 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  * \author Bernie Innocenti <bernie@codewiz.org>
37  * \author Stefano Fedrigo <aleph@develer.com>
38  * \author Luca Ottaviano <lottaviano@develer.com>
39  */
40
41 #include "hw/hw_ser.h"  /* Required for bus macros overrides */
42 #include <hw/hw_cpufreq.h>  /* CPU_FREQ */
43
44 #include "cfg/cfg_ser.h"
45
46 #include <cfg/macros.h> /* DIV_ROUND */
47 #include <cfg/debug.h>
48 #include <cfg/cfg_arch.h> // ARCH_NIGHTTEST
49
50 #include <drv/ser.h>
51 #include <drv/ser_p.h>
52 #include <drv/timer.h>
53
54 #include <struct/fifobuf.h>
55
56 #include <avr/io.h>
57
58 #if defined(__AVR_LIBC_VERSION__) && (__AVR_LIBC_VERSION__ >= 10400UL)
59         #include <avr/interrupt.h>
60 #else
61         #include <avr/signal.h>
62 #endif
63
64
65 #if !CONFIG_SER_HWHANDSHAKE
66         /**
67          * \name Hardware handshake (RTS/CTS).
68          * \{
69          */
70         #define RTS_ON      do {} while (0)
71         #define RTS_OFF     do {} while (0)
72         #define IS_CTS_ON   true
73         #define EIMSKF_CTS  0 /**< Dummy value, must be overridden */
74         /*\}*/
75 #endif
76
77 #if CPU_AVR_ATMEGA1281 || CPU_AVR_ATMEGA1280
78         #define BIT_RXCIE0 RXCIE0
79         #define BIT_RXEN0  RXEN0
80         #define BIT_TXEN0  TXEN0
81         #define BIT_UDRIE0 UDRIE0
82
83         #define BIT_RXCIE1 RXCIE1
84         #define BIT_RXEN1  RXEN1
85         #define BIT_TXEN1  TXEN1
86         #define BIT_UDRIE1 UDRIE1
87         #if CPU_AVR_ATMEGA1280
88                 #define BIT_RXCIE2 RXCIE2
89                 #define BIT_RXEN2  RXEN2
90                 #define BIT_TXEN2  TXEN2
91                 #define BIT_UDRIE2 UDRIE2
92
93                 #define BIT_RXCIE3 RXCIE3
94                 #define BIT_RXEN3  RXEN3
95                 #define BIT_TXEN3  TXEN3
96                 #define BIT_UDRIE3 UDRIE3
97         #endif
98 #elif CPU_AVR_ATMEGA168 || CPU_AVR_ATMEGA328P
99         #define BIT_RXCIE0 RXCIE0
100         #define BIT_RXEN0  RXEN0
101         #define BIT_TXEN0  TXEN0
102         #define BIT_UDRIE0 UDRIE0
103
104         #define BIT_RXCIE1 RXCIE0
105         #define BIT_RXEN1  RXEN0
106         #define BIT_TXEN1  TXEN0
107         #define BIT_UDRIE1 UDRIE0
108 #else
109         #define BIT_RXCIE0 RXCIE
110         #define BIT_RXEN0  RXEN
111         #define BIT_TXEN0  TXEN
112         #define BIT_UDRIE0 UDRIE
113
114         #define BIT_RXCIE1 RXCIE
115         #define BIT_RXEN1  RXEN
116         #define BIT_TXEN1  TXEN
117         #define BIT_UDRIE1 UDRIE
118 #endif
119
120
121 /**
122  * \name Overridable serial bus hooks
123  *
124  * These can be redefined in hw.h to implement
125  * special bus policies such as half-duplex, 485, etc.
126  *
127  *
128  * \code
129  *  TXBEGIN      TXCHAR      TXEND  TXOFF
130  *    |   __________|__________ |     |
131  *    |   |   |   |   |   |   | |     |
132  *    v   v   v   v   v   v   v v     v
133  * ______  __  __  __  __  __  __  ________________
134  *       \/  \/  \/  \/  \/  \/  \/
135  * ______/\__/\__/\__/\__/\__/\__/
136  *
137  * \endcode
138  *
139  * \{
140  */
141 #ifndef SER_UART0_BUS_TXINIT
142         /**
143          * Default TXINIT macro - invoked in uart0_init()
144          *
145          * - Enable both the receiver and the transmitter
146          * - Enable only the RX complete interrupt
147          */
148         #define SER_UART0_BUS_TXINIT do { \
149                 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0); \
150         } while (0)
151 #endif
152
153 #ifndef SER_UART0_BUS_TXBEGIN
154         /**
155          * Invoked before starting a transmission
156          *
157          * - Enable both the receiver and the transmitter
158          * - Enable both the RX complete and UDR empty interrupts
159          */
160         #define SER_UART0_BUS_TXBEGIN do { \
161                 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_UDRIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0); \
162         } while (0)
163 #endif
164
165 #ifndef SER_UART0_BUS_TXCHAR
166         /**
167          * Invoked to send one character.
168          */
169         #define SER_UART0_BUS_TXCHAR(c) do { \
170                 UDR0 = (c); \
171         } while (0)
172 #endif
173
174 #ifndef SER_UART0_BUS_TXEND
175         /**
176          * Invoked as soon as the txfifo becomes empty
177          *
178          * - Keep both the receiver and the transmitter enabled
179          * - Keep the RX complete interrupt enabled
180          * - Disable the UDR empty interrupt
181          */
182         #define SER_UART0_BUS_TXEND do { \
183                 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0); \
184         } while (0)
185 #endif
186
187 #ifndef SER_UART0_BUS_TXOFF
188         /**
189          * \def SER_UART0_BUS_TXOFF
190          *
191          * Invoked after the last character has been transmitted
192          *
193          * The default is no action.
194          */
195         #ifdef __doxygen__
196         #define SER_UART0_BUS_TXOFF
197         #endif
198 #endif
199
200 #ifndef SER_UART1_BUS_TXINIT
201         /** \sa SER_UART0_BUS_TXINIT */
202         #define SER_UART1_BUS_TXINIT do { \
203                 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1); \
204         } while (0)
205 #endif
206 #ifndef SER_UART1_BUS_TXBEGIN
207         /** \sa SER_UART0_BUS_TXBEGIN */
208         #define SER_UART1_BUS_TXBEGIN do { \
209                 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_UDRIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1); \
210         } while (0)
211 #endif
212 #ifndef SER_UART1_BUS_TXCHAR
213         /** \sa SER_UART0_BUS_TXCHAR */
214         #define SER_UART1_BUS_TXCHAR(c) do { \
215                 UDR1 = (c); \
216         } while (0)
217 #endif
218 #ifndef SER_UART1_BUS_TXEND
219         /** \sa SER_UART0_BUS_TXEND */
220         #define SER_UART1_BUS_TXEND do { \
221                 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1); \
222         } while (0)
223 #endif
224 #ifndef SER_UART1_BUS_TXOFF
225         /**
226          * \def SER_UART1_BUS_TXOFF
227          *
228          * \see SER_UART0_BUS_TXOFF
229          */
230         #ifdef __doxygen__
231         #define SER_UART1_BUS_TXOFF
232         #endif
233 #endif
234
235 #ifndef SER_UART2_BUS_TXINIT
236         /** \sa SER_UART0_BUS_TXINIT */
237         #define SER_UART2_BUS_TXINIT do { \
238                 UCSR2B = BV(BIT_RXCIE2) | BV(BIT_RXEN2) | BV(BIT_TXEN2); \
239         } while (0)
240 #endif
241 #ifndef SER_UART2_BUS_TXBEGIN
242         /** \sa SER_UART0_BUS_TXBEGIN */
243         #define SER_UART2_BUS_TXBEGIN do { \
244                 UCSR2B = BV(BIT_RXCIE2) | BV(BIT_UDRIE2) | BV(BIT_RXEN2) | BV(BIT_TXEN2); \
245         } while (0)
246 #endif
247 #ifndef SER_UART2_BUS_TXCHAR
248         /** \sa SER_UART0_BUS_TXCHAR */
249         #define SER_UART2_BUS_TXCHAR(c) do { \
250                 UDR2 = (c); \
251         } while (0)
252 #endif
253 #ifndef SER_UART2_BUS_TXEND
254         /** \sa SER_UART0_BUS_TXEND */
255         #define SER_UART2_BUS_TXEND do { \
256                 UCSR2B = BV(BIT_RXCIE2) | BV(BIT_RXEN2) | BV(BIT_TXEN2); \
257         } while (0)
258 #endif
259 #ifndef SER_UART2_BUS_TXOFF
260         /**
261          * \def SER_UART2_BUS_TXOFF
262          *
263          * \see SER_UART0_BUS_TXOFF
264          */
265         #ifdef __doxygen__
266         #define SER_UART2_BUS_TXOFF
267         #endif
268 #endif
269
270 #ifndef SER_UART3_BUS_TXINIT
271         /** \sa SER_UART0_BUS_TXINIT */
272         #define SER_UART3_BUS_TXINIT do { \
273                 UCSR3B = BV(BIT_RXCIE3) | BV(BIT_RXEN3) | BV(BIT_TXEN3); \
274         } while (0)
275 #endif
276 #ifndef SER_UART3_BUS_TXBEGIN
277         /** \sa SER_UART0_BUS_TXBEGIN */
278         #define SER_UART3_BUS_TXBEGIN do { \
279                 UCSR3B = BV(BIT_RXCIE3) | BV(BIT_UDRIE3) | BV(BIT_RXEN3) | BV(BIT_TXEN3); \
280         } while (0)
281 #endif
282 #ifndef SER_UART3_BUS_TXCHAR
283         /** \sa SER_UART0_BUS_TXCHAR */
284         #define SER_UART3_BUS_TXCHAR(c) do { \
285                 UDR3 = (c); \
286         } while (0)
287 #endif
288 #ifndef SER_UART3_BUS_TXEND
289         /** \sa SER_UART0_BUS_TXEND */
290         #define SER_UART3_BUS_TXEND do { \
291                 UCSR3B = BV(BIT_RXCIE3) | BV(BIT_RXEN3) | BV(BIT_TXEN3); \
292         } while (0)
293 #endif
294 #ifndef SER_UART3_BUS_TXOFF
295         /**
296          * \def SER_UART3_BUS_TXOFF
297          *
298          * \see SER_UART0_BUS_TXOFF
299          */
300         #ifdef __doxygen__
301         #define SER_UART3_BUS_TXOFF
302         #endif
303 #endif
304 /*\}*/
305
306
307 /**
308  * \name Overridable SPI hooks
309  *
310  * These can be redefined in hw.h to implement
311  * special bus policies such as slave select pin handling, etc.
312  *
313  * \{
314  */
315 #ifndef SER_SPI_BUS_TXINIT
316         /**
317          * Default TXINIT macro - invoked in spi_init()
318          * The default is no action.
319          */
320         #define SER_SPI_BUS_TXINIT
321 #endif
322
323 #ifndef SER_SPI_BUS_TXCLOSE
324         /**
325          * Invoked after the last character has been transmitted.
326          * The default is no action.
327          */
328         #define SER_SPI_BUS_TXCLOSE
329 #endif
330 /*\}*/
331
332
333 /* SPI port and pin configuration */
334 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA103 || CPU_AVR_ATMEGA1281 \
335     || CPU_AVR_ATMEGA1280
336         #define SPI_PORT      PORTB
337         #define SPI_DDR       DDRB
338         #define SPI_SS_BIT    PB0
339         #define SPI_SCK_BIT   PB1
340         #define SPI_MOSI_BIT  PB2
341         #define SPI_MISO_BIT  PB3
342 // TODO: these bits are the same as ATMEGA8 but the defines in avr-gcc are different.
343 // They should be the same!
344 #elif CPU_AVR_ATMEGA328P
345         #define SPI_PORT      PORTB
346         #define SPI_DDR       DDRB
347         #define SPI_SS_BIT    PORTB2
348         #define SPI_SCK_BIT   PORTB5
349         #define SPI_MOSI_BIT  PORTB3
350         #define SPI_MISO_BIT  PORTB4
351 #elif CPU_AVR_ATMEGA8 || CPU_AVR_ATMEGA168
352         #define SPI_PORT      PORTB
353         #define SPI_DDR       DDRB
354         #define SPI_SS_BIT    PB2
355         #define SPI_SCK_BIT   PB5
356         #define SPI_MOSI_BIT  PB3
357         #define SPI_MISO_BIT  PB4
358 #else
359         #error Unknown architecture
360 #endif
361
362 /* USART register definitions */
363 #if CPU_AVR_ATMEGA1280
364         #define AVR_HAS_UART1 1
365         #define AVR_HAS_UART2 1
366         #define AVR_HAS_UART3 1
367 #elif CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA1281
368         #define AVR_HAS_UART1 1
369         #define AVR_HAS_UART2 0
370         #define AVR_HAS_UART3 0
371 #elif CPU_AVR_ATMEGA168 || CPU_AVR_ATMEGA328P
372         #define AVR_HAS_UART1 0
373         #define AVR_HAS_UART2 0
374         #define AVR_HAS_UART3 0
375         #define USART0_UDRE_vect USART_UDRE_vect
376         #define USART0_RX_vect USART_RX_vect
377         #define USART0_TX_vect USART_TX_vect
378 #elif CPU_AVR_ATMEGA8
379         #define AVR_HAS_UART1 0
380         #define AVR_HAS_UART2 0
381         #define AVR_HAS_UART3 0
382         #define UCSR0A UCSRA
383         #define UCSR0B UCSRB
384         #define UCSR0C UCSRC
385         #define UDR0   UDR
386         #define UBRR0L UBRRL
387         #define UBRR0H UBRRH
388         #define USART0_UDRE_vect USART_UDRE_vect
389         #define USART0_RX_vect USART_RX_vect
390         #define USART0_TX_vect USART_TX_vect
391 #elif CPU_AVR_ATMEGA103
392         #define AVR_HAS_UART1 0
393         #define AVR_HAS_UART2 0
394         #define AVR_HAS_UART3 0
395         #define UCSR0B UCR
396         #define UDR0   UDR
397         #define UCSR0A USR
398         #define UBRR0L UBRR
399         #define USART0_UDRE_vect USART_UDRE_vect
400         #define USART0_RX_vect USART_RX_vect
401         #define USART0_TX_vect USART_TX_vect
402 #else
403         #error Unknown architecture
404 #endif
405
406
407 /* From the high-level serial driver */
408 extern struct Serial *ser_handles[SER_CNT];
409
410 /* TX and RX buffers */
411 static unsigned char uart0_txbuffer[CONFIG_UART0_TXBUFSIZE];
412 static unsigned char uart0_rxbuffer[CONFIG_UART0_RXBUFSIZE];
413 #if AVR_HAS_UART1
414         static unsigned char uart1_txbuffer[CONFIG_UART1_TXBUFSIZE];
415         static unsigned char uart1_rxbuffer[CONFIG_UART1_RXBUFSIZE];
416 #endif
417 #if AVR_HAS_UART2
418         static unsigned char uart2_txbuffer[CONFIG_UART2_TXBUFSIZE];
419         static unsigned char uart2_rxbuffer[CONFIG_UART2_RXBUFSIZE];
420 #endif
421 #if AVR_HAS_UART3
422         static unsigned char uart3_txbuffer[CONFIG_UART3_TXBUFSIZE];
423         static unsigned char uart3_rxbuffer[CONFIG_UART3_RXBUFSIZE];
424 #endif
425 static unsigned char spi_txbuffer[CONFIG_SPI_TXBUFSIZE];
426 static unsigned char spi_rxbuffer[CONFIG_SPI_RXBUFSIZE];
427
428
429 /**
430  * Internal hardware state structure
431  *
432  * The \a sending variable is true while the transmission
433  * interrupt is retriggering itself.
434  *
435  * For the USARTs the \a sending flag is useful for taking specific
436  * actions before sending a burst of data, at the start of a trasmission
437  * but not before every char sent.
438  *
439  * For the SPI, this flag is necessary because the SPI sends and receives
440  * bytes at the same time and the SPI IRQ is unique for send/receive.
441  * The only way to start transmission is to write data in SPDR (this
442  * is done by spi_starttx()). We do this *only* if a transfer is
443  * not already started.
444  */
445 struct AvrSerial
446 {
447         struct SerialHardware hw;
448         volatile bool sending;
449 };
450
451
452
453 /*
454  * Callbacks
455  */
456 static void uart0_init(
457         UNUSED_ARG(struct SerialHardware *, _hw),
458         UNUSED_ARG(struct Serial *, ser))
459 {
460         SER_UART0_BUS_TXINIT;
461         RTS_ON;
462         SER_STROBE_INIT;
463 }
464
465 static void uart0_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
466 {
467         UCSR0B = 0;
468 }
469
470 static void uart0_enabletxirq(struct SerialHardware *_hw)
471 {
472         struct AvrSerial *hw = (struct AvrSerial *)_hw;
473
474         /*
475          * WARNING: racy code here!  The tx interrupt sets hw->sending to false
476          * when it runs with an empty fifo.  The order of statements in the
477          * if-block matters.
478          */
479         if (!hw->sending)
480         {
481                 hw->sending = true;
482                 SER_UART0_BUS_TXBEGIN;
483         }
484 }
485
486 static void uart0_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
487 {
488         /* Compute baud-rate period */
489         uint16_t period = DIV_ROUND(CPU_FREQ / 16UL, rate) - 1;
490
491 #if !CPU_AVR_ATMEGA103
492         UBRR0H = (period) >> 8;
493 #endif
494         UBRR0L = (period);
495
496         //DB(kprintf("uart0_setbaudrate(rate=%lu): period=%d\n", rate, period);)
497 }
498
499 static void uart0_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
500 {
501 #if !CPU_AVR_ATMEGA103
502         UCSR0C = (UCSR0C & ~(BV(UPM01) | BV(UPM00))) | ((parity) << UPM00);
503 #endif
504 }
505
506 #if AVR_HAS_UART1
507
508 static void uart1_init(
509         UNUSED_ARG(struct SerialHardware *, _hw),
510         UNUSED_ARG(struct Serial *, ser))
511 {
512         SER_UART1_BUS_TXINIT;
513         RTS_ON;
514         SER_STROBE_INIT;
515 }
516
517 static void uart1_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
518 {
519         UCSR1B = 0;
520 }
521
522 static void uart1_enabletxirq(struct SerialHardware *_hw)
523 {
524         struct AvrSerial *hw = (struct AvrSerial *)_hw;
525
526         /*
527          * WARNING: racy code here!  The tx interrupt
528          * sets hw->sending to false when it runs with
529          * an empty fifo.  The order of the statements
530          * in the if-block matters.
531          */
532         if (!hw->sending)
533         {
534                 hw->sending = true;
535                 SER_UART1_BUS_TXBEGIN;
536         }
537 }
538
539 static void uart1_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
540 {
541         /* Compute baud-rate period */
542         uint16_t period = DIV_ROUND(CPU_FREQ / 16UL, rate) - 1;
543
544         UBRR1H = (period) >> 8;
545         UBRR1L = (period);
546
547         //DB(kprintf("uart1_setbaudrate(rate=%ld): period=%d\n", rate, period);)
548 }
549
550 static void uart1_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
551 {
552         UCSR1C = (UCSR1C & ~(BV(UPM11) | BV(UPM10))) | ((parity) << UPM10);
553 }
554
555 #endif // AVR_HAS_UART1
556
557 #if AVR_HAS_UART2
558
559 static void uart2_init(
560         UNUSED_ARG(struct SerialHardware *, _hw),
561         UNUSED_ARG(struct Serial *, ser))
562 {
563         SER_UART2_BUS_TXINIT;
564         RTS_ON;
565         SER_STROBE_INIT;
566 }
567
568 static void uart2_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
569 {
570         UCSR2B = 0;
571 }
572
573 static void uart2_enabletxirq(struct SerialHardware *_hw)
574 {
575         struct AvrSerial *hw = (struct AvrSerial *)_hw;
576
577         /*
578          * WARNING: racy code here!  The tx interrupt
579          * sets hw->sending to false when it runs with
580          * an empty fifo.  The order of the statements
581          * in the if-block matters.
582          */
583         if (!hw->sending)
584         {
585                 hw->sending = true;
586                 SER_UART2_BUS_TXBEGIN;
587         }
588 }
589
590 static void uart2_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
591 {
592         /* Compute baud-rate period */
593         uint16_t period = DIV_ROUND(CPU_FREQ / 16UL, rate) - 1;
594
595         UBRR2H = (period) >> 8;
596         UBRR2L = (period);
597
598         //DB(kprintf("uart2_setbaudrate(rate=%ld): period=%d\n", rate, period);)
599 }
600
601 static void uart2_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
602 {
603         UCSR2C = (UCSR2C & ~(BV(UPM21) | BV(UPM20))) | ((parity) << UPM20);
604 }
605
606 #endif // AVR_HAS_UART2
607
608 #if AVR_HAS_UART3
609
610 static void uart3_init(
611         UNUSED_ARG(struct SerialHardware *, _hw),
612         UNUSED_ARG(struct Serial *, ser))
613 {
614         SER_UART3_BUS_TXINIT;
615         RTS_ON;
616         SER_STROBE_INIT;
617 }
618
619 static void uart3_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
620 {
621         UCSR3B = 0;
622 }
623
624 static void uart3_enabletxirq(struct SerialHardware *_hw)
625 {
626         struct AvrSerial *hw = (struct AvrSerial *)_hw;
627
628         /*
629          * WARNING: racy code here!  The tx interrupt
630          * sets hw->sending to false when it runs with
631          * an empty fifo.  The order of the statements
632          * in the if-block matters.
633          */
634         if (!hw->sending)
635         {
636                 hw->sending = true;
637                 SER_UART3_BUS_TXBEGIN;
638         }
639 }
640
641 static void uart3_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
642 {
643         /* Compute baud-rate period */
644         uint16_t period = DIV_ROUND(CPU_FREQ / 16UL, rate) - 1;
645
646         UBRR3H = (period) >> 8;
647         UBRR3L = (period);
648
649         //DB(kprintf("uart3_setbaudrate(rate=%ld): period=%d\n", rate, period);)
650 }
651
652 static void uart3_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
653 {
654         UCSR3C = (UCSR3C & ~(BV(UPM31) | BV(UPM30))) | ((parity) << UPM30);
655 }
656
657 #endif // AVR_HAS_UART3
658
659
660 static void spi_init(UNUSED_ARG(struct SerialHardware *, _hw), UNUSED_ARG(struct Serial *, ser))
661 {
662         /*
663          * Set MOSI and SCK ports out, MISO in.
664          *
665          * The ATmega64/128 datasheet explicitly states that the input/output
666          * state of the SPI pins is not significant, as when the SPI is
667          * active the I/O port are overrided.
668          * This is *blatantly FALSE*.
669          *
670          * Moreover, the MISO pin on the board_kc *must* be in high impedance
671          * state even when the SPI is off, because the line is wired together
672          * with the KBus serial RX, and the transmitter of the slave boards
673          * would be unable to drive the line.
674          */
675         ATOMIC(SPI_DDR |= (BV(SPI_MOSI_BIT) | BV(SPI_SCK_BIT)));
676
677         /*
678          * If the SPI master mode is activated and the SS pin is in input and tied low,
679          * the SPI hardware will automatically switch to slave mode!
680          * For proper communication this pins should therefore be:
681          * - as output
682          * - as input but tied high forever!
683          * This driver set the pin as output.
684          */
685         #warning FIXME:SPI SS pin set as output for proper operation, check schematics for possible conflicts.
686         ATOMIC(SPI_DDR |= BV(SPI_SS_BIT));
687
688         ATOMIC(SPI_DDR &= ~BV(SPI_MISO_BIT));
689         /* Enable SPI, IRQ on, Master */
690         SPCR = BV(SPE) | BV(SPIE) | BV(MSTR);
691
692         /* Set data order */
693         #if CONFIG_SPI_DATA_ORDER == SER_LSB_FIRST
694                 SPCR |= BV(DORD);
695         #endif
696
697         /* Set SPI clock rate */
698         #if CONFIG_SPI_CLOCK_DIV == 128
699                 SPCR |= (BV(SPR1) | BV(SPR0));
700         #elif (CONFIG_SPI_CLOCK_DIV == 64 || CONFIG_SPI_CLOCK_DIV == 32)
701                 SPCR |= BV(SPR1);
702         #elif (CONFIG_SPI_CLOCK_DIV == 16 || CONFIG_SPI_CLOCK_DIV == 8)
703                 SPCR |= BV(SPR0);
704         #elif (CONFIG_SPI_CLOCK_DIV == 4 || CONFIG_SPI_CLOCK_DIV == 2)
705                 // SPR0 & SDPR1 both at 0
706         #else
707                 #error Unsupported SPI clock division factor.
708         #endif
709
710         /* Set SPI2X bit (spi double frequency) */
711         #if (CONFIG_SPI_CLOCK_DIV == 128 || CONFIG_SPI_CLOCK_DIV == 64 \
712           || CONFIG_SPI_CLOCK_DIV == 16 || CONFIG_SPI_CLOCK_DIV == 4)
713                 SPSR &= ~BV(SPI2X);
714         #elif (CONFIG_SPI_CLOCK_DIV == 32 || CONFIG_SPI_CLOCK_DIV == 8 || CONFIG_SPI_CLOCK_DIV == 2)
715                 SPSR |= BV(SPI2X);
716         #else
717                 #error Unsupported SPI clock division factor.
718         #endif
719
720         /* Set clock polarity */
721         #if CONFIG_SPI_CLOCK_POL == 1
722                 SPCR |= BV(CPOL);
723         #endif
724
725         /* Set clock phase */
726         #if CONFIG_SPI_CLOCK_PHASE == 1
727                 SPCR |= BV(CPHA);
728         #endif
729         SER_SPI_BUS_TXINIT;
730
731         SER_STROBE_INIT;
732 }
733
734 static void spi_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
735 {
736         SPCR = 0;
737
738         SER_SPI_BUS_TXCLOSE;
739
740         /* Set all pins as inputs */
741         ATOMIC(SPI_DDR &= ~(BV(SPI_MISO_BIT) | BV(SPI_MOSI_BIT) | BV(SPI_SCK_BIT) | BV(SPI_SS_BIT)));
742 }
743
744 static void spi_starttx(struct SerialHardware *_hw)
745 {
746         struct AvrSerial *hw = (struct AvrSerial *)_hw;
747
748         cpu_flags_t flags;
749         IRQ_SAVE_DISABLE(flags);
750
751         /* Send data only if the SPI is not already transmitting */
752         if (!hw->sending && !fifo_isempty(&ser_handles[SER_SPI]->txfifo))
753         {
754                 hw->sending = true;
755                 SPDR = fifo_pop(&ser_handles[SER_SPI]->txfifo);
756         }
757
758         IRQ_RESTORE(flags);
759 }
760
761 static void spi_setbaudrate(
762         UNUSED_ARG(struct SerialHardware *, _hw),
763         UNUSED_ARG(unsigned long, rate))
764 {
765         // nop
766 }
767
768 static void spi_setparity(UNUSED_ARG(struct SerialHardware *, _hw), UNUSED_ARG(int, parity))
769 {
770         // nop
771 }
772
773 static bool tx_sending(struct SerialHardware* _hw)
774 {
775         struct AvrSerial *hw = (struct AvrSerial *)_hw;
776         return hw->sending;
777 }
778
779
780
781 // FIXME: move into compiler.h?  Ditch?
782 #if COMPILER_C99
783         #define C99INIT(name,val) .name = val
784 #elif defined(__GNUC__)
785         #define C99INIT(name,val) name: val
786 #else
787         #warning No designated initializers, double check your code
788         #define C99INIT(name,val) (val)
789 #endif
790
791 /*
792  * High-level interface data structures
793  */
794 static const struct SerialHardwareVT UART0_VT =
795 {
796         C99INIT(init, uart0_init),
797         C99INIT(cleanup, uart0_cleanup),
798         C99INIT(setBaudrate, uart0_setbaudrate),
799         C99INIT(setParity, uart0_setparity),
800         C99INIT(txStart, uart0_enabletxirq),
801         C99INIT(txSending, tx_sending),
802 };
803
804 #if AVR_HAS_UART1
805 static const struct SerialHardwareVT UART1_VT =
806 {
807         C99INIT(init, uart1_init),
808         C99INIT(cleanup, uart1_cleanup),
809         C99INIT(setBaudrate, uart1_setbaudrate),
810         C99INIT(setParity, uart1_setparity),
811         C99INIT(txStart, uart1_enabletxirq),
812         C99INIT(txSending, tx_sending),
813 };
814 #endif // AVR_HAS_UART1
815
816 #if AVR_HAS_UART2
817 static const struct SerialHardwareVT UART2_VT =
818 {
819         C99INIT(init, uart2_init),
820         C99INIT(cleanup, uart2_cleanup),
821         C99INIT(setBaudrate, uart2_setbaudrate),
822         C99INIT(setParity, uart2_setparity),
823         C99INIT(txStart, uart2_enabletxirq),
824         C99INIT(txSending, tx_sending),
825 };
826 #endif // AVR_HAS_UART2
827
828 #if AVR_HAS_UART3
829 static const struct SerialHardwareVT UART3_VT =
830 {
831         C99INIT(init, uart3_init),
832         C99INIT(cleanup, uart3_cleanup),
833         C99INIT(setBaudrate, uart3_setbaudrate),
834         C99INIT(setParity, uart3_setparity),
835         C99INIT(txStart, uart3_enabletxirq),
836         C99INIT(txSending, tx_sending),
837 };
838 #endif // AVR_HAS_UART3
839
840 static const struct SerialHardwareVT SPI_VT =
841 {
842         C99INIT(init, spi_init),
843         C99INIT(cleanup, spi_cleanup),
844         C99INIT(setBaudrate, spi_setbaudrate),
845         C99INIT(setParity, spi_setparity),
846         C99INIT(txStart, spi_starttx),
847         C99INIT(txSending, tx_sending),
848 };
849
850 static struct AvrSerial UARTDescs[SER_CNT] =
851 {
852         {
853                 C99INIT(hw, /**/) {
854                         C99INIT(table, &UART0_VT),
855                         C99INIT(txbuffer, uart0_txbuffer),
856                         C99INIT(rxbuffer, uart0_rxbuffer),
857                         C99INIT(txbuffer_size, sizeof(uart0_txbuffer)),
858                         C99INIT(rxbuffer_size, sizeof(uart0_rxbuffer)),
859                 },
860                 C99INIT(sending, false),
861         },
862 #if AVR_HAS_UART1
863         {
864                 C99INIT(hw, /**/) {
865                         C99INIT(table, &UART1_VT),
866                         C99INIT(txbuffer, uart1_txbuffer),
867                         C99INIT(rxbuffer, uart1_rxbuffer),
868                         C99INIT(txbuffer_size, sizeof(uart1_txbuffer)),
869                         C99INIT(rxbuffer_size, sizeof(uart1_rxbuffer)),
870                 },
871                 C99INIT(sending, false),
872         },
873 #endif
874 #if AVR_HAS_UART2
875         {
876                 C99INIT(hw, /**/) {
877                         C99INIT(table, &UART2_VT),
878                         C99INIT(txbuffer, uart2_txbuffer),
879                         C99INIT(rxbuffer, uart2_rxbuffer),
880                         C99INIT(txbuffer_size, sizeof(uart2_txbuffer)),
881                         C99INIT(rxbuffer_size, sizeof(uart2_rxbuffer)),
882                 },
883                 C99INIT(sending, false),
884         },
885 #endif
886 #if AVR_HAS_UART3
887         {
888                 C99INIT(hw, /**/) {
889                         C99INIT(table, &UART3_VT),
890                         C99INIT(txbuffer, uart3_txbuffer),
891                         C99INIT(rxbuffer, uart3_rxbuffer),
892                         C99INIT(txbuffer_size, sizeof(uart3_txbuffer)),
893                         C99INIT(rxbuffer_size, sizeof(uart3_rxbuffer)),
894                 },
895                 C99INIT(sending, false),
896         },
897 #endif
898         {
899                 C99INIT(hw, /**/) {
900                         C99INIT(table, &SPI_VT),
901                         C99INIT(txbuffer, spi_txbuffer),
902                         C99INIT(rxbuffer, spi_rxbuffer),
903                         C99INIT(txbuffer_size, sizeof(spi_txbuffer)),
904                         C99INIT(rxbuffer_size, sizeof(spi_rxbuffer)),
905                 },
906                 C99INIT(sending, false),
907         }
908 };
909
910 struct SerialHardware *ser_hw_getdesc(int unit)
911 {
912         ASSERT(unit < SER_CNT);
913         return &UARTDescs[unit].hw;
914 }
915
916
917 /*
918  * Interrupt handlers
919  */
920
921 #if CONFIG_SER_HWHANDSHAKE
922
923 /// This interrupt is triggered when the CTS line goes high
924 DECLARE_ISR(SIG_CTS)
925 {
926         // Re-enable UDR empty interrupt and TX, then disable CTS interrupt
927         UCSR0B = BV(BIT_RXCIE0) | BV(BIT_UDRIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0);
928         EIMSK &= ~EIMSKF_CTS;
929 }
930
931 #endif // CONFIG_SER_HWHANDSHAKE
932
933
934 /**
935  * Serial 0 TX interrupt handler
936  */
937 DECLARE_ISR(USART0_UDRE_vect)
938 {
939         SER_STROBE_ON;
940
941         struct FIFOBuffer * const txfifo = &ser_handles[SER_UART0]->txfifo;
942
943         if (fifo_isempty(txfifo))
944         {
945                 SER_UART0_BUS_TXEND;
946 #ifndef SER_UART0_BUS_TXOFF
947                 UARTDescs[SER_UART0].sending = false;
948 #endif
949         }
950 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA103
951         else if (!IS_CTS_ON)
952         {
953                 // Disable rx interrupt and tx, enable CTS interrupt
954                 // UNTESTED
955                 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0);
956                 EIFR |= EIMSKF_CTS;
957                 EIMSK |= EIMSKF_CTS;
958         }
959 #endif
960         else
961         {
962                 char c = fifo_pop(txfifo);
963                 SER_UART0_BUS_TXCHAR(c);
964         }
965
966         SER_STROBE_OFF;
967 }
968
969 #ifdef SER_UART0_BUS_TXOFF
970 /**
971  * Serial port 0 TX complete interrupt handler.
972  *
973  * This IRQ is usually disabled.  The UDR-empty interrupt
974  * enables it when there's no more data to transmit.
975  * We need to wait until the last character has been
976  * transmitted before switching the 485 transceiver to
977  * receive mode.
978  *
979  * The txfifo might have been refilled by putchar() while
980  * we were waiting for the transmission complete interrupt.
981  * In this case, we must restart the UDR empty interrupt,
982  * otherwise we'd stop the serial port with some data
983  * still pending in the buffer.
984  */
985 DECLARE_ISR(USART0_TX_vect)
986 {
987         SER_STROBE_ON;
988
989         struct FIFOBuffer * const txfifo = &ser_handles[SER_UART0]->txfifo;
990         if (fifo_isempty(txfifo))
991         {
992                 SER_UART0_BUS_TXOFF;
993                 UARTDescs[SER_UART0].sending = false;
994         }
995         else
996                 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_UDRIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0);
997
998         SER_STROBE_OFF;
999 }
1000 #endif /* SER_UART0_BUS_TXOFF */
1001
1002
1003 #if AVR_HAS_UART1
1004
1005 /**
1006  * Serial 1 TX interrupt handler
1007  */
1008 DECLARE_ISR(USART1_UDRE_vect)
1009 {
1010         SER_STROBE_ON;
1011
1012         struct FIFOBuffer * const txfifo = &ser_handles[SER_UART1]->txfifo;
1013
1014         if (fifo_isempty(txfifo))
1015         {
1016                 SER_UART1_BUS_TXEND;
1017 #ifndef SER_UART1_BUS_TXOFF
1018                 UARTDescs[SER_UART1].sending = false;
1019 #endif
1020         }
1021 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA103
1022         else if (!IS_CTS_ON)
1023         {
1024                 // Disable rx interrupt and tx, enable CTS interrupt
1025                 // UNTESTED
1026                 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1);
1027                 EIFR |= EIMSKF_CTS;
1028                 EIMSK |= EIMSKF_CTS;
1029         }
1030 #endif
1031         else
1032         {
1033                 char c = fifo_pop(txfifo);
1034                 SER_UART1_BUS_TXCHAR(c);
1035         }
1036
1037         SER_STROBE_OFF;
1038 }
1039
1040 #ifdef SER_UART1_BUS_TXOFF
1041 /**
1042  * Serial port 1 TX complete interrupt handler.
1043  *
1044  * \sa port 0 TX complete handler.
1045  */
1046 DECLARE_ISR(USART1_TX_vect)
1047 {
1048         SER_STROBE_ON;
1049
1050         struct FIFOBuffer * const txfifo = &ser_handles[SER_UART1]->txfifo;
1051         if (fifo_isempty(txfifo))
1052         {
1053                 SER_UART1_BUS_TXOFF;
1054                 UARTDescs[SER_UART1].sending = false;
1055         }
1056         else
1057                 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_UDRIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1);
1058
1059         SER_STROBE_OFF;
1060 }
1061 #endif /* SER_UART1_BUS_TXOFF */
1062
1063 #endif // AVR_HAS_UART1
1064
1065 #if AVR_HAS_UART2
1066
1067 /**
1068  * Serial 2 TX interrupt handler
1069  */
1070 DECLARE_ISR(USART2_UDRE_vect)
1071 {
1072         SER_STROBE_ON;
1073
1074         struct FIFOBuffer * const txfifo = &ser_handles[SER_UART2]->txfifo;
1075
1076         if (fifo_isempty(txfifo))
1077         {
1078                 SER_UART2_BUS_TXEND;
1079 #ifndef SER_UART2_BUS_TXOFF
1080                 UARTDescs[SER_UART2].sending = false;
1081 #endif
1082         }
1083
1084 /**
1085  * ATMEGA64, 128 and 103 do not have more than 2 USARTs
1086
1087 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA103
1088         else if (!IS_CTS_ON)
1089         {
1090                 // Disable rx interrupt and tx, enable CTS interrupt
1091                 // UNTESTED
1092                 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1);
1093                 EIFR |= EIMSKF_CTS;
1094                 EIMSK |= EIMSKF_CTS;
1095         }
1096 #endif
1097
1098  */
1099         else
1100         {
1101                 char c = fifo_pop(txfifo);
1102                 SER_UART2_BUS_TXCHAR(c);
1103         }
1104
1105         SER_STROBE_OFF;
1106 }
1107
1108 #ifdef SER_UART2_BUS_TXOFF
1109 /**
1110  * Serial port 2 TX complete interrupt handler.
1111  *
1112  * \sa port 0 TX complete handler.
1113  */
1114 DECLARE_ISR(USART2_TX_vect)
1115 {
1116         SER_STROBE_ON;
1117
1118         struct FIFOBuffer * const txfifo = &ser_handles[SER_UART2]->txfifo;
1119         if (fifo_isempty(txfifo))
1120         {
1121                 SER_UART2_BUS_TXOFF;
1122                 UARTDescs[SER_UART2].sending = false;
1123         }
1124         else
1125                 UCSR2B = BV(BIT_RXCIE2) | BV(BIT_UDRIE2) | BV(BIT_RXEN2) | BV(BIT_TXEN2);
1126
1127         SER_STROBE_OFF;
1128 }
1129 #endif /* SER_UART2_BUS_TXOFF */
1130
1131 #endif // AVR_HAS_UART2
1132
1133 #if AVR_HAS_UART3
1134
1135 /**
1136  * Serial 3 TX interrupt handler
1137  */
1138 DECLARE_ISR(USART3_UDRE_vect)
1139 {
1140         SER_STROBE_ON;
1141
1142         struct FIFOBuffer * const txfifo = &ser_handles[SER_UART3]->txfifo;
1143
1144         if (fifo_isempty(txfifo))
1145         {
1146                 SER_UART3_BUS_TXEND;
1147 #ifndef SER_UART3_BUS_TXOFF
1148                 UARTDescs[SER_UART3].sending = false;
1149 #endif
1150         }
1151
1152 /**
1153  * ATMEGA64, 128 and 103 do not have more than 2 USARTs
1154
1155 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA103
1156         else if (!IS_CTS_ON)
1157         {
1158                 // Disable rx interrupt and tx, enable CTS interrupt
1159                 // UNTESTED
1160                 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1);
1161                 EIFR |= EIMSKF_CTS;
1162                 EIMSK |= EIMSKF_CTS;
1163         }
1164 #endif
1165
1166  */
1167         else
1168         {
1169                 char c = fifo_pop(txfifo);
1170                 SER_UART3_BUS_TXCHAR(c);
1171         }
1172
1173         SER_STROBE_OFF;
1174 }
1175
1176 #ifdef SER_UART3_BUS_TXOFF
1177 /**
1178  * Serial port 3 TX complete interrupt handler.
1179  *
1180  * \sa port 0 TX complete handler.
1181  */
1182 DECLARE_ISR(USART3_TX_vect)
1183 {
1184         SER_STROBE_ON;
1185
1186         struct FIFOBuffer * const txfifo = &ser_handles[SER_UART3]->txfifo;
1187         if (fifo_isempty(txfifo))
1188         {
1189                 SER_UART3_BUS_TXOFF;
1190                 UARTDescs[SER_UART3].sending = false;
1191         }
1192         else
1193                 UCSR3B = BV(BIT_RXCIE3) | BV(BIT_UDRIE3) | BV(BIT_RXEN3) | BV(BIT_TXEN3);
1194
1195         SER_STROBE_OFF;
1196 }
1197 #endif /* SER_UART3_BUS_TXOFF */
1198
1199 #endif // AVR_HAS_UART3
1200
1201
1202 /**
1203  * Serial 0 RX complete interrupt handler.
1204  *
1205  * This handler is interruptible.
1206  * Interrupt are reenabled as soon as recv complete interrupt is
1207  * disabled. Using INTERRUPT() is troublesome when the serial
1208  * is heavily loaded, because an interrupt could be retriggered
1209  * when executing the handler prologue before RXCIE is disabled.
1210  *
1211  * \note The code that re-enables interrupts is commented out
1212  *       because in some nasty cases the interrupt is retriggered.
1213  *       This is probably due to the RXC flag being set before
1214  *       RXCIE is cleared.  Unfortunately the RXC flag is read-only
1215  *       and can't be cleared by code.
1216  */
1217 DECLARE_ISR(USART0_RX_vect)
1218 {
1219         SER_STROBE_ON;
1220
1221         /* Disable Recv complete IRQ */
1222         //UCSR0B &= ~BV(RXCIE);
1223         //IRQ_ENABLE;
1224
1225         /* Should be read before UDR */
1226         ser_handles[SER_UART0]->status |= UCSR0A & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
1227
1228         /* To clear the RXC flag we must _always_ read the UDR even when we're
1229          * not going to accept the incoming data, otherwise a new interrupt
1230          * will occur once the handler terminates.
1231          */
1232         char c = UDR0;
1233         struct FIFOBuffer * const rxfifo = &ser_handles[SER_UART0]->rxfifo;
1234
1235         if (fifo_isfull(rxfifo))
1236                 ser_handles[SER_UART0]->status |= SERRF_RXFIFOOVERRUN;
1237         else
1238         {
1239                 fifo_push(rxfifo, c);
1240 #if CONFIG_SER_HWHANDSHAKE
1241                 if (fifo_isfull(rxfifo))
1242                         RTS_OFF;
1243 #endif
1244         }
1245
1246         /* Reenable receive complete int */
1247         //IRQ_DISABLE;
1248         //UCSR0B |= BV(RXCIE);
1249
1250         SER_STROBE_OFF;
1251 }
1252
1253
1254 #if AVR_HAS_UART1
1255
1256 /**
1257  * Serial 1 RX complete interrupt handler.
1258  *
1259  * This handler is interruptible.
1260  * Interrupt are reenabled as soon as recv complete interrupt is
1261  * disabled. Using INTERRUPT() is troublesome when the serial
1262  * is heavily loaded, because an interrupt could be retriggered
1263  * when executing the handler prologue before RXCIE is disabled.
1264  *
1265  * \see DECLARE_ISR(USART1_RX_vect)
1266  */
1267 DECLARE_ISR(USART1_RX_vect)
1268 {
1269         SER_STROBE_ON;
1270
1271         /* Disable Recv complete IRQ */
1272         //UCSR1B &= ~BV(RXCIE);
1273         //IRQ_ENABLE;
1274
1275         /* Should be read before UDR */
1276         ser_handles[SER_UART1]->status |= UCSR1A & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
1277
1278         /* To avoid an IRQ storm, we must _always_ read the UDR even when we're
1279          * not going to accept the incoming data
1280          */
1281         char c = UDR1;
1282         struct FIFOBuffer * const rxfifo = &ser_handles[SER_UART1]->rxfifo;
1283         //ASSERT_VALID_FIFO(rxfifo);
1284
1285         if (UNLIKELY(fifo_isfull(rxfifo)))
1286                 ser_handles[SER_UART1]->status |= SERRF_RXFIFOOVERRUN;
1287         else
1288         {
1289                 fifo_push(rxfifo, c);
1290 #if CONFIG_SER_HWHANDSHAKE
1291                 if (fifo_isfull(rxfifo))
1292                         RTS_OFF;
1293 #endif
1294         }
1295         /* Re-enable receive complete int */
1296         //IRQ_DISABLE;
1297         //UCSR1B |= BV(RXCIE);
1298
1299         SER_STROBE_OFF;
1300 }
1301
1302 #endif // AVR_HAS_UART1
1303
1304 #if AVR_HAS_UART2
1305
1306 /**
1307  * Serial 2 RX complete interrupt handler.
1308  *
1309  * This handler is interruptible.
1310  * Interrupt are reenabled as soon as recv complete interrupt is
1311  * disabled. Using INTERRUPT() is troublesome when the serial
1312  * is heavily loaded, because an interrupt could be retriggered
1313  * when executing the handler prologue before RXCIE is disabled.
1314  *
1315  * \see DECLARE_ISR(USART2_RX_vect)
1316  */
1317 DECLARE_ISR(USART2_RX_vect)
1318 {
1319         SER_STROBE_ON;
1320
1321         /* Disable Recv complete IRQ */
1322         //UCSR1B &= ~BV(RXCIE);
1323         //IRQ_ENABLE;
1324
1325         /* Should be read before UDR */
1326         ser_handles[SER_UART2]->status |= UCSR2A & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
1327
1328         /* To avoid an IRQ storm, we must _always_ read the UDR even when we're
1329          * not going to accept the incoming data
1330          */
1331         char c = UDR2;
1332         struct FIFOBuffer * const rxfifo = &ser_handles[SER_UART2]->rxfifo;
1333         //ASSERT_VALID_FIFO(rxfifo);
1334
1335         if (UNLIKELY(fifo_isfull(rxfifo)))
1336                 ser_handles[SER_UART2]->status |= SERRF_RXFIFOOVERRUN;
1337         else
1338         {
1339                 fifo_push(rxfifo, c);
1340 #if CONFIG_SER_HWHANDSHAKE
1341                 if (fifo_isfull(rxfifo))
1342                         RTS_OFF;
1343 #endif
1344         }
1345         /* Re-enable receive complete int */
1346         //IRQ_DISABLE;
1347         //UCSR1B |= BV(RXCIE);
1348
1349         SER_STROBE_OFF;
1350 }
1351
1352 #endif // AVR_HAS_UART2
1353
1354 #if AVR_HAS_UART3
1355
1356 /**
1357  * Serial 3 RX complete interrupt handler.
1358  *
1359  * This handler is interruptible.
1360  * Interrupt are reenabled as soon as recv complete interrupt is
1361  * disabled. Using INTERRUPT() is troublesome when the serial
1362  * is heavily loaded, because an interrupt could be retriggered
1363  * when executing the handler prologue before RXCIE is disabled.
1364  *
1365  * \see DECLARE_ISR(USART3_RX_vect)
1366  */
1367 DECLARE_ISR(USART3_RX_vect)
1368 {
1369         SER_STROBE_ON;
1370
1371         /* Disable Recv complete IRQ */
1372         //UCSR1B &= ~BV(RXCIE);
1373         //IRQ_ENABLE;
1374
1375         /* Should be read before UDR */
1376         ser_handles[SER_UART3]->status |= UCSR3A & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
1377
1378         /* To avoid an IRQ storm, we must _always_ read the UDR even when we're
1379          * not going to accept the incoming data
1380          */
1381         char c = UDR3;
1382         struct FIFOBuffer * const rxfifo = &ser_handles[SER_UART3]->rxfifo;
1383         //ASSERT_VALID_FIFO(rxfifo);
1384
1385         if (UNLIKELY(fifo_isfull(rxfifo)))
1386                 ser_handles[SER_UART3]->status |= SERRF_RXFIFOOVERRUN;
1387         else
1388         {
1389                 fifo_push(rxfifo, c);
1390 #if CONFIG_SER_HWHANDSHAKE
1391                 if (fifo_isfull(rxfifo))
1392                         RTS_OFF;
1393 #endif
1394         }
1395         /* Re-enable receive complete int */
1396         //IRQ_DISABLE;
1397         //UCSR1B |= BV(RXCIE);
1398
1399         SER_STROBE_OFF;
1400 }
1401
1402 #endif // AVR_HAS_UART3
1403
1404
1405 /**
1406  * SPI interrupt handler
1407  */
1408 DECLARE_ISR(SPI_STC_vect)
1409 {
1410         SER_STROBE_ON;
1411
1412         /* Read incoming byte. */
1413         if (!fifo_isfull(&ser_handles[SER_SPI]->rxfifo))
1414                 fifo_push(&ser_handles[SER_SPI]->rxfifo, SPDR);
1415         /*
1416          * FIXME
1417         else
1418                 ser_handles[SER_SPI]->status |= SERRF_RXFIFOOVERRUN;
1419         */
1420
1421         /* Send */
1422         if (!fifo_isempty(&ser_handles[SER_SPI]->txfifo))
1423                 SPDR = fifo_pop(&ser_handles[SER_SPI]->txfifo);
1424         else
1425                 UARTDescs[SER_SPI].sending = false;
1426
1427         SER_STROBE_OFF;
1428 }