AWS IoT demonstration using the Avnet Shield (AT&T LTE) and the FRDM-K64F target board.

Dependencies:   K64F_FATFileSystem

Fork of mbed-os-example-tls-tls-client by mbed-os-examples

Committer:
ampembeng
Date:
Thu Dec 01 18:05:38 2016 +0000
Revision:
15:6f2798e45099
Initial commit.  Demo works with both the FRDM wired Ethernet and the Avnet Shield wireless modem.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ampembeng 15:6f2798e45099 1 /*
ampembeng 15:6f2798e45099 2 Copyright (c) 2010 Andy Kirkham
ampembeng 15:6f2798e45099 3
ampembeng 15:6f2798e45099 4 Permission is hereby granted, free of charge, to any person obtaining a copy
ampembeng 15:6f2798e45099 5 of this software and associated documentation files (the "Software"), to deal
ampembeng 15:6f2798e45099 6 in the Software without restriction, including without limitation the rights
ampembeng 15:6f2798e45099 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
ampembeng 15:6f2798e45099 8 copies of the Software, and to permit persons to whom the Software is
ampembeng 15:6f2798e45099 9 furnished to do so, subject to the following conditions:
ampembeng 15:6f2798e45099 10
ampembeng 15:6f2798e45099 11 The above copyright notice and this permission notice shall be included in
ampembeng 15:6f2798e45099 12 all copies or substantial portions of the Software.
ampembeng 15:6f2798e45099 13
ampembeng 15:6f2798e45099 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
ampembeng 15:6f2798e45099 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
ampembeng 15:6f2798e45099 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
ampembeng 15:6f2798e45099 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
ampembeng 15:6f2798e45099 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ampembeng 15:6f2798e45099 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
ampembeng 15:6f2798e45099 20 THE SOFTWARE.
ampembeng 15:6f2798e45099 21
ampembeng 15:6f2798e45099 22 @file MODSERIAL.h
ampembeng 15:6f2798e45099 23 @purpose Extends Serial to provide fully buffered IO
ampembeng 15:6f2798e45099 24 @version see ChangeLog.c
ampembeng 15:6f2798e45099 25 @date Nov 2010
ampembeng 15:6f2798e45099 26 @author Andy Kirkham
ampembeng 15:6f2798e45099 27 */
ampembeng 15:6f2798e45099 28
ampembeng 15:6f2798e45099 29 #ifndef MODSERIAL_H
ampembeng 15:6f2798e45099 30 #define MODSERIAL_H
ampembeng 15:6f2798e45099 31
ampembeng 15:6f2798e45099 32 /** @defgroup API The MODSERIAL API */
ampembeng 15:6f2798e45099 33 /** @defgroup MISC Misc MODSERIAL functions */
ampembeng 15:6f2798e45099 34 /** @defgroup INTERNALS MODSERIAL Internals */
ampembeng 15:6f2798e45099 35
ampembeng 15:6f2798e45099 36 #ifndef MODSERIAL_DEFAULT_RX_BUFFER_SIZE
ampembeng 15:6f2798e45099 37 #define MODSERIAL_DEFAULT_RX_BUFFER_SIZE 256
ampembeng 15:6f2798e45099 38 #endif
ampembeng 15:6f2798e45099 39
ampembeng 15:6f2798e45099 40 #ifndef MODSERIAL_DEFAULT_TX_BUFFER_SIZE
ampembeng 15:6f2798e45099 41 #define MODSERIAL_DEFAULT_TX_BUFFER_SIZE 256
ampembeng 15:6f2798e45099 42 #endif
ampembeng 15:6f2798e45099 43
ampembeng 15:6f2798e45099 44 #include "mbed.h"
ampembeng 15:6f2798e45099 45 #include "serial_api.h"
ampembeng 15:6f2798e45099 46
ampembeng 15:6f2798e45099 47 namespace AjK {
ampembeng 15:6f2798e45099 48
ampembeng 15:6f2798e45099 49 // Forward reference.
ampembeng 15:6f2798e45099 50 class MODSERIAL;
ampembeng 15:6f2798e45099 51
ampembeng 15:6f2798e45099 52 /**
ampembeng 15:6f2798e45099 53 * @author Andy Kirkham
ampembeng 15:6f2798e45099 54 * @see http://mbed.org/cookbook/MODSERIAL
ampembeng 15:6f2798e45099 55 * @see example3a.cpp
ampembeng 15:6f2798e45099 56 * @see example3b.cpp
ampembeng 15:6f2798e45099 57 * @see API
ampembeng 15:6f2798e45099 58 *
ampembeng 15:6f2798e45099 59 * <b>MODSERIAL_IRQ_INFO</b> is a class used to pass information (and access to protected
ampembeng 15:6f2798e45099 60 * MODSERIAL functions) to IRQ callbacks.
ampembeng 15:6f2798e45099 61 */
ampembeng 15:6f2798e45099 62 class MODSERIAL_IRQ_INFO
ampembeng 15:6f2798e45099 63 {
ampembeng 15:6f2798e45099 64 public:
ampembeng 15:6f2798e45099 65 friend class MODSERIAL;
ampembeng 15:6f2798e45099 66
ampembeng 15:6f2798e45099 67 MODSERIAL *serial;
ampembeng 15:6f2798e45099 68
ampembeng 15:6f2798e45099 69 MODSERIAL_IRQ_INFO() { serial = 0; }
ampembeng 15:6f2798e45099 70
ampembeng 15:6f2798e45099 71 /** rxDiscardLastChar()
ampembeng 15:6f2798e45099 72 *
ampembeng 15:6f2798e45099 73 * Remove the last char placed into the rx buffer.
ampembeng 15:6f2798e45099 74 * This is an operation that can only be performed
ampembeng 15:6f2798e45099 75 * by an rxCallback function.
ampembeng 15:6f2798e45099 76 * @ingroup API
ampembeng 15:6f2798e45099 77 * @return The byte removed from the buffer.
ampembeng 15:6f2798e45099 78 */
ampembeng 15:6f2798e45099 79 int rxDiscardLastChar(void);
ampembeng 15:6f2798e45099 80
ampembeng 15:6f2798e45099 81 protected:
ampembeng 15:6f2798e45099 82
ampembeng 15:6f2798e45099 83 /** setSerial()
ampembeng 15:6f2798e45099 84 *
ampembeng 15:6f2798e45099 85 * Used internally by MODSERIAL to set the "this" pointer
ampembeng 15:6f2798e45099 86 * of the MODSERIAL that created this object.
ampembeng 15:6f2798e45099 87 * @ingroup INTERNAL
ampembeng 15:6f2798e45099 88 * @param A pointer to a MODSERIAL object instance.
ampembeng 15:6f2798e45099 89 */
ampembeng 15:6f2798e45099 90 void setSerial(MODSERIAL *s) { serial = s; }
ampembeng 15:6f2798e45099 91 };
ampembeng 15:6f2798e45099 92
ampembeng 15:6f2798e45099 93 // Forward reference dummy class.
ampembeng 15:6f2798e45099 94 class MODSERIAL_callback_dummy;
ampembeng 15:6f2798e45099 95
ampembeng 15:6f2798e45099 96 /**
ampembeng 15:6f2798e45099 97 * @author Andy Kirkham
ampembeng 15:6f2798e45099 98 * @see http://mbed.org/cookbook/MODSERIAL
ampembeng 15:6f2798e45099 99 * @see example3a.cpp
ampembeng 15:6f2798e45099 100 * @see example3b.cpp
ampembeng 15:6f2798e45099 101 * @see API
ampembeng 15:6f2798e45099 102 *
ampembeng 15:6f2798e45099 103 * <b>MODSERIAL_callback</b> is a class used to hold application callbacks that
ampembeng 15:6f2798e45099 104 * MODSERIAL can invoke on certain events.
ampembeng 15:6f2798e45099 105 */
ampembeng 15:6f2798e45099 106 class MODSERIAL_callback
ampembeng 15:6f2798e45099 107 {
ampembeng 15:6f2798e45099 108 protected:
ampembeng 15:6f2798e45099 109
ampembeng 15:6f2798e45099 110 //! C callback function pointer.
ampembeng 15:6f2798e45099 111 void (*c_callback)(MODSERIAL_IRQ_INFO *);
ampembeng 15:6f2798e45099 112
ampembeng 15:6f2798e45099 113 //! C++ callback object/method pointer (the object part).
ampembeng 15:6f2798e45099 114 MODSERIAL_callback_dummy *obj_callback;
ampembeng 15:6f2798e45099 115
ampembeng 15:6f2798e45099 116 //! C++ callback object/method pointer (the method part).
ampembeng 15:6f2798e45099 117 void (MODSERIAL_callback_dummy::*method_callback)(MODSERIAL_IRQ_INFO *);
ampembeng 15:6f2798e45099 118
ampembeng 15:6f2798e45099 119 public:
ampembeng 15:6f2798e45099 120
ampembeng 15:6f2798e45099 121 /** Constructor
ampembeng 15:6f2798e45099 122 */
ampembeng 15:6f2798e45099 123 MODSERIAL_callback() {
ampembeng 15:6f2798e45099 124 c_callback = 0;
ampembeng 15:6f2798e45099 125 obj_callback = 0;
ampembeng 15:6f2798e45099 126 method_callback = 0;
ampembeng 15:6f2798e45099 127 }
ampembeng 15:6f2798e45099 128
ampembeng 15:6f2798e45099 129 /** attach - Overloaded attachment function.
ampembeng 15:6f2798e45099 130 *
ampembeng 15:6f2798e45099 131 * Attach a C type function pointer as the callback.
ampembeng 15:6f2798e45099 132 *
ampembeng 15:6f2798e45099 133 * Note, the callback function prototype must be:-
ampembeng 15:6f2798e45099 134 * @code
ampembeng 15:6f2798e45099 135 * void myCallbackFunction(MODSERIAL_IRQ_INFO *);
ampembeng 15:6f2798e45099 136 * @endcode
ampembeng 15:6f2798e45099 137 * @param A C function pointer to call.
ampembeng 15:6f2798e45099 138 */
ampembeng 15:6f2798e45099 139 void attach(void (*function)(MODSERIAL_IRQ_INFO *) = 0) { c_callback = function; }
ampembeng 15:6f2798e45099 140
ampembeng 15:6f2798e45099 141 /** attach - Overloaded attachment function.
ampembeng 15:6f2798e45099 142 *
ampembeng 15:6f2798e45099 143 * Attach a C++ type object/method pointer as the callback.
ampembeng 15:6f2798e45099 144 *
ampembeng 15:6f2798e45099 145 * Note, the callback method prototype must be:-
ampembeng 15:6f2798e45099 146 * @code
ampembeng 15:6f2798e45099 147 * public:
ampembeng 15:6f2798e45099 148 * void myCallbackFunction(MODSERIAL_IRQ_INFO *);
ampembeng 15:6f2798e45099 149 * @endcode
ampembeng 15:6f2798e45099 150 * @param A C++ object pointer.
ampembeng 15:6f2798e45099 151 * @param A C++ method within the object to call.
ampembeng 15:6f2798e45099 152 */
ampembeng 15:6f2798e45099 153 template<class T>
ampembeng 15:6f2798e45099 154 void attach(T* item, void (T::*method)(MODSERIAL_IRQ_INFO *)) {
ampembeng 15:6f2798e45099 155 obj_callback = (MODSERIAL_callback_dummy *)item;
ampembeng 15:6f2798e45099 156 method_callback = (void (MODSERIAL_callback_dummy::*)(MODSERIAL_IRQ_INFO *))method;
ampembeng 15:6f2798e45099 157 }
ampembeng 15:6f2798e45099 158
ampembeng 15:6f2798e45099 159 /** call - Overloaded callback initiator.
ampembeng 15:6f2798e45099 160 *
ampembeng 15:6f2798e45099 161 * call the callback function.
ampembeng 15:6f2798e45099 162 *
ampembeng 15:6f2798e45099 163 * @param A pointer to a MODSERIAL_IRQ_INFO object.
ampembeng 15:6f2798e45099 164 */
ampembeng 15:6f2798e45099 165 void call(MODSERIAL_IRQ_INFO *arg) {
ampembeng 15:6f2798e45099 166 if (c_callback != 0) {
ampembeng 15:6f2798e45099 167 (*c_callback)(arg);
ampembeng 15:6f2798e45099 168 }
ampembeng 15:6f2798e45099 169 else {
ampembeng 15:6f2798e45099 170 if (obj_callback != 0 && method_callback != 0) {
ampembeng 15:6f2798e45099 171 (obj_callback->*method_callback)(arg);
ampembeng 15:6f2798e45099 172 }
ampembeng 15:6f2798e45099 173 }
ampembeng 15:6f2798e45099 174 }
ampembeng 15:6f2798e45099 175 };
ampembeng 15:6f2798e45099 176
ampembeng 15:6f2798e45099 177 /**
ampembeng 15:6f2798e45099 178 * @author Andy Kirkham
ampembeng 15:6f2798e45099 179 * @see http://mbed.org/cookbook/MODSERIAL
ampembeng 15:6f2798e45099 180 * @see http://mbed.org/handbook/Serial
ampembeng 15:6f2798e45099 181 * @see example1.cpp
ampembeng 15:6f2798e45099 182 * @see example2.cpp
ampembeng 15:6f2798e45099 183 * @see example3a.cpp
ampembeng 15:6f2798e45099 184 * @see example3b.cpp
ampembeng 15:6f2798e45099 185 * @see example_dma.cpp
ampembeng 15:6f2798e45099 186 * @see API
ampembeng 15:6f2798e45099 187 *
ampembeng 15:6f2798e45099 188 * <b>MODSERIAL</b> extends the Mbed library <a href="/handbook/Serial">Serial</a> to provide fully buffered
ampembeng 15:6f2798e45099 189 * TX and RX streams. Buffer length is fully customisable.
ampembeng 15:6f2798e45099 190 *
ampembeng 15:6f2798e45099 191 * Before using MODSERIAL users should be familar with Mbed's standard <a href="/handbook/Serial">Serial</a>
ampembeng 15:6f2798e45099 192 * library object. MODSERIAL is a direct "drop in" replacement for <a href="/handbook/Serial">Serial</a>. Where
ampembeng 15:6f2798e45099 193 * previously Serial was used, MODSERIAL can be used as adirect replacement instantly offering standard
ampembeng 15:6f2798e45099 194 * TX and RX buffering. By default, both TX and RX buffers are 256 bytes in length.
ampembeng 15:6f2798e45099 195 *
ampembeng 15:6f2798e45099 196 * @image html /media/uploads/mbedofficial/serial_interfaces.png
ampembeng 15:6f2798e45099 197 *
ampembeng 15:6f2798e45099 198 * Standard example:
ampembeng 15:6f2798e45099 199 * @code
ampembeng 15:6f2798e45099 200 * #include "mbed.h"
ampembeng 15:6f2798e45099 201 * #include "MODSERIAL.h"
ampembeng 15:6f2798e45099 202 *
ampembeng 15:6f2798e45099 203 * MODSERIAL pc(USBTX, USBRX); // tx, rx
ampembeng 15:6f2798e45099 204 *
ampembeng 15:6f2798e45099 205 * int main() {
ampembeng 15:6f2798e45099 206 * pc.printf("Hello World!");
ampembeng 15:6f2798e45099 207 * while(1) {
ampembeng 15:6f2798e45099 208 * pc.putc(pc.getc() + 1);
ampembeng 15:6f2798e45099 209 * }
ampembeng 15:6f2798e45099 210 * }
ampembeng 15:6f2798e45099 211 * @endcode
ampembeng 15:6f2798e45099 212 *
ampembeng 15:6f2798e45099 213 * Example with alternate buffer length:
ampembeng 15:6f2798e45099 214 * @code
ampembeng 15:6f2798e45099 215 * #include "mbed.h"
ampembeng 15:6f2798e45099 216 * #include "MODSERIAL.h"
ampembeng 15:6f2798e45099 217 *
ampembeng 15:6f2798e45099 218 * // Make TX and RX buffers 512byes in length
ampembeng 15:6f2798e45099 219 * MODSERIAL pc(USBTX, USBRX, 512); // tx, rx
ampembeng 15:6f2798e45099 220 *
ampembeng 15:6f2798e45099 221 * int main() {
ampembeng 15:6f2798e45099 222 * pc.printf("Hello World!");
ampembeng 15:6f2798e45099 223 * while(1) {
ampembeng 15:6f2798e45099 224 * pc.putc(pc.getc() + 1);
ampembeng 15:6f2798e45099 225 * }
ampembeng 15:6f2798e45099 226 * }
ampembeng 15:6f2798e45099 227 * @endcode
ampembeng 15:6f2798e45099 228 *
ampembeng 15:6f2798e45099 229 * Example with alternate buffer length:
ampembeng 15:6f2798e45099 230 * @code
ampembeng 15:6f2798e45099 231 * #include "mbed.h"
ampembeng 15:6f2798e45099 232 * #include "MODSERIAL.h"
ampembeng 15:6f2798e45099 233 *
ampembeng 15:6f2798e45099 234 * // Make TX 1024bytes and RX 512byes in length
ampembeng 15:6f2798e45099 235 * MODSERIAL pc(USBTX, USBRX, 1024, 512); // tx, rx
ampembeng 15:6f2798e45099 236 *
ampembeng 15:6f2798e45099 237 * int main() {
ampembeng 15:6f2798e45099 238 * pc.printf("Hello World!");
ampembeng 15:6f2798e45099 239 * while(1) {
ampembeng 15:6f2798e45099 240 * pc.putc(pc.getc() + 1);
ampembeng 15:6f2798e45099 241 * }
ampembeng 15:6f2798e45099 242 * }
ampembeng 15:6f2798e45099 243 * @endcode
ampembeng 15:6f2798e45099 244 */
ampembeng 15:6f2798e45099 245 class MODSERIAL : public Serial
ampembeng 15:6f2798e45099 246 {
ampembeng 15:6f2798e45099 247 public:
ampembeng 15:6f2798e45099 248
ampembeng 15:6f2798e45099 249 // Allow instances of MODSERIAL_IRQ_INFO to use protected properties and methods.
ampembeng 15:6f2798e45099 250 friend class MODSERIAL_IRQ_INFO;
ampembeng 15:6f2798e45099 251
ampembeng 15:6f2798e45099 252 //! A copy of the Serial parity enum
ampembeng 15:6f2798e45099 253 /** @see http://mbed.org/projects/libraries/api/mbed/trunk/Serial#Serial.format */
ampembeng 15:6f2798e45099 254 enum Parity {
ampembeng 15:6f2798e45099 255 None = 0
ampembeng 15:6f2798e45099 256 , Odd
ampembeng 15:6f2798e45099 257 , Even
ampembeng 15:6f2798e45099 258 , Forced1
ampembeng 15:6f2798e45099 259 , Forced0
ampembeng 15:6f2798e45099 260 };
ampembeng 15:6f2798e45099 261
ampembeng 15:6f2798e45099 262 //! A copy of the Serial IrqType enum
ampembeng 15:6f2798e45099 263 enum IrqType {
ampembeng 15:6f2798e45099 264 RxIrq = 0
ampembeng 15:6f2798e45099 265 , TxIrq
ampembeng 15:6f2798e45099 266 , RxOvIrq
ampembeng 15:6f2798e45099 267 , TxOvIrq
ampembeng 15:6f2798e45099 268 , TxEmpty
ampembeng 15:6f2798e45099 269 , RxAutoDetect
ampembeng 15:6f2798e45099 270 , NumOfIrqTypes
ampembeng 15:6f2798e45099 271 };
ampembeng 15:6f2798e45099 272
ampembeng 15:6f2798e45099 273 //! Non-blocking functions return code.
ampembeng 15:6f2798e45099 274 enum Result {
ampembeng 15:6f2798e45099 275 Ok = 0 /*!< Ok. */
ampembeng 15:6f2798e45099 276 , NoMemory = -1 /*!< Memory allocation failed. */
ampembeng 15:6f2798e45099 277 , NoChar = -1 /*!< No character in buffer. */
ampembeng 15:6f2798e45099 278 , BufferOversize = -2 /*!< Oversized buffer. */
ampembeng 15:6f2798e45099 279 };
ampembeng 15:6f2798e45099 280
ampembeng 15:6f2798e45099 281 /**
ampembeng 15:6f2798e45099 282 * The MODSERIAL constructor is used to initialise the serial object.
ampembeng 15:6f2798e45099 283 *
ampembeng 15:6f2798e45099 284 * @param tx PinName of the TX pin.
ampembeng 15:6f2798e45099 285 * @param rx PinName of the TX pin.
ampembeng 15:6f2798e45099 286 */
ampembeng 15:6f2798e45099 287 MODSERIAL(PinName tx, PinName rx, const char* name = NULL);
ampembeng 15:6f2798e45099 288
ampembeng 15:6f2798e45099 289 /**
ampembeng 15:6f2798e45099 290 * The MODSERIAL constructor is used to initialise the serial object.
ampembeng 15:6f2798e45099 291 *
ampembeng 15:6f2798e45099 292 * @param tx PinName of the TX pin.
ampembeng 15:6f2798e45099 293 * @param rx PinName of the TX pin.
ampembeng 15:6f2798e45099 294 * @param bufferSize Integer of the TX and RX buffer sizes.
ampembeng 15:6f2798e45099 295 */
ampembeng 15:6f2798e45099 296 MODSERIAL(PinName tx, PinName rx, int bufferSize, const char* name = NULL);
ampembeng 15:6f2798e45099 297
ampembeng 15:6f2798e45099 298 /**
ampembeng 15:6f2798e45099 299 * The MODSERIAL constructor is used to initialise the serial object.
ampembeng 15:6f2798e45099 300 *
ampembeng 15:6f2798e45099 301 * @param tx PinName of the TX pin.
ampembeng 15:6f2798e45099 302 * @param rx PinName of the TX pin.
ampembeng 15:6f2798e45099 303 * @param txBufferSize Integer of the TX buffer sizes.
ampembeng 15:6f2798e45099 304 * @param rxBufferSize Integer of the RX buffer sizes.
ampembeng 15:6f2798e45099 305 */
ampembeng 15:6f2798e45099 306 MODSERIAL(PinName tx, PinName rx, int txBufferSize, int rxBufferSize, const char* name = NULL);
ampembeng 15:6f2798e45099 307
ampembeng 15:6f2798e45099 308 virtual ~MODSERIAL();
ampembeng 15:6f2798e45099 309
ampembeng 15:6f2798e45099 310 /**
ampembeng 15:6f2798e45099 311 * Function: attach
ampembeng 15:6f2798e45099 312 *
ampembeng 15:6f2798e45099 313 * The Mbed standard <a href="/handbook/Serial">Serial</a> library object allows an interrupt callback
ampembeng 15:6f2798e45099 314 * to be made when a byte is received by the TX or RX UART hardware. MODSERIAL traps these interrupts
ampembeng 15:6f2798e45099 315 * to enable it's buffering system. However, after the byte has been received/sent under interrupt control,
ampembeng 15:6f2798e45099 316 * MODSERIAL can callback a user function as a notification of the interrupt. Note, user code should not
ampembeng 15:6f2798e45099 317 * directly interact with the Uart hardware, MODSERIAL does that, instead, MODSERIAL API functions should
ampembeng 15:6f2798e45099 318 * be used.
ampembeng 15:6f2798e45099 319 *
ampembeng 15:6f2798e45099 320 * <b>Note</b>, a character is written out then, if there is room in the TX FIFO and the TX buffer is empty,
ampembeng 15:6f2798e45099 321 * putc() will put the character directly into THR (the output holding register). If the TX FIFO is full and
ampembeng 15:6f2798e45099 322 * cannot accept the character, it is placed into the TX output buffer. The TX interrupts are then enabled
ampembeng 15:6f2798e45099 323 * so that when the TX FIFO empties, the TX buffer is then transferred to the THR FIFO. The TxIrq will ONLY
ampembeng 15:6f2798e45099 324 * be activated when this transfer of a character from BUFFER to THR FIFO takes place. If your character
ampembeng 15:6f2798e45099 325 * throughput is not high bandwidth, then the 16 byte TX FIFO may be enough and the TX output buffer may
ampembeng 15:6f2798e45099 326 * never come into play.
ampembeng 15:6f2798e45099 327 *
ampembeng 15:6f2798e45099 328 * @code
ampembeng 15:6f2798e45099 329 * #include "mbed.h"
ampembeng 15:6f2798e45099 330 * #include "MODSERIAL.h"
ampembeng 15:6f2798e45099 331 *
ampembeng 15:6f2798e45099 332 * DigitalOut led1(LED1);
ampembeng 15:6f2798e45099 333 * DigitalOut led2(LED2);
ampembeng 15:6f2798e45099 334 * DigitalOut led3(LED3);
ampembeng 15:6f2798e45099 335 *
ampembeng 15:6f2798e45099 336 * // To test, connect p9 to p10 as a loopback.
ampembeng 15:6f2798e45099 337 * MODSERIAL pc(p9, p10);
ampembeng 15:6f2798e45099 338 *
ampembeng 15:6f2798e45099 339 * // This function is called when a character goes into the TX buffer.
ampembeng 15:6f2798e45099 340 * void txCallback(void) {
ampembeng 15:6f2798e45099 341 * led2 = !led2;
ampembeng 15:6f2798e45099 342 * }
ampembeng 15:6f2798e45099 343 *
ampembeng 15:6f2798e45099 344 * // This function is called when a character goes into the RX buffer.
ampembeng 15:6f2798e45099 345 * void rxCallback(void) {
ampembeng 15:6f2798e45099 346 * led3 = !led3;
ampembeng 15:6f2798e45099 347 * }
ampembeng 15:6f2798e45099 348 *
ampembeng 15:6f2798e45099 349 * int main() {
ampembeng 15:6f2798e45099 350 * pc.baud(115200);
ampembeng 15:6f2798e45099 351 * pc.attach(&txCallback, MODSERIAL::TxIrq);
ampembeng 15:6f2798e45099 352 * pc.attach(&rxCallback, MODSERIAL::RxIrq);
ampembeng 15:6f2798e45099 353 *
ampembeng 15:6f2798e45099 354 * while(1) {
ampembeng 15:6f2798e45099 355 * led1 = !led1;
ampembeng 15:6f2798e45099 356 * wait(0.5);
ampembeng 15:6f2798e45099 357 * pc.putc('A');
ampembeng 15:6f2798e45099 358 * wait(0.5);
ampembeng 15:6f2798e45099 359 * }
ampembeng 15:6f2798e45099 360 * ]
ampembeng 15:6f2798e45099 361 * @endcode
ampembeng 15:6f2798e45099 362 *
ampembeng 15:6f2798e45099 363 * @ingroup API
ampembeng 15:6f2798e45099 364 * @param fptr A pointer to a void function, or 0 to set as none
ampembeng 15:6f2798e45099 365 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
ampembeng 15:6f2798e45099 366 */
ampembeng 15:6f2798e45099 367 void attach(void (*fptr)(MODSERIAL_IRQ_INFO *), IrqType type = RxIrq) { _isr[type].attach(fptr); }
ampembeng 15:6f2798e45099 368
ampembeng 15:6f2798e45099 369 /**
ampembeng 15:6f2798e45099 370 * Function: attach
ampembeng 15:6f2798e45099 371 *
ampembeng 15:6f2798e45099 372 * The Mbed standard <a href="/handbook/Serial">Serial</a> library object allows an interrupt callback
ampembeng 15:6f2798e45099 373 * to be made when a byte is received by the TX or RX UART hardware. MODSERIAL traps these interrupts
ampembeng 15:6f2798e45099 374 * to enable it's buffering system. However, after the byte has been received/sent under interrupt control,
ampembeng 15:6f2798e45099 375 * MODSERIAL can callback a user function as a notification of the interrupt. Note, user code should not
ampembeng 15:6f2798e45099 376 * directly interact with the Uart hardware, MODSERIAL does that, instead, MODSERIAL API functions should
ampembeng 15:6f2798e45099 377 * be used.
ampembeng 15:6f2798e45099 378 *
ampembeng 15:6f2798e45099 379 * <b>Note</b>, a character is written out then, if there is room in the TX FIFO and the TX buffer is empty,
ampembeng 15:6f2798e45099 380 * putc() will put the character directly into THR (the output holding register). If the TX FIFO is full and
ampembeng 15:6f2798e45099 381 * cannot accept the character, it is placed into the TX output buffer. The TX interrupts are then enabled
ampembeng 15:6f2798e45099 382 * so that when the TX FIFO empties, the TX buffer is then transferred to the THR FIFO. The TxIrq will ONLY
ampembeng 15:6f2798e45099 383 * be activated when this transfer of a character from BUFFER to THR FIFO takes place. If your character
ampembeng 15:6f2798e45099 384 * throughput is not high bandwidth, then the 16 byte TX FIFO may be enough and the TX output buffer may
ampembeng 15:6f2798e45099 385 * never come into play.
ampembeng 15:6f2798e45099 386 *
ampembeng 15:6f2798e45099 387 * @code
ampembeng 15:6f2798e45099 388 * #include "mbed.h"
ampembeng 15:6f2798e45099 389 * #include "MODSERIAL.h"
ampembeng 15:6f2798e45099 390 *
ampembeng 15:6f2798e45099 391 * DigitalOut led1(LED1);
ampembeng 15:6f2798e45099 392 * DigitalOut led2(LED2);
ampembeng 15:6f2798e45099 393 * DigitalOut led3(LED3);
ampembeng 15:6f2798e45099 394 *
ampembeng 15:6f2798e45099 395 * // To test, connect p9 to p10 as a loopback.
ampembeng 15:6f2798e45099 396 * MODSERIAL pc(p9, p10);
ampembeng 15:6f2798e45099 397 *
ampembeng 15:6f2798e45099 398 * class Foo {
ampembeng 15:6f2798e45099 399 * public:
ampembeng 15:6f2798e45099 400 * // This method is called when a character goes into the TX buffer.
ampembeng 15:6f2798e45099 401 * void txCallback(void) { led2 = !led2; }
ampembeng 15:6f2798e45099 402 *
ampembeng 15:6f2798e45099 403 * // This method is called when a character goes into the RX buffer.
ampembeng 15:6f2798e45099 404 * void rxCallback(void) { led3 = !led3; }
ampembeng 15:6f2798e45099 405 * };
ampembeng 15:6f2798e45099 406 *
ampembeng 15:6f2798e45099 407 * Foo foo;
ampembeng 15:6f2798e45099 408 *
ampembeng 15:6f2798e45099 409 * int main() {
ampembeng 15:6f2798e45099 410 * pc.baud(115200);
ampembeng 15:6f2798e45099 411 * pc.attach(&foo, &Foo::txCallback, MODSERIAL::TxIrq);
ampembeng 15:6f2798e45099 412 * pc.attach(&foo, &Foo::rxCallback, MODSERIAL::RxIrq);
ampembeng 15:6f2798e45099 413 *
ampembeng 15:6f2798e45099 414 * while(1) {
ampembeng 15:6f2798e45099 415 * led1 = !led1;
ampembeng 15:6f2798e45099 416 * wait(0.5);
ampembeng 15:6f2798e45099 417 * pc.putc('A');
ampembeng 15:6f2798e45099 418 * wait(0.5);
ampembeng 15:6f2798e45099 419 * }
ampembeng 15:6f2798e45099 420 * ]
ampembeng 15:6f2798e45099 421 * @endcode
ampembeng 15:6f2798e45099 422 *
ampembeng 15:6f2798e45099 423 * @ingroup API
ampembeng 15:6f2798e45099 424 * @param tptr A pointer to the object to call the member function on
ampembeng 15:6f2798e45099 425 * @param mptr A pointer to the member function to be called
ampembeng 15:6f2798e45099 426 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
ampembeng 15:6f2798e45099 427 */
ampembeng 15:6f2798e45099 428 template<typename T>
ampembeng 15:6f2798e45099 429 void attach(T* tptr, void (T::*mptr)(MODSERIAL_IRQ_INFO *), IrqType type = RxIrq) {
ampembeng 15:6f2798e45099 430 if((mptr != 0) && (tptr != 0)) {
ampembeng 15:6f2798e45099 431 _isr[type].attach(tptr, mptr);
ampembeng 15:6f2798e45099 432 }
ampembeng 15:6f2798e45099 433 }
ampembeng 15:6f2798e45099 434
ampembeng 15:6f2798e45099 435 /**
ampembeng 15:6f2798e45099 436 * @see attach
ampembeng 15:6f2798e45099 437 * @ingroup API
ampembeng 15:6f2798e45099 438 */
ampembeng 15:6f2798e45099 439 void connect(void (*fptr)(MODSERIAL_IRQ_INFO *), IrqType type = RxIrq) { _isr[RxIrq].attach(fptr); }
ampembeng 15:6f2798e45099 440
ampembeng 15:6f2798e45099 441 /**
ampembeng 15:6f2798e45099 442 * @see attach
ampembeng 15:6f2798e45099 443 * @ingroup API
ampembeng 15:6f2798e45099 444 */
ampembeng 15:6f2798e45099 445 template<typename T>
ampembeng 15:6f2798e45099 446 void connect(T* tptr, void (T::*mptr)(MODSERIAL_IRQ_INFO *), IrqType type = RxIrq) {
ampembeng 15:6f2798e45099 447 if((mptr != 0) && (tptr != 0)) {
ampembeng 15:6f2798e45099 448 _isr[type].attach(tptr, mptr);
ampembeng 15:6f2798e45099 449 }
ampembeng 15:6f2798e45099 450 }
ampembeng 15:6f2798e45099 451
ampembeng 15:6f2798e45099 452 /**
ampembeng 15:6f2798e45099 453 * Function: writeable
ampembeng 15:6f2798e45099 454 *
ampembeng 15:6f2798e45099 455 * Determine if there is space available to write a byte
ampembeng 15:6f2798e45099 456 *
ampembeng 15:6f2798e45099 457 * @ingroup API
ampembeng 15:6f2798e45099 458 * @return 1 if there is space to write a character, else 0
ampembeng 15:6f2798e45099 459 */
ampembeng 15:6f2798e45099 460 int writeable() { return txBufferFull() ? 0 : 1; }
ampembeng 15:6f2798e45099 461
ampembeng 15:6f2798e45099 462 /**
ampembeng 15:6f2798e45099 463 * Function: readable
ampembeng 15:6f2798e45099 464 *
ampembeng 15:6f2798e45099 465 * Determine if there is a byte available to read
ampembeng 15:6f2798e45099 466 *
ampembeng 15:6f2798e45099 467 * @ingroup API
ampembeng 15:6f2798e45099 468 * @return 1 if there is a character available to read, else 0
ampembeng 15:6f2798e45099 469 */
ampembeng 15:6f2798e45099 470 int readable() { return rxBufferEmpty() ? 0 : 1; }
ampembeng 15:6f2798e45099 471
ampembeng 15:6f2798e45099 472 /**
ampembeng 15:6f2798e45099 473 * Function: txBufferSane
ampembeng 15:6f2798e45099 474 *
ampembeng 15:6f2798e45099 475 * Determine if the TX buffer has been initialized.
ampembeng 15:6f2798e45099 476 *
ampembeng 15:6f2798e45099 477 * @ingroup API
ampembeng 15:6f2798e45099 478 * @return true if the buffer is initialized, else false
ampembeng 15:6f2798e45099 479 */
ampembeng 15:6f2798e45099 480 bool txBufferSane(void) { return buffer[TxIrq] != (char *)NULL ? true : false; }
ampembeng 15:6f2798e45099 481
ampembeng 15:6f2798e45099 482 /**
ampembeng 15:6f2798e45099 483 * Function: rxBufferSane
ampembeng 15:6f2798e45099 484 *
ampembeng 15:6f2798e45099 485 * Determine if the RX buffer has been initialized.
ampembeng 15:6f2798e45099 486 *
ampembeng 15:6f2798e45099 487 * @ingroup API
ampembeng 15:6f2798e45099 488 * @return true if the buffer is initialized, else false
ampembeng 15:6f2798e45099 489 */
ampembeng 15:6f2798e45099 490 bool rxBufferSane(void) { return buffer[TxIrq] != (char *)NULL ? true : false; }
ampembeng 15:6f2798e45099 491
ampembeng 15:6f2798e45099 492 /**
ampembeng 15:6f2798e45099 493 * Function: txBufferGetCount
ampembeng 15:6f2798e45099 494 *
ampembeng 15:6f2798e45099 495 * Returns how many bytes are in the TX buffer
ampembeng 15:6f2798e45099 496 *
ampembeng 15:6f2798e45099 497 * @ingroup API
ampembeng 15:6f2798e45099 498 * @return The number of bytes in the TX buffer
ampembeng 15:6f2798e45099 499 */
ampembeng 15:6f2798e45099 500 int txBufferGetCount(void) { return buffer_count[TxIrq]; }
ampembeng 15:6f2798e45099 501
ampembeng 15:6f2798e45099 502 /**
ampembeng 15:6f2798e45099 503 * Function: rxBufferGetCount
ampembeng 15:6f2798e45099 504 *
ampembeng 15:6f2798e45099 505 * Returns how many bytes are in the RX buffer
ampembeng 15:6f2798e45099 506 *
ampembeng 15:6f2798e45099 507 * @ingroup API
ampembeng 15:6f2798e45099 508 * @return The number of bytes in the RX buffer
ampembeng 15:6f2798e45099 509 */
ampembeng 15:6f2798e45099 510 int rxBufferGetCount(void) { return buffer_count[RxIrq]; }
ampembeng 15:6f2798e45099 511
ampembeng 15:6f2798e45099 512 /**
ampembeng 15:6f2798e45099 513 * Function: txBufferGetSize
ampembeng 15:6f2798e45099 514 *
ampembeng 15:6f2798e45099 515 * Returns the current size of the TX buffer
ampembeng 15:6f2798e45099 516 *
ampembeng 15:6f2798e45099 517 * @ingroup API
ampembeng 15:6f2798e45099 518 * @return The length iof the TX buffer in bytes
ampembeng 15:6f2798e45099 519 */
ampembeng 15:6f2798e45099 520 int txBufferGetSize(int size) { return buffer_size[TxIrq]; }
ampembeng 15:6f2798e45099 521
ampembeng 15:6f2798e45099 522 /**
ampembeng 15:6f2798e45099 523 * Function: rxBufferGetSize
ampembeng 15:6f2798e45099 524 *
ampembeng 15:6f2798e45099 525 * Returns the current size of the RX buffer
ampembeng 15:6f2798e45099 526 *
ampembeng 15:6f2798e45099 527 * @ingroup API
ampembeng 15:6f2798e45099 528 * @return The length iof the RX buffer in bytes
ampembeng 15:6f2798e45099 529 */
ampembeng 15:6f2798e45099 530 int rxBufferGetSize(int size) { return buffer_size[RxIrq]; }
ampembeng 15:6f2798e45099 531
ampembeng 15:6f2798e45099 532 /**
ampembeng 15:6f2798e45099 533 * Function: txBufferFull
ampembeng 15:6f2798e45099 534 *
ampembeng 15:6f2798e45099 535 * Is the TX buffer full?
ampembeng 15:6f2798e45099 536 *
ampembeng 15:6f2798e45099 537 * @ingroup API
ampembeng 15:6f2798e45099 538 * @return true if the TX buffer is full, otherwise false
ampembeng 15:6f2798e45099 539 */
ampembeng 15:6f2798e45099 540 bool txBufferFull(void);
ampembeng 15:6f2798e45099 541
ampembeng 15:6f2798e45099 542 /**
ampembeng 15:6f2798e45099 543 * Function: rxBufferFull
ampembeng 15:6f2798e45099 544 *
ampembeng 15:6f2798e45099 545 * Is the RX buffer full?
ampembeng 15:6f2798e45099 546 *
ampembeng 15:6f2798e45099 547 * @ingroup API
ampembeng 15:6f2798e45099 548 * @return true if the RX buffer is full, otherwise false
ampembeng 15:6f2798e45099 549 */
ampembeng 15:6f2798e45099 550 bool rxBufferFull(void);
ampembeng 15:6f2798e45099 551
ampembeng 15:6f2798e45099 552 /**
ampembeng 15:6f2798e45099 553 * Function: txBufferEmpty
ampembeng 15:6f2798e45099 554 *
ampembeng 15:6f2798e45099 555 * Is the TX buffer empty?
ampembeng 15:6f2798e45099 556 *
ampembeng 15:6f2798e45099 557 * @ingroup API
ampembeng 15:6f2798e45099 558 * @return true if the TX buffer is empty, otherwise false
ampembeng 15:6f2798e45099 559 */
ampembeng 15:6f2798e45099 560 bool txBufferEmpty(void);
ampembeng 15:6f2798e45099 561
ampembeng 15:6f2798e45099 562 /**
ampembeng 15:6f2798e45099 563 * Function: rxBufferEmpty
ampembeng 15:6f2798e45099 564 *
ampembeng 15:6f2798e45099 565 * Is the RX buffer empty?
ampembeng 15:6f2798e45099 566 *
ampembeng 15:6f2798e45099 567 * @ingroup API
ampembeng 15:6f2798e45099 568 * @return true if the RX buffer is empty, otherwise false
ampembeng 15:6f2798e45099 569 */
ampembeng 15:6f2798e45099 570 bool rxBufferEmpty(void);
ampembeng 15:6f2798e45099 571
ampembeng 15:6f2798e45099 572 /**
ampembeng 15:6f2798e45099 573 * Function: txBufferSetSize
ampembeng 15:6f2798e45099 574 *
ampembeng 15:6f2798e45099 575 * Change the TX buffer size.
ampembeng 15:6f2798e45099 576 *
ampembeng 15:6f2798e45099 577 * @see Result
ampembeng 15:6f2798e45099 578 * @ingroup API
ampembeng 15:6f2798e45099 579 * @param size The new TX buffer size in bytes.
ampembeng 15:6f2798e45099 580 * @param m Perform a memory sanity check. Errs the Mbed if memory alloc fails.
ampembeng 15:6f2798e45099 581 * @return Result Ok on success.
ampembeng 15:6f2798e45099 582 */
ampembeng 15:6f2798e45099 583 int txBufferSetSize(int size, bool m) { return resizeBuffer(size, TxIrq, m); }
ampembeng 15:6f2798e45099 584
ampembeng 15:6f2798e45099 585 /**
ampembeng 15:6f2798e45099 586 * Function: rxBufferSetSize
ampembeng 15:6f2798e45099 587 *
ampembeng 15:6f2798e45099 588 * Change the RX buffer size.
ampembeng 15:6f2798e45099 589 *
ampembeng 15:6f2798e45099 590 * @see Result
ampembeng 15:6f2798e45099 591 * @ingroup API
ampembeng 15:6f2798e45099 592 * @param size The new RX buffer size in bytes.
ampembeng 15:6f2798e45099 593 * @param m Perform a memory sanity check. Errs the Mbed if memory alloc fails.
ampembeng 15:6f2798e45099 594 * @return Result Ok on success.
ampembeng 15:6f2798e45099 595 */
ampembeng 15:6f2798e45099 596 int rxBufferSetSize(int size, bool m) { return resizeBuffer(size, RxIrq, m); }
ampembeng 15:6f2798e45099 597
ampembeng 15:6f2798e45099 598 /**
ampembeng 15:6f2798e45099 599 * Function: txBufferSetSize
ampembeng 15:6f2798e45099 600 *
ampembeng 15:6f2798e45099 601 * Change the TX buffer size.
ampembeng 15:6f2798e45099 602 * Always performs a memory sanity check, halting the Mbed on failure.
ampembeng 15:6f2798e45099 603 *
ampembeng 15:6f2798e45099 604 * @see Result
ampembeng 15:6f2798e45099 605 * @ingroup API
ampembeng 15:6f2798e45099 606 * @param size The new TX buffer size in bytes.
ampembeng 15:6f2798e45099 607 * @return Result Ok on success.
ampembeng 15:6f2798e45099 608 */
ampembeng 15:6f2798e45099 609 int txBufferSetSize(int size) { return resizeBuffer(size, TxIrq, true); }
ampembeng 15:6f2798e45099 610
ampembeng 15:6f2798e45099 611 /**
ampembeng 15:6f2798e45099 612 * Function: rxBufferSetSize
ampembeng 15:6f2798e45099 613 *
ampembeng 15:6f2798e45099 614 * Change the RX buffer size.
ampembeng 15:6f2798e45099 615 * Always performs a memory sanity check, halting the Mbed on failure.
ampembeng 15:6f2798e45099 616 *
ampembeng 15:6f2798e45099 617 * @see Result
ampembeng 15:6f2798e45099 618 * @ingroup API
ampembeng 15:6f2798e45099 619 * @param size The new RX buffer size in bytes.
ampembeng 15:6f2798e45099 620 * @return Result Ok on success.
ampembeng 15:6f2798e45099 621 */
ampembeng 15:6f2798e45099 622 int rxBufferSetSize(int size) { return resizeBuffer(size, RxIrq, true); }
ampembeng 15:6f2798e45099 623
ampembeng 15:6f2798e45099 624 /**
ampembeng 15:6f2798e45099 625 * Function: txBufferFlush
ampembeng 15:6f2798e45099 626 *
ampembeng 15:6f2798e45099 627 * Remove all bytes from the TX buffer.
ampembeng 15:6f2798e45099 628 * @ingroup API
ampembeng 15:6f2798e45099 629 */
ampembeng 15:6f2798e45099 630 void txBufferFlush(void) { flushBuffer(TxIrq); }
ampembeng 15:6f2798e45099 631
ampembeng 15:6f2798e45099 632 /**
ampembeng 15:6f2798e45099 633 * Function: rxBufferFlush
ampembeng 15:6f2798e45099 634 *
ampembeng 15:6f2798e45099 635 * Remove all bytes from the RX buffer.
ampembeng 15:6f2798e45099 636 * @ingroup API
ampembeng 15:6f2798e45099 637 */
ampembeng 15:6f2798e45099 638 void rxBufferFlush(void) { flushBuffer(RxIrq); }
ampembeng 15:6f2798e45099 639
ampembeng 15:6f2798e45099 640 /**
ampembeng 15:6f2798e45099 641 * Function: getcNb
ampembeng 15:6f2798e45099 642 *
ampembeng 15:6f2798e45099 643 * Like getc() but is non-blocking. If no bytes are in the RX buffer this
ampembeng 15:6f2798e45099 644 * function returns Result::NoChar (-1)
ampembeng 15:6f2798e45099 645 *
ampembeng 15:6f2798e45099 646 * @ingroup API
ampembeng 15:6f2798e45099 647 * @return A byte from the RX buffer or Result::NoChar (-1) if bufer empty.
ampembeng 15:6f2798e45099 648 */
ampembeng 15:6f2798e45099 649 int getcNb() { return __getc(false); }
ampembeng 15:6f2798e45099 650
ampembeng 15:6f2798e45099 651 /**
ampembeng 15:6f2798e45099 652 * Function: getc
ampembeng 15:6f2798e45099 653 *
ampembeng 15:6f2798e45099 654 * Overloaded version of Serial::getc()
ampembeng 15:6f2798e45099 655 *
ampembeng 15:6f2798e45099 656 * This function blocks (if the RX buffer is empty the function will wait for a
ampembeng 15:6f2798e45099 657 * character to arrive and then return that character).
ampembeng 15:6f2798e45099 658 *
ampembeng 15:6f2798e45099 659 * @ingroup API
ampembeng 15:6f2798e45099 660 * @return A byte from the RX buffer
ampembeng 15:6f2798e45099 661 */
ampembeng 15:6f2798e45099 662 int getc() { return __getc(true); }
ampembeng 15:6f2798e45099 663
ampembeng 15:6f2798e45099 664 /**
ampembeng 15:6f2798e45099 665 * Function: txGetLastChar
ampembeng 15:6f2798e45099 666 *
ampembeng 15:6f2798e45099 667 * Rteurn the last byte to pass through the TX interrupt handler.
ampembeng 15:6f2798e45099 668 *
ampembeng 15:6f2798e45099 669 * @ingroup MISC
ampembeng 15:6f2798e45099 670 * @return The byte
ampembeng 15:6f2798e45099 671 */
ampembeng 15:6f2798e45099 672 char txGetLastChar(void) { return txc; }
ampembeng 15:6f2798e45099 673
ampembeng 15:6f2798e45099 674 /**
ampembeng 15:6f2798e45099 675 * Function: rxGetLastChar
ampembeng 15:6f2798e45099 676 *
ampembeng 15:6f2798e45099 677 * Return the last byte to pass through the RX interrupt handler.
ampembeng 15:6f2798e45099 678 *
ampembeng 15:6f2798e45099 679 * @ingroup MISC
ampembeng 15:6f2798e45099 680 * @return The byte
ampembeng 15:6f2798e45099 681 */
ampembeng 15:6f2798e45099 682 char rxGetLastChar(void) { return rxc; }
ampembeng 15:6f2798e45099 683
ampembeng 15:6f2798e45099 684
ampembeng 15:6f2798e45099 685
ampembeng 15:6f2798e45099 686 /**
ampembeng 15:6f2798e45099 687 * Function: autoDetectChar
ampembeng 15:6f2798e45099 688 *
ampembeng 15:6f2798e45099 689 * Set the char that, if seen incoming, invokes the AutoDetectChar callback.
ampembeng 15:6f2798e45099 690 *
ampembeng 15:6f2798e45099 691 * @ingroup API
ampembeng 15:6f2798e45099 692 * @param int c The character to detect.
ampembeng 15:6f2798e45099 693 */
ampembeng 15:6f2798e45099 694 void autoDetectChar(char c) { auto_detect_char = c; }
ampembeng 15:6f2798e45099 695
ampembeng 15:6f2798e45099 696 /**
ampembeng 15:6f2798e45099 697 * Function: move
ampembeng 15:6f2798e45099 698 *
ampembeng 15:6f2798e45099 699 * Move contents of RX buffer to external buffer. Stops if "end" detected.
ampembeng 15:6f2798e45099 700 *
ampembeng 15:6f2798e45099 701 * @ingroup API
ampembeng 15:6f2798e45099 702 * @param char *s The destination buffer address
ampembeng 15:6f2798e45099 703 * @param int max The maximum number of chars to move.
ampembeng 15:6f2798e45099 704 * @param char end If this char is detected stop moving.
ampembeng 15:6f2798e45099 705 */
ampembeng 15:6f2798e45099 706 int move(char *s, int max, char end) {
ampembeng 15:6f2798e45099 707 int counter = 0;
ampembeng 15:6f2798e45099 708 char c;
ampembeng 15:6f2798e45099 709 while(readable()) {
ampembeng 15:6f2798e45099 710 c = getc();
ampembeng 15:6f2798e45099 711 if (c == end) break;
ampembeng 15:6f2798e45099 712 *(s++) = c;
ampembeng 15:6f2798e45099 713 counter++;
ampembeng 15:6f2798e45099 714 if (counter == max) break;
ampembeng 15:6f2798e45099 715 }
ampembeng 15:6f2798e45099 716 return counter;
ampembeng 15:6f2798e45099 717 }
ampembeng 15:6f2798e45099 718
ampembeng 15:6f2798e45099 719 /**
ampembeng 15:6f2798e45099 720 * Function: move (overloaded)
ampembeng 15:6f2798e45099 721 *
ampembeng 15:6f2798e45099 722 * Move contents of RX buffer to external buffer. Stops if auto_detect_char detected.
ampembeng 15:6f2798e45099 723 *
ampembeng 15:6f2798e45099 724 * @ingroup API
ampembeng 15:6f2798e45099 725 * @param int max The maximum number of chars to move.
ampembeng 15:6f2798e45099 726 * @param char *s The destination buffer address
ampembeng 15:6f2798e45099 727 */
ampembeng 15:6f2798e45099 728 int move(char *s, int max) {
ampembeng 15:6f2798e45099 729 return move(s, max, auto_detect_char);
ampembeng 15:6f2798e45099 730 }
ampembeng 15:6f2798e45099 731
ampembeng 15:6f2798e45099 732 /**
ampembeng 15:6f2798e45099 733 * Function: claim
ampembeng 15:6f2798e45099 734 *
ampembeng 15:6f2798e45099 735 * Redirect a stream to this MODSERIAL object
ampembeng 15:6f2798e45099 736 *
ampembeng 15:6f2798e45099 737 * Important: A name parameter must have been added when creating the MODSERIAL object:
ampembeng 15:6f2798e45099 738 *
ampembeng 15:6f2798e45099 739 * @code
ampembeng 15:6f2798e45099 740 * #include "MODSERIAL.h"
ampembeng 15:6f2798e45099 741 * ...
ampembeng 15:6f2798e45099 742 * MODSERIAL pc(USBTX, USBRX, "modser");
ampembeng 15:6f2798e45099 743 *
ampembeng 15:6f2798e45099 744 * int main() {
ampembeng 15:6f2798e45099 745 * pc.claim() // capture <stdout>
ampembeng 15:6f2798e45099 746 * pc.printf("Uses the MODSERIAL library\r\n");
ampembeng 15:6f2798e45099 747 * printf("So does this!\r\n");
ampembeng 15:6f2798e45099 748 * }
ampembeng 15:6f2798e45099 749 * @endcode
ampembeng 15:6f2798e45099 750 *
ampembeng 15:6f2798e45099 751 * @ingroup API
ampembeng 15:6f2798e45099 752 * @param FILE *stream The stream to redirect (default = stdout)
ampembeng 15:6f2798e45099 753 * @return true if succeeded, else false
ampembeng 15:6f2798e45099 754 */
ampembeng 15:6f2798e45099 755 bool claim(FILE *stream = stdout);
ampembeng 15:6f2798e45099 756
ampembeng 15:6f2798e45099 757 #if 0 // Inhereted from Serial/Stream, for documentation only
ampembeng 15:6f2798e45099 758 /**
ampembeng 15:6f2798e45099 759 * Function: putc
ampembeng 15:6f2798e45099 760 *
ampembeng 15:6f2798e45099 761 * Write a character
ampembeng 15:6f2798e45099 762 * Inhereted from Serial/Stream
ampembeng 15:6f2798e45099 763 *
ampembeng 15:6f2798e45099 764 * @see http://mbed.org/projects/libraries/api/mbed/trunk/Serial#Serial.putc
ampembeng 15:6f2798e45099 765 * @ingroup API
ampembeng 15:6f2798e45099 766 * @param c The character to write to the serial port
ampembeng 15:6f2798e45099 767 */
ampembeng 15:6f2798e45099 768 int putc(int c);
ampembeng 15:6f2798e45099 769 #endif
ampembeng 15:6f2798e45099 770
ampembeng 15:6f2798e45099 771 #if 0 // Inhereted from Serial/Stream, for documentation only
ampembeng 15:6f2798e45099 772 /**
ampembeng 15:6f2798e45099 773 * Function: printf
ampembeng 15:6f2798e45099 774 *
ampembeng 15:6f2798e45099 775 * Write a formated string
ampembeng 15:6f2798e45099 776 * Inhereted from Serial/Stream
ampembeng 15:6f2798e45099 777 *
ampembeng 15:6f2798e45099 778 * @see http://mbed.org/projects/libraries/api/mbed/trunk/Serial#Serial.printf
ampembeng 15:6f2798e45099 779 * @ingroup API
ampembeng 15:6f2798e45099 780 * @param format A printf-style format string, followed by the variables to use in formating the string.
ampembeng 15:6f2798e45099 781 */
ampembeng 15:6f2798e45099 782 int printf(const char* format, ...);
ampembeng 15:6f2798e45099 783 #endif
ampembeng 15:6f2798e45099 784
ampembeng 15:6f2798e45099 785 #if 0 // Inhereted from Serial/Stream, for documentation only
ampembeng 15:6f2798e45099 786 /**
ampembeng 15:6f2798e45099 787 * Function: scanf
ampembeng 15:6f2798e45099 788 *
ampembeng 15:6f2798e45099 789 * Read a formated string
ampembeng 15:6f2798e45099 790 * Inhereted from Serial/Stream
ampembeng 15:6f2798e45099 791 *
ampembeng 15:6f2798e45099 792 * @see http://mbed.org/projects/libraries/api/mbed/trunk/Serial#Serial.scanf
ampembeng 15:6f2798e45099 793 * @ingroup API
ampembeng 15:6f2798e45099 794 * @param format - A scanf-style format string, followed by the pointers to variables to store the results.
ampembeng 15:6f2798e45099 795 */
ampembeng 15:6f2798e45099 796 int scanf(const char* format, ...);
ampembeng 15:6f2798e45099 797 #endif
ampembeng 15:6f2798e45099 798
ampembeng 15:6f2798e45099 799 protected:
ampembeng 15:6f2798e45099 800 /**
ampembeng 15:6f2798e45099 801 * Used to pass information to callbacks.
ampembeng 15:6f2798e45099 802 * @ingroup INTERNALS
ampembeng 15:6f2798e45099 803 */
ampembeng 15:6f2798e45099 804 MODSERIAL_IRQ_INFO callbackInfo;
ampembeng 15:6f2798e45099 805
ampembeng 15:6f2798e45099 806 /**
ampembeng 15:6f2798e45099 807 * Remove the last char placed into the rx buffer.
ampembeng 15:6f2798e45099 808 * This is an operation that can only be performed
ampembeng 15:6f2798e45099 809 * by an rxCallback function. To protect the buffers
ampembeng 15:6f2798e45099 810 * this function is defined protected so that a
ampembeng 15:6f2798e45099 811 * regular application cannot call it directly. It
ampembeng 15:6f2798e45099 812 * can only be called by the public version within a
ampembeng 15:6f2798e45099 813 * MODSERIAL_IRQ_INFO class.
ampembeng 15:6f2798e45099 814 * @ingroup INTERNALS
ampembeng 15:6f2798e45099 815 * @return The byte removed from the buffer.
ampembeng 15:6f2798e45099 816 */
ampembeng 15:6f2798e45099 817 int rxDiscardLastChar(void);
ampembeng 15:6f2798e45099 818
ampembeng 15:6f2798e45099 819 private:
ampembeng 15:6f2798e45099 820
ampembeng 15:6f2798e45099 821 /**
ampembeng 15:6f2798e45099 822 * A pointer to the UART peripheral base address being used.
ampembeng 15:6f2798e45099 823 * @ingroup INTERNALS
ampembeng 15:6f2798e45099 824 */
ampembeng 15:6f2798e45099 825 void *_base;
ampembeng 15:6f2798e45099 826
ampembeng 15:6f2798e45099 827 /**
ampembeng 15:6f2798e45099 828 * The last byte to pass through the TX IRQ handler.
ampembeng 15:6f2798e45099 829 * @ingroup INTERNALS
ampembeng 15:6f2798e45099 830 */
ampembeng 15:6f2798e45099 831 volatile char txc;
ampembeng 15:6f2798e45099 832
ampembeng 15:6f2798e45099 833 /**
ampembeng 15:6f2798e45099 834 * The last byte to pass through the RX IRQ handler.
ampembeng 15:6f2798e45099 835 * @ingroup INTERNALS
ampembeng 15:6f2798e45099 836 */
ampembeng 15:6f2798e45099 837 volatile char rxc;
ampembeng 15:6f2798e45099 838
ampembeng 15:6f2798e45099 839 /**
ampembeng 15:6f2798e45099 840 * Pointers to the TX and RX buffers.
ampembeng 15:6f2798e45099 841 * @ingroup INTERNALS
ampembeng 15:6f2798e45099 842 */
ampembeng 15:6f2798e45099 843 volatile char *buffer[2];
ampembeng 15:6f2798e45099 844
ampembeng 15:6f2798e45099 845 /**
ampembeng 15:6f2798e45099 846 * Buffer in pointers.
ampembeng 15:6f2798e45099 847 * @ingroup INTERNALS
ampembeng 15:6f2798e45099 848 */
ampembeng 15:6f2798e45099 849 volatile int buffer_in[2];
ampembeng 15:6f2798e45099 850
ampembeng 15:6f2798e45099 851 /**
ampembeng 15:6f2798e45099 852 * Buffer out pointers.
ampembeng 15:6f2798e45099 853 * @ingroup INTERNALS
ampembeng 15:6f2798e45099 854 */
ampembeng 15:6f2798e45099 855 volatile int buffer_out[2];
ampembeng 15:6f2798e45099 856
ampembeng 15:6f2798e45099 857 /**
ampembeng 15:6f2798e45099 858 * Buffer lengths.
ampembeng 15:6f2798e45099 859 * @ingroup INTERNALS
ampembeng 15:6f2798e45099 860 */
ampembeng 15:6f2798e45099 861 volatile int buffer_size[2];
ampembeng 15:6f2798e45099 862
ampembeng 15:6f2798e45099 863 /**
ampembeng 15:6f2798e45099 864 * Buffer content counters.
ampembeng 15:6f2798e45099 865 * @ingroup INTERNALS
ampembeng 15:6f2798e45099 866 */
ampembeng 15:6f2798e45099 867 volatile int buffer_count[2];
ampembeng 15:6f2798e45099 868
ampembeng 15:6f2798e45099 869 /**
ampembeng 15:6f2798e45099 870 * Buffer overflow.
ampembeng 15:6f2798e45099 871 * @ingroup INTERNALS
ampembeng 15:6f2798e45099 872 */
ampembeng 15:6f2798e45099 873 volatile int buffer_overflow[2];
ampembeng 15:6f2798e45099 874
ampembeng 15:6f2798e45099 875 /**
ampembeng 15:6f2798e45099 876 * Auto-detect character.
ampembeng 15:6f2798e45099 877 * @ingroup INTERNALS
ampembeng 15:6f2798e45099 878 */
ampembeng 15:6f2798e45099 879 volatile char auto_detect_char;
ampembeng 15:6f2798e45099 880
ampembeng 15:6f2798e45099 881 /**
ampembeng 15:6f2798e45099 882 * Callback system.
ampembeng 15:6f2798e45099 883 * @ingroup INTERNALS
ampembeng 15:6f2798e45099 884 */
ampembeng 15:6f2798e45099 885 MODSERIAL_callback _isr[NumOfIrqTypes];
ampembeng 15:6f2798e45099 886
ampembeng 15:6f2798e45099 887 /**
ampembeng 15:6f2798e45099 888 * TX Interrupt Service Routine.
ampembeng 15:6f2798e45099 889 * @ingroup INTERNALS
ampembeng 15:6f2798e45099 890 */
ampembeng 15:6f2798e45099 891 void isr_tx(bool doCallback);
ampembeng 15:6f2798e45099 892
ampembeng 15:6f2798e45099 893 /**
ampembeng 15:6f2798e45099 894 * TX Interrupt Service Routine stub version.
ampembeng 15:6f2798e45099 895 * @ingroup INTERNALS
ampembeng 15:6f2798e45099 896 */
ampembeng 15:6f2798e45099 897 void isr_tx(void) { isr_tx(true); }
ampembeng 15:6f2798e45099 898
ampembeng 15:6f2798e45099 899
ampembeng 15:6f2798e45099 900 /**
ampembeng 15:6f2798e45099 901 * RX Interrupt Service Routine.
ampembeng 15:6f2798e45099 902 * @ingroup INTERNALS
ampembeng 15:6f2798e45099 903 */
ampembeng 15:6f2798e45099 904 void isr_rx(void);
ampembeng 15:6f2798e45099 905
ampembeng 15:6f2798e45099 906 /**
ampembeng 15:6f2798e45099 907 * Get a character from the RX buffer
ampembeng 15:6f2798e45099 908 * @ingroup INTERNALS
ampembeng 15:6f2798e45099 909 * @param bool True to block (wait for input)
ampembeng 15:6f2798e45099 910 * @return A byte from the buffer.
ampembeng 15:6f2798e45099 911 */
ampembeng 15:6f2798e45099 912 int __getc(bool);
ampembeng 15:6f2798e45099 913
ampembeng 15:6f2798e45099 914 /**
ampembeng 15:6f2798e45099 915 * Put a character from the TX buffer
ampembeng 15:6f2798e45099 916 * @ingroup INTERNALS
ampembeng 15:6f2798e45099 917 * @param bool True to block (wait for space in the TX buffer if full)
ampembeng 15:6f2798e45099 918 * @return 0 on success
ampembeng 15:6f2798e45099 919 */
ampembeng 15:6f2798e45099 920 int __putc(int c, bool);
ampembeng 15:6f2798e45099 921
ampembeng 15:6f2798e45099 922 /**
ampembeng 15:6f2798e45099 923 * Function: _putc
ampembeng 15:6f2798e45099 924 * Overloaded virtual function.
ampembeng 15:6f2798e45099 925 */
ampembeng 15:6f2798e45099 926 virtual int _putc(int c) { return __putc(c, true); }
ampembeng 15:6f2798e45099 927
ampembeng 15:6f2798e45099 928 /**
ampembeng 15:6f2798e45099 929 * Function: _getc
ampembeng 15:6f2798e45099 930 * Overloaded virtual function.
ampembeng 15:6f2798e45099 931 */
ampembeng 15:6f2798e45099 932 virtual int _getc() { return __getc(true); }
ampembeng 15:6f2798e45099 933
ampembeng 15:6f2798e45099 934 /**
ampembeng 15:6f2798e45099 935 * Function: init
ampembeng 15:6f2798e45099 936 * Initialize the MODSERIAL object
ampembeng 15:6f2798e45099 937 * @ingroup INTERNALS
ampembeng 15:6f2798e45099 938 */
ampembeng 15:6f2798e45099 939 void init(int txSize, int rxSize, PinName rx);
ampembeng 15:6f2798e45099 940
ampembeng 15:6f2798e45099 941 /**
ampembeng 15:6f2798e45099 942 * Function: flushBuffer
ampembeng 15:6f2798e45099 943 * @ingroup INTERNALS
ampembeng 15:6f2798e45099 944 */
ampembeng 15:6f2798e45099 945 void flushBuffer(IrqType type);
ampembeng 15:6f2798e45099 946
ampembeng 15:6f2798e45099 947 /**
ampembeng 15:6f2798e45099 948 * Function: resizeBuffer
ampembeng 15:6f2798e45099 949 * @ingroup INTERNALS
ampembeng 15:6f2798e45099 950 */
ampembeng 15:6f2798e45099 951 int resizeBuffer(int size, IrqType type = RxIrq, bool memory_check = true);
ampembeng 15:6f2798e45099 952
ampembeng 15:6f2798e45099 953 /**
ampembeng 15:6f2798e45099 954 * Function: moveRingBuffer
ampembeng 15:6f2798e45099 955 * @ingroup INTERNALS
ampembeng 15:6f2798e45099 956 */
ampembeng 15:6f2798e45099 957 void moveRingBuffer(char * newBuffer, IrqType type);
ampembeng 15:6f2798e45099 958
ampembeng 15:6f2798e45099 959
ampembeng 15:6f2798e45099 960
ampembeng 15:6f2798e45099 961
ampembeng 15:6f2798e45099 962 //DEVICE SPECIFIC FUNCTIONS:
ampembeng 15:6f2798e45099 963 private:
ampembeng 15:6f2798e45099 964 /**
ampembeng 15:6f2798e45099 965 * Set pointers to UART and IRQ
ampembeng 15:6f2798e45099 966 */
ampembeng 15:6f2798e45099 967 void setBase( void );
ampembeng 15:6f2798e45099 968
ampembeng 15:6f2798e45099 969 /**
ampembeng 15:6f2798e45099 970 * If required, allows for adding specific settings
ampembeng 15:6f2798e45099 971 */
ampembeng 15:6f2798e45099 972 void initDevice( void );
ampembeng 15:6f2798e45099 973
ampembeng 15:6f2798e45099 974 IRQn_Type _IRQ;
ampembeng 15:6f2798e45099 975
ampembeng 15:6f2798e45099 976 public:
ampembeng 15:6f2798e45099 977 /**
ampembeng 15:6f2798e45099 978 * Function: txIsBusy
ampembeng 15:6f2798e45099 979 *
ampembeng 15:6f2798e45099 980 * If the Uart is still actively sending characters this
ampembeng 15:6f2798e45099 981 * function will return true.
ampembeng 15:6f2798e45099 982 *
ampembeng 15:6f2798e45099 983 * @ingroup API
ampembeng 15:6f2798e45099 984 * @return bool
ampembeng 15:6f2798e45099 985 */
ampembeng 15:6f2798e45099 986 bool txIsBusy(void);
ampembeng 15:6f2798e45099 987
ampembeng 15:6f2798e45099 988
ampembeng 15:6f2798e45099 989 };
ampembeng 15:6f2798e45099 990
ampembeng 15:6f2798e45099 991 }; // namespace AjK ends
ampembeng 15:6f2798e45099 992
ampembeng 15:6f2798e45099 993 using namespace AjK;
ampembeng 15:6f2798e45099 994
ampembeng 15:6f2798e45099 995 #endif
ampembeng 15:6f2798e45099 996
ampembeng 15:6f2798e45099 997