#!/bin/env python3 # Copyright (C) 2015-2026 Herbert Poetzl import sys import serial import struct from smbus import SMBus from time import sleep from icsp import * tty = "/dev/ttyPS1" sel = sys.argv[1] if len(sys.argv) > 1 else "A" ser = serial.Serial( port = tty, baudrate = 10000000, bytesize = serial.EIGHTBITS, parity = serial.PARITY_NONE, stopbits = serial.STOPBITS_ONE, # interCharTimeout = 0.2, timeout = 0.1, xonxoff = False, rtscts = False, dsrdtr = False); i2c0 = SMBus(0) i2c2 = SMBus(2) print(icsp_cmd(ser, b'Z')) # tristate MCLR (icsp) pb_ver = "0.30" try: if i2c2.read_byte(0x30) >= 0: pb_ver = "0.29" except: pass try: if i2c2.read_byte(0x70) >= 0: pb_ver = "0.23" except: pass print("assuming power board version", pb_ver); if sel == "A": # toggle A_!RST print("selecting RFW [bus A] ...") if pb_ver == "0.30": iob = i2c0.read_byte_data(0x22, 0x15) i2c0.write_byte_data(0x22, 0x15, iob&0x3F|0x40) elif pb_ver == "0.29": i2c2.write_byte(0x30, 0x2) # steer mux else: i2c2.write_byte(0x70, 0x5) # steer mux ioa = i2c0.read_byte_data(0x23, 0x14) i2c0.write_byte_data(0x23, 0x14, ioa&~0x10) elif sel == "B": # toggle B_!RST print("selecting RFE [bus B] ...") if pb_ver == "0.30": iob = i2c0.read_byte_data(0x22, 0x15) i2c0.write_byte_data(0x22, 0x15, iob&0x3F|0x80) elif pb_ver == "0.29": i2c2.write_byte(0x30, 0x1) # steer mux else: i2c2.write_byte(0x70, 0x4) # steer mux iob = i2c0.read_byte_data(0x22, 0x14) i2c0.write_byte_data(0x22, 0x14, iob&~0x10) print(icsp_cmd(ser, b'L')) # take MCLR low (icsp) print(icsp_cmd(ser, b'#', 9)) # reset checksum print(icsp_enter_lvp(ser)) # enter LVP print("reading config memory ...") icsp_load_conf(ser) conf = icsp_read_data(ser, 0x11) print([hex(_) for _ in conf]) if conf[6] == 0x305b and conf[5] in (0x2000, 0x2001): print("pic16f1718 found.") prog = True if conf[5] == 0x2004 and conf[6] == 0x3042: print("pic16f1708 found.") prog = True print(icsp_cmd(ser, b'#', 9)) # retrieve checksum print(icsp_cmd(ser, b'Z')) # tristate MCLK (icsp) if pb_ver == "0.29": print("mux = 0x%02x" % i2c2.read_byte(0x30)) if sel == "A": # bring A_!RST high again i2c0.write_byte_data(0x23, 0x14, ioa|0x10) elif sel == "B": # bring B_!RST high again i2c0.write_byte_data(0x22, 0x14, iob|0x10) elif sel == "P": pass else: if pb_ver == "0.30": iob = i2c0.read_byte_data(0x22, 0x15) i2c0.write_byte_data(0x22, 0x15, iob&0x3F) elif pb_ver == "0.29": i2c2.write_byte(0x30, 0x0) # disable mux else: i2c2.write_byte(0x70, 0x0) # disable mux