@web-font-path: "roboto-debian.css";
v2.2.0 (RP2040)
Loading...
Searching...
No Matches
adc.h
Go to the documentation of this file.
/*
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifndef _HARDWARE_ADC_H
#define _HARDWARE_ADC_H
#include "pico.h"
#include "hardware/structs/adc.h"
#include "hardware/gpio.h"
// PICO_CONFIG: PARAM_ASSERTIONS_ENABLED_HARDWARE_ADC, Enable/disable assertions in the hardware_adc module, type=bool, default=0, group=hardware_adc
#ifndef PARAM_ASSERTIONS_ENABLED_HARDWARE_ADC
#ifdef PARAM_ASSERTIONS_ENABLED_ADC // backwards compatibility with SDK < 2.0.0
#define PARAM_ASSERTIONS_ENABLED_HARDWARE_ADC PARAM_ASSERTIONS_ENABLED_ADC
#else
#define PARAM_ASSERTIONS_ENABLED_HARDWARE_ADC 0
#endif
#endif
#ifndef ADC_TEMPERATURE_CHANNEL_NUM
#define ADC_TEMPERATURE_CHANNEL_NUM (NUM_ADC_CHANNELS - 1)
#endif
// PICO_CONFIG: PICO_ADC_CLKDIV_ROUND_NEAREST, True if floating point ADC clock divisors should be rounded to the nearest possible clock divisor rather than rounding down, type=bool, default=PICO_CLKDIV_ROUND_NEAREST, group=hardware_adc
#ifndef PICO_ADC_CLKDIV_ROUND_NEAREST
#define PICO_ADC_CLKDIV_ROUND_NEAREST PICO_CLKDIV_ROUND_NEAREST
#endif
#ifdef __cplusplus
extern "C" {
#endif
void adc_init(void);
static inline void adc_gpio_init(uint gpio) {
invalid_params_if(HARDWARE_ADC, gpio < ADC_BASE_PIN || gpio >= ADC_BASE_PIN + NUM_ADC_CHANNELS - 1);
// Select NULL function to make output driver hi-Z
gpio_set_function(gpio, GPIO_FUNC_NULL);
// Also disable digital pulls and digital receiver
gpio_disable_pulls(gpio);
gpio_set_input_enabled(gpio, false);
}
static inline void adc_select_input(uint input) {
valid_params_if(HARDWARE_ADC, input < NUM_ADC_CHANNELS);
hw_write_masked(&adc_hw->cs, input << ADC_CS_AINSEL_LSB, ADC_CS_AINSEL_BITS);
}
static inline uint adc_get_selected_input(void) {
return (adc_hw->cs & ADC_CS_AINSEL_BITS) >> ADC_CS_AINSEL_LSB;
}
static inline void adc_set_round_robin(uint input_mask) {
valid_params_if(HARDWARE_ADC, input_mask < (1 << NUM_ADC_CHANNELS));
hw_write_masked(&adc_hw->cs, input_mask << ADC_CS_RROBIN_LSB, ADC_CS_RROBIN_BITS);
}
static inline void adc_set_temp_sensor_enabled(bool enable) {
if (enable)
hw_set_bits(&adc_hw->cs, ADC_CS_TS_EN_BITS);
else
hw_clear_bits(&adc_hw->cs, ADC_CS_TS_EN_BITS);
}
static inline uint16_t adc_read(void) {
hw_set_bits(&adc_hw->cs, ADC_CS_START_ONCE_BITS);
while (!(adc_hw->cs & ADC_CS_READY_BITS))
tight_loop_contents();
return (uint16_t) adc_hw->result;
}
static inline void adc_run(bool run) {
if (run)
hw_set_bits(&adc_hw->cs, ADC_CS_START_MANY_BITS);
else
hw_clear_bits(&adc_hw->cs, ADC_CS_START_MANY_BITS);
}
static inline void adc_set_clkdiv(float clkdiv) {
invalid_params_if(HARDWARE_ADC, clkdiv >= 1 << REG_FIELD_WIDTH(ADC_DIV_INT));
const int frac_bit_count = REG_FIELD_WIDTH(ADC_DIV_FRAC);
#if PICO_ADC_CLKDIV_ROUND_NEAREST
clkdiv += 0.5f / (1 << frac_bit_count); // round to the nearest fraction
#endif
adc_hw->div = (uint32_t)(clkdiv * (float) (1 << frac_bit_count));
}
static inline void adc_fifo_setup(bool en, bool dreq_en, uint16_t dreq_thresh, bool err_in_fifo, bool byte_shift) {
hw_write_masked(&adc_hw->fcs,
(bool_to_bit(en) << ADC_FCS_EN_LSB) |
(bool_to_bit(dreq_en) << ADC_FCS_DREQ_EN_LSB) |
(((uint)dreq_thresh) << ADC_FCS_THRESH_LSB) |
(bool_to_bit(err_in_fifo) << ADC_FCS_ERR_LSB) |
(bool_to_bit(byte_shift) << ADC_FCS_SHIFT_LSB),
ADC_FCS_EN_BITS |
ADC_FCS_DREQ_EN_BITS |
ADC_FCS_THRESH_BITS |
ADC_FCS_ERR_BITS |
ADC_FCS_SHIFT_BITS
);
}
static inline bool adc_fifo_is_empty(void) {
return adc_hw->fcs & ADC_FCS_EMPTY_BITS;
}
static inline uint8_t adc_fifo_get_level(void) {
return (adc_hw->fcs & ADC_FCS_LEVEL_BITS) >> ADC_FCS_LEVEL_LSB;
}
static inline uint16_t adc_fifo_get(void) {
return (uint16_t)adc_hw->fifo;
}
static inline uint16_t adc_fifo_get_blocking(void) {
while (adc_fifo_is_empty())
tight_loop_contents();
return (uint16_t)adc_hw->fifo;
}
static inline void adc_fifo_drain(void) {
// Potentially there is still a conversion in progress -- wait for this to complete before draining
while (!(adc_hw->cs & ADC_CS_READY_BITS))
tight_loop_contents();
while (!adc_fifo_is_empty())
(void) adc_fifo_get();
}
static inline void adc_irq_set_enabled(bool enabled) {
adc_hw->inte = !!enabled;
}
#ifdef __cplusplus
}
#endif
#endif
gpio.h
adc_set_round_robin
static void adc_set_round_robin(uint input_mask)
Round Robin sampling selector.
Definition adc.h:150
adc_fifo_setup
static void adc_fifo_setup(bool en, bool dreq_en, uint16_t dreq_thresh, bool err_in_fifo, bool byte_shift)
Setup the ADC FIFO.
Definition adc.h:232
adc_init
void adc_init(void)
Initialise the ADC HW.
Definition adc.c:11
adc_set_clkdiv
static void adc_set_clkdiv(float clkdiv)
Set the ADC Clock divisor.
Definition adc.h:204
adc_set_temp_sensor_enabled
static void adc_set_temp_sensor_enabled(bool enable)
Enable the onboard temperature sensor.
Definition adc.h:161
adc_select_input
static void adc_select_input(uint input)
ADC input select.
Definition adc.h:118
adc_run
static void adc_run(bool run)
Enable or disable free-running sampling mode.
Definition adc.h:189
adc_fifo_get_blocking
static uint16_t adc_fifo_get_blocking(void)
Wait for the ADC FIFO to have data.
Definition adc.h:286
adc_fifo_get
static uint16_t adc_fifo_get(void)
Get ADC result from FIFO.
Definition adc.h:277
adc_get_selected_input
static uint adc_get_selected_input(void)
Get the currently selected ADC input channel.
Definition adc.h:137
adc_gpio_init
static void adc_gpio_init(uint gpio)
Initialise the gpio for use as an ADC pin.
Definition adc.h:95
adc_fifo_drain
static void adc_fifo_drain(void)
Drain the ADC FIFO.
Definition adc.h:297
adc_read
static uint16_t adc_read(void)
Perform a single conversion.
Definition adc.h:175
adc_irq_set_enabled
static void adc_irq_set_enabled(bool enabled)
Enable/Disable ADC interrupts.
Definition adc.h:310
adc_fifo_get_level
static uint8_t adc_fifo_get_level(void)
Get number of entries in the ADC FIFO.
Definition adc.h:268
adc_fifo_is_empty
static bool adc_fifo_is_empty(void)
Check FIFO empty state.
Definition adc.h:252
hw_set_bits
static __force_inline void hw_set_bits(io_rw_32 *addr, uint32_t mask)
Atomically set the specified bits to 1 in a HW register.
Definition address_mapped.h:135
hw_write_masked
static __force_inline void hw_write_masked(io_rw_32 *addr, uint32_t values, uint32_t write_mask)
Set new values for a sub-set of the bits in a HW register.
Definition address_mapped.h:171
hw_clear_bits
static __force_inline void hw_clear_bits(io_rw_32 *addr, uint32_t mask)
Atomically clear the specified bits to 0 in a HW register.
Definition address_mapped.h:145
gpio_set_function
void gpio_set_function(uint gpio, gpio_function_t fn)
Select GPIO function.
Definition gpio.c:38
gpio_set_input_enabled
void gpio_set_input_enabled(uint gpio, bool enabled)
Enable GPIO input.
Definition gpio.c:265
gpio_disable_pulls
static void gpio_disable_pulls(uint gpio)
Disable pulls on specified GPIO.
Definition gpio.h:342
GPIO_FUNC_NULL
@ GPIO_FUNC_NULL
Select NULL as GPIO pin function.
Definition io_bank0.h:41
tight_loop_contents
static __force_inline void tight_loop_contents(void)
No-op function for the body of tight loops.
Definition common.h:74
pico.h