Dependencies:   PinDetect TextLCD mbed mRotaryEncoder

Committer:
cicklaus
Date:
Mon Feb 13 02:11:20 2012 +0000
Revision:
0:afb2650fb49a

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cicklaus 0:afb2650fb49a 1 /*
cicklaus 0:afb2650fb49a 2 Copyright (c) 2010 Andy Kirkham
cicklaus 0:afb2650fb49a 3
cicklaus 0:afb2650fb49a 4 Permission is hereby granted, free of charge, to any person obtaining a copy
cicklaus 0:afb2650fb49a 5 of this software and associated documentation files (the "Software"), to deal
cicklaus 0:afb2650fb49a 6 in the Software without restriction, including without limitation the rights
cicklaus 0:afb2650fb49a 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
cicklaus 0:afb2650fb49a 8 copies of the Software, and to permit persons to whom the Software is
cicklaus 0:afb2650fb49a 9 furnished to do so, subject to the following conditions:
cicklaus 0:afb2650fb49a 10
cicklaus 0:afb2650fb49a 11 The above copyright notice and this permission notice shall be included in
cicklaus 0:afb2650fb49a 12 all copies or substantial portions of the Software.
cicklaus 0:afb2650fb49a 13
cicklaus 0:afb2650fb49a 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cicklaus 0:afb2650fb49a 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cicklaus 0:afb2650fb49a 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cicklaus 0:afb2650fb49a 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cicklaus 0:afb2650fb49a 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
cicklaus 0:afb2650fb49a 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
cicklaus 0:afb2650fb49a 20 THE SOFTWARE.
cicklaus 0:afb2650fb49a 21 */
cicklaus 0:afb2650fb49a 22
cicklaus 0:afb2650fb49a 23 #include "MODSERIAL.h"
cicklaus 0:afb2650fb49a 24 #include "MACROS.h"
cicklaus 0:afb2650fb49a 25
cicklaus 0:afb2650fb49a 26 namespace AjK {
cicklaus 0:afb2650fb49a 27
cicklaus 0:afb2650fb49a 28 int
cicklaus 0:afb2650fb49a 29 MODSERIAL::__putc(int c, bool block) {
cicklaus 0:afb2650fb49a 30
cicklaus 0:afb2650fb49a 31 // If no buffer is in use fall back to standard TX FIFO usage.
cicklaus 0:afb2650fb49a 32 // Note, we must block in this case and ignore bool "block"
cicklaus 0:afb2650fb49a 33 // so as to maintain compat with Mbed Serial.
cicklaus 0:afb2650fb49a 34 if (buffer[TxIrq] == (char *)NULL || buffer_size[TxIrq] == 0) {
cicklaus 0:afb2650fb49a 35 while (! MODSERIAL_THR_HAS_SPACE) ; // Wait for space in the TX FIFO.
cicklaus 0:afb2650fb49a 36 _THR = (uint32_t)c;
cicklaus 0:afb2650fb49a 37 return 0;
cicklaus 0:afb2650fb49a 38 }
cicklaus 0:afb2650fb49a 39
cicklaus 0:afb2650fb49a 40 if ( MODSERIAL_THR_HAS_SPACE && MODSERIAL_TX_BUFFER_EMPTY && dmaSendChannel == -1 ) {
cicklaus 0:afb2650fb49a 41 _THR = (uint32_t)c;
cicklaus 0:afb2650fb49a 42 }
cicklaus 0:afb2650fb49a 43 else {
cicklaus 0:afb2650fb49a 44 if (block) {
cicklaus 0:afb2650fb49a 45 uint32_t ier = _IER; _IER = 1;
cicklaus 0:afb2650fb49a 46 while ( MODSERIAL_TX_BUFFER_FULL ) { // Blocks!
cicklaus 0:afb2650fb49a 47 // If putc() is called from an ISR then we are stuffed
cicklaus 0:afb2650fb49a 48 // because in an ISR no bytes from the TX buffer will
cicklaus 0:afb2650fb49a 49 // get transferred to teh TX FIFOs while we block here.
cicklaus 0:afb2650fb49a 50 // So, to work around this, instead of sitting in a
cicklaus 0:afb2650fb49a 51 // loop waiting for space in the TX buffer (which will
cicklaus 0:afb2650fb49a 52 // never happen in IRQ context), check to see if the
cicklaus 0:afb2650fb49a 53 // TX FIFO has space available to move bytes from the
cicklaus 0:afb2650fb49a 54 // TX buffer to TX FIFO to make space. The easiest way
cicklaus 0:afb2650fb49a 55 // to do this is to poll the isr_tx() function while we
cicklaus 0:afb2650fb49a 56 // are blocking.
cicklaus 0:afb2650fb49a 57 isr_tx(false);
cicklaus 0:afb2650fb49a 58 }
cicklaus 0:afb2650fb49a 59 _IER = ier;
cicklaus 0:afb2650fb49a 60 }
cicklaus 0:afb2650fb49a 61 else if( MODSERIAL_TX_BUFFER_FULL ) {
cicklaus 0:afb2650fb49a 62 buffer_overflow[TxIrq] = c; // Oh dear, no room in buffer.
cicklaus 0:afb2650fb49a 63 _isr[TxOvIrq].call(&this->callbackInfo);
cicklaus 0:afb2650fb49a 64 return -1;
cicklaus 0:afb2650fb49a 65 }
cicklaus 0:afb2650fb49a 66 _IER &= ~2;
cicklaus 0:afb2650fb49a 67 buffer[TxIrq][buffer_in[TxIrq]] = c;
cicklaus 0:afb2650fb49a 68 buffer_count[TxIrq]++;
cicklaus 0:afb2650fb49a 69 buffer_in[TxIrq]++;
cicklaus 0:afb2650fb49a 70 if (buffer_in[TxIrq] >= buffer_size[TxIrq]) {
cicklaus 0:afb2650fb49a 71 buffer_in[TxIrq] = 0;
cicklaus 0:afb2650fb49a 72 }
cicklaus 0:afb2650fb49a 73 _IER |= 2;
cicklaus 0:afb2650fb49a 74 }
cicklaus 0:afb2650fb49a 75
cicklaus 0:afb2650fb49a 76 return 0;
cicklaus 0:afb2650fb49a 77 }
cicklaus 0:afb2650fb49a 78
cicklaus 0:afb2650fb49a 79 }; // namespace AjK ends