tetete
AsyncSerial.cpp@10:1baa8e833ac6, 2017-08-22 (annotated)
- Committer:
- babylonica
- Date:
- Tue Aug 22 07:30:07 2017 +0000
- Revision:
- 10:1baa8e833ac6
- Parent:
- 9:709719dddd3e
- Child:
- 12:bfe3984fb2e5
AsyncSerial is RawSerial 2 Async has Raw.
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.cpp |
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 | 5:a50cdaa90d6a | 11 | |
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 | #include "AsyncSerial.hpp" |
babylonica | 5:a50cdaa90d6a | 20 | |
babylonica | 10:1baa8e833ac6 | 21 | AsyncSerial::AsyncSerial(PinName txpin, PinName rxpin, uint32_t baudrate, uint32_t buffer_size){ |
babylonica | 10:1baa8e833ac6 | 22 | // RawSerial port init |
babylonica | 10:1baa8e833ac6 | 23 | serial = new RawSerial(txpin, rxpin, baudrate); |
babylonica | 10:1baa8e833ac6 | 24 | |
babylonica | 10:1baa8e833ac6 | 25 | // FIFO init |
babylonica | 10:1baa8e833ac6 | 26 | fifo_tx = new FIFO<uint8_t>(buffer_size); |
babylonica | 10:1baa8e833ac6 | 27 | fifo_rx = new FIFO<uint8_t>(buffer_size); |
babylonica | 10:1baa8e833ac6 | 28 | |
babylonica | 5:a50cdaa90d6a | 29 | //Initialize ISR |
babylonica | 10:1baa8e833ac6 | 30 | serial->attach(this, &AsyncSerial::ISR_TX, serial->TxIrq); |
babylonica | 10:1baa8e833ac6 | 31 | serial->attach(this, &AsyncSerial::ISR_RX, serial->RxIrq); |
babylonica | 5:a50cdaa90d6a | 32 | |
babylonica | 10:1baa8e833ac6 | 33 | fifo_tx->clear(); |
babylonica | 10:1baa8e833ac6 | 34 | fifo_rx->clear(); |
babylonica | 5:a50cdaa90d6a | 35 | |
babylonica | 5:a50cdaa90d6a | 36 | Is_Serial_Sending = false; |
babylonica | 5:a50cdaa90d6a | 37 | |
babylonica | 5:a50cdaa90d6a | 38 | return; |
babylonica | 5:a50cdaa90d6a | 39 | } |
babylonica | 5:a50cdaa90d6a | 40 | |
babylonica | 5:a50cdaa90d6a | 41 | AsyncSerial::~AsyncSerial(){ |
babylonica | 10:1baa8e833ac6 | 42 | serial->attach(NULL, serial->TxIrq); |
babylonica | 10:1baa8e833ac6 | 43 | serial->attach(NULL, serial->RxIrq); |
babylonica | 10:1baa8e833ac6 | 44 | |
babylonica | 10:1baa8e833ac6 | 45 | delete serial; |
babylonica | 5:a50cdaa90d6a | 46 | return; |
babylonica | 5:a50cdaa90d6a | 47 | } |
babylonica | 5:a50cdaa90d6a | 48 | |
babylonica | 5:a50cdaa90d6a | 49 | void AsyncSerial::ISR_TX(void){ |
babylonica | 5:a50cdaa90d6a | 50 | int data; |
babylonica | 5:a50cdaa90d6a | 51 | |
babylonica | 10:1baa8e833ac6 | 52 | if( fifo_tx->available() > 0 ){ |
babylonica | 10:1baa8e833ac6 | 53 | data = (int)fifo_tx->get(); |
babylonica | 10:1baa8e833ac6 | 54 | serial->putc(data); |
babylonica | 5:a50cdaa90d6a | 55 | }else{ |
babylonica | 5:a50cdaa90d6a | 56 | Is_Serial_Sending = false; |
babylonica | 10:1baa8e833ac6 | 57 | } |
babylonica | 5:a50cdaa90d6a | 58 | } |
babylonica | 5:a50cdaa90d6a | 59 | |
babylonica | 5:a50cdaa90d6a | 60 | void AsyncSerial::ISR_RX(void){ |
babylonica | 5:a50cdaa90d6a | 61 | uint8_t data; |
babylonica | 5:a50cdaa90d6a | 62 | |
babylonica | 10:1baa8e833ac6 | 63 | data = (uint8_t)serial->getc(); |
babylonica | 10:1baa8e833ac6 | 64 | fifo_rx->put(data); |
babylonica | 5:a50cdaa90d6a | 65 | } |
babylonica | 5:a50cdaa90d6a | 66 | |
babylonica | 5:a50cdaa90d6a | 67 | int AsyncSerial::readable(void){ |
babylonica | 10:1baa8e833ac6 | 68 | return (int)fifo_rx->available(); |
babylonica | 5:a50cdaa90d6a | 69 | } |
babylonica | 5:a50cdaa90d6a | 70 | |
babylonica | 5:a50cdaa90d6a | 71 | int AsyncSerial::writeable(void){ |
babylonica | 5:a50cdaa90d6a | 72 | return 1; |
babylonica | 5:a50cdaa90d6a | 73 | } |
babylonica | 5:a50cdaa90d6a | 74 | |
babylonica | 5:a50cdaa90d6a | 75 | int AsyncSerial::getc(void){ |
babylonica | 10:1baa8e833ac6 | 76 | return (int)fifo_rx->get(); |
babylonica | 5:a50cdaa90d6a | 77 | } |
babylonica | 5:a50cdaa90d6a | 78 | |
babylonica | 5:a50cdaa90d6a | 79 | int AsyncSerial::peekc(void){ |
babylonica | 10:1baa8e833ac6 | 80 | return (int)fifo_rx->peek(); |
babylonica | 5:a50cdaa90d6a | 81 | } |
babylonica | 5:a50cdaa90d6a | 82 | |
babylonica | 8:7390f9bb28d3 | 83 | void AsyncSerial::putc(int c){ |
babylonica | 5:a50cdaa90d6a | 84 | if( Is_Serial_Sending ){ |
babylonica | 10:1baa8e833ac6 | 85 | fifo_tx->put((uint8_t)c); |
babylonica | 5:a50cdaa90d6a | 86 | }else{ |
babylonica | 5:a50cdaa90d6a | 87 | Is_Serial_Sending = true; |
babylonica | 10:1baa8e833ac6 | 88 | serial->putc(c); |
babylonica | 5:a50cdaa90d6a | 89 | } |
babylonica | 8:7390f9bb28d3 | 90 | return; |
babylonica | 5:a50cdaa90d6a | 91 | } |
babylonica | 5:a50cdaa90d6a | 92 | |
babylonica | 8:7390f9bb28d3 | 93 | void AsyncSerial::puts(const char *str){ |
babylonica | 5:a50cdaa90d6a | 94 | uint8_t temp; |
babylonica | 0:907ac3c2fadc | 95 | |
babylonica | 5:a50cdaa90d6a | 96 | for(uint32_t i = 0; i < strlen(str); i++){ |
babylonica | 5:a50cdaa90d6a | 97 | temp = (uint8_t)str[i]; |
babylonica | 10:1baa8e833ac6 | 98 | fifo_tx->put(temp); |
babylonica | 5:a50cdaa90d6a | 99 | } |
babylonica | 5:a50cdaa90d6a | 100 | |
babylonica | 5:a50cdaa90d6a | 101 | if( !Is_Serial_Sending ){ |
babylonica | 5:a50cdaa90d6a | 102 | Is_Serial_Sending = true; |
babylonica | 10:1baa8e833ac6 | 103 | serial->putc((int)fifo_tx->get()); |
babylonica | 5:a50cdaa90d6a | 104 | } |
babylonica | 6:08e3b1bba0ac | 105 | |
babylonica | 6:08e3b1bba0ac | 106 | AsyncSerial::putc('\r'); |
babylonica | 5:a50cdaa90d6a | 107 | AsyncSerial::putc('\n'); |
babylonica | 8:7390f9bb28d3 | 108 | return; |
babylonica | 5:a50cdaa90d6a | 109 | } |
babylonica | 5:a50cdaa90d6a | 110 | |
babylonica | 5:a50cdaa90d6a | 111 | int AsyncSerial::printf(const char *format, ...){ |
babylonica | 5:a50cdaa90d6a | 112 | int32_t wrote_length = 0; |
babylonica | 5:a50cdaa90d6a | 113 | char string_buffer[PRINTF_STRING_BUFFER_SIZE]; |
babylonica | 5:a50cdaa90d6a | 114 | |
babylonica | 5:a50cdaa90d6a | 115 | memset(string_buffer, 0, PRINTF_STRING_BUFFER_SIZE); |
babylonica | 5:a50cdaa90d6a | 116 | |
babylonica | 5:a50cdaa90d6a | 117 | va_list arg; |
babylonica | 5:a50cdaa90d6a | 118 | va_start(arg, format); |
babylonica | 5:a50cdaa90d6a | 119 | wrote_length = vsprintf(string_buffer, format, arg); |
babylonica | 5:a50cdaa90d6a | 120 | |
babylonica | 5:a50cdaa90d6a | 121 | if( wrote_length > PRINTF_STRING_BUFFER_SIZE ) { |
babylonica | 5:a50cdaa90d6a | 122 | error("%s @ %d : String is too large, string buffer overwrite. (Max buffer size: %d Wrote length: %d)\n", __FILE__, __LINE__, PRINTF_STRING_BUFFER_SIZE, wrote_length); |
babylonica | 5:a50cdaa90d6a | 123 | va_end(arg); |
babylonica | 5:a50cdaa90d6a | 124 | return 0; |
babylonica | 5:a50cdaa90d6a | 125 | } |
babylonica | 5:a50cdaa90d6a | 126 | |
babylonica | 5:a50cdaa90d6a | 127 | if( wrote_length < 0 ){ |
babylonica | 5:a50cdaa90d6a | 128 | va_end(arg); |
babylonica | 5:a50cdaa90d6a | 129 | error("Function vsprintf() was failed."); |
babylonica | 5:a50cdaa90d6a | 130 | return 0; |
babylonica | 5:a50cdaa90d6a | 131 | } |
babylonica | 5:a50cdaa90d6a | 132 | |
babylonica | 5:a50cdaa90d6a | 133 | va_end(arg); |
babylonica | 8:7390f9bb28d3 | 134 | AsyncSerial::write((uint8_t*)string_buffer, wrote_length); |
babylonica | 5:a50cdaa90d6a | 135 | |
babylonica | 8:7390f9bb28d3 | 136 | return wrote_length; |
babylonica | 5:a50cdaa90d6a | 137 | } |
babylonica | 5:a50cdaa90d6a | 138 | |
babylonica | 5:a50cdaa90d6a | 139 | int AsyncSerial::write(const uint8_t *buffer, int length){ |
babylonica | 5:a50cdaa90d6a | 140 | uint8_t temp; |
babylonica | 5:a50cdaa90d6a | 141 | |
babylonica | 5:a50cdaa90d6a | 142 | if ( length < 1 ){ |
babylonica | 5:a50cdaa90d6a | 143 | return 0; |
babylonica | 5:a50cdaa90d6a | 144 | } |
babylonica | 5:a50cdaa90d6a | 145 | |
babylonica | 5:a50cdaa90d6a | 146 | for(uint32_t i = 0; i < length; i++){ |
babylonica | 5:a50cdaa90d6a | 147 | temp = (uint8_t)buffer[i]; |
babylonica | 10:1baa8e833ac6 | 148 | fifo_tx->put(temp); |
babylonica | 5:a50cdaa90d6a | 149 | } |
babylonica | 5:a50cdaa90d6a | 150 | |
babylonica | 5:a50cdaa90d6a | 151 | if( !Is_Serial_Sending ){ |
babylonica | 5:a50cdaa90d6a | 152 | Is_Serial_Sending = true; |
babylonica | 10:1baa8e833ac6 | 153 | serial->putc((int)fifo_tx->get()); |
babylonica | 5:a50cdaa90d6a | 154 | } |
babylonica | 5:a50cdaa90d6a | 155 | |
babylonica | 8:7390f9bb28d3 | 156 | return 1; |
babylonica | 5:a50cdaa90d6a | 157 | } |
babylonica | 5:a50cdaa90d6a | 158 | |
babylonica | 10:1baa8e833ac6 | 159 | void AsyncSerial::abort_read(void){ |
babylonica | 10:1baa8e833ac6 | 160 | fifo_rx->clear(); |
babylonica | 10:1baa8e833ac6 | 161 | return; |
babylonica | 10:1baa8e833ac6 | 162 | } |
babylonica | 10:1baa8e833ac6 | 163 | |
babylonica | 10:1baa8e833ac6 | 164 | void AsyncSerial::abort_write(void){ |
babylonica | 10:1baa8e833ac6 | 165 | fifo_tx->clear(); |
babylonica | 5:a50cdaa90d6a | 166 | return; |
babylonica | 5:a50cdaa90d6a | 167 | } |
babylonica | 5:a50cdaa90d6a | 168 | |
babylonica | 5:a50cdaa90d6a | 169 | void AsyncSerial::wait(void){ |
babylonica | 10:1baa8e833ac6 | 170 | while( fifo_tx->available() > 0 ){} |
babylonica | 5:a50cdaa90d6a | 171 | return; |
babylonica | 5:a50cdaa90d6a | 172 | } |
babylonica | 5:a50cdaa90d6a | 173 | |
babylonica | 10:1baa8e833ac6 | 174 | void AsyncSerial::format(int bits, RawSerial::Parity parity, int stop_bits){ |
babylonica | 10:1baa8e833ac6 | 175 | serial->attach(NULL, serial->TxIrq); |
babylonica | 10:1baa8e833ac6 | 176 | serial->attach(NULL, serial->RxIrq); |
babylonica | 5:a50cdaa90d6a | 177 | |
babylonica | 10:1baa8e833ac6 | 178 | serial->format(bits, parity, stop_bits); |
babylonica | 5:a50cdaa90d6a | 179 | |
babylonica | 10:1baa8e833ac6 | 180 | serial->attach(this, &AsyncSerial::ISR_TX, serial->TxIrq); |
babylonica | 10:1baa8e833ac6 | 181 | serial->attach(this, &AsyncSerial::ISR_RX, serial->RxIrq); |
babylonica | 5:a50cdaa90d6a | 182 | } |
babylonica | 5:a50cdaa90d6a | 183 | |
babylonica | 5:a50cdaa90d6a | 184 | void AsyncSerial::baud(int baudrate){ |
babylonica | 10:1baa8e833ac6 | 185 | serial->attach(NULL, serial->TxIrq); |
babylonica | 10:1baa8e833ac6 | 186 | serial->attach(NULL, serial->RxIrq); |
babylonica | 5:a50cdaa90d6a | 187 | |
babylonica | 10:1baa8e833ac6 | 188 | serial->baud(baudrate); |
babylonica | 5:a50cdaa90d6a | 189 | |
babylonica | 10:1baa8e833ac6 | 190 | serial->attach(this, &AsyncSerial::ISR_TX, serial->TxIrq); |
babylonica | 10:1baa8e833ac6 | 191 | serial->attach(this, &AsyncSerial::ISR_RX, serial->RxIrq); |
babylonica | 5:a50cdaa90d6a | 192 | } |
babylonica | 5:a50cdaa90d6a | 193 |