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