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