sensors: add ads7953 adc

* sensors: add ads7953 adc

* Update src/drivers/adc/ads7953/ADS7953.h

Co-authored-by: Jacob Dahl <37091262+dakejahl@users.noreply.github.com>

* Implemented changes suggested by review

* Implemented suggested changes

* removed unused variables and moved scope of ch_id

* Activated distance sensor again

* Update msg/AdcReport.msg

Co-authored-by: Hamish Willee <hamishwillee@gmail.com>

* Update ADC report message field comments

* Update ADC msg - fix layout

* update comments

* changed group to Sensors in module.yaml

* created new module subcategory "adc"

* reverted group change in module.yaml

* added module descrption to modules_driver.md

* removed module description in modules_driver.md (autogenerated)

* removed unused variable, changed board_adc publication method to "multi"

* added static assert

---------

Co-authored-by: Jacob Dahl <37091262+dakejahl@users.noreply.github.com>
Co-authored-by: Hamish Willee <hamishwillee@gmail.com>
This commit is contained in:
Phil-Engljaehringer 2025-11-04 17:22:10 +01:00 committed by GitHub
parent 8cb1c31f46
commit fb13b880ce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 343 additions and 8 deletions

View File

@ -219,6 +219,12 @@ then
pcf8583 start -X -a 0x51
fi
# ADC sensor ADS7953 external SPI
if param compare -s ADC_ADS7953_EN 1
then
ads7953 start -S
fi
# probe for optional external I2C devices
if param compare SENS_EXT_I2C_PRB 1
then

View File

@ -15,7 +15,7 @@ class ModuleDocumentation(object):
# TOC in https://github.com/PX4/PX4-Autopilot/blob/main/docs/en/SUMMARY.md
valid_categories = ['driver', 'estimator', 'controller', 'system',
'communication', 'command', 'template', 'simulation', 'autotune']
valid_subcategories = ['', 'camera', 'distance_sensor', 'imu', 'ins', 'airspeed_sensor',
valid_subcategories = ['', 'adc', 'camera', 'distance_sensor', 'imu', 'ins', 'airspeed_sensor',
'magnetometer', 'baro', 'optical_flow', 'radio_control','rpm_sensor', 'transponder']
max_line_length = 80 # wrap lines that are longer than this

View File

@ -1,6 +1,10 @@
uint64 timestamp # time since system start (microseconds)
uint32 device_id # unique device ID for the sensor that does not change between power cycles
int16[12] channel_id # ADC channel IDs, negative for non-existent, TODO: should be kept same as array index
int32[12] raw_data # ADC channel raw value, accept negative value, valid if channel ID is positive
uint32 resolution # ADC channel resolution
float32 v_ref # ADC channel voltage reference, use to calculate LSB voltage(lsb=scale/resolution)
# ADC raw data.
#
# Communicates raw data from an analog-to-digital converter (ADC) to other modules, such as battery status.
uint64 timestamp # [us] Time since system start
uint32 device_id # [-] unique device ID for the sensor that does not change between power cycles
int16[16] channel_id # [-] ADC channel IDs, negative for non-existent, TODO: should be kept same as array index
int32[16] raw_data # [-] ADC channel raw value, accept negative value, valid if channel ID is positive
uint32 resolution # [-] ADC channel resolution
float32 v_ref # [V] ADC channel voltage reference, use to calculate LSB voltage(lsb=scale/resolution)

View File

@ -0,0 +1,122 @@
#include "ADS7953.h"
#include <px4_platform_common/getopt.h>
#include <px4_platform_common/module.h>
#include <drivers/drv_adc.h>
#include <parameters/param.h>
ADS7953::ADS7953(const I2CSPIDriverConfig &config) :
SPI(config),
I2CSPIDriver(config),
ModuleParams(nullptr)
{
static_assert(arraySize(adc_report_s::channel_id) >= NUM_CHANNELS, "ADS7953 reports 16 channels");
}
int ADS7953::init()
{
int ret = SPI::init();
if (ret != PX4_OK) {
PX4_DEBUG("SPI::init failed (%i)", ret);
return ret;
}
_adc_report.device_id = this->get_device_id();
_adc_report.v_ref = _adc_ads7953_refv.get();
_adc_report.resolution = 4096;
for (unsigned i = 0; i < PX4_MAX_ADC_CHANNELS; ++i) {
_adc_report.channel_id[i] = -1;
}
ScheduleOnInterval(10_ms);
return PX4_OK;
}
int ADS7953::probe()
{
// The ADS7953 has no ID register which we can check, so we verify the device via the returned channel ID.
// We set the mode to "manual mode" and the channel to measure to 1.
// If the returned channel ID on the third message is 1, we assume the ADS7953 is connected.
uint8_t recv_data[2];
int ret = rw_msg(&recv_data[0], 1, true);
if (ret != PX4_OK) {
PX4_DEBUG("ADS7953 probing failed (%i)", ret);
return ret;
}
ret |= rw_msg(&recv_data[0], 0, false);
ret |= rw_msg(&recv_data[0], 0, true);
if (ret != PX4_OK || (recv_data[0] >> 4) != 1U) {
PX4_DEBUG("ADS7953 probing failed (%i)", ret);
return PX4_ERROR;
}
PX4_INFO("ADS7953 was found");
return PX4_OK;
}
int ADS7953::rw_msg(uint8_t *recv_data, uint8_t ch, bool change_channel)
{
uint8_t send_data[2];
if (change_channel) {
send_data[0] = 0x10 | (ch >> 1);
send_data[1] = 0x00 | (ch << 7);
} else {
send_data[0] = 0x00;
send_data[1] = 0x00;
}
return transfer(&send_data[0], &recv_data[0], 2);
}
int ADS7953::get_measurements()
{
uint8_t recv_data[2];
int count = 0;
uint16_t mask = 0x00;
uint8_t idx = 0;
while (count < NUM_CHANNELS) {
if (rw_msg(&recv_data[0], idx, true) == PX4_OK) {
uint8_t ch_id = (recv_data[0] >> 4);
//check if we already have a measurement for the returned channel
if (!(mask & (1U << ch_id))) {
mask |= (1U << ch_id);
count++;
_adc_report.channel_id[ch_id] = ch_id;
_adc_report.raw_data[ch_id] = ((((uint16_t) recv_data[0]) & 0x0F) << 8) | recv_data[1];
}
}
// Find index to measure next
for (int i = 1; i <= NUM_CHANNELS; i++) {
uint8_t candidate_id = (idx + i) % NUM_CHANNELS;
if (!(mask & (1U << candidate_id))) {
idx = candidate_id;
break;
}
}
}
return 0;
}
void ADS7953::RunImpl()
{
get_measurements();
_adc_report.timestamp = hrt_absolute_time();
_adc_report_pub.publish(_adc_report);
for (unsigned i = 0; i < PX4_MAX_ADC_CHANNELS; ++i) {
_adc_report.channel_id[i] = -1;
}
}

View File

@ -0,0 +1,43 @@
#pragma once
#include <drivers/device/spi.h>
#include <drivers/drv_hrt.h>
#include <px4_platform_common/i2c_spi_buses.h>
#include <lib/drivers/device/spi.h>
#include <lib/parameters/param.h>
#include <uORB/topics/adc_report.h>
#include <uORB/PublicationMulti.hpp>
#include <lib/mixer_module/mixer_module.hpp>
#include <px4_platform_common/module_params.h>
#include <uORB/topics/parameter_update.h>
#include <uORB/Subscription.hpp>
using namespace time_literals;
class ADS7953 : public device::SPI, public I2CSPIDriver<ADS7953>, public ModuleParams
{
public:
ADS7953(const I2CSPIDriverConfig &config);
virtual ~ADS7953() = default;
static void print_usage();
int init() override;
void RunImpl();
int probe() override;
private:
static constexpr int NUM_CHANNELS = 16;
uORB::PublicationMulti<adc_report_s> _adc_report_pub{ORB_ID(adc_report)};
static const hrt_abstime SAMPLE_INTERVAL{50_ms};
DEFINE_PARAMETERS(
(ParamFloat<px4::params::ADC_ADS7953_REFV>) _adc_ads7953_refv
)
adc_report_s _adc_report{};
int get_measurements();
int rw_msg(uint8_t *recv_data, uint8_t ch, bool change_channel);
};

View File

@ -0,0 +1,46 @@
############################################################################
#
# Copyright (c) 2025 PX4 Development Team. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name PX4 nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
px4_add_module(
MODULE drivers__adc__ads7953
MAIN ads7953
COMPILE_FLAGS
SRCS
ADS7953.cpp
ADS7953.h
ads7953_main.cpp
MODULE_CONFIG
module.yaml
DEPENDS
px4_work_queue
)

View File

@ -0,0 +1,5 @@
menuconfig DRIVERS_ADC_ADS7953
bool "ADS7953 driver"
default n
---help---
Enable support for ADS7953

View File

@ -0,0 +1,77 @@
/****************************************************************************
*
* Copyright (c) 2025 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#include <px4_platform_common/getopt.h>
#include <px4_platform_common/module.h>
#include "ADS7953.h"
void ADS7953::print_usage()
{
PRINT_MODULE_USAGE_NAME("ads7953", "driver");
PRINT_MODULE_USAGE_SUBCATEGORY("adc");
PRINT_MODULE_USAGE_COMMAND("start");
PRINT_MODULE_USAGE_PARAMS_I2C_SPI_DRIVER(false, true);
PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
}
extern "C" int ads7953_main(int argc, char *argv[])
{
using ThisDriver = ADS7953;
BusCLIArguments cli{false, true};
cli.spi_mode = SPIDEV_MODE0;
cli.default_spi_frequency = 10 * 1000 * 1000;
const char *name = MODULE_NAME;
const char *verb = cli.parseDefaultArguments(argc, argv);
if (!verb) {
ThisDriver::print_usage();
return -1;
}
BusInstanceIterator iterator(name, cli, DRV_ADC_DEVTYPE_ADS7953);
if (!strcmp(verb, "start")) {
return ThisDriver::module_start(cli, iterator);
}
if (!strcmp(verb, "stop")) {
return ThisDriver::module_stop(iterator);
}
if (!strcmp(verb, "status")) {
return ThisDriver::module_status(iterator);
}
ThisDriver::print_usage();
return -1;
}

View File

@ -0,0 +1,29 @@
__max_num_config_instances: &max_num_config_instances 1
module_name: ADS7953
parameters:
- group: ADC
definitions:
ADC_ADS7953_EN:
description:
short: Enable ADS7953
long: |
Enable the driver for the ADS7953 board
type: boolean
reboot_required: true
default: 0
ADC_ADS7953_REFV:
description:
short: Applied reference Voltage.
long: |
The voltage applied to the ADS7953 board as reference
type: float
unit: V
min: 2.0
max: 3.0
decimal: 2
increment: 0.01
reboot_required: true
default: 2.5

View File

@ -50,6 +50,7 @@
#include <px4_platform_common/px4_config.h>
#include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp>
#include <uORB/Publication.hpp>
#include <uORB/PublicationMulti.hpp>
#include <uORB/topics/adc_report.h>
#include <uORB/topics/system_power.h>
@ -110,7 +111,7 @@ private:
const uint32_t _base_address;
px4_adc_msg_t *_samples{nullptr}; /**< sample buffer */
uORB::Publication<adc_report_s> _to_adc_report{ORB_ID(adc_report)};
uORB::PublicationMulti<adc_report_s> _to_adc_report{ORB_ID(adc_report)};
uORB::Publication<system_power_s> _to_system_power{ORB_ID(system_power)};
#ifdef BOARD_GPIO_VDD_5V_COMP_VALID

View File

@ -260,6 +260,8 @@
#define DRV_INS_DEVTYPE_SBG 0xEC
#define DRV_ADC_DEVTYPE_ADS7953 0xED
#define DRV_DEVTYPE_UNUSED 0xff
#endif /* _DRV_SENSOR_H */