Fork of the MODSERIAL library by Erik Olieman.
Fork of MODSERIAL by
Revision 33:9c93ce7cb9d8, committed 2013-07-11
- Comitter:
- Sissors
- Date:
- Thu Jul 11 13:34:53 2013 +0000
- Parent:
- 32:91e4dba7ebe2
- Child:
- 34:76793a84f9e5
- Commit message:
- v0.1 for KL25 support
;
Changed in this revision
--- a/FLUSH.cpp Mon Jun 10 19:06:21 2013 +0000 +++ b/FLUSH.cpp Thu Jul 11 13:34:53 2013 +0000 @@ -28,20 +28,20 @@ void MODSERIAL::flushBuffer(IrqType type) { - uint32_t ier = _IER; + uint32_t irq_req = MODSERIAL_IRQ_REG; switch(type) { - case TxIrq: _IER &= ~(1UL << 1); break; - case RxIrq: _IER &= ~(1UL << 0); break; + case TxIrq: DISABLE_TX_IRQ; break; + case RxIrq: DISABLE_RX_IRQ; break; } buffer_in[type] = 0; buffer_out[type] = 0; buffer_count[type] = 0; buffer_overflow[type] = 0; switch(type) { - case TxIrq: _FCR = MODSERIAL_FIFO_TX_RESET; break; - case RxIrq: _FCR = MODSERIAL_FIFO_RX_RESET; break; + case TxIrq: RESET_TX_FIFO; break; + case RxIrq: RESET_RX_FIFO; break; } - _IER = ier; + MODSERIAL_IRQ_REG = irq_req; } }; // namespace AjK ends
--- a/GETC.cpp Mon Jun 10 19:06:21 2013 +0000 +++ b/GETC.cpp Thu Jul 11 13:34:53 2013 +0000 @@ -32,8 +32,8 @@ // Note, we must block in this case and ignore bool "block" // so as to maintain compat with Mbed Serial. if (buffer_size[RxIrq] == 0 || buffer[RxIrq] == (char *)NULL) { - while(! MODSERIAL_RBR_HAS_DATA ) ; - return (int)(_RBR & 0xFF); + while(! MODSERIAL_READABLE ) ; + return (int)(MODSERIAL_READ_REG & 0xFF); } if (block) { while ( MODSERIAL_RX_BUFFER_EMPTY ) ; } // Blocks. @@ -50,10 +50,10 @@ // Temporarily disable the RX IRQ so that we do not re-enter // it under interrupts. if ( ! MODSERIAL_RX_BUFFER_FULL ) { - uint32_t ier = _IER; - _IER &= ~(1UL << 0); + uint32_t irq_reg = MODSERIAL_IRQ_REG; + DISABLE_RX_IRQ; isr_rx(); - _IER = ier; + MODSERIAL_IRQ_REG = irq_reg; } __disable_irq();
--- a/INIT.cpp Mon Jun 10 19:06:21 2013 +0000 +++ b/INIT.cpp Thu Jul 11 13:34:53 2013 +0000 @@ -23,29 +23,23 @@ #include "MODSERIAL.h" #include "MACROS.h" + #define MODSERIAL_FCR 0x08 + #define _FCR *((char *)_base+MODSERIAL_FCR) + + #define MODSERIAL_FIFO_ENABLE 1 +#define MODSERIAL_FIFO_RX_RESET 2 +#define MODSERIAL_FIFO_TX_RESET 4 + namespace AjK { void MODSERIAL::init( int txSize, int rxSize, PinName rx ) { - disableIrq(); + NVIC_DisableIRQ(_IRQ); + setBase(); callbackInfo.setSerial(this); - -#ifdef __LPC11UXX_H__ - - _base = LPC_USART; - -#else - switch( _serial.index ) { - case 0: _base = LPC_UART0; break; - case 1: _base = LPC_UART1; break; - case 2: _base = LPC_UART2; break; - case 3: _base = LPC_UART3; break; - default: _base = NULL; break; - } -#endif dmaSendChannel = -1; moddma_p = (void *)NULL; @@ -71,11 +65,15 @@ error("MODSERIAL must have a defined UART to function."); } - _FCR = MODSERIAL_FIFO_ENABLE | MODSERIAL_FIFO_RX_RESET | MODSERIAL_FIFO_TX_RESET; + + ENABLE_FIFO; + + //_FCR = MODSERIAL_FIFO_ENABLE | MODSERIAL_FIFO_RX_RESET | MODSERIAL_FIFO_TX_RESET; auto_detect_char = 0; - enableIrq(); + NVIC_EnableIRQ(_IRQ); } }; // namespace AjK ends +
--- a/ISR_RX.cpp Mon Jun 10 19:06:21 2013 +0000 +++ b/ISR_RX.cpp Thu Jul 11 13:34:53 2013 +0000 @@ -28,13 +28,15 @@ void MODSERIAL::isr_rx(void) { + DigitalOut led(LED2); + led = 1; if (! _base || buffer_size[RxIrq] == 0 || buffer[RxIrq] == (char *)NULL) { _isr[RxIrq].call(&this->callbackInfo); return; } - while( MODSERIAL_RBR_HAS_DATA ) { - rxc = (char)(_RBR & 0xFF); + while( MODSERIAL_READABLE ) { + rxc = (char)(MODSERIAL_READ_REG & 0xFF); if ( MODSERIAL_RX_BUFFER_FULL ) { buffer_overflow[RxIrq] = rxc; // Oh dear, no room in buffer. _isr[RxOvIrq].call(&this->callbackInfo);
--- a/ISR_TX.cpp Mon Jun 10 19:06:21 2013 +0000 +++ b/ISR_TX.cpp Thu Jul 11 13:34:53 2013 +0000 @@ -33,8 +33,8 @@ return; } - while (! MODSERIAL_TX_BUFFER_EMPTY && MODSERIAL_THR_HAS_SPACE ) { - _THR = txc = (uint8_t)(buffer[TxIrq][buffer_out[TxIrq]]); + while (! MODSERIAL_TX_BUFFER_EMPTY && MODSERIAL_WRITABLE ) { + MODSERIAL_WRITE_REG = txc = (uint8_t)(buffer[TxIrq][buffer_out[TxIrq]]); buffer_count[TxIrq]--; buffer_out[TxIrq]++; if (buffer_out[TxIrq] >= buffer_size[TxIrq]) { @@ -44,7 +44,7 @@ } if ( MODSERIAL_TX_BUFFER_EMPTY ) { - _IER = 1; + DISABLE_TX_IRQ; _isr[TxEmpty].call(&this->callbackInfo); } }
--- a/MACROS.h Mon Jun 10 19:06:21 2013 +0000 +++ b/MACROS.h Thu Jul 11 13:34:53 2013 +0000 @@ -23,48 +23,69 @@ #ifndef MODSERIAL_MACROS_H #define MODSERIAL_MACROS_H -#define MODSERIAL_RBR 0x00 -#define MODSERIAL_THR 0x00 -#define MODSERIAL_DLL 0x00 -#define MODSERIAL_IER 0x04 -#define MODSERIAL_DML 0x04 -#define MODSERIAL_IIR 0x08 -#define MODSERIAL_FCR 0x08 -#define MODSERIAL_LCR 0x0C -#define MODSERIAL_LSR 0x14 -#define MODSERIAL_SCR 0x1C -#define MODSERIAL_ACR 0x20 -#define MODSERIAL_ICR 0x24 -#define MODSERIAL_FDR 0x28 -#define MODSERIAL_TER 0x30 - -#define MODSERIAL_LSR_RDR (1UL << 0) -#define MODSERIAL_LSR_OE (1UL << 1) -#define MODSERIAL_LSR_PE (1UL << 2) -#define MODSERIAL_LSR_FE (1UL << 3) -#define MODSERIAL_LSR_BR (1UL << 4) -#define MODSERIAL_LSR_THRE (1UL << 5) -#define MODSERIAL_LSR_TEMT (1UL << 6) -#define MODSERIAL_LSR_RXFE (1UL << 7) - -#define MODSERIAL_FIFO_ENABLE 1 -#define MODSERIAL_FIFO_RX_RESET 2 -#define MODSERIAL_FIFO_TX_RESET 4 - -#define _RBR *((char *)_base+MODSERIAL_RBR) -#define _THR *((char *)_base+MODSERIAL_THR) -#define _IIR *((char *)_base+MODSERIAL_IIR) -#define _IER *((char *)_base+MODSERIAL_IER) -#define _LSR *((char *)_base+MODSERIAL_LSR) -#define _FCR *((char *)_base+MODSERIAL_FCR) #define MODSERIAL_TX_BUFFER_EMPTY (buffer_count[TxIrq]==0) #define MODSERIAL_RX_BUFFER_EMPTY (buffer_count[RxIrq]==0) #define MODSERIAL_TX_BUFFER_FULL (buffer_count[TxIrq]==buffer_size[TxIrq]) #define MODSERIAL_RX_BUFFER_FULL (buffer_count[RxIrq]==buffer_size[RxIrq]) -#define MODSERIAL_THR_HAS_SPACE ((int)_LSR&MODSERIAL_LSR_THRE) -#define MODSERIAL_TEMT_IS_EMPTY ((int)_LSR&MODSERIAL_LSR_TEMT) -#define MODSERIAL_RBR_HAS_DATA ((int)_LSR&MODSERIAL_LSR_RDR) + +#ifdef TARGET_LPC1768 + +#define MODSERIAL_IRQ_REG ((LPC_UART_TypeDef*)_base)->IER +#define DISABLE_TX_IRQ MODSERIAL_IRQ_REG &= ~(1UL << 1) +#define DISABLE_RX_IRQ MODSERIAL_IRQ_REG &= ~(1UL << 0) +#define ENABLE_TX_IRQ MODSERIAL_IRQ_REG |= (1UL << 1) +#define ENABLE_RX_IRQ MODSERIAL_IRQ_REG |= (1UL << 0) + +#define RESET_TX_FIFO ((LPC_UART_TypeDef*)_base)->FCR |= (1UL<<2) +#define RESET_RX_FIFO ((LPC_UART_TypeDef*)_base)->FCR |= (1UL<<1) +#define ENABLE_FIFO ((LPC_UART_TypeDef*)_base)->FCR = (1UL<<0) + (1UL<<1) + (1UL<<2) + +#define MODSERIAL_READ_REG ((LPC_UART_TypeDef*)_base)->RBR +#define MODSERIAL_WRITE_REG ((LPC_UART_TypeDef*)_base)->THR +#define MODSERIAL_READABLE ((((LPC_UART_TypeDef*)_base)->LSR & (1UL<<0)) != 0) +#define MODSERIAL_WRITABLE ((((LPC_UART_TypeDef*)_base)->LSR & (1UL<<5)) != 0) + #endif + +#ifdef TARGET_LPC11U24 + +#define MODSERIAL_IRQ_REG ((LPC_UART_TypeDef*)_base)->IER +#define DISABLE_TX_IRQ MODSERIAL_IRQ_REG &= ~(1UL << 1) +#define DISABLE_RX_IRQ MODSERIAL_IRQ_REG &= ~(1UL << 0) +#define ENABLE_TX_IRQ MODSERIAL_IRQ_REG |= (1UL << 1) +#define ENABLE_RX_IRQ MODSERIAL_IRQ_REG |= (1UL << 0) + +#define RESET_TX_FIFO ((LPC_UART_TypeDef*)_base)->FCR |= (1UL<<2) +#define RESET_RX_FIFO ((LPC_UART_TypeDef*)_base)->FCR |= (1UL<<1) +#define ENABLE_FIFO ((LPC_UART_TypeDef*)_base)->FCR = (1UL<<0) + (1UL<<1) + (1UL<<2) + +#define MODSERIAL_READ_REG ((LPC_UART_TypeDef*)_base)->RBR +#define MODSERIAL_WRITE_REG ((LPC_UART_TypeDef*)_base)->THR +#define MODSERIAL_READABLE ((((LPC_UART_TypeDef*)_base)->LSR & (1UL<<0)) != 0) +#define MODSERIAL_WRITABLE ((((LPC_UART_TypeDef*)_base)->LSR & (1UL<<5)) != 0) + +#endif + +#ifdef TARGET_KL25Z + +#define MODSERIAL_IRQ_REG ((UART_Type*)_base)->C2 +#define DISABLE_TX_IRQ MODSERIAL_IRQ_REG &= ~(1UL << UART_C2_TIE_SHIFT) +#define DISABLE_RX_IRQ MODSERIAL_IRQ_REG &= ~(1UL << UART_C2_RIE_SHIFT) +#define ENABLE_TX_IRQ MODSERIAL_IRQ_REG |= (1UL << UART_C2_TIE_SHIFT) +#define ENABLE_RX_IRQ MODSERIAL_IRQ_REG |= (1UL << UART_C2_RIE_SHIFT) + +#define RESET_TX_FIFO while(0 == 1) +#define RESET_RX_FIFO while(0 == 1) +#define ENABLE_FIFO while(0 == 1) + +#define MODSERIAL_READ_REG ((UART_Type*)_base)->D +#define MODSERIAL_WRITE_REG ((UART_Type*)_base)->D +#define MODSERIAL_READABLE ((((UART_Type*)_base)->S1 & (1UL<<5)) != 0) +#define MODSERIAL_WRITABLE ((((UART_Type*)_base)->S1 & (1UL<<7)) != 0) + +#endif + +#endif
--- a/MODSERIAL.cpp Mon Jun 10 19:06:21 2013 +0000 +++ b/MODSERIAL.cpp Thu Jul 11 13:34:53 2013 +0000 @@ -48,74 +48,33 @@ MODSERIAL::~MODSERIAL() { - disableIrq(); + NVIC_DisableIRQ(_IRQ); if ( buffer[0] != NULL) free((char *)buffer[0] ); if ( buffer[1] != NULL) free((char *)buffer[1] ); } -bool -MODSERIAL::txBufferFull( void ) +bool MODSERIAL::txBufferFull( void ) { return MODSERIAL_TX_BUFFER_FULL; } -bool -MODSERIAL::rxBufferFull( void ) +bool MODSERIAL::rxBufferFull( void ) { return MODSERIAL_RX_BUFFER_FULL; } -bool -MODSERIAL::txBufferEmpty( void ) +bool MODSERIAL::txBufferEmpty( void ) { return MODSERIAL_TX_BUFFER_EMPTY; } -bool -MODSERIAL::rxBufferEmpty( void ) +bool MODSERIAL::rxBufferEmpty( void ) { return MODSERIAL_RX_BUFFER_EMPTY; } -bool -MODSERIAL::txIsBusy( void ) -{ - return ( _LSR & ( 3UL << 5 ) == 0 ) ? true : false; -} -void -MODSERIAL::disableIrq( void ) -{ - -#ifdef __LPC11UXX_H__ - NVIC_DisableIRQ( UART_IRQn ); -#else - switch( _serial.index ) { - case 0: NVIC_DisableIRQ( UART0_IRQn ); break; - case 1: NVIC_DisableIRQ( UART1_IRQn ); break; - case 2: NVIC_DisableIRQ( UART2_IRQn ); break; - case 3: NVIC_DisableIRQ( UART3_IRQn ); break; - } -#endif -} - -void -MODSERIAL::enableIrq(void) -{ -#ifdef __LPC11UXX_H__ - NVIC_EnableIRQ( UART_IRQn ); -#else - switch( _serial.index ) { - case 0: NVIC_EnableIRQ( UART0_IRQn ); break; - case 1: NVIC_EnableIRQ( UART1_IRQn ); break; - case 2: NVIC_EnableIRQ( UART2_IRQn ); break; - case 3: NVIC_EnableIRQ( UART3_IRQn ); break; - } -#endif -} - -int -MODSERIAL::rxDiscardLastChar( void ) +int MODSERIAL::rxDiscardLastChar( void ) { // This function can only be called indirectly from // an rxCallback function. Therefore, we know we
--- a/MODSERIAL.h Mon Jun 10 19:06:21 2013 +0000 +++ b/MODSERIAL.h Thu Jul 11 13:34:53 2013 +0000 @@ -681,16 +681,7 @@ */ char rxGetLastChar(void) { return rxc; } - /** - * Function: txIsBusy - * - * If the Uart is still actively sending characters this - * function will return true. - * - * @ingroup API - * @return bool - */ - bool txIsBusy(void); + /** * Function: autoDetectChar @@ -887,19 +878,7 @@ */ void isr_rx(void); - /** - * Disable the interrupts for this Uart. - * @ingroup INTERNALS - */ - void disableIrq(void); - - /** - * Enable the interrupts for this Uart. - * @ingroup INTERNALS - */ - void enableIrq(void); - - /** + /** * Get a character from the RX buffer * @ingroup INTERNALS * @param bool True to block (wait for input) @@ -1080,6 +1059,26 @@ #endif // MODDMA_H + +//DEVICE SPECIFIC FUNCTIONS: + void setBase( void ); + + IRQn_Type _IRQ; + + public: + /** + * Function: txIsBusy + * + * If the Uart is still actively sending characters this + * function will return true. + * + * @ingroup API + * @return bool + */ + bool txIsBusy(void); + + + }; }; // namespace AjK ends
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MODSERIAL_KL25Z.cpp Thu Jul 11 13:34:53 2013 +0000 @@ -0,0 +1,18 @@ +#ifdef TARGET_KL25Z +#include "MODSERIAL.h" + +void MODSERIAL::setBase(void ) { +switch( _serial.index ) { + case 0: _base = UART0; _IRQ = UART0_IRQn; break; + case 1: _base = UART1; _IRQ = UART1_IRQn; break; + case 2: _base = UART2; _IRQ = UART2_IRQn; break; + default: _base = NULL; _IRQ = (IRQn_Type)NULL; break; + } +} + +bool MODSERIAL::txIsBusy( void ) +{ + return ( ((UART_Type*)_base)->S1 & ( 1UL << 6 ) == 0 ) ? true : false; +} +#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MODSERIAL_LPC11U24.cpp Thu Jul 11 13:34:53 2013 +0000 @@ -0,0 +1,13 @@ +#ifdef TARGET_LPC11U24 +#include "MODSERIAL.h" + +void MODSERIAL::setBase(void ) { + _base = LPC_USART; + _IRQ = UART_IRQn; +} + +bool MODSERIAL::txIsBusy( void ) +{ + return ( ((LPC_USART_Type*)_base)->LSR & ( 3UL << 5 ) == 0 ) ? true : false; +} +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MODSERIAL_LPC1768.cpp Thu Jul 11 13:34:53 2013 +0000 @@ -0,0 +1,20 @@ +#ifdef TARGET_LPC1768 +#include "MODSERIAL.h" + + +void MODSERIAL::setBase(void ) { +switch( _serial.index ) { + case 0: _base = LPC_UART0; _IRQ = UART0_IRQn; break; + case 1: _base = LPC_UART1; _IRQ = UART1_IRQn; break; + case 2: _base = LPC_UART2; _IRQ = UART2_IRQn; break; + case 3: _base = LPC_UART3; _IRQ = UART3_IRQn; break; + default: _base = NULL; _IRQ = (IRQn_Type)NULL; break; + } +} + +bool MODSERIAL::txIsBusy( void ) +{ + return ( ((LPC_UART_TypeDef*)_base)->LSR & ( 3UL << 5 ) == 0 ) ? true : false; +} + +#endif
--- a/PUTC.cpp Mon Jun 10 19:06:21 2013 +0000 +++ b/PUTC.cpp Thu Jul 11 13:34:53 2013 +0000 @@ -32,17 +32,17 @@ // Note, we must block in this case and ignore bool "block" // so as to maintain compat with Mbed Serial. if (buffer[TxIrq] == (char *)NULL || buffer_size[TxIrq] == 0) { - while (! MODSERIAL_THR_HAS_SPACE) ; // Wait for space in the TX FIFO. - _THR = (uint32_t)c; + while (! MODSERIAL_WRITABLE) ; // Wait for space in the TX FIFO. + MODSERIAL_WRITE_REG = (uint32_t)c; return 0; } - - if ( MODSERIAL_THR_HAS_SPACE && MODSERIAL_TX_BUFFER_EMPTY && dmaSendChannel == -1 ) { - _THR = (uint32_t)c; + + if ( MODSERIAL_WRITABLE && MODSERIAL_TX_BUFFER_EMPTY && dmaSendChannel == -1 ) { + MODSERIAL_WRITE_REG = (uint32_t)c; } else { if (block) { - uint32_t ier = _IER; _IER = 1; + uint32_t irq_reg = MODSERIAL_IRQ_REG; DISABLE_TX_IRQ; while ( MODSERIAL_TX_BUFFER_FULL ) { // Blocks! // If putc() is called from an ISR then we are stuffed // because in an ISR no bytes from the TX buffer will @@ -56,14 +56,14 @@ // are blocking. isr_tx(false); } - _IER = ier; + MODSERIAL_IRQ_REG = irq_reg; } else if( MODSERIAL_TX_BUFFER_FULL ) { buffer_overflow[TxIrq] = c; // Oh dear, no room in buffer. _isr[TxOvIrq].call(&this->callbackInfo); return -1; } - _IER &= ~2; + DISABLE_TX_IRQ; buffer[TxIrq][buffer_in[TxIrq]] = c; __disable_irq(); buffer_count[TxIrq]++; @@ -72,7 +72,7 @@ if (buffer_in[TxIrq] >= buffer_size[TxIrq]) { buffer_in[TxIrq] = 0; } - _IER |= 2; + ENABLE_TX_IRQ; } return 0;
--- a/RESIZE.cpp Mon Jun 10 19:06:21 2013 +0000 +++ b/RESIZE.cpp Thu Jul 11 13:34:53 2013 +0000 @@ -35,7 +35,7 @@ if (buffer_size[type] == size) return rval; // Make sure the ISR cannot use the buffers while we are manipulating them. - disableIrq(); + NVIC_DisableIRQ(_IRQ); // If the requested buffer size is larger than the current size, // attempt to create a new buffer and use it. @@ -47,7 +47,7 @@ } // Start the ISR system again with the new buffers. - enableIrq(); + NVIC_EnableIRQ(_IRQ); return rval; }
--- a/example1.cpp Mon Jun 10 19:06:21 2013 +0000 +++ b/example1.cpp Thu Jul 11 13:34:53 2013 +0000 @@ -10,7 +10,7 @@ DigitalOut led1(LED1); DigitalOut led2(LED2); -DigitalOut led3(LED3); +DigitalOut led3(LED2); DigitalOut led4(LED4); MODSERIAL pc(USBTX, USBRX); @@ -57,7 +57,7 @@ pc.baud(PC_BAUD); // Use a deliberatly slow baud to fill up the TX buffer - uart.baud(1200); + uart.baud(300); uart.attach(&txCallback, MODSERIAL::TxIrq); uart.attach(&rxCallback, MODSERIAL::RxIrq); @@ -68,18 +68,19 @@ led1 = 1; // Show start of sending with LED1. - for (int loop = 0; loop < 512; loop++) { + for (int loop = 0; loop < 2; loop++) { uart.printf("%c", c); c++; if (c > 'Z') c = 'A'; } - + pc.printf("%c", '!'); led1 = 0; // Show the end of sending by switching off LED1. // End program. Flash LED4. Notice how LED 2 and 3 continue // to flash for a short period while the interrupt system // continues to send the characters left in the TX buffer. - + wait(2); + pc.printf("\n\n\r%02X", UART2->C2); while(1) { led4 = !led4; wait(0.25);