Call long running function on ESP32

Hello!
I'm trying to run code work of what has a duration about several seconds.
And the problem is that it's only work in continuous manner (if it launched in loop section):

void loop() { 
    activation_sequence();
}

But I need to run it only once, for example, in setup section like this:

void setup() {
  pinMode(sew_button, INPUT);
  for(uint16_t i; i < 8192; i++)
    firmware[i] = 0xAA;
    activation_sequence();
}

When I try to run it like in the last example, work of the function "activation_sequnce" is broken after some milliseconds, far before end of function, and the following after call of function line of code is executed.

I need this function to be executed fully before execution of following call of this function instruction. How to reach this?

I suppose that the causes are some timeouts or watchdogs and they should be increased. Or blocking call of function should be used. But I don't know how to do this.

Please show code that compiles. You may be doing something bad in the remaining code.

void act_sync_frame() {
  delayMicroseconds(100);
  digitalWrite(swim, LOW);
  delayMicroseconds(16);
  digitalWrite(swim, HIGH);
  delayMicroseconds(500);
  digitalWrite(swim, LOW);

  for (uint8_t i = 0; i < 9; i++) {
    delayMicroseconds(500);

    if (i % 2 == 0) digitalWrite(swim, HIGH);
    else digitalWrite(swim, LOW);
  }

  for (uint8_t i = 0; i < 7; i++) {
    delayMicroseconds(250);

    if (i % 2 == 0) digitalWrite(swim, LOW);
    else digitalWrite(swim, HIGH);
  }
  delayMicroseconds(500);
  digitalWrite(swim, HIGH);
  delayMicroseconds(1);
  pinMode(swim, INPUT);
  while(digitalRead(swim) != LOW);digitalWrite(rst, HIGH);
  while(digitalRead(swim) != HIGH) swim_clock++;
  pinMode(swim, OUTPUT);
  digitalWrite(swim, HIGH);
  delayMicroseconds(1);
  HSI_corrector = (float) swim_clock / 342.0;
  short_phase = (float) short_phase * HSI_corrector + 1.0;
  LS_long_phase = (float) LS_long_phase * HSI_corrector + 1.0;
  HS_long_phase = (float) HS_long_phase * HSI_corrector + 1.0;
}

volatile void activation_sequence() {
  uint8_t SWIM_CSR[1] = {SAFE_MASK | SWIM_DM | PRI};

  digitalWrite(rst, HIGH);
  digitalWrite(swim, HIGH);
  pinMode(rst, OUTPUT);
  pinMode(swim, OUTPUT);
  digitalWrite(rst, LOW);

  act_sync_frame();
  //writeByte(SWIM_CSR);
  WOTF_command(1, 0x00007F80, SWIM_CSR);

  digitalWrite(rst, HIGH);
  //WOTF_command(1, 0x00007F80, &SWIM_CSR);

  delayNanoseconds_25(LS_long_phase + 1000);
}

Try inserting short delays (e.g.., 1 microsecond) in your while loops to allow the thread to yield control. You may be timing out the watchdog. Keep the serial monitor open while you run the program and it will show the error call stack if your watchdog is resetting.

I can't afford insert short delays because I need a hundred nanoseconds accuracy

Can you suggest how to disable the watchdog if you know, please?

If you're using the arduino framework, IIRC you can't avoid starting the watchdog and typically they can't be turned off after starting.

I don't think it's wise to attempt to get that level of timing accuracy from busy loops when running with an RTOS. My experience tells me that you'll get very jittery timing. Unfortunately, I haven't used ESP32 enough to know how all its timer functions work, but that would be the right approach.

There's this; ESP32TimerInterrupt - Arduino Reference but as I said, I have no experience with it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.