2018-01-05 02:19:51 -04:00
|
|
|
/*
|
|
|
|
* This file is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
2019-10-20 10:31:12 -03:00
|
|
|
*
|
2018-01-05 02:19:51 -04:00
|
|
|
* Code by Andrew Tridgell and Siddharth Bharat Purohit
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "AP_HAL_ChibiOS.h"
|
|
|
|
|
2023-01-17 00:15:15 -04:00
|
|
|
// available ADC channels for allocation
|
2018-01-05 02:19:51 -04:00
|
|
|
#define ANALOG_MAX_CHANNELS 16
|
|
|
|
|
2023-01-17 00:15:15 -04:00
|
|
|
// physical ADC channels per ADC HAL driver
|
|
|
|
// This is MCU dependent, currently STM32H7 has the highest number of ADC_INs i.e. 20
|
|
|
|
#define HAL_MAX_ANALOG_IN_CHANNELS 20
|
|
|
|
|
|
|
|
#ifndef HAL_NUM_ANALOG_INPUTS
|
|
|
|
#if defined(HAL_ANALOG3_PINS) || HAL_WITH_MCU_MONITORING
|
|
|
|
#define HAL_NUM_ANALOG_INPUTS 3
|
|
|
|
#elif defined(HAL_ANALOG2_PINS)
|
|
|
|
#define HAL_NUM_ANALOG_INPUTS 2
|
|
|
|
#else
|
|
|
|
#define HAL_NUM_ANALOG_INPUTS 1
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2018-01-07 23:37:25 -04:00
|
|
|
// number of samples on each channel to gather on each DMA callback
|
|
|
|
#define ADC_DMA_BUF_DEPTH 8
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2018-10-28 20:24:01 -03:00
|
|
|
#if HAL_USE_ADC == TRUE && !defined(HAL_DISABLE_ADC_DRIVER)
|
2018-03-06 18:41:03 -04:00
|
|
|
|
2018-01-13 00:02:05 -04:00
|
|
|
class ChibiOS::AnalogSource : public AP_HAL::AnalogSource {
|
2018-01-05 02:19:51 -04:00
|
|
|
public:
|
2018-01-13 00:02:05 -04:00
|
|
|
friend class ChibiOS::AnalogIn;
|
2020-05-08 03:46:57 -03:00
|
|
|
AnalogSource(int16_t pin);
|
2018-11-07 06:58:46 -04:00
|
|
|
float read_average() override;
|
|
|
|
float read_latest() override;
|
2021-09-22 16:47:38 -03:00
|
|
|
bool set_pin(uint8_t p) override WARN_IF_UNUSED;
|
2018-11-07 06:58:46 -04:00
|
|
|
float voltage_average() override;
|
|
|
|
float voltage_latest() override;
|
|
|
|
float voltage_average_ratiometric() override;
|
2018-01-05 02:19:51 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
// what value it has
|
|
|
|
int16_t _pin;
|
|
|
|
float _value;
|
|
|
|
float _value_ratiometric;
|
|
|
|
float _latest_value;
|
|
|
|
uint8_t _sum_count;
|
|
|
|
float _sum_value;
|
|
|
|
float _sum_ratiometric;
|
|
|
|
void _add_value(float v, float vcc5V);
|
|
|
|
float _pin_scaler();
|
2018-10-11 20:35:32 -03:00
|
|
|
HAL_Semaphore _semaphore;
|
2018-01-05 02:19:51 -04:00
|
|
|
};
|
|
|
|
|
2018-01-13 00:02:05 -04:00
|
|
|
class ChibiOS::AnalogIn : public AP_HAL::AnalogIn {
|
2018-01-05 02:19:51 -04:00
|
|
|
public:
|
2018-01-13 00:02:05 -04:00
|
|
|
friend class AnalogSource;
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
void init() override;
|
|
|
|
AP_HAL::AnalogSource* channel(int16_t pin) override;
|
|
|
|
void _timer_tick(void);
|
|
|
|
float board_voltage(void) override { return _board_voltage; }
|
|
|
|
float servorail_voltage(void) override { return _servorail_voltage; }
|
|
|
|
uint16_t power_status_flags(void) override { return _power_flags; }
|
2020-07-19 21:34:42 -03:00
|
|
|
uint16_t accumulated_power_status_flags(void) const override { return _accumulated_power_flags; }
|
2021-08-20 21:19:16 -03:00
|
|
|
|
|
|
|
#if HAL_WITH_MCU_MONITORING
|
|
|
|
float mcu_temperature(void) override { return _mcu_temperature; }
|
|
|
|
float mcu_voltage(void) override { return _mcu_voltage; }
|
|
|
|
float mcu_voltage_max(void) override { return _mcu_voltage_max; }
|
|
|
|
float mcu_voltage_min(void) override { return _mcu_voltage_min; }
|
|
|
|
#endif
|
2018-04-08 06:30:32 -03:00
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
private:
|
2023-01-17 00:15:15 -04:00
|
|
|
void read_adc(uint8_t index, uint32_t *val);
|
2018-04-08 06:30:32 -03:00
|
|
|
void update_power_flags(void);
|
2023-01-17 00:15:15 -04:00
|
|
|
void setup_adc(uint8_t index);
|
2021-08-20 21:19:16 -03:00
|
|
|
static void adccallback(ADCDriver *adcp);
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2018-01-13 00:02:05 -04:00
|
|
|
ChibiOS::AnalogSource* _channels[ANALOG_MAX_CHANNELS];
|
2018-01-05 02:19:51 -04:00
|
|
|
|
|
|
|
uint32_t _last_run;
|
|
|
|
float _board_voltage;
|
|
|
|
float _servorail_voltage;
|
2018-09-15 02:43:54 -03:00
|
|
|
float _rssi_voltage;
|
2018-01-05 02:19:51 -04:00
|
|
|
uint16_t _power_flags;
|
2020-07-19 21:34:42 -03:00
|
|
|
uint16_t _accumulated_power_flags; // bitmask of all _power_flags ever set
|
|
|
|
|
2023-01-17 00:15:15 -04:00
|
|
|
ADCConversionGroup adcgrpcfg[HAL_NUM_ANALOG_INPUTS];
|
|
|
|
|
|
|
|
static uint8_t get_num_grp_channels(uint8_t index);
|
|
|
|
static uint8_t get_pin_channel(uint8_t adc_index, uint8_t pin_index);
|
|
|
|
static uint8_t get_analog_pin(uint8_t adc_index, uint8_t pin_index);
|
|
|
|
static float get_pin_scaling(uint8_t adc_index, uint8_t pin_index);
|
|
|
|
static uint8_t get_adc_index(ADCDriver* adcp);
|
2018-01-07 23:37:25 -04:00
|
|
|
|
|
|
|
struct pin_info {
|
|
|
|
uint8_t channel;
|
2023-01-17 00:15:15 -04:00
|
|
|
uint8_t analog_pin;
|
2018-01-07 23:37:25 -04:00
|
|
|
float scaling;
|
2019-10-20 10:31:12 -03:00
|
|
|
};
|
2018-01-07 23:37:25 -04:00
|
|
|
static const pin_info pin_config[];
|
2023-01-17 00:15:15 -04:00
|
|
|
#ifdef HAL_ANALOG2_PINS
|
|
|
|
static const pin_info pin_config_2[];
|
|
|
|
#endif
|
|
|
|
#if defined(HAL_ANALOG3_PINS) || HAL_WITH_MCU_MONITORING
|
|
|
|
static const pin_info pin_config_3[];
|
|
|
|
#endif
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2023-01-17 00:15:15 -04:00
|
|
|
static adcsample_t *samples[HAL_NUM_ANALOG_INPUTS];
|
|
|
|
static uint32_t *sample_sum[HAL_NUM_ANALOG_INPUTS];
|
|
|
|
static uint32_t sample_count[HAL_NUM_ANALOG_INPUTS];
|
2021-08-20 21:19:16 -03:00
|
|
|
|
2021-02-05 09:18:04 -04:00
|
|
|
HAL_Semaphore _semaphore;
|
2021-08-20 21:19:16 -03:00
|
|
|
|
|
|
|
#if HAL_WITH_MCU_MONITORING
|
2023-01-17 00:15:15 -04:00
|
|
|
uint16_t _mcu_monitor_sample_count;
|
|
|
|
uint32_t _mcu_monitor_temperature_accum;
|
|
|
|
uint32_t _mcu_monitor_voltage_accum;
|
|
|
|
uint16_t _mcu_vrefint_min;
|
|
|
|
uint16_t _mcu_vrefint_max;
|
|
|
|
|
2021-08-20 21:19:16 -03:00
|
|
|
float _mcu_temperature;
|
|
|
|
float _mcu_voltage;
|
|
|
|
float _mcu_voltage_min;
|
|
|
|
float _mcu_voltage_max;
|
|
|
|
#endif
|
2018-01-05 02:19:51 -04:00
|
|
|
};
|
2018-03-06 18:41:03 -04:00
|
|
|
|
|
|
|
#endif // HAL_USE_ADC
|