simple beginners questions about ESP32 - Arduino

YvesDS
Posts: 13
Joined: Sat Sep 30, 2017 10:57 am
Location: Bredene (Belgium)

simple beginners questions about ESP32 - Arduino

Postby YvesDS » Fri Oct 13, 2017 6:34 pm

Hi all,

i have some simple questions about ESP32 programming in Arduino :

My setup :
Windows 7, Arduino IDE with latest ESP32 toolchain installed, NodeMCU-32S board, 80Mhz,921600, COM4

The esp32 has two cores, one can assign a task to either one of the cores.
I would like to do some logic on one core: taking input from a sensor (photocell) connected to GPIO25 through a interrupt and ISR (where i take the time when the interrupt fired and store it in a volatile variable "interruptExecuteTime")
On the second core i would like to do the output part (first on serial monitor, later on I2C LCD)
so the question is : how to create 2 tasks and according functions for the two seperate cores, and assign them to the correct core?
how to trigger the different tasks each time the sensors fire the interrupt?
is it OK to pass the data by declaring a volatile variable?

I have done this to start with :

Code: Select all

#include "arduino.h"

const byte interruptPin = 25;
volatile int interruptCounter = 0;
volatile double interruptExecuteTime = 0;
static int taskCore0 = 0;
static int taskCore1 = 1;
int numberOfInterrupts = 0;
 
portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;

void core1Task( void * pvParameters ){
      Serial.print("An interrupt has occurred. Total: ");
      Serial.print(numberOfInterrupts);
      Serial.print(" at :");
      Serial.print(interruptExecuteTime);
      Serial.print(" on Core :");
      Serial.println(xPortGetCoreID());
}
void handleInterrupt(void * pvParameters) {
  portENTER_CRITICAL_ISR(&mux);
  interruptExecuteTime = millis(); // this is the important part for me
  interruptCounter++;
  portEXIT_CRITICAL_ISR(&mux);
}

 
void setup() {
 
  Serial.begin(115200);
  Serial.println("Monitoring interrupts: ");
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, FALLING);
  xTaskCreatePinnedToCore(
                  handleInterrupt,   /* Function to implement the task */
                  "handleinterrupt", /* Name of the task */
                  10000,      /* Stack size in words */
                  NULL,       /* Task input parameter */
                  0,          /* Priority of the task */
                  NULL,       /* Task handle. */
                  taskCore0);  /* Core where the task should run */
  xTaskCreatePinnedToCore(
                  core1Task,   /* Function to implement the task */
                  "core1Task", /* Name of the task */
                  10000,      /* Stack size in words */
                  NULL,       /* Task input parameter */
                  0,          /* Priority of the task */
                  NULL,       /* Task handle. */
                  taskCore1);  /* Core where the task should run */
}
 
 
void loop() {
 
  if(interruptCounter>0){ // greater than 0 means there was a interrupt fired
       portENTER_CRITICAL(&mux);
      interruptCounter--; // decrease to reset the value
      portEXIT_CRITICAL(&mux);
      numberOfInterrupts++; // count the numbers of interrupts fired at this moment
}
  if (numberOfInterrupts >= 25){
    numberOfInterrupts = 0;
    esp_restart(); // restart example by cleaning all data and resetting MCU
  }
}
when i compile, i get this error message :

Code: Select all

C:\Users\Beheerder\Documents\Arduino\ESP32 projects\esp_restart_with_interrupts_example1\esp_restart_with_interrupts_example1.ino: In function 'void setup()':

esp_restart_with_interrupts_example1:34: error: invalid conversion from 'void (*)(void*)' to 'void (*)()' [-fpermissive]

   attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, FALLING);

                                                                                ^
In file included from C:\Users\Beheerder\Documents\Arduino\hardware\espressif\esp32\cores\esp32/esp32-hal.h:52:0,

                 from C:\Users\Beheerder\Documents\Arduino\hardware\espressif\esp32\cores\esp32/Arduino.h:35,

                 from C:\Users\BEHEER~1\AppData\Local\Temp\arduino_build_465995\sketch\esp_restart_with_interrupts_example1.ino.cpp:1:

C:\Users\Beheerder\Documents\Arduino\hardware\espressif\esp32\cores\esp32/esp32-hal-gpio.h:81:6: note:   initializing argument 2 of 'void attachInterrupt(uint8_t, void (*)(), int)'

 void attachInterrupt(uint8_t pin, void (*)(void), int mode);

      ^
exit status 1
invalid conversion from 'void (*)(void*)' to 'void (*)()' [-fpermissive]
So my code did not work for me :(
Since i am a absolute beginner on the ESP32 platform, this looked like a no-brainer, but that came out very different ;)
What am i doing wrong?
What should i read as documentation on this?

Grtz,
Yves

Markonian
Posts: 3
Joined: Sat Oct 21, 2017 5:48 pm

Re: simple beginners questions about ESP32 - Arduino

Postby Markonian » Sat Oct 21, 2017 6:04 pm

Please re-read the first error message. It tells you exactly what the error is.

Now if you look to see why the compiler wants to convert 'void (*)(void*)' to 'void (*)()', you will notice that attachInterrupt() requires a pointer to a function that returns nothing and takes NO ARGUMENTS. You are supplying a function that has 1 ARGUMENT.

See the documentation for attachInterrupt() for further details.

Now, my question for anyone that can help... what's wrong with my embedded link markup above? It doesn't seem to work the way I expected (should not show markup and url, just link text).

YvesDS
Posts: 13
Joined: Sat Sep 30, 2017 10:57 am
Location: Bredene (Belgium)

Re: simple beginners questions about ESP32 - Arduino

Postby YvesDS » Sun Oct 22, 2017 11:53 am

Markonian wrote:Please re-read the first error message. It tells you exactly what the error is.
Now if you look to see why the compiler wants to convert 'void (*)(void*)' to 'void (*)()', you will notice that attachInterrupt() requires a pointer to a function that returns nothing and takes NO ARGUMENTS. You are supplying a function that has 1 ARGUMENT.
so i need to write : "attachInterrupt(digitalPinToInterrupt(interruptPin), &handleInterrupt, FALLING);" then? (by & referencing then?)
Grtz,
Yves

ahue32
Posts: 16
Joined: Mon Nov 06, 2017 10:09 am

Re: simple beginners questions about ESP32 - Arduino

Postby ahue32 » Tue Nov 28, 2017 8:20 am

YvesDS wrote: so i need to write : "attachInterrupt(digitalPinToInterrupt(interruptPin), &handleInterrupt, FALLING);" then? (by & referencing then?)
Grtz,
Yves
Markonian meant that "void handleInterrupt(void * pvParameters)" takes the argument "pvParameters", but you can't call a function with an argument. So it would have to look like this "void handleInterrupt()". To me it seems that you don't neet "pvParameters" anyway (I might be wrong though...)

Markonian
Posts: 3
Joined: Sat Oct 21, 2017 5:48 pm

Re: simple beginners questions about ESP32 - Arduino

Postby Markonian » Wed Jan 03, 2018 5:20 pm

I apologize for not following this topic. I just noticed it today. I guess I was not automatically subscribed if I post a reply?

I'm also sorry for my lack of clarity in my previous post to this topic.

In setup() you are calling the function attachInterrupt. The 2nd parameter is the name of the function handleInterrupt, which is automatically converted to a pointer to the function handleInterrupt. However, attachInterrupt requires a function having 0 arguments, whereas the function you are supplying, handleInterrupt, has 1 argument. The compiler attempts to convert what you have supplied as 2nd argument of attachInterrupt, finds a type mismatch, attempts to convert what you've supplied to a "pointer to 0 arg function returning void", but can't (because it's a real logical error), and produces: "error: invalid conversion from 'void (*)(void*)' to 'void (*)()'".

User avatar
jgustavoam
Posts: 117
Joined: Thu Feb 01, 2018 2:43 pm
Location: Belo Horizonte , Brazil
Contact:

Re: simple beginners questions about ESP32 - Arduino

Postby jgustavoam » Mon Feb 19, 2018 12:14 am

Hi, I'm a beginner on ESP32 as well.
I made some changes (suppress) just for testing and this code works. GPIO_2 generates 5 Hz pulses to test the interrupt pin (connect GPIO_2 to GPIO_25). Make your changes as needed.

Code: Select all

#include "arduino.h"

const byte interruptPin = 25;
volatile int interruptCounter = 0;
volatile double interruptExecuteTime = 0;
static int taskCore0 = 0;
static int taskCore1 = 1;
int numberOfInterrupts = 0;

//portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;

void core1Task () //( void * pvParameters )
{
  Serial.print("An interrupt has occurred. Total: ");
  Serial.print(numberOfInterrupts);
  Serial.print(" at :");
  Serial.print(interruptExecuteTime);
  Serial.println(" on Core :");
  delay(800);
  //Serial.println(xPortGetCoreID());
}

void handleInterrupt()   //void * pvParameters)
{
  //portENTER_CRITICAL_ISR(&mux);
  interruptExecuteTime = millis();   // this is the important part for me
  interruptCounter++;
  core1Task ();
  // portEXIT_CRITICAL_ISR(&mux);
}


void setup()
{
  Serial.begin(115200);
  Serial.println("Monitoring interrupts: ");
  pinMode(interruptPin, INPUT);

  // Generating 5 Hz Pulses - For test, attach GPIO_2 to GPIO_25
  pinMode(2, OUTPUT);       // GPIO_2 as Output
  ledcAttachPin(2, 0);      // GPIO_2 attached to PWM Channel 0
  ledcSetup(0, 5, 13);     // Channel 0 , freq 5Hz , 13 bit resolution
  ledcWrite(0, 819);        // Enable frequency with dutty cycle 10%

  attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, FALLING);

  /*xTaskCreatePinnedToCore
    (
    handleInterrupt,   // Function to implement the task
    "handleinterrupt", // Name of the task
    10000,             // Stack size in words
    NULL,              // Task input parameter
    0,                 // Priority of the task
    NULL,              // Task handle.
    taskCore0);        // Core where the task should run

    xTaskCreatePinnedToCore(
    core1Task,         // Function to implement the task
    "core1Task",       // Name of the task
    10000,             // Stack size in words
    NULL,              // Task input parameter
    0,                 // Priority of the task
    NULL,              // Task handle.
    taskCore1);        // Core where the task should run */
}


void loop()
{
  if (interruptCounter > 0)
  { // greater than 0 means there was a interrupt fired
    //  portENTER_CRITICAL(&mux);
    interruptCounter--; // decrease to reset the value
    //   portEXIT_CRITICAL(&mux);
    numberOfInterrupts++; // count the numbers of interrupts fired at this moment
  }
  if (numberOfInterrupts >= 25)
  {
    numberOfInterrupts = 0;
    //esp_restart(); // restart example by cleaning all data and resetting MCU
  }
}
Console IDE Arduino (115200Bps)

Code: Select all

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:812
load:0x40078000,len:0
load:0x40078000,len:11404
entry 0x40078a28
Monitoring interrupts: 
An interrupt has occurred. Total: 0 at :244.00 on Core :
An interrupt has occurred. Total: 0 at :444.00 on Core :
An interrupt has occurred. Total: 0 at :644.00 on Core :
An interrupt has occurred. Total: 0 at :844.00 on Core :
An interrupt has occurred. Total: 0 at :1044.00 on Core :
An interrupt has occurred. Total: 5 at :1244.00 on Core :
An interrupt has occurred. Total: 6 at :1444.00 on Core :
An interrupt has occurred. Total: 7 at :1644.00 on Core :
An interrupt has occurred. Total: 8 at :1844.00 on Core :
An interrupt has occurred. Total: 8 at :2044.00 on Core :
An interrupt has occurred. Total: 8 at :2244.00 on Core :
An interrupt has occurred. Total: 8 at :2444.00 on Core :
An interrupt has occurred. Total: 8 at :2644.00 on Core :
An interrupt has occurred. Total: 13 at :2844.00 on Core :
An interrupt has occurred. Total: 14 at :3044.00 on Core :
Retired IBM Brasil
Electronic hobbyist since 1976.

Who is online

Users browsing this forum: No registered users and 79 guests