mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-03 11:38:56 +08:00
drivers: use updated I2C SPI driver interface
This commit is contained in:
@@ -47,7 +47,18 @@ int ADS1115::init()
|
||||
config[0] = CONFIG_HIGH_OS_NOACT | CONFIG_HIGH_MUX_P0NG | CONFIG_HIGH_PGA_6144 | CONFIG_HIGH_MODE_SS;
|
||||
config[1] = CONFIG_LOW_DR_250SPS | CONFIG_LOW_COMP_MODE_TRADITIONAL | CONFIG_LOW_COMP_POL_RESET |
|
||||
CONFIG_LOW_COMP_LAT_NONE | CONFIG_LOW_COMP_QU_DISABLE;
|
||||
return writeReg(ADDRESSPOINTER_REG_CONFIG, config, 2);
|
||||
ret = writeReg(ADDRESSPOINTER_REG_CONFIG, config, 2);
|
||||
|
||||
if (ret != PX4_OK) {
|
||||
PX4_ERR("writeReg failed (%i)", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
setChannel(ADS1115::A0); // prepare for the first measure.
|
||||
|
||||
ScheduleOnInterval(SAMPLE_INTERVAL / 4, SAMPLE_INTERVAL / 4);
|
||||
|
||||
return PX4_OK;
|
||||
}
|
||||
|
||||
int ADS1115::setChannel(ADS1115::ChannelSelection ch)
|
||||
@@ -194,4 +205,4 @@ int ADS1115::writeReg(uint8_t addr, uint8_t *buf, size_t len)
|
||||
buffer[0] = addr;
|
||||
memcpy(buffer + 1, buf, sizeof(uint8_t)*len);
|
||||
return transfer(buffer, len + 1, nullptr, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,24 +105,17 @@ using namespace time_literals;
|
||||
class ADS1115 : public device::I2C, public I2CSPIDriver<ADS1115>
|
||||
{
|
||||
public:
|
||||
ADS1115(I2CSPIBusOption bus_option, int bus, int addr, int bus_frequency);
|
||||
ADS1115(const I2CSPIDriverConfig &config);
|
||||
~ADS1115() override;
|
||||
|
||||
int Begin();
|
||||
|
||||
int init() override;
|
||||
|
||||
static I2CSPIDriverBase *instantiate(const BusCLIArguments &cli, const BusInstanceIterator &iterator,
|
||||
int runtime_instance);
|
||||
|
||||
static void print_usage();
|
||||
|
||||
void RunImpl();
|
||||
|
||||
protected:
|
||||
|
||||
adc_report_s _adc_report = {};
|
||||
|
||||
void print_status() override;
|
||||
|
||||
void exit_and_cleanup() override;
|
||||
@@ -133,9 +126,11 @@ private:
|
||||
|
||||
static const hrt_abstime SAMPLE_INTERVAL{50_ms};
|
||||
|
||||
adc_report_s _adc_report{};
|
||||
|
||||
perf_counter_t _cycle_perf;
|
||||
|
||||
int _channel_cycle_count = 0;
|
||||
int _channel_cycle_count{0};
|
||||
|
||||
// ADS1115 logic part
|
||||
enum ChannelSelection {
|
||||
@@ -159,4 +154,4 @@ private:
|
||||
|
||||
int writeReg(uint8_t addr, uint8_t *buf, size_t len);
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
@@ -42,9 +42,9 @@
|
||||
#include <px4_platform_common/module.h>
|
||||
#include <drivers/drv_adc.h>
|
||||
|
||||
ADS1115::ADS1115(I2CSPIBusOption bus_option, int bus, int addr, int bus_frequency) :
|
||||
I2C(DRV_ADC_DEVTYPE_ADS1115, nullptr, bus, addr, bus_frequency),
|
||||
I2CSPIDriver(MODULE_NAME, px4::device_bus_to_wq(get_device_id()), bus_option, bus, addr),
|
||||
ADS1115::ADS1115(const I2CSPIDriverConfig &config) :
|
||||
I2C(config),
|
||||
I2CSPIDriver(config),
|
||||
_cycle_perf(perf_alloc(PC_ELAPSED, MODULE_NAME": single-sample"))
|
||||
{
|
||||
_adc_report.device_id = this->get_device_id();
|
||||
@@ -63,22 +63,6 @@ ADS1115::~ADS1115()
|
||||
perf_free(_cycle_perf);
|
||||
}
|
||||
|
||||
int ADS1115::Begin()
|
||||
{
|
||||
int ret = init();
|
||||
|
||||
if (ret != PX4_OK) {
|
||||
PX4_ERR("ADS1115 init failed");
|
||||
return ret;
|
||||
}
|
||||
|
||||
setChannel(ADS1115::A0); // prepare for the first measure.
|
||||
|
||||
ScheduleOnInterval(SAMPLE_INTERVAL / 4, SAMPLE_INTERVAL / 4);
|
||||
|
||||
return PX4_OK;
|
||||
}
|
||||
|
||||
void ADS1115::exit_and_cleanup()
|
||||
{
|
||||
I2CSPIDriverBase::exit_and_cleanup(); // nothing to do
|
||||
@@ -140,25 +124,6 @@ void ADS1115::RunImpl()
|
||||
perf_end(_cycle_perf);
|
||||
}
|
||||
|
||||
I2CSPIDriverBase *ADS1115::instantiate(const BusCLIArguments &cli, const BusInstanceIterator &iterator,
|
||||
int runtime_instance)
|
||||
{
|
||||
ADS1115 *instance = new ADS1115(iterator.configuredBusOption(), iterator.bus(), cli.i2c_address,
|
||||
cli.bus_frequency);
|
||||
|
||||
if (!instance) {
|
||||
PX4_ERR("alloc failed");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (OK != instance->Begin()) {
|
||||
delete instance;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
void ADS1115::print_usage()
|
||||
{
|
||||
PRINT_MODULE_USAGE_NAME("ads1115", "driver");
|
||||
@@ -176,17 +141,12 @@ void ADS1115::print_status()
|
||||
|
||||
extern "C" int ads1115_main(int argc, char *argv[])
|
||||
{
|
||||
int ch;
|
||||
using ThisDriver = ADS1115;
|
||||
BusCLIArguments cli{true, false};
|
||||
cli.default_i2c_frequency = 400000;
|
||||
cli.i2c_address = 0x48;
|
||||
|
||||
while ((ch = cli.getOpt(argc, argv, "")) != EOF) {
|
||||
|
||||
}
|
||||
|
||||
const char *verb = cli.optArg();
|
||||
const char *verb = cli.parseDefaultArguments(argc, argv);
|
||||
|
||||
if (!verb) {
|
||||
ThisDriver::print_usage();
|
||||
|
||||
Reference in New Issue
Block a user