Last active
May 19, 2019 21:02
-
-
Save darksinge/3b812b4bd9060bb67f1e169f81414bb2 to your computer and use it in GitHub Desktop.
Python script to control a Raspberry Pi's GPIO pins for programming an EEPROM (work in progress...)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RPi.GPIO as GPIO | |
import os | |
import time | |
import math | |
usleep = lambda x: time.sleep(x / 1000000.0) | |
GPIO.setwarnings(False) | |
class EEPROMProgrammer(object): | |
@property | |
def address_pins(self): | |
return [9, 10, 22, 27, 17, 4, 3, 2, 18, 23, 24] | |
@property | |
def data_pins(self): | |
return [5, 0, 11, 20, 16, 12, 1, 7] | |
def __init__(self): | |
self.we = 14 | |
self.oe = 15 | |
self.on_init() | |
def on_init(self): | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(self.we, GPIO.OUT) | |
GPIO.setup(self.oe, GPIO.OUT) | |
GPIO.output([self.we, self.oe], 1) | |
GPIO.setup(list(self.address_pins), GPIO.OUT, initial=0) | |
GPIO.setup(list(self.data_pins), GPIO.OUT, initial=0) | |
usleep(2000) # wait for write inhibit max time | |
def set_address(self, addr): # type: (list) -> None | |
addr = self._tobinary(addr, size=len(self.address_pins)) | |
for i, bit in enumerate(addr): | |
try: | |
GPIO.output(self.address_pins[i], bit) | |
except IndexError: | |
print(self.address_pins, i) | |
raise | |
def set_bits(self, bits): # type: (list) -> None | |
if isinstance(bits, str): | |
bits = list(map(int, bits)) | |
for i, bit in enumerate(bits): | |
if not bit: | |
bit = 0 | |
else: | |
bit = 1 | |
GPIO.output(self.data_pins[i], bit) | |
def cleanup(self): | |
GPIO.cleanup() | |
self.on_init() | |
def _tolist(self, s): | |
if isinstance(s, str): | |
return list(map(int, s)) | |
return s | |
def _tobinary(self, x, size=10): # type: (int) -> list | |
proto_str = '{0:0' + str(size) + 'b}' | |
if size == 10: | |
return self._tolist('{0:010b}'.format(x))[::-1] | |
else: | |
return self._tolist(proto_str.format(x))[::-1] | |
def we_enable(self): | |
GPIO.output(self.we, 0) | |
def we_disable(self): | |
GPIO.output(self.we, 1) | |
def oe_enable(self): | |
GPIO.output(self.oe, 0) | |
def oe_disable(self): | |
GPIO.output(self.oe, 1) | |
def write(self, bits, addr): | |
## usleep(7000) | |
## bits = bits[::-1] | |
self.we_disable() | |
self.oe_disable() | |
self.set_address(addr) | |
self.set_bits(bits) | |
usleep(200) | |
self.we_enable() | |
self.set_bits(bits) | |
self.we_disable() | |
usleep(7000) | |
def continue_prompt(): | |
x = input("continue ([y]/n)? ").lower() | |
return x != 'n' | |
if __name__ == '__main__': | |
prog = EEPROMProgrammer() | |
prog.oe_disable() | |
i = 0 | |
## for i in range(8): | |
## b10 = int(math.pow(2, i)) | |
## bits = prog._tobinary(b10, size=8) | |
## bits = "".join(map(str, bits)) | |
## prog.write(bits, i) | |
number_bits = [ | |
'1111110', | |
'0110000', | |
'1101101', | |
'1111001', | |
'0110011', | |
'1011011', | |
'1011111', | |
'1110000', | |
'1111111', | |
'1110011', | |
'1110111', | |
'0011111', | |
'1001110', | |
'0111101', | |
'1001111', | |
'1000111' | |
] | |
number_bits = map(lambda x: x[::-1] + '0', number_bits) | |
for i, num in enumerate(number_bits): | |
prog.write(num, i) | |
for i in range(16, 256): | |
bits = prog._tobinary(i, size=8) | |
bits = "".join(map(str, bits)) | |
## prog.write(bits, i) | |
prog.write('11111111', i) | |
prog.oe_enable() | |
while 0: | |
addr_b10 = int(math.pow(2, i)) | |
prog.set_address(addr_b10) | |
addr = prog._tobinary(addr_b10, size=len(prog.address_pins)) | |
print(i, addr) | |
if not continue_prompt(): | |
break | |
if i < 10: | |
i += 1 | |
else: | |
i = 0 | |
j = 0 | |
prog.oe_disable() | |
while 0: | |
bits = prog._tobinary(j, size=8) | |
bits = "".join(map(str, bits)) | |
prog.set_bits(bits) | |
print(j, bits) | |
if not continue_prompt(): | |
break | |
if j < 255: | |
j += 1 | |
else: | |
j = 0 | |
prog.oe_enable() | |
GPIO.cleanup(prog.data_pins) | |
n = 15 | |
i = 0 | |
while 1: | |
prog.set_address(i) | |
print(i) | |
## if not continue_prompt(): | |
## break | |
time.sleep(0.5) | |
if i < n: | |
i += 1 | |
else: | |
i = 0 | |
prog.cleanup() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment