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