Skip to content

[v4.2] Hardware timer functions not working, interrupts fail - known issue? #4743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
taligentx opened this issue Jan 25, 2021 · 23 comments
Closed

Comments

@taligentx
Copy link
Contributor

Hi folks,

The hardware timer functions seem to have an issue in the IDF 4.2 branch. The following sketch correctly blinks the onboard LED in 1.0.5-rc6 - under 4.2, this sketch compiles but fails to fire the interrupt:

hw_timer_t * timer = NULL;
#define ledPin LED_BUILTIN

void onTimer() {
  if (digitalRead(ledPin)) digitalWrite(ledPin, 0);
  else digitalWrite(ledPin, 1);
}

void setup() {
  pinMode(ledPin, OUTPUT);
  timer = timerBegin(0, 80, true);
  timerAttachInterrupt(timer, &onTimer, true);
  timerAlarmWrite(timer, 1000000, true);
  timerAlarmEnable(timer);
}

void loop() {}

The native IDF functions do work in the 4.2 branch, as tested in this example:

#include "esp_timer.h"
#define ledPin LED_BUILTIN

void onTimer() {
  if (digitalRead(ledPin)) digitalWrite(ledPin, 0);
  else digitalWrite(ledPin, 1);
}

esp_timer_handle_t timer;
const esp_timer_create_args_t timerParameters = { .callback = reinterpret_cast<esp_timer_cb_t>(&onTimer) };

void setup() {
  pinMode(ledPin, OUTPUT);
  esp_timer_create(&timerParameters, &timer);
  esp_timer_start_periodic(timer, 1000000);
}

void loop() {}

Is this currently the expected behavior? Per esp32-hal-timer.c:

void timerAttachInterrupt(hw_timer_t *timer, void (*fn)(void), bool edge){
	// EDGE DOES NOT WORK CURRENTLY
	edge = false;

Thanks!

Environment: NodeMCU ESP-32S generic dev module, ESP32-S2 official dev board, macOS 11.1, Arduino IDE 1.8.13, core 1.0.5-rc6 and idf-release/v4.2.

@ClemensAtElektor
Copy link

ClemensAtElektor commented Mar 16, 2021

To me, in the function setup() the second parameter in the call to timerAttachInterrupt should be onTimer without the '&', like so:

timerAttachInterrupt(timer, onTimer, true);

I think this is a bug in the timer examples RepeatTime and WatchdogTimer, but I may be mistaken.

@RoscoeTheDog
Copy link

RoscoeTheDog commented Mar 19, 2021

I am having this issue as well on my ESP32-S2-Saola-1 board. I have taken the direct example, and even tried taking out the pass by reference (&) from the code. When running the code, the serial monitor prints nothing. I have tried printing the incrimenter from the ISR and it stays 0, indicating the ISR is not running.

Any updates or solutions on this? Many libraries seem to rely on the Arduino framework instead of the ESP-IDF framework. When there are timers involved in these libraries then I need to adjust the code to compensate for this. I am trying to use a stepper driver library which relies on this right now.

@lbernstone
Copy link
Contributor

I am fairly certain the Ticker library still works for a simple timer. If you need more control, the code in the Ticker library can be used as a template.

@taligentx
Copy link
Contributor Author

I am fairly certain the Ticker library still works for a simple timer. If you need more control, the code in the Ticker library can be used as a template.

Ticker uses the ESP-IDF functions as in my second example, and should be fine for milliseconds and seconds-level timers. Unfortunately it currently doesn't handle microsecond timers and wouldn't work for my library (dscKeybusInterface), which requires a 250 microsecond timer. IR libraries also use microsecond level timers.

@me-no-dev
Copy link
Member

I've tested and I am using the timer driver for Marlin. Please provide a full minimum sketch to reproduce :)

@RoscoeTheDog
Copy link

Here is full example sketch, taken from the OP. Neither attaching the interrupt pass-by-reference or pass-by-copy does anything.

I also tried using this board with the ESP32TimerInterrupt library. The author confirmed this library does not work with the S2 variant of the ESP32. It would not be a big deal as long as I had some other way of utilizing Timer/ISR functionality. As it stands currently, this board which has been out for almost 2 years now still does not seem to have timer functionality/support under the Arduino framework whatsoever, and there is no way to combine the frameworks together in a "compatibility" mode, as the compiler complains that the board is unsupported for the esp-idf+ framework. I have tried almost everything now. It seems as though you have to choose one framework or the other at the moment which sucks because most of the available libraries out there right now are mostly supported under the Arduino framework.

#include <Arduino.h>
#include "esp_timer.h"
hw_timer_t *timer = NULL;
#define ledPin 17

void onTimer()
{
	Serial.println("ISR Running");
}

void setup()
{
	pinMode(ledPin, OUTPUT);
	timer = timerBegin(0, 80, true);
	timerAttachInterrupt(timer, onTimer, true);
	timerAlarmWrite(timer, 1000000, true);
	timerAlarmEnable(timer);
}

void loop() {}

PlatformIO config:

[env:esp32-s2-saola-1]
platform = https://github.com/platformio/platform-espressif32.git#feature/arduino-idf-v4.2
// you can try adding platform_packages="idf-framework git URL here"
board = esp32-s2-saola-1
framework = arduino // if you add "esp-idf+" after arduino with the included platform packages, compiler will complain "this board is unsupported" Note that it does work if you only choose one or the other regardless depending on which one you choose.
lib_deps = 
	https://github.com/khoih-prog/ESP32TimerInterrupt.git
monitor_speed = 115200
upload_port = COM7
board_build.f_cpu = 240000000L

@taligentx
Copy link
Contributor Author

Hi @me-no-dev,

I've tested and I am using the timer driver for Marlin. Please provide a full minimum sketch to reproduce :)

I've re-tested the first example on a freshly installed macOS 10.14 system, new Arduino install, new checkout of the 'idf-releases/v4.2' branch with the following steps - only modified to remove --depth 1 from git clone to get all branches:
https://github.com/espressif/arduino-esp32/blob/master/docs/arduino-ide/mac.md

mkdir -p ~/Documents/Arduino/hardware/espressif && \
cd ~/Documents/Arduino/hardware/espressif && \
git clone https://github.com/espressif/arduino-esp32.git esp32 && \
cd esp32 && \
git submodule update --init --recursive --depth 1 && \
cd tools && \
python get.py 

Compiled and uploaded to a NodeMCU esp32 board and again confirmed that the timer interrupt is not firing. Checked out the master branch, re-launched Arduino, re-compiled and uploaded to the board and the timer interrupt is working correctly.

Let me know if there's any additional info I can provide to help replicate this issue - thanks!

-Nikhil

@me-no-dev
Copy link
Member

@taligentx I'll have a look in a few days, but in general the timer is hardware and Arduino has it's own low-level driver which have not changed, so it is really strange how the hardware would not work between versions :)

@blackhack
Copy link
Contributor

Bumping, right now on master branch (cf43d17) the problem still persist, I even tried with https://github.com/khoih-prog/ESP32TimerInterrupt/commits/master but to no avail. I check-out to idf-release/v3.3 and the same code works without problem.

I tried both examples RepeatTimer.ino from ESP32/Timer and TimerInterruptTest.ino from the library.

@breedx2
Copy link

breedx2 commented Jun 20, 2021

Same thing here on a recent install. Timer code from the examples does nothing, cannot get it to fire. Tried:

  • different divider frequencies (80/240)
  • println in the ISR directly vs. just incrementing a value and printing that value from loop()
  • with/without a mutex
  • ARDUINO_ISR_ATTR vs IRAM_ATTR on the isr callback

What a drag/showstopper. Happy to try other things and to troubleshoot/experiment, because my project is dead without a working hardware timer.

@breedx2
Copy link

breedx2 commented Jun 26, 2021

To me, in the function setup() the second parameter in the call to timerAttachInterrupt should be onTimer without the '&', like so:

timerAttachInterrupt(timer, onTimer, true);

I think this is a bug in the timer examples RepeatTime and WatchdogTimer, but I may be mistaken.

I don't think this is true. It did not help in my case anyway. I tried it. Does it work for you @ClemensAtElektor or were you just speculating?

@blackhack
Copy link
Contributor

To me, in the function setup() the second parameter in the call to timerAttachInterrupt should be onTimer without the '&', like so:
timerAttachInterrupt(timer, onTimer, true);
I think this is a bug in the timer examples RepeatTime and WatchdogTimer, but I may be mistaken.

I don't think this is true. It did not help in my case anyway. I tried it. Does it work for you @ClemensAtElektor or were you just speculating?

& operator is optional when addressing a function pointer, so for the compiler is irrelevant which format is used, the bug is in the esp32-arduino core somewhere.

@JohnMacrae
Copy link
Contributor

JohnMacrae commented Jun 27, 2021

I took the Timer example code on a known good board and compiled and uploaded it using the Boards manager release version (not Alpha). It runs fine. I then took the same code example and compiled it using the latest git Alpha (on both Win 10 and Ubuntu) and uploaded it. The timer does not work.

@RoscoeTheDog
Copy link

@me-no-dev

Why was this closed? It is clear that people are still encountering this bug/issue and there is no working solution.

@me-no-dev
Copy link
Member

it was closed automatically by the commit linked above, because it contains the fix

@RoscoeTheDog
Copy link

Apologies.. I did not see the hyperlink to the solution. Great news. Many thanks

@breedx2
Copy link

breedx2 commented Jul 1, 2021

I confirmed tonight that this fix also works for me. Thanks for the work! Very appreciated. 🙏

pedrominatel added a commit that referenced this issue Jul 5, 2021
Docs: Fixed typo

Docs: Boards tree fixed

Removed link from the getting started OS icons

Pin layout images updated and added the requirements file for sphinx extensions

Copy button extension enabled on conf.py

Added information on the Getting Started Guide

Removed pygments_style from conf.py

Added SoC and Module image

Added Windows install guide and boards manager images

Added OTA Web Update to the docs and removed from md files

Added DFU and ESPNOW files into the docs

Fixed Typo

Changes on the boards, getting started sections and menu order changed for Lib Builder

Added references on the boards to fix broken link

Added the basic tutorial as template

Fix hardware timers

Fixes: #5337
Fixes: #4743

Thanks to @maxgerhardt

Added code style reference and WIP on the libs template

Added peripherals tutorial

Added peripherals tutorial fix

Update on the WiFi lib docs and removed old md files

Update on the WiFi lib docs

Fixed the examples references for WiFi Lib

Fixed code-block on begin function - WiFi Docs

Updates on WiFi lib docs

Fixed the AP section on docs

Review in progress: API, boards and tutorials

Fixed typo on Strapping pins

Review done
@spotrevoc
Copy link

I have updated to the latest versions of Arduino (1.8.15) and arduino-esp32 (2.0.0-alpha1), verified that esp32-hal-timer.c includes the fixes in the commit above, recompiled, and I still cannot get the timers to work on the esp32-s2.

@blackhack
Copy link
Contributor

blackhack commented Jul 6, 2021

I have updated to the latest versions of Arduino (1.8.15) and arduino-esp32 (2.0.0-alpha1), verified that esp32-hal-timer.c includes the fixes in the commit above, recompiled, and I still cannot get the timers to work on the esp32-s2.

That release is from April 17, for this fix to be merge you need to directly use git latest version.
https://github.com/espressif/arduino-esp32/archive/refs/heads/master.zip

@spotrevoc
Copy link

spotrevoc commented Jul 6, 2021

I have the most recent version. I nuked my entire Arduino install and got fresh hardware cores and libs today. Installed straight from the .git and went into esp32-hal-timer.c and physically verified that it contains the fix in the commit above (it does). See the date modified on the file in the screen snip below, it's literally from an hour ago.
image

ESP_IDF native code works just fine.

@blackhack
Copy link
Contributor

I have the most recent version. I nuked my entire Arduino install and got fresh hardware cores and libs today. Installed straight from the .git and went into esp32-hal-timer.c and physically verified that it contains the fix in the commit above (it does). See the date modified on the file in the screen snip below, it's literally from an hour ago.
image

ESP_IDF native code works just fine.

Sadly I don't have an esp32-s2 available to do some testing, the fix works for sure on esp32. Try looking for any residual Arduino folder on %userprofile%\AppData\Local\Temp and delete it (They shouldn't be there anyway after closing the IDE).

Or %userprofile%\AppData\Local\Temp\VMBuilds if using VisualMicro

@alireza369
Copy link

I confirmed tonight that this fix also works for me. Thanks for the work! Very appreciated. 🙏

hi how did you solved this problem ?
plz share whit us 😘

@breedx2
Copy link

breedx2 commented Oct 21, 2021

I confirmed tonight that this fix also works for me. Thanks for the work! Very appreciated. pray

hi how did you solved this problem ? plz share whit us kissing_heart

Pretty sure I just used a version of the code after 8f46bad was merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants