Git Product home page Git Product logo

khoih-prog / mbed_rpi_pico_timerinterrupt Goto Github PK

View Code? Open in Web Editor NEW
10.0 3.0 1.0 143 KB

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.

License: MIT License

C 57.51% C++ 42.00% Shell 0.49%
interrupt timers isr non-blocking mission-critical hardware-timers timerinterrupt timerinterrupt-library isr-based-timer isr-based

mbed_rpi_pico_timerinterrupt's Introduction

MBED_RPI_PICO_TimerInterrupt Library

arduino-library-badge GitHub release GitHub contributions welcome GitHub issues

Donate to my libraries using BuyMeACoffee



Table of Contents



Important Change from v1.1.0

Please have a look at HOWTO Fix Multiple Definitions Linker Error

Features

This library enables you to use Interrupt from Hardware Timers on MBED RP2040-based boards such as Nano_RP2040_Connect, RASPBERRY_PI_PICO, using Arduino-mbed RP2040 core

As Hardware Timers are rare, and very precious assets of any board, this library now enables you to use up to 16 ISR-based Timers, while consuming only 1 Hardware Timer. Timers' interval is very long (ulong millisecs).

Now with these new 16 ISR-based timers, the maximum interval is practically unlimited (limited only by unsigned long milliseconds) while the accuracy is nearly perfect compared to software timers.

The most important feature is they're ISR-based timers. Therefore, their executions are not blocked by bad-behaving functions / tasks. This important feature is absolutely necessary for mission-critical tasks.

The ISR_16_Timers_Array_Complex example will demonstrate the nearly perfect accuracy compared to software timers by printing the actual elapsed millisecs of each type of timers.

Being ISR-based timers, their executions are not blocked by bad-behaving functions / tasks, such as connecting to WiFi, Internet and Blynk services. You can also have many (up to 16) timers to use.

This non-being-blocked important feature is absolutely necessary for mission-critical tasks.

You'll see blynkTimer Software is blocked while system is connecting to WiFi / Internet / Blynk, as well as by blocking task in loop(), using delay() function as an example. The elapsed time then is very unaccurate

Why using ISR-based Hardware Timer Interrupt is better

Imagine you have a system with a mission-critical function, measuring water level and control the sump pump or doing something much more important. You normally use a software timer to poll, or even place the function in loop(). But what if another function is blocking the loop() or setup().

So your function might not be executed, and the result would be disastrous.

You'd prefer to have your function called, no matter what happening with other functions (busy loop, bug, etc.).

The correct choice is to use a Hardware Timer with Interrupt to call your function.

These 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 necessary if you need to measure some data requiring better accuracy.

Functions using normal software timers, relying on loop() and calling millis(), won't work if the loop() or setup() is blocked by certain operation. For example, certain function is blocking while it's connecting to WiFi or some services.

The catch is your function is now part of an ISR (Interrupt Service Routine), and must be lean / mean, and follow certain rules. More to read on:

HOWTO Attach Interrupt


Currently supported Boards

  1. RP2040-based boards such as Nano_RP2040_Connect, RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040, etc. using Arduino-mbed RP2040 core

Important Notes about ISR

  1. Inside the attached function, delay() won’t work and the value returned by millis() will not increment. Serial data received while in the function may be lost. You should declare as volatile any variables that you modify within the attached function.

  2. Typically global variables are used to pass data between an ISR and the main program. To make sure variables shared between an ISR and the main program are updated correctly, declare them as volatile.

  3. For this MBED-based core, never use Serial.print(ln) inside ISR or the system will hang.



Prerequisites

  1. Arduino IDE 1.8.19+ for Arduino. GitHub release
  2. Arduino mbed_rp2040 core 3.4.1+ for Arduino (Use Arduino Board Manager) RP2040-based boards, such as Arduino Nano RP2040 Connect, RASPBERRY_PI_PICO, etc.. GitHub release
  3. To use with certain example, depending on which Ethernet card you're using:
  4. To use with certain example


Installation

Use Arduino Library Manager

The best and easiest way is to use Arduino Library Manager. Search for MBED_RPI_PICO_TimerInterrupt, then select / install the latest version. You can also use this link arduino-library-badge for more detailed instructions.

Manual Install

Another way to install is to:

  1. Navigate to MBED_RPI_PICO_TimerInterrupt page.
  2. Download the latest release MBED_RPI_PICO_TimerInterrupt-main.zip.
  3. Extract the zip file to MBED_RPI_PICO_TimerInterrupt-main directory
  4. Copy whole MBED_RPI_PICO_TimerInterrupt-main folder to Arduino libraries' directory such as ~/Arduino/libraries/.

VS Code & PlatformIO

  1. Install VS Code
  2. Install PlatformIO
  3. Install MBED_RPI_PICO_TimerInterrupt library by using Library Manager. Search for MBED_RPI_PICO_TimerInterrupt in Platform.io Author's Libraries
  4. Use included platformio.ini file from examples to ensure that all dependent libraries will installed automatically. Please visit documentation for the other options and examples at Project Configuration File


HOWTO Fix Multiple Definitions Linker Error

The current library implementation, using xyz-Impl.h instead of standard xyz.cpp, possibly creates certain Multiple Definitions Linker error in certain use cases.

You can include these .hpp files

// Can be included as many times as necessary, without `Multiple Definitions` Linker Error
#include "MBED_RPi_Pico_TimerInterrupt.hpp"   //https://github.com/khoih-prog/MBED_RPI_PICO_TimerInterrupt

// Can be included as many times as necessary, without `Multiple Definitions` Linker Error
#include "MBED_RPi_Pico_ISR_Timer.hpp"        //https://github.com/khoih-prog/MBED_RPI_PICO_TimerInterrupt

in many files. But be sure to use the following .h files in just 1 .h, .cpp or .ino file, which must not be included in any other file, to avoid Multiple Definitions Linker Error

// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include "MBED_RPi_Pico_TimerInterrupt.h"     //https://github.com/khoih-prog/MBED_RPI_PICO_TimerInterrupt

// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include "MBED_RPi_Pico_ISR_Timer.h"          //https://github.com/khoih-prog/MBED_RPI_PICO_TimerInterrupt

Check the new multiFileProject example for a HOWTO demo.



More useful Information

The RPI_PICO system timer peripheral provides a global microsecond timebase for the system, and generates interrupts based on this timebase. It supports the following features: • A single 64-bit counter, incrementing once per microsecond • This counter can be read from a pair of latching registers, for race-free reads over a 32-bit bus. • Four alarms: match on the lower 32 bits of counter, IRQ on match: TIMER_IRQ_0-TIMER_IRQ_3


Now with these new 16 ISR-based timers (while consuming only 1 hardware timer), the maximum interval is practically unlimited (limited only by unsigned long milliseconds). The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers Therefore, their executions are not blocked by bad-behaving functions / tasks. This important feature is absolutely necessary for mission-critical tasks.

The ISR_Timer_Complex example will demonstrate the nearly perfect accuracy compared to software timers by printing the actual elapsed millisecs of each type of timers. Being ISR-based timers, their executions are not blocked by bad-behaving functions / tasks, such as connecting to WiFi, Internet and Blynk services. You can also have many (up to 16) timers to use. This non-being-blocked important feature is absolutely necessary for mission-critical tasks. You'll see blynkTimer Software is blocked while system is connecting to WiFi / Internet / Blynk, as well as by blocking task in loop(), using delay() function as an example. The elapsed time then is very unaccurate



Usage

Before using any Timer, you have to make sure the Timer has not been used by any other purpose. TIMER_IRQ_0, TIMER_IRQ_1, TIMER_IRQ_2 and TIMER_IRQ_3 are supported for RP2040-based boards.

1. Using only Hardware Timer directly

1.1 Init Hardware Timer

// Select the timer you're using, from ITimer0(0)-ITimer3(3)
// Init MBED_RPI_PICO_Timer
MBED_RPI_PICO_Timer ITimer1(1);

1.2 Set Hardware Timer Interval and attach Timer Interrupt Handler function

Use one of these functions with interval in unsigned long microseconds

// interval (in us), callback is ISR
bool setInterval(unsigned long interval, pico_timer_callback callback);

// interval (in us), callback is ISR
bool attachInterruptInterval(unsigned long interval, pico_timer_callback callback)

as follows

// Never use Serial.print inside this mbed ISR. Will hang the system
void TimerHandler(uint alarm_num)
{
  // Doing something here inside ISR
}

#define TIMER_INTERVAL_MS        5000L

// Init MBED_RPI_PICO_Timer
MBED_RPI_PICO_Timer ITimer(0);

void setup()
{
  ....
  
  // Interval in unsigned long microseconds
  if (ITimer.attachInterruptInterval(TIMER_INTERVAL_MS * 1000, TimerHandler))
    Serial.println("Starting ITimer OK, millis() = " + String(millis()));
  else
    Serial.println("Can't set ITimer. Select another freq. or timer");
}  

1.3 Set Hardware Timer Frequency and attach Timer Interrupt Handler function

Use one of these functions with frequency in float Hz

// frequency (in Hz), callback is ISR
bool setFrequency(float frequency, pico_timer_callback callback)

// frequency (in Hz), callback is ISR
bool attachInterrupt(float frequency, timer_callback callback);

as follows

// Never use Serial.print inside this mbed ISR. Will hang the system
void TimerHandler(uint alarm_num)
{
  ///////////////////////////////////////////////////////////
  // Always call this for MBED RP2040 before processing ISR
  TIMER_ISR_START(alarm_num);
  ///////////////////////////////////////////////////////////
  
  // Doing something here inside ISR

  ////////////////////////////////////////////////////////////
  // Always call this for MBED RP2040 after processing ISR
  TIMER_ISR_END(alarm_num);
  ////////////////////////////////////////////////////////////
}

#define TIMER_FREQ_HZ        5555.555

// Init MBED_RPI_PICO_Timer
MBED_RPI_PICO_Timer ITimer(0);

void setup()
{
  ....
  
  // Frequency in float Hz
  if (ITimer.attachInterrupt(TIMER_FREQ_HZ, TimerHandler))
    Serial.println("Starting ITimer OK, millis() = " + String(millis()));
  else
    Serial.println("Can't set ITimer. Select another freq. or timer");
}  

2. Using 16 ISR_based Timers from 1 Hardware Timer

2.1 Important Note

The 16 ISR_based Timers, designed for long timer intervals, only support using unsigned long millisec intervals. If you have to use much higher frequency or sub-millisecond interval, you have to use the Hardware Timers directly as in 1.3 Set Hardware Timer Frequency and attach Timer Interrupt Handler function

2.2 Init Hardware Timer and ISR-based Timer

// Init MBED_RPI_PICO_Timer
MBED_RPI_PICO_Timer ITimer1(1);

// Init ISR_Timer
// Each ISR_Timer can service 16 different ISR-based timers
MBED_RPI_PICO_ISR_Timer ISR_timer;

2.3 Set Hardware Timer Interval and attach Timer Interrupt Handler functions

// Never use Serial.print inside this mbed ISR. Will hang the system
void TimerHandler(uint alarm_num)
{
  ///////////////////////////////////////////////////////////
  // Always call this for MBED RP2040 before processing ISR
  TIMER_ISR_START(alarm_num);
  ///////////////////////////////////////////////////////////
  
  ISR_timer.run();

  ////////////////////////////////////////////////////////////
  // Always call this for MBED RP2040 after processing ISR
  TIMER_ISR_END(alarm_num);
  ////////////////////////////////////////////////////////////
}

#define HW_TIMER_INTERVAL_MS          50L

#define TIMER_INTERVAL_2S             2000L
#define TIMER_INTERVAL_5S             5000L
#define TIMER_INTERVAL_11S            11000L
#define TIMER_INTERVAL_101S           101000L

// In AVR, avoid doing something fancy in ISR, for example complex Serial.print with String() argument
// The pure simple Serial.prints here are just for demonstration and testing. Must be eliminate in working environment
// Or you can get this run-time error / crash
void doingSomething2s()
{
  // Doing something here inside ISR every 2 seconds
}
  
void doingSomething5s()
{
  // Doing something here inside ISR every 5 seconds
}

void doingSomething11s()
{
  // Doing something here inside ISR  every 11 seconds
}

void doingSomething101s()
{
  // Doing something here inside ISR every 101 seconds
}

void setup()
{
  ....
  
  if (ITimer1.attachInterruptInterval(HW_TIMER_INTERVAL_MS * 1000, TimerHandler))
  {
    Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(millis());
  }
  else
    Serial.println(F("Can't set ITimer1. Select another freq. or timer"));

  // Just to demonstrate, don't use too many ISR Timers if not absolutely necessary
  // You can use up to 16 timer for each ISR_Timer
  ISR_timer.setInterval(TIMER_INTERVAL_2S, doingSomething2s);
  ISR_timer.setInterval(TIMER_INTERVAL_5S, doingSomething5s);
  ISR_timer.setInterval(TIMER_INTERVAL_11S, doingSomething11s);
  ISR_timer.setInterval(TIMER_INTERVAL_101S, doingSomething101s);
}  


Examples

  1. Argument_Complex
  2. Argument_None
  3. Argument_Simple
  4. Change_Interval
  5. ISR_16_Timers_Array_Complex
  6. ISR_Timers_Array_Simple
  7. SwitchDebounce
  8. TimerInterruptTest
  9. 50ms_HWTimer New
  10. multiFileProject New


#if ( defined(ARDUINO_NANO_RP2040_CONNECT) || defined(ARDUINO_RASPBERRY_PI_PICO) || defined(ARDUINO_ADAFRUIT_FEATHER_RP2040) || \
defined(ARDUINO_GENERIC_RP2040) ) && defined(ARDUINO_ARCH_MBED)
#define USING_MBED_RPI_PICO_TIMER_INTERRUPT true
#else
#error This code is intended to run on the MBED RASPBERRY_PI_PICO platform! Please check your Tools->Board setting.
#endif
// These define's must be placed at the beginning before #include "TimerInterrupt_Generic.h"
// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
#define _TIMERINTERRUPT_LOGLEVEL_ 4
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include "MBED_RPi_Pico_TimerInterrupt.h"
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include "MBED_RPi_Pico_ISR_Timer.h"
#include <SimpleTimer.h> // https://github.com/schinken/SimpleTimer
// Init MBED_RPI_PICO_Timer
MBED_RPI_PICO_Timer ITimer1(1);
MBED_RPI_PICO_ISRTimer ISR_timer;
#ifndef LED_BUILTIN
#define LED_BUILTIN 25
#endif
#define LED_TOGGLE_INTERVAL_MS 1000L
// You have to use longer time here if having problem because Arduino AVR clock is low, 16MHz => lower accuracy.
// Tested OK with 1ms when not much load => higher accuracy.
#define TIMER_INTERVAL_MS 1L
volatile uint32_t startMillis = 0;
volatile uint32_t deltaMillis2s = 0;
volatile uint32_t deltaMillis5s = 0;
volatile uint32_t previousMillis2s = 0;
volatile uint32_t previousMillis5s = 0;
// Never use Serial.print inside this mbed ISR. Will hang the system
void TimerHandler(uint alarm_num)
{
static bool toggle = false;
static int timeRun = 0;
///////////////////////////////////////////////////////////
// Always call this for MBED RP2040 before processing ISR
TIMER_ISR_START(alarm_num);
///////////////////////////////////////////////////////////
ISR_timer.run();
// Toggle LED every LED_TOGGLE_INTERVAL_MS = 2000ms = 2s
if (++timeRun == ((LED_TOGGLE_INTERVAL_MS) / TIMER_INTERVAL_MS) )
{
timeRun = 0;
//timer interrupt toggles pin LED_BUILTIN
digitalWrite(LED_BUILTIN, toggle);
toggle = !toggle;
}
////////////////////////////////////////////////////////////
// Always call this for MBED RP2040 after processing ISR
TIMER_ISR_END(alarm_num);
////////////////////////////////////////////////////////////
}
void doingSomething2s()
{
unsigned long currentMillis = millis();
deltaMillis2s = currentMillis - previousMillis2s;
previousMillis2s = currentMillis;
}
void doingSomething5s()
{
unsigned long currentMillis = millis();
deltaMillis5s = currentMillis - previousMillis5s;
previousMillis5s = currentMillis;
}
/////////////////////////////////////////////////
#define SIMPLE_TIMER_MS 2000L
// Init SimpleTimer
SimpleTimer simpleTimer;
// Here is software Timer, you can do somewhat fancy stuffs without many issues.
// But always avoid
// 1. Long delay() it just doing nothing and pain-without-gain wasting CPU power.Plan and design your code / strategy ahead
// 2. Very long "do", "while", "for" loops without predetermined exit time.
void simpleTimerDoingSomething2s()
{
static unsigned long previousMillis = startMillis;
unsigned long currMillis = millis();
Serial.print(F("SimpleTimer : programmed ")); Serial.print(SIMPLE_TIMER_MS);
Serial.print(F("ms, current time ms : ")); Serial.print(currMillis);
Serial.print(F(", Delta ms : ")); Serial.println(currMillis - previousMillis);
Serial.print(F("Timer2s actual : ")); Serial.println(deltaMillis2s);
Serial.print(F("Timer5s actual : ")); Serial.println(deltaMillis5s);
previousMillis = currMillis;
}
////////////////////////////////////////////////
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
while (!Serial);
Serial.print(F("\nStarting ISR_Timers_Array_Simple on ")); Serial.println(BOARD_NAME);
Serial.println(MBED_RPI_PICO_TIMER_INTERRUPT_VERSION);
if (ITimer1.attachInterruptInterval(TIMER_INTERVAL_MS * 1000, TimerHandler))
{
Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(millis());
}
else
Serial.println(F("Can't set ITimer1. Select another freq. or timer"));
previousMillis5s = previousMillis2s = millis();
ISR_timer.setInterval(2000L, doingSomething2s);
ISR_timer.setInterval(5000L, doingSomething5s);
// You need this timer for non-critical tasks. Avoid abusing ISR if not absolutely necessary.
simpleTimer.setInterval(SIMPLE_TIMER_MS, simpleTimerDoingSomething2s);
}
#define BLOCKING_TIME_MS 10000L
void loop()
{
// This unadvised blocking task is used to demonstrate the blocking effects onto the execution and accuracy to Software timer
// You see the time elapse of ISR_Timer still accurate, whereas very unaccurate for Software Timer
// The time elapse for 2000ms software timer now becomes 3000ms (BLOCKING_TIME_MS)
// While that of ISR_Timer is still prefect.
delay(BLOCKING_TIME_MS);
// You need this Software timer for non-critical tasks. Avoid abusing ISR if not absolutely necessary
// You don't need to and never call ISR_Timer.run() here in the loop(). It's already handled by ISR timer.
simpleTimer.run();
}



Debug Terminal Output Samples

1. ISR_Timers_Array_Simple on RaspberryPi Pico

The following is the sample terminal output when running example ISR_Timers_Array_Simple to demonstrate the accuracy of ISR Hardware Timer, especially when system is very busy. The ISR timer is programmed for 2s, is activated exactly after 2.000s !!!

While software timer, programmed for 2s, is activated after more than 10.000s !!!

Starting ISR_Timers_Array_Simple on RaspberryPi Pico
MBED_RPi_Pico_TimerInterrupt v1.2.0
[TISR] _timerNo = 1, Clock (Hz) = 1000000.00, _fre (Hz) = 1000.00
[TISR] _count = 0-1000
[TISR] hardware_alarm_set_target, uS = 1000
Starting ITimer1 OK, millis() = 1690
SimpleTimer : programmed 2000ms, current time ms : 11290, Delta ms : 11290
Timer2s actual : 2000
Timer5s actual : 5000
SimpleTimer : programmed 2000ms, current time ms : 20890, Delta ms : 9600
Timer2s actual : 2000
Timer5s actual : 5000
SimpleTimer : programmed 2000ms, current time ms : 30490, Delta ms : 9600
Timer2s actual : 2000
Timer5s actual : 5000

2. TimerInterruptTest on RaspberryPi Pico

The following is the sample terminal output when running example TimerInterruptTest to demonstrate how to start/stop Hardware Timers on MBED RP2040-based boards.

Starting TimerInterruptTest on RaspberryPi Pico
MBED_RPi_Pico_TimerInterrupt v1.2.0
[TISR] _timerNo = 0, Clock (Hz) = 1000000.00, _fre (Hz) = 1.00
[TISR] _count = 0-1000000
[TISR] hardware_alarm_set_target, uS = 1000000
Starting ITimer0 OK, millis() = 1787
[TISR] _timerNo = 1, Clock (Hz) = 1000000.00, _fre (Hz) = 0.33
[TISR] _count = 0-3000000
[TISR] hardware_alarm_set_target, uS = 3000000
Starting ITimer1 OK, millis() = 1789
Stop ITimer0, millis() = 5001
Start ITimer0, millis() = 10002
Stop ITimer1, millis() = 15001
Stop ITimer0, millis() = 15003
Start ITimer0, millis() = 20004
Stop ITimer0, millis() = 25005
Start ITimer1, millis() = 30002
Start ITimer0, millis() = 30006

3. Change_Interval on RaspberryPi Pico

The following is the sample terminal output when running example Change_Interval to demonstrate how to change Timer Interval on-the-fly on MBED RP2040-based boards.

Starting Change_Interval on RaspberryPi Pico
MBED_RPi_Pico_TimerInterrupt v1.2.0
[TISR] _timerNo = 0, Clock (Hz) = 1000000.00, _fre (Hz) = 0.50
[TISR] _count = 0-2000000
[TISR] hardware_alarm_set_target, uS = 2000000
Starting  ITimer0 OK, millis() = 1282
[TISR] _timerNo = 1, Clock (Hz) = 1000000.00, _fre (Hz) = 0.20
[TISR] _count = 0-5000000
[TISR] hardware_alarm_set_target, uS = 5000000
Starting  ITimer1 OK, millis() = 1284
Time = 10001, Timer0Count = 4, Timer1Count = 1
Time = 20002, Timer0Count = 9, Timer1Count = 3
[TISR] MBED_RPI_PICO_TimerInterrupt: _timerNo = 0 , _fre = 1000000.00
[TISR] _count = 0 - 4000000
[TISR] hardware_alarm_set_target, uS = 4000000
[TISR] MBED_RPI_PICO_TimerInterrupt: _timerNo = 1 , _fre = 1000000.00
[TISR] _count = 0 - 10000000
[TISR] hardware_alarm_set_target, uS = 10000000
Changing Interval, Timer0 = 4000,  Timer1 = 10000
Time = 30003, Timer0Count = 11, Timer1Count = 3
Time = 40004, Timer0Count = 14, Timer1Count = 4
[TISR] MBED_RPI_PICO_TimerInterrupt: _timerNo = 0 , _fre = 1000000.00
[TISR] _count = 0 - 2000000
[TISR] hardware_alarm_set_target, uS = 2000000
[TISR] MBED_RPI_PICO_TimerInterrupt: _timerNo = 1 , _fre = 1000000.00
[TISR] _count = 0 - 5000000
[TISR] hardware_alarm_set_target, uS = 5000000
Changing Interval, Timer0 = 2000,  Timer1 = 5000
Time = 50005, Timer0Count = 18, Timer1Count = 6

4. SwitchDebounce on RaspberryPi Pico

The following is the sample terminal output when running example SwitchDebounce

Starting SwitchDebounce on RaspberryPi Pico
MBED_RPi_Pico_TimerInterrupt v1.2.0
[TISR] _timerNo = 1, Clock (Hz) = 1000000.00, _fre (Hz) = 1000.00
[TISR] _count = 0-1000
[TISR] hardware_alarm_set_target, uS = 1000
Starting ITimer1 OK, millis() = 1185
SW Pressed, from millis() = 4537
SW Released, from millis() = 4888
SW Pressed total time ms = 351
SW Pressed, from millis() = 5266
SW Released, from millis() = 5604
SW Pressed total time ms = 338
SW Pressed, from millis() = 6600
SW Long Pressed, total time ms = 11589 - 6600 = 4989
SW Long Pressed, total time ms = 11640 - 6600 = 5040
SW Long Pressed, total time ms = 11691 - 6600 = 5091
SW Long Pressed, total time ms = 11742 - 6600 = 5142
SW Long Pressed, total time ms = 11793 - 6600 = 5193
SW Long Pressed, total time ms = 11844 - 6600 = 5244
SW Long Pressed, total time ms = 11895 - 6600 = 5295
SW Released, from millis() = 11996
SW Pressed total time ms = 5396
SW Pressed, from millis() = 13007
SW Released, from millis() = 13155
SW Pressed total time ms = 148

5. ISR_16_Timers_Array_Complex on RaspberryPi Pico

The following is the sample terminal output when running example ISR_16_Timers_Array_Complex to demonstrate the accuracy of ISR Hardware Timer, especially when system is very busy. The SimpleTimer, programmed for 2s, is activated only after nearly 10s !!!

Starting ISR_16_Timers_Array_Complex on RaspberryPi Pico
MBED_RPi_Pico_TimerInterrupt v1.2.0
[TISR] _timerNo = 0, Clock (Hz) = 1000000.00, _fre (Hz) = 100.00
[TISR] _count = 0-10000
[TISR] hardware_alarm_set_target, uS = 10000
Starting ITimer OK, millis() = 1621
SimpleTimer : 2, ms : 11622, Dms : 10000
Timer : 0, programmed : 5000, actual : 5009
Timer : 1, programmed : 10000, actual : 0
Timer : 2, programmed : 15000, actual : 0
Timer : 3, programmed : 20000, actual : 0
Timer : 4, programmed : 25000, actual : 0
Timer : 5, programmed : 30000, actual : 0
Timer : 6, programmed : 35000, actual : 0
Timer : 7, programmed : 40000, actual : 0
Timer : 8, programmed : 45000, actual : 0
Timer : 9, programmed : 50000, actual : 0
Timer : 10, programmed : 55000, actual : 0
Timer : 11, programmed : 60000, actual : 0
Timer : 12, programmed : 65000, actual : 0
Timer : 13, programmed : 70000, actual : 0
Timer : 14, programmed : 75000, actual : 0
Timer : 15, programmed : 80000, actual : 0
SimpleTimer : 2, ms : 21628, Dms : 10006
Timer : 0, programmed : 5000, actual : 5000
Timer : 1, programmed : 10000, actual : 10009
Timer : 2, programmed : 15000, actual : 15009
Timer : 3, programmed : 20000, actual : 0
Timer : 4, programmed : 25000, actual : 0
Timer : 5, programmed : 30000, actual : 0
Timer : 6, programmed : 35000, actual : 0
Timer : 7, programmed : 40000, actual : 0
Timer : 8, programmed : 45000, actual : 0
Timer : 9, programmed : 50000, actual : 0
Timer : 10, programmed : 55000, actual : 0
Timer : 11, programmed : 60000, actual : 0
Timer : 12, programmed : 65000, actual : 0
Timer : 13, programmed : 70000, actual : 0
Timer : 14, programmed : 75000, actual : 0
Timer : 15, programmed : 80000, actual : 0
SimpleTimer : 2, ms : 31634, Dms : 10006
Timer : 0, programmed : 5000, actual : 5000
Timer : 1, programmed : 10000, actual : 10000
Timer : 2, programmed : 15000, actual : 15000
Timer : 3, programmed : 20000, actual : 20009
Timer : 4, programmed : 25000, actual : 25009
Timer : 5, programmed : 30000, actual : 30009
Timer : 6, programmed : 35000, actual : 0
Timer : 7, programmed : 40000, actual : 0
Timer : 8, programmed : 45000, actual : 0
Timer : 9, programmed : 50000, actual : 0
Timer : 10, programmed : 55000, actual : 0
Timer : 11, programmed : 60000, actual : 0
Timer : 12, programmed : 65000, actual : 0
Timer : 13, programmed : 70000, actual : 0
Timer : 14, programmed : 75000, actual : 0
Timer : 15, programmed : 80000, actual : 0
SimpleTimer : 2, ms : 41640, Dms : 10006
Timer : 0, programmed : 5000, actual : 5000
Timer : 1, programmed : 10000, actual : 10000
Timer : 2, programmed : 15000, actual : 15000
Timer : 3, programmed : 20000, actual : 20000
Timer : 4, programmed : 25000, actual : 25009
Timer : 5, programmed : 30000, actual : 30009
Timer : 6, programmed : 35000, actual : 35009
Timer : 7, programmed : 40000, actual : 40009
Timer : 8, programmed : 45000, actual : 0
Timer : 9, programmed : 50000, actual : 0
Timer : 10, programmed : 55000, actual : 0
Timer : 11, programmed : 60000, actual : 0
Timer : 12, programmed : 65000, actual : 0
Timer : 13, programmed : 70000, actual : 0
Timer : 14, programmed : 75000, actual : 0
Timer : 15, programmed : 80000, actual : 0
SimpleTimer : 2, ms : 51646, Dms : 10006
Timer : 0, programmed : 5000, actual : 5000
Timer : 1, programmed : 10000, actual : 10000
Timer : 2, programmed : 15000, actual : 15000
Timer : 3, programmed : 20000, actual : 20000
Timer : 4, programmed : 25000, actual : 25000
Timer : 5, programmed : 30000, actual : 30009
Timer : 6, programmed : 35000, actual : 35009
Timer : 7, programmed : 40000, actual : 40009
Timer : 8, programmed : 45000, actual : 45009
Timer : 9, programmed : 50000, actual : 50009
Timer : 10, programmed : 55000, actual : 0
Timer : 11, programmed : 60000, actual : 0
Timer : 12, programmed : 65000, actual : 0
Timer : 13, programmed : 70000, actual : 0
Timer : 14, programmed : 75000, actual : 0
Timer : 15, programmed : 80000, actual : 0
SimpleTimer : 2, ms : 61652, Dms : 10006
Timer : 0, programmed : 5000, actual : 5000
Timer : 1, programmed : 10000, actual : 10000
Timer : 2, programmed : 15000, actual : 15000
Timer : 3, programmed : 20000, actual : 20000
Timer : 4, programmed : 25000, actual : 25000
Timer : 5, programmed : 30000, actual : 30000
Timer : 6, programmed : 35000, actual : 35009
Timer : 7, programmed : 40000, actual : 40009
Timer : 8, programmed : 45000, actual : 45009
Timer : 9, programmed : 50000, actual : 50009
Timer : 10, programmed : 55000, actual : 55009
Timer : 11, programmed : 60000, actual : 60009
Timer : 12, programmed : 65000, actual : 0
Timer : 13, programmed : 70000, actual : 0
Timer : 14, programmed : 75000, actual : 0
Timer : 15, programmed : 80000, actual : 0
SimpleTimer : 2, ms : 71658, Dms : 10006
Timer : 0, programmed : 5000, actual : 5000
Timer : 1, programmed : 10000, actual : 10000
Timer : 2, programmed : 15000, actual : 15000
Timer : 3, programmed : 20000, actual : 20000
Timer : 4, programmed : 25000, actual : 25000
Timer : 5, programmed : 30000, actual : 30000
Timer : 6, programmed : 35000, actual : 35000
Timer : 7, programmed : 40000, actual : 40009
Timer : 8, programmed : 45000, actual : 45009
Timer : 9, programmed : 50000, actual : 50009
Timer : 10, programmed : 55000, actual : 55009
Timer : 11, programmed : 60000, actual : 60009
Timer : 12, programmed : 65000, actual : 65009
Timer : 13, programmed : 70000, actual : 70009
Timer : 14, programmed : 75000, actual : 0
Timer : 15, programmed : 80000, actual : 0
SimpleTimer : 2, ms : 81664, Dms : 10006
Timer : 0, programmed : 5000, actual : 5000
Timer : 1, programmed : 10000, actual : 10000
Timer : 2, programmed : 15000, actual : 15000
Timer : 3, programmed : 20000, actual : 20000
Timer : 4, programmed : 25000, actual : 25000
Timer : 5, programmed : 30000, actual : 30000
Timer : 6, programmed : 35000, actual : 35000
Timer : 7, programmed : 40000, actual : 40000
Timer : 8, programmed : 45000, actual : 45009
Timer : 9, programmed : 50000, actual : 50009
Timer : 10, programmed : 55000, actual : 55009
Timer : 11, programmed : 60000, actual : 60009
Timer : 12, programmed : 65000, actual : 65009
Timer : 13, programmed : 70000, actual : 70009
Timer : 14, programmed : 75000, actual : 75009
Timer : 15, programmed : 80000, actual : 80009
...
SimpleTimer : 2, ms : 161712, Dms : 10006
Timer : 0, programmed : 5000, actual : 5000
Timer : 1, programmed : 10000, actual : 10000
Timer : 2, programmed : 15000, actual : 15000
Timer : 3, programmed : 20000, actual : 20000
Timer : 4, programmed : 25000, actual : 25000
Timer : 5, programmed : 30000, actual : 30000
Timer : 6, programmed : 35000, actual : 35000
Timer : 7, programmed : 40000, actual : 40000
Timer : 8, programmed : 45000, actual : 45000
Timer : 9, programmed : 50000, actual : 50000
Timer : 10, programmed : 55000, actual : 55000
Timer : 11, programmed : 60000, actual : 60000
Timer : 12, programmed : 65000, actual : 65000
Timer : 13, programmed : 70000, actual : 70000
Timer : 14, programmed : 75000, actual : 75000
Timer : 15, programmed : 80000, actual : 80000
SimpleTimer : 2, ms : 171718, Dms : 10006
Timer : 0, programmed : 5000, actual : 5000
Timer : 1, programmed : 10000, actual : 10000
Timer : 2, programmed : 15000, actual : 15000
Timer : 3, programmed : 20000, actual : 20000
Timer : 4, programmed : 25000, actual : 25000
Timer : 5, programmed : 30000, actual : 30000
Timer : 6, programmed : 35000, actual : 35000
Timer : 7, programmed : 40000, actual : 40000
Timer : 8, programmed : 45000, actual : 45000
Timer : 9, programmed : 50000, actual : 50000
Timer : 10, programmed : 55000, actual : 55000
Timer : 11, programmed : 60000, actual : 60000
Timer : 12, programmed : 65000, actual : 65000
Timer : 13, programmed : 70000, actual : 70000
Timer : 14, programmed : 75000, actual : 75000
Timer : 15, programmed : 80000, actual : 80000


Debug

Debug is enabled by default on Serial.

You can also change the debugging level (TIMERINTERRUPT_LOGLEVEL) from 0 to 4

// These define's must be placed at the beginning before #include "MBED_RPI_PICO_TimerInterrupt.h"
// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
#define _TIMERINTERRUPT_LOGLEVEL_     0

Troubleshooting

If you get compilation errors, more often than not, you may need to install a newer version of the core for Arduino boards.

Sometimes, the library will only work if you update the board core to the latest version because I am using newly added functions.



Issues

Submit issues to: MBED_RPI_PICO_TimerInterrupt issues


TO DO

  1. Search for bug and improvement.

DONE

  1. Basic hardware timers for RP2040-based boards such as Nano_RP2040_Connect, RASPBERRY_PI_PICO, using Arduino-mbed RP2040 core
  2. More hardware-initiated software-enabled timers
  3. Longer time interval
  4. Add Version String
  5. Add Table of Contents
  6. Fix multiple-definitions linker error
  7. Optimize library code by using reference-passing instead of value-passing
  8. Using float instead of ulong for interval for better accuracy


Contributions and Thanks

Many thanks for everyone for bug reporting, new feature suggesting, testing and contributing to the development of this library.

  1. Neil Baylis to report issue Poor accuracy on timer interrupt frequency or interval. #4 leading to version v1.2.0 to fix poor-timer-accuracy bug
pixpop
Neil Baylis


Contributing

If you want to contribute to this project:

  • Report bugs and errors
  • Ask for enhancements
  • Create issues and pull requests
  • Tell other people about this library

License

  • The library is licensed under MIT

Copyright

Copyright 2021- Khoi Hoang

mbed_rpi_pico_timerinterrupt's People

Contributors

khoih-prog avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

devklee

mbed_rpi_pico_timerinterrupt's Issues

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

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)

Interrupts stop after 94 minutes

Describe the bug

I setup an interrupt based on the sample code, running at 8kHz on a Raspberry Pi Pico, developing on PlatformIO, VS Code on a Mac version 11.6. The interrupt runs fine for about 94 minutes, then stops. It then starts up 3.5 hours later, but only runs for another 4 minutes before stopping again.

Expected behavior

Interrupt should continue to run

Actual behavior

Interrupt routine stops after about 94 minutes

Code to Reproduce

// These define's must be placed at the beginning before #include "TimerInterrupt_Generic.h"
// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
#define _TIMERINTERRUPT_LOGLEVEL_ 4

// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include "MBED_RPi_Pico_TimerInterrupt.h"

#ifndef LED_BUILTIN
#define LED_BUILTIN 25  // Pin LED_BUILTIN mapped to pin GPIO25 of RPI_PICO, control on-board LED
#endif

#define PIN_D1 1  // Pin D1 mapped to pin GPIO1 of RPI_PICO

volatile uint32_t counter = 0;

// Never use Serial.print inside this mbed ISR. Will hang the system
void TimerHandler1(uint alarm_num) {
    static bool toggle1 = false;

    ///////////////////////////////////////////////////////////
    // Always call this for MBED RP2040 before processing ISR
    TIMER_ISR_START(alarm_num);
    ///////////////////////////////////////////////////////////

    // timer interrupt toggles outputPin
    digitalWrite(PIN_D1, toggle1);
    toggle1 = !toggle1;
    counter++;


    ////////////////////////////////////////////////////////////
    // Always call this for MBED RP2040 after processing ISR
    TIMER_ISR_END(alarm_num);
    ////////////////////////////////////////////////////////////
}

#define TIMER1_INTERVAL_US 125

// Init RPI_PICO_Timer
MBED_RPI_PICO_Timer ITimer1(1);

void setup() {
    pinMode(LED_BUILTIN, OUTPUT);
    pinMode(PIN_D1, OUTPUT);

    Serial.begin(115200);
    while (!Serial)
        ;

    delay(100);

    Serial.print(F("\nStarting Argument_None on "));
    Serial.println(BOARD_NAME);
    Serial.println(MBED_RPI_PICO_TIMER_INTERRUPT_VERSION);

    // Interval in microsecs
    if (ITimer1.attachInterruptInterval(TIMER1_INTERVAL_US, TimerHandler1)) {
        Serial.print(F("Starting ITimer1 OK, millis() = "));
        Serial.println(millis());
    } else
        Serial.println(F("Can't set ITimer1. Select another Timer, freq. or timer"));
}

void loop() {
  static uint32_t last_counter = 0;
  static uint32_t last_running = 0;
  uint32_t running = ( counter != last_counter );
  last_counter = counter;
  if ( last_running != running ) {
    last_running = running;
    Serial.print(millis());
    Serial.print(' ');
    Serial.println(running ? " Running ":" Not Running ");
  }
  delay(1);
}

Above code output

Starting Argument_None on RaspberryPi Pico
MBED_RPi_Pico_TimerInterrupt v1.2.0
[TISR] _timerNo = 1, Clock (Hz) = 1000000.00, _fre (Hz) = 8000.00
[TISR] _count = 0-125
[TISR] hardware_alarm_set_target, uS = 125
Starting ITimer1 OK, millis() = 1201
1201  Running 
5637146  Not Running 
18522047  Running 
18790483  Not Running 

Scope output

I also had a scope probe on D1. It output a 4kHz square wave for the first 94 minutes, then stopped.

Information

  • Platform.io version Core 6.1.5·Home 3.4.3
  • khoih-prog/MBED_RPI_PICO_TimerInterrupt@^1.2.0
  • Arduino mbed_rp2040 Core Version (e.g. Arduino mbed_rp2040 core v3.3.0)
  • RP2040 Board type: RASPBERRY_PI_PICO & Seeed XAIO RP2040

Disclaimer

My first time submitting a report. Be gentle if I didn't do it right. :)

I2C and SPI halt in ISR

Hi,

i'm trying to acquire some external sensor on the SPI and I2C bus in the ISR, but at the initializatione the system goes in fault, blinking the red led.

Is this a known bug?

Poor accuracy on timer interrupt frequency or interval.

Describe the bug

Timer interrupts at a lower frequency than expected.

Arduino IDE 1.8.19
Arduino MBED nano 3.3.0
MBED_RPI_PICO_TimerInterrupt 1.1.2
Hardware: Arduino nano rp2040

Steps to Reproduce

I set a frequency of 20 kHz, and then count 20k interrupts. I check mills() before and after. The delta comes out to 1020 ms where it should be 1000. So it's off by 2%.

Expected behavior

I expected it to take 1000 ms to count 20000 interrupts at 20000 Hz

Actual behavior

It took 1020 ms, so it's off by 2%. I see the same error if I program it by interval or by frequency.

Test code:

#include <MBED_RPi_Pico_TimerInterrupt.h>
#include <MBED_RPi_Pico_TimerInterrupt.hpp>
#include <MBED_RPi_Pico_ISR_Timer.h>
#include <MBED_RPi_Pico_ISR_Timer.hpp>

#define TIMER_FREQ_HZ        20000

MBED_RPI_PICO_Timer ITimer1(1);

int delta = 0;

void TimerHandler(uint alarm_num) // Frequency version
{
  static int zz = 0;
  static int prev_millis = 0;
  int new_millis;

  TIMER_ISR_START(alarm_num);
  
  zz++;
  if(zz >= TIMER_FREQ_HZ) {
    zz = 0;
    new_millis = millis();
    delta = new_millis - prev_millis;
    prev_millis = new_millis;
  }

  TIMER_ISR_END(alarm_num);
}

void setup() {
  Serial.begin(9600);
  delay(2500);

  if (ITimer1.attachInterrupt(TIMER_FREQ_HZ, TimerHandler))
    Serial.println("Starting ITimer OK, millis() = " + String(millis()));
  else
    Serial.println("Can't set ITimer. Select another freq. or timer");
}

void loop() {
  delay(1000);
  Serial.println(delta);
}

Sample printout:

Starting ITimer OK, millis() = 2500
0
3520
1020
1020
1020
1020
1020
1020
1020
1020
1020
1020
1020

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.