diff options
| author | Benedict <benedict@0xb8000.de> | 2018-04-12 13:29:03 +0200 |
|---|---|---|
| committer | Benedict <benedict@0xb8000.de> | 2018-04-12 13:29:03 +0200 |
| commit | 849f42ed497a0fb4e604b206022c32df6164369b (patch) | |
| tree | f18b1f36626c6cd3677669c2953104245ba2cf85 /eeprom_pi.py | |
pi: eeprom: added file to read/write AT28C EEPROM
This lets you write the AT28 EEPROM with the Raspberry Pi.
Diffstat (limited to 'eeprom_pi.py')
| -rw-r--r-- | eeprom_pi.py | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/eeprom_pi.py b/eeprom_pi.py new file mode 100644 index 0000000..2b46892 --- /dev/null +++ b/eeprom_pi.py @@ -0,0 +1,129 @@ +import RPi.GPIO as GPIO +import time + +def cleanup(): + GPIO.cleanup() + +def init_board(write = True): + GPIO.setmode(GPIO.BOARD) + + if write: + oparm = GPIO.OUT + else: + oparm = GPIO.IN + + # GPIO 0-7 + # used as data pins, configure as input/output + GPIO.setup(11, oparm) + GPIO.setup(12, oparm) + GPIO.setup(13, oparm) + GPIO.setup(15, oparm) + GPIO.setup(16, oparm) + GPIO.setup(18, oparm) + GPIO.setup(22, oparm) + GPIO.setup(7, oparm) + + # left side of T-cobbler, used as address pins + # configure address pins always out + GPIO.setup(26, GPIO.OUT) + GPIO.setup(24, GPIO.OUT) + GPIO.setup(23, GPIO.OUT) + GPIO.setup(21, GPIO.OUT) + GPIO.setup(19, GPIO.OUT) + GPIO.setup(10, GPIO.OUT) + GPIO.setup(8, GPIO.OUT) + + # used to trigger a write + GPIO.setup(5, GPIO.OUT) + + # used to read + GPIO.setup(3, GPIO.OUT) + + # set the pins to high, pins are active low + GPIO.output(5, True) + GPIO.output(3, True) + + + +def read_data(): + data = (GPIO.input(11)) + print(GPIO.input(11)) + data |= (GPIO.input(12) << 0x1) + print(GPIO.input(12)) + data |= (GPIO.input(13) << 0x2) + print(GPIO.input(13)) + data |= (GPIO.input(15) << 0x3) + print(GPIO.input(15)) + data |= (GPIO.input(16) << 0x4) + print(GPIO.input(16)) + data |= (GPIO.input(18) << 0x5) + print(GPIO.input(18)) + data |= (GPIO.input(22) << 0x6) + print(GPIO.input(22)) + data |= (GPIO.input(7) << 0x7) + print(GPIO.input(7)) + print("read data: %s" % data) + return data + +# data should be one byte +def set_data(data): + GPIO.output(11, (data & 0x1) > 0) + print((data & 0x1) > 0) + GPIO.output(12, (data & 0x2) > 0) + print((data & 0x2) > 0) + GPIO.output(13, (data & 0x4) > 0) + print((data & 0x4) > 0) + GPIO.output(15, (data & 0x8) > 0) + print((data & 0x8) > 0) + GPIO.output(16, (data & 0x10) > 0) + print((data & 0x10) > 0) + GPIO.output(18, (data & 0x20) > 0) + print((data & 0x20) > 0) + GPIO.output(22, (data & 0x40) > 0) + print((data & 0x40) > 0) + GPIO.output(7, (data & 0x80) > 0) + print((data & 0x80) > 0) + +# address should be one byte +def set_address(address): + GPIO.output(26, (address & 0x1) > 0) + print((address & 0x1) > 0) + GPIO.output(24, (address & 0x2) > 0) + print((address & 0x2) > 0) + GPIO.output(23, (address & 0x4) > 0) + print((address & 0x4) > 0) + GPIO.output(21, (address & 0x8) > 0) + print((address & 0x8) > 0) + GPIO.output(19, (address & 0x10) > 0) + print((address & 0x10) > 0) + GPIO.output(10, (address & 0x20) > 0) + print((address & 0x20) > 0) + GPIO.output(8, (address & 0x40) > 0) + print((address & 0x40) > 0) + # for DEBUG use only 7 address bits to have one left for read trigger + #GPIO.output(5, (address & 0x80) > 0) + #print((address & 0x80) > 0) + +def write_data_at(address, data): + print("set address to: %s" % address) + set_address(address) + print("set data to: %s" % data) + set_data(data) + # wait so everythings is stable + time.sleep(0.3) + # trigger write on pin 5 + GPIO.output(5, False) + time.sleep(0.3) + GPIO.output(5, True) + + +def read_data_at(address): + print("set address to: %s" % address) + set_address(address) + # wait a little bit so the address is stable + time.sleep(0.3) + # trigger output enable + GPIO.output(3, False) + data = read_data() + GPIO.output(3, True) + print("read data %s" % data) |
