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 || CPU_AVR_ATMEGA2560
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 || CPU_AVR_ATMEGA2560
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 UCSR0A = 0; /* The Arduino Uno bootloader turns on U2X0 */ \
150 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0); \
154 #ifndef SER_UART0_BUS_TXBEGIN
156 * Invoked before starting a transmission
158 * - Enable both the receiver and the transmitter
159 * - Enable both the RX complete and UDR empty interrupts
161 #define SER_UART0_BUS_TXBEGIN do { \
162 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_UDRIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0); \
166 #ifndef SER_UART0_BUS_TXCHAR
168 * Invoked to send one character.
170 #define SER_UART0_BUS_TXCHAR(c) do { \
175 #ifndef SER_UART0_BUS_TXEND
177 * Invoked as soon as the txfifo becomes empty
179 * - Keep both the receiver and the transmitter enabled
180 * - Keep the RX complete interrupt enabled
181 * - Disable the UDR empty interrupt
183 #define SER_UART0_BUS_TXEND do { \
184 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0); \
188 #ifndef SER_UART0_BUS_TXOFF
190 * \def SER_UART0_BUS_TXOFF
192 * Invoked after the last character has been transmitted
194 * The default is no action.
197 #define SER_UART0_BUS_TXOFF
201 #ifndef SER_UART1_BUS_TXINIT
202 /** \sa SER_UART0_BUS_TXINIT */
203 #define SER_UART1_BUS_TXINIT do { \
204 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1); \
207 #ifndef SER_UART1_BUS_TXBEGIN
208 /** \sa SER_UART0_BUS_TXBEGIN */
209 #define SER_UART1_BUS_TXBEGIN do { \
210 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_UDRIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1); \
213 #ifndef SER_UART1_BUS_TXCHAR
214 /** \sa SER_UART0_BUS_TXCHAR */
215 #define SER_UART1_BUS_TXCHAR(c) do { \
219 #ifndef SER_UART1_BUS_TXEND
220 /** \sa SER_UART0_BUS_TXEND */
221 #define SER_UART1_BUS_TXEND do { \
222 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1); \
225 #ifndef SER_UART1_BUS_TXOFF
227 * \def SER_UART1_BUS_TXOFF
229 * \see SER_UART0_BUS_TXOFF
232 #define SER_UART1_BUS_TXOFF
236 #ifndef SER_UART2_BUS_TXINIT
237 /** \sa SER_UART0_BUS_TXINIT */
238 #define SER_UART2_BUS_TXINIT do { \
239 UCSR2B = BV(BIT_RXCIE2) | BV(BIT_RXEN2) | BV(BIT_TXEN2); \
242 #ifndef SER_UART2_BUS_TXBEGIN
243 /** \sa SER_UART0_BUS_TXBEGIN */
244 #define SER_UART2_BUS_TXBEGIN do { \
245 UCSR2B = BV(BIT_RXCIE2) | BV(BIT_UDRIE2) | BV(BIT_RXEN2) | BV(BIT_TXEN2); \
248 #ifndef SER_UART2_BUS_TXCHAR
249 /** \sa SER_UART0_BUS_TXCHAR */
250 #define SER_UART2_BUS_TXCHAR(c) do { \
254 #ifndef SER_UART2_BUS_TXEND
255 /** \sa SER_UART0_BUS_TXEND */
256 #define SER_UART2_BUS_TXEND do { \
257 UCSR2B = BV(BIT_RXCIE2) | BV(BIT_RXEN2) | BV(BIT_TXEN2); \
260 #ifndef SER_UART2_BUS_TXOFF
262 * \def SER_UART2_BUS_TXOFF
264 * \see SER_UART0_BUS_TXOFF
267 #define SER_UART2_BUS_TXOFF
271 #ifndef SER_UART3_BUS_TXINIT
272 /** \sa SER_UART0_BUS_TXINIT */
273 #define SER_UART3_BUS_TXINIT do { \
274 UCSR3B = BV(BIT_RXCIE3) | BV(BIT_RXEN3) | BV(BIT_TXEN3); \
277 #ifndef SER_UART3_BUS_TXBEGIN
278 /** \sa SER_UART0_BUS_TXBEGIN */
279 #define SER_UART3_BUS_TXBEGIN do { \
280 UCSR3B = BV(BIT_RXCIE3) | BV(BIT_UDRIE3) | BV(BIT_RXEN3) | BV(BIT_TXEN3); \
283 #ifndef SER_UART3_BUS_TXCHAR
284 /** \sa SER_UART0_BUS_TXCHAR */
285 #define SER_UART3_BUS_TXCHAR(c) do { \
289 #ifndef SER_UART3_BUS_TXEND
290 /** \sa SER_UART0_BUS_TXEND */
291 #define SER_UART3_BUS_TXEND do { \
292 UCSR3B = BV(BIT_RXCIE3) | BV(BIT_RXEN3) | BV(BIT_TXEN3); \
295 #ifndef SER_UART3_BUS_TXOFF
297 * \def SER_UART3_BUS_TXOFF
299 * \see SER_UART0_BUS_TXOFF
302 #define SER_UART3_BUS_TXOFF
309 * \name Overridable SPI hooks
311 * These can be redefined in hw.h to implement
312 * special bus policies such as slave select pin handling, etc.
316 #ifndef SER_SPI_BUS_TXINIT
318 * Default TXINIT macro - invoked in spi_init()
319 * The default is no action.
321 #define SER_SPI_BUS_TXINIT
324 #ifndef SER_SPI_BUS_TXCLOSE
326 * Invoked after the last character has been transmitted.
327 * The default is no action.
329 #define SER_SPI_BUS_TXCLOSE
334 /* SPI port and pin configuration */
335 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA103 || CPU_AVR_ATMEGA1281 \
336 || CPU_AVR_ATMEGA1280 || CPU_AVR_ATMEGA2560
337 #define SPI_PORT PORTB
339 #define SPI_SS_BIT PB0
340 #define SPI_SCK_BIT PB1
341 #define SPI_MOSI_BIT PB2
342 #define SPI_MISO_BIT PB3
343 // TODO: these bits are the same as ATMEGA8 but the defines in avr-gcc are different.
344 // They should be the same!
345 #elif CPU_AVR_ATMEGA328P
346 #define SPI_PORT PORTB
348 #define SPI_SS_BIT PORTB2
349 #define SPI_SCK_BIT PORTB5
350 #define SPI_MOSI_BIT PORTB3
351 #define SPI_MISO_BIT PORTB4
352 #elif CPU_AVR_ATMEGA8 || CPU_AVR_ATMEGA168
353 #define SPI_PORT PORTB
355 #define SPI_SS_BIT PB2
356 #define SPI_SCK_BIT PB5
357 #define SPI_MOSI_BIT PB3
358 #define SPI_MISO_BIT PB4
359 #elif CPU_AVR_ATMEGA32
360 #define SPI_PORT PORTB
362 #define SPI_SS_BIT PB4
363 #define SPI_SCK_BIT PB7
364 #define SPI_MOSI_BIT PB5
365 #define SPI_MISO_BIT PB6
367 #error Unknown architecture
370 /* USART register definitions */
371 #if CPU_AVR_ATMEGA1280 || CPU_AVR_ATMEGA2560
372 #define AVR_HAS_UART1 1
373 #define AVR_HAS_UART2 1
374 #define AVR_HAS_UART3 1
375 #elif CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA1281
376 #define AVR_HAS_UART1 1
377 #define AVR_HAS_UART2 0
378 #define AVR_HAS_UART3 0
379 #elif CPU_AVR_ATMEGA168 || CPU_AVR_ATMEGA328P
380 #define AVR_HAS_UART1 0
381 #define AVR_HAS_UART2 0
382 #define AVR_HAS_UART3 0
383 #define USART0_UDRE_vect USART_UDRE_vect
384 #define USART0_RX_vect USART_RX_vect
385 #define USART0_TX_vect USART_TX_vect
386 #elif CPU_AVR_ATMEGA8 || CPU_AVR_ATMEGA32
387 #define AVR_HAS_UART1 0
388 #define AVR_HAS_UART2 0
389 #define AVR_HAS_UART3 0
398 #define USART0_UDRE_vect USART_UDRE_vect
399 #define USART0_RX_vect USART_RXC_vect
400 #define USART0_TX_vect USART_TXC_vect
401 #elif CPU_AVR_ATMEGA103
402 #define AVR_HAS_UART1 0
403 #define AVR_HAS_UART2 0
404 #define AVR_HAS_UART3 0
409 #define USART0_UDRE_vect USART_UDRE_vect
410 #define USART0_RX_vect USART_RX_vect
411 #define USART0_TX_vect USART_TX_vect
413 #error Unknown architecture
417 /* From the high-level serial driver */
418 extern struct Serial *ser_handles[SER_CNT];
420 /* TX and RX buffers */
421 static unsigned char uart0_txbuffer[CONFIG_UART0_TXBUFSIZE];
422 static unsigned char uart0_rxbuffer[CONFIG_UART0_RXBUFSIZE];
424 static unsigned char uart1_txbuffer[CONFIG_UART1_TXBUFSIZE];
425 static unsigned char uart1_rxbuffer[CONFIG_UART1_RXBUFSIZE];
428 static unsigned char uart2_txbuffer[CONFIG_UART2_TXBUFSIZE];
429 static unsigned char uart2_rxbuffer[CONFIG_UART2_RXBUFSIZE];
432 static unsigned char uart3_txbuffer[CONFIG_UART3_TXBUFSIZE];
433 static unsigned char uart3_rxbuffer[CONFIG_UART3_RXBUFSIZE];
435 static unsigned char spi_txbuffer[CONFIG_SPI_TXBUFSIZE];
436 static unsigned char spi_rxbuffer[CONFIG_SPI_RXBUFSIZE];
440 * Internal hardware state structure
442 * The \a sending variable is true while the transmission
443 * interrupt is retriggering itself.
445 * For the USARTs the \a sending flag is useful for taking specific
446 * actions before sending a burst of data, at the start of a trasmission
447 * but not before every char sent.
449 * For the SPI, this flag is necessary because the SPI sends and receives
450 * bytes at the same time and the SPI IRQ is unique for send/receive.
451 * The only way to start transmission is to write data in SPDR (this
452 * is done by spi_starttx()). We do this *only* if a transfer is
453 * not already started.
457 struct SerialHardware hw;
458 volatile bool sending;
466 static void uart0_init(
467 UNUSED_ARG(struct SerialHardware *, _hw),
468 UNUSED_ARG(struct Serial *, ser))
470 SER_UART0_BUS_TXINIT;
475 static void uart0_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
480 static void uart0_enabletxirq(struct SerialHardware *_hw)
482 struct AvrSerial *hw = (struct AvrSerial *)_hw;
485 * WARNING: racy code here! The tx interrupt sets hw->sending to false
486 * when it runs with an empty fifo. The order of statements in the
492 SER_UART0_BUS_TXBEGIN;
496 static void uart0_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
498 /* Compute baud-rate period */
499 uint16_t period = DIV_ROUND(CPU_FREQ / 16UL, rate) - 1;
501 #if !CPU_AVR_ATMEGA103
502 UBRR0H = (period) >> 8;
506 //DB(kprintf("uart0_setbaudrate(rate=%lu): period=%d\n", rate, period);)
509 static void uart0_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
511 #if !CPU_AVR_ATMEGA103
512 UCSR0C = (UCSR0C & ~(BV(UPM01) | BV(UPM00))) | ((parity) << UPM00);
518 static void uart1_init(
519 UNUSED_ARG(struct SerialHardware *, _hw),
520 UNUSED_ARG(struct Serial *, ser))
522 SER_UART1_BUS_TXINIT;
527 static void uart1_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
532 static void uart1_enabletxirq(struct SerialHardware *_hw)
534 struct AvrSerial *hw = (struct AvrSerial *)_hw;
537 * WARNING: racy code here! The tx interrupt
538 * sets hw->sending to false when it runs with
539 * an empty fifo. The order of the statements
540 * in the if-block matters.
545 SER_UART1_BUS_TXBEGIN;
549 static void uart1_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
551 /* Compute baud-rate period */
552 uint16_t period = DIV_ROUND(CPU_FREQ / 16UL, rate) - 1;
554 UBRR1H = (period) >> 8;
557 //DB(kprintf("uart1_setbaudrate(rate=%ld): period=%d\n", rate, period);)
560 static void uart1_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
562 UCSR1C = (UCSR1C & ~(BV(UPM11) | BV(UPM10))) | ((parity) << UPM10);
565 #endif // AVR_HAS_UART1
569 static void uart2_init(
570 UNUSED_ARG(struct SerialHardware *, _hw),
571 UNUSED_ARG(struct Serial *, ser))
573 SER_UART2_BUS_TXINIT;
578 static void uart2_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
583 static void uart2_enabletxirq(struct SerialHardware *_hw)
585 struct AvrSerial *hw = (struct AvrSerial *)_hw;
588 * WARNING: racy code here! The tx interrupt
589 * sets hw->sending to false when it runs with
590 * an empty fifo. The order of the statements
591 * in the if-block matters.
596 SER_UART2_BUS_TXBEGIN;
600 static void uart2_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
602 /* Compute baud-rate period */
603 uint16_t period = DIV_ROUND(CPU_FREQ / 16UL, rate) - 1;
605 UBRR2H = (period) >> 8;
608 //DB(kprintf("uart2_setbaudrate(rate=%ld): period=%d\n", rate, period);)
611 static void uart2_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
613 UCSR2C = (UCSR2C & ~(BV(UPM21) | BV(UPM20))) | ((parity) << UPM20);
616 #endif // AVR_HAS_UART2
620 static void uart3_init(
621 UNUSED_ARG(struct SerialHardware *, _hw),
622 UNUSED_ARG(struct Serial *, ser))
624 SER_UART3_BUS_TXINIT;
629 static void uart3_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
634 static void uart3_enabletxirq(struct SerialHardware *_hw)
636 struct AvrSerial *hw = (struct AvrSerial *)_hw;
639 * WARNING: racy code here! The tx interrupt
640 * sets hw->sending to false when it runs with
641 * an empty fifo. The order of the statements
642 * in the if-block matters.
647 SER_UART3_BUS_TXBEGIN;
651 static void uart3_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
653 /* Compute baud-rate period */
654 uint16_t period = DIV_ROUND(CPU_FREQ / 16UL, rate) - 1;
656 UBRR3H = (period) >> 8;
659 //DB(kprintf("uart3_setbaudrate(rate=%ld): period=%d\n", rate, period);)
662 static void uart3_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
664 UCSR3C = (UCSR3C & ~(BV(UPM31) | BV(UPM30))) | ((parity) << UPM30);
667 #endif // AVR_HAS_UART3
670 static void spi_init(UNUSED_ARG(struct SerialHardware *, _hw), UNUSED_ARG(struct Serial *, ser))
673 * Set MOSI and SCK ports out, MISO in.
675 * The ATmega64/128 datasheet explicitly states that the input/output
676 * state of the SPI pins is not significant, as when the SPI is
677 * active the I/O port are overrided.
678 * This is *blatantly FALSE*.
680 * Moreover, the MISO pin on the board_kc *must* be in high impedance
681 * state even when the SPI is off, because the line is wired together
682 * with the KBus serial RX, and the transmitter of the slave boards
683 * would be unable to drive the line.
685 ATOMIC(SPI_DDR |= (BV(SPI_MOSI_BIT) | BV(SPI_SCK_BIT)));
688 * If the SPI master mode is activated and the SS pin is in input and tied low,
689 * the SPI hardware will automatically switch to slave mode!
690 * For proper communication this pins should therefore be:
692 * - as input but tied high forever!
693 * This driver set the pin as output.
695 #warning FIXME:SPI SS pin set as output for proper operation, check schematics for possible conflicts.
696 ATOMIC(SPI_DDR |= BV(SPI_SS_BIT));
698 ATOMIC(SPI_DDR &= ~BV(SPI_MISO_BIT));
699 /* Enable SPI, IRQ on, Master */
700 SPCR = BV(SPE) | BV(SPIE) | BV(MSTR);
703 #if CONFIG_SPI_DATA_ORDER == SER_LSB_FIRST
707 /* Set SPI clock rate */
708 #if CONFIG_SPI_CLOCK_DIV == 128
709 SPCR |= (BV(SPR1) | BV(SPR0));
710 #elif (CONFIG_SPI_CLOCK_DIV == 64 || CONFIG_SPI_CLOCK_DIV == 32)
712 #elif (CONFIG_SPI_CLOCK_DIV == 16 || CONFIG_SPI_CLOCK_DIV == 8)
714 #elif (CONFIG_SPI_CLOCK_DIV == 4 || CONFIG_SPI_CLOCK_DIV == 2)
715 // SPR0 & SDPR1 both at 0
717 #error Unsupported SPI clock division factor.
720 /* Set SPI2X bit (spi double frequency) */
721 #if (CONFIG_SPI_CLOCK_DIV == 128 || CONFIG_SPI_CLOCK_DIV == 64 \
722 || CONFIG_SPI_CLOCK_DIV == 16 || CONFIG_SPI_CLOCK_DIV == 4)
724 #elif (CONFIG_SPI_CLOCK_DIV == 32 || CONFIG_SPI_CLOCK_DIV == 8 || CONFIG_SPI_CLOCK_DIV == 2)
727 #error Unsupported SPI clock division factor.
730 /* Set clock polarity */
731 #if CONFIG_SPI_CLOCK_POL == 1
735 /* Set clock phase */
736 #if CONFIG_SPI_CLOCK_PHASE == 1
744 static void spi_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
750 /* Set all pins as inputs */
751 ATOMIC(SPI_DDR &= ~(BV(SPI_MISO_BIT) | BV(SPI_MOSI_BIT) | BV(SPI_SCK_BIT) | BV(SPI_SS_BIT)));
754 static void spi_starttx(struct SerialHardware *_hw)
756 struct AvrSerial *hw = (struct AvrSerial *)_hw;
759 IRQ_SAVE_DISABLE(flags);
761 /* Send data only if the SPI is not already transmitting */
762 if (!hw->sending && !fifo_isempty(&ser_handles[SER_SPI]->txfifo))
765 SPDR = fifo_pop(&ser_handles[SER_SPI]->txfifo);
771 static void spi_setbaudrate(
772 UNUSED_ARG(struct SerialHardware *, _hw),
773 UNUSED_ARG(unsigned long, rate))
778 static void spi_setparity(UNUSED_ARG(struct SerialHardware *, _hw), UNUSED_ARG(int, parity))
783 static bool tx_sending(struct SerialHardware* _hw)
785 struct AvrSerial *hw = (struct AvrSerial *)_hw;
791 // FIXME: move into compiler.h? Ditch?
793 #define C99INIT(name,val) .name = val
794 #elif defined(__GNUC__)
795 #define C99INIT(name,val) name: val
797 #warning No designated initializers, double check your code
798 #define C99INIT(name,val) (val)
802 * High-level interface data structures
804 static const struct SerialHardwareVT UART0_VT =
806 C99INIT(init, uart0_init),
807 C99INIT(cleanup, uart0_cleanup),
808 C99INIT(setBaudrate, uart0_setbaudrate),
809 C99INIT(setParity, uart0_setparity),
810 C99INIT(txStart, uart0_enabletxirq),
811 C99INIT(txSending, tx_sending),
815 static const struct SerialHardwareVT UART1_VT =
817 C99INIT(init, uart1_init),
818 C99INIT(cleanup, uart1_cleanup),
819 C99INIT(setBaudrate, uart1_setbaudrate),
820 C99INIT(setParity, uart1_setparity),
821 C99INIT(txStart, uart1_enabletxirq),
822 C99INIT(txSending, tx_sending),
824 #endif // AVR_HAS_UART1
827 static const struct SerialHardwareVT UART2_VT =
829 C99INIT(init, uart2_init),
830 C99INIT(cleanup, uart2_cleanup),
831 C99INIT(setBaudrate, uart2_setbaudrate),
832 C99INIT(setParity, uart2_setparity),
833 C99INIT(txStart, uart2_enabletxirq),
834 C99INIT(txSending, tx_sending),
836 #endif // AVR_HAS_UART2
839 static const struct SerialHardwareVT UART3_VT =
841 C99INIT(init, uart3_init),
842 C99INIT(cleanup, uart3_cleanup),
843 C99INIT(setBaudrate, uart3_setbaudrate),
844 C99INIT(setParity, uart3_setparity),
845 C99INIT(txStart, uart3_enabletxirq),
846 C99INIT(txSending, tx_sending),
848 #endif // AVR_HAS_UART3
850 static const struct SerialHardwareVT SPI_VT =
852 C99INIT(init, spi_init),
853 C99INIT(cleanup, spi_cleanup),
854 C99INIT(setBaudrate, spi_setbaudrate),
855 C99INIT(setParity, spi_setparity),
856 C99INIT(txStart, spi_starttx),
857 C99INIT(txSending, tx_sending),
860 static struct AvrSerial UARTDescs[SER_CNT] =
864 C99INIT(table, &UART0_VT),
865 C99INIT(txbuffer, uart0_txbuffer),
866 C99INIT(rxbuffer, uart0_rxbuffer),
867 C99INIT(txbuffer_size, sizeof(uart0_txbuffer)),
868 C99INIT(rxbuffer_size, sizeof(uart0_rxbuffer)),
870 C99INIT(sending, false),
875 C99INIT(table, &UART1_VT),
876 C99INIT(txbuffer, uart1_txbuffer),
877 C99INIT(rxbuffer, uart1_rxbuffer),
878 C99INIT(txbuffer_size, sizeof(uart1_txbuffer)),
879 C99INIT(rxbuffer_size, sizeof(uart1_rxbuffer)),
881 C99INIT(sending, false),
887 C99INIT(table, &UART2_VT),
888 C99INIT(txbuffer, uart2_txbuffer),
889 C99INIT(rxbuffer, uart2_rxbuffer),
890 C99INIT(txbuffer_size, sizeof(uart2_txbuffer)),
891 C99INIT(rxbuffer_size, sizeof(uart2_rxbuffer)),
893 C99INIT(sending, false),
899 C99INIT(table, &UART3_VT),
900 C99INIT(txbuffer, uart3_txbuffer),
901 C99INIT(rxbuffer, uart3_rxbuffer),
902 C99INIT(txbuffer_size, sizeof(uart3_txbuffer)),
903 C99INIT(rxbuffer_size, sizeof(uart3_rxbuffer)),
905 C99INIT(sending, false),
910 C99INIT(table, &SPI_VT),
911 C99INIT(txbuffer, spi_txbuffer),
912 C99INIT(rxbuffer, spi_rxbuffer),
913 C99INIT(txbuffer_size, sizeof(spi_txbuffer)),
914 C99INIT(rxbuffer_size, sizeof(spi_rxbuffer)),
916 C99INIT(sending, false),
920 struct SerialHardware *ser_hw_getdesc(int unit)
922 ASSERT(unit < SER_CNT);
923 return &UARTDescs[unit].hw;
931 #if CONFIG_SER_HWHANDSHAKE
933 /// This interrupt is triggered when the CTS line goes high
936 // Re-enable UDR empty interrupt and TX, then disable CTS interrupt
937 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_UDRIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0);
938 EIMSK &= ~EIMSKF_CTS;
941 #endif // CONFIG_SER_HWHANDSHAKE
945 * Serial 0 TX interrupt handler
947 DECLARE_ISR(USART0_UDRE_vect)
951 struct FIFOBuffer * const txfifo = &ser_handles[SER_UART0]->txfifo;
953 if (fifo_isempty(txfifo))
956 #ifndef SER_UART0_BUS_TXOFF
957 UARTDescs[SER_UART0].sending = false;
960 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA103
963 // Disable rx interrupt and tx, enable CTS interrupt
965 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0);
972 char c = fifo_pop(txfifo);
973 SER_UART0_BUS_TXCHAR(c);
979 #ifdef SER_UART0_BUS_TXOFF
981 * Serial port 0 TX complete interrupt handler.
983 * This IRQ is usually disabled. The UDR-empty interrupt
984 * enables it when there's no more data to transmit.
985 * We need to wait until the last character has been
986 * transmitted before switching the 485 transceiver to
989 * The txfifo might have been refilled by putchar() while
990 * we were waiting for the transmission complete interrupt.
991 * In this case, we must restart the UDR empty interrupt,
992 * otherwise we'd stop the serial port with some data
993 * still pending in the buffer.
995 DECLARE_ISR(USART0_TX_vect)
999 struct FIFOBuffer * const txfifo = &ser_handles[SER_UART0]->txfifo;
1000 if (fifo_isempty(txfifo))
1002 SER_UART0_BUS_TXOFF;
1003 UARTDescs[SER_UART0].sending = false;
1006 UCSR0B = BV(BIT_RXCIE0) | BV(BIT_UDRIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0);
1010 #endif /* SER_UART0_BUS_TXOFF */
1016 * Serial 1 TX interrupt handler
1018 DECLARE_ISR(USART1_UDRE_vect)
1022 struct FIFOBuffer * const txfifo = &ser_handles[SER_UART1]->txfifo;
1024 if (fifo_isempty(txfifo))
1026 SER_UART1_BUS_TXEND;
1027 #ifndef SER_UART1_BUS_TXOFF
1028 UARTDescs[SER_UART1].sending = false;
1031 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA103
1032 else if (!IS_CTS_ON)
1034 // Disable rx interrupt and tx, enable CTS interrupt
1036 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1);
1038 EIMSK |= EIMSKF_CTS;
1043 char c = fifo_pop(txfifo);
1044 SER_UART1_BUS_TXCHAR(c);
1050 #ifdef SER_UART1_BUS_TXOFF
1052 * Serial port 1 TX complete interrupt handler.
1054 * \sa port 0 TX complete handler.
1056 DECLARE_ISR(USART1_TX_vect)
1060 struct FIFOBuffer * const txfifo = &ser_handles[SER_UART1]->txfifo;
1061 if (fifo_isempty(txfifo))
1063 SER_UART1_BUS_TXOFF;
1064 UARTDescs[SER_UART1].sending = false;
1067 UCSR1B = BV(BIT_RXCIE1) | BV(BIT_UDRIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1);
1071 #endif /* SER_UART1_BUS_TXOFF */
1073 #endif // AVR_HAS_UART1
1078 * Serial 2 TX interrupt handler
1080 DECLARE_ISR(USART2_UDRE_vect)
1084 struct FIFOBuffer * const txfifo = &ser_handles[SER_UART2]->txfifo;
1086 if (fifo_isempty(txfifo))
1088 SER_UART2_BUS_TXEND;
1089 #ifndef SER_UART2_BUS_TXOFF
1090 UARTDescs[SER_UART2].sending = false;
1095 char c = fifo_pop(txfifo);
1096 SER_UART2_BUS_TXCHAR(c);
1102 #ifdef SER_UART2_BUS_TXOFF
1104 * Serial port 2 TX complete interrupt handler.
1106 * \sa port 0 TX complete handler.
1108 DECLARE_ISR(USART2_TX_vect)
1112 struct FIFOBuffer * const txfifo = &ser_handles[SER_UART2]->txfifo;
1113 if (fifo_isempty(txfifo))
1115 SER_UART2_BUS_TXOFF;
1116 UARTDescs[SER_UART2].sending = false;
1119 UCSR2B = BV(BIT_RXCIE2) | BV(BIT_UDRIE2) | BV(BIT_RXEN2) | BV(BIT_TXEN2);
1123 #endif /* SER_UART2_BUS_TXOFF */
1125 #endif // AVR_HAS_UART2
1130 * Serial 3 TX interrupt handler
1132 DECLARE_ISR(USART3_UDRE_vect)
1136 struct FIFOBuffer * const txfifo = &ser_handles[SER_UART3]->txfifo;
1138 if (fifo_isempty(txfifo))
1140 SER_UART3_BUS_TXEND;
1141 #ifndef SER_UART3_BUS_TXOFF
1142 UARTDescs[SER_UART3].sending = false;
1147 char c = fifo_pop(txfifo);
1148 SER_UART3_BUS_TXCHAR(c);
1154 #ifdef SER_UART3_BUS_TXOFF
1156 * Serial port 3 TX complete interrupt handler.
1158 * \sa port 0 TX complete handler.
1160 DECLARE_ISR(USART3_TX_vect)
1164 struct FIFOBuffer * const txfifo = &ser_handles[SER_UART3]->txfifo;
1165 if (fifo_isempty(txfifo))
1167 SER_UART3_BUS_TXOFF;
1168 UARTDescs[SER_UART3].sending = false;
1171 UCSR3B = BV(BIT_RXCIE3) | BV(BIT_UDRIE3) | BV(BIT_RXEN3) | BV(BIT_TXEN3);
1175 #endif /* SER_UART3_BUS_TXOFF */
1177 #endif // AVR_HAS_UART3
1181 * Serial 0 RX complete interrupt handler.
1183 * This handler is interruptible.
1184 * Interrupt are reenabled as soon as recv complete interrupt is
1185 * disabled. Using INTERRUPT() is troublesome when the serial
1186 * is heavily loaded, because an interrupt could be retriggered
1187 * when executing the handler prologue before RXCIE is disabled.
1189 * \note The code that re-enables interrupts is commented out
1190 * because in some nasty cases the interrupt is retriggered.
1191 * This is probably due to the RXC flag being set before
1192 * RXCIE is cleared. Unfortunately the RXC flag is read-only
1193 * and can't be cleared by code.
1195 DECLARE_ISR(USART0_RX_vect)
1199 /* Disable Recv complete IRQ */
1200 //UCSR0B &= ~BV(RXCIE);
1203 /* Should be read before UDR */
1204 ser_handles[SER_UART0]->status |= UCSR0A & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
1206 /* To clear the RXC flag we must _always_ read the UDR even when we're
1207 * not going to accept the incoming data, otherwise a new interrupt
1208 * will occur once the handler terminates.
1211 struct FIFOBuffer * const rxfifo = &ser_handles[SER_UART0]->rxfifo;
1213 if (fifo_isfull(rxfifo))
1214 ser_handles[SER_UART0]->status |= SERRF_RXFIFOOVERRUN;
1217 fifo_push(rxfifo, c);
1218 #if CONFIG_SER_HWHANDSHAKE
1219 if (fifo_isfull(rxfifo))
1224 /* Reenable receive complete int */
1226 //UCSR0B |= BV(RXCIE);
1235 * Serial 1 RX complete interrupt handler.
1237 * This handler is interruptible.
1238 * Interrupt are reenabled as soon as recv complete interrupt is
1239 * disabled. Using INTERRUPT() is troublesome when the serial
1240 * is heavily loaded, because an interrupt could be retriggered
1241 * when executing the handler prologue before RXCIE is disabled.
1243 * \see DECLARE_ISR(USART1_RX_vect)
1245 DECLARE_ISR(USART1_RX_vect)
1249 /* Disable Recv complete IRQ */
1250 //UCSR1B &= ~BV(RXCIE);
1253 /* Should be read before UDR */
1254 ser_handles[SER_UART1]->status |= UCSR1A & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
1256 /* To avoid an IRQ storm, we must _always_ read the UDR even when we're
1257 * not going to accept the incoming data
1260 struct FIFOBuffer * const rxfifo = &ser_handles[SER_UART1]->rxfifo;
1261 //ASSERT_VALID_FIFO(rxfifo);
1263 if (UNLIKELY(fifo_isfull(rxfifo)))
1264 ser_handles[SER_UART1]->status |= SERRF_RXFIFOOVERRUN;
1267 fifo_push(rxfifo, c);
1268 #if CONFIG_SER_HWHANDSHAKE
1269 if (fifo_isfull(rxfifo))
1273 /* Re-enable receive complete int */
1275 //UCSR1B |= BV(RXCIE);
1280 #endif // AVR_HAS_UART1
1285 * Serial 2 RX complete interrupt handler.
1287 * This handler is interruptible.
1288 * Interrupt are reenabled as soon as recv complete interrupt is
1289 * disabled. Using INTERRUPT() is troublesome when the serial
1290 * is heavily loaded, because an interrupt could be retriggered
1291 * when executing the handler prologue before RXCIE is disabled.
1293 * \see DECLARE_ISR(USART2_RX_vect)
1295 DECLARE_ISR(USART2_RX_vect)
1299 /* Disable Recv complete IRQ */
1300 //UCSR1B &= ~BV(RXCIE);
1303 /* Should be read before UDR */
1304 ser_handles[SER_UART2]->status |= UCSR2A & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
1306 /* To avoid an IRQ storm, we must _always_ read the UDR even when we're
1307 * not going to accept the incoming data
1310 struct FIFOBuffer * const rxfifo = &ser_handles[SER_UART2]->rxfifo;
1311 //ASSERT_VALID_FIFO(rxfifo);
1313 if (UNLIKELY(fifo_isfull(rxfifo)))
1314 ser_handles[SER_UART2]->status |= SERRF_RXFIFOOVERRUN;
1317 fifo_push(rxfifo, c);
1318 #if CONFIG_SER_HWHANDSHAKE
1319 if (fifo_isfull(rxfifo))
1323 /* Re-enable receive complete int */
1325 //UCSR1B |= BV(RXCIE);
1330 #endif // AVR_HAS_UART2
1335 * Serial 3 RX complete interrupt handler.
1337 * This handler is interruptible.
1338 * Interrupt are reenabled as soon as recv complete interrupt is
1339 * disabled. Using INTERRUPT() is troublesome when the serial
1340 * is heavily loaded, because an interrupt could be retriggered
1341 * when executing the handler prologue before RXCIE is disabled.
1343 * \see DECLARE_ISR(USART3_RX_vect)
1345 DECLARE_ISR(USART3_RX_vect)
1349 /* Disable Recv complete IRQ */
1350 //UCSR1B &= ~BV(RXCIE);
1353 /* Should be read before UDR */
1354 ser_handles[SER_UART3]->status |= UCSR3A & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
1356 /* To avoid an IRQ storm, we must _always_ read the UDR even when we're
1357 * not going to accept the incoming data
1360 struct FIFOBuffer * const rxfifo = &ser_handles[SER_UART3]->rxfifo;
1361 //ASSERT_VALID_FIFO(rxfifo);
1363 if (UNLIKELY(fifo_isfull(rxfifo)))
1364 ser_handles[SER_UART3]->status |= SERRF_RXFIFOOVERRUN;
1367 fifo_push(rxfifo, c);
1368 #if CONFIG_SER_HWHANDSHAKE
1369 if (fifo_isfull(rxfifo))
1373 /* Re-enable receive complete int */
1375 //UCSR1B |= BV(RXCIE);
1380 #endif // AVR_HAS_UART3
1384 * SPI interrupt handler
1386 DECLARE_ISR(SPI_STC_vect)
1390 /* Read incoming byte. */
1391 if (!fifo_isfull(&ser_handles[SER_SPI]->rxfifo))
1392 fifo_push(&ser_handles[SER_SPI]->rxfifo, SPDR);
1396 ser_handles[SER_SPI]->status |= SERRF_RXFIFOOVERRUN;
1400 if (!fifo_isempty(&ser_handles[SER_SPI]->txfifo))
1401 SPDR = fifo_pop(&ser_handles[SER_SPI]->txfifo);
1403 UARTDescs[SER_SPI].sending = false;