The driver for the ESP8266 WiFi module - isConnected function added, working and tested with mbed os 5.7.6

Fork of esp8266-driver by ESP8266

Committer:
mareszym
Date:
Sun Nov 04 12:50:49 2018 +0000
Revision:
2:0bc0b654995c
Parent:
0:6946b0b9e323
isConnected function added

Who changed what in which revision?

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