test

Dependencies:   mbed Watchdog

Dependents:   STM32-MC_node

Committer:
ommpy
Date:
Mon Jul 06 17:18:59 2020 +0530
Revision:
0:d383e2dee0f7
first commit

Who changed what in which revision?

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