Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MAX44000 PWM_Tone_Library nexpaq_mdk
Fork of LED_Demo by
SerialBase.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #ifndef MBED_SERIALBASE_H 00017 #define MBED_SERIALBASE_H 00018 00019 #include "platform.h" 00020 00021 #if DEVICE_SERIAL 00022 00023 #include "Stream.h" 00024 #include "Callback.h" 00025 #include "serial_api.h" 00026 #include "toolchain.h" 00027 00028 #if DEVICE_SERIAL_ASYNCH 00029 #include "CThunk.h" 00030 #include "dma_api.h" 00031 #endif 00032 00033 namespace mbed { 00034 00035 /** A base class for serial port implementations 00036 * Can't be instantiated directly (use Serial or RawSerial) 00037 * 00038 * @Note Synchronization level: Set by subclass 00039 */ 00040 class SerialBase { 00041 00042 public: 00043 /** Set the baud rate of the serial port 00044 * 00045 * @param baudrate The baudrate of the serial port (default = 9600). 00046 */ 00047 void baud(int baudrate); 00048 00049 enum Parity { 00050 None = 0, 00051 Odd, 00052 Even, 00053 Forced1, 00054 Forced0 00055 }; 00056 00057 enum IrqType { 00058 RxIrq = 0, 00059 TxIrq, 00060 00061 IrqCnt 00062 }; 00063 00064 enum Flow { 00065 Disabled = 0, 00066 RTS, 00067 CTS, 00068 RTSCTS 00069 }; 00070 00071 /** Set the transmission format used by the serial port 00072 * 00073 * @param bits The number of bits in a word (5-8; default = 8) 00074 * @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None) 00075 * @param stop The number of stop bits (1 or 2; default = 1) 00076 */ 00077 void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1); 00078 00079 /** Determine if there is a character available to read 00080 * 00081 * @returns 00082 * 1 if there is a character available to read, 00083 * 0 otherwise 00084 */ 00085 int readable(); 00086 00087 /** Determine if there is space available to write a character 00088 * 00089 * @returns 00090 * 1 if there is space to write a character, 00091 * 0 otherwise 00092 */ 00093 int writeable(); 00094 00095 /** Attach a function to call whenever a serial interrupt is generated 00096 * 00097 * @param func A pointer to a void function, or 0 to set as none 00098 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) 00099 */ 00100 void attach(Callback<void()> func, IrqType type=RxIrq); 00101 00102 /** Attach a member function to call whenever a serial interrupt is generated 00103 * 00104 * @param obj pointer to the object to call the member function on 00105 * @param method pointer to the member function to be called 00106 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) 00107 * @deprecated 00108 * The attach function does not support cv-qualifiers. Replaced by 00109 * attach(callback(obj, method), type). 00110 */ 00111 template<typename T> 00112 MBED_DEPRECATED_SINCE("mbed-os-5.1", 00113 "The attach function does not support cv-qualifiers. Replaced by " 00114 "attach(callback(obj, method), type).") 00115 void attach(T *obj, void (T::*method)(), IrqType type=RxIrq) { 00116 attach(callback(obj, method), type); 00117 } 00118 00119 /** Attach a member function to call whenever a serial interrupt is generated 00120 * 00121 * @param obj pointer to the object to call the member function on 00122 * @param method pointer to the member function to be called 00123 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) 00124 * @deprecated 00125 * The attach function does not support cv-qualifiers. Replaced by 00126 * attach(callback(obj, method), type). 00127 */ 00128 template<typename T> 00129 MBED_DEPRECATED_SINCE("mbed-os-5.1", 00130 "The attach function does not support cv-qualifiers. Replaced by " 00131 "attach(callback(obj, method), type).") 00132 void attach(T *obj, void (*method)(T*), IrqType type=RxIrq) { 00133 attach(callback(obj, method), type); 00134 } 00135 00136 /** Generate a break condition on the serial line 00137 */ 00138 void send_break(); 00139 00140 protected: 00141 00142 /** Acquire exclusive access to this serial port 00143 */ 00144 virtual void lock(void); 00145 00146 /** Release exclusive access to this serial port 00147 */ 00148 virtual void unlock(void); 00149 00150 public: 00151 00152 #if DEVICE_SERIAL_FC 00153 /** Set the flow control type on the serial port 00154 * 00155 * @param type the flow control type (Disabled, RTS, CTS, RTSCTS) 00156 * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS) 00157 * @param flow2 the second flow control pin (CTS for RTSCTS) 00158 */ 00159 void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC); 00160 #endif 00161 00162 static void _irq_handler(uint32_t id, SerialIrq irq_type); 00163 00164 #if DEVICE_SERIAL_ASYNCH 00165 00166 /** Begin asynchronous write using 8bit buffer. The completition invokes registered TX event callback 00167 * 00168 * @param buffer The buffer where received data will be stored 00169 * @param length The buffer length in bytes 00170 * @param callback The event callback function 00171 * @param event The logical OR of TX events 00172 */ 00173 int write(const uint8_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE); 00174 00175 /** Begin asynchronous write using 16bit buffer. The completition invokes registered TX event callback 00176 * 00177 * @param buffer The buffer where received data will be stored 00178 * @param length The buffer length in bytes 00179 * @param callback The event callback function 00180 * @param event The logical OR of TX events 00181 */ 00182 int write(const uint16_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE); 00183 00184 /** Abort the on-going write transfer 00185 */ 00186 void abort_write(); 00187 00188 /** Begin asynchronous reading using 8bit buffer. The completition invokes registred RX event callback. 00189 * 00190 * @param buffer The buffer where received data will be stored 00191 * @param length The buffer length in bytes 00192 * @param callback The event callback function 00193 * @param event The logical OR of RX events 00194 * @param char_match The matching character 00195 */ 00196 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); 00197 00198 /** Begin asynchronous reading using 16bit buffer. The completition invokes registred RX event callback. 00199 * 00200 * @param buffer The buffer where received data will be stored 00201 * @param length The buffer length in bytes 00202 * @param callback The event callback function 00203 * @param event The logical OR of RX events 00204 * @param char_match The matching character 00205 */ 00206 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); 00207 00208 /** Abort the on-going read transfer 00209 */ 00210 void abort_read(); 00211 00212 /** Configure DMA usage suggestion for non-blocking TX transfers 00213 * 00214 * @param usage The usage DMA hint for peripheral 00215 * @return Zero if the usage was set, -1 if a transaction is on-going 00216 */ 00217 int set_dma_usage_tx(DMAUsage usage); 00218 00219 /** Configure DMA usage suggestion for non-blocking RX transfers 00220 * 00221 * @param usage The usage DMA hint for peripheral 00222 * @return Zero if the usage was set, -1 if a transaction is on-going 00223 */ 00224 int set_dma_usage_rx(DMAUsage usage); 00225 00226 protected: 00227 void start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event, unsigned char char_match); 00228 void start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event); 00229 void interrupt_handler_asynch(void); 00230 #endif 00231 00232 protected: 00233 SerialBase(PinName tx, PinName rx); 00234 virtual ~SerialBase() { 00235 } 00236 00237 int _base_getc(); 00238 int _base_putc(int c); 00239 00240 #if DEVICE_SERIAL_ASYNCH 00241 CThunk<SerialBase> _thunk_irq; 00242 event_callback_t _tx_callback; 00243 event_callback_t _rx_callback; 00244 DMAUsage _tx_usage; 00245 DMAUsage _rx_usage; 00246 #endif 00247 00248 serial_t _serial; 00249 Callback<void()> _irq[IrqCnt]; 00250 int _baud; 00251 00252 }; 00253 00254 } // namespace mbed 00255 00256 #endif 00257 00258 #endif
Generated on Tue Jul 12 2022 12:28:48 by
