takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

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 "platform/Callback.h"
00024 #include "hal/serial_api.h"
00025 #include "platform/mbed_toolchain.h"
00026 #include "platform/NonCopyable.h"
00027 
00028 #if DEVICE_SERIAL_ASYNCH
00029 #include "platform/CThunk.h"
00030 #include "hal/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     {
00119         attach(callback(obj, method), type);
00120     }
00121 
00122     /** Attach a member function to call whenever a serial interrupt is generated
00123      *
00124      *  @param obj pointer to the object to call the member function on
00125      *  @param method pointer to the member function to be called
00126      *  @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
00127      *  @deprecated
00128      *      The attach function does not support cv-qualifiers. Replaced by
00129      *      attach(callback(obj, method), type).
00130      */
00131     template<typename T>
00132     MBED_DEPRECATED_SINCE("mbed-os-5.1",
00133                           "The attach function does not support cv-qualifiers. Replaced by "
00134                           "attach(callback(obj, method), type).")
00135     void attach(T *obj, void (*method)(T *), IrqType type = RxIrq)
00136     {
00137         attach(callback(obj, method), type);
00138     }
00139 
00140     /** Generate a break condition on the serial line
00141      */
00142     void send_break();
00143 
00144 protected:
00145 
00146     /** Acquire exclusive access to this serial port
00147      */
00148     virtual void lock(void);
00149 
00150     /** Release exclusive access to this serial port
00151      */
00152     virtual void unlock(void);
00153 
00154 public:
00155 
00156 #if DEVICE_SERIAL_FC
00157     /** Set the flow control type on the serial port
00158      *
00159      *  @param type the flow control type (Disabled, RTS, CTS, RTSCTS)
00160      *  @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
00161      *  @param flow2 the second flow control pin (CTS for RTSCTS)
00162      */
00163     void set_flow_control(Flow type, PinName flow1 = NC, PinName flow2 = NC);
00164 #endif
00165 
00166     static void _irq_handler(uint32_t id, SerialIrq irq_type);
00167 
00168 #if DEVICE_SERIAL_ASYNCH
00169 
00170     /** Begin asynchronous write using 8bit buffer. The completition invokes registered TX event callback
00171      *
00172      *  This function locks the deep sleep until any event has occurred
00173      *
00174      *  @param buffer   The buffer where received data will be stored
00175      *  @param length   The buffer length in bytes
00176      *  @param callback The event callback function
00177      *  @param event    The logical OR of TX events
00178      */
00179     int write(const uint8_t *buffer, int length, const event_callback_t &callback, int event = SERIAL_EVENT_TX_COMPLETE);
00180 
00181     /** Begin asynchronous write using 16bit buffer. The completition invokes registered TX event callback
00182      *
00183      *  This function locks the deep sleep until any event has occurred
00184      *
00185      *  @param buffer   The buffer where received data will be stored
00186      *  @param length   The buffer length in bytes
00187      *  @param callback The event callback function
00188      *  @param event    The logical OR of TX events
00189      */
00190     int write(const uint16_t *buffer, int length, const event_callback_t &callback, int event = SERIAL_EVENT_TX_COMPLETE);
00191 
00192     /** Abort the on-going write transfer
00193      */
00194     void abort_write();
00195 
00196     /** Begin asynchronous reading using 8bit buffer. The completition invokes registred RX event callback.
00197      *
00198      *  This function locks the deep sleep until any event has occurred
00199      *
00200      *  @param buffer     The buffer where received data will be stored
00201      *  @param length     The buffer length in bytes
00202      *  @param callback   The event callback function
00203      *  @param event      The logical OR of RX events
00204      *  @param char_match The matching character
00205      */
00206     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);
00207 
00208     /** Begin asynchronous reading using 16bit buffer. The completition invokes registred RX event callback.
00209      *
00210      *  This function locks the deep sleep until any event has occurred
00211      *
00212      *  @param buffer     The buffer where received data will be stored
00213      *  @param length     The buffer length in bytes
00214      *  @param callback   The event callback function
00215      *  @param event      The logical OR of RX events
00216      *  @param char_match The matching character
00217      */
00218     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);
00219 
00220     /** Abort the on-going read transfer
00221      */
00222     void abort_read();
00223 
00224     /** Configure DMA usage suggestion for non-blocking TX transfers
00225      *
00226      *  @param usage The usage DMA hint for peripheral
00227      *  @return Zero if the usage was set, -1 if a transaction is on-going
00228      */
00229     int set_dma_usage_tx(DMAUsage usage);
00230 
00231     /** Configure DMA usage suggestion for non-blocking RX transfers
00232      *
00233      *  @param usage The usage DMA hint for peripheral
00234      *  @return Zero if the usage was set, -1 if a transaction is on-going
00235      */
00236     int set_dma_usage_rx(DMAUsage usage);
00237 
00238 protected:
00239     void start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t &callback, int event, unsigned char char_match);
00240     void start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t &callback, int event);
00241     void interrupt_handler_asynch(void);
00242 #endif
00243 
00244 protected:
00245     SerialBase(PinName tx, PinName rx, int baud);
00246     virtual ~SerialBase();
00247 
00248     int _base_getc();
00249     int _base_putc(int c);
00250 
00251 #if DEVICE_SERIAL_ASYNCH
00252     CThunk<SerialBase>  _thunk_irq;
00253     DMAUsage _tx_usage;
00254     DMAUsage _rx_usage;
00255     event_callback_t _tx_callback;
00256     event_callback_t _rx_callback;
00257 #endif
00258 
00259     serial_t         _serial;
00260     Callback<void()> _irq[IrqCnt];
00261     int              _baud;
00262 
00263 };
00264 
00265 } // namespace mbed
00266 
00267 #endif
00268 
00269 #endif