Page 1 of 1

Are any registers available for faster user variables?

Posted: Tue May 10, 2022 3:32 pm
by HankLloydRight
Hello,
I'm developing an ESP32 based DAC/driver for laser projectors. I'm getting pretty good performance in the main loop that runs via an interrupt (up to 70kHz). I'm also using direct register writes (instead of digitalWrite) to send data to the DACs.
But there are several user-settable variables that could change at any time (X/Y invert, scan speed, brightness, offsets, etc). Right now, I'm storing those in an NVRAM struct using Preferences (since I want the user-selected values to persist between sessions).
But is there a performance penalty in retrieving these NVRAM values to use when rendering the data in the main loop? Is there a better way to load these settings in spare register locations (they are mostly uint8/16/32) to cut down on the time to retrieve these values from NVRAM? Or is there essentially little or no difference?
Thank you.

Re: Are any registers available for faster user variables?

Posted: Wed May 11, 2022 1:29 am
by ESP_Sprite
If anything, it saves you a function call if you cache those variables locally.

Re: Are any registers available for faster user variables?

Posted: Wed May 11, 2022 11:57 am
by HankLloydRight
ESP_Sprite wrote:
Wed May 11, 2022 1:29 am
If anything, it saves you a function call if you cache those variables locally.
Thanks. Perhaps some code examples below will help explain (I should have included this first).

I'm creating a struct in memory and then calling GetPrefs() to populate that struct from NVRAM. I call this function once at system setup.

I'm then using prefs.current_settings.size, prefs.current_settings.invertX, prefs.current_settings.invertY, etc in the main loop.

It's these struct vars I'm wondering if there's a performance hit by doing it that way, or is there a faster way to access these variables?

Thanks.

  1. struct projector_settings_t {
  2.   char state;
  3.   float bright;
  4.   uint8_t pattern,blanking,size;
  5.   int16_t yoffset,xoffset;
  6.   uint32_t target_scan_rate;
  7.   boolean flipXY,invertX,invertY,optimize,initalized=false;
  8. };
  9.  
  10. struct preferences_t {
  11.   boolean flipOLED=true,StartWifi=false,StartBLE=true,StartSDCard=true;
  12.   uint8_t frame_delay=0;
  13.   projector_settings_t current_settings;
  14.   projector_settings_t saved_settings;
  15. };
  16.  
  17. preferences_t prefs;
  18.  
  19. void GetPrefs() {
  20.   preferences.begin("settings", false);
  21.   preferences.getBytes("prefs", &prefs, sizeof(prefs));
  22.   preferences.end();
  23. }

Re: Are any registers available for faster user variables?

Posted: Thu May 12, 2022 1:36 am
by ESP_Sprite
No, this is pretty much optimal. This way, the data is stored in internal memory which runs at the CPU clock.

Re: Are any registers available for faster user variables?

Posted: Thu May 12, 2022 10:45 am
by HankLloydRight
ESP_Sprite wrote:
Thu May 12, 2022 1:36 am
No, this is pretty much optimal. This way, the data is stored in internal memory which runs at the CPU clock.
Great, thank you!