diff --git a/fast-servo/pyfastservo/si5340.py b/fast-servo/pyfastservo/si5340.py index 67132be..0b70371 100644 --- a/fast-servo/pyfastservo/si5340.py +++ b/fast-servo/pyfastservo/si5340.py @@ -35,6 +35,15 @@ STATUS_LOSREF = 0x04 STATUS_LOL = 0x08 +def write_register(bus, address, value): + try: + page = address >> 8 + register = address & 0xFF + bus.write_byte_data(IC_ADDR, 0x01, page) + bus.write_byte_data(IC_ADDR, register, value) + except Exception as e: + raise Exception(f"Write failed 0x{value:02X} at 0x{address:04X}: {e}") + def write_preamble(bus): preamble = [ (0x0B24, 0xC0), @@ -45,7 +54,7 @@ def write_preamble(bus): (0x0B4E, 0x1A), ] for address, value in preamble: - bus.write_byte_data(IC_ADDR, address, value) + write_register(bus, address, value) def write_postamble(bus): postamble = [ @@ -54,7 +63,7 @@ def write_postamble(bus): (0x0B25, 0x02), ] for address, value in postamble: - bus.write_byte_data(IC_ADDR, address, value) + write_register(bus, address, value) def wait_device_ready(bus): for _ in range(15): @@ -244,7 +253,7 @@ def configure_si5340(): print("Writing main configuration...") for address, value in main_config: - bus.write_byte_data(IC_ADDR, address, value) + write_register(bus, address, value) print("Main configuration written") write_postamble(bus)