v1.5.0
Go to the documentation of this file.
7 #ifndef _HARDWARE_ADC_H
8 #define _HARDWARE_ADC_H
11 #include "hardware/structs/adc.h"
48 #ifndef PARAM_ASSERTIONS_ENABLED_ADC
49 #define PARAM_ASSERTIONS_ENABLED_ADC 0
70 invalid_params_if(ADC, gpio < 26 || gpio > 29);
87 valid_params_if(ADC, input < NUM_ADC_CHANNELS);
88 hw_write_masked(&adc_hw->cs, input << ADC_CS_AINSEL_LSB, ADC_CS_AINSEL_BITS);
97 return (adc_hw->cs & ADC_CS_AINSEL_BITS) >> ADC_CS_AINSEL_LSB;
109 valid_params_if(ADC, input_mask < (1 << NUM_ADC_CHANNELS));
110 hw_write_masked(&adc_hw->cs, input_mask << ADC_CS_RROBIN_LSB, ADC_CS_RROBIN_BITS);
136 while (!(adc_hw->cs & ADC_CS_READY_BITS))
139 return (uint16_t) adc_hw->result;
163 invalid_params_if(ADC, clkdiv >= 1 << (ADC_DIV_INT_MSB - ADC_DIV_INT_LSB + 1));
164 adc_hw->div = (uint32_t)(clkdiv * (
float) (1 << ADC_DIV_INT_LSB));
178 static inline void adc_fifo_setup(
bool en,
bool dreq_en, uint16_t dreq_thresh,
bool err_in_fifo,
bool byte_shift) {
180 (bool_to_bit(en) << ADC_FCS_EN_LSB) |
181 (bool_to_bit(dreq_en) << ADC_FCS_DREQ_EN_LSB) |
182 (((uint)dreq_thresh) << ADC_FCS_THRESH_LSB) |
183 (bool_to_bit(err_in_fifo) << ADC_FCS_ERR_LSB) |
184 (bool_to_bit(byte_shift) << ADC_FCS_SHIFT_LSB),
186 ADC_FCS_DREQ_EN_BITS |
187 ADC_FCS_THRESH_BITS |
199 return !!(adc_hw->fcs & ADC_FCS_EMPTY_BITS);
208 return (adc_hw->fcs & ADC_FCS_LEVEL_BITS) >> ADC_FCS_LEVEL_LSB;
217 return (uint16_t)adc_hw->fifo;
228 return (uint16_t)adc_hw->fifo;
238 while (!(adc_hw->cs & ADC_CS_READY_BITS))
250 adc_hw->inte = !!enabled;
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:131
static void gpio_disable_pulls(uint gpio)
Disable pulls on specified GPIO.
Definition: gpio.h:251
static uint16_t adc_read(void)
Perform a single conversion.
Definition: adc.h:133
static void adc_set_clkdiv(float clkdiv)
Set the ADC Clock divisor.
Definition: adc.h:162
static void adc_gpio_init(uint gpio)
Initialise the gpio for use as an ADC pin.
Definition: adc.h:69
static void adc_fifo_drain(void)
Drain the ADC FIFO.
Definition: adc.h:236
static void adc_set_temp_sensor_enabled(bool enable)
Enable the onboard temperature sensor.
Definition: adc.h:119
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:157
static void adc_irq_set_enabled(bool enabled)
Enable/Disable ADC interrupts.
Definition: adc.h:249
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:121
static bool adc_fifo_is_empty(void)
Check FIFO empty state.
Definition: adc.h:198
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:178
void adc_init(void)
Initialise the ADC HW.
Definition: adc.c:11
static void adc_run(bool run)
Enable or disable free-running sampling mode.
Definition: adc.h:147
void gpio_set_function(uint gpio, enum gpio_function fn)
Select GPIO function.
Definition: gpio.c:32
static void adc_select_input(uint input)
ADC input select.
Definition: adc.h:86
static uint16_t adc_fifo_get(void)
Get ADC result from FIFO.
Definition: adc.h:216
void gpio_set_input_enabled(uint gpio, bool enabled)
Enable GPIO input.
Definition: gpio.c:239
static uint16_t adc_fifo_get_blocking(void)
Wait for the ADC FIFO to have data.
Definition: adc.h:225
static void adc_set_round_robin(uint input_mask)
Round Robin sampling selector.
Definition: adc.h:108
static uint8_t adc_fifo_get_level(void)
Get number of entries in the ADC FIFO.
Definition: adc.h:207
static uint adc_get_selected_input(void)
Get the currently selected ADC input channel.
Definition: adc.h:96