test

Dependencies:   mbed Watchdog stm32-sensor-base2

Committer:
ruslanbredun
Date:
Fri Nov 05 14:39:08 2021 +0000
Revision:
19:7935ca386e13
Parent:
11:32eeb052cda5
LastFirmWare05/11/21;

Who changed what in which revision?

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