Fork of the MODSERIAL library by Erik Olieman.

Dependents:   trs_master

Fork of MODSERIAL by Erik -

Committer:
Sissors
Date:
Sun Feb 08 19:59:54 2015 +0000
Revision:
45:8ef4f91813fd
Parent:
36:b04ce87dc424
Added K22F support (probably)

Who changed what in which revision?

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