temp

Dependencies:   mbed SDFileSystem MS5607 ADXL345_I2C FATFileSystem

Committer:
IKobayashi
Date:
Mon Mar 16 23:37:42 2020 +0900
Revision:
0:c88c3b616c00
copy

Who changed what in which revision?

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