"""Si5351A driver for the airband radio. MicroPython. The radio uses two outputs (BRINGUP_LOG.md): CLK0 = LO1 = receive frequency - 10.7 MHz, 2 mA drive CLK2 = LO2 = 10.25 MHz CLK1 is off. Each output has its own PLL and a fixed whole-number divider, so a retune rewrites only the eight PLL A registers. That is one short I2C burst, with no PLL reset and no click. """ import time ADDR = 0x60 LO1_DIV = 6 # PLL A runs at 6 x LO1: 644-758 MHz for 118-137 MHz receive LO2_DIV = 80 # PLL B runs at 80 x 10.25 MHz = 820 MHz PLL_A, PLL_B = 26, 34 # first register of each PLL's settings MS0, MS2 = 42, 58 # first register of each output divider's settings DRIVE = {2: 0, 4: 1, 6: 2, 8: 3} class Si5351: def __init__(self, i2c, xtal_hz=25_000_000, lo2_drive_ma=2): self.i2c = i2c self.xtal = xtal_hz self.lo2_drive = DRIVE[lo2_drive_ma] def present(self): try: self.i2c.readfrom_mem(ADDR, 0, 1) return True except OSError: return False def _write(self, reg, values): self.i2c.writeto_mem(ADDR, reg, bytes(values)) def _pll(self, reg, vco_hz): """Set a PLL to vco_hz = crystal x (a + b/c).""" c = 1048575 a, rest = divmod(vco_hz, self.xtal) b = rest * c // self.xtal p1 = 128 * a + 128 * b // c - 512 p2 = 128 * b - c * (128 * b // c) self._write(reg, (c >> 8 & 0xFF, c & 0xFF, p1 >> 16 & 0x03, p1 >> 8 & 0xFF, p1 & 0xFF, (c >> 12 & 0xF0) | (p2 >> 16 & 0x0F), p2 >> 8 & 0xFF, p2 & 0xFF)) def _divider(self, reg, n): """Set an output divider to the even whole number n.""" p1 = 128 * n - 512 self._write(reg, (0, 1, p1 >> 16 & 0x03, p1 >> 8 & 0xFF, p1 & 0xFF, 0, 0, 0)) def start(self, lo1_hz, lo2_hz): """Full setup from power-on. Raises OSError if the chip doesn't answer.""" for _ in range(50): # the chip is busy for a moment after power-on if not self.i2c.readfrom_mem(ADDR, 0, 1)[0] & 0x80: break time.sleep_ms(10) self._write(3, (0xFF,)) # all outputs off while setting up self._write(16, (0x80,) * 8) # all eight output drivers powered down self._divider(MS0, LO1_DIV) self._divider(MS2, LO2_DIV) self._pll(PLL_A, lo1_hz * LO1_DIV) self._pll(PLL_B, lo2_hz * LO2_DIV) self._write(16, (0x4C,)) # CLK0: whole-number divider, PLL A, 2 mA self._write(18, (0x6C | self.lo2_drive,)) # CLK2: whole-number divider, PLL B self._write(177, (0xA0,)) # reset both PLLs once self._write(3, (0xFA,)) # CLK0 and CLK2 on, the rest off def tune(self, lo1_hz): """Move LO1. One 9-byte I2C write.""" self._pll(PLL_A, lo1_hz * LO1_DIV)