Library for Bluetooth Low Energy Module ble 4.0 HM-11

Committer:
igbt6
Date:
Thu Feb 25 02:44:30 2016 +0000
Revision:
7:aa4675590203
Parent:
5:9a00e7bb0275
more functions implemented

Who changed what in which revision?

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