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>
51 #include <drv/ser_p.h>
52 #include <drv/timer.h>
54 #include <struct/fifobuf.h>
58 #if defined(__AVR_LIBC_VERSION__) && (__AVR_LIBC_VERSION__ >= 10400UL)
59 #include <avr/interrupt.h>
61 #include <avr/signal.h>
65 #if !CONFIG_SER_HWHANDSHAKE
67 * \name Hardware handshake (RTS/CTS).
70 #define RTS_ON do {} while (0)
71 #define RTS_OFF do {} while (0)
72 #define IS_CTS_ON true
73 #define EIMSKF_CTS 0 /**< Dummy value, must be overridden */
77 #if CPU_AVR_ATMEGA1281
78 #define BIT_RXCIE0 RXCIE0
79 #define BIT_RXEN0 RXEN0
80 #define BIT_TXEN0 TXEN0
81 #define BIT_UDRIE0 UDRIE0
83 #define BIT_RXCIE1 RXCIE1
84 #define BIT_RXEN1 RXEN1
85 #define BIT_TXEN1 TXEN1
86 #define BIT_UDRIE1 UDRIE1
88 #define BIT_RXCIE0 RXCIE
89 #define BIT_RXEN0 RXEN
90 #define BIT_TXEN0 TXEN
91 #define BIT_UDRIE0 UDRIE
93 #define BIT_RXCIE1 RXCIE
94 #define BIT_RXEN1 RXEN
95 #define BIT_TXEN1 TXEN
96 #define BIT_UDRIE1 UDRIE
101 * \name Overridable serial bus hooks
103 * These can be redefined in hw.h to implement
104 * special bus policies such as half-duplex, 485, etc.
108 * TXBEGIN TXCHAR TXEND TXOFF
109 * | __________|__________ | |
112 * ______ __ __ __ __ __ __ ________________
113 * \/ \/ \/ \/ \/ \/ \/
114 * ______/\__/\__/\__/\__/\__/\__/
120 #ifndef SER_UART0_BUS_TXINIT
122 * Default TXINIT macro - invoked in uart0_init()
124 * - Enable both the receiver and the transmitter
125 * - Enable only the RX complete interrupt
127 #define SER_UART0_BUS_TXINIT do { \
128 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0); \
132 #ifndef SER_UART0_BUS_TXBEGIN
134 * Invoked before starting a transmission
136 * - Enable both the receiver and the transmitter
137 * - Enable both the RX complete and UDR empty interrupts
139 #define SER_UART0_BUS_TXBEGIN do { \
140 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_UDRIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0); \
144 #ifndef SER_UART0_BUS_TXCHAR
146 * Invoked to send one character.
148 #define SER_UART0_BUS_TXCHAR(c) do { \
153 #ifndef SER_UART0_BUS_TXEND
155 * Invoked as soon as the txfifo becomes empty
157 * - Keep both the receiver and the transmitter enabled
158 * - Keep the RX complete interrupt enabled
159 * - Disable the UDR empty interrupt
161 #define SER_UART0_BUS_TXEND do { \
162 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0); \
166 #ifndef SER_UART0_BUS_TXOFF
168 * \def SER_UART0_BUS_TXOFF
170 * Invoked after the last character has been transmitted
172 * The default is no action.
175 #define SER_UART0_BUS_TXOFF
179 #ifndef SER_UART1_BUS_TXINIT
180 /** \sa SER_UART0_BUS_TXINIT */
181 #define SER_UART1_BUS_TXINIT do { \
182 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1); \
185 #ifndef SER_UART1_BUS_TXBEGIN
186 /** \sa SER_UART0_BUS_TXBEGIN */
187 #define SER_UART1_BUS_TXBEGIN do { \
188 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_UDRIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1); \
191 #ifndef SER_UART1_BUS_TXCHAR
192 /** \sa SER_UART0_BUS_TXCHAR */
193 #define SER_UART1_BUS_TXCHAR(c) do { \
197 #ifndef SER_UART1_BUS_TXEND
198 /** \sa SER_UART0_BUS_TXEND */
199 #define SER_UART1_BUS_TXEND do { \
200 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1); \
203 #ifndef SER_UART1_BUS_TXOFF
205 * \def SER_UART1_BUS_TXOFF
207 * \see SER_UART0_BUS_TXOFF
210 #define SER_UART1_BUS_TXOFF
217 * \name Overridable SPI hooks
219 * These can be redefined in hw.h to implement
220 * special bus policies such as slave select pin handling, etc.
224 #ifndef SER_SPI_BUS_TXINIT
226 * Default TXINIT macro - invoked in spi_init()
227 * The default is no action.
229 #define SER_SPI_BUS_TXINIT
232 #ifndef SER_SPI_BUS_TXCLOSE
234 * Invoked after the last character has been transmitted.
235 * The default is no action.
237 #define SER_SPI_BUS_TXCLOSE
242 /* SPI port and pin configuration */
243 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA103 || CPU_AVR_ATMEGA1281
244 #define SPI_PORT PORTB
246 #define SPI_SS_BIT PB0
247 #define SPI_SCK_BIT PB1
248 #define SPI_MOSI_BIT PB2
249 #define SPI_MISO_BIT PB3
250 #elif CPU_AVR_ATMEGA8
251 #define SPI_PORT PORTB
253 #define SPI_SS_BIT PB2
254 #define SPI_SCK_BIT PB5
255 #define SPI_MOSI_BIT PB3
256 #define SPI_MISO_BIT PB4
258 #error Unknown architecture
261 /* USART register definitions */
262 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA1281
263 #define AVR_HAS_UART1 1
264 #elif CPU_AVR_ATMEGA8
265 #define AVR_HAS_UART1 0
272 #define SIG_UART0_DATA SIG_UART_DATA
273 #define SIG_UART0_RECV SIG_UART_RECV
274 #define SIG_UART0_TRANS SIG_UART_TRANS
275 #elif CPU_AVR_ATMEGA103
276 #define AVR_HAS_UART1 0
281 #define SIG_UART0_DATA SIG_UART_DATA
282 #define SIG_UART0_RECV SIG_UART_RECV
283 #define SIG_UART0_TRANS SIG_UART_TRANS
285 #error Unknown architecture
290 * \def CONFIG_SER_STROBE
292 * This is a debug facility that can be used to
293 * monitor SER interrupt activity on an external pin.
295 * To use strobes, redefine the macros SER_STROBE_ON,
296 * SER_STROBE_OFF and SER_STROBE_INIT and set
297 * CONFIG_SER_STROBE to 1.
299 #if !defined(CONFIG_SER_STROBE) || !CONFIG_SER_STROBE
300 #define SER_STROBE_ON do {/*nop*/} while(0)
301 #define SER_STROBE_OFF do {/*nop*/} while(0)
302 #define SER_STROBE_INIT do {/*nop*/} while(0)
306 /* From the high-level serial driver */
307 extern struct Serial *ser_handles[SER_CNT];
309 /* TX and RX buffers */
310 static unsigned char uart0_txbuffer[CONFIG_UART0_TXBUFSIZE];
311 static unsigned char uart0_rxbuffer[CONFIG_UART0_RXBUFSIZE];
313 static unsigned char uart1_txbuffer[CONFIG_UART1_TXBUFSIZE];
314 static unsigned char uart1_rxbuffer[CONFIG_UART1_RXBUFSIZE];
316 static unsigned char spi_txbuffer[CONFIG_SPI_TXBUFSIZE];
317 static unsigned char spi_rxbuffer[CONFIG_SPI_RXBUFSIZE];
321 * Internal hardware state structure
323 * The \a sending variable is true while the transmission
324 * interrupt is retriggering itself.
326 * For the USARTs the \a sending flag is useful for taking specific
327 * actions before sending a burst of data, at the start of a trasmission
328 * but not before every char sent.
330 * For the SPI, this flag is necessary because the SPI sends and receives
331 * bytes at the same time and the SPI IRQ is unique for send/receive.
332 * The only way to start transmission is to write data in SPDR (this
333 * is done by spi_starttx()). We do this *only* if a transfer is
334 * not already started.
338 struct SerialHardware hw;
339 volatile bool sending;
347 static void uart0_init(
348 UNUSED_ARG(struct SerialHardware *, _hw),
349 UNUSED_ARG(struct Serial *, ser))
351 SER_UART0_BUS_TXINIT;
356 static void uart0_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
361 static void uart0_enabletxirq(struct SerialHardware *_hw)
363 struct AvrSerial *hw = (struct AvrSerial *)_hw;
366 * WARNING: racy code here! The tx interrupt sets hw->sending to false
367 * when it runs with an empty fifo. The order of statements in the
373 SER_UART0_BUS_TXBEGIN;
377 static void uart0_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
379 /* Compute baud-rate period */
380 uint16_t period = DIV_ROUND(CPU_FREQ / 16UL, rate) - 1;
382 #if !CPU_AVR_ATMEGA103
383 UBRR0H = (period) >> 8;
387 //DB(kprintf("uart0_setbaudrate(rate=%lu): period=%d\n", rate, period);)
390 static void uart0_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
392 #if !CPU_AVR_ATMEGA103
393 UCSR0C = (UCSR0C & ~(BV(UPM01) | BV(UPM00))) | ((parity) << UPM00);
399 static void uart1_init(
400 UNUSED_ARG(struct SerialHardware *, _hw),
401 UNUSED_ARG(struct Serial *, ser))
403 SER_UART1_BUS_TXINIT;
408 static void uart1_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
413 static void uart1_enabletxirq(struct SerialHardware *_hw)
415 struct AvrSerial *hw = (struct AvrSerial *)_hw;
418 * WARNING: racy code here! The tx interrupt
419 * sets hw->sending to false when it runs with
420 * an empty fifo. The order of the statements
421 * in the if-block matters.
426 SER_UART1_BUS_TXBEGIN;
430 static void uart1_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
432 /* Compute baud-rate period */
433 uint16_t period = DIV_ROUND(CPU_FREQ / 16UL, rate) - 1;
435 UBRR1H = (period) >> 8;
438 //DB(kprintf("uart1_setbaudrate(rate=%ld): period=%d\n", rate, period);)
441 static void uart1_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
443 UCSR1C = (UCSR1C & ~(BV(UPM11) | BV(UPM10))) | ((parity) << UPM10);
446 #endif // AVR_HAS_UART1
448 static void spi_init(UNUSED_ARG(struct SerialHardware *, _hw), UNUSED_ARG(struct Serial *, ser))
451 * Set MOSI and SCK ports out, MISO in.
453 * The ATmega64/128 datasheet explicitly states that the input/output
454 * state of the SPI pins is not significant, as when the SPI is
455 * active the I/O port are overrided.
456 * This is *blatantly FALSE*.
458 * Moreover, the MISO pin on the board_kc *must* be in high impedance
459 * state even when the SPI is off, because the line is wired together
460 * with the KBus serial RX, and the transmitter of the slave boards
461 * would be unable to drive the line.
463 ATOMIC(SPI_DDR |= (BV(SPI_MOSI_BIT) | BV(SPI_SCK_BIT)));
466 * If the SPI master mode is activated and the SS pin is in input and tied low,
467 * the SPI hardware will automatically switch to slave mode!
468 * For proper communication this pins should therefore be:
470 * - as input but tied high forever!
471 * This driver set the pin as output.
473 #warning SPI SS pin set as output for proper operation, check schematics for possible conflicts.
474 ATOMIC(SPI_DDR |= BV(SPI_SS_BIT));
476 ATOMIC(SPI_DDR &= ~BV(SPI_MISO_BIT));
477 /* Enable SPI, IRQ on, Master */
478 SPCR = BV(SPE) | BV(SPIE) | BV(MSTR);
481 #if CONFIG_SPI_DATA_ORDER == SER_LSB_FIRST
485 /* Set SPI clock rate */
486 #if CONFIG_SPI_CLOCK_DIV == 128
487 SPCR |= (BV(SPR1) | BV(SPR0));
488 #elif (CONFIG_SPI_CLOCK_DIV == 64 || CONFIG_SPI_CLOCK_DIV == 32)
490 #elif (CONFIG_SPI_CLOCK_DIV == 16 || CONFIG_SPI_CLOCK_DIV == 8)
492 #elif (CONFIG_SPI_CLOCK_DIV == 4 || CONFIG_SPI_CLOCK_DIV == 2)
493 // SPR0 & SDPR1 both at 0
495 #error Unsupported SPI clock division factor.
498 /* Set SPI2X bit (spi double frequency) */
499 #if (CONFIG_SPI_CLOCK_DIV == 128 || CONFIG_SPI_CLOCK_DIV == 64 \
500 || CONFIG_SPI_CLOCK_DIV == 16 || CONFIG_SPI_CLOCK_DIV == 4)
502 #elif (CONFIG_SPI_CLOCK_DIV == 32 || CONFIG_SPI_CLOCK_DIV == 8 || CONFIG_SPI_CLOCK_DIV == 2)
505 #error Unsupported SPI clock division factor.
508 /* Set clock polarity */
509 #if CONFIG_SPI_CLOCK_POL == 1
513 /* Set clock phase */
514 #if CONFIG_SPI_CLOCK_PHASE == 1
522 static void spi_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
528 /* Set all pins as inputs */
529 ATOMIC(SPI_DDR &= ~(BV(SPI_MISO_BIT) | BV(SPI_MOSI_BIT) | BV(SPI_SCK_BIT) | BV(SPI_SS_BIT)));
532 static void spi_starttx(struct SerialHardware *_hw)
534 struct AvrSerial *hw = (struct AvrSerial *)_hw;
537 IRQ_SAVE_DISABLE(flags);
539 /* Send data only if the SPI is not already transmitting */
540 if (!hw->sending && !fifo_isempty(&ser_handles[SER_SPI]->txfifo))
543 SPDR = fifo_pop(&ser_handles[SER_SPI]->txfifo);
549 static void spi_setbaudrate(
550 UNUSED_ARG(struct SerialHardware *, _hw),
551 UNUSED_ARG(unsigned long, rate))
556 static void spi_setparity(UNUSED_ARG(struct SerialHardware *, _hw), UNUSED_ARG(int, parity))
561 static bool tx_sending(struct SerialHardware* _hw)
563 struct AvrSerial *hw = (struct AvrSerial *)_hw;
569 // FIXME: move into compiler.h? Ditch?
571 #define C99INIT(name,val) .name = val
572 #elif defined(__GNUC__)
573 #define C99INIT(name,val) name: val
575 #warning No designated initializers, double check your code
576 #define C99INIT(name,val) (val)
580 * High-level interface data structures
582 static const struct SerialHardwareVT UART0_VT =
584 C99INIT(init, uart0_init),
585 C99INIT(cleanup, uart0_cleanup),
586 C99INIT(setBaudrate, uart0_setbaudrate),
587 C99INIT(setParity, uart0_setparity),
588 C99INIT(txStart, uart0_enabletxirq),
589 C99INIT(txSending, tx_sending),
593 static const struct SerialHardwareVT UART1_VT =
595 C99INIT(init, uart1_init),
596 C99INIT(cleanup, uart1_cleanup),
597 C99INIT(setBaudrate, uart1_setbaudrate),
598 C99INIT(setParity, uart1_setparity),
599 C99INIT(txStart, uart1_enabletxirq),
600 C99INIT(txSending, tx_sending),
602 #endif // AVR_HAS_UART1
604 static const struct SerialHardwareVT SPI_VT =
606 C99INIT(init, spi_init),
607 C99INIT(cleanup, spi_cleanup),
608 C99INIT(setBaudrate, spi_setbaudrate),
609 C99INIT(setParity, spi_setparity),
610 C99INIT(txStart, spi_starttx),
611 C99INIT(txSending, tx_sending),
614 static struct AvrSerial UARTDescs[SER_CNT] =
618 C99INIT(table, &UART0_VT),
619 C99INIT(txbuffer, uart0_txbuffer),
620 C99INIT(rxbuffer, uart0_rxbuffer),
621 C99INIT(txbuffer_size, sizeof(uart0_txbuffer)),
622 C99INIT(rxbuffer_size, sizeof(uart0_rxbuffer)),
624 C99INIT(sending, false),
629 C99INIT(table, &UART1_VT),
630 C99INIT(txbuffer, uart1_txbuffer),
631 C99INIT(rxbuffer, uart1_rxbuffer),
632 C99INIT(txbuffer_size, sizeof(uart1_txbuffer)),
633 C99INIT(rxbuffer_size, sizeof(uart1_rxbuffer)),
635 C99INIT(sending, false),
640 C99INIT(table, &SPI_VT),
641 C99INIT(txbuffer, spi_txbuffer),
642 C99INIT(rxbuffer, spi_rxbuffer),
643 C99INIT(txbuffer_size, sizeof(spi_txbuffer)),
644 C99INIT(rxbuffer_size, sizeof(spi_rxbuffer)),
646 C99INIT(sending, false),
650 struct SerialHardware *ser_hw_getdesc(int unit)
652 ASSERT(unit < SER_CNT);
653 return &UARTDescs[unit].hw;
661 #if CONFIG_SER_HWHANDSHAKE
663 /// This interrupt is triggered when the CTS line goes high
666 // Re-enable UDR empty interrupt and TX, then disable CTS interrupt
667 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_UDRIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0);
668 EIMSK &= ~EIMSKF_CTS;
671 #endif // CONFIG_SER_HWHANDSHAKE
675 * Serial 0 TX interrupt handler
677 SIGNAL(USART0_UDRE_vect)
681 struct FIFOBuffer * const txfifo = &ser_handles[SER_UART0]->txfifo;
683 if (fifo_isempty(txfifo))
686 #ifndef SER_UART0_BUS_TXOFF
687 UARTDescs[SER_UART0].sending = false;
690 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA103
693 // Disable rx interrupt and tx, enable CTS interrupt
695 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0);
702 char c = fifo_pop(txfifo);
703 SER_UART0_BUS_TXCHAR(c);
709 #ifdef SER_UART0_BUS_TXOFF
711 * Serial port 0 TX complete interrupt handler.
713 * This IRQ is usually disabled. The UDR-empty interrupt
714 * enables it when there's no more data to transmit.
715 * We need to wait until the last character has been
716 * transmitted before switching the 485 transceiver to
719 * The txfifo might have been refilled by putchar() while
720 * we were waiting for the transmission complete interrupt.
721 * In this case, we must restart the UDR empty interrupt,
722 * otherwise we'd stop the serial port with some data
723 * still pending in the buffer.
725 SIGNAL(SIG_UART0_TRANS)
729 struct FIFOBuffer * const txfifo = &ser_handles[SER_UART0]->txfifo;
730 if (fifo_isempty(txfifo))
733 UARTDescs[SER_UART0].sending = false;
736 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_UDRIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0);
740 #endif /* SER_UART0_BUS_TXOFF */
746 * Serial 1 TX interrupt handler
748 SIGNAL(USART1_UDRE_vect)
752 struct FIFOBuffer * const txfifo = &ser_handles[SER_UART1]->txfifo;
754 if (fifo_isempty(txfifo))
757 #ifndef SER_UART1_BUS_TXOFF
758 UARTDescs[SER_UART1].sending = false;
761 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA103
764 // Disable rx interrupt and tx, enable CTS interrupt
766 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1);
773 char c = fifo_pop(txfifo);
774 SER_UART1_BUS_TXCHAR(c);
780 #ifdef SER_UART1_BUS_TXOFF
782 * Serial port 1 TX complete interrupt handler.
784 * \sa port 0 TX complete handler.
786 SIGNAL(USART1_TX_vect)
790 struct FIFOBuffer * const txfifo = &ser_handles[SER_UART1]->txfifo;
791 if (fifo_isempty(txfifo))
794 UARTDescs[SER_UART1].sending = false;
797 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_UDRIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1);
801 #endif /* SER_UART1_BUS_TXOFF */
803 #endif // AVR_HAS_UART1
807 * Serial 0 RX complete interrupt handler.
809 * This handler is interruptible.
810 * Interrupt are reenabled as soon as recv complete interrupt is
811 * disabled. Using INTERRUPT() is troublesome when the serial
812 * is heavily loaded, because an interrupt could be retriggered
813 * when executing the handler prologue before RXCIE is disabled.
815 * \note The code that re-enables interrupts is commented out
816 * because in some nasty cases the interrupt is retriggered.
817 * This is probably due to the RXC flag being set before
818 * RXCIE is cleared. Unfortunately the RXC flag is read-only
819 * and can't be cleared by code.
821 SIGNAL(USART0_RX_vect)
825 /* Disable Recv complete IRQ */
826 //UCSR0B &= ~BV(RXCIE);
829 /* Should be read before UDR */
830 ser_handles[SER_UART0]->status |= UCSR0A & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
832 /* To clear the RXC flag we must _always_ read the UDR even when we're
833 * not going to accept the incoming data, otherwise a new interrupt
834 * will occur once the handler terminates.
837 struct FIFOBuffer * const rxfifo = &ser_handles[SER_UART0]->rxfifo;
839 if (fifo_isfull(rxfifo))
840 ser_handles[SER_UART0]->status |= SERRF_RXFIFOOVERRUN;
843 fifo_push(rxfifo, c);
844 #if CONFIG_SER_HWHANDSHAKE
845 if (fifo_isfull(rxfifo))
850 /* Reenable receive complete int */
852 //UCSR0B |= BV(RXCIE);
861 * Serial 1 RX complete interrupt handler.
863 * This handler is interruptible.
864 * Interrupt are reenabled as soon as recv complete interrupt is
865 * disabled. Using INTERRUPT() is troublesome when the serial
866 * is heavily loaded, because an interrupt could be retriggered
867 * when executing the handler prologue before RXCIE is disabled.
869 * \see SIGNAL(USART1_RX_vect)
871 SIGNAL(USART1_RX_vect)
875 /* Disable Recv complete IRQ */
876 //UCSR1B &= ~BV(RXCIE);
879 /* Should be read before UDR */
880 ser_handles[SER_UART1]->status |= UCSR1A & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
882 /* To avoid an IRQ storm, we must _always_ read the UDR even when we're
883 * not going to accept the incoming data
886 struct FIFOBuffer * const rxfifo = &ser_handles[SER_UART1]->rxfifo;
887 //ASSERT_VALID_FIFO(rxfifo);
889 if (UNLIKELY(fifo_isfull(rxfifo)))
890 ser_handles[SER_UART1]->status |= SERRF_RXFIFOOVERRUN;
893 fifo_push(rxfifo, c);
894 #if CONFIG_SER_HWHANDSHAKE
895 if (fifo_isfull(rxfifo))
899 /* Re-enable receive complete int */
901 //UCSR1B |= BV(RXCIE);
906 #endif // AVR_HAS_UART1
910 * SPI interrupt handler
916 /* Read incoming byte. */
917 if (!fifo_isfull(&ser_handles[SER_SPI]->rxfifo))
918 fifo_push(&ser_handles[SER_SPI]->rxfifo, SPDR);
922 ser_handles[SER_SPI]->status |= SERRF_RXFIFOOVERRUN;
926 if (!fifo_isempty(&ser_handles[SER_SPI]->txfifo))
927 SPDR = fifo_pop(&ser_handles[SER_SPI]->txfifo);
929 UARTDescs[SER_SPI].sending = false;