Trying to port an interrupt script for Arduino Uno to ESP32

Hello,

I am not sure if correct place but thought someone might know where I am going wrong, so I am completely new to both Arduino's and ESP32, mostly dabbled with Pi's, however, trying to do a project at mo to get ultrasonic sensors to respond quicker, so found a really useful site here covering how to use interrupts to get things to work a bit quicker,

I have tried to modify the sketch to better understand how it works, it does compile but when it goes onto the ESP32 it just keeps rebooting, was hoping someone could point me in the direction of the errors in my way!

Modified Sketch:

#define trigPin 23                                  // Pin 23 trigger output
#define echoPin 22                                    // Pin 22 Echo input
#define echo_int 0                                    // Interrupt id for echo pulse

#define TIMER_US 80                                   // 80 uS timer duration 
#define TICK_COUNTS 4000                              // 200 mS worth of timer ticks

volatile long echo_start = 0;                         // Records start of echo pulse 
volatile long echo_end = 0;                           // Records end of echo pulse
volatile long echo_duration = 0;                      // Duration - difference between end and start
volatile int trigger_time_count = 0;                  // Count down counter to trigger pulse time
volatile long range_flasher_counter = 0;              // Count down counter for flashing distance LED

hw_timer_t * Timer1 = NULL;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
 
// --------------------------
// timerIsr() 50uS second interrupt ISR()
// Called every time the hardware timer 1 times out.
// --------------------------

void IRAM_ATTR timerIsr() {
  portENTER_CRITICAL_ISR(&timerMux);
  trigger_pulse();    
  portEXIT_CRITICAL_ISR(&timerMux);
 
}

// ----------------------------------
// setup() routine called first.
// A one time routine executed at power up or reset time.
// Used to initialise hardware.
// ----------------------------------
void setup() 
{
  pinMode(trigPin, OUTPUT);                           // Trigger pin set to output
  pinMode(echoPin, INPUT);                            // Echo pin set to input
  
  Timer1 = timerBegin(0, TIMER_US, true);                        // Initialise timer 1
  timerAttachInterrupt(Timer1, &timerIsr, true);                 // Attach interrupt to the timer service routine 
  timerAttachInterrupt(echo_int, echo_interrupt, CHANGE);  // Attach interrupt to the sensor echo input
  Serial.begin (9600);                                // Initialise the serial monitor output
}

// ----------------------------------
// loop() Runs continuously in a loop.
// This is the background routine where most of the processing usualy takes place.
// Non time critical tasks should be run from here.
// ----------------------------------
void loop()
{
    Serial.println(echo_duration / 58);               // Print the distance in centimeters
    delay(100);                                       // every 100 mS
}


// --------------------------
// trigger_pulse() called every 10 uS to schedule trigger pulses.
// Generates a pulse one timer tick long.
// Minimum trigger pulse width for the HC-SR04 is 10 us. This system
// delivers a 10 uS pulse.
// --------------------------
void trigger_pulse()
{
      static volatile int state = 0;                 // State machine variable

      if (!(--trigger_time_count))                   // Count to 200mS
      {                                              // Time out - Initiate trigger pulse
         trigger_time_count = TICK_COUNTS;           // Reload
         state = 1;                                  // Changing to state 1 initiates a pulse
      }
    
      switch(state)                                  // State machine handles delivery of trigger pulse
      {
        case 0:                                      // Normal state does nothing
            break;
        
        case 1:                                      // Initiate pulse
           digitalWrite(trigPin, HIGH);              // Set the trigger output high
           state = 2;                                // and set state to 2
           break;
        
        case 2:                                      // Complete the pulse
        default:      
           digitalWrite(trigPin, LOW);               // Set the trigger output low
           state = 0;                                // and return state to normal 0
           break;
     }
}

// --------------------------
// echo_interrupt() External interrupt from HC-SR04 echo signal. 
// Called every time the echo signal changes state.
//
// Note: this routine does not handle the case where the timer
//       counter overflows which will result in the occassional error.
// --------------------------
void echo_interrupt()
{
  switch (digitalRead(echoPin))                     // Test to see if the signal is high or low
  {
    case HIGH:                                      // High so must be the start of the echo pulse
      echo_end = 0;                                 // Clear the end time
      echo_start = micros();                        // Save the start time
      break;
      
    case LOW:                                       // Low so must be the end of hte echo pulse
      echo_end = micros();                          // Save the end time
      echo_duration = echo_end - echo_start;        // Calculate the pulse duration
      break;
  }
}

// --------------------------
// distance_flasher() Called from the timer 1 timerIsr service routine.
// Flashes the onboard LED at a rate inversely proportional
// to distance. The closer it gets the higher the frequency.
// --------------------------
void distance_flasher()
{
      if (--range_flasher_counter <= 0)                // Decrement and test the flash timer
      {                                                // Flash timer time out
         if (echo_duration < 25000)                    // If the echo duration is within limits
         {
           range_flasher_counter = echo_duration * 2;  // Reload the timer with the current echo duration
         }
         else
         {
           range_flasher_counter = 25000;              // If out of range use a default
         }

      }
}

The error I am seeing on the ESP32 is:

ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_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:0x3fff0030,len:1324
ho 0 tail 12 room 4
load:0x40078000,len:13480
ho 0 tail 12 room 4
load:0x40080400,len:3604
entry 0x400805f0
Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.

Core  1 register dump:
PC      : 0x400d15ef  PS      : 0x00060d30  A0      : 0x800d1064  A1      : 0x3ffb27d0  
A2      : 0x00000000  A3      : 0x400d0fdc  A4      : 0x00000001  A5      : 0x3ffbdb70  
A6      : 0x00000000  A7      : 0x003fffff  A8      : 0x800d162c  A9      : 0x3ffb27a0  
A10     : 0x00000000  A11     : 0x00000000  A12     : 0x40081100  A13     : 0x3ffb8a88  
A14     : 0x00000000  A15     : 0x3ffc1228  SAR     : 0x00000020  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00000001  LBEG    : 0x40085c18  LEND    : 0x40085c23  LCOUNT  : 0xffffffff  


Backtrace:0x400d15ec:0x3ffb27d00x400d1061:0x3ffb27f0 0x400d1d26:0x3ffb2820 




ELF file SHA256: 0000000000000000

Any help would be massively appreciated.

Subsequent behavior of the panic handler can be set using CONFIG_ESP_SYSTEM_PANIC configuration choice. The available options are:

  • Print registers and reboot (CONFIG_ESP_SYSTEM_PANIC_PRINT_REBOOT) — default option.This will print register values at the point of the exception, print the backtrace, and restart the chip.
  • Print registers and halt (CONFIG_ESP_SYSTEM_PANIC_PRINT_HALT)Similar to the above option, but halt instead of rebooting. External reset is required to restart the program.
  • Silent reboot (CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT)Don’t print registers or backtrace, restart the chip immediately.
  • Invoke GDB Stub (CONFIG_ESP_SYSTEM_PANIC_GDBSTUB)Start GDB server which can communicate with GDB over console UART port. This option will only provide read-only debugging or post-mortem debugging. See GDB Stub for more details.
  • Invoke dynamic GDB Stub (ESP_SYSTEM_GDBSTUB_RUNTIME)Start GDB server which can communicate with GDB over console UART port. This option allows the user to debug a program at run time and set break points, alter the execution, etc. See GDB Stub for more details.

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