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 ARM UART and SPI I/O driver
37 * \version $Id: ser_at91.c 20881 2008-03-04 14:07:02Z batt $
38 * \author Daniele Basile <asterix@develer.com>
41 #include "hw/hw_ser.h" /* Required for bus macros overrides */
42 #include "hw/hw_cpu.h" /* CLOCK_FREQ */
44 #include "cfg/cfg_ser.h"
45 #include <cfg/debug.h>
53 #include <drv/ser_p.h>
55 #include <struct/fifobuf.h>
58 #define SERIRQ_PRIORITY 4 ///< default priority for serial irqs.
61 * \name Overridable serial bus hooks
63 * These can be redefined in hw.h to implement
64 * special bus policies such as half-duplex, 485, etc.
68 * TXBEGIN TXCHAR TXEND TXOFF
69 * | __________|__________ | |
72 * ______ __ __ __ __ __ __ ________________
73 * \/ \/ \/ \/ \/ \/ \/
74 * ______/\__/\__/\__/\__/\__/\__/
81 #ifndef SER_UART0_BUS_TXINIT
83 * Default TXINIT macro - invoked in uart0_init()
85 * - Disable GPIO on USART0 tx/rx pins
87 #if !CPU_ARM_AT91SAM7S256 && !CPU_ARM_AT91SAM7X256 && !CPU_ARM_AT91SAM7X128
88 #warning Check USART0 pins!
90 #define SER_UART0_BUS_TXINIT do { \
91 PIOA_PDR = BV(RXD0) | BV(TXD0); \
96 #ifndef SER_UART0_BUS_TXBEGIN
98 * Invoked before starting a transmission
100 #define SER_UART0_BUS_TXBEGIN
103 #ifndef SER_UART0_BUS_TXCHAR
105 * Invoked to send one character.
107 #define SER_UART0_BUS_TXCHAR(c) do { \
112 #ifndef SER_UART0_BUS_TXEND
114 * Invoked as soon as the txfifo becomes empty
116 #define SER_UART0_BUS_TXEND
119 /* End USART0 macros */
121 #ifndef SER_UART1_BUS_TXINIT
123 * Default TXINIT macro - invoked in uart1_init()
125 * - Disable GPIO on USART1 tx/rx pins
127 #if !CPU_ARM_AT91SAM7S256 && !CPU_ARM_AT91SAM7X256 && !CPU_ARM_AT91SAM7X128
128 #warning Check USART1 pins!
130 #define SER_UART1_BUS_TXINIT do { \
131 PIOA_PDR = BV(RXD1) | BV(TXD1); \
136 #ifndef SER_UART1_BUS_TXBEGIN
138 * Invoked before starting a transmission
140 #define SER_UART1_BUS_TXBEGIN
143 #ifndef SER_UART1_BUS_TXCHAR
145 * Invoked to send one character.
147 #define SER_UART1_BUS_TXCHAR(c) do { \
152 #ifndef SER_UART1_BUS_TXEND
154 * Invoked as soon as the txfifo becomes empty
156 #define SER_UART1_BUS_TXEND
160 * \name Overridable SPI hooks
162 * These can be redefined in hw.h to implement
163 * special bus policies such as slave select pin handling, etc.
168 #ifndef SER_SPI0_BUS_TXINIT
170 * Default TXINIT macro - invoked in spi_init()
171 * The default is no action.
173 #define SER_SPI0_BUS_TXINIT
176 #ifndef SER_SPI0_BUS_TXCLOSE
178 * Invoked after the last character has been transmitted.
179 * The default is no action.
181 #define SER_SPI0_BUS_TXCLOSE
184 #if CPU_ARM_AT91SAM7X128 || CPU_ARM_AT91SAM7X256
186 #ifndef SER_SPI1_BUS_TXINIT
188 * Default TXINIT macro - invoked in spi_init()
189 * The default is no action.
191 #define SER_SPI1_BUS_TXINIT
194 #ifndef SER_SPI1_BUS_TXCLOSE
196 * Invoked after the last character has been transmitted.
197 * The default is no action.
199 #define SER_SPI1_BUS_TXCLOSE
206 * \def CONFIG_SER_STROBE
208 * This is a debug facility that can be used to
209 * monitor SER interrupt activity on an external pin.
211 * To use strobes, redefine the macros SER_STROBE_ON,
212 * SER_STROBE_OFF and SER_STROBE_INIT and set
213 * CONFIG_SER_STROBE to 1.
215 #if !defined(CONFIG_SER_STROBE) || !CONFIG_SER_STROBE
216 #define SER_STROBE_ON do {/*nop*/} while(0)
217 #define SER_STROBE_OFF do {/*nop*/} while(0)
218 #define SER_STROBE_INIT do {/*nop*/} while(0)
222 /* From the high-level serial driver */
223 extern struct Serial *ser_handles[SER_CNT];
225 /* TX and RX buffers */
226 static unsigned char uart0_txbuffer[CONFIG_UART0_TXBUFSIZE];
227 static unsigned char uart0_rxbuffer[CONFIG_UART0_RXBUFSIZE];
229 static unsigned char uart1_txbuffer[CONFIG_UART1_TXBUFSIZE];
230 static unsigned char uart1_rxbuffer[CONFIG_UART1_RXBUFSIZE];
232 static unsigned char spi0_txbuffer[CONFIG_SPI0_TXBUFSIZE];
233 static unsigned char spi0_rxbuffer[CONFIG_SPI0_RXBUFSIZE];
234 #if CPU_ARM_AT91SAM7X128 || CPU_ARM_AT91SAM7X256
235 static unsigned char spi1_txbuffer[CONFIG_SPI1_TXBUFSIZE];
236 static unsigned char spi1_rxbuffer[CONFIG_SPI1_RXBUFSIZE];
240 * Internal hardware state structure
242 * The \a sending variable is true while the transmission
243 * interrupt is retriggering itself.
245 * For the USARTs the \a sending flag is useful for taking specific
246 * actions before sending a burst of data, at the start of a trasmission
247 * but not before every char sent.
249 * For the SPI, this flag is necessary because the SPI sends and receives
250 * bytes at the same time and the SPI IRQ is unique for send/receive.
251 * The only way to start transmission is to write data in SPDR (this
252 * is done by spi_starttx()). We do this *only* if a transfer is
253 * not already started.
257 struct SerialHardware hw;
258 volatile bool sending;
263 #if CPU_ARM_AT91SAM7X128 || CPU_ARM_AT91SAM7X256
264 struct Serial *ser_spi1 = &ser_handles[SER_SPI1];
267 static void uart0_irq_dispatcher(void);
268 static void uart1_irq_dispatcher(void);
269 static void spi0_irq_handler(void);
270 #if CPU_ARM_AT91SAM7X128 || CPU_ARM_AT91SAM7X256
271 static void spi1_irq_handler(void);
274 * Callbacks for USART0
276 static void uart0_init(
277 UNUSED_ARG(struct SerialHardware *, _hw),
278 UNUSED_ARG(struct Serial *, ser))
280 US0_IDR = 0xFFFFFFFF;
281 /* Set the vector. */
282 AIC_SVR(US0_ID) = uart0_irq_dispatcher;
283 /* Initialize to level sensitive with defined priority. */
284 AIC_SMR(US0_ID) = AIC_SRCTYPE_INT_LEVEL_SENSITIVE | SERIRQ_PRIORITY;
285 PMC_PCER = BV(US0_ID);
289 * - Set serial param: mode Normal, 8bit data, 1bit stop, parity none
290 * - Enable both the receiver and the transmitter
291 * - Enable only the RX complete interrupt
293 US0_CR = BV(US_RSTRX) | BV(US_RSTTX);
294 US0_MR = US_CHMODE_NORMAL | US_CHRL_8 | US_NBSTOP_1 | US_PAR_NO;
295 US0_CR = BV(US_RXEN) | BV(US_TXEN);
296 US0_IER = BV(US_RXRDY);
298 SER_UART0_BUS_TXINIT;
300 /* Enable the USART IRQ */
301 AIC_IECR = BV(US0_ID);
306 static void uart0_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
308 US0_CR = BV(US_RSTRX) | BV(US_RSTTX) | BV(US_RXDIS) | BV(US_TXDIS) | BV(US_RSTSTA);
311 static void uart0_enabletxirq(struct SerialHardware *_hw)
313 struct ArmSerial *hw = (struct ArmSerial *)_hw;
316 * WARNING: racy code here! The tx interrupt sets hw->sending to false
317 * when it runs with an empty fifo. The order of statements in the
324 * - Enable the transmitter
325 * - Enable TX empty interrupt
327 SER_UART0_BUS_TXBEGIN;
328 US0_IER = BV(US_TXEMPTY);
332 static void uart0_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
334 /* Compute baud-rate period */
335 US0_BRGR = CLOCK_FREQ / (16 * rate);
336 //DB(kprintf("uart0_setbaudrate(rate=%lu): period=%d\n", rate, period);)
339 static void uart0_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
341 US0_MR &= ~US_PAR_MASK;
342 /* Set UART parity */
345 case SER_PARITY_NONE:
351 case SER_PARITY_EVEN:
354 US0_MR |= US_PAR_EVEN;
360 US0_MR |= US_PAR_ODD;
369 * Callbacks for USART1
371 static void uart1_init(
372 UNUSED_ARG(struct SerialHardware *, _hw),
373 UNUSED_ARG(struct Serial *, ser))
375 US1_IDR = 0xFFFFFFFF;
376 /* Set the vector. */
377 AIC_SVR(US1_ID) = uart1_irq_dispatcher;
378 /* Initialize to level sensitive with defined priority. */
379 AIC_SMR(US1_ID) = AIC_SRCTYPE_INT_LEVEL_SENSITIVE | SERIRQ_PRIORITY;
380 PMC_PCER = BV(US1_ID);
384 * - Set serial param: mode Normal, 8bit data, 1bit stop, parity none
385 * - Enable both the receiver and the transmitter
386 * - Enable only the RX complete interrupt
388 US1_CR = BV(US_RSTRX) | BV(US_RSTTX);
389 US1_MR = US_CHMODE_NORMAL | US_CHRL_8 | US_NBSTOP_1 | US_PAR_NO;
390 US1_CR = BV(US_RXEN) | BV(US_TXEN);
391 US1_IER = BV(US_RXRDY);
393 SER_UART1_BUS_TXINIT;
395 /* Enable the USART IRQ */
396 AIC_IECR = BV(US1_ID);
401 static void uart1_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
403 US1_CR = BV(US_RSTRX) | BV(US_RSTTX) | BV(US_RXDIS) | BV(US_TXDIS) | BV(US_RSTSTA);
406 static void uart1_enabletxirq(struct SerialHardware *_hw)
408 struct ArmSerial *hw = (struct ArmSerial *)_hw;
411 * WARNING: racy code here! The tx interrupt sets hw->sending to false
412 * when it runs with an empty fifo. The order of statements in the
419 * - Enable the transmitter
420 * - Enable TX empty interrupt
422 SER_UART1_BUS_TXBEGIN;
423 US1_IER = BV(US_TXEMPTY);
427 static void uart1_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
429 /* Compute baud-rate period */
430 US1_BRGR = CLOCK_FREQ / (16 * rate);
431 //DB(kprintf("uart0_setbaudrate(rate=%lu): period=%d\n", rate, period);)
434 static void uart1_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
436 US1_MR &= ~US_PAR_MASK;
437 /* Set UART parity */
440 case SER_PARITY_NONE:
446 case SER_PARITY_EVEN:
449 US1_MR |= US_PAR_EVEN;
455 US1_MR |= US_PAR_ODD;
465 static void spi0_init(UNUSED_ARG(struct SerialHardware *, _hw), UNUSED_ARG(struct Serial *, ser))
467 /* Disable PIO on SPI pins */
468 PIOA_PDR = BV(SPI0_SPCK) | BV(SPI0_MOSI) | BV(SPI0_MISO);
471 SPI0_CR = BV(SPI_SWRST);
474 * Set SPI to master mode, fixed peripheral select, chip select directly connected to a peripheral device,
475 * SPI clock set to MCK, mode fault detection disabled, loopback disable, NPCS0 active, Delay between CS = 0
477 SPI0_MR = BV(SPI_MSTR) | BV(SPI_MODFDIS);
481 * At reset clock division factor is set to 0, that is
482 * *forbidden*. Set SPI clock to minimum to keep it valid.
484 SPI0_CSR0 = BV(SPI_NCPHA) | (255 << SPI_SCBR_SHIFT);
486 /* Disable all irqs */
487 SPI0_IDR = 0xFFFFFFFF;
488 /* Set the vector. */
489 AIC_SVR(SPI0_ID) = spi0_irq_handler;
490 /* Initialize to edge triggered with defined priority. */
491 AIC_SMR(SPI0_ID) = AIC_SRCTYPE_INT_EDGE_TRIGGERED | SERIRQ_PRIORITY;
492 /* Enable the USART IRQ */
493 AIC_IECR = BV(SPI0_ID);
494 PMC_PCER = BV(SPI0_ID);
496 /* Enable interrupt on tx buffer empty */
497 SPI0_IER = BV(SPI_TXEMPTY);
500 SPI0_CR = BV(SPI_SPIEN);
508 static void spi0_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
511 SPI0_CR = BV(SPI_SPIDIS);
513 /* Disable all irqs */
514 SPI0_IDR = 0xFFFFFFFF;
516 SER_SPI0_BUS_TXCLOSE;
518 /* Enable PIO on SPI pins */
519 PIOA_PER = BV(SPI0_SPCK) | BV(SPI0_MOSI) | BV(SPI0_MISO);
522 static void spi0_starttx(struct SerialHardware *_hw)
524 struct ArmSerial *hw = (struct ArmSerial *)_hw;
527 IRQ_SAVE_DISABLE(flags);
529 /* Send data only if the SPI is not already transmitting */
530 if (!hw->sending && !fifo_isempty(&ser_handles[SER_SPI0]->txfifo))
533 SPI0_TDR = fifo_pop(&ser_handles[SER_SPI0]->txfifo);
539 static void spi0_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
541 SPI0_CSR0 &= ~SPI_SCBR;
543 ASSERT((uint8_t)DIV_ROUND(CLOCK_FREQ, rate));
544 SPI0_CSR0 |= DIV_ROUND(CLOCK_FREQ, rate) << SPI_SCBR_SHIFT;
547 #if CPU_ARM_AT91SAM7X128 || CPU_ARM_AT91SAM7X256
549 static void spi1_init(UNUSED_ARG(struct SerialHardware *, _hw), UNUSED_ARG(struct Serial *, ser))
551 /* Disable PIO on SPI pins */
552 PIOA_PDR = BV(SPI1_SPCK) | BV(SPI1_MOSI) | BV(SPI1_MISO);
555 SPI1_CR = BV(SPI_SWRST);
558 * Set SPI to master mode, fixed peripheral select, chip select directly connected to a peripheral device,
559 * SPI clock set to MCK, mode fault detection disabled, loopback disable, NPCS0 active, Delay between CS = 0
561 SPI1_MR = BV(SPI_MSTR) | BV(SPI_MODFDIS);
565 * At reset clock division factor is set to 0, that is
566 * *forbidden*. Set SPI clock to minimum to keep it valid.
568 SPI1_CSR0 = BV(SPI_NCPHA) | (255 << SPI_SCBR_SHIFT);
570 /* Disable all irqs */
571 SPI1_IDR = 0xFFFFFFFF;
572 /* Set the vector. */
573 AIC_SVR(SPI1_ID) = spi1_irq_handler;
574 /* Initialize to edge triggered with defined priority. */
575 AIC_SMR(SPI1_ID) = AIC_SRCTYPE_INT_EDGE_TRIGGERED | SERIRQ_PRIORITY;
576 /* Enable the USART IRQ */
577 AIC_IECR = BV(SPI1_ID);
578 PMC_PCER = BV(SPI1_ID);
580 /* Enable interrupt on tx buffer empty */
581 SPI1_IER = BV(SPI_TXEMPTY);
584 SPI1_CR = BV(SPI_SPIEN);
592 static void spi1_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
595 SPI1_CR = BV(SPI_SPIDIS);
597 /* Disable all irqs */
598 SPI1_IDR = 0xFFFFFFFF;
600 SER_SPI1_BUS_TXCLOSE;
602 /* Enable PIO on SPI pins */
603 PIOA_PER = BV(SPI1_SPCK) | BV(SPI1_MOSI) | BV(SPI1_MISO);
606 static void spi1_starttx(struct SerialHardware *_hw)
608 struct ArmSerial *hw = (struct ArmSerial *)_hw;
611 IRQ_SAVE_DISABLE(flags);
613 /* Send data only if the SPI is not already transmitting */
614 if (!hw->sending && !fifo_isempty(&ser_spi1->txfifo))
617 SPI1_TDR = fifo_pop(&ser_spi1->txfifo);
623 static void spi1_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
625 SPI1_CSR0 &= ~SPI_SCBR;
627 ASSERT((uint8_t)DIV_ROUND(CLOCK_FREQ, rate));
628 SPI1_CSR0 |= DIV_ROUND(CLOCK_FREQ, rate) << SPI_SCBR_SHIFT;
632 static void spi_setparity(UNUSED_ARG(struct SerialHardware *, _hw), UNUSED_ARG(int, parity))
638 static bool tx_sending(struct SerialHardware* _hw)
640 struct ArmSerial *hw = (struct ArmSerial *)_hw;
644 // FIXME: move into compiler.h? Ditch?
646 #define C99INIT(name,val) .name = val
647 #elif defined(__GNUC__)
648 #define C99INIT(name,val) name: val
650 #warning No designated initializers, double check your code
651 #define C99INIT(name,val) (val)
655 * High-level interface data structures
657 static const struct SerialHardwareVT UART0_VT =
659 C99INIT(init, uart0_init),
660 C99INIT(cleanup, uart0_cleanup),
661 C99INIT(setBaudrate, uart0_setbaudrate),
662 C99INIT(setParity, uart0_setparity),
663 C99INIT(txStart, uart0_enabletxirq),
664 C99INIT(txSending, tx_sending),
667 static const struct SerialHardwareVT UART1_VT =
669 C99INIT(init, uart1_init),
670 C99INIT(cleanup, uart1_cleanup),
671 C99INIT(setBaudrate, uart1_setbaudrate),
672 C99INIT(setParity, uart1_setparity),
673 C99INIT(txStart, uart1_enabletxirq),
674 C99INIT(txSending, tx_sending),
677 static const struct SerialHardwareVT SPI0_VT =
679 C99INIT(init, spi0_init),
680 C99INIT(cleanup, spi0_cleanup),
681 C99INIT(setBaudrate, spi0_setbaudrate),
682 C99INIT(setParity, spi_setparity),
683 C99INIT(txStart, spi0_starttx),
684 C99INIT(txSending, tx_sending),
686 #if CPU_ARM_AT91SAM7X128 || CPU_ARM_AT91SAM7X256
687 static const struct SerialHardwareVT SPI1_VT =
689 C99INIT(init, spi1_init),
690 C99INIT(cleanup, spi1_cleanup),
691 C99INIT(setBaudrate, spi1_setbaudrate),
692 C99INIT(setParity, spi_setparity),
693 C99INIT(txStart, spi1_starttx),
694 C99INIT(txSending, tx_sending),
698 static struct ArmSerial UARTDescs[SER_CNT] =
702 C99INIT(table, &UART0_VT),
703 C99INIT(txbuffer, uart0_txbuffer),
704 C99INIT(rxbuffer, uart0_rxbuffer),
705 C99INIT(txbuffer_size, sizeof(uart0_txbuffer)),
706 C99INIT(rxbuffer_size, sizeof(uart0_rxbuffer)),
708 C99INIT(sending, false),
712 C99INIT(table, &UART1_VT),
713 C99INIT(txbuffer, uart1_txbuffer),
714 C99INIT(rxbuffer, uart1_rxbuffer),
715 C99INIT(txbuffer_size, sizeof(uart1_txbuffer)),
716 C99INIT(rxbuffer_size, sizeof(uart1_rxbuffer)),
718 C99INIT(sending, false),
723 C99INIT(table, &SPI0_VT),
724 C99INIT(txbuffer, spi0_txbuffer),
725 C99INIT(rxbuffer, spi0_rxbuffer),
726 C99INIT(txbuffer_size, sizeof(spi0_txbuffer)),
727 C99INIT(rxbuffer_size, sizeof(spi0_rxbuffer)),
729 C99INIT(sending, false),
731 #if CPU_ARM_AT91SAM7X128 || CPU_ARM_AT91SAM7X256
734 C99INIT(table, &SPI1_VT),
735 C99INIT(txbuffer, spi1_txbuffer),
736 C99INIT(rxbuffer, spi1_rxbuffer),
737 C99INIT(txbuffer_size, sizeof(spi1_txbuffer)),
738 C99INIT(rxbuffer_size, sizeof(spi1_rxbuffer)),
740 C99INIT(sending, false),
746 struct SerialHardware *ser_hw_getdesc(int unit)
748 ASSERT(unit < SER_CNT);
749 return &UARTDescs[unit].hw;
753 * Serial 0 TX interrupt handler
755 static void uart0_irq_tx(void)
759 struct FIFOBuffer * const txfifo = &ser_handles[SER_UART0]->txfifo;
761 if (fifo_isempty(txfifo))
764 * - Disable the TX empty interrupts
766 US0_IDR = BV(US_TXEMPTY);
768 UARTDescs[SER_UART0].sending = false;
772 char c = fifo_pop(txfifo);
773 SER_UART0_BUS_TXCHAR(c);
780 * Serial 0 RX complete interrupt handler.
782 static void uart0_irq_rx(void)
786 /* Should be read before US_CRS */
787 ser_handles[SER_UART0]->status |= US0_CSR & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
788 US0_CR = BV(US_RSTSTA);
791 struct FIFOBuffer * const rxfifo = &ser_handles[SER_UART0]->rxfifo;
793 if (fifo_isfull(rxfifo))
794 ser_handles[SER_UART0]->status |= SERRF_RXFIFOOVERRUN;
796 fifo_push(rxfifo, c);
802 * Serial IRQ dispatcher for USART0.
804 static void uart0_irq_dispatcher(void) __attribute__ ((interrupt));
805 static void uart0_irq_dispatcher(void)
807 if (US0_CSR & BV(US_RXRDY))
810 if (US0_CSR & BV(US_TXEMPTY))
813 /* Inform hw that we have served the IRQ */
818 * Serial 1 TX interrupt handler
820 static void uart1_irq_tx(void)
824 struct FIFOBuffer * const txfifo = &ser_handles[SER_UART1]->txfifo;
826 if (fifo_isempty(txfifo))
829 * - Disable the TX empty interrupts
831 US1_IDR = BV(US_TXEMPTY);
833 UARTDescs[SER_UART1].sending = false;
837 char c = fifo_pop(txfifo);
838 SER_UART1_BUS_TXCHAR(c);
845 * Serial 1 RX complete interrupt handler.
847 static void uart1_irq_rx(void)
851 /* Should be read before US_CRS */
852 ser_handles[SER_UART1]->status |= US1_CSR & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
853 US1_CR = BV(US_RSTSTA);
856 struct FIFOBuffer * const rxfifo = &ser_handles[SER_UART1]->rxfifo;
858 if (fifo_isfull(rxfifo))
859 ser_handles[SER_UART1]->status |= SERRF_RXFIFOOVERRUN;
861 fifo_push(rxfifo, c);
867 * Serial IRQ dispatcher for USART1.
869 static void uart1_irq_dispatcher(void) __attribute__ ((interrupt));
870 static void uart1_irq_dispatcher(void)
872 if (US1_CSR & BV(US_RXRDY))
875 if (US1_CSR & BV(US_TXEMPTY))
878 /* Inform hw that we have served the IRQ */
883 * SPI0 interrupt handler
885 static void spi0_irq_handler(void) __attribute__ ((interrupt));
886 static void spi0_irq_handler(void)
891 /* Read incoming byte. */
892 if (!fifo_isfull(&ser_handles[SER_SPI0]->rxfifo))
893 fifo_push(&ser_handles[SER_SPI0]->rxfifo, c);
897 ser_handles[SER_SPI0]->status |= SERRF_RXFIFOOVERRUN;
901 if (!fifo_isempty(&ser_handles[SER_SPI0]->txfifo))
902 SPI0_TDR = fifo_pop(&ser_handles[SER_SPI0]->txfifo);
904 UARTDescs[SER_SPI0].sending = false;
906 /* Inform hw that we have served the IRQ */
912 #if CPU_ARM_AT91SAM7X128 || CPU_ARM_AT91SAM7X256
914 * SPI1 interrupt handler
916 static void spi1_irq_handler(void) __attribute__ ((interrupt));
917 static void spi1_irq_handler(void)
922 /* Read incoming byte. */
923 if (!fifo_isfull(&ser_spi1->rxfifo))
924 fifo_push(&ser_spi1->rxfifo, c);
928 ser_spi1->status |= SERRF_RXFIFOOVERRUN;
932 if (!fifo_isempty(&ser_spi1->txfifo))
933 SPI1_TDR = fifo_pop(&ser_spi1->txfifo);
935 UARTDescs[SER_SPI1].sending = false;
937 /* Inform hw that we have served the IRQ */