Modified MODSERIAL library

Dependents:   BeaconAvoid

Committer:
mpanetta
Date:
Sun Sep 09 22:28:07 2012 +0000
Revision:
0:ef4645a79fca
[mbed] converted /BeaconAvoid/MODSERIAL

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mpanetta 0:ef4645a79fca 1 /*
mpanetta 0:ef4645a79fca 2 Copyright (c) 2010 Andy Kirkham
mpanetta 0:ef4645a79fca 3
mpanetta 0:ef4645a79fca 4 Permission is hereby granted, free of charge, to any person obtaining a copy
mpanetta 0:ef4645a79fca 5 of this software and associated documentation files (the "Software"), to deal
mpanetta 0:ef4645a79fca 6 in the Software without restriction, including without limitation the rights
mpanetta 0:ef4645a79fca 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
mpanetta 0:ef4645a79fca 8 copies of the Software, and to permit persons to whom the Software is
mpanetta 0:ef4645a79fca 9 furnished to do so, subject to the following conditions:
mpanetta 0:ef4645a79fca 10
mpanetta 0:ef4645a79fca 11 The above copyright notice and this permission notice shall be included in
mpanetta 0:ef4645a79fca 12 all copies or substantial portions of the Software.
mpanetta 0:ef4645a79fca 13
mpanetta 0:ef4645a79fca 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
mpanetta 0:ef4645a79fca 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
mpanetta 0:ef4645a79fca 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
mpanetta 0:ef4645a79fca 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
mpanetta 0:ef4645a79fca 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mpanetta 0:ef4645a79fca 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
mpanetta 0:ef4645a79fca 20 THE SOFTWARE.
mpanetta 0:ef4645a79fca 21 */
mpanetta 0:ef4645a79fca 22
mpanetta 0:ef4645a79fca 23 #include "MODSERIAL.h"
mpanetta 0:ef4645a79fca 24 #include "MACROS.h"
mpanetta 0:ef4645a79fca 25
mpanetta 0:ef4645a79fca 26 namespace AjK {
mpanetta 0:ef4645a79fca 27
mpanetta 0:ef4645a79fca 28 int
mpanetta 0:ef4645a79fca 29 MODSERIAL::__getc(bool block)
mpanetta 0:ef4645a79fca 30 {
mpanetta 0:ef4645a79fca 31 // If no buffer is in use fall back to standard RX FIFO usage.
mpanetta 0:ef4645a79fca 32 // Note, we must block in this case and ignore bool "block"
mpanetta 0:ef4645a79fca 33 // so as to maintain compat with Mbed Serial.
mpanetta 0:ef4645a79fca 34 if (buffer_size[RxIrq] == 0 || buffer[RxIrq] == (char *)NULL) {
mpanetta 0:ef4645a79fca 35 while(! MODSERIAL_RBR_HAS_DATA ) ;
mpanetta 0:ef4645a79fca 36 return (int)(_RBR & 0xFF);
mpanetta 0:ef4645a79fca 37 }
mpanetta 0:ef4645a79fca 38
mpanetta 0:ef4645a79fca 39 if (block) { while ( MODSERIAL_RX_BUFFER_EMPTY ) ; } // Blocks.
mpanetta 0:ef4645a79fca 40 else if ( MODSERIAL_RX_BUFFER_EMPTY ) return -1;
mpanetta 0:ef4645a79fca 41
mpanetta 0:ef4645a79fca 42 int c = buffer[RxIrq][buffer_out[RxIrq]];
mpanetta 0:ef4645a79fca 43 buffer_out[RxIrq]++;
mpanetta 0:ef4645a79fca 44 if (buffer_out[RxIrq] >= buffer_size[RxIrq]) {
mpanetta 0:ef4645a79fca 45 buffer_out[RxIrq] = 0;
mpanetta 0:ef4645a79fca 46 }
mpanetta 0:ef4645a79fca 47
mpanetta 0:ef4645a79fca 48 // If we have made space in the RX Buffer then copy over
mpanetta 0:ef4645a79fca 49 // any characters in the RX FIFO that my reside there.
mpanetta 0:ef4645a79fca 50 // Temporarily disable the RX IRQ so that we do not re-enter
mpanetta 0:ef4645a79fca 51 // it under interrupts.
mpanetta 0:ef4645a79fca 52 if ( ! MODSERIAL_RX_BUFFER_FULL ) {
mpanetta 0:ef4645a79fca 53 uint32_t ier = _IER;
mpanetta 0:ef4645a79fca 54 _IER &= ~(1UL << 0);
mpanetta 0:ef4645a79fca 55 isr_rx();
mpanetta 0:ef4645a79fca 56 _IER = ier;
mpanetta 0:ef4645a79fca 57 }
mpanetta 0:ef4645a79fca 58
mpanetta 0:ef4645a79fca 59 buffer_count[RxIrq]--;
mpanetta 0:ef4645a79fca 60 return c;
mpanetta 0:ef4645a79fca 61 }
mpanetta 0:ef4645a79fca 62
mpanetta 0:ef4645a79fca 63 }; // namespace AjK ends