|
| 1 | +/**************************************************************************************************************************** |
| 2 | + Change_Interval.ino |
| 3 | + For NRF52 boards using mbed-RTOS such as Nano-33-BLE |
| 4 | + Written by Khoi Hoang |
| 5 | +
|
| 6 | + Built by Khoi Hoang https://github.com/khoih-prog/NRF52_MBED_TimerInterrupt |
| 7 | + Licensed under MIT license |
| 8 | +
|
| 9 | + Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by |
| 10 | + unsigned long miliseconds), you just consume only one NRF52 timer and avoid conflicting with other cores' tasks. |
| 11 | + The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers |
| 12 | + Therefore, their executions are not blocked by bad-behaving functions / tasks. |
| 13 | + This important feature is absolutely necessary for mission-critical tasks. |
| 14 | +
|
| 15 | + Based on SimpleTimer - A timer library for Arduino. |
| 16 | + |
| 17 | + Copyright (c) 2010 OTTOTECNICA Italy |
| 18 | +
|
| 19 | + Based on BlynkTimer.h |
| 20 | + Author: Volodymyr Shymanskyy |
| 21 | +
|
| 22 | + Version: 1.0.2 |
| 23 | +
|
| 24 | + Version Modified By Date Comments |
| 25 | + ------- ----------- ---------- ----------- |
| 26 | + 1.0.1 K Hoang 22/11/2020 Initial coding and sync with NRF52_TimerInterrupt |
| 27 | + 1.0.2 K Hoang 23/11/2020 Add and optimize examples |
| 28 | +*****************************************************************************************************************************/ |
| 29 | + |
| 30 | +/* |
| 31 | + Notes: |
| 32 | + Special design is necessary to share data between interrupt code and the rest of your program. |
| 33 | + Variables usually need to be "volatile" types. Volatile tells the compiler to avoid optimizations that assume |
| 34 | + variable can not spontaneously change. Because your function may change variables while your program is using them, |
| 35 | + the compiler needs this hint. But volatile alone is often not enough. |
| 36 | + When accessing shared variables, usually interrupts must be disabled. Even with volatile, |
| 37 | + if the interrupt changes a multi-byte variable between a sequence of instructions, it can be read incorrectly. |
| 38 | + If your data is multiple variables, such as an array and a count, usually interrupts need to be disabled |
| 39 | + or the entire sequence of your code which accesses the data. |
| 40 | +*/ |
| 41 | + |
| 42 | +#if !( ARDUINO_ARCH_NRF52840 && TARGET_NAME == ARDUINO_NANO33BLE ) |
| 43 | + #error This code is designed to run on nRF52-based Nano-33-BLE boards using mbed-RTOS platform! Please check your Tools->Board setting. |
| 44 | +#endif |
| 45 | + |
| 46 | +// These define's must be placed at the beginning before #include "NRF52TimerInterrupt.h" |
| 47 | +// For Nano33-BLE, don't use Serial.print() in ISR as system will definitely hang. |
| 48 | +#define NRF52_MBED_TIMER_INTERRUPT_DEBUG 0 |
| 49 | + |
| 50 | +#include "NRF52_MBED_TimerInterrupt.h" |
| 51 | + |
| 52 | +//#ifndef LED_BUILTIN |
| 53 | +// #define LED_BUILTIN D13 |
| 54 | +//#endif |
| 55 | + |
| 56 | +#ifndef LED_BLUE_PIN |
| 57 | + #define LED_BLUE_PIN D7 |
| 58 | +#endif |
| 59 | + |
| 60 | +#ifndef LED_RED_PIN |
| 61 | + #define LED_RED_PIN D8 |
| 62 | +#endif |
| 63 | + |
| 64 | +#define TIMER0_INTERVAL_MS 500 //1000 |
| 65 | +#define TIMER1_INTERVAL_MS 2000 |
| 66 | + |
| 67 | +volatile uint32_t Timer0Count = 0; |
| 68 | +volatile uint32_t Timer1Count = 0; |
| 69 | + |
| 70 | +// Depending on the board, you can select NRF52 Hardware Timer from NRF_TIMER_1,NRF_TIMER_3,NRF_TIMER_4 (1,3 and 4) |
| 71 | +// If you select the already-used NRF_TIMER_0 or NRF_TIMER_2, it'll be auto modified to use NRF_TIMER_1 |
| 72 | + |
| 73 | +// Init NRF52 timer NRF_TIMER1 |
| 74 | +NRF52_MBED_Timer ITimer0(NRF_TIMER_4); |
| 75 | + |
| 76 | +// Init NRF52 timer NRF_TIMER3 |
| 77 | +NRF52_MBED_Timer ITimer1(NRF_TIMER_3); |
| 78 | + |
| 79 | +void printResult(uint32_t currTime) |
| 80 | +{ |
| 81 | + Serial.printf("Time = %ld, Timer0Count = %lu, , Timer1Count = %lu\n", currTime, Timer0Count, Timer1Count); |
| 82 | +} |
| 83 | + |
| 84 | +void TimerHandler0(void) |
| 85 | +{ |
| 86 | + static bool toggle0 = false; |
| 87 | + |
| 88 | + // Flag for checking to be sure ISR is working as SErial.print is not OK here in ISR |
| 89 | + Timer0Count++; |
| 90 | + |
| 91 | + //timer interrupt toggles pin LED_BUILTIN |
| 92 | + digitalWrite(LED_BUILTIN, toggle0); |
| 93 | + toggle0 = !toggle0; |
| 94 | +} |
| 95 | + |
| 96 | +void TimerHandler1(void) |
| 97 | +{ |
| 98 | + static bool toggle1 = false; |
| 99 | + |
| 100 | + // Flag for checking to be sure ISR is working as Serial.print is not OK here in ISR |
| 101 | + Timer1Count++; |
| 102 | + |
| 103 | + //timer interrupt toggles outputPin |
| 104 | + digitalWrite(LED_BLUE_PIN, toggle1); |
| 105 | + toggle1 = !toggle1; |
| 106 | +} |
| 107 | + |
| 108 | +void setup() |
| 109 | +{ |
| 110 | + pinMode(LED_BUILTIN, OUTPUT); |
| 111 | + pinMode(LED_BLUE_PIN, OUTPUT); |
| 112 | + |
| 113 | + Serial.begin(115200); |
| 114 | + while (!Serial); |
| 115 | + |
| 116 | + delay(100); |
| 117 | + |
| 118 | + Serial.printf("\nStarting Change_Interval on %s\n", BOARD_NAME); |
| 119 | + Serial.printf("Version : v%s\n", NRF52_MBED_TIMER_INTERRUPT_VERSION); |
| 120 | + |
| 121 | + // Interval in microsecs |
| 122 | + if (ITimer0.attachInterruptInterval(TIMER0_INTERVAL_MS * 1000, TimerHandler0)) |
| 123 | + { |
| 124 | + Serial.printf("Starting ITimer0 OK, millis() = %ld\n", millis()); |
| 125 | + } |
| 126 | + else |
| 127 | + Serial.println("Can't set ITimer0. Select another freq. or timer"); |
| 128 | + |
| 129 | + // Interval in microsecs |
| 130 | + if (ITimer1.attachInterruptInterval(TIMER1_INTERVAL_MS * 1000, TimerHandler1)) |
| 131 | + { |
| 132 | + Serial.printf("Starting ITimer1 OK, millis() = %ld\n", millis()); |
| 133 | + } |
| 134 | + else |
| 135 | + Serial.println("Can't set ITimer1. Select another freq. or timer"); |
| 136 | +} |
| 137 | + |
| 138 | +#define CHECK_INTERVAL_MS 10000L |
| 139 | +#define CHANGE_INTERVAL_MS 20000L |
| 140 | + |
| 141 | +void loop() |
| 142 | +{ |
| 143 | + static uint32_t lastTime = 0; |
| 144 | + static uint32_t lastChangeTime = 0; |
| 145 | + static uint32_t currTime; |
| 146 | + static uint32_t multFactor = 0; |
| 147 | + |
| 148 | + currTime = millis(); |
| 149 | + |
| 150 | + if (currTime - lastTime > CHECK_INTERVAL_MS) |
| 151 | + { |
| 152 | + printResult(currTime); |
| 153 | + lastTime = currTime; |
| 154 | + |
| 155 | + if (currTime - lastChangeTime > CHANGE_INTERVAL_MS) |
| 156 | + { |
| 157 | + //setInterval(unsigned long interval, timerCallback callback) |
| 158 | + multFactor = (multFactor + 1) % 2; |
| 159 | + |
| 160 | + ITimer0.setInterval(TIMER0_INTERVAL_MS * 1000 * (multFactor + 1), TimerHandler0); |
| 161 | + ITimer1.setInterval(TIMER1_INTERVAL_MS * 1000 * (multFactor + 1), TimerHandler1); |
| 162 | + |
| 163 | + Serial.printf("Changing Interval, Timer0 = %lu, Timer1 = %lu\n", TIMER0_INTERVAL_MS * (multFactor + 1), TIMER1_INTERVAL_MS * (multFactor + 1)); |
| 164 | + |
| 165 | + lastChangeTime = currTime; |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments