Added support for WNC M14A2A Cellular LTE Data Module.

Dependencies:   WNC14A2AInterface

Dependents:   http-example-wnc http-example-wnc-modified

Committer:
root@developer-sjc-cyan-compiler.local.mbed.org
Date:
Sun Apr 23 18:40:51 2017 +0000
Revision:
5:391eac6a0a94
Parent:
0:2563b0415d1f
Added tag att_cellular_K64_wnc_14A2A_20170423 for changeset daf182af022b

Who changed what in which revision?

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