Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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