SMS Scheduler that will automatically send and receive text messages using the Enfora 1308 GSM Modem. Please note that it uses a modified NetServices library and to set the baud rate for the GSM Modem to 19.2K.

Dependencies:   mbed

Committer:
mafischl
Date:
Thu Oct 13 18:01:31 2011 +0000
Revision:
1:5a7cce9994a3
Parent:
0:d9266031f832

        

Who changed what in which revision?

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