ATParser for EMW3162 wifi module

Dependents:   EMWConfig

Committer:
Maggie17
Date:
Wed Nov 02 05:28:49 2016 +0000
Revision:
1:47dd607c7707
Parent:
0:1b9f0bcf0b2b
first release

Who changed what in which revision?

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