NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

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