Futaba S-BUS Library. Let you control 16 servos and 2 digital channels

Dependencies:   mbed

Committer:
Digixx
Date:
Wed Mar 07 18:18:43 2012 +0000
Revision:
0:83e415034198

        

Who changed what in which revision?

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