Bug fix release

Dependents:   AntiTheftGPS XbeeReceive XbeeSend Superball_Ball2 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MODSERIAL.cpp Source File

MODSERIAL.cpp

00001 /*
00002     Copyright (c) 2010 Andy Kirkham
00003  
00004     Permission is hereby granted, free of charge, to any person obtaining a copy
00005     of this software and associated documentation files (the "Software"), to deal
00006     in the Software without restriction, including without limitation the rights
00007     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008     copies of the Software, and to permit persons to whom the Software is
00009     furnished to do so, subject to the following conditions:
00010  
00011     The above copyright notice and this permission notice shall be included in
00012     all copies or substantial portions of the Software.
00013  
00014     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020     THE SOFTWARE.
00021     
00022     @file          MODSERIAL.h 
00023     @purpose       Extends Serial to provide fully buffered IO
00024     @version       1.6
00025     @date          Nov 2010
00026     @author        Andy Kirkham    
00027 */
00028 
00029 #include "MODSERIAL.h"
00030 #include "MACROS.h"
00031 
00032 namespace AjK {
00033 
00034 MODSERIAL::MODSERIAL( PinName tx, PinName rx, const char* name ) : Serial( tx, rx, name )
00035 {
00036     init( MODSERIAL_DEFAULT_TX_BUFFER_SIZE, MODSERIAL_DEFAULT_RX_BUFFER_SIZE, rx );
00037 }
00038 
00039 MODSERIAL::MODSERIAL( PinName tx, PinName rx, int bufferSize, const char* name ) : Serial( tx, rx, name )
00040 {
00041     init( bufferSize, bufferSize, rx );
00042 }
00043 
00044 MODSERIAL::MODSERIAL( PinName tx, PinName rx, int txSize, int rxSize, const char* name ) : Serial( tx, rx, name )
00045 {
00046     init( txSize, rxSize, rx );
00047 }
00048 
00049 MODSERIAL::~MODSERIAL()
00050 {
00051     disableIrq();
00052     if ( buffer[0] != NULL) free((char *)buffer[0] );
00053     if ( buffer[1] != NULL) free((char *)buffer[1] );    
00054 }
00055 
00056 bool 
00057 MODSERIAL::txBufferFull( void ) 
00058 { 
00059     return MODSERIAL_TX_BUFFER_FULL; 
00060 }
00061 
00062 bool 
00063 MODSERIAL::rxBufferFull( void ) 
00064 { 
00065     return MODSERIAL_RX_BUFFER_FULL; 
00066 }
00067 
00068 bool 
00069 MODSERIAL::txBufferEmpty( void ) 
00070 { 
00071     return MODSERIAL_TX_BUFFER_EMPTY; 
00072 }
00073 
00074 bool 
00075 MODSERIAL::rxBufferEmpty( void ) 
00076 { 
00077     return MODSERIAL_RX_BUFFER_EMPTY; 
00078 }
00079 
00080 bool 
00081 MODSERIAL::txIsBusy( void ) 
00082 { 
00083     return ( _LSR & ( 3UL << 5 ) == 0 ) ? true : false; 
00084 } 
00085 
00086 void
00087 MODSERIAL::disableIrq( void )
00088 {
00089 
00090 #ifdef __LPC11UXX_H__
00091     NVIC_DisableIRQ( UART_IRQn );
00092 #else
00093     switch( _serial.index ) {
00094         case 0:   NVIC_DisableIRQ( UART0_IRQn ); break;
00095         case 1:   NVIC_DisableIRQ( UART1_IRQn ); break;
00096         case 2:   NVIC_DisableIRQ( UART2_IRQn ); break;
00097         case 3:   NVIC_DisableIRQ( UART3_IRQn ); break;
00098     }
00099 #endif
00100 }
00101 
00102 void
00103 MODSERIAL::enableIrq(void)
00104 {
00105 #ifdef __LPC11UXX_H__
00106     NVIC_EnableIRQ( UART_IRQn );
00107 #else
00108     switch( _serial.index ) {
00109         case 0:   NVIC_EnableIRQ( UART0_IRQn ); break;
00110         case 1:   NVIC_EnableIRQ( UART1_IRQn ); break;
00111         case 2:   NVIC_EnableIRQ( UART2_IRQn ); break;
00112         case 3:   NVIC_EnableIRQ( UART3_IRQn ); break;
00113     }
00114 #endif
00115 }
00116 
00117 int 
00118 MODSERIAL::rxDiscardLastChar( void )
00119 {
00120     // This function can only be called indirectly from
00121     // an rxCallback function. Therefore, we know we 
00122     // just placed a char into the buffer.
00123     char c = buffer[RxIrq][buffer_in[RxIrq]];
00124     
00125     if (buffer_count[RxIrq]) {        
00126         buffer_count[RxIrq]--;
00127         buffer_in[RxIrq]--;
00128         if (buffer_in[RxIrq] < 0) {
00129             buffer_in[RxIrq] = buffer_size[RxIrq] - 1;
00130         }
00131     }
00132     
00133     return (int)c;
00134 }
00135 
00136 
00137 }; // namespace AjK ends