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