MHVLib  20111011
An efficiency oriented runtime library for AVR microcontrollers
A:/eclipse/mhvlib/MHV_EPP.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011, Make, Hack, Void Inc
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *  * Redistributions of source code must retain the above copyright
00008  *    notice, this list of conditions and the following disclaimer.
00009  *  * Redistributions in binary form must reproduce the above copyright
00010  *    notice, this list of conditions and the following disclaimer in the
00011  *    documentation and/or other materials provided with the distribution.
00012  *  * Neither the name of the Make, Hack, Void nor the
00013  *    names of its contributors may be used to endorse or promote products
00014  *    derived from this software without specific prior written permission.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00017  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019  * DISCLAIMED. IN NO EVENT SHALL MAKE, HACK, VOID BE LIABLE FOR ANY
00020  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00021  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00022  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00023  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00024  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00025  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  */
00027 
00028 #if 0
00029 #include <MHV_EPP.h>
00030 
00053 #define MHV_EPP_DATA_STROBE             0
00054 #define MHV_EPP_ADDRESS_STROBE  1
00055 #define MHV_EPP_WRITE                   2
00056 #define MHV_EPP_INTERRUPT               3
00057 #define MHV_EPP_WAIT                    4
00058 #define MHV_EPP_RESET                   5
00059 
00060 #define MHV_EPP_DATA    _dataDir, _dataInput, _dataOutput
00061 #define MHV_EPP_CONTROL _controlDir, _controlInput, _controlOutput
00062 
00070 MHV_EPP::MHV_EPP(MHV_EPP_MODE mode,
00071                 volatile uint8_t *dataDir, volatile uint8_t *dataInput, volatile uint8_t *dataOutput,
00072                 uint8_t dataBit, uint8_t dataPcInt,
00073                 volatile uint8_t *controlDir, volatile uint8_t *controlInput, volatile uint8_t *controlOutput,
00074                 uint8_t controlBit, uint8_t controlPcInt) {
00075         _address = 0;
00076         _mode = mode;
00077 
00078         _dataDir = dataDir;
00079         _dataInput = dataInput;
00080         _dataOutput = dataOutput;
00081 
00082         _controlDir = controlDir;
00083         _controlInput = controlInput;
00084         _controlOutput = controlOutput;
00085 
00086 // Set up the pin directions
00087         *_dataDir = 0;
00088         *_controlDir = (*_controlDir & 0xc0) | 0x27;
00089 
00090 // Set up the pin pullups for inputs, initial states for outputs
00091         *_dataOutput = 0;
00092         *_controlOutput = (*_controlOutput & 0xc0) | 0x00;
00093         _controlPrevious = *_controlInput;
00094 }
00095 
00099 void MHV_EPP::poll() {
00100         uint8_t control = *_controlInput;
00101         uint8_t changed = control ^ _controlPrevious;
00102 
00103         if (changed & _BV(MHV_EPP_DATA_STROBE)) {
00104                 if (control & _BV(MHV_EPP_DATA_STROBE)) {
00105                         releaseStrobe(false);
00106                 } else {
00107                         interruptStrobe(false);
00108                 }
00109         } else if (changed & _BV(MHV_EPP_ADDRESS_STROBE)) {
00110                 if (control & _BV(MHV_EPP_ADDRESS_STROBE)) {
00111                         releaseStrobe(true);
00112                 } else {
00113                         interruptStrobe(true);
00114                 }
00115         } else if ((changed & _BV(MHV_EPP_RESET)) &&
00116                         (control & _BV(MHV_EPP_RESET))) {
00117                 interruptReset();
00118         }
00119 
00120         _controlPrevious = control;
00121 }
00122 
00126 void MHV_EPP::interruptReset() {
00127         _reset();
00128 }
00129 
00134 void MHV_EPP::interruptStrobe(bool address) {
00135         if (mhv_pinRead(MHV_EPP_CONTROL, MHV_EPP_WRITE, 0)) {
00136 // Read cycle (host is reading from us), write is active low
00137 
00138 // Set data port to output
00139                 *_dataDir = 0x00;
00140 
00141 // Write the data to the host
00142                 *_dataOutput = _read(address++);
00143         } else {
00144 // Write cycle (host is writing to us)
00145 
00146 // Set data port to input
00147                 *_dataDir = 0xff;
00148 
00149 // Read the data from the host
00150                 _write(address++, *_dataOutput);
00151         }
00152 
00153 // Raise wait
00154         mhv_pinOn(MHV_EPP_CONTROL, MHV_EPP_WAIT, 0);
00155 }
00156 
00161 void MHV_EPP::releaseStrobe(bool address) {
00162 // Drop wait
00163         mhv_pinOff(MHV_EPP_CONTROL, MHV_EPP_WAIT, 0);
00164 }
00165 
00166 #endif