esp-rs / esp-hal

no_std Hardware Abstraction Layers for ESP32 microcontrollers
https://docs.esp-rs.org/esp-hal/
Apache License 2.0
752 stars 210 forks source link

`async` ADC read #1643

Open jounathaen opened 5 months ago

jounathaen commented 5 months ago

Hi, I'm not so deep in the esp32's internals and also not in the code of the HAL, but I wondered if the ADC reading could be made async.

If I read the docs correctly, one basically has to use nb::block for a read, probably wasting a few hundred/thousand cycles in the process.

jessebraham commented 5 months ago

Based on a quick skimming of the TRM (for the C6, at least) it appears that this should be possible by configuring APB_SARADC_ADC_DONE_INT, which "triggers when the ADC completes one data conversion". As long as we have an interrupt we can create a future for it.

Also thank you for pointing this out, this is very valuable feedback! It's easy to overlook things like this :)

lu-zero commented 3 months ago

I just stumbled upon this problem. I wanted to have an embassy wifi example returning its battery status and nb::block does not look like it can be used.

nan-mu commented 3 months ago

I also don't know much about the specific operation of adc. I want to ask, can't we simply use the current read_oneshot to encapsulate a future? I will post an example later.

Update: Failing that, it seems like nb is refactoring their future macros, and then, I can't wrap read_oneshot without using unsafe

nan-mu commented 3 months ago

I'm wondering if we could have the ADC own all the pins it needs? That might make it easier to do asynchronous stuff, but it might complicate the type declaration.