ser_avr.c

Go to the documentation of this file.
00001 
00042 #include "hw/hw_ser.h"  /* Required for bus macros overrides */
00043 #include <hw/hw_cpufreq.h>  /* CPU_FREQ */
00044 
00045 #include "cfg/cfg_ser.h"
00046 
00047 #include <cfg/macros.h> /* DIV_ROUND */
00048 #include <cfg/debug.h>
00049 #include <cfg/cfg_arch.h> // ARCH_NIGHTTEST
00050 
00051 #include <drv/ser.h>
00052 #include <drv/ser_p.h>
00053 #include <drv/timer.h>
00054 
00055 #include <struct/fifobuf.h>
00056 
00057 #include <avr/io.h>
00058 
00059 #if defined(__AVR_LIBC_VERSION__) && (__AVR_LIBC_VERSION__ >= 10400UL)
00060     #include <avr/interrupt.h>
00061 #else
00062     #include <avr/signal.h>
00063 #endif
00064 
00065 
00066 #if !CONFIG_SER_HWHANDSHAKE
00067 
00071     #define RTS_ON      do {} while (0)
00072     #define RTS_OFF     do {} while (0)
00073     #define IS_CTS_ON   true
00074     #define EIMSKF_CTS  0 
00075     /*\}*/
00076 #endif
00077 
00078 #if CPU_AVR_ATMEGA1281
00079     #define BIT_RXCIE0 RXCIE0
00080     #define BIT_RXEN0  RXEN0
00081     #define BIT_TXEN0  TXEN0
00082     #define BIT_UDRIE0 UDRIE0
00083 
00084     #define BIT_RXCIE1 RXCIE1
00085     #define BIT_RXEN1  RXEN1
00086     #define BIT_TXEN1  TXEN1
00087     #define BIT_UDRIE1 UDRIE1
00088 #else
00089     #define BIT_RXCIE0 RXCIE
00090     #define BIT_RXEN0  RXEN
00091     #define BIT_TXEN0  TXEN
00092     #define BIT_UDRIE0 UDRIE
00093 
00094     #define BIT_RXCIE1 RXCIE
00095     #define BIT_RXEN1  RXEN
00096     #define BIT_TXEN1  TXEN
00097     #define BIT_UDRIE1 UDRIE
00098 #endif
00099 
00100 
00121 #ifndef SER_UART0_BUS_TXINIT
00122 
00128     #define SER_UART0_BUS_TXINIT do { \
00129         UCSR0B = BV(BIT_RXCIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0); \
00130     } while (0)
00131 #endif
00132 
00133 #ifndef SER_UART0_BUS_TXBEGIN
00134 
00140     #define SER_UART0_BUS_TXBEGIN do { \
00141         UCSR0B = BV(BIT_RXCIE0) | BV(BIT_UDRIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0); \
00142     } while (0)
00143 #endif
00144 
00145 #ifndef SER_UART0_BUS_TXCHAR
00146 
00149     #define SER_UART0_BUS_TXCHAR(c) do { \
00150         UDR0 = (c); \
00151     } while (0)
00152 #endif
00153 
00154 #ifndef SER_UART0_BUS_TXEND
00155 
00162     #define SER_UART0_BUS_TXEND do { \
00163         UCSR0B = BV(BIT_RXCIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0); \
00164     } while (0)
00165 #endif
00166 
00167 #ifndef SER_UART0_BUS_TXOFF
00168 
00175     #ifdef __doxygen__
00176     #define SER_UART0_BUS_TXOFF
00177     #endif
00178 #endif
00179 
00180 #ifndef SER_UART1_BUS_TXINIT
00181 
00182     #define SER_UART1_BUS_TXINIT do { \
00183         UCSR1B = BV(BIT_RXCIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1); \
00184     } while (0)
00185 #endif
00186 #ifndef SER_UART1_BUS_TXBEGIN
00187 
00188     #define SER_UART1_BUS_TXBEGIN do { \
00189         UCSR1B = BV(BIT_RXCIE1) | BV(BIT_UDRIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1); \
00190     } while (0)
00191 #endif
00192 #ifndef SER_UART1_BUS_TXCHAR
00193 
00194     #define SER_UART1_BUS_TXCHAR(c) do { \
00195         UDR1 = (c); \
00196     } while (0)
00197 #endif
00198 #ifndef SER_UART1_BUS_TXEND
00199 
00200     #define SER_UART1_BUS_TXEND do { \
00201         UCSR1B = BV(BIT_RXCIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1); \
00202     } while (0)
00203 #endif
00204 #ifndef SER_UART1_BUS_TXOFF
00205 
00210     #ifdef __doxygen__
00211     #define SER_UART1_BUS_TXOFF
00212     #endif
00213 #endif
00214 /*\}*/
00215 
00216 
00225 #ifndef SER_SPI_BUS_TXINIT
00226 
00230     #define SER_SPI_BUS_TXINIT
00231 #endif
00232 
00233 #ifndef SER_SPI_BUS_TXCLOSE
00234 
00238     #define SER_SPI_BUS_TXCLOSE
00239 #endif
00240 /*\}*/
00241 
00242 
00243 /* SPI port and pin configuration */
00244 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA103 || CPU_AVR_ATMEGA1281
00245     #define SPI_PORT      PORTB
00246     #define SPI_DDR       DDRB
00247     #define SPI_SS_BIT    PB0
00248     #define SPI_SCK_BIT   PB1
00249     #define SPI_MOSI_BIT  PB2
00250     #define SPI_MISO_BIT  PB3
00251 #elif CPU_AVR_ATMEGA8
00252     #define SPI_PORT      PORTB
00253     #define SPI_DDR       DDRB
00254     #define SPI_SS_BIT    PB2
00255     #define SPI_SCK_BIT   PB5
00256     #define SPI_MOSI_BIT  PB3
00257     #define SPI_MISO_BIT  PB4
00258 #else
00259     #error Unknown architecture
00260 #endif
00261 
00262 /* USART register definitions */
00263 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA1281
00264     #define AVR_HAS_UART1 1
00265 #elif CPU_AVR_ATMEGA8
00266     #define AVR_HAS_UART1 0
00267     #define UCSR0A UCSRA
00268     #define UCSR0B UCSRB
00269     #define UCSR0C UCSRC
00270     #define UDR0   UDR
00271     #define UBRR0L UBRRL
00272     #define UBRR0H UBRRH
00273     #define SIG_UART0_DATA SIG_UART_DATA
00274     #define SIG_UART0_RECV SIG_UART_RECV
00275     #define SIG_UART0_TRANS SIG_UART_TRANS
00276 #elif CPU_AVR_ATMEGA103
00277     #define AVR_HAS_UART1 0
00278     #define UCSR0B UCR
00279     #define UDR0   UDR
00280     #define UCSR0A USR
00281     #define UBRR0L UBRR
00282     #define SIG_UART0_DATA SIG_UART_DATA
00283     #define SIG_UART0_RECV SIG_UART_RECV
00284     #define SIG_UART0_TRANS SIG_UART_TRANS
00285 #else
00286     #error Unknown architecture
00287 #endif
00288 
00289 
00290 /* From the high-level serial driver */
00291 extern struct Serial *ser_handles[SER_CNT];
00292 
00293 /* TX and RX buffers */
00294 static unsigned char uart0_txbuffer[CONFIG_UART0_TXBUFSIZE];
00295 static unsigned char uart0_rxbuffer[CONFIG_UART0_RXBUFSIZE];
00296 #if AVR_HAS_UART1
00297     static unsigned char uart1_txbuffer[CONFIG_UART1_TXBUFSIZE];
00298     static unsigned char uart1_rxbuffer[CONFIG_UART1_RXBUFSIZE];
00299 #endif
00300 static unsigned char spi_txbuffer[CONFIG_SPI_TXBUFSIZE];
00301 static unsigned char spi_rxbuffer[CONFIG_SPI_RXBUFSIZE];
00302 
00303 
00320 struct AvrSerial
00321 {
00322     struct SerialHardware hw;
00323     volatile bool sending;
00324 };
00325 
00326 
00327 
00328 /*
00329  * Callbacks
00330  */
00331 static void uart0_init(
00332     UNUSED_ARG(struct SerialHardware *, _hw),
00333     UNUSED_ARG(struct Serial *, ser))
00334 {
00335     SER_UART0_BUS_TXINIT;
00336     RTS_ON;
00337     SER_STROBE_INIT;
00338 }
00339 
00340 static void uart0_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
00341 {
00342     UCSR0B = 0;
00343 }
00344 
00345 static void uart0_enabletxirq(struct SerialHardware *_hw)
00346 {
00347     struct AvrSerial *hw = (struct AvrSerial *)_hw;
00348 
00349     /*
00350      * WARNING: racy code here!  The tx interrupt sets hw->sending to false
00351      * when it runs with an empty fifo.  The order of statements in the
00352      * if-block matters.
00353      */
00354     if (!hw->sending)
00355     {
00356         hw->sending = true;
00357         SER_UART0_BUS_TXBEGIN;
00358     }
00359 }
00360 
00361 static void uart0_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
00362 {
00363     /* Compute baud-rate period */
00364     uint16_t period = DIV_ROUND(CPU_FREQ / 16UL, rate) - 1;
00365 
00366 #if !CPU_AVR_ATMEGA103
00367     UBRR0H = (period) >> 8;
00368 #endif
00369     UBRR0L = (period);
00370 
00371     //DB(kprintf("uart0_setbaudrate(rate=%lu): period=%d\n", rate, period);)
00372 }
00373 
00374 static void uart0_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
00375 {
00376 #if !CPU_AVR_ATMEGA103
00377     UCSR0C = (UCSR0C & ~(BV(UPM01) | BV(UPM00))) | ((parity) << UPM00);
00378 #endif
00379 }
00380 
00381 #if AVR_HAS_UART1
00382 
00383 static void uart1_init(
00384     UNUSED_ARG(struct SerialHardware *, _hw),
00385     UNUSED_ARG(struct Serial *, ser))
00386 {
00387     SER_UART1_BUS_TXINIT;
00388     RTS_ON;
00389     SER_STROBE_INIT;
00390 }
00391 
00392 static void uart1_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
00393 {
00394     UCSR1B = 0;
00395 }
00396 
00397 static void uart1_enabletxirq(struct SerialHardware *_hw)
00398 {
00399     struct AvrSerial *hw = (struct AvrSerial *)_hw;
00400 
00401     /*
00402      * WARNING: racy code here!  The tx interrupt
00403      * sets hw->sending to false when it runs with
00404      * an empty fifo.  The order of the statements
00405      * in the if-block matters.
00406      */
00407     if (!hw->sending)
00408     {
00409         hw->sending = true;
00410         SER_UART1_BUS_TXBEGIN;
00411     }
00412 }
00413 
00414 static void uart1_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
00415 {
00416     /* Compute baud-rate period */
00417     uint16_t period = DIV_ROUND(CPU_FREQ / 16UL, rate) - 1;
00418 
00419     UBRR1H = (period) >> 8;
00420     UBRR1L = (period);
00421 
00422     //DB(kprintf("uart1_setbaudrate(rate=%ld): period=%d\n", rate, period);)
00423 }
00424 
00425 static void uart1_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
00426 {
00427     UCSR1C = (UCSR1C & ~(BV(UPM11) | BV(UPM10))) | ((parity) << UPM10);
00428 }
00429 
00430 #endif // AVR_HAS_UART1
00431 
00432 static void spi_init(UNUSED_ARG(struct SerialHardware *, _hw), UNUSED_ARG(struct Serial *, ser))
00433 {
00434     /*
00435      * Set MOSI and SCK ports out, MISO in.
00436      *
00437      * The ATmega64/128 datasheet explicitly states that the input/output
00438      * state of the SPI pins is not significant, as when the SPI is
00439      * active the I/O port are overrided.
00440      * This is *blatantly FALSE*.
00441      *
00442      * Moreover, the MISO pin on the board_kc *must* be in high impedance
00443      * state even when the SPI is off, because the line is wired together
00444      * with the KBus serial RX, and the transmitter of the slave boards
00445      * would be unable to drive the line.
00446      */
00447     ATOMIC(SPI_DDR |= (BV(SPI_MOSI_BIT) | BV(SPI_SCK_BIT)));
00448 
00449     /*
00450      * If the SPI master mode is activated and the SS pin is in input and tied low,
00451      * the SPI hardware will automatically switch to slave mode!
00452      * For proper communication this pins should therefore be:
00453      * - as output
00454      * - as input but tied high forever!
00455      * This driver set the pin as output.
00456      */
00457     #warning FIXME:SPI SS pin set as output for proper operation, check schematics for possible conflicts.
00458     ATOMIC(SPI_DDR |= BV(SPI_SS_BIT));
00459 
00460     ATOMIC(SPI_DDR &= ~BV(SPI_MISO_BIT));
00461     /* Enable SPI, IRQ on, Master */
00462     SPCR = BV(SPE) | BV(SPIE) | BV(MSTR);
00463 
00464     /* Set data order */
00465     #if CONFIG_SPI_DATA_ORDER == SER_LSB_FIRST
00466         SPCR |= BV(DORD);
00467     #endif
00468 
00469     /* Set SPI clock rate */
00470     #if CONFIG_SPI_CLOCK_DIV == 128
00471         SPCR |= (BV(SPR1) | BV(SPR0));
00472     #elif (CONFIG_SPI_CLOCK_DIV == 64 || CONFIG_SPI_CLOCK_DIV == 32)
00473         SPCR |= BV(SPR1);
00474     #elif (CONFIG_SPI_CLOCK_DIV == 16 || CONFIG_SPI_CLOCK_DIV == 8)
00475         SPCR |= BV(SPR0);
00476     #elif (CONFIG_SPI_CLOCK_DIV == 4 || CONFIG_SPI_CLOCK_DIV == 2)
00477         // SPR0 & SDPR1 both at 0
00478     #else
00479         #error Unsupported SPI clock division factor.
00480     #endif
00481 
00482     /* Set SPI2X bit (spi double frequency) */
00483     #if (CONFIG_SPI_CLOCK_DIV == 128 || CONFIG_SPI_CLOCK_DIV == 64 \
00484       || CONFIG_SPI_CLOCK_DIV == 16 || CONFIG_SPI_CLOCK_DIV == 4)
00485         SPSR &= ~BV(SPI2X);
00486     #elif (CONFIG_SPI_CLOCK_DIV == 32 || CONFIG_SPI_CLOCK_DIV == 8 || CONFIG_SPI_CLOCK_DIV == 2)
00487         SPSR |= BV(SPI2X);
00488     #else
00489         #error Unsupported SPI clock division factor.
00490     #endif
00491 
00492     /* Set clock polarity */
00493     #if CONFIG_SPI_CLOCK_POL == 1
00494         SPCR |= BV(CPOL);
00495     #endif
00496 
00497     /* Set clock phase */
00498     #if CONFIG_SPI_CLOCK_PHASE == 1
00499         SPCR |= BV(CPHA);
00500     #endif
00501     SER_SPI_BUS_TXINIT;
00502 
00503     SER_STROBE_INIT;
00504 }
00505 
00506 static void spi_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
00507 {
00508     SPCR = 0;
00509 
00510     SER_SPI_BUS_TXCLOSE;
00511 
00512     /* Set all pins as inputs */
00513     ATOMIC(SPI_DDR &= ~(BV(SPI_MISO_BIT) | BV(SPI_MOSI_BIT) | BV(SPI_SCK_BIT) | BV(SPI_SS_BIT)));
00514 }
00515 
00516 static void spi_starttx(struct SerialHardware *_hw)
00517 {
00518     struct AvrSerial *hw = (struct AvrSerial *)_hw;
00519 
00520     cpu_flags_t flags;
00521     IRQ_SAVE_DISABLE(flags);
00522 
00523     /* Send data only if the SPI is not already transmitting */
00524     if (!hw->sending && !fifo_isempty(&ser_handles[SER_SPI]->txfifo))
00525     {
00526         hw->sending = true;
00527         SPDR = fifo_pop(&ser_handles[SER_SPI]->txfifo);
00528     }
00529 
00530     IRQ_RESTORE(flags);
00531 }
00532 
00533 static void spi_setbaudrate(
00534     UNUSED_ARG(struct SerialHardware *, _hw),
00535     UNUSED_ARG(unsigned long, rate))
00536 {
00537     // nop
00538 }
00539 
00540 static void spi_setparity(UNUSED_ARG(struct SerialHardware *, _hw), UNUSED_ARG(int, parity))
00541 {
00542     // nop
00543 }
00544 
00545 static bool tx_sending(struct SerialHardware* _hw)
00546 {
00547     struct AvrSerial *hw = (struct AvrSerial *)_hw;
00548     return hw->sending;
00549 }
00550 
00551 
00552 
00553 // FIXME: move into compiler.h?  Ditch?
00554 #if COMPILER_C99
00555     #define C99INIT(name,val) .name = val
00556 #elif defined(__GNUC__)
00557     #define C99INIT(name,val) name: val
00558 #else
00559     #warning No designated initializers, double check your code
00560     #define C99INIT(name,val) (val)
00561 #endif
00562 
00563 /*
00564  * High-level interface data structures
00565  */
00566 static const struct SerialHardwareVT UART0_VT =
00567 {
00568     C99INIT(init, uart0_init),
00569     C99INIT(cleanup, uart0_cleanup),
00570     C99INIT(setBaudrate, uart0_setbaudrate),
00571     C99INIT(setParity, uart0_setparity),
00572     C99INIT(txStart, uart0_enabletxirq),
00573     C99INIT(txSending, tx_sending),
00574 };
00575 
00576 #if AVR_HAS_UART1
00577 static const struct SerialHardwareVT UART1_VT =
00578 {
00579     C99INIT(init, uart1_init),
00580     C99INIT(cleanup, uart1_cleanup),
00581     C99INIT(setBaudrate, uart1_setbaudrate),
00582     C99INIT(setParity, uart1_setparity),
00583     C99INIT(txStart, uart1_enabletxirq),
00584     C99INIT(txSending, tx_sending),
00585 };
00586 #endif // AVR_HAS_UART1
00587 
00588 static const struct SerialHardwareVT SPI_VT =
00589 {
00590     C99INIT(init, spi_init),
00591     C99INIT(cleanup, spi_cleanup),
00592     C99INIT(setBaudrate, spi_setbaudrate),
00593     C99INIT(setParity, spi_setparity),
00594     C99INIT(txStart, spi_starttx),
00595     C99INIT(txSending, tx_sending),
00596 };
00597 
00598 static struct AvrSerial UARTDescs[SER_CNT] =
00599 {
00600     {
00601         C99INIT(hw, ) {
00602             C99INIT(table, &UART0_VT),
00603             C99INIT(txbuffer, uart0_txbuffer),
00604             C99INIT(rxbuffer, uart0_rxbuffer),
00605             C99INIT(txbuffer_size, sizeof(uart0_txbuffer)),
00606             C99INIT(rxbuffer_size, sizeof(uart0_rxbuffer)),
00607         },
00608         C99INIT(sending, false),
00609     },
00610 #if AVR_HAS_UART1
00611     {
00612         C99INIT(hw, ) {
00613             C99INIT(table, &UART1_VT),
00614             C99INIT(txbuffer, uart1_txbuffer),
00615             C99INIT(rxbuffer, uart1_rxbuffer),
00616             C99INIT(txbuffer_size, sizeof(uart1_txbuffer)),
00617             C99INIT(rxbuffer_size, sizeof(uart1_rxbuffer)),
00618         },
00619         C99INIT(sending, false),
00620     },
00621 #endif
00622     {
00623         C99INIT(hw, ) {
00624             C99INIT(table, &SPI_VT),
00625             C99INIT(txbuffer, spi_txbuffer),
00626             C99INIT(rxbuffer, spi_rxbuffer),
00627             C99INIT(txbuffer_size, sizeof(spi_txbuffer)),
00628             C99INIT(rxbuffer_size, sizeof(spi_rxbuffer)),
00629         },
00630         C99INIT(sending, false),
00631     }
00632 };
00633 
00634 struct SerialHardware *ser_hw_getdesc(int unit)
00635 {
00636     ASSERT(unit < SER_CNT);
00637     return &UARTDescs[unit].hw;
00638 }
00639 
00640 
00641 /*
00642  * Interrupt handlers
00643  */
00644 
00645 #if CONFIG_SER_HWHANDSHAKE
00646 
00648 DECLARE_ISR(SIG_CTS)
00649 {
00650     // Re-enable UDR empty interrupt and TX, then disable CTS interrupt
00651     UCSR0B = BV(BIT_RXCIE0) | BV(BIT_UDRIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0);
00652     EIMSK &= ~EIMSKF_CTS;
00653 }
00654 
00655 #endif // CONFIG_SER_HWHANDSHAKE
00656 
00657 
00661 DECLARE_ISR(USART0_UDRE_vect)
00662 {
00663     SER_STROBE_ON;
00664 
00665     struct FIFOBuffer * const txfifo = &ser_handles[SER_UART0]->txfifo;
00666 
00667     if (fifo_isempty(txfifo))
00668     {
00669         SER_UART0_BUS_TXEND;
00670 #ifndef SER_UART0_BUS_TXOFF
00671         UARTDescs[SER_UART0].sending = false;
00672 #endif
00673     }
00674 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA103
00675     else if (!IS_CTS_ON)
00676     {
00677         // Disable rx interrupt and tx, enable CTS interrupt
00678         // UNTESTED
00679         UCSR0B = BV(BIT_RXCIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0);
00680         EIFR |= EIMSKF_CTS;
00681         EIMSK |= EIMSKF_CTS;
00682     }
00683 #endif
00684     else
00685     {
00686         char c = fifo_pop(txfifo);
00687         SER_UART0_BUS_TXCHAR(c);
00688     }
00689 
00690     SER_STROBE_OFF;
00691 }
00692 
00693 #ifdef SER_UART0_BUS_TXOFF
00694 
00709 DECLARE_ISR(SIG_UART0_TRANS)
00710 {
00711     SER_STROBE_ON;
00712 
00713     struct FIFOBuffer * const txfifo = &ser_handles[SER_UART0]->txfifo;
00714     if (fifo_isempty(txfifo))
00715     {
00716         SER_UART0_BUS_TXOFF;
00717         UARTDescs[SER_UART0].sending = false;
00718     }
00719     else
00720         UCSR0B = BV(BIT_RXCIE0) | BV(BIT_UDRIE0) | BV(BIT_RXEN0) | BV(BIT_TXEN0);
00721 
00722     SER_STROBE_OFF;
00723 }
00724 #endif /* SER_UART0_BUS_TXOFF */
00725 
00726 
00727 #if AVR_HAS_UART1
00728 
00732 DECLARE_ISR(USART1_UDRE_vect)
00733 {
00734     SER_STROBE_ON;
00735 
00736     struct FIFOBuffer * const txfifo = &ser_handles[SER_UART1]->txfifo;
00737 
00738     if (fifo_isempty(txfifo))
00739     {
00740         SER_UART1_BUS_TXEND;
00741 #ifndef SER_UART1_BUS_TXOFF
00742         UARTDescs[SER_UART1].sending = false;
00743 #endif
00744     }
00745 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA103
00746     else if (!IS_CTS_ON)
00747     {
00748         // Disable rx interrupt and tx, enable CTS interrupt
00749         // UNTESTED
00750         UCSR1B = BV(BIT_RXCIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1);
00751         EIFR |= EIMSKF_CTS;
00752         EIMSK |= EIMSKF_CTS;
00753     }
00754 #endif
00755     else
00756     {
00757         char c = fifo_pop(txfifo);
00758         SER_UART1_BUS_TXCHAR(c);
00759     }
00760 
00761     SER_STROBE_OFF;
00762 }
00763 
00764 #ifdef SER_UART1_BUS_TXOFF
00765 
00770 DECLARE_ISR(USART1_TX_vect)
00771 {
00772     SER_STROBE_ON;
00773 
00774     struct FIFOBuffer * const txfifo = &ser_handles[SER_UART1]->txfifo;
00775     if (fifo_isempty(txfifo))
00776     {
00777         SER_UART1_BUS_TXOFF;
00778         UARTDescs[SER_UART1].sending = false;
00779     }
00780     else
00781         UCSR1B = BV(BIT_RXCIE1) | BV(BIT_UDRIE1) | BV(BIT_RXEN1) | BV(BIT_TXEN1);
00782 
00783     SER_STROBE_OFF;
00784 }
00785 #endif /* SER_UART1_BUS_TXOFF */
00786 
00787 #endif // AVR_HAS_UART1
00788 
00789 
00805 DECLARE_ISR(USART0_RX_vect)
00806 {
00807     SER_STROBE_ON;
00808 
00809     /* Disable Recv complete IRQ */
00810     //UCSR0B &= ~BV(RXCIE);
00811     //IRQ_ENABLE;
00812 
00813     /* Should be read before UDR */
00814     ser_handles[SER_UART0]->status |= UCSR0A & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
00815 
00816     /* To clear the RXC flag we must _always_ read the UDR even when we're
00817      * not going to accept the incoming data, otherwise a new interrupt
00818      * will occur once the handler terminates.
00819      */
00820     char c = UDR0;
00821     struct FIFOBuffer * const rxfifo = &ser_handles[SER_UART0]->rxfifo;
00822 
00823     if (fifo_isfull(rxfifo))
00824         ser_handles[SER_UART0]->status |= SERRF_RXFIFOOVERRUN;
00825     else
00826     {
00827         fifo_push(rxfifo, c);
00828 #if CONFIG_SER_HWHANDSHAKE
00829         if (fifo_isfull(rxfifo))
00830             RTS_OFF;
00831 #endif
00832     }
00833 
00834     /* Reenable receive complete int */
00835     //IRQ_DISABLE;
00836     //UCSR0B |= BV(RXCIE);
00837 
00838     SER_STROBE_OFF;
00839 }
00840 
00841 
00842 #if AVR_HAS_UART1
00843 
00855 DECLARE_ISR(USART1_RX_vect)
00856 {
00857     SER_STROBE_ON;
00858 
00859     /* Disable Recv complete IRQ */
00860     //UCSR1B &= ~BV(RXCIE);
00861     //IRQ_ENABLE;
00862 
00863     /* Should be read before UDR */
00864     ser_handles[SER_UART1]->status |= UCSR1A & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
00865 
00866     /* To avoid an IRQ storm, we must _always_ read the UDR even when we're
00867      * not going to accept the incoming data
00868      */
00869     char c = UDR1;
00870     struct FIFOBuffer * const rxfifo = &ser_handles[SER_UART1]->rxfifo;
00871     //ASSERT_VALID_FIFO(rxfifo);
00872 
00873     if (UNLIKELY(fifo_isfull(rxfifo)))
00874         ser_handles[SER_UART1]->status |= SERRF_RXFIFOOVERRUN;
00875     else
00876     {
00877         fifo_push(rxfifo, c);
00878 #if CONFIG_SER_HWHANDSHAKE
00879         if (fifo_isfull(rxfifo))
00880             RTS_OFF;
00881 #endif
00882     }
00883     /* Re-enable receive complete int */
00884     //IRQ_DISABLE;
00885     //UCSR1B |= BV(RXCIE);
00886 
00887     SER_STROBE_OFF;
00888 }
00889 
00890 #endif // AVR_HAS_UART1
00891 
00892 
00896 DECLARE_ISR(SIG_SPI)
00897 {
00898     SER_STROBE_ON;
00899 
00900     /* Read incoming byte. */
00901     if (!fifo_isfull(&ser_handles[SER_SPI]->rxfifo))
00902         fifo_push(&ser_handles[SER_SPI]->rxfifo, SPDR);
00903     /*
00904      * FIXME
00905     else
00906         ser_handles[SER_SPI]->status |= SERRF_RXFIFOOVERRUN;
00907     */
00908 
00909     /* Send */
00910     if (!fifo_isempty(&ser_handles[SER_SPI]->txfifo))
00911         SPDR = fifo_pop(&ser_handles[SER_SPI]->txfifo);
00912     else
00913         UARTDescs[SER_SPI].sending = false;
00914 
00915     SER_STROBE_OFF;
00916 }