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