From 340ccc27abf3dba38b1be28f08791202ceb6b1fb Mon Sep 17 00:00:00 2001 From: Khoi Hoang <57012152+khoih-prog@users.noreply.github.com> Date: Wed, 26 Oct 2022 02:01:22 -0400 Subject: [PATCH] v1.4.1 to add support to Seeeduino nRF52 ### Releases v1.4.1 1. Add support to Seeeduino nRF52840-based boards such as **SEEED_XIAO_NRF52840 and SEEED_XIAO_NRF52840_SENSE**, etc. using Seeeduino `mbed` core 2. Add astyle using `allman` style. Restyle the library 3. Update all examples --- CONTRIBUTING.md | 30 ++++- changelog.md | 7 ++ examples/Argument_None/Argument_None.ino | 39 +++++-- examples/Change_Interval/Change_Interval.ino | 43 ++++--- examples/FakeAnalogWrite/FakeAnalogWrite.ino | 83 +++++++++----- .../ISR_16_Timers_Array.ino | 51 ++++++--- .../ISR_16_Timers_Array_Complex.ino | 54 ++++++--- examples/SwitchDebounce/SwitchDebounce.ino | 29 +++-- .../TimerInterruptLEDDemo.ino | 23 +++- .../TimerInterruptTest/TimerInterruptTest.ino | 53 ++++++--- .../multiFileProject/multiFileProject.ino | 4 +- library.json | 2 +- library.properties | 2 +- platformio/platformio.ini | 2 + src/NRF52_MBED_ISR_Timer-Impl.h | 108 +++++++++--------- src/NRF52_MBED_ISR_Timer.h | 3 +- src/NRF52_MBED_ISR_Timer.hpp | 11 +- src/NRF52_MBED_TimerInterrupt.h | 5 +- src/NRF52_MBED_TimerInterrupt.hpp | 11 +- src/NRF52_MBED_TimerInterrupt_Impl.h | 27 ++--- src/TimerInterrupt_Generic_Debug.h | 5 +- utils/astyle_library.conf | 70 ++++++++++++ utils/restyle.sh | 6 + 23 files changed, 460 insertions(+), 208 deletions(-) create mode 100644 utils/astyle_library.conf create mode 100644 utils/restyle.sh diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 3583e30..59545df 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -15,7 +15,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.19) or Platform.io version -* Arduino `mbed` Core Version (e.g. Arduino `mbed` core v1.3.2 or Arduino `mbed_nano` core v2.6.1 ) +* `nRF52840 mbed` Core Version (e.g. Arduino mbed_nano core v3.4.1, Seeeduino mbed core v2.7.2) +* `nRF52840-based Nano_33_BLE` Board type (e.g. Nano_33_BLE, Nano_33_BLE_Sense, SEEED_XIAO_NRF52840 and SEEED_XIAO_NRF52840_SENSE, etc.) * Contextual information (e.g. what you were trying to achieve) * Simplest possible steps to reproduce * Anything that might be relevant in your opinion, such as: @@ -27,12 +28,13 @@ Please ensure to specify the following: ``` Arduino IDE version: 1.8.19 -Arduino mbed_nano` core v2.6.1 +Arduino mbed_nano core v3.4.1 +Nano_33_BLE board OS: Ubuntu 20.04 LTS -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 +Linux xy-Inspiron-3593 5.15.0-52-generic #58~20.04.1-Ubuntu SMP Thu Oct 13 13:09:46 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux Context: -I encountered a crash while trying to use the Timer Interrupt. +I encountered a crash while using this library Steps to reproduce: 1. ... @@ -41,12 +43,32 @@ Steps to reproduce: 4. ... ``` +--- + ### Sending Feature Requests Feel free to post feature requests. It's helpful if you can explain exactly why the feature would be useful. There are usually some outstanding feature requests in the [existing issues list](https://github.com/khoih-prog/NRF52_MBED_TimerInterrupt/issues?q=is%3Aopen+is%3Aissue+label%3Aenhancement), feel free to add comments to them. +--- + ### Sending Pull Requests Pull Requests with changes and fixes are also welcome! + +Please use the `astyle` to reformat the updated library code as follows (demo for Ubuntu Linux) + +1. Change directory to the library GitHub + +``` +xy@xy-Inspiron-3593:~$ cd Arduino/xy/NRF52_MBED_TimerInterrupt_GitHub/ +xy@xy-Inspiron-3593:~/Arduino/xy/NRF52_MBED_TimerInterrupt_GitHub$ +``` + +2. Issue astyle command + +``` +xy@xy-Inspiron-3593:~/Arduino/xy/NRF52_MBED_TimerInterrupt_GitHub$ bash utils/restyle.sh +``` + diff --git a/changelog.md b/changelog.md index f66c5aa..4eb13df 100644 --- a/changelog.md +++ b/changelog.md @@ -12,6 +12,7 @@ ## Table of Contents * [Changelog](#changelog) + * [Releases v1.4.1](#releases-v141) * [Releases v1.4.0](#releases-v140) * [Releases v1.3.0](#releases-v130) * [Releases v1.2.1](#releases-v121) @@ -25,6 +26,12 @@ ## Changelog +### Releases v1.4.1 + +1. Add support to Seeeduino nRF52840-based boards such as **SEEED_XIAO_NRF52840 and SEEED_XIAO_NRF52840_SENSE**, etc. using Seeeduino `mbed` core +2. Add astyle using `allman` style. Restyle the library +3. Update all examples + ### Releases v1.4.0 1. Fix `multiple-definitions` linker error. Drop `src_cpp` and `src_h` directories diff --git a/examples/Argument_None/Argument_None.ino b/examples/Argument_None/Argument_None.ino index 3f5e7a0..7ebed75 100644 --- a/examples/Argument_None/Argument_None.ino +++ b/examples/Argument_None/Argument_None.ino @@ -44,11 +44,19 @@ //#endif #ifndef LED_BLUE_PIN - #define LED_BLUE_PIN D7 + #if defined(LEDB) + #define LED_BLUE_PIN LEDB + #else + #define LED_BLUE_PIN D7 + #endif #endif #ifndef LED_RED_PIN - #define LED_RED_PIN D8 + #if defined(LEDR) + #define LED_RED_PIN LEDR + #else + #define LED_RED_PIN D8 + #endif #endif #define TIMER0_INTERVAL_MS 500 //1000 @@ -73,9 +81,12 @@ NRF52_MBED_Timer ITimer1(NRF_TIMER_3); void printResult(uint32_t currTime) { - Serial.print(F("Time = ")); Serial.print(currTime); - Serial.print(F(", Timer0Count = ")); Serial.print(Timer0Count); - Serial.print(F(", Timer1Count = ")); Serial.println(Timer1Count); + Serial.print(F("Time = ")); + Serial.print(currTime); + Serial.print(F(", Timer0Count = ")); + Serial.print(Timer0Count); + Serial.print(F(", Timer1Count = ")); + Serial.println(Timer1Count); } void TimerHandler0() @@ -96,7 +107,7 @@ void TimerHandler1() // Flag for checking to be sure ISR is working as Serial.print is not OK here in ISR Timer1Count++; - + //timer interrupt toggles outputPin digitalWrite(LED_BLUE_PIN, toggle1); toggle1 = !toggle1; @@ -106,19 +117,22 @@ void setup() { pinMode(LED_BUILTIN, OUTPUT); pinMode(LED_BLUE_PIN, OUTPUT); - + Serial.begin(115200); - while (!Serial); + + while (!Serial && millis() < 5000); delay(100); - Serial.print(F("\nStarting Argument_None on ")); Serial.println(BOARD_NAME); + Serial.print(F("\nStarting Argument_None on ")); + Serial.println(BOARD_NAME); Serial.println(NRF52_MBED_TIMER_INTERRUPT_VERSION); - + // Interval in microsecs if (ITimer0.attachInterruptInterval(TIMER0_INTERVAL_MS * 1000, TimerHandler0)) { - Serial.print(F("Starting ITimer0 OK, millis() = ")); Serial.println(millis()); + Serial.print(F("Starting ITimer0 OK, millis() = ")); + Serial.println(millis()); } else Serial.println(F("Can't set ITimer0. Select another freq. or timer")); @@ -126,7 +140,8 @@ void setup() // Interval in microsecs if (ITimer1.attachInterruptInterval(TIMER1_INTERVAL_MS * 1000, TimerHandler1)) { - Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(millis()); + Serial.print(F("Starting ITimer1 OK, millis() = ")); + Serial.println(millis()); } else Serial.println(F("Can't set ITimer1. Select another freq. or timer")); diff --git a/examples/Change_Interval/Change_Interval.ino b/examples/Change_Interval/Change_Interval.ino index ebdd123..f632fd1 100644 --- a/examples/Change_Interval/Change_Interval.ino +++ b/examples/Change_Interval/Change_Interval.ino @@ -26,7 +26,7 @@ */ #if !( ARDUINO_ARCH_NRF52840 && TARGET_NAME == ARDUINO_NANO33BLE ) -#error This code is designed to run on nRF52-based Nano-33-BLE boards using mbed-RTOS platform! Please check your Tools->Board setting. + #error This code is designed to run on nRF52-based Nano-33-BLE boards using mbed-RTOS platform! Please check your Tools->Board setting. #endif // These define's must be placed at the beginning before #include "NRF52TimerInterrupt.h" @@ -44,11 +44,19 @@ //#endif #ifndef LED_BLUE_PIN -#define LED_BLUE_PIN D7 + #if defined(LEDB) + #define LED_BLUE_PIN LEDB + #else + #define LED_BLUE_PIN D7 + #endif #endif #ifndef LED_RED_PIN -#define LED_RED_PIN D8 + #if defined(LEDR) + #define LED_RED_PIN LEDR + #else + #define LED_RED_PIN D8 + #endif #endif #define TIMER0_INTERVAL_MS 500 //1000 @@ -73,9 +81,12 @@ NRF52_MBED_Timer ITimer1(NRF_TIMER_3); void printResult(uint32_t currTime) { - Serial.print(F("Time = ")); Serial.print(currTime); - Serial.print(F(", Timer0Count = ")); Serial.print(Timer0Count); - Serial.print(F(", Timer1Count = ")); Serial.println(Timer1Count); + Serial.print(F("Time = ")); + Serial.print(currTime); + Serial.print(F(", Timer0Count = ")); + Serial.print(Timer0Count); + Serial.print(F(", Timer1Count = ")); + Serial.println(Timer1Count); } void TimerHandler0() @@ -108,17 +119,20 @@ void setup() pinMode(LED_BLUE_PIN, OUTPUT); Serial.begin(115200); - while (!Serial); + + while (!Serial && millis() < 5000); delay(100); - Serial.print(F("\nStarting Change_Interval on ")); Serial.println(BOARD_NAME); + Serial.print(F("\nStarting Change_Interval on ")); + Serial.println(BOARD_NAME); Serial.println(NRF52_MBED_TIMER_INTERRUPT_VERSION); // Interval in microsecs if (ITimer0.attachInterruptInterval(TIMER0_INTERVAL_MS * 1000, TimerHandler0)) { - Serial.print(F("Starting ITimer0 OK, millis() = ")); Serial.println(millis()); + Serial.print(F("Starting ITimer0 OK, millis() = ")); + Serial.println(millis()); } else Serial.println(F("Can't set ITimer0. Select another freq. or timer")); @@ -126,7 +140,8 @@ void setup() // Interval in microsecs if (ITimer1.attachInterruptInterval(TIMER1_INTERVAL_MS * 1000, TimerHandler1)) { - Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(millis()); + Serial.print(F("Starting ITimer1 OK, millis() = ")); + Serial.println(millis()); } else Serial.println(F("Can't set ITimer1. Select another freq. or timer")); @@ -157,9 +172,11 @@ void loop() ITimer0.setInterval(TIMER0_INTERVAL_MS * 1000 * (multFactor + 1), TimerHandler0); ITimer1.setInterval(TIMER1_INTERVAL_MS * 1000 * (multFactor + 1), TimerHandler1); - Serial.print(F("Changing Interval, Timer0 = ")); Serial.print(TIMER0_INTERVAL_MS * (multFactor + 1)); - Serial.print(F(", Timer1 = ")); Serial.println(TIMER1_INTERVAL_MS * (multFactor + 1)); - + Serial.print(F("Changing Interval, Timer0 = ")); + Serial.print(TIMER0_INTERVAL_MS * (multFactor + 1)); + Serial.print(F(", Timer1 = ")); + Serial.println(TIMER1_INTERVAL_MS * (multFactor + 1)); + lastChangeTime = currTime; } } diff --git a/examples/FakeAnalogWrite/FakeAnalogWrite.ino b/examples/FakeAnalogWrite/FakeAnalogWrite.ino index 7952c41..7a226c1 100644 --- a/examples/FakeAnalogWrite/FakeAnalogWrite.ino +++ b/examples/FakeAnalogWrite/FakeAnalogWrite.ino @@ -50,12 +50,20 @@ #define LED_BUILTIN D13 #endif -#ifndef LED_BLUE - #define LED_BLUE D7 +#ifndef LED_BLUE_PIN + #if defined(LEDB) + #define LED_BLUE_PIN LEDB + #else + #define LED_BLUE_PIN D7 + #endif #endif -#ifndef LED_RED - #define LED_RED D8 +#ifndef LED_RED_PIN + #if defined(LEDR) + #define LED_RED_PIN LEDR + #else + #define LED_RED_PIN D8 + #endif #endif // For PWM_Value from 0-255.You can change to 1024 or 2048 @@ -65,7 +73,8 @@ // You have to calibrate and update this mapping table float mappingTable[MAPPING_TABLE_SIZE] = -{ 0.0, 3.281, 6.860, 10.886, 15.285, 20.355, 26.096, 32.732, 40.785, 50.180, +{ + 0.0, 3.281, 6.860, 10.886, 15.285, 20.355, 26.096, 32.732, 40.785, 50.180, 62.557, 79.557, 104.461, 136.075, 163.066, 181.930, 195.724, 207.132, 216.228, 223.684, 230.395, 236.136, 241.206, 245.680, 249.781, 253.509 }; @@ -187,18 +196,21 @@ void setup() pinMode(LED_BUILTIN, OUTPUT); Serial.begin(115200); - while (!Serial); + + while (!Serial && millis() < 5000); delay(100); - Serial.print(F("\nStarting FakeAnalogWrite on ")); Serial.println(BOARD_NAME); + Serial.print(F("\nStarting FakeAnalogWrite on ")); + Serial.println(BOARD_NAME); Serial.println(NRF52_MBED_TIMER_INTERRUPT_VERSION); // Interval in microsecs if (ITimer.attachInterruptInterval(HW_TIMER_INTERVAL_US, TimerHandler)) { startMillis = millis(); - Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(startMillis); + Serial.print(F("Starting ITimer OK, millis() = ")); + Serial.println(startMillis); } else Serial.println(F("Can't set ITimer correctly. Select another freq. or interval")); @@ -226,19 +238,20 @@ void fakeAnalogWrite(uint16_t pin, uint16_t value) if ( (curISRTimerData[i].beingUsed) && (curISRTimerData[i].pin == pin) ) { localValue = (value < MAX_PWM_VALUE) ? value : MAX_PWM_VALUE; - + if (curISRTimerData[i].PWM_PremapValue == localValue) { -#if (TIMER_INTERRUPT_DEBUG > 0) - Serial.print(F("Ignore : Same Value for index = ")); Serial.println(i); +#if (TIMER_INTERRUPT_DEBUG > 0) + Serial.print(F("Ignore : Same Value for index = ")); + Serial.println(i); #endif - + return; } else if (curISRTimerData[i].PWM_Value >= 0) - { + { curISRTimerData[i].PWM_PremapValue = localValue; - + // Mapping to corect value if ( ( localValue == 0) || ( localValue == MAX_PWM_VALUE - 1) ) { @@ -258,20 +271,25 @@ void fakeAnalogWrite(uint16_t pin, uint16_t value) } #if (TIMER_INTERRUPT_DEBUG > 1) - Serial.print(F("localIndex = ")); Serial.println(localIndex); + Serial.print(F("localIndex = ")); + Serial.println(localIndex); #endif // Can use map() function // Can use map() function curISRTimerData[i].PWM_Value = (uint16_t) ( (localIndex * 10 ) + - ( (value - mappingTable[localIndex]) * 10 ) / (mappingTable[localIndex + 1] - mappingTable[localIndex]) ); + ( (value - mappingTable[localIndex]) * 10 ) / (mappingTable[localIndex + 1] - mappingTable[localIndex]) ); #if (TIMER_INTERRUPT_DEBUG > 0) - Serial.print(F("Update index = ")); Serial.print(i); - Serial.print(F(", pin = ")); Serial.print(pin); - Serial.print(F(", input PWM_Value = ")); Serial.print(value); - Serial.print(F(", mapped PWM_Value = ")); Serial.println(curISRTimerData[i].PWM_Value); -#endif + Serial.print(F("Update index = ")); + Serial.print(i); + Serial.print(F(", pin = ")); + Serial.print(pin); + Serial.print(F(", input PWM_Value = ")); + Serial.print(value); + Serial.print(F(", mapped PWM_Value = ")); + Serial.println(curISRTimerData[i].PWM_Value); +#endif } } else @@ -316,13 +334,14 @@ void fakeAnalogWrite(uint16_t pin, uint16_t value) } #if (TIMER_INTERRUPT_DEBUG > 1) - Serial.print(F("localIndex = ")); Serial.println(localIndex); + Serial.print(F("localIndex = ")); + Serial.println(localIndex); #endif // Can use map() function // Can use map() function curISRTimerData[i].PWM_Value = (uint16_t) ( (localIndex * 10 ) + - ( (value - mappingTable[localIndex]) * 10 ) / (mappingTable[localIndex + 1] - mappingTable[localIndex]) ); + ( (value - mappingTable[localIndex]) * 10 ) / (mappingTable[localIndex + 1] - mappingTable[localIndex]) ); } curISRTimerData[i].countPWM = 0; @@ -330,10 +349,14 @@ void fakeAnalogWrite(uint16_t pin, uint16_t value) pinMode(pin, OUTPUT); #if (TIMER_INTERRUPT_DEBUG > 0) - Serial.print(F("Add index = ")); Serial.print(i); - Serial.print(F(", pin = ")); Serial.print(pin); - Serial.print(F(", input PWM_Value = ")); Serial.print(value); - Serial.print(F(", mapped PWM_Value = ")); Serial.println(curISRTimerData[i].PWM_Value); + Serial.print(F("Add index = ")); + Serial.print(i); + Serial.print(F(", pin = ")); + Serial.print(pin); + Serial.print(F(", input PWM_Value = ")); + Serial.print(value); + Serial.print(F(", mapped PWM_Value = ")); + Serial.println(curISRTimerData[i].PWM_Value); #endif return; @@ -360,8 +383,10 @@ void loop() fakeAnalogWrite(D9, i * DIVIDER); #if (TIMER_INTERRUPT_DEBUG > 0) - Serial.print(F("Test PWM_Value = ")); Serial.print(i * DIVIDER); - Serial.print(F(", max = ")); Serial.println(MAX_PWM_VALUE - 1); + Serial.print(F("Test PWM_Value = ")); + Serial.print(i * DIVIDER); + Serial.print(F(", max = ")); + Serial.println(MAX_PWM_VALUE - 1); #endif delay(DELAY_BETWEEN_CHANGE_MS); diff --git a/examples/ISR_16_Timers_Array/ISR_16_Timers_Array.ino b/examples/ISR_16_Timers_Array/ISR_16_Timers_Array.ino index 64b7998..b7d65e3 100644 --- a/examples/ISR_16_Timers_Array/ISR_16_Timers_Array.ino +++ b/examples/ISR_16_Timers_Array/ISR_16_Timers_Array.ino @@ -52,15 +52,23 @@ #include // https://github.com/jfturcot/SimpleTimer #ifndef LED_BUILTIN -#define LED_BUILTIN D13 + #define LED_BUILTIN D13 #endif -#ifndef LED_BLUE -#define LED_BLUE D7 +#ifndef LED_BLUE_PIN + #if defined(LEDB) + #define LED_BLUE_PIN LEDB + #else + #define LED_BLUE_PIN D7 + #endif #endif -#ifndef LED_RED -#define LED_RED D8 +#ifndef LED_RED_PIN + #if defined(LEDR) + #define LED_RED_PIN LEDR + #else + #define LED_RED_PIN D8 + #endif #endif #define HW_TIMER_INTERVAL_US 10000L @@ -118,10 +126,10 @@ uint32_t TimerInterval[NUMBER_ISR_TIMERS] = void doingSomething(int index) { -unsigned long currentMillis = millis(); + unsigned long currentMillis = millis(); -deltaMillis[index] = currentMillis - previousMillis[index]; -previousMillis[index] = currentMillis; + deltaMillis[index] = currentMillis - previousMillis[index]; + previousMillis[index] = currentMillis; } // In NRF52, avoid doing something fancy in ISR, for example Serial.print() @@ -233,15 +241,21 @@ void simpleTimerDoingSomething2s() unsigned long currMillis = millis(); - Serial.print(F("SimpleTimer : "));Serial.print(SIMPLE_TIMER_MS / 1000); - Serial.print(F(", ms : ")); Serial.print(currMillis); - Serial.print(F(", Dms : ")); Serial.println(currMillis - previousMillis); + Serial.print(F("SimpleTimer : ")); + Serial.print(SIMPLE_TIMER_MS / 1000); + Serial.print(F(", ms : ")); + Serial.print(currMillis); + Serial.print(F(", Dms : ")); + Serial.println(currMillis - previousMillis); for (uint16_t i = 0; i < NUMBER_ISR_TIMERS; i++) { - Serial.print(F("Timer : ")); Serial.print(i); - Serial.print(F(", programmed : ")); Serial.print(TimerInterval[i]); - Serial.print(F(", actual : ")); Serial.println(deltaMillis[i]); + Serial.print(F("Timer : ")); + Serial.print(i); + Serial.print(F(", programmed : ")); + Serial.print(TimerInterval[i]); + Serial.print(F(", actual : ")); + Serial.println(deltaMillis[i]); } previousMillis = currMillis; @@ -252,18 +266,21 @@ void setup() pinMode(LED_BUILTIN, OUTPUT); Serial.begin(115200); - while (!Serial); + + while (!Serial && millis() < 5000); delay(100); - Serial.print(F("\nStarting ISR_16_Timers_Array on ")); Serial.println(BOARD_NAME); + Serial.print(F("\nStarting ISR_16_Timers_Array on ")); + Serial.println(BOARD_NAME); Serial.println(NRF52_MBED_TIMER_INTERRUPT_VERSION); // Interval in microsecs if (ITimer.attachInterruptInterval(HW_TIMER_INTERVAL_US, TimerHandler)) { startMillis = millis(); - Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(startMillis); + Serial.print(F("Starting ITimer OK, millis() = ")); + Serial.println(startMillis); } else Serial.println(F("Can't set ITimer. Select another freq. or interval")); diff --git a/examples/ISR_16_Timers_Array_Complex/ISR_16_Timers_Array_Complex.ino b/examples/ISR_16_Timers_Array_Complex/ISR_16_Timers_Array_Complex.ino index 601090b..4c39e6c 100644 --- a/examples/ISR_16_Timers_Array_Complex/ISR_16_Timers_Array_Complex.ino +++ b/examples/ISR_16_Timers_Array_Complex/ISR_16_Timers_Array_Complex.ino @@ -52,15 +52,23 @@ #include // https://github.com/jfturcot/SimpleTimer #ifndef LED_BUILTIN -#define LED_BUILTIN D13 + #define LED_BUILTIN D13 #endif -#ifndef LED_BLUE -#define LED_BLUE D7 +#ifndef LED_BLUE_PIN + #if defined(LEDB) + #define LED_BLUE_PIN LEDB + #else + #define LED_BLUE_PIN D7 + #endif #endif -#ifndef LED_RED -#define LED_RED D8 +#ifndef LED_RED_PIN + #if defined(LEDR) + #define LED_RED_PIN LEDR + #else + #define LED_RED_PIN D8 + #endif #endif #define HW_TIMER_INTERVAL_US 10000L @@ -293,20 +301,29 @@ void simpleTimerDoingSomething2s() unsigned long currMillis = millis(); - Serial.print(F("SimpleTimer : ")); Serial.print(SIMPLE_TIMER_MS / 1000); - Serial.print(F(", ms : ")); Serial.print(currMillis); - Serial.print(F(", Dms : ")); Serial.println(currMillis - previousMillis); + Serial.print(F("SimpleTimer : ")); + Serial.print(SIMPLE_TIMER_MS / 1000); + Serial.print(F(", ms : ")); + Serial.print(currMillis); + Serial.print(F(", Dms : ")); + Serial.println(currMillis - previousMillis); for (uint16_t i = 0; i < NUMBER_ISR_TIMERS; i++) { #if USE_COMPLEX_STRUCT - Serial.print(F("Timer : ")); Serial.print(i); - Serial.print(F(", programmed : ")); Serial.print(curISRTimerData[i].TimerInterval); - Serial.print(F(", actual : ")); Serial.println(curISRTimerData[i].deltaMillis); + Serial.print(F("Timer : ")); + Serial.print(i); + Serial.print(F(", programmed : ")); + Serial.print(curISRTimerData[i].TimerInterval); + Serial.print(F(", actual : ")); + Serial.println(curISRTimerData[i].deltaMillis); #else - Serial.print(F("Timer : ")); Serial.print(i); - Serial.print(F(", programmed : ")); Serial.print(TimerInterval[i]); - Serial.print(F(", actual : ")); Serial.println(deltaMillis[i]); + Serial.print(F("Timer : ")); + Serial.print(i); + Serial.print(F(", programmed : ")); + Serial.print(TimerInterval[i]); + Serial.print(F(", actual : ")); + Serial.println(deltaMillis[i]); #endif } @@ -318,18 +335,21 @@ void setup() pinMode(LED_BUILTIN, OUTPUT); Serial.begin(115200); - while (!Serial); + + while (!Serial && millis() < 5000); delay(100); - Serial.print(F("\nStarting ISR_16_Timers_Array_Complex on ")); Serial.println(BOARD_NAME); + Serial.print(F("\nStarting ISR_16_Timers_Array_Complex on ")); + Serial.println(BOARD_NAME); Serial.println(NRF52_MBED_TIMER_INTERRUPT_VERSION); // Interval in microsecs if (ITimer.attachInterruptInterval(HW_TIMER_INTERVAL_US, TimerHandler)) { startMillis = millis(); - Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(startMillis); + Serial.print(F("Starting ITimer OK, millis() = ")); + Serial.println(startMillis); } else Serial.println(F("Can't set ITimer. Select another freq. or interval")); diff --git a/examples/SwitchDebounce/SwitchDebounce.ino b/examples/SwitchDebounce/SwitchDebounce.ino index 49b3b85..dd1aaa7 100644 --- a/examples/SwitchDebounce/SwitchDebounce.ino +++ b/examples/SwitchDebounce/SwitchDebounce.ino @@ -49,7 +49,19 @@ //#endif #ifndef LED_BLUE_PIN - #define LED_BLUE_PIN D7 + #if defined(LEDB) + #define LED_BLUE_PIN LEDB + #else + #define LED_BLUE_PIN D7 + #endif +#endif + +#ifndef LED_RED_PIN + #if defined(LEDR) + #define LED_RED_PIN LEDR + #else + #define LED_RED_PIN D8 + #endif #endif unsigned int SWPin = D2; @@ -76,7 +88,7 @@ unsigned int debounceCountSWPressed = 0; unsigned int debounceCountSWReleased = 0; bool toggle0 = false; bool toggle1 = false; - + void TimerHandler() { if ( (!digitalRead(SWPin)) ) @@ -139,17 +151,20 @@ void setup() pinMode(LED_BLUE_PIN, OUTPUT); Serial.begin(115200); - while (!Serial); + + while (!Serial && millis() < 5000); delay(100); - Serial.print(F("\nStarting SwitchDebounce on ")); Serial.println(BOARD_NAME); + Serial.print(F("\nStarting SwitchDebounce on ")); + Serial.println(BOARD_NAME); Serial.println(NRF52_MBED_TIMER_INTERRUPT_VERSION); - + // Interval in microsecs if (ITimer.attachInterruptInterval(TIMER_INTERVAL_MS * 1000, TimerHandler)) { - Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(millis()); + Serial.print(F("Starting ITimer OK, millis() = ")); + Serial.println(millis()); } else Serial.println(F("Can't set ITimer. Select another freq. or timer")); @@ -160,7 +175,7 @@ void printResult(uint32_t currTime) Serial.print(F("Time = ")); Serial.print(currTime); Serial.print(F(", Switch = ")); - Serial.println(SWLongPressed? F("LongPressed") : ( SWPressed? F("Pressed") : F("Released") ) ); + Serial.println(SWLongPressed ? F("LongPressed") : ( SWPressed ? F("Pressed") : F("Released") ) ); } #define CHECK_INTERVAL_MS 1000L diff --git a/examples/TimerInterruptLEDDemo/TimerInterruptLEDDemo.ino b/examples/TimerInterruptLEDDemo/TimerInterruptLEDDemo.ino index b7dad60..4ea76f9 100644 --- a/examples/TimerInterruptLEDDemo/TimerInterruptLEDDemo.ino +++ b/examples/TimerInterruptLEDDemo/TimerInterruptLEDDemo.ino @@ -47,13 +47,21 @@ //#endif #ifndef LED_BLUE_PIN - #define LED_BLUE_PIN D7 + #if defined(LEDB) + #define LED_BLUE_PIN LEDB + #else + #define LED_BLUE_PIN D7 + #endif #endif #ifndef LED_RED_PIN - #define LED_RED_PIN D8 + #if defined(LEDR) + #define LED_RED_PIN LEDR + #else + #define LED_RED_PIN D8 + #endif #endif - + #define HW_TIMER_INTERVAL_MS 1 // For core mbed core 1.3.2- @@ -100,11 +108,13 @@ void doingSomething3() void setup() { Serial.begin(115200); - while (!Serial); + + while (!Serial && millis() < 5000); delay(100); - Serial.print(F("\nStarting TimerInterruptLEDDemo on ")); Serial.println(BOARD_NAME); + Serial.print(F("\nStarting TimerInterruptLEDDemo on ")); + Serial.println(BOARD_NAME); Serial.println(NRF52_MBED_TIMER_INTERRUPT_VERSION); // configure pin in output mode @@ -115,7 +125,8 @@ void setup() // Interval in microsecs if (ITimer.attachInterruptInterval(HW_TIMER_INTERVAL_MS * 1000, TimerHandler)) { - Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(millis()); + Serial.print(F("Starting ITimer OK, millis() = ")); + Serial.println(millis()); } else Serial.println(F("Can't set ITimer. Select another freq. or timer")); diff --git a/examples/TimerInterruptTest/TimerInterruptTest.ino b/examples/TimerInterruptTest/TimerInterruptTest.ino index 92cfbd6..f343284 100644 --- a/examples/TimerInterruptTest/TimerInterruptTest.ino +++ b/examples/TimerInterruptTest/TimerInterruptTest.ino @@ -43,11 +43,19 @@ //#endif #ifndef LED_BLUE_PIN - #define LED_BLUE_PIN D7 + #if defined(LEDB) + #define LED_BLUE_PIN LEDB + #else + #define LED_BLUE_PIN D7 + #endif #endif #ifndef LED_RED_PIN - #define LED_RED_PIN D8 + #if defined(LEDR) + #define LED_RED_PIN LEDR + #else + #define LED_RED_PIN D8 + #endif #endif #define TIMER0_INTERVAL_MS 1000 @@ -74,7 +82,7 @@ static bool toggle1 = false; NRF52_MBED_Timer ITimer0(NRF_TIMER_3); void TimerHandler0() -{ +{ preMillisTimer0 = millis(); //timer interrupt toggles pin LED_BUILTIN @@ -98,20 +106,23 @@ void setup() { pinMode(LED_BUILTIN, OUTPUT); pinMode(LED_BLUE_PIN, OUTPUT); - + Serial.begin(115200); - while (!Serial); - + + while (!Serial && millis() < 5000); + delay(100); - - Serial.print(F("\nStarting TimerInterruptTest on ")); Serial.println(BOARD_NAME); + + Serial.print(F("\nStarting TimerInterruptTest on ")); + Serial.println(BOARD_NAME); Serial.println(NRF52_MBED_TIMER_INTERRUPT_VERSION); // Interval in microsecs if (ITimer0.attachInterruptInterval(TIMER0_INTERVAL_MS * 1000, TimerHandler0)) { preMillisTimer0 = millis(); - Serial.print(F("Starting ITimer0 OK, millis() = ")); Serial.println(preMillisTimer0); + Serial.print(F("Starting ITimer0 OK, millis() = ")); + Serial.println(preMillisTimer0); } else Serial.println(F("Can't set ITimer0. Select another freq. or timer")); @@ -120,7 +131,8 @@ void setup() if (ITimer1.attachInterruptInterval(TIMER1_INTERVAL_MS * 1000, TimerHandler1)) { preMillisTimer1 = millis(); - Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(preMillisTimer1); + Serial.print(F("Starting ITimer1 OK, millis() = ")); + Serial.println(preMillisTimer1); } else Serial.println(F("Can't set ITimer1. Select another freq. or timer")); @@ -128,12 +140,12 @@ void setup() void loop() { - static unsigned long lastTimer0 = 0; + static unsigned long lastTimer0 = 0; static bool timer0Stopped = false; - + static unsigned long lastTimer1 = 0; static bool timer1Stopped = false; - + if (millis() - lastTimer0 > TIMER0_DURATION_MS) { @@ -142,14 +154,17 @@ void loop() if (timer0Stopped) { preMillisTimer0 = millis(); - Serial.print(F("Start ITimer0, millis() = ")); Serial.println(preMillisTimer0); + Serial.print(F("Start ITimer0, millis() = ")); + Serial.println(preMillisTimer0); ITimer0.restartTimer(); } else { - Serial.print(F("Stop ITimer0, millis() = ")); Serial.println(millis()); + Serial.print(F("Stop ITimer0, millis() = ")); + Serial.println(millis()); ITimer0.stopTimer(); } + timer0Stopped = !timer0Stopped; } @@ -160,15 +175,17 @@ void loop() if (timer1Stopped) { preMillisTimer1 = millis(); - Serial.print(F("Start ITimer1, millis() = ")); Serial.println(preMillisTimer1); + Serial.print(F("Start ITimer1, millis() = ")); + Serial.println(preMillisTimer1); ITimer1.restartTimer(); } else { - Serial.print(F("Stop ITimer1, millis() = ")); Serial.println(millis()); + Serial.print(F("Stop ITimer1, millis() = ")); + Serial.println(millis()); ITimer1.stopTimer(); } - + timer1Stopped = !timer1Stopped; } } diff --git a/examples/multiFileProject/multiFileProject.ino b/examples/multiFileProject/multiFileProject.ino index e1786b4..ce17bd8 100644 --- a/examples/multiFileProject/multiFileProject.ino +++ b/examples/multiFileProject/multiFileProject.ino @@ -27,12 +27,12 @@ // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error #include "NRF52_MBED_ISR_Timer.h" -void setup() +void setup() { // put your setup code here, to run once: } -void loop() +void loop() { // put your main code here, to run repeatedly: } diff --git a/library.json b/library.json index 353c02d..9f150da 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "NRF52_MBED_TimerInterrupt", - "version": "1.4.0", + "version": "1.4.1", "keywords": "timing, device, control, timer, interrupt, hardware, isr, isr-based, hardware-timer, mission-critical, accuracy, precise, non-blocking, nrf52, mbed, mbed-nano, nano-33-ble", "description": "This library enables you to use Interrupt from Hardware Timers on an NRF52-based board using mbed-RTOS such as Nano-33-BLE. 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": diff --git a/library.properties b/library.properties index 494d2e0..b13354c 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=NRF52_MBED_TimerInterrupt -version=1.4.0 +version=1.4.1 author=Khoi Hoang maintainer=Khoi Hoang sentence=This library enables you to use Interrupt from Hardware Timers on an NRF52-based board using mbed-RTOS such as Nano-33-BLE. diff --git a/platformio/platformio.ini b/platformio/platformio.ini index 0d8a6b8..f6ac963 100644 --- a/platformio/platformio.ini +++ b/platformio/platformio.ini @@ -36,6 +36,8 @@ upload_speed = 921600 ; Checks for the compatibility with frameworks and dev/platforms lib_compat_mode = strict +lib_ldf_mode = chain+ +;lib_ldf_mode = deep+ lib_deps = diff --git a/src/NRF52_MBED_ISR_Timer-Impl.h b/src/NRF52_MBED_ISR_Timer-Impl.h index 09f6a37..ec1ea37 100644 --- a/src/NRF52_MBED_ISR_Timer-Impl.h +++ b/src/NRF52_MBED_ISR_Timer-Impl.h @@ -19,7 +19,7 @@ Based on BlynkTimer.h Author: Volodymyr Shymanskyy - Version: 1.4.0 + Version: 1.4.1 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -29,7 +29,8 @@ 1.2.0 K.Hoang 11/01/2021 Add better debug feature. Optimize code and examples to reduce RAM usage 1.2.1 K.Hoang 04/05/2021 Add mbed_nano to list of compatible architectures 1.3.0 K.Hoang 09/09/2021 Don't use NRF_TIMER_1 because of mbed_nano core v2.0.0+ - 1.4.0 K.Hoang 22/01/2022 Fix `multiple-definitions` linker error. Fix bug + 1.4.0 K.Hoang 22/01/2022 Fix `multiple-definitions` linker error + 1.4.0 K.Hoang 26/10/2022 Add support to SEEED_XIAO_NRF52840 and SEEED_XIAO_NRF52840_SENSE using mbed *****************************************************************************************************************************/ #pragma once @@ -45,11 +46,11 @@ NRF52_MBED_ISR_Timer::NRF52_MBED_ISR_Timer() { } -void NRF52_MBED_ISR_Timer::init() +void NRF52_MBED_ISR_Timer::init() { unsigned long current_millis = millis(); //elapsed(); - for (uint8_t i = 0; i < MAX_NUMBER_TIMERS; i++) + for (uint8_t i = 0; i < MAX_NUMBER_TIMERS; i++) { memset((void*) &timer[i], 0, sizeof (timer_t)); timer[i].prev_millis = current_millis; @@ -58,7 +59,7 @@ void NRF52_MBED_ISR_Timer::init() numTimers = 0; } -void NRF52_MBED_ISR_Timer::run() +void NRF52_MBED_ISR_Timer::run() { uint8_t i; unsigned long current_millis; @@ -66,42 +67,42 @@ void NRF52_MBED_ISR_Timer::run() // get current time current_millis = millis(); //elapsed(); - for (i = 0; i < MAX_NUMBER_TIMERS; i++) + for (i = 0; i < MAX_NUMBER_TIMERS; i++) { timer[i].toBeCalled = TIMER_DEFCALL_DONTRUN; // no callback == no timer, i.e. jump over empty slots - if (timer[i].callback != NULL) + 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) + 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) + if (timer[i].enabled) { // "run forever" timers must always be executed - if (timer[i].maxNumRuns == TIMER_RUN_FOREVER) + if (timer[i].maxNumRuns == TIMER_RUN_FOREVER) { timer[i].toBeCalled = TIMER_DEFCALL_RUNONLY; } // other timers get executed the specified number of times - else if (timer[i].numRuns < timer[i].maxNumRuns) + else if (timer[i].numRuns < timer[i].maxNumRuns) { timer[i].toBeCalled = TIMER_DEFCALL_RUNONLY; timer[i].numRuns++; // after the last run, delete the timer - if (timer[i].numRuns >= timer[i].maxNumRuns) + if (timer[i].numRuns >= timer[i].maxNumRuns) { timer[i].toBeCalled = TIMER_DEFCALL_RUNANDDEL; } @@ -111,7 +112,7 @@ void NRF52_MBED_ISR_Timer::run() } } - for (i = 0; i < MAX_NUMBER_TIMERS; i++) + for (i = 0; i < MAX_NUMBER_TIMERS; i++) { if (timer[i].toBeCalled == TIMER_DEFCALL_DONTRUN) continue; @@ -129,18 +130,18 @@ void NRF52_MBED_ISR_Timer::run() // find the first available slot // return -1 if none found -int NRF52_MBED_ISR_Timer::findFirstFreeSlot() +int NRF52_MBED_ISR_Timer::findFirstFreeSlot() { // all slots are used - if (numTimers >= MAX_NUMBER_TIMERS) + if (numTimers >= MAX_NUMBER_TIMERS) { return -1; } // return the first slot with no callback (i.e. free) - for (uint8_t i = 0; i < MAX_NUMBER_TIMERS; i++) + for (uint8_t i = 0; i < MAX_NUMBER_TIMERS; i++) { - if (timer[i].callback == NULL) + if (timer[i].callback == NULL) { return i; } @@ -151,22 +152,23 @@ int NRF52_MBED_ISR_Timer::findFirstFreeSlot() } -int NRF52_MBED_ISR_Timer::setupTimer(const unsigned long& d, void* f, void* p, bool h, const unsigned& n) +int NRF52_MBED_ISR_Timer::setupTimer(const unsigned long& d, void* f, void* p, bool h, const unsigned& n) { int freeTimer; - if (numTimers < 0) + if (numTimers < 0) { init(); } freeTimer = findFirstFreeSlot(); - if (freeTimer < 0) + + if (freeTimer < 0) { return -1; } - if (f == NULL) + if (f == NULL) { return -1; } @@ -185,71 +187,71 @@ int NRF52_MBED_ISR_Timer::setupTimer(const unsigned long& d, void* f, void* p, b } -int NRF52_MBED_ISR_Timer::setTimer(const unsigned long& d, timerCallback f, const unsigned& n) +int NRF52_MBED_ISR_Timer::setTimer(const unsigned long& d, timerCallback f, const unsigned& n) { return setupTimer(d, (void *)f, NULL, false, n); } -int NRF52_MBED_ISR_Timer::setTimer(const unsigned long& d, timerCallback_p f, void* p, const unsigned& n) +int NRF52_MBED_ISR_Timer::setTimer(const unsigned long& d, timerCallback_p f, void* p, const unsigned& n) { return setupTimer(d, (void *)f, p, true, n); } -int NRF52_MBED_ISR_Timer::setInterval(const unsigned long& d, timerCallback f) +int NRF52_MBED_ISR_Timer::setInterval(const unsigned long& d, timerCallback f) { return setupTimer(d, (void *)f, NULL, false, TIMER_RUN_FOREVER); } -int NRF52_MBED_ISR_Timer::setInterval(const unsigned long& d, timerCallback_p f, void* p) +int NRF52_MBED_ISR_Timer::setInterval(const unsigned long& d, timerCallback_p f, void* p) { return setupTimer(d, (void *)f, p, true, TIMER_RUN_FOREVER); } -int NRF52_MBED_ISR_Timer::setTimeout(const unsigned long& d, timerCallback f) +int NRF52_MBED_ISR_Timer::setTimeout(const unsigned long& d, timerCallback f) { return setupTimer(d, (void *)f, NULL, false, TIMER_RUN_ONCE); } -int NRF52_MBED_ISR_Timer::setTimeout(const unsigned long& d, timerCallback_p f, void* p) +int NRF52_MBED_ISR_Timer::setTimeout(const unsigned long& d, timerCallback_p f, void* p) { return setupTimer(d, (void *)f, p, true, TIMER_RUN_ONCE); } -bool NRF52_MBED_ISR_Timer::changeInterval(const unsigned& numTimer, const unsigned long& d) +bool NRF52_MBED_ISR_Timer::changeInterval(const unsigned& numTimer, const unsigned long& d) { - if (numTimer >= MAX_NUMBER_TIMERS) + if (numTimer >= MAX_NUMBER_TIMERS) { return false; } // Updates interval of existing specified timer - if (timer[numTimer].callback != NULL) + if (timer[numTimer].callback != NULL) { timer[numTimer].delay = d; timer[numTimer].prev_millis = millis(); return true; } - + // false return for non-used numTimer, no callback return false; } -void NRF52_MBED_ISR_Timer::deleteTimer(const unsigned& timerId) +void NRF52_MBED_ISR_Timer::deleteTimer(const unsigned& timerId) { - if (timerId >= MAX_NUMBER_TIMERS) + if (timerId >= MAX_NUMBER_TIMERS) { return; } // nothing to delete if no timers are in use - if (numTimers == 0) + if (numTimers == 0) { return; } // don't decrease the number of timers if the specified slot is already empty - if (timer[timerId].callback != NULL) + if (timer[timerId].callback != NULL) { memset((void*) &timer[timerId], 0, sizeof (timer_t)); timer[timerId].prev_millis = millis(); @@ -260,9 +262,9 @@ void NRF52_MBED_ISR_Timer::deleteTimer(const unsigned& timerId) } // function contributed by code@rowansimms.com -void NRF52_MBED_ISR_Timer::restartTimer(const unsigned& numTimer) +void NRF52_MBED_ISR_Timer::restartTimer(const unsigned& numTimer) { - if (numTimer >= MAX_NUMBER_TIMERS) + if (numTimer >= MAX_NUMBER_TIMERS) { return; } @@ -271,9 +273,9 @@ void NRF52_MBED_ISR_Timer::restartTimer(const unsigned& numTimer) } -bool NRF52_MBED_ISR_Timer::isEnabled(const unsigned& numTimer) +bool NRF52_MBED_ISR_Timer::isEnabled(const unsigned& numTimer) { - if (numTimer >= MAX_NUMBER_TIMERS) + if (numTimer >= MAX_NUMBER_TIMERS) { return false; } @@ -282,9 +284,9 @@ bool NRF52_MBED_ISR_Timer::isEnabled(const unsigned& numTimer) } -void NRF52_MBED_ISR_Timer::enable(const unsigned& numTimer) +void NRF52_MBED_ISR_Timer::enable(const unsigned& numTimer) { - if (numTimer >= MAX_NUMBER_TIMERS) + if (numTimer >= MAX_NUMBER_TIMERS) { return; } @@ -293,9 +295,9 @@ void NRF52_MBED_ISR_Timer::enable(const unsigned& numTimer) } -void NRF52_MBED_ISR_Timer::disable(const unsigned& numTimer) +void NRF52_MBED_ISR_Timer::disable(const unsigned& numTimer) { - if (numTimer >= MAX_NUMBER_TIMERS) + if (numTimer >= MAX_NUMBER_TIMERS) { return; } @@ -303,35 +305,35 @@ void NRF52_MBED_ISR_Timer::disable(const unsigned& numTimer) timer[numTimer].enabled = false; } -void NRF52_MBED_ISR_Timer::enableAll() +void NRF52_MBED_ISR_Timer::enableAll() { // Enable all timers with a callback assigned (used) - for (uint8_t i = 0; i < MAX_NUMBER_TIMERS; i++) + for (uint8_t i = 0; i < MAX_NUMBER_TIMERS; i++) { - if (timer[i].callback != NULL && timer[i].numRuns == TIMER_RUN_FOREVER) + if (timer[i].callback != NULL && timer[i].numRuns == TIMER_RUN_FOREVER) { timer[i].enabled = true; } } } -void NRF52_MBED_ISR_Timer::disableAll() +void NRF52_MBED_ISR_Timer::disableAll() { // Disable all timers with a callback assigned (used) - for (uint8_t i = 0; i < MAX_NUMBER_TIMERS; i++) + for (uint8_t i = 0; i < MAX_NUMBER_TIMERS; i++) { - if (timer[i].callback != NULL && timer[i].numRuns == TIMER_RUN_FOREVER) + if (timer[i].callback != NULL && timer[i].numRuns == TIMER_RUN_FOREVER) { timer[i].enabled = false; } } } -void NRF52_MBED_ISR_Timer::toggle(const unsigned& numTimer) +void NRF52_MBED_ISR_Timer::toggle(const unsigned& numTimer) { - if (numTimer >= MAX_NUMBER_TIMERS) + if (numTimer >= MAX_NUMBER_TIMERS) { return; } @@ -340,7 +342,7 @@ void NRF52_MBED_ISR_Timer::toggle(const unsigned& numTimer) } -unsigned NRF52_MBED_ISR_Timer::getNumTimers() +unsigned NRF52_MBED_ISR_Timer::getNumTimers() { return numTimers; } diff --git a/src/NRF52_MBED_ISR_Timer.h b/src/NRF52_MBED_ISR_Timer.h index 6917f94..970874d 100644 --- a/src/NRF52_MBED_ISR_Timer.h +++ b/src/NRF52_MBED_ISR_Timer.h @@ -19,7 +19,7 @@ Based on BlynkTimer.h Author: Volodymyr Shymanskyy - Version: 1.4.0 + Version: 1.4.1 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -30,6 +30,7 @@ 1.2.1 K.Hoang 04/05/2021 Add mbed_nano to list of compatible architectures 1.3.0 K.Hoang 09/09/2021 Don't use NRF_TIMER_1 because of mbed_nano core v2.0.0+ 1.4.0 K.Hoang 22/01/2022 Fix `multiple-definitions` linker error + 1.4.0 K.Hoang 26/10/2022 Add support to SEEED_XIAO_NRF52840 and SEEED_XIAO_NRF52840_SENSE using mbed *****************************************************************************************************************************/ #pragma once diff --git a/src/NRF52_MBED_ISR_Timer.hpp b/src/NRF52_MBED_ISR_Timer.hpp index 34018cd..9f16bcf 100644 --- a/src/NRF52_MBED_ISR_Timer.hpp +++ b/src/NRF52_MBED_ISR_Timer.hpp @@ -19,7 +19,7 @@ Based on BlynkTimer.h Author: Volodymyr Shymanskyy - Version: 1.4.0 + Version: 1.4.1 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -29,7 +29,8 @@ 1.2.0 K.Hoang 11/01/2021 Add better debug feature. Optimize code and examples to reduce RAM usage 1.2.1 K.Hoang 04/05/2021 Add mbed_nano to list of compatible architectures 1.3.0 K.Hoang 09/09/2021 Don't use NRF_TIMER_1 because of mbed_nano core v2.0.0+ - 1.4.0 K.Hoang 22/01/2022 Fix `multiple-definitions` linker error. Fix bug + 1.4.0 K.Hoang 22/01/2022 Fix `multiple-definitions` linker error + 1.4.0 K.Hoang 26/10/2022 Add support to SEEED_XIAO_NRF52840 and SEEED_XIAO_NRF52840_SENSE using mbed *****************************************************************************************************************************/ #pragma once @@ -42,13 +43,13 @@ #endif #ifndef NRF52_MBED_TIMER_INTERRUPT_VERSION - #define NRF52_MBED_TIMER_INTERRUPT_VERSION "NRF52_MBED_TimerInterrupt v1.4.0" + #define NRF52_MBED_TIMER_INTERRUPT_VERSION "NRF52_MBED_TimerInterrupt v1.4.1" #define NRF52_MBED_TIMER_INTERRUPT_VERSION_MAJOR 1 #define NRF52_MBED_TIMER_INTERRUPT_VERSION_MINOR 4 - #define NRF52_MBED_TIMER_INTERRUPT_VERSION_PATCH 0 + #define NRF52_MBED_TIMER_INTERRUPT_VERSION_PATCH 1 - #define NRF52_MBED_TIMER_INTERRUPT_VERSION_INT 1004000 + #define NRF52_MBED_TIMER_INTERRUPT_VERSION_INT 1004001 #endif #include "TimerInterrupt_Generic_Debug.h" diff --git a/src/NRF52_MBED_TimerInterrupt.h b/src/NRF52_MBED_TimerInterrupt.h index cdad344..4ff826a 100644 --- a/src/NRF52_MBED_TimerInterrupt.h +++ b/src/NRF52_MBED_TimerInterrupt.h @@ -19,7 +19,7 @@ Based on BlynkTimer.h Author: Volodymyr Shymanskyy - Version: 1.4.0 + Version: 1.4.1 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -29,7 +29,8 @@ 1.2.0 K.Hoang 11/01/2021 Add better debug feature. Optimize code and examples to reduce RAM usage 1.2.1 K.Hoang 04/05/2021 Add mbed_nano to list of compatible architectures 1.3.0 K.Hoang 09/09/2021 Don't use NRF_TIMER_1 because of mbed_nano core v2.0.0+ - 1.4.0 K.Hoang 22/01/2022 Fix `multiple-definitions` linker error. Fix bug + 1.4.0 K.Hoang 22/01/2022 Fix `multiple-definitions` linker error + 1.4.0 K.Hoang 26/10/2022 Add support to SEEED_XIAO_NRF52840 and SEEED_XIAO_NRF52840_SENSE using mbed *****************************************************************************************************************************/ #pragma once diff --git a/src/NRF52_MBED_TimerInterrupt.hpp b/src/NRF52_MBED_TimerInterrupt.hpp index 54d2130..8358a07 100644 --- a/src/NRF52_MBED_TimerInterrupt.hpp +++ b/src/NRF52_MBED_TimerInterrupt.hpp @@ -19,7 +19,7 @@ Based on BlynkTimer.h Author: Volodymyr Shymanskyy - Version: 1.4.0 + Version: 1.4.1 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -29,7 +29,8 @@ 1.2.0 K.Hoang 11/01/2021 Add better debug feature. Optimize code and examples to reduce RAM usage 1.2.1 K.Hoang 04/05/2021 Add mbed_nano to list of compatible architectures 1.3.0 K.Hoang 09/09/2021 Don't use NRF_TIMER_1 because of mbed_nano core v2.0.0+ - 1.4.0 K.Hoang 22/01/2022 Fix `multiple-definitions` linker error. Fix bug + 1.4.0 K.Hoang 22/01/2022 Fix `multiple-definitions` linker error + 1.4.0 K.Hoang 26/10/2022 Add support to SEEED_XIAO_NRF52840 and SEEED_XIAO_NRF52840_SENSE using mbed *****************************************************************************************************************************/ /* nRF52 has 5 Hardware TIMERs: NRF_TIMER0-NRF_TIMER4 @@ -86,13 +87,13 @@ #include "hal/nrf_timer.h" #ifndef NRF52_MBED_TIMER_INTERRUPT_VERSION - #define NRF52_MBED_TIMER_INTERRUPT_VERSION "NRF52_MBED_TimerInterrupt v1.4.0" + #define NRF52_MBED_TIMER_INTERRUPT_VERSION "NRF52_MBED_TimerInterrupt v1.4.1" #define NRF52_MBED_TIMER_INTERRUPT_VERSION_MAJOR 1 #define NRF52_MBED_TIMER_INTERRUPT_VERSION_MINOR 4 - #define NRF52_MBED_TIMER_INTERRUPT_VERSION_PATCH 0 + #define NRF52_MBED_TIMER_INTERRUPT_VERSION_PATCH 1 - #define NRF52_MBED_TIMER_INTERRUPT_VERSION_INT 1004000 + #define NRF52_MBED_TIMER_INTERRUPT_VERSION_INT 1004001 #endif #include "TimerInterrupt_Generic_Debug.h" diff --git a/src/NRF52_MBED_TimerInterrupt_Impl.h b/src/NRF52_MBED_TimerInterrupt_Impl.h index 70ec5f4..0a5aa3a 100644 --- a/src/NRF52_MBED_TimerInterrupt_Impl.h +++ b/src/NRF52_MBED_TimerInterrupt_Impl.h @@ -19,7 +19,7 @@ Based on BlynkTimer.h Author: Volodymyr Shymanskyy - Version: 1.4.0 + Version: 1.4.1 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -29,7 +29,8 @@ 1.2.0 K.Hoang 11/01/2021 Add better debug feature. Optimize code and examples to reduce RAM usage 1.2.1 K.Hoang 04/05/2021 Add mbed_nano to list of compatible architectures 1.3.0 K.Hoang 09/09/2021 Don't use NRF_TIMER_1 because of mbed_nano core v2.0.0+ - 1.4.0 K.Hoang 22/01/2022 Fix `multiple-definitions` linker error. Fix bug + 1.4.0 K.Hoang 22/01/2022 Fix `multiple-definitions` linker error + 1.4.0 K.Hoang 26/10/2022 Add support to SEEED_XIAO_NRF52840 and SEEED_XIAO_NRF52840_SENSE using mbed *****************************************************************************************************************************/ #pragma once @@ -42,36 +43,36 @@ // only Timer 1, 3 and 4 are available extern "C" void TIMER1_IRQHandler_v() { - if (nRF52Timers[1]) + if (nRF52Timers[1]) { nRF52Timers[1]->detachInterrupt(); - + (*(nRF52Timers[1]->getCallback()))(); - + nRF52Timers[1]->enableTimer(); } } -extern "C" void TIMER3_IRQHandler_v() +extern "C" void TIMER3_IRQHandler_v() { - if (nRF52Timers[3]) + if (nRF52Timers[3]) { nRF52Timers[3]->detachInterrupt(); - + (*(nRF52Timers[3]->getCallback()))(); - + nRF52Timers[3]->enableTimer(); } } -extern "C" void TIMER4_IRQHandler_v() +extern "C" void TIMER4_IRQHandler_v() { - if (nRF52Timers[4]) + if (nRF52Timers[4]) { nRF52Timers[4]->detachInterrupt(); - + (*(nRF52Timers[4]->getCallback()))(); - + nRF52Timers[4]->enableTimer(); } } diff --git a/src/TimerInterrupt_Generic_Debug.h b/src/TimerInterrupt_Generic_Debug.h index 86920d5..5f318c5 100644 --- a/src/TimerInterrupt_Generic_Debug.h +++ b/src/TimerInterrupt_Generic_Debug.h @@ -19,7 +19,7 @@ Based on BlynkTimer.h Author: Volodymyr Shymanskyy - Version: 1.4.0 + Version: 1.4.1 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -29,7 +29,8 @@ 1.2.0 K.Hoang 11/01/2021 Add better debug feature. Optimize code and examples to reduce RAM usage 1.2.1 K.Hoang 04/05/2021 Add mbed_nano to list of compatible architectures 1.3.0 K.Hoang 09/09/2021 Don't use NRF_TIMER_1 because of mbed_nano core v2.0.0+ - 1.4.0 K.Hoang 22/01/2022 Fix `multiple-definitions` linker error. Fix bug + 1.4.0 K.Hoang 22/01/2022 Fix `multiple-definitions` linker error + 1.4.0 K.Hoang 26/10/2022 Add support to SEEED_XIAO_NRF52840 and SEEED_XIAO_NRF52840_SENSE using mbed *****************************************************************************************************************************/ #pragma once diff --git a/utils/astyle_library.conf b/utils/astyle_library.conf new file mode 100644 index 0000000..8a73bc2 --- /dev/null +++ b/utils/astyle_library.conf @@ -0,0 +1,70 @@ +# Code formatting rules for Arduino libraries, modified from for KH libraries: +# +# https://github.com/arduino/Arduino/blob/master/build/shared/examples_formatter.conf +# + +# astyle --style=allman -s2 -t2 -C -S -xW -Y -M120 -f -p -xg -H -xb -c --xC120 -xL *.h *.cpp *.ino + +--mode=c +--lineend=linux +--style=allman + +# -r or -R +#--recursive + +# -c => Converts tabs into spaces +convert-tabs + +# -s2 => 2 spaces indentation +--indent=spaces=2 + +# -t2 => tab =2 spaces +#--indent=tab=2 + +# -C +--indent-classes + +# -S +--indent-switches + +# -xW +--indent-preproc-block + +# -Y => indent classes, switches (and cases), comments starting at column 1 +--indent-col1-comments + +# -M120 => maximum of 120 spaces to indent a continuation line +--max-continuation-indent=120 + +# -xC120 => max‑code‑length will break a line if the code exceeds # characters +--max-code-length=120 + +# -f => +--break-blocks + +# -p => put a space around operators +--pad-oper + +# -xg => Insert space padding after commas +--pad-comma + +# -H => put a space after if/for/while +pad-header + +# -xb => Break one line headers (e.g. if/for/while) +--break-one-line-headers + +# -c => Converts tabs into spaces +#--convert-tabs + +# if you like one-liners, keep them +#keep-one-line-statements + +# -xV +--attach-closing-while + +#unpad-paren + +# -xp +remove-comment-prefix + diff --git a/utils/restyle.sh b/utils/restyle.sh new file mode 100644 index 0000000..bcd846f --- /dev/null +++ b/utils/restyle.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +for dir in . ; do + find $dir -type f \( -name "*.c" -o -name "*.h" -o -name "*.cpp" -o -name "*.ino" \) -exec astyle --suffix=none --options=./utils/astyle_library.conf \{\} \; +done +