Official Sheffield ARMBand micro:bit program

Committer:
MrBedfordVan
Date:
Mon Oct 17 12:41:20 2016 +0000
Revision:
0:b9164b348919
Official Sheffield ARMBand Micro:bit program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MrBedfordVan 0:b9164b348919 1 /*
MrBedfordVan 0:b9164b348919 2 The MIT License (MIT)
MrBedfordVan 0:b9164b348919 3
MrBedfordVan 0:b9164b348919 4 Copyright (c) 2016 British Broadcasting Corporation.
MrBedfordVan 0:b9164b348919 5 This software is provided by Lancaster University by arrangement with the BBC.
MrBedfordVan 0:b9164b348919 6
MrBedfordVan 0:b9164b348919 7 Permission is hereby granted, free of charge, to any person obtaining a
MrBedfordVan 0:b9164b348919 8 copy of this software and associated documentation files (the "Software"),
MrBedfordVan 0:b9164b348919 9 to deal in the Software without restriction, including without limitation
MrBedfordVan 0:b9164b348919 10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
MrBedfordVan 0:b9164b348919 11 and/or sell copies of the Software, and to permit persons to whom the
MrBedfordVan 0:b9164b348919 12 Software is furnished to do so, subject to the following conditions:
MrBedfordVan 0:b9164b348919 13
MrBedfordVan 0:b9164b348919 14 The above copyright notice and this permission notice shall be included in
MrBedfordVan 0:b9164b348919 15 all copies or substantial portions of the Software.
MrBedfordVan 0:b9164b348919 16
MrBedfordVan 0:b9164b348919 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
MrBedfordVan 0:b9164b348919 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
MrBedfordVan 0:b9164b348919 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
MrBedfordVan 0:b9164b348919 20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
MrBedfordVan 0:b9164b348919 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
MrBedfordVan 0:b9164b348919 22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
MrBedfordVan 0:b9164b348919 23 DEALINGS IN THE SOFTWARE.
MrBedfordVan 0:b9164b348919 24 */
MrBedfordVan 0:b9164b348919 25
MrBedfordVan 0:b9164b348919 26 #ifndef MICROBIT_SERIAL_H
MrBedfordVan 0:b9164b348919 27 #define MICROBIT_SERIAL_H
MrBedfordVan 0:b9164b348919 28
MrBedfordVan 0:b9164b348919 29 #include "mbed.h"
MrBedfordVan 0:b9164b348919 30 #include "ManagedString.h"
MrBedfordVan 0:b9164b348919 31
MrBedfordVan 0:b9164b348919 32 #define MICROBIT_SERIAL_DEFAULT_BAUD_RATE 115200
MrBedfordVan 0:b9164b348919 33 #define MICROBIT_SERIAL_DEFAULT_BUFFER_SIZE 20
MrBedfordVan 0:b9164b348919 34
MrBedfordVan 0:b9164b348919 35 #define MICROBIT_SERIAL_EVT_DELIM_MATCH 1
MrBedfordVan 0:b9164b348919 36 #define MICROBIT_SERIAL_EVT_HEAD_MATCH 2
MrBedfordVan 0:b9164b348919 37 #define MICROBIT_SERIAL_EVT_RX_FULL 3
MrBedfordVan 0:b9164b348919 38
MrBedfordVan 0:b9164b348919 39 #define MICROBIT_SERIAL_RX_IN_USE 1
MrBedfordVan 0:b9164b348919 40 #define MICROBIT_SERIAL_TX_IN_USE 2
MrBedfordVan 0:b9164b348919 41 #define MICROBIT_SERIAL_RX_BUFF_INIT 4
MrBedfordVan 0:b9164b348919 42 #define MICROBIT_SERIAL_TX_BUFF_INIT 8
MrBedfordVan 0:b9164b348919 43
MrBedfordVan 0:b9164b348919 44
MrBedfordVan 0:b9164b348919 45 enum MicroBitSerialMode
MrBedfordVan 0:b9164b348919 46 {
MrBedfordVan 0:b9164b348919 47 ASYNC,
MrBedfordVan 0:b9164b348919 48 SYNC_SPINWAIT,
MrBedfordVan 0:b9164b348919 49 SYNC_SLEEP
MrBedfordVan 0:b9164b348919 50 };
MrBedfordVan 0:b9164b348919 51
MrBedfordVan 0:b9164b348919 52 /**
MrBedfordVan 0:b9164b348919 53 * Class definition for MicroBitSerial.
MrBedfordVan 0:b9164b348919 54 *
MrBedfordVan 0:b9164b348919 55 * Represents an instance of RawSerial which accepts micro:bit specific data types.
MrBedfordVan 0:b9164b348919 56 */
MrBedfordVan 0:b9164b348919 57 class MicroBitSerial : public RawSerial
MrBedfordVan 0:b9164b348919 58 {
MrBedfordVan 0:b9164b348919 59
MrBedfordVan 0:b9164b348919 60 //holds that state of the mutex locks for all MicroBitSerial instances.
MrBedfordVan 0:b9164b348919 61 static uint8_t status;
MrBedfordVan 0:b9164b348919 62
MrBedfordVan 0:b9164b348919 63 //holds the state of the baudrate for all MicroBitSerial instances.
MrBedfordVan 0:b9164b348919 64 static int baudrate;
MrBedfordVan 0:b9164b348919 65
MrBedfordVan 0:b9164b348919 66 //delimeters used for matching on receive.
MrBedfordVan 0:b9164b348919 67 ManagedString delimeters;
MrBedfordVan 0:b9164b348919 68
MrBedfordVan 0:b9164b348919 69 //a variable used when a user calls the eventAfter() method.
MrBedfordVan 0:b9164b348919 70 int rxBuffHeadMatch;
MrBedfordVan 0:b9164b348919 71
MrBedfordVan 0:b9164b348919 72 uint8_t *rxBuff;
MrBedfordVan 0:b9164b348919 73 uint8_t rxBuffSize;
MrBedfordVan 0:b9164b348919 74 volatile uint16_t rxBuffHead;
MrBedfordVan 0:b9164b348919 75 uint16_t rxBuffTail;
MrBedfordVan 0:b9164b348919 76
MrBedfordVan 0:b9164b348919 77
MrBedfordVan 0:b9164b348919 78 uint8_t *txBuff;
MrBedfordVan 0:b9164b348919 79 uint8_t txBuffSize;
MrBedfordVan 0:b9164b348919 80 uint16_t txBuffHead;
MrBedfordVan 0:b9164b348919 81 volatile uint16_t txBuffTail;
MrBedfordVan 0:b9164b348919 82
MrBedfordVan 0:b9164b348919 83 /**
MrBedfordVan 0:b9164b348919 84 * An internal interrupt callback for MicroBitSerial configured for when a
MrBedfordVan 0:b9164b348919 85 * character is received.
MrBedfordVan 0:b9164b348919 86 *
MrBedfordVan 0:b9164b348919 87 * Each time a character is received fill our circular buffer!
MrBedfordVan 0:b9164b348919 88 */
MrBedfordVan 0:b9164b348919 89 void dataReceived();
MrBedfordVan 0:b9164b348919 90
MrBedfordVan 0:b9164b348919 91 /**
MrBedfordVan 0:b9164b348919 92 * An internal interrupt callback for MicroBitSerial.
MrBedfordVan 0:b9164b348919 93 *
MrBedfordVan 0:b9164b348919 94 * Each time the Serial module's buffer is empty, write a character if we have
MrBedfordVan 0:b9164b348919 95 * characters to write.
MrBedfordVan 0:b9164b348919 96 */
MrBedfordVan 0:b9164b348919 97 void dataWritten();
MrBedfordVan 0:b9164b348919 98
MrBedfordVan 0:b9164b348919 99 /**
MrBedfordVan 0:b9164b348919 100 * An internal method to configure an interrupt on tx buffer and also
MrBedfordVan 0:b9164b348919 101 * a best effort copy operation to move bytes from a user buffer to our txBuff
MrBedfordVan 0:b9164b348919 102 *
MrBedfordVan 0:b9164b348919 103 * @param string a pointer to the first character of the users' buffer.
MrBedfordVan 0:b9164b348919 104 *
MrBedfordVan 0:b9164b348919 105 * @param len the length of the string, and ultimately the maximum number of bytes
MrBedfordVan 0:b9164b348919 106 * that will be copied dependent on the state of txBuff
MrBedfordVan 0:b9164b348919 107 *
MrBedfordVan 0:b9164b348919 108 * @param mode determines whether to configure the current fiber context or not. If
MrBedfordVan 0:b9164b348919 109 * The mode is SYNC_SPINWAIT, the context will not be configured, otherwise
MrBedfordVan 0:b9164b348919 110 * no context will be configured.
MrBedfordVan 0:b9164b348919 111 *
MrBedfordVan 0:b9164b348919 112 * @return the number of bytes copied into the buffer.
MrBedfordVan 0:b9164b348919 113 */
MrBedfordVan 0:b9164b348919 114 int setTxInterrupt(uint8_t *string, int len, MicroBitSerialMode mode);
MrBedfordVan 0:b9164b348919 115
MrBedfordVan 0:b9164b348919 116 /**
MrBedfordVan 0:b9164b348919 117 * Locks the mutex so that others can't use this serial instance for reception
MrBedfordVan 0:b9164b348919 118 */
MrBedfordVan 0:b9164b348919 119 void lockRx();
MrBedfordVan 0:b9164b348919 120
MrBedfordVan 0:b9164b348919 121 /**
MrBedfordVan 0:b9164b348919 122 * Locks the mutex so that others can't use this serial instance for transmission
MrBedfordVan 0:b9164b348919 123 */
MrBedfordVan 0:b9164b348919 124 void lockTx();
MrBedfordVan 0:b9164b348919 125
MrBedfordVan 0:b9164b348919 126 /**
MrBedfordVan 0:b9164b348919 127 * Unlocks the mutex so that others can use this serial instance for reception
MrBedfordVan 0:b9164b348919 128 */
MrBedfordVan 0:b9164b348919 129 void unlockRx();
MrBedfordVan 0:b9164b348919 130
MrBedfordVan 0:b9164b348919 131 /**
MrBedfordVan 0:b9164b348919 132 * Unlocks the mutex so that others can use this serial instance for transmission
MrBedfordVan 0:b9164b348919 133 */
MrBedfordVan 0:b9164b348919 134 void unlockTx();
MrBedfordVan 0:b9164b348919 135
MrBedfordVan 0:b9164b348919 136 /**
MrBedfordVan 0:b9164b348919 137 * We do not want to always have our buffers initialised, especially if users to not
MrBedfordVan 0:b9164b348919 138 * use them. We only bring them up on demand.
MrBedfordVan 0:b9164b348919 139 */
MrBedfordVan 0:b9164b348919 140 int initialiseRx();
MrBedfordVan 0:b9164b348919 141
MrBedfordVan 0:b9164b348919 142 /**
MrBedfordVan 0:b9164b348919 143 * We do not want to always have our buffers initialised, especially if users to not
MrBedfordVan 0:b9164b348919 144 * use them. We only bring them up on demand.
MrBedfordVan 0:b9164b348919 145 */
MrBedfordVan 0:b9164b348919 146 int initialiseTx();
MrBedfordVan 0:b9164b348919 147
MrBedfordVan 0:b9164b348919 148 /**
MrBedfordVan 0:b9164b348919 149 * An internal method that either spin waits if mode is set to SYNC_SPINWAIT
MrBedfordVan 0:b9164b348919 150 * or puts the fiber to sleep if the mode is set to SYNC_SLEEP
MrBedfordVan 0:b9164b348919 151 *
MrBedfordVan 0:b9164b348919 152 * @param mode the selected mode, one of: ASYNC, SYNC_SPINWAIT, SYNC_SLEEP
MrBedfordVan 0:b9164b348919 153 */
MrBedfordVan 0:b9164b348919 154 void send(MicroBitSerialMode mode);
MrBedfordVan 0:b9164b348919 155
MrBedfordVan 0:b9164b348919 156 /**
MrBedfordVan 0:b9164b348919 157 * Reads a single character from the rxBuff
MrBedfordVan 0:b9164b348919 158 *
MrBedfordVan 0:b9164b348919 159 * @param mode the selected mode, one of: ASYNC, SYNC_SPINWAIT, SYNC_SLEEP. Each mode
MrBedfordVan 0:b9164b348919 160 * gives a different behaviour:
MrBedfordVan 0:b9164b348919 161 *
MrBedfordVan 0:b9164b348919 162 * ASYNC - A character is read from the rxBuff if available, if there
MrBedfordVan 0:b9164b348919 163 * are no characters to be read, a value of zero is returned immediately.
MrBedfordVan 0:b9164b348919 164 *
MrBedfordVan 0:b9164b348919 165 * SYNC_SPINWAIT - A character is read from the rxBuff if available, if there
MrBedfordVan 0:b9164b348919 166 * are no characters to be read, this method will spin
MrBedfordVan 0:b9164b348919 167 * (lock up the processor) until a character is available.
MrBedfordVan 0:b9164b348919 168 *
MrBedfordVan 0:b9164b348919 169 * SYNC_SLEEP - A character is read from the rxBuff if available, if there
MrBedfordVan 0:b9164b348919 170 * are no characters to be read, the calling fiber sleeps
MrBedfordVan 0:b9164b348919 171 * until there is a character available.
MrBedfordVan 0:b9164b348919 172 *
MrBedfordVan 0:b9164b348919 173 * Defaults to SYNC_SLEEP.
MrBedfordVan 0:b9164b348919 174 *
MrBedfordVan 0:b9164b348919 175 * @return a character from the circular buffer, or MICROBIT_NO_DATA is there
MrBedfordVan 0:b9164b348919 176 * are no characters in the buffer.
MrBedfordVan 0:b9164b348919 177 */
MrBedfordVan 0:b9164b348919 178 int getChar(MicroBitSerialMode mode);
MrBedfordVan 0:b9164b348919 179
MrBedfordVan 0:b9164b348919 180 /**
MrBedfordVan 0:b9164b348919 181 * An internal method that copies values from a circular buffer to a linear buffer.
MrBedfordVan 0:b9164b348919 182 *
MrBedfordVan 0:b9164b348919 183 * @param circularBuff a pointer to the source circular buffer
MrBedfordVan 0:b9164b348919 184 *
MrBedfordVan 0:b9164b348919 185 * @param circularBuffSize the size of the circular buffer
MrBedfordVan 0:b9164b348919 186 *
MrBedfordVan 0:b9164b348919 187 * @param linearBuff a pointer to the destination linear buffer
MrBedfordVan 0:b9164b348919 188 *
MrBedfordVan 0:b9164b348919 189 * @param tailPosition the tail position in the circular buffer you want to copy from
MrBedfordVan 0:b9164b348919 190 *
MrBedfordVan 0:b9164b348919 191 * @param headPosition the head position in the circular buffer you want to copy to
MrBedfordVan 0:b9164b348919 192 *
MrBedfordVan 0:b9164b348919 193 * @note this method assumes that the linear buffer has the appropriate amount of
MrBedfordVan 0:b9164b348919 194 * memory to contain the copy operation
MrBedfordVan 0:b9164b348919 195 */
MrBedfordVan 0:b9164b348919 196 void circularCopy(uint8_t *circularBuff, uint8_t circularBuffSize, uint8_t *linearBuff, uint16_t tailPosition, uint16_t headPosition);
MrBedfordVan 0:b9164b348919 197
MrBedfordVan 0:b9164b348919 198 public:
MrBedfordVan 0:b9164b348919 199
MrBedfordVan 0:b9164b348919 200 /**
MrBedfordVan 0:b9164b348919 201 * Constructor.
MrBedfordVan 0:b9164b348919 202 * Create an instance of MicroBitSerial
MrBedfordVan 0:b9164b348919 203 *
MrBedfordVan 0:b9164b348919 204 * @param tx the Pin to be used for transmission
MrBedfordVan 0:b9164b348919 205 *
MrBedfordVan 0:b9164b348919 206 * @param rx the Pin to be used for receiving data
MrBedfordVan 0:b9164b348919 207 *
MrBedfordVan 0:b9164b348919 208 * @param rxBufferSize the size of the buffer to be used for receiving bytes
MrBedfordVan 0:b9164b348919 209 *
MrBedfordVan 0:b9164b348919 210 * @param txBufferSize the size of the buffer to be used for transmitting bytes
MrBedfordVan 0:b9164b348919 211 *
MrBedfordVan 0:b9164b348919 212 * @code
MrBedfordVan 0:b9164b348919 213 * MicroBitSerial serial(USBTX, USBRX);
MrBedfordVan 0:b9164b348919 214 * @endcode
MrBedfordVan 0:b9164b348919 215 * @note the default baud rate is 115200. More API details can be found:
MrBedfordVan 0:b9164b348919 216 * -https://github.com/mbedmicro/mbed/blob/master/libraries/mbed/api/SerialBase.h
MrBedfordVan 0:b9164b348919 217 * -https://github.com/mbedmicro/mbed/blob/master/libraries/mbed/api/RawSerial.h
MrBedfordVan 0:b9164b348919 218 *
MrBedfordVan 0:b9164b348919 219 * Buffers aren't allocated until the first send or receive respectively.
MrBedfordVan 0:b9164b348919 220 */
MrBedfordVan 0:b9164b348919 221 MicroBitSerial(PinName tx, PinName rx, uint8_t rxBufferSize = MICROBIT_SERIAL_DEFAULT_BUFFER_SIZE, uint8_t txBufferSize = MICROBIT_SERIAL_DEFAULT_BUFFER_SIZE);
MrBedfordVan 0:b9164b348919 222
MrBedfordVan 0:b9164b348919 223 /**
MrBedfordVan 0:b9164b348919 224 * Sends a single character over the serial line.
MrBedfordVan 0:b9164b348919 225 *
MrBedfordVan 0:b9164b348919 226 * @param c the character to send
MrBedfordVan 0:b9164b348919 227 *
MrBedfordVan 0:b9164b348919 228 * @param mode the selected mode, one of: ASYNC, SYNC_SPINWAIT, SYNC_SLEEP. Each mode
MrBedfordVan 0:b9164b348919 229 * gives a different behaviour:
MrBedfordVan 0:b9164b348919 230 *
MrBedfordVan 0:b9164b348919 231 * ASYNC - the character is copied into the txBuff and returns immediately.
MrBedfordVan 0:b9164b348919 232 *
MrBedfordVan 0:b9164b348919 233 * SYNC_SPINWAIT - the character is copied into the txBuff and this method
MrBedfordVan 0:b9164b348919 234 * will spin (lock up the processor) until the character has
MrBedfordVan 0:b9164b348919 235 * been sent.
MrBedfordVan 0:b9164b348919 236 *
MrBedfordVan 0:b9164b348919 237 * SYNC_SLEEP - the character is copied into the txBuff and the fiber sleeps
MrBedfordVan 0:b9164b348919 238 * until the character has been sent. This allows other fibers
MrBedfordVan 0:b9164b348919 239 * to continue execution.
MrBedfordVan 0:b9164b348919 240 *
MrBedfordVan 0:b9164b348919 241 * Defaults to SYNC_SLEEP.
MrBedfordVan 0:b9164b348919 242 *
MrBedfordVan 0:b9164b348919 243 * @return the number of bytes written, or MICROBIT_SERIAL_IN_USE if another fiber
MrBedfordVan 0:b9164b348919 244 * is using the serial instance for transmission.
MrBedfordVan 0:b9164b348919 245 */
MrBedfordVan 0:b9164b348919 246 int sendChar(char c, MicroBitSerialMode mode = MICROBIT_DEFAULT_SERIAL_MODE);
MrBedfordVan 0:b9164b348919 247
MrBedfordVan 0:b9164b348919 248 /**
MrBedfordVan 0:b9164b348919 249 * Sends a ManagedString over the serial line.
MrBedfordVan 0:b9164b348919 250 *
MrBedfordVan 0:b9164b348919 251 * @param s the string to send
MrBedfordVan 0:b9164b348919 252 *
MrBedfordVan 0:b9164b348919 253 * @param mode the selected mode, one of: ASYNC, SYNC_SPINWAIT, SYNC_SLEEP. Each mode
MrBedfordVan 0:b9164b348919 254 * gives a different behaviour:
MrBedfordVan 0:b9164b348919 255 *
MrBedfordVan 0:b9164b348919 256 * ASYNC - bytes are copied into the txBuff and returns immediately.
MrBedfordVan 0:b9164b348919 257 *
MrBedfordVan 0:b9164b348919 258 * SYNC_SPINWAIT - bytes are copied into the txBuff and this method
MrBedfordVan 0:b9164b348919 259 * will spin (lock up the processor) until all bytes
MrBedfordVan 0:b9164b348919 260 * have been sent.
MrBedfordVan 0:b9164b348919 261 *
MrBedfordVan 0:b9164b348919 262 * SYNC_SLEEP - bytes are copied into the txBuff and the fiber sleeps
MrBedfordVan 0:b9164b348919 263 * until all bytes have been sent. This allows other fibers
MrBedfordVan 0:b9164b348919 264 * to continue execution.
MrBedfordVan 0:b9164b348919 265 *
MrBedfordVan 0:b9164b348919 266 * Defaults to SYNC_SLEEP.
MrBedfordVan 0:b9164b348919 267 *
MrBedfordVan 0:b9164b348919 268 * @return the number of bytes written, MICROBIT_SERIAL_IN_USE if another fiber
MrBedfordVan 0:b9164b348919 269 * is using the serial instance for transmission, MICROBIT_INVALID_PARAMETER
MrBedfordVan 0:b9164b348919 270 * if buffer is invalid, or the given bufferLen is <= 0.
MrBedfordVan 0:b9164b348919 271 */
MrBedfordVan 0:b9164b348919 272 int send(ManagedString s, MicroBitSerialMode mode = MICROBIT_DEFAULT_SERIAL_MODE);
MrBedfordVan 0:b9164b348919 273
MrBedfordVan 0:b9164b348919 274 /**
MrBedfordVan 0:b9164b348919 275 * Sends a buffer of known length over the serial line.
MrBedfordVan 0:b9164b348919 276 *
MrBedfordVan 0:b9164b348919 277 * @param buffer a pointer to the first character of the buffer
MrBedfordVan 0:b9164b348919 278 *
MrBedfordVan 0:b9164b348919 279 * @param len the number of bytes that are safely available to read.
MrBedfordVan 0:b9164b348919 280 *
MrBedfordVan 0:b9164b348919 281 * @param mode the selected mode, one of: ASYNC, SYNC_SPINWAIT, SYNC_SLEEP. Each mode
MrBedfordVan 0:b9164b348919 282 * gives a different behaviour:
MrBedfordVan 0:b9164b348919 283 *
MrBedfordVan 0:b9164b348919 284 * ASYNC - bytes are copied into the txBuff and returns immediately.
MrBedfordVan 0:b9164b348919 285 *
MrBedfordVan 0:b9164b348919 286 * SYNC_SPINWAIT - bytes are copied into the txBuff and this method
MrBedfordVan 0:b9164b348919 287 * will spin (lock up the processor) until all bytes
MrBedfordVan 0:b9164b348919 288 * have been sent.
MrBedfordVan 0:b9164b348919 289 *
MrBedfordVan 0:b9164b348919 290 * SYNC_SLEEP - bytes are copied into the txBuff and the fiber sleeps
MrBedfordVan 0:b9164b348919 291 * until all bytes have been sent. This allows other fibers
MrBedfordVan 0:b9164b348919 292 * to continue execution.
MrBedfordVan 0:b9164b348919 293 *
MrBedfordVan 0:b9164b348919 294 * Defaults to SYNC_SLEEP.
MrBedfordVan 0:b9164b348919 295 *
MrBedfordVan 0:b9164b348919 296 * @return the number of bytes written, MICROBIT_SERIAL_IN_USE if another fiber
MrBedfordVan 0:b9164b348919 297 * is using the serial instance for transmission, MICROBIT_INVALID_PARAMETER
MrBedfordVan 0:b9164b348919 298 * if buffer is invalid, or the given bufferLen is <= 0.
MrBedfordVan 0:b9164b348919 299 */
MrBedfordVan 0:b9164b348919 300 int send(uint8_t *buffer, int bufferLen, MicroBitSerialMode mode = MICROBIT_DEFAULT_SERIAL_MODE);
MrBedfordVan 0:b9164b348919 301
MrBedfordVan 0:b9164b348919 302 /**
MrBedfordVan 0:b9164b348919 303 * Reads a single character from the rxBuff
MrBedfordVan 0:b9164b348919 304 *
MrBedfordVan 0:b9164b348919 305 * @param mode the selected mode, one of: ASYNC, SYNC_SPINWAIT, SYNC_SLEEP. Each mode
MrBedfordVan 0:b9164b348919 306 * gives a different behaviour:
MrBedfordVan 0:b9164b348919 307 *
MrBedfordVan 0:b9164b348919 308 * ASYNC - A character is read from the rxBuff if available, if there
MrBedfordVan 0:b9164b348919 309 * are no characters to be read, a value of MICROBIT_NO_DATA is returned immediately.
MrBedfordVan 0:b9164b348919 310 *
MrBedfordVan 0:b9164b348919 311 * SYNC_SPINWAIT - A character is read from the rxBuff if available, if there
MrBedfordVan 0:b9164b348919 312 * are no characters to be read, this method will spin
MrBedfordVan 0:b9164b348919 313 * (lock up the processor) until a character is available.
MrBedfordVan 0:b9164b348919 314 *
MrBedfordVan 0:b9164b348919 315 * SYNC_SLEEP - A character is read from the rxBuff if available, if there
MrBedfordVan 0:b9164b348919 316 * are no characters to be read, the calling fiber sleeps
MrBedfordVan 0:b9164b348919 317 * until there is a character available.
MrBedfordVan 0:b9164b348919 318 *
MrBedfordVan 0:b9164b348919 319 * Defaults to SYNC_SLEEP.
MrBedfordVan 0:b9164b348919 320 *
MrBedfordVan 0:b9164b348919 321 * @return a character, MICROBIT_SERIAL_IN_USE if another fiber is using the serial instance for reception,
MrBedfordVan 0:b9164b348919 322 * MICROBIT_NO_RESOURCES if buffer allocation did not complete successfully, or MICROBIT_NO_DATA if
MrBedfordVan 0:b9164b348919 323 * the rx buffer is empty and the mode given is ASYNC.
MrBedfordVan 0:b9164b348919 324 */
MrBedfordVan 0:b9164b348919 325 int read(MicroBitSerialMode mode = MICROBIT_DEFAULT_SERIAL_MODE);
MrBedfordVan 0:b9164b348919 326
MrBedfordVan 0:b9164b348919 327 /**
MrBedfordVan 0:b9164b348919 328 * Reads multiple characters from the rxBuff and returns them as a ManagedString
MrBedfordVan 0:b9164b348919 329 *
MrBedfordVan 0:b9164b348919 330 * @param size the number of characters to read.
MrBedfordVan 0:b9164b348919 331 *
MrBedfordVan 0:b9164b348919 332 * @param mode the selected mode, one of: ASYNC, SYNC_SPINWAIT, SYNC_SLEEP. Each mode
MrBedfordVan 0:b9164b348919 333 * gives a different behaviour:
MrBedfordVan 0:b9164b348919 334 *
MrBedfordVan 0:b9164b348919 335 * ASYNC - If the desired number of characters are available, this will return
MrBedfordVan 0:b9164b348919 336 * a ManagedString with the expected size. Otherwise, it will read however
MrBedfordVan 0:b9164b348919 337 * many characters there are available.
MrBedfordVan 0:b9164b348919 338 *
MrBedfordVan 0:b9164b348919 339 * SYNC_SPINWAIT - If the desired number of characters are available, this will return
MrBedfordVan 0:b9164b348919 340 * a ManagedString with the expected size. Otherwise, this method will spin
MrBedfordVan 0:b9164b348919 341 * (lock up the processor) until the desired number of characters have been read.
MrBedfordVan 0:b9164b348919 342 *
MrBedfordVan 0:b9164b348919 343 * SYNC_SLEEP - If the desired number of characters are available, this will return
MrBedfordVan 0:b9164b348919 344 * a ManagedString with the expected size. Otherwise, the calling fiber sleeps
MrBedfordVan 0:b9164b348919 345 * until the desired number of characters have been read.
MrBedfordVan 0:b9164b348919 346 *
MrBedfordVan 0:b9164b348919 347 * Defaults to SYNC_SLEEP.
MrBedfordVan 0:b9164b348919 348 *
MrBedfordVan 0:b9164b348919 349 * @return A ManagedString, or an empty ManagedString if an error was encountered during the read.
MrBedfordVan 0:b9164b348919 350 */
MrBedfordVan 0:b9164b348919 351 ManagedString read(int size, MicroBitSerialMode mode = MICROBIT_DEFAULT_SERIAL_MODE);
MrBedfordVan 0:b9164b348919 352
MrBedfordVan 0:b9164b348919 353 /**
MrBedfordVan 0:b9164b348919 354 * Reads multiple characters from the rxBuff and fills a user buffer.
MrBedfordVan 0:b9164b348919 355 *
MrBedfordVan 0:b9164b348919 356 * @param buffer a pointer to a user allocated buffer.
MrBedfordVan 0:b9164b348919 357 *
MrBedfordVan 0:b9164b348919 358 * @param bufferLen the amount of data that can be safely stored
MrBedfordVan 0:b9164b348919 359 *
MrBedfordVan 0:b9164b348919 360 * @param mode the selected mode, one of: ASYNC, SYNC_SPINWAIT, SYNC_SLEEP. Each mode
MrBedfordVan 0:b9164b348919 361 * gives a different behaviour:
MrBedfordVan 0:b9164b348919 362 *
MrBedfordVan 0:b9164b348919 363 * ASYNC - If the desired number of characters are available, this will fill
MrBedfordVan 0:b9164b348919 364 * the given buffer. Otherwise, it will fill the buffer with however
MrBedfordVan 0:b9164b348919 365 * many characters there are available.
MrBedfordVan 0:b9164b348919 366 *
MrBedfordVan 0:b9164b348919 367 * SYNC_SPINWAIT - If the desired number of characters are available, this will fill
MrBedfordVan 0:b9164b348919 368 * the given buffer. Otherwise, this method will spin (lock up the processor)
MrBedfordVan 0:b9164b348919 369 * and fill the buffer until the desired number of characters have been read.
MrBedfordVan 0:b9164b348919 370 *
MrBedfordVan 0:b9164b348919 371 * SYNC_SLEEP - If the desired number of characters are available, this will fill
MrBedfordVan 0:b9164b348919 372 * the given buffer. Otherwise, the calling fiber sleeps
MrBedfordVan 0:b9164b348919 373 * until the desired number of characters have been read.
MrBedfordVan 0:b9164b348919 374 *
MrBedfordVan 0:b9164b348919 375 * Defaults to SYNC_SLEEP.
MrBedfordVan 0:b9164b348919 376 *
MrBedfordVan 0:b9164b348919 377 * @return the number of characters read, or MICROBIT_SERIAL_IN_USE if another fiber
MrBedfordVan 0:b9164b348919 378 * is using the instance for receiving.
MrBedfordVan 0:b9164b348919 379 */
MrBedfordVan 0:b9164b348919 380 int read(uint8_t *buffer, int bufferLen, MicroBitSerialMode mode = MICROBIT_DEFAULT_SERIAL_MODE);
MrBedfordVan 0:b9164b348919 381
MrBedfordVan 0:b9164b348919 382 /**
MrBedfordVan 0:b9164b348919 383 * Reads until one of the delimeters matches a character in the rxBuff
MrBedfordVan 0:b9164b348919 384 *
MrBedfordVan 0:b9164b348919 385 * @param delimeters a ManagedString containing a sequence of delimeter characters e.g. ManagedString("\r\n")
MrBedfordVan 0:b9164b348919 386 *
MrBedfordVan 0:b9164b348919 387 * @param mode the selected mode, one of: ASYNC, SYNC_SPINWAIT, SYNC_SLEEP. Each mode
MrBedfordVan 0:b9164b348919 388 * gives a different behaviour:
MrBedfordVan 0:b9164b348919 389 *
MrBedfordVan 0:b9164b348919 390 * ASYNC - If one of the delimeters matches a character already in the rxBuff
MrBedfordVan 0:b9164b348919 391 * this method will return a ManagedString up to the delimeter.
MrBedfordVan 0:b9164b348919 392 * Otherwise, it will return an Empty ManagedString.
MrBedfordVan 0:b9164b348919 393 *
MrBedfordVan 0:b9164b348919 394 * SYNC_SPINWAIT - If one of the delimeters matches a character already in the rxBuff
MrBedfordVan 0:b9164b348919 395 * this method will return a ManagedString up to the delimeter.
MrBedfordVan 0:b9164b348919 396 * Otherwise, this method will spin (lock up the processor) until a
MrBedfordVan 0:b9164b348919 397 * received character matches one of the delimeters.
MrBedfordVan 0:b9164b348919 398 *
MrBedfordVan 0:b9164b348919 399 * SYNC_SLEEP - If one of the delimeters matches a character already in the rxBuff
MrBedfordVan 0:b9164b348919 400 * this method will return a ManagedString up to the delimeter.
MrBedfordVan 0:b9164b348919 401 * Otherwise, the calling fiber sleeps until a character matching one
MrBedfordVan 0:b9164b348919 402 * of the delimeters is seen.
MrBedfordVan 0:b9164b348919 403 *
MrBedfordVan 0:b9164b348919 404 * Defaults to SYNC_SLEEP.
MrBedfordVan 0:b9164b348919 405 *
MrBedfordVan 0:b9164b348919 406 * @return A ManagedString containing the characters up to a delimeter, or an Empty ManagedString,
MrBedfordVan 0:b9164b348919 407 * if another fiber is currently using this instance for reception.
MrBedfordVan 0:b9164b348919 408 *
MrBedfordVan 0:b9164b348919 409 * @note delimeters are matched on a per byte basis.
MrBedfordVan 0:b9164b348919 410 */
MrBedfordVan 0:b9164b348919 411 ManagedString readUntil(ManagedString delimeters, MicroBitSerialMode mode = MICROBIT_DEFAULT_SERIAL_MODE);
MrBedfordVan 0:b9164b348919 412
MrBedfordVan 0:b9164b348919 413 /**
MrBedfordVan 0:b9164b348919 414 * A wrapper around the inherited method "baud" so we can trap the baud rate
MrBedfordVan 0:b9164b348919 415 * as it changes and restore it if redirect() is called.
MrBedfordVan 0:b9164b348919 416 *
MrBedfordVan 0:b9164b348919 417 * @param baudrate the new baudrate. See:
MrBedfordVan 0:b9164b348919 418 * - https://github.com/mbedmicro/mbed/blob/master/libraries/mbed/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/serial_api.c
MrBedfordVan 0:b9164b348919 419 * for permitted baud rates.
MrBedfordVan 0:b9164b348919 420 *
MrBedfordVan 0:b9164b348919 421 * @return MICROBIT_INVALID_PARAMETER if baud rate is less than 0, otherwise MICROBIT_OK.
MrBedfordVan 0:b9164b348919 422 *
MrBedfordVan 0:b9164b348919 423 * @note the underlying implementation chooses the first allowable rate at or above that requested.
MrBedfordVan 0:b9164b348919 424 */
MrBedfordVan 0:b9164b348919 425 void baud(int baudrate);
MrBedfordVan 0:b9164b348919 426
MrBedfordVan 0:b9164b348919 427 /**
MrBedfordVan 0:b9164b348919 428 * A way of dynamically configuring the serial instance to use pins other than USBTX and USBRX.
MrBedfordVan 0:b9164b348919 429 *
MrBedfordVan 0:b9164b348919 430 * @param tx the new transmission pin.
MrBedfordVan 0:b9164b348919 431 *
MrBedfordVan 0:b9164b348919 432 * @param rx the new reception pin.
MrBedfordVan 0:b9164b348919 433 *
MrBedfordVan 0:b9164b348919 434 * @return MICROBIT_SERIAL_IN_USE if another fiber is currently transmitting or receiving, otherwise MICROBIT_OK.
MrBedfordVan 0:b9164b348919 435 */
MrBedfordVan 0:b9164b348919 436 int redirect(PinName tx, PinName rx);
MrBedfordVan 0:b9164b348919 437
MrBedfordVan 0:b9164b348919 438 /**
MrBedfordVan 0:b9164b348919 439 * Configures an event to be fired after "len" characters.
MrBedfordVan 0:b9164b348919 440 *
MrBedfordVan 0:b9164b348919 441 * Will generate an event with the ID: MICROBIT_ID_SERIAL and the value MICROBIT_SERIAL_EVT_HEAD_MATCH.
MrBedfordVan 0:b9164b348919 442 *
MrBedfordVan 0:b9164b348919 443 * @param len the number of characters to wait before triggering the event.
MrBedfordVan 0:b9164b348919 444 *
MrBedfordVan 0:b9164b348919 445 * @param mode the selected mode, one of: ASYNC, SYNC_SPINWAIT, SYNC_SLEEP. Each mode
MrBedfordVan 0:b9164b348919 446 * gives a different behaviour:
MrBedfordVan 0:b9164b348919 447 *
MrBedfordVan 0:b9164b348919 448 * ASYNC - Will configure the event and return immediately.
MrBedfordVan 0:b9164b348919 449 *
MrBedfordVan 0:b9164b348919 450 * SYNC_SPINWAIT - will return MICROBIT_INVALID_PARAMETER
MrBedfordVan 0:b9164b348919 451 *
MrBedfordVan 0:b9164b348919 452 * SYNC_SLEEP - Will configure the event and block the current fiber until the
MrBedfordVan 0:b9164b348919 453 * event is received.
MrBedfordVan 0:b9164b348919 454 *
MrBedfordVan 0:b9164b348919 455 * @return MICROBIT_INVALID_PARAMETER if the mode given is SYNC_SPINWAIT, otherwise MICROBIT_OK.
MrBedfordVan 0:b9164b348919 456 */
MrBedfordVan 0:b9164b348919 457 int eventAfter(int len, MicroBitSerialMode mode = ASYNC);
MrBedfordVan 0:b9164b348919 458
MrBedfordVan 0:b9164b348919 459 /**
MrBedfordVan 0:b9164b348919 460 * Configures an event to be fired on a match with one of the delimeters.
MrBedfordVan 0:b9164b348919 461 *
MrBedfordVan 0:b9164b348919 462 * Will generate an event with the ID: MICROBIT_ID_SERIAL and the value MICROBIT_SERIAL_EVT_DELIM_MATCH.
MrBedfordVan 0:b9164b348919 463 *
MrBedfordVan 0:b9164b348919 464 * @param delimeters the characters to match received characters against e.g. ManagedString("\n")
MrBedfordVan 0:b9164b348919 465 *
MrBedfordVan 0:b9164b348919 466 * @param mode the selected mode, one of: ASYNC, SYNC_SPINWAIT, SYNC_SLEEP. Each mode
MrBedfordVan 0:b9164b348919 467 * gives a different behaviour:
MrBedfordVan 0:b9164b348919 468 *
MrBedfordVan 0:b9164b348919 469 * ASYNC - Will configure the event and return immediately.
MrBedfordVan 0:b9164b348919 470 *
MrBedfordVan 0:b9164b348919 471 * SYNC_SPINWAIT - will return MICROBIT_INVALID_PARAMETER
MrBedfordVan 0:b9164b348919 472 *
MrBedfordVan 0:b9164b348919 473 * SYNC_SLEEP - Will configure the event and block the current fiber until the
MrBedfordVan 0:b9164b348919 474 * event is received.
MrBedfordVan 0:b9164b348919 475 *
MrBedfordVan 0:b9164b348919 476 * @return MICROBIT_INVALID_PARAMETER if the mode given is SYNC_SPINWAIT, otherwise MICROBIT_OK.
MrBedfordVan 0:b9164b348919 477 *
MrBedfordVan 0:b9164b348919 478 * @note delimeters are matched on a per byte basis.
MrBedfordVan 0:b9164b348919 479 */
MrBedfordVan 0:b9164b348919 480 int eventOn(ManagedString delimeters, MicroBitSerialMode mode = ASYNC);
MrBedfordVan 0:b9164b348919 481
MrBedfordVan 0:b9164b348919 482 /**
MrBedfordVan 0:b9164b348919 483 * Determines whether there is any data waiting in our Rx buffer.
MrBedfordVan 0:b9164b348919 484 *
MrBedfordVan 0:b9164b348919 485 * @return 1 if we have space, 0 if we do not.
MrBedfordVan 0:b9164b348919 486 *
MrBedfordVan 0:b9164b348919 487 * @note We do not wrap the super's readable() method as we don't want to
MrBedfordVan 0:b9164b348919 488 * interfere with communities that use manual calls to serial.readable().
MrBedfordVan 0:b9164b348919 489 */
MrBedfordVan 0:b9164b348919 490 int isReadable();
MrBedfordVan 0:b9164b348919 491
MrBedfordVan 0:b9164b348919 492 /**
MrBedfordVan 0:b9164b348919 493 * Determines if we have space in our txBuff.
MrBedfordVan 0:b9164b348919 494 *
MrBedfordVan 0:b9164b348919 495 * @return 1 if we have space, 0 if we do not.
MrBedfordVan 0:b9164b348919 496 *
MrBedfordVan 0:b9164b348919 497 * @note We do not wrap the super's writeable() method as we don't want to
MrBedfordVan 0:b9164b348919 498 * interfere with communities that use manual calls to serial.writeable().
MrBedfordVan 0:b9164b348919 499 */
MrBedfordVan 0:b9164b348919 500 int isWriteable();
MrBedfordVan 0:b9164b348919 501
MrBedfordVan 0:b9164b348919 502 /**
MrBedfordVan 0:b9164b348919 503 * Reconfigures the size of our rxBuff
MrBedfordVan 0:b9164b348919 504 *
MrBedfordVan 0:b9164b348919 505 * @param size the new size for our rxBuff
MrBedfordVan 0:b9164b348919 506 *
MrBedfordVan 0:b9164b348919 507 * @return MICROBIT_SERIAL_IN_USE if another fiber is currently using this instance
MrBedfordVan 0:b9164b348919 508 * for reception, otherwise MICROBIT_OK.
MrBedfordVan 0:b9164b348919 509 */
MrBedfordVan 0:b9164b348919 510 int setRxBufferSize(uint8_t size);
MrBedfordVan 0:b9164b348919 511
MrBedfordVan 0:b9164b348919 512 /**
MrBedfordVan 0:b9164b348919 513 * Reconfigures the size of our txBuff
MrBedfordVan 0:b9164b348919 514 *
MrBedfordVan 0:b9164b348919 515 * @param size the new size for our txBuff
MrBedfordVan 0:b9164b348919 516 *
MrBedfordVan 0:b9164b348919 517 * @return MICROBIT_SERIAL_IN_USE if another fiber is currently using this instance
MrBedfordVan 0:b9164b348919 518 * for transmission, otherwise MICROBIT_OK.
MrBedfordVan 0:b9164b348919 519 */
MrBedfordVan 0:b9164b348919 520 int setTxBufferSize(uint8_t size);
MrBedfordVan 0:b9164b348919 521
MrBedfordVan 0:b9164b348919 522 /**
MrBedfordVan 0:b9164b348919 523 * The size of our rx buffer in bytes.
MrBedfordVan 0:b9164b348919 524 *
MrBedfordVan 0:b9164b348919 525 * @return the current size of rxBuff in bytes
MrBedfordVan 0:b9164b348919 526 */
MrBedfordVan 0:b9164b348919 527 int getRxBufferSize();
MrBedfordVan 0:b9164b348919 528
MrBedfordVan 0:b9164b348919 529 /**
MrBedfordVan 0:b9164b348919 530 * The size of our tx buffer in bytes.
MrBedfordVan 0:b9164b348919 531 *
MrBedfordVan 0:b9164b348919 532 * @return the current size of txBuff in bytes
MrBedfordVan 0:b9164b348919 533 */
MrBedfordVan 0:b9164b348919 534 int getTxBufferSize();
MrBedfordVan 0:b9164b348919 535
MrBedfordVan 0:b9164b348919 536 /**
MrBedfordVan 0:b9164b348919 537 * Sets the tail to match the head of our circular buffer for reception,
MrBedfordVan 0:b9164b348919 538 * effectively clearing the reception buffer.
MrBedfordVan 0:b9164b348919 539 *
MrBedfordVan 0:b9164b348919 540 * @return MICROBIT_SERIAL_IN_USE if another fiber is currently using this instance
MrBedfordVan 0:b9164b348919 541 * for reception, otherwise MICROBIT_OK.
MrBedfordVan 0:b9164b348919 542 */
MrBedfordVan 0:b9164b348919 543 int clearRxBuffer();
MrBedfordVan 0:b9164b348919 544
MrBedfordVan 0:b9164b348919 545 /**
MrBedfordVan 0:b9164b348919 546 * Sets the tail to match the head of our circular buffer for transmission,
MrBedfordVan 0:b9164b348919 547 * effectively clearing the transmission buffer.
MrBedfordVan 0:b9164b348919 548 *
MrBedfordVan 0:b9164b348919 549 * @return MICROBIT_SERIAL_IN_USE if another fiber is currently using this instance
MrBedfordVan 0:b9164b348919 550 * for transmission, otherwise MICROBIT_OK.
MrBedfordVan 0:b9164b348919 551 */
MrBedfordVan 0:b9164b348919 552 int clearTxBuffer();
MrBedfordVan 0:b9164b348919 553
MrBedfordVan 0:b9164b348919 554 /**
MrBedfordVan 0:b9164b348919 555 * The number of bytes currently stored in our rx buffer waiting to be digested,
MrBedfordVan 0:b9164b348919 556 * by the user.
MrBedfordVan 0:b9164b348919 557 *
MrBedfordVan 0:b9164b348919 558 * @return The currently buffered number of bytes in our rxBuff.
MrBedfordVan 0:b9164b348919 559 */
MrBedfordVan 0:b9164b348919 560 int rxBufferedSize();
MrBedfordVan 0:b9164b348919 561
MrBedfordVan 0:b9164b348919 562 /**
MrBedfordVan 0:b9164b348919 563 * The number of bytes currently stored in our tx buffer waiting to be transmitted
MrBedfordVan 0:b9164b348919 564 * by the hardware.
MrBedfordVan 0:b9164b348919 565 *
MrBedfordVan 0:b9164b348919 566 * @return The currently buffered number of bytes in our txBuff.
MrBedfordVan 0:b9164b348919 567 */
MrBedfordVan 0:b9164b348919 568 int txBufferedSize();
MrBedfordVan 0:b9164b348919 569
MrBedfordVan 0:b9164b348919 570 /**
MrBedfordVan 0:b9164b348919 571 * Determines if the serial bus is currently in use by another fiber for reception.
MrBedfordVan 0:b9164b348919 572 *
MrBedfordVan 0:b9164b348919 573 * @return The state of our mutex lock for reception.
MrBedfordVan 0:b9164b348919 574 *
MrBedfordVan 0:b9164b348919 575 * @note Only one fiber can call read at a time
MrBedfordVan 0:b9164b348919 576 */
MrBedfordVan 0:b9164b348919 577 int rxInUse();
MrBedfordVan 0:b9164b348919 578
MrBedfordVan 0:b9164b348919 579 /**
MrBedfordVan 0:b9164b348919 580 * Determines if the serial bus is currently in use by another fiber for transmission.
MrBedfordVan 0:b9164b348919 581 *
MrBedfordVan 0:b9164b348919 582 * @return The state of our mutex lock for transmition.
MrBedfordVan 0:b9164b348919 583 *
MrBedfordVan 0:b9164b348919 584 * @note Only one fiber can call send at a time
MrBedfordVan 0:b9164b348919 585 */
MrBedfordVan 0:b9164b348919 586 int txInUse();
MrBedfordVan 0:b9164b348919 587
MrBedfordVan 0:b9164b348919 588 /**
MrBedfordVan 0:b9164b348919 589 * Detaches a previously configured interrupt
MrBedfordVan 0:b9164b348919 590 *
MrBedfordVan 0:b9164b348919 591 * @param interruptType one of Serial::RxIrq or Serial::TxIrq
MrBedfordVan 0:b9164b348919 592 */
MrBedfordVan 0:b9164b348919 593 void detach(Serial::IrqType interuptType);
MrBedfordVan 0:b9164b348919 594
MrBedfordVan 0:b9164b348919 595 };
MrBedfordVan 0:b9164b348919 596
MrBedfordVan 0:b9164b348919 597 #endif