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