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