How to use 2 cores in ESP32

andreoracio
Posts: 1
Joined: Mon Aug 15, 2022 5:40 pm

How to use 2 cores in ESP32

Postby andreoracio » Mon Aug 15, 2022 6:31 pm

Hello guys,

I have an ESP32 DEVKIT V1, I'm trying to use 2 skets running separately, one on core 0 and one on core 1.

I'm using VsCode + PlatformIO

I'm using the code below, but it doesn't work.

Code: Select all


#include <Arduino.h>
#include <SPI.h>
#include <mcp2515.h>
#include <Arduino_FreeRTOS.h>

// =============== Mapeamento de Hardware ==================
//==========================================================

#define GPIO4   4  //entrada para medir a frequência no pino digital 2
#define GPIO34 34
#define GPIO35 35

TaskHandle_t Task1;
TaskHandle_t Task2;

// ================== Variaveis Globais ====================
//==========================================================

#define resolucao 0.000805  //resolucao = Vref/4096 => 3.3/4096

//FREQUNCÍMETRO
long freq, freq1, counter;
int pulseCount;
boolean pulse = 1;

//AMPERÍMETRO
#define resistencia 159
float corrente = 0;    
float tensao = 0;
float corrente1 = 0;    
float tensao1 = 0;

// ================= Funções Auxiliares ====================
//==========================================================

long frequencia();
void task1code (void * pvParameters);
void task2code (void * pvParameters);

// =============== Configurações Iniciais ==================
//==========================================================

void setup() 
{
  Serial.begin(9600);

  pinMode(GPIO4,INPUT);    //Configura como entrada
  
  pulse = 0x01;             //Seta variável de controle

    //create a task that will be executed in the Task1code() function, with priority 1 and executed on core 0
  xTaskCreatePinnedToCore(
                    Task1code,   /* Task function. */
                    "Task1",     /* name of task. */
                    10000,       /* Stack size of task */
                    NULL,        /* parameter of the task */
                    1,           /* priority of the task */
                    &Task1,      /* Task handle to keep track of created task */
                    0);          /* pin task to core 0 */                  
  delay(500); 

  //create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1
  xTaskCreatePinnedToCore(
                    Task2code,   /* Task function. */
                    "Task2",     /* name of task. */
                    10000,       /* Stack size of task */
                    NULL,        /* parameter of the task */
                    1,           /* priority of the task */
                    &Task2,      /* Task handle to keep track of created task */
                    1);          /* pin task to core 1 */
    delay(500); 
  
} //end setup

//Task1code: blinks an LED every 1000 ms
void Task1code( void * pvParameters )
{
  Serial.print("Task1 running on core ");
  Serial.println(xPortGetCoreID());

  for(;;)
  {
    adc_noise = analogRead(GPIO34);
    tensao = adc_noise*resolucao;
    corrente = (tensao / resistencia)*1000;

    adc_noise2 = analogRead(GPIO35);
    tensao1 = adc_noise2*resolucao;
    corrente1 = (tensao1 / resistencia)*1000;
  } 
}

//Task2code: blinks an LED every 700 ms
void Task2code( void * pvParameters )
{
  Serial.print("Task2 running on core ");
  Serial.println(xPortGetCoreID());

  for(;;)
  {
    freq1 = frequencia();

    if(freq1 > 500)
    {
      Serial.print("Frequencia: ");
      Serial.println(freq1);
    }
  }
}

/*
// --- Loop Infinito ---
void loop() 
{
  

} //end loop
*/

//============================================================================
//============================================================================

long frequencia()
{
counter = millis();      //counter recebe o valor do tempo em ms
  
  if(digitalRead(GPIO4))  //Entrada de frequência em nível alto?
  {                        //Sim...
  
    if(pulse) pulseCount++;  //incrementa pulseCount se variável de controle for verdadeira
     
    pulse = 0x00;            //limpa variável de controle

  }
  else                     //Não...
  {
    pulse = 0x01;          //Seta variável de controle
  }


  if(counter%100 == 0x00)     //Passaram-se 200 ms?
  {                           //Sim...
    freq = pulseCount*10;       //Atualiza frequência (200 x 5 = 1000ms) 
    
    pulseCount = 0x00;     //Reinicia contador

  }
  return freq;
}

It displays the following error:
fatal error: avr/io.h: No such file or directory

Does anyone have any suggestions on how to solve this problem?

Thank you.

Thanks

ESP_Sprite
Posts: 9729
Joined: Thu Nov 26, 2015 4:08 am

Re: How to use 2 cores in ESP32

Postby ESP_Sprite » Tue Aug 16, 2022 2:56 am

Moving to the Arduino subforum.

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: How to use 2 cores in ESP32

Postby lbernstone » Tue Aug 16, 2022 4:08 am

No idea what Arduino_FreeRTOS.h is, but you don't need it.
https://github.com/espressif/arduino-es ... eeRTOS.ino

Who is online

Users browsing this forum: No registered users and 79 guests