NetServices Stack source
Dependents: HelloWorld ServoInterfaceBoardExample1 4180_Lab4
sio.cpp
00001 00002 /* 00003 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) 00004 00005 Permission is hereby granted, free of charge, to any person obtaining a copy 00006 of this software and associated documentation files (the "Software"), to deal 00007 in the Software without restriction, including without limitation the rights 00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00009 copies of the Software, and to permit persons to whom the Software is 00010 furnished to do so, subject to the following conditions: 00011 00012 The above copyright notice and this permission notice shall be included in 00013 all copies or substantial portions of the Software. 00014 00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00021 THE SOFTWARE. 00022 */ 00023 00024 /* 00025 00026 This is a wrapper around Serial if for lwIP (acts as a serial driver) 00027 00028 See sio.h for functions to be implemented 00029 00030 sio_fd_t is a void* defined type, we use it as a SerialBuf ptr 00031 00032 00033 00034 */ 00035 00036 00037 #include "netCfg.h" 00038 #if NET_PPP 00039 00040 //#define MAX_SERIAL_PORTS 8 00041 00042 #include "lwip/sio.h" 00043 #include "mbed.h" 00044 //#include "sioMgr.h" 00045 #include "drv/serial/buf/SerialBuf.h" 00046 00047 //#define __DEBUG 00048 #include "dbg/dbg.h" 00049 00050 //extern "C" { 00051 00052 /** 00053 * Opens a serial device for communication. 00054 * 00055 * @param devnum device number 00056 * @return handle to serial device if successful, NULL otherwise 00057 */ 00058 sio_fd_t sio_open(u8_t devnum) 00059 { 00060 #if 0 00061 SerialBuf* pIf = SioMgr::getIf(devnum); 00062 if(pIf == NULL) 00063 return NULL; 00064 00065 //Got a SerialBuf* object 00066 //WARN: It HAS to be initialised (instanciated + attached to a Serial obj) 00067 00068 return (sio_fd_t) pIf; 00069 #endif 00070 return NULL; 00071 } 00072 00073 /** 00074 * Sends a single character to the serial device. 00075 * 00076 * @param c character to send 00077 * @param fd serial device handle 00078 * 00079 * @note This function will block until the character can be sent. 00080 */ 00081 void sio_send(u8_t c, sio_fd_t fd) 00082 { 00083 SerialBuf* pIf = (SerialBuf*) fd; 00084 //while(!pIf->writeable()); 00085 pIf->putc( (char) c ); 00086 } 00087 00088 /** 00089 * Receives a single character from the serial device. 00090 * 00091 * @param fd serial device handle 00092 * 00093 * @note This function will block until a character is received. 00094 */ 00095 u8_t sio_recv(sio_fd_t fd) 00096 { 00097 SerialBuf* pIf = (SerialBuf*) fd; 00098 pIf->setReadMode(false); 00099 while(!pIf->readable()); 00100 return (u8_t) pIf->getc(); 00101 } 00102 00103 /** 00104 * Reads from the serial device. 00105 * 00106 * @param fd serial device handle 00107 * @param data pointer to data buffer for receiving 00108 * @param len maximum length (in bytes) of data to receive 00109 * @return number of bytes actually received - may be 0 if aborted by sio_read_abort 00110 * 00111 * @note This function will block until data can be received. The blocking 00112 * can be cancelled by calling sio_read_abort(). 00113 */ 00114 static volatile bool m_abort = false; 00115 u32_t sio_read(sio_fd_t fd, u8_t *data, u32_t len) 00116 { 00117 u32_t recvd = 0; //bytes received 00118 SerialBuf* pIf = (SerialBuf*) fd; 00119 pIf->setReadMode(false); 00120 while(!m_abort && len) 00121 { 00122 while(!pIf->readable()); 00123 *data = (u8_t) pIf->getc(); 00124 data++; 00125 len--; 00126 recvd++; 00127 } 00128 m_abort = false; 00129 return recvd; 00130 } 00131 00132 /** 00133 * Tries to read from the serial device. Same as sio_read but returns 00134 * immediately if no data is available and never blocks. 00135 * 00136 * @param fd serial device handle 00137 * @param data pointer to data buffer for receiving 00138 * @param len maximum length (in bytes) of data to receive 00139 * @return number of bytes actually received 00140 */ 00141 u32_t sio_tryread(sio_fd_t fd, u8_t *data, u32_t len) 00142 { 00143 u32_t recvd = 0; //bytes received 00144 SerialBuf* pIf = (SerialBuf*) fd; 00145 pIf->setReadMode(false); 00146 while(len) 00147 { 00148 /* if(!pIf->readable()) 00149 { 00150 wait_ms(4); 00151 }*/ 00152 if(!pIf->readable()) 00153 { 00154 return recvd; 00155 } 00156 *data = (u8_t) pIf->getc(); 00157 data++; 00158 len--; 00159 recvd++; 00160 } 00161 return recvd; 00162 } 00163 00164 /** 00165 * Writes to the serial device. 00166 * 00167 * @param fd serial device handle 00168 * @param data pointer to data to send 00169 * @param len length (in bytes) of data to send 00170 * @return number of bytes actually sent 00171 * 00172 * @note This function will block until all data can be sent. 00173 */ 00174 u32_t sio_write(sio_fd_t fd, u8_t *data, u32_t len) 00175 { 00176 u32_t sent = 0; //bytes sent 00177 SerialBuf* pIf = (SerialBuf*) fd; 00178 while(len) 00179 { 00180 while(!pIf->writeable()); 00181 pIf->putc(*data); 00182 data++; 00183 len--; 00184 sent++; 00185 } 00186 return sent; //Well, this is bound to be len if no interrupt mechanism 00187 } 00188 00189 /** 00190 * Aborts a blocking sio_read() call. 00191 * 00192 * @param fd serial device handle 00193 */ 00194 void sio_read_abort(sio_fd_t fd) 00195 { 00196 m_abort = true; 00197 } 00198 00199 //} 00200 00201 #endif 00202
Generated on Tue Jul 12 2022 11:52:58 by 1.7.2