MHVLib  20111227
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                 MHV_DECLARE_PIN(data),
00072                 MHV_DECLARE_PIN(control)) {
00073         _address = 0;
00074         _mode = mode;
00075 
00076         _dataDir = dataDir;
00077         _dataInput = dataInput;
00078         _dataOutput = dataOutput;
00079 
00080         _controlDir = controlDir;
00081         _controlInput = controlInput;
00082         _controlOutput = controlOutput;
00083 
00084 // Set up the pin directions
00085         *_dataDir = 0;
00086         *_controlDir = (*_controlDir & 0xc0) | 0x27;
00087 
00088 // Set up the pin pullups for inputs, initial states for outputs
00089         *_dataOutput = 0;
00090         *_controlOutput = (*_controlOutput & 0xc0) | 0x00;
00091         _controlPrevious = *_controlInput;
00092 }
00093 
00097 void MHV_EPP::poll() {
00098         uint8_t control = *_controlInput;
00099         uint8_t changed = control ^ _controlPrevious;
00100 
00101         if (changed & _BV(MHV_EPP_DATA_STROBE)) {
00102                 if (control & _BV(MHV_EPP_DATA_STROBE)) {
00103                         releaseStrobe(false);
00104                 } else {
00105                         interruptStrobe(false);
00106                 }
00107         } else if (changed & _BV(MHV_EPP_ADDRESS_STROBE)) {
00108                 if (control & _BV(MHV_EPP_ADDRESS_STROBE)) {
00109                         releaseStrobe(true);
00110                 } else {
00111                         interruptStrobe(true);
00112                 }
00113         } else if ((changed & _BV(MHV_EPP_RESET)) &&
00114                         (control & _BV(MHV_EPP_RESET))) {
00115                 interruptReset();
00116         }
00117 
00118         _controlPrevious = control;
00119 }
00120 
00124 void MHV_EPP::interruptReset() {
00125         _reset();
00126 }
00127 
00132 void MHV_EPP::interruptStrobe(bool address) {
00133         if (mhv_pinRead(MHV_EPP_CONTROL, MHV_EPP_WRITE, 0)) {
00134 // Read cycle (host is reading from us), write is active low
00135 
00136 // Set data port to output
00137                 *_dataDir = 0x00;
00138 
00139 // Write the data to the host
00140                 *_dataOutput = _read(address++);
00141         } else {
00142 // Write cycle (host is writing to us)
00143 
00144 // Set data port to input
00145                 *_dataDir = 0xff;
00146 
00147 // Read the data from the host
00148                 _write(address++, *_dataOutput);
00149         }
00150 
00151 // Raise wait
00152         mhv_pinOn(MHV_EPP_CONTROL, MHV_EPP_WAIT, 0);
00153 }
00154 
00159 void MHV_EPP::releaseStrobe(bool address) {
00160 // Drop wait
00161         mhv_pinOff(MHV_EPP_CONTROL, MHV_EPP_WAIT, 0);
00162 }
00163 
00164 #endif