Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async analogRead #220

Closed
ppisljar opened this issue Feb 18, 2017 · 9 comments
Closed

async analogRead #220

ppisljar opened this issue Feb 18, 2017 · 9 comments
Labels
Type: Feature request Feature request for Arduino ESP32

Comments

@ppisljar
Copy link

if i understand correctly we can only do a sync analog read at the moment ?
this has a problem of being super slow and wasting many cpu cycles ...

i tried to split analogRead into 3 functions .... analogReadAsync, analogReadAsyncReady and analogReadAsyncGet :

void IRAM_ATTR __analogReadAsync(uint8_t pin)
{
    int8_t channel = digitalPinToAnalogChannel(pin);
    SET_PERI_REG_BITS(SENS_SAR_MEAS_START1_REG, SENS_SAR1_EN_PAD, (1 << channel), SENS_SAR1_EN_PAD_S);
    CLEAR_PERI_REG_MASK(SENS_SAR_MEAS_START1_REG, SENS_MEAS1_START_SAR_M);
    SET_PERI_REG_MASK(SENS_SAR_MEAS_START1_REG, SENS_MEAS1_START_SAR_M);
}

bool IRAM_ATTR __analogReadAsyncReady()
{
    return (GET_PERI_REG_MASK(SENS_SAR_MEAS_START1_REG, SENS_MEAS1_DONE_SAR) != 0);
}

uint16_t IRAM_ATTR __analogReadAsyncGet()
{
    return GET_PERI_REG_BITS2(SENS_SAR_MEAS_START1_REG, SENS_MEAS1_DATA_SAR, SENS_MEAS1_DATA_SAR_S);
}

and then use it like:

analogAsyncRead(34);
if (analogReadAsyncReady) myvalue = analogReadAsyncGet();

however this is far from perfect and could be implemented better i guess.

also is there an Interrupt i could set to be fired when adc reading is done ? or something like free running mode on atmega chips ? can i somehow change clock freq to make it faster (and less precise i guess) ?

@me-no-dev
Copy link
Member

Using Async here is really wrong :) this is not async at all. Interrupts are :)
give me some time to fix some other stuff I'm working on and I'll have another look at the ADC hardware and see what I can do.
Probably would not use "analogRead" in the names at all :)

@ppisljar
Copy link
Author

yeah i agree, its just something i was able to quickly hack together, and it seems its working (instead of waiting for adc finished flag in a loop i just am able to do other things now), but this should be solved with interrupts .... but i don't know enough to do it in the correct way :)

i would appreciate it if you can provide a better way to do it (also timer with interrupt would be nice)

@me-no-dev me-no-dev added the Type: Feature request Feature request for Arduino ESP32 label Feb 23, 2017
@me-no-dev
Copy link
Member

ADC non-blocking api has been implemented :)

void setup(){
  Serial.begin(115200);
  adcAttachPin(35);
  adcAttachPin(25);
}

void loop(){
  //start 1 pin for each ADC bus
  adcStart(35);
  adcStart(25);
  //you can check adcBusy(pin), but adcEnd will wait anyway
  uint16_t a0 = adcEnd(35);
  uint16_t a1 = adcEnd(25);
  Serial.printf("ADC: %u, %u\n", a0, a1);
}

@chemicstry
Copy link
Contributor

Does ESP32 have capabilities to do continuous ADC sampling via DMA? I want to do audio recording and not sure if it's possible.

@me-no-dev
Copy link
Member

me neither at this point, but could be researched ;) At quick glance, I can not see register definitions for controlling interrupt, I do not see anything like it implemented in idf, but I could be wrong.

@wrgallo
Copy link

wrgallo commented Oct 7, 2020

ADC non-blocking api has been implemented :)

void setup(){
  Serial.begin(115200);
  adcAttachPin(35);
  adcAttachPin(25);
}

void loop(){
  //start 1 pin for each ADC bus
  adcStart(35);
  adcStart(25);
  //you can check adcBusy(pin), but adcEnd will wait anyway
  uint16_t a0 = adcEnd(35);
  uint16_t a1 = adcEnd(25);
  Serial.printf("ADC: %u, %u\n", a0, a1);
}

This was removed for some reason, should this issue be oppened again?

Removed in d8b2098

@Tomat7
Copy link

Tomat7 commented Mar 5, 2021

Hello!

Why this function was removed?
What to use instead?

ILYA.

@Triangulix
Copy link

Hi
I just updated the toolchain from 1.0.4 to 1.0.6. Why are the functions for non-blocking ADC operations gone? I need them!

@stg
Copy link

stg commented Nov 11, 2022

Not sure if, or for how long, this would work for you (it's ESP32-S3 specific) - but I needed asynchronous/non-blocking ADC conversions today (running 1.0.5), and i needed them to be a lot faster than using the regular API:

https://github.com/stg/ESP32-S3-FastAnalogRead

Here's hoping it might be useful to others landing here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Feature request Feature request for Arduino ESP32
Projects
None yet
Development

No branches or pull requests

7 participants