#ifndef __SI5351_H__
#define __SI5351_H__ __SI5351_H__

#include <stdint.h>

#include "twi.h"

static const uint8_t SI5351_ADDRESS = 96;

enum si5351_pll {
    SI5351_PLLA = 0,
    SI5351_PLLB = 1,
};

static const uint8_t SI5351_PLL_BASE_ADDR[] = {26, 34};

enum si5351_multisynth {
    SI5351_MS0 = 0,
    SI5351_MS1 = 1,
    SI5351_MS2 = 2,
};

static const uint8_t SI5351_MULTISYNTH_BASE[] = {42, 50, 58};
static const uint8_t SI5351_CLK_CTRL[] = {16, 17, 18};

enum si5351_reg {
    CLK_ENABLE_CONTROL = 3,
    PLLX_SRC = 15,
    PLL_RESET = 177,
    XTAL_LOAD_CAP = 183
};


struct si5351_params {
    uint32_t p1;
    uint32_t p2;
    uint32_t p3;
};

static inline uint8_t si5351_write8(uint8_t reg, uint8_t value) {
    uint8_t data[3] = {reg, value};
    return twi_write(SI5351_ADDRESS, data, 2);
}

void si5351_init(uint32_t freq_xtal, uint32_t freq_a, uint32_t freq_b);
void si5351_ms_enable_output(enum si5351_multisynth synth);
void si5351_ms_disable_output(enum si5351_multisynth synth);
void si5351_ms_set_source(enum si5351_multisynth synth, enum si5351_pll pll);
void si5351_ms_set_freq(enum si5351_multisynth synth, uint32_t freq);

uint8_t si5351_write_params(uint8_t base, struct si5351_params params);

static inline void si5351_ms_write_params(enum si5351_multisynth synth, struct si5351_params params) {
    si5351_write_params(SI5351_MULTISYNTH_BASE[synth], params);
}


#endif
