diff --git a/demos/ARM7-AT91SAM7X-GCC/Makefile b/demos/ARM7-AT91SAM7X-GCC/Makefile index 4622e181f..34f5ff8b2 100644 --- a/demos/ARM7-AT91SAM7X-GCC/Makefile +++ b/demos/ARM7-AT91SAM7X-GCC/Makefile @@ -53,8 +53,9 @@ CSRC = ${PORTSRC} \ ${KERNSRC} \ ${TESTSRC} \ ../../os/io/pal.c \ - ../../os/ports/GCC/ARM7/AT91SAM7X/pal_lld.c \ - ../../os/ports/GCC/ARM7/AT91SAM7X/sam7x_serial.c \ + ../../os/io/serial.c \ + ../../os/io/platforms/AT91SAM7X/pal_lld.c \ + ../../os/io/platforms/AT91SAM7X/serial_lld.c \ at91lib/aic.c \ board.c main.c @@ -88,8 +89,9 @@ ASMSRC = $(PORTASM) \ INCDIR = $(PORTINC) $(KERNINC) $(TESTINC) \ ../../os/io \ - ../../os/ports/GCC/ARM7/AT91SAM7X \ - ../../os/various + ../../os/io/platforms/AT91SAM7X \ + ../../os/various \ + ../../os/ports/GCC/ARM7/AT91SAM7X # # Project, sources and paths diff --git a/demos/ARM7-AT91SAM7X-GCC/board.c b/demos/ARM7-AT91SAM7X-GCC/board.c index aac51cf0c..fb52c8f3b 100644 --- a/demos/ARM7-AT91SAM7X-GCC/board.c +++ b/demos/ARM7-AT91SAM7X-GCC/board.c @@ -19,12 +19,11 @@ #include #include +#include #include "board.h" #include "at91lib/aic.h" -#include - /* * FIQ Handler weak symbol defined in vectors.s. */ @@ -169,7 +168,7 @@ void hwinit1(void) { /* * Serial driver initialization, RTS/CTS pins enabled for USART0 only. */ - serial_init(AT91C_AIC_PRIOR_HIGHEST - 2, AT91C_AIC_PRIOR_HIGHEST - 2); + sdInit(); AT91C_BASE_PIOA->PIO_PDR = AT91C_PA3_RTS0 | AT91C_PA4_CTS0; AT91C_BASE_PIOA->PIO_ASR = AT91C_PIO_PA3 | AT91C_PIO_PA4; AT91C_BASE_PIOA->PIO_PPUDR = AT91C_PIO_PA3 | AT91C_PIO_PA4; diff --git a/demos/ARM7-AT91SAM7X-GCC/main.c b/demos/ARM7-AT91SAM7X-GCC/main.c index 614b649cd..d1ca61cc7 100644 --- a/demos/ARM7-AT91SAM7X-GCC/main.c +++ b/demos/ARM7-AT91SAM7X-GCC/main.c @@ -19,10 +19,10 @@ #include #include +#include #include #include "board.h" -#include static WORKING_AREA(waThread1, 64); static msg_t Thread1(void *arg) { @@ -42,6 +42,11 @@ static msg_t Thread1(void *arg) { */ int main(int argc, char **argv) { + /* + * Activates the communication port 1 using the driver default configuration. + */ + sdStart(&COM1, NULL); + /* * Creates the blinker thread. */ @@ -53,7 +58,7 @@ int main(int argc, char **argv) { while (TRUE) { chThdSleepMilliseconds(500); if (!palReadPad(IOPORT_B, PIOB_SW1)) - chFDDWrite(&COM1, (uint8_t *)"Hello World!\r\n", 14); + sdWrite(&COM1, (uint8_t *)"Hello World!\r\n", 14); if (!palReadPad(IOPORT_B, PIOB_SW2)) TestThread(&COM1); } diff --git a/os/io/platforms/AT91SAM7X/serial_lld.c b/os/io/platforms/AT91SAM7X/serial_lld.c index 40b00483d..e053aac13 100644 --- a/os/io/platforms/AT91SAM7X/serial_lld.c +++ b/os/io/platforms/AT91SAM7X/serial_lld.c @@ -29,8 +29,21 @@ #include "at91lib/aic.h" +#if USE_SAM7X_USART0 || defined(__DOXYGEN__) +/** @brief USART0 serial driver identifier.*/ +SerialDriver COM1; +#endif + +#if USE_SAM7X_USART1 || defined(__DOXYGEN__) +/** @brief USART1 serial driver identifier.*/ +SerialDriver COM2; +#endif + /** @brief Driver default configuration.*/ static const SerialDriverConfig default_config = { + 38400, + AT91C_US_USMODE_NORMAL | AT91C_US_CLKS_CLOCK | + AT91C_US_CHRL_8_BITS | AT91C_US_PAR_NONE | AT91C_US_NBSTOP_1_BIT }; /*===========================================================================*/ @@ -69,13 +82,106 @@ void usart_init(AT91PS_USART u, const SerialDriverConfig *config) { */ void usart_deinit(AT91PS_USART u) { + /* Disables IRQ sources and stop operations.*/ + u->US_IDR = 0xFFFFFFFF; + u->US_CR = AT91C_US_RSTRX | AT91C_US_RSTTX | AT91C_US_RSTSTA; + u->US_MR = 0; + u->US_RTOR = 0; + u->US_TTGR = 0; } +/** + * @brief Error handling routine. + * @param[in] err USART CSR register value + * @param[in] sdp communication channel associated to the USART + */ +static void set_error(AT91_REG csr, SerialDriver *sdp) { + sdflags_t sts = 0; + + if (csr & AT91C_US_OVRE) + sts |= SD_OVERRUN_ERROR; + if (csr & AT91C_US_PARE) + sts |= SD_PARITY_ERROR; + if (csr & AT91C_US_FRAME) + sts |= SD_FRAMING_ERROR; + if (csr & AT91C_US_RXBRK) + sts |= SD_BREAK_DETECTED; + chSysLockFromIsr(); + sdAddFlagsI(sdp, sts); + chSysUnlockFromIsr(); +} + +#if defined(__GNU__) +__attribute__((noinline)) +#endif +/** + * @brief Common IRQ handler. + * @param[in] u pointer to an USART I/O block + * @param[in] com communication channel associated to the USART + */ +static void serve_interrupt(AT91PS_USART u, SerialDriver *sdp) { + + if (u->US_CSR & AT91C_US_RXRDY) { + chSysLockFromIsr(); + sdIncomingDataI(sdp, u->US_RHR); + chSysUnlockFromIsr(); + } + if (u->US_CSR & AT91C_US_TXRDY) { + chSysLockFromIsr(); + msg_t b = sdRequestDataI(sdp); + chSysUnlockFromIsr(); + if (b < Q_OK) + u->US_IDR = AT91C_US_TXRDY; + else + u->US_THR = b; + } + if (u->US_CSR & (AT91C_US_OVRE | AT91C_US_FRAME | AT91C_US_PARE | AT91C_US_RXBRK)) { + set_error(u->US_CSR, sdp); + u->US_CR = AT91C_US_RSTSTA; + } + AT91C_BASE_AIC->AIC_EOICR = 0; +} + +#if USE_SAM7X_USART0 || defined(__DOXYGEN__) +static void notify1(void) { + + AT91C_BASE_US0->US_IER = AT91C_US_TXRDY; +} +#endif + +#if USE_SAM7X_USART1 || defined(__DOXYGEN__) +static void notify2(void) { + + AT91C_BASE_US1->US_IER = AT91C_US_TXRDY; +} +#endif /*===========================================================================*/ /* Low Level Driver interrupt handlers. */ /*===========================================================================*/ +#if USE_SAM7X_USART0 || defined(__DOXYGEN__) +CH_IRQ_HANDLER(USART0IrqHandler) { + + CH_IRQ_PROLOGUE(); + + serve_interrupt(AT91C_BASE_US0, &COM1); + + CH_IRQ_EPILOGUE(); +} +#endif + +#if USE_SAM7X_USART1 || defined(__DOXYGEN__) +CH_IRQ_HANDLER(USART1IrqHandler) { + + CH_IRQ_PROLOGUE(); + + serve_interrupt(AT91C_BASE_US1, &COM2); + + CH_IRQ_EPILOGUE(); +} +#endif + /*===========================================================================*/ /* Low Level Driver exported functions. */ /*===========================================================================*/ @@ -85,6 +191,25 @@ void usart_deinit(AT91PS_USART u) { */ void sd_lld_init(void) { +#if USE_SAM7X_USART0 + sdObjectInit(&COM1, NULL, notify1); + AT91C_BASE_PIOA->PIO_PDR = AT91C_PA0_RXD0 | AT91C_PA1_TXD0; + AT91C_BASE_PIOA->PIO_ASR = AT91C_PIO_PA0 | AT91C_PIO_PA1; + AT91C_BASE_PIOA->PIO_PPUDR = AT91C_PIO_PA0 | AT91C_PIO_PA1; + AIC_ConfigureIT(AT91C_ID_US0, + AT91C_AIC_SRCTYPE_HIGH_LEVEL | SAM7X_USART0_PRIORITY, + USART0IrqHandler); +#endif + +#if USE_SAM7X_USART1 + sdObjectInit(&COM2, NULL, notify2); + AT91C_BASE_PIOA->PIO_PDR = AT91C_PA5_RXD1 | AT91C_PA6_TXD1; + AT91C_BASE_PIOA->PIO_ASR = AT91C_PIO_PA5 | AT91C_PIO_PA6; + AT91C_BASE_PIOA->PIO_PPUDR = AT91C_PIO_PA5 | AT91C_PIO_PA6; + AIC_ConfigureIT(AT91C_ID_US1, + AT91C_AIC_SRCTYPE_HIGH_LEVEL | SAM7X_USART1_PRIORITY, + USART1IrqHandler); +#endif } /** @@ -100,6 +225,28 @@ void sd_lld_start(SerialDriver *sdp, const SerialDriverConfig *config) { if (config == NULL) config = &default_config; +#if USE_SAM7X_USART0 + if (&COM1 == sdp) { + /* Starts the clock and clears possible sources of immediate interrupts.*/ + AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_US0); + /* USART initialization.*/ + usart_init(AT91C_BASE_US0, config); + /* Enables associated interrupt vector.*/ + AIC_EnableIT(AT91C_ID_US0); + return; + } +#endif +#if USE_SAM7X_USART1 + if (&COM2 == sdp) { + /* Starts the clock and clears possible sources of immediate interrupts.*/ + AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_US1); + /* USART initialization.*/ + usart_init(AT91C_BASE_US1, config); + /* Enables associated interrupt vector.*/ + AIC_EnableIT(AT91C_ID_US1); + return; + } +#endif } /** @@ -111,6 +258,22 @@ void sd_lld_start(SerialDriver *sdp, const SerialDriverConfig *config) { */ void sd_lld_stop(SerialDriver *sdp) { +#if USE_LPC214x_UART1 + if (&COM1 == sdp) { + usart_deinit(AT91C_BASE_US0); + AT91C_BASE_PMC->PMC_PCDR = (1 << AT91C_ID_US0); + AIC_DisableIT(AT91C_ID_US0); + return; + } +#endif +#if USE_LPC214x_UART2 + if (&COM2 == sdp) { + usart_deinit(AT91C_BASE_US1); + AT91C_BASE_PMC->PMC_PCDR = (1 << AT91C_ID_US1); + AIC_DisableIT(AT91C_ID_US1); + return; + } +#endif } /** @} */ diff --git a/os/io/platforms/AT91SAM7X/serial_lld.h b/os/io/platforms/AT91SAM7X/serial_lld.h index dfd41398d..6156c3933 100644 --- a/os/io/platforms/AT91SAM7X/serial_lld.h +++ b/os/io/platforms/AT91SAM7X/serial_lld.h @@ -59,6 +59,20 @@ #define USE_SAM7X_USART1 TRUE #endif +/** + * @brief UART1 interrupt priority level setting. + */ +#if !defined(SAM7X_USART0_PRIORITY) || defined(__DOXYGEN__) +#define SAM7X_USART0_PRIORITY (AT91C_AIC_PRIOR_HIGHEST - 2) +#endif + +/** + * @brief UART2 interrupt priority level setting. + */ +#if !defined(SAM7X_USART1_PRIORITY) || defined(__DOXYGEN__) +#define SAM7X_USART1_PRIORITY (AT91C_AIC_PRIOR_HIGHEST - 2) +#endif + /*===========================================================================*/ /* Unsupported event flags and custom events. */ /*===========================================================================*/ @@ -124,11 +138,11 @@ typedef struct { /*===========================================================================*/ /** @cond never*/ -#if !defined(USE_SAM7X_USART0) -extern FullDuplexDriver COM1; +#if defined(USE_SAM7X_USART0) +extern SerialDriver COM1; #endif -#if !defined(USE_SAM7X_USART1) -extern FullDuplexDriver COM2; +#if defined(USE_SAM7X_USART1) +extern SerialDriver COM2; #endif #ifdef __cplusplus diff --git a/os/io/platforms/LPC214x/serial_lld.h b/os/io/platforms/LPC214x/serial_lld.h index 04328e64b..b543d844c 100644 --- a/os/io/platforms/LPC214x/serial_lld.h +++ b/os/io/platforms/LPC214x/serial_lld.h @@ -89,7 +89,6 @@ #define LPC214x_UART2_PRIORITY 2 #endif - /*===========================================================================*/ /* Unsupported event flags and custom events. */ /*===========================================================================*/