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