Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
090eadf7ba | ||
|
|
08b28e29c3 | ||
|
|
44c3602009 | ||
|
|
8b4f5220e6 | ||
|
|
3c0d65a3ad | ||
|
|
3d0e9831b3 | ||
|
|
7f5d92deab | ||
|
|
8fab85d670 | ||
|
|
e764e8fb1a | ||
|
|
0747f36f22 | ||
|
|
05c0eef7cd |
+5
-5
@@ -14,8 +14,8 @@ If you don't find anything, please [open a new issue](https://github.com/khoih-p
|
||||
|
||||
Please ensure to specify the following:
|
||||
|
||||
* Arduino IDE version (e.g. 1.8.16) or Platform.io version
|
||||
* `RP2040` Core Version (e.g. RP2040 core v1.9.5)
|
||||
* Arduino IDE version (e.g. 1.8.19) or Platform.io version
|
||||
* `RP2040` Core Version (e.g. RP2040 core v1.9.14)
|
||||
* `RP2040` Board type (e.g. RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040, GENERIC_RP2040, etc.)
|
||||
* Contextual information (e.g. what you were trying to achieve)
|
||||
* Simplest possible steps to reproduce
|
||||
@@ -27,11 +27,11 @@ Please ensure to specify the following:
|
||||
### Example
|
||||
|
||||
```
|
||||
Arduino IDE version: 1.8.16
|
||||
RP2040 core v1.9.5
|
||||
Arduino IDE version: 1.8.19
|
||||
RP2040 core v1.9.14
|
||||
RASPBERRY_PI_PICO Module
|
||||
OS: Ubuntu 20.04 LTS
|
||||
Linux xy-Inspiron-3593 5.4.0-86-generic #97-Ubuntu SMP Fri Sep 17 19:19:40 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
|
||||
Linux xy-Inspiron-3593 5.4.0-96-generic #109-Ubuntu SMP Wed Jan 12 16:49:16 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
|
||||
|
||||
Context:
|
||||
I encountered a crash while using TimerInterrupt.
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
## 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)
|
||||
@@ -47,6 +48,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)
|
||||
@@ -68,6 +70,10 @@
|
||||
---
|
||||
---
|
||||
|
||||
### 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
|
||||
@@ -111,7 +117,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, ADAFRUIT_FEATHER_RP2040, Nano_RP2040_Connect, GENERIC_RP2040**, etc.
|
||||
|
||||
---
|
||||
|
||||
@@ -128,9 +134,12 @@ The catch is **your function is now part of an ISR (Interrupt Service Routine),
|
||||
|
||||
## Prerequisites
|
||||
|
||||
1. [`Arduino IDE 1.8.16+` for Arduino](https://www.arduino.cc/en/Main/Software)
|
||||
2. [`Earle Philhower's arduino-pico core v1.9.5+`](https://github.com/earlephilhower/arduino-pico) for RP2040-based boards such as **RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040**, etc. [](https://github.com/earlephilhower/arduino-pico/releases/latest)
|
||||
|
||||
1. [`Arduino IDE 1.8.19+` for Arduino](https://github.com/arduino/Arduino). [](https://github.com/arduino/Arduino/releases/latest)
|
||||
2. [`Earle Philhower's arduino-pico core v1.9.14+`](https://github.com/earlephilhower/arduino-pico) for RP2040-based boards such as **RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040**, etc. [](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.
|
||||
|
||||
|
||||
---
|
||||
---
|
||||
|
||||
@@ -202,24 +211,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.
|
||||
|
||||
---
|
||||
---
|
||||
@@ -425,6 +436,7 @@ void setup()
|
||||
7. [RPM_Measure](examples/RPM_Measure)
|
||||
8. [SwitchDebounce](examples/SwitchDebounce)
|
||||
9. [TimerInterruptTest](examples/TimerInterruptTest)
|
||||
10. [**multiFileProject**](examples/multiFileProject). **New**
|
||||
|
||||
---
|
||||
---
|
||||
@@ -438,10 +450,13 @@ void setup()
|
||||
#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);
|
||||
@@ -539,18 +554,19 @@ void setup()
|
||||
Serial.begin(115200);
|
||||
while (!Serial);
|
||||
|
||||
Serial.print(F("\nStarting ISR_Timers_Array_Simple on "));
|
||||
Serial.println(BOARD_NAME);
|
||||
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))
|
||||
if (ITimer1.attachInterruptInterval(TIMER_INTERVAL_MS * 1000, TimerHandler))
|
||||
{
|
||||
Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(millis());
|
||||
}
|
||||
else
|
||||
Serial.println(F("Can't set ITimer1. Select another freq. or timer"));
|
||||
|
||||
previousMillis5s = previousMillis2s = millis();
|
||||
|
||||
ISR_timer.setInterval(2000L, doingSomething2s);
|
||||
ISR_timer.setInterval(5000L, doingSomething5s);
|
||||
|
||||
@@ -585,15 +601,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.1
|
||||
RPi_Pico_TimerInterrupt v1.2.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
|
||||
@@ -615,15 +628,15 @@ The following is the sample terminal output when running example [TimerInterrupt
|
||||
|
||||
```
|
||||
Starting TimerInterruptTest on RASPBERRY_PI_PICO
|
||||
RPi_Pico_TimerInterrupt v1.1.1
|
||||
RPi_Pico_TimerInterrupt v1.2.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
|
||||
@@ -659,14 +672,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.1
|
||||
RPi_Pico_TimerInterrupt v1.2.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
|
||||
@@ -740,10 +753,10 @@ The following is the sample terminal output when running example [SwitchDebounce
|
||||
|
||||
```
|
||||
Starting SwitchDebounce on RASPBERRY_PI_PICO
|
||||
RPi_Pico_TimerInterrupt v1.1.1
|
||||
RPi_Pico_TimerInterrupt v1.2.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
|
||||
@@ -764,12 +777,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.1
|
||||
Starting ISR_Timers_Array_Simple on RASPBERRY_PI_PICO
|
||||
RPi_Pico_TimerInterrupt v1.2.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
|
||||
@@ -788,12 +800,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.1
|
||||
Starting ISR_16_Timers_Array_Complex on RASPBERRY_PI_PICO
|
||||
RPi_Pico_TimerInterrupt v1.2.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,7 +994,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`
|
||||
|
||||
|
||||
---
|
||||
---
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
## Table of Contents
|
||||
|
||||
* [Changelog](#changelog)
|
||||
* [Releases v1.2.0](#releases-v120)
|
||||
* [Releases v1.1.1](#releases-v111)
|
||||
* [Releases v1.1.0](#releases-v110)
|
||||
* [Releases v1.0.1](#releases-v101)
|
||||
@@ -23,6 +24,13 @@
|
||||
|
||||
## Changelog
|
||||
|
||||
### 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`
|
||||
|
||||
@@ -18,13 +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
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
// These define's must be placed at the beginning before #include "TimerInterrupt_Generic.h"
|
||||
@@ -33,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)
|
||||
|
||||
@@ -18,13 +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
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
/*
|
||||
@@ -46,6 +39,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"
|
||||
|
||||
#ifndef LED_BUILTIN
|
||||
|
||||
@@ -18,13 +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
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
// These define's must be placed at the beginning before #include "TimerInterrupt_Generic.h"
|
||||
@@ -33,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)
|
||||
|
||||
@@ -18,13 +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
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
/*
|
||||
@@ -45,6 +38,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"
|
||||
|
||||
#ifndef LED_BUILTIN
|
||||
|
||||
@@ -18,13 +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
|
||||
*****************************************************************************************************************************/
|
||||
/*
|
||||
Notes:
|
||||
@@ -50,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
|
||||
@@ -71,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,13 +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
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
// These define's must be placed at the beginning before #include "TimerInterrupt_Generic.h"
|
||||
@@ -33,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);
|
||||
|
||||
@@ -18,13 +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
|
||||
*****************************************************************************************************************************/
|
||||
/*
|
||||
Notes:
|
||||
@@ -53,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
|
||||
|
||||
@@ -18,13 +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
|
||||
*****************************************************************************************************************************/
|
||||
/*
|
||||
Notes:
|
||||
@@ -50,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,13 +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
|
||||
*****************************************************************************************************************************/
|
||||
/*
|
||||
Notes:
|
||||
@@ -44,6 +37,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"
|
||||
|
||||
#ifndef LED_BUILTIN
|
||||
|
||||
@@ -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:
|
||||
}
|
||||
@@ -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
|
||||
|
||||
+3
-2
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "RPI_PICO_TimerInterrupt",
|
||||
"version": "1.1.1",
|
||||
"version": "1.2.0",
|
||||
"keywords": "timing, device, control, timer, interrupt, hardware, isr, isr-based, hardware-timer, mission-critical, accuracy, precise, non-blocking, raspberry-pico, rpi-pico, pico, rp2040, arduino-pico",
|
||||
"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":
|
||||
@@ -22,8 +22,9 @@
|
||||
"tests"
|
||||
]
|
||||
},
|
||||
"license": "MIT",
|
||||
"frameworks": "*",
|
||||
"platforms": ["raspberrypi"],
|
||||
"examples": "examples/*/*/*.ino",
|
||||
"license": "MIT"
|
||||
"headers": ["RPi_Pico_TimerInterrupt.h", "RPi_Pico_ISR_Timer.h", "RPi_Pico_ISR_Timer.hpp"]
|
||||
}
|
||||
|
||||
+3
-3
@@ -1,12 +1,12 @@
|
||||
name=RPI_PICO_TimerInterrupt
|
||||
version=1.1.1
|
||||
version=1.2.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.
|
||||
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
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
Based on BlynkTimer.h
|
||||
Author: Volodymyr Shymanskyy
|
||||
|
||||
Version: 1.1.1
|
||||
Version: 1.2.0
|
||||
|
||||
Version Modified By Date Comments
|
||||
------- ----------- ---------- -----------
|
||||
@@ -33,6 +33,7 @@
|
||||
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.
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#pragma once
|
||||
@@ -160,7 +161,7 @@ int RPI_PICO_ISR_Timer::findFirstFreeSlot()
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
|
||||
@@ -194,37 +195,37 @@ int RPI_PICO_ISR_Timer::setupTimer(unsigned long d, void* f, void* p, bool h, un
|
||||
}
|
||||
|
||||
|
||||
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)
|
||||
{
|
||||
@@ -250,7 +251,7 @@ 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)
|
||||
{
|
||||
@@ -282,7 +283,7 @@ 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)
|
||||
{
|
||||
@@ -299,7 +300,7 @@ void RPI_PICO_ISR_Timer::restartTimer(unsigned numTimer)
|
||||
}
|
||||
|
||||
|
||||
bool RPI_PICO_ISR_Timer::isEnabled(unsigned numTimer)
|
||||
bool RPI_PICO_ISR_Timer::isEnabled(const unsigned& numTimer)
|
||||
{
|
||||
if (numTimer >= RPI_PICO_MAX_TIMERS)
|
||||
{
|
||||
@@ -310,7 +311,7 @@ bool RPI_PICO_ISR_Timer::isEnabled(unsigned numTimer)
|
||||
}
|
||||
|
||||
|
||||
void RPI_PICO_ISR_Timer::enable(unsigned numTimer)
|
||||
void RPI_PICO_ISR_Timer::enable(const unsigned& numTimer)
|
||||
{
|
||||
if (numTimer >= RPI_PICO_MAX_TIMERS)
|
||||
{
|
||||
@@ -321,7 +322,7 @@ void RPI_PICO_ISR_Timer::enable(unsigned numTimer)
|
||||
}
|
||||
|
||||
|
||||
void RPI_PICO_ISR_Timer::disable(unsigned numTimer)
|
||||
void RPI_PICO_ISR_Timer::disable(const unsigned& numTimer)
|
||||
{
|
||||
if (numTimer >= RPI_PICO_MAX_TIMERS)
|
||||
{
|
||||
@@ -370,7 +371,7 @@ 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)
|
||||
{
|
||||
|
||||
+3
-154
@@ -25,7 +25,7 @@
|
||||
Based on BlynkTimer.h
|
||||
Author: Volodymyr Shymanskyy
|
||||
|
||||
Version: 1.1.1
|
||||
Version: 1.2.0
|
||||
|
||||
Version Modified By Date Comments
|
||||
------- ----------- ---------- -----------
|
||||
@@ -33,6 +33,7 @@
|
||||
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.
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#pragma once
|
||||
@@ -40,159 +41,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.1"
|
||||
#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,7 +25,7 @@
|
||||
Based on BlynkTimer.h
|
||||
Author: Volodymyr Shymanskyy
|
||||
|
||||
Version: 1.1.1
|
||||
Version: 1.2.0
|
||||
|
||||
Version Modified By Date Comments
|
||||
------- ----------- ---------- -----------
|
||||
@@ -33,12 +33,13 @@
|
||||
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.
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#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)
|
||||
@@ -50,7 +51,13 @@
|
||||
#endif
|
||||
|
||||
#ifndef RPI_PICO_TIMER_INTERRUPT_VERSION
|
||||
#define RPI_PICO_TIMER_INTERRUPT_VERSION "RPi_Pico_TimerInterrupt v1.1.1"
|
||||
#define RPI_PICO_TIMER_INTERRUPT_VERSION "RPi_Pico_TimerInterrupt v1.2.0"
|
||||
|
||||
#define RPI_PICO_TIMER_INTERRUPT_VERSION_MAJOR 1
|
||||
#define RPI_PICO_TIMER_INTERRUPT_VERSION_MINOR 2
|
||||
#define RPI_PICO_TIMER_INTERRUPT_VERSION_PATCH 0
|
||||
|
||||
#define RPI_PICO_TIMER_INTERRUPT_VERSION_INT 1002000
|
||||
#endif
|
||||
|
||||
#include "TimerInterrupt_Generic_Debug.h"
|
||||
@@ -96,50 +103,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();
|
||||
@@ -148,7 +155,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();
|
||||
@@ -168,7 +175,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();
|
||||
@@ -193,8 +200,6 @@ class RPI_PICO_ISR_Timer
|
||||
};
|
||||
|
||||
|
||||
#include "RPi_Pico_ISR_Timer-Impl.h"
|
||||
|
||||
#endif // ISR_TIMER_GENERIC_H
|
||||
#endif // ISR_TIMER_GENERIC_HPP
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
Based on BlynkTimer.h
|
||||
Author: Volodymyr Shymanskyy
|
||||
|
||||
Version: 1.1.1
|
||||
Version: 1.2.0
|
||||
|
||||
Version Modified By Date Comments
|
||||
------- ----------- ---------- -----------
|
||||
@@ -34,6 +34,7 @@
|
||||
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.
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#pragma once
|
||||
@@ -51,30 +52,25 @@
|
||||
#endif
|
||||
|
||||
#ifndef RPI_PICO_TIMER_INTERRUPT_VERSION
|
||||
#define RPI_PICO_TIMER_INTERRUPT_VERSION "RPi_Pico_TimerInterrupt v1.1.1"
|
||||
#define RPI_PICO_TIMER_INTERRUPT_VERSION "RPi_Pico_TimerInterrupt v1.2.0"
|
||||
|
||||
#define RPI_PICO_TIMER_INTERRUPT_VERSION_MAJOR 1
|
||||
#define RPI_PICO_TIMER_INTERRUPT_VERSION_MINOR 2
|
||||
#define RPI_PICO_TIMER_INTERRUPT_VERSION_PATCH 0
|
||||
|
||||
#define RPI_PICO_TIMER_INTERRUPT_VERSION_INT 1002000
|
||||
#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"
|
||||
|
||||
@@ -107,7 +103,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
|
||||
uint64_t _timerCount; // count to activate timer, in us
|
||||
|
||||
struct repeating_timer _timer;
|
||||
|
||||
@@ -118,20 +114,29 @@ class RPI_PICO_TimerInterrupt
|
||||
_timerNo = timerNo;
|
||||
_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 = (uint64_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;
|
||||
|
||||
@@ -141,7 +146,7 @@ class RPI_PICO_TimerInterrupt
|
||||
cancel_repeating_timer(&_timer);
|
||||
add_repeating_timer_us(_timerCount, _callback, NULL, &_timer);
|
||||
|
||||
TISR_LOGWARN1(F("add_repeating_timer_us ="), _timerCount);
|
||||
TISR_LOGWARN1(F("add_repeating_timer_us = "), _timerCount);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -155,19 +160,19 @@ 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);
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
Based on BlynkTimer.h
|
||||
Author: Volodymyr Shymanskyy
|
||||
|
||||
Version: 1.1.1
|
||||
Version: 1.2.0
|
||||
|
||||
Version Modified By Date Comments
|
||||
------- ----------- ---------- -----------
|
||||
@@ -33,6 +33,7 @@
|
||||
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.
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#pragma once
|
||||
@@ -68,39 +69,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
|
||||
|
||||
@@ -1,386 +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.1
|
||||
|
||||
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
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,197 +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.1
|
||||
|
||||
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
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#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.1"
|
||||
#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
|
||||
|
||||
|
||||
@@ -1,217 +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.1
|
||||
|
||||
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
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#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.1"
|
||||
#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
|
||||
|
||||
@@ -1,106 +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.1
|
||||
|
||||
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
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#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
|
||||
@@ -1,390 +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.1
|
||||
|
||||
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
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#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
|
||||
|
||||
@@ -1,217 +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.1
|
||||
|
||||
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
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#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.1"
|
||||
#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
|
||||
|
||||
@@ -1,106 +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.1
|
||||
|
||||
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
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#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
|
||||
Reference in New Issue
Block a user