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 Bernie Innocenti <bernie@codewiz.org>
34 * \brief AVR UART and SPI I/O driver (Implementation)
38 * \author Bernie Innocenti <bernie@codewiz.org>
39 * \author Stefano Fedrigo <aleph@develer.com>
42 #include "hw/hw_ser.h" /* Required for bus macros overrides */
43 #include <hw/hw_cpufreq.h> /* CPU_FREQ */
45 #include "cfg/cfg_ser.h"
47 #include <cfg/macros.h> /* DIV_ROUND */
48 #include <cfg/debug.h>
49 #include <cfg/cfg_arch.h> // ARCH_NIGHTTEST
52 #include <drv/ser_p.h>
53 #include <drv/timer.h>
55 #include <struct/fifobuf.h>
59 #if defined(__AVR_LIBC_VERSION__) && (__AVR_LIBC_VERSION__ >= 10400UL)
60 #include <avr/interrupt.h>
62 #include <avr/signal.h>
66 #if !CONFIG_SER_HWHANDSHAKE
68 * \name Hardware handshake (RTS/CTS).
71 #define RTS_ON do {} while (0)
72 #define RTS_OFF do {} while (0)
73 #define IS_CTS_ON true
74 #define EIMSKF_CTS 0 /**< Dummy value, must be overridden */
78 #if CPU_AVR_ATMEGA1281
79 #define BIT_RXCIE0 RXCIE0
80 #define BIT_RXEN0 RXEN0
81 #define BIT_TXEN0 TXEN0
82 #define BIT_UDRIE0 UDRIE0
84 #define BIT_RXCIE1 RXCIE1
85 #define BIT_RXEN1 RXEN1
86 #define BIT_TXEN1 TXEN1
87 #define BIT_UDRIE1 UDRIE1
89 #define BIT_RXCIE0 RXCIE
90 #define BIT_RXEN0 RXEN
91 #define BIT_TXEN0 TXEN
92 #define BIT_UDRIE0 UDRIE
94 #define BIT_RXCIE1 RXCIE
95 #define BIT_RXEN1 RXEN
96 #define BIT_TXEN1 TXEN
97 #define BIT_UDRIE1 UDRIE
102 * \name Overridable serial bus hooks
104 * These can be redefined in hw.h to implement
105 * special bus policies such as half-duplex, 485, etc.
109 * TXBEGIN TXCHAR TXEND TXOFF
110 * | __________|__________ | |
113 * ______ __ __ __ __ __ __ ________________
114 * \/ \/ \/ \/ \/ \/ \/
115 * ______/\__/\__/\__/\__/\__/\__/
121 #ifndef SER_UART0_BUS_TXINIT
123 * Default TXINIT macro - invoked in uart0_init()
125 * - Enable both the receiver and the transmitter
126 * - Enable only the RX complete interrupt
128 #define SER_UART0_BUS_TXINIT do { \
129 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0); \
133 #ifndef SER_UART0_BUS_TXBEGIN
135 * Invoked before starting a transmission
137 * - Enable both the receiver and the transmitter
138 * - Enable both the RX complete and UDR empty interrupts
140 #define SER_UART0_BUS_TXBEGIN do { \
141 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_UDRIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0); \
145 #ifndef SER_UART0_BUS_TXCHAR
147 * Invoked to send one character.
149 #define SER_UART0_BUS_TXCHAR(c) do { \
154 #ifndef SER_UART0_BUS_TXEND
156 * Invoked as soon as the txfifo becomes empty
158 * - Keep both the receiver and the transmitter enabled
159 * - Keep the RX complete interrupt enabled
160 * - Disable the UDR empty interrupt
162 #define SER_UART0_BUS_TXEND do { \
163 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0); \
167 #ifndef SER_UART0_BUS_TXOFF
169 * \def SER_UART0_BUS_TXOFF
171 * Invoked after the last character has been transmitted
173 * The default is no action.
176 #define SER_UART0_BUS_TXOFF
180 #ifndef SER_UART1_BUS_TXINIT
181 /** \sa SER_UART0_BUS_TXINIT */
182 #define SER_UART1_BUS_TXINIT do { \
183 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1); \
186 #ifndef SER_UART1_BUS_TXBEGIN
187 /** \sa SER_UART0_BUS_TXBEGIN */
188 #define SER_UART1_BUS_TXBEGIN do { \
189 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_UDRIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1); \
192 #ifndef SER_UART1_BUS_TXCHAR
193 /** \sa SER_UART0_BUS_TXCHAR */
194 #define SER_UART1_BUS_TXCHAR(c) do { \
198 #ifndef SER_UART1_BUS_TXEND
199 /** \sa SER_UART0_BUS_TXEND */
200 #define SER_UART1_BUS_TXEND do { \
201 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1); \
204 #ifndef SER_UART1_BUS_TXOFF
206 * \def SER_UART1_BUS_TXOFF
208 * \see SER_UART0_BUS_TXOFF
211 #define SER_UART1_BUS_TXOFF
218 * \name Overridable SPI hooks
220 * These can be redefined in hw.h to implement
221 * special bus policies such as slave select pin handling, etc.
225 #ifndef SER_SPI_BUS_TXINIT
227 * Default TXINIT macro - invoked in spi_init()
228 * The default is no action.
230 #define SER_SPI_BUS_TXINIT
233 #ifndef SER_SPI_BUS_TXCLOSE
235 * Invoked after the last character has been transmitted.
236 * The default is no action.
238 #define SER_SPI_BUS_TXCLOSE
243 /* SPI port and pin configuration */
244 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA103 || CPU_AVR_ATMEGA1281
245 #define SPI_PORT PORTB
247 #define SPI_SS_BIT PB0
248 #define SPI_SCK_BIT PB1
249 #define SPI_MOSI_BIT PB2
250 #define SPI_MISO_BIT PB3
251 #elif CPU_AVR_ATMEGA8
252 #define SPI_PORT PORTB
254 #define SPI_SS_BIT PB2
255 #define SPI_SCK_BIT PB5
256 #define SPI_MOSI_BIT PB3
257 #define SPI_MISO_BIT PB4
259 #error Unknown architecture
262 /* USART register definitions */
263 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA1281
264 #define AVR_HAS_UART1 1
265 #elif CPU_AVR_ATMEGA8
266 #define AVR_HAS_UART1 0
273 #define SIG_UART0_DATA SIG_UART_DATA
274 #define SIG_UART0_RECV SIG_UART_RECV
275 #define SIG_UART0_TRANS SIG_UART_TRANS
276 #elif CPU_AVR_ATMEGA103
277 #define AVR_HAS_UART1 0
282 #define SIG_UART0_DATA SIG_UART_DATA
283 #define SIG_UART0_RECV SIG_UART_RECV
284 #define SIG_UART0_TRANS SIG_UART_TRANS
286 #error Unknown architecture
290 /* From the high-level serial driver */
291 extern struct Serial *ser_handles[SER_CNT];
293 /* TX and RX buffers */
294 static unsigned char uart0_txbuffer[CONFIG_UART0_TXBUFSIZE];
295 static unsigned char uart0_rxbuffer[CONFIG_UART0_RXBUFSIZE];
297 static unsigned char uart1_txbuffer[CONFIG_UART1_TXBUFSIZE];
298 static unsigned char uart1_rxbuffer[CONFIG_UART1_RXBUFSIZE];
300 static unsigned char spi_txbuffer[CONFIG_SPI_TXBUFSIZE];
301 static unsigned char spi_rxbuffer[CONFIG_SPI_RXBUFSIZE];
305 * Internal hardware state structure
307 * The \a sending variable is true while the transmission
308 * interrupt is retriggering itself.
310 * For the USARTs the \a sending flag is useful for taking specific
311 * actions before sending a burst of data, at the start of a trasmission
312 * but not before every char sent.
314 * For the SPI, this flag is necessary because the SPI sends and receives
315 * bytes at the same time and the SPI IRQ is unique for send/receive.
316 * The only way to start transmission is to write data in SPDR (this
317 * is done by spi_starttx()). We do this *only* if a transfer is
318 * not already started.
322 struct SerialHardware hw;
323 volatile bool sending;
331 static void uart0_init(
332 UNUSED_ARG(struct SerialHardware *, _hw),
333 UNUSED_ARG(struct Serial *, ser))
335 SER_UART0_BUS_TXINIT;
340 static void uart0_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
345 static void uart0_enabletxirq(struct SerialHardware *_hw)
347 struct AvrSerial *hw = (struct AvrSerial *)_hw;
350 * WARNING: racy code here! The tx interrupt sets hw->sending to false
351 * when it runs with an empty fifo. The order of statements in the
357 SER_UART0_BUS_TXBEGIN;
361 static void uart0_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
363 /* Compute baud-rate period */
364 uint16_t period = DIV_ROUND(CPU_FREQ / 16UL, rate) - 1;
366 #if !CPU_AVR_ATMEGA103
367 UBRR0H = (period) >> 8;
371 //DB(kprintf("uart0_setbaudrate(rate=%lu): period=%d\n", rate, period);)
374 static void uart0_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
376 #if !CPU_AVR_ATMEGA103
377 UCSR0C = (UCSR0C & ~(BV(UPM01) | BV(UPM00))) | ((parity) << UPM00);
383 static void uart1_init(
384 UNUSED_ARG(struct SerialHardware *, _hw),
385 UNUSED_ARG(struct Serial *, ser))
387 SER_UART1_BUS_TXINIT;
392 static void uart1_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
397 static void uart1_enabletxirq(struct SerialHardware *_hw)
399 struct AvrSerial *hw = (struct AvrSerial *)_hw;
402 * WARNING: racy code here! The tx interrupt
403 * sets hw->sending to false when it runs with
404 * an empty fifo. The order of the statements
405 * in the if-block matters.
410 SER_UART1_BUS_TXBEGIN;
414 static void uart1_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
416 /* Compute baud-rate period */
417 uint16_t period = DIV_ROUND(CPU_FREQ / 16UL, rate) - 1;
419 UBRR1H = (period) >> 8;
422 //DB(kprintf("uart1_setbaudrate(rate=%ld): period=%d\n", rate, period);)
425 static void uart1_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
427 UCSR1C = (UCSR1C & ~(BV(UPM11) | BV(UPM10))) | ((parity) << UPM10);
430 #endif // AVR_HAS_UART1
432 static void spi_init(UNUSED_ARG(struct SerialHardware *, _hw), UNUSED_ARG(struct Serial *, ser))
435 * Set MOSI and SCK ports out, MISO in.
437 * The ATmega64/128 datasheet explicitly states that the input/output
438 * state of the SPI pins is not significant, as when the SPI is
439 * active the I/O port are overrided.
440 * This is *blatantly FALSE*.
442 * Moreover, the MISO pin on the board_kc *must* be in high impedance
443 * state even when the SPI is off, because the line is wired together
444 * with the KBus serial RX, and the transmitter of the slave boards
445 * would be unable to drive the line.
447 ATOMIC(SPI_DDR |= (BV(SPI_MOSI_BIT) | BV(SPI_SCK_BIT)));
450 * If the SPI master mode is activated and the SS pin is in input and tied low,
451 * the SPI hardware will automatically switch to slave mode!
452 * For proper communication this pins should therefore be:
454 * - as input but tied high forever!
455 * This driver set the pin as output.
457 #if !(ARCH & ARCH_NIGHTTEST)
458 #warning SPI SS pin set as output for proper operation, check schematics for possible conflicts.
460 ATOMIC(SPI_DDR |= BV(SPI_SS_BIT));
462 ATOMIC(SPI_DDR &= ~BV(SPI_MISO_BIT));
463 /* Enable SPI, IRQ on, Master */
464 SPCR = BV(SPE) | BV(SPIE) | BV(MSTR);
467 #if CONFIG_SPI_DATA_ORDER == SER_LSB_FIRST
471 /* Set SPI clock rate */
472 #if CONFIG_SPI_CLOCK_DIV == 128
473 SPCR |= (BV(SPR1) | BV(SPR0));
474 #elif (CONFIG_SPI_CLOCK_DIV == 64 || CONFIG_SPI_CLOCK_DIV == 32)
476 #elif (CONFIG_SPI_CLOCK_DIV == 16 || CONFIG_SPI_CLOCK_DIV == 8)
478 #elif (CONFIG_SPI_CLOCK_DIV == 4 || CONFIG_SPI_CLOCK_DIV == 2)
479 // SPR0 & SDPR1 both at 0
481 #error Unsupported SPI clock division factor.
484 /* Set SPI2X bit (spi double frequency) */
485 #if (CONFIG_SPI_CLOCK_DIV == 128 || CONFIG_SPI_CLOCK_DIV == 64 \
486 || CONFIG_SPI_CLOCK_DIV == 16 || CONFIG_SPI_CLOCK_DIV == 4)
488 #elif (CONFIG_SPI_CLOCK_DIV == 32 || CONFIG_SPI_CLOCK_DIV == 8 || CONFIG_SPI_CLOCK_DIV == 2)
491 #error Unsupported SPI clock division factor.
494 /* Set clock polarity */
495 #if CONFIG_SPI_CLOCK_POL == 1
499 /* Set clock phase */
500 #if CONFIG_SPI_CLOCK_PHASE == 1
508 static void spi_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
514 /* Set all pins as inputs */
515 ATOMIC(SPI_DDR &= ~(BV(SPI_MISO_BIT) | BV(SPI_MOSI_BIT) | BV(SPI_SCK_BIT) | BV(SPI_SS_BIT)));
518 static void spi_starttx(struct SerialHardware *_hw)
520 struct AvrSerial *hw = (struct AvrSerial *)_hw;
523 IRQ_SAVE_DISABLE(flags);
525 /* Send data only if the SPI is not already transmitting */
526 if (!hw->sending && !fifo_isempty(&ser_handles[SER_SPI]->txfifo))
529 SPDR = fifo_pop(&ser_handles[SER_SPI]->txfifo);
535 static void spi_setbaudrate(
536 UNUSED_ARG(struct SerialHardware *, _hw),
537 UNUSED_ARG(unsigned long, rate))
542 static void spi_setparity(UNUSED_ARG(struct SerialHardware *, _hw), UNUSED_ARG(int, parity))
547 static bool tx_sending(struct SerialHardware* _hw)
549 struct AvrSerial *hw = (struct AvrSerial *)_hw;
555 // FIXME: move into compiler.h? Ditch?
557 #define C99INIT(name,val) .name = val
558 #elif defined(__GNUC__)
559 #define C99INIT(name,val) name: val
561 #warning No designated initializers, double check your code
562 #define C99INIT(name,val) (val)
566 * High-level interface data structures
568 static const struct SerialHardwareVT UART0_VT =
570 C99INIT(init, uart0_init),
571 C99INIT(cleanup, uart0_cleanup),
572 C99INIT(setBaudrate, uart0_setbaudrate),
573 C99INIT(setParity, uart0_setparity),
574 C99INIT(txStart, uart0_enabletxirq),
575 C99INIT(txSending, tx_sending),
579 static const struct SerialHardwareVT UART1_VT =
581 C99INIT(init, uart1_init),
582 C99INIT(cleanup, uart1_cleanup),
583 C99INIT(setBaudrate, uart1_setbaudrate),
584 C99INIT(setParity, uart1_setparity),
585 C99INIT(txStart, uart1_enabletxirq),
586 C99INIT(txSending, tx_sending),
588 #endif // AVR_HAS_UART1
590 static const struct SerialHardwareVT SPI_VT =
592 C99INIT(init, spi_init),
593 C99INIT(cleanup, spi_cleanup),
594 C99INIT(setBaudrate, spi_setbaudrate),
595 C99INIT(setParity, spi_setparity),
596 C99INIT(txStart, spi_starttx),
597 C99INIT(txSending, tx_sending),
600 static struct AvrSerial UARTDescs[SER_CNT] =
604 C99INIT(table, &UART0_VT),
605 C99INIT(txbuffer, uart0_txbuffer),
606 C99INIT(rxbuffer, uart0_rxbuffer),
607 C99INIT(txbuffer_size, sizeof(uart0_txbuffer)),
608 C99INIT(rxbuffer_size, sizeof(uart0_rxbuffer)),
610 C99INIT(sending, false),
615 C99INIT(table, &UART1_VT),
616 C99INIT(txbuffer, uart1_txbuffer),
617 C99INIT(rxbuffer, uart1_rxbuffer),
618 C99INIT(txbuffer_size, sizeof(uart1_txbuffer)),
619 C99INIT(rxbuffer_size, sizeof(uart1_rxbuffer)),
621 C99INIT(sending, false),
626 C99INIT(table, &SPI_VT),
627 C99INIT(txbuffer, spi_txbuffer),
628 C99INIT(rxbuffer, spi_rxbuffer),
629 C99INIT(txbuffer_size, sizeof(spi_txbuffer)),
630 C99INIT(rxbuffer_size, sizeof(spi_rxbuffer)),
632 C99INIT(sending, false),
636 struct SerialHardware *ser_hw_getdesc(int unit)
638 ASSERT(unit < SER_CNT);
639 return &UARTDescs[unit].hw;
647 #if CONFIG_SER_HWHANDSHAKE
649 /// This interrupt is triggered when the CTS line goes high
652 // Re-enable UDR empty interrupt and TX, then disable CTS interrupt
653 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_UDRIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0);
654 EIMSK &= ~EIMSKF_CTS;
657 #endif // CONFIG_SER_HWHANDSHAKE
661 * Serial 0 TX interrupt handler
663 SIGNAL(USART0_UDRE_vect)
667 struct FIFOBuffer * const txfifo = &ser_handles[SER_UART0]->txfifo;
669 if (fifo_isempty(txfifo))
672 #ifndef SER_UART0_BUS_TXOFF
673 UARTDescs[SER_UART0].sending = false;
676 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA103
679 // Disable rx interrupt and tx, enable CTS interrupt
681 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0);
688 char c = fifo_pop(txfifo);
689 SER_UART0_BUS_TXCHAR(c);
695 #ifdef SER_UART0_BUS_TXOFF
697 * Serial port 0 TX complete interrupt handler.
699 * This IRQ is usually disabled. The UDR-empty interrupt
700 * enables it when there's no more data to transmit.
701 * We need to wait until the last character has been
702 * transmitted before switching the 485 transceiver to
705 * The txfifo might have been refilled by putchar() while
706 * we were waiting for the transmission complete interrupt.
707 * In this case, we must restart the UDR empty interrupt,
708 * otherwise we'd stop the serial port with some data
709 * still pending in the buffer.
711 SIGNAL(SIG_UART0_TRANS)
715 struct FIFOBuffer * const txfifo = &ser_handles[SER_UART0]->txfifo;
716 if (fifo_isempty(txfifo))
719 UARTDescs[SER_UART0].sending = false;
722 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_UDRIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0);
726 #endif /* SER_UART0_BUS_TXOFF */
732 * Serial 1 TX interrupt handler
734 SIGNAL(USART1_UDRE_vect)
738 struct FIFOBuffer * const txfifo = &ser_handles[SER_UART1]->txfifo;
740 if (fifo_isempty(txfifo))
743 #ifndef SER_UART1_BUS_TXOFF
744 UARTDescs[SER_UART1].sending = false;
747 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA103
750 // Disable rx interrupt and tx, enable CTS interrupt
752 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1);
759 char c = fifo_pop(txfifo);
760 SER_UART1_BUS_TXCHAR(c);
766 #ifdef SER_UART1_BUS_TXOFF
768 * Serial port 1 TX complete interrupt handler.
770 * \sa port 0 TX complete handler.
772 SIGNAL(USART1_TX_vect)
776 struct FIFOBuffer * const txfifo = &ser_handles[SER_UART1]->txfifo;
777 if (fifo_isempty(txfifo))
780 UARTDescs[SER_UART1].sending = false;
783 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_UDRIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1);
787 #endif /* SER_UART1_BUS_TXOFF */
789 #endif // AVR_HAS_UART1
793 * Serial 0 RX complete interrupt handler.
795 * This handler is interruptible.
796 * Interrupt are reenabled as soon as recv complete interrupt is
797 * disabled. Using INTERRUPT() is troublesome when the serial
798 * is heavily loaded, because an interrupt could be retriggered
799 * when executing the handler prologue before RXCIE is disabled.
801 * \note The code that re-enables interrupts is commented out
802 * because in some nasty cases the interrupt is retriggered.
803 * This is probably due to the RXC flag being set before
804 * RXCIE is cleared. Unfortunately the RXC flag is read-only
805 * and can't be cleared by code.
807 SIGNAL(USART0_RX_vect)
811 /* Disable Recv complete IRQ */
812 //UCSR0B &= ~BV(RXCIE);
815 /* Should be read before UDR */
816 ser_handles[SER_UART0]->status |= UCSR0A & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
818 /* To clear the RXC flag we must _always_ read the UDR even when we're
819 * not going to accept the incoming data, otherwise a new interrupt
820 * will occur once the handler terminates.
823 struct FIFOBuffer * const rxfifo = &ser_handles[SER_UART0]->rxfifo;
825 if (fifo_isfull(rxfifo))
826 ser_handles[SER_UART0]->status |= SERRF_RXFIFOOVERRUN;
829 fifo_push(rxfifo, c);
830 #if CONFIG_SER_HWHANDSHAKE
831 if (fifo_isfull(rxfifo))
836 /* Reenable receive complete int */
838 //UCSR0B |= BV(RXCIE);
847 * Serial 1 RX complete interrupt handler.
849 * This handler is interruptible.
850 * Interrupt are reenabled as soon as recv complete interrupt is
851 * disabled. Using INTERRUPT() is troublesome when the serial
852 * is heavily loaded, because an interrupt could be retriggered
853 * when executing the handler prologue before RXCIE is disabled.
855 * \see SIGNAL(USART1_RX_vect)
857 SIGNAL(USART1_RX_vect)
861 /* Disable Recv complete IRQ */
862 //UCSR1B &= ~BV(RXCIE);
865 /* Should be read before UDR */
866 ser_handles[SER_UART1]->status |= UCSR1A & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
868 /* To avoid an IRQ storm, we must _always_ read the UDR even when we're
869 * not going to accept the incoming data
872 struct FIFOBuffer * const rxfifo = &ser_handles[SER_UART1]->rxfifo;
873 //ASSERT_VALID_FIFO(rxfifo);
875 if (UNLIKELY(fifo_isfull(rxfifo)))
876 ser_handles[SER_UART1]->status |= SERRF_RXFIFOOVERRUN;
879 fifo_push(rxfifo, c);
880 #if CONFIG_SER_HWHANDSHAKE
881 if (fifo_isfull(rxfifo))
885 /* Re-enable receive complete int */
887 //UCSR1B |= BV(RXCIE);
892 #endif // AVR_HAS_UART1
896 * SPI interrupt handler
902 /* Read incoming byte. */
903 if (!fifo_isfull(&ser_handles[SER_SPI]->rxfifo))
904 fifo_push(&ser_handles[SER_SPI]->rxfifo, SPDR);
908 ser_handles[SER_SPI]->status |= SERRF_RXFIFOOVERRUN;
912 if (!fifo_isempty(&ser_handles[SER_SPI]->txfifo))
913 SPDR = fifo_pop(&ser_handles[SER_SPI]->txfifo);
915 UARTDescs[SER_SPI].sending = false;