diff --git a/ROMFS/px4fmu_common/init.d/rc.sensors b/ROMFS/px4fmu_common/init.d/rc.sensors index 759acb3b51..d558d381b1 100644 --- a/ROMFS/px4fmu_common/init.d/rc.sensors +++ b/ROMFS/px4fmu_common/init.d/rc.sensors @@ -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 diff --git a/Tools/px4moduledoc/srcparser.py b/Tools/px4moduledoc/srcparser.py index 8b56e7bb08..749acef762 100644 --- a/Tools/px4moduledoc/srcparser.py +++ b/Tools/px4moduledoc/srcparser.py @@ -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 diff --git a/msg/AdcReport.msg b/msg/AdcReport.msg index 1ae72b6d6c..4a56bc2875 100644 --- a/msg/AdcReport.msg +++ b/msg/AdcReport.msg @@ -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) diff --git a/src/drivers/adc/ads7953/ADS7953.cpp b/src/drivers/adc/ads7953/ADS7953.cpp new file mode 100644 index 0000000000..e2076ef115 --- /dev/null +++ b/src/drivers/adc/ads7953/ADS7953.cpp @@ -0,0 +1,122 @@ + +#include "ADS7953.h" +#include +#include +#include +#include + +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; + } +} diff --git a/src/drivers/adc/ads7953/ADS7953.h b/src/drivers/adc/ads7953/ADS7953.h new file mode 100644 index 0000000000..03ffcb7b81 --- /dev/null +++ b/src/drivers/adc/ads7953/ADS7953.h @@ -0,0 +1,43 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace time_literals; + + +class ADS7953 : public device::SPI, public I2CSPIDriver, 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_pub{ORB_ID(adc_report)}; + + static const hrt_abstime SAMPLE_INTERVAL{50_ms}; + + DEFINE_PARAMETERS( + (ParamFloat) _adc_ads7953_refv + ) + adc_report_s _adc_report{}; + + int get_measurements(); + int rw_msg(uint8_t *recv_data, uint8_t ch, bool change_channel); +}; diff --git a/src/drivers/adc/ads7953/CMakeLists.txt b/src/drivers/adc/ads7953/CMakeLists.txt new file mode 100644 index 0000000000..fbb25e5f5d --- /dev/null +++ b/src/drivers/adc/ads7953/CMakeLists.txt @@ -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 + ) diff --git a/src/drivers/adc/ads7953/Kconfig b/src/drivers/adc/ads7953/Kconfig new file mode 100644 index 0000000000..a50f4c8b26 --- /dev/null +++ b/src/drivers/adc/ads7953/Kconfig @@ -0,0 +1,5 @@ +menuconfig DRIVERS_ADC_ADS7953 + bool "ADS7953 driver" + default n + ---help--- + Enable support for ADS7953 diff --git a/src/drivers/adc/ads7953/ads7953_main.cpp b/src/drivers/adc/ads7953/ads7953_main.cpp new file mode 100644 index 0000000000..9c9de10faf --- /dev/null +++ b/src/drivers/adc/ads7953/ads7953_main.cpp @@ -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 +#include +#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; +} diff --git a/src/drivers/adc/ads7953/module.yaml b/src/drivers/adc/ads7953/module.yaml new file mode 100644 index 0000000000..9097daeb08 --- /dev/null +++ b/src/drivers/adc/ads7953/module.yaml @@ -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 diff --git a/src/drivers/adc/board_adc/ADC.hpp b/src/drivers/adc/board_adc/ADC.hpp index 47f9538814..c765d596b4 100644 --- a/src/drivers/adc/board_adc/ADC.hpp +++ b/src/drivers/adc/board_adc/ADC.hpp @@ -50,6 +50,7 @@ #include #include #include +#include #include #include @@ -110,7 +111,7 @@ private: const uint32_t _base_address; px4_adc_msg_t *_samples{nullptr}; /**< sample buffer */ - uORB::Publication _to_adc_report{ORB_ID(adc_report)}; + uORB::PublicationMulti _to_adc_report{ORB_ID(adc_report)}; uORB::Publication _to_system_power{ORB_ID(system_power)}; #ifdef BOARD_GPIO_VDD_5V_COMP_VALID diff --git a/src/drivers/drv_sensor.h b/src/drivers/drv_sensor.h index 1f74122014..eeafc722cf 100644 --- a/src/drivers/drv_sensor.h +++ b/src/drivers/drv_sensor.h @@ -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 */