Jon Freeman / AsyncSerial

Dependencies:   FIFO

Dependents:   Brute_TS_Controller_2018_11

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AsyncSerial.hpp Source File

AsyncSerial.hpp

Go to the documentation of this file.
00001 // -*- coding: utf-8 -*-
00002 /**
00003  @file      AsyncSerial.hpp
00004  @brief     Asynchronous (Non-brocking) Serial Communication library with variable length software ring buffer (FIFO). You can use also RawSerial Library's method. You can set the baudrate of the serial communication when instantiating.
00005  
00006  @author    T.Kawamura
00007  @version   1.3
00008  @date      2017-03-29  T.Kawamura  Written for C++/mbed.
00009  @date      2017-03-30  T.Kawamura  Bug Fixed: Cannot use format(), baud().
00010  @date      2017-06-17  T.Kawamura  Update: FIFO Buffer Fixed.
00011  @date      2017-06-23  T.Kawamura  Bug Fixed: Cant use wait().
00012  @see 
00013  Copyright (C) 2017 T.Kawamura.
00014  Released under the MIT license.
00015  http://opensource.org/licenses/mit-license.php
00016  
00017 */
00018 
00019 #ifndef ASYNCSERIAL_H
00020 #define ASYNCSERIAL_H
00021 
00022 #include "mbed.h"
00023 #include "FIFO.hpp"
00024 #include <stdarg.h>
00025 #include <string.h>
00026 
00027 using namespace std;
00028 
00029 //#define PRINTF_STRING_BUFFER_SIZE 256
00030 #define PRINTF_STRING_BUFFER_SIZE 512
00031 
00032 /**
00033     @class  AsyncSerial
00034   @brief    Asynchronous Serial Communication with FIFO
00035 */
00036 class AsyncSerial{
00037 private:
00038     
00039     DigitalOut *led;
00040     
00041     
00042     
00043     // Serial port
00044     RawSerial *serial;
00045     
00046     // FIFO is uint8_t(unsigned char) buffer
00047     FIFO<uint8_t> *fifo_tx;
00048     FIFO<uint8_t> *fifo_rx;
00049     
00050     bool Is_Serial_Sending;
00051     
00052     void ISR_TX(void);
00053     void ISR_RX(void);
00054 
00055 public:
00056 
00057     /**
00058          @brief Create a new AsyncSerial Port.
00059          @param txpin   Tx pin name (Defined in PinName.h)
00060          @param rxpin   Rx pin name (Defined in PinName.h)
00061          @param baudrate    Baudrate (ex: 115200). Default value is 9600.
00062          @param buffer_size Buffer size. Default value is 256. (byte)
00063     */
00064     AsyncSerial(PinName txpin, PinName rxpin, uint32_t baudrate=9600, uint32_t buffer_size=256);
00065     /**
00066          @brief Disable the AsyncSerial Port.
00067          @param No parameters.
00068     */
00069     virtual ~AsyncSerial(void);
00070 
00071     /**
00072          @brief Get how many bytes are in the rx buffer.
00073          @param No parameters.
00074          @return    Size of readable data. (byte)
00075     */
00076     virtual int readable(void);
00077 
00078     /**
00079          @brief Check writeable or not.
00080          @param No Parameters.
00081          @retval    1   Always return 1 because this library provides asynchronous serial.
00082     */
00083     virtual int writeable(void);
00084 
00085     /**
00086          @brief Get 1byte from the AsyncSerial port.
00087          @param No parameters.
00088          @retval    All Got Data
00089          @retval    0   Error.
00090     */
00091     virtual int getc(void);
00092 
00093     /**
00094          @brief Peek 1byte from the AsyncSerial port.
00095          @param No parameters.
00096          @retval    ALL Got Data
00097          @retval    0   Error.
00098     */
00099     virtual int peekc(void);
00100 
00101     /**
00102          @brief Put 1byte to the AsyncSerial port.
00103          @param data    A Data for put
00104          @return        Nothing.
00105     */
00106     virtual void putc(int c);
00107 
00108     /**
00109          @brief Write a string with new line. The string must be NULL terminated.
00110          @param *str    A String for write (Must be NULL terminated).
00111          @return        Nothing.
00112     */
00113     virtual void puts(const char *str);
00114 
00115     /**
00116          @brief Write a formatted string to the AsyncSerial port. 
00117          @param *format A Formatted string for write.
00118          @retval    0   Error.
00119          @retval    1+  Wrote string size (byte).
00120     */
00121     virtual int printf(const char *format, ...);
00122 
00123     /**
00124          @brief Write byte array to the AsyncSerial port.
00125          @param *s  A pointer to the array for write.
00126          @param length  Write size (byte).
00127          @retval    0   Error.
00128          @retval    1   Success.
00129     */
00130     virtual int write(const uint8_t *buffer, int length);
00131 
00132     /**
00133          @brief Abort the on-going read transfer.
00134          @param No parameters.
00135     */
00136     virtual void abort_read(void);
00137     
00138     /**
00139          @brief Abort the on-going write transfer.
00140          @param No parameters.
00141     */
00142     virtual void abort_write(void);
00143     
00144     /**
00145          @brief Wait until finish all sending.
00146          @param No parameters.
00147     */
00148     virtual void wait(void);
00149 
00150     /**
00151          @brief Set bits, parity and stop bits.
00152          @param bits    Bits (5 ~ 8)
00153          @param parity  Parity
00154          @param stop_bits   Stop bits (1 or 2)
00155     */
00156     virtual void format(int bits=8, RawSerial::Parity parity=RawSerial::None, int stop_bits=1);
00157 
00158     /**
00159          @brief Set baud rate.
00160          @param baudrate    baudrate (bps).
00161     */
00162     virtual void baud(int baudrate);
00163     
00164 };
00165 
00166 #endif
00167