tetete
AsyncSerial.cpp@0:907ac3c2fadc, 2017-03-30 (annotated)
- Committer:
- babylonica
- Date:
- Thu Mar 30 02:18:24 2017 +0000
- Revision:
- 0:907ac3c2fadc
- Child:
- 1:d3af33dfc87d
First Commit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
babylonica | 0:907ac3c2fadc | 1 | // -*- coding: utf-8 -*- |
babylonica | 0:907ac3c2fadc | 2 | /** |
babylonica | 0:907ac3c2fadc | 3 | @file AsyncSerial.cpp |
babylonica | 0:907ac3c2fadc | 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 | 0:907ac3c2fadc | 5 | |
babylonica | 0:907ac3c2fadc | 6 | @author T.Kawamura |
babylonica | 0:907ac3c2fadc | 7 | @version 1.0 |
babylonica | 0:907ac3c2fadc | 8 | @date 2017-03-29 T.Kawamura Written for C++/mbed. |
babylonica | 0:907ac3c2fadc | 9 | |
babylonica | 0:907ac3c2fadc | 10 | @see |
babylonica | 0:907ac3c2fadc | 11 | Copyright (C) 2017 T.Kawamura. |
babylonica | 0:907ac3c2fadc | 12 | Released under the MIT license. |
babylonica | 0:907ac3c2fadc | 13 | http://opensource.org/licenses/mit-license.php |
babylonica | 0:907ac3c2fadc | 14 | |
babylonica | 0:907ac3c2fadc | 15 | */ |
babylonica | 0:907ac3c2fadc | 16 | |
babylonica | 0:907ac3c2fadc | 17 | #include "AsyncSerial.hpp" |
babylonica | 0:907ac3c2fadc | 18 | |
babylonica | 0:907ac3c2fadc | 19 | 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 | 0:907ac3c2fadc | 20 | //Initialize ISR |
babylonica | 0:907ac3c2fadc | 21 | RawSerial::attach(this, &AsyncSerial::ISR_TX, RawSerial::TxIrq); |
babylonica | 0:907ac3c2fadc | 22 | RawSerial::attach(this, &AsyncSerial::ISR_RX, RawSerial::RxIrq); |
babylonica | 0:907ac3c2fadc | 23 | |
babylonica | 0:907ac3c2fadc | 24 | fifo_tx.clear(); |
babylonica | 0:907ac3c2fadc | 25 | fifo_rx.clear(); |
babylonica | 0:907ac3c2fadc | 26 | |
babylonica | 0:907ac3c2fadc | 27 | Is_Serial_Sending = false; |
babylonica | 0:907ac3c2fadc | 28 | |
babylonica | 0:907ac3c2fadc | 29 | return; |
babylonica | 0:907ac3c2fadc | 30 | } |
babylonica | 0:907ac3c2fadc | 31 | |
babylonica | 0:907ac3c2fadc | 32 | AsyncSerial::~AsyncSerial(){ |
babylonica | 0:907ac3c2fadc | 33 | RawSerial::attach(NULL, RawSerial::TxIrq); |
babylonica | 0:907ac3c2fadc | 34 | RawSerial::attach(NULL, RawSerial::RxIrq); |
babylonica | 0:907ac3c2fadc | 35 | |
babylonica | 0:907ac3c2fadc | 36 | return; |
babylonica | 0:907ac3c2fadc | 37 | } |
babylonica | 0:907ac3c2fadc | 38 | |
babylonica | 0:907ac3c2fadc | 39 | void AsyncSerial::ISR_TX(void){ |
babylonica | 0:907ac3c2fadc | 40 | int data; |
babylonica | 0:907ac3c2fadc | 41 | |
babylonica | 0:907ac3c2fadc | 42 | if( fifo_tx.available() > 0 ){ |
babylonica | 0:907ac3c2fadc | 43 | data = (int)fifo_tx.get(); |
babylonica | 0:907ac3c2fadc | 44 | RawSerial::putc(data); |
babylonica | 0:907ac3c2fadc | 45 | }else{ |
babylonica | 0:907ac3c2fadc | 46 | Is_Serial_Sending = false; |
babylonica | 0:907ac3c2fadc | 47 | } |
babylonica | 0:907ac3c2fadc | 48 | } |
babylonica | 0:907ac3c2fadc | 49 | |
babylonica | 0:907ac3c2fadc | 50 | void AsyncSerial::ISR_RX(void){ |
babylonica | 0:907ac3c2fadc | 51 | uint8_t data; |
babylonica | 0:907ac3c2fadc | 52 | |
babylonica | 0:907ac3c2fadc | 53 | data = (uint8_t)RawSerial::getc(); |
babylonica | 0:907ac3c2fadc | 54 | fifo_rx.put(data); |
babylonica | 0:907ac3c2fadc | 55 | } |
babylonica | 0:907ac3c2fadc | 56 | |
babylonica | 0:907ac3c2fadc | 57 | uint32_t AsyncSerial::readable(void){ |
babylonica | 0:907ac3c2fadc | 58 | return fifo_rx.available(); |
babylonica | 0:907ac3c2fadc | 59 | } |
babylonica | 0:907ac3c2fadc | 60 | |
babylonica | 0:907ac3c2fadc | 61 | uint8_t AsyncSerial::writeable(void){ |
babylonica | 0:907ac3c2fadc | 62 | return 1; |
babylonica | 0:907ac3c2fadc | 63 | } |
babylonica | 0:907ac3c2fadc | 64 | |
babylonica | 0:907ac3c2fadc | 65 | uint8_t AsyncSerial::getc(void){ |
babylonica | 0:907ac3c2fadc | 66 | return fifo_rx.get(); |
babylonica | 0:907ac3c2fadc | 67 | } |
babylonica | 0:907ac3c2fadc | 68 | |
babylonica | 0:907ac3c2fadc | 69 | uint8_t AsyncSerial::peekc(void){ |
babylonica | 0:907ac3c2fadc | 70 | return fifo_rx.peek(); |
babylonica | 0:907ac3c2fadc | 71 | } |
babylonica | 0:907ac3c2fadc | 72 | |
babylonica | 0:907ac3c2fadc | 73 | uint8_t AsyncSerial::putc(uint8_t data){ |
babylonica | 0:907ac3c2fadc | 74 | uint8_t status; |
babylonica | 0:907ac3c2fadc | 75 | |
babylonica | 0:907ac3c2fadc | 76 | if( Is_Serial_Sending ){ |
babylonica | 0:907ac3c2fadc | 77 | status = fifo_tx.put(data); |
babylonica | 0:907ac3c2fadc | 78 | if( status != 0 ){ |
babylonica | 0:907ac3c2fadc | 79 | return 1; |
babylonica | 0:907ac3c2fadc | 80 | }else{ |
babylonica | 0:907ac3c2fadc | 81 | return 0; |
babylonica | 0:907ac3c2fadc | 82 | } |
babylonica | 0:907ac3c2fadc | 83 | }else{ |
babylonica | 0:907ac3c2fadc | 84 | Is_Serial_Sending = true; |
babylonica | 0:907ac3c2fadc | 85 | RawSerial::putc((int)data); |
babylonica | 0:907ac3c2fadc | 86 | } |
babylonica | 0:907ac3c2fadc | 87 | return 1; |
babylonica | 0:907ac3c2fadc | 88 | } |
babylonica | 0:907ac3c2fadc | 89 | |
babylonica | 0:907ac3c2fadc | 90 | uint8_t AsyncSerial::puts(const char *str){ |
babylonica | 0:907ac3c2fadc | 91 | uint8_t temp, status = 0; |
babylonica | 0:907ac3c2fadc | 92 | |
babylonica | 0:907ac3c2fadc | 93 | for(uint16_t i = 0; i < strlen(str); i++){ |
babylonica | 0:907ac3c2fadc | 94 | temp = (uint8_t)str[i]; |
babylonica | 0:907ac3c2fadc | 95 | status = fifo_tx.put(temp); |
babylonica | 0:907ac3c2fadc | 96 | } |
babylonica | 0:907ac3c2fadc | 97 | |
babylonica | 0:907ac3c2fadc | 98 | if( !Is_Serial_Sending ){ |
babylonica | 0:907ac3c2fadc | 99 | Is_Serial_Sending = true; |
babylonica | 0:907ac3c2fadc | 100 | RawSerial::putc((int)fifo_tx.get()); |
babylonica | 0:907ac3c2fadc | 101 | } |
babylonica | 0:907ac3c2fadc | 102 | |
babylonica | 0:907ac3c2fadc | 103 | if( status == 0 ){ |
babylonica | 0:907ac3c2fadc | 104 | return 0; |
babylonica | 0:907ac3c2fadc | 105 | } |
babylonica | 0:907ac3c2fadc | 106 | |
babylonica | 0:907ac3c2fadc | 107 | AsyncSerial::putc('\n'); |
babylonica | 0:907ac3c2fadc | 108 | return 1; |
babylonica | 0:907ac3c2fadc | 109 | } |
babylonica | 0:907ac3c2fadc | 110 | |
babylonica | 0:907ac3c2fadc | 111 | uint16_t AsyncSerial::printf(const char *format, ...){ |
babylonica | 0:907ac3c2fadc | 112 | int32_t wrote_length = 0; |
babylonica | 0:907ac3c2fadc | 113 | char string_buffer[PRINTF_STRING_BUFFER_SIZE]; |
babylonica | 0:907ac3c2fadc | 114 | |
babylonica | 0:907ac3c2fadc | 115 | memset(string_buffer, 0, PRINTF_STRING_BUFFER_SIZE); |
babylonica | 0:907ac3c2fadc | 116 | |
babylonica | 0:907ac3c2fadc | 117 | va_list arg; |
babylonica | 0:907ac3c2fadc | 118 | va_start(arg, format); |
babylonica | 0:907ac3c2fadc | 119 | wrote_length = vsprintf(string_buffer, format, arg); |
babylonica | 0:907ac3c2fadc | 120 | |
babylonica | 0:907ac3c2fadc | 121 | if( wrote_length > PRINTF_STRING_BUFFER_SIZE ) { |
babylonica | 0:907ac3c2fadc | 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 | 0:907ac3c2fadc | 123 | va_end(arg); |
babylonica | 0:907ac3c2fadc | 124 | return 0; |
babylonica | 0:907ac3c2fadc | 125 | } |
babylonica | 0:907ac3c2fadc | 126 | |
babylonica | 0:907ac3c2fadc | 127 | if( wrote_length < 0 ){ |
babylonica | 0:907ac3c2fadc | 128 | va_end(arg); |
babylonica | 0:907ac3c2fadc | 129 | error("Function vsprintf() was failed."); |
babylonica | 0:907ac3c2fadc | 130 | return 0; |
babylonica | 0:907ac3c2fadc | 131 | } |
babylonica | 0:907ac3c2fadc | 132 | |
babylonica | 0:907ac3c2fadc | 133 | va_end(arg); |
babylonica | 0:907ac3c2fadc | 134 | wrote_length = AsyncSerial::write((uint8_t*)string_buffer, wrote_length); |
babylonica | 0:907ac3c2fadc | 135 | |
babylonica | 0:907ac3c2fadc | 136 | return (uint16_t)wrote_length; |
babylonica | 0:907ac3c2fadc | 137 | } |
babylonica | 0:907ac3c2fadc | 138 | |
babylonica | 0:907ac3c2fadc | 139 | uint8_t AsyncSerial::write(const uint8_t *s, uint16_t length){ |
babylonica | 0:907ac3c2fadc | 140 | uint8_t temp, status; |
babylonica | 0:907ac3c2fadc | 141 | |
babylonica | 0:907ac3c2fadc | 142 | if ( length < 1 ){ |
babylonica | 0:907ac3c2fadc | 143 | return 0; |
babylonica | 0:907ac3c2fadc | 144 | } |
babylonica | 0:907ac3c2fadc | 145 | |
babylonica | 0:907ac3c2fadc | 146 | for(uint16_t i = 0; i < length; i++){ |
babylonica | 0:907ac3c2fadc | 147 | temp = (uint8_t)s[i]; |
babylonica | 0:907ac3c2fadc | 148 | status = fifo_tx.put(temp); |
babylonica | 0:907ac3c2fadc | 149 | } |
babylonica | 0:907ac3c2fadc | 150 | |
babylonica | 0:907ac3c2fadc | 151 | if( !Is_Serial_Sending ){ |
babylonica | 0:907ac3c2fadc | 152 | Is_Serial_Sending = true; |
babylonica | 0:907ac3c2fadc | 153 | RawSerial::putc((int)fifo_tx.get()); |
babylonica | 0:907ac3c2fadc | 154 | } |
babylonica | 0:907ac3c2fadc | 155 | |
babylonica | 0:907ac3c2fadc | 156 | if( status == 0 ){ |
babylonica | 0:907ac3c2fadc | 157 | return 0; |
babylonica | 0:907ac3c2fadc | 158 | } |
babylonica | 0:907ac3c2fadc | 159 | |
babylonica | 0:907ac3c2fadc | 160 | return 1; |
babylonica | 0:907ac3c2fadc | 161 | } |
babylonica | 0:907ac3c2fadc | 162 | |
babylonica | 0:907ac3c2fadc | 163 | void AsyncSerial::flush(void){ |
babylonica | 0:907ac3c2fadc | 164 | fifo_rx.clear(); |
babylonica | 0:907ac3c2fadc | 165 | return; |
babylonica | 0:907ac3c2fadc | 166 | } |
babylonica | 0:907ac3c2fadc | 167 | |
babylonica | 0:907ac3c2fadc | 168 | void AsyncSerial::wait(void){ |
babylonica | 0:907ac3c2fadc | 169 | while( fifo_rx.available() > 0 ){} |
babylonica | 0:907ac3c2fadc | 170 | return; |
babylonica | 0:907ac3c2fadc | 171 | } |
babylonica | 0:907ac3c2fadc | 172 |