Inherit from Serial and use software buffers for TX and RX. This allows the UART peripherals to operate in a IRQ driven mode. Overrides most (but not all) stdio functions as Serial did

Dependencies:   Buffer

Dependents:   mbed_esp8266_demo

Fork of BufferedSerial by Sam Grove

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BufferedSerial.h Source File

BufferedSerial.h

Go to the documentation of this file.
00001 
00002 /**
00003  * @file    BufferedSerial.h
00004  * @brief   Software Buffer - Extends mbed Serial functionallity adding irq driven TX and RX
00005  * @author  sam grove
00006  * @version 1.0
00007  * @see     
00008  *
00009  * Copyright (c) 2013
00010  *
00011  * Licensed under the Apache License, Version 2.0 (the "License");
00012  * you may not use this file except in compliance with the License.
00013  * You may obtain a copy of the License at
00014  *
00015  *     http://www.apache.org/licenses/LICENSE-2.0
00016  *
00017  * Unless required by applicable law or agreed to in writing, software
00018  * distributed under the License is distributed on an "AS IS" BASIS,
00019  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020  * See the License for the specific language governing permissions and
00021  * limitations under the License.
00022  */
00023 
00024 #ifndef BUFFEREDSERIAL_H
00025 #define BUFFEREDSERIAL_H
00026  
00027 #include "mbed.h"
00028 #include "Buffer.h"
00029 
00030 // Base Class
00031 #define SERIAL_BASE  RawSerial
00032 
00033 /** A serial port (UART) for communication with other serial devices
00034  *
00035  * Can be used for Full Duplex communication, or Simplex by specifying
00036  * one pin as NC (Not Connected)
00037  *
00038  * Example:
00039  * @code
00040  *  #include "mbed.h"
00041  *  #include "BufferedSerial.h"
00042  *
00043  *  BufferedSerial pc(USBTX, USBRX);
00044  *
00045  *  int main()
00046  *  { 
00047  *      while(1)
00048  *      {
00049  *          Timer s;
00050  *        
00051  *          s.start();
00052  *          pc.printf("Hello World - buffered\n");
00053  *          int buffered_time = s.read_us();
00054  *          wait(0.1f); // give time for the buffer to empty
00055  *        
00056  *          s.reset();
00057  *          printf("Hello World - blocking\n");
00058  *          int polled_time = s.read_us();
00059  *          s.stop();
00060  *          wait(0.1f); // give time for the buffer to empty
00061  *        
00062  *          pc.printf("printf buffered took %d us\n", buffered_time);
00063  *          pc.printf("printf blocking took %d us\n", polled_time);
00064  *          wait(0.5f);
00065  *      }
00066  *  }
00067  * @endcode
00068  */
00069 
00070 /**
00071  *  @class BufferedSerial
00072  *  @brief Software buffers and interrupt driven tx and rx for Serial
00073  */  
00074 class BufferedSerial : public SERIAL_BASE 
00075 {
00076 private:
00077     Buffer <char> _rxbuf;
00078     Buffer <char> _txbuf;
00079     uint32_t      _buf_size;
00080     uint32_t      _tx_multiple;
00081  
00082     void rxIrq(void);
00083     void txIrq(void);
00084     void prime(void);
00085     
00086 public:
00087     /** Create a BufferedSerial port, connected to the specified transmit and receive pins
00088      *  @param tx Transmit pin
00089      *  @param rx Receive pin
00090      *  @param buf_size printf() buffer size
00091      *  @param tx_multiple amount of max printf() present in the internal ring buffer at one time
00092      *  @param name optional name
00093      *  @note Either tx or rx may be specified as NC if unused
00094      */
00095     BufferedSerial(PinName tx, PinName rx, uint32_t buf_size = 256, uint32_t tx_multiple = 4,const char* name=NULL);
00096     
00097     /** Destroy a BufferedSerial port
00098      */
00099     virtual ~BufferedSerial(void);
00100     
00101     /** Check on how many bytes are in the rx buffer
00102      *  @return 1 if something exists, 0 otherwise
00103      */
00104     virtual int readable(void);
00105     
00106     /** Check to see if the tx buffer has room
00107      *  @return 1 always has room and can overwrite previous content if too small / slow
00108      */
00109     virtual int writeable(void);
00110     
00111     /** Get a single byte from the BufferedSerial Port.
00112      *  Should check readable() before calling this.
00113      *  @return A byte that came in on the Serial Port
00114      */
00115     virtual int getc(void);
00116     
00117     /**
00118      * Read a line from the BufferedSerial Port.
00119      * @param s The string buffer to read from Serial Port
00120      * @param size the max number to read from Serial Port
00121      * @return the string read from Serial Port
00122      */
00123     char* readl(char *s, int size);
00124 
00125     /** Write a single byte to the BufferedSerial Port.
00126      *  @param c The byte to write to the Serial Port
00127      *  @return The byte that was written to the Serial Port Buffer
00128      */
00129     virtual int putc(int c);
00130     
00131     /** Write a string to the BufferedSerial Port. Must be NULL terminated
00132      *  @param s The string to write to the Serial Port
00133      *  @return The number of bytes written to the Serial Port Buffer
00134      */
00135     virtual int puts(const char *s);
00136     
00137     /** Write a formatted string to the BufferedSerial Port.
00138      *  @param format The string + format specifiers to write to the Serial Port
00139      *  @return The number of bytes written to the Serial Port Buffer
00140      */
00141     virtual int printf(const char* format, ...);
00142     
00143     /** Write data to the Buffered Serial Port
00144      *  @param s A pointer to data to send
00145      *  @param length The amount of data being pointed to
00146      *  @return The number of bytes written to the Serial Port Buffer
00147      */
00148     virtual ssize_t write(const void *s, std::size_t length);
00149 };
00150 
00151 #endif