khoih-prog / MBED_RPI_PICO_TimerInterrupt

This library enables you to use Interrupt from Hardware Timers on RP2040-based boards such as Nano_RP2040_Connect, RASPBERRY_PI_PICO. These MBED_RPI_PICO_TimerInterrupt Hardware Timers, using Interrupt, still work even if other functions are blocking. Moreover, they are much more precise (certainly depending on clock frequency accuracy) than other software timers using millis() or micros(). That's mandatory if you need to measure some data requiring better accuracy. It now supports 16 ISR-based Timers, while consuming only 1 Hardware Timer. Timers' interval is very long (ulong millisecs). The most important feature is they're ISR-based Timers. Therefore, their executions are not blocked by bad-behaving functions or tasks. This important feature is absolutely necessary for mission-critical tasks.
MIT License
11 stars 1 forks source link

[Nano_RP2040_Connect] Using built-in RGB LED in an interrupt? #1

Closed reind33r closed 3 years ago

reind33r commented 3 years ago

Describe the bug

Good morning! I tried to make the built-in RGB LEDs of the Nano RP2040 Connect blink with interrupts (both hardware and ISR_based). It results in the board crashing.

Rather than with your (awesome!) library, I suspect it might be a problem with the digitalWrite/analogWrite functions defined by the WiFiNINA library to write to the RGB LED... they seem to communicate via SPI to the WiFi module (https://github.com/arduino-libraries/WiFiNINA/blob/master/src/utility/wifi_drv.cpp). Would that be too "time-consuming" for an ISR? I only have a basic knowledge of interrupts...

(And I have a blocking task in the main program, so a pure software timer would not work, that's why I thought of using interrupts.)

Steps to Reproduce

From the "50ms_HWTimer.ino", I added #include <WiFiNINA.h> to use the RGB LED.

Then I replaced LED_BUILTIN with LEDR. In TimerHandler0 function, I also replaced the digitalWrite call with:

if(toggle0) {
    digitalWrite(LEDR, LOW);
  } else {
    digitalWrite(LEDR, HIGH);
  }

Expected behavior

The RGB LED blinks.

Actual behavior

The board crashes. (LED_BUILTIN flashes 4x short and 4x long)

khoih-prog commented 3 years ago

Hi @reind33r

Thanks for your interest in the library.

That's a known issue for ArduinoCore-mbed core, happening for Portenta_H7 and I guess to boards sharing the same core, check

  1. analogWrite inside an interrupt
  2. analogWrite crashes #351

Try using the following libraries instead

  1. RP2040_PWM
  2. nRF52_MBED_Slow_PWM

As the issue is not a bug of this library, I'm closing it now. Follow up using those links

Good Luck,

PS:

The RGB LEDs of Nano_RP2040_Connect is just a kludge, never use it in ISR.