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.
Fork of mbed 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 "FunctionPointer.h" 00025 #include "serial_api.h" 00026 00027 #if DEVICE_SERIAL_ASYNCH 00028 #include "CThunk.h" 00029 #include "dma_api.h" 00030 #endif 00031 00032 namespace mbed { 00033 00034 /** A base class for serial port implementations 00035 * Can't be instantiated directly (use Serial or RawSerial) 00036 */ 00037 class SerialBase { 00038 00039 public: 00040 /** Set the baud rate of the serial port 00041 * 00042 * @param baudrate The baudrate of the serial port (default = 9600). 00043 */ 00044 void baud(int baudrate); 00045 00046 enum Parity { 00047 None = 0, 00048 Odd, 00049 Even, 00050 Forced1, 00051 Forced0 00052 }; 00053 00054 enum IrqType { 00055 RxIrq = 0, 00056 TxIrq 00057 }; 00058 00059 enum Flow { 00060 Disabled = 0, 00061 RTS, 00062 CTS, 00063 RTSCTS 00064 }; 00065 00066 /** Set the transmission format used by the serial port 00067 * 00068 * @param bits The number of bits in a word (5-8; default = 8) 00069 * @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None) 00070 * @param stop The number of stop bits (1 or 2; default = 1) 00071 */ 00072 void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1); 00073 00074 /** Determine if there is a character available to read 00075 * 00076 * @returns 00077 * 1 if there is a character available to read, 00078 * 0 otherwise 00079 */ 00080 int readable(); 00081 00082 /** Determine if there is space available to write a character 00083 * 00084 * @returns 00085 * 1 if there is space to write a character, 00086 * 0 otherwise 00087 */ 00088 int writeable(); 00089 00090 /** Attach a function to call whenever a serial interrupt is generated 00091 * 00092 * @param fptr A pointer to a void function, or 0 to set as none 00093 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) 00094 */ 00095 void attach(void (*fptr)(void), IrqType type=RxIrq); 00096 00097 /** Attach a member function to call whenever a serial interrupt is generated 00098 * 00099 * @param tptr pointer to the object to call the member function on 00100 * @param mptr pointer to the member function to be called 00101 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) 00102 */ 00103 template<typename T> 00104 void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) { 00105 if((mptr != NULL) && (tptr != NULL)) { 00106 _irq[type].attach(tptr, mptr); 00107 serial_irq_set(&_serial, (SerialIrq)type, 1); 00108 } 00109 } 00110 00111 /** Generate a break condition on the serial line 00112 */ 00113 void send_break(); 00114 00115 #if DEVICE_SERIAL_FC 00116 /** Set the flow control type on the serial port 00117 * 00118 * @param type the flow control type (Disabled, RTS, CTS, RTSCTS) 00119 * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS) 00120 * @param flow2 the second flow control pin (CTS for RTSCTS) 00121 */ 00122 void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC); 00123 #endif 00124 00125 static void _irq_handler(uint32_t id, SerialIrq irq_type); 00126 00127 #if DEVICE_SERIAL_ASYNCH 00128 00129 /** Begin asynchronous write using 8bit buffer. The completition invokes registered TX event callback 00130 * 00131 * @param buffer The buffer where received data will be stored 00132 * @param length The buffer length 00133 * @param callback The event callback function 00134 * @param event The logical OR of TX events 00135 */ 00136 int write(uint8_t *buffer, int length, const event_callback_t & callback, int event = SERIAL_EVENT_TX_COMPLETE); 00137 00138 /** Begin asynchronous write using 16bit buffer. The completition invokes registered TX event callback 00139 * 00140 * @param buffer The buffer where received data will be stored 00141 * @param length The buffer length 00142 * @param callback The event callback function 00143 * @param event The logical OR of TX events 00144 */ 00145 int write(uint16_t *buffer, int length, const event_callback_t & callback, int event = SERIAL_EVENT_TX_COMPLETE); 00146 00147 /** Abort the on-going write transfer 00148 */ 00149 void abort_write(); 00150 00151 /** Begin asynchronous reading using 8bit buffer. The completition invokes registred RX event callback. 00152 * 00153 * @param buffer The buffer where received data will be stored 00154 * @param length The buffer length 00155 * @param callback The event callback function 00156 * @param event The logical OR of RX events 00157 * @param char_match The matching character 00158 */ 00159 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); 00160 00161 /** Begin asynchronous reading using 16bit buffer. The completition invokes registred RX event callback. 00162 * 00163 * @param buffer The buffer where received data will be stored 00164 * @param length The buffer length 00165 * @param callback The event callback function 00166 * @param event The logical OR of RX events 00167 * @param char_match The matching character 00168 */ 00169 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); 00170 00171 /** Abort the on-going read transfer 00172 */ 00173 void abort_read(); 00174 00175 /** Configure DMA usage suggestion for non-blocking TX transfers 00176 * 00177 * @param usage The usage DMA hint for peripheral 00178 * @return Zero if the usage was set, -1 if a transaction is on-going 00179 */ 00180 int set_dma_usage_tx(DMAUsage usage); 00181 00182 /** Configure DMA usage suggestion for non-blocking RX transfers 00183 * 00184 * @param usage The usage DMA hint for peripheral 00185 * @return Zero if the usage was set, -1 if a transaction is on-going 00186 */ 00187 int set_dma_usage_rx(DMAUsage usage); 00188 00189 protected: 00190 void start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t & callback, int event, unsigned char char_match); 00191 void start_write(void *buffer, int buffer_size, char buffer_width, const event_callback_t & callback, int event); 00192 void interrupt_handler_asynch(void); 00193 #endif 00194 00195 protected: 00196 SerialBase(PinName tx, PinName rx); 00197 virtual ~SerialBase() { 00198 } 00199 00200 int _base_getc(); 00201 int _base_putc(int c); 00202 00203 #if DEVICE_SERIAL_ASYNCH 00204 CThunk<SerialBase> _thunk_irq; 00205 event_callback_t _tx_callback; 00206 event_callback_t _rx_callback; 00207 DMAUsage _tx_usage; 00208 DMAUsage _rx_usage; 00209 #endif 00210 00211 serial_t _serial; 00212 FunctionPointer _irq[2]; 00213 int _baud; 00214 00215 }; 00216 00217 } // namespace mbed 00218 00219 #endif 00220 00221 #endif
Generated on Tue Jul 12 2022 19:00:10 by
1.7.2
