Skip to content

Commit

Permalink
ads1115: Probe for reset value before init, do not spam console on de…
Browse files Browse the repository at this point in the history
…vice lost
  • Loading branch information
ThomasDebrunner authored and Thomas Stastny committed May 8, 2023
1 parent 38505c8 commit da14650
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/drivers/adc/ads1115/ADS1115.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ int ADS1115::init()
int ret = I2C::init();

if (ret != PX4_OK) {
PX4_ERR("I2C init failed");
return ret;
}

Expand All @@ -61,6 +60,24 @@ int ADS1115::init()
return PX4_OK;
}

int ADS1115::probe()
{
uint8_t buf[2] = {};
int ret = readReg(ADDRESSPOINTER_REG_CONFIG, buf, 2);

if (ret != PX4_OK) {
DEVICE_DEBUG("readReg failed (%i)", ret);
return ret;
}

if (buf[0] != CONFIG_RESET_VALUE_HIGH || buf[1] != CONFIG_RESET_VALUE_LOW) {
DEVICE_DEBUG("ADS1115 not found");
return PX4_ERROR;
}

return PX4_OK;
}

int ADS1115::setChannel(ADS1115::ChannelSelection ch)
{
uint8_t buf[2] = {};
Expand Down
7 changes: 7 additions & 0 deletions src/drivers/adc/ads1115/ADS1115.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@
#define CONFIG_LOW_COMP_QU_AFTER4 0x02
#define CONFIG_LOW_COMP_QU_DISABLE 0x03

#define CONFIG_RESET_VALUE_HIGH 0x85
#define CONFIG_RESET_VALUE_LOW 0x83

using namespace time_literals;

/*
Expand All @@ -114,6 +117,8 @@ class ADS1115 : public device::I2C, public I2CSPIDriver<ADS1115>

void RunImpl();

int probe() override;

protected:

void print_status() override;
Expand All @@ -132,6 +137,8 @@ class ADS1115 : public device::I2C, public I2CSPIDriver<ADS1115>

int _channel_cycle_count{0};

bool _reported_ready_last_cycle{false};

// ADS1115 logic part
enum ChannelSelection {
Invalid = -1, A0 = 0, A1, A2, A3
Expand Down
10 changes: 9 additions & 1 deletion src/drivers/adc/ads1115/ads1115_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ void ADS1115::RunImpl()
_adc_report.timestamp = hrt_absolute_time();

if (isSampleReady()) { // whether ADS1115 is ready to be read or not
if (!_reported_ready_last_cycle) {
PX4_INFO("ADS1115: reported ready");
_reported_ready_last_cycle = true;
}

int16_t buf;
ADS1115::ChannelSelection ch = cycleMeasure(&buf);
++_channel_cycle_count;
Expand Down Expand Up @@ -118,7 +123,10 @@ void ADS1115::RunImpl()
}

} else {
PX4_WARN("ADS1115 not ready!");
if (_reported_ready_last_cycle) {
_reported_ready_last_cycle = false;
PX4_ERR("ADS1115: not ready. Device lost?");
}
}

perf_end(_cycle_perf);
Expand Down

0 comments on commit da14650

Please sign in to comment.