Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: InvertedPendulum2017 SerialConnect mbed_2018 mbed_2019_rx3 ... more
AsyncSerial.hpp
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 00031 /** 00032 @class AsyncSerial 00033 @brief Asynchronous Serial Communication with FIFO 00034 */ 00035 class AsyncSerial{ 00036 private: 00037 //DigitalOut *led; 00038 00039 // Serial port 00040 RawSerial *serial; 00041 00042 // FIFO is uint8_t(unsigned char) buffer 00043 FIFO<uint8_t> *fifo_tx; 00044 FIFO<uint8_t> *fifo_rx; 00045 00046 bool Is_Serial_Sending; 00047 00048 void ISR_TX(void); 00049 void ISR_RX(void); 00050 00051 public: 00052 00053 /** 00054 @brief Create a new AsyncSerial Port. 00055 @param txpin Tx pin name (Defined in PinName.h) 00056 @param rxpin Rx pin name (Defined in PinName.h) 00057 @param baudrate Baudrate (ex: 115200). Default value is 9600. 00058 @param buffer_size Buffer size. Default value is 256. (byte) 00059 */ 00060 AsyncSerial(PinName txpin, PinName rxpin, uint32_t baudrate=9600, uint32_t buffer_size=256); 00061 /** 00062 @brief Disable the AsyncSerial Port. 00063 @param No parameters. 00064 */ 00065 virtual ~AsyncSerial(void); 00066 00067 /** 00068 @brief Get how many bytes are in the rx buffer. 00069 @param No parameters. 00070 @return Size of readable data. (byte) 00071 */ 00072 virtual int readable(void); 00073 00074 /** 00075 @brief Check writeable or not. 00076 @param No Parameters. 00077 @retval 1 Always return 1 because this library provides asynchronous serial. 00078 */ 00079 virtual int writeable(void); 00080 00081 /** 00082 @brief Get 1byte from the AsyncSerial port. 00083 @param No parameters. 00084 @retval All Got Data 00085 @retval 0 Error. 00086 */ 00087 virtual int getc(void); 00088 00089 /** 00090 @brief Peek 1byte from the AsyncSerial port. 00091 @param No parameters. 00092 @retval ALL Got Data 00093 @retval 0 Error. 00094 */ 00095 virtual int peekc(void); 00096 00097 /** 00098 @brief Put 1byte to the AsyncSerial port. 00099 @param data A Data for put 00100 @return Nothing. 00101 */ 00102 virtual void putc(int c); 00103 00104 /** 00105 @brief Write a string with new line. The string must be NULL terminated. 00106 @param *str A String for write (Must be NULL terminated). 00107 @return Nothing. 00108 */ 00109 virtual void puts(const char *str); 00110 00111 /** 00112 @brief Write a formatted string to the AsyncSerial port. 00113 @param *format A Formatted string for write. 00114 @retval 0 Error. 00115 @retval 1+ Wrote string size (byte). 00116 */ 00117 virtual int printf(const char *format, ...); 00118 00119 /** 00120 @brief Write byte array to the AsyncSerial port. 00121 @param *s A pointer to the array for write. 00122 @param length Write size (byte). 00123 @retval 0 Error. 00124 @retval 1 Success. 00125 */ 00126 virtual int write(const uint8_t *buffer, int length); 00127 00128 /** 00129 @brief Abort the on-going read transfer. 00130 @param No parameters. 00131 */ 00132 virtual void abort_read(void); 00133 00134 /** 00135 @brief Abort the on-going write transfer. 00136 @param No parameters. 00137 */ 00138 virtual void abort_write(void); 00139 00140 /** 00141 @brief Wait until finish all sending. 00142 @param No parameters. 00143 */ 00144 virtual void wait(void); 00145 00146 /** 00147 @brief Set bits, parity and stop bits. 00148 @param bits Bits (5 ~ 8) 00149 @param parity Parity 00150 @param stop_bits Stop bits (1 or 2) 00151 */ 00152 virtual void format(int bits=8, RawSerial::Parity parity=RawSerial::None, int stop_bits=1); 00153 00154 /** 00155 @brief Set baud rate. 00156 @param baudrate baudrate (bps). 00157 */ 00158 virtual void baud(int baudrate); 00159 00160 }; 00161 00162 #endif 00163
Generated on Tue Jul 12 2022 20:12:48 by
1.7.2