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