MODSERIAL with support for more devices

Fork of MODSERIAL by Erik -

Committer:
AjK
Date:
Tue Feb 08 00:57:48 2011 +0000
Revision:
14:3c3b87617bfe
Parent:
12:8c7394e2ae7f
Child:
18:21ef26402365
1.14 See ChangeLog

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) {
AjK 8:775f860e94d3 35 while (! MODSERIAL_THR_HAS_SPACE) ; // Wait for space in the TX FIFO.
AjK 8:775f860e94d3 36 _THR = (uint32_t)c;
AjK 8:775f860e94d3 37 return 0;
AjK 8:775f860e94d3 38 }
AjK 8:775f860e94d3 39
AjK 9:b3cdae80e7a9 40 if ( MODSERIAL_THR_HAS_SPACE && MODSERIAL_TX_BUFFER_EMPTY && dmaSendChannel == -1 ) {
AjK 4:28de979b77cf 41 _THR = (uint32_t)c;
AjK 4:28de979b77cf 42 }
AjK 4:28de979b77cf 43 else {
AjK 8:775f860e94d3 44 if (block) {
AjK 12:8c7394e2ae7f 45 uint32_t ier = _IER; _IER = 1;
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 }
AjK 12:8c7394e2ae7f 59 _IER = ier;
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 8:775f860e94d3 63 _isr[TxOvIrq].call();
AjK 8:775f860e94d3 64 return -1;
AjK 0:eb2522b41db8 65 }
AjK 14:3c3b87617bfe 66 _IER &= ~2;
AjK 8:775f860e94d3 67 buffer[TxIrq][buffer_in[TxIrq]] = c;
AjK 8:775f860e94d3 68 buffer_count[TxIrq]++;
AjK 8:775f860e94d3 69 buffer_in[TxIrq]++;
AjK 8:775f860e94d3 70 if (buffer_in[TxIrq] >= buffer_size[TxIrq]) {
AjK 8:775f860e94d3 71 buffer_in[TxIrq] = 0;
AjK 8:775f860e94d3 72 }
AjK 14:3c3b87617bfe 73 _IER |= 2;
AjK 0:eb2522b41db8 74 }
AjK 4:28de979b77cf 75
AjK 0:eb2522b41db8 76 return 0;
AjK 0:eb2522b41db8 77 }
AjK 0:eb2522b41db8 78
AjK 0:eb2522b41db8 79 }; // namespace AjK ends