22 Commits
Author SHA1 Message Date
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
Khoi Hoang 090eadf7ba v1.2.0 to fix multiple-definitions linker error
### Releases v1.2.0

1. Fix `multiple-definitions` linker error. Drop `src_cpp` and `src_h` directories
2. Add example [multiFileProject](examples/multiFileProject) to demo for multiple-file project.
3. Optimize library code by using `reference-passing` instead of `value-passing`
4. Update all examples
2022-01-22 00:37:49 -05:00
Khoi Hoang 08b28e29c3 v1.2.0 to fix `multiple-definitions linker error
### Releases v1.2.0

1. Fix `multiple-definitions` linker error. Drop `src_cpp` and `src_h` directories
2. Add example [multiFileProject](examples/multiFileProject) to demo for multiple-file project.
3. Optimize library code by using `reference-passing` instead of `value-passing`
4. Update all examples
2022-01-22 00:35:04 -05:00
Khoi Hoang 44c3602009 v1.2.0 to fix multiple-definitions linker error
### Releases v1.2.0

1. Fix `multiple-definitions` linker error. Drop `src_cpp` and `src_h` directories
2. Add example [multiFileProject](examples/multiFileProject) to demo for multiple-file project.
3. Optimize library code by using `reference-passing` instead of `value-passing`
4. Update all examples
2022-01-22 00:32:59 -05:00
Khoi Hoang 8b4f5220e6 Delete TimerInterrupt_Generic_Debug.h 2022-01-22 00:31:39 -05:00
Khoi Hoang 3c0d65a3ad Delete RPi_Pico_TimerInterrupt.h 2022-01-22 00:31:35 -05:00
Khoi Hoang 3d0e9831b3 Delete RPi_Pico_ISR_Timer.h 2022-01-22 00:31:31 -05:00
Khoi Hoang 7f5d92deab Delete RPi_Pico_ISR_Timer-Impl.h 2022-01-22 00:31:27 -05:00
Khoi Hoang 8fab85d670 Delete TimerInterrupt_Generic_Debug.h 2022-01-22 00:31:20 -05:00
Khoi Hoang e764e8fb1a Delete RPi_Pico_TimerInterrupt.h 2022-01-22 00:31:16 -05:00
Khoi Hoang 0747f36f22 Delete RPi_Pico_ISR_Timer.h 2022-01-22 00:31:11 -05:00
Khoi Hoang 05c0eef7cd Delete RPi_Pico_ISR_Timer.cpp 2022-01-22 00:31:05 -05:00
Khoi Hoang 891bf52cb5 v1.1.1 to fix platform
### Releases v1.1.1

1. Fix platform in `library.json`
2021-10-22 17:04:19 -04:00
Khoi Hoang d897d409ff v1.1.1 to fix platform
### Releases v1.1.1

1. Fix platform in `library.json`
2021-10-22 17:03:29 -04:00
Khoi Hoang 151c3792fc v1.1.1 to fix platform
### Releases v1.1.1

1. Fix platform in `library.json`
2021-10-22 17:02:41 -04:00
Khoi Hoang 895091a6d8 Add strict option for PIO
; Checks for the compatibility with frameworks and dev/platforms
lib_compat_mode = strict
2021-10-04 19:23:20 -04:00
Khoi Hoang 9670667e07 Update README.md 2021-08-12 16:37:24 -04:00
Khoi Hoang c1026db079 Add files via upload 2021-07-30 15:54:13 -04:00
Khoi Hoang 6d2412afc5 Create auto-github-actions.yml 2021-07-19 20:13:44 -04:00
34 changed files with 897 additions and 2266 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-46-generic #49~20.04.1-Ubuntu SMP Thu Aug 4 19:15:44 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. ...
+31
View File
@@ -0,0 +1,31 @@
# Configuration for probot-stale - https://github.com/probot/stale
daysUntilStale: 60
daysUntilClose: 14
limitPerRun: 30
staleLabel: stale
exemptLabels:
- pinned
- security
- "to be implemented"
- "for reference"
- "move to PR"
- "enhancement"
only: issues
onlyLabels: []
exemptProjects: false
exemptMilestones: false
exemptAssignees: false
markComment: >
[STALE_SET] This issue has been automatically marked as stale because it has not had
recent activity. It will be closed in 14 days if no further activity occurs. Thank you
for your contributions.
unmarkComment: >
[STALE_CLR] This issue has been removed from the stale queue. Please ensure activity to keep it openin the future.
closeComment: >
[STALE_DEL] This stale issue has been automatically closed. Thank you for your contributions.
+12
View File
@@ -0,0 +1,12 @@
name: auto-github-actions
on: [push]
jobs:
check-bats-version:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '14'
- run: npm install -g bats
- run: bats -v
+8 -7
View File
@@ -14,9 +14,9 @@ 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.15) or Platform.io version
* `RP2040` Core Version (e.g. RP2040 core v1.8.1)
* `RP2040` Board type (e.g. RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040, GENERIC_RP2040, etc.)
* 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:
@@ -27,14 +27,14 @@ Please ensure to specify the following:
### Example
```
Arduino IDE version: 1.8.15
RP2040 core v1.8.1
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-74-generic #83-Ubuntu SMP Sat May 8 02:35:39 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
Linux xy-Inspiron-3593 5.15.0-46-generic #49~20.04.1-Ubuntu SMP Thu Aug 4 19:15:44 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!
+83 -242
View File
@@ -2,24 +2,25 @@
[![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>
---
---
## Table of Contents
* [Important Change from v1.2.0](#Important-Change-from-v120)
* [Why do we need this RPI_PICO_TimerInterrupt library](#why-do-we-need-this-rpi_pico_timerinterrupt-library)
* [Features](#features)
* [Why using ISR-based Hardware Timer Interrupt is better](#why-using-isr-based-hardware-timer-interrupt-is-better)
* [Currently supported Boards](#currently-supported-boards)
* [Important Notes about ISR](#important-notes-about-isr)
* [Changelog](#changelog)
* [Releases v1.1.0](#releases-v110)
* [Releases v1.0.1](#releases-v101)
* [Initial Releases v1.0.0](#initial-releases-v100)
* [Changelog](changelog.md)
* [Prerequisites](#prerequisites)
* [Installation](#installation)
* [Use Arduino Library Manager](#use-arduino-library-manager)
@@ -50,6 +51,7 @@
* [ 7. RPM_Measure](examples/RPM_Measure)
* [ 8. SwitchDebounce](examples/SwitchDebounce)
* [ 9. TimerInterruptTest](examples/TimerInterruptTest)
* [ 10. **multiFileProject**](examples/multiFileProject) **New**
* [Example ISR_Timers_Array_Simple](#example-isr_timers_array_simple)
* [Debug Terminal Output Samples](#debug-terminal-output-samples)
* [1. ISR_Timers_Array_Simple on RASPBERRY_PI_PICO](#1-isr_timers_array_simple-on-raspberry_pi_pico)
@@ -60,7 +62,6 @@
* [6. ISR_16_Timers_Array_Complex on ADAFRUIT_ITSYBITSY_RP2040](#6-isr_16_timers_array_complex-on-adafruit_itsybitsy_rp2040)
* [Debug](#debug)
* [Troubleshooting](#troubleshooting)
* [Releases](#releases)
* [Issues](#issues)
* [TO DO](#to-do)
* [DONE](#done)
@@ -72,6 +73,12 @@
---
---
### Important Change from v1.2.0
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
@@ -115,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 and 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)
---
@@ -129,31 +136,15 @@ The catch is **your function is now part of an ISR (Interrupt Service Routine),
---
---
## Changelog
### Releases v1.1.0
1. Add support to new boards (**ADAFRUIT_ITSYBITSY_RP2040, ADAFRUIT_QTPY_RP2040, ADAFRUIT_STEMMAFRIEND_RP2040, ADAFRUIT_TRINKEYQT_RP2040, ADAFRUIT_MACROPAD_RP2040, SPARKFUN_PROMICRO_RP2040, etc.**) using the arduino-pico core
2. Add `ISR_16_Timers_Array_Complex` examples.
3. Fix examples' bug
### Releases v1.0.1
1. Update README and Packages' Patches to match core arduino-pico core v1.4.0
### Initial Releases v1.0.0
1. Initial coding to support RP2040-based boards such as **RASPBERRY_PI_PICO**, etc. using [**Earle Philhower's arduino-pico** core](https://github.com/earlephilhower/arduino-pico)
---
---
## Prerequisites
1. [`Arduino IDE 1.8.15+` for Arduino](https://www.arduino.cc/en/Main/Software)
2. [`Earle Philhower's arduino-pico core v1.8.1+`](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)
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 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.
---
---
@@ -169,16 +160,16 @@ 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)
4. Use included [platformio.ini](platformio/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](https://docs.platformio.org/page/projectconf.html)
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)
---
@@ -225,24 +216,26 @@ With core after v1.4.0, this step is not necessary anymore thanks to the PR [Add
### 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. Although it's simple to just modify several lines of code, either in the library or in the application, the library is adding 2 more source directories
The current library implementation, using `xyz-Impl.h` instead of standard `xyz.cpp`, possibly creates certain `Multiple Definitions` Linker error in certain use cases.
1. **scr_h** for new h-only files
2. **src_cpp** for standard h/cpp files
You can include these `.hpp` or `.h` files
besides the standard **src** directory.
```
// 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
To use the **old standard cpp** way, locate this library' directory, then just
// Can be included as many times as necessary, without `Multiple Definitions` Linker Error
#include "RPi_Pico_ISR_Timer.hpp" //https://github.com/khoih-prog/RPI_PICO_TimerInterrupt
```
1. **Delete the all the files in src directory.**
2. **Copy all the files in src_cpp directory into src.**
3. Close then reopen the application code in Arduino IDE, etc. to recompile from scratch.
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
To re-use the **new h-only** way, just
```
// 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
```
1. **Delete the all the files in src directory.**
2. **Copy the files in src_h directory into src.**
3. Close then reopen the application code in Arduino IDE, etc. to recompile from scratch.
Check the new [**multiFileProject** example](examples/multiFileProject) for a `HOWTO` demo.
---
---
@@ -448,154 +441,15 @@ void setup()
7. [RPM_Measure](examples/RPM_Measure)
8. [SwitchDebounce](examples/SwitchDebounce)
9. [TimerInterruptTest](examples/TimerInterruptTest)
10. [**multiFileProject**](examples/multiFileProject). **New**
---
---
### 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/f2ca9ced6ef985653cb234a3268b237b0fa257f1/examples/ISR_Timers_Array_Simple/ISR_Timers_Array_Simple.ino#L23-L166
#include "RPi_Pico_TimerInterrupt.h"
#include "RPi_Pico_ISR_Timer.h"
#include <SimpleTimer.h> // https://github.com/schinken/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"));
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();
}
```
---
---
@@ -608,15 +462,12 @@ 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.1.0
RPi_Pico_TimerInterrupt v1.3.0
CPU Frequency = 125 MHz
[TISR] RPI_PICO_TimerInterrupt: _timerNo = 1 , _fre = 1000000.00
[TISR] _count = 0 - 1000
[TISR] add_repeating_timer_us = 1000
Starting ITimer1 OK, millis() = 1707
[TISR] RPI_PICO_TimerInterrupt: _timerNo = 3 , _fre = 1000000.00
[TISR] _count = 0 - 1000
[TISR] _timerNo = 1, Clock (Hz) = 1000000.00, _fre (Hz) = 1000.00
[TISR] _count = 0-1000
[TISR] add_repeating_timer_us = 1000
Starting ITimer3 OK, millis() = 1707
SimpleTimer : programmed 2000ms, current time ms : 11707, Delta ms : 11707
@@ -638,15 +489,15 @@ The following is the sample terminal output when running example [TimerInterrupt
```
Starting TimerInterruptTest on RASPBERRY_PI_PICO
RPi_Pico_TimerInterrupt v1.1.0
RPi_Pico_TimerInterrupt v1.3.0
CPU Frequency = 125 MHz
[TISR] RPI_PICO_TimerInterrupt: _timerNo = 0 , _fre = 1000000.00
[TISR] _count = 0 - 1000000
[TISR] timer_set_alarm_value (us) = 1000000
Starting ITimer0 OK, millis() = 1781
[TISR] RPI_PICO_TimerInterrupt: _timerNo = 0 , _fre = 1000000.00
[TISR] _count = 0 - 3000000
[TISR] timer_set_alarm_value (us) = 3000000
[TISR] _timerNo = 0, Clock (Hz) = 1000000.00, _fre (Hz) = 1.00
[TISR] _count = 0-1000000
[TISR] add_repeating_timer_us = 1000000
Starting ITimer0 OK, millis() = 882
[TISR] _timerNo = 1, Clock (Hz) = 1000000.00, _fre (Hz) = 0.33
[TISR] _count = 0-3000000
[TISR] add_repeating_timer_us = 3000000
Starting ITimer1 OK, millis() = 1782
ITimer0 called, millis() = 2781
ITimer0 called, millis() = 3781
@@ -682,14 +533,14 @@ The following is the sample terminal output when running example [Change_Interva
```
Starting Change_Interval on RASPBERRY_PI_PICO
RPi_Pico_TimerInterrupt v1.1.0
RPi_Pico_TimerInterrupt v1.3.0
CPU Frequency = 125 MHz
[TISR] RPI_PICO_TimerInterrupt: _timerNo = 0 , _fre = 1000000.00
[TISR] _count = 0 - 2000000
[TISR] _timerNo = 0, Clock (Hz) = 1000000.00, _fre (Hz) = 0.50
[TISR] _count = 0-2000000
[TISR] add_repeating_timer_us = 2000000
Starting ITimer0 OK, millis() = 1544
[TISR] RPI_PICO_TimerInterrupt: _timerNo = 1 , _fre = 1000000.00
[TISR] _count = 0 - 5000000
Starting ITimer0 OK, millis() = 2363
[TISR] _timerNo = 1, Clock (Hz) = 1000000.00, _fre (Hz) = 0.20
[TISR] _count = 0-5000000
[TISR] add_repeating_timer_us = 5000000
Starting ITimer1 OK, millis() = 1544
ITimer0: millis() = 3544
@@ -763,10 +614,10 @@ The following is the sample terminal output when running example [SwitchDebounce
```
Starting SwitchDebounce on RASPBERRY_PI_PICO
RPi_Pico_TimerInterrupt v1.1.0
RPi_Pico_TimerInterrupt v1.3.0
CPU Frequency = 125 MHz
[TISR] RPI_PICO_TimerInterrupt: _timerNo = 0 , _fre = 1000000.00
[TISR] _count = 0 - 20000
[TISR] _timerNo = 1, Clock (Hz) = 1000000.00, _fre (Hz) = 50.00
[TISR] _count = 0-20000
[TISR] add_repeating_timer_us = 20000
Starting ITimer1 OK, millis() = 1302
SW Press, from millis() = 77377
@@ -787,12 +638,11 @@ SW Pressed total time ms = 1181
The following is the sample terminal output when running example [ISR_Timers_Array_Simple](examples/ISR_Timers_Array_Simple) on ADAFRUIT_FEATHER_RP2040
```
Starting ISR_Timers_Array_Simple on ADAFRUIT_FEATHER_RP2040
RPi_Pico_TimerInterrupt v1.1.0
Starting ISR_Timers_Array_Simple on RASPBERRY_PI_PICO
RPi_Pico_TimerInterrupt v1.3.0
CPU Frequency = 125 MHz
[TISR] RPI_PICO_TimerInterrupt: _timerNo = 1 , _fre = 1000000.00
[TISR] _count = 0 - 1000
[TISR] _timerNo = 1, Clock (Hz) = 1000000.00, _fre (Hz) = 1000.00
[TISR] _count = 0-1000
[TISR] add_repeating_timer_us = 1000
Starting ITimer1 OK, millis() = 1701
SimpleTimer : programmed 2000ms, current time ms : 11707, Delta ms : 11707
@@ -811,12 +661,11 @@ Timer5s actual : 5000
The following is the sample terminal output when running example [ISR_16_Timers_Array_Complex](examples/ISR_16_Timers_Array_Complex) on ADAFRUIT_ITSYBITSY_RP2040
```
Starting ISR_16_Timers_Array_Complex on ADAFRUIT_ITSYBITSY_RP2040
RPi_Pico_TimerInterrupt v1.1.0
Starting ISR_16_Timers_Array_Complex on RASPBERRY_PI_PICO
RPi_Pico_TimerInterrupt v1.3.0
CPU Frequency = 125 MHz
[TISR] RPI_PICO_TimerInterrupt: _timerNo = 1 , _fre = 1000000.00
[TISR] _count = 0 - 10000
[TISR] _timerNo = 1, Clock (Hz) = 1000000.00, _fre (Hz) = 100.00
[TISR] _count = 0-10000
[TISR] add_repeating_timer_us = 10000
Starting ITimer OK, millis() = 1797
SimpleTimer : 2, ms : 11798, Dms : 10001
@@ -983,25 +832,6 @@ If you get compilation errors, more often than not, you may need to install a ne
Sometimes, the library will only work if you update the board core to the latest version because I am using newly added functions.
---
---
## Releases
### Releases v1.1.0
1. Add support to new boards (**ADAFRUIT_ITSYBITSY_RP2040, ADAFRUIT_QTPY_RP2040, ADAFRUIT_STEMMAFRIEND_RP2040, ADAFRUIT_TRINKEYQT_RP2040, ADAFRUIT_MACROPAD_RP2040, SPARKFUN_PROMICRO_RP2040, etc.**) using the arduino-pico core
2. Add `ISR_16_Timers_Array_Complex` examples.
3. Fix examples' bug
### Releases v1.0.1
1. Update README and Packages' Patches to match core arduino-pico core v1.4.0
### Initial Releases v1.0.0
1. Initial coding to support **RP2040-based boards such as RASPBERRY_PI_PICO**, etc. using [**Earle Philhower's arduino-pico** core](https://github.com/earlephilhower/arduino-pico)
---
---
@@ -1025,7 +855,10 @@ Submit issues to: [RPI_PICO_TimerInterrupt issues](https://github.com/khoih-prog
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, etc.**) using the arduino-pico core
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)
---
---
@@ -1034,6 +867,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>
---
@@ -1050,7 +891,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)
---
+59
View File
@@ -0,0 +1,59 @@
# RPI_PICO_TimerInterrupt Library
[![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)
[![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)
---
---
## Table of Contents
* [Changelog](#changelog)
* [Releases v1.3.0](#releases-v130)
* [Releases v1.2.0](#releases-v120)
* [Releases v1.1.1](#releases-v111)
* [Releases v1.1.0](#releases-v110)
* [Releases v1.0.1](#releases-v101)
* [Initial Releases v1.0.0](#initial-releases-v100)
---
---
## Changelog
### 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
2. Add example [multiFileProject](examples/multiFileProject) to demo for multiple-file project.
3. Optimize library code by using `reference-passing` instead of `value-passing`
4. Update all examples
### Releases v1.1.1
1. Fix platform in `library.json`
### Releases v1.1.0
1. Add support to new boards (**ADAFRUIT_ITSYBITSY_RP2040, ADAFRUIT_QTPY_RP2040, ADAFRUIT_STEMMAFRIEND_RP2040, ADAFRUIT_TRINKEYQT_RP2040, ADAFRUIT_MACROPAD_RP2040, SPARKFUN_PROMICRO_RP2040, etc.**) using the arduino-pico core
2. Add `ISR_16_Timers_Array_Complex` examples.
3. Fix examples' bug
### Releases v1.0.1
1. Update README and Packages' Patches to match core arduino-pico core v1.4.0
### Initial Releases v1.0.0
1. Initial coding to support RP2040-based boards such as **RASPBERRY_PI_PICO**, etc. using [**Earle Philhower's arduino-pico** core](https://github.com/earlephilhower/arduino-pico)
+1 -15
View File
@@ -18,21 +18,6 @@
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.
Based on SimpleTimer - A timer library for Arduino.
Author: mromani@ottotecnica.com
Copyright (c) 2010 OTTOTECNICA Italy
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Version: 1.1.0
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 11/05/2021 Initial coding to support RP2040-based boards such as RASPBERRY_PI_PICO. etc.
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
*****************************************************************************************************************************/
// These define's must be placed at the beginning before #include "TimerInterrupt_Generic.h"
@@ -41,6 +26,7 @@
#define TIMER_INTERRUPT_DEBUG 1
#define _TIMERINTERRUPT_LOGLEVEL_ 4
// Can be included as many times as necessary, without `Multiple Definitions` Linker Error
#include "RPi_Pico_TimerInterrupt.h"
#if !defined(LED_BUILTIN)
+2 -16
View File
@@ -18,21 +18,6 @@
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.
Based on SimpleTimer - A timer library for Arduino.
Author: mromani@ottotecnica.com
Copyright (c) 2010 OTTOTECNICA Italy
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Version: 1.1.0
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 11/05/2021 Initial coding to support RP2040-based boards such as RASPBERRY_PI_PICO. etc.
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
*****************************************************************************************************************************/
/*
@@ -54,10 +39,11 @@
#define TIMER_INTERRUPT_DEBUG 1
#define _TIMERINTERRUPT_LOGLEVEL_ 4
// Can be included as many times as necessary, without `Multiple Definitions` Linker Error
#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
+1 -15
View File
@@ -18,21 +18,6 @@
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.
Based on SimpleTimer - A timer library for Arduino.
Author: mromani@ottotecnica.com
Copyright (c) 2010 OTTOTECNICA Italy
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Version: 1.1.0
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 11/05/2021 Initial coding to support RP2040-based boards such as RASPBERRY_PI_PICO. etc.
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
*****************************************************************************************************************************/
// These define's must be placed at the beginning before #include "TimerInterrupt_Generic.h"
@@ -41,6 +26,7 @@
#define TIMER_INTERRUPT_DEBUG 1
#define _TIMERINTERRUPT_LOGLEVEL_ 4
// Can be included as many times as necessary, without `Multiple Definitions` Linker Error
#include "RPi_Pico_TimerInterrupt.h"
#if !defined(LED_BUILTIN)
+2 -16
View File
@@ -18,21 +18,6 @@
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.
Based on SimpleTimer - A timer library for Arduino.
Author: mromani@ottotecnica.com
Copyright (c) 2010 OTTOTECNICA Italy
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Version: 1.1.0
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 11/05/2021 Initial coding to support RP2040-based boards such as RASPBERRY_PI_PICO. etc.
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
*****************************************************************************************************************************/
/*
@@ -53,10 +38,11 @@
#define TIMER_INTERRUPT_DEBUG 1
#define _TIMERINTERRUPT_LOGLEVEL_ 4
// Can be included as many times as necessary, without `Multiple Definitions` Linker Error
#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
@@ -18,21 +18,6 @@
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.
Based on SimpleTimer - A timer library for Arduino.
Author: mromani@ottotecnica.com
Copyright (c) 2010 OTTOTECNICA Italy
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Version: 1.1.0
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 11/05/2021 Initial coding to support RP2040-based boards such as RASPBERRY_PI_PICO. etc.
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
*****************************************************************************************************************************/
/*
Notes:
@@ -58,7 +43,10 @@
// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
#define _TIMERINTERRUPT_LOGLEVEL_ 4
// 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
@@ -79,7 +67,6 @@
volatile uint32_t startMillis = 0;
// You can select RPI_PICO_Timer from 0 to 3
// Init RPI_PICO_Timer
RPI_PICO_Timer ITimer(1);
@@ -18,21 +18,6 @@
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.
Based on SimpleTimer - A timer library for Arduino.
Author: mromani@ottotecnica.com
Copyright (c) 2010 OTTOTECNICA Italy
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Version: 1.1.0
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 11/05/2021 Initial coding to support RP2040-based boards such as RASPBERRY_PI_PICO. etc.
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
*****************************************************************************************************************************/
// These define's must be placed at the beginning before #include "TimerInterrupt_Generic.h"
@@ -41,10 +26,13 @@
#define TIMER_INTERRUPT_DEBUG 1
#define _TIMERINTERRUPT_LOGLEVEL_ 4
// 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/schinken/SimpleTimer
#include <SimpleTimer.h> // https://github.com/jfturcot/SimpleTimer
// Init RPI_PICO_Timer
RPI_PICO_Timer ITimer1(1);
+1 -15
View File
@@ -18,21 +18,6 @@
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.
Based on SimpleTimer - A timer library for Arduino.
Author: mromani@ottotecnica.com
Copyright (c) 2010 OTTOTECNICA Italy
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Version: 1.1.0
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 11/05/2021 Initial coding to support RP2040-based boards such as RASPBERRY_PI_PICO. etc.
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
*****************************************************************************************************************************/
/*
Notes:
@@ -61,6 +46,7 @@
#define TIMER_INTERRUPT_DEBUG 1
#define _TIMERINTERRUPT_LOGLEVEL_ 4
// Can be included as many times as necessary, without `Multiple Definitions` Linker Error
#include "RPi_Pico_TimerInterrupt.h"
#define PIN_D1 1 // Pin D1 mapped to pin GPIO1 of RPI_PICO
+1 -15
View File
@@ -18,21 +18,6 @@
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.
Based on SimpleTimer - A timer library for Arduino.
Author: mromani@ottotecnica.com
Copyright (c) 2010 OTTOTECNICA Italy
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Version: 1.1.0
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 11/05/2021 Initial coding to support RP2040-based boards such as RASPBERRY_PI_PICO. etc.
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
*****************************************************************************************************************************/
/*
Notes:
@@ -58,6 +43,7 @@
#define TIMER_INTERRUPT_DEBUG 1
#define _TIMERINTERRUPT_LOGLEVEL_ 4
// Can be included as many times as necessary, without `Multiple Definitions` Linker Error
#include "RPi_Pico_TimerInterrupt.h"
#define PIN_D1 1 // Pin D1 mapped to pin GPIO1 of RPI_PICO
@@ -18,21 +18,6 @@
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.
Based on SimpleTimer - A timer library for Arduino.
Author: mromani@ottotecnica.com
Copyright (c) 2010 OTTOTECNICA Italy
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Version: 1.1.0
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 11/05/2021 Initial coding to support RP2040-based boards such as RASPBERRY_PI_PICO. etc.
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
*****************************************************************************************************************************/
/*
Notes:
@@ -52,10 +37,11 @@
#define TIMER_INTERRUPT_DEBUG 1
#define _TIMERINTERRUPT_LOGLEVEL_ 4
// Can be included as many times as necessary, without `Multiple Definitions` Linker Error
#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
@@ -0,0 +1,19 @@
/****************************************************************************************************************************
multiFileProject.cpp
RPi_Pico_ISR_Timer-Impl.h
For RP2040-based boards such as RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040.
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/RPI_PICO_TimerInterrupt
Licensed under MIT license
Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by
unsigned long miliseconds), you just consume only one SAMD timer and avoid conflicting with other cores' tasks.
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.
*****************************************************************************************************************************/
// To demo how to include files in multi-file Projects
#include "multiFileProject.h"
@@ -0,0 +1,25 @@
/****************************************************************************************************************************
multiFileProject.h
RPi_Pico_ISR_Timer-Impl.h
For RP2040-based boards such as RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040.
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/RPI_PICO_TimerInterrupt
Licensed under MIT license
Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by
unsigned long miliseconds), you just consume only one SAMD timer and avoid conflicting with other cores' tasks.
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.
*****************************************************************************************************************************/
// To demo how to include files in multi-file Projects
#pragma once
// Can be included as many times as necessary, without `Multiple Definitions` Linker Error
#include "RPi_Pico_TimerInterrupt.h"
// Can be included as many times as necessary, without `Multiple Definitions` Linker Error
#include "RPi_Pico_ISR_Timer.hpp"
@@ -0,0 +1,38 @@
/****************************************************************************************************************************
multiFileProject.ino
For Teensy boards
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/Teensy_TimerInterrupt
Licensed under MIT license
Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by
unsigned long miliseconds), you just consume only one NRF52 timer and avoid conflicting with other cores' tasks.
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.
*****************************************************************************************************************************/
// To demo how to include files in multi-file Projects
#if !( defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_RASPBERRY_PI_PICO) || defined(ARDUINO_ADAFRUIT_FEATHER_RP2040) || defined(ARDUINO_GENERIC_RP2040) ) && !defined(ARDUINO_ARCH_MBED)
#error This code is intended to run on the non-mbed RP2040 arduino-pico platform! Please check your Tools->Board setting.
#endif
#include "multiFileProject.h"
// 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"
void setup()
{
// put your setup code here, to run once:
}
void loop()
{
// put your main code here, to run repeatedly:
}
+6
View File
@@ -48,3 +48,9 @@ getNumAvailableTimers KEYWORD2
#######################################
# Constants (LITERAL1)
#######################################
RPI_PICO_TIMER_INTERRUPT_VERSION LITERAL1
RPI_PICO_TIMER_INTERRUPT_VERSION_MAJOR LITERAL1
RPI_PICO_TIMER_INTERRUPT_VERSION_MINOR LITERAL1
RPI_PICO_TIMER_INTERRUPT_VERSION_PATCH LITERAL1
RPI_PICO_TIMER_INTERRUPT_VERSION_INT LITERAL1
+6 -5
View File
@@ -1,7 +1,7 @@
{
"name": "RPI_PICO_TimerInterrupt",
"version": "1.1.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.0",
"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": {
@@ -22,8 +22,9 @@
"tests"
]
},
"license": "MIT",
"frameworks": "*",
"platforms": "rp2040",
"platforms": ["raspberrypi"],
"examples": "examples/*/*/*.ino",
"license": "MIT"
"headers": ["RPi_Pico_TimerInterrupt.h", "RPi_Pico_ISR_Timer.h", "RPi_Pico_ISR_Timer.hpp"]
}
+4 -4
View File
@@ -1,12 +1,12 @@
name=RPI_PICO_TimerInterrupt
version=1.1.0
version=1.3.0
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
paragraph=These 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.
category=Timing, Control, Device, Time, Timer
paragraph=These 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 is 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 are 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.
category=Device Control
url=https://github.com/khoih-prog/RPI_PICO_TimerInterrupt
architectures=rp2040
repository=https://github.com/khoih-prog/RPI_PICO_TimerInterrupt
license=MIT
includes=RPi_Pico_TimerInterrupt.h,RPi_Pico_ISR_Timer.h
includes=RPi_Pico_TimerInterrupt.h,RPi_Pico_ISR_Timer.h,RPi_Pico_ISR_Timer.hpp
+365
View File
@@ -0,0 +1,365 @@
;PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[platformio]
; ============================================================
; chose environment:
; ESP8266
; ESP32
; SAMD
; NRF52
; STM32
; pico
; ============================================================
;default_envs = ESP8266
;default_envs = ESP32
;default_envs = SAMD
default_envs = pico
;default_envs = NRF52
;default_envs = STM32
;default_envs = portenta_h7_m7
;default_envs = portenta_h7_m4
[env]
; ============================================================
; Serial configuration
; choose upload speed, serial-monitor speed
; ============================================================
upload_speed = 921600
;upload_port = COM11
;monitor_speed = 9600
;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 =
build_flags =
; set your debug output (default=Serial)
; -D DEBUG_ESP_PORT=Serial
; comment the folowing line to enable WiFi debugging
; -D NDEBUG
[env:portenta_h7_m7]
platform = ststm32
board = portenta_h7_m7
framework = arduino
[env:portenta_h7_m4]
platform = ststm32
board = portenta_h7_m4
framework = arduino
[env:pico]
; ============================================================
; Just a sample
; You have to research and fix if there is issue
; ============================================================
platform = raspberrypi
board = pico
framework = arduino
upload_protocol = picotool
[env:ESP8266]
platform = espressif8266
framework = arduino
; ============================================================
; Board configuration
; choose your board by uncommenting one of the following lines
; ============================================================
;board = gen4iod
;board = huzzah
;board = oak
;board = esp_wroom_02
;board = espduino
;board = espectro
;board = espino
;board = espresso_lite_v1
;board = espresso_lite_v2
;board = esp12e
;board = esp01_1m
;board = esp01
;board = esp07
;board = esp8285
;board = heltec_wifi_kit_8
;board = inventone
;board = nodemcu
board = nodemcuv2
;board = modwifi
;board = phoenix_v1
;board = phoenix_v2
;board = sparkfunBlynk
;board = thing
;board = thingdev
;board = esp210
;board = espinotee
;board = d1
;board = d1_mini
;board = d1_mini_lite
;board = d1_mini_pro
;board = wifi_slot
;board = wifiduino
;board = wifinfo
;board = wio_link
;board = wio_node
;board = xinabox_cw01
;board = esp32doit-devkit-v1
[env:ESP32]
platform = espressif32
framework = arduino
; ============================================================
; Board configuration
; choose your board by uncommenting one of the following lines
; ============================================================
;board = esp32cam
;board = alksesp32
;board = featheresp32
;board = espea32
;board = bpi-bit
;board = d-duino-32
board = esp32doit-devkit-v1
;board = pocket_32
;board = fm-devkit
;board = pico32
;board = esp32-evb
;board = esp32-gateway
;board = esp32-pro
;board = esp32-poe
;board = oroca_edubot
;board = onehorse32dev
;board = lopy
;board = lopy4
;board = wesp32
;board = esp32thing
;board = sparkfun_lora_gateway_1-channel
;board = ttgo-lora32-v1
;board = ttgo-t-beam
;board = turta_iot_node
;board = lolin_d32
;board = lolin_d32_pro
;board = lolin32
;board = wemosbat
;board = widora-air
;board = xinabox_cw02
;board = iotbusio
;board = iotbusproteus
;board = nina_w10
[env:SAMD]
platform = atmelsam
framework = arduino
; ============================================================
; Choose your board by uncommenting one of the following lines
; ============================================================
; ============================================================
; Board configuration Adafruit SAMD
; ============================================================
;board = adafruit_feather_m0
;board = adafruit_feather_m0_express
;board = adafruit_metro_m0
;board = adafruit_circuitplayground_m0
;board = adafruit_gemma_m0
;board = adafruit_trinket_m0
;board = adafruit_itsybitsy_m0
;board = adafruit_pirkey
;board = adafruit_hallowing
;board = adafruit_crickit_m0
;board = adafruit_metro_m4
;board = adafruit_grandcentral_m4
board = adafruit_itsybitsy_m4
;board = adafruit_feather_m4
;board = adafruit_trellis_m4
;board = adafruit_pyportal_m4
;board = adafruit_pyportal_m4_titano
;board = adafruit_pybadge_m4
;board = adafruit_metro_m4_airliftlite
;board = adafruit_pygamer_m4
;board = adafruit_pygamer_advance_m4
;board = adafruit_pybadge_airlift_m4
;board = adafruit_monster_m4sk
;board = adafruit_hallowing_m4
; ============================================================
; Board configuration Arduino SAMD and SAM
; ============================================================
;board = arduino_zero_edbg
;board = arduino_zero_native
;board = mkr1000
;board = mkrzero
;board = mkrwifi1010
;board = nano_33_iot
;board = mkrfox1200
;board = mkrwan1300
;board = mkrwan1310
;board = mkrgsm1400
;board = mkrnb1500
;board = mkrvidor4000
;board = adafruit_circuitplayground_m0
;board = mzero_pro_bl_dbg
;board = mzero_pro_bl
;board = mzero_bl
;board = tian
;board = tian_cons
;board = arduino_due_x_dbg
;board = arduino_due_x
; ============================================================
; Board configuration Seeeduino SAMD
; ============================================================
;board = seeed_wio_terminal
;board = Seeed_femto_m0
;board = seeed_XIAO_m0
;board = Wio_Lite_MG126
;board = WioGPS
;board = zero
;board = rolawan
;board = seeed_grove_ui_wireless
[env:NRF52]
platform = nordicnrf52
framework = arduino
; ============================================================
; Board configuration Adafruit nRF52
; choose your board by uncommenting one of the following lines
; ============================================================
;board = feather52832
board = feather52840
;board = feather52840sense
;board = itsybitsy52840
;board = cplaynrf52840
;board = cluenrf52840
;board = metro52840
;board = pca10056
;board = particle_xenon
;board = mdbt50qrx
;board = ninab302
;board = ninab112
[env:STM32]
platform = ststm32
framework = arduino
; ============================================================
; Choose your board by uncommenting one of the following lines
; ============================================================
; ============================================================
; Board configuration Nucleo-144
; ============================================================
;board = nucleo_f207zg
;board = nucleo_f429zi
;board = nucleo_f746zg
;board = nucleo_f756zg
;board = nucleo_f767zi
;board = nucleo_h743zi
;board = nucleo_l496zg
;board = nucleo_l496zg-p
;board = nucleo_l4r5zi
;board = nucleo_l4r5zi-p
; ============================================================
; Board configuration Nucleo-64
; ============================================================
;board = nucleo_f030r8
;board = nucleo_f072rb
;board = nucleo_f091rc
;board = nucleo_f103rb
;board = nucleo_f302r8
;board = nucleo_f303re
;board = nucleo_f401re
;board = nucleo_f411re
;board = nucleo_f446re
;board = nucleo_g071rb
;board = nucleo_g431rb
;board = nucleo_g474re
;board = nucleo_l053r8
;board = nucleo_l073rz
;board = nucleo_l152re
;board = nucleo_l433rc_p
;board = nucleo_l452re
;board = nucleo_l452re-p
;board = nucleo_l476rg
;board = pnucleo_wb55rg
; ============================================================
; Board configuration Nucleo-32
; ============================================================
;board = nucleo_f031k6
;board = nucleo_l031k6
;board = nucleo_l412kb
;board = nucleo_l432lc
;board = nucleo_f303k8
;board = nucleo_g431kb
; ============================================================
; Board configuration Discovery Boards
; ============================================================
;board = disco_f030r8
;board = disco_f072rb
;board = disco_f030r8
;board = disco_f100rb
;board = disco_f407vg
;board = disco_f413zh
;board = disco_f746ng
;board = disco_g0316
;board = disco_l475vg_iot
;board = disco_f072cz-lrwan1
; ============================================================
; Board configuration STM32MP1 Boards
; ============================================================
;board = stm32mp157a-dk1
;board = stm32mp157c-dk2
; ============================================================
; Board configuration Generic Boards
; ============================================================
;board = bluepill_f103c6
;board = bluepill_f103c8
;board = blackpill_f103c8
;board = stm32f103cx
;board = stm32f103rx
;board = stm32f103tx
;board = stm32f103vx
;board = stm32f103zx
;board = stm32f103zet6
;board = maplemini_f103cb
;board = blackpill_f303cc
;board = black_f407ve
;board = black_f407vg
;board = black_f407ze
;board = black_f407zg
;board = blue_f407ve_mini
;board = blackpill_f401cc
;board = blackpill_f411ce
;board = coreboard_f401rc
;board = feather_f405
; ============================================================
; Board configuration Many more Boards to be filled
; ============================================================
+55 -15
View File
@@ -25,13 +25,16 @@
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Version: 1.1.0
Version: 1.3.0
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 11/05/2021 Initial coding to support RP2040-based boards such as RASPBERRY_PI_PICO. etc.
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.3.0 K.Hoang 25/09/2022 Fix severe bug affecting time between the starts
*****************************************************************************************************************************/
#pragma once
@@ -41,11 +44,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();
@@ -59,6 +66,8 @@ void RPI_PICO_ISR_Timer::init()
numTimers = 0;
}
////////////////////////////////////////////////////////////////
void RPI_PICO_ISR_Timer::run()
{
uint8_t i;
@@ -134,6 +143,7 @@ void RPI_PICO_ISR_Timer::run()
}
////////////////////////////////////////////////////////////////
// find the first available slot
// return -1 if none found
@@ -158,8 +168,9 @@ int RPI_PICO_ISR_Timer::findFirstFreeSlot()
return -1;
}
////////////////////////////////////////////////////////////////
int RPI_PICO_ISR_Timer::setupTimer(unsigned long d, void* f, void* p, bool h, unsigned n)
int RPI_PICO_ISR_Timer::setupTimer(const unsigned long& d, void* f, void* p, bool h, const unsigned& n)
{
int freeTimer;
@@ -192,38 +203,51 @@ int RPI_PICO_ISR_Timer::setupTimer(unsigned long d, void* f, void* p, bool h, un
return freeTimer;
}
////////////////////////////////////////////////////////////////
int RPI_PICO_ISR_Timer::setTimer(unsigned long d, timer_callback f, unsigned n)
int RPI_PICO_ISR_Timer::setTimer(const unsigned long& d, timer_callback f, const unsigned& n)
{
return setupTimer(d, (void *)f, NULL, false, n);
}
int RPI_PICO_ISR_Timer::setTimer(unsigned long d, timer_callback_p f, void* p, unsigned n)
////////////////////////////////////////////////////////////////
int RPI_PICO_ISR_Timer::setTimer(const unsigned long& d, timer_callback_p f, void* p, const unsigned& n)
{
return setupTimer(d, (void *)f, p, true, n);
}
int RPI_PICO_ISR_Timer::setInterval(unsigned long d, timer_callback f)
////////////////////////////////////////////////////////////////
int RPI_PICO_ISR_Timer::setInterval(const unsigned long& d, timer_callback f)
{
return setupTimer(d, (void *)f, NULL, false, RPI_PICO_RUN_FOREVER);
}
int RPI_PICO_ISR_Timer::setInterval(unsigned long d, timer_callback_p f, void* p)
////////////////////////////////////////////////////////////////
int RPI_PICO_ISR_Timer::setInterval(const unsigned long& d, timer_callback_p f, void* p)
{
return setupTimer(d, (void *)f, p, true, RPI_PICO_RUN_FOREVER);
}
int RPI_PICO_ISR_Timer::setTimeout(unsigned long d, timer_callback f)
////////////////////////////////////////////////////////////////
int RPI_PICO_ISR_Timer::setTimeout(const unsigned long& d, timer_callback f)
{
return setupTimer(d, (void *)f, NULL, false, RPI_PICO_RUN_ONCE);
}
int RPI_PICO_ISR_Timer::setTimeout(unsigned long d, timer_callback_p f, void* p)
////////////////////////////////////////////////////////////////
int RPI_PICO_ISR_Timer::setTimeout(const unsigned long& d, timer_callback_p f, void* p)
{
return setupTimer(d, (void *)f, p, true, RPI_PICO_RUN_ONCE);
}
bool RPI_PICO_ISR_Timer::changeInterval(unsigned numTimer, unsigned long d)
////////////////////////////////////////////////////////////////
bool RPI_PICO_ISR_Timer::changeInterval(const unsigned& numTimer, const unsigned long& d)
{
if (numTimer >= RPI_PICO_MAX_TIMERS)
{
@@ -249,7 +273,9 @@ bool RPI_PICO_ISR_Timer::changeInterval(unsigned numTimer, unsigned long d)
return false;
}
void RPI_PICO_ISR_Timer::deleteTimer(unsigned timerId)
////////////////////////////////////////////////////////////////
void RPI_PICO_ISR_Timer::deleteTimer(const unsigned& timerId)
{
if (timerId >= RPI_PICO_MAX_TIMERS)
{
@@ -280,8 +306,10 @@ void RPI_PICO_ISR_Timer::deleteTimer(unsigned timerId)
}
}
////////////////////////////////////////////////////////////////
// function contributed by code@rowansimms.com
void RPI_PICO_ISR_Timer::restartTimer(unsigned numTimer)
void RPI_PICO_ISR_Timer::restartTimer(const unsigned& numTimer)
{
if (numTimer >= RPI_PICO_MAX_TIMERS)
{
@@ -297,8 +325,9 @@ void RPI_PICO_ISR_Timer::restartTimer(unsigned numTimer)
rp2040.resumeOtherCore();
}
////////////////////////////////////////////////////////////////
bool RPI_PICO_ISR_Timer::isEnabled(unsigned numTimer)
bool RPI_PICO_ISR_Timer::isEnabled(const unsigned& numTimer)
{
if (numTimer >= RPI_PICO_MAX_TIMERS)
{
@@ -308,8 +337,9 @@ bool RPI_PICO_ISR_Timer::isEnabled(unsigned numTimer)
return timer[numTimer].enabled;
}
////////////////////////////////////////////////////////////////
void RPI_PICO_ISR_Timer::enable(unsigned numTimer)
void RPI_PICO_ISR_Timer::enable(const unsigned& numTimer)
{
if (numTimer >= RPI_PICO_MAX_TIMERS)
{
@@ -319,8 +349,9 @@ void RPI_PICO_ISR_Timer::enable(unsigned numTimer)
timer[numTimer].enabled = true;
}
////////////////////////////////////////////////////////////////
void RPI_PICO_ISR_Timer::disable(unsigned numTimer)
void RPI_PICO_ISR_Timer::disable(const unsigned& numTimer)
{
if (numTimer >= RPI_PICO_MAX_TIMERS)
{
@@ -330,6 +361,8 @@ void RPI_PICO_ISR_Timer::disable(unsigned numTimer)
timer[numTimer].enabled = false;
}
////////////////////////////////////////////////////////////////
void RPI_PICO_ISR_Timer::enableAll()
{
// Enable all timers with a callback assigned (used)
@@ -349,6 +382,8 @@ void RPI_PICO_ISR_Timer::enableAll()
rp2040.resumeOtherCore();
}
////////////////////////////////////////////////////////////////
void RPI_PICO_ISR_Timer::disableAll()
{
// Disable all timers with a callback assigned (used)
@@ -369,7 +404,9 @@ void RPI_PICO_ISR_Timer::disableAll()
}
void RPI_PICO_ISR_Timer::toggle(unsigned numTimer)
////////////////////////////////////////////////////////////////
void RPI_PICO_ISR_Timer::toggle(const unsigned& numTimer)
{
if (numTimer >= RPI_PICO_MAX_TIMERS)
{
@@ -379,11 +416,14 @@ void RPI_PICO_ISR_Timer::toggle(unsigned numTimer)
timer[numTimer].enabled = !timer[numTimer].enabled;
}
////////////////////////////////////////////////////////////////
unsigned RPI_PICO_ISR_Timer::getNumTimers()
{
return numTimers;
}
////////////////////////////////////////////////////////////////
#endif // ISR_TIMER_GENERIC_IMPL_H
+5 -154
View File
@@ -25,13 +25,16 @@
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Version: 1.1.0
Version: 1.3.0
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 11/05/2021 Initial coding to support RP2040-based boards such as RASPBERRY_PI_PICO. etc.
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.3.0 K.Hoang 25/09/2022 Fix severe bug affecting time between the starts
*****************************************************************************************************************************/
#pragma once
@@ -39,159 +42,7 @@
#ifndef ISR_TIMER_GENERIC_H
#define ISR_TIMER_GENERIC_H
#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
#endif
#define USING_RPI_PICO_TIMER_INTERRUPT true
#else
#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.1.0"
#endif
#include "TimerInterrupt_Generic_Debug.h"
#include <stddef.h>
#include <inttypes.h>
#include "pico/multicore.h"
#if defined(ARDUINO)
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#endif
#define FLAG_VALUE 0xDEADBEEF
#define RPI_PICO_ISR_Timer RPI_PICO_ISRTimer
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
#define RPI_PICO_RUN_FOREVER 0
#define RPI_PICO_RUN_ONCE 1
// constructor
RPI_PICO_ISR_Timer();
void init();
// this function must be called inside loop()
void run();
// 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(unsigned long 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(unsigned long 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(unsigned long 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(unsigned long 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(unsigned long d, timer_callback f, 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(unsigned long d, timer_callback_p f, void* p, unsigned n);
// updates interval of the specified timer
bool changeInterval(unsigned numTimer, unsigned long d);
// destroy the specified timer
void deleteTimer(unsigned numTimer);
// restart the specified timer
void restartTimer(unsigned numTimer);
// returns true if the specified timer is enabled
bool isEnabled(unsigned numTimer);
// enables the specified timer
void enable(unsigned numTimer);
// disables the specified timer
void disable(unsigned numTimer);
// enables all timers
void enableAll();
// disables all timers
void disableAll();
// enables the specified timer if it's currently disabled, and vice-versa
void toggle(unsigned numTimer);
// returns the number of used timers
unsigned getNumTimers();
// returns the number of available timers
unsigned getNumAvailableTimers()
{
return RPI_PICO_MAX_TIMERS - numTimers;
};
private:
// deferred call constants
#define RPI_PICO_DEFCALL_DONTRUN 0 // don't call the callback function
#define RPI_PICO_DEFCALL_RUNONLY 1 // call the callback function but don't delete the timer
#define RPI_PICO_DEFCALL_RUNANDDEL 2 // call the callback function and delete the 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(unsigned long d, void* f, void* p, bool h, unsigned n);
// find the first available slot
int findFirstFreeSlot();
typedef struct
{
unsigned long prev_millis; // value returned by the millis() function in the previous run() call
void* callback; // pointer to the callback function
void* param; // function parameter
bool hasParam; // true if callback takes a parameter
unsigned long delay; // delay value
unsigned maxNumRuns; // number of runs to be executed
unsigned numRuns; // number of executed runs
bool enabled; // true if enabled
unsigned toBeCalled; // deferred function call (sort of) - N.B.: only used in run()
} timer_t;
volatile timer_t timer[RPI_PICO_MAX_TIMERS];
// actual number of timers in use (-1 means uninitialized)
volatile int numTimers;
};
#include "RPi_Pico_ISR_Timer.hpp"
#include "RPi_Pico_ISR_Timer-Impl.h"
#endif // ISR_TIMER_GENERIC_H
@@ -1,5 +1,5 @@
/****************************************************************************************************************************
RPi_Pico_ISR_Timer.h
RPi_Pico_ISR_Timer.hpp
For RP2040-based boards such as RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040.
Written by Khoi Hoang
@@ -25,19 +25,24 @@
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Version: 1.1.0
Version: 1.3.0
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 11/05/2021 Initial coding to support RP2040-based boards such as RASPBERRY_PI_PICO. etc.
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.3.0 K.Hoang 25/09/2022 Fix severe bug affecting time between the starts
*****************************************************************************************************************************/
#pragma once
#ifndef ISR_TIMER_GENERIC_H
#define ISR_TIMER_GENERIC_H
#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)
@@ -48,10 +53,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.1.0"
#define RPI_PICO_TIMER_INTERRUPT_VERSION "RPi_Pico_TimerInterrupt v1.3.0"
#define RPI_PICO_TIMER_INTERRUPT_VERSION_MAJOR 1
#define RPI_PICO_TIMER_INTERRUPT_VERSION_MINOR 3
#define RPI_PICO_TIMER_INTERRUPT_VERSION_PATCH 0
#define RPI_PICO_TIMER_INTERRUPT_VERSION_INT 1003000
#endif
////////////////////////////////////////////////////////////////
#include "TimerInterrupt_Generic_Debug.h"
#include <stddef.h>
@@ -75,9 +90,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
@@ -95,50 +111,50 @@ 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(unsigned long d, timer_callback f);
int setInterval(const unsigned long& 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(unsigned long d, timer_callback_p f, void* p);
int setInterval(const unsigned long& 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(unsigned long d, timer_callback f);
int setTimeout(const unsigned long& 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(unsigned long d, timer_callback_p f, void* p);
int setTimeout(const unsigned long& 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(unsigned long d, timer_callback f, unsigned n);
int setTimer(const unsigned long& 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(unsigned long d, timer_callback_p f, void* p, unsigned n);
int setTimer(const unsigned long& d, timer_callback_p f, void* p, const unsigned& n);
// updates interval of the specified timer
bool changeInterval(unsigned numTimer, unsigned long d);
bool changeInterval(const unsigned& numTimer, const unsigned long& d);
// destroy the specified timer
void deleteTimer(unsigned numTimer);
void deleteTimer(const unsigned& numTimer);
// restart the specified timer
void restartTimer(unsigned numTimer);
void restartTimer(const unsigned& numTimer);
// returns true if the specified timer is enabled
bool isEnabled(unsigned numTimer);
bool isEnabled(const unsigned& numTimer);
// enables the specified timer
void enable(unsigned numTimer);
void enable(const unsigned& numTimer);
// disables the specified timer
void disable(unsigned numTimer);
void disable(const unsigned& numTimer);
// enables all timers
void enableAll();
@@ -147,7 +163,7 @@ class RPI_PICO_ISR_Timer
void disableAll();
// enables the specified timer if it's currently disabled, and vice-versa
void toggle(unsigned numTimer);
void toggle(const unsigned& numTimer);
// returns the number of used timers
unsigned getNumTimers();
@@ -167,7 +183,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(unsigned long d, void* f, void* p, bool h, unsigned n);
int setupTimer(const unsigned long& d, void* f, void* p, bool h, const unsigned& n);
// find the first available slot
int findFirstFreeSlot();
@@ -191,9 +207,8 @@ class RPI_PICO_ISR_Timer
volatile int numTimers;
};
////////////////////////////////////////////////////////////////
#include "RPi_Pico_ISR_Timer-Impl.h"
#endif // ISR_TIMER_GENERIC_H
#endif // ISR_TIMER_GENERIC_HPP
+87 -38
View File
@@ -26,13 +26,16 @@
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Version: 1.1.0
Version: 1.3.0
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 11/05/2021 Initial coding to support RP2040-based boards such as RASPBERRY_PI_PICO. etc.
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.3.0 K.Hoang 25/09/2022 Fix severe bug affecting time between the starts
*****************************************************************************************************************************/
#pragma once
@@ -49,34 +52,37 @@
#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.1.0"
#define RPI_PICO_TIMER_INTERRUPT_VERSION "RPi_Pico_TimerInterrupt v1.3.0"
#define RPI_PICO_TIMER_INTERRUPT_VERSION_MAJOR 1
#define RPI_PICO_TIMER_INTERRUPT_VERSION_MINOR 3
#define RPI_PICO_TIMER_INTERRUPT_VERSION_PATCH 0
#define RPI_PICO_TIMER_INTERRUPT_VERSION_INT 1003000
#endif
////////////////////////////////////////////////////////////////
#ifndef TIMER_INTERRUPT_DEBUG
#define TIMER_INTERRUPT_DEBUG 0
#endif
#if defined(ARDUINO_ARCH_MBED)
#warning Using MBED RP2040 platform
#include "pico.h"
//#include "pico/stdio.h"
#include "pico/time.h"
#include "hardware/gpio.h"
#include "hardware/uart.h"
#include "hardware/timer.h"
#include "hardware/irq.h"
#else
#warning Using RP2040 platform
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/timer.h"
#include "hardware/irq.h"
#endif
////////////////////////////////////////////////////////////////
#include "Arduino.h"
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/timer.h"
#include "hardware/irq.h"
#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
@@ -97,6 +103,7 @@ typedef RPI_PICO_TimerInterrupt RPI_PICO_Timer;
typedef bool (*pico_timer_callback) (struct repeating_timer *t);
////////////////////////////////////////////////////////////////
class RPI_PICO_TimerInterrupt
{
@@ -106,7 +113,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;
@@ -118,29 +125,48 @@ class RPI_PICO_TimerInterrupt
_callback = NULL;
};
////////////////////////////////////////////////////////////////
#define TIM_CLOCK_FREQ ( (float) 1000000.0f )
// frequency (in hertz) 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 setFrequency(float frequency, pico_timer_callback callback)
bool setFrequency(const float& frequency, pico_timer_callback callback)
{
if (_timerNo < MAX_RPI_PICO_NUM_TIMERS)
{
// 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 = (float) 1000000;
_timerCount = (uint64_t) _frequency / frequency;
{
if ( (frequency == 0.0f) || (frequency > 100000.0f) || (callback == NULL) )
{
TISR_LOGERROR(F("Error. frequency == 0, higher than 100KHz or callback == NULL "));
TISR_LOGWARN3(F("RPI_PICO_TimerInterrupt: _timerNo ="), _timerNo, F(", _fre ="), _frequency);
TISR_LOGWARN3(F("_count ="), (uint32_t) (_timerCount >> 32) , F("-"), (uint32_t) (_timerCount));
return false;
}
// 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 = (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));
_callback = callback;
// 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;
}
@@ -152,64 +178,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(unsigned long interval, pico_timer_callback callback)
bool setInterval(const unsigned long& interval, pico_timer_callback callback)
{
return setFrequency((float) (1000000.0f / interval), callback);
}
bool attachInterrupt(float frequency, pico_timer_callback 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(unsigned long interval, pico_timer_callback callback)
bool attachInterruptInterval(const unsigned long& 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
+29 -19
View File
@@ -25,13 +25,16 @@
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Version: 1.1.0
Version: 1.3.0
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 11/05/2021 Initial coding to support RP2040-based boards such as RASPBERRY_PI_PICO. etc.
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.3.0 K.Hoang 25/09/2022 Fix severe bug affecting time between the starts
*****************************************************************************************************************************/
#pragma once
@@ -39,12 +42,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
@@ -67,39 +74,42 @@ const char TISR_SP[] = " ";
#define TISR_PRINT_MARK TISR_PRINT(TISR_MARK)
#define TISR_PRINT_SP TISR_PRINT(TISR_SP)
//////////////////////////////////////////
///////////////////////////////////////
#define TISR_LOGERROR(x) if(_TIMERINTERRUPT_LOGLEVEL_>0) { TISR_PRINT_MARK; TISR_PRINTLN(x); }
#define TISR_LOGERROR0(x) if(_TIMERINTERRUPT_LOGLEVEL_>0) { TISR_PRINT(x); }
#define TISR_LOGERROR1(x,y) if(_TIMERINTERRUPT_LOGLEVEL_>0) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINTLN(y); }
#define TISR_LOGERROR2(x,y,z) if(_TIMERINTERRUPT_LOGLEVEL_>0) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINT(y); TISR_PRINT_SP; TISR_PRINTLN(z); }
#define TISR_LOGERROR3(x,y,z,w) if(_TIMERINTERRUPT_LOGLEVEL_>0) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINT(y); TISR_PRINT_SP; TISR_PRINT(z); TISR_PRINT_SP; TISR_PRINTLN(w); }
#define TISR_LOGERROR1(x,y) if(_TIMERINTERRUPT_LOGLEVEL_>0) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINTLN(y); }
#define TISR_LOGERROR2(x,y,z) if(_TIMERINTERRUPT_LOGLEVEL_>0) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT(y); TISR_PRINTLN(z); }
#define TISR_LOGERROR3(x,y,z,w) if(_TIMERINTERRUPT_LOGLEVEL_>0) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT(y); TISR_PRINT(z); TISR_PRINTLN(w); }
#define TISR_LOGERROR5(x,y,z,w, xx, yy) if(_TIMERINTERRUPT_LOGLEVEL_>0) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT(y); TISR_PRINT(z); TISR_PRINT(w); TISR_PRINT(xx); TISR_PRINTLN(yy); }
//////////////////////////////////////////
///////////////////////////////////////
#define TISR_LOGWARN(x) if(_TIMERINTERRUPT_LOGLEVEL_>1) { TISR_PRINT_MARK; TISR_PRINTLN(x); }
#define TISR_LOGWARN0(x) if(_TIMERINTERRUPT_LOGLEVEL_>1) { TISR_PRINT(x); }
#define TISR_LOGWARN1(x,y) if(_TIMERINTERRUPT_LOGLEVEL_>1) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINTLN(y); }
#define TISR_LOGWARN2(x,y,z) if(_TIMERINTERRUPT_LOGLEVEL_>1) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINT(y); TISR_PRINT_SP; TISR_PRINTLN(z); }
#define TISR_LOGWARN3(x,y,z,w) if(_TIMERINTERRUPT_LOGLEVEL_>1) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINT(y); TISR_PRINT_SP; TISR_PRINT(z); TISR_PRINT_SP; TISR_PRINTLN(w); }
#define TISR_LOGWARN1(x,y) if(_TIMERINTERRUPT_LOGLEVEL_>1) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINTLN(y); }
#define TISR_LOGWARN2(x,y,z) if(_TIMERINTERRUPT_LOGLEVEL_>1) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT(y); TISR_PRINTLN(z); }
#define TISR_LOGWARN3(x,y,z,w) if(_TIMERINTERRUPT_LOGLEVEL_>1) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT(y); TISR_PRINT(z); TISR_PRINTLN(w); }
#define TISR_LOGWARN5(x,y,z,w, xx, yy) if(_TIMERINTERRUPT_LOGLEVEL_>1) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT(y); TISR_PRINT(z); TISR_PRINT(w); TISR_PRINT(xx); TISR_PRINTLN(yy); }
//////////////////////////////////////////
///////////////////////////////////////
#define TISR_LOGINFO(x) if(_TIMERINTERRUPT_LOGLEVEL_>2) { TISR_PRINT_MARK; TISR_PRINTLN(x); }
#define TISR_LOGINFO0(x) if(_TIMERINTERRUPT_LOGLEVEL_>2) { TISR_PRINT(x); }
#define TISR_LOGINFO1(x,y) if(_TIMERINTERRUPT_LOGLEVEL_>2) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINTLN(y); }
#define TISR_LOGINFO2(x,y,z) if(_TIMERINTERRUPT_LOGLEVEL_>2) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINT(y); TISR_PRINT_SP; TISR_PRINTLN(z); }
#define TISR_LOGINFO3(x,y,z,w) if(_TIMERINTERRUPT_LOGLEVEL_>2) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINT(y); TISR_PRINT_SP; TISR_PRINT(z); TISR_PRINT_SP; TISR_PRINTLN(w); }
#define TISR_LOGINFO1(x,y) if(_TIMERINTERRUPT_LOGLEVEL_>2) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINTLN(y); }
#define TISR_LOGINFO2(x,y,z) if(_TIMERINTERRUPT_LOGLEVEL_>2) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT(y); TISR_PRINTLN(z); }
#define TISR_LOGINFO3(x,y,z,w) if(_TIMERINTERRUPT_LOGLEVEL_>2) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT(y); TISR_PRINT(z); TISR_PRINTLN(w); }
#define TISR_LOGINFO5(x,y,z,w, xx, yy) if(_TIMERINTERRUPT_LOGLEVEL_>2) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT(y); TISR_PRINT(z); TISR_PRINT(w); TISR_PRINT(xx); TISR_PRINTLN(yy); }
//////////////////////////////////////////
///////////////////////////////////////
#define TISR_LOGDEBUG(x) if(_TIMERINTERRUPT_LOGLEVEL_>3) { TISR_PRINT_MARK; TISR_PRINTLN(x); }
#define TISR_LOGDEBUG0(x) if(_TIMERINTERRUPT_LOGLEVEL_>3) { TISR_PRINT(x); }
#define TISR_LOGDEBUG1(x,y) if(_TIMERINTERRUPT_LOGLEVEL_>3) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINTLN(y); }
#define TISR_LOGDEBUG2(x,y,z) if(_TIMERINTERRUPT_LOGLEVEL_>3) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINT(y); TISR_PRINT_SP; TISR_PRINTLN(z); }
#define TISR_LOGDEBUG3(x,y,z,w) if(_TIMERINTERRUPT_LOGLEVEL_>3) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINT(y); TISR_PRINT_SP; TISR_PRINT(z); TISR_PRINT_SP; TISR_PRINTLN(w); }
#define TISR_LOGDEBUG1(x,y) if(_TIMERINTERRUPT_LOGLEVEL_>3) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINTLN(y); }
#define TISR_LOGDEBUG2(x,y,z) if(_TIMERINTERRUPT_LOGLEVEL_>3) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT(y); TISR_PRINTLN(z); }
#define TISR_LOGDEBUG3(x,y,z,w) if(_TIMERINTERRUPT_LOGLEVEL_>3) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT(y); TISR_PRINT(z); TISR_PRINTLN(w); }
#define TISR_LOGDEBUG5(x,y,z,w, xx, yy) if(_TIMERINTERRUPT_LOGLEVEL_>3) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT(y); TISR_PRINT(z); TISR_PRINT(w); TISR_PRINT(xx); TISR_PRINTLN(yy); }
//////////////////////////////////////////
///////////////////////////////////////
#endif //TIMERINTERRUPT_GENERIC_DEBUG_H
-385
View File
@@ -1,385 +0,0 @@
/****************************************************************************************************************************
RPi_Pico_ISR_Timer.cpp
For RP2040-based boards such as RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040.
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/RPI_PICO_TimerInterrupt
Licensed under MIT license
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 even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by
unsigned long miliseconds), you just consume only one RPI_PICO timer and avoid conflicting with other cores' tasks.
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.
Based on SimpleTimer - A timer library for Arduino.
Author: mromani@ottotecnica.com
Copyright (c) 2010 OTTOTECNICA Italy
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Version: 1.1.0
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 11/05/2021 Initial coding to support RP2040-based boards such as RASPBERRY_PI_PICO. etc.
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
*****************************************************************************************************************************/
#include <string.h>
#include "RPi_Pico_ISR_Timer.h"
RPI_PICO_ISR_Timer::RPI_PICO_ISR_Timer()
: numTimers (-1)
{
}
void RPI_PICO_ISR_Timer::init()
{
unsigned long current_millis = millis(); //elapsed();
for (uint8_t i = 0; i < RPI_PICO_MAX_TIMERS; i++)
{
memset((void*) &timer[i], 0, sizeof (timer_t));
timer[i].prev_millis = current_millis;
}
numTimers = 0;
}
void RPI_PICO_ISR_Timer::run()
{
uint8_t i;
unsigned long current_millis;
// get current time
current_millis = millis(); //elapsed();
// RPI_PICO is a multi core / multi processing chip. It is mandatory to disable task switches during ISR
rp2040.idleOtherCore();
for (i = 0; i < RPI_PICO_MAX_TIMERS; i++)
{
timer[i].toBeCalled = RPI_PICO_DEFCALL_DONTRUN;
// no callback == no timer, i.e. jump over empty slots
if (timer[i].callback != NULL)
{
// is it time to process this timer ?
// see http://arduino.cc/forum/index.php/topic,124048.msg932592.html#msg932592
if ((current_millis - timer[i].prev_millis) >= timer[i].delay)
{
unsigned long skipTimes = (current_millis - timer[i].prev_millis) / timer[i].delay;
// update time
timer[i].prev_millis += timer[i].delay * skipTimes;
// check if the timer callback has to be executed
if (timer[i].enabled)
{
// "run forever" timers must always be executed
if (timer[i].maxNumRuns == RPI_PICO_RUN_FOREVER)
{
timer[i].toBeCalled = RPI_PICO_DEFCALL_RUNONLY;
}
// other timers get executed the specified number of times
else if (timer[i].numRuns < timer[i].maxNumRuns)
{
timer[i].toBeCalled = RPI_PICO_DEFCALL_RUNONLY;
timer[i].numRuns++;
// after the last run, delete the timer
if (timer[i].numRuns >= timer[i].maxNumRuns)
{
timer[i].toBeCalled = RPI_PICO_DEFCALL_RUNANDDEL;
}
}
}
}
}
}
for (i = 0; i < RPI_PICO_MAX_TIMERS; i++)
{
if (timer[i].toBeCalled == RPI_PICO_DEFCALL_DONTRUN)
continue;
if (timer[i].hasParam)
(*(timer_callback_p)timer[i].callback)(timer[i].param);
else
(*(timer_callback)timer[i].callback)();
if (timer[i].toBeCalled == RPI_PICO_DEFCALL_RUNANDDEL)
deleteTimer(i);
}
// RPI_PICO is a multi core / multi processing chip. It is mandatory to disable task switches during ISR
rp2040.resumeOtherCore();
}
// find the first available slot
// return -1 if none found
int RPI_PICO_ISR_Timer::findFirstFreeSlot()
{
// all slots are used
if (numTimers >= RPI_PICO_MAX_TIMERS)
{
return -1;
}
// return the first slot with no callback (i.e. free)
for (uint8_t i = 0; i < RPI_PICO_MAX_TIMERS; i++)
{
if (timer[i].callback == NULL)
{
return i;
}
}
// no free slots found
return -1;
}
int RPI_PICO_ISR_Timer::setupTimer(unsigned long d, void* f, void* p, bool h, unsigned n)
{
int freeTimer;
if (numTimers < 0)
{
init();
}
freeTimer = findFirstFreeSlot();
if (freeTimer < 0)
{
return -1;
}
if (f == NULL)
{
return -1;
}
timer[freeTimer].delay = d;
timer[freeTimer].callback = f;
timer[freeTimer].param = p;
timer[freeTimer].hasParam = h;
timer[freeTimer].maxNumRuns = n;
timer[freeTimer].enabled = true;
timer[freeTimer].prev_millis = millis();
numTimers++;
return freeTimer;
}
int RPI_PICO_ISR_Timer::setTimer(unsigned long d, timer_callback f, unsigned n)
{
return setupTimer(d, (void *)f, NULL, false, n);
}
int RPI_PICO_ISR_Timer::setTimer(unsigned long d, timer_callback_p f, void* p, unsigned n)
{
return setupTimer(d, (void *)f, p, true, n);
}
int RPI_PICO_ISR_Timer::setInterval(unsigned long d, timer_callback f)
{
return setupTimer(d, (void *)f, NULL, false, RPI_PICO_RUN_FOREVER);
}
int RPI_PICO_ISR_Timer::setInterval(unsigned long d, timer_callback_p f, void* p)
{
return setupTimer(d, (void *)f, p, true, RPI_PICO_RUN_FOREVER);
}
int RPI_PICO_ISR_Timer::setTimeout(unsigned long d, timer_callback f)
{
return setupTimer(d, (void *)f, NULL, false, RPI_PICO_RUN_ONCE);
}
int RPI_PICO_ISR_Timer::setTimeout(unsigned long d, timer_callback_p f, void* p)
{
return setupTimer(d, (void *)f, p, true, RPI_PICO_RUN_ONCE);
}
bool RPI_PICO_ISR_Timer::changeInterval(unsigned numTimer, unsigned long d)
{
if (numTimer >= RPI_PICO_MAX_TIMERS)
{
return false;
}
// Updates interval of existing specified timer
if (timer[numTimer].callback != NULL)
{
// RPI_PICO is a multi core / multi processing chip. It is mandatory to disable task switches during modifying shared vars
rp2040.idleOtherCore();
timer[numTimer].delay = d;
timer[numTimer].prev_millis = millis();
// RPI_PICO is a multi core / multi processing chip. It is mandatory to disable task switches during modifying shared vars
rp2040.resumeOtherCore();
return true;
}
// false return for non-used numTimer, no callback
return false;
}
void RPI_PICO_ISR_Timer::deleteTimer(unsigned timerId)
{
if (timerId >= RPI_PICO_MAX_TIMERS)
{
return;
}
// nothing to delete if no timers are in use
if (numTimers == 0)
{
return;
}
// don't decrease the number of timers if the specified slot is already empty
if (timer[timerId].callback != NULL)
{
// RPI_PICO is a multi core / multi processing chip. It is mandatory to disable task switches during modifying shared vars
rp2040.idleOtherCore();
memset((void*) &timer[timerId], 0, sizeof (timer_t));
timer[timerId].prev_millis = millis();
// update number of timers
numTimers--;
// RPI_PICO is a multi core / multi processing chip. It is mandatory to disable task switches during modifying shared vars
rp2040.resumeOtherCore();
}
}
// function contributed by code@rowansimms.com
void RPI_PICO_ISR_Timer::restartTimer(unsigned numTimer)
{
if (numTimer >= RPI_PICO_MAX_TIMERS)
{
return;
}
// RPI_PICO is a multi core / multi processing chip. It is mandatory to disable task switches during modifying shared vars
rp2040.idleOtherCore();
timer[numTimer].prev_millis = millis();
// RPI_PICO is a multi core / multi processing chip. It is mandatory to disable task switches during modifying shared vars
rp2040.resumeOtherCore();
}
bool RPI_PICO_ISR_Timer::isEnabled(unsigned numTimer)
{
if (numTimer >= RPI_PICO_MAX_TIMERS)
{
return false;
}
return timer[numTimer].enabled;
}
void RPI_PICO_ISR_Timer::enable(unsigned numTimer)
{
if (numTimer >= RPI_PICO_MAX_TIMERS)
{
return;
}
timer[numTimer].enabled = true;
}
void RPI_PICO_ISR_Timer::disable(unsigned numTimer)
{
if (numTimer >= RPI_PICO_MAX_TIMERS)
{
return;
}
timer[numTimer].enabled = false;
}
void RPI_PICO_ISR_Timer::enableAll()
{
// Enable all timers with a callback assigned (used)
// RPI_PICO is a multi core / multi processing chip. It is mandatory to disable task switches during modifying shared vars
rp2040.idleOtherCore();
for (uint8_t i = 0; i < RPI_PICO_MAX_TIMERS; i++)
{
if (timer[i].callback != NULL && timer[i].numRuns == RPI_PICO_RUN_FOREVER)
{
timer[i].enabled = true;
}
}
// RPI_PICO is a multi core / multi processing chip. It is mandatory to disable task switches during modifying shared vars
rp2040.resumeOtherCore();
}
void RPI_PICO_ISR_Timer::disableAll()
{
// Disable all timers with a callback assigned (used)
// RPI_PICO is a multi core / multi processing chip. It is mandatory to disable task switches during modifying shared vars
rp2040.idleOtherCore();
for (uint8_t i = 0; i < RPI_PICO_MAX_TIMERS; i++)
{
if (timer[i].callback != NULL && timer[i].numRuns == RPI_PICO_RUN_FOREVER)
{
timer[i].enabled = false;
}
}
// RPI_PICO is a multi core / multi processing chip. It is mandatory to disable task switches during modifying shared vars
rp2040.resumeOtherCore();
}
void RPI_PICO_ISR_Timer::toggle(unsigned numTimer)
{
if (numTimer >= RPI_PICO_MAX_TIMERS)
{
return;
}
timer[numTimer].enabled = !timer[numTimer].enabled;
}
unsigned RPI_PICO_ISR_Timer::getNumTimers()
{
return numTimers;
}
-196
View File
@@ -1,196 +0,0 @@
/****************************************************************************************************************************
RPi_Pico_ISR_Timer.h
For RP2040-based boards such as RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040.
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/RPI_PICO_TimerInterrupt
Licensed under MIT license
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 even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by
unsigned long miliseconds), you just consume only one RPI_PICO timer and avoid conflicting with other cores' tasks.
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.
Based on SimpleTimer - A timer library for Arduino.
Author: mromani@ottotecnica.com
Copyright (c) 2010 OTTOTECNICA Italy
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Version: 1.1.0
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 11/05/2021 Initial coding to support RP2040-based boards such as RASPBERRY_PI_PICO. etc.
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
*****************************************************************************************************************************/
#pragma once
#ifndef ISR_TIMER_GENERIC_H
#define ISR_TIMER_GENERIC_H
#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
#endif
#define USING_RPI_PICO_TIMER_INTERRUPT true
#else
#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.1.0"
#endif
#include "TimerInterrupt_Generic_Debug.h"
#include <stddef.h>
#include <inttypes.h>
#include "pico/multicore.h"
#if defined(ARDUINO)
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#endif
#define FLAG_VALUE 0xDEADBEEF
#define RPI_PICO_ISR_Timer RPI_PICO_ISRTimer
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
#define RPI_PICO_RUN_FOREVER 0
#define RPI_PICO_RUN_ONCE 1
// constructor
RPI_PICO_ISR_Timer();
void init();
// this function must be called inside loop()
void run();
// 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(unsigned long 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(unsigned long 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(unsigned long 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(unsigned long 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(unsigned long d, timer_callback f, 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(unsigned long d, timer_callback_p f, void* p, unsigned n);
// updates interval of the specified timer
bool changeInterval(unsigned numTimer, unsigned long d);
// destroy the specified timer
void deleteTimer(unsigned numTimer);
// restart the specified timer
void restartTimer(unsigned numTimer);
// returns true if the specified timer is enabled
bool isEnabled(unsigned numTimer);
// enables the specified timer
void enable(unsigned numTimer);
// disables the specified timer
void disable(unsigned numTimer);
// enables all timers
void enableAll();
// disables all timers
void disableAll();
// enables the specified timer if it's currently disabled, and vice-versa
void toggle(unsigned numTimer);
// returns the number of used timers
unsigned getNumTimers();
// returns the number of available timers
unsigned getNumAvailableTimers()
{
return RPI_PICO_MAX_TIMERS - numTimers;
};
private:
// deferred call constants
#define RPI_PICO_DEFCALL_DONTRUN 0 // don't call the callback function
#define RPI_PICO_DEFCALL_RUNONLY 1 // call the callback function but don't delete the timer
#define RPI_PICO_DEFCALL_RUNANDDEL 2 // call the callback function and delete the 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(unsigned long d, void* f, void* p, bool h, unsigned n);
// find the first available slot
int findFirstFreeSlot();
typedef struct
{
unsigned long prev_millis; // value returned by the millis() function in the previous run() call
void* callback; // pointer to the callback function
void* param; // function parameter
bool hasParam; // true if callback takes a parameter
unsigned long delay; // delay value
unsigned maxNumRuns; // number of runs to be executed
unsigned numRuns; // number of executed runs
bool enabled; // true if enabled
unsigned toBeCalled; // deferred function call (sort of) - N.B.: only used in run()
} timer_t;
volatile timer_t timer[RPI_PICO_MAX_TIMERS];
// actual number of timers in use (-1 means uninitialized)
volatile int numTimers;
};
#endif // ISR_TIMER_GENERIC_H
-216
View File
@@ -1,216 +0,0 @@
/****************************************************************************************************************************
RPi_Pico_TimerInterrupt.h
For RP2040-based boards such as RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040.
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/RPI_PICO_TimerInterrupt
Licensed under MIT license
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 even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by
unsigned long miliseconds), you just consume only one RPI_PICO timer and avoid conflicting with other cores' tasks.
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.
Based on SimpleTimer - A timer library for Arduino.
Author: mromani@ottotecnica.com
Copyright (c) 2010 OTTOTECNICA Italy
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Version: 1.1.0
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 11/05/2021 Initial coding to support RP2040-based boards such as RASPBERRY_PI_PICO. etc.
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
*****************************************************************************************************************************/
#pragma once
#ifndef RPI_PICO_TIMERINTERRUPT_H
#define RPI_PICO_TIMERINTERRUPT_H
#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
#endif
#define USING_RPI_PICO_TIMER_INTERRUPT true
#else
#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.1.0"
#endif
#ifndef TIMER_INTERRUPT_DEBUG
#define TIMER_INTERRUPT_DEBUG 0
#endif
#if defined(ARDUINO_ARCH_MBED)
#warning Using MBED RP2040 platform
#include "pico.h"
//#include "pico/stdio.h"
#include "pico/time.h"
#include "hardware/gpio.h"
#include "hardware/uart.h"
#include "hardware/timer.h"
#include "hardware/irq.h"
#else
#warning Using RP2040 platform
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/timer.h"
#include "hardware/irq.h"
#endif
#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
• Enable the appropriate timer interrupt at the processor (see Section 2.3.2)
• Write the time you would like the interrupt to fire to ALARM0 (i.e. the current value in TIMERAWL plus your desired
alarm time in microseconds). Writing the time to the ALARM register sets the ARMED bit as a side effect.
Once the alarm has fired, the ARMED bit will be set to 0 . To clear the latched interrupt, write a 1 to the appropriate bit in
INTR.
*/
class RPI_PICO_TimerInterrupt;
typedef RPI_PICO_TimerInterrupt RPI_PICO_Timer;
// We can use many timers here
#define MAX_RPI_PICO_NUM_TIMERS 4
typedef bool (*pico_timer_callback) (struct repeating_timer *t);
class RPI_PICO_TimerInterrupt
{
private:
uint8_t _timerNo;
pico_timer_callback _callback; // pointer to the callback function
float _frequency; // Timer frequency
uint64_t _timerCount; // count to activate timer, in us
struct repeating_timer _timer;
public:
RPI_PICO_TimerInterrupt(uint8_t timerNo)
{
_timerNo = timerNo;
_callback = NULL;
};
// frequency (in hertz) 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 setFrequency(float frequency, pico_timer_callback callback)
{
if (_timerNo < MAX_RPI_PICO_NUM_TIMERS)
{
// 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 = (float) 1000000;
_timerCount = (uint64_t) _frequency / frequency;
TISR_LOGWARN3(F("RPI_PICO_TimerInterrupt: _timerNo ="), _timerNo, F(", _fre ="), _frequency);
TISR_LOGWARN3(F("_count ="), (uint32_t) (_timerCount >> 32) , F("-"), (uint32_t) (_timerCount));
_callback = callback;
// 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);
cancel_repeating_timer(&_timer);
add_repeating_timer_us(_timerCount, _callback, NULL, &_timer);
TISR_LOGWARN1(F("add_repeating_timer_us ="), _timerCount);
return true;
}
else
{
TISR_LOGERROR(F("Error. Timer must be 0-3"));
return false;
}
}
// 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(unsigned long interval, pico_timer_callback callback)
{
return setFrequency((float) (1000000.0f / interval), callback);
}
bool attachInterrupt(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(unsigned long 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
void reattachInterrupt()
{
add_repeating_timer_us(_timerCount, _callback, NULL, &_timer);
}
// Duration (in milliseconds). Duration = 0 or not specified => run indefinitely
void enableTimer()
{
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);
}
int8_t getTimer() __attribute__((always_inline))
{
return _timerNo;
};
}; // class RPI_PICO_TimerInterrupt
#endif // RPI_PICO_TIMERINTERRUPT_H
-105
View File
@@ -1,105 +0,0 @@
/****************************************************************************************************************************
TimerInterrupt_Generic_Debug.h
For RP2040-based boards such as RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040.
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/RPI_PICO_TimerInterrupt
Licensed under MIT license
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 even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by
unsigned long miliseconds), you just consume only one RPI_PICO timer and avoid conflicting with other cores' tasks.
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.
Based on SimpleTimer - A timer library for Arduino.
Author: mromani@ottotecnica.com
Copyright (c) 2010 OTTOTECNICA Italy
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Version: 1.1.0
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 11/05/2021 Initial coding to support RP2040-based boards such as RASPBERRY_PI_PICO. etc.
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
*****************************************************************************************************************************/
#pragma once
#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
// 2: WARN: errors and warnings
// 3: INFO: errors, warnings and informational (default)
// 4: DEBUG: errors, warnings, informational and debug
#ifndef _TIMERINTERRUPT_LOGLEVEL_
#define _TIMERINTERRUPT_LOGLEVEL_ 1
#endif
//////////////////////////////////////////
const char TISR_MARK[] = "[TISR] ";
const char TISR_SP[] = " ";
#define TISR_PRINT TISR_DBG_PORT.print
#define TISR_PRINTLN TISR_DBG_PORT.println
#define TISR_PRINT_MARK TISR_PRINT(TISR_MARK)
#define TISR_PRINT_SP TISR_PRINT(TISR_SP)
//////////////////////////////////////////
#define TISR_LOGERROR(x) if(_TIMERINTERRUPT_LOGLEVEL_>0) { TISR_PRINT_MARK; TISR_PRINTLN(x); }
#define TISR_LOGERROR0(x) if(_TIMERINTERRUPT_LOGLEVEL_>0) { TISR_PRINT(x); }
#define TISR_LOGERROR1(x,y) if(_TIMERINTERRUPT_LOGLEVEL_>0) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINTLN(y); }
#define TISR_LOGERROR2(x,y,z) if(_TIMERINTERRUPT_LOGLEVEL_>0) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINT(y); TISR_PRINT_SP; TISR_PRINTLN(z); }
#define TISR_LOGERROR3(x,y,z,w) if(_TIMERINTERRUPT_LOGLEVEL_>0) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINT(y); TISR_PRINT_SP; TISR_PRINT(z); TISR_PRINT_SP; TISR_PRINTLN(w); }
//////////////////////////////////////////
#define TISR_LOGWARN(x) if(_TIMERINTERRUPT_LOGLEVEL_>1) { TISR_PRINT_MARK; TISR_PRINTLN(x); }
#define TISR_LOGWARN0(x) if(_TIMERINTERRUPT_LOGLEVEL_>1) { TISR_PRINT(x); }
#define TISR_LOGWARN1(x,y) if(_TIMERINTERRUPT_LOGLEVEL_>1) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINTLN(y); }
#define TISR_LOGWARN2(x,y,z) if(_TIMERINTERRUPT_LOGLEVEL_>1) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINT(y); TISR_PRINT_SP; TISR_PRINTLN(z); }
#define TISR_LOGWARN3(x,y,z,w) if(_TIMERINTERRUPT_LOGLEVEL_>1) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINT(y); TISR_PRINT_SP; TISR_PRINT(z); TISR_PRINT_SP; TISR_PRINTLN(w); }
//////////////////////////////////////////
#define TISR_LOGINFO(x) if(_TIMERINTERRUPT_LOGLEVEL_>2) { TISR_PRINT_MARK; TISR_PRINTLN(x); }
#define TISR_LOGINFO0(x) if(_TIMERINTERRUPT_LOGLEVEL_>2) { TISR_PRINT(x); }
#define TISR_LOGINFO1(x,y) if(_TIMERINTERRUPT_LOGLEVEL_>2) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINTLN(y); }
#define TISR_LOGINFO2(x,y,z) if(_TIMERINTERRUPT_LOGLEVEL_>2) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINT(y); TISR_PRINT_SP; TISR_PRINTLN(z); }
#define TISR_LOGINFO3(x,y,z,w) if(_TIMERINTERRUPT_LOGLEVEL_>2) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINT(y); TISR_PRINT_SP; TISR_PRINT(z); TISR_PRINT_SP; TISR_PRINTLN(w); }
//////////////////////////////////////////
#define TISR_LOGDEBUG(x) if(_TIMERINTERRUPT_LOGLEVEL_>3) { TISR_PRINT_MARK; TISR_PRINTLN(x); }
#define TISR_LOGDEBUG0(x) if(_TIMERINTERRUPT_LOGLEVEL_>3) { TISR_PRINT(x); }
#define TISR_LOGDEBUG1(x,y) if(_TIMERINTERRUPT_LOGLEVEL_>3) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINTLN(y); }
#define TISR_LOGDEBUG2(x,y,z) if(_TIMERINTERRUPT_LOGLEVEL_>3) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINT(y); TISR_PRINT_SP; TISR_PRINTLN(z); }
#define TISR_LOGDEBUG3(x,y,z,w) if(_TIMERINTERRUPT_LOGLEVEL_>3) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINT(y); TISR_PRINT_SP; TISR_PRINT(z); TISR_PRINT_SP; TISR_PRINTLN(w); }
//////////////////////////////////////////
#endif //TIMERINTERRUPT_GENERIC_DEBUG_H
-389
View File
@@ -1,389 +0,0 @@
/****************************************************************************************************************************
RPi_Pico_ISR_Timer-Impl.h
For RP2040-based boards such as RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040.
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/RPI_PICO_TimerInterrupt
Licensed under MIT license
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 even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by
unsigned long miliseconds), you just consume only one RPI_PICO timer and avoid conflicting with other cores' tasks.
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.
Based on SimpleTimer - A timer library for Arduino.
Author: mromani@ottotecnica.com
Copyright (c) 2010 OTTOTECNICA Italy
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Version: 1.1.0
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 11/05/2021 Initial coding to support RP2040-based boards such as RASPBERRY_PI_PICO. etc.
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
*****************************************************************************************************************************/
#pragma once
#ifndef ISR_TIMER_GENERIC_IMPL_H
#define ISR_TIMER_GENERIC_IMPL_H
#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();
for (uint8_t i = 0; i < RPI_PICO_MAX_TIMERS; i++)
{
memset((void*) &timer[i], 0, sizeof (timer_t));
timer[i].prev_millis = current_millis;
}
numTimers = 0;
}
void RPI_PICO_ISR_Timer::run()
{
uint8_t i;
unsigned long current_millis;
// get current time
current_millis = millis(); //elapsed();
// RPI_PICO is a multi core / multi processing chip. It is mandatory to disable task switches during ISR
rp2040.idleOtherCore();
for (i = 0; i < RPI_PICO_MAX_TIMERS; i++)
{
timer[i].toBeCalled = RPI_PICO_DEFCALL_DONTRUN;
// no callback == no timer, i.e. jump over empty slots
if (timer[i].callback != NULL)
{
// is it time to process this timer ?
// see http://arduino.cc/forum/index.php/topic,124048.msg932592.html#msg932592
if ((current_millis - timer[i].prev_millis) >= timer[i].delay)
{
unsigned long skipTimes = (current_millis - timer[i].prev_millis) / timer[i].delay;
// update time
timer[i].prev_millis += timer[i].delay * skipTimes;
// check if the timer callback has to be executed
if (timer[i].enabled)
{
// "run forever" timers must always be executed
if (timer[i].maxNumRuns == RPI_PICO_RUN_FOREVER)
{
timer[i].toBeCalled = RPI_PICO_DEFCALL_RUNONLY;
}
// other timers get executed the specified number of times
else if (timer[i].numRuns < timer[i].maxNumRuns)
{
timer[i].toBeCalled = RPI_PICO_DEFCALL_RUNONLY;
timer[i].numRuns++;
// after the last run, delete the timer
if (timer[i].numRuns >= timer[i].maxNumRuns)
{
timer[i].toBeCalled = RPI_PICO_DEFCALL_RUNANDDEL;
}
}
}
}
}
}
for (i = 0; i < RPI_PICO_MAX_TIMERS; i++)
{
if (timer[i].toBeCalled == RPI_PICO_DEFCALL_DONTRUN)
continue;
if (timer[i].hasParam)
(*(timer_callback_p)timer[i].callback)(timer[i].param);
else
(*(timer_callback)timer[i].callback)();
if (timer[i].toBeCalled == RPI_PICO_DEFCALL_RUNANDDEL)
deleteTimer(i);
}
// RPI_PICO is a multi core / multi processing chip. It is mandatory to disable task switches during ISR
rp2040.resumeOtherCore();
}
// find the first available slot
// return -1 if none found
int RPI_PICO_ISR_Timer::findFirstFreeSlot()
{
// all slots are used
if (numTimers >= RPI_PICO_MAX_TIMERS)
{
return -1;
}
// return the first slot with no callback (i.e. free)
for (uint8_t i = 0; i < RPI_PICO_MAX_TIMERS; i++)
{
if (timer[i].callback == NULL)
{
return i;
}
}
// no free slots found
return -1;
}
int RPI_PICO_ISR_Timer::setupTimer(unsigned long d, void* f, void* p, bool h, unsigned n)
{
int freeTimer;
if (numTimers < 0)
{
init();
}
freeTimer = findFirstFreeSlot();
if (freeTimer < 0)
{
return -1;
}
if (f == NULL)
{
return -1;
}
timer[freeTimer].delay = d;
timer[freeTimer].callback = f;
timer[freeTimer].param = p;
timer[freeTimer].hasParam = h;
timer[freeTimer].maxNumRuns = n;
timer[freeTimer].enabled = true;
timer[freeTimer].prev_millis = millis();
numTimers++;
return freeTimer;
}
int RPI_PICO_ISR_Timer::setTimer(unsigned long d, timer_callback f, unsigned n)
{
return setupTimer(d, (void *)f, NULL, false, n);
}
int RPI_PICO_ISR_Timer::setTimer(unsigned long d, timer_callback_p f, void* p, unsigned n)
{
return setupTimer(d, (void *)f, p, true, n);
}
int RPI_PICO_ISR_Timer::setInterval(unsigned long d, timer_callback f)
{
return setupTimer(d, (void *)f, NULL, false, RPI_PICO_RUN_FOREVER);
}
int RPI_PICO_ISR_Timer::setInterval(unsigned long d, timer_callback_p f, void* p)
{
return setupTimer(d, (void *)f, p, true, RPI_PICO_RUN_FOREVER);
}
int RPI_PICO_ISR_Timer::setTimeout(unsigned long d, timer_callback f)
{
return setupTimer(d, (void *)f, NULL, false, RPI_PICO_RUN_ONCE);
}
int RPI_PICO_ISR_Timer::setTimeout(unsigned long d, timer_callback_p f, void* p)
{
return setupTimer(d, (void *)f, p, true, RPI_PICO_RUN_ONCE);
}
bool RPI_PICO_ISR_Timer::changeInterval(unsigned numTimer, unsigned long d)
{
if (numTimer >= RPI_PICO_MAX_TIMERS)
{
return false;
}
// Updates interval of existing specified timer
if (timer[numTimer].callback != NULL)
{
// RPI_PICO is a multi core / multi processing chip. It is mandatory to disable task switches during modifying shared vars
rp2040.idleOtherCore();
timer[numTimer].delay = d;
timer[numTimer].prev_millis = millis();
// RPI_PICO is a multi core / multi processing chip. It is mandatory to disable task switches during modifying shared vars
rp2040.resumeOtherCore();
return true;
}
// false return for non-used numTimer, no callback
return false;
}
void RPI_PICO_ISR_Timer::deleteTimer(unsigned timerId)
{
if (timerId >= RPI_PICO_MAX_TIMERS)
{
return;
}
// nothing to delete if no timers are in use
if (numTimers == 0)
{
return;
}
// don't decrease the number of timers if the specified slot is already empty
if (timer[timerId].callback != NULL)
{
// RPI_PICO is a multi core / multi processing chip. It is mandatory to disable task switches during modifying shared vars
rp2040.idleOtherCore();
memset((void*) &timer[timerId], 0, sizeof (timer_t));
timer[timerId].prev_millis = millis();
// update number of timers
numTimers--;
// RPI_PICO is a multi core / multi processing chip. It is mandatory to disable task switches during modifying shared vars
rp2040.resumeOtherCore();
}
}
// function contributed by code@rowansimms.com
void RPI_PICO_ISR_Timer::restartTimer(unsigned numTimer)
{
if (numTimer >= RPI_PICO_MAX_TIMERS)
{
return;
}
// RPI_PICO is a multi core / multi processing chip. It is mandatory to disable task switches during modifying shared vars
rp2040.idleOtherCore();
timer[numTimer].prev_millis = millis();
// RPI_PICO is a multi core / multi processing chip. It is mandatory to disable task switches during modifying shared vars
rp2040.resumeOtherCore();
}
bool RPI_PICO_ISR_Timer::isEnabled(unsigned numTimer)
{
if (numTimer >= RPI_PICO_MAX_TIMERS)
{
return false;
}
return timer[numTimer].enabled;
}
void RPI_PICO_ISR_Timer::enable(unsigned numTimer)
{
if (numTimer >= RPI_PICO_MAX_TIMERS)
{
return;
}
timer[numTimer].enabled = true;
}
void RPI_PICO_ISR_Timer::disable(unsigned numTimer)
{
if (numTimer >= RPI_PICO_MAX_TIMERS)
{
return;
}
timer[numTimer].enabled = false;
}
void RPI_PICO_ISR_Timer::enableAll()
{
// Enable all timers with a callback assigned (used)
// RPI_PICO is a multi core / multi processing chip. It is mandatory to disable task switches during modifying shared vars
rp2040.idleOtherCore();
for (uint8_t i = 0; i < RPI_PICO_MAX_TIMERS; i++)
{
if (timer[i].callback != NULL && timer[i].numRuns == RPI_PICO_RUN_FOREVER)
{
timer[i].enabled = true;
}
}
// RPI_PICO is a multi core / multi processing chip. It is mandatory to disable task switches during modifying shared vars
rp2040.resumeOtherCore();
}
void RPI_PICO_ISR_Timer::disableAll()
{
// Disable all timers with a callback assigned (used)
// RPI_PICO is a multi core / multi processing chip. It is mandatory to disable task switches during modifying shared vars
rp2040.idleOtherCore();
for (uint8_t i = 0; i < RPI_PICO_MAX_TIMERS; i++)
{
if (timer[i].callback != NULL && timer[i].numRuns == RPI_PICO_RUN_FOREVER)
{
timer[i].enabled = false;
}
}
// RPI_PICO is a multi core / multi processing chip. It is mandatory to disable task switches during modifying shared vars
rp2040.resumeOtherCore();
}
void RPI_PICO_ISR_Timer::toggle(unsigned numTimer)
{
if (numTimer >= RPI_PICO_MAX_TIMERS)
{
return;
}
timer[numTimer].enabled = !timer[numTimer].enabled;
}
unsigned RPI_PICO_ISR_Timer::getNumTimers()
{
return numTimers;
}
#endif // ISR_TIMER_GENERIC_IMPL_H
-216
View File
@@ -1,216 +0,0 @@
/****************************************************************************************************************************
RPi_Pico_TimerInterrupt.h
For RP2040-based boards such as RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040.
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/RPI_PICO_TimerInterrupt
Licensed under MIT license
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 even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by
unsigned long miliseconds), you just consume only one RPI_PICO timer and avoid conflicting with other cores' tasks.
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.
Based on SimpleTimer - A timer library for Arduino.
Author: mromani@ottotecnica.com
Copyright (c) 2010 OTTOTECNICA Italy
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Version: 1.1.0
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 11/05/2021 Initial coding to support RP2040-based boards such as RASPBERRY_PI_PICO. etc.
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
*****************************************************************************************************************************/
#pragma once
#ifndef RPI_PICO_TIMERINTERRUPT_H
#define RPI_PICO_TIMERINTERRUPT_H
#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
#endif
#define USING_RPI_PICO_TIMER_INTERRUPT true
#else
#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.1.0"
#endif
#ifndef TIMER_INTERRUPT_DEBUG
#define TIMER_INTERRUPT_DEBUG 0
#endif
#if defined(ARDUINO_ARCH_MBED)
#warning Using MBED RP2040 platform
#include "pico.h"
//#include "pico/stdio.h"
#include "pico/time.h"
#include "hardware/gpio.h"
#include "hardware/uart.h"
#include "hardware/timer.h"
#include "hardware/irq.h"
#else
#warning Using RP2040 platform
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/timer.h"
#include "hardware/irq.h"
#endif
#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
• Enable the appropriate timer interrupt at the processor (see Section 2.3.2)
• Write the time you would like the interrupt to fire to ALARM0 (i.e. the current value in TIMERAWL plus your desired
alarm time in microseconds). Writing the time to the ALARM register sets the ARMED bit as a side effect.
Once the alarm has fired, the ARMED bit will be set to 0 . To clear the latched interrupt, write a 1 to the appropriate bit in
INTR.
*/
class RPI_PICO_TimerInterrupt;
typedef RPI_PICO_TimerInterrupt RPI_PICO_Timer;
// We can use many timers here
#define MAX_RPI_PICO_NUM_TIMERS 4
typedef bool (*pico_timer_callback) (struct repeating_timer *t);
class RPI_PICO_TimerInterrupt
{
private:
uint8_t _timerNo;
pico_timer_callback _callback; // pointer to the callback function
float _frequency; // Timer frequency
uint64_t _timerCount; // count to activate timer, in us
struct repeating_timer _timer;
public:
RPI_PICO_TimerInterrupt(uint8_t timerNo)
{
_timerNo = timerNo;
_callback = NULL;
};
// frequency (in hertz) 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 setFrequency(float frequency, pico_timer_callback callback)
{
if (_timerNo < MAX_RPI_PICO_NUM_TIMERS)
{
// 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 = (float) 1000000;
_timerCount = (uint64_t) _frequency / frequency;
TISR_LOGWARN3(F("RPI_PICO_TimerInterrupt: _timerNo ="), _timerNo, F(", _fre ="), _frequency);
TISR_LOGWARN3(F("_count ="), (uint32_t) (_timerCount >> 32) , F("-"), (uint32_t) (_timerCount));
_callback = callback;
// 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);
cancel_repeating_timer(&_timer);
add_repeating_timer_us(_timerCount, _callback, NULL, &_timer);
TISR_LOGWARN1(F("add_repeating_timer_us ="), _timerCount);
return true;
}
else
{
TISR_LOGERROR(F("Error. Timer must be 0-3"));
return false;
}
}
// 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(unsigned long interval, pico_timer_callback callback)
{
return setFrequency((float) (1000000.0f / interval), callback);
}
bool attachInterrupt(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(unsigned long 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
void reattachInterrupt()
{
add_repeating_timer_us(_timerCount, _callback, NULL, &_timer);
}
// Duration (in milliseconds). Duration = 0 or not specified => run indefinitely
void enableTimer()
{
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);
}
int8_t getTimer() __attribute__((always_inline))
{
return _timerNo;
};
}; // class RPI_PICO_TimerInterrupt
#endif // RPI_PICO_TIMERINTERRUPT_H
-105
View File
@@ -1,105 +0,0 @@
/****************************************************************************************************************************
TimerInterrupt_Generic_Debug.h
For RP2040-based boards such as RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040.
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/RPI_PICO_TimerInterrupt
Licensed under MIT license
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 even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by
unsigned long miliseconds), you just consume only one RPI_PICO timer and avoid conflicting with other cores' tasks.
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.
Based on SimpleTimer - A timer library for Arduino.
Author: mromani@ottotecnica.com
Copyright (c) 2010 OTTOTECNICA Italy
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Version: 1.1.0
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 11/05/2021 Initial coding to support RP2040-based boards such as RASPBERRY_PI_PICO. etc.
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
*****************************************************************************************************************************/
#pragma once
#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
// 2: WARN: errors and warnings
// 3: INFO: errors, warnings and informational (default)
// 4: DEBUG: errors, warnings, informational and debug
#ifndef _TIMERINTERRUPT_LOGLEVEL_
#define _TIMERINTERRUPT_LOGLEVEL_ 1
#endif
//////////////////////////////////////////
const char TISR_MARK[] = "[TISR] ";
const char TISR_SP[] = " ";
#define TISR_PRINT TISR_DBG_PORT.print
#define TISR_PRINTLN TISR_DBG_PORT.println
#define TISR_PRINT_MARK TISR_PRINT(TISR_MARK)
#define TISR_PRINT_SP TISR_PRINT(TISR_SP)
//////////////////////////////////////////
#define TISR_LOGERROR(x) if(_TIMERINTERRUPT_LOGLEVEL_>0) { TISR_PRINT_MARK; TISR_PRINTLN(x); }
#define TISR_LOGERROR0(x) if(_TIMERINTERRUPT_LOGLEVEL_>0) { TISR_PRINT(x); }
#define TISR_LOGERROR1(x,y) if(_TIMERINTERRUPT_LOGLEVEL_>0) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINTLN(y); }
#define TISR_LOGERROR2(x,y,z) if(_TIMERINTERRUPT_LOGLEVEL_>0) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINT(y); TISR_PRINT_SP; TISR_PRINTLN(z); }
#define TISR_LOGERROR3(x,y,z,w) if(_TIMERINTERRUPT_LOGLEVEL_>0) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINT(y); TISR_PRINT_SP; TISR_PRINT(z); TISR_PRINT_SP; TISR_PRINTLN(w); }
//////////////////////////////////////////
#define TISR_LOGWARN(x) if(_TIMERINTERRUPT_LOGLEVEL_>1) { TISR_PRINT_MARK; TISR_PRINTLN(x); }
#define TISR_LOGWARN0(x) if(_TIMERINTERRUPT_LOGLEVEL_>1) { TISR_PRINT(x); }
#define TISR_LOGWARN1(x,y) if(_TIMERINTERRUPT_LOGLEVEL_>1) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINTLN(y); }
#define TISR_LOGWARN2(x,y,z) if(_TIMERINTERRUPT_LOGLEVEL_>1) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINT(y); TISR_PRINT_SP; TISR_PRINTLN(z); }
#define TISR_LOGWARN3(x,y,z,w) if(_TIMERINTERRUPT_LOGLEVEL_>1) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINT(y); TISR_PRINT_SP; TISR_PRINT(z); TISR_PRINT_SP; TISR_PRINTLN(w); }
//////////////////////////////////////////
#define TISR_LOGINFO(x) if(_TIMERINTERRUPT_LOGLEVEL_>2) { TISR_PRINT_MARK; TISR_PRINTLN(x); }
#define TISR_LOGINFO0(x) if(_TIMERINTERRUPT_LOGLEVEL_>2) { TISR_PRINT(x); }
#define TISR_LOGINFO1(x,y) if(_TIMERINTERRUPT_LOGLEVEL_>2) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINTLN(y); }
#define TISR_LOGINFO2(x,y,z) if(_TIMERINTERRUPT_LOGLEVEL_>2) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINT(y); TISR_PRINT_SP; TISR_PRINTLN(z); }
#define TISR_LOGINFO3(x,y,z,w) if(_TIMERINTERRUPT_LOGLEVEL_>2) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINT(y); TISR_PRINT_SP; TISR_PRINT(z); TISR_PRINT_SP; TISR_PRINTLN(w); }
//////////////////////////////////////////
#define TISR_LOGDEBUG(x) if(_TIMERINTERRUPT_LOGLEVEL_>3) { TISR_PRINT_MARK; TISR_PRINTLN(x); }
#define TISR_LOGDEBUG0(x) if(_TIMERINTERRUPT_LOGLEVEL_>3) { TISR_PRINT(x); }
#define TISR_LOGDEBUG1(x,y) if(_TIMERINTERRUPT_LOGLEVEL_>3) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINTLN(y); }
#define TISR_LOGDEBUG2(x,y,z) if(_TIMERINTERRUPT_LOGLEVEL_>3) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINT(y); TISR_PRINT_SP; TISR_PRINTLN(z); }
#define TISR_LOGDEBUG3(x,y,z,w) if(_TIMERINTERRUPT_LOGLEVEL_>3) { TISR_PRINT_MARK; TISR_PRINT(x); TISR_PRINT_SP; TISR_PRINT(y); TISR_PRINT_SP; TISR_PRINT(z); TISR_PRINT_SP; TISR_PRINTLN(w); }
//////////////////////////////////////////
#endif //TIMERINTERRUPT_GENERIC_DEBUG_H