positioning updates
Fork of gnss by
serial_pipe.cpp@1:ef70a58a6c98, 2017-04-10 (annotated)
- Committer:
- rob.meades@u-blox.com
- Date:
- Mon Apr 10 11:28:24 2017 +0100
- Revision:
- 1:ef70a58a6c98
- Child:
- 2:b10ca4aa2e5e
Add files to repo, removing temp.txt placeholder.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rob.meades@u-blox.com | 1:ef70a58a6c98 | 1 | /* Copyright (c) 2017 Michael Ammann |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 2 | * |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 4 | * you may not use this file except in compliance with the License. |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 5 | * You may obtain a copy of the License at |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 6 | * |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 8 | * |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 9 | * Unless required by applicable law or agreed to in writing, software |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 12 | * See the License for the specific language governing permissions and |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 13 | * limitations under the License. |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 14 | */ |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 15 | |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 16 | #include "serial_pipe.h" |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 17 | |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 18 | SerialPipe::SerialPipe(PinName tx, PinName rx, int baudrate, int rxSize, int txSize) : |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 19 | _SerialPipeBase(tx, rx, baudrate), |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 20 | _pipeRx( (rx!=NC) ? rxSize : 0), |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 21 | _pipeTx( (tx!=NC) ? txSize : 0) |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 22 | { |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 23 | if (rx!=NC) |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 24 | attach(this, &SerialPipe::rxIrqBuf, RxIrq); |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 25 | } |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 26 | |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 27 | SerialPipe::~SerialPipe(void) |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 28 | { |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 29 | attach(NULL, RxIrq); |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 30 | attach(NULL, TxIrq); |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 31 | } |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 32 | |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 33 | // tx channel |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 34 | int SerialPipe::writeable(void) |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 35 | { |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 36 | return _pipeTx.free(); |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 37 | } |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 38 | |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 39 | int SerialPipe::putc(int c) |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 40 | { |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 41 | c = _pipeTx.putc(c); |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 42 | txStart(); |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 43 | return c; |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 44 | } |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 45 | |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 46 | int SerialPipe::put(const void* buffer, int length, bool blocking) |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 47 | { |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 48 | int count = length; |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 49 | const char* ptr = (const char*)buffer; |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 50 | if (count) |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 51 | { |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 52 | do |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 53 | { |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 54 | int written = _pipeTx.put(ptr, count, false); |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 55 | if (written) { |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 56 | ptr += written; |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 57 | count -= written; |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 58 | txStart(); |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 59 | } |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 60 | else if (!blocking) |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 61 | break; |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 62 | /* nothing / just wait */; |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 63 | } |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 64 | while (count); |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 65 | } |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 66 | return (length - count); |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 67 | } |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 68 | |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 69 | void SerialPipe::txCopy(void) |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 70 | { |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 71 | while (_SerialPipeBase::writeable() && _pipeTx.readable()) |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 72 | { |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 73 | char c = _pipeTx.getc(); |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 74 | _SerialPipeBase::_base_putc(c); |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 75 | } |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 76 | } |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 77 | |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 78 | void SerialPipe::txIrqBuf(void) |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 79 | { |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 80 | txCopy(); |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 81 | // detach tx isr if we are done |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 82 | if (!_pipeTx.readable()) |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 83 | attach(NULL, TxIrq); |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 84 | } |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 85 | |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 86 | void SerialPipe::txStart(void) |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 87 | { |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 88 | // disable the tx isr to avoid interruption |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 89 | attach(NULL, TxIrq); |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 90 | txCopy(); |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 91 | // attach the tx isr to handle the remaining data |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 92 | if (_pipeTx.readable()) |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 93 | attach(this, &SerialPipe::txIrqBuf, TxIrq); |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 94 | } |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 95 | |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 96 | // rx channel |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 97 | int SerialPipe::readable(void) |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 98 | { |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 99 | return _pipeRx.readable(); |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 100 | } |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 101 | |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 102 | int SerialPipe::getc(void) |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 103 | { |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 104 | if (!_pipeRx.readable()) |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 105 | return EOF; |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 106 | return _pipeRx.getc(); |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 107 | } |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 108 | |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 109 | int SerialPipe::get(void* buffer, int length, bool blocking) |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 110 | { |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 111 | return _pipeRx.get((char*)buffer,length,blocking); |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 112 | } |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 113 | |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 114 | void SerialPipe::rxIrqBuf(void) |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 115 | { |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 116 | while (_SerialPipeBase::readable()) |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 117 | { |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 118 | char c = _SerialPipeBase::_base_getc(); |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 119 | if (_pipeRx.writeable()) |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 120 | _pipeRx.putc(c); |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 121 | else |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 122 | /* overflow */; |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 123 | } |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 124 | } |
rob.meades@u-blox.com | 1:ef70a58a6c98 | 125 |