old netservices
Fork of NetServices by
drv/serial/lwip/sio.cpp@8:1fb1dbb9ad9e, 2013-03-06 (annotated)
- Committer:
- cwang365
- Date:
- Wed Mar 06 00:34:07 2013 +0000
- Revision:
- 8:1fb1dbb9ad9e
- Parent:
- 0:ac1725ba162c
old net services
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
segundo | 0:ac1725ba162c | 1 | |
segundo | 0:ac1725ba162c | 2 | /* |
segundo | 0:ac1725ba162c | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
segundo | 0:ac1725ba162c | 4 | |
segundo | 0:ac1725ba162c | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
segundo | 0:ac1725ba162c | 6 | of this software and associated documentation files (the "Software"), to deal |
segundo | 0:ac1725ba162c | 7 | in the Software without restriction, including without limitation the rights |
segundo | 0:ac1725ba162c | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
segundo | 0:ac1725ba162c | 9 | copies of the Software, and to permit persons to whom the Software is |
segundo | 0:ac1725ba162c | 10 | furnished to do so, subject to the following conditions: |
segundo | 0:ac1725ba162c | 11 | |
segundo | 0:ac1725ba162c | 12 | The above copyright notice and this permission notice shall be included in |
segundo | 0:ac1725ba162c | 13 | all copies or substantial portions of the Software. |
segundo | 0:ac1725ba162c | 14 | |
segundo | 0:ac1725ba162c | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
segundo | 0:ac1725ba162c | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
segundo | 0:ac1725ba162c | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
segundo | 0:ac1725ba162c | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
segundo | 0:ac1725ba162c | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
segundo | 0:ac1725ba162c | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
segundo | 0:ac1725ba162c | 21 | THE SOFTWARE. |
segundo | 0:ac1725ba162c | 22 | */ |
segundo | 0:ac1725ba162c | 23 | |
segundo | 0:ac1725ba162c | 24 | /* |
segundo | 0:ac1725ba162c | 25 | |
segundo | 0:ac1725ba162c | 26 | This is a wrapper around Serial if for lwIP (acts as a serial driver) |
segundo | 0:ac1725ba162c | 27 | |
segundo | 0:ac1725ba162c | 28 | See sio.h for functions to be implemented |
segundo | 0:ac1725ba162c | 29 | |
segundo | 0:ac1725ba162c | 30 | sio_fd_t is a void* defined type, we use it as a SerialBuf ptr |
segundo | 0:ac1725ba162c | 31 | |
segundo | 0:ac1725ba162c | 32 | |
segundo | 0:ac1725ba162c | 33 | |
segundo | 0:ac1725ba162c | 34 | */ |
segundo | 0:ac1725ba162c | 35 | |
segundo | 0:ac1725ba162c | 36 | |
segundo | 0:ac1725ba162c | 37 | #include "netCfg.h" |
segundo | 0:ac1725ba162c | 38 | #if NET_PPP |
segundo | 0:ac1725ba162c | 39 | |
segundo | 0:ac1725ba162c | 40 | //#define MAX_SERIAL_PORTS 8 |
segundo | 0:ac1725ba162c | 41 | |
segundo | 0:ac1725ba162c | 42 | #include "lwip/sio.h" |
segundo | 0:ac1725ba162c | 43 | #include "mbed.h" |
segundo | 0:ac1725ba162c | 44 | //#include "sioMgr.h" |
segundo | 0:ac1725ba162c | 45 | #include "drv/serial/buf/SerialBuf.h" |
segundo | 0:ac1725ba162c | 46 | |
segundo | 0:ac1725ba162c | 47 | //#define __DEBUG |
segundo | 0:ac1725ba162c | 48 | #include "dbg/dbg.h" |
segundo | 0:ac1725ba162c | 49 | |
segundo | 0:ac1725ba162c | 50 | //extern "C" { |
segundo | 0:ac1725ba162c | 51 | |
segundo | 0:ac1725ba162c | 52 | /** |
segundo | 0:ac1725ba162c | 53 | * Opens a serial device for communication. |
segundo | 0:ac1725ba162c | 54 | * |
segundo | 0:ac1725ba162c | 55 | * @param devnum device number |
segundo | 0:ac1725ba162c | 56 | * @return handle to serial device if successful, NULL otherwise |
segundo | 0:ac1725ba162c | 57 | */ |
segundo | 0:ac1725ba162c | 58 | sio_fd_t sio_open(u8_t devnum) |
segundo | 0:ac1725ba162c | 59 | { |
segundo | 0:ac1725ba162c | 60 | #if 0 |
segundo | 0:ac1725ba162c | 61 | SerialBuf* pIf = SioMgr::getIf(devnum); |
segundo | 0:ac1725ba162c | 62 | if(pIf == NULL) |
segundo | 0:ac1725ba162c | 63 | return NULL; |
segundo | 0:ac1725ba162c | 64 | |
segundo | 0:ac1725ba162c | 65 | //Got a SerialBuf* object |
segundo | 0:ac1725ba162c | 66 | //WARN: It HAS to be initialised (instanciated + attached to a Serial obj) |
segundo | 0:ac1725ba162c | 67 | |
segundo | 0:ac1725ba162c | 68 | return (sio_fd_t) pIf; |
segundo | 0:ac1725ba162c | 69 | #endif |
segundo | 0:ac1725ba162c | 70 | return NULL; |
segundo | 0:ac1725ba162c | 71 | } |
segundo | 0:ac1725ba162c | 72 | |
segundo | 0:ac1725ba162c | 73 | /** |
segundo | 0:ac1725ba162c | 74 | * Sends a single character to the serial device. |
segundo | 0:ac1725ba162c | 75 | * |
segundo | 0:ac1725ba162c | 76 | * @param c character to send |
segundo | 0:ac1725ba162c | 77 | * @param fd serial device handle |
segundo | 0:ac1725ba162c | 78 | * |
segundo | 0:ac1725ba162c | 79 | * @note This function will block until the character can be sent. |
segundo | 0:ac1725ba162c | 80 | */ |
segundo | 0:ac1725ba162c | 81 | void sio_send(u8_t c, sio_fd_t fd) |
segundo | 0:ac1725ba162c | 82 | { |
segundo | 0:ac1725ba162c | 83 | SerialBuf* pIf = (SerialBuf*) fd; |
segundo | 0:ac1725ba162c | 84 | //while(!pIf->writeable()); |
segundo | 0:ac1725ba162c | 85 | pIf->putc( (char) c ); |
segundo | 0:ac1725ba162c | 86 | } |
segundo | 0:ac1725ba162c | 87 | |
segundo | 0:ac1725ba162c | 88 | /** |
segundo | 0:ac1725ba162c | 89 | * Receives a single character from the serial device. |
segundo | 0:ac1725ba162c | 90 | * |
segundo | 0:ac1725ba162c | 91 | * @param fd serial device handle |
segundo | 0:ac1725ba162c | 92 | * |
segundo | 0:ac1725ba162c | 93 | * @note This function will block until a character is received. |
segundo | 0:ac1725ba162c | 94 | */ |
segundo | 0:ac1725ba162c | 95 | u8_t sio_recv(sio_fd_t fd) |
segundo | 0:ac1725ba162c | 96 | { |
segundo | 0:ac1725ba162c | 97 | SerialBuf* pIf = (SerialBuf*) fd; |
segundo | 0:ac1725ba162c | 98 | pIf->setReadMode(false); |
segundo | 0:ac1725ba162c | 99 | while(!pIf->readable()); |
segundo | 0:ac1725ba162c | 100 | return (u8_t) pIf->getc(); |
segundo | 0:ac1725ba162c | 101 | } |
segundo | 0:ac1725ba162c | 102 | |
segundo | 0:ac1725ba162c | 103 | /** |
segundo | 0:ac1725ba162c | 104 | * Reads from the serial device. |
segundo | 0:ac1725ba162c | 105 | * |
segundo | 0:ac1725ba162c | 106 | * @param fd serial device handle |
segundo | 0:ac1725ba162c | 107 | * @param data pointer to data buffer for receiving |
segundo | 0:ac1725ba162c | 108 | * @param len maximum length (in bytes) of data to receive |
segundo | 0:ac1725ba162c | 109 | * @return number of bytes actually received - may be 0 if aborted by sio_read_abort |
segundo | 0:ac1725ba162c | 110 | * |
segundo | 0:ac1725ba162c | 111 | * @note This function will block until data can be received. The blocking |
segundo | 0:ac1725ba162c | 112 | * can be cancelled by calling sio_read_abort(). |
segundo | 0:ac1725ba162c | 113 | */ |
segundo | 0:ac1725ba162c | 114 | static volatile bool m_abort = false; |
segundo | 0:ac1725ba162c | 115 | u32_t sio_read(sio_fd_t fd, u8_t *data, u32_t len) |
segundo | 0:ac1725ba162c | 116 | { |
segundo | 0:ac1725ba162c | 117 | u32_t recvd = 0; //bytes received |
segundo | 0:ac1725ba162c | 118 | SerialBuf* pIf = (SerialBuf*) fd; |
segundo | 0:ac1725ba162c | 119 | pIf->setReadMode(false); |
segundo | 0:ac1725ba162c | 120 | while(!m_abort && len) |
segundo | 0:ac1725ba162c | 121 | { |
segundo | 0:ac1725ba162c | 122 | while(!pIf->readable()); |
segundo | 0:ac1725ba162c | 123 | *data = (u8_t) pIf->getc(); |
segundo | 0:ac1725ba162c | 124 | data++; |
segundo | 0:ac1725ba162c | 125 | len--; |
segundo | 0:ac1725ba162c | 126 | recvd++; |
segundo | 0:ac1725ba162c | 127 | } |
segundo | 0:ac1725ba162c | 128 | m_abort = false; |
segundo | 0:ac1725ba162c | 129 | return recvd; |
segundo | 0:ac1725ba162c | 130 | } |
segundo | 0:ac1725ba162c | 131 | |
segundo | 0:ac1725ba162c | 132 | /** |
segundo | 0:ac1725ba162c | 133 | * Tries to read from the serial device. Same as sio_read but returns |
segundo | 0:ac1725ba162c | 134 | * immediately if no data is available and never blocks. |
segundo | 0:ac1725ba162c | 135 | * |
segundo | 0:ac1725ba162c | 136 | * @param fd serial device handle |
segundo | 0:ac1725ba162c | 137 | * @param data pointer to data buffer for receiving |
segundo | 0:ac1725ba162c | 138 | * @param len maximum length (in bytes) of data to receive |
segundo | 0:ac1725ba162c | 139 | * @return number of bytes actually received |
segundo | 0:ac1725ba162c | 140 | */ |
segundo | 0:ac1725ba162c | 141 | u32_t sio_tryread(sio_fd_t fd, u8_t *data, u32_t len) |
segundo | 0:ac1725ba162c | 142 | { |
segundo | 0:ac1725ba162c | 143 | u32_t recvd = 0; //bytes received |
segundo | 0:ac1725ba162c | 144 | SerialBuf* pIf = (SerialBuf*) fd; |
segundo | 0:ac1725ba162c | 145 | pIf->setReadMode(false); |
segundo | 0:ac1725ba162c | 146 | while(len) |
segundo | 0:ac1725ba162c | 147 | { |
segundo | 0:ac1725ba162c | 148 | /* if(!pIf->readable()) |
segundo | 0:ac1725ba162c | 149 | { |
segundo | 0:ac1725ba162c | 150 | wait_ms(4); |
segundo | 0:ac1725ba162c | 151 | }*/ |
segundo | 0:ac1725ba162c | 152 | if(!pIf->readable()) |
segundo | 0:ac1725ba162c | 153 | { |
segundo | 0:ac1725ba162c | 154 | return recvd; |
segundo | 0:ac1725ba162c | 155 | } |
segundo | 0:ac1725ba162c | 156 | *data = (u8_t) pIf->getc(); |
segundo | 0:ac1725ba162c | 157 | data++; |
segundo | 0:ac1725ba162c | 158 | len--; |
segundo | 0:ac1725ba162c | 159 | recvd++; |
segundo | 0:ac1725ba162c | 160 | } |
segundo | 0:ac1725ba162c | 161 | return recvd; |
segundo | 0:ac1725ba162c | 162 | } |
segundo | 0:ac1725ba162c | 163 | |
segundo | 0:ac1725ba162c | 164 | /** |
segundo | 0:ac1725ba162c | 165 | * Writes to the serial device. |
segundo | 0:ac1725ba162c | 166 | * |
segundo | 0:ac1725ba162c | 167 | * @param fd serial device handle |
segundo | 0:ac1725ba162c | 168 | * @param data pointer to data to send |
segundo | 0:ac1725ba162c | 169 | * @param len length (in bytes) of data to send |
segundo | 0:ac1725ba162c | 170 | * @return number of bytes actually sent |
segundo | 0:ac1725ba162c | 171 | * |
segundo | 0:ac1725ba162c | 172 | * @note This function will block until all data can be sent. |
segundo | 0:ac1725ba162c | 173 | */ |
segundo | 0:ac1725ba162c | 174 | u32_t sio_write(sio_fd_t fd, u8_t *data, u32_t len) |
segundo | 0:ac1725ba162c | 175 | { |
segundo | 0:ac1725ba162c | 176 | u32_t sent = 0; //bytes sent |
segundo | 0:ac1725ba162c | 177 | SerialBuf* pIf = (SerialBuf*) fd; |
segundo | 0:ac1725ba162c | 178 | while(len) |
segundo | 0:ac1725ba162c | 179 | { |
segundo | 0:ac1725ba162c | 180 | while(!pIf->writeable()); |
segundo | 0:ac1725ba162c | 181 | pIf->putc(*data); |
segundo | 0:ac1725ba162c | 182 | data++; |
segundo | 0:ac1725ba162c | 183 | len--; |
segundo | 0:ac1725ba162c | 184 | sent++; |
segundo | 0:ac1725ba162c | 185 | } |
segundo | 0:ac1725ba162c | 186 | return sent; //Well, this is bound to be len if no interrupt mechanism |
segundo | 0:ac1725ba162c | 187 | } |
segundo | 0:ac1725ba162c | 188 | |
segundo | 0:ac1725ba162c | 189 | /** |
segundo | 0:ac1725ba162c | 190 | * Aborts a blocking sio_read() call. |
segundo | 0:ac1725ba162c | 191 | * |
segundo | 0:ac1725ba162c | 192 | * @param fd serial device handle |
segundo | 0:ac1725ba162c | 193 | */ |
segundo | 0:ac1725ba162c | 194 | void sio_read_abort(sio_fd_t fd) |
segundo | 0:ac1725ba162c | 195 | { |
segundo | 0:ac1725ba162c | 196 | m_abort = true; |
segundo | 0:ac1725ba162c | 197 | } |
segundo | 0:ac1725ba162c | 198 | |
segundo | 0:ac1725ba162c | 199 | //} |
segundo | 0:ac1725ba162c | 200 | |
segundo | 0:ac1725ba162c | 201 | #endif |
segundo | 0:ac1725ba162c | 202 |