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