rp2040: Add support for ADC on rp2350b chips

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2025-12-15 21:52:47 -05:00
parent 3a3e9fa2f1
commit a3e24d2172

View file

@ -1,6 +1,6 @@
// ADC functions on rp2040
//
// Copyright (C) 2021 Kevin O'Connor <kevin@koconnor.net>
// Copyright (C) 2021-2025 Kevin O'Connor <kevin@koconnor.net>
//
// This file may be distributed under the terms of the GNU GPLv3 license.
@ -10,6 +10,7 @@
#include "hardware/structs/adc.h" // adc_hw
#include "hardware/structs/padsbank0.h" // padsbank0_hw
#include "hardware/structs/resets.h" // RESETS_RESET_ADC_BITS
#include "hardware/structs/sysinfo.h" // sysinfo_hw
#include "internal.h" // enable_pclock
#include "sched.h" // sched_shutdown
@ -21,7 +22,19 @@ DECL_ENUMERATION("pin", "ADC_TEMPERATURE", ADC_TEMPERATURE_PIN);
struct gpio_adc
gpio_adc_setup(uint32_t pin)
{
if ((pin < 26 || pin > 29) && pin != ADC_TEMPERATURE_PIN)
uint32_t min_gpio = 26, max_gpio = 29, adc_temp_chan = 4;
#if CONFIG_MACH_RP2350
// Check for rp2350b package
if (!is_enabled_pclock(RESETS_RESET_SYSINFO_BITS))
enable_pclock(RESETS_RESET_SYSINFO_BITS);
if (!(sysinfo_hw->package_sel & SYSINFO_PACKAGE_SEL_BITS)) {
min_gpio = 40;
max_gpio = 47;
adc_temp_chan = 8;
}
#endif
if ((pin < min_gpio || pin > max_gpio) && pin != ADC_TEMPERATURE_PIN)
shutdown("Not a valid ADC pin");
// Enable the ADC
@ -32,10 +45,10 @@ gpio_adc_setup(uint32_t pin)
uint8_t chan;
if (pin == ADC_TEMPERATURE_PIN) {
chan = 4;
chan = adc_temp_chan;
adc_hw->cs |= ADC_CS_TS_EN_BITS;
} else {
chan = pin - 26;
chan = pin - min_gpio;
padsbank0_hw->io[pin] = PADS_BANK0_GPIO0_OD_BITS;
}