tetete
AsyncSerial.cpp@11:690c3439cfd2, 2017-08-22 (annotated)
- Committer:
- babylonica
- Date:
- Tue Aug 22 07:40:57 2017 +0000
- Revision:
- 11:690c3439cfd2
- Parent:
- 9:709719dddd3e
This is Stable Version (Needs Mbed OS 2 Rev.145 or older)
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 | 11:690c3439cfd2 | 7 | @version 1.2(Stable) |
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 | 11:690c3439cfd2 | 12 | This version needs Mbed OS 2 Rev.145 or older. |
babylonica | 11:690c3439cfd2 | 13 | |
babylonica | 5:a50cdaa90d6a | 14 | @see |
babylonica | 5:a50cdaa90d6a | 15 | Copyright (C) 2017 T.Kawamura. |
babylonica | 5:a50cdaa90d6a | 16 | Released under the MIT license. |
babylonica | 5:a50cdaa90d6a | 17 | http://opensource.org/licenses/mit-license.php |
babylonica | 5:a50cdaa90d6a | 18 | |
babylonica | 5:a50cdaa90d6a | 19 | */ |
babylonica | 5:a50cdaa90d6a | 20 | |
babylonica | 5:a50cdaa90d6a | 21 | #include "AsyncSerial.hpp" |
babylonica | 5:a50cdaa90d6a | 22 | |
babylonica | 5:a50cdaa90d6a | 23 | AsyncSerial::AsyncSerial(PinName txpin, PinName rxpin, uint32_t baudrate, uint32_t buffer_size) : RawSerial(txpin, rxpin, baudrate), fifo_tx(buffer_size), fifo_rx(buffer_size){ |
babylonica | 5:a50cdaa90d6a | 24 | //Initialize ISR |
babylonica | 5:a50cdaa90d6a | 25 | RawSerial::attach(this, &AsyncSerial::ISR_TX, RawSerial::TxIrq); |
babylonica | 5:a50cdaa90d6a | 26 | RawSerial::attach(this, &AsyncSerial::ISR_RX, RawSerial::RxIrq); |
babylonica | 5:a50cdaa90d6a | 27 | |
babylonica | 5:a50cdaa90d6a | 28 | fifo_tx.clear(); |
babylonica | 5:a50cdaa90d6a | 29 | fifo_rx.clear(); |
babylonica | 5:a50cdaa90d6a | 30 | |
babylonica | 5:a50cdaa90d6a | 31 | Is_Serial_Sending = false; |
babylonica | 5:a50cdaa90d6a | 32 | |
babylonica | 5:a50cdaa90d6a | 33 | return; |
babylonica | 5:a50cdaa90d6a | 34 | } |
babylonica | 5:a50cdaa90d6a | 35 | |
babylonica | 5:a50cdaa90d6a | 36 | AsyncSerial::~AsyncSerial(){ |
babylonica | 5:a50cdaa90d6a | 37 | RawSerial::attach(NULL, RawSerial::TxIrq); |
babylonica | 5:a50cdaa90d6a | 38 | RawSerial::attach(NULL, RawSerial::RxIrq); |
babylonica | 5:a50cdaa90d6a | 39 | |
babylonica | 5:a50cdaa90d6a | 40 | return; |
babylonica | 5:a50cdaa90d6a | 41 | } |
babylonica | 5:a50cdaa90d6a | 42 | |
babylonica | 5:a50cdaa90d6a | 43 | void AsyncSerial::ISR_TX(void){ |
babylonica | 5:a50cdaa90d6a | 44 | int data; |
babylonica | 5:a50cdaa90d6a | 45 | |
babylonica | 5:a50cdaa90d6a | 46 | if( fifo_tx.available() > 0 ){ |
babylonica | 5:a50cdaa90d6a | 47 | data = (int)fifo_tx.get(); |
babylonica | 5:a50cdaa90d6a | 48 | RawSerial::putc(data); |
babylonica | 5:a50cdaa90d6a | 49 | }else{ |
babylonica | 5:a50cdaa90d6a | 50 | Is_Serial_Sending = false; |
babylonica | 5:a50cdaa90d6a | 51 | } |
babylonica | 5:a50cdaa90d6a | 52 | } |
babylonica | 5:a50cdaa90d6a | 53 | |
babylonica | 5:a50cdaa90d6a | 54 | void AsyncSerial::ISR_RX(void){ |
babylonica | 5:a50cdaa90d6a | 55 | uint8_t data; |
babylonica | 5:a50cdaa90d6a | 56 | |
babylonica | 5:a50cdaa90d6a | 57 | data = (uint8_t)RawSerial::getc(); |
babylonica | 5:a50cdaa90d6a | 58 | fifo_rx.put(data); |
babylonica | 5:a50cdaa90d6a | 59 | } |
babylonica | 5:a50cdaa90d6a | 60 | |
babylonica | 5:a50cdaa90d6a | 61 | int AsyncSerial::readable(void){ |
babylonica | 5:a50cdaa90d6a | 62 | return (int)fifo_rx.available(); |
babylonica | 5:a50cdaa90d6a | 63 | } |
babylonica | 5:a50cdaa90d6a | 64 | |
babylonica | 5:a50cdaa90d6a | 65 | int AsyncSerial::writeable(void){ |
babylonica | 5:a50cdaa90d6a | 66 | return 1; |
babylonica | 5:a50cdaa90d6a | 67 | } |
babylonica | 5:a50cdaa90d6a | 68 | |
babylonica | 5:a50cdaa90d6a | 69 | int AsyncSerial::getc(void){ |
babylonica | 5:a50cdaa90d6a | 70 | return (int)fifo_rx.get(); |
babylonica | 5:a50cdaa90d6a | 71 | } |
babylonica | 5:a50cdaa90d6a | 72 | |
babylonica | 5:a50cdaa90d6a | 73 | int AsyncSerial::peekc(void){ |
babylonica | 5:a50cdaa90d6a | 74 | return (int)fifo_rx.peek(); |
babylonica | 5:a50cdaa90d6a | 75 | } |
babylonica | 5:a50cdaa90d6a | 76 | |
babylonica | 8:7390f9bb28d3 | 77 | void AsyncSerial::putc(int c){ |
babylonica | 5:a50cdaa90d6a | 78 | if( Is_Serial_Sending ){ |
babylonica | 8:7390f9bb28d3 | 79 | fifo_tx.put((uint8_t)c); |
babylonica | 5:a50cdaa90d6a | 80 | }else{ |
babylonica | 5:a50cdaa90d6a | 81 | Is_Serial_Sending = true; |
babylonica | 5:a50cdaa90d6a | 82 | RawSerial::putc(c); |
babylonica | 5:a50cdaa90d6a | 83 | } |
babylonica | 8:7390f9bb28d3 | 84 | return; |
babylonica | 5:a50cdaa90d6a | 85 | } |
babylonica | 5:a50cdaa90d6a | 86 | |
babylonica | 8:7390f9bb28d3 | 87 | void AsyncSerial::puts(const char *str){ |
babylonica | 5:a50cdaa90d6a | 88 | uint8_t temp; |
babylonica | 0:907ac3c2fadc | 89 | |
babylonica | 5:a50cdaa90d6a | 90 | for(uint32_t i = 0; i < strlen(str); i++){ |
babylonica | 5:a50cdaa90d6a | 91 | temp = (uint8_t)str[i]; |
babylonica | 8:7390f9bb28d3 | 92 | fifo_tx.put(temp); |
babylonica | 5:a50cdaa90d6a | 93 | } |
babylonica | 5:a50cdaa90d6a | 94 | |
babylonica | 5:a50cdaa90d6a | 95 | if( !Is_Serial_Sending ){ |
babylonica | 5:a50cdaa90d6a | 96 | Is_Serial_Sending = true; |
babylonica | 5:a50cdaa90d6a | 97 | RawSerial::putc((int)fifo_tx.get()); |
babylonica | 5:a50cdaa90d6a | 98 | } |
babylonica | 6:08e3b1bba0ac | 99 | |
babylonica | 6:08e3b1bba0ac | 100 | AsyncSerial::putc('\r'); |
babylonica | 5:a50cdaa90d6a | 101 | AsyncSerial::putc('\n'); |
babylonica | 8:7390f9bb28d3 | 102 | return; |
babylonica | 5:a50cdaa90d6a | 103 | } |
babylonica | 5:a50cdaa90d6a | 104 | |
babylonica | 5:a50cdaa90d6a | 105 | int AsyncSerial::printf(const char *format, ...){ |
babylonica | 5:a50cdaa90d6a | 106 | int32_t wrote_length = 0; |
babylonica | 5:a50cdaa90d6a | 107 | char string_buffer[PRINTF_STRING_BUFFER_SIZE]; |
babylonica | 5:a50cdaa90d6a | 108 | |
babylonica | 5:a50cdaa90d6a | 109 | memset(string_buffer, 0, PRINTF_STRING_BUFFER_SIZE); |
babylonica | 5:a50cdaa90d6a | 110 | |
babylonica | 5:a50cdaa90d6a | 111 | va_list arg; |
babylonica | 5:a50cdaa90d6a | 112 | va_start(arg, format); |
babylonica | 5:a50cdaa90d6a | 113 | wrote_length = vsprintf(string_buffer, format, arg); |
babylonica | 5:a50cdaa90d6a | 114 | |
babylonica | 5:a50cdaa90d6a | 115 | if( wrote_length > PRINTF_STRING_BUFFER_SIZE ) { |
babylonica | 5:a50cdaa90d6a | 116 | 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 | 117 | va_end(arg); |
babylonica | 5:a50cdaa90d6a | 118 | return 0; |
babylonica | 5:a50cdaa90d6a | 119 | } |
babylonica | 5:a50cdaa90d6a | 120 | |
babylonica | 5:a50cdaa90d6a | 121 | if( wrote_length < 0 ){ |
babylonica | 5:a50cdaa90d6a | 122 | va_end(arg); |
babylonica | 5:a50cdaa90d6a | 123 | error("Function vsprintf() was failed."); |
babylonica | 5:a50cdaa90d6a | 124 | return 0; |
babylonica | 5:a50cdaa90d6a | 125 | } |
babylonica | 5:a50cdaa90d6a | 126 | |
babylonica | 5:a50cdaa90d6a | 127 | va_end(arg); |
babylonica | 8:7390f9bb28d3 | 128 | AsyncSerial::write((uint8_t*)string_buffer, wrote_length); |
babylonica | 5:a50cdaa90d6a | 129 | |
babylonica | 8:7390f9bb28d3 | 130 | return wrote_length; |
babylonica | 5:a50cdaa90d6a | 131 | } |
babylonica | 5:a50cdaa90d6a | 132 | |
babylonica | 5:a50cdaa90d6a | 133 | int AsyncSerial::write(const uint8_t *buffer, int length){ |
babylonica | 5:a50cdaa90d6a | 134 | uint8_t temp; |
babylonica | 5:a50cdaa90d6a | 135 | |
babylonica | 5:a50cdaa90d6a | 136 | if ( length < 1 ){ |
babylonica | 5:a50cdaa90d6a | 137 | return 0; |
babylonica | 5:a50cdaa90d6a | 138 | } |
babylonica | 5:a50cdaa90d6a | 139 | |
babylonica | 5:a50cdaa90d6a | 140 | for(uint32_t i = 0; i < length; i++){ |
babylonica | 5:a50cdaa90d6a | 141 | temp = (uint8_t)buffer[i]; |
babylonica | 8:7390f9bb28d3 | 142 | fifo_tx.put(temp); |
babylonica | 5:a50cdaa90d6a | 143 | } |
babylonica | 5:a50cdaa90d6a | 144 | |
babylonica | 5:a50cdaa90d6a | 145 | if( !Is_Serial_Sending ){ |
babylonica | 5:a50cdaa90d6a | 146 | Is_Serial_Sending = true; |
babylonica | 5:a50cdaa90d6a | 147 | RawSerial::putc((int)fifo_tx.get()); |
babylonica | 5:a50cdaa90d6a | 148 | } |
babylonica | 5:a50cdaa90d6a | 149 | |
babylonica | 8:7390f9bb28d3 | 150 | return 1; |
babylonica | 5:a50cdaa90d6a | 151 | } |
babylonica | 5:a50cdaa90d6a | 152 | |
babylonica | 5:a50cdaa90d6a | 153 | void AsyncSerial::flush(void){ |
babylonica | 5:a50cdaa90d6a | 154 | fifo_rx.clear(); |
babylonica | 5:a50cdaa90d6a | 155 | return; |
babylonica | 5:a50cdaa90d6a | 156 | } |
babylonica | 5:a50cdaa90d6a | 157 | |
babylonica | 5:a50cdaa90d6a | 158 | void AsyncSerial::wait(void){ |
babylonica | 9:709719dddd3e | 159 | while( fifo_tx.available() > 0 ){} |
babylonica | 5:a50cdaa90d6a | 160 | return; |
babylonica | 5:a50cdaa90d6a | 161 | } |
babylonica | 5:a50cdaa90d6a | 162 | |
babylonica | 5:a50cdaa90d6a | 163 | void AsyncSerial::format(int bits, Parity parity, int stop_bits){ |
babylonica | 5:a50cdaa90d6a | 164 | RawSerial::attach(NULL, RawSerial::TxIrq); |
babylonica | 5:a50cdaa90d6a | 165 | RawSerial::attach(NULL, RawSerial::RxIrq); |
babylonica | 5:a50cdaa90d6a | 166 | |
babylonica | 5:a50cdaa90d6a | 167 | RawSerial::format(bits, parity, stop_bits); |
babylonica | 5:a50cdaa90d6a | 168 | |
babylonica | 5:a50cdaa90d6a | 169 | RawSerial::attach(this, &AsyncSerial::ISR_TX, RawSerial::TxIrq); |
babylonica | 5:a50cdaa90d6a | 170 | RawSerial::attach(this, &AsyncSerial::ISR_RX, RawSerial::RxIrq); |
babylonica | 5:a50cdaa90d6a | 171 | } |
babylonica | 5:a50cdaa90d6a | 172 | |
babylonica | 5:a50cdaa90d6a | 173 | void AsyncSerial::baud(int baudrate){ |
babylonica | 5:a50cdaa90d6a | 174 | RawSerial::attach(NULL, RawSerial::TxIrq); |
babylonica | 5:a50cdaa90d6a | 175 | RawSerial::attach(NULL, RawSerial::RxIrq); |
babylonica | 5:a50cdaa90d6a | 176 | |
babylonica | 5:a50cdaa90d6a | 177 | RawSerial::baud(baudrate); |
babylonica | 5:a50cdaa90d6a | 178 | |
babylonica | 5:a50cdaa90d6a | 179 | RawSerial::attach(this, &AsyncSerial::ISR_TX, RawSerial::TxIrq); |
babylonica | 5:a50cdaa90d6a | 180 | RawSerial::attach(this, &AsyncSerial::ISR_RX, RawSerial::RxIrq); |
babylonica | 5:a50cdaa90d6a | 181 | } |
babylonica | 5:a50cdaa90d6a | 182 |