* add C027_Support fork

Fork of C027_Support by u-blox

Committer:
mazgch
Date:
Fri Nov 22 08:23:30 2013 +0000
Revision:
15:5eda64e5b9d1
Parent:
14:69c3e57ef0f5
Child:
16:4a7ba1887e81
- rebase the serial pipe on SerialBase; - fix nonblocking put

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mazgch 9:e7a5959ffae1 1 #pragma once
mazgch 9:e7a5959ffae1 2
mazgch 9:e7a5959ffae1 3 #include "SerialPipe.h"
mazgch 9:e7a5959ffae1 4
mazgch 15:5eda64e5b9d1 5 SerialPipe::SerialPipe(PinName tx, PinName rx, int rxSize, int txSize)
mazgch 15:5eda64e5b9d1 6 : _SerialPipeBase(tx,rx), _pipeRx(rxSize), _pipeTx(txSize)
mazgch 9:e7a5959ffae1 7 {
mazgch 9:e7a5959ffae1 8 attach(this, &SerialPipe::rxIrqBuf, RxIrq);
mazgch 9:e7a5959ffae1 9 attach(this, &SerialPipe::txIrqBuf, TxIrq);
mazgch 9:e7a5959ffae1 10 }
mazgch 9:e7a5959ffae1 11
mazgch 9:e7a5959ffae1 12 SerialPipe::~SerialPipe(void)
mazgch 9:e7a5959ffae1 13 {
mazgch 9:e7a5959ffae1 14 attach(NULL, RxIrq);
mazgch 9:e7a5959ffae1 15 attach(NULL, TxIrq);
mazgch 9:e7a5959ffae1 16 }
mazgch 9:e7a5959ffae1 17
mazgch 9:e7a5959ffae1 18 // tx channel
mazgch 13:e2446fcdc246 19 int SerialPipe::writeable(void)
mazgch 13:e2446fcdc246 20 {
mazgch 13:e2446fcdc246 21 return _pipeTx.free();
mazgch 13:e2446fcdc246 22 }
mazgch 13:e2446fcdc246 23
mazgch 13:e2446fcdc246 24 int SerialPipe::putc(int c)
mazgch 13:e2446fcdc246 25 {
mazgch 15:5eda64e5b9d1 26 c = _pipeTx.putc(c);
mazgch 15:5eda64e5b9d1 27 txStart();
mazgch 15:5eda64e5b9d1 28 return c;
mazgch 13:e2446fcdc246 29 }
mazgch 13:e2446fcdc246 30
mazgch 13:e2446fcdc246 31 int SerialPipe::put(const void* buffer, int length, bool blocking)
mazgch 9:e7a5959ffae1 32 {
mazgch 13:e2446fcdc246 33 int count = length;
mazgch 13:e2446fcdc246 34 const char* ptr = (const char*)buffer;
mazgch 15:5eda64e5b9d1 35 if (count)
mazgch 9:e7a5959ffae1 36 {
mazgch 15:5eda64e5b9d1 37 do
mazgch 15:5eda64e5b9d1 38 {
mazgch 15:5eda64e5b9d1 39 int written = _pipeTx.put(ptr, count, false);
mazgch 15:5eda64e5b9d1 40 ptr += written;
mazgch 15:5eda64e5b9d1 41 count -= written;
mazgch 15:5eda64e5b9d1 42 txStart();
mazgch 15:5eda64e5b9d1 43 }
mazgch 15:5eda64e5b9d1 44 while (count && blocking);
mazgch 9:e7a5959ffae1 45 }
mazgch 13:e2446fcdc246 46 return (length - count);
mazgch 13:e2446fcdc246 47 }
mazgch 13:e2446fcdc246 48
mazgch 9:e7a5959ffae1 49 void SerialPipe::txIrqBuf(void)
mazgch 9:e7a5959ffae1 50 {
mazgch 15:5eda64e5b9d1 51 while (_SerialPipeBase::writeable() && _pipeTx.readable())
mazgch 11:b084552b03fe 52 {
mazgch 13:e2446fcdc246 53 char c = _pipeTx.getc();
mazgch 15:5eda64e5b9d1 54 _SerialPipeBase::_base_putc(c);
mazgch 11:b084552b03fe 55 }
mazgch 9:e7a5959ffae1 56 }
mazgch 9:e7a5959ffae1 57
mazgch 13:e2446fcdc246 58 void SerialPipe::txStart(void)
mazgch 13:e2446fcdc246 59 {
mazgch 13:e2446fcdc246 60 __disable_irq();
mazgch 13:e2446fcdc246 61 txIrqBuf();
mazgch 13:e2446fcdc246 62 __enable_irq();
mazgch 13:e2446fcdc246 63 }
mazgch 13:e2446fcdc246 64
mazgch 9:e7a5959ffae1 65 // rx channel
mazgch 9:e7a5959ffae1 66 int SerialPipe::readable(void)
mazgch 9:e7a5959ffae1 67 {
mazgch 9:e7a5959ffae1 68 return _pipeRx.readable();
mazgch 9:e7a5959ffae1 69 }
mazgch 9:e7a5959ffae1 70
mazgch 9:e7a5959ffae1 71 int SerialPipe::getc(void)
mazgch 9:e7a5959ffae1 72 {
mazgch 15:5eda64e5b9d1 73 if (!_pipeRx.readable())
mazgch 15:5eda64e5b9d1 74 return EOF;
mazgch 15:5eda64e5b9d1 75 return _pipeRx.getc();
mazgch 13:e2446fcdc246 76 }
mazgch 13:e2446fcdc246 77
mazgch 13:e2446fcdc246 78 int SerialPipe::get(void* buffer, int length, bool blocking)
mazgch 13:e2446fcdc246 79 {
mazgch 13:e2446fcdc246 80 return _pipeRx.get((char*)buffer,length,blocking);
mazgch 13:e2446fcdc246 81 }
mazgch 13:e2446fcdc246 82
mazgch 9:e7a5959ffae1 83 void SerialPipe::rxIrqBuf(void)
mazgch 9:e7a5959ffae1 84 {
mazgch 15:5eda64e5b9d1 85 while (_SerialPipeBase::readable())
mazgch 9:e7a5959ffae1 86 {
mazgch 15:5eda64e5b9d1 87 char c = _SerialPipeBase::_base_getc();
mazgch 9:e7a5959ffae1 88 if (_pipeRx.writeable())
mazgch 13:e2446fcdc246 89 _pipeRx.putc(c);
mazgch 9:e7a5959ffae1 90 else
mazgch 9:e7a5959ffae1 91 /* overflow */;
mazgch 9:e7a5959ffae1 92 }
mazgch 9:e7a5959ffae1 93 }
mazgch 9:e7a5959ffae1 94
mazgch 9:e7a5959ffae1 95 // -----------------------------------------------------------------------
mazgch 9:e7a5959ffae1 96
mazgch 15:5eda64e5b9d1 97 SerialPipeEx::SerialPipeEx(PinName tx, PinName rx, int rxSize, int txSize)
mazgch 15:5eda64e5b9d1 98 : SerialPipe(tx,rx,rxSize,txSize)
mazgch 9:e7a5959ffae1 99 {}
mazgch 9:e7a5959ffae1 100
mazgch 13:e2446fcdc246 101 int SerialPipeEx::getLine(char* buffer, int length)
mazgch 9:e7a5959ffae1 102 {
mazgch 14:69c3e57ef0f5 103 return getLine(buffer, length, &_pipeRx);
mazgch 14:69c3e57ef0f5 104 }
mazgch 14:69c3e57ef0f5 105
mazgch 14:69c3e57ef0f5 106 int SerialPipeEx::getLine(char* buffer, int length, Pipe<char>* pipe)
mazgch 14:69c3e57ef0f5 107 {
mazgch 9:e7a5959ffae1 108 int o = 0;
mazgch 9:e7a5959ffae1 109 int i = 0;
mazgch 14:69c3e57ef0f5 110 int l = pipe->start();
mazgch 13:e2446fcdc246 111 while ((i < l) && (o < length))
mazgch 9:e7a5959ffae1 112 {
mazgch 14:69c3e57ef0f5 113 int t = pipe->next();
mazgch 9:e7a5959ffae1 114 i ++;
mazgch 9:e7a5959ffae1 115 if (t == '\r') // terminate commands with carriage return
mazgch 9:e7a5959ffae1 116 {
mazgch 14:69c3e57ef0f5 117 pipe->done();
mazgch 13:e2446fcdc246 118 if (length > o)
mazgch 13:e2446fcdc246 119 buffer[o] = '\0';
mazgch 12:684b31d5482b 120 return o; // if enter send the zero char
mazgch 9:e7a5959ffae1 121 }
mazgch 9:e7a5959ffae1 122 else if (t == '\n') // skip/filter new line
mazgch 9:e7a5959ffae1 123 /* skip */;
mazgch 9:e7a5959ffae1 124 else if (t != '\b') // normal char (no backspace)
mazgch 13:e2446fcdc246 125 buffer[o++] = t;
mazgch 9:e7a5959ffae1 126 else if (o > 0) // backspace
mazgch 9:e7a5959ffae1 127 o --; // remove it
mazgch 9:e7a5959ffae1 128 }
mazgch 9:e7a5959ffae1 129 o = 0;
mazgch 13:e2446fcdc246 130 if (length > 0)
mazgch 13:e2446fcdc246 131 buffer[0] = '\0';
mazgch 9:e7a5959ffae1 132 return WAIT;
mazgch 9:e7a5959ffae1 133 }