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