Page 1 of 1

ULP counter

Posted: Mon May 18, 2020 9:46 am
by totsom
Hello everyone,
We are a team developing a software for the ESP32 and we are using the ULP coprocessor. We have encountered a problem when inplementing a counter. The ULP code used is the following:

Code: Select all

move r0, counter
ld r1, r0, 0          
add r1, r1, 1         
st r1, r0, 0
To print the variable in the main ESP32 program:

Code: Select all

printf("Counter value = %d\n", (ulp_counter &= UINT16_MAX));
The numbers that appear on screen are the double of what they are supposed to be. Do you know what may be the problem?
We think that it may be because it is a 16 bit variable in a 32 bit word, and when reading we read the correct 16 bit number followed by a 16 bit number 0.

If it is of any interest, we are currently using Arduino IDE with duff2013's ulptool.

Thanks in advance,
Marc

Re: ULP counter

Posted: Mon May 18, 2020 3:56 pm
by WiFive

Re: ULP counter

Posted: Tue May 19, 2020 7:21 am
by totsom
Thank you very much for your response it was really useful, altough we do not understand the reason why this happens..
In our case:
- First we put the main ESP32 cores to sleep
- The ULP coprocessor initializes a variable at 0
- A function inside the ULP adds 1 to this variable
- Wake up main ESP32 cores
- Print the variable. The result is 2
Why this happens?

Re: ULP counter

Posted: Tue May 19, 2020 8:56 am
by boarchuz
Is it possible the ULP is running again (incrementing to 2) before the SOC has time to wake up and read the value? What is your ULP wakeup period?

Re: ULP counter

Posted: Wed May 20, 2020 8:07 am
by totsom
Hello boarchuz, we have though of this possibility but after testing we don't think this is the case because after adding 1 to the variable we wake up the main cores and print the value. How could we try it better? The ULP wakeup period is 20ms.

Re: ULP counter

Posted: Wed May 20, 2020 9:07 am
by WiFive
You have to call halt if you don't want it to keep running

https://github.com/espressif/esp-idf/bl ... /wake_up.S

Re: ULP counter

Posted: Wed May 20, 2020 2:42 pm
by boarchuz
The default behaviour is to run repeatedly, waking every 20ms in your case.
You'll need to stop the timer like this:
https://github.com/espressif/esp-iot-so ... #L234-L237
Or if you only intend for it to run once (without wakeup timer) you can use this instead of ulp_run:
https://github.com/boarchuz/HULP/blob/7 ... p.cpp#L344