tetete
AsyncSerial.hpp@5:a50cdaa90d6a, 2017-03-30 (annotated)
- Committer:
- babylonica
- Date:
- Thu Mar 30 13:13:01 2017 +0000
- Revision:
- 5:a50cdaa90d6a
- Parent:
- 4:c7555051a851
- Child:
- 8:7390f9bb28d3
Version Update.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
babylonica | 5:a50cdaa90d6a | 1 | // -*- coding: utf-8 -*- |
babylonica | 5:a50cdaa90d6a | 2 | /** |
babylonica | 5:a50cdaa90d6a | 3 | @file AsyncSerial.hpp |
babylonica | 5:a50cdaa90d6a | 4 | @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. |
babylonica | 5:a50cdaa90d6a | 5 | |
babylonica | 5:a50cdaa90d6a | 6 | @author T.Kawamura |
babylonica | 5:a50cdaa90d6a | 7 | @version 1.1 |
babylonica | 5:a50cdaa90d6a | 8 | @date 2017-03-29 T.Kawamura Written for C++/mbed. |
babylonica | 5:a50cdaa90d6a | 9 | @date 2017-03-30 T.Kawamura Bug Fixed: Cannot use format(), baud(). |
babylonica | 5:a50cdaa90d6a | 10 | |
babylonica | 5:a50cdaa90d6a | 11 | @see |
babylonica | 5:a50cdaa90d6a | 12 | Copyright (C) 2017 T.Kawamura. |
babylonica | 5:a50cdaa90d6a | 13 | Released under the MIT license. |
babylonica | 5:a50cdaa90d6a | 14 | http://opensource.org/licenses/mit-license.php |
babylonica | 5:a50cdaa90d6a | 15 | |
babylonica | 5:a50cdaa90d6a | 16 | */ |
babylonica | 5:a50cdaa90d6a | 17 | |
babylonica | 5:a50cdaa90d6a | 18 | #ifndef ASYNCSERIAL_H |
babylonica | 5:a50cdaa90d6a | 19 | #define ASYNCSERIAL_H |
babylonica | 5:a50cdaa90d6a | 20 | |
babylonica | 5:a50cdaa90d6a | 21 | #include "mbed.h" |
babylonica | 5:a50cdaa90d6a | 22 | #include "FIFO.hpp" |
babylonica | 5:a50cdaa90d6a | 23 | #include <stdarg.h> |
babylonica | 5:a50cdaa90d6a | 24 | #include <string.h> |
babylonica | 5:a50cdaa90d6a | 25 | |
babylonica | 5:a50cdaa90d6a | 26 | using namespace std; |
babylonica | 5:a50cdaa90d6a | 27 | |
babylonica | 5:a50cdaa90d6a | 28 | #define PRINTF_STRING_BUFFER_SIZE 256 |
babylonica | 5:a50cdaa90d6a | 29 | |
babylonica | 5:a50cdaa90d6a | 30 | /** |
babylonica | 5:a50cdaa90d6a | 31 | @class AsyncSerial |
babylonica | 5:a50cdaa90d6a | 32 | @brief Asynchronous Serial Communication with FIFO |
babylonica | 5:a50cdaa90d6a | 33 | */ |
babylonica | 5:a50cdaa90d6a | 34 | class AsyncSerial : public RawSerial{ |
babylonica | 5:a50cdaa90d6a | 35 | private: |
babylonica | 5:a50cdaa90d6a | 36 | |
babylonica | 5:a50cdaa90d6a | 37 | // FIFO is uint8_t(unsigned char) buffer; |
babylonica | 5:a50cdaa90d6a | 38 | FIFO<uint8_t> fifo_tx; |
babylonica | 5:a50cdaa90d6a | 39 | FIFO<uint8_t> fifo_rx; |
babylonica | 5:a50cdaa90d6a | 40 | |
babylonica | 5:a50cdaa90d6a | 41 | bool Is_Serial_Sending; |
babylonica | 5:a50cdaa90d6a | 42 | |
babylonica | 5:a50cdaa90d6a | 43 | void ISR_TX(void); |
babylonica | 5:a50cdaa90d6a | 44 | void ISR_RX(void); |
babylonica | 5:a50cdaa90d6a | 45 | |
babylonica | 5:a50cdaa90d6a | 46 | public: |
babylonica | 5:a50cdaa90d6a | 47 | |
babylonica | 5:a50cdaa90d6a | 48 | /** |
babylonica | 5:a50cdaa90d6a | 49 | @brief Create a new AsyncSerial Port. |
babylonica | 5:a50cdaa90d6a | 50 | @param txpin Tx pin name (Defined in PinName.h) |
babylonica | 5:a50cdaa90d6a | 51 | @param rxpin Rx pin name (Defined in PinName.h) |
babylonica | 5:a50cdaa90d6a | 52 | @param baudrate Baudrate (ex: 115200). Default value is 9600. |
babylonica | 5:a50cdaa90d6a | 53 | @param buffer_size Buffer size. Default value is 256. (byte) |
babylonica | 5:a50cdaa90d6a | 54 | */ |
babylonica | 5:a50cdaa90d6a | 55 | AsyncSerial(PinName txpin, PinName rxpin, uint32_t baudrate=9600, uint32_t buffer_size=256); |
babylonica | 5:a50cdaa90d6a | 56 | /** |
babylonica | 5:a50cdaa90d6a | 57 | @brief Disable the AsyncSerial Port. |
babylonica | 5:a50cdaa90d6a | 58 | @param No parameters. |
babylonica | 5:a50cdaa90d6a | 59 | */ |
babylonica | 5:a50cdaa90d6a | 60 | virtual ~AsyncSerial(void); |
babylonica | 5:a50cdaa90d6a | 61 | |
babylonica | 5:a50cdaa90d6a | 62 | /** |
babylonica | 5:a50cdaa90d6a | 63 | @brief Get how many bytes are in the rx buffer. |
babylonica | 5:a50cdaa90d6a | 64 | @param No parameters. |
babylonica | 5:a50cdaa90d6a | 65 | @return Size of readable data. (byte) |
babylonica | 5:a50cdaa90d6a | 66 | */ |
babylonica | 5:a50cdaa90d6a | 67 | virtual int readable(void); |
babylonica | 5:a50cdaa90d6a | 68 | |
babylonica | 5:a50cdaa90d6a | 69 | /** |
babylonica | 5:a50cdaa90d6a | 70 | @brief Check writeable or not. |
babylonica | 5:a50cdaa90d6a | 71 | @param No Parameters. |
babylonica | 5:a50cdaa90d6a | 72 | @retval 1 Always return 1 because this library provides asynchronous serial. |
babylonica | 5:a50cdaa90d6a | 73 | */ |
babylonica | 5:a50cdaa90d6a | 74 | virtual int writeable(void); |
babylonica | 0:907ac3c2fadc | 75 | |
babylonica | 5:a50cdaa90d6a | 76 | /** |
babylonica | 5:a50cdaa90d6a | 77 | @brief Get 1byte from the AsyncSerial port. |
babylonica | 5:a50cdaa90d6a | 78 | @param No parameters. |
babylonica | 5:a50cdaa90d6a | 79 | @retval All Got Data |
babylonica | 5:a50cdaa90d6a | 80 | @retval 0 Error. |
babylonica | 5:a50cdaa90d6a | 81 | */ |
babylonica | 5:a50cdaa90d6a | 82 | virtual int getc(void); |
babylonica | 5:a50cdaa90d6a | 83 | |
babylonica | 5:a50cdaa90d6a | 84 | /** |
babylonica | 5:a50cdaa90d6a | 85 | @brief Peek 1byte from the AsyncSerial port. |
babylonica | 5:a50cdaa90d6a | 86 | @param No parameters. |
babylonica | 5:a50cdaa90d6a | 87 | @retval ALL Got Data |
babylonica | 5:a50cdaa90d6a | 88 | @retval 0 Error. |
babylonica | 5:a50cdaa90d6a | 89 | */ |
babylonica | 5:a50cdaa90d6a | 90 | virtual int peekc(void); |
babylonica | 5:a50cdaa90d6a | 91 | |
babylonica | 5:a50cdaa90d6a | 92 | /** |
babylonica | 5:a50cdaa90d6a | 93 | @brief Put 1byte to the AsyncSerial port. |
babylonica | 5:a50cdaa90d6a | 94 | @param data A Data for put |
babylonica | 5:a50cdaa90d6a | 95 | @retval 0 Error. |
babylonica | 5:a50cdaa90d6a | 96 | @retval 1 Success. |
babylonica | 5:a50cdaa90d6a | 97 | */ |
babylonica | 5:a50cdaa90d6a | 98 | virtual int putc(int c); |
babylonica | 5:a50cdaa90d6a | 99 | |
babylonica | 5:a50cdaa90d6a | 100 | /** |
babylonica | 5:a50cdaa90d6a | 101 | @brief Write a string with new line. The string must be NULL terminated. |
babylonica | 5:a50cdaa90d6a | 102 | @param *str A String for write (Must be NULL terminated). |
babylonica | 5:a50cdaa90d6a | 103 | @retval 0 Error. |
babylonica | 5:a50cdaa90d6a | 104 | @retval 1 Success. |
babylonica | 5:a50cdaa90d6a | 105 | */ |
babylonica | 5:a50cdaa90d6a | 106 | virtual int puts(const char *str); |
babylonica | 5:a50cdaa90d6a | 107 | |
babylonica | 5:a50cdaa90d6a | 108 | /** |
babylonica | 5:a50cdaa90d6a | 109 | @brief Write a formatted string to the AsyncSerial port. |
babylonica | 5:a50cdaa90d6a | 110 | @param *format A Formatted string for write. |
babylonica | 5:a50cdaa90d6a | 111 | @retval 0 Error. |
babylonica | 5:a50cdaa90d6a | 112 | @retval 1+ Wrote string size (byte). |
babylonica | 5:a50cdaa90d6a | 113 | */ |
babylonica | 5:a50cdaa90d6a | 114 | virtual int printf(const char *format, ...); |
babylonica | 5:a50cdaa90d6a | 115 | |
babylonica | 5:a50cdaa90d6a | 116 | /** |
babylonica | 5:a50cdaa90d6a | 117 | @brief Write byte array to the AsyncSerial port. |
babylonica | 5:a50cdaa90d6a | 118 | @param *s A pointer to the array for write. |
babylonica | 5:a50cdaa90d6a | 119 | @param length Write size (byte). |
babylonica | 5:a50cdaa90d6a | 120 | @retval 0 Error. |
babylonica | 5:a50cdaa90d6a | 121 | @retval 1 Success. |
babylonica | 5:a50cdaa90d6a | 122 | */ |
babylonica | 5:a50cdaa90d6a | 123 | virtual int write(const uint8_t *buffer, int length); |
babylonica | 5:a50cdaa90d6a | 124 | |
babylonica | 5:a50cdaa90d6a | 125 | /** |
babylonica | 5:a50cdaa90d6a | 126 | @brief Clear the rx buffer by compulsion. |
babylonica | 5:a50cdaa90d6a | 127 | @param No parameters. |
babylonica | 5:a50cdaa90d6a | 128 | */ |
babylonica | 5:a50cdaa90d6a | 129 | virtual void flush(void); |
babylonica | 5:a50cdaa90d6a | 130 | |
babylonica | 5:a50cdaa90d6a | 131 | /** |
babylonica | 5:a50cdaa90d6a | 132 | @brief Wait until finish all sending. |
babylonica | 5:a50cdaa90d6a | 133 | @param No parameters. |
babylonica | 5:a50cdaa90d6a | 134 | */ |
babylonica | 5:a50cdaa90d6a | 135 | virtual void wait(void); |
babylonica | 5:a50cdaa90d6a | 136 | |
babylonica | 5:a50cdaa90d6a | 137 | /** |
babylonica | 5:a50cdaa90d6a | 138 | @brief Set bits, parity and stop bits. |
babylonica | 5:a50cdaa90d6a | 139 | @param bits Bits (5 ~ 8) |
babylonica | 5:a50cdaa90d6a | 140 | @param parity Parity |
babylonica | 5:a50cdaa90d6a | 141 | @param stop_bits Stop bits (1 or 2) |
babylonica | 5:a50cdaa90d6a | 142 | */ |
babylonica | 5:a50cdaa90d6a | 143 | virtual void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1); |
babylonica | 5:a50cdaa90d6a | 144 | |
babylonica | 5:a50cdaa90d6a | 145 | /** |
babylonica | 5:a50cdaa90d6a | 146 | @brief Set baud rate. |
babylonica | 5:a50cdaa90d6a | 147 | @param baudrate baudrate (bps). |
babylonica | 5:a50cdaa90d6a | 148 | */ |
babylonica | 5:a50cdaa90d6a | 149 | virtual void baud(int baudrate); |
babylonica | 5:a50cdaa90d6a | 150 | |
babylonica | 5:a50cdaa90d6a | 151 | }; |
babylonica | 5:a50cdaa90d6a | 152 | |
babylonica | 5:a50cdaa90d6a | 153 | #endif |
babylonica | 5:a50cdaa90d6a | 154 |