a4328f41b8552b60af93b6919b098484969672d3
[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 #elif CPU_AVR_ATMEGA32
359         #define SPI_PORT      PORTB
360         #define SPI_DDR       DDRB
361         #define SPI_SS_BIT    PB4
362         #define SPI_SCK_BIT   PB7
363         #define SPI_MOSI_BIT  PB5
364         #define SPI_MISO_BIT  PB6
365 #else
366         #error Unknown architecture
367 #endif
368
369 /* USART register definitions */
370 #if CPU_AVR_ATMEGA1280
371         #define AVR_HAS_UART1 1
372         #define AVR_HAS_UART2 1
373         #define AVR_HAS_UART3 1
374 #elif CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA1281
375         #define AVR_HAS_UART1 1
376         #define AVR_HAS_UART2 0
377         #define AVR_HAS_UART3 0
378 #elif CPU_AVR_ATMEGA168 || CPU_AVR_ATMEGA328P
379         #define AVR_HAS_UART1 0
380         #define AVR_HAS_UART2 0
381         #define AVR_HAS_UART3 0
382         #define USART0_UDRE_vect USART_UDRE_vect
383         #define USART0_RX_vect USART_RX_vect
384         #define USART0_TX_vect USART_TX_vect
385 #elif CPU_AVR_ATMEGA8 || CPU_AVR_ATMEGA32
386         #define AVR_HAS_UART1 0
387         #define AVR_HAS_UART2 0
388         #define AVR_HAS_UART3 0
389         #define UCSR0A UCSRA
390         #define UCSR0B UCSRB
391         #define UCSR0C UCSRC
392         #define UDR0   UDR
393         #define UBRR0L UBRRL
394         #define UBRR0H UBRRH
395         #define UPM01  UPM1
396         #define UPM00  UPM0
397         #define USART0_UDRE_vect USART_UDRE_vect
398         #define USART0_RX_vect USART_RXC_vect
399         #define USART0_TX_vect USART_TXC_vect
400 #elif CPU_AVR_ATMEGA103
401         #define AVR_HAS_UART1 0
402         #define AVR_HAS_UART2 0
403         #define AVR_HAS_UART3 0
404         #define UCSR0B UCR
405         #define UDR0   UDR
406         #define UCSR0A USR
407         #define UBRR0L UBRR
408         #define USART0_UDRE_vect USART_UDRE_vect
409         #define USART0_RX_vect USART_RX_vect
410         #define USART0_TX_vect USART_TX_vect
411 #else
412         #error Unknown architecture
413 #endif
414
415
416 /* From the high-level serial driver */
417 extern struct Serial *ser_handles[SER_CNT];
418
419 /* TX and RX buffers */
420 static unsigned char uart0_txbuffer[CONFIG_UART0_TXBUFSIZE];
421 static unsigned char uart0_rxbuffer[CONFIG_UART0_RXBUFSIZE];
422 #if AVR_HAS_UART1
423         static unsigned char uart1_txbuffer[CONFIG_UART1_TXBUFSIZE];
424         static unsigned char uart1_rxbuffer[CONFIG_UART1_RXBUFSIZE];
425 #endif
426 #if AVR_HAS_UART2
427         static unsigned char uart2_txbuffer[CONFIG_UART2_TXBUFSIZE];
428         static unsigned char uart2_rxbuffer[CONFIG_UART2_RXBUFSIZE];
429 #endif
430 #if AVR_HAS_UART3
431         static unsigned char uart3_txbuffer[CONFIG_UART3_TXBUFSIZE];
432         static unsigned char uart3_rxbuffer[CONFIG_UART3_RXBUFSIZE];
433 #endif
434 static unsigned char spi_txbuffer[CONFIG_SPI_TXBUFSIZE];
435 static unsigned char spi_rxbuffer[CONFIG_SPI_RXBUFSIZE];
436
437
438 /**
439  * Internal hardware state structure
440  *
441  * The \a sending variable is true while the transmission
442  * interrupt is retriggering itself.
443  *
444  * For the USARTs the \a sending flag is useful for taking specific
445  * actions before sending a burst of data, at the start of a trasmission
446  * but not before every char sent.
447  *
448  * For the SPI, this flag is necessary because the SPI sends and receives
449  * bytes at the same time and the SPI IRQ is unique for send/receive.
450  * The only way to start transmission is to write data in SPDR (this
451  * is done by spi_starttx()). We do this *only* if a transfer is
452  * not already started.
453  */
454 struct AvrSerial
455 {
456         struct SerialHardware hw;
457         volatile bool sending;
458 };
459
460
461
462 /*
463  * Callbacks
464  */
465 static void uart0_init(
466         UNUSED_ARG(struct SerialHardware *, _hw),
467         UNUSED_ARG(struct Serial *, ser))
468 {
469         SER_UART0_BUS_TXINIT;
470         RTS_ON;
471         SER_STROBE_INIT;
472 }
473
474 static void uart0_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
475 {
476         UCSR0B = 0;
477 }
478
479 static void uart0_enabletxirq(struct SerialHardware *_hw)
480 {
481         struct AvrSerial *hw = (struct AvrSerial *)_hw;
482
483         /*
484          * WARNING: racy code here!  The tx interrupt sets hw->sending to false
485          * when it runs with an empty fifo.  The order of statements in the
486          * if-block matters.
487          */
488         if (!hw->sending)
489         {
490                 hw->sending = true;
491                 SER_UART0_BUS_TXBEGIN;
492         }
493 }
494
495 static void uart0_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
496 {
497         /* Compute baud-rate period */
498         uint16_t period = DIV_ROUND(CPU_FREQ / 16UL, rate) - 1;
499
500 #if !CPU_AVR_ATMEGA103
501         UBRR0H = (period) >> 8;
502 #endif
503         UBRR0L = (period);
504
505         //DB(kprintf("uart0_setbaudrate(rate=%lu): period=%d\n", rate, period);)
506 }
507
508 static void uart0_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
509 {
510 #if !CPU_AVR_ATMEGA103
511         UCSR0C = (UCSR0C & ~(BV(UPM01) | BV(UPM00))) | ((parity) << UPM00);
512 #endif
513 }
514
515 #if AVR_HAS_UART1
516
517 static void uart1_init(
518         UNUSED_ARG(struct SerialHardware *, _hw),
519         UNUSED_ARG(struct Serial *, ser))
520 {
521         SER_UART1_BUS_TXINIT;
522         RTS_ON;
523         SER_STROBE_INIT;
524 }
525
526 static void uart1_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
527 {
528         UCSR1B = 0;
529 }
530
531 static void uart1_enabletxirq(struct SerialHardware *_hw)
532 {
533         struct AvrSerial *hw = (struct AvrSerial *)_hw;
534
535         /*
536          * WARNING: racy code here!  The tx interrupt
537          * sets hw->sending to false when it runs with
538          * an empty fifo.  The order of the statements
539          * in the if-block matters.
540          */
541         if (!hw->sending)
542         {
543                 hw->sending = true;
544                 SER_UART1_BUS_TXBEGIN;
545         }
546 }
547
548 static void uart1_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
549 {
550         /* Compute baud-rate period */
551         uint16_t period = DIV_ROUND(CPU_FREQ / 16UL, rate) - 1;
552
553         UBRR1H = (period) >> 8;
554         UBRR1L = (period);
555
556         //DB(kprintf("uart1_setbaudrate(rate=%ld): period=%d\n", rate, period);)
557 }
558
559 static void uart1_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
560 {
561         UCSR1C = (UCSR1C & ~(BV(UPM11) | BV(UPM10))) | ((parity) << UPM10);
562 }
563
564 #endif // AVR_HAS_UART1
565
566 #if AVR_HAS_UART2
567
568 static void uart2_init(
569         UNUSED_ARG(struct SerialHardware *, _hw),
570         UNUSED_ARG(struct Serial *, ser))
571 {
572         SER_UART2_BUS_TXINIT;
573         RTS_ON;
574         SER_STROBE_INIT;
575 }
576
577 static void uart2_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
578 {
579         UCSR2B = 0;
580 }
581
582 static void uart2_enabletxirq(struct SerialHardware *_hw)
583 {
584         struct AvrSerial *hw = (struct AvrSerial *)_hw;
585
586         /*
587          * WARNING: racy code here!  The tx interrupt
588          * sets hw->sending to false when it runs with
589          * an empty fifo.  The order of the statements
590          * in the if-block matters.
591          */
592         if (!hw->sending)
593         {
594                 hw->sending = true;
595                 SER_UART2_BUS_TXBEGIN;
596         }
597 }
598
599 static void uart2_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
600 {
601         /* Compute baud-rate period */
602         uint16_t period = DIV_ROUND(CPU_FREQ / 16UL, rate) - 1;
603
604         UBRR2H = (period) >> 8;
605         UBRR2L = (period);
606
607         //DB(kprintf("uart2_setbaudrate(rate=%ld): period=%d\n", rate, period);)
608 }
609
610 static void uart2_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
611 {
612         UCSR2C = (UCSR2C & ~(BV(UPM21) | BV(UPM20))) | ((parity) << UPM20);
613 }
614
615 #endif // AVR_HAS_UART2
616
617 #if AVR_HAS_UART3
618
619 static void uart3_init(
620         UNUSED_ARG(struct SerialHardware *, _hw),
621         UNUSED_ARG(struct Serial *, ser))
622 {
623         SER_UART3_BUS_TXINIT;
624         RTS_ON;
625         SER_STROBE_INIT;
626 }
627
628 static void uart3_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
629 {
630         UCSR3B = 0;
631 }
632
633 static void uart3_enabletxirq(struct SerialHardware *_hw)
634 {
635         struct AvrSerial *hw = (struct AvrSerial *)_hw;
636
637         /*
638          * WARNING: racy code here!  The tx interrupt
639          * sets hw->sending to false when it runs with
640          * an empty fifo.  The order of the statements
641          * in the if-block matters.
642          */
643         if (!hw->sending)
644         {
645                 hw->sending = true;
646                 SER_UART3_BUS_TXBEGIN;
647         }
648 }
649
650 static void uart3_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
651 {
652         /* Compute baud-rate period */
653         uint16_t period = DIV_ROUND(CPU_FREQ / 16UL, rate) - 1;
654
655         UBRR3H = (period) >> 8;
656         UBRR3L = (period);
657
658         //DB(kprintf("uart3_setbaudrate(rate=%ld): period=%d\n", rate, period);)
659 }
660
661 static void uart3_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
662 {
663         UCSR3C = (UCSR3C & ~(BV(UPM31) | BV(UPM30))) | ((parity) << UPM30);
664 }
665
666 #endif // AVR_HAS_UART3
667
668
669 static void spi_init(UNUSED_ARG(struct SerialHardware *, _hw), UNUSED_ARG(struct Serial *, ser))
670 {
671         /*
672          * Set MOSI and SCK ports out, MISO in.
673          *
674          * The ATmega64/128 datasheet explicitly states that the input/output
675          * state of the SPI pins is not significant, as when the SPI is
676          * active the I/O port are overrided.
677          * This is *blatantly FALSE*.
678          *
679          * Moreover, the MISO pin on the board_kc *must* be in high impedance
680          * state even when the SPI is off, because the line is wired together
681          * with the KBus serial RX, and the transmitter of the slave boards
682          * would be unable to drive the line.
683          */
684         ATOMIC(SPI_DDR |= (BV(SPI_MOSI_BIT) | BV(SPI_SCK_BIT)));
685
686         /*
687          * If the SPI master mode is activated and the SS pin is in input and tied low,
688          * the SPI hardware will automatically switch to slave mode!
689          * For proper communication this pins should therefore be:
690          * - as output
691          * - as input but tied high forever!
692          * This driver set the pin as output.
693          */
694         #warning FIXME:SPI SS pin set as output for proper operation, check schematics for possible conflicts.
695         ATOMIC(SPI_DDR |= BV(SPI_SS_BIT));
696
697         ATOMIC(SPI_DDR &= ~BV(SPI_MISO_BIT));
698         /* Enable SPI, IRQ on, Master */
699         SPCR = BV(SPE) | BV(SPIE) | BV(MSTR);
700
701         /* Set data order */
702         #if CONFIG_SPI_DATA_ORDER == SER_LSB_FIRST
703                 SPCR |= BV(DORD);
704         #endif
705
706         /* Set SPI clock rate */
707         #if CONFIG_SPI_CLOCK_DIV == 128
708                 SPCR |= (BV(SPR1) | BV(SPR0));
709         #elif (CONFIG_SPI_CLOCK_DIV == 64 || CONFIG_SPI_CLOCK_DIV == 32)
710                 SPCR |= BV(SPR1);
711         #elif (CONFIG_SPI_CLOCK_DIV == 16 || CONFIG_SPI_CLOCK_DIV == 8)
712                 SPCR |= BV(SPR0);
713         #elif (CONFIG_SPI_CLOCK_DIV == 4 || CONFIG_SPI_CLOCK_DIV == 2)
714                 // SPR0 & SDPR1 both at 0
715         #else
716                 #error Unsupported SPI clock division factor.
717         #endif
718
719         /* Set SPI2X bit (spi double frequency) */
720         #if (CONFIG_SPI_CLOCK_DIV == 128 || CONFIG_SPI_CLOCK_DIV == 64 \
721           || CONFIG_SPI_CLOCK_DIV == 16 || CONFIG_SPI_CLOCK_DIV == 4)
722                 SPSR &= ~BV(SPI2X);
723         #elif (CONFIG_SPI_CLOCK_DIV == 32 || CONFIG_SPI_CLOCK_DIV == 8 || CONFIG_SPI_CLOCK_DIV == 2)
724                 SPSR |= BV(SPI2X);
725         #else
726                 #error Unsupported SPI clock division factor.
727         #endif
728
729         /* Set clock polarity */
730         #if CONFIG_SPI_CLOCK_POL == 1
731                 SPCR |= BV(CPOL);
732         #endif
733
734         /* Set clock phase */
735         #if CONFIG_SPI_CLOCK_PHASE == 1
736                 SPCR |= BV(CPHA);
737         #endif
738         SER_SPI_BUS_TXINIT;
739
740         SER_STROBE_INIT;
741 }
742
743 static void spi_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
744 {
745         SPCR = 0;
746
747         SER_SPI_BUS_TXCLOSE;
748
749         /* Set all pins as inputs */
750         ATOMIC(SPI_DDR &= ~(BV(SPI_MISO_BIT) | BV(SPI_MOSI_BIT) | BV(SPI_SCK_BIT) | BV(SPI_SS_BIT)));
751 }
752
753 static void spi_starttx(struct SerialHardware *_hw)
754 {
755         struct AvrSerial *hw = (struct AvrSerial *)_hw;
756
757         cpu_flags_t flags;
758         IRQ_SAVE_DISABLE(flags);
759
760         /* Send data only if the SPI is not already transmitting */
761         if (!hw->sending && !fifo_isempty(&ser_handles[SER_SPI]->txfifo))
762         {
763                 hw->sending = true;
764                 SPDR = fifo_pop(&ser_handles[SER_SPI]->txfifo);
765         }
766
767         IRQ_RESTORE(flags);
768 }
769
770 static void spi_setbaudrate(
771         UNUSED_ARG(struct SerialHardware *, _hw),
772         UNUSED_ARG(unsigned long, rate))
773 {
774         // nop
775 }
776
777 static void spi_setparity(UNUSED_ARG(struct SerialHardware *, _hw), UNUSED_ARG(int, parity))
778 {
779         // nop
780 }
781
782 static bool tx_sending(struct SerialHardware* _hw)
783 {
784         struct AvrSerial *hw = (struct AvrSerial *)_hw;
785         return hw->sending;
786 }
787
788
789
790 // FIXME: move into compiler.h?  Ditch?
791 #if COMPILER_C99
792         #define C99INIT(name,val) .name = val
793 #elif defined(__GNUC__)
794         #define C99INIT(name,val) name: val
795 #else
796         #warning No designated initializers, double check your code
797         #define C99INIT(name,val) (val)
798 #endif
799
800 /*
801  * High-level interface data structures
802  */
803 static const struct SerialHardwareVT UART0_VT =
804 {
805         C99INIT(init, uart0_init),
806         C99INIT(cleanup, uart0_cleanup),
807         C99INIT(setBaudrate, uart0_setbaudrate),
808         C99INIT(setParity, uart0_setparity),
809         C99INIT(txStart, uart0_enabletxirq),
810         C99INIT(txSending, tx_sending),
811 };
812
813 #if AVR_HAS_UART1
814 static const struct SerialHardwareVT UART1_VT =
815 {
816         C99INIT(init, uart1_init),
817         C99INIT(cleanup, uart1_cleanup),
818         C99INIT(setBaudrate, uart1_setbaudrate),
819         C99INIT(setParity, uart1_setparity),
820         C99INIT(txStart, uart1_enabletxirq),
821         C99INIT(txSending, tx_sending),
822 };
823 #endif // AVR_HAS_UART1
824
825 #if AVR_HAS_UART2
826 static const struct SerialHardwareVT UART2_VT =
827 {
828         C99INIT(init, uart2_init),
829         C99INIT(cleanup, uart2_cleanup),
830         C99INIT(setBaudrate, uart2_setbaudrate),
831         C99INIT(setParity, uart2_setparity),
832         C99INIT(txStart, uart2_enabletxirq),
833         C99INIT(txSending, tx_sending),
834 };
835 #endif // AVR_HAS_UART2
836
837 #if AVR_HAS_UART3
838 static const struct SerialHardwareVT UART3_VT =
839 {
840         C99INIT(init, uart3_init),
841         C99INIT(cleanup, uart3_cleanup),
842         C99INIT(setBaudrate, uart3_setbaudrate),
843         C99INIT(setParity, uart3_setparity),
844         C99INIT(txStart, uart3_enabletxirq),
845         C99INIT(txSending, tx_sending),
846 };
847 #endif // AVR_HAS_UART3
848
849 static const struct SerialHardwareVT SPI_VT =
850 {
851         C99INIT(init, spi_init),
852         C99INIT(cleanup, spi_cleanup),
853         C99INIT(setBaudrate, spi_setbaudrate),
854         C99INIT(setParity, spi_setparity),
855         C99INIT(txStart, spi_starttx),
856         C99INIT(txSending, tx_sending),
857 };
858
859 static struct AvrSerial UARTDescs[SER_CNT] =
860 {
861         {
862                 C99INIT(hw, /**/) {
863                         C99INIT(table, &UART0_VT),
864                         C99INIT(txbuffer, uart0_txbuffer),
865                         C99INIT(rxbuffer, uart0_rxbuffer),
866                         C99INIT(txbuffer_size, sizeof(uart0_txbuffer)),
867                         C99INIT(rxbuffer_size, sizeof(uart0_rxbuffer)),
868                 },
869                 C99INIT(sending, false),
870         },
871 #if AVR_HAS_UART1
872         {
873                 C99INIT(hw, /**/) {
874                         C99INIT(table, &UART1_VT),
875                         C99INIT(txbuffer, uart1_txbuffer),
876                         C99INIT(rxbuffer, uart1_rxbuffer),
877                         C99INIT(txbuffer_size, sizeof(uart1_txbuffer)),
878                         C99INIT(rxbuffer_size, sizeof(uart1_rxbuffer)),
879                 },
880                 C99INIT(sending, false),
881         },
882 #endif
883 #if AVR_HAS_UART2
884         {
885                 C99INIT(hw, /**/) {
886                         C99INIT(table, &UART2_VT),
887                         C99INIT(txbuffer, uart2_txbuffer),
888                         C99INIT(rxbuffer, uart2_rxbuffer),
889                         C99INIT(txbuffer_size, sizeof(uart2_txbuffer)),
890                         C99INIT(rxbuffer_size, sizeof(uart2_rxbuffer)),
891                 },
892                 C99INIT(sending, false),
893         },
894 #endif
895 #if AVR_HAS_UART3
896         {
897                 C99INIT(hw, /**/) {
898                         C99INIT(table, &UART3_VT),
899                         C99INIT(txbuffer, uart3_txbuffer),
900                         C99INIT(rxbuffer, uart3_rxbuffer),
901                         C99INIT(txbuffer_size, sizeof(uart3_txbuffer)),
902                         C99INIT(rxbuffer_size, sizeof(uart3_rxbuffer)),
903                 },
904                 C99INIT(sending, false),
905         },
906 #endif
907         {
908                 C99INIT(hw, /**/) {
909                         C99INIT(table, &SPI_VT),
910                         C99INIT(txbuffer, spi_txbuffer),
911                         C99INIT(rxbuffer, spi_rxbuffer),
912                         C99INIT(txbuffer_size, sizeof(spi_txbuffer)),
913                         C99INIT(rxbuffer_size, sizeof(spi_rxbuffer)),
914                 },
915                 C99INIT(sending, false),
916         }
917 };
918
919 struct SerialHardware *ser_hw_getdesc(int unit)
920 {
921         ASSERT(unit < SER_CNT);
922         return &UARTDescs[unit].hw;
923 }
924
925
926 /*
927  * Interrupt handlers
928  */
929
930 #if CONFIG_SER_HWHANDSHAKE
931
932 /// This interrupt is triggered when the CTS line goes high
933 DECLARE_ISR(SIG_CTS)
934 {
935         // Re-enable UDR empty interrupt and TX, then disable CTS interrupt
936         UCSR0B = BV(BIT_RXCIE0) | BV(BIT_UDRIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0);
937         EIMSK &= ~EIMSKF_CTS;
938 }
939
940 #endif // CONFIG_SER_HWHANDSHAKE
941
942
943 /**
944  * Serial 0 TX interrupt handler
945  */
946 DECLARE_ISR(USART0_UDRE_vect)
947 {
948         SER_STROBE_ON;
949
950         struct FIFOBuffer * const txfifo = &ser_handles[SER_UART0]->txfifo;
951
952         if (fifo_isempty(txfifo))
953         {
954                 SER_UART0_BUS_TXEND;
955 #ifndef SER_UART0_BUS_TXOFF
956                 UARTDescs[SER_UART0].sending = false;
957 #endif
958         }
959 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA103
960         else if (!IS_CTS_ON)
961         {
962                 // Disable rx interrupt and tx, enable CTS interrupt
963                 // UNTESTED
964                 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0);
965                 EIFR |= EIMSKF_CTS;
966                 EIMSK |= EIMSKF_CTS;
967         }
968 #endif
969         else
970         {
971                 char c = fifo_pop(txfifo);
972                 SER_UART0_BUS_TXCHAR(c);
973         }
974
975         SER_STROBE_OFF;
976 }
977
978 #ifdef SER_UART0_BUS_TXOFF
979 /**
980  * Serial port 0 TX complete interrupt handler.
981  *
982  * This IRQ is usually disabled.  The UDR-empty interrupt
983  * enables it when there's no more data to transmit.
984  * We need to wait until the last character has been
985  * transmitted before switching the 485 transceiver to
986  * receive mode.
987  *
988  * The txfifo might have been refilled by putchar() while
989  * we were waiting for the transmission complete interrupt.
990  * In this case, we must restart the UDR empty interrupt,
991  * otherwise we'd stop the serial port with some data
992  * still pending in the buffer.
993  */
994 DECLARE_ISR(USART0_TX_vect)
995 {
996         SER_STROBE_ON;
997
998         struct FIFOBuffer * const txfifo = &ser_handles[SER_UART0]->txfifo;
999         if (fifo_isempty(txfifo))
1000         {
1001                 SER_UART0_BUS_TXOFF;
1002                 UARTDescs[SER_UART0].sending = false;
1003         }
1004         else
1005                 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_UDRIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0);
1006
1007         SER_STROBE_OFF;
1008 }
1009 #endif /* SER_UART0_BUS_TXOFF */
1010
1011
1012 #if AVR_HAS_UART1
1013
1014 /**
1015  * Serial 1 TX interrupt handler
1016  */
1017 DECLARE_ISR(USART1_UDRE_vect)
1018 {
1019         SER_STROBE_ON;
1020
1021         struct FIFOBuffer * const txfifo = &ser_handles[SER_UART1]->txfifo;
1022
1023         if (fifo_isempty(txfifo))
1024         {
1025                 SER_UART1_BUS_TXEND;
1026 #ifndef SER_UART1_BUS_TXOFF
1027                 UARTDescs[SER_UART1].sending = false;
1028 #endif
1029         }
1030 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA103
1031         else if (!IS_CTS_ON)
1032         {
1033                 // Disable rx interrupt and tx, enable CTS interrupt
1034                 // UNTESTED
1035                 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1);
1036                 EIFR |= EIMSKF_CTS;
1037                 EIMSK |= EIMSKF_CTS;
1038         }
1039 #endif
1040         else
1041         {
1042                 char c = fifo_pop(txfifo);
1043                 SER_UART1_BUS_TXCHAR(c);
1044         }
1045
1046         SER_STROBE_OFF;
1047 }
1048
1049 #ifdef SER_UART1_BUS_TXOFF
1050 /**
1051  * Serial port 1 TX complete interrupt handler.
1052  *
1053  * \sa port 0 TX complete handler.
1054  */
1055 DECLARE_ISR(USART1_TX_vect)
1056 {
1057         SER_STROBE_ON;
1058
1059         struct FIFOBuffer * const txfifo = &ser_handles[SER_UART1]->txfifo;
1060         if (fifo_isempty(txfifo))
1061         {
1062                 SER_UART1_BUS_TXOFF;
1063                 UARTDescs[SER_UART1].sending = false;
1064         }
1065         else
1066                 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_UDRIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1);
1067
1068         SER_STROBE_OFF;
1069 }
1070 #endif /* SER_UART1_BUS_TXOFF */
1071
1072 #endif // AVR_HAS_UART1
1073
1074 #if AVR_HAS_UART2
1075
1076 /**
1077  * Serial 2 TX interrupt handler
1078  */
1079 DECLARE_ISR(USART2_UDRE_vect)
1080 {
1081         SER_STROBE_ON;
1082
1083         struct FIFOBuffer * const txfifo = &ser_handles[SER_UART2]->txfifo;
1084
1085         if (fifo_isempty(txfifo))
1086         {
1087                 SER_UART2_BUS_TXEND;
1088 #ifndef SER_UART2_BUS_TXOFF
1089                 UARTDescs[SER_UART2].sending = false;
1090 #endif
1091         }
1092         else
1093         {
1094                 char c = fifo_pop(txfifo);
1095                 SER_UART2_BUS_TXCHAR(c);
1096         }
1097
1098         SER_STROBE_OFF;
1099 }
1100
1101 #ifdef SER_UART2_BUS_TXOFF
1102 /**
1103  * Serial port 2 TX complete interrupt handler.
1104  *
1105  * \sa port 0 TX complete handler.
1106  */
1107 DECLARE_ISR(USART2_TX_vect)
1108 {
1109         SER_STROBE_ON;
1110
1111         struct FIFOBuffer * const txfifo = &ser_handles[SER_UART2]->txfifo;
1112         if (fifo_isempty(txfifo))
1113         {
1114                 SER_UART2_BUS_TXOFF;
1115                 UARTDescs[SER_UART2].sending = false;
1116         }
1117         else
1118                 UCSR2B = BV(BIT_RXCIE2) | BV(BIT_UDRIE2) | BV(BIT_RXEN2) | BV(BIT_TXEN2);
1119
1120         SER_STROBE_OFF;
1121 }
1122 #endif /* SER_UART2_BUS_TXOFF */
1123
1124 #endif // AVR_HAS_UART2
1125
1126 #if AVR_HAS_UART3
1127
1128 /**
1129  * Serial 3 TX interrupt handler
1130  */
1131 DECLARE_ISR(USART3_UDRE_vect)
1132 {
1133         SER_STROBE_ON;
1134
1135         struct FIFOBuffer * const txfifo = &ser_handles[SER_UART3]->txfifo;
1136
1137         if (fifo_isempty(txfifo))
1138         {
1139                 SER_UART3_BUS_TXEND;
1140 #ifndef SER_UART3_BUS_TXOFF
1141                 UARTDescs[SER_UART3].sending = false;
1142 #endif
1143         }
1144         else
1145         {
1146                 char c = fifo_pop(txfifo);
1147                 SER_UART3_BUS_TXCHAR(c);
1148         }
1149
1150         SER_STROBE_OFF;
1151 }
1152
1153 #ifdef SER_UART3_BUS_TXOFF
1154 /**
1155  * Serial port 3 TX complete interrupt handler.
1156  *
1157  * \sa port 0 TX complete handler.
1158  */
1159 DECLARE_ISR(USART3_TX_vect)
1160 {
1161         SER_STROBE_ON;
1162
1163         struct FIFOBuffer * const txfifo = &ser_handles[SER_UART3]->txfifo;
1164         if (fifo_isempty(txfifo))
1165         {
1166                 SER_UART3_BUS_TXOFF;
1167                 UARTDescs[SER_UART3].sending = false;
1168         }
1169         else
1170                 UCSR3B = BV(BIT_RXCIE3) | BV(BIT_UDRIE3) | BV(BIT_RXEN3) | BV(BIT_TXEN3);
1171
1172         SER_STROBE_OFF;
1173 }
1174 #endif /* SER_UART3_BUS_TXOFF */
1175
1176 #endif // AVR_HAS_UART3
1177
1178
1179 /**
1180  * Serial 0 RX complete interrupt handler.
1181  *
1182  * This handler is interruptible.
1183  * Interrupt are reenabled as soon as recv complete interrupt is
1184  * disabled. Using INTERRUPT() is troublesome when the serial
1185  * is heavily loaded, because an interrupt could be retriggered
1186  * when executing the handler prologue before RXCIE is disabled.
1187  *
1188  * \note The code that re-enables interrupts is commented out
1189  *       because in some nasty cases the interrupt is retriggered.
1190  *       This is probably due to the RXC flag being set before
1191  *       RXCIE is cleared.  Unfortunately the RXC flag is read-only
1192  *       and can't be cleared by code.
1193  */
1194 DECLARE_ISR(USART0_RX_vect)
1195 {
1196         SER_STROBE_ON;
1197
1198         /* Disable Recv complete IRQ */
1199         //UCSR0B &= ~BV(RXCIE);
1200         //IRQ_ENABLE;
1201
1202         /* Should be read before UDR */
1203         ser_handles[SER_UART0]->status |= UCSR0A & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
1204
1205         /* To clear the RXC flag we must _always_ read the UDR even when we're
1206          * not going to accept the incoming data, otherwise a new interrupt
1207          * will occur once the handler terminates.
1208          */
1209         char c = UDR0;
1210         struct FIFOBuffer * const rxfifo = &ser_handles[SER_UART0]->rxfifo;
1211
1212         if (fifo_isfull(rxfifo))
1213                 ser_handles[SER_UART0]->status |= SERRF_RXFIFOOVERRUN;
1214         else
1215         {
1216                 fifo_push(rxfifo, c);
1217 #if CONFIG_SER_HWHANDSHAKE
1218                 if (fifo_isfull(rxfifo))
1219                         RTS_OFF;
1220 #endif
1221         }
1222
1223         /* Reenable receive complete int */
1224         //IRQ_DISABLE;
1225         //UCSR0B |= BV(RXCIE);
1226
1227         SER_STROBE_OFF;
1228 }
1229
1230
1231 #if AVR_HAS_UART1
1232
1233 /**
1234  * Serial 1 RX complete interrupt handler.
1235  *
1236  * This handler is interruptible.
1237  * Interrupt are reenabled as soon as recv complete interrupt is
1238  * disabled. Using INTERRUPT() is troublesome when the serial
1239  * is heavily loaded, because an interrupt could be retriggered
1240  * when executing the handler prologue before RXCIE is disabled.
1241  *
1242  * \see DECLARE_ISR(USART1_RX_vect)
1243  */
1244 DECLARE_ISR(USART1_RX_vect)
1245 {
1246         SER_STROBE_ON;
1247
1248         /* Disable Recv complete IRQ */
1249         //UCSR1B &= ~BV(RXCIE);
1250         //IRQ_ENABLE;
1251
1252         /* Should be read before UDR */
1253         ser_handles[SER_UART1]->status |= UCSR1A & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
1254
1255         /* To avoid an IRQ storm, we must _always_ read the UDR even when we're
1256          * not going to accept the incoming data
1257          */
1258         char c = UDR1;
1259         struct FIFOBuffer * const rxfifo = &ser_handles[SER_UART1]->rxfifo;
1260         //ASSERT_VALID_FIFO(rxfifo);
1261
1262         if (UNLIKELY(fifo_isfull(rxfifo)))
1263                 ser_handles[SER_UART1]->status |= SERRF_RXFIFOOVERRUN;
1264         else
1265         {
1266                 fifo_push(rxfifo, c);
1267 #if CONFIG_SER_HWHANDSHAKE
1268                 if (fifo_isfull(rxfifo))
1269                         RTS_OFF;
1270 #endif
1271         }
1272         /* Re-enable receive complete int */
1273         //IRQ_DISABLE;
1274         //UCSR1B |= BV(RXCIE);
1275
1276         SER_STROBE_OFF;
1277 }
1278
1279 #endif // AVR_HAS_UART1
1280
1281 #if AVR_HAS_UART2
1282
1283 /**
1284  * Serial 2 RX complete interrupt handler.
1285  *
1286  * This handler is interruptible.
1287  * Interrupt are reenabled as soon as recv complete interrupt is
1288  * disabled. Using INTERRUPT() is troublesome when the serial
1289  * is heavily loaded, because an interrupt could be retriggered
1290  * when executing the handler prologue before RXCIE is disabled.
1291  *
1292  * \see DECLARE_ISR(USART2_RX_vect)
1293  */
1294 DECLARE_ISR(USART2_RX_vect)
1295 {
1296         SER_STROBE_ON;
1297
1298         /* Disable Recv complete IRQ */
1299         //UCSR1B &= ~BV(RXCIE);
1300         //IRQ_ENABLE;
1301
1302         /* Should be read before UDR */
1303         ser_handles[SER_UART2]->status |= UCSR2A & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
1304
1305         /* To avoid an IRQ storm, we must _always_ read the UDR even when we're
1306          * not going to accept the incoming data
1307          */
1308         char c = UDR2;
1309         struct FIFOBuffer * const rxfifo = &ser_handles[SER_UART2]->rxfifo;
1310         //ASSERT_VALID_FIFO(rxfifo);
1311
1312         if (UNLIKELY(fifo_isfull(rxfifo)))
1313                 ser_handles[SER_UART2]->status |= SERRF_RXFIFOOVERRUN;
1314         else
1315         {
1316                 fifo_push(rxfifo, c);
1317 #if CONFIG_SER_HWHANDSHAKE
1318                 if (fifo_isfull(rxfifo))
1319                         RTS_OFF;
1320 #endif
1321         }
1322         /* Re-enable receive complete int */
1323         //IRQ_DISABLE;
1324         //UCSR1B |= BV(RXCIE);
1325
1326         SER_STROBE_OFF;
1327 }
1328
1329 #endif // AVR_HAS_UART2
1330
1331 #if AVR_HAS_UART3
1332
1333 /**
1334  * Serial 3 RX complete interrupt handler.
1335  *
1336  * This handler is interruptible.
1337  * Interrupt are reenabled as soon as recv complete interrupt is
1338  * disabled. Using INTERRUPT() is troublesome when the serial
1339  * is heavily loaded, because an interrupt could be retriggered
1340  * when executing the handler prologue before RXCIE is disabled.
1341  *
1342  * \see DECLARE_ISR(USART3_RX_vect)
1343  */
1344 DECLARE_ISR(USART3_RX_vect)
1345 {
1346         SER_STROBE_ON;
1347
1348         /* Disable Recv complete IRQ */
1349         //UCSR1B &= ~BV(RXCIE);
1350         //IRQ_ENABLE;
1351
1352         /* Should be read before UDR */
1353         ser_handles[SER_UART3]->status |= UCSR3A & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
1354
1355         /* To avoid an IRQ storm, we must _always_ read the UDR even when we're
1356          * not going to accept the incoming data
1357          */
1358         char c = UDR3;
1359         struct FIFOBuffer * const rxfifo = &ser_handles[SER_UART3]->rxfifo;
1360         //ASSERT_VALID_FIFO(rxfifo);
1361
1362         if (UNLIKELY(fifo_isfull(rxfifo)))
1363                 ser_handles[SER_UART3]->status |= SERRF_RXFIFOOVERRUN;
1364         else
1365         {
1366                 fifo_push(rxfifo, c);
1367 #if CONFIG_SER_HWHANDSHAKE
1368                 if (fifo_isfull(rxfifo))
1369                         RTS_OFF;
1370 #endif
1371         }
1372         /* Re-enable receive complete int */
1373         //IRQ_DISABLE;
1374         //UCSR1B |= BV(RXCIE);
1375
1376         SER_STROBE_OFF;
1377 }
1378
1379 #endif // AVR_HAS_UART3
1380
1381
1382 /**
1383  * SPI interrupt handler
1384  */
1385 DECLARE_ISR(SPI_STC_vect)
1386 {
1387         SER_STROBE_ON;
1388
1389         /* Read incoming byte. */
1390         if (!fifo_isfull(&ser_handles[SER_SPI]->rxfifo))
1391                 fifo_push(&ser_handles[SER_SPI]->rxfifo, SPDR);
1392         /*
1393          * FIXME
1394         else
1395                 ser_handles[SER_SPI]->status |= SERRF_RXFIFOOVERRUN;
1396         */
1397
1398         /* Send */
1399         if (!fifo_isempty(&ser_handles[SER_SPI]->txfifo))
1400                 SPDR = fifo_pop(&ser_handles[SER_SPI]->txfifo);
1401         else
1402                 UARTDescs[SER_SPI].sending = false;
1403
1404         SER_STROBE_OFF;
1405 }