++

Fork of mbed-stm32l0/l1-src by lzbp li

Committer:
lzbpli
Date:
Thu Sep 22 02:11:40 2016 +0000
Revision:
638:e94dc43c7733
Parent:
564:24a7119bd73a
jianjianjian

Who changed what in which revision?

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