Increase ULP program size

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

Increase ULP program size

Postby boarchuz » Mon Sep 03, 2018 12:24 pm

ulp_process_macros_and_load() fails with this error:

Code: Select all

E (22) ulp: program too big: 161 words, max is 128 words
Is it possible to simply increase program size or will I have to try to rewrite it a lot more efficiently? Any general tips for an amateur to reduce the size?

duffster
Posts: 16
Joined: Sun Mar 04, 2018 2:17 am

Re: Increase ULP program size

Postby duffster » Tue Sep 04, 2018 9:30 pm

You can try to edit the sdkconfig file in your esp32-arduino install. Search for CONFIG_ULP_COPROC_RESERVE_MEM and add more memory like 1024. My sdkcofig file was located in ../tools/sdk/

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

Re: Increase ULP program size

Postby boarchuz » Thu Jan 24, 2019 7:18 pm

I finally had a need to do this, and I think I found an issue.

ulp_process_macros_and_load returns ESP_ERR_ULP_SIZE_TOO_BIG even if CONFIG_ULP_COPROC_RESERVE_MEM has been adequately increased. I suspect it's using the default value (512) for comparison, instead of using the current config.

I tested with a 129-word ULP program in PlatformIO with "#define CONFIG_ULP_COPROC_RESERVE_MEM 1024" in sdkconfig.h.
-Testing with printf("ULP Memory: %d\n",CONFIG_ULP_COPROC_RESERVE_MEM); shows that the new value, 1024, has taken effect.
-ulp_process_macros_and_load() still returns an error: "E (55) ulp: program too big: 129 words, max is 128 words"

I tried the function at esp-idf/components/ulp/ulp_macro.c and it works perfectly!

Redstoned_boy
Posts: 5
Joined: Thu Jan 10, 2019 8:54 pm

Re: Increase ULP program size

Postby Redstoned_boy » Sun Jan 27, 2019 3:32 pm

So did it work for You in the end?
If it worked, what did You change exactly?
I ran into the same problem of less space then specified.
I measured just as You that 500 bytes of pure commands is the limit.
https://github.com/duff2013/arduino_ulp/issues/21

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

Re: Increase ULP program size

Postby boarchuz » Mon Jan 28, 2019 8:53 am

The more I fiddled with it, the more confused I became. I'll just dump a bunch of info and thoughts here in case it's useful for anyone who finds this thread or can point out where I'm going wrong...

What I noticed was that the custom "CONFIG_ULP_COPROC_RESERVE_MEM" seemed to have no effect *within* the ulp_process_macros_and_load scope.

I edited ulp.h to print debug output like printf("ULP Mem in ulp.h: %d",CONFIG_ULP_COPROC_RESERVE_MEM);
Then with code that looked something like this:

Code: Select all

#include "sdkconfig.h" //with CONFIG_ULP_COPROC_RESERVE_MEM set to 1024, for example
...
printf("ULP Mem in main app: %d",CONFIG_ULP_COPROC_RESERVE_MEM);
...
ulp_process_macros_and_load(...parameters...); //in which it also prints the debug output mentioned earlier
...
printf("ULP Mem in main app: %d",CONFIG_ULP_COPROC_RESERVE_MEM);
...
My output was something like this:

Code: Select all

ULP Mem in main app: 1024
ULP Mem in ulp.h: 512
ULP Mem in main app: 1024
So the sdkconfig.h is definitely being loaded, but it's as if the ulp.h is compiled *before* the custom CONFIG_ULP_COPROC_RESERVE_MEM is defined (ie. before sdkconfig.h is included) and/or it redefines it at some point back to the default 512.

This also explained why I was able to load the program with what I thought was a fixed version as noted in my earlier comment. I actually had copied that function into a new class I had made. I realise now that what was actually happening was that it was simply using the custom value because it was within my app 'scope', not the underlying framework where the value remains the 512 default.

I'm still a little too new to this to know if it's something I'm doing wrong or if there's an issue somewhere (and I only just made the switch to PlatformIO).

Other things: I can see in the value being used here: https://github.com/espressif/arduino-es ... p32_out.ld
but here it's hardcoded as 512 (still shouldn't affect the definition though): https://github.com/espressif/arduino-es ... p32_out.ld
I tried random things like different sizes, sprinkling "include sdkconfig.h" all over the place to make sure that was being loaded before it was needed. Unsurprisingly, no benefit.

Anyway, if you want a workaround you can either copy the function into your code, or just edit the function to skip the size check for now. It will load and run fine, it will just overflow into RTC slow memory so you'll need to manually write and read from the actual offset if you're using it (eg. anything with IRAM_DATA_ATTR). (Eventually, I just found another way to cram more work into 128 words so the ULP program fits in the default allocation.) That's all for now.

chegewara
Posts: 2207
Joined: Wed Jun 14, 2017 9:00 pm

Re: Increase ULP program size

Postby chegewara » Mon Jan 28, 2019 9:27 am

The only answer i can think of now is that library/component that is used by ULP is precompiled with CONFIG_ULP_COPROC_RESERVE_MEM = 512 and changing it in sdkconfig makes no difference. It will works only if you use arduino as component.

Redstoned_boy
Posts: 5
Joined: Thu Jan 10, 2019 8:54 pm

Re: Increase ULP program size

Postby Redstoned_boy » Mon Jan 28, 2019 1:34 pm

boarchuz wrote:
Mon Jan 28, 2019 8:53 am
Anyway, if you want a workaround you can either copy the function into your code, or just edit the function to skip the size check for now. It will load and run fine, it will just overflow into RTC slow memory so you'll need to manually write and read from the actual offset if you're using it (eg. anything with IRAM_DATA_ATTR). (Eventually, I just found another way to cram more work into 128 words so the ULP program fits in the default allocation.) That's all for now.
Which function do you mean exactly?
In which file is it?

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

Re: Increase ULP program size

Postby boarchuz » Mon Jan 28, 2019 2:30 pm

https://github.com/espressif/esp-idf/bl ... lp_macro.c

Put a copy of that file in your project directory, rename "ulp_process_macros_and_load" in there to something like "unsafe_ulp_process_macros_and_load" and comment out line "return ESP_ERR_ULP_SIZE_TOO_BIG;", then #include "ulp_macro.c" in your sketch. Use: unsafe_ulp_process_macros_and_load(addr, program, &size);

Redstoned_boy
Posts: 5
Joined: Thu Jan 10, 2019 8:54 pm

Re: Increase ULP program size

Postby Redstoned_boy » Mon Jan 28, 2019 4:45 pm

I'm actually don't using the macros but the "real" ulp instruction instead. Unfortunatly I couldn't get it working for me with the function ulp_load_binary in ulp.c . When I try to upload an ulp-program with more than 512 bytes.

Redstoned_boy
Posts: 5
Joined: Thu Jan 10, 2019 8:54 pm

Re: Increase ULP program size

Postby Redstoned_boy » Mon Jan 28, 2019 5:20 pm

So I finally got it working for the assembly-like ulp-code. The key was as You said the custom function without the return-instructions except of ESP_OK and changing the LENGTH-value in esp32.ulp.ld: ram(RW) : ORIGIN = 0, LENGTH = 8192.
A #define here will not work. I have to test it more to say more about it, but I've got a 560-bytes program runningon the ULP.

Who is online

Users browsing this forum: lbernstone and 60 guests