ESP32S3 FSM ULP Code Question

dmydmy
Posts: 2
Joined: Tue Feb 11, 2025 12:07 pm

ESP32S3 FSM ULP Code Question

Postby dmydmy » Tue Feb 11, 2025 3:08 pm

Hi all,
I have a question regarding my ESP32S3 FSM ULP program. This one here works, when I periodically read out address 130 in the Arduino code I can see the counter increasing

Code: Select all

const ulp_insn_t ulp_prog[] = {
    M_LABEL(0),
    I_MOVI(R1, 130),
    I_LD(R0, R1, 0),
    I_ADDI(R0, R0, 1),
    I_ST(R0, R1, 0),
    M_BX(0)
};
However, if want to run the counter up to 100 and then enter an infinite loop the counter stops at 1. why?

Code: Select all

const ulp_insn_t ulp_prog[] = {
    M_LABEL(0),
    I_MOVI(R1, 130),
    I_LD(R0, R1, 0),
    I_ADDI(R0, R0, 1),
    I_ST(R0, R1, 0),
    I_BL(3, 100),
    M_LABEL(3),
    M_BX(3),
    M_BX(0)
};
Why doesn't it branch to M_BX(0) when R0 is less than 100, increment the counter by, repeat until R0 = 100 and then do an infinite loop M_LABEL(3), M_BX(3)?

For reference, here's the entire Arduino code:

Code: Select all

#include "esp32s3/ulp.h"
#include "ulp_common.h"

// Define the ULP program
const ulp_insn_t ulp_prog[] = {
    M_LABEL(0),
    I_MOVI(R1, 130),
    I_LD(R0, R1, 0),
    I_ADDI(R0, R0, 1),
    I_ST(R0, R1, 0),
    I_BL(3, 100),
    M_LABEL(3),
    M_BX(3),
    M_BX(0)
};

void setup() {
  Serial.begin(115200);
  
  // Initialize ULP
  size_t prog_size = sizeof(ulp_prog) / sizeof(ulp_insn_t);
  ESP_ERROR_CHECK(ulp_process_macros_and_load(0, ulp_prog, &prog_size));
  
  // Start ULP program
  ESP_ERROR_CHECK(ulp_run(0));

  // Initialize counter to 0
  RTC_SLOW_MEM[130] =  0;
}

void loop() {
  // Read counter value
  uint32_t counter = RTC_SLOW_MEM[130];
  Serial.printf("Counter value: %ld\n", counter);
  
  delay(1000);  // Wait for 1 second before next read
}
Thanks a lot in advance!
dmy

dmydmy
Posts: 2
Joined: Tue Feb 11, 2025 12:07 pm

Re: ESP32S3 FSM ULP Code Question

Postby dmydmy » Thu Feb 13, 2025 11:33 am

Found the solution.

Code: Select all

M_LABEL(),
does NOT count as an instruction. So it needs to be

Code: Select all

I_BL(2, 100),
for the code to work.

Who is online

Users browsing this forum: No registered users and 76 guests