From f2645a53b9ea829285bbd8e8ab4f54e4ea225976 Mon Sep 17 00:00:00 2001 From: Sylvain Pelissier Date: Wed, 22 May 2019 20:08:49 +0200 Subject: [PATCH] Add contrib script for 93c46 EEPROM --- contrib/pyHydra_93c46/README.md | 13 +++++ contrib/pyHydra_93c46/hydra_93c46.py | 73 ++++++++++++++++++++++++ contrib/pyHydrabus/pyHydrabus/auxpin.py | 2 +- contrib/pyHydrabus/pyHydrabus/rawwire.py | 13 ++++- 4 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 contrib/pyHydra_93c46/README.md create mode 100755 contrib/pyHydra_93c46/hydra_93c46.py diff --git a/contrib/pyHydra_93c46/README.md b/contrib/pyHydra_93c46/README.md new file mode 100644 index 00000000..a45a684a --- /dev/null +++ b/contrib/pyHydra_93c46/README.md @@ -0,0 +1,13 @@ +Script to communicate with Hydrabus to 93c46 3-wire EEPROM. + +The script uses the pyHydrabus library. +This script requires Python 3.2+ + +Dependencies: +pip3 install serial + +Author: Sylvain Pelissier + +License: GPLv3 (https://choosealicense.com/licenses/gpl-3.0/) + +(if license conflicts with hydrafw license, hydrafw license prevails) diff --git a/contrib/pyHydra_93c46/hydra_93c46.py b/contrib/pyHydra_93c46/hydra_93c46.py new file mode 100755 index 00000000..c215e1be --- /dev/null +++ b/contrib/pyHydra_93c46/hydra_93c46.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 +# +# Script to communicate to 94c46 3-wire EEPROM with Hydrabus. +# +# Author: Sylvain Pelissier +# License: GPLv3 (https://choosealicense.com/licenses/gpl-3.0/) +# +import pyHydrabus + +from binascii import hexlify + +def write_enable(): + # Write enable is 10011XXXX + cs.value=1 + sm.sda = 1 + sm.clock() + sm.sda = 0 + sm.clocks(2) + sm.sda = 1 + sm.clocks(8) + cs.value=0 + +def write_disable(): + # Write disable is 10000XXXXX + cs.value=1 + sm.sda = 1 + sm.clock() + sm.sda = 0 + sm.clocks(10) + cs.value=0 + +def read(address, num): + address = address & 0x3f + cs.value=1 + sm.write(bytes([0x01, 0x80 | address])) + data = sm.read(num) + cs.value=0 + + return data + +def write(address, data): + + if len(data) % 2 == 1: + print("Data length must be even") + exit(2) + + for i in range(0, len(data) , 2): + cs.value=1 + sm.write(bytes([0x01, 0x40 | address+i//2]) + data[i:i+2]) + cs.value=0 + +if __name__ == '__main__': + sm=pyHydrabus.RawWire('/dev/ttyACM0') + sm.gpio_mode = 1 + sm.set_speed(50000) + sm.wires = 3 + + # CS pin configuration with auxiliary pin PC4 + cs = sm.AUX[0] + cs.direction=0 + cs.value=0 + + # Read memory cotent + print(hexlify(read(0, 128))) + + # Write 4 bytes of data + write_enable() + data = b"\xa5\xa6\x45\x46" + write(0,data) + write_disable() + + # Read memory cotent + print(hexlify(read(0, 128))) diff --git a/contrib/pyHydrabus/pyHydrabus/auxpin.py b/contrib/pyHydrabus/pyHydrabus/auxpin.py index 7fff5aa6..bbbbf810 100644 --- a/contrib/pyHydrabus/pyHydrabus/auxpin.py +++ b/contrib/pyHydrabus/pyHydrabus/auxpin.py @@ -12,7 +12,7 @@ class AUXPin(): :example: >>> import pyHydrabus - >>> i=pyHydrabus.I2C('/dev/hydrabus') + >>> i=pyHydrabus.RawWire('/dev/hydrabus') >>> # Set AUX pin 0 (PC4) to output >>> i.AUX[0].direction = 0 >>> # Set AUX pin to logical 1 diff --git a/contrib/pyHydrabus/pyHydrabus/rawwire.py b/contrib/pyHydrabus/pyHydrabus/rawwire.py index 4104c1d2..6d477c66 100644 --- a/contrib/pyHydrabus/pyHydrabus/rawwire.py +++ b/contrib/pyHydrabus/pyHydrabus/rawwire.py @@ -23,7 +23,14 @@ class RawWire(Protocol): :example: - TODO + >>> import pyHydrabus + >>> i=pyHydrabus.I2C('/dev/hydrabus') + >>> # Set SDA to high + >>> sm.sda = 1 + >>> # Send two clock ticks + >>> sm.clocks(2) + >>> # Read two bytes + >>> data = sm.read(2) """ @@ -254,7 +261,7 @@ def wires(self, value): @property def gpio_mode(self): """ - Raw-Wire GPIO mode (1=Push-Pull, 2=Open Drain) + Raw-Wire GPIO mode (0=Push-Pull, 1=Open Drain) """ return (self._config & 0b1000) >> 3 @@ -269,5 +276,5 @@ def gpio_mode(self, value): self._configure_port() return True else: - self._logger.error("Incorrect value. Must be 1 or 2") + self._logger.error("Incorrect value. Must be 0 or 1")