uLisp as freertos task


#1

With these modifications I can correct ulisp as a freertos task.
As an example, we run the task Taskmain(),“arduino loop()” and a second task TaskBlink(), which blinks led 2, (LED_BUILTIN did not work for me)
This works fine, the led blinks and through the serial port I have the ulisp repl.
If in the repl I execute for example (+ 2 2 2 2) many times and in less than a second it gives me this error
"touchreadGuru Meditation Error: Core 1 panic’ed (Unhandled debug exception). 14:29:05.013 -> Debug exception reason: Stack canary watchpoint triggered (Main) "
Could it be due to the execution time of the task?
Thanks

#if CONFIG_FREERTOS_UNICORE
#define ARDUINO_RUNNING_CORE 0
#else
#define ARDUINO_RUNNING_CORE 1
#endif
#define LED_BUILTIN 2
// Setup
void TaskBlink( void *pvParameters );
void TaskMain( void *pvParameters );

void setup () {
  Serial.begin(9600);
  int start = millis();
  while ((millis() - start) < 5000) { if (Serial) break; }
  initworkspace();
  initenv();
  initsleep();
  initgfx();
  pfstring(PSTR("uLisp 4.3a "), pserial); pln(pserial);

  // Now set up two tasks to run independently.
  xTaskCreatePinnedToCore(
    TaskBlink
    ,  "TaskBlink"   // A name just for humans
    ,  1024  // This stack size can be checked & adjusted by reading the Stack Highwater
    ,  NULL
    ,  2  // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
    ,  NULL 
    ,  ARDUINO_RUNNING_CORE);

  xTaskCreatePinnedToCore(
    TaskMain
    ,  "Main"
    ,  1024  // Stack size
    ,  NULL
    ,  1  // Priority
    ,  NULL 
    ,  ARDUINO_RUNNING_CORE);

  }

void loop () 
{
}

void TaskMain(void *pvParameters)  // This is a task.
{
  (void) pvParameters;

  if (!setjmp(exception)) {
    #if defined(resetautorun)
    volatile int autorun = 12; // Fudge to keep code size the same
    #else
    volatile int autorun = 13;
    #endif
    if (autorun == 12) autorunimage();
  }
  // Come here after error
   vTaskDelay(100 / portTICK_PERIOD_MS);while (Serial.available()) Serial.read();
  clrflag(NOESC); BreakLevel = 0;
  for (int i=0; i<TRACEMAX; i++) TraceDepth[i] = 0;
  #if defined(sdcardsupport)
  SDpfile.close(); SDgfile.close();
  #endif
  #if defined(lisplibrary)
  if (!tstflag(LIBRARYLOADED)) { setflag(LIBRARYLOADED); loadfromlibrary(NULL); }
  #endif
  client.stop();
  repl(NULL);
  
}

void TaskBlink(void *pvParameters)  // This is a task.
{
  (void) pvParameters;
  // initialize digital LED_BUILTIN on pin 13 as an output.
  pinMode(2, OUTPUT);

  for (;;) // A Task shall never return or exit.
  {
    digitalWrite(2, HIGH);   // turn the LED on (HIGH is the voltage level)
    vTaskDelay(1000 / portTICK_PERIOD_MS );  // vTaskDelay wants ticks, not milliseconds
    digitalWrite(2, LOW);    // turn the LED off by making the voltage LOW
    vTaskDelay(1000 / portTICK_PERIOD_MS); // 1 second delay
  }
}

#2

If in the repl I execute for example (+ 2 2 2 2) many times and in less than a second it gives me this error
"touchreadGuru Meditation Error: Core 1 panic’ed (Unhandled debug exception). 14:29:05.013 -> Debug exception reason: Stack canary watchpoint triggered (Main) "
Could it be due to the execution time of the task?

“Stack canary” implies that the stack is overflowing. I’m not sure what could be causing that.


#3

This is the full trace of the error:

14:29:54.186 -> 9215> (+ Guru Meditation Error: Core  1 panic'ed (Unhandled debug exception). 
14:29:54.513 -> Debug exception reason: Stack canary watchpoint triggered (Main) 
14:29:54.559 -> Core  1 register dump:
14:29:54.606 -> PC      : 0x40084f39  PS      : 0x00060236  A0      : 0x00060230  A1      : 0x3ffb28d0  
14:29:54.699 -> A2      : 0x00000000  A3      : 0x00060223  A4      : 0x00060220  A5      : 0x00060223  
14:29:54.792 -> A6      : 0x00060223  A7      : 0x00000001  A8      : 0x00000001  A9      : 0x00000004  
14:29:54.885 -> A10     : 0x00060223  A11     : 0x00000032  A12     : 0x00000001  A13     : 0x10624dd3  
14:29:54.980 -> A14     : 0x007bf5f8  A15     : 0x003fffff  SAR     : 0x0000000a  EXCCAUSE: 0x00000001  
14:29:55.074 -> EXCVADDR: 0x00000000  LBEG    : 0x4008976c  LEND    : 0x40089776  LCOUNT  : 0x00000000

#4

Increase the task size from 1024 to 2048 and it no longer gives the error.
What would be the maximum assignable value?

 xTaskCreatePinnedToCore(
    TaskMain
    ,  "Main"
    ,  2048  // Stack size
    ,  NULL
    ,  2  // Priority
    ,  NULL 
    ,  ARDUINO_RUNNING_CORE);

  }

#5

Sorry, I don’t know.