diff --git a/mdio.py b/mdio.py index 553fee93fecfb9d303dd0be0658003bdd5e20ee3..af67a1c85720064e8f9afa2db95c661f521a34a8 100644 --- a/mdio.py +++ b/mdio.py @@ -6,84 +6,167 @@ import time as time ClockPin = 23 DataPin = 22 -print("write data") - -phy_reg = 0x16 -phy_reg_bits = "{:0>5b}".format(phy_reg) -stri = "Phy reg bits is {}".format(phy_reg_bits) -print(stri) - -phy_addr = 0x1E -phy_addr_bits = "{:0>5b}".format(phy_addr) -stri = "Phy addr bits is {}".format(phy_addr_bits) -print(stri) - -phy_data = 0x16 -phy_data_bits = "{:0>16b}".format(phy_reg) -stri = "Phy data bits is {}".format(phy_data_bits) -print(stri) - -# Set IO pins to right mode -GPIO.setmode(GPIO.BCM) -GPIO.setup(ClockPin, GPIO.OUT) -GPIO.setup(DataPin, GPIO.OUT) - -# Preamble -GPIO.output(DataPin, False) -GPIO.output(ClockPin, False) -for preamble_cnt in range(32): +def write_mdio(wr_data=0x16): + + print("write data") + + phy_reg = 0x16 + phy_reg_bits = "{:0>5b}".format(phy_reg) + stri = "Phy reg bits is {}".format(phy_reg_bits) + print(stri) + + phy_addr = 0x1E + phy_addr_bits = "{:0>5b}".format(phy_addr) + stri = "Phy addr bits is {}".format(phy_addr_bits) + print(stri) + + phy_data = wr_data + phy_data_bits = "{:0>16b}".format(phy_reg) + stri = "Phy data bits is {}".format(phy_data_bits) + print(stri) + + # Set IO pins to right mode + GPIO.setmode(GPIO.BCM) + GPIO.setup(ClockPin, GPIO.OUT) + GPIO.setup(DataPin, GPIO.OUT) + + # Preamble + GPIO.output(DataPin, False) + GPIO.output(ClockPin, False) + for preamble_cnt in range(32): + GPIO.output(ClockPin, True) + # time.sleep(0.1) + GPIO.output(ClockPin, False) + # time.sleep(0.1) + + # Start of Frame + GPIO.output(DataPin, False) GPIO.output(ClockPin, True) -# time.sleep(0.1) GPIO.output(ClockPin, False) -# time.sleep(0.1) - -# Start of Frame -GPIO.output(DataPin, False) -GPIO.output(ClockPin, True) -GPIO.output(ClockPin, False) -GPIO.output(DataPin, True) -GPIO.output(ClockPin, True) -GPIO.output(ClockPin, True) - -# Operant Write -GPIO.output(DataPin, False) -GPIO.output(ClockPin, True) -GPIO.output(ClockPin, False) -GPIO.output(DataPin, False) -GPIO.output(ClockPin, True) -GPIO.output(ClockPin, True) - -# Phy address -for bit in phy_addr_bits: - if bit == '1': - GPIO.output(DataPin, True) - else: - GPIO.output(DataPin, False) + GPIO.output(DataPin, True) GPIO.output(ClockPin, True) GPIO.output(ClockPin, False) -# phy reg -for bit in phy_reg_bits: - if bit == '1': - GPIO.output(DataPin, True) - else: - GPIO.output(DataPin, False) + # Operant Write + GPIO.output(DataPin, False) GPIO.output(ClockPin, True) GPIO.output(ClockPin, False) + GPIO.output(DataPin, True) + GPIO.output(ClockPin, True) + GPIO.output(ClockPin, False) + + # Phy address + for bit in phy_addr_bits: + if bit == '1': + GPIO.output(DataPin, True) + else: + GPIO.output(DataPin, False) + GPIO.output(ClockPin, True) + GPIO.output(ClockPin, False) -# TA -GPIO.output(ClockPin, True) -GPIO.output(ClockPin, False) -GPIO.output(ClockPin, True) -GPIO.output(ClockPin, False) - -#data -for bit in phy_data_bits: - if bit == '1': - GPIO.output(DataPin, True) - else: - GPIO.output(DataPin, False) + # phy reg + for bit in phy_reg_bits: + if bit == '1': + GPIO.output(DataPin, True) + else: + GPIO.output(DataPin, False) + GPIO.output(ClockPin, True) + GPIO.output(ClockPin, False) + + # TA + GPIO.output(ClockPin, True) + GPIO.output(ClockPin, False) GPIO.output(ClockPin, True) GPIO.output(ClockPin, False) -GPIO.cleanup() \ No newline at end of file + #data + for bit in phy_data_bits: + if bit == '1': + GPIO.output(DataPin, True) + else: + GPIO.output(DataPin, False) + GPIO.output(ClockPin, True) + GPIO.output(ClockPin, False) + GPIO.cleanup() + +def read_mdio(): + print("write data") + + phy_reg = 0x16 + phy_reg_bits = "{:0>5b}".format(phy_reg) + stri = "Phy reg bits is {}".format(phy_reg_bits) + print(stri) + + phy_addr = 0x1E + phy_addr_bits = "{:0>5b}".format(phy_addr) + stri = "Phy addr bits is {}".format(phy_addr_bits) + print(stri) + + # Set IO pins to right mode + GPIO.setmode(GPIO.BCM) + GPIO.setup(ClockPin, GPIO.OUT) + GPIO.setup(DataPin, GPIO.OUT) + + # Preamble + GPIO.output(DataPin, False) + GPIO.output(ClockPin, False) + for preamble_cnt in range(32): + GPIO.output(ClockPin, True) + # time.sleep(0.1) + GPIO.output(ClockPin, False) + # time.sleep(0.1) + + # Start of Frame + GPIO.output(DataPin, False) + GPIO.output(ClockPin, True) + GPIO.output(ClockPin, False) + GPIO.output(DataPin, True) + GPIO.output(ClockPin, True) + GPIO.output(ClockPin, False) + + # Operant Read + GPIO.output(DataPin, True) + GPIO.output(ClockPin, True) + GPIO.output(ClockPin, False) + GPIO.output(DataPin, False) + GPIO.output(ClockPin, True) + GPIO.output(ClockPin, False) + + # Phy address + for bit in phy_addr_bits: + if bit == '1': + GPIO.output(DataPin, True) + else: + GPIO.output(DataPin, False) + GPIO.output(ClockPin, True) + GPIO.output(ClockPin, False) + + # phy reg + for bit in phy_reg_bits: + if bit == '1': + GPIO.output(DataPin, True) + else: + GPIO.output(DataPin, False) + GPIO.output(ClockPin, True) + GPIO.output(ClockPin, False) + + GPIO.setup(DataPin, GPIO.IN) + # TA + GPIO.output(ClockPin, True) + GPIO.output(ClockPin, False) + GPIO.output(ClockPin, True) + GPIO.output(ClockPin, False) + + # data + data=[] + for bit in phy_data_bits: + GPIO.output(ClockPin, True) + data.append(GPIO.input(DataPin)) + GPIO.output(ClockPin, False) + + print(data) + GPIO.cleanup() + +write_mdio(0x16) +read_mdio() +