Logo Search packages:      
Sourcecode: odyssey version File versions

ParallelPort.cxx

/* Copyright (C) 2002-2004  Mark Andrew Aikens <marka@desert.cx>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: ParallelPort.cxx,v 1.12 2004/08/22 18:21:19 marka Exp $
 */
using namespace std;

#include <stdio.h>
#include <stdexcept>
#include "ParallelPort.h"
#include "ConfigFile.h"
#include "Util.h"


#define READPINDATA(cname, prefix) \
      if(!config->get_integer("io", cname, &value)) \
            throw runtime_error("Missing I/O configuration parameter io." cname); \
      this->prefix##invert = 0; \
      if(value < 0) { \
            this->prefix##invert = 1; \
            value *= -1; \
      } \
      if((value > 25) || (value == 0) || (pin2reg[value-1] == -1)) \
            throw runtime_error("Invalid value for configuration parameter io." cname); \
      if(hw_invert[value-1]) \
            this->prefix##invert ^= 1; \
      this->prefix##reg = pin2reg[value-1]; \
      this->prefix##bit = pin2bit[value-1];


/* Mapping of parallel port pin #'s to I/O register offset. */
static char pin2reg[25] = {
      2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 1, 2, 2,
      -1, -1, -1, -1, -1, -1, -1, -1
};

/* Mapping of parallel port pin #'s to I/O register bit positions. */
static char pin2bit[25] = {
      0, 0, 1, 2, 3, 4, 5, 6, 7, 6, 7, 5, 4, 1, 3, 2, 3,
      -1, -1, -1, -1, -1, -1, -1, -1
};

/* Flags for each pin, indicating if the parallel port hardware inverts the
 * value. (BSY, STB, ALF, DSL) */
static char hw_invert[25] = {
      1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1,
      0, 0, 0, 0, 0, 0, 0, 0
};


00064 ParallelPort::ParallelPort(int port) : IO(port) {
      long value;

      READPINDATA("rdatapin", rdata);
      READPINDATA("wdatapin", wdata);
      READPINDATA("clkpin", clk);
      READPINDATA("vpppin", vpp);
      READPINDATA("pwrpin", pwr);

      /* The rdata2pin is optional */
      this->rdata2reg = -1;
      if(config->get_integer("io", "rdata2pin", &value) && (value != 0)) {
            READPINDATA("rdata2pin", rdata2);
      }
}


00081 ParallelPort::~ParallelPort() {
}


00085 void ParallelPort::set_clk(bool state) {
      this->set_pin_state("clk", this->clkreg, this->clkbit, this->clkinvert,
        state);
}


00091 bool ParallelPort::get_clk(void) {
      return this->get_pin_state("clk", this->clkreg, this->clkbit,
        this->clkinvert);
}


00097 void ParallelPort::set_data(bool state) {
      this->set_pin_state("wdata", this->wdatareg, this->wdatabit,
        this->wdatainvert, state);
}


00103 bool ParallelPort::get_data(void) {
      return this->get_pin_state("wdata", this->wdatareg, this->wdatabit,
        this->wdatainvert);
}


00109 bool ParallelPort::read_data(void) {
      bool data, data2;

      data = this->get_pin_state("rdata", this->rdatareg, this->rdatabit,
        this->rdatainvert);
      if(this->rdata2reg >= 0) {
            data2 = this->get_pin_state("rdata2", this->rdata2reg, this->rdata2bit,
              this->rdata2invert);
            if(data != data2) {
                  THROW_ERROR(runtime_error, "Discrepancy: rdata %d != rdata2 %d",
                    data, data2);
            }
      }
      return data;
}


00126 void ParallelPort::set_vpp(bool state) {
      this->set_pin_state("vpp", this->vppreg, this->vppbit, this->vppinvert,
        state);
}


00132 bool ParallelPort::get_vpp(void) {
      return this->get_pin_state("vpp", this->vppreg, this->vppbit,
        this->vppinvert);
}


00138 void ParallelPort::set_pwr(bool state) {
      this->set_pin_state("pwr", this->pwrreg, this->pwrbit, this->pwrinvert,
        state);
}


00144 bool ParallelPort::get_pwr(void) {
      return this->get_pin_state("pwr", this->pwrreg, this->pwrbit,
        this->pwrinvert);
}

Generated by  Doxygen 1.6.0   Back to index