1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
/*
Copyright (C) 2015 Roy R Rankin
This file is part of gpsim.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*
This module simulates a 7 bit address slave I2C device with an 8 bit
I/O bus. The direction of the 8 bit bus is controlled by the R/W bit
sent from the master.
This module allows multi-pin devices such as LCD displays to be
connected to the processor via the 2 pin I2C bus.
*/
#include <cstdio>
//#define DEBUG
#if defined(DEBUG)
#define Dprintf(arg) {printf("%s:%d ",__FILE__,__LINE__); printf arg; }
#else
#define Dprintf(arg) {}
#endif
#include "config.h" // get the definition for HAVE_GUI
#ifdef HAVE_GUI
#include <gtk/gtk.h>
#endif
#include "src/gpsim_time.h"
#include "src/stimuli.h"
#include "src/ioports.h"
#include "src/symbol.h"
#include "src/value.h"
#include "src/packages.h"
#include "src/gpsim_interface.h"
#include "i2c2par.h"
class AddAttribute : public Integer {
public:
I2C2PAR_Modules::i2c2par *i2cpt;
explicit AddAttribute(I2C2PAR_Modules::i2c2par *_i2cpt) :
Integer("Slave_Address", 0x27, "I2C Slave Address"), i2cpt(_i2cpt)
{
gint64 v;
Integer::get(v);
set(v);
}
virtual void set(gint64 v)
{
Integer::set(v);
if (i2cpt) {
i2cpt->i2c_slave_address = (v << 1);
}
}
};
class IOPort : public PortModule
//class IOPort : public PortRegister
{
public:
unsigned int direction;
// virtual void put(unsigned int new_value);
explicit IOPort(unsigned int _num_iopins = 8);
void update_pin_directions(unsigned int);
void put(unsigned int);
unsigned int get();
};
//IOPort::IOPort(unsigned int _num_iopins) : PortRegister(_num_iopins, "P", "")
IOPort::IOPort(unsigned int _num_iopins)
: PortModule(_num_iopins), direction(0)
{
}
void IOPort::put(unsigned int value)
{
for (int i = 0; i < 8; i++) {
IOPIN *m_pin;
unsigned int bit = 1 << i;
if ((m_pin = getPin(i))) {
m_pin->putState((value & bit) == bit);
}
}
}
unsigned int IOPort::get()
{
unsigned int value = 0;
for (int i = 0; i < 8; i++) {
IOPIN *m_pin;
unsigned int bit = 1 << i;
if ((m_pin = getPin(i))) {
if (m_pin->getState()) {
value |= bit;
}
}
}
return value;
}
void IOPort::update_pin_directions(unsigned int new_direction)
{
if ((new_direction ^ direction) & 1) {
direction = new_direction & 1;
for (int i = 0; i < 8; i++) {
IOPIN *m_pin;
if ((m_pin = getPin(i))) {
m_pin->update_direction(direction, true);
if (m_pin->snode) {
m_pin->snode->update();
}
}
}
}
}
namespace I2C2PAR_Modules {
i2c2par::i2c2par(const char *_name)
: i2c_slave(), Module(_name, "i2c2par"), pins(nullptr)
{
io_port = new IOPort(8);
Addattr = new AddAttribute(this);
addSymbol(Addattr);
//Addattr->set(0x27);
}
i2c2par::~i2c2par()
{
delete io_port;
delete Addattr;
for (int i = 0; i < 8; i++) {
removeSymbol(pins[i]);
}
delete [] pins;
removeSymbol((IOPIN *)scl);
removeSymbol((IOPIN *)sda);
// set sda, scl to zero as package deletes them,
// thus stopping ~i2c_slave from trying to delete them also
sda = nullptr;
scl = nullptr;
}
void i2c2par::put_data(unsigned int data)
{
Dprintf(("i2c2par::put_data() 0x%x\n", data));
io_port->put(data);
}
unsigned int i2c2par::get_data()
{
Dprintf(("i2c2par::get_data() 0x%x\n", io_port->get()));
return io_port->get();
}
void i2c2par::slave_transmit(bool input)
{
io_port->update_pin_directions(input == false);
}
bool i2c2par::match_address()
{
Dprintf(("i2c2par::match_address() 0x%x\n", xfr_data));
return ((xfr_data & 0xfe) == i2c_slave_address);
}
Module *i2c2par::construct(const char *_new_name)
{
std::string att_name = _new_name;
i2c2par *pEE = new i2c2par(_new_name);
pEE->create_iopin_map();
return pEE;
}
void i2c2par::create_iopin_map()
{
pins = new IO_bi_directional_pu *[8];
char pin_name[] = "p0";
addSymbol((IOPIN *)sda);
addSymbol((IOPIN *)scl);
package = new Package(10);
for (int i = 0; i < 8; i++) {
pin_name[1] = '0' + i;
pins[i] = new IO_bi_directional_pu(pin_name);
package->assign_pin(i < 4 ? i + 1 : i + 3, io_port->addPin(pins[i], i));
addSymbol(pins[i]);
}
package->assign_pin(5, (IOPIN *)(sda));
package->assign_pin(6, (IOPIN *)(scl));
}
} // end of namespace I2C2PAR_Modules
|