Renesas GR-PEACH OpenCV Development / gr-peach-opencv-project-sd-card_update

Fork of gr-peach-opencv-project-sd-card by the do

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SerialBase.h Source File

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