khoih-prog / NRF52_MBED_TimerInterrupt

This library enables you to use Interrupt from Hardware Timers on an NRF52-based board using mbed-RTOS such as Nano-33-BLE. These nRF52 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.
MIT License
16 stars 2 forks source link

NRF52_MBED_Timer's TimerHandler delay #4

Closed klyatskin closed 3 years ago

klyatskin commented 3 years ago
// Arduino Nano 33 BLE

#define TIMER_INTERRUPT_DEBUG         0
#define _TIMERINTERRUPT_LOGLEVEL_     0
#define HW_TIMER_INTERVAL_MS      1

#include "NRF52_MBED_TimerInterrupt.h"
#include "NRF52_MBED_ISR_Timer.h"

NRF52_MBED_Timer ITimer(NRF_TIMER_3);

// Init NRF52 timer NRF_TIMER3
NRF52_MBED_Timer ITimer(NRF_TIMER_3);
// Init NRF52_MBED_ISRTimer
// Each NRF52_MBED_ISRTimer can service 16 different ISR-based timers
NRF52_MBED_ISRTimer ISR_Timer;

#define TIMER_INTERVAL_1s    1000L

volatile static unsigned int count = 0;
void TimerHandler() {
    count += 1;
    if (count % 100000 == 0) // **** should happen every 1 sec? - does not work
        digitalWrite(LED_PWR, !digitalRead(LED_PWR)); 
    ISR_Timer.run();
}

void onTimer1s() { // once per 1 sec. - it works
     digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
   }

void setup() {
    // Interval in microsecs
    if (ITimer.attachInterruptInterval(HW_TIMER_INTERVAL_MS * 1000/100, TimerHandler)) {  // **** every 10 us?
        // Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(millis());
    } else {
       //  Serial.println(F("Can't set ITimer. Select another freq. or timer"));
    }
    ISR_Timer.setInterval(TIMER_INTERVAL_1s, onTimer1s); 
 }

void loop() {
}

For some reason LED_PWR blink are about twice as slow... onTimer1s works as expected.

khoih-prog commented 3 years ago

Interrupt-related code requires better skills and experience and it's better to use only and only if i's absolutely necessary.

Don't use too fast 10uS timer interrupt.

You also have to test using some of the examples first, to fully understand what they're doing before modifying to fit your purpose.

Start from ISR_16_Timers_Array example, then moving on gradually.

I don't want to spend anytime on this issue as it's not the library issue.

Good Luck,

klyatskin commented 3 years ago

Good morning,

This sample code for test blinking works but the higher frequency it has the lower accuracy is reached for the blink period. I used 10us just to illustrate that while the code works the blinking period is about doubled from expected.

What I need is 150us frequency for 50% load pwm. Can I do it accurately through the interrupt? Is there any better way to chat with you?

Thank you anyway for the library. It works well for me with other projects. -- Konstantin

On Mon, Jul 19, 2021 at 11:09 AM Khoi Hoang @.***> wrote:

Interrupt-related code requires better skills and experience and it's better to use only and only if i's absolutely necessary.

Don't use too fast 10uS timer interrupt.

You also have to test using some of the examples first, to fully understand what they're doing before modifying to fit your purpose.

Start from ISR_16_Timers_Array example https://github.com/khoih-prog/NRF52_MBED_TimerInterrupt/tree/main/examples/ISR_16_Timers_Array, then moving on gradually.

I don't want to spend anytime on this issue as it's not the library issue.

Good Luck,

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/khoih-prog/NRF52_MBED_TimerInterrupt/issues/4#issuecomment-882627753, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAMJPTIA4M33JUZSTRUIMGTTYQ53NANCNFSM5AS7JRQA .

khoih-prog commented 3 years ago

Glad to know that it's somehow helpful to you and you're very welcome.

What I need is 150us frequency for 50% load pwm.

You have to write the code yourself or use any PWM library working with MBED Nano-33-BLE. If there is no ready-made library, just port any PWM one to NRF52 MBED platform.

It all depends on your code (using how many MCU cycles, etc. ) and the core. Also don't use time-consuming ISR_Timer.setInterval() which is designed for slow-pacing jobs.

Is there any better way to chat with you?

Sorry, I won't do this. You're on your own