Bug fix release

Dependents:   AntiTheftGPS XbeeReceive XbeeSend Superball_Ball2 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PUTC.cpp Source File

PUTC.cpp

00001 /*
00002     Copyright (c) 2010 Andy Kirkham
00003  
00004     Permission is hereby granted, free of charge, to any person obtaining a copy
00005     of this software and associated documentation files (the "Software"), to deal
00006     in the Software without restriction, including without limitation the rights
00007     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008     copies of the Software, and to permit persons to whom the Software is
00009     furnished to do so, subject to the following conditions:
00010  
00011     The above copyright notice and this permission notice shall be included in
00012     all copies or substantial portions of the Software.
00013  
00014     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020     THE SOFTWARE.
00021 */
00022 
00023 #include "MODSERIAL.h"
00024 #include "MACROS.h"
00025 
00026 namespace AjK {
00027 
00028 int
00029 MODSERIAL::__putc(int c, bool block) {
00030     
00031     // If no buffer is in use fall back to standard TX FIFO usage.
00032     // Note, we must block in this case and ignore bool "block" 
00033     // so as to maintain compat with Mbed Serial.
00034     if (buffer[TxIrq] == (char *)NULL || buffer_size[TxIrq] == 0) {
00035         while (! MODSERIAL_THR_HAS_SPACE) ; // Wait for space in the TX FIFO.
00036         _THR = (uint32_t)c;
00037         return 0;
00038     }
00039     
00040     if ( MODSERIAL_THR_HAS_SPACE && MODSERIAL_TX_BUFFER_EMPTY && dmaSendChannel == -1 ) {
00041         _THR = (uint32_t)c;
00042     }
00043     else {
00044         if (block) {
00045             uint32_t ier = _IER; _IER = 1;
00046             while ( MODSERIAL_TX_BUFFER_FULL ) {  // Blocks!
00047                 // If putc() is called from an ISR then we are stuffed
00048                 // because in an ISR no bytes from the TX buffer will 
00049                 // get transferred to teh TX FIFOs while we block here.
00050                 // So, to work around this, instead of sitting in a 
00051                 // loop waiting for space in the TX buffer (which will
00052                 // never happen in IRQ context), check to see if the
00053                 // TX FIFO has space available to move bytes from the
00054                 // TX buffer to TX FIFO to make space. The easiest way
00055                 // to do this is to poll the isr_tx() function while we
00056                 // are blocking.
00057                 isr_tx(false);
00058             }
00059             _IER = ier;
00060         }
00061         else if( MODSERIAL_TX_BUFFER_FULL ) {
00062             buffer_overflow[TxIrq] = c; // Oh dear, no room in buffer.
00063             _isr[TxOvIrq].call(&this->callbackInfo);
00064             return -1;
00065         }
00066         _IER &= ~2;
00067         buffer[TxIrq][buffer_in[TxIrq]] = c;
00068         __disable_irq();
00069         buffer_count[TxIrq]++;
00070         __enable_irq();
00071         buffer_in[TxIrq]++;
00072         if (buffer_in[TxIrq] >= buffer_size[TxIrq]) {
00073             buffer_in[TxIrq] = 0;
00074         }            
00075         _IER |= 2;        
00076     }
00077       
00078     return 0;
00079 }
00080 
00081 }; // namespace AjK ends