11#include "hardware/structs/adc.h"
57#ifndef PARAM_ASSERTIONS_ENABLED_HARDWARE_ADC
58#ifdef PARAM_ASSERTIONS_ENABLED_ADC
59#define PARAM_ASSERTIONS_ENABLED_HARDWARE_ADC PARAM_ASSERTIONS_ENABLED_ADC
61#define PARAM_ASSERTIONS_ENABLED_HARDWARE_ADC 0
68#ifndef ADC_TEMPERATURE_CHANNEL_NUM
69#define ADC_TEMPERATURE_CHANNEL_NUM (NUM_ADC_CHANNELS - 1)
73#ifndef PICO_ADC_CLKDIV_ROUND_NEAREST
74#define PICO_ADC_CLKDIV_ROUND_NEAREST PICO_CLKDIV_ROUND_NEAREST
95 invalid_params_if(HARDWARE_ADC, gpio < ADC_BASE_PIN || gpio >= ADC_BASE_PIN + NUM_ADC_CHANNELS - 1);
118 valid_params_if(HARDWARE_ADC, input < NUM_ADC_CHANNELS);
119 hw_write_masked(&adc_hw->cs, input << ADC_CS_AINSEL_LSB, ADC_CS_AINSEL_BITS);
137 return (adc_hw->cs & ADC_CS_AINSEL_BITS) >> ADC_CS_AINSEL_LSB;
150 valid_params_if(HARDWARE_ADC, input_mask < (1 << NUM_ADC_CHANNELS));
151 hw_write_masked(&adc_hw->cs, input_mask << ADC_CS_RROBIN_LSB, ADC_CS_RROBIN_BITS);
177 while (!(adc_hw->cs & ADC_CS_READY_BITS))
180 return (uint16_t) adc_hw->result;
204 invalid_params_if(HARDWARE_ADC, clkdiv >= 1 << REG_FIELD_WIDTH(ADC_DIV_INT));
205 const int frac_bit_count = REG_FIELD_WIDTH(ADC_DIV_FRAC);
206#if PICO_ADC_CLKDIV_ROUND_NEAREST
207 clkdiv += 0.5f / (1 << frac_bit_count);
209 adc_hw->div = (uint32_t)(clkdiv * (
float) (1 << frac_bit_count));
224 static inline void adc_fifo_setup(
bool en,
bool dreq_en, uint16_t dreq_thresh,
bool err_in_fifo,
bool byte_shift) {
226 (bool_to_bit(en) << ADC_FCS_EN_LSB) |
227 (bool_to_bit(dreq_en) << ADC_FCS_DREQ_EN_LSB) |
228 (((uint)dreq_thresh) << ADC_FCS_THRESH_LSB) |
229 (bool_to_bit(err_in_fifo) << ADC_FCS_ERR_LSB) |
230 (bool_to_bit(byte_shift) << ADC_FCS_SHIFT_LSB),
232 ADC_FCS_DREQ_EN_BITS |
233 ADC_FCS_THRESH_BITS |
245 return adc_hw->fcs & ADC_FCS_EMPTY_BITS;
255 return (adc_hw->fcs & ADC_FCS_LEVEL_BITS) >> ADC_FCS_LEVEL_LSB;
264 return (uint16_t)adc_hw->fifo;
275 return (uint16_t)adc_hw->fifo;
285 while (!(adc_hw->cs & ADC_CS_READY_BITS))
297 adc_hw->inte = !!enabled;
static void adc_set_round_robin(uint input_mask)
Round Robin sampling selector.
Definition adc.h:149
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:224
void adc_init(void)
Initialise the ADC HW.
Definition adc.c:11
static void adc_set_clkdiv(float clkdiv)
Set the ADC Clock divisor.
Definition adc.h:203
static void adc_set_temp_sensor_enabled(bool enable)
Enable the onboard temperature sensor.
Definition adc.h:160
static void adc_select_input(uint input)
ADC input select.
Definition adc.h:117
static void adc_run(bool run)
Enable or disable free-running sampling mode.
Definition adc.h:188
static uint16_t adc_fifo_get_blocking(void)
Wait for the ADC FIFO to have data.
Definition adc.h:272
static uint16_t adc_fifo_get(void)
Get ADC result from FIFO.
Definition adc.h:263
static uint adc_get_selected_input(void)
Get the currently selected ADC input channel.
Definition adc.h:136
static void adc_gpio_init(uint gpio)
Initialise the gpio for use as an ADC pin.
Definition adc.h:94
static void adc_fifo_drain(void)
Drain the ADC FIFO.
Definition adc.h:283
static uint16_t adc_read(void)
Perform a single conversion.
Definition adc.h:174
static void adc_irq_set_enabled(bool enabled)
Enable/Disable ADC interrupts.
Definition adc.h:296
static uint8_t adc_fifo_get_level(void)
Get number of entries in the ADC FIFO.
Definition adc.h:254
static bool adc_fifo_is_empty(void)
Check FIFO empty state.
Definition adc.h:244
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
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
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
void gpio_set_function(uint gpio, gpio_function_t fn)
Select GPIO function.
Definition gpio.c:38
void gpio_set_input_enabled(uint gpio, bool enabled)
Enable GPIO input.
Definition gpio.c:266
static void gpio_disable_pulls(uint gpio)
Disable pulls on specified GPIO.
Definition gpio.h:348
@ GPIO_FUNC_NULL
Select NULL as GPIO pin function.
Definition io_bank0.h:45