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