1212
1313#include "hardware/adc.h"
1414
15- #define ADC_PIN_COUNT (NUM_ADC_CHANNELS - 1)
16-
17- #if ADC_PIN_COUNT == 4
18- #define ADC_FIRST_PIN_NUMBER 26
19- #else
20- #define ADC_FIRST_PIN_NUMBER 40
21- #endif
22-
23- // Voltage monitor is special on Pico W, because this pin is shared between the
24- // voltage monitor function and the wifi function. Special handling is required
25- // to read the analog voltage.
15+ // On many boards with a CYW43 radio co-processor, CYW43_DEFAULT_PIN_WL_CLOCK (usually GPIO29),
16+ // is both a voltage monitor and also SPI SCK to the CYW43.
17+ // Special handling is required to read the analog voltage.
2618#if CIRCUITPY_CYW43
2719#include "bindings/cyw43/__init__.h"
28- #define SPECIAL_PIN (pin ) (pin->number == 29 )
20+ #define SPECIAL_PIN (pin ) (pin->number == CYW43_DEFAULT_PIN_WL_CLOCK )
2921
3022const mcu_pin_obj_t * common_hal_analogio_analogin_validate_pin (mp_obj_t obj ) {
3123 return validate_obj_is_free_pin_or_gpio29 (obj , MP_QSTR_pin );
@@ -35,7 +27,7 @@ const mcu_pin_obj_t *common_hal_analogio_analogin_validate_pin(mp_obj_t obj) {
3527#endif
3628
3729void common_hal_analogio_analogin_construct (analogio_analogin_obj_t * self , const mcu_pin_obj_t * pin ) {
38- if (pin -> number < ADC_FIRST_PIN_NUMBER || pin -> number > ADC_FIRST_PIN_NUMBER + ADC_PIN_COUNT ) {
30+ if (pin -> number < ADC_BASE_PIN || pin -> number > ADC_BASE_PIN + NUM_ADC_CHANNELS - 1 ) {
3931 raise_ValueError_invalid_pin ();
4032 }
4133
@@ -70,15 +62,15 @@ uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) {
7062 uint32_t old_pad = pads_bank0_hw -> io [self -> pin -> number ];
7163 uint32_t old_ctrl = io_bank0_hw -> io [self -> pin -> number ].ctrl ;
7264 adc_gpio_init (self -> pin -> number );
73- adc_select_input (self -> pin -> number - ADC_FIRST_PIN_NUMBER );
65+ adc_select_input (self -> pin -> number - ADC_BASE_PIN );
7466 common_hal_mcu_delay_us (100 );
7567 value = adc_read ();
7668 gpio_init (self -> pin -> number );
7769 pads_bank0_hw -> io [self -> pin -> number ] = old_pad ;
7870 io_bank0_hw -> io [self -> pin -> number ].ctrl = old_ctrl ;
7971 common_hal_mcu_enable_interrupts ();
8072 } else {
81- adc_select_input (self -> pin -> number - ADC_FIRST_PIN_NUMBER );
73+ adc_select_input (self -> pin -> number - ADC_BASE_PIN );
8274 value = adc_read ();
8375 }
8476 // Stretch 12-bit ADC reading to 16-bit range
0 commit comments