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, 2010 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)
36 * \author Bernie Innocenti <bernie@codewiz.org>
37 * \author Stefano Fedrigo <aleph@develer.com>
38 * \author Luca Ottaviano <lottaviano@develer.com>
41 #include "hw/hw_ser.h" /* Required for bus macros overrides */
42 #include <hw/hw_cpufreq.h> /* CPU_FREQ */
44 #include "cfg/cfg_ser.h"
46 #include <cfg/macros.h> /* DIV_ROUND */
47 #include <cfg/debug.h>
48 #include <cfg/cfg_arch.h> // ARCH_NIGHTTEST
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 || CPU_AVR_ATMEGA1280
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
87 #if CPU_AVR_ATMEGA1280
88 #define BIT_RXCIE2 RXCIE2
89 #define BIT_RXEN2 RXEN2
90 #define BIT_TXEN2 TXEN2
91 #define BIT_UDRIE2 UDRIE2
93 #define BIT_RXCIE3 RXCIE3
94 #define BIT_RXEN3 RXEN3
95 #define BIT_TXEN3 TXEN3
96 #define BIT_UDRIE3 UDRIE3
98 #elif CPU_AVR_ATMEGA168 || CPU_AVR_ATMEGA328P
99 #define BIT_RXCIE0 RXCIE0
100 #define BIT_RXEN0 RXEN0
101 #define BIT_TXEN0 TXEN0
102 #define BIT_UDRIE0 UDRIE0
104 #define BIT_RXCIE1 RXCIE0
105 #define BIT_RXEN1 RXEN0
106 #define BIT_TXEN1 TXEN0
107 #define BIT_UDRIE1 UDRIE0
109 #define BIT_RXCIE0 RXCIE
110 #define BIT_RXEN0 RXEN
111 #define BIT_TXEN0 TXEN
112 #define BIT_UDRIE0 UDRIE
114 #define BIT_RXCIE1 RXCIE
115 #define BIT_RXEN1 RXEN
116 #define BIT_TXEN1 TXEN
117 #define BIT_UDRIE1 UDRIE
122 * \name Overridable serial bus hooks
124 * These can be redefined in hw.h to implement
125 * special bus policies such as half-duplex, 485, etc.
129 * TXBEGIN TXCHAR TXEND TXOFF
130 * | __________|__________ | |
133 * ______ __ __ __ __ __ __ ________________
134 * \/ \/ \/ \/ \/ \/ \/
135 * ______/\__/\__/\__/\__/\__/\__/
141 #ifndef SER_UART0_BUS_TXINIT
143 * Default TXINIT macro - invoked in uart0_init()
145 * - Enable both the receiver and the transmitter
146 * - Enable only the RX complete interrupt
148 #define SER_UART0_BUS_TXINIT do { \
149 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0); \
153 #ifndef SER_UART0_BUS_TXBEGIN
155 * Invoked before starting a transmission
157 * - Enable both the receiver and the transmitter
158 * - Enable both the RX complete and UDR empty interrupts
160 #define SER_UART0_BUS_TXBEGIN do { \
161 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_UDRIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0); \
165 #ifndef SER_UART0_BUS_TXCHAR
167 * Invoked to send one character.
169 #define SER_UART0_BUS_TXCHAR(c) do { \
174 #ifndef SER_UART0_BUS_TXEND
176 * Invoked as soon as the txfifo becomes empty
178 * - Keep both the receiver and the transmitter enabled
179 * - Keep the RX complete interrupt enabled
180 * - Disable the UDR empty interrupt
182 #define SER_UART0_BUS_TXEND do { \
183 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0); \
187 #ifndef SER_UART0_BUS_TXOFF
189 * \def SER_UART0_BUS_TXOFF
191 * Invoked after the last character has been transmitted
193 * The default is no action.
196 #define SER_UART0_BUS_TXOFF
200 #ifndef SER_UART1_BUS_TXINIT
201 /** \sa SER_UART0_BUS_TXINIT */
202 #define SER_UART1_BUS_TXINIT do { \
203 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1); \
206 #ifndef SER_UART1_BUS_TXBEGIN
207 /** \sa SER_UART0_BUS_TXBEGIN */
208 #define SER_UART1_BUS_TXBEGIN do { \
209 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_UDRIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1); \
212 #ifndef SER_UART1_BUS_TXCHAR
213 /** \sa SER_UART0_BUS_TXCHAR */
214 #define SER_UART1_BUS_TXCHAR(c) do { \
218 #ifndef SER_UART1_BUS_TXEND
219 /** \sa SER_UART0_BUS_TXEND */
220 #define SER_UART1_BUS_TXEND do { \
221 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1); \
224 #ifndef SER_UART1_BUS_TXOFF
226 * \def SER_UART1_BUS_TXOFF
228 * \see SER_UART0_BUS_TXOFF
231 #define SER_UART1_BUS_TXOFF
235 #ifndef SER_UART2_BUS_TXINIT
236 /** \sa SER_UART0_BUS_TXINIT */
237 #define SER_UART2_BUS_TXINIT do { \
238 UCSR2B = BV(BIT_RXCIE2) | BV(BIT_RXEN2) | BV(BIT_TXEN2); \
241 #ifndef SER_UART2_BUS_TXBEGIN
242 /** \sa SER_UART0_BUS_TXBEGIN */
243 #define SER_UART2_BUS_TXBEGIN do { \
244 UCSR2B = BV(BIT_RXCIE2) | BV(BIT_UDRIE2) | BV(BIT_RXEN2) | BV(BIT_TXEN2); \
247 #ifndef SER_UART2_BUS_TXCHAR
248 /** \sa SER_UART0_BUS_TXCHAR */
249 #define SER_UART2_BUS_TXCHAR(c) do { \
253 #ifndef SER_UART2_BUS_TXEND
254 /** \sa SER_UART0_BUS_TXEND */
255 #define SER_UART2_BUS_TXEND do { \
256 UCSR2B = BV(BIT_RXCIE2) | BV(BIT_RXEN2) | BV(BIT_TXEN2); \
259 #ifndef SER_UART2_BUS_TXOFF
261 * \def SER_UART2_BUS_TXOFF
263 * \see SER_UART0_BUS_TXOFF
266 #define SER_UART2_BUS_TXOFF
270 #ifndef SER_UART3_BUS_TXINIT
271 /** \sa SER_UART0_BUS_TXINIT */
272 #define SER_UART3_BUS_TXINIT do { \
273 UCSR3B = BV(BIT_RXCIE3) | BV(BIT_RXEN3) | BV(BIT_TXEN3); \
276 #ifndef SER_UART3_BUS_TXBEGIN
277 /** \sa SER_UART0_BUS_TXBEGIN */
278 #define SER_UART3_BUS_TXBEGIN do { \
279 UCSR3B = BV(BIT_RXCIE3) | BV(BIT_UDRIE3) | BV(BIT_RXEN3) | BV(BIT_TXEN3); \
282 #ifndef SER_UART3_BUS_TXCHAR
283 /** \sa SER_UART0_BUS_TXCHAR */
284 #define SER_UART3_BUS_TXCHAR(c) do { \
288 #ifndef SER_UART3_BUS_TXEND
289 /** \sa SER_UART0_BUS_TXEND */
290 #define SER_UART3_BUS_TXEND do { \
291 UCSR3B = BV(BIT_RXCIE3) | BV(BIT_RXEN3) | BV(BIT_TXEN3); \
294 #ifndef SER_UART3_BUS_TXOFF
296 * \def SER_UART3_BUS_TXOFF
298 * \see SER_UART0_BUS_TXOFF
301 #define SER_UART3_BUS_TXOFF
308 * \name Overridable SPI hooks
310 * These can be redefined in hw.h to implement
311 * special bus policies such as slave select pin handling, etc.
315 #ifndef SER_SPI_BUS_TXINIT
317 * Default TXINIT macro - invoked in spi_init()
318 * The default is no action.
320 #define SER_SPI_BUS_TXINIT
323 #ifndef SER_SPI_BUS_TXCLOSE
325 * Invoked after the last character has been transmitted.
326 * The default is no action.
328 #define SER_SPI_BUS_TXCLOSE
333 /* SPI port and pin configuration */
334 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA103 || CPU_AVR_ATMEGA1281 \
335 || CPU_AVR_ATMEGA1280
336 #define SPI_PORT PORTB
338 #define SPI_SS_BIT PB0
339 #define SPI_SCK_BIT PB1
340 #define SPI_MOSI_BIT PB2
341 #define SPI_MISO_BIT PB3
342 // TODO: these bits are the same as ATMEGA8 but the defines in avr-gcc are different.
343 // They should be the same!
344 #elif CPU_AVR_ATMEGA328P
345 #define SPI_PORT PORTB
347 #define SPI_SS_BIT PORTB2
348 #define SPI_SCK_BIT PORTB5
349 #define SPI_MOSI_BIT PORTB3
350 #define SPI_MISO_BIT PORTB4
351 #elif CPU_AVR_ATMEGA8 || CPU_AVR_ATMEGA168
352 #define SPI_PORT PORTB
354 #define SPI_SS_BIT PB2
355 #define SPI_SCK_BIT PB5
356 #define SPI_MOSI_BIT PB3
357 #define SPI_MISO_BIT PB4
359 #error Unknown architecture
362 /* USART register definitions */
363 #if CPU_AVR_ATMEGA1280
364 #define AVR_HAS_UART1 1
365 #define AVR_HAS_UART2 1
366 #define AVR_HAS_UART3 1
367 #elif CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA1281
368 #define AVR_HAS_UART1 1
369 #define AVR_HAS_UART2 0
370 #define AVR_HAS_UART3 0
371 #elif CPU_AVR_ATMEGA168 || CPU_AVR_ATMEGA328P
372 #define AVR_HAS_UART1 0
373 #define AVR_HAS_UART2 0
374 #define AVR_HAS_UART3 0
375 #define USART0_UDRE_vect USART_UDRE_vect
376 #define USART0_RX_vect USART_RX_vect
377 #define USART0_TX_vect USART_TX_vect
378 #elif CPU_AVR_ATMEGA8
379 #define AVR_HAS_UART1 0
380 #define AVR_HAS_UART2 0
381 #define AVR_HAS_UART3 0
388 #define USART0_UDRE_vect USART_UDRE_vect
389 #define USART0_RX_vect USART_RX_vect
390 #define USART0_TX_vect USART_TX_vect
391 #elif CPU_AVR_ATMEGA103
392 #define AVR_HAS_UART1 0
393 #define AVR_HAS_UART2 0
394 #define AVR_HAS_UART3 0
399 #define USART0_UDRE_vect USART_UDRE_vect
400 #define USART0_RX_vect USART_RX_vect
401 #define USART0_TX_vect USART_TX_vect
403 #error Unknown architecture
407 /* From the high-level serial driver */
408 extern struct Serial *ser_handles[SER_CNT];
410 /* TX and RX buffers */
411 static unsigned char uart0_txbuffer[CONFIG_UART0_TXBUFSIZE];
412 static unsigned char uart0_rxbuffer[CONFIG_UART0_RXBUFSIZE];
414 static unsigned char uart1_txbuffer[CONFIG_UART1_TXBUFSIZE];
415 static unsigned char uart1_rxbuffer[CONFIG_UART1_RXBUFSIZE];
418 static unsigned char uart2_txbuffer[CONFIG_UART2_TXBUFSIZE];
419 static unsigned char uart2_rxbuffer[CONFIG_UART2_RXBUFSIZE];
422 static unsigned char uart3_txbuffer[CONFIG_UART3_TXBUFSIZE];
423 static unsigned char uart3_rxbuffer[CONFIG_UART3_RXBUFSIZE];
425 static unsigned char spi_txbuffer[CONFIG_SPI_TXBUFSIZE];
426 static unsigned char spi_rxbuffer[CONFIG_SPI_RXBUFSIZE];
430 * Internal hardware state structure
432 * The \a sending variable is true while the transmission
433 * interrupt is retriggering itself.
435 * For the USARTs the \a sending flag is useful for taking specific
436 * actions before sending a burst of data, at the start of a trasmission
437 * but not before every char sent.
439 * For the SPI, this flag is necessary because the SPI sends and receives
440 * bytes at the same time and the SPI IRQ is unique for send/receive.
441 * The only way to start transmission is to write data in SPDR (this
442 * is done by spi_starttx()). We do this *only* if a transfer is
443 * not already started.
447 struct SerialHardware hw;
448 volatile bool sending;
456 static void uart0_init(
457 UNUSED_ARG(struct SerialHardware *, _hw),
458 UNUSED_ARG(struct Serial *, ser))
460 SER_UART0_BUS_TXINIT;
465 static void uart0_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
470 static void uart0_enabletxirq(struct SerialHardware *_hw)
472 struct AvrSerial *hw = (struct AvrSerial *)_hw;
475 * WARNING: racy code here! The tx interrupt sets hw->sending to false
476 * when it runs with an empty fifo. The order of statements in the
482 SER_UART0_BUS_TXBEGIN;
486 static void uart0_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
488 /* Compute baud-rate period */
489 uint16_t period = DIV_ROUND(CPU_FREQ / 16UL, rate) - 1;
491 #if !CPU_AVR_ATMEGA103
492 UBRR0H = (period) >> 8;
496 //DB(kprintf("uart0_setbaudrate(rate=%lu): period=%d\n", rate, period);)
499 static void uart0_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
501 #if !CPU_AVR_ATMEGA103
502 UCSR0C = (UCSR0C & ~(BV(UPM01) | BV(UPM00))) | ((parity) << UPM00);
508 static void uart1_init(
509 UNUSED_ARG(struct SerialHardware *, _hw),
510 UNUSED_ARG(struct Serial *, ser))
512 SER_UART1_BUS_TXINIT;
517 static void uart1_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
522 static void uart1_enabletxirq(struct SerialHardware *_hw)
524 struct AvrSerial *hw = (struct AvrSerial *)_hw;
527 * WARNING: racy code here! The tx interrupt
528 * sets hw->sending to false when it runs with
529 * an empty fifo. The order of the statements
530 * in the if-block matters.
535 SER_UART1_BUS_TXBEGIN;
539 static void uart1_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
541 /* Compute baud-rate period */
542 uint16_t period = DIV_ROUND(CPU_FREQ / 16UL, rate) - 1;
544 UBRR1H = (period) >> 8;
547 //DB(kprintf("uart1_setbaudrate(rate=%ld): period=%d\n", rate, period);)
550 static void uart1_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
552 UCSR1C = (UCSR1C & ~(BV(UPM11) | BV(UPM10))) | ((parity) << UPM10);
555 #endif // AVR_HAS_UART1
559 static void uart2_init(
560 UNUSED_ARG(struct SerialHardware *, _hw),
561 UNUSED_ARG(struct Serial *, ser))
563 SER_UART2_BUS_TXINIT;
568 static void uart2_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
573 static void uart2_enabletxirq(struct SerialHardware *_hw)
575 struct AvrSerial *hw = (struct AvrSerial *)_hw;
578 * WARNING: racy code here! The tx interrupt
579 * sets hw->sending to false when it runs with
580 * an empty fifo. The order of the statements
581 * in the if-block matters.
586 SER_UART2_BUS_TXBEGIN;
590 static void uart2_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
592 /* Compute baud-rate period */
593 uint16_t period = DIV_ROUND(CPU_FREQ / 16UL, rate) - 1;
595 UBRR2H = (period) >> 8;
598 //DB(kprintf("uart2_setbaudrate(rate=%ld): period=%d\n", rate, period);)
601 static void uart2_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
603 UCSR2C = (UCSR2C & ~(BV(UPM21) | BV(UPM20))) | ((parity) << UPM20);
606 #endif // AVR_HAS_UART2
610 static void uart3_init(
611 UNUSED_ARG(struct SerialHardware *, _hw),
612 UNUSED_ARG(struct Serial *, ser))
614 SER_UART3_BUS_TXINIT;
619 static void uart3_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
624 static void uart3_enabletxirq(struct SerialHardware *_hw)
626 struct AvrSerial *hw = (struct AvrSerial *)_hw;
629 * WARNING: racy code here! The tx interrupt
630 * sets hw->sending to false when it runs with
631 * an empty fifo. The order of the statements
632 * in the if-block matters.
637 SER_UART3_BUS_TXBEGIN;
641 static void uart3_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
643 /* Compute baud-rate period */
644 uint16_t period = DIV_ROUND(CPU_FREQ / 16UL, rate) - 1;
646 UBRR3H = (period) >> 8;
649 //DB(kprintf("uart3_setbaudrate(rate=%ld): period=%d\n", rate, period);)
652 static void uart3_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
654 UCSR3C = (UCSR3C & ~(BV(UPM31) | BV(UPM30))) | ((parity) << UPM30);
657 #endif // AVR_HAS_UART3
660 static void spi_init(UNUSED_ARG(struct SerialHardware *, _hw), UNUSED_ARG(struct Serial *, ser))
663 * Set MOSI and SCK ports out, MISO in.
665 * The ATmega64/128 datasheet explicitly states that the input/output
666 * state of the SPI pins is not significant, as when the SPI is
667 * active the I/O port are overrided.
668 * This is *blatantly FALSE*.
670 * Moreover, the MISO pin on the board_kc *must* be in high impedance
671 * state even when the SPI is off, because the line is wired together
672 * with the KBus serial RX, and the transmitter of the slave boards
673 * would be unable to drive the line.
675 ATOMIC(SPI_DDR |= (BV(SPI_MOSI_BIT) | BV(SPI_SCK_BIT)));
678 * If the SPI master mode is activated and the SS pin is in input and tied low,
679 * the SPI hardware will automatically switch to slave mode!
680 * For proper communication this pins should therefore be:
682 * - as input but tied high forever!
683 * This driver set the pin as output.
685 #warning FIXME:SPI SS pin set as output for proper operation, check schematics for possible conflicts.
686 ATOMIC(SPI_DDR |= BV(SPI_SS_BIT));
688 ATOMIC(SPI_DDR &= ~BV(SPI_MISO_BIT));
689 /* Enable SPI, IRQ on, Master */
690 SPCR = BV(SPE) | BV(SPIE) | BV(MSTR);
693 #if CONFIG_SPI_DATA_ORDER == SER_LSB_FIRST
697 /* Set SPI clock rate */
698 #if CONFIG_SPI_CLOCK_DIV == 128
699 SPCR |= (BV(SPR1) | BV(SPR0));
700 #elif (CONFIG_SPI_CLOCK_DIV == 64 || CONFIG_SPI_CLOCK_DIV == 32)
702 #elif (CONFIG_SPI_CLOCK_DIV == 16 || CONFIG_SPI_CLOCK_DIV == 8)
704 #elif (CONFIG_SPI_CLOCK_DIV == 4 || CONFIG_SPI_CLOCK_DIV == 2)
705 // SPR0 & SDPR1 both at 0
707 #error Unsupported SPI clock division factor.
710 /* Set SPI2X bit (spi double frequency) */
711 #if (CONFIG_SPI_CLOCK_DIV == 128 || CONFIG_SPI_CLOCK_DIV == 64 \
712 || CONFIG_SPI_CLOCK_DIV == 16 || CONFIG_SPI_CLOCK_DIV == 4)
714 #elif (CONFIG_SPI_CLOCK_DIV == 32 || CONFIG_SPI_CLOCK_DIV == 8 || CONFIG_SPI_CLOCK_DIV == 2)
717 #error Unsupported SPI clock division factor.
720 /* Set clock polarity */
721 #if CONFIG_SPI_CLOCK_POL == 1
725 /* Set clock phase */
726 #if CONFIG_SPI_CLOCK_PHASE == 1
734 static void spi_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
740 /* Set all pins as inputs */
741 ATOMIC(SPI_DDR &= ~(BV(SPI_MISO_BIT) | BV(SPI_MOSI_BIT) | BV(SPI_SCK_BIT) | BV(SPI_SS_BIT)));
744 static void spi_starttx(struct SerialHardware *_hw)
746 struct AvrSerial *hw = (struct AvrSerial *)_hw;
749 IRQ_SAVE_DISABLE(flags);
751 /* Send data only if the SPI is not already transmitting */
752 if (!hw->sending && !fifo_isempty(&ser_handles[SER_SPI]->txfifo))
755 SPDR = fifo_pop(&ser_handles[SER_SPI]->txfifo);
761 static void spi_setbaudrate(
762 UNUSED_ARG(struct SerialHardware *, _hw),
763 UNUSED_ARG(unsigned long, rate))
768 static void spi_setparity(UNUSED_ARG(struct SerialHardware *, _hw), UNUSED_ARG(int, parity))
773 static bool tx_sending(struct SerialHardware* _hw)
775 struct AvrSerial *hw = (struct AvrSerial *)_hw;
781 // FIXME: move into compiler.h? Ditch?
783 #define C99INIT(name,val) .name = val
784 #elif defined(__GNUC__)
785 #define C99INIT(name,val) name: val
787 #warning No designated initializers, double check your code
788 #define C99INIT(name,val) (val)
792 * High-level interface data structures
794 static const struct SerialHardwareVT UART0_VT =
796 C99INIT(init, uart0_init),
797 C99INIT(cleanup, uart0_cleanup),
798 C99INIT(setBaudrate, uart0_setbaudrate),
799 C99INIT(setParity, uart0_setparity),
800 C99INIT(txStart, uart0_enabletxirq),
801 C99INIT(txSending, tx_sending),
805 static const struct SerialHardwareVT UART1_VT =
807 C99INIT(init, uart1_init),
808 C99INIT(cleanup, uart1_cleanup),
809 C99INIT(setBaudrate, uart1_setbaudrate),
810 C99INIT(setParity, uart1_setparity),
811 C99INIT(txStart, uart1_enabletxirq),
812 C99INIT(txSending, tx_sending),
814 #endif // AVR_HAS_UART1
817 static const struct SerialHardwareVT UART2_VT =
819 C99INIT(init, uart2_init),
820 C99INIT(cleanup, uart2_cleanup),
821 C99INIT(setBaudrate, uart2_setbaudrate),
822 C99INIT(setParity, uart2_setparity),
823 C99INIT(txStart, uart2_enabletxirq),
824 C99INIT(txSending, tx_sending),
826 #endif // AVR_HAS_UART2
829 static const struct SerialHardwareVT UART3_VT =
831 C99INIT(init, uart3_init),
832 C99INIT(cleanup, uart3_cleanup),
833 C99INIT(setBaudrate, uart3_setbaudrate),
834 C99INIT(setParity, uart3_setparity),
835 C99INIT(txStart, uart3_enabletxirq),
836 C99INIT(txSending, tx_sending),
838 #endif // AVR_HAS_UART3
840 static const struct SerialHardwareVT SPI_VT =
842 C99INIT(init, spi_init),
843 C99INIT(cleanup, spi_cleanup),
844 C99INIT(setBaudrate, spi_setbaudrate),
845 C99INIT(setParity, spi_setparity),
846 C99INIT(txStart, spi_starttx),
847 C99INIT(txSending, tx_sending),
850 static struct AvrSerial UARTDescs[SER_CNT] =
854 C99INIT(table, &UART0_VT),
855 C99INIT(txbuffer, uart0_txbuffer),
856 C99INIT(rxbuffer, uart0_rxbuffer),
857 C99INIT(txbuffer_size, sizeof(uart0_txbuffer)),
858 C99INIT(rxbuffer_size, sizeof(uart0_rxbuffer)),
860 C99INIT(sending, false),
865 C99INIT(table, &UART1_VT),
866 C99INIT(txbuffer, uart1_txbuffer),
867 C99INIT(rxbuffer, uart1_rxbuffer),
868 C99INIT(txbuffer_size, sizeof(uart1_txbuffer)),
869 C99INIT(rxbuffer_size, sizeof(uart1_rxbuffer)),
871 C99INIT(sending, false),
877 C99INIT(table, &UART2_VT),
878 C99INIT(txbuffer, uart2_txbuffer),
879 C99INIT(rxbuffer, uart2_rxbuffer),
880 C99INIT(txbuffer_size, sizeof(uart2_txbuffer)),
881 C99INIT(rxbuffer_size, sizeof(uart2_rxbuffer)),
883 C99INIT(sending, false),
889 C99INIT(table, &UART3_VT),
890 C99INIT(txbuffer, uart3_txbuffer),
891 C99INIT(rxbuffer, uart3_rxbuffer),
892 C99INIT(txbuffer_size, sizeof(uart3_txbuffer)),
893 C99INIT(rxbuffer_size, sizeof(uart3_rxbuffer)),
895 C99INIT(sending, false),
900 C99INIT(table, &SPI_VT),
901 C99INIT(txbuffer, spi_txbuffer),
902 C99INIT(rxbuffer, spi_rxbuffer),
903 C99INIT(txbuffer_size, sizeof(spi_txbuffer)),
904 C99INIT(rxbuffer_size, sizeof(spi_rxbuffer)),
906 C99INIT(sending, false),
910 struct SerialHardware *ser_hw_getdesc(int unit)
912 ASSERT(unit < SER_CNT);
913 return &UARTDescs[unit].hw;
921 #if CONFIG_SER_HWHANDSHAKE
923 /// This interrupt is triggered when the CTS line goes high
926 // Re-enable UDR empty interrupt and TX, then disable CTS interrupt
927 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_UDRIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0);
928 EIMSK &= ~EIMSKF_CTS;
931 #endif // CONFIG_SER_HWHANDSHAKE
935 * Serial 0 TX interrupt handler
937 DECLARE_ISR(USART0_UDRE_vect)
941 struct FIFOBuffer * const txfifo = &ser_handles[SER_UART0]->txfifo;
943 if (fifo_isempty(txfifo))
946 #ifndef SER_UART0_BUS_TXOFF
947 UARTDescs[SER_UART0].sending = false;
950 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA103
953 // Disable rx interrupt and tx, enable CTS interrupt
955 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0);
962 char c = fifo_pop(txfifo);
963 SER_UART0_BUS_TXCHAR(c);
969 #ifdef SER_UART0_BUS_TXOFF
971 * Serial port 0 TX complete interrupt handler.
973 * This IRQ is usually disabled. The UDR-empty interrupt
974 * enables it when there's no more data to transmit.
975 * We need to wait until the last character has been
976 * transmitted before switching the 485 transceiver to
979 * The txfifo might have been refilled by putchar() while
980 * we were waiting for the transmission complete interrupt.
981 * In this case, we must restart the UDR empty interrupt,
982 * otherwise we'd stop the serial port with some data
983 * still pending in the buffer.
985 DECLARE_ISR(USART0_TX_vect)
989 struct FIFOBuffer * const txfifo = &ser_handles[SER_UART0]->txfifo;
990 if (fifo_isempty(txfifo))
993 UARTDescs[SER_UART0].sending = false;
996 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_UDRIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0);
1000 #endif /* SER_UART0_BUS_TXOFF */
1006 * Serial 1 TX interrupt handler
1008 DECLARE_ISR(USART1_UDRE_vect)
1012 struct FIFOBuffer * const txfifo = &ser_handles[SER_UART1]->txfifo;
1014 if (fifo_isempty(txfifo))
1016 SER_UART1_BUS_TXEND;
1017 #ifndef SER_UART1_BUS_TXOFF
1018 UARTDescs[SER_UART1].sending = false;
1021 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA103
1022 else if (!IS_CTS_ON)
1024 // Disable rx interrupt and tx, enable CTS interrupt
1026 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1);
1028 EIMSK |= EIMSKF_CTS;
1033 char c = fifo_pop(txfifo);
1034 SER_UART1_BUS_TXCHAR(c);
1040 #ifdef SER_UART1_BUS_TXOFF
1042 * Serial port 1 TX complete interrupt handler.
1044 * \sa port 0 TX complete handler.
1046 DECLARE_ISR(USART1_TX_vect)
1050 struct FIFOBuffer * const txfifo = &ser_handles[SER_UART1]->txfifo;
1051 if (fifo_isempty(txfifo))
1053 SER_UART1_BUS_TXOFF;
1054 UARTDescs[SER_UART1].sending = false;
1057 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_UDRIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1);
1061 #endif /* SER_UART1_BUS_TXOFF */
1063 #endif // AVR_HAS_UART1
1068 * Serial 2 TX interrupt handler
1070 DECLARE_ISR(USART2_UDRE_vect)
1074 struct FIFOBuffer * const txfifo = &ser_handles[SER_UART2]->txfifo;
1076 if (fifo_isempty(txfifo))
1078 SER_UART2_BUS_TXEND;
1079 #ifndef SER_UART2_BUS_TXOFF
1080 UARTDescs[SER_UART2].sending = false;
1085 char c = fifo_pop(txfifo);
1086 SER_UART2_BUS_TXCHAR(c);
1092 #ifdef SER_UART2_BUS_TXOFF
1094 * Serial port 2 TX complete interrupt handler.
1096 * \sa port 0 TX complete handler.
1098 DECLARE_ISR(USART2_TX_vect)
1102 struct FIFOBuffer * const txfifo = &ser_handles[SER_UART2]->txfifo;
1103 if (fifo_isempty(txfifo))
1105 SER_UART2_BUS_TXOFF;
1106 UARTDescs[SER_UART2].sending = false;
1109 UCSR2B = BV(BIT_RXCIE2) | BV(BIT_UDRIE2) | BV(BIT_RXEN2) | BV(BIT_TXEN2);
1113 #endif /* SER_UART2_BUS_TXOFF */
1115 #endif // AVR_HAS_UART2
1120 * Serial 3 TX interrupt handler
1122 DECLARE_ISR(USART3_UDRE_vect)
1126 struct FIFOBuffer * const txfifo = &ser_handles[SER_UART3]->txfifo;
1128 if (fifo_isempty(txfifo))
1130 SER_UART3_BUS_TXEND;
1131 #ifndef SER_UART3_BUS_TXOFF
1132 UARTDescs[SER_UART3].sending = false;
1137 char c = fifo_pop(txfifo);
1138 SER_UART3_BUS_TXCHAR(c);
1144 #ifdef SER_UART3_BUS_TXOFF
1146 * Serial port 3 TX complete interrupt handler.
1148 * \sa port 0 TX complete handler.
1150 DECLARE_ISR(USART3_TX_vect)
1154 struct FIFOBuffer * const txfifo = &ser_handles[SER_UART3]->txfifo;
1155 if (fifo_isempty(txfifo))
1157 SER_UART3_BUS_TXOFF;
1158 UARTDescs[SER_UART3].sending = false;
1161 UCSR3B = BV(BIT_RXCIE3) | BV(BIT_UDRIE3) | BV(BIT_RXEN3) | BV(BIT_TXEN3);
1165 #endif /* SER_UART3_BUS_TXOFF */
1167 #endif // AVR_HAS_UART3
1171 * Serial 0 RX complete interrupt handler.
1173 * This handler is interruptible.
1174 * Interrupt are reenabled as soon as recv complete interrupt is
1175 * disabled. Using INTERRUPT() is troublesome when the serial
1176 * is heavily loaded, because an interrupt could be retriggered
1177 * when executing the handler prologue before RXCIE is disabled.
1179 * \note The code that re-enables interrupts is commented out
1180 * because in some nasty cases the interrupt is retriggered.
1181 * This is probably due to the RXC flag being set before
1182 * RXCIE is cleared. Unfortunately the RXC flag is read-only
1183 * and can't be cleared by code.
1185 DECLARE_ISR(USART0_RX_vect)
1189 /* Disable Recv complete IRQ */
1190 //UCSR0B &= ~BV(RXCIE);
1193 /* Should be read before UDR */
1194 ser_handles[SER_UART0]->status |= UCSR0A & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
1196 /* To clear the RXC flag we must _always_ read the UDR even when we're
1197 * not going to accept the incoming data, otherwise a new interrupt
1198 * will occur once the handler terminates.
1201 struct FIFOBuffer * const rxfifo = &ser_handles[SER_UART0]->rxfifo;
1203 if (fifo_isfull(rxfifo))
1204 ser_handles[SER_UART0]->status |= SERRF_RXFIFOOVERRUN;
1207 fifo_push(rxfifo, c);
1208 #if CONFIG_SER_HWHANDSHAKE
1209 if (fifo_isfull(rxfifo))
1214 /* Reenable receive complete int */
1216 //UCSR0B |= BV(RXCIE);
1225 * Serial 1 RX complete interrupt handler.
1227 * This handler is interruptible.
1228 * Interrupt are reenabled as soon as recv complete interrupt is
1229 * disabled. Using INTERRUPT() is troublesome when the serial
1230 * is heavily loaded, because an interrupt could be retriggered
1231 * when executing the handler prologue before RXCIE is disabled.
1233 * \see DECLARE_ISR(USART1_RX_vect)
1235 DECLARE_ISR(USART1_RX_vect)
1239 /* Disable Recv complete IRQ */
1240 //UCSR1B &= ~BV(RXCIE);
1243 /* Should be read before UDR */
1244 ser_handles[SER_UART1]->status |= UCSR1A & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
1246 /* To avoid an IRQ storm, we must _always_ read the UDR even when we're
1247 * not going to accept the incoming data
1250 struct FIFOBuffer * const rxfifo = &ser_handles[SER_UART1]->rxfifo;
1251 //ASSERT_VALID_FIFO(rxfifo);
1253 if (UNLIKELY(fifo_isfull(rxfifo)))
1254 ser_handles[SER_UART1]->status |= SERRF_RXFIFOOVERRUN;
1257 fifo_push(rxfifo, c);
1258 #if CONFIG_SER_HWHANDSHAKE
1259 if (fifo_isfull(rxfifo))
1263 /* Re-enable receive complete int */
1265 //UCSR1B |= BV(RXCIE);
1270 #endif // AVR_HAS_UART1
1275 * Serial 2 RX complete interrupt handler.
1277 * This handler is interruptible.
1278 * Interrupt are reenabled as soon as recv complete interrupt is
1279 * disabled. Using INTERRUPT() is troublesome when the serial
1280 * is heavily loaded, because an interrupt could be retriggered
1281 * when executing the handler prologue before RXCIE is disabled.
1283 * \see DECLARE_ISR(USART2_RX_vect)
1285 DECLARE_ISR(USART2_RX_vect)
1289 /* Disable Recv complete IRQ */
1290 //UCSR1B &= ~BV(RXCIE);
1293 /* Should be read before UDR */
1294 ser_handles[SER_UART2]->status |= UCSR2A & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
1296 /* To avoid an IRQ storm, we must _always_ read the UDR even when we're
1297 * not going to accept the incoming data
1300 struct FIFOBuffer * const rxfifo = &ser_handles[SER_UART2]->rxfifo;
1301 //ASSERT_VALID_FIFO(rxfifo);
1303 if (UNLIKELY(fifo_isfull(rxfifo)))
1304 ser_handles[SER_UART2]->status |= SERRF_RXFIFOOVERRUN;
1307 fifo_push(rxfifo, c);
1308 #if CONFIG_SER_HWHANDSHAKE
1309 if (fifo_isfull(rxfifo))
1313 /* Re-enable receive complete int */
1315 //UCSR1B |= BV(RXCIE);
1320 #endif // AVR_HAS_UART2
1325 * Serial 3 RX complete interrupt handler.
1327 * This handler is interruptible.
1328 * Interrupt are reenabled as soon as recv complete interrupt is
1329 * disabled. Using INTERRUPT() is troublesome when the serial
1330 * is heavily loaded, because an interrupt could be retriggered
1331 * when executing the handler prologue before RXCIE is disabled.
1333 * \see DECLARE_ISR(USART3_RX_vect)
1335 DECLARE_ISR(USART3_RX_vect)
1339 /* Disable Recv complete IRQ */
1340 //UCSR1B &= ~BV(RXCIE);
1343 /* Should be read before UDR */
1344 ser_handles[SER_UART3]->status |= UCSR3A & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
1346 /* To avoid an IRQ storm, we must _always_ read the UDR even when we're
1347 * not going to accept the incoming data
1350 struct FIFOBuffer * const rxfifo = &ser_handles[SER_UART3]->rxfifo;
1351 //ASSERT_VALID_FIFO(rxfifo);
1353 if (UNLIKELY(fifo_isfull(rxfifo)))
1354 ser_handles[SER_UART3]->status |= SERRF_RXFIFOOVERRUN;
1357 fifo_push(rxfifo, c);
1358 #if CONFIG_SER_HWHANDSHAKE
1359 if (fifo_isfull(rxfifo))
1363 /* Re-enable receive complete int */
1365 //UCSR1B |= BV(RXCIE);
1370 #endif // AVR_HAS_UART3
1374 * SPI interrupt handler
1376 DECLARE_ISR(SPI_STC_vect)
1380 /* Read incoming byte. */
1381 if (!fifo_isfull(&ser_handles[SER_SPI]->rxfifo))
1382 fifo_push(&ser_handles[SER_SPI]->rxfifo, SPDR);
1386 ser_handles[SER_SPI]->status |= SERRF_RXFIFOOVERRUN;
1390 if (!fifo_isempty(&ser_handles[SER_SPI]->txfifo))
1391 SPDR = fifo_pop(&ser_handles[SER_SPI]->txfifo);
1393 UARTDescs[SER_SPI].sending = false;