Marco Zecchini
/
Example_RTOS
Rtos API example
Embed:
(wiki syntax)
Show/hide line numbers
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/platform.h" 00020 00021 #if defined (DEVICE_SERIAL) || defined(DOXYGEN_ONLY) 00022 00023 #include "Callback.h" 00024 #include "serial_api.h" 00025 #include "mbed_toolchain.h" 00026 #include "platform/NonCopyable.h" 00027 00028 #if DEVICE_SERIAL_ASYNCH 00029 #include "CThunk.h" 00030 #include "dma_api.h" 00031 #endif 00032 00033 namespace mbed { 00034 /** \addtogroup drivers */ 00035 00036 /** A base class for serial port implementations 00037 * Can't be instantiated directly (use Serial or RawSerial) 00038 * 00039 * @note Synchronization level: Set by subclass 00040 * @ingroup drivers 00041 */ 00042 class SerialBase : private NonCopyable<SerialBase> { 00043 00044 public: 00045 /** Set the baud rate of the serial port 00046 * 00047 * @param baudrate The baudrate of the serial port (default = 9600). 00048 */ 00049 void baud(int baudrate); 00050 00051 enum Parity { 00052 None = 0, 00053 Odd, 00054 Even, 00055 Forced1, 00056 Forced0 00057 }; 00058 00059 enum IrqType { 00060 RxIrq = 0, 00061 TxIrq, 00062 00063 IrqCnt 00064 }; 00065 00066 enum Flow { 00067 Disabled = 0, 00068 RTS, 00069 CTS, 00070 RTSCTS 00071 }; 00072 00073 /** Set the transmission format used by the serial port 00074 * 00075 * @param bits The number of bits in a word (5-8; default = 8) 00076 * @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None) 00077 * @param stop_bits The number of stop bits (1 or 2; default = 1) 00078 */ 00079 void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1); 00080 00081 /** Determine if there is a character available to read 00082 * 00083 * @returns 00084 * 1 if there is a character available to read, 00085 * 0 otherwise 00086 */ 00087 int readable(); 00088 00089 /** Determine if there is space available to write a character 00090 * 00091 * @returns 00092 * 1 if there is space to write a character, 00093 * 0 otherwise 00094 */ 00095 int writeable(); 00096 00097 /** Attach a function to call whenever a serial interrupt is generated 00098 * 00099 * @param func A pointer to a void function, or 0 to set as none 00100 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) 00101 */ 00102 void attach(Callback<void()> func, IrqType type=RxIrq); 00103 00104 /** Attach a member function to call whenever a serial interrupt is generated 00105 * 00106 * @param obj pointer to the object to call the member function on 00107 * @param method pointer to the member function to be called 00108 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) 00109 * @deprecated 00110 * The attach function does not support cv-qualifiers. Replaced by 00111 * attach(callback(obj, method), type). 00112 */ 00113 template<typename T> 00114 MBED_DEPRECATED_SINCE("mbed-os-5.1", 00115 "The attach function does not support cv-qualifiers. Replaced by " 00116 "attach(callback(obj, method), type).") 00117 void attach(T *obj, void (T::*method)(), IrqType type=RxIrq) { 00118 attach(callback(obj, method), type); 00119 } 00120 00121 /** Attach a member function to call whenever a serial interrupt is generated 00122 * 00123 * @param obj pointer to the object to call the member function on 00124 * @param method pointer to the member function to be called 00125 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) 00126 * @deprecated 00127 * The attach function does not support cv-qualifiers. Replaced by 00128 * attach(callback(obj, method), type). 00129 */ 00130 template<typename T> 00131 MBED_DEPRECATED_SINCE("mbed-os-5.1", 00132 "The attach function does not support cv-qualifiers. Replaced by " 00133 "attach(callback(obj, method), type).") 00134 void attach(T *obj, void (*method)(T*), IrqType type=RxIrq) { 00135 attach(callback(obj, method), type); 00136 } 00137 00138 /** Generate a break condition on the serial line 00139 */ 00140 void send_break(); 00141 00142 protected: 00143 00144 /** Acquire exclusive access to this serial port 00145 */ 00146 virtual void lock(void); 00147 00148 /** Release exclusive access to this serial port 00149 */ 00150 virtual void unlock(void); 00151 00152 public: 00153 00154 #if DEVICE_SERIAL_FC 00155 /** Set the flow control type on the serial port 00156 * 00157 * @param type the flow control type (Disabled, RTS, CTS, RTSCTS) 00158 * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS) 00159 * @param flow2 the second flow control pin (CTS for RTSCTS) 00160 */ 00161 void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC); 00162 #endif 00163 00164 static void _irq_handler(uint32_t id, SerialIrq irq_type); 00165 00166 #if DEVICE_SERIAL_ASYNCH 00167 00168 /** Begin asynchronous write using 8bit buffer. The completition invokes registered TX event callback 00169 * 00170 * This function locks the deep sleep until any event has occured 00171 * 00172 * @param buffer The buffer where received data will be stored 00173 * @param length The buffer length in bytes 00174 * @param callback The event callback function 00175 * @param event The logical OR of TX events 00176 */ 00177 int write(const uint8_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE); 00178 00179 /** Begin asynchronous write using 16bit buffer. The completition invokes registered TX event callback 00180 * 00181 * This function locks the deep sleep until any event has occured 00182 * 00183 * @param buffer The buffer where received data will be stored 00184 * @param length The buffer length in bytes 00185 * @param callback The event callback function 00186 * @param event The logical OR of TX events 00187 */ 00188 int write(const uint16_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE); 00189 00190 /** Abort the on-going write transfer 00191 */ 00192 void abort_write(); 00193 00194 /** Begin asynchronous reading using 8bit buffer. The completition invokes registred RX event callback. 00195 * 00196 * This function locks the deep sleep until any event has occured 00197 * 00198 * @param buffer The buffer where received data will be stored 00199 * @param length The buffer length in bytes 00200 * @param callback The event callback function 00201 * @param event The logical OR of RX events 00202 * @param char_match The matching character 00203 */ 00204 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); 00205 00206 /** Begin asynchronous reading using 16bit buffer. The completition invokes registred RX event callback. 00207 * 00208 * This function locks the deep sleep until any event has occured 00209 * 00210 * @param buffer The buffer where received data will be stored 00211 * @param length The buffer length in bytes 00212 * @param callback The event callback function 00213 * @param event The logical OR of RX events 00214 * @param char_match The matching character 00215 */ 00216 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); 00217 00218 /** Abort the on-going read transfer 00219 */ 00220 void abort_read(); 00221 00222 /** Configure DMA usage suggestion for non-blocking TX transfers 00223 * 00224 * @param usage The usage DMA hint for peripheral 00225 * @return Zero if the usage was set, -1 if a transaction is on-going 00226 */ 00227 int set_dma_usage_tx(DMAUsage usage); 00228 00229 /** Configure DMA usage suggestion for non-blocking RX transfers 00230 * 00231 * @param usage The usage DMA hint for peripheral 00232 * @return Zero if the usage was set, -1 if a transaction is on-going 00233 */ 00234 int set_dma_usage_rx(DMAUsage usage); 00235 00236 protected: 00237 void start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event, unsigned char char_match); 00238 void start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event); 00239 void interrupt_handler_asynch(void); 00240 #endif 00241 00242 protected: 00243 SerialBase(PinName tx, PinName rx, int baud); 00244 virtual ~SerialBase(); 00245 00246 int _base_getc(); 00247 int _base_putc(int c); 00248 00249 #if DEVICE_SERIAL_ASYNCH 00250 CThunk<SerialBase> _thunk_irq; 00251 DMAUsage _tx_usage; 00252 DMAUsage _rx_usage; 00253 event_callback_t _tx_callback; 00254 event_callback_t _rx_callback; 00255 #endif 00256 00257 serial_t _serial; 00258 Callback<void()> _irq[IrqCnt]; 00259 int _baud; 00260 00261 }; 00262 00263 } // namespace mbed 00264 00265 #endif 00266 00267 #endif
Generated on Sun Jul 17 2022 08:25:30 by 1.7.2