-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Timer functions do not work at all in PlatformIO #5337
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
Comments
Hi @maxgerhardt ! Thanks for the detailed submission :) |
You are right, with Thanks for the fix. |
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
I was very frustrated with ESP32 RepeatTimer example (above example) but this solved the problem hanging on timer using PlatformIo. void stopTimer() { |
For fixing Hardware Timers: Timer functions do not work at all in PlatformIO espressif#5337 espressif#5337 espressif@8f46bad?diff=split
Hardware:
Board: ESP32 Dev Module
Core Installation version: 1.0.6 as well as 2.0.0-alpha1 and current master 21947eb
IDE name: Platform.io
Flash Frequency: 40Mhz
PSRAM enabled: no
Upload Speed: 921600
Computer OS: Windows 10
Description:
I am attempting to replicate the tutorial Hardware Interrrupts here, specifically the code here. The sketch uses functions from
esp32-hal-timer.c
to setup timer 0 with an alarm that will trigger the execution of an ISR in which a pin is toggled at a rate of 1Hz.When uploading this sketch in PlatformIO, using all default settings (meaning also core version 1.0.6), this does not work. There is no blinking going on the defined LED pin at all.
The following sketch was used, which additionally prints some debug information on the
Serial
. A LED is connected to IO2.platformio.ini
The serial output clearly shows the problem: The alarm in the timer is not enabled.
Time keeps advancing as expected for the timer though.
I was able to fix this issue by going into the
C:\Users\<user>\.platformio\packages\framework-arduinoespressif32\cores\esp32\esp32-hal-timer-c
file and addingvolatile
declaration to the timer registers struct.arduino-esp32/cores/esp32/esp32-hal-timer.c
Lines 50 to 54 in 21947eb
to
Adding this single keyword immediately got the LED blinking.
So okay I think, the Arduino-ESP32 has a bug regarding compiler optimizations where a write to the alarm-enable register is not executed in order (or, at all) due to the developers forgetting that the registers need to be marked
volatile
. That fixes it for the 1.0.6 core.Note that the same fixing effect can be achieved by not declaring the struct
volatile
but instead placing a call to function betweentimerAlarmWrite()
andtimerAlarmEnable()
insetup()
, e.g. a simpledelayMicroseconds(0);
. This strongly hints at me to the compiler optimizing (away) or reordering something in the sequence of writes to the timer registers.However, I then also cross-checked with the latest master version of the core and compiler.
Pulling the latest compiler and Arduino core (without any source code changes to the core or the sketch) and re-uploading showed again that the blinky was not working (kinda expected).
However, the behavior on the Serial monitor shows a different story.
You can see that right off the bat, the
alarm seconds
value returns is suddenly.. 6.42? Which makes no sense since it should be 1.0. The timer seconds keep counting up until hitting 6.42, and then the alarm goes from enabled to disabled.Adding the
volatile
keyword to the struct definition as shown above does change the output.Now the timer seconds to read correctly as
1.00
but after 1 elapsed second, the alarm is just disabled and never continues...However from there, no matter where I add
volatile
in the register struct definition, I can not get it to function correctly. There might be additional issues somewhere else.In any case, I see this is a bug. For 1.0.6 it's a bug reproducable with PlatformIO and also fixable as I've shown, As for 2.0.0-alpha1 and the current master version, I see the same bug source (still no
volatile
on timer regs) but that is not the only needed thing to keep it going.Are you able to reproduce the issue in PlatformIO?
Magically per the linked video, Arduino IDE seems to work with some versions though? Can anyone test this with the latest master?
The text was updated successfully, but these errors were encountered: