From 21b907c94677776684b86440ec081a0252cd02e2 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Sun, 1 Jul 2012 14:10:13 +0000 Subject: Arduino Mega support. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@4379 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/hal/platforms/AVR/serial_lld.c | 81 ++++++++++++++++++++++++++++++++------- 1 file changed, 67 insertions(+), 14 deletions(-) (limited to 'os/hal/platforms/AVR/serial_lld.c') diff --git a/os/hal/platforms/AVR/serial_lld.c b/os/hal/platforms/AVR/serial_lld.c index e070d5448..c35cdca4b 100644 --- a/os/hal/platforms/AVR/serial_lld.c +++ b/os/hal/platforms/AVR/serial_lld.c @@ -62,7 +62,7 @@ SerialDriver SD2; */ static const SerialConfig default_config = { UBRR(SERIAL_DEFAULT_BITRATE), - (1 << UCSZ1) | (1 << UCSZ0) + USART_CHAR_SIZE_8 }; /*===========================================================================*/ @@ -71,12 +71,31 @@ static const SerialConfig default_config = { static void set_error(uint8_t sra, SerialDriver *sdp) { chnflags_t sts = 0; + uint8_t dor = 0; + uint8_t upe = 0; + uint8_t fe = 0; - if (sra & (1 << DOR)) +#if USE_AVR_USART0 + if (&SD1 == sdp) { + dor = (1 << DOR0); + upe = (1 << UPE0); + fe = (1 << FE0); + } +#endif + +#if USE_AVR_USART1 + if (&SD2 == sdp) { + dor = (1 << DOR1); + upe = (1 << UPE1); + fe = (1 << FE1); + } +#endif + + if (sra & dor) sts |= SD_OVERRUN_ERROR; - if (sra & (1 << UPE)) + if (sra & upe) sts |= SD_PARITY_ERROR; - if (sra & (1 << FE)) + if (sra & fe) sts |= SD_FRAMING_ERROR; chSysLockFromIsr(); chnAddFlagsI(sdp, sts); @@ -87,7 +106,7 @@ static void set_error(uint8_t sra, SerialDriver *sdp) { static void notify1(GenericQueue *qp) { (void)qp; - UCSR0B |= (1 << UDRIE); + UCSR0B |= (1 << UDRIE0); } /** @@ -100,8 +119,25 @@ static void usart0_init(const SerialConfig *config) { UBRR0L = config->sc_brr; UBRR0H = config->sc_brr >> 8; UCSR0A = 0; - UCSR0B = (1 << RXEN) | (1 << TXEN) | (1 << RXCIE); - UCSR0C = config->sc_csrc; + UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0); + switch (config->sc_bits_per_char) { + case USART_CHAR_SIZE_5: + UCSR0C = 0; + break; + case USART_CHAR_SIZE_6: + UCSR0C = (1 << UCSZ00); + break; + case USART_CHAR_SIZE_7: + UCSR0C = (1 << UCSZ01); + break; + case USART_CHAR_SIZE_9: + UCSR0B |= (1 << UCSZ02); + UCSR0C = (1 << UCSZ00) | (1 << UCSZ01); + break; + case USART_CHAR_SIZE_8: + default: + UCSR0C = (1 << UCSZ00) | (1 << UCSZ01); + } } /** @@ -119,7 +155,7 @@ static void usart0_deinit(void) { static void notify2(GenericQueue *qp) { (void)qp; - UCSR1B |= (1 << UDRIE); + UCSR1B |= (1 << UDRIE1); } /** @@ -132,8 +168,25 @@ static void usart1_init(const SerialConfig *config) { UBRR1L = config->sc_brr; UBRR1H = config->sc_brr >> 8; UCSR1A = 0; - UCSR1B = (1 << RXEN) | (1 << TXEN) | (1 << RXCIE); - UCSR1C = config->sc_csrc; + UCSR1B = (1 << RXEN1) | (1 << TXEN1) | (1 << RXCIE1); + switch (config->sc_bits_per_char) { + case USART_CHAR_SIZE_5: + UCSR1C = 0; + break; + case USART_CHAR_SIZE_6: + UCSR1C = (1 << UCSZ10); + break; + case USART_CHAR_SIZE_7: + UCSR1C = (1 << UCSZ11); + break; + case USART_CHAR_SIZE_9: + UCSR1B |= (1 << UCSZ12); + UCSR1C = (1 << UCSZ10) | (1 << UCSZ11); + break; + case USART_CHAR_SIZE_8: + default: + UCSR1C = (1 << UCSZ10) | (1 << UCSZ11); + } } /** @@ -163,7 +216,7 @@ CH_IRQ_HANDLER(USART0_RX_vect) { CH_IRQ_PROLOGUE(); sra = UCSR0A; - if (sra & ((1 << DOR) | (1 << UPE) | (1 << FE))) + if (sra & ((1 << DOR0) | (1 << UPE0) | (1 << FE0))) set_error(sra, &SD1); chSysLockFromIsr(); sdIncomingDataI(&SD1, UDR0); @@ -186,7 +239,7 @@ CH_IRQ_HANDLER(USART0_UDRE_vect) { b = sdRequestDataI(&SD1); chSysUnlockFromIsr(); if (b < Q_OK) - UCSR0B &= ~(1 << UDRIE); + UCSR0B &= ~(1 << UDRIE0); else UDR0 = b; @@ -206,7 +259,7 @@ CH_IRQ_HANDLER(USART1_RX_vect) { CH_IRQ_PROLOGUE(); sra = UCSR1A; - if (sra & ((1 << DOR) | (1 << UPE) | (1 << FE))) + if (sra & ((1 << DOR1) | (1 << UPE1) | (1 << FE1))) set_error(sra, &SD2); chSysLockFromIsr(); sdIncomingDataI(&SD2, UDR1); @@ -229,7 +282,7 @@ CH_IRQ_HANDLER(USART1_UDRE_vect) { b = sdRequestDataI(&SD2); chSysUnlockFromIsr(); if (b < Q_OK) - UCSR1B &= ~(1 << UDRIE); + UCSR1B &= ~(1 << UDRIE1); else UDR1 = b; -- cgit v1.2.3