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 /* mbed Microcontroller Library
MrBedfordVan 0:b9164b348919 2 * Copyright (c) 2006-2013 ARM Limited
MrBedfordVan 0:b9164b348919 3 *
MrBedfordVan 0:b9164b348919 4 * Licensed under the Apache License, Version 2.0 (the "License");
MrBedfordVan 0:b9164b348919 5 * you may not use this file except in compliance with the License.
MrBedfordVan 0:b9164b348919 6 * You may obtain a copy of the License at
MrBedfordVan 0:b9164b348919 7 *
MrBedfordVan 0:b9164b348919 8 * http://www.apache.org/licenses/LICENSE-2.0
MrBedfordVan 0:b9164b348919 9 *
MrBedfordVan 0:b9164b348919 10 * Unless required by applicable law or agreed to in writing, software
MrBedfordVan 0:b9164b348919 11 * distributed under the License is distributed on an "AS IS" BASIS,
MrBedfordVan 0:b9164b348919 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
MrBedfordVan 0:b9164b348919 13 * See the License for the specific language governing permissions and
MrBedfordVan 0:b9164b348919 14 * limitations under the License.
MrBedfordVan 0:b9164b348919 15 */
MrBedfordVan 0:b9164b348919 16 #ifndef MBED_SERIALBASE_H
MrBedfordVan 0:b9164b348919 17 #define MBED_SERIALBASE_H
MrBedfordVan 0:b9164b348919 18
MrBedfordVan 0:b9164b348919 19 #include "platform.h"
MrBedfordVan 0:b9164b348919 20
MrBedfordVan 0:b9164b348919 21 #if DEVICE_SERIAL
MrBedfordVan 0:b9164b348919 22
MrBedfordVan 0:b9164b348919 23 #include "Stream.h"
MrBedfordVan 0:b9164b348919 24 #include "FunctionPointer.h"
MrBedfordVan 0:b9164b348919 25 #include "serial_api.h"
MrBedfordVan 0:b9164b348919 26
MrBedfordVan 0:b9164b348919 27 #if DEVICE_SERIAL_ASYNCH
MrBedfordVan 0:b9164b348919 28 #include "CThunk.h"
MrBedfordVan 0:b9164b348919 29 #include "dma_api.h"
MrBedfordVan 0:b9164b348919 30 #endif
MrBedfordVan 0:b9164b348919 31
MrBedfordVan 0:b9164b348919 32 namespace mbed {
MrBedfordVan 0:b9164b348919 33
MrBedfordVan 0:b9164b348919 34 /** A base class for serial port implementations
MrBedfordVan 0:b9164b348919 35 * Can't be instantiated directly (use Serial or RawSerial)
MrBedfordVan 0:b9164b348919 36 */
MrBedfordVan 0:b9164b348919 37 class SerialBase {
MrBedfordVan 0:b9164b348919 38
MrBedfordVan 0:b9164b348919 39 public:
MrBedfordVan 0:b9164b348919 40 /** Set the baud rate of the serial port
MrBedfordVan 0:b9164b348919 41 *
MrBedfordVan 0:b9164b348919 42 * @param baudrate The baudrate of the serial port (default = 9600).
MrBedfordVan 0:b9164b348919 43 */
MrBedfordVan 0:b9164b348919 44 void baud(int baudrate);
MrBedfordVan 0:b9164b348919 45
MrBedfordVan 0:b9164b348919 46 enum Parity {
MrBedfordVan 0:b9164b348919 47 None = 0,
MrBedfordVan 0:b9164b348919 48 Odd,
MrBedfordVan 0:b9164b348919 49 Even,
MrBedfordVan 0:b9164b348919 50 Forced1,
MrBedfordVan 0:b9164b348919 51 Forced0
MrBedfordVan 0:b9164b348919 52 };
MrBedfordVan 0:b9164b348919 53
MrBedfordVan 0:b9164b348919 54 enum IrqType {
MrBedfordVan 0:b9164b348919 55 RxIrq = 0,
MrBedfordVan 0:b9164b348919 56 TxIrq
MrBedfordVan 0:b9164b348919 57 };
MrBedfordVan 0:b9164b348919 58
MrBedfordVan 0:b9164b348919 59 enum Flow {
MrBedfordVan 0:b9164b348919 60 Disabled = 0,
MrBedfordVan 0:b9164b348919 61 RTS,
MrBedfordVan 0:b9164b348919 62 CTS,
MrBedfordVan 0:b9164b348919 63 RTSCTS
MrBedfordVan 0:b9164b348919 64 };
MrBedfordVan 0:b9164b348919 65
MrBedfordVan 0:b9164b348919 66 /** Set the transmission format used by the serial port
MrBedfordVan 0:b9164b348919 67 *
MrBedfordVan 0:b9164b348919 68 * @param bits The number of bits in a word (5-8; default = 8)
MrBedfordVan 0:b9164b348919 69 * @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None)
MrBedfordVan 0:b9164b348919 70 * @param stop The number of stop bits (1 or 2; default = 1)
MrBedfordVan 0:b9164b348919 71 */
MrBedfordVan 0:b9164b348919 72 void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1);
MrBedfordVan 0:b9164b348919 73
MrBedfordVan 0:b9164b348919 74 /** Determine if there is a character available to read
MrBedfordVan 0:b9164b348919 75 *
MrBedfordVan 0:b9164b348919 76 * @returns
MrBedfordVan 0:b9164b348919 77 * 1 if there is a character available to read,
MrBedfordVan 0:b9164b348919 78 * 0 otherwise
MrBedfordVan 0:b9164b348919 79 */
MrBedfordVan 0:b9164b348919 80 int readable();
MrBedfordVan 0:b9164b348919 81
MrBedfordVan 0:b9164b348919 82 /** Determine if there is space available to write a character
MrBedfordVan 0:b9164b348919 83 *
MrBedfordVan 0:b9164b348919 84 * @returns
MrBedfordVan 0:b9164b348919 85 * 1 if there is space to write a character,
MrBedfordVan 0:b9164b348919 86 * 0 otherwise
MrBedfordVan 0:b9164b348919 87 */
MrBedfordVan 0:b9164b348919 88 int writeable();
MrBedfordVan 0:b9164b348919 89
MrBedfordVan 0:b9164b348919 90 /** Attach a function to call whenever a serial interrupt is generated
MrBedfordVan 0:b9164b348919 91 *
MrBedfordVan 0:b9164b348919 92 * @param fptr A pointer to a void function, or 0 to set as none
MrBedfordVan 0:b9164b348919 93 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
MrBedfordVan 0:b9164b348919 94 */
MrBedfordVan 0:b9164b348919 95 void attach(void (*fptr)(void), IrqType type=RxIrq);
MrBedfordVan 0:b9164b348919 96
MrBedfordVan 0:b9164b348919 97 /** Attach a member function to call whenever a serial interrupt is generated
MrBedfordVan 0:b9164b348919 98 *
MrBedfordVan 0:b9164b348919 99 * @param tptr pointer to the object to call the member function on
MrBedfordVan 0:b9164b348919 100 * @param mptr pointer to the member function to be called
MrBedfordVan 0:b9164b348919 101 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
MrBedfordVan 0:b9164b348919 102 */
MrBedfordVan 0:b9164b348919 103 template<typename T>
MrBedfordVan 0:b9164b348919 104 void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
MrBedfordVan 0:b9164b348919 105 if((mptr != NULL) && (tptr != NULL)) {
MrBedfordVan 0:b9164b348919 106 _irq[type].attach(tptr, mptr);
MrBedfordVan 0:b9164b348919 107 serial_irq_set(&_serial, (SerialIrq)type, 1);
MrBedfordVan 0:b9164b348919 108 } else {
MrBedfordVan 0:b9164b348919 109 serial_irq_set(&_serial, (SerialIrq)type, 0);
MrBedfordVan 0:b9164b348919 110 }
MrBedfordVan 0:b9164b348919 111 }
MrBedfordVan 0:b9164b348919 112
MrBedfordVan 0:b9164b348919 113 /** Generate a break condition on the serial line
MrBedfordVan 0:b9164b348919 114 */
MrBedfordVan 0:b9164b348919 115 void send_break();
MrBedfordVan 0:b9164b348919 116
MrBedfordVan 0:b9164b348919 117 #if DEVICE_SERIAL_FC
MrBedfordVan 0:b9164b348919 118 /** Set the flow control type on the serial port
MrBedfordVan 0:b9164b348919 119 *
MrBedfordVan 0:b9164b348919 120 * @param type the flow control type (Disabled, RTS, CTS, RTSCTS)
MrBedfordVan 0:b9164b348919 121 * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
MrBedfordVan 0:b9164b348919 122 * @param flow2 the second flow control pin (CTS for RTSCTS)
MrBedfordVan 0:b9164b348919 123 */
MrBedfordVan 0:b9164b348919 124 void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC);
MrBedfordVan 0:b9164b348919 125 #endif
MrBedfordVan 0:b9164b348919 126
MrBedfordVan 0:b9164b348919 127 static void _irq_handler(uint32_t id, SerialIrq irq_type);
MrBedfordVan 0:b9164b348919 128
MrBedfordVan 0:b9164b348919 129 #if DEVICE_SERIAL_ASYNCH
MrBedfordVan 0:b9164b348919 130
MrBedfordVan 0:b9164b348919 131 /** Begin asynchronous write using 8bit buffer. The completition invokes registered TX event callback
MrBedfordVan 0:b9164b348919 132 *
MrBedfordVan 0:b9164b348919 133 * @param buffer The buffer where received data will be stored
MrBedfordVan 0:b9164b348919 134 * @param length The buffer length in bytes
MrBedfordVan 0:b9164b348919 135 * @param callback The event callback function
MrBedfordVan 0:b9164b348919 136 * @param event The logical OR of TX events
MrBedfordVan 0:b9164b348919 137 */
MrBedfordVan 0:b9164b348919 138 int write(const uint8_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE);
MrBedfordVan 0:b9164b348919 139
MrBedfordVan 0:b9164b348919 140 /** Begin asynchronous write using 16bit buffer. The completition invokes registered TX event callback
MrBedfordVan 0:b9164b348919 141 *
MrBedfordVan 0:b9164b348919 142 * @param buffer The buffer where received data will be stored
MrBedfordVan 0:b9164b348919 143 * @param length The buffer length in bytes
MrBedfordVan 0:b9164b348919 144 * @param callback The event callback function
MrBedfordVan 0:b9164b348919 145 * @param event The logical OR of TX events
MrBedfordVan 0:b9164b348919 146 */
MrBedfordVan 0:b9164b348919 147 int write(const uint16_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE);
MrBedfordVan 0:b9164b348919 148
MrBedfordVan 0:b9164b348919 149 /** Abort the on-going write transfer
MrBedfordVan 0:b9164b348919 150 */
MrBedfordVan 0:b9164b348919 151 void abort_write();
MrBedfordVan 0:b9164b348919 152
MrBedfordVan 0:b9164b348919 153 /** Begin asynchronous reading using 8bit buffer. The completition invokes registred RX event callback.
MrBedfordVan 0:b9164b348919 154 *
MrBedfordVan 0:b9164b348919 155 * @param buffer The buffer where received data will be stored
MrBedfordVan 0:b9164b348919 156 * @param length The buffer length in bytes
MrBedfordVan 0:b9164b348919 157 * @param callback The event callback function
MrBedfordVan 0:b9164b348919 158 * @param event The logical OR of RX events
MrBedfordVan 0:b9164b348919 159 * @param char_match The matching character
MrBedfordVan 0:b9164b348919 160 */
MrBedfordVan 0:b9164b348919 161 int read(uint8_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_RX_COMPLETE, unsigned char char_match = SERIAL_RESERVED_CHAR_MATCH);
MrBedfordVan 0:b9164b348919 162
MrBedfordVan 0:b9164b348919 163 /** Begin asynchronous reading using 16bit buffer. The completition invokes registred RX event callback.
MrBedfordVan 0:b9164b348919 164 *
MrBedfordVan 0:b9164b348919 165 * @param buffer The buffer where received data will be stored
MrBedfordVan 0:b9164b348919 166 * @param length The buffer length in bytes
MrBedfordVan 0:b9164b348919 167 * @param callback The event callback function
MrBedfordVan 0:b9164b348919 168 * @param event The logical OR of RX events
MrBedfordVan 0:b9164b348919 169 * @param char_match The matching character
MrBedfordVan 0:b9164b348919 170 */
MrBedfordVan 0:b9164b348919 171 int read(uint16_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_RX_COMPLETE, unsigned char char_match = SERIAL_RESERVED_CHAR_MATCH);
MrBedfordVan 0:b9164b348919 172
MrBedfordVan 0:b9164b348919 173 /** Abort the on-going read transfer
MrBedfordVan 0:b9164b348919 174 */
MrBedfordVan 0:b9164b348919 175 void abort_read();
MrBedfordVan 0:b9164b348919 176
MrBedfordVan 0:b9164b348919 177 /** Configure DMA usage suggestion for non-blocking TX transfers
MrBedfordVan 0:b9164b348919 178 *
MrBedfordVan 0:b9164b348919 179 * @param usage The usage DMA hint for peripheral
MrBedfordVan 0:b9164b348919 180 * @return Zero if the usage was set, -1 if a transaction is on-going
MrBedfordVan 0:b9164b348919 181 */
MrBedfordVan 0:b9164b348919 182 int set_dma_usage_tx(DMAUsage usage);
MrBedfordVan 0:b9164b348919 183
MrBedfordVan 0:b9164b348919 184 /** Configure DMA usage suggestion for non-blocking RX transfers
MrBedfordVan 0:b9164b348919 185 *
MrBedfordVan 0:b9164b348919 186 * @param usage The usage DMA hint for peripheral
MrBedfordVan 0:b9164b348919 187 * @return Zero if the usage was set, -1 if a transaction is on-going
MrBedfordVan 0:b9164b348919 188 */
MrBedfordVan 0:b9164b348919 189 int set_dma_usage_rx(DMAUsage usage);
MrBedfordVan 0:b9164b348919 190
MrBedfordVan 0:b9164b348919 191 protected:
MrBedfordVan 0:b9164b348919 192 void start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event, unsigned char char_match);
MrBedfordVan 0:b9164b348919 193 void start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event);
MrBedfordVan 0:b9164b348919 194 void interrupt_handler_asynch(void);
MrBedfordVan 0:b9164b348919 195 #endif
MrBedfordVan 0:b9164b348919 196
MrBedfordVan 0:b9164b348919 197 protected:
MrBedfordVan 0:b9164b348919 198 SerialBase(PinName tx, PinName rx);
MrBedfordVan 0:b9164b348919 199 virtual ~SerialBase() {
MrBedfordVan 0:b9164b348919 200 }
MrBedfordVan 0:b9164b348919 201
MrBedfordVan 0:b9164b348919 202 int _base_getc();
MrBedfordVan 0:b9164b348919 203 int _base_putc(int c);
MrBedfordVan 0:b9164b348919 204
MrBedfordVan 0:b9164b348919 205 #if DEVICE_SERIAL_ASYNCH
MrBedfordVan 0:b9164b348919 206 CThunk<SerialBase> _thunk_irq;
MrBedfordVan 0:b9164b348919 207 event_callback_t _tx_callback;
MrBedfordVan 0:b9164b348919 208 event_callback_t _rx_callback;
MrBedfordVan 0:b9164b348919 209 DMAUsage _tx_usage;
MrBedfordVan 0:b9164b348919 210 DMAUsage _rx_usage;
MrBedfordVan 0:b9164b348919 211 #endif
MrBedfordVan 0:b9164b348919 212
MrBedfordVan 0:b9164b348919 213 serial_t _serial;
MrBedfordVan 0:b9164b348919 214 FunctionPointer _irq[2];
MrBedfordVan 0:b9164b348919 215 int _baud;
MrBedfordVan 0:b9164b348919 216
MrBedfordVan 0:b9164b348919 217 };
MrBedfordVan 0:b9164b348919 218
MrBedfordVan 0:b9164b348919 219 } // namespace mbed
MrBedfordVan 0:b9164b348919 220
MrBedfordVan 0:b9164b348919 221 #endif
MrBedfordVan 0:b9164b348919 222
MrBedfordVan 0:b9164b348919 223 #endif