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