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