Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
rs232.cpp
00001 #include "mbed.h" 00002 #include "rs232.h" 00003 00004 00005 00006 //--------------------------------------- 00007 // Hardware recources 00008 //--------------------------------------- 00009 Serial rs232(p13, p14); // TXD, RXD 00010 Timeout rs232Timeout; 00011 DigitalOut rs232RTS(p12); // RTS 00012 DigitalIn rs232CTS(p11); // CTS 00013 00014 00015 //--------------------------------------- 00016 // Prototypes 00017 //--------------------------------------- 00018 static void RS232_reset( void ); 00019 static void RS232_rxCallback( void ); 00020 static void RS232_rxTimeout( void ); 00021 00022 00023 //--------------------------------------- 00024 // Internal variables 00025 //--------------------------------------- 00026 static char RS232Buffer[RS232_BUFFER_LENGTH]; 00027 static sRS232_handler RS232_handler; 00028 00029 00030 00031 //--------------------------------------- 00032 // External variables 00033 //--------------------------------------- 00034 00035 00036 00037 //--------------------------------------- 00038 // Global Functions 00039 //--------------------------------------- 00040 void RS232_init( void ) 00041 { 00042 rs232RTS = 0; 00043 rs232.baud(RS232_BAUDRATE); 00044 rs232.format(RS232_BITS, Serial::None, RS232_STOPBIT ); 00045 RS232_reset(); 00046 rs232.attach( &RS232_rxCallback, Serial::RxIrq ); // call RS232_rx() on every RX interrupt 00047 } 00048 00049 00050 00051 // Return 1: Sending was successfull 00052 // Return 0: Could not send data, eithter to many bytes or zero bytes 00053 int RS232_sendData( char *ptrData, int NumBytes ) 00054 { 00055 int i; 00056 00057 if( (NumBytes == 0) || (NumBytes >= RS232_BUFFER_LENGTH ) ) 00058 { 00059 return 0; 00060 } 00061 00062 // Send data 00063 for( i = 0; i < NumBytes; i++ ) 00064 { 00065 while( !rs232.writeable() ); 00066 rs232.putc(*ptrData); 00067 ptrData++; 00068 }//for 00069 00070 return 1; 00071 } 00072 00073 00074 // Return 1: New data available, get pointer to buffer and number of bytes to read 00075 // Return 0: No new data available 00076 int RS232_receiveData( char *& ptrData, int *ptrNumBytes ) 00077 { 00078 // Dummy operation to avoid compiler warning 00079 ptrData++; 00080 00081 if( RS232_handler.mode != RS232_MODE_RX_READY ) 00082 { 00083 *ptrNumBytes = 0; 00084 return 0; 00085 } 00086 00087 *ptrNumBytes = RS232_handler.bytesToRead; 00088 ptrData = &RS232Buffer[0]; 00089 00090 RS232_reset(); 00091 return 1; 00092 } 00093 00094 00095 00096 void RS232_setRTS( int level) 00097 { 00098 if( level ) 00099 { 00100 rs232RTS = 0; 00101 } 00102 else 00103 { 00104 rs232RTS = 1; 00105 } 00106 00107 } 00108 00109 00110 int RS232_getCTS( void ) 00111 { 00112 return rs232CTS; 00113 } 00114 00115 00116 //--------------------------------------- 00117 // Internal Functions 00118 //--------------------------------------- 00119 00120 static void RS232_reset( void ) 00121 { 00122 RS232_handler.ptrReadPositon = &RS232Buffer[0]; 00123 RS232_handler.bytesToRead = 0; 00124 RS232_handler.mode = RS232_MODE_LISTEN; 00125 } 00126 00127 00128 // Function is called on RX interrupt 00129 void RS232_rxCallback( void ) 00130 { 00131 char tempChar; 00132 00133 tempChar = rs232.getc(); 00134 00135 if( (RS232_handler.mode != RS232_MODE_LISTEN) || (RS232_handler.bytesToRead >= RS232_BUFFER_LENGTH) ) 00136 { 00137 return; 00138 } 00139 00140 // Restart timeout timer 00141 rs232Timeout.attach_us( &RS232_rxTimeout, RS232_RX_TIMEOUT_US ); 00142 // Save received byte 00143 *RS232_handler.ptrReadPositon = tempChar; 00144 RS232_handler.ptrReadPositon++; 00145 RS232_handler.bytesToRead++; 00146 } 00147 00148 00149 void RS232_rxTimeout( void ) 00150 { 00151 // Disable timeout 00152 rs232Timeout.detach(); 00153 RS232_handler.mode = RS232_MODE_RX_READY; // Transmission is ready 00154 }
Generated on Sat Jul 16 2022 03:23:51 by
