Changes to support running on smaller memory LPC device LPC1764
Fork of mbed-dev 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 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 * @Note Synchronization level: Set by subclass 00038 */ 00039 class SerialBase { 00040 00041 public: 00042 /** Set the baud rate of the serial port 00043 * 00044 * @param baudrate The baudrate of the serial port (default = 9600). 00045 */ 00046 void baud(int baudrate); 00047 00048 enum Parity { 00049 None = 0, 00050 Odd, 00051 Even, 00052 Forced1, 00053 Forced0 00054 }; 00055 00056 enum IrqType { 00057 RxIrq = 0, 00058 TxIrq, 00059 00060 IrqCnt 00061 }; 00062 00063 enum Flow { 00064 Disabled = 0, 00065 RTS, 00066 CTS, 00067 RTSCTS 00068 }; 00069 00070 /** Set the transmission format used by the serial port 00071 * 00072 * @param bits The number of bits in a word (5-8; default = 8) 00073 * @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None) 00074 * @param stop The number of stop bits (1 or 2; default = 1) 00075 */ 00076 void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1); 00077 00078 /** Determine if there is a character available to read 00079 * 00080 * @returns 00081 * 1 if there is a character available to read, 00082 * 0 otherwise 00083 */ 00084 int readable(); 00085 00086 /** Determine if there is space available to write a character 00087 * 00088 * @returns 00089 * 1 if there is space to write a character, 00090 * 0 otherwise 00091 */ 00092 int writeable(); 00093 00094 /** Attach a function to call whenever a serial interrupt is generated 00095 * 00096 * @param func A pointer to a void function, or 0 to set as none 00097 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) 00098 */ 00099 void attach(Callback<void()> func, IrqType type=RxIrq); 00100 00101 /** Attach a member function to call whenever a serial interrupt is generated 00102 * 00103 * @param obj pointer to the object to call the member function on 00104 * @param method pointer to the member function to be called 00105 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) 00106 */ 00107 template<typename T> 00108 void attach(T *obj, void (T::*method)(), IrqType type=RxIrq) { 00109 attach(Callback<void()>(obj, method), type); 00110 } 00111 00112 /** Attach a member function to call whenever a serial interrupt is generated 00113 * 00114 * @param obj pointer to the object to call the member function on 00115 * @param method pointer to the member function to be called 00116 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) 00117 */ 00118 template<typename T> 00119 void attach(T *obj, void (*method)(T*), IrqType type=RxIrq) { 00120 attach(Callback<void()>(obj, method), type); 00121 } 00122 00123 /** Generate a break condition on the serial line 00124 */ 00125 void send_break(); 00126 00127 protected: 00128 00129 /** Acquire exclusive access to this serial port 00130 */ 00131 virtual void lock(void); 00132 00133 /** Release exclusive access to this serial port 00134 */ 00135 virtual void unlock(void); 00136 00137 public: 00138 00139 #if DEVICE_SERIAL_FC 00140 /** Set the flow control type on the serial port 00141 * 00142 * @param type the flow control type (Disabled, RTS, CTS, RTSCTS) 00143 * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS) 00144 * @param flow2 the second flow control pin (CTS for RTSCTS) 00145 */ 00146 void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC); 00147 #endif 00148 00149 static void _irq_handler(uint32_t id, SerialIrq irq_type); 00150 00151 #if DEVICE_SERIAL_ASYNCH 00152 00153 /** Begin asynchronous write using 8bit buffer. The completition invokes registered TX 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 TX events 00159 */ 00160 int write(const uint8_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE); 00161 00162 /** Begin asynchronous write using 16bit buffer. The completition invokes registered TX event callback 00163 * 00164 * @param buffer The buffer where received data will be stored 00165 * @param length The buffer length in bytes 00166 * @param callback The event callback function 00167 * @param event The logical OR of TX events 00168 */ 00169 int write(const uint16_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE); 00170 00171 /** Abort the on-going write transfer 00172 */ 00173 void abort_write(); 00174 00175 /** Begin asynchronous reading using 8bit buffer. The completition invokes registred RX 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 RX events 00181 * @param char_match The matching character 00182 */ 00183 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); 00184 00185 /** Begin asynchronous reading using 16bit buffer. The completition invokes registred RX event callback. 00186 * 00187 * @param buffer The buffer where received data will be stored 00188 * @param length The buffer length in bytes 00189 * @param callback The event callback function 00190 * @param event The logical OR of RX events 00191 * @param char_match The matching character 00192 */ 00193 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); 00194 00195 /** Abort the on-going read transfer 00196 */ 00197 void abort_read(); 00198 00199 /** Configure DMA usage suggestion for non-blocking TX transfers 00200 * 00201 * @param usage The usage DMA hint for peripheral 00202 * @return Zero if the usage was set, -1 if a transaction is on-going 00203 */ 00204 int set_dma_usage_tx(DMAUsage usage); 00205 00206 /** Configure DMA usage suggestion for non-blocking RX transfers 00207 * 00208 * @param usage The usage DMA hint for peripheral 00209 * @return Zero if the usage was set, -1 if a transaction is on-going 00210 */ 00211 int set_dma_usage_rx(DMAUsage usage); 00212 00213 protected: 00214 void start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event, unsigned char char_match); 00215 void start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event); 00216 void interrupt_handler_asynch(void); 00217 #endif 00218 00219 protected: 00220 SerialBase(PinName tx, PinName rx); 00221 virtual ~SerialBase() { 00222 } 00223 00224 int _base_getc(); 00225 int _base_putc(int c); 00226 00227 #if DEVICE_SERIAL_ASYNCH 00228 CThunk<SerialBase> _thunk_irq; 00229 event_callback_t _tx_callback; 00230 event_callback_t _rx_callback; 00231 DMAUsage _tx_usage; 00232 DMAUsage _rx_usage; 00233 #endif 00234 00235 serial_t _serial; 00236 Callback<void()> _irq[IrqCnt]; 00237 int _baud; 00238 00239 }; 00240 00241 } // namespace mbed 00242 00243 #endif 00244 00245 #endif
Generated on Tue Jul 12 2022 15:57:08 by 1.7.2