carduinoesp32freertosrtos

ESP32 Rtos problem, when compile GURU error


I have a Guru error when I try to compile and upload this code, this is only one part of the code, but the error is in this part.

In simulation and in my esp32 dev kit I get the same error!

#include <Arduino.h>
#include "esp_types.h"

#include <Wire.h>
#include <stdio.h>
#include <string.h>
#include <EEPROM.h>
#include <driver/ledc.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#include <BluetoothSerial.h>

#include <esp_task_wdt.h>

#define WATCHDOG_TIMOUT 30

#define MIN_SPEED_FAN 10 // In %
#define MAX_SPEED_FAN 100 // In %
#define INCRESE_STEP_SPEED 0.5 // In %

// Pins defines
#define LM_ADC 12
#define PRESSURE_ADC 35
#define PWM_FAN 13
#define OUT_HEATER 32

// Size of the data
#define DATA_SIZE 32

// Flash address
#define ADDR_REF_LM_VOL_1 0
#define ADDR_REF_LM_VOL_2 1
#define ADDR_REF_LM_VOL_3 2
#define ADDR_REF_LM_VOL_4 3
#define ADDR_PC_LOW_TEMP_1 5
#define ADDR_PC_LOW_TEMP_2 6
#define ADDR_PC_LOW_TEMP_3 7
#define ADDR_PC_LOW_TEMP_4 8
#define ADDR_PC_HIGH_TEMP_1 9
#define ADDR_PC_HIGH_TEMP_2 10
#define ADDR_PC_HIGH_TEMP_3 11
#define ADDR_PC_HIGH_TEMP_4 12
#define ADDR_PC_LOW_PRES_1 13
#define ADDR_PC_LOW_PRES_2 14
#define ADDR_PC_LOW_PRES_3 15
#define ADDR_PC_LOW_PRES_4 16
#define ADDR_PC_HIGH_PRES_1 17
#define ADDR_PC_HIGH_PRES_2 18
#define ADDR_PC_HIGH_PRES_3 19
#define ADDR_PC_HIGH_PRES_4 20

#define DELAY_READ 3000

// Create class bluetooth, to handle bluetooth communication
BluetoothSerial SerialBT;

TaskHandle_t taskControleTemperaturaHandle;

void taskLeituraSensor(void *pvParameters);

//_________________________________________________________________________Lm_________________________________________________________________________
// Reference voltage input lm
int iLmRefMv;
int iTempLmRefMv[] = {0, 0, 0, 0};
// Readed value LM
int iLmValue;
// Mean LM reads
float fMeanLmRead;
// Value read in Milli volts
float fMilliVolts;
// Temperature un C
float fTempC;

//_________________________________________________________________________Pressure___________________________________________________________________
// Readed value of pression
int iPressureValue;
//Mean pressure reads
float fMeanPressureRead;

//_________________________________________________________________________Temperature Control___________________________________________________________________
// Lower point of control of the temperature, this value is in C and should be multiply be 100
int iLowPcTemp;
int iTempLowPcTemp[] = {0, 0, 0, 0};
// High point of control of the temperature, this value is in C and should be multiply be 100
int iHighPcTemp;
int iTempHighPcTemp[] = {0, 0, 0, 0};
// Rember if the temperature is below the PC low
int iRecordBelowPcRange = 0;
// Rember if the temperature is above the PC low
int iRecordAbovePcRange = 0;

//_________________________________________________________________________Pressure Control___________________________________________________________________
// Lower point of control of the temperature, this value is in C and should be multiply be 100
int iLowPcPress;
int iTempLowPcPress[] = {0, 0, 0, 0};
// High point of control of the temperature, this value is in C and should be multiply be 100
int iHighPcPress;
int iTempHighPcPress[] = {0, 0, 0, 0};

int iPwmFrequency = 500;
int iPwmResolution = 12;
int iPwmValue = 0;

//_________________________________________________________________________Qtd of reads_________________________________________________________________________
uint8_t uiQtdRead = 30;

//_________________________________________________________________________Bluetooth variables_________________________________________________________________________
String sReceivedData = "";

//_________________________________________________________________________Setup_________________________________________________________________________
void setup() {
  // Serial comunicação com o PC
  Serial.begin(115200);

  //Serial Bluetooth
  //SerialBT.begin("Termikas");

  EEPROM.begin(DATA_SIZE);

  EEPROM.get(ADDR_REF_LM_VOL_1, iTempLmRefMv[0]);
  EEPROM.get(ADDR_REF_LM_VOL_2, iTempLmRefMv[1]);
  EEPROM.get(ADDR_REF_LM_VOL_3, iTempLmRefMv[2]);
  EEPROM.get(ADDR_REF_LM_VOL_4, iTempLmRefMv[3]);

  iLmRefMv = (iTempLmRefMv[0] - 48) * 1000;
  iLmRefMv += (iTempLmRefMv[1] - 48) * 100;
  iLmRefMv += (iTempLmRefMv[2] - 48) * 10;
  iLmRefMv += (iTempLmRefMv[3] - 48);

  EEPROM.get(ADDR_PC_LOW_TEMP_1, iTempLowPcTemp[0]);
  EEPROM.get(ADDR_PC_LOW_TEMP_2, iTempLowPcTemp[1]);
  EEPROM.get(ADDR_PC_LOW_TEMP_3, iTempLowPcTemp[2]);
  EEPROM.get(ADDR_PC_LOW_TEMP_4, iTempLowPcTemp[3]);

  iLowPcTemp = (iTempLowPcTemp[0] - 48) * 1000;
  iLowPcTemp += (iTempLowPcTemp[1] - 48) * 100;
  iLowPcTemp += (iTempLowPcTemp[2] - 48) * 10;
  iLowPcTemp += (iTempLowPcTemp[3] - 48);

  EEPROM.get(ADDR_PC_HIGH_TEMP_1, iTempHighPcTemp[0]);
  EEPROM.get(ADDR_PC_HIGH_TEMP_2, iTempHighPcTemp[1]);
  EEPROM.get(ADDR_PC_HIGH_TEMP_3, iTempHighPcTemp[2]);
  EEPROM.get(ADDR_PC_HIGH_TEMP_4, iTempHighPcTemp[3]);

  iHighPcTemp = (iTempHighPcTemp[0] - 48) * 1000;
  iHighPcTemp += (iTempHighPcTemp[1] - 48) * 100;
  iHighPcTemp += (iTempHighPcTemp[2] - 48) * 10;
  iHighPcTemp += (iTempHighPcTemp[3] - 48);

  EEPROM.get(ADDR_PC_LOW_PRES_1, iTempLowPcPress[0]);
  EEPROM.get(ADDR_PC_LOW_PRES_2, iTempLowPcPress[1]);
  EEPROM.get(ADDR_PC_LOW_PRES_3, iTempLowPcPress[2]);
  EEPROM.get(ADDR_PC_LOW_PRES_4, iTempLowPcPress[3]);
 
  iLowPcPress = (iTempLowPcPress[0] - 48) * 1000;
  iLowPcPress += (iTempLowPcPress[1] - 48) * 100;
  iLowPcPress += (iTempLowPcPress[2] - 48) * 10;
  iLowPcPress += (iTempLowPcPress[3] - 48);

  EEPROM.get(ADDR_PC_HIGH_PRES_1, iTempHighPcPress[0]);
  EEPROM.get(ADDR_PC_HIGH_PRES_2, iTempHighPcPress[1]);
  EEPROM.get(ADDR_PC_HIGH_PRES_3, iTempHighPcPress[2]);
  EEPROM.get(ADDR_PC_HIGH_PRES_4, iTempHighPcPress[3]);

  iHighPcPress = (iTempHighPcPress[0] - 48) * 1000;
  iHighPcPress += (iTempHighPcPress[1] - 48) * 100;
  iHighPcPress += (iTempHighPcPress[2] - 48) * 10;
  iHighPcPress += (iTempHighPcPress[3] - 48);
  
  pinMode(PWM_FAN, OUTPUT);
  pinMode(OUT_HEATER, OUTPUT);

  ledcSetup(0, iPwmFrequency, iPwmResolution);
  ledcAttachPin(PWM_FAN, 0);

  //xTaskCreatePinnedToCore(taskControleTemperatura, "Controle aquecedores", 1024, NULL, 1, NULL, tskNO_AFFINITY);
  //xTaskCreatePinnedToCore(taskControlePressao, "Controle Pressao", 1024, NULL, 2, NULL, tskNO_AFFINITY);
  xTaskCreate(taskLeituraSensor, "lmRead", configMINIMAL_STACK_SIZE + 1024, NULL, 1, &taskControleTemperaturaHandle);
  //xTaskCreatePinnedToCore(taskLeituraPressao, "Leitura do sensor de pressao", 1024, NULL, 19, NULL, tskNO_AFFINITY);

  digitalWrite(OUT_HEATER, LOW);

}

void loop() {
  for(int i = 0; i < 100000000; i++){

  }
}

void taskLeituraSensor(void * pvParameters){
  fTempC = 0;
  Serial.println("In read");
  /*for(int i = 0; i < uiQtdRead; i++){
    iLmValue = analogRead(LM_ADC);
    fMilliVolts = iLmValue * (iLmRefMv / 4096);
    fTempC = fMilliVolts/10;
    fMeanLmRead += fTempC;
  }
  Serial.println("afer for");
  fTempC = fTempC / uiQtdRead;
  Serial.println("afer fo22r");*/
  vTaskDelay(pdMS_TO_TICKS(3000)); 
}

When a up this code to a esp32 I have this error


Guru Meditation Error: Core  0 panic'ed (IllegalInstruction). Exception was unhandled.
Memory dump at 0x400d1fb4: e5f81fa1 f01dc1fe 22006136
Core  0 register dump:
PC      : 0x400d1fba  PS      : 0x00060e30  A0      : 0x00000000  A1      : 0x3ffbb900  
A2      : 0x00000000  A3      : 0x00000000  A4      : 0x00000000  A5      : 0x00000000  
A6      : 0x00000000  A7      : 0x00000000  A8      : 0x800d1fba  A9      : 0x3ffbb8e0  
A10     : 0x00000bb8  A11     : 0x3ffbf398  A12     : 0x00000014  A13     : 0x00000000  
A14     : 0x3ffbd2d0  A15     : 0x80000001  SAR     : 0x00000000  EXCCAUSE: 0x00000000  
EXCVADDR: 0x00000000  LBEG    : 0x4008f89d  LEND    : 0x4008f8ad  LCOUNT  : 0xfffffffd  


Backtrace: 0x400d1fb7:0x3ffbb900




ELF file SHA256: d76f5191ead975b1

This is only one part of the code, but the problem is in this area!

With execption decoder I get this error:

PC: 0x400d22dc
EXCVADDR: 0x00000000

Decoding stack results
0x400d22d9: TwoWire::TwoWire(unsigned char) at C:\Users\Dell-XPS\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.7\cores\esp32/Stream.h line 54

Solution

  • You should use a delay in loop() rather then a busy-wait loop. Any watchdog enabled will not be serviced while in that loop and any lower/equal priority tasks will be blocked from running:

    void loop() 
    {
      vTaskDelay( 1 ) ;  // do nothing, allow scheduler to run
    }
    

    I understand that in platformio on ESP32 setup() and loop() themselves run in an RTOS context.

    Your taskLeituraSensor() runs to completion and returns - that is not usual behaviour for a task function - normally a task runs in an indefinite loop. A task should at least delete itself to recover resources on termination:

    void taskLeituraSensor(void * pvParameters)
    {
      .
      .
      .
    
      vTaskDelay(pdMS_TO_TICKS(3000)); 
      vTaskDelete(0) ;  // Delete self
    }
    

    In any event it is not at all clear what the purpose of the delay is for if the task simply terminates in any event.

    A more usual task structure would be:

    void taskLeituraSensor(void * pvParameters)
    {
      for(;;)
      {
        .
        .
        .
    
        vTaskDelay(pdMS_TO_TICKS(3000)); 
      }
    }
    

    Or if the task is intended to terminate at some point:

    void taskLeituraSensor(void * pvParameters)
    {
      bool terminate = false ;
    
      while( !terminate )  // Run until terminated
      {
        .
        .
        .
    
        vTaskDelay(pdMS_TO_TICKS(3000)); 
      }
    
      vTaskDelete(0) ;  // Delete self on terminate
    }