@web-font-path: "roboto-debian.css";
v2.2.0 (RP2040)
Loading...
Searching...
No Matches
hardware_adc

Analog to Digital Converter (ADC) API. More...

## Functions

void | adc_init (void)
| Initialise the ADC HW.
static void | adc_gpio_init (uint gpio)
| Initialise the gpio for use as an ADC pin.
static void | adc_select_input (uint input)
| ADC input select.
static uint | adc_get_selected_input (void)
| Get the currently selected ADC input channel.
static void | adc_set_round_robin (uint input_mask)
| Round Robin sampling selector.
static void | adc_set_temp_sensor_enabled (bool enable)
| Enable the onboard temperature sensor.
static uint16_t | adc_read (void)
| Perform a single conversion.
static void | adc_run (bool run)
| Enable or disable free-running sampling mode.
static void | adc_set_clkdiv (float clkdiv)
| Set the ADC Clock divisor.
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.
static bool | adc_fifo_is_empty (void)
| Check FIFO empty state.
static uint8_t | adc_fifo_get_level (void)
| Get number of entries in the ADC FIFO.
static uint16_t | adc_fifo_get (void)
| Get ADC result from FIFO.
static uint16_t | adc_fifo_get_blocking (void)
| Wait for the ADC FIFO to have data.
static void | adc_fifo_drain (void)
| Drain the ADC FIFO.
static void | adc_irq_set_enabled (bool enabled)
| Enable/Disable ADC interrupts.

## Detailed Description

Analog to Digital Converter (ADC) API.

RP-series microcontrollers have an internal analogue-digital converter (ADC) with the following features:

- SAR ADC
- 500 kS/s (Using an independent 48MHz clock)
- 12 bit (RP2040 8.7 ENOB, RP2350 9.2 ENOB)
- RP2040 5 input mux:
  - 4 inputs that are available on package pins shared with GPIO[29:26]
  - 1 input is dedicated to the internal temperature sensor
  - 4 element receive sample FIFO
- One input dedicated to the internal temperature sensor (see Section 12.4.6)
- Interrupt generation
- DMA interface

Although there is only one ADC you can specify the input to it using the adc_select_input() function. In round robin mode (adc_set_round_robin()), the ADC will use that input and move to the next one after a read.

RP2040, RP2350 QFN-60: User ADC inputs are on 0-3 (GPIO 26-29), the temperature sensor is on input 4. RP2350 QFN-80 : User ADC inputs are on 0-7 (GPIO 40-47), the temperature sensor is on input 8.

Temperature sensor values can be approximated in centigrade as:

T = 27 - (ADC_Voltage - 0.706)/0.001721

## Example

## Function Documentation

## adc_fifo_drain()

void adc_fifo_drain | ( | void |  | ) |

inline static

Drain the ADC FIFO.

Will wait for any conversion to complete then drain the FIFO, discarding any results.

## adc_fifo_get()

uint16_t adc_fifo_get | ( | void |  | ) |

inline static

Get ADC result from FIFO.

Pops the latest result from the ADC FIFO.

## adc_fifo_get_blocking()

uint16_t adc_fifo_get_blocking | ( | void |  | ) |

inline static

Wait for the ADC FIFO to have data.

Blocks until data is present in the FIFO

## adc_fifo_get_level()

uint8_t adc_fifo_get_level | ( | void |  | ) |

inline static

Get number of entries in the ADC FIFO.

On RP2040 the FIFO is 4 samples long.

This function will return how many samples are currently present.

## adc_fifo_is_empty()

bool adc_fifo_is_empty | ( | void |  | ) |

inline static

Check FIFO empty state.

Returns
  Returns true if the FIFO is empty

## adc_fifo_setup()

void adc_fifo_setup | ( | bool | en,
|  | bool | dreq_en,
|  | uint16_t | dreq_thresh,
|  | bool | err_in_fifo,
|  | bool | byte_shift )

inline static

Setup the ADC FIFO.

On RP2040 the FIFO is 4 samples long.

If a conversion is completed and the FIFO is full, the result is dropped.

Parameters

  en | Enables write each conversion result to the FIFO
  dreq_en | Enable DMA requests when FIFO contains data
  dreq_thresh | Threshold for DMA requests/FIFO IRQ if enabled.
  err_in_fifo | If enabled, bit 15 of the FIFO contains error flag for each sample
  byte_shift | Shift FIFO contents to be one byte in size (for byte DMA) - enables DMA to byte buffers.

## adc_get_selected_input()

uint adc_get_selected_input | ( | void |  | ) |

inline static

Get the currently selected ADC input channel.

Returns
  The currently selected input channel.

On RP2040 0...3 are GPIOs 26...29 respectively. Input 4 is the onboard temperature sensor.

## adc_gpio_init()

void adc_gpio_init | ( | uint | gpio | ) |

inline static

Initialise the gpio for use as an ADC pin.

Prepare a GPIO for use with ADC by disabling all digital functions.

Parameters

  gpio | The GPIO number to use. Allowable GPIO numbers are 26 to 29 inclusive on RP2040 or RP2350A, 40-48 inclusive on RP2350B

## adc_irq_set_enabled()

void adc_irq_set_enabled | ( | bool | enabled | ) |

inline static

Enable/Disable ADC interrupts.

Parameters

  enabled | Set to true to enable the ADC interrupts, false to disable

## adc_read()

uint16_t adc_read | ( | void |  | ) |

inline static

Perform a single conversion.

Performs an ADC conversion, waits for the result, and then returns it.

Returns
  Result of the conversion.

## adc_run()

void adc_run | ( | bool | run | ) |

inline static

Enable or disable free-running sampling mode.

Parameters

  run | false to disable, true to enable free running conversion mode.

## adc_select_input()

void adc_select_input | ( | uint | input | ) |

inline static

ADC input select.

Select an ADC input On RP2040 0...3 are GPIOs 26...29 respectively. Input 4 is the onboard temperature sensor.

Parameters

  input | Input to select.

## adc_set_clkdiv()

void adc_set_clkdiv | ( | float | clkdiv | ) |

inline static

Set the ADC Clock divisor.

Period of samples will be (1 + div) cycles on average. Note it takes 96 cycles to perform a conversion, so any period less than that will be clamped to 96.

Parameters

  clkdiv | If non-zero, conversion will be started at intervals rather than back to back.

## adc_set_round_robin()

void adc_set_round_robin | ( | uint | input_mask | ) |

inline static

Round Robin sampling selector.

This function sets which inputs are to be run through in round robin mode. RP2040, RP2350 QFN-60: Value between 0 and 0x1f (bit 0 to bit 4 for GPIO 26 to 29 and temperature sensor input respectively) RP2350 QFN-80: Value between 0 and 0xff (bit 0 to bit 7 for GPIO 40 to 47 and temperature sensor input respectively)

Parameters

  input_mask | A bit pattern indicating which of the 5/8 inputs are to be sampled. Write a value of 0 to disable round robin sampling.

## adc_set_temp_sensor_enabled()

void adc_set_temp_sensor_enabled | ( | bool | enable | ) |

inline static

Enable the onboard temperature sensor.

Parameters

  enable | Set true to power on the onboard temperature sensor, false to power off.