Reaction Wheel Actuated Satellite Dynamics Test Platform

Dependencies:   mbed

Diploma Thesis in Aerospace Engineering, January 2014

University of Applied Sciences Munich, Faculty 03

Electronics:

  • 1x mbed NXP LPC 1768 Microcontroller
  • 2x XBee S1 Radios + Sparkfun USB Adapter
  • 1x CHR UM6-lt IMU
  • 4x Graupner BEC 8 Motor Controllers
  • 4x ROXXY 2826/09 Brushless Motors
  • 1x Hacker TopFuel LiPo 1300mAh Battery
  • 1x big Selfmade BreakOutBoard to connect all components
  • 1x small BreakOutBoard to connect IMU

Hardware developed with Catia V5R20

Manufactoring Technology: Rapid Prototyping - EOS Formiga P110

Controlled via text based menu with DockLight

__________________

Committer:
DimitriGruebel
Date:
Wed Jul 09 07:35:50 2014 +0000
Revision:
0:1447d2f773db
Dynamics Test Platform

Who changed what in which revision?

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