This class provides an API to communicate with a u-blox GNSS chip. The files here were originally part of the C027_Support library (https://developer.mbed.org/teams/ublox/code/C027_Support/ at revision 138:dafbbf31bf76) but have been separated out, primarily for use on the u-blox C030 board where the cellular interace portion of the C027_Support library will instead be provided through the new mbed Cellular API.

Dependents:   example-ublox-at-cellular-interface-ext example-low-power-sleep example-C030-out-of-box-demo example-C030-out-of-box-demo ... more

Committer:
fahimalavi
Date:
Wed May 29 16:45:00 2019 +0500
Revision:
33:75163fa7e453
Parent:
2:b10ca4aa2e5e
Code style corrected

Who changed what in which revision?

UserRevisionLine numberNew 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 {
RobMeades 2:b10ca4aa2e5e 23 if (rx!=NC) {
RobMeades 2:b10ca4aa2e5e 24 attach(callback(this, &SerialPipe::rxIrqBuf), RxIrq);
RobMeades 2:b10ca4aa2e5e 25 }
rob.meades@u-blox.com 1:ef70a58a6c98 26 }
rob.meades@u-blox.com 1:ef70a58a6c98 27
rob.meades@u-blox.com 1:ef70a58a6c98 28 SerialPipe::~SerialPipe(void)
rob.meades@u-blox.com 1:ef70a58a6c98 29 {
rob.meades@u-blox.com 1:ef70a58a6c98 30 attach(NULL, RxIrq);
rob.meades@u-blox.com 1:ef70a58a6c98 31 attach(NULL, TxIrq);
rob.meades@u-blox.com 1:ef70a58a6c98 32 }
rob.meades@u-blox.com 1:ef70a58a6c98 33
rob.meades@u-blox.com 1:ef70a58a6c98 34 // tx channel
rob.meades@u-blox.com 1:ef70a58a6c98 35 int SerialPipe::writeable(void)
rob.meades@u-blox.com 1:ef70a58a6c98 36 {
rob.meades@u-blox.com 1:ef70a58a6c98 37 return _pipeTx.free();
rob.meades@u-blox.com 1:ef70a58a6c98 38 }
rob.meades@u-blox.com 1:ef70a58a6c98 39
rob.meades@u-blox.com 1:ef70a58a6c98 40 int SerialPipe::putc(int c)
rob.meades@u-blox.com 1:ef70a58a6c98 41 {
rob.meades@u-blox.com 1:ef70a58a6c98 42 c = _pipeTx.putc(c);
rob.meades@u-blox.com 1:ef70a58a6c98 43 txStart();
rob.meades@u-blox.com 1:ef70a58a6c98 44 return c;
rob.meades@u-blox.com 1:ef70a58a6c98 45 }
rob.meades@u-blox.com 1:ef70a58a6c98 46
rob.meades@u-blox.com 1:ef70a58a6c98 47 int SerialPipe::put(const void* buffer, int length, bool blocking)
rob.meades@u-blox.com 1:ef70a58a6c98 48 {
rob.meades@u-blox.com 1:ef70a58a6c98 49 int count = length;
rob.meades@u-blox.com 1:ef70a58a6c98 50 const char* ptr = (const char*)buffer;
RobMeades 2:b10ca4aa2e5e 51 if (count) {
RobMeades 2:b10ca4aa2e5e 52 do {
rob.meades@u-blox.com 1:ef70a58a6c98 53 int written = _pipeTx.put(ptr, count, false);
rob.meades@u-blox.com 1:ef70a58a6c98 54 if (written) {
rob.meades@u-blox.com 1:ef70a58a6c98 55 ptr += written;
rob.meades@u-blox.com 1:ef70a58a6c98 56 count -= written;
rob.meades@u-blox.com 1:ef70a58a6c98 57 txStart();
rob.meades@u-blox.com 1:ef70a58a6c98 58 }
RobMeades 2:b10ca4aa2e5e 59 else if (!blocking) {
RobMeades 2:b10ca4aa2e5e 60 /* nothing / just wait */;
rob.meades@u-blox.com 1:ef70a58a6c98 61 break;
RobMeades 2:b10ca4aa2e5e 62 }
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 }
RobMeades 2:b10ca4aa2e5e 66
rob.meades@u-blox.com 1:ef70a58a6c98 67 return (length - count);
rob.meades@u-blox.com 1:ef70a58a6c98 68 }
rob.meades@u-blox.com 1:ef70a58a6c98 69
rob.meades@u-blox.com 1:ef70a58a6c98 70 void SerialPipe::txCopy(void)
rob.meades@u-blox.com 1:ef70a58a6c98 71 {
RobMeades 2:b10ca4aa2e5e 72 while (_SerialPipeBase::writeable() && _pipeTx.readable()) {
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
RobMeades 2:b10ca4aa2e5e 82 if (!_pipeTx.readable()) {
rob.meades@u-blox.com 1:ef70a58a6c98 83 attach(NULL, TxIrq);
RobMeades 2:b10ca4aa2e5e 84 }
rob.meades@u-blox.com 1:ef70a58a6c98 85 }
rob.meades@u-blox.com 1:ef70a58a6c98 86
rob.meades@u-blox.com 1:ef70a58a6c98 87 void SerialPipe::txStart(void)
rob.meades@u-blox.com 1:ef70a58a6c98 88 {
rob.meades@u-blox.com 1:ef70a58a6c98 89 // disable the tx isr to avoid interruption
rob.meades@u-blox.com 1:ef70a58a6c98 90 attach(NULL, TxIrq);
rob.meades@u-blox.com 1:ef70a58a6c98 91 txCopy();
rob.meades@u-blox.com 1:ef70a58a6c98 92 // attach the tx isr to handle the remaining data
RobMeades 2:b10ca4aa2e5e 93 if (_pipeTx.readable()) {
RobMeades 2:b10ca4aa2e5e 94 attach(callback(this, &SerialPipe::txIrqBuf), TxIrq);
RobMeades 2:b10ca4aa2e5e 95 }
rob.meades@u-blox.com 1:ef70a58a6c98 96 }
rob.meades@u-blox.com 1:ef70a58a6c98 97
rob.meades@u-blox.com 1:ef70a58a6c98 98 // rx channel
rob.meades@u-blox.com 1:ef70a58a6c98 99 int SerialPipe::readable(void)
rob.meades@u-blox.com 1:ef70a58a6c98 100 {
rob.meades@u-blox.com 1:ef70a58a6c98 101 return _pipeRx.readable();
rob.meades@u-blox.com 1:ef70a58a6c98 102 }
rob.meades@u-blox.com 1:ef70a58a6c98 103
rob.meades@u-blox.com 1:ef70a58a6c98 104 int SerialPipe::getc(void)
rob.meades@u-blox.com 1:ef70a58a6c98 105 {
RobMeades 2:b10ca4aa2e5e 106 if (!_pipeRx.readable()) {
rob.meades@u-blox.com 1:ef70a58a6c98 107 return EOF;
RobMeades 2:b10ca4aa2e5e 108 }
RobMeades 2:b10ca4aa2e5e 109
rob.meades@u-blox.com 1:ef70a58a6c98 110 return _pipeRx.getc();
rob.meades@u-blox.com 1:ef70a58a6c98 111 }
rob.meades@u-blox.com 1:ef70a58a6c98 112
rob.meades@u-blox.com 1:ef70a58a6c98 113 int SerialPipe::get(void* buffer, int length, bool blocking)
rob.meades@u-blox.com 1:ef70a58a6c98 114 {
rob.meades@u-blox.com 1:ef70a58a6c98 115 return _pipeRx.get((char*)buffer,length,blocking);
rob.meades@u-blox.com 1:ef70a58a6c98 116 }
rob.meades@u-blox.com 1:ef70a58a6c98 117
rob.meades@u-blox.com 1:ef70a58a6c98 118 void SerialPipe::rxIrqBuf(void)
rob.meades@u-blox.com 1:ef70a58a6c98 119 {
rob.meades@u-blox.com 1:ef70a58a6c98 120 while (_SerialPipeBase::readable())
rob.meades@u-blox.com 1:ef70a58a6c98 121 {
rob.meades@u-blox.com 1:ef70a58a6c98 122 char c = _SerialPipeBase::_base_getc();
RobMeades 2:b10ca4aa2e5e 123 if (_pipeRx.writeable()) {
rob.meades@u-blox.com 1:ef70a58a6c98 124 _pipeRx.putc(c);
RobMeades 2:b10ca4aa2e5e 125 } else {
RobMeades 2:b10ca4aa2e5e 126 /* overflow */
RobMeades 2:b10ca4aa2e5e 127 }
rob.meades@u-blox.com 1:ef70a58a6c98 128 }
rob.meades@u-blox.com 1:ef70a58a6c98 129 }