v1.3.0 to fix severe bug affecting start time

### Releases v1.3.0

1. Fix  affecting time between the starts. Check [Enable fixed timing between timer calls (vs fixed time btw. end of timer call and next call as implemented) #3](https://github.com/khoih-prog/RPI_PICO_TimerInterrupt/issues/3)
This commit is contained in:
Khoi Hoang
2022-09-25 19:41:28 -04:00
committed by GitHub
parent f2ca9ced6e
commit b99aafc23a
14 changed files with 171 additions and 198 deletions
+40 -2
View File
@@ -25,7 +25,7 @@
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Version: 1.2.0
Version: 1.3.0
Version Modified By Date Comments
------- ----------- ---------- -----------
@@ -33,7 +33,8 @@
1.0.1 K Hoang 18/05/2021 Update README and Packages' Patches to match core arduino-pico core v1.4.0
1.1.0 K Hoang 10/00/2021 Add support to new boards using the arduino-pico core
1.1.1 K Hoang 22/10/2021 Fix platform in library.json for PIO
1.2.0 K.Hoang 21/01/2022 Fix `multiple-definitions` linker error.
1.2.0 K.Hoang 21/01/2022 Fix `multiple-definitions` linker error
1.3.0 K.Hoang 25/09/2022 Fix severe bug affecting time between the starts
*****************************************************************************************************************************/
#pragma once
@@ -43,11 +44,15 @@
#include <string.h>
////////////////////////////////////////////////////////////////
RPI_PICO_ISR_Timer::RPI_PICO_ISR_Timer()
: numTimers (-1)
{
}
////////////////////////////////////////////////////////////////
void RPI_PICO_ISR_Timer::init()
{
unsigned long current_millis = millis(); //elapsed();
@@ -61,6 +66,8 @@ void RPI_PICO_ISR_Timer::init()
numTimers = 0;
}
////////////////////////////////////////////////////////////////
void RPI_PICO_ISR_Timer::run()
{
uint8_t i;
@@ -136,6 +143,7 @@ void RPI_PICO_ISR_Timer::run()
}
////////////////////////////////////////////////////////////////
// find the first available slot
// return -1 if none found
@@ -160,6 +168,7 @@ int RPI_PICO_ISR_Timer::findFirstFreeSlot()
return -1;
}
////////////////////////////////////////////////////////////////
int RPI_PICO_ISR_Timer::setupTimer(const unsigned long& d, void* f, void* p, bool h, const unsigned& n)
{
@@ -194,37 +203,50 @@ int RPI_PICO_ISR_Timer::setupTimer(const unsigned long& d, void* f, void* p, boo
return freeTimer;
}
////////////////////////////////////////////////////////////////
int RPI_PICO_ISR_Timer::setTimer(const unsigned long& d, timer_callback f, const unsigned& n)
{
return setupTimer(d, (void *)f, NULL, false, n);
}
////////////////////////////////////////////////////////////////
int RPI_PICO_ISR_Timer::setTimer(const unsigned long& d, timer_callback_p f, void* p, const unsigned& n)
{
return setupTimer(d, (void *)f, p, true, n);
}
////////////////////////////////////////////////////////////////
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(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(const unsigned long& d, timer_callback f)
{
return setupTimer(d, (void *)f, NULL, false, RPI_PICO_RUN_ONCE);
}
////////////////////////////////////////////////////////////////
int RPI_PICO_ISR_Timer::setTimeout(const unsigned long& d, timer_callback_p f, void* p)
{
return setupTimer(d, (void *)f, p, true, RPI_PICO_RUN_ONCE);
}
////////////////////////////////////////////////////////////////
bool RPI_PICO_ISR_Timer::changeInterval(const unsigned& numTimer, const unsigned long& d)
{
if (numTimer >= RPI_PICO_MAX_TIMERS)
@@ -251,6 +273,8 @@ bool RPI_PICO_ISR_Timer::changeInterval(const unsigned& numTimer, const unsigned
return false;
}
////////////////////////////////////////////////////////////////
void RPI_PICO_ISR_Timer::deleteTimer(const unsigned& timerId)
{
if (timerId >= RPI_PICO_MAX_TIMERS)
@@ -282,6 +306,8 @@ void RPI_PICO_ISR_Timer::deleteTimer(const unsigned& timerId)
}
}
////////////////////////////////////////////////////////////////
// function contributed by code@rowansimms.com
void RPI_PICO_ISR_Timer::restartTimer(const unsigned& numTimer)
{
@@ -299,6 +325,7 @@ void RPI_PICO_ISR_Timer::restartTimer(const unsigned& numTimer)
rp2040.resumeOtherCore();
}
////////////////////////////////////////////////////////////////
bool RPI_PICO_ISR_Timer::isEnabled(const unsigned& numTimer)
{
@@ -310,6 +337,7 @@ bool RPI_PICO_ISR_Timer::isEnabled(const unsigned& numTimer)
return timer[numTimer].enabled;
}
////////////////////////////////////////////////////////////////
void RPI_PICO_ISR_Timer::enable(const unsigned& numTimer)
{
@@ -321,6 +349,7 @@ void RPI_PICO_ISR_Timer::enable(const unsigned& numTimer)
timer[numTimer].enabled = true;
}
////////////////////////////////////////////////////////////////
void RPI_PICO_ISR_Timer::disable(const unsigned& numTimer)
{
@@ -332,6 +361,8 @@ void RPI_PICO_ISR_Timer::disable(const unsigned& numTimer)
timer[numTimer].enabled = false;
}
////////////////////////////////////////////////////////////////
void RPI_PICO_ISR_Timer::enableAll()
{
// Enable all timers with a callback assigned (used)
@@ -351,6 +382,8 @@ void RPI_PICO_ISR_Timer::enableAll()
rp2040.resumeOtherCore();
}
////////////////////////////////////////////////////////////////
void RPI_PICO_ISR_Timer::disableAll()
{
// Disable all timers with a callback assigned (used)
@@ -371,6 +404,8 @@ void RPI_PICO_ISR_Timer::disableAll()
}
////////////////////////////////////////////////////////////////
void RPI_PICO_ISR_Timer::toggle(const unsigned& numTimer)
{
if (numTimer >= RPI_PICO_MAX_TIMERS)
@@ -381,11 +416,14 @@ void RPI_PICO_ISR_Timer::toggle(const unsigned& numTimer)
timer[numTimer].enabled = !timer[numTimer].enabled;
}
////////////////////////////////////////////////////////////////
unsigned RPI_PICO_ISR_Timer::getNumTimers()
{
return numTimers;
}
////////////////////////////////////////////////////////////////
#endif // ISR_TIMER_GENERIC_IMPL_H
+3 -2
View File
@@ -25,7 +25,7 @@
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Version: 1.2.0
Version: 1.3.0
Version Modified By Date Comments
------- ----------- ---------- -----------
@@ -33,7 +33,8 @@
1.0.1 K Hoang 18/05/2021 Update README and Packages' Patches to match core arduino-pico core v1.4.0
1.1.0 K Hoang 10/00/2021 Add support to new boards using the arduino-pico core
1.1.1 K Hoang 22/10/2021 Fix platform in library.json for PIO
1.2.0 K.Hoang 21/01/2022 Fix `multiple-definitions` linker error.
1.2.0 K.Hoang 21/01/2022 Fix `multiple-definitions` linker error
1.3.0 K.Hoang 25/09/2022 Fix severe bug affecting time between the starts
*****************************************************************************************************************************/
#pragma once
+15 -6
View File
@@ -25,7 +25,7 @@
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Version: 1.2.0
Version: 1.3.0
Version Modified By Date Comments
------- ----------- ---------- -----------
@@ -33,7 +33,8 @@
1.0.1 K Hoang 18/05/2021 Update README and Packages' Patches to match core arduino-pico core v1.4.0
1.1.0 K Hoang 10/00/2021 Add support to new boards using the arduino-pico core
1.1.1 K Hoang 22/10/2021 Fix platform in library.json for PIO
1.2.0 K.Hoang 21/01/2022 Fix `multiple-definitions` linker error.
1.2.0 K.Hoang 21/01/2022 Fix `multiple-definitions` linker error
1.3.0 K.Hoang 25/09/2022 Fix severe bug affecting time between the starts
*****************************************************************************************************************************/
#pragma once
@@ -41,6 +42,8 @@
#ifndef ISR_TIMER_GENERIC_HPP
#define ISR_TIMER_GENERIC_HPP
////////////////////////////////////////////////////////////////
#if ( defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_RASPBERRY_PI_PICO) || defined(ARDUINO_ADAFRUIT_FEATHER_RP2040) || defined(ARDUINO_GENERIC_RP2040) ) && !defined(ARDUINO_ARCH_MBED)
#if defined(USING_RPI_PICO_TIMER_INTERRUPT)
#undef USING_RPI_PICO_TIMER_INTERRUPT
@@ -50,16 +53,20 @@
#error This code is intended to run on the non-mbed RP2040 arduino-pico platform! Please check your Tools->Board setting.
#endif
////////////////////////////////////////////////////////////////
#ifndef RPI_PICO_TIMER_INTERRUPT_VERSION
#define RPI_PICO_TIMER_INTERRUPT_VERSION "RPi_Pico_TimerInterrupt v1.2.0"
#define RPI_PICO_TIMER_INTERRUPT_VERSION "RPi_Pico_TimerInterrupt v1.3.0"
#define RPI_PICO_TIMER_INTERRUPT_VERSION_MAJOR 1
#define RPI_PICO_TIMER_INTERRUPT_VERSION_MINOR 2
#define RPI_PICO_TIMER_INTERRUPT_VERSION_MINOR 3
#define RPI_PICO_TIMER_INTERRUPT_VERSION_PATCH 0
#define RPI_PICO_TIMER_INTERRUPT_VERSION_INT 1002000
#define RPI_PICO_TIMER_INTERRUPT_VERSION_INT 1003000
#endif
////////////////////////////////////////////////////////////////
#include "TimerInterrupt_Generic_Debug.h"
#include <stddef.h>
@@ -83,9 +90,10 @@
typedef void (*timer_callback)();
typedef void (*timer_callback_p)(void *);
////////////////////////////////////////////////////////////////
class RPI_PICO_ISR_Timer
{
public:
// maximum number of timers
#define RPI_PICO_MAX_TIMERS 16
@@ -199,6 +207,7 @@ class RPI_PICO_ISR_Timer
volatile int numTimers;
};
////////////////////////////////////////////////////////////////
#endif // ISR_TIMER_GENERIC_HPP
+57 -14
View File
@@ -26,7 +26,7 @@
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Version: 1.2.0
Version: 1.3.0
Version Modified By Date Comments
------- ----------- ---------- -----------
@@ -34,7 +34,8 @@
1.0.1 K Hoang 18/05/2021 Update README and Packages' Patches to match core arduino-pico core v1.4.0
1.1.0 K Hoang 10/00/2021 Add support to new boards using the arduino-pico core
1.1.1 K Hoang 22/10/2021 Fix platform in library.json for PIO
1.2.0 K.Hoang 21/01/2022 Fix `multiple-definitions` linker error.
1.2.0 K.Hoang 21/01/2022 Fix `multiple-definitions` linker error
1.3.0 K.Hoang 25/09/2022 Fix severe bug affecting time between the starts
*****************************************************************************************************************************/
#pragma once
@@ -51,20 +52,26 @@
#error This code is intended to run on the non-mbed RP2040 arduino-pico platform! Please check your Tools->Board setting.
#endif
////////////////////////////////////////////////////////////////
#ifndef RPI_PICO_TIMER_INTERRUPT_VERSION
#define RPI_PICO_TIMER_INTERRUPT_VERSION "RPi_Pico_TimerInterrupt v1.2.0"
#define RPI_PICO_TIMER_INTERRUPT_VERSION "RPi_Pico_TimerInterrupt v1.3.0"
#define RPI_PICO_TIMER_INTERRUPT_VERSION_MAJOR 1
#define RPI_PICO_TIMER_INTERRUPT_VERSION_MINOR 2
#define RPI_PICO_TIMER_INTERRUPT_VERSION_MINOR 3
#define RPI_PICO_TIMER_INTERRUPT_VERSION_PATCH 0
#define RPI_PICO_TIMER_INTERRUPT_VERSION_INT 1002000
#define RPI_PICO_TIMER_INTERRUPT_VERSION_INT 1003000
#endif
////////////////////////////////////////////////////////////////
#ifndef TIMER_INTERRUPT_DEBUG
#define TIMER_INTERRUPT_DEBUG 0
#endif
////////////////////////////////////////////////////////////////
#include "Arduino.h"
#include <stdio.h>
@@ -74,6 +81,8 @@
#include "TimerInterrupt_Generic_Debug.h"
////////////////////////////////////////////////////////////////
/*
To enable an alarm:
• Enable the interrupt at the timer with a write to the appropriate alarm bit in INTE: i.e. (1 << 0) for ALARM0
@@ -94,6 +103,7 @@ typedef RPI_PICO_TimerInterrupt RPI_PICO_Timer;
typedef bool (*pico_timer_callback) (struct repeating_timer *t);
////////////////////////////////////////////////////////////////
class RPI_PICO_TimerInterrupt
{
@@ -103,7 +113,7 @@ class RPI_PICO_TimerInterrupt
pico_timer_callback _callback; // pointer to the callback function
float _frequency; // Timer frequency
uint64_t _timerCount; // count to activate timer, in us
int64_t _timerCount; // count to activate timer, in us
struct repeating_timer _timer;
@@ -114,6 +124,8 @@ class RPI_PICO_TimerInterrupt
_timerNo = timerNo;
_callback = NULL;
};
////////////////////////////////////////////////////////////////
#define TIM_CLOCK_FREQ ( (float) 1000000.0f )
@@ -133,7 +145,7 @@ class RPI_PICO_TimerInterrupt
// select timer frequency is 1MHz for better accuracy. We don't use 16-bit prescaler for now.
// Will use later if very low frequency is needed.
_frequency = frequency;
_timerCount = (uint64_t) TIM_CLOCK_FREQ / frequency;
_timerCount = (int64_t) (TIM_CLOCK_FREQ / frequency);
TISR_LOGWARN5(F("_timerNo = "), _timerNo, F(", Clock (Hz) = "), TIM_CLOCK_FREQ, F(", _fre (Hz) = "), _frequency);
TISR_LOGWARN3(F("_count = "), (uint32_t) (_timerCount >> 32) , F("-"), (uint32_t) (_timerCount));
@@ -143,10 +155,18 @@ class RPI_PICO_TimerInterrupt
// static bool add_repeating_timer_us(int64_t delay_us, repeating_timer_callback_t callback, void *user_data, repeating_timer_t *out);
// static bool add_repeating_timer_ms(int64_t delay_ms, repeating_timer_callback_t callback, void *user_data, repeating_timer_t *out);
// bool cancel_repeating_timer (repeating_timer_t *timer);
//////////////////////////////////////////////////////////////////////////
// Important Notes
// delay_ms the repeat delay in milliseconds; if >0 then this is the delay between one callback ending and the next
// starting; if <0 then this is the negative of the time between the starts of the callbacks.
// The value of 0 is treated as 1 microsecond
//////////////////////////////////////////////////////////////////////////
cancel_repeating_timer(&_timer);
add_repeating_timer_us(_timerCount, _callback, NULL, &_timer);
// Use negative value to select time between the starts of the callbacks
add_repeating_timer_us(-(_timerCount), _callback, NULL, &_timer);
TISR_LOGWARN1(F("add_repeating_timer_us = "), _timerCount);
TISR_LOGWARN1(F("add_repeating_timer_us between starts = "), _timerCount);
return true;
}
@@ -158,6 +178,8 @@ class RPI_PICO_TimerInterrupt
}
}
////////////////////////////////////////////////////////////////
// interval (in microseconds) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely
// No params and duration now. To be added in the future by adding similar functions here
bool setInterval(const unsigned long& interval, pico_timer_callback callback)
@@ -165,11 +187,15 @@ class RPI_PICO_TimerInterrupt
return setFrequency((float) (1000000.0f / interval), callback);
}
////////////////////////////////////////////////////////////////
bool attachInterrupt(const float& frequency, pico_timer_callback callback)
{
return setFrequency(frequency, callback);
}
////////////////////////////////////////////////////////////////
// interval (in microseconds) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely
// No params and duration now. To be added in the future by adding similar functions here
bool attachInterruptInterval(const unsigned long& interval, pico_timer_callback callback)
@@ -177,45 +203,62 @@ class RPI_PICO_TimerInterrupt
return setFrequency( (float) ( 1000000.0f / interval), callback);
}
////////////////////////////////////////////////////////////////
void detachInterrupt()
{
cancel_repeating_timer(&_timer);
}
////////////////////////////////////////////////////////////////
void disableTimer()
{
cancel_repeating_timer(&_timer);
}
// Duration (in milliseconds). Duration = 0 or not specified => run indefinitely
////////////////////////////////////////////////////////////////
// Duration (in microseconds). Duration = 0 or not specified => run indefinitely
void reattachInterrupt()
{
add_repeating_timer_us(_timerCount, _callback, NULL, &_timer);
add_repeating_timer_us(-(_timerCount), _callback, NULL, &_timer);
}
// Duration (in milliseconds). Duration = 0 or not specified => run indefinitely
////////////////////////////////////////////////////////////////
// Duration (in microseconds). Duration = 0 or not specified => run indefinitely
void enableTimer()
{
add_repeating_timer_us(_timerCount, _callback, NULL, &_timer);
add_repeating_timer_us(-(_timerCount), _callback, NULL, &_timer);
}
////////////////////////////////////////////////////////////////
// Just stop clock source, clear the count
void stopTimer()
{
cancel_repeating_timer(&_timer);
}
////////////////////////////////////////////////////////////////
// Just reconnect clock source, start current count from 0
void restartTimer()
{
cancel_repeating_timer(&_timer);
add_repeating_timer_us(_timerCount, _callback, NULL, &_timer);
add_repeating_timer_us(-(_timerCount), _callback, NULL, &_timer);
}
////////////////////////////////////////////////////////////////
int8_t getTimer() __attribute__((always_inline))
{
return _timerNo;
};
////////////////////////////////////////////////////////////////
}; // class RPI_PICO_TimerInterrupt
#endif // RPI_PICO_TIMERINTERRUPT_H
+7 -2
View File
@@ -25,7 +25,7 @@
Based on BlynkTimer.h
Author: Volodymyr Shymanskyy
Version: 1.2.0
Version: 1.3.0
Version Modified By Date Comments
------- ----------- ---------- -----------
@@ -33,7 +33,8 @@
1.0.1 K Hoang 18/05/2021 Update README and Packages' Patches to match core arduino-pico core v1.4.0
1.1.0 K Hoang 10/00/2021 Add support to new boards using the arduino-pico core
1.1.1 K Hoang 22/10/2021 Fix platform in library.json for PIO
1.2.0 K.Hoang 21/01/2022 Fix `multiple-definitions` linker error.
1.2.0 K.Hoang 21/01/2022 Fix `multiple-definitions` linker error
1.3.0 K.Hoang 25/09/2022 Fix severe bug affecting time between the starts
*****************************************************************************************************************************/
#pragma once
@@ -41,12 +42,16 @@
#ifndef TIMERINTERRUPT_GENERIC_DEBUG_H
#define TIMERINTERRUPT_GENERIC_DEBUG_H
////////////////////////////////////////////////////////////////
#ifdef TIMERINTERRUPT_DEBUG_PORT
#define TISR_DBG_PORT TIMERINTERRUPT_DEBUG_PORT
#else
#define TISR_DBG_PORT Serial
#endif
////////////////////////////////////////////////////////////////
// Change _TIMERINTERRUPT_LOGLEVEL_ to set tracing and logging verbosity
// 0: DISABLED: no logging
// 1: ERROR: errors