25 #define RD_ADC_USE_DIVIDER 1.00f
26 #define RD_ADC_USE_VDD 3.30f
28 #define RD_ADC_DATA_COUNTER 1
29 #define RD_ADC_DATA_START 0
30 #define RD_ADC_DATA_VOLTAGE 1
32 #define RD_ADC_DEFAULT_BITFIELD 0
33 #define RD_ADC_CLEAN_BYTE 0
34 #define RD_ADC_INIT_BYTE 0
36 #define RT_ADC_CH_UNUSED (0xFFU)
39 static bool m_is_configured;
40 static bool m_vdd_prepared;
41 static bool m_vdd_sampled;
44 static uint8_t m_handle;
46 static uint8_t m_next_channel;
55 static rd_status_t channel_assign (
const uint8_t handle)
63 else if (RT_ADC_CH_UNUSED == m_channel[handle])
67 m_channel[handle] = m_next_channel++;
86 #ifdef RI_ADC_ADV_CONFIG
87 .p_pin.resistor = RI_ADC_RESISTOR_DISABLED,
95 #ifdef RI_ADC_ADV_CONFIG
97 .acqtime = RI_ADC_ACQTIME_10US,
103 .
vdd = RD_ADC_USE_VDD,
104 .divider = RD_ADC_USE_DIVIDER,
121 float adc_values[RD_ADC_DATA_COUNTER] = {0};
123 if (
false == m_ratio)
127 &adc_values[RD_ADC_DATA_START]);
133 &adc_values[RD_ADC_DATA_START]);
139 d_adc.
data = adc_values;
140 d_adc.
valid = adc_fields;
141 d_adc.
fields = adc_fields;
164 m_channel[ii] = RT_ADC_CH_UNUSED;
177 m_is_configured =
false;
178 m_vdd_prepared =
false;
179 m_vdd_sampled =
false;
186 m_channel[ii] = RT_ADC_CH_UNUSED;
201 return (RD_ADC_INIT_BYTE != m_is_init);
232 err_code |= channel_assign (m_handle);
244 m_is_configured =
true;
278 err_code |= rt_adc_mcu_data_get (data);
295 if (
false == m_ratio)
301 err_code |= rt_adc_mcu_data_get (data);
320 m_vdd_prepared =
true;
343 float battery_values;
344 battery.
data = &battery_values;
349 m_vdd_prepared =
false;
350 m_vdd_sampled =
true;
360 if (
true == m_vdd_sampled)
373 const uint8_t handle,
float *
const sample)
377 if ( (NULL == configuration) || (NULL == sample)) {
return RD_ERROR_NULL; }
381 float battery_values;
382 d_adc.
data = &battery_values;
399 const uint8_t handle,
float *
const sample)
403 if ( (NULL == configuration) || (NULL == sample)) {
return RD_ERROR_NULL; }
408 d_adc.
data = &rate_values;
#define RD_ERROR_INVALID_PARAM
Invalid Parameter.
#define RD_ERROR_NULL
Null Pointer.
#define RD_ERROR_FATAL
Program should always reset after this.
uint32_t rd_status_t
bitfield for representing errors
#define RD_ERROR_RESOURCES
Not enough resources for operation.
#define RD_SUCCESS
Internal Error.
#define RD_UINT64_INVALID
Signal that value should not be used.
#define RD_ERROR_INVALID_STATE
Invalid state, operation disallowed in this state.
void rd_sensor_data_populate(rd_sensor_data_t *const target, const rd_sensor_data_t *const provided, const rd_sensor_data_fields_t requested)
Populate given target data with data provided by sensor as requested.
uint64_t rd_sensor_timestamp_get(void)
Calls the timestamp function and returns its value.
float rd_sensor_data_parse(const rd_sensor_data_t *const provided, const rd_sensor_data_fields_t requested)
Parse data from provided struct.
rd_status_t rt_adc_vdd_get(float *const vdd)
Get VDD.
rd_status_t rt_adc_configure_se(rd_sensor_configuration_t *const config, const uint8_t handle, const rt_adc_mode_t mode)
Configure ADC before sampling.
rd_status_t rt_adc_init(void)
Reserve ADC.
rd_status_t rt_adc_absolute_sample(rd_sensor_configuration_t *const configuration, const uint8_t handle, float *const sample)
Get absolute Voltage Sample from selected ADC handle.
rd_status_t rt_adc_vdd_prepare(rd_sensor_configuration_t *const vdd_adc_configuration)
Prepare for sampling VDD.
rd_status_t rt_adc_ratiometric_sample(rd_sensor_configuration_t *const configuration, const uint8_t handle, float *const sample)
Get ratiometric VDD Sample from selected ADC handle.
rd_status_t rt_adc_uninit(void)
Uninitialize ADC to release it for other users.
rd_status_t rt_adc_sample(void)
Take a new sample on ADC configured in single-shot/sleep mode.
rd_status_t rt_adc_ratio_get(rd_sensor_data_t *const data)
Populate data with latest ratiometric value.
bool rt_adc_is_init(void)
Check if ADC is initialized.
rd_status_t rt_adc_voltage_get(rd_sensor_data_t *const data)
Populate data with latest sample.
rd_status_t rt_adc_vdd_sample(void)
Sample VDD.
@ RATIOMETRIC
ADC compares value to VDD.
@ ABSOLUTE
ADC measures absolute voltage in volts.
Header to enable and disable module compilation.
Ruuvi error codes and error check function.
Ruuvi sensor interface Lifecycle: Beta
Interface for controlling ADC onboard MCU.
rd_status_t ri_adc_uninit(bool config_default)
Uninitialize ADC.
@ RI_ADC_MODE_SINGLE
Single ended mode.
@ RI_ADC_VREF_INTERNAL
Internal voltage reference.
rd_status_t ri_adc_get_data_ratio(uint8_t channel_num, ri_adc_get_data_t *p_config, float *p_data)
Get ADC data in volts.
rd_status_t ri_adc_get_data_absolute(uint8_t channel_num, ri_adc_get_data_t *p_config, float *p_data)
Get ADC data in volts.
@ RI_ADC_CH_NUM
Number of ADC inputs.
@ RI_ADC_AINVDD
Analog supply voltage.
@ RI_ADC_GND
GND of device.
bool ri_adc_mcu_is_valid_ch(const uint8_t ch)
Return true if given channel index can be used by underlying implementation.
rd_status_t ri_adc_stop(uint8_t channel_num)
Stop use ADC channel.
rd_status_t ri_adc_configure(uint8_t channel_num, ri_adc_pins_config_t *p_pins, ri_adc_channel_config_t *p_config)
Configure ADC channel.
rd_status_t ri_adc_init(ri_adc_config_t *p_config)
Initialization of ADC.
bool ri_atomic_flag(ri_atomic_t *const flag, const bool set)
Atomic flag check and set/clear function.
volatile uint32_t ri_atomic_t
define atomic type - not portable to 8-bit.
All sensors must implement configuration functions which accept this struct.
unsigned int voltage_v
Voltage, volts.
unsigned int voltage_ratio
Voltage, ratio to maximum.
Generic sensor data struct.
float * data
Data of sensor. Must contain as many elements as fields has bits set.
uint64_t timestamp_ms
Timestamp of the event, rd_sensor_timestamp_get.
rd_sensor_data_fields_t valid
Listing of valid data in this sample.
rd_sensor_data_fields_t fields
Description of datafields which may be contained in this sample.
ri_adc_pin_config_t p_pin
Union to access sensor data.
uint32_t bitfield
Bitfield used to access sensor data.
rd_sensor_data_bitfield_t datas
Structured data field.