Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Test_SoftSerial_for_L432KC Test_SoftSerial
BufferedSoftSerial.cpp@12:7ab9ab7210e7, 2020-05-12 (annotated)
- Committer:
- kenjiArai
- Date:
- Tue May 12 05:28:37 2020 +0000
- Revision:
- 12:7ab9ab7210e7
- Parent:
- 11:f186cc69acdf
change buffer size
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Sissors | 6:4e3e18b41ea6 | 1 | /** |
| Sissors | 6:4e3e18b41ea6 | 2 | * @file BufferedSoftSerial.cpp |
| kenjiArai | 11:f186cc69acdf | 3 | * @brief Software Buffer |
| kenjiArai | 11:f186cc69acdf | 4 | * - Extends mbed Serial functionallity adding irq driven TX and RX |
| Sissors | 6:4e3e18b41ea6 | 5 | * @author sam grove |
| Sissors | 6:4e3e18b41ea6 | 6 | * @version 1.0 |
| Sissors | 6:4e3e18b41ea6 | 7 | * @see |
| Sissors | 6:4e3e18b41ea6 | 8 | * |
| Sissors | 6:4e3e18b41ea6 | 9 | * Copyright (c) 2013 |
| Sissors | 6:4e3e18b41ea6 | 10 | * |
| Sissors | 6:4e3e18b41ea6 | 11 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| Sissors | 6:4e3e18b41ea6 | 12 | * you may not use this file except in compliance with the License. |
| Sissors | 6:4e3e18b41ea6 | 13 | * You may obtain a copy of the License at |
| Sissors | 6:4e3e18b41ea6 | 14 | * |
| Sissors | 6:4e3e18b41ea6 | 15 | * http://www.apache.org/licenses/LICENSE-2.0 |
| Sissors | 6:4e3e18b41ea6 | 16 | * |
| Sissors | 6:4e3e18b41ea6 | 17 | * Unless required by applicable law or agreed to in writing, software |
| Sissors | 6:4e3e18b41ea6 | 18 | * distributed under the License is distributed on an "AS IS" BASIS, |
| Sissors | 6:4e3e18b41ea6 | 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| Sissors | 6:4e3e18b41ea6 | 20 | * See the License for the specific language governing permissions and |
| Sissors | 6:4e3e18b41ea6 | 21 | * limitations under the License. |
| Sissors | 6:4e3e18b41ea6 | 22 | */ |
| Sissors | 6:4e3e18b41ea6 | 23 | |
| kenjiArai | 12:7ab9ab7210e7 | 24 | // Modified by K.Arai / JH1PJL May 12th, 2020 |
| kenjiArai | 11:f186cc69acdf | 25 | |
| Sissors | 6:4e3e18b41ea6 | 26 | #include "BufferedSoftSerial.h" |
| Sissors | 6:4e3e18b41ea6 | 27 | #include <stdarg.h> |
| Sissors | 6:4e3e18b41ea6 | 28 | |
| Sissors | 6:4e3e18b41ea6 | 29 | BufferedSoftSerial::BufferedSoftSerial(PinName tx, PinName rx, const char* name) |
| Sissors | 6:4e3e18b41ea6 | 30 | : SoftSerial(tx, rx, name) |
| Sissors | 6:4e3e18b41ea6 | 31 | { |
| kenjiArai | 11:f186cc69acdf | 32 | _rxbuf.reset(); |
| kenjiArai | 11:f186cc69acdf | 33 | _txbuf.reset(); |
| kenjiArai | 11:f186cc69acdf | 34 | SoftSerial::attach(this, &BufferedSoftSerial::rx_Irq, SoftSerial::RxIrq); |
| Sissors | 6:4e3e18b41ea6 | 35 | return; |
| Sissors | 6:4e3e18b41ea6 | 36 | } |
| Sissors | 6:4e3e18b41ea6 | 37 | |
| Sissors | 6:4e3e18b41ea6 | 38 | int BufferedSoftSerial::readable(void) |
| Sissors | 6:4e3e18b41ea6 | 39 | { |
| kenjiArai | 11:f186cc69acdf | 40 | return !_rxbuf.empty(); |
| Sissors | 6:4e3e18b41ea6 | 41 | } |
| Sissors | 6:4e3e18b41ea6 | 42 | |
| Sissors | 6:4e3e18b41ea6 | 43 | int BufferedSoftSerial::writeable(void) |
| Sissors | 6:4e3e18b41ea6 | 44 | { |
| kenjiArai | 11:f186cc69acdf | 45 | return _txbuf.full(); |
| Sissors | 6:4e3e18b41ea6 | 46 | } |
| Sissors | 6:4e3e18b41ea6 | 47 | |
| Sissors | 6:4e3e18b41ea6 | 48 | int BufferedSoftSerial::getc(void) |
| Sissors | 6:4e3e18b41ea6 | 49 | { |
| kenjiArai | 11:f186cc69acdf | 50 | char c; |
| kenjiArai | 11:f186cc69acdf | 51 | while(_rxbuf.empty()) { |
| kenjiArai | 11:f186cc69acdf | 52 | ; |
| kenjiArai | 11:f186cc69acdf | 53 | } |
| kenjiArai | 11:f186cc69acdf | 54 | _rxbuf.pop(c); |
| kenjiArai | 11:f186cc69acdf | 55 | return c; |
| Sissors | 6:4e3e18b41ea6 | 56 | } |
| Sissors | 6:4e3e18b41ea6 | 57 | |
| Sissors | 6:4e3e18b41ea6 | 58 | int BufferedSoftSerial::putc(int c) |
| Sissors | 6:4e3e18b41ea6 | 59 | { |
| kenjiArai | 11:f186cc69acdf | 60 | _txbuf.push((char)c); |
| kenjiArai | 11:f186cc69acdf | 61 | prime(); |
| Sissors | 6:4e3e18b41ea6 | 62 | return c; |
| Sissors | 6:4e3e18b41ea6 | 63 | } |
| Sissors | 6:4e3e18b41ea6 | 64 | |
| Sissors | 6:4e3e18b41ea6 | 65 | int BufferedSoftSerial::puts(const char *s) |
| Sissors | 6:4e3e18b41ea6 | 66 | { |
| Sissors | 6:4e3e18b41ea6 | 67 | const char* ptr = s; |
| Sissors | 6:4e3e18b41ea6 | 68 | |
| Sissors | 6:4e3e18b41ea6 | 69 | while(*(ptr) != 0) { |
| kenjiArai | 11:f186cc69acdf | 70 | _txbuf.push(*(ptr++)); |
| Sissors | 6:4e3e18b41ea6 | 71 | } |
| kenjiArai | 11:f186cc69acdf | 72 | #if 0 |
| kenjiArai | 11:f186cc69acdf | 73 | _txbuf.push('\n'); // done per puts definition |
| kenjiArai | 11:f186cc69acdf | 74 | #endif |
| kenjiArai | 11:f186cc69acdf | 75 | prime(); |
| Sissors | 6:4e3e18b41ea6 | 76 | return (ptr - s) + 1; |
| Sissors | 6:4e3e18b41ea6 | 77 | } |
| Sissors | 6:4e3e18b41ea6 | 78 | |
| Sissors | 6:4e3e18b41ea6 | 79 | int BufferedSoftSerial::printf(const char* format, ...) |
| Sissors | 6:4e3e18b41ea6 | 80 | { |
| Sissors | 6:4e3e18b41ea6 | 81 | char buf[256] = {0}; |
| Sissors | 6:4e3e18b41ea6 | 82 | int r = 0; |
| Sissors | 6:4e3e18b41ea6 | 83 | |
| Sissors | 6:4e3e18b41ea6 | 84 | va_list arg; |
| Sissors | 6:4e3e18b41ea6 | 85 | va_start(arg, format); |
| Sissors | 6:4e3e18b41ea6 | 86 | r = vsprintf(buf, format, arg); |
| Sissors | 6:4e3e18b41ea6 | 87 | // this may not hit the heap but should alert the user anyways |
| Sissors | 6:4e3e18b41ea6 | 88 | if(r > sizeof(buf)) { |
| Sissors | 6:4e3e18b41ea6 | 89 | error("%s %d buffer overwrite!\n", __FILE__, __LINE__); |
| Sissors | 6:4e3e18b41ea6 | 90 | } |
| Sissors | 6:4e3e18b41ea6 | 91 | va_end(arg); |
| kenjiArai | 11:f186cc69acdf | 92 | r = write(buf, r); |
| Sissors | 6:4e3e18b41ea6 | 93 | return r; |
| Sissors | 6:4e3e18b41ea6 | 94 | } |
| Sissors | 6:4e3e18b41ea6 | 95 | |
| Sissors | 6:4e3e18b41ea6 | 96 | ssize_t BufferedSoftSerial::write(const void *s, size_t length) |
| Sissors | 6:4e3e18b41ea6 | 97 | { |
| Sissors | 6:4e3e18b41ea6 | 98 | const char* ptr = (const char*)s; |
| Sissors | 6:4e3e18b41ea6 | 99 | const char* end = ptr + length; |
| Sissors | 6:4e3e18b41ea6 | 100 | |
| Sissors | 6:4e3e18b41ea6 | 101 | while (ptr != end) { |
| kenjiArai | 11:f186cc69acdf | 102 | _txbuf.push(*(ptr++)); |
| Sissors | 6:4e3e18b41ea6 | 103 | } |
| kenjiArai | 11:f186cc69acdf | 104 | prime(); |
| Sissors | 6:4e3e18b41ea6 | 105 | return ptr - (const char*)s; |
| Sissors | 6:4e3e18b41ea6 | 106 | } |
| Sissors | 6:4e3e18b41ea6 | 107 | |
| Sissors | 6:4e3e18b41ea6 | 108 | |
| kenjiArai | 11:f186cc69acdf | 109 | void BufferedSoftSerial::rx_Irq(void) |
| Sissors | 6:4e3e18b41ea6 | 110 | { |
| Sissors | 6:4e3e18b41ea6 | 111 | // read from the peripheral and make sure something is available |
| Sissors | 6:4e3e18b41ea6 | 112 | if(SoftSerial::readable()) { |
| kenjiArai | 11:f186cc69acdf | 113 | _rxbuf.push(_getc()); // if so load them into a buffer |
| Sissors | 6:4e3e18b41ea6 | 114 | } |
| Sissors | 6:4e3e18b41ea6 | 115 | return; |
| Sissors | 6:4e3e18b41ea6 | 116 | } |
| Sissors | 6:4e3e18b41ea6 | 117 | |
| kenjiArai | 11:f186cc69acdf | 118 | void BufferedSoftSerial::tx_Irq(void) |
| Sissors | 6:4e3e18b41ea6 | 119 | { |
| kenjiArai | 11:f186cc69acdf | 120 | // see if there is room in the hardware fifo and |
| kenjiArai | 11:f186cc69acdf | 121 | // if something is in the software fifo |
| Sissors | 6:4e3e18b41ea6 | 122 | while(SoftSerial::writeable()) { |
| kenjiArai | 11:f186cc69acdf | 123 | if(!_txbuf.empty()) { |
| kenjiArai | 11:f186cc69acdf | 124 | char c; |
| kenjiArai | 11:f186cc69acdf | 125 | _txbuf.pop(c); |
| kenjiArai | 11:f186cc69acdf | 126 | _putc((int)c); |
| Sissors | 6:4e3e18b41ea6 | 127 | } else { |
| Sissors | 6:4e3e18b41ea6 | 128 | // disable the TX interrupt when there is nothing left to send |
| Sissors | 6:4e3e18b41ea6 | 129 | SoftSerial::attach(NULL, SoftSerial::TxIrq); |
| Sissors | 6:4e3e18b41ea6 | 130 | break; |
| Sissors | 6:4e3e18b41ea6 | 131 | } |
| Sissors | 6:4e3e18b41ea6 | 132 | } |
| Sissors | 6:4e3e18b41ea6 | 133 | return; |
| Sissors | 6:4e3e18b41ea6 | 134 | } |
| Sissors | 6:4e3e18b41ea6 | 135 | |
| Sissors | 6:4e3e18b41ea6 | 136 | void BufferedSoftSerial::prime(void) |
| Sissors | 6:4e3e18b41ea6 | 137 | { |
| Sissors | 6:4e3e18b41ea6 | 138 | // if already busy then the irq will pick this up |
| Sissors | 6:4e3e18b41ea6 | 139 | if(SoftSerial::writeable()) { |
| kenjiArai | 11:f186cc69acdf | 140 | // make sure not to cause contention in the irq |
| kenjiArai | 11:f186cc69acdf | 141 | SoftSerial::attach(NULL, SoftSerial::TxIrq); |
| kenjiArai | 11:f186cc69acdf | 142 | // only write to hardware in one place |
| kenjiArai | 11:f186cc69acdf | 143 | tx_Irq(); |
| kenjiArai | 11:f186cc69acdf | 144 | SoftSerial::attach(this,&BufferedSoftSerial::tx_Irq,SoftSerial::TxIrq); |
| Sissors | 6:4e3e18b41ea6 | 145 | } |
| Sissors | 6:4e3e18b41ea6 | 146 | return; |
| Sissors | 6:4e3e18b41ea6 | 147 | } |