4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
30 * Copyright 2000 Bernardo Innocenti <bernie@codewiz.org>
34 * \brief AVR UART and SPI I/O driver
36 * Rationale for project_ks hardware.
38 * The serial 0 on the board_kf board is used to communicate with the
39 * smart card, which has the TX and RX lines connected together. To
40 * allow the smart card to drive the RX line of the CPU the CPU TX has
41 * to be in a high impedance state.
42 * Whenever a transmission is done and there is nothing more to send
43 * the transmitter is turn off. The output pin is held in input with
44 * pull-up enabled, to avoid capturing noise from the nearby RX line.
46 * The line on the KBus port must keep sending data, even when
47 * there is nothing to transmit, because a burst data transfer
48 * generates noise on the audio channels.
49 * This is accomplished using the multiprocessor mode of the
50 * ATmega64/128 serial.
52 * The receiver keeps the MPCM bit always on. When useful data
53 * is trasmitted the address bit is set. The receiver hardware
54 * consider the frame as address info and receive it.
55 * When useless fill bytes are sent the address bit is cleared
56 * and the receiver will ignore them, avoiding useless triggering
60 * \author Bernardo Innocenti <bernie@develer.com>
61 * \author Stefano Fedrigo <aleph@develer.com>
66 *#* Revision 1.34 2006/11/23 13:19:02 batt
67 *#* Add support for ATmega1281.
69 *#* Revision 1.33 2006/09/13 18:21:24 bernie
70 *#* Add configurable SPI pin mapping.
72 *#* Revision 1.32 2006/07/19 12:56:26 bernie
73 *#* Convert to new Doxygen style.
75 *#* Revision 1.31 2006/05/18 00:37:29 bernie
76 *#* Use hw_ser.h instead of ubiquitous hw.h.
78 *#* Revision 1.30 2006/02/17 22:23:06 bernie
79 *#* Update POSIX serial emulator.
81 *#* Revision 1.29 2005/11/27 23:31:48 bernie
82 *#* Support avr-libc 1.4.
84 *#* Revision 1.28 2005/11/04 16:20:02 bernie
85 *#* Fix reference to README.devlib in header.
87 *#* Revision 1.27 2005/07/03 15:19:31 bernie
90 *#* Revision 1.26 2005/04/11 19:10:27 bernie
91 *#* Include top-level headers from cfg/ subdir.
93 *#* Revision 1.25 2005/01/25 08:37:26 bernie
94 *#* CONFIG_SER_HWHANDSHAKE fixes.
96 *#* Revision 1.24 2005/01/14 00:49:16 aleph
97 *#* Rename callbacks; SerialHardwareVT.txSending: New callback; Add SPI_BUS macros.
99 *#* Revision 1.23 2005/01/11 18:09:07 aleph
100 *#* Add ATmega8 SPI port definitions; Fix transmit complete IRQ bug; add strobe macros to uart1 and spi
102 *#* Revision 1.22 2004/12/31 17:47:45 bernie
103 *#* Rename UNUSED() to UNUSED_ARG().
105 *#* Revision 1.21 2004/12/13 12:07:06 bernie
106 *#* DISABLE_IRQSAVE/ENABLE_IRQRESTORE: Convert to IRQ_SAVE_DISABLE/IRQ_RESTORE.
108 *#* Revision 1.20 2004/12/13 11:51:43 bernie
109 *#* Fix a latent bug with reentrant serial IRQs.
111 *#* Revision 1.19 2004/12/13 11:51:08 bernie
112 *#* DISABLE_INTS/ENABLE_INTS: Convert to IRQ_DISABLE/IRQ_ENABLE.
114 *#* Revision 1.18 2004/12/08 08:03:48 bernie
117 *#* Revision 1.17 2004/10/19 07:52:35 bernie
118 *#* Reset parity bits before overwriting them (Fixed by batt in project_ks).
120 *#* Revision 1.16 2004/10/03 18:45:48 bernie
121 *#* Convert to new-style config macros; Allow compiling with a C++ compiler (mostly).
123 *#* Revision 1.15 2004/09/14 21:05:36 bernie
124 *#* Use debug.h instead of kdebug.h; Use new AVR pin names; Spelling fixes.
126 *#* Revision 1.14 2004/09/06 21:50:00 bernie
129 *#* Revision 1.13 2004/09/06 21:40:50 bernie
130 *#* Move buffer handling in chip-specific driver.
132 *#* Revision 1.12 2004/08/29 22:06:10 bernie
133 *#* Fix a bug in the (unused) RTS/CTS code; Clarify documentation.
135 *#* Revision 1.10 2004/08/10 06:30:41 bernie
136 *#* Major redesign of serial bus policy handling.
138 *#* Revision 1.9 2004/08/02 20:20:29 aleph
139 *#* Merge from project_ks
141 *#* Revision 1.8 2004/07/29 22:57:09 bernie
142 *#* Several tweaks to reduce code size on ATmega8.
144 *#* Revision 1.7 2004/07/18 21:54:23 bernie
145 *#* Add ATmega8 support.
147 *#* Revision 1.5 2004/06/27 15:25:40 aleph
148 *#* Add missing callbacks for SPI;
149 *#* Change UNUSED() macro to new version with two args;
150 *#* Use TX line filling only on the correct KBUS serial port;
151 *#* Fix nasty IRQ disabling bug in recv complete hander for port 1.
153 *#* Revision 1.4 2004/06/03 11:27:09 bernie
154 *#* Add dual-license information.
156 *#* Revision 1.3 2004/06/02 21:35:24 aleph
157 *#* Serial enhancements: interruptible receive handler and 8 bit serial status for AVR; remove volatile attribute to FIFOBuffer, useless for new fifobuf routens
159 *#* Revision 1.2 2004/05/23 18:21:53 bernie
160 *#* Trim CVS logs and cleanup header info.
167 #include <hw_ser.h> /* Required for bus macros overrides */
168 #include <hw_cpu.h> /* CLOCK_FREQ */
169 #include <appconfig.h>
171 #include <cfg/debug.h>
172 #include <drv/timer.h>
173 #include <mware/fifobuf.h>
176 #if defined(__AVR_LIBC_VERSION__) && (__AVR_LIBC_VERSION__ >= 10400UL)
177 #include <avr/interrupt.h>
179 #include <avr/signal.h>
183 #if !CONFIG_SER_HWHANDSHAKE
185 * \name Hardware handshake (RTS/CTS).
188 #define RTS_ON do {} while (0)
189 #define RTS_OFF do {} while (0)
190 #define IS_CTS_ON true
191 #define EIMSKF_CTS 0 /**< Dummy value, must be overridden */
195 #if CPU_AVR_ATMEGA1281
196 #define BIT_RXCIE0 RXCIE0
197 #define BIT_RXEN0 RXEN0
198 #define BIT_TXEN0 TXEN0
199 #define BIT_UDRIE0 UDRIE0
201 #define BIT_RXCIE1 RXCIE1
202 #define BIT_RXEN1 RXEN1
203 #define BIT_TXEN1 TXEN1
204 #define BIT_UDRIE1 UDRIE1
206 #define BIT_RXCIE0 RXCIE
207 #define BIT_RXEN0 RXEN
208 #define BIT_TXEN0 TXEN
209 #define BIT_UDRIE0 UDRIE
211 #define BIT_RXCIE1 RXCIE
212 #define BIT_RXEN1 RXEN
213 #define BIT_TXEN1 TXEN
214 #define BIT_UDRIE1 UDRIE
219 * \name Overridable serial bus hooks
221 * These can be redefined in hw.h to implement
222 * special bus policies such as half-duplex, 485, etc.
226 * TXBEGIN TXCHAR TXEND TXOFF
227 * | __________|__________ | |
230 * ______ __ __ __ __ __ __ ________________
231 * \/ \/ \/ \/ \/ \/ \/
232 * ______/\__/\__/\__/\__/\__/\__/
238 #ifndef SER_UART0_BUS_TXINIT
240 * Default TXINIT macro - invoked in uart0_init()
242 * - Enable both the receiver and the transmitter
243 * - Enable only the RX complete interrupt
245 #define SER_UART0_BUS_TXINIT do { \
246 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0); \
250 #ifndef SER_UART0_BUS_TXBEGIN
252 * Invoked before starting a transmission
254 * - Enable both the receiver and the transmitter
255 * - Enable both the RX complete and UDR empty interrupts
257 #define SER_UART0_BUS_TXBEGIN do { \
258 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_UDRIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0); \
262 #ifndef SER_UART0_BUS_TXCHAR
264 * Invoked to send one character.
266 #define SER_UART0_BUS_TXCHAR(c) do { \
271 #ifndef SER_UART0_BUS_TXEND
273 * Invoked as soon as the txfifo becomes empty
275 * - Keep both the receiver and the transmitter enabled
276 * - Keep the RX complete interrupt enabled
277 * - Disable the UDR empty interrupt
279 #define SER_UART0_BUS_TXEND do { \
280 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0); \
284 #ifndef SER_UART0_BUS_TXOFF
286 * \def SER_UART0_BUS_TXOFF
288 * Invoked after the last character has been transmitted
290 * The default is no action.
293 #define SER_UART0_BUS_TXOFF
297 #ifndef SER_UART1_BUS_TXINIT
298 /** \sa SER_UART0_BUS_TXINIT */
299 #define SER_UART1_BUS_TXINIT do { \
300 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1); \
303 #ifndef SER_UART1_BUS_TXBEGIN
304 /** \sa SER_UART0_BUS_TXBEGIN */
305 #define SER_UART1_BUS_TXBEGIN do { \
306 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_UDRIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1); \
309 #ifndef SER_UART1_BUS_TXCHAR
310 /** \sa SER_UART0_BUS_TXCHAR */
311 #define SER_UART1_BUS_TXCHAR(c) do { \
315 #ifndef SER_UART1_BUS_TXEND
316 /** \sa SER_UART0_BUS_TXEND */
317 #define SER_UART1_BUS_TXEND do { \
318 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1); \
321 #ifndef SER_UART1_BUS_TXOFF
323 * \def SER_UART1_BUS_TXOFF
325 * \see SER_UART0_BUS_TXOFF
328 #define SER_UART1_BUS_TXOFF
335 * \name Overridable SPI hooks
337 * These can be redefined in hw.h to implement
338 * special bus policies such as slave select pin handling, etc.
342 #ifndef SER_SPI_BUS_TXINIT
344 * Default TXINIT macro - invoked in spi_init()
345 * The default is no action.
347 #define SER_SPI_BUS_TXINIT
350 #ifndef SER_SPI_BUS_TXCLOSE
352 * Invoked after the last character has been transmitted.
353 * The default is no action.
355 #define SER_SPI_BUS_TXCLOSE
360 /* SPI port and pin configuration */
361 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA103 || CPU_AVR_ATMEGA1281
362 #define SPI_PORT PORTB
364 #define SPI_SS_BIT PB0
365 #define SPI_SCK_BIT PB1
366 #define SPI_MOSI_BIT PB2
367 #define SPI_MISO_BIT PB3
368 #elif CPU_AVR_ATMEGA8
369 #define SPI_PORT PORTB
371 #define SPI_SS_BIT PB2
372 #define SPI_SCK_BIT PB5
373 #define SPI_MOSI_BIT PB3
374 #define SPI_MISO_BIT PB4
376 #error Unknown architecture
379 /* USART register definitions */
380 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA1281
381 #define AVR_HAS_UART1 1
382 #elif CPU_AVR_ATMEGA8
383 #define AVR_HAS_UART1 0
390 #define SIG_UART0_DATA SIG_UART_DATA
391 #define SIG_UART0_RECV SIG_UART_RECV
392 #define SIG_UART0_TRANS SIG_UART_TRANS
393 #elif CPU_AVR_ATMEGA103
394 #define AVR_HAS_UART1 0
399 #define SIG_UART0_DATA SIG_UART_DATA
400 #define SIG_UART0_RECV SIG_UART_RECV
401 #define SIG_UART0_TRANS SIG_UART_TRANS
403 #error Unknown architecture
408 * \def CONFIG_SER_STROBE
410 * This is a debug facility that can be used to
411 * monitor SER interrupt activity on an external pin.
413 * To use strobes, redefine the macros SER_STROBE_ON,
414 * SER_STROBE_OFF and SER_STROBE_INIT and set
415 * CONFIG_SER_STROBE to 1.
417 #if !defined(CONFIG_SER_STROBE) || !CONFIG_SER_STROBE
418 #define SER_STROBE_ON do {/*nop*/} while(0)
419 #define SER_STROBE_OFF do {/*nop*/} while(0)
420 #define SER_STROBE_INIT do {/*nop*/} while(0)
424 /* From the high-level serial driver */
425 extern struct Serial ser_handles[SER_CNT];
427 /* TX and RX buffers */
428 static unsigned char uart0_txbuffer[CONFIG_UART0_TXBUFSIZE];
429 static unsigned char uart0_rxbuffer[CONFIG_UART0_RXBUFSIZE];
431 static unsigned char uart1_txbuffer[CONFIG_UART1_TXBUFSIZE];
432 static unsigned char uart1_rxbuffer[CONFIG_UART1_RXBUFSIZE];
434 static unsigned char spi_txbuffer[CONFIG_SPI_TXBUFSIZE];
435 static unsigned char spi_rxbuffer[CONFIG_SPI_RXBUFSIZE];
439 * Internal hardware state structure
441 * The \a sending variable is true while the transmission
442 * interrupt is retriggering itself.
444 * For the USARTs the \a sending flag is useful for taking specific
445 * actions before sending a burst of data, at the start of a trasmission
446 * but not before every char sent.
448 * For the SPI, this flag is necessary because the SPI sends and receives
449 * bytes at the same time and the SPI IRQ is unique for send/receive.
450 * The only way to start transmission is to write data in SPDR (this
451 * is done by spi_starttx()). We do this *only* if a transfer is
452 * not already started.
456 struct SerialHardware hw;
457 volatile bool sending;
462 * These are to trick GCC into *not* using absolute addressing mode
463 * when accessing ser_handles, which is very expensive.
465 * Accessing through these pointers generates much shorter
466 * (and hopefully faster) code.
468 struct Serial *ser_uart0 = &ser_handles[SER_UART0];
470 struct Serial *ser_uart1 = &ser_handles[SER_UART1];
472 struct Serial *ser_spi = &ser_handles[SER_SPI];
479 static void uart0_init(
480 UNUSED_ARG(struct SerialHardware *, _hw),
481 UNUSED_ARG(struct Serial *, ser))
483 SER_UART0_BUS_TXINIT;
488 static void uart0_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
493 static void uart0_enabletxirq(struct SerialHardware *_hw)
495 struct AvrSerial *hw = (struct AvrSerial *)_hw;
498 * WARNING: racy code here! The tx interrupt sets hw->sending to false
499 * when it runs with an empty fifo. The order of statements in the
505 SER_UART0_BUS_TXBEGIN;
509 static void uart0_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
511 /* Compute baud-rate period */
512 uint16_t period = (((CLOCK_FREQ / 16UL) + (rate / 2)) / rate) - 1;
514 #if !CPU_AVR_ATMEGA103
515 UBRR0H = (period) >> 8;
519 //DB(kprintf("uart0_setbaudrate(rate=%lu): period=%d\n", rate, period);)
522 static void uart0_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
524 #if !CPU_AVR_ATMEGA103
525 UCSR0C = (UCSR0C & ~(BV(UPM01) | BV(UPM00))) | ((parity) << UPM00);
531 static void uart1_init(
532 UNUSED_ARG(struct SerialHardware *, _hw),
533 UNUSED_ARG(struct Serial *, ser))
535 SER_UART1_BUS_TXINIT;
540 static void uart1_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
545 static void uart1_enabletxirq(struct SerialHardware *_hw)
547 struct AvrSerial *hw = (struct AvrSerial *)_hw;
550 * WARNING: racy code here! The tx interrupt
551 * sets hw->sending to false when it runs with
552 * an empty fifo. The order of the statements
553 * in the if-block matters.
558 SER_UART1_BUS_TXBEGIN;
562 static void uart1_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
564 /* Compute baud-rate period */
565 uint16_t period = (((CLOCK_FREQ / 16UL) + (rate / 2)) / rate) - 1;
567 UBRR1H = (period) >> 8;
570 //DB(kprintf("uart1_setbaudrate(rate=%ld): period=%d\n", rate, period);)
573 static void uart1_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
575 UCSR1C = (UCSR1C & ~(BV(UPM11) | BV(UPM10))) | ((parity) << UPM10);
578 #endif // AVR_HAS_UART1
580 static void spi_init(UNUSED_ARG(struct SerialHardware *, _hw), UNUSED_ARG(struct Serial *, ser))
583 * Set MOSI and SCK ports out, MISO in.
585 * The ATmega64/128 datasheet explicitly states that the input/output
586 * state of the SPI pins is not significant, as when the SPI is
587 * active the I/O port are overrided.
588 * This is *blatantly FALSE*.
590 * Moreover, the MISO pin on the board_kc *must* be in high impedance
591 * state even when the SPI is off, because the line is wired together
592 * with the KBus serial RX, and the transmitter of the slave boards
593 * would be unable to drive the line.
595 ATOMIC(SPI_DDR |= (BV(SPI_MOSI_BIT) | BV(SPI_SCK_BIT)));
598 * If the SPI master mode is activated and the SS pin is in input and tied low,
599 * the SPI hardware will automatically switch to slave mode!
600 * For proper communication this pins should therefore be:
602 * - as input but tied high forever!
603 * This driver set the pin as output.
605 #warning SPI SS pin set as output for proper operation, check schematics for possible conflicts.
606 ATOMIC(SPI_DDR |= BV(SPI_SS_BIT));
608 ATOMIC(SPI_DDR &= ~BV(SPI_MISO_BIT));
609 /* Enable SPI, IRQ on, Master */
610 SPCR = BV(SPE) | BV(SPIE) | BV(MSTR);
613 #if CONFIG_SPI_DATA_ORDER == SER_LSB_FIRST
617 /* Set SPI clock rate */
618 #if CONFIG_SPI_CLOCK_DIV == 128
619 SPCR |= (BV(SPR1) | BV(SPR0));
620 #elif (CONFIG_SPI_CLOCK_DIV == 64 || CONFIG_SPI_CLOCK_DIV == 32)
622 #elif (CONFIG_SPI_CLOCK_DIV == 16 || CONFIG_SPI_CLOCK_DIV == 8)
624 #elif (CONFIG_SPI_CLOCK_DIV == 4 || CONFIG_SPI_CLOCK_DIV == 2)
625 // SPR0 & SDPR1 both at 0
627 #error Unsupported SPI clock division factor.
630 /* Set SPI2X bit (spi double frequency) */
631 #if (CONFIG_SPI_CLOCK_DIV == 128 || CONFIG_SPI_CLOCK_DIV == 64 \
632 || CONFIG_SPI_CLOCK_DIV == 16 || CONFIG_SPI_CLOCK_DIV == 4)
634 #elif (CONFIG_SPI_CLOCK_DIV == 32 || CONFIG_SPI_CLOCK_DIV == 8 || CONFIG_SPI_CLOCK_DIV == 2)
637 #error Unsupported SPI clock division factor.
640 /* Set clock polarity */
641 #if CONFIG_SPI_CLOCK_POL == 1
645 /* Set clock phase */
646 #if CONFIG_SPI_CLOCK_PHASE == 1
654 static void spi_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
660 /* Set all pins as inputs */
661 ATOMIC(SPI_DDR &= ~(BV(SPI_MISO_BIT) | BV(SPI_MOSI_BIT) | BV(SPI_SCK_BIT) | BV(SPI_SS_BIT)));
664 static void spi_starttx(struct SerialHardware *_hw)
666 struct AvrSerial *hw = (struct AvrSerial *)_hw;
669 IRQ_SAVE_DISABLE(flags);
671 /* Send data only if the SPI is not already transmitting */
672 if (!hw->sending && !fifo_isempty(&ser_spi->txfifo))
675 SPDR = fifo_pop(&ser_spi->txfifo);
681 static void spi_setbaudrate(
682 UNUSED_ARG(struct SerialHardware *, _hw),
683 UNUSED_ARG(unsigned long, rate))
688 static void spi_setparity(UNUSED_ARG(struct SerialHardware *, _hw), UNUSED_ARG(int, parity))
693 static bool tx_sending(struct SerialHardware* _hw)
695 struct AvrSerial *hw = (struct AvrSerial *)_hw;
701 // FIXME: move into compiler.h? Ditch?
703 #define C99INIT(name,val) .name = val
704 #elif defined(__GNUC__)
705 #define C99INIT(name,val) name: val
707 #warning No designated initializers, double check your code
708 #define C99INIT(name,val) (val)
712 * High-level interface data structures
714 static const struct SerialHardwareVT UART0_VT =
716 C99INIT(init, uart0_init),
717 C99INIT(cleanup, uart0_cleanup),
718 C99INIT(setBaudrate, uart0_setbaudrate),
719 C99INIT(setParity, uart0_setparity),
720 C99INIT(txStart, uart0_enabletxirq),
721 C99INIT(txSending, tx_sending),
725 static const struct SerialHardwareVT UART1_VT =
727 C99INIT(init, uart1_init),
728 C99INIT(cleanup, uart1_cleanup),
729 C99INIT(setBaudrate, uart1_setbaudrate),
730 C99INIT(setParity, uart1_setparity),
731 C99INIT(txStart, uart1_enabletxirq),
732 C99INIT(txSending, tx_sending),
734 #endif // AVR_HAS_UART1
736 static const struct SerialHardwareVT SPI_VT =
738 C99INIT(init, spi_init),
739 C99INIT(cleanup, spi_cleanup),
740 C99INIT(setBaudrate, spi_setbaudrate),
741 C99INIT(setParity, spi_setparity),
742 C99INIT(txStart, spi_starttx),
743 C99INIT(txSending, tx_sending),
746 static struct AvrSerial UARTDescs[SER_CNT] =
750 C99INIT(table, &UART0_VT),
751 C99INIT(txbuffer, uart0_txbuffer),
752 C99INIT(rxbuffer, uart0_rxbuffer),
753 C99INIT(txbuffer_size, sizeof(uart0_txbuffer)),
754 C99INIT(rxbuffer_size, sizeof(uart0_rxbuffer)),
756 C99INIT(sending, false),
761 C99INIT(table, &UART1_VT),
762 C99INIT(txbuffer, uart1_txbuffer),
763 C99INIT(rxbuffer, uart1_rxbuffer),
764 C99INIT(txbuffer_size, sizeof(uart1_txbuffer)),
765 C99INIT(rxbuffer_size, sizeof(uart1_rxbuffer)),
767 C99INIT(sending, false),
772 C99INIT(table, &SPI_VT),
773 C99INIT(txbuffer, spi_txbuffer),
774 C99INIT(rxbuffer, spi_rxbuffer),
775 C99INIT(txbuffer_size, sizeof(spi_txbuffer)),
776 C99INIT(rxbuffer_size, sizeof(spi_rxbuffer)),
778 C99INIT(sending, false),
782 struct SerialHardware *ser_hw_getdesc(int unit)
784 ASSERT(unit < SER_CNT);
785 return &UARTDescs[unit].hw;
793 #if CONFIG_SER_HWHANDSHAKE
795 /// This interrupt is triggered when the CTS line goes high
798 // Re-enable UDR empty interrupt and TX, then disable CTS interrupt
799 UCSR0B = BV(RXCIE) | BV(UDRIE) | BV(RXEN) | BV(TXEN);
800 EIMSK &= ~EIMSKF_CTS;
803 #endif // CONFIG_SER_HWHANDSHAKE
807 * Serial 0 TX interrupt handler
809 SIGNAL(USART0_UDRE_vect)
813 struct FIFOBuffer * const txfifo = &ser_uart0->txfifo;
815 if (fifo_isempty(txfifo))
818 #ifndef SER_UART0_BUS_TXOFF
819 UARTDescs[SER_UART0].sending = false;
822 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA103
825 // Disable rx interrupt and tx, enable CTS interrupt
827 UCSR0B = BV(RXCIE) | BV(RXEN) | BV(TXEN);
834 char c = fifo_pop(txfifo);
835 SER_UART0_BUS_TXCHAR(c);
841 #ifdef SER_UART0_BUS_TXOFF
843 * Serial port 0 TX complete interrupt handler.
845 * This IRQ is usually disabled. The UDR-empty interrupt
846 * enables it when there's no more data to transmit.
847 * We need to wait until the last character has been
848 * transmitted before switching the 485 transceiver to
851 * The txfifo might have been refilled by putchar() while
852 * we were waiting for the transmission complete interrupt.
853 * In this case, we must restart the UDR empty interrupt,
854 * otherwise we'd stop the serial port with some data
855 * still pending in the buffer.
857 SIGNAL(SIG_UART0_TRANS)
861 struct FIFOBuffer * const txfifo = &ser_uart0->txfifo;
862 if (fifo_isempty(txfifo))
865 UARTDescs[SER_UART0].sending = false;
868 UCSR0B = BV(RXCIE) | BV(UDRIE) | BV(RXEN) | BV(TXEN);
872 #endif /* SER_UART0_BUS_TXOFF */
878 * Serial 1 TX interrupt handler
880 SIGNAL(USART1_UDRE_vect)
884 struct FIFOBuffer * const txfifo = &ser_uart1->txfifo;
886 if (fifo_isempty(txfifo))
889 #ifndef SER_UART1_BUS_TXOFF
890 UARTDescs[SER_UART1].sending = false;
893 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA103
896 // Disable rx interrupt and tx, enable CTS interrupt
898 UCSR1B = BV(RXCIE) | BV(RXEN) | BV(TXEN);
905 char c = fifo_pop(txfifo);
906 SER_UART1_BUS_TXCHAR(c);
912 #ifdef SER_UART1_BUS_TXOFF
914 * Serial port 1 TX complete interrupt handler.
916 * \sa port 0 TX complete handler.
918 SIGNAL(SIG_UART1_TRANS)
922 struct FIFOBuffer * const txfifo = &ser_uart1->txfifo;
923 if (fifo_isempty(txfifo))
926 UARTDescs[SER_UART1].sending = false;
929 UCSR1B = BV(RXCIE) | BV(UDRIE) | BV(RXEN) | BV(TXEN);
933 #endif /* SER_UART1_BUS_TXOFF */
935 #endif // AVR_HAS_UART1
939 * Serial 0 RX complete interrupt handler.
941 * This handler is interruptible.
942 * Interrupt are reenabled as soon as recv complete interrupt is
943 * disabled. Using INTERRUPT() is troublesome when the serial
944 * is heavily loaded, because an interrupt could be retriggered
945 * when executing the handler prologue before RXCIE is disabled.
947 * \note The code that re-enables interrupts is commented out
948 * because in some nasty cases the interrupt is retriggered.
949 * This is probably due to the RXC flag being set before
950 * RXCIE is cleared. Unfortunately the RXC flag is read-only
951 * and can't be cleared by code.
953 SIGNAL(USART0_RX_vect)
957 /* Disable Recv complete IRQ */
958 //UCSR0B &= ~BV(RXCIE);
961 /* Should be read before UDR */
962 ser_uart0->status |= UCSR0A & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
964 /* To clear the RXC flag we must _always_ read the UDR even when we're
965 * not going to accept the incoming data, otherwise a new interrupt
966 * will occur once the handler terminates.
969 struct FIFOBuffer * const rxfifo = &ser_uart0->rxfifo;
971 if (fifo_isfull(rxfifo))
972 ser_uart0->status |= SERRF_RXFIFOOVERRUN;
975 fifo_push(rxfifo, c);
976 #if CONFIG_SER_HWHANDSHAKE
977 if (fifo_isfull(rxfifo))
982 /* Reenable receive complete int */
984 //UCSR0B |= BV(RXCIE);
993 * Serial 1 RX complete interrupt handler.
995 * This handler is interruptible.
996 * Interrupt are reenabled as soon as recv complete interrupt is
997 * disabled. Using INTERRUPT() is troublesome when the serial
998 * is heavily loaded, because an interrupt could be retriggered
999 * when executing the handler prologue before RXCIE is disabled.
1001 * \see SIGNAL(USART1_RX_vect)
1003 SIGNAL(USART1_RX_vect)
1007 /* Disable Recv complete IRQ */
1008 //UCSR1B &= ~BV(RXCIE);
1011 /* Should be read before UDR */
1012 ser_uart1->status |= UCSR1A & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
1014 /* To avoid an IRQ storm, we must _always_ read the UDR even when we're
1015 * not going to accept the incoming data
1018 struct FIFOBuffer * const rxfifo = &ser_uart1->rxfifo;
1019 //ASSERT_VALID_FIFO(rxfifo);
1021 if (UNLIKELY(fifo_isfull(rxfifo)))
1022 ser_uart1->status |= SERRF_RXFIFOOVERRUN;
1025 fifo_push(rxfifo, c);
1026 #if CONFIG_SER_HWHANDSHAKE
1027 if (fifo_isfull(rxfifo))
1031 /* Re-enable receive complete int */
1033 //UCSR1B |= BV(RXCIE);
1038 #endif // AVR_HAS_UART1
1042 * SPI interrupt handler
1048 /* Read incoming byte. */
1049 if (!fifo_isfull(&ser_spi->rxfifo))
1050 fifo_push(&ser_spi->rxfifo, SPDR);
1054 ser_spi->status |= SERRF_RXFIFOOVERRUN;
1058 if (!fifo_isempty(&ser_spi->txfifo))
1059 SPDR = fifo_pop(&ser_spi->txfifo);
1061 UARTDescs[SER_SPI].sending = false;