DISABLE_INTS/ENABLE_INTS: Convert to IRQ_DISABLE/IRQ_ENABLE.
[bertos.git] / drv / ser_avr.c
1 /*!
2  * \file
3  * <!--
4  * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 2000 Bernardo Innocenti <bernie@codewiz.org>
6  * This file is part of DevLib - See devlib/README for information.
7  * -->
8  *
9  * \brief AVR UART and SPI I/O driver
10  *
11  * Rationale for project_ks hardware.
12  *
13  * The serial 0 on the board_kf board is used to communicate with the
14  * smart card, which has the TX and RX lines connected together. To
15  * allow the smart card to drive the RX line of the CPU the CPU TX has
16  * to be in a high impedance state.
17  * Whenever a transmission is done and there is nothing more to send
18  * the transmitter is turn off. The output pin is held in input with
19  * pull-up enabled, to avoid capturing noise from the nearby RX line.
20  *
21  * The line on the KBus port must keep sending data, even when
22  * there is nothing to transmit, because a burst data transfer
23  * generates noise on the audio channels.
24  * This is accomplished using the multiprocessor mode of the
25  * ATmega64/128 serial.
26  *
27  * The receiver keeps the MPCM bit always on. When useful data
28  * is trasmitted the address bit is set. The receiver hardware
29  * consider the frame as address info and receive it.
30  * When useless fill bytes are sent the address bit is cleared
31  * and the receiver will ignore them, avoiding useless triggering
32  * of RXC interrupt.
33  *
34  * \version $Id$
35  * \author Bernardo Innocenti <bernie@develer.com>
36  * \author Stefano Fedrigo <aleph@develer.com>
37  */
38
39 /*#*
40  *#* $Log$
41  *#* Revision 1.19  2004/12/13 11:51:08  bernie
42  *#* DISABLE_INTS/ENABLE_INTS: Convert to IRQ_DISABLE/IRQ_ENABLE.
43  *#*
44  *#* Revision 1.18  2004/12/08 08:03:48  bernie
45  *#* Doxygen fixes.
46  *#*
47  *#* Revision 1.17  2004/10/19 07:52:35  bernie
48  *#* Reset parity bits before overwriting them (Fixed by batt in project_ks).
49  *#*
50  *#* Revision 1.16  2004/10/03 18:45:48  bernie
51  *#* Convert to new-style config macros; Allow compiling with a C++ compiler (mostly).
52  *#*
53  *#* Revision 1.15  2004/09/14 21:05:36  bernie
54  *#* Use debug.h instead of kdebug.h; Use new AVR pin names; Spelling fixes.
55  *#*
56  *#* Revision 1.14  2004/09/06 21:50:00  bernie
57  *#* Spelling fixes.
58  *#*
59  *#* Revision 1.13  2004/09/06 21:40:50  bernie
60  *#* Move buffer handling in chip-specific driver.
61  *#*
62  *#* Revision 1.12  2004/08/29 22:06:10  bernie
63  *#* Fix a bug in the (unused) RTS/CTS code; Clarify documentation.
64  *#*
65  *#* Revision 1.10  2004/08/10 06:30:41  bernie
66  *#* Major redesign of serial bus policy handling.
67  *#*
68  *#* Revision 1.9  2004/08/02 20:20:29  aleph
69  *#* Merge from project_ks
70  *#*
71  *#* Revision 1.8  2004/07/29 22:57:09  bernie
72  *#* Several tweaks to reduce code size on ATmega8.
73  *#*
74  *#* Revision 1.7  2004/07/18 21:54:23  bernie
75  *#* Add ATmega8 support.
76  *#*
77  *#* Revision 1.5  2004/06/27 15:25:40  aleph
78  *#* Add missing callbacks for SPI;
79  *#* Change UNUSED() macro to new version with two args;
80  *#* Use TX line filling only on the correct KBUS serial port;
81  *#* Fix nasty IRQ disabling bug in recv complete hander for port 1.
82  *#*
83  *#* Revision 1.4  2004/06/03 11:27:09  bernie
84  *#* Add dual-license information.
85  *#*
86  *#* Revision 1.3  2004/06/02 21:35:24  aleph
87  *#* Serial enhancements: interruptible receive handler and 8 bit serial status for AVR; remove volatile attribute to FIFOBuffer, useless for new fifobuf routens
88  *#*
89  *#* Revision 1.2  2004/05/23 18:21:53  bernie
90  *#* Trim CVS logs and cleanup header info.
91  *#*
92  *#*/
93
94 #include "ser.h"
95 #include "ser_p.h"
96 #include "config.h"
97 #include "hw.h"  /* Required for bus macros overrides */
98
99 #include <debug.h>
100 #include <drv/timer.h>
101 #include <mware/fifobuf.h>
102
103 #include <avr/signal.h>
104 #include <avr/io.h>
105
106
107 /*!
108  * \name Hardware handshake (RTS/CTS).
109  * \{
110  */
111 #ifndef RTS_ON
112 #define RTS_ON      do {} while (0)
113 #endif
114 #ifndef RTS_OFF
115 #define RTS_OFF     do {} while (0)
116 #endif
117 #ifndef IS_CTS_ON
118 #define IS_CTS_ON   true
119 #endif
120 #ifndef EIMSKB_CTS
121 #define EIMSKB_CTS  0 /*!< Dummy value, must be overridden */
122 #endif
123 /*\}*/
124
125
126 /*!
127  * \name Overridable serial bus hooks
128  *
129  * These can be redefined in hw.h to implement
130  * special bus policies such as half-duplex, 485, etc.
131  *
132  *
133  * \code
134  *  TXBEGIN      TXCHAR      TXEND  TXOFF
135  *    |   __________|__________ |     |
136  *    |   |   |   |   |   |   | |     |
137  *    v   v   v   v   v   v   v v     v
138  * ______  __  __  __  __  __  __  ________________
139  *       \/  \/  \/  \/  \/  \/  \/
140  * ______/\__/\__/\__/\__/\__/\__/
141  *
142  * \endcode
143  *
144  * \{
145  */
146 #ifndef SER_UART0_BUS_TXINIT
147         /*!
148          * Default TXINIT macro - invoked in uart0_init()
149          *
150          * - Enable both the receiver and the transmitter
151          * - Enable only the RX complete interrupt
152          */
153         #define SER_UART0_BUS_TXINIT do { \
154                 UCSR0B = BV(RXCIE) | BV(RXEN) | BV(TXEN); \
155         } while (0)
156 #endif
157
158 #ifndef SER_UART0_BUS_TXBEGIN
159         /*!
160          * Invoked before starting a transmission
161          *
162          * - Enable both the receiver and the transmitter
163          * - Enable both the RX complete and UDR empty interrupts
164          */
165         #define SER_UART0_BUS_TXBEGIN do { \
166                 UCSR0B = BV(RXCIE) | BV(UDRIE) | BV(RXEN) | BV(TXEN); \
167         } while (0)
168 #endif
169
170 #ifndef SER_UART0_BUS_TXCHAR
171         /*!
172          * Invoked to send one character.
173          */
174         #define SER_UART0_BUS_TXCHAR(c) do { \
175                 UDR0 = (c); \
176         } while (0)
177 #endif
178
179 #ifndef SER_UART0_BUS_TXEND
180         /*!
181          * Invoked as soon as the txfifo becomes empty
182          *
183          * - Keep both the receiver and the transmitter enabled
184          * - Keep the RX complete interrupt enabled
185          * - Disable the UDR empty interrupt
186          */
187         #define SER_UART0_BUS_TXEND do { \
188                 UCSR0B = BV(RXCIE) | BV(RXEN) | BV(TXEN); \
189         } while (0)
190 #endif
191
192 #ifndef SER_UART0_BUS_TXOFF
193         /*!
194          * \def SER_UART0_BUS_TXOFF
195          *
196          * Invoked after the last character has been transmitted
197          *
198          * The default is no action.
199          */
200         #ifdef __doxygen__
201         #define SER_UART0_BUS_TXOFF
202         #endif
203 #endif
204
205 #ifndef SER_UART1_BUS_TXINIT
206         /*! \sa SER_UART0_BUS_TXINIT */
207         #define SER_UART1_BUS_TXINIT do { \
208                 UCSR1B = BV(RXCIE) | BV(RXEN) | BV(TXEN); \
209         } while (0)
210 #endif
211 #ifndef SER_UART1_BUS_TXBEGIN
212         /*! \sa SER_UART0_BUS_TXBEGIN */
213         #define SER_UART1_BUS_TXBEGIN do { \
214                 UCSR1B = BV(RXCIE) | BV(UDRIE) | BV(RXEN) | BV(TXEN); \
215         } while (0)
216 #endif
217 #ifndef SER_UART1_BUS_TXCHAR
218         /*! \sa SER_UART0_BUS_TXCHAR */
219         #define SER_UART1_BUS_TXCHAR(c) do { \
220                 UDR1 = (c); \
221         } while (0)
222 #endif
223 #ifndef SER_UART1_BUS_TXEND
224         /*! \sa SER_UART0_BUS_TXEND */
225         #define SER_UART1_BUS_TXEND do { \
226                 UCSR1B = BV(RXCIE) | BV(RXEN) | BV(TXEN); \
227         } while (0)
228 #endif
229 #ifndef SER_UART1_BUS_TXOFF
230         /*!
231          * \def SER_UART1_BUS_TXOFF
232          *
233          * \see SER_UART0_BUS_TXOFF
234          */
235         #ifdef __doxygen__
236         #define SER_UART1_BUS_TXOFF
237         #endif
238 #endif
239 /*\}*/
240
241
242 /* SPI port and pin configuration */
243 #define SPI_PORT      PORTB
244 #define SPI_DDR       DDRB
245 #define SPI_SCK_BIT   PB1
246 #define SPI_MOSI_BIT  PB2
247 #define SPI_MISO_BIT  PB3
248
249 /* USART register definitions */
250 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128
251         #define AVR_HAS_UART1 1
252 #elif CPU_AVR_ATMEGA8
253         #define AVR_HAS_UART1 0
254         #define UCSR0A UCSRA
255         #define UCSR0B UCSRB
256         #define UCSR0C UCSRC
257         #define UDR0   UDR
258         #define UBRR0L UBRRL
259         #define UBRR0H UBRRH
260         #define SIG_UART0_DATA SIG_UART_DATA
261         #define SIG_UART0_RECV SIG_UART_RECV
262 #elif CPU_AVR_ATMEGA103
263         #define AVR_HAS_UART1 0
264         #define UCSR0B UCR
265         #define UDR0   UDR
266         #define UCSR0A USR
267         #define UBRR0L UBRR
268         #define SIG_UART0_DATA SIG_UART_DATA
269         #define SIG_UART0_RECV SIG_UART_RECV
270 #else
271         #error Unknown architecture
272 #endif
273
274
275 /*!
276  * \def CONFIG_SER_STROBE
277  *
278  * This is a debug facility that can be used to
279  * monitor SER interrupt activity on an external pin.
280  *
281  * To use strobes, redefine the macros SER_STROBE_ON,
282  * SER_STROBE_OFF and SER_STROBE_INIT and set
283  * CONFIG_SER_STROBE to 1.
284  */
285 #if !defined(CONFIG_SER_STROBE) || !CONFIG_SER_STROBE
286         #define SER_STROBE_ON    do {/*nop*/} while(0)
287         #define SER_STROBE_OFF   do {/*nop*/} while(0)
288         #define SER_STROBE_INIT  do {/*nop*/} while(0)
289 #endif
290
291
292 /* From the high-level serial driver */
293 extern struct Serial ser_handles[SER_CNT];
294
295 /* TX and RX buffers */
296 static unsigned char uart0_txbuffer[CONFIG_UART0_TXBUFSIZE];
297 static unsigned char uart0_rxbuffer[CONFIG_UART0_RXBUFSIZE];
298 #if AVR_HAS_UART1
299         static unsigned char uart1_txbuffer[CONFIG_UART1_TXBUFSIZE];
300         static unsigned char uart1_rxbuffer[CONFIG_UART1_RXBUFSIZE];
301 #endif
302 static unsigned char spi_txbuffer[CONFIG_SPI_TXBUFSIZE];
303 static unsigned char spi_rxbuffer[CONFIG_SPI_RXBUFSIZE];
304
305
306 /*!
307  * Internal hardware state structure
308  *
309  * The \a sending variable is true while the transmission
310  * interrupt is retriggering itself.
311  *
312  * For the USARTs the \a sending flag is useful for taking specific
313  * actions before sending a burst of data, at the start of a trasmission
314  * but not before every char sent.
315  *
316  * For the SPI, this flag is necessary because the SPI sends and receives
317  * bytes at the same time and the SPI IRQ is unique for send/receive.
318  * The only way to start transmission is to write data in SPDR (this
319  * is done by spi_starttx()). We do this *only* if a transfer is
320  * not already started.
321  */
322 struct AvrSerial
323 {
324         struct SerialHardware hw;
325         volatile bool sending;
326 };
327
328
329 /*
330  * These are to trick GCC into *not* using absolute addressing mode
331  * when accessing ser_handles, which is very expensive.
332  *
333  * Accessing through these pointers generates much shorter
334  * (and hopefully faster) code.
335  */
336 struct Serial *ser_uart0 = &ser_handles[SER_UART0];
337 #if AVR_HAS_UART1
338 struct Serial *ser_uart1 = &ser_handles[SER_UART1];
339 #endif
340 struct Serial *ser_spi = &ser_handles[SER_SPI];
341
342
343
344 /*
345  * Callbacks
346  */
347 static void uart0_init(UNUSED(struct SerialHardware *, _hw), UNUSED(struct Serial *, ser))
348 {
349         SER_UART0_BUS_TXINIT;
350         RTS_ON;
351 }
352
353 static void uart0_cleanup(UNUSED(struct SerialHardware *, _hw))
354 {
355         UCSR0B = 0;
356 }
357
358 static void uart0_enabletxirq(struct SerialHardware *_hw)
359 {
360         struct AvrSerial *hw = (struct AvrSerial *)_hw;
361
362         /*
363          * WARNING: racy code here!  The tx interrupt sets hw->sending to false
364          * when it runs with an empty fifo.  The order of statements in the
365          * if-block matters.
366          */
367         if (!hw->sending)
368         {
369                 hw->sending = true;
370                 SER_UART0_BUS_TXBEGIN;
371         }
372 }
373
374 static void uart0_setbaudrate(UNUSED(struct SerialHardware *, _hw), unsigned long rate)
375 {
376         /* Compute baud-rate period */
377         uint16_t period = (((CLOCK_FREQ / 16UL) + (rate / 2)) / rate) - 1;
378
379 #ifndef __AVR_ATmega103__
380         UBRR0H = (period) >> 8;
381 #endif
382         UBRR0L = (period);
383
384         //DB(kprintf("uart0_setbaudrate(rate=%lu): period=%d\n", rate, period);)
385 }
386
387 static void uart0_setparity(UNUSED(struct SerialHardware *, _hw), int parity)
388 {
389 #if !CPU_AVR_ATMEGA103
390         UCSR0C = (UCSR0C & ~(BV(UPM1) | BV(UPM0))) | ((parity) << UPM0);
391 #endif
392 }
393
394 #if AVR_HAS_UART1
395
396 static void uart1_init(UNUSED(struct SerialHardware *, _hw), UNUSED(struct Serial *, ser))
397 {
398         SER_UART1_BUS_TXINIT;
399         RTS_ON;
400         SER_STROBE_INIT;
401 }
402
403 static void uart1_cleanup(UNUSED(struct SerialHardware *, _hw))
404 {
405         UCSR1B = 0;
406 }
407
408 static void uart1_enabletxirq(struct SerialHardware *_hw)
409 {
410         struct AvrSerial *hw = (struct AvrSerial *)_hw;
411
412         /*
413          * WARNING: racy code here!  The tx interrupt
414          * sets hw->sending to false when it runs with
415          * an empty fifo.  The order of the statements
416          * in the if-block matters.
417          */
418         if (!hw->sending)
419         {
420                 hw->sending = true;
421                 SER_UART1_BUS_TXBEGIN;
422         }
423 }
424
425 static void uart1_setbaudrate(UNUSED(struct SerialHardware *, _hw), unsigned long rate)
426 {
427         /* Compute baud-rate period */
428         uint16_t period = (((CLOCK_FREQ / 16UL) + (rate / 2)) / rate) - 1;
429
430         UBRR1H = (period) >> 8;
431         UBRR1L = (period);
432
433         //DB(kprintf("uart1_setbaudrate(rate=%ld): period=%d\n", rate, period);)
434 }
435
436 static void uart1_setparity(UNUSED(struct SerialHardware *, _hw), int parity)
437 {
438         UCSR1C = (UCSR1C & ~(BV(UPM1) | BV(UPM0))) | ((parity) << UPM0);
439 }
440
441 #endif // AVR_HAS_UART1
442
443 static void spi_init(UNUSED(struct SerialHardware *, _hw), UNUSED(struct Serial *, ser))
444 {
445         /*
446          * Set MOSI and SCK ports out, MISO in.
447          *
448          * The ATmega64/128 datasheet explicitly states that the input/output
449          * state of the SPI pins is not significant, as when the SPI is
450          * active the I/O port are overrided.
451          * This is *blatantly FALSE*.
452          *
453          * Moreover, the MISO pin on the board_kc *must* be in high impedance
454          * state even when the SPI is off, because the line is wired together
455          * with the KBus serial RX, and the transmitter of the slave boards
456          * would be unable to drive the line.
457          */
458         SPI_DDR |= BV(SPI_MOSI_BIT) | BV(SPI_SCK_BIT);
459         SPI_DDR &= ~BV(SPI_MISO_BIT);
460         /* Enable SPI, IRQ on, Master, CPU_CLOCK/16 */
461         SPCR = BV(SPE) | BV(SPIE) | BV(MSTR) | BV(SPR0);
462 }
463
464 static void spi_cleanup(UNUSED(struct SerialHardware *, _hw))
465 {
466         SPCR = 0;
467         /* Set all pins as inputs */
468         SPI_DDR &= ~(BV(SPI_MISO_BIT) | BV(SPI_MOSI_BIT) | BV(SPI_SCK_BIT));
469 }
470
471 static void spi_starttx(struct SerialHardware *_hw)
472 {
473         struct AvrSerial *hw = (struct AvrSerial *)_hw;
474
475         cpuflags_t flags;
476         DISABLE_IRQSAVE(flags);
477
478         /* Send data only if the SPI is not already transmitting */
479         if (!hw->sending && !fifo_isempty(&ser_spi->txfifo))
480         {
481                 hw->sending = true;
482                 SPDR = fifo_pop(&ser_spi->txfifo);
483         }
484
485         ENABLE_IRQRESTORE(flags);
486 }
487
488 static void spi_setbaudrate(UNUSED(struct SerialHardware *, _hw), UNUSED(unsigned long, rate))
489 {
490         // nop
491 }
492
493 static void spi_setparity(UNUSED(struct SerialHardware *, _hw), UNUSED(int, parity))
494 {
495         // nop
496 }
497
498
499 // FIXME: move into compiler.h?  Ditch?
500 #if COMPILER_C99
501         #define C99INIT(name,val) .name = val
502 #elif defined(__GNUC__)
503         #define C99INIT(name,val) name: val
504 #else
505         #warning No designated initializers, double check your code
506         #define C99INIT(name,val) (val)
507 #endif
508
509 /*
510  * High-level interface data structures
511  */
512 static const struct SerialHardwareVT UART0_VT =
513 {
514         C99INIT(init, uart0_init),
515         C99INIT(cleanup, uart0_cleanup),
516         C99INIT(setbaudrate, uart0_setbaudrate),
517         C99INIT(setparity, uart0_setparity),
518         C99INIT(enabletxirq, uart0_enabletxirq),
519 };
520
521 #if AVR_HAS_UART1
522 static const struct SerialHardwareVT UART1_VT =
523 {
524         C99INIT(init, uart1_init),
525         C99INIT(cleanup, uart1_cleanup),
526         C99INIT(setbaudrate, uart1_setbaudrate),
527         C99INIT(setparity, uart1_setparity),
528         C99INIT(enabletxirq, uart1_enabletxirq),
529 };
530 #endif // AVR_HAS_UART1
531
532 static const struct SerialHardwareVT SPI_VT =
533 {
534         C99INIT(init, spi_init),
535         C99INIT(cleanup, spi_cleanup),
536         C99INIT(setbaudrate, spi_setbaudrate),
537         C99INIT(setparity, spi_setparity),
538         C99INIT(enabletxirq, spi_starttx),
539 };
540
541 static struct AvrSerial UARTDescs[SER_CNT] =
542 {
543         {
544                 C99INIT(hw, /**/) {
545                         C99INIT(table, &UART0_VT),
546                         C99INIT(txbuffer, uart0_txbuffer),
547                         C99INIT(rxbuffer, uart0_rxbuffer),
548                         C99INIT(txbuffer_size, sizeof(uart0_txbuffer)),
549                         C99INIT(rxbuffer_size, sizeof(uart0_rxbuffer)),
550                 },
551                 C99INIT(sending, false),
552         },
553 #if AVR_HAS_UART1
554         {
555                 C99INIT(hw, /**/) {
556                         C99INIT(table, &UART1_VT),
557                         C99INIT(txbuffer, uart1_txbuffer),
558                         C99INIT(rxbuffer, uart1_rxbuffer),
559                         C99INIT(txbuffer_size, sizeof(uart1_txbuffer)),
560                         C99INIT(rxbuffer_size, sizeof(uart1_rxbuffer)),
561                 },
562                 C99INIT(sending, false),
563         },
564 #endif
565         {
566                 C99INIT(hw, /**/) {
567                         C99INIT(table, &SPI_VT),
568                         C99INIT(txbuffer, spi_txbuffer),
569                         C99INIT(rxbuffer, spi_rxbuffer),
570                         C99INIT(txbuffer_size, sizeof(spi_txbuffer)),
571                         C99INIT(rxbuffer_size, sizeof(spi_rxbuffer)),
572                 },
573                 C99INIT(sending, false),
574         }
575 };
576
577 struct SerialHardware* ser_hw_getdesc(int unit)
578 {
579         ASSERT(unit < SER_CNT);
580         return &UARTDescs[unit].hw;
581 }
582
583
584 /*
585  * Interrupt handlers
586  */
587
588 #if CONFIG_SER_HWHANDSHAKE
589
590 //! This interrupt is triggered when the CTS line goes high
591 SIGNAL(SIG_CTS)
592 {
593         // Re-enable UDR empty interrupt and TX, then disable CTS interrupt
594         UCSR0B = BV(RXCIE) | BV(UDRIE) | BV(RXEN) | BV(TXEN);
595         cbi(EIMSK, EIMSKB_CTS);
596 }
597
598 #endif // CONFIG_SER_HWHANDSHAKE
599
600
601 /*!
602  * Serial 0 TX interrupt handler
603  */
604 SIGNAL(SIG_UART0_DATA)
605 {
606         SER_STROBE_ON;
607
608         struct FIFOBuffer * const txfifo = &ser_uart0->txfifo;
609
610         if (fifo_isempty(txfifo))
611         {
612                 SER_UART0_BUS_TXEND;
613 #ifndef SER_UART0_BUS_TXOFF
614                 UARTDescs[SER_UART0].sending = false;
615 #endif
616         }
617 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA103
618         else if (!IS_CTS_ON)
619         {
620                 // Disable rx interrupt and tx, enable CTS interrupt
621                 // UNTESTED
622                 UCSR0B = BV(RXCIE) | BV(RXEN) | BV(TXEN);
623                 sbi(EIFR, EIMSKB_CTS);
624                 sbi(EIMSK, EIMSKB_CTS);
625         }
626 #endif
627         else
628         {
629                 char c = fifo_pop(txfifo);
630                 SER_UART0_BUS_TXCHAR(c);
631         }
632
633         SER_STROBE_OFF;
634 }
635
636 #ifdef SER_UART0_BUS_TXOFF
637 /*!
638  * Serial port 0 TX complete interrupt handler.
639  *
640  * This IRQ is usually disabled.  The UDR-empty interrupt
641  * enables it when there's no more data to transmit.
642  * We need to wait until the last character has been
643  * transmitted before switching the 485 transceiver to
644  * receive mode.
645  *
646  * The txfifo might have been refilled by putchar() while
647  * we were waiting for the transmission complete interrupt.
648  * In this case, we must restart the UDR empty interrupt,
649  * otherwise we'd stop the serial port with some data
650  * still pending in the buffer.
651  */
652 SIGNAL(SIG_UART0_TRANS)
653 {
654         SER_STROBE_ON;
655
656         struct FIFOBuffer * const txfifo = &ser_uart0->txfifo;
657         if (fifo_isempty(txfifo))
658         {
659                 SER_UART0_BUS_TXOFF;
660                 UARTDescs[SER_UART0].sending = false;
661         }
662         else
663                 UCSR0B = BV(RXCIE) | BV(UDRIE) | BV(RXEN) | BV(TXEN);
664
665         SER_STROBE_OFF;
666 }
667 #endif /* SER_UART0_BUS_TXOFF */
668
669
670 #if AVR_HAS_UART1
671
672 /*!
673  * Serial 1 TX interrupt handler
674  */
675 SIGNAL(SIG_UART1_DATA)
676 {
677         SER_STROBE_ON;
678
679         struct FIFOBuffer * const txfifo = &ser_uart1->txfifo;
680
681         if (fifo_isempty(txfifo))
682         {
683                 SER_UART1_BUS_TXEND;
684 #ifndef SER_UART1_BUS_TXOFF
685                 UARTDescs[SER_UART1].sending = false;
686 #endif
687         }
688 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA103
689         else if (!IS_CTS_ON)
690         {
691                 // Disable rx interrupt and tx, enable CTS interrupt
692                 // UNTESTED
693                 UCSR1B = BV(RXCIE) | BV(RXEN) | BV(TXEN);
694                 sbi(EIFR, EIMSKB_CTS);
695                 sbi(EIMSK, EIMSKB_CTS);
696         }
697 #endif
698         else
699         {
700                 char c = fifo_pop(txfifo);
701                 SER_UART1_BUS_TXCHAR(c);
702         }
703
704         SER_STROBE_OFF;
705 }
706
707 #ifdef SER_UART1_BUS_TXOFF
708 /*!
709  * Serial port 1 TX complete interrupt handler.
710  *
711  * \sa port 0 TX complete handler.
712  */
713 SIGNAL(SIG_UART1_TRANS)
714 {
715         SER_STROBE_ON;
716
717         struct FIFOBuffer * const txfifo = &ser_uart1->txfifo;
718         if (fifo_isempty(txfifo))
719         {
720                 SER_UART1_BUS_TXOFF;
721                 UARTDescs[SER_UART1].sending = false;
722         }
723         else
724                 UCSR1B = BV(RXCIE) | BV(UDRIE) | BV(RXEN) | BV(TXEN);
725
726         SER_STROBE_OFF;
727 }
728 #endif /* SER_UART1_BUS_TXOFF */
729
730 #endif // AVR_HAS_UART1
731
732
733 /*!
734  * Serial 0 RX complete interrupt handler.
735  *
736  * This handler is interruptible.
737  * Interrupt are reenabled as soon as recv complete interrupt is
738  * disabled. Using INTERRUPT() is troublesome when the serial
739  * is heavily loaded, because an interrupt could be retriggered
740  * when executing the handler prologue before RXCIE is disabled.
741  *
742  * \note The code that re-enables interrupts is commented out
743  *       because in some nasty cases the interrupt is retriggered.
744  *       This is probably due to the RXC flag being set before
745  *       RXCIE is cleared.  Unfortunately the RXC flag is read-only
746  *       and can't be cleared by code.
747  */
748 SIGNAL(SIG_UART0_RECV)
749 {
750         SER_STROBE_ON;
751
752         /* Disable Recv complete IRQ */
753         //UCSR0B &= ~BV(RXCIE);
754         //IRQ_ENABLE;
755
756         /* Should be read before UDR */
757         ser_uart0->status |= UCSR0A & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
758
759         /* To clear the RXC flag we must _always_ read the UDR even when we're
760          * not going to accept the incoming data, otherwise a new interrupt
761          * will occur once the handler terminates.
762          */
763         char c = UDR0;
764         struct FIFOBuffer * const rxfifo = &ser_uart0->rxfifo;
765
766         if (fifo_isfull(rxfifo))
767                 ser_uart0->status |= SERRF_RXFIFOOVERRUN;
768         else
769         {
770                 fifo_push(rxfifo, c);
771 #if CONFIG_SER_HWHANDSHAKE
772                 if (fifo_isfull(rxfifo))
773                         RTS_OFF;
774 #endif
775         }
776
777         /* Reenable receive complete int */
778         //IRQ_DISABLE;
779         //UCSR0B |= BV(RXCIE);
780
781         SER_STROBE_OFF;
782 }
783
784
785 #if AVR_HAS_UART1
786
787 /*!
788  * Serial 1 RX complete interrupt handler.
789  *
790  * This handler is interruptible.
791  * Interrupt are reenabled as soon as recv complete interrupt is
792  * disabled. Using INTERRUPT() is troublesome when the serial
793  * is heavily loaded, because an interrupt could be retriggered
794  * when executing the handler prologue before RXCIE is disabled.
795  *
796  * \see SIGNAL(SIG_UART0_RECV)
797  */
798 SIGNAL(SIG_UART1_RECV)
799 {
800         SER_STROBE_ON;
801
802         /* Disable Recv complete IRQ */
803         //UCSR1B &= ~BV(RXCIE);
804         //IRQ_ENABLE;
805
806         /* Should be read before UDR */
807         ser_uart1->status |= UCSR1A & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
808
809         /* To avoid an IRQ storm, we must _always_ read the UDR even when we're
810          * not going to accept the incoming data
811          */
812         char c = UDR1;
813         struct FIFOBuffer * const rxfifo = &ser_uart1->rxfifo;
814         //ASSERT_VALID_FIFO(rxfifo);
815
816         if (UNLIKELY(fifo_isfull(rxfifo)))
817                 ser_uart1->status |= SERRF_RXFIFOOVERRUN;
818         else
819         {
820                 fifo_push(rxfifo, c);
821 #if CONFIG_SER_HWHANDSHAKE
822                 if (fifo_isfull(rxfifo))
823                         RTS_OFF;
824 #endif
825         }
826         /* Re-enable receive complete int */
827         //UCSR1B |= BV(RXCIE);
828
829         SER_STROBE_OFF;
830 }
831
832 #endif // AVR_HAS_UART1
833
834
835 /*!
836  * SPI interrupt handler
837  */
838 SIGNAL(SIG_SPI)
839 {
840         /* Read incoming byte. */
841         if (!fifo_isfull(&ser_spi->rxfifo))
842                 fifo_push(&ser_spi->rxfifo, SPDR);
843         /*
844          * FIXME
845         else
846                 ser_spi->status |= SERRF_RXFIFOOVERRUN;
847         */
848
849         /* Send */
850         if (!fifo_isempty(&ser_spi->txfifo))
851                 SPDR = fifo_pop(&ser_spi->txfifo);
852         else
853                 UARTDescs[SER_SPI].sending = false;
854 }