ULP counter

totsom
Posts: 4
Joined: Mon May 18, 2020 9:27 am

ULP counter

Postby totsom » Mon May 18, 2020 9:46 am

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

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: ULP counter

Postby WiFive » Mon May 18, 2020 3:56 pm


totsom
Posts: 4
Joined: Mon May 18, 2020 9:27 am

Re: ULP counter

Postby totsom » Tue May 19, 2020 7:21 am

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?

boarchuz
Posts: 606
Joined: Tue Aug 21, 2018 5:28 am

Re: ULP counter

Postby boarchuz » Tue May 19, 2020 8:56 am

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?

totsom
Posts: 4
Joined: Mon May 18, 2020 9:27 am

Re: ULP counter

Postby totsom » Wed May 20, 2020 8:07 am

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.

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: ULP counter

Postby WiFive » Wed May 20, 2020 9:07 am

You have to call halt if you don't want it to keep running

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

boarchuz
Posts: 606
Joined: Tue Aug 21, 2018 5:28 am

Re: ULP counter

Postby boarchuz » Wed May 20, 2020 2:42 pm

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

Who is online

Users browsing this forum: Bing [Bot] and 124 guests