![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
OneNet_IoT_demo for ASC platform
Dependencies: Common_lib ESP8266 EdpKit_lib cJSON_lib driver_mbed_HP20x driver_mbed_TH02 wifi_example
Fork of mbed-os-example-esp8266 by
esp8266-driver/ESP8266/ATParser/BufferedSerial/BufferedSerial.cpp@1:b4a718e62e0b, 2017-01-12 (annotated)
- Committer:
- sarahmarshy
- Date:
- Thu Jan 12 22:05:15 2017 +0000
- Revision:
- 1:b4a718e62e0b
- Parent:
- 0:b887535f68bf
Update esp8266-driver
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sarahmarshy | 1:b4a718e62e0b | 1 | /** |
sarahmarshy | 1:b4a718e62e0b | 2 | * @file BufferedSerial.cpp |
sarahmarshy | 1:b4a718e62e0b | 3 | * @brief Software Buffer - Extends mbed Serial functionallity adding irq driven TX and RX |
sarahmarshy | 1:b4a718e62e0b | 4 | * @author sam grove |
sarahmarshy | 1:b4a718e62e0b | 5 | * @version 1.0 |
sarahmarshy | 1:b4a718e62e0b | 6 | * @see |
sarahmarshy | 1:b4a718e62e0b | 7 | * |
sarahmarshy | 1:b4a718e62e0b | 8 | * Copyright (c) 2013 |
sarahmarshy | 1:b4a718e62e0b | 9 | * |
sarahmarshy | 1:b4a718e62e0b | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sarahmarshy | 1:b4a718e62e0b | 11 | * you may not use this file except in compliance with the License. |
sarahmarshy | 1:b4a718e62e0b | 12 | * You may obtain a copy of the License at |
sarahmarshy | 1:b4a718e62e0b | 13 | * |
sarahmarshy | 1:b4a718e62e0b | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
sarahmarshy | 1:b4a718e62e0b | 15 | * |
sarahmarshy | 1:b4a718e62e0b | 16 | * Unless required by applicable law or agreed to in writing, software |
sarahmarshy | 1:b4a718e62e0b | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
sarahmarshy | 1:b4a718e62e0b | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sarahmarshy | 1:b4a718e62e0b | 19 | * See the License for the specific language governing permissions and |
sarahmarshy | 1:b4a718e62e0b | 20 | * limitations under the License. |
sarahmarshy | 1:b4a718e62e0b | 21 | */ |
sarahmarshy | 1:b4a718e62e0b | 22 | |
sarahmarshy | 1:b4a718e62e0b | 23 | #include "BufferedSerial.h" |
sarahmarshy | 1:b4a718e62e0b | 24 | #include <stdarg.h> |
sarahmarshy | 1:b4a718e62e0b | 25 | |
sarahmarshy | 1:b4a718e62e0b | 26 | extern "C" int BufferedPrintfC(void *stream, int size, const char* format, va_list arg); |
sarahmarshy | 1:b4a718e62e0b | 27 | |
sarahmarshy | 1:b4a718e62e0b | 28 | BufferedSerial::BufferedSerial(PinName tx, PinName rx, uint32_t buf_size, uint32_t tx_multiple, const char* name) |
sarahmarshy | 1:b4a718e62e0b | 29 | : RawSerial(tx, rx) , _rxbuf(buf_size), _txbuf((uint32_t)(tx_multiple*buf_size)) |
sarahmarshy | 1:b4a718e62e0b | 30 | { |
sarahmarshy | 1:b4a718e62e0b | 31 | RawSerial::attach(this, &BufferedSerial::rxIrq, Serial::RxIrq); |
sarahmarshy | 1:b4a718e62e0b | 32 | this->_buf_size = buf_size; |
sarahmarshy | 1:b4a718e62e0b | 33 | this->_tx_multiple = tx_multiple; |
sarahmarshy | 1:b4a718e62e0b | 34 | return; |
sarahmarshy | 1:b4a718e62e0b | 35 | } |
sarahmarshy | 1:b4a718e62e0b | 36 | |
sarahmarshy | 1:b4a718e62e0b | 37 | BufferedSerial::~BufferedSerial(void) |
sarahmarshy | 1:b4a718e62e0b | 38 | { |
sarahmarshy | 1:b4a718e62e0b | 39 | RawSerial::attach(NULL, RawSerial::RxIrq); |
sarahmarshy | 1:b4a718e62e0b | 40 | RawSerial::attach(NULL, RawSerial::TxIrq); |
sarahmarshy | 1:b4a718e62e0b | 41 | |
sarahmarshy | 1:b4a718e62e0b | 42 | return; |
sarahmarshy | 1:b4a718e62e0b | 43 | } |
sarahmarshy | 1:b4a718e62e0b | 44 | |
sarahmarshy | 1:b4a718e62e0b | 45 | int BufferedSerial::readable(void) |
sarahmarshy | 1:b4a718e62e0b | 46 | { |
sarahmarshy | 1:b4a718e62e0b | 47 | return _rxbuf.available(); // note: look if things are in the buffer |
sarahmarshy | 1:b4a718e62e0b | 48 | } |
sarahmarshy | 1:b4a718e62e0b | 49 | |
sarahmarshy | 1:b4a718e62e0b | 50 | int BufferedSerial::writeable(void) |
sarahmarshy | 1:b4a718e62e0b | 51 | { |
sarahmarshy | 1:b4a718e62e0b | 52 | return 1; // buffer allows overwriting by design, always true |
sarahmarshy | 1:b4a718e62e0b | 53 | } |
sarahmarshy | 1:b4a718e62e0b | 54 | |
sarahmarshy | 1:b4a718e62e0b | 55 | int BufferedSerial::getc(void) |
sarahmarshy | 1:b4a718e62e0b | 56 | { |
sarahmarshy | 1:b4a718e62e0b | 57 | return _rxbuf; |
sarahmarshy | 1:b4a718e62e0b | 58 | } |
sarahmarshy | 1:b4a718e62e0b | 59 | |
sarahmarshy | 1:b4a718e62e0b | 60 | int BufferedSerial::putc(int c) |
sarahmarshy | 1:b4a718e62e0b | 61 | { |
sarahmarshy | 1:b4a718e62e0b | 62 | _txbuf = (char)c; |
sarahmarshy | 1:b4a718e62e0b | 63 | BufferedSerial::prime(); |
sarahmarshy | 1:b4a718e62e0b | 64 | |
sarahmarshy | 1:b4a718e62e0b | 65 | return c; |
sarahmarshy | 1:b4a718e62e0b | 66 | } |
sarahmarshy | 1:b4a718e62e0b | 67 | |
sarahmarshy | 1:b4a718e62e0b | 68 | int BufferedSerial::puts(const char *s) |
sarahmarshy | 1:b4a718e62e0b | 69 | { |
sarahmarshy | 1:b4a718e62e0b | 70 | if (s != NULL) { |
sarahmarshy | 1:b4a718e62e0b | 71 | const char* ptr = s; |
sarahmarshy | 1:b4a718e62e0b | 72 | |
sarahmarshy | 1:b4a718e62e0b | 73 | while(*(ptr) != 0) { |
sarahmarshy | 1:b4a718e62e0b | 74 | _txbuf = *(ptr++); |
sarahmarshy | 1:b4a718e62e0b | 75 | } |
sarahmarshy | 1:b4a718e62e0b | 76 | _txbuf = '\n'; // done per puts definition |
sarahmarshy | 1:b4a718e62e0b | 77 | BufferedSerial::prime(); |
sarahmarshy | 1:b4a718e62e0b | 78 | |
sarahmarshy | 1:b4a718e62e0b | 79 | return (ptr - s) + 1; |
sarahmarshy | 1:b4a718e62e0b | 80 | } |
sarahmarshy | 1:b4a718e62e0b | 81 | return 0; |
sarahmarshy | 1:b4a718e62e0b | 82 | } |
sarahmarshy | 1:b4a718e62e0b | 83 | |
sarahmarshy | 1:b4a718e62e0b | 84 | extern "C" size_t BufferedSerialThunk(void *buf_serial, const void *s, size_t length) |
sarahmarshy | 1:b4a718e62e0b | 85 | { |
sarahmarshy | 1:b4a718e62e0b | 86 | BufferedSerial *buffered_serial = (BufferedSerial *)buf_serial; |
sarahmarshy | 1:b4a718e62e0b | 87 | return buffered_serial->write(s, length); |
sarahmarshy | 1:b4a718e62e0b | 88 | } |
sarahmarshy | 1:b4a718e62e0b | 89 | |
sarahmarshy | 1:b4a718e62e0b | 90 | int BufferedSerial::printf(const char* format, ...) |
sarahmarshy | 1:b4a718e62e0b | 91 | { |
sarahmarshy | 1:b4a718e62e0b | 92 | va_list arg; |
sarahmarshy | 1:b4a718e62e0b | 93 | va_start(arg, format); |
sarahmarshy | 1:b4a718e62e0b | 94 | int r = BufferedPrintfC((void*)this, this->_buf_size, format, arg); |
sarahmarshy | 1:b4a718e62e0b | 95 | va_end(arg); |
sarahmarshy | 1:b4a718e62e0b | 96 | return r; |
sarahmarshy | 1:b4a718e62e0b | 97 | } |
sarahmarshy | 1:b4a718e62e0b | 98 | |
sarahmarshy | 1:b4a718e62e0b | 99 | ssize_t BufferedSerial::write(const void *s, size_t length) |
sarahmarshy | 1:b4a718e62e0b | 100 | { |
sarahmarshy | 1:b4a718e62e0b | 101 | if (s != NULL && length > 0) { |
sarahmarshy | 1:b4a718e62e0b | 102 | const char* ptr = (const char*)s; |
sarahmarshy | 1:b4a718e62e0b | 103 | const char* end = ptr + length; |
sarahmarshy | 1:b4a718e62e0b | 104 | |
sarahmarshy | 1:b4a718e62e0b | 105 | while (ptr != end) { |
sarahmarshy | 1:b4a718e62e0b | 106 | _txbuf = *(ptr++); |
sarahmarshy | 1:b4a718e62e0b | 107 | } |
sarahmarshy | 1:b4a718e62e0b | 108 | BufferedSerial::prime(); |
sarahmarshy | 1:b4a718e62e0b | 109 | |
sarahmarshy | 1:b4a718e62e0b | 110 | return ptr - (const char*)s; |
sarahmarshy | 1:b4a718e62e0b | 111 | } |
sarahmarshy | 1:b4a718e62e0b | 112 | return 0; |
sarahmarshy | 1:b4a718e62e0b | 113 | } |
sarahmarshy | 1:b4a718e62e0b | 114 | |
sarahmarshy | 1:b4a718e62e0b | 115 | |
sarahmarshy | 1:b4a718e62e0b | 116 | void BufferedSerial::rxIrq(void) |
sarahmarshy | 1:b4a718e62e0b | 117 | { |
sarahmarshy | 1:b4a718e62e0b | 118 | // read from the peripheral and make sure something is available |
sarahmarshy | 1:b4a718e62e0b | 119 | if(serial_readable(&_serial)) { |
sarahmarshy | 1:b4a718e62e0b | 120 | _rxbuf = serial_getc(&_serial); // if so load them into a buffer |
sarahmarshy | 1:b4a718e62e0b | 121 | // trigger callback if necessary |
sarahmarshy | 1:b4a718e62e0b | 122 | if (_cbs[RxIrq]) { |
sarahmarshy | 1:b4a718e62e0b | 123 | _cbs[RxIrq](); |
sarahmarshy | 1:b4a718e62e0b | 124 | } |
sarahmarshy | 1:b4a718e62e0b | 125 | } |
sarahmarshy | 1:b4a718e62e0b | 126 | |
sarahmarshy | 1:b4a718e62e0b | 127 | return; |
sarahmarshy | 1:b4a718e62e0b | 128 | } |
sarahmarshy | 1:b4a718e62e0b | 129 | |
sarahmarshy | 1:b4a718e62e0b | 130 | void BufferedSerial::txIrq(void) |
sarahmarshy | 1:b4a718e62e0b | 131 | { |
sarahmarshy | 1:b4a718e62e0b | 132 | // see if there is room in the hardware fifo and if something is in the software fifo |
sarahmarshy | 1:b4a718e62e0b | 133 | while(serial_writable(&_serial)) { |
sarahmarshy | 1:b4a718e62e0b | 134 | if(_txbuf.available()) { |
sarahmarshy | 1:b4a718e62e0b | 135 | serial_putc(&_serial, (int)_txbuf.get()); |
sarahmarshy | 1:b4a718e62e0b | 136 | } else { |
sarahmarshy | 1:b4a718e62e0b | 137 | // disable the TX interrupt when there is nothing left to send |
sarahmarshy | 1:b4a718e62e0b | 138 | RawSerial::attach(NULL, RawSerial::TxIrq); |
sarahmarshy | 1:b4a718e62e0b | 139 | // trigger callback if necessary |
sarahmarshy | 1:b4a718e62e0b | 140 | if (_cbs[TxIrq]) { |
sarahmarshy | 1:b4a718e62e0b | 141 | _cbs[TxIrq](); |
sarahmarshy | 1:b4a718e62e0b | 142 | } |
sarahmarshy | 1:b4a718e62e0b | 143 | break; |
sarahmarshy | 1:b4a718e62e0b | 144 | } |
sarahmarshy | 1:b4a718e62e0b | 145 | } |
sarahmarshy | 1:b4a718e62e0b | 146 | |
sarahmarshy | 1:b4a718e62e0b | 147 | return; |
sarahmarshy | 1:b4a718e62e0b | 148 | } |
sarahmarshy | 1:b4a718e62e0b | 149 | |
sarahmarshy | 1:b4a718e62e0b | 150 | void BufferedSerial::prime(void) |
sarahmarshy | 1:b4a718e62e0b | 151 | { |
sarahmarshy | 1:b4a718e62e0b | 152 | // if already busy then the irq will pick this up |
sarahmarshy | 1:b4a718e62e0b | 153 | if(serial_writable(&_serial)) { |
sarahmarshy | 1:b4a718e62e0b | 154 | RawSerial::attach(NULL, RawSerial::TxIrq); // make sure not to cause contention in the irq |
sarahmarshy | 1:b4a718e62e0b | 155 | BufferedSerial::txIrq(); // only write to hardware in one place |
sarahmarshy | 1:b4a718e62e0b | 156 | RawSerial::attach(this, &BufferedSerial::txIrq, RawSerial::TxIrq); |
sarahmarshy | 1:b4a718e62e0b | 157 | } |
sarahmarshy | 1:b4a718e62e0b | 158 | |
sarahmarshy | 1:b4a718e62e0b | 159 | return; |
sarahmarshy | 1:b4a718e62e0b | 160 | } |
sarahmarshy | 1:b4a718e62e0b | 161 | |
sarahmarshy | 1:b4a718e62e0b | 162 | void BufferedSerial::attach(Callback<void()> func, IrqType type) |
sarahmarshy | 1:b4a718e62e0b | 163 | { |
sarahmarshy | 1:b4a718e62e0b | 164 | _cbs[type] = func; |
sarahmarshy | 1:b4a718e62e0b | 165 | } |
sarahmarshy | 1:b4a718e62e0b | 166 |