* add C027_Support fork

Fork of C027_Support by u-blox

Committer:
mazgch
Date:
Tue Nov 19 09:02:35 2013 +0000
Revision:
14:69c3e57ef0f5
Parent:
13:e2446fcdc246
Child:
15:5eda64e5b9d1
have a static getline function

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