mbed library sources for airmote
Fork of mbed-src by
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.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 } else { 00109 serial_irq_set(&_serial, (SerialIrq)type, 0); 00110 } 00111 } 00112 00113 /** Generate a break condition on the serial line 00114 */ 00115 void send_break(); 00116 00117 #if DEVICE_SERIAL_FC 00118 /** Set the flow control type on the serial port 00119 * 00120 * @param type the flow control type (Disabled, RTS, CTS, RTSCTS) 00121 * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS) 00122 * @param flow2 the second flow control pin (CTS for RTSCTS) 00123 */ 00124 void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC); 00125 #endif 00126 00127 static void _irq_handler(uint32_t id, SerialIrq irq_type); 00128 00129 #if DEVICE_SERIAL_ASYNCH 00130 00131 /** Begin asynchronous write using 8bit buffer. The completition invokes registered TX event callback 00132 * 00133 * @param buffer The buffer where received data will be stored 00134 * @param length The buffer length in bytes 00135 * @param callback The event callback function 00136 * @param event The logical OR of TX events 00137 */ 00138 int write(const uint8_t *buffer, int length, const event_callback_t & callback, int event = SERIAL_EVENT_TX_COMPLETE); 00139 00140 /** Begin asynchronous write using 16bit buffer. The completition invokes registered TX event callback 00141 * 00142 * @param buffer The buffer where received data will be stored 00143 * @param length The buffer length in bytes 00144 * @param callback The event callback function 00145 * @param event The logical OR of TX events 00146 */ 00147 int write(const uint16_t *buffer, int length, const event_callback_t & callback, int event = SERIAL_EVENT_TX_COMPLETE); 00148 00149 /** Abort the on-going write transfer 00150 */ 00151 void abort_write(); 00152 00153 /** Begin asynchronous reading using 8bit buffer. The completition invokes registred RX event callback. 00154 * 00155 * @param buffer The buffer where received data will be stored 00156 * @param length The buffer length in bytes 00157 * @param callback The event callback function 00158 * @param event The logical OR of RX events 00159 * @param char_match The matching character 00160 */ 00161 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); 00162 00163 /** Begin asynchronous reading using 16bit buffer. The completition invokes registred RX event callback. 00164 * 00165 * @param buffer The buffer where received data will be stored 00166 * @param length The buffer length in bytes 00167 * @param callback The event callback function 00168 * @param event The logical OR of RX events 00169 * @param char_match The matching character 00170 */ 00171 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); 00172 00173 /** Abort the on-going read transfer 00174 */ 00175 void abort_read(); 00176 00177 /** Configure DMA usage suggestion for non-blocking TX transfers 00178 * 00179 * @param usage The usage DMA hint for peripheral 00180 * @return Zero if the usage was set, -1 if a transaction is on-going 00181 */ 00182 int set_dma_usage_tx(DMAUsage usage); 00183 00184 /** Configure DMA usage suggestion for non-blocking RX transfers 00185 * 00186 * @param usage The usage DMA hint for peripheral 00187 * @return Zero if the usage was set, -1 if a transaction is on-going 00188 */ 00189 int set_dma_usage_rx(DMAUsage usage); 00190 00191 protected: 00192 void start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t & callback, int event, unsigned char char_match); 00193 void start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t & callback, int event); 00194 void interrupt_handler_asynch(void); 00195 #endif 00196 00197 protected: 00198 SerialBase(PinName tx, PinName rx); 00199 virtual ~SerialBase() { 00200 } 00201 00202 int _base_getc(); 00203 int _base_putc(int c); 00204 00205 #if DEVICE_SERIAL_ASYNCH 00206 CThunk<SerialBase> _thunk_irq; 00207 event_callback_t _tx_callback; 00208 event_callback_t _rx_callback; 00209 DMAUsage _tx_usage; 00210 DMAUsage _rx_usage; 00211 #endif 00212 00213 serial_t _serial; 00214 FunctionPointer _irq[2]; 00215 int _baud; 00216 00217 }; 00218 00219 } // namespace mbed 00220 00221 #endif 00222 00223 #endif
Generated on Tue Jul 12 2022 16:19:20 by 1.7.2