Skip to content

Commit

Permalink
fix si5351 i2c handle
Browse files Browse the repository at this point in the history
  • Loading branch information
paulh002 committed Jun 8, 2023
1 parent d4ac379 commit 7cf0d62
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
14 changes: 7 additions & 7 deletions SoapyHifiBerrySettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Device interface
**********************************************************************/
const cfg::File::ConfigMap defaultOptions = {
{"si5351", {{"correction", cfg::makeOption("0")}, {"mode", cfg::makeOption("single")} , {"rxdrive", cfg::makeOption("2")}, {"t.xdrive", cfg::makeOption("2")}}},
{"si5351", {{"correction", cfg::makeOption("0")}, {"mode", cfg::makeOption("single")} , {"rxdrive", cfg::makeOption("8")}, {"t.xdrive", cfg::makeOption("8")}}},
{"sound", {{"device", cfg::makeOption("snd_rpi_hifiberry_dacplusadcpro")}, {"samplerate", cfg::makeOption("192000")}, {"input", cfg::makeOption("DIFF")}}}};

int SoapyHifiBerry::get_int(string section, string key)
Expand Down Expand Up @@ -39,7 +39,7 @@ SoapyHifiBerry::SoapyHifiBerry(const SoapySDR::Kwargs &args)
SoapySDR_setLogLevel(SOAPY_SDR_INFO);
SoapySDR_log(SOAPY_SDR_INFO, "SoapyHifiBerry::SoapyHifiBerry constructor called");
no_channels = 1;
txDrive = rxDrive = SI5351_DRIVE_2MA;
txDrive = rxDrive = SI5351_DRIVE_8MA;
modeIQ = false;

uptr_cfg = make_unique<cfg::File>();
Expand All @@ -63,7 +63,7 @@ SoapyHifiBerry::SoapyHifiBerry(const SoapySDR::Kwargs &args)
rxDrive = SI5351_DRIVE_8MA;
break;
default:
rxDrive = SI5351_DRIVE_2MA;
rxDrive = SI5351_DRIVE_8MA;
break;
}

Expand All @@ -80,7 +80,7 @@ SoapyHifiBerry::SoapyHifiBerry(const SoapySDR::Kwargs &args)
txDrive = SI5351_DRIVE_8MA;
break;
default:
txDrive = SI5351_DRIVE_2MA;
txDrive = SI5351_DRIVE_8MA;
break;
}

Expand Down Expand Up @@ -145,7 +145,7 @@ SoapyHifiBerry::SoapyHifiBerry(const SoapySDR::Kwargs &args)
{
pTCA9548 = std::make_unique<TCA9548>("/dev/i2c-1", 0x70);
pTCA9548->begin(1);
pSI5351 = make_unique<Si5351>("/dev/i2c-1", SI5351_BUS_BASE_ADDR);
pSI5351 = make_unique<Si5351>("/dev/i2c-1", SI5351_BUS_BASE_ADDR, CLK_VFO_I, CLK_VFO_Q);
if (pSI5351->init(SI5351_CRYSTAL_LOAD_8PF, 0, 0))
{
pSI5351->drive_strength(CLK_VFO_I, rxDrive);
Expand All @@ -156,8 +156,8 @@ SoapyHifiBerry::SoapyHifiBerry(const SoapySDR::Kwargs &args)
pSI5351->update_status();
}
pTCA9548->setChannelMask(2);
pSI5351tx = make_unique<Si5351>("/dev/i2c-1", SI5351_BUS_BASE_ADDR);
if (pSI5351tx->init(SI5351_CRYSTAL_LOAD_8PF, 0, 0))
pSI5351tx = make_unique<Si5351>("/dev/i2c-1", SI5351_BUS_BASE_ADDR, CLK_VFO_I, CLK_VFO_Q);
if (pSI5351tx->init(SI5351_CRYSTAL_LOAD_8PF, 0, 0, pSI5351->getFileHandle()))
{
pSI5351tx->drive_strength(CLK_VFO_I, rxDrive);
pSI5351tx->drive_strength(CLK_VFO_Q, rxDrive);
Expand Down
31 changes: 19 additions & 12 deletions si5351.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,27 @@ Si5351::~Si5351()
* I2C address.
*
*/
bool Si5351::init(uint8_t xtal_load_c, uint32_t xo_freq, int32_t corr)
bool Si5351::init(uint8_t xtal_load_c, uint32_t xo_freq, int32_t corr, int fileHandle)
{
//Open i2c device
int file;
if((file = open(i2c_filepath.data(), O_RDWR)) < 0) {
return false;
}
i2c_file = file;
if (fileHandle)
i2c_file = fileHandle;
else
{
int file;
if ((file = open(i2c_filepath.data(), O_RDWR)) < 0)
{
return false;
}
i2c_file = file;

//Set address
if(ioctl(file, I2C_SLAVE_FORCE, i2c_bus_addr) < 0) {
return false;
}
// Wait for SYS_INIT flag to be clear, indicating that device is ready
//Set address
if (ioctl(file, I2C_SLAVE_FORCE, i2c_bus_addr) < 0)
{
return false;
}
}
// Wait for SYS_INIT flag to be clear, indicating that device is ready
uint8_t status_reg = 0;
do{
status_reg = si5351_read(SI5351_DEVICE_STATUS);
Expand Down Expand Up @@ -1829,7 +1836,7 @@ uint8_t Si5351::select_r_div_ms67(uint64_t *freq)
return r_div;
}

void Si5351::setIQFrequency(long freq)
void Si5351::setIQFrequency(int freq)
{
int mult = 0;

Expand Down
5 changes: 3 additions & 2 deletions si5351.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,11 @@ class Si5351
public:
Si5351(const char *i2c_device_filepath, uint8_t i2c_addr = SI5351_BUS_BASE_ADDR, si5351_clock iclock = SI5351_CLK0, si5351_clock qclock = SI5351_CLK1);
~Si5351();
bool init(uint8_t, uint32_t, int32_t);
bool init(uint8_t, uint32_t, int32_t, int fileHandle = 0);
void reset(void);
uint8_t set_freq(uint64_t, enum si5351_clock);
uint8_t set_freq_manual(uint64_t, uint64_t, enum si5351_clock);
void setIQFrequency(long freq);
void setIQFrequency(int freq);
void set_pll(uint64_t, enum si5351_pll);
void set_ms(enum si5351_clock, struct Si5351RegSet, uint8_t, uint8_t, uint8_t);
void output_enable(enum si5351_clock, uint8_t);
Expand Down Expand Up @@ -354,6 +354,7 @@ class Si5351
enum si5351_pll_input plla_ref_osc;
enum si5351_pll_input pllb_ref_osc;
uint32_t xtal_freq[2];
int getFileHandle() { return i2c_file; }

private:
uint64_t pll_calc(enum si5351_pll, uint64_t, struct Si5351RegSet *, int32_t, uint8_t);
Expand Down

0 comments on commit 7cf0d62

Please sign in to comment.