This is the final version of Mini Gateway for Automation and Security desgined for Renesas GR Peach Design Contest

Dependencies:   GR-PEACH_video GraphicsFramework HTTPServer R_BSP mbed-rpc mbed-rtos Socket lwip-eth lwip-sys lwip FATFileSystem

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

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