@web-font-path: "roboto-debian.css";
Loading...
Searching...
No Matches
adc.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef _HARDWARE_ADC_H
8#define _HARDWARE_ADC_H
9
10#include "pico.h"
11#include "hardware/structs/adc.h"
12#include "hardware/gpio.h"
13
57// PICO_CONFIG: PARAM_ASSERTIONS_ENABLED_HARDWARE_ADC, Enable/disable assertions in the hardware_adc module, type=bool, default=0, group=hardware_adc
58#ifndef PARAM_ASSERTIONS_ENABLED_HARDWARE_ADC
59#ifdef PARAM_ASSERTIONS_ENABLED_ADC // backwards compatibility with SDK < 2.0.0
60#define PARAM_ASSERTIONS_ENABLED_HARDWARE_ADC PARAM_ASSERTIONS_ENABLED_ADC
61#else
62#define PARAM_ASSERTIONS_ENABLED_HARDWARE_ADC 0
63#endif
64#endif
65
69#ifndef ADC_TEMPERATURE_CHANNEL_NUM
70#define ADC_TEMPERATURE_CHANNEL_NUM (NUM_ADC_CHANNELS - 1)
71#endif
72
73#ifdef __cplusplus
74extern "C" {
75#endif
76
81void adc_init(void);
82
90static inline void adc_gpio_init(uint gpio) {
91 invalid_params_if(HARDWARE_ADC, gpio < ADC_BASE_PIN || gpio >= ADC_BASE_PIN + NUM_ADC_CHANNELS - 1);
92 // Select NULL function to make output driver hi-Z
94 // Also disable digital pulls and digital receiver
96 gpio_set_input_enabled(gpio, false);
97}
98
113static inline void adc_select_input(uint input) {
114 valid_params_if(HARDWARE_ADC, input < NUM_ADC_CHANNELS);
115 hw_write_masked(&adc_hw->cs, input << ADC_CS_AINSEL_LSB, ADC_CS_AINSEL_BITS);
116}
117
132static inline uint adc_get_selected_input(void) {
133 return (adc_hw->cs & ADC_CS_AINSEL_BITS) >> ADC_CS_AINSEL_LSB;
134}
135
145static inline void adc_set_round_robin(uint input_mask) {
146 valid_params_if(HARDWARE_ADC, input_mask < (1 << NUM_ADC_CHANNELS));
147 hw_write_masked(&adc_hw->cs, input_mask << ADC_CS_RROBIN_LSB, ADC_CS_RROBIN_BITS);
148}
149
156static inline void adc_set_temp_sensor_enabled(bool enable) {
157 if (enable)
158 hw_set_bits(&adc_hw->cs, ADC_CS_TS_EN_BITS);
159 else
160 hw_clear_bits(&adc_hw->cs, ADC_CS_TS_EN_BITS);
161}
162
170static inline uint16_t adc_read(void) {
171 hw_set_bits(&adc_hw->cs, ADC_CS_START_ONCE_BITS);
172
173 while (!(adc_hw->cs & ADC_CS_READY_BITS))
175
176 return (uint16_t) adc_hw->result;
177}
178
184static inline void adc_run(bool run) {
185 if (run)
186 hw_set_bits(&adc_hw->cs, ADC_CS_START_MANY_BITS);
187 else
188 hw_clear_bits(&adc_hw->cs, ADC_CS_START_MANY_BITS);
189}
190
199static inline void adc_set_clkdiv(float clkdiv) {
200 invalid_params_if(HARDWARE_ADC, clkdiv >= 1 << (ADC_DIV_INT_MSB - ADC_DIV_INT_LSB + 1));
201 adc_hw->div = (uint32_t)(clkdiv * (float) (1 << ADC_DIV_INT_LSB));
202}
203
223 static inline void adc_fifo_setup(bool en, bool dreq_en, uint16_t dreq_thresh, bool err_in_fifo, bool byte_shift) {
224 hw_write_masked(&adc_hw->fcs,
225 (bool_to_bit(en) << ADC_FCS_EN_LSB) |
226 (bool_to_bit(dreq_en) << ADC_FCS_DREQ_EN_LSB) |
227 (((uint)dreq_thresh) << ADC_FCS_THRESH_LSB) |
228 (bool_to_bit(err_in_fifo) << ADC_FCS_ERR_LSB) |
229 (bool_to_bit(byte_shift) << ADC_FCS_SHIFT_LSB),
230 ADC_FCS_EN_BITS |
231 ADC_FCS_DREQ_EN_BITS |
232 ADC_FCS_THRESH_BITS |
233 ADC_FCS_ERR_BITS |
234 ADC_FCS_SHIFT_BITS
235 );
236}
237
243static inline bool adc_fifo_is_empty(void) {
244 return adc_hw->fcs & ADC_FCS_EMPTY_BITS;
245}
246
259static inline uint8_t adc_fifo_get_level(void) {
260 return (adc_hw->fcs & ADC_FCS_LEVEL_BITS) >> ADC_FCS_LEVEL_LSB;
261}
262
268static inline uint16_t adc_fifo_get(void) {
269 return (uint16_t)adc_hw->fifo;
270}
271
277static inline uint16_t adc_fifo_get_blocking(void) {
278 while (adc_fifo_is_empty())
280 return (uint16_t)adc_hw->fifo;
281}
282
288static inline void adc_fifo_drain(void) {
289 // Potentially there is still a conversion in progress -- wait for this to complete before draining
290 while (!(adc_hw->cs & ADC_CS_READY_BITS))
292 while (!adc_fifo_is_empty())
293 (void) adc_fifo_get();
294}
295
301static inline void adc_irq_set_enabled(bool enabled) {
302 adc_hw->inte = !!enabled;
303}
304
305#ifdef __cplusplus
306}
307#endif
308
309#endif
static void adc_set_round_robin(uint input_mask)
Round Robin sampling selector.
Definition adc.h:145
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:223
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:199
static void adc_set_temp_sensor_enabled(bool enable)
Enable the onboard temperature sensor.
Definition adc.h:156
static void adc_select_input(uint input)
ADC input select.
Definition adc.h:113
static void adc_run(bool run)
Enable or disable free-running sampling mode.
Definition adc.h:184
static uint16_t adc_fifo_get_blocking(void)
Wait for the ADC FIFO to have data.
Definition adc.h:277
static uint16_t adc_fifo_get(void)
Get ADC result from FIFO.
Definition adc.h:268
static uint adc_get_selected_input(void)
Get the currently selected ADC input channel.
Definition adc.h:132
static void adc_gpio_init(uint gpio)
Initialise the gpio for use as an ADC pin.
Definition adc.h:90
static void adc_fifo_drain(void)
Drain the ADC FIFO.
Definition adc.h:288
static uint16_t adc_read(void)
Perform a single conversion.
Definition adc.h:170
static void adc_irq_set_enabled(bool enabled)
Enable/Disable ADC interrupts.
Definition adc.h:301
static uint8_t adc_fifo_get_level(void)
Get number of entries in the ADC FIFO.
Definition adc.h:259
static bool adc_fifo_is_empty(void)
Check FIFO empty state.
Definition adc.h:243
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:273
static void gpio_disable_pulls(uint gpio)
Disable pulls on specified GPIO.
Definition gpio.h:337
@ GPIO_FUNC_NULL
Select NULL as GPIO pin function.
Definition io_bank0.h:41
static __force_inline void tight_loop_contents(void)
No-op function for the body of tight loops.
Definition platform.h:87