Futaba S-BUS Library. Let you control 16 servos and 2 digital channels

Dependencies:   mbed

Committer:
Digixx
Date:
Wed Mar 07 18:18:43 2012 +0000
Revision:
0:83e415034198

        

Who changed what in which revision?

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