ruuvi.drivers.c  ${PROJECT_VERSION}
Drivers for external sensors and peripherals on embedded systems.
ADC tasks

Internal Analog-to-digital converter control. More...

Files

file  ruuvi_task_adc.c
 
file  ruuvi_task_adc.h
 

Enumerations

enum  rt_adc_mode_t { RATIOMETRIC , ABSOLUTE }
 

Functions

rd_status_t rt_adc_init (void)
 Reserve ADC. More...
 
rd_status_t rt_adc_uninit (void)
 Uninitialize ADC to release it for other users. More...
 
bool rt_adc_is_init (void)
 Check if ADC is initialized. More...
 
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. More...
 
rd_status_t rt_adc_sample (void)
 Take a new sample on ADC configured in single-shot/sleep mode. More...
 
rd_status_t rt_adc_voltage_get (rd_sensor_data_t *const data)
 Populate data with latest sample. More...
 
rd_status_t rt_adc_ratio_get (rd_sensor_data_t *const data)
 Populate data with latest ratiometric value. More...
 
rd_status_t rt_adc_vdd_prepare (rd_sensor_configuration_t *const vdd_adc_configuration)
 Prepare for sampling VDD. More...
 
rd_status_t rt_adc_vdd_sample (void)
 Sample VDD. More...
 
rd_status_t rt_adc_vdd_get (float *const vdd)
 Get VDD. More...
 
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. More...
 
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. More...
 

Detailed Description

Internal Analog-to-digital converter control.

Enumeration Type Documentation

◆ rt_adc_mode_t

Enumerator
RATIOMETRIC 

ADC compares value to VDD.

ABSOLUTE 

ADC measures absolute voltage in volts.

Definition at line 59 of file ruuvi_task_adc.h.

Function Documentation

◆ rt_adc_absolute_sample()

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.

This function initializes ADC, reads absolute voltage sample from selected handle. The parameter sample is absolute voltage relative to the device ground.

Parameters
[in]configurationConfiguration of ADC.
[in]handleHandle to ADC, i.e. ADC pin.
[in]samplePointer to a Voltage Sample.
Return values
RD_SUCCESSon success
errorcode from stack on error.

◆ rt_adc_configure_se()

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.

This function readies the ADC for sampling. Configuring the ADC may take some time (< 1 ms) while actual sample must be as fast as possible to catch transients.

Note: ADC should be configured to sleep or continuous mode. To take a single sample, call rt_adc_sample after configuration. Configuring ADC into single sample mode is equivalent to calling rt_adc_sample and configuring ADC into sleep immediately.

Parameters
[in,out]configConfiguration of ADC.
[in]handleHandle to ADC, i.e. ADC pin.
[in]modesampling mode, rt_adc_mode_t.
Return values
RD_SUCCESSon success.
RD_ERROR_INVALID_STATEif ADC is not initialized or if it is already configured.

◆ rt_adc_init()

rd_status_t rt_adc_init ( void  )

Reserve ADC.

Return values
RD_SUCCESSon success.
RD_ERROR_INVALID_STATEif ADC is already initialized.

◆ rt_adc_is_init()

bool rt_adc_is_init ( void  )

Check if ADC is initialized.

Return values
trueif ADC is initialized.
falseif ADC is not initialized.

◆ rt_adc_ratio_get()

rd_status_t rt_adc_ratio_get ( rd_sensor_data_t *const  data)

Populate data with latest ratiometric value.

The data is ratio between 0.0 (gnd) and 1.0 (VDD). However the implementation is allowed to return negative values and values higher than 1.0 if the real voltage is beyond the supply rails or if differential sample is negative.

Parameters
[in]dataData which has a field for ratiometric ADC value
Return values
RD_SUCCESSon success
RD_ERROR_INVALID_STATEif ADC is not initialized or configured.
errorcode from stack on error.

◆ rt_adc_ratiometric_sample()

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.

This function initializes ADC, reads ratiometric voltage sample from selected handle. The parameter sample is a ration of voltage pin, relative to VDD, varying between 0.0 (GND) and 1.0 (VDD). Additionally the parameter sample could be outside limits as described in rt_adc_ratio_get.

Parameters
[in]configurationConfiguration of ADC.
[in]handleHandle to ADC, i.e. ADC pin.
[in]samplePointer to a Voltage Sample.
Return values
RD_SUCCESSon success
errorcode from stack on error.

◆ rt_adc_sample()

rd_status_t rt_adc_sample ( void  )

Take a new sample on ADC configured in single-shot/sleep mode.

If this function returns RD_SUCCESS new sample can be immediately read with rt_adc_voltage_get or rt_adc_ratio_get

Return values
RD_SUCCESSSampling was successful
RD_ERROR_INVALID_STATEADC is not initialized or configured

◆ rt_adc_uninit()

rd_status_t rt_adc_uninit ( void  )

Uninitialize ADC to release it for other users.

Return values
RD_SUCCESSon success.
RD_ERROR_FATALif ADC lock can't be released. Reboot.

◆ rt_adc_vdd_get()

rd_status_t rt_adc_vdd_get ( float *const  vdd)

Get VDD.

This function should be called any time after rt_adc_vdd_sample. The value returned will remain fixed until next call to rt_adc_vdd_sample.

Parameters
[out]vddVDD voltage in volts.
Return values
RD_SUCCESSon success
RD_ERROR_INVALID_STATEif rt_adc_vdd_sample wasn't called.

◆ rt_adc_vdd_prepare()

rd_status_t rt_adc_vdd_prepare ( rd_sensor_configuration_t *const  vdd_adc_configuration)

Prepare for sampling VDD.

This function should be called before entering energy intensive activity, such as using radio to transmit data. After calling this function ADC is primed for measuring the voltage droop of battery. On ADC configuration error, uninitialize ADC.

Return values
RD_SUCCESSon success
RD_ERROR_INVALID_STATEif ADC is already initialized.

◆ rt_adc_vdd_sample()

rd_status_t rt_adc_vdd_sample ( void  )

Sample VDD.

This function should be called as soon as possible after energy intensive activity. After a successful call value returned by rt_adc_vdd_get is updated and ADC is released.

Return values
RD_SUCCESSon success
RD_ERROR_INVALID_STATEif rt_adc_vdd_prepare wasn't called.

◆ rt_adc_voltage_get()

rd_status_t rt_adc_voltage_get ( rd_sensor_data_t *const  data)

Populate data with latest sample.

The data is absolute voltage relative to device ground.

Parameters
[in]dataData which has a field for absolute ADC value
Return values
RD_SUCCESSon success
RD_ERROR_INVALID_STATEif ADC is not initialized or configured.
errorcode from stack on error.