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