MODSERIAL with support for more devices
Fork of MODSERIAL by
PUTC.cpp@8:775f860e94d3, 2010-11-22 (annotated)
- Committer:
- AjK
- Date:
- Mon Nov 22 09:58:34 2010 +0000
- Revision:
- 8:775f860e94d3
- Parent:
- 6:c8f77fe1cc10
- Child:
- 9:b3cdae80e7a9
1.8
Who changed what in which revision?
User | Revision | Line number | New 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 | 8:775f860e94d3 | 40 | if ( MODSERIAL_THR_HAS_SPACE && MODSERIAL_TX_BUFFER_EMPTY ) { |
AjK | 4:28de979b77cf | 41 | _THR = (uint32_t)c; |
AjK | 4:28de979b77cf | 42 | } |
AjK | 4:28de979b77cf | 43 | else { |
AjK | 8:775f860e94d3 | 44 | if (block) { |
AjK | 8:775f860e94d3 | 45 | while ( MODSERIAL_TX_BUFFER_FULL ) { // Blocks! |
AjK | 8:775f860e94d3 | 46 | // If putc() is called from an ISR then we are stuffed |
AjK | 8:775f860e94d3 | 47 | // because in an ISR no bytes from the TX buffer will |
AjK | 8:775f860e94d3 | 48 | // get transferred to teh TX FIFOs while we block here. |
AjK | 8:775f860e94d3 | 49 | // So, to work around this, instead of sitting in a |
AjK | 8:775f860e94d3 | 50 | // loop waiting for space in the TX buffer (which will |
AjK | 8:775f860e94d3 | 51 | // never happen in IRQ context), check to see if the |
AjK | 8:775f860e94d3 | 52 | // TX FIFO has space available to move bytes from the |
AjK | 8:775f860e94d3 | 53 | // TX buffer to TX FIFO to make space. The easiest way |
AjK | 8:775f860e94d3 | 54 | // to do this is to poll the isr_tx() function while we |
AjK | 8:775f860e94d3 | 55 | // are blocking. |
AjK | 8:775f860e94d3 | 56 | isr_tx(false); |
AjK | 5:8365c4cf8f33 | 57 | } |
AjK | 8:775f860e94d3 | 58 | } |
AjK | 8:775f860e94d3 | 59 | else if( MODSERIAL_TX_BUFFER_FULL ) { |
AjK | 8:775f860e94d3 | 60 | buffer_overflow[TxIrq] = c; // Oh dear, no room in buffer. |
AjK | 8:775f860e94d3 | 61 | _isr[TxOvIrq].call(); |
AjK | 8:775f860e94d3 | 62 | return -1; |
AjK | 0:eb2522b41db8 | 63 | } |
AjK | 8:775f860e94d3 | 64 | buffer[TxIrq][buffer_in[TxIrq]] = c; |
AjK | 8:775f860e94d3 | 65 | buffer_count[TxIrq]++; |
AjK | 8:775f860e94d3 | 66 | buffer_in[TxIrq]++; |
AjK | 8:775f860e94d3 | 67 | if (buffer_in[TxIrq] >= buffer_size[TxIrq]) { |
AjK | 8:775f860e94d3 | 68 | buffer_in[TxIrq] = 0; |
AjK | 8:775f860e94d3 | 69 | } |
AjK | 8:775f860e94d3 | 70 | _IER |= 0x2; |
AjK | 0:eb2522b41db8 | 71 | } |
AjK | 4:28de979b77cf | 72 | |
AjK | 0:eb2522b41db8 | 73 | return 0; |
AjK | 0:eb2522b41db8 | 74 | } |
AjK | 0:eb2522b41db8 | 75 | |
AjK | 0:eb2522b41db8 | 76 | }; // namespace AjK ends |