Skip to content

Commit

Permalink
Refactor the ADC to use the new API.
Browse files Browse the repository at this point in the history
  • Loading branch information
floitsch committed Jan 29, 2025
1 parent a065f50 commit ccd0ae0
Show file tree
Hide file tree
Showing 5 changed files with 325 additions and 173 deletions.
52 changes: 37 additions & 15 deletions lib/gpio/adc.toit
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,47 @@ Analog-to-Digital Conversion.
This library provides ways to read analogue voltage values from GPIO pins that
support it.
On the ESP32, there are two ADCs. ADC1 (pins 32-39) should be preferred as
ADC2 (pins 0, 2, 4, 12-15, 25-27) has lots of restrictions. It can't be
used when WiFi is active, and some of the pins are
strapping pins). By default, ADC2 is disabled, and users need to pass in a flag to
allow its use.
On most ESP32 variants, there are two ADCs. ADC1 should be preferred as
ADC2 has lots of restrictions. It can't be used when WiFi is active, and some
of the pins are strapping pins.
By default, ADC2 is disabled, and users need to pass in a flag to allow its use.
# Examples
```
import gpio
import gpio.adc show Adc
main:
adc := Adc (gpio.Pin 34)
pin := gpio.Pin 34
adc := Adc pin
print adc.get
adc.close
pin.close
```
# Pins
## ESP32
ADC1: Pins 32-39
ADC2: Pins 0, 2, 4, 12-15, 25-27.
## ESP32C3
ADC1: Pins 0-4
Pin 5 is an ADC pin of channel 2. However, the controller of ADC channel 2
may enter an inoperative state and was therefore disabled by Espressif.
An errata was published.
## ESP32C6
ADC1: Pins 0-6
No ADC2.
## ESP32S2
ADC1: Pins 1-10
ADC2: Pins 11-20
## ESP32S3
ADC1: Pins 1-10
ADC2: Pins 11-20
*/

/**
Expand All @@ -44,17 +69,14 @@ class Adc:
tune the attenuation of the underlying ADC unit. If no $max-voltage is
provided, the ADC uses the maximum voltage range of the pin.
If $allow-restricted is true, allows pins that are restricted.
See the ESP32 section below.
# ESP32
On the ESP32, there are two ADCs. ADC1 (pins 32-39) should be preferred as
ADC2 (pins 0, 2, 4, 12-15, 25-27) has lots of restrictions. It can't be
used when WiFi is active, and some of the pins are
strapping pins). By default, ADC2 is disabled, and users need to pass in the
# ESP32 and variants.
On most ESP32 variants, there are two ADCs. Typically, ADC1 should be
preferred as ADC2 has lots of restrictions. It can't be
used when WiFi is active, and some of the pins are strapping pins.
By default, ADC2 is disabled, and users need to pass in the
$allow-restricted flag to allow its use.
*/
constructor .pin --max-voltage/float?=null --allow-restricted/bool=false:
constructor .pin --max-voltage/float?=null --allow-restricted/bool?=false:
resource_ = adc-init_ resource-freeing-module_ pin.num allow-restricted (max-voltage ? max-voltage : 0.0)

/**
Expand Down
5 changes: 3 additions & 2 deletions lib/gpio/gpio.toit
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ Pins 12-17 are normally connected to flash/PSRAM, and should not be used.
Pins 18-19 are JTAG pins, and should not be used if JTAG support is needed.
Pins 0-5 are RTC pins and can be used in deep-sleep.
Pins 0-4 are ADC pins of channel 1.
Pin 5 is an ADC pin of channel 2. ADC channel 2 has restrictions and should be
avoided if possible.
Pin 5 is an ADC pin of channel 2. However, the controller of ADC channel 2
may enter an inoperative state and was therefore disabled by Espressif.
An errata was published.
# ESP23C6
The ESP32C6 has 31 physical pins (0-30). Each pin can be used as
Expand Down
Loading

0 comments on commit ccd0ae0

Please sign in to comment.