7 Commits
Author SHA1 Message Date
Khoi Hoang 2b37b5925d Update .github 2022-09-29 22:06:40 -04:00
Khoi Hoang 6b319e531f v1.3.1 using float for interval
### Releases v1.3.1

1. Using `float` instead of `ulong` for better interval accuracy
2022-09-29 22:06:10 -04:00
Khoi Hoang b7d8f4b4b7 v1.3.1 using float for interval
### Releases v1.3.1

1. Using `float` instead of `ulong` for better interval accuracy
2022-09-29 22:03:58 -04:00
Khoi Hoang f010d88221 Update .github 2022-09-25 19:41:54 -04:00
Khoi Hoang b99aafc23a v1.3.0 to fix severe bug affecting start time
### Releases v1.3.0

1. Fix  affecting time between the starts. Check [Enable fixed timing between timer calls (vs fixed time btw. end of timer call and next call as implemented) #3](https://github.com/khoih-prog/RPI_PICO_TimerInterrupt/issues/3)
2022-09-25 19:41:28 -04:00
Khoi Hoang f2ca9ced6e Fix typo 2022-02-08 21:29:52 -05:00
Khoi Hoang 45f0418bfe Add buymeacoffee
Finally, in need of some real coffee ;=))
2022-02-03 19:09:12 -05:00
21 changed files with 259 additions and 244 deletions
+10 -7
View File
@@ -35,24 +35,27 @@ If applicable, add screenshots to help explain your problem.
Please ensure to specify the following:
* Arduino IDE version (e.g. 1.8.13) or Platform.io version
* `ESP8266`,`ESP32` or `STM32` Core Version (e.g. ESP8266 core v2.7.4, ESP32 v1.0.5 or STM32 v1.9.0)
* Arduino IDE version (e.g. 1.8.19) or Platform.io version
* `RP2040` Core Version (e.g. RP2040 core v2.5.4)
* `RP2040` Board type (e.g. RASPBERRY_PI_PICO, RASPBERRY_PI_PICO_W, ADAFRUIT_FEATHER_RP2040, GENERIC_RP2040, etc.)
* Contextual information (e.g. what you were trying to achieve)
* Simplest possible steps to reproduce
* Anything that might be relevant in your opinion, such as:
* Operating system (Windows, Ubuntu, etc.) and the output of `uname -a`
* Network configuration
### Example
```
Arduino IDE version: 1.8.13
ESP32 Core Version 1.0.5
Arduino IDE version: 1.8.19
RP2040 core v2.5.4
RASPBERRY_PI_PICO Module
OS: Ubuntu 20.04 LTS
Linux xy-Inspiron-3593 5.4.0-66-generic #74-Ubuntu SMP Wed Jan 27 22:54:38 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
Linux xy-Inspiron-3593 5.15.0-48-generic #54~20.04.1-Ubuntu SMP Thu Sep 1 16:17:26 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Context:
I encountered an endless loop while trying to connect to Local WiFi.
I encountered a crash while using this library
Steps to reproduce:
1. ...
+6 -5
View File
@@ -15,8 +15,8 @@ If you don't find anything, please [open a new issue](https://github.com/khoih-p
Please ensure to specify the following:
* Arduino IDE version (e.g. 1.8.19) or Platform.io version
* `RP2040` Core Version (e.g. RP2040 core v1.9.14)
* `RP2040` Board type (e.g. RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040, GENERIC_RP2040, etc.)
* `RP2040` Core Version (e.g. RP2040 core v2.5.4)
* `RP2040` Board type (e.g. RASPBERRY_PI_PICO, RASPBERRY_PI_PICO_W, ADAFRUIT_FEATHER_RP2040, GENERIC_RP2040, etc.)
* Contextual information (e.g. what you were trying to achieve)
* Simplest possible steps to reproduce
* Anything that might be relevant in your opinion, such as:
@@ -28,13 +28,13 @@ Please ensure to specify the following:
```
Arduino IDE version: 1.8.19
RP2040 core v1.9.14
RP2040 core v2.5.4
RASPBERRY_PI_PICO Module
OS: Ubuntu 20.04 LTS
Linux xy-Inspiron-3593 5.4.0-96-generic #109-Ubuntu SMP Wed Jan 12 16:49:16 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Linux xy-Inspiron-3593 5.15.0-48-generic #54~20.04.1-Ubuntu SMP Thu Sep 1 16:17:26 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Context:
I encountered a crash while using TimerInterrupt.
I encountered a crash while using this library
Steps to reproduce:
1. ...
@@ -51,3 +51,4 @@ There are usually some outstanding feature requests in the [existing issues list
### Sending Pull Requests
Pull Requests with changes and fixes are also welcome!
+48 -178
View File
@@ -2,10 +2,13 @@
[![arduino-library-badge](https://www.ardu-badge.com/badge/RPI_PICO_TimerInterrupt.svg?)](https://www.ardu-badge.com/RPI_PICO_TimerInterrupt)
[![GitHub release](https://img.shields.io/github/release/khoih-prog/RPI_PICO_TimerInterrupt.svg)](https://github.com/khoih-prog/RPI_PICO_TimerInterrupt/releases)
[![GitHub](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/khoih-prog/RPI_PICO_TimerInterrupt/blob/master/LICENSE)
[![GitHub](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/khoih-prog/RPI_PICO_TimerInterrupt/blob/main/LICENSE)
[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](#Contributing)
[![GitHub issues](https://img.shields.io/github/issues/khoih-prog/RPI_PICO_TimerInterrupt.svg)](http://github.com/khoih-prog/RPI_PICO_TimerInterrupt/issues)
<a href="https://www.buymeacoffee.com/khoihprog6" title="Donate to my libraries using BuyMeACoffee"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Donate to my libraries using BuyMeACoffee" style="height: 50px !important;width: 181px !important;" ></a>
<a href="https://www.buymeacoffee.com/khoihprog6" title="Donate to my libraries using BuyMeACoffee"><img src="https://img.shields.io/badge/buy%20me%20a%20coffee-donate-orange.svg?logo=buy-me-a-coffee&logoColor=FFDD00" style="height: 20px !important;width: 200px !important;" ></a>
---
---
@@ -74,6 +77,8 @@
Please have a look at [HOWTO Fix `Multiple Definitions` Linker Error](#howto-fix-multiple-definitions-linker-error)
---
### Why do we need this [RPI_PICO_TimerInterrupt library](https://github.com/khoih-prog/RPI_PICO_TimerInterrupt)
## Features
@@ -117,7 +122,7 @@ The catch is **your function is now part of an ISR (Interrupt Service Routine),
### Currently supported Boards
1. RP2040-based boards such as **RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040, Nano_RP2040_Connect, GENERIC_RP2040**, etc.
1. RP2040-based boards such as **RASPBERRY_PI_PICO, RASPBERRY_PI_PICO_W, ADAFRUIT_FEATHER_RP2040, Nano_RP2040_Connect, GENERIC_RP2040**, etc., using [`arduino-pico core`](https://github.com/earlephilhower/arduino-pico)
---
@@ -135,7 +140,7 @@ The catch is **your function is now part of an ISR (Interrupt Service Routine),
## Prerequisites
1. [`Arduino IDE 1.8.19+` for Arduino](https://github.com/arduino/Arduino). [![GitHub release](https://img.shields.io/github/release/arduino/Arduino.svg)](https://github.com/arduino/Arduino/releases/latest)
2. [`Earle Philhower's arduino-pico core v1.9.14+`](https://github.com/earlephilhower/arduino-pico) for RP2040-based boards such as **RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040**, etc. [![GitHub release](https://img.shields.io/github/release/earlephilhower/arduino-pico.svg)](https://github.com/earlephilhower/arduino-pico/releases/latest)
2. [`Earle Philhower's arduino-pico core v2.5.4+`](https://github.com/earlephilhower/arduino-pico) for RP2040-based boards such as **RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040**, etc. [![GitHub release](https://img.shields.io/github/release/earlephilhower/arduino-pico.svg)](https://github.com/earlephilhower/arduino-pico/releases/latest)
3. To use with certain example
- [`SimpleTimer library`](https://github.com/jfturcot/SimpleTimer) for [ISR_Timers_Array_Simple](examples/ISR_Timers_Array_Simple) and [ISR_16_Timers_Array_Complex](examples/ISR_16_Timers_Array_Complex) examples.
@@ -155,15 +160,15 @@ You can also use this link [![arduino-library-badge](https://www.ardu-badge.com/
Another way to install is to:
1. Navigate to [**RPI_PICO_TimerInterrupt**](https://github.com/khoih-prog/RPI_PICO_TimerInterrupt) page.
2. Download the latest release `RPI_PICO_TimerInterrupt-master.zip`.
3. Extract the zip file to `RPI_PICO_TimerInterrupt-master` directory
4. Copy whole `RPI_PICO_TimerInterrupt-master` folder to Arduino libraries' directory such as `~/Arduino/libraries/`.
2. Download the latest release `RPI_PICO_TimerInterrupt-main.zip`.
3. Extract the zip file to `RPI_PICO_TimerInterrupt-main` directory
4. Copy whole `RPI_PICO_TimerInterrupt-main` folder to Arduino libraries' directory such as `~/Arduino/libraries/`.
### VS Code & PlatformIO
1. Install [VS Code](https://code.visualstudio.com/)
2. Install [PlatformIO](https://platformio.org/platformio-ide)
3. Install [**RPI_PICO_TimerInterrupt** library](https://platformio.org/lib/show/12177/RPI_PICO_TimerInterrupt) or [**RPI_PICO_TimerInterrupt** library](https://platformio.org/lib/show/12273/RPI_PICO_TimerInterrupt) by using [Library Manager](https://platformio.org/lib/show/12177/RPI_PICO_TimerInterrupt/installation). Search for **RPI_PICO_TimerInterrupt** in [Platform.io Author's Libraries](https://platformio.org/lib/search?query=author:%22Khoi%20Hoang%22)
3. Install [**RPI_PICO_TimerInterrupt** library](https://registry.platformio.org/libraries/khoih-prog/RPI_PICO_TimerInterrupt) by using [Library Manager](https://registry.platformio.org/libraries/khoih-prog/RPI_PICO_TimerInterrupt/installation). Search for **RPI_PICO_TimerInterrupt** in [Platform.io Author's Libraries](https://platformio.org/lib/search?query=author:%22Khoi%20Hoang%22)
4. Please visit documentation for the other options and examples at [Project Configuration File](https://docs.platformio.org/page/projectconf.html)
@@ -215,7 +220,7 @@ The current library implementation, using `xyz-Impl.h` instead of standard `xyz.
You can include these `.hpp` or `.h` files
```
```C++
// Can be included as many times as necessary, without `Multiple Definitions` Linker Error
#include "RPi_Pico_TimerInterrupt.h" //https://github.com/khoih-prog/RPI_PICO_TimerInterrupt
@@ -225,7 +230,7 @@ You can include these `.hpp` or `.h` files
in many files. But be sure to use the following `.h` file **in just 1 `.h`, `.cpp` or `.ino` file**, which must **not be included in any other file**, to avoid `Multiple Definitions` Linker Error
```
```C++
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include "RPi_Pico_ISR_Timer.h" //https://github.com/khoih-prog/RPI_PICO_TimerInterrupt
```
@@ -266,7 +271,7 @@ Before using any Timer, you have to make sure the Timer has not been used by any
### 1.1 Init Hardware Timer
```
```C++
// Select the timer you're using, from ITimer0(0)-ITimer3(3)
// Init RPI_PICO_Timer
RPI_PICO_Timer ITimer1(1);
@@ -276,7 +281,7 @@ RPI_PICO_Timer ITimer1(1);
Use one of these functions with **interval in unsigned long microseconds**
```
```C++
// interval (in us), callback is ISR
bool setInterval(unsigned long interval, pico_timer_callback callback);
@@ -286,7 +291,7 @@ bool attachInterruptInterval(unsigned long interval, pico_timer_callback callbac
as follows
```
```C++
void TimerHandler()
{
// Doing something here inside ISR
@@ -313,7 +318,7 @@ void setup()
Use one of these functions with **frequency in float Hz**
```
```C++
// frequency (in Hz), callback is ISR
bool setFrequency(float frequency, pico_timer_callback callback)
@@ -323,7 +328,7 @@ bool attachInterrupt(float frequency, timer_callback callback);
as follows
```
```C++
void TimerHandler()
{
// Doing something here inside ISR
@@ -355,7 +360,7 @@ The 16 ISR_based Timers, designed for long timer intervals, only support using *
### 2.2 Init Hardware Timer and ISR-based Timer
```
```C++
// Init RPI_PICO_Timer
RPI_PICO_Timer ITimer1(1);
@@ -366,7 +371,7 @@ RPI_PICO_ISR_Timer ISR_timer;
### 2.3 Set Hardware Timer Interval and attach Timer Interrupt Handler functions
```
```C++
void TimerHandler()
{
ISR_timer.run();
@@ -443,152 +448,8 @@ void setup()
### Example [ISR_Timers_Array_Simple](examples/ISR_Timers_Array_Simple)
```
// These define's must be placed at the beginning before #include "TimerInterrupt_Generic.h"
// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
#define TIMER_INTERRUPT_DEBUG 1
#define _TIMERINTERRUPT_LOGLEVEL_ 4
https://github.com/khoih-prog/RPI_PICO_TimerInterrupt/blob/b7d8f4b4b7eabec8c140fc55d88efb82939a6ad6/examples/ISR_Timers_Array_Simple/ISR_Timers_Array_Simple.ino#L23-L168
// Can be included as many times as necessary, without `Multiple Definitions` Linker Error
#include "RPi_Pico_TimerInterrupt.h"
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include "RPi_Pico_ISR_Timer.h"
#include <SimpleTimer.h> // https://github.com/jfturcot/SimpleTimer
// Init RPI_PICO_Timer
RPI_PICO_Timer ITimer1(1);
RPI_PICO_ISR_Timer 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;
bool TimerHandler(struct repeating_timer *t)
{
static bool toggle = false;
static int timeRun = 0;
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;
}
return true;
}
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(RPI_PICO_TIMER_INTERRUPT_VERSION);
Serial.print(F("CPU Frequency = ")); Serial.print(F_CPU / 1000000); Serial.println(F(" MHz"));
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();
}
```
---
---
@@ -601,9 +462,8 @@ The following is the sample terminal output when running example [ISR_Timers_Arr
While software timer, **programmed for 2s, is activated after more than 10.000s !!!**
```
Starting ISR_Timers_Array_Simple on RASPBERRY_PI_PICO
RPi_Pico_TimerInterrupt v1.2.0
RPi_Pico_TimerInterrupt v1.3.1
CPU Frequency = 125 MHz
[TISR] _timerNo = 1, Clock (Hz) = 1000000.00, _fre (Hz) = 1000.00
[TISR] _count = 0-1000
@@ -628,7 +488,7 @@ The following is the sample terminal output when running example [TimerInterrupt
```
Starting TimerInterruptTest on RASPBERRY_PI_PICO
RPi_Pico_TimerInterrupt v1.2.0
RPi_Pico_TimerInterrupt v1.3.1
CPU Frequency = 125 MHz
[TISR] _timerNo = 0, Clock (Hz) = 1000000.00, _fre (Hz) = 1.00
[TISR] _count = 0-1000000
@@ -672,7 +532,7 @@ The following is the sample terminal output when running example [Change_Interva
```
Starting Change_Interval on RASPBERRY_PI_PICO
RPi_Pico_TimerInterrupt v1.2.0
RPi_Pico_TimerInterrupt v1.3.1
CPU Frequency = 125 MHz
[TISR] _timerNo = 0, Clock (Hz) = 1000000.00, _fre (Hz) = 0.50
[TISR] _count = 0-2000000
@@ -753,7 +613,7 @@ The following is the sample terminal output when running example [SwitchDebounce
```
Starting SwitchDebounce on RASPBERRY_PI_PICO
RPi_Pico_TimerInterrupt v1.2.0
RPi_Pico_TimerInterrupt v1.3.1
CPU Frequency = 125 MHz
[TISR] _timerNo = 1, Clock (Hz) = 1000000.00, _fre (Hz) = 50.00
[TISR] _count = 0-20000
@@ -778,7 +638,7 @@ The following is the sample terminal output when running example [ISR_Timers_Arr
```
Starting ISR_Timers_Array_Simple on RASPBERRY_PI_PICO
RPi_Pico_TimerInterrupt v1.2.0
RPi_Pico_TimerInterrupt v1.3.1
CPU Frequency = 125 MHz
[TISR] _timerNo = 1, Clock (Hz) = 1000000.00, _fre (Hz) = 1000.00
[TISR] _count = 0-1000
@@ -801,7 +661,7 @@ The following is the sample terminal output when running example [ISR_16_Timers_
```
Starting ISR_16_Timers_Array_Complex on RASPBERRY_PI_PICO
RPi_Pico_TimerInterrupt v1.2.0
RPi_Pico_TimerInterrupt v1.3.1
CPU Frequency = 125 MHz
[TISR] _timerNo = 1, Clock (Hz) = 1000000.00, _fre (Hz) = 100.00
[TISR] _count = 0-10000
@@ -954,7 +814,7 @@ Debug is enabled by default on Serial.
You can also change the debugging level (_TIMERINTERRUPT_LOGLEVEL_) from 0 to 4
```cpp
```C++
// These define's must be placed at the beginning before #include "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.
@@ -989,14 +849,16 @@ Submit issues to: [RPI_PICO_TimerInterrupt issues](https://github.com/khoih-prog
## DONE
1. Basic hardware timers for **RP2040-based boards such as RASPBERRY_PI_PICO**, using [**Earle Philhower's arduino-pico** core](https://github.com/earlephilhower/arduino-pico)
2. More hardware-initiated software-enabled timers
3. Longer time interval
4. Add Version String
5. Add Table of Contents
6. Add support to new boards (**ADAFRUIT_ITSYBITSY_RP2040, ADAFRUIT_QTPY_RP2040, ADAFRUIT_STEMMAFRIEND_RP2040, ADAFRUIT_TRINKEYQT_RP2040, ADAFRUIT_MACROPAD_RP2040, SPARKFUN_PROMICRO_RP2040, Nano_RP2040_Connect, etc.**) using the arduino-pico core
7. Fix `multiple-definitions` linker error
8. Optimize library code by using `reference-passing` instead of `value-passing`
1. Basic hardware timers for **RP2040-based boards such as RASPBERRY_PI_PICO**, using [**Earle Philhower's arduino-pico** core](https://github.com/earlephilhower/arduino-pico)
2. More hardware-initiated software-enabled timers
3. Longer time interval
4. Add Version String
5. Add Table of Contents
6. Add support to new boards (**ADAFRUIT_ITSYBITSY_RP2040, ADAFRUIT_QTPY_RP2040, ADAFRUIT_STEMMAFRIEND_RP2040, ADAFRUIT_TRINKEYQT_RP2040, ADAFRUIT_MACROPAD_RP2040, SPARKFUN_PROMICRO_RP2040, Nano_RP2040_Connect, etc.**) using the arduino-pico core
7. Fix `multiple-definitions` linker error
8. Optimize library code by using `reference-passing` instead of `value-passing`
9. Fix severe bug affecting time between the starts. Check [Enable fixed timing between timer calls (vs fixed time btw. end of timer call and next call as implemented) #3](https://github.com/khoih-prog/RPI_PICO_TimerInterrupt/issues/3)
10. Using `float` instead of `ulong` for better interval accuracy
---
@@ -1006,6 +868,14 @@ Submit issues to: [RPI_PICO_TimerInterrupt issues](https://github.com/khoih-prog
Many thanks for everyone for bug reporting, new feature suggesting, testing and contributing to the development of this library.
1. [AndreasOKircher](https://github.com/AndreasOKircher) to report issue [Enable fixed timing between timer calls (vs fixed time btw. end of timer call and next call as implemented) #3](https://github.com/khoih-prog/RPI_PICO_TimerInterrupt/issues/3) leading to version v1.3.0 to fix `severe bug` affecting time between the starts
<table>
<tr>
<td align="center"><a href="https://github.com/AndreasOKircher"><img src="https://github.com/AndreasOKircher.png" width="100px;" alt="AndreasOKircher"/><br /><sub><b>AndreasOKircher</b></sub></a><br /></td>
</tr>
</table>
---
@@ -1022,7 +892,7 @@ If you want to contribute to this project:
### License
- The library is licensed under [MIT](https://github.com/khoih-prog/RPI_PICO_TimerInterrupt/blob/master/LICENSE)
- The library is licensed under [MIT](https://github.com/khoih-prog/RPI_PICO_TimerInterrupt/blob/main/LICENSE)
---
+11
View File
@@ -12,6 +12,8 @@
## Table of Contents
* [Changelog](#changelog)
* [Releases v1.3.1](#releases-v131)
* [Releases v1.3.0](#releases-v130)
* [Releases v1.2.0](#releases-v120)
* [Releases v1.1.1](#releases-v111)
* [Releases v1.1.0](#releases-v110)
@@ -24,6 +26,15 @@
## Changelog
### Releases v1.3.1
1. Using `float` instead of `ulong` for better interval accuracy
### Releases v1.3.0
1. Fix severe bug affecting time between the starts. Check [Enable fixed timing between timer calls (vs fixed time btw. end of timer call and next call as implemented) #3](https://github.com/khoih-prog/RPI_PICO_TimerInterrupt/issues/3)
### Releases v1.2.0
1. Fix `multiple-definitions` linker error. Drop `src_cpp` and `src_h` directories
@@ -47,6 +47,8 @@ volatile pinStruct myOutputPins = { LED_BUILTIN, 0, 1 };
bool TimerHandler(struct repeating_timer *t)
{
(void) t;
static bool toggle = false;
//timer interrupt toggles pins
+5 -1
View File
@@ -43,13 +43,15 @@
#include "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
#define LED_BUILTIN 25
#endif
#define PIN_D1 1 // Pin D1 mapped to pin GPIO1 of RPI_PICO
bool TimerHandler0(struct repeating_timer *t)
{
(void) t;
static bool toggle0 = false;
#if (TIMER_INTERRUPT_DEBUG > 0)
@@ -65,6 +67,8 @@ bool TimerHandler0(struct repeating_timer *t)
bool TimerHandler1(struct repeating_timer *t)
{
(void) t;
static bool toggle1 = false;
#if (TIMER_INTERRUPT_DEBUG > 0)
@@ -44,6 +44,8 @@ RPI_PICO_Timer ITimer2(2);
bool TimerHandler1(struct repeating_timer *t)
{
(void) t;
static bool toggle1 = false;
#if (TIMER_INTERRUPT_DEBUG > 0)
@@ -61,6 +63,8 @@ bool TimerHandler1(struct repeating_timer *t)
bool TimerHandler2(struct repeating_timer *t)
{
(void) t;
static bool toggle2 = false;
#if (TIMER_INTERRUPT_DEBUG > 0)
+5 -1
View File
@@ -42,7 +42,7 @@
#include "RPi_Pico_TimerInterrupt.h"
#ifndef LED_BUILTIN
#define LED_BUILTIN 25 // Pin D2 mapped to pin GPIO2/ADC12 of ESP32, control on-board LED
#define LED_BUILTIN 25
#endif
#define PIN_D1 1 // Pin D1 mapped to pin GPIO1 of RPI_PICO
@@ -52,6 +52,8 @@ volatile uint32_t Timer1Count = 0;
bool TimerHandler0(struct repeating_timer *t)
{
(void) t;
static bool toggle0 = false;
// Flag for checking to be sure ISR is working as Serial.print is not OK here in ISR
@@ -70,6 +72,8 @@ bool TimerHandler0(struct repeating_timer *t)
bool TimerHandler1(struct repeating_timer *t)
{
(void) t;
static bool toggle1 = false;
// Flag for checking to be sure ISR is working as Serial.print is not OK here in ISR
@@ -80,6 +80,8 @@ RPI_PICO_ISR_Timer ISR_Timer;
// Never use Serial.print inside this ISR. Will hang the system
bool TimerHandler(struct repeating_timer *t)
{
(void) t;
static bool toggle = false;
static int timeRun = 0;
@@ -60,6 +60,8 @@ volatile uint32_t previousMillis5s = 0;
bool TimerHandler(struct repeating_timer *t)
{
(void) t;
static bool toggle = false;
static int timeRun = 0;
+2
View File
@@ -69,6 +69,8 @@ volatile int debounceCounter;
bool TimerHandler0(struct repeating_timer *t)
{
(void) t;
if ( !digitalRead(SWPin) && (debounceCounter >= DEBOUNCING_INTERVAL_MS / TIMER0_INTERVAL_MS ) )
{
//min time between pulses has passed
@@ -64,6 +64,8 @@ volatile bool SWLongPressed = false;
bool TimerHandler1(struct repeating_timer *t)
{
(void) t;
static unsigned int debounceCountSWPressed = 0;
static unsigned int debounceCountSWReleased = 0;
@@ -41,13 +41,15 @@
#include "RPi_Pico_TimerInterrupt.h"
#ifndef LED_BUILTIN
#define LED_BUILTIN 25 // Pin D2 mapped to pin GPIO2/ADC12 of ESP32, control on-board LED
#define LED_BUILTIN 25
#endif
#define PIN_D1 1 // Pin D1 mapped to pin GPIO1 of RPI_PICO
bool TimerHandler0(struct repeating_timer *t)
{
(void) t;
static bool toggle0 = false;
static bool started = false;
@@ -70,6 +72,8 @@ bool TimerHandler0(struct repeating_timer *t)
bool TimerHandler1(struct repeating_timer *t)
{
(void) t;
static bool toggle1 = false;
static bool started = false;
+3 -3
View File
@@ -1,7 +1,7 @@
{
"name": "RPI_PICO_TimerInterrupt",
"version": "1.2.0",
"keywords": "timing, device, control, timer, interrupt, hardware, isr, isr-based, hardware-timer, mission-critical, accuracy, precise, non-blocking, raspberry-pico, rpi-pico, pico, rp2040, arduino-pico",
"version": "1.3.1",
"keywords": "timing, device, control, timer, interrupt, hardware, isr, isr-based, hardware-timer, mission-critical, accuracy, non-blocking, raspberry-pico, rpi-pico, pico, rp2040, arduino-pico, rp2040w, raspberry-pico-w, pico-w",
"description": "This library enables you to use Interrupt from Hardware Timers on RP2040-based boards such as RASPBERRY_PI_PICO. 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. 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.",
"authors":
{
@@ -12,7 +12,7 @@
"repository":
{
"type": "git",
"url": "//https://github.com/khoih-prog/RPI_PICO_TimerInterrupt"
"url": "https://github.com/khoih-prog/RPI_PICO_TimerInterrupt"
},
"homepage": "https://github.com/khoih-prog/RPI_PICO_TimerInterrupt",
"export": {
+1 -1
View File
@@ -1,5 +1,5 @@
name=RPI_PICO_TimerInterrupt
version=1.2.0
version=1.3.1
author=Khoi Hoang <khoih.prog@gmail.com>
maintainer=Khoi Hoang <khoih.prog@gmail.com>
sentence=This library enables you to use Interrupt from Hardware Timers on RP2040-based boards such as RASPBERRY_PI_PICO
+3
View File
@@ -38,7 +38,10 @@ upload_speed = 921600
;monitor_port = COM11
; Checks for the compatibility with frameworks and dev/platforms
; To adjust as necessary
lib_compat_mode = strict
lib_ldf_mode = chain+
;lib_ldf_mode = deep+
lib_deps =
+49 -10
View File
@@ -25,7 +25,7 @@
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Version: 1.2.0
Version: 1.3.1
Version Modified By Date Comments
------- ----------- ---------- -----------
@@ -33,7 +33,9 @@
1.0.1 K Hoang 18/05/2021 Update README and Packages' Patches to match core arduino-pico core v1.4.0
1.1.0 K Hoang 10/00/2021 Add support to new boards using the arduino-pico core
1.1.1 K Hoang 22/10/2021 Fix platform in library.json for PIO
1.2.0 K.Hoang 21/01/2022 Fix `multiple-definitions` linker error.
1.2.0 K.Hoang 21/01/2022 Fix `multiple-definitions` linker error
1.3.0 K.Hoang 25/09/2022 Fix severe bug affecting time between the starts
1.3.1 K.Hoang 29/09/2022 Using float instead of ulong for interval
*****************************************************************************************************************************/
#pragma once
@@ -43,11 +45,15 @@
#include <string.h>
////////////////////////////////////////////////////////////////
RPI_PICO_ISR_Timer::RPI_PICO_ISR_Timer()
: numTimers (-1)
{
}
////////////////////////////////////////////////////////////////
void RPI_PICO_ISR_Timer::init()
{
unsigned long current_millis = millis(); //elapsed();
@@ -61,6 +67,8 @@ void RPI_PICO_ISR_Timer::init()
numTimers = 0;
}
////////////////////////////////////////////////////////////////
void RPI_PICO_ISR_Timer::run()
{
uint8_t i;
@@ -136,6 +144,7 @@ void RPI_PICO_ISR_Timer::run()
}
////////////////////////////////////////////////////////////////
// find the first available slot
// return -1 if none found
@@ -160,8 +169,9 @@ int RPI_PICO_ISR_Timer::findFirstFreeSlot()
return -1;
}
////////////////////////////////////////////////////////////////
int RPI_PICO_ISR_Timer::setupTimer(const unsigned long& d, void* f, void* p, bool h, const unsigned& n)
int RPI_PICO_ISR_Timer::setupTimer(const float& d, void* f, void* p, bool h, const unsigned& n)
{
int freeTimer;
@@ -194,38 +204,51 @@ int RPI_PICO_ISR_Timer::setupTimer(const unsigned long& d, void* f, void* p, boo
return freeTimer;
}
////////////////////////////////////////////////////////////////
int RPI_PICO_ISR_Timer::setTimer(const unsigned long& d, timer_callback f, const unsigned& n)
int RPI_PICO_ISR_Timer::setTimer(const float& d, timer_callback f, const unsigned& n)
{
return setupTimer(d, (void *)f, NULL, false, n);
}
int RPI_PICO_ISR_Timer::setTimer(const unsigned long& d, timer_callback_p f, void* p, const unsigned& n)
////////////////////////////////////////////////////////////////
int RPI_PICO_ISR_Timer::setTimer(const float& d, timer_callback_p f, void* p, const unsigned& n)
{
return setupTimer(d, (void *)f, p, true, n);
}
int RPI_PICO_ISR_Timer::setInterval(const unsigned long& d, timer_callback f)
////////////////////////////////////////////////////////////////
int RPI_PICO_ISR_Timer::setInterval(const float& d, timer_callback f)
{
return setupTimer(d, (void *)f, NULL, false, RPI_PICO_RUN_FOREVER);
}
int RPI_PICO_ISR_Timer::setInterval(const unsigned long& d, timer_callback_p f, void* p)
////////////////////////////////////////////////////////////////
int RPI_PICO_ISR_Timer::setInterval(const float& d, timer_callback_p f, void* p)
{
return setupTimer(d, (void *)f, p, true, RPI_PICO_RUN_FOREVER);
}
int RPI_PICO_ISR_Timer::setTimeout(const unsigned long& d, timer_callback f)
////////////////////////////////////////////////////////////////
int RPI_PICO_ISR_Timer::setTimeout(const float& d, timer_callback f)
{
return setupTimer(d, (void *)f, NULL, false, RPI_PICO_RUN_ONCE);
}
int RPI_PICO_ISR_Timer::setTimeout(const unsigned long& d, timer_callback_p f, void* p)
////////////////////////////////////////////////////////////////
int RPI_PICO_ISR_Timer::setTimeout(const float& d, timer_callback_p f, void* p)
{
return setupTimer(d, (void *)f, p, true, RPI_PICO_RUN_ONCE);
}
bool RPI_PICO_ISR_Timer::changeInterval(const unsigned& numTimer, const unsigned long& d)
////////////////////////////////////////////////////////////////
bool RPI_PICO_ISR_Timer::changeInterval(const unsigned& numTimer, const float& d)
{
if (numTimer >= RPI_PICO_MAX_TIMERS)
{
@@ -251,6 +274,8 @@ bool RPI_PICO_ISR_Timer::changeInterval(const unsigned& numTimer, const unsigned
return false;
}
////////////////////////////////////////////////////////////////
void RPI_PICO_ISR_Timer::deleteTimer(const unsigned& timerId)
{
if (timerId >= RPI_PICO_MAX_TIMERS)
@@ -282,6 +307,8 @@ void RPI_PICO_ISR_Timer::deleteTimer(const unsigned& timerId)
}
}
////////////////////////////////////////////////////////////////
// function contributed by code@rowansimms.com
void RPI_PICO_ISR_Timer::restartTimer(const unsigned& numTimer)
{
@@ -299,6 +326,7 @@ void RPI_PICO_ISR_Timer::restartTimer(const unsigned& numTimer)
rp2040.resumeOtherCore();
}
////////////////////////////////////////////////////////////////
bool RPI_PICO_ISR_Timer::isEnabled(const unsigned& numTimer)
{
@@ -310,6 +338,7 @@ bool RPI_PICO_ISR_Timer::isEnabled(const unsigned& numTimer)
return timer[numTimer].enabled;
}
////////////////////////////////////////////////////////////////
void RPI_PICO_ISR_Timer::enable(const unsigned& numTimer)
{
@@ -321,6 +350,7 @@ void RPI_PICO_ISR_Timer::enable(const unsigned& numTimer)
timer[numTimer].enabled = true;
}
////////////////////////////////////////////////////////////////
void RPI_PICO_ISR_Timer::disable(const unsigned& numTimer)
{
@@ -332,6 +362,8 @@ void RPI_PICO_ISR_Timer::disable(const unsigned& numTimer)
timer[numTimer].enabled = false;
}
////////////////////////////////////////////////////////////////
void RPI_PICO_ISR_Timer::enableAll()
{
// Enable all timers with a callback assigned (used)
@@ -351,6 +383,8 @@ void RPI_PICO_ISR_Timer::enableAll()
rp2040.resumeOtherCore();
}
////////////////////////////////////////////////////////////////
void RPI_PICO_ISR_Timer::disableAll()
{
// Disable all timers with a callback assigned (used)
@@ -371,6 +405,8 @@ void RPI_PICO_ISR_Timer::disableAll()
}
////////////////////////////////////////////////////////////////
void RPI_PICO_ISR_Timer::toggle(const unsigned& numTimer)
{
if (numTimer >= RPI_PICO_MAX_TIMERS)
@@ -381,11 +417,14 @@ void RPI_PICO_ISR_Timer::toggle(const unsigned& numTimer)
timer[numTimer].enabled = !timer[numTimer].enabled;
}
////////////////////////////////////////////////////////////////
unsigned RPI_PICO_ISR_Timer::getNumTimers()
{
return numTimers;
}
////////////////////////////////////////////////////////////////
#endif // ISR_TIMER_GENERIC_IMPL_H
+4 -2
View File
@@ -25,7 +25,7 @@
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Version: 1.2.0
Version: 1.3.1
Version Modified By Date Comments
------- ----------- ---------- -----------
@@ -33,7 +33,9 @@
1.0.1 K Hoang 18/05/2021 Update README and Packages' Patches to match core arduino-pico core v1.4.0
1.1.0 K Hoang 10/00/2021 Add support to new boards using the arduino-pico core
1.1.1 K Hoang 22/10/2021 Fix platform in library.json for PIO
1.2.0 K.Hoang 21/01/2022 Fix `multiple-definitions` linker error.
1.2.0 K.Hoang 21/01/2022 Fix `multiple-definitions` linker error
1.3.0 K.Hoang 25/09/2022 Fix severe bug affecting time between the starts
1.3.1 K.Hoang 29/09/2022 Using float instead of ulong for interval
*****************************************************************************************************************************/
#pragma once
+26 -16
View File
@@ -25,7 +25,7 @@
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Version: 1.2.0
Version: 1.3.1
Version Modified By Date Comments
------- ----------- ---------- -----------
@@ -33,7 +33,9 @@
1.0.1 K Hoang 18/05/2021 Update README and Packages' Patches to match core arduino-pico core v1.4.0
1.1.0 K Hoang 10/00/2021 Add support to new boards using the arduino-pico core
1.1.1 K Hoang 22/10/2021 Fix platform in library.json for PIO
1.2.0 K.Hoang 21/01/2022 Fix `multiple-definitions` linker error.
1.2.0 K.Hoang 21/01/2022 Fix `multiple-definitions` linker error
1.3.0 K.Hoang 25/09/2022 Fix severe bug affecting time between the starts
1.3.1 K.Hoang 29/09/2022 Using float instead of ulong for interval
*****************************************************************************************************************************/
#pragma once
@@ -41,6 +43,8 @@
#ifndef ISR_TIMER_GENERIC_HPP
#define ISR_TIMER_GENERIC_HPP
////////////////////////////////////////////////////////////////
#if ( defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_RASPBERRY_PI_PICO) || defined(ARDUINO_ADAFRUIT_FEATHER_RP2040) || defined(ARDUINO_GENERIC_RP2040) ) && !defined(ARDUINO_ARCH_MBED)
#if defined(USING_RPI_PICO_TIMER_INTERRUPT)
#undef USING_RPI_PICO_TIMER_INTERRUPT
@@ -50,16 +54,20 @@
#error This code is intended to run on the non-mbed RP2040 arduino-pico platform! Please check your Tools->Board setting.
#endif
////////////////////////////////////////////////////////////////
#ifndef RPI_PICO_TIMER_INTERRUPT_VERSION
#define RPI_PICO_TIMER_INTERRUPT_VERSION "RPi_Pico_TimerInterrupt v1.2.0"
#define RPI_PICO_TIMER_INTERRUPT_VERSION "RPi_Pico_TimerInterrupt v1.3.1"
#define RPI_PICO_TIMER_INTERRUPT_VERSION_MAJOR 1
#define RPI_PICO_TIMER_INTERRUPT_VERSION_MINOR 2
#define RPI_PICO_TIMER_INTERRUPT_VERSION_PATCH 0
#define RPI_PICO_TIMER_INTERRUPT_VERSION_MINOR 3
#define RPI_PICO_TIMER_INTERRUPT_VERSION_PATCH 1
#define RPI_PICO_TIMER_INTERRUPT_VERSION_INT 1002000
#define RPI_PICO_TIMER_INTERRUPT_VERSION_INT 1003001
#endif
////////////////////////////////////////////////////////////////
#include "TimerInterrupt_Generic_Debug.h"
#include <stddef.h>
@@ -83,9 +91,10 @@
typedef void (*timer_callback)();
typedef void (*timer_callback_p)(void *);
////////////////////////////////////////////////////////////////
class RPI_PICO_ISR_Timer
{
public:
// maximum number of timers
#define RPI_PICO_MAX_TIMERS 16
@@ -103,35 +112,35 @@ class RPI_PICO_ISR_Timer
// Timer will call function 'f' every 'd' milliseconds forever
// returns the timer number (numTimer) on success or
// -1 on failure (f == NULL) or no free timers
int setInterval(const unsigned long& d, timer_callback f);
int setInterval(const float& d, timer_callback f);
// Timer will call function 'f' with parameter 'p' every 'd' milliseconds forever
// returns the timer number (numTimer) on success or
// -1 on failure (f == NULL) or no free timers
int setInterval(const unsigned long& d, timer_callback_p f, void* p);
int setInterval(const float& d, timer_callback_p f, void* p);
// Timer will call function 'f' after 'd' milliseconds one time
// returns the timer number (numTimer) on success or
// -1 on failure (f == NULL) or no free timers
int setTimeout(const unsigned long& d, timer_callback f);
int setTimeout(const float& d, timer_callback f);
// Timer will call function 'f' with parameter 'p' after 'd' milliseconds one time
// returns the timer number (numTimer) on success or
// -1 on failure (f == NULL) or no free timers
int setTimeout(const unsigned long& d, timer_callback_p f, void* p);
int setTimeout(const float& d, timer_callback_p f, void* p);
// Timer will call function 'f' every 'd' milliseconds 'n' times
// returns the timer number (numTimer) on success or
// -1 on failure (f == NULL) or no free timers
int setTimer(const unsigned long& d, timer_callback f, const unsigned& n);
int setTimer(const float& d, timer_callback f, const unsigned& n);
// Timer will call function 'f' with parameter 'p' every 'd' milliseconds 'n' times
// returns the timer number (numTimer) on success or
// -1 on failure (f == NULL) or no free timers
int setTimer(const unsigned long& d, timer_callback_p f, void* p, const unsigned& n);
int setTimer(const float& d, timer_callback_p f, void* p, const unsigned& n);
// updates interval of the specified timer
bool changeInterval(const unsigned& numTimer, const unsigned long& d);
bool changeInterval(const unsigned& numTimer, const float& d);
// destroy the specified timer
void deleteTimer(const unsigned& numTimer);
@@ -175,7 +184,7 @@ class RPI_PICO_ISR_Timer
// low level function to initialize and enable a new timer
// returns the timer number (numTimer) on success or
// -1 on failure (f == NULL) or no free timers
int setupTimer(const unsigned long& d, void* f, void* p, bool h, const unsigned& n);
int setupTimer(const float& d, void* f, void* p, bool h, const unsigned& n);
// find the first available slot
int findFirstFreeSlot();
@@ -186,7 +195,7 @@ class RPI_PICO_ISR_Timer
void* callback; // pointer to the callback function
void* param; // function parameter
bool hasParam; // true if callback takes a parameter
unsigned long delay; // delay value
float delay; // delay value
unsigned maxNumRuns; // number of runs to be executed
unsigned numRuns; // number of executed runs
bool enabled; // true if enabled
@@ -199,6 +208,7 @@ class RPI_PICO_ISR_Timer
volatile int numTimers;
};
////////////////////////////////////////////////////////////////
#endif // ISR_TIMER_GENERIC_HPP
+61 -17
View File
@@ -26,7 +26,7 @@
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Version: 1.2.0
Version: 1.3.1
Version Modified By Date Comments
------- ----------- ---------- -----------
@@ -34,7 +34,9 @@
1.0.1 K Hoang 18/05/2021 Update README and Packages' Patches to match core arduino-pico core v1.4.0
1.1.0 K Hoang 10/00/2021 Add support to new boards using the arduino-pico core
1.1.1 K Hoang 22/10/2021 Fix platform in library.json for PIO
1.2.0 K.Hoang 21/01/2022 Fix `multiple-definitions` linker error.
1.2.0 K.Hoang 21/01/2022 Fix `multiple-definitions` linker error
1.3.0 K.Hoang 25/09/2022 Fix severe bug affecting time between the starts
1.3.1 K.Hoang 29/09/2022 Using float instead of ulong for interval
*****************************************************************************************************************************/
#pragma once
@@ -51,20 +53,26 @@
#error This code is intended to run on the non-mbed RP2040 arduino-pico platform! Please check your Tools->Board setting.
#endif
////////////////////////////////////////////////////////////////
#ifndef RPI_PICO_TIMER_INTERRUPT_VERSION
#define RPI_PICO_TIMER_INTERRUPT_VERSION "RPi_Pico_TimerInterrupt v1.2.0"
#define RPI_PICO_TIMER_INTERRUPT_VERSION "RPi_Pico_TimerInterrupt v1.3.1"
#define RPI_PICO_TIMER_INTERRUPT_VERSION_MAJOR 1
#define RPI_PICO_TIMER_INTERRUPT_VERSION_MINOR 2
#define RPI_PICO_TIMER_INTERRUPT_VERSION_PATCH 0
#define RPI_PICO_TIMER_INTERRUPT_VERSION_MINOR 3
#define RPI_PICO_TIMER_INTERRUPT_VERSION_PATCH 1
#define RPI_PICO_TIMER_INTERRUPT_VERSION_INT 1002000
#define RPI_PICO_TIMER_INTERRUPT_VERSION_INT 1003001
#endif
////////////////////////////////////////////////////////////////
#ifndef TIMER_INTERRUPT_DEBUG
#define TIMER_INTERRUPT_DEBUG 0
#endif
////////////////////////////////////////////////////////////////
#include "Arduino.h"
#include <stdio.h>
@@ -74,6 +82,8 @@
#include "TimerInterrupt_Generic_Debug.h"
////////////////////////////////////////////////////////////////
/*
To enable an alarm:
• Enable the interrupt at the timer with a write to the appropriate alarm bit in INTE: i.e. (1 << 0) for ALARM0
@@ -94,6 +104,7 @@ typedef RPI_PICO_TimerInterrupt RPI_PICO_Timer;
typedef bool (*pico_timer_callback) (struct repeating_timer *t);
////////////////////////////////////////////////////////////////
class RPI_PICO_TimerInterrupt
{
@@ -103,7 +114,7 @@ class RPI_PICO_TimerInterrupt
pico_timer_callback _callback; // pointer to the callback function
float _frequency; // Timer frequency
uint64_t _timerCount; // count to activate timer, in us
int64_t _timerCount; // count to activate timer, in us
struct repeating_timer _timer;
@@ -114,6 +125,8 @@ class RPI_PICO_TimerInterrupt
_timerNo = timerNo;
_callback = NULL;
};
////////////////////////////////////////////////////////////////
#define TIM_CLOCK_FREQ ( (float) 1000000.0f )
@@ -133,7 +146,7 @@ class RPI_PICO_TimerInterrupt
// select timer frequency is 1MHz for better accuracy. We don't use 16-bit prescaler for now.
// Will use later if very low frequency is needed.
_frequency = frequency;
_timerCount = (uint64_t) TIM_CLOCK_FREQ / frequency;
_timerCount = (int64_t) (TIM_CLOCK_FREQ / frequency);
TISR_LOGWARN5(F("_timerNo = "), _timerNo, F(", Clock (Hz) = "), TIM_CLOCK_FREQ, F(", _fre (Hz) = "), _frequency);
TISR_LOGWARN3(F("_count = "), (uint32_t) (_timerCount >> 32) , F("-"), (uint32_t) (_timerCount));
@@ -143,10 +156,18 @@ class RPI_PICO_TimerInterrupt
// static bool add_repeating_timer_us(int64_t delay_us, repeating_timer_callback_t callback, void *user_data, repeating_timer_t *out);
// static bool add_repeating_timer_ms(int64_t delay_ms, repeating_timer_callback_t callback, void *user_data, repeating_timer_t *out);
// bool cancel_repeating_timer (repeating_timer_t *timer);
//////////////////////////////////////////////////////////////////////////
// Important Notes
// delay_ms the repeat delay in milliseconds; if >0 then this is the delay between one callback ending and the next
// starting; if <0 then this is the negative of the time between the starts of the callbacks.
// The value of 0 is treated as 1 microsecond
//////////////////////////////////////////////////////////////////////////
cancel_repeating_timer(&_timer);
add_repeating_timer_us(_timerCount, _callback, NULL, &_timer);
// Use negative value to select time between the starts of the callbacks
add_repeating_timer_us(-(_timerCount), _callback, NULL, &_timer);
TISR_LOGWARN1(F("add_repeating_timer_us = "), _timerCount);
TISR_LOGWARN1(F("add_repeating_timer_us between starts = "), _timerCount);
return true;
}
@@ -158,64 +179,87 @@ class RPI_PICO_TimerInterrupt
}
}
////////////////////////////////////////////////////////////////
// interval (in microseconds) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely
// No params and duration now. To be added in the future by adding similar functions here
bool setInterval(const unsigned long& interval, pico_timer_callback callback)
bool setInterval(const float& interval, pico_timer_callback callback)
{
return setFrequency((float) (1000000.0f / interval), callback);
}
////////////////////////////////////////////////////////////////
bool attachInterrupt(const float& frequency, pico_timer_callback callback)
{
return setFrequency(frequency, callback);
}
////////////////////////////////////////////////////////////////
// interval (in microseconds) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely
// No params and duration now. To be added in the future by adding similar functions here
bool attachInterruptInterval(const unsigned long& interval, pico_timer_callback callback)
bool attachInterruptInterval(const float& interval, pico_timer_callback callback)
{
return setFrequency( (float) ( 1000000.0f / interval), callback);
}
////////////////////////////////////////////////////////////////
void detachInterrupt()
{
cancel_repeating_timer(&_timer);
}
////////////////////////////////////////////////////////////////
void disableTimer()
{
cancel_repeating_timer(&_timer);
}
// Duration (in milliseconds). Duration = 0 or not specified => run indefinitely
////////////////////////////////////////////////////////////////
// Duration (in microseconds). Duration = 0 or not specified => run indefinitely
void reattachInterrupt()
{
add_repeating_timer_us(_timerCount, _callback, NULL, &_timer);
add_repeating_timer_us(-(_timerCount), _callback, NULL, &_timer);
}
// Duration (in milliseconds). Duration = 0 or not specified => run indefinitely
////////////////////////////////////////////////////////////////
// Duration (in microseconds). Duration = 0 or not specified => run indefinitely
void enableTimer()
{
add_repeating_timer_us(_timerCount, _callback, NULL, &_timer);
add_repeating_timer_us(-(_timerCount), _callback, NULL, &_timer);
}
////////////////////////////////////////////////////////////////
// Just stop clock source, clear the count
void stopTimer()
{
cancel_repeating_timer(&_timer);
}
////////////////////////////////////////////////////////////////
// Just reconnect clock source, start current count from 0
void restartTimer()
{
cancel_repeating_timer(&_timer);
add_repeating_timer_us(_timerCount, _callback, NULL, &_timer);
add_repeating_timer_us(-(_timerCount), _callback, NULL, &_timer);
}
////////////////////////////////////////////////////////////////
int8_t getTimer() __attribute__((always_inline))
{
return _timerNo;
};
////////////////////////////////////////////////////////////////
}; // class RPI_PICO_TimerInterrupt
#endif // RPI_PICO_TIMERINTERRUPT_H
+8 -2
View File
@@ -25,7 +25,7 @@
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Version: 1.2.0
Version: 1.3.1
Version Modified By Date Comments
------- ----------- ---------- -----------
@@ -33,7 +33,9 @@
1.0.1 K Hoang 18/05/2021 Update README and Packages' Patches to match core arduino-pico core v1.4.0
1.1.0 K Hoang 10/00/2021 Add support to new boards using the arduino-pico core
1.1.1 K Hoang 22/10/2021 Fix platform in library.json for PIO
1.2.0 K.Hoang 21/01/2022 Fix `multiple-definitions` linker error.
1.2.0 K.Hoang 21/01/2022 Fix `multiple-definitions` linker error
1.3.0 K.Hoang 25/09/2022 Fix severe bug affecting time between the starts
1.3.1 K.Hoang 29/09/2022 Using float instead of ulong for interval
*****************************************************************************************************************************/
#pragma once
@@ -41,12 +43,16 @@
#ifndef TIMERINTERRUPT_GENERIC_DEBUG_H
#define TIMERINTERRUPT_GENERIC_DEBUG_H
////////////////////////////////////////////////////////////////
#ifdef TIMERINTERRUPT_DEBUG_PORT
#define TISR_DBG_PORT TIMERINTERRUPT_DEBUG_PORT
#else
#define TISR_DBG_PORT Serial
#endif
////////////////////////////////////////////////////////////////
// Change _TIMERINTERRUPT_LOGLEVEL_ to set tracing and logging verbosity
// 0: DISABLED: no logging
// 1: ERROR: errors