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