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