From 2e01bf2610b1558fe24f6f7960b4e331109d078e Mon Sep 17 00:00:00 2001 From: Khoi Hoang <57012152+khoih-prog@users.noreply.github.com> Date: Fri, 4 Mar 2022 00:43:22 -0500 Subject: [PATCH] v1.2.1 to fix `DutyCycle` bug ### Releases v1.2.1 1. Fix `DutyCycle` bug. Check [float precisison of DutyCycle only sometimes working #3](https://github.com/khoih-prog/SAMD_Slow_PWM/issues/3) 2. Fix `New Period` display bug. Check [random dropouts #4](https://github.com/khoih-prog/SAMD_Slow_PWM/issues/4) 3. Update examples --- CONTRIBUTING.md | 8 +- README.md | 603 ++------------------------------- changelog.md | 7 + library.json | 2 +- library.properties | 2 +- src/PWM_Generic_Debug.h | 4 +- src/RP2040_Slow_PWM.h | 4 +- src/RP2040_Slow_PWM.hpp | 21 +- src/RP2040_Slow_PWM_ISR.h | 4 +- src/RP2040_Slow_PWM_ISR.hpp | 21 +- src/RP2040_Slow_PWM_ISR_Impl.h | 6 +- 11 files changed, 79 insertions(+), 603 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index baf1184..b12dd7c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -15,7 +15,7 @@ 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 -* `RP2040` Core Version (e.g. arduino-pico core v1.9.15) +* `RP2040` Core Version (e.g. arduino-pico core v1.12.0) * `RP2040` Board type (e.g. NANO_RP2040_CONNECT, RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040, GENERIC_RP2040, etc.) * Contextual information (e.g. what you were trying to achieve) * Simplest possible steps to reproduce @@ -28,13 +28,13 @@ Please ensure to specify the following: ``` Arduino IDE version: 1.8.19 -arduino-pico core v1.9.5 +arduino-pico core v1.12.0 RASPBERRY_PI_PICO 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.4.0-100-generic #113-Ubuntu SMP Thu Feb 3 18:43:29 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux Context: -I encountered a crash while using TimerInterrupt. +I encountered a crash while using Slow PWM. Steps to reproduce: 1. ... diff --git a/README.md b/README.md index b4031e5..8409dcc 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,8 @@ [![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](#Contributing) [![GitHub issues](https://img.shields.io/github/issues/khoih-prog/RP2040_Slow_PWM.svg)](http://github.com/khoih-prog/RP2040_Slow_PWM/issues) -Buy Me A Coffee +Donate to my libraries using BuyMeACoffee + --- @@ -140,7 +141,7 @@ The catch is **your function is now part of an ISR (Interrupt Service Routine), ## Prerequisites 1. [`Arduino IDE 1.8.19+` for Arduino](https://github.com/arduino/Arduino). [![GitHub release](https://img.shields.io/github/release/arduino/Arduino.svg)](https://github.com/arduino/Arduino/releases/latest) - 2. [**Earle Philhower's arduino-pico core v1.10.0+**](https://github.com/earlephilhower/arduino-pico) for RP2040-based boards such as **RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040**, etc. [![GitHub release](https://img.shields.io/github/release/earlephilhower/arduino-pico.svg)](https://github.com/earlephilhower/arduino-pico/releases/latest) + 2. [**Earle Philhower's arduino-pico core v1.12.0+**](https://github.com/earlephilhower/arduino-pico) for RP2040-based boards such as **RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040**, etc. [![GitHub release](https://img.shields.io/github/release/earlephilhower/arduino-pico.svg)](https://github.com/earlephilhower/arduino-pico/releases/latest) 3. To use with certain example - [`SimpleTimer library`](https://github.com/jfturcot/SimpleTimer) to use with some examples. @@ -169,7 +170,7 @@ Another way to install is to: 1. Install [VS Code](https://code.visualstudio.com/) 2. Install [PlatformIO](https://platformio.org/platformio-ide) -3. Install [**RP2040_Slow_PWM** library](https://platformio.org/lib/show/12868/RP2040_Slow_PWM) by using [Library Manager](https://platformio.org/lib/show/12868/RP2040_Slow_PWM/installation). Search for **RP2040_Slow_PWM** in [Platform.io Author's Libraries](https://platformio.org/lib/search?query=author:%22Khoi%20Hoang%22) +3. Install [**RP2040_Slow_PWM** library](https://registry.platformio.org/libraries/khoih-prog/RP2040_Slow_PWM) by using [Library Manager](https://registry.platformio.org/libraries/khoih-prog/RP2040_Slow_PWM/installation). Search for **RP2040_Slow_PWM** in [Platform.io Author's Libraries](https://platformio.org/lib/search?query=author:%22Khoi%20Hoang%22) 4. Use included [platformio.ini](platformio/platformio.ini) file from examples to ensure that all dependent libraries will installed automatically. Please visit documentation for the other options and examples at [Project Configuration File](https://docs.platformio.org/page/projectconf.html) @@ -281,574 +282,9 @@ void setup() ### Example [ISR_16_PWMs_Array_Complex](examples/ISR_16_PWMs_Array_Complex) -``` -#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 - -// These define's must be placed at the beginning before #include "ESP32_PWM.h" -// _PWM_LOGLEVEL_ from 0 to 4 -// Don't define _PWM_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system. -#define _PWM_LOGLEVEL_ 4 - -#define USING_MICROS_RESOLUTION true //false - -// Default is true, uncomment to false -//#define CHANGING_PWM_END_OF_CYCLE false - -// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error -#include "RP2040_Slow_PWM.h" - -#include // https://github.com/jfturcot/SimpleTimer - -#define LED_OFF HIGH -#define LED_ON LOW - -#ifndef LED_BUILTIN - #define LED_BUILTIN 25 -#endif - -#ifndef LED_BLUE - #define LED_BLUE 10 -#endif - -#ifndef LED_RED - #define LED_RED 11 -#endif - -#define HW_TIMER_INTERVAL_US 20L - -volatile uint64_t startMicros = 0; - -// Init RPI_PICO_Timer -RP2040_Timer ITimer(0); - -// Init MBED_RP2040_Slow_PWM, each can service 16 different ISR-based PWM channels -RP2040_Slow_PWM ISR_PWM; - -////////////////////////////////////////////////////// - -bool TimerHandler(struct repeating_timer *t) -{ - (void) t; - - ISR_PWM.run(); - - return true; -} - -///////////////////////////////////////////////// - -#define NUMBER_ISR_PWMS 16 +https://github.com/khoih-prog/RP2040_Slow_PWM/blob/1c170849098772e46c9dcd2b3e43af0816dc83e7/examples/ISR_16_PWMs_Array_Complex/ISR_16_PWMs_Array_Complex.ino#L16-L581 -#define PIN_D0 0 -#define PIN_D1 1 -#define PIN_D2 2 -#define PIN_D3 3 -#define PIN_D4 4 -#define PIN_D5 5 -#define PIN_D6 6 -#define PIN_D7 7 -#define PIN_D8 8 -#define PIN_D9 9 -#define PIN_D10 10 -#define PIN_D11 11 -#define PIN_D12 12 - -typedef void (*irqCallback) (); - -////////////////////////////////////////////////////// - -#define USE_COMPLEX_STRUCT true - -#define USING_PWM_FREQUENCY true - -////////////////////////////////////////////////////// - -#if USE_COMPLEX_STRUCT - -typedef struct -{ - uint32_t PWM_Pin; - irqCallback irqCallbackStartFunc; - irqCallback irqCallbackStopFunc; - -#if USING_PWM_FREQUENCY - float PWM_Freq; -#else - uint32_t PWM_Period; -#endif - - float PWM_DutyCycle; - - uint64_t deltaMicrosStart; - uint64_t previousMicrosStart; - - uint64_t deltaMicrosStop; - uint64_t previousMicrosStop; - -} ISR_PWM_Data; - -// In RP2040, avoid doing something fancy in ISR, for example Serial.print() -// The pure simple Serial.prints here are just for demonstration and testing. Must be eliminate in working environment -// Or you can get this run-time error / crash - -void doingSomethingStart(int index); - -void doingSomethingStop(int index); - -#else // #if USE_COMPLEX_STRUCT - -volatile unsigned long deltaMicrosStart [] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; -volatile unsigned long previousMicrosStart [] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - -volatile unsigned long deltaMicrosStop [] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; -volatile unsigned long previousMicrosStop [] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - -// You can assign pins here. Be carefull to select good pin to use or crash, e.g pin 6-11 -uint32_t PWM_Pin[] = -{ - LED_BUILTIN, LED_BLUE, LED_RED, PIN_D0, PIN_D1, PIN_D2, PIN_D3, PIN_D4, - PIN_D5, PIN_D6, PIN_D7, PIN_D8, PIN_D9, PIN_D10, PIN_D11, PIN_D12 -}; - -// You can assign any interval for any timer here, in microseconds -uint32_t PWM_Period[] = -{ - 1000000L, 500000L, 333333L, 250000L, 200000L, 166667L, 142857L, 125000L, - 111111L, 100000L, 66667L, 50000L, 40000L, 33333L, 25000L, 20000L -}; - -// You can assign any interval for any timer here, in Hz -float PWM_Freq[] = -{ - 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, - 9.0f, 10.0f, 15.0f, 20.0f, 25.0f, 30.0f, 40.0f, 50.0f -}; - -// You can assign any interval for any timer here, in milliseconds -float PWM_DutyCycle[] = -{ - 5.0, 10.0, 20.0, 30.0, 40.0, 45.0, 50.0, 55.0, - 60.0, 65.0, 70.0, 75.0, 80.0, 85.0, 90.0, 95.0 -}; - -void doingSomethingStart(int index) -{ - unsigned long currentMicros = micros(); - - deltaMicrosStart[index] = currentMicros - previousMicrosStart[index]; - previousMicrosStart[index] = currentMicros; -} - -void doingSomethingStop(int index) -{ - unsigned long currentMicros = micros(); - - // Count from start to stop PWM pulse - deltaMicrosStop[index] = currentMicros - previousMicrosStart[index]; - previousMicrosStop[index] = currentMicros; -} - -#endif // #if USE_COMPLEX_STRUCT - -//////////////////////////////////// -// Shared -//////////////////////////////////// - -void doingSomethingStart0() -{ - doingSomethingStart(0); -} - -void doingSomethingStart1() -{ - doingSomethingStart(1); -} - -void doingSomethingStart2() -{ - doingSomethingStart(2); -} - -void doingSomethingStart3() -{ - doingSomethingStart(3); -} - -void doingSomethingStart4() -{ - doingSomethingStart(4); -} - -void doingSomethingStart5() -{ - doingSomethingStart(5); -} - -void doingSomethingStart6() -{ - doingSomethingStart(6); -} - -void doingSomethingStart7() -{ - doingSomethingStart(7); -} - -void doingSomethingStart8() -{ - doingSomethingStart(8); -} - -void doingSomethingStart9() -{ - doingSomethingStart(9); -} - -void doingSomethingStart10() -{ - doingSomethingStart(10); -} - -void doingSomethingStart11() -{ - doingSomethingStart(11); -} - -void doingSomethingStart12() -{ - doingSomethingStart(12); -} - -void doingSomethingStart13() -{ - doingSomethingStart(13); -} -void doingSomethingStart14() -{ - doingSomethingStart(14); -} - -void doingSomethingStart15() -{ - doingSomethingStart(15); -} - -////////////////////////////////////////////////////// - -void doingSomethingStop0() -{ - doingSomethingStop(0); -} - -void doingSomethingStop1() -{ - doingSomethingStop(1); -} - -void doingSomethingStop2() -{ - doingSomethingStop(2); -} - -void doingSomethingStop3() -{ - doingSomethingStop(3); -} - -void doingSomethingStop4() -{ - doingSomethingStop(4); -} - -void doingSomethingStop5() -{ - doingSomethingStop(5); -} - -void doingSomethingStop6() -{ - doingSomethingStop(6); -} - -void doingSomethingStop7() -{ - doingSomethingStop(7); -} - -void doingSomethingStop8() -{ - doingSomethingStop(8); -} - -void doingSomethingStop9() -{ - doingSomethingStop(9); -} - -void doingSomethingStop10() -{ - doingSomethingStop(10); -} - -void doingSomethingStop11() -{ - doingSomethingStop(11); -} - -void doingSomethingStop12() -{ - doingSomethingStop(12); -} - -void doingSomethingStop13() -{ - doingSomethingStop(13); -} - -void doingSomethingStop14() -{ - doingSomethingStop(14); -} - -void doingSomethingStop15() -{ - doingSomethingStop(15); -} - -////////////////////////////////////////////////////// - -#if USE_COMPLEX_STRUCT - - #if USING_PWM_FREQUENCY - - ISR_PWM_Data curISR_PWM_Data[] = - { - // pin, irqCallbackStartFunc, irqCallbackStopFunc, PWM_Freq, PWM_DutyCycle, deltaMicrosStart, previousMicrosStart, deltaMicrosStop, previousMicrosStop - { LED_BUILTIN, doingSomethingStart0, doingSomethingStop0, 1, 5, 0, 0, 0, 0 }, - { LED_BLUE, doingSomethingStart1, doingSomethingStop1, 2, 10, 0, 0, 0, 0 }, - { LED_RED, doingSomethingStart2, doingSomethingStop2, 3, 20, 0, 0, 0, 0 }, - { PIN_D0, doingSomethingStart3, doingSomethingStop3, 4, 30, 0, 0, 0, 0 }, - { PIN_D1, doingSomethingStart4, doingSomethingStop4, 5, 40, 0, 0, 0, 0 }, - { PIN_D2, doingSomethingStart5, doingSomethingStop5, 6, 45, 0, 0, 0, 0 }, - { PIN_D3, doingSomethingStart6, doingSomethingStop6, 7, 50, 0, 0, 0, 0 }, - { PIN_D4, doingSomethingStart7, doingSomethingStop7, 8, 55, 0, 0, 0, 0 }, - { PIN_D5, doingSomethingStart8, doingSomethingStop8, 9, 60, 0, 0, 0, 0 }, - { PIN_D6, doingSomethingStart9, doingSomethingStop9, 10, 65, 0, 0, 0, 0 }, - { PIN_D7, doingSomethingStart10, doingSomethingStop10, 15, 70, 0, 0, 0, 0 }, - { PIN_D8, doingSomethingStart11, doingSomethingStop11, 20, 75, 0, 0, 0, 0 }, - { PIN_D9, doingSomethingStart12, doingSomethingStop12, 25, 80, 0, 0, 0, 0 }, - { PIN_D10, doingSomethingStart13, doingSomethingStop13, 30, 85, 0, 0, 0, 0 }, - { PIN_D11, doingSomethingStart14, doingSomethingStop14, 40, 90, 0, 0, 0, 0 }, - { PIN_D12, doingSomethingStart15, doingSomethingStop15, 50, 95, 0, 0, 0, 0 } - }; - - #else // #if USING_PWM_FREQUENCY - - ISR_PWM_Data curISR_PWM_Data[] = - { - // pin, irqCallbackStartFunc, irqCallbackStopFunc, PWM_Period, PWM_DutyCycle, deltaMicrosStart, previousMicrosStart, deltaMicrosStop, previousMicrosStop - { LED_BUILTIN, doingSomethingStart0, doingSomethingStop0, 1000000L, 5, 0, 0, 0, 0 }, - { LED_BLUE, doingSomethingStart1, doingSomethingStop1, 500000L, 10, 0, 0, 0, 0 }, - { LED_RED, doingSomethingStart2, doingSomethingStop2, 333333L, 20, 0, 0, 0, 0 }, - { PIN_D0, doingSomethingStart3, doingSomethingStop3, 250000L, 30, 0, 0, 0, 0 }, - { PIN_D1, doingSomethingStart4, doingSomethingStop4, 200000L, 40, 0, 0, 0, 0 }, - { PIN_D2, doingSomethingStart5, doingSomethingStop5, 166667L, 45, 0, 0, 0, 0 }, - { PIN_D3, doingSomethingStart6, doingSomethingStop6, 142857L, 50, 0, 0, 0, 0 }, - { PIN_D4, doingSomethingStart7, doingSomethingStop7, 125000L, 55, 0, 0, 0, 0 }, - { PIN_D5, doingSomethingStart8, doingSomethingStop8, 111111L, 60, 0, 0, 0, 0 }, - { PIN_D6, doingSomethingStart9, doingSomethingStop9, 100000L, 65, 0, 0, 0, 0 }, - { PIN_D7, doingSomethingStart10, doingSomethingStop10, 66667L, 70, 0, 0, 0, 0 }, - { PIN_D8, doingSomethingStart11, doingSomethingStop11, 50000L, 75, 0, 0, 0, 0 }, - { PIN_D9, doingSomethingStart12, doingSomethingStop12, 40000L, 80, 0, 0, 0, 0 }, - { PIN_D10, doingSomethingStart13, doingSomethingStop13, 33333L, 85, 0, 0, 0, 0 }, - { PIN_D11, doingSomethingStart14, doingSomethingStop14, 25000L, 90, 0, 0, 0, 0 }, - { PIN_D12, doingSomethingStart15, doingSomethingStop15, 20000L, 95, 0, 0, 0, 0 } - }; - - #endif // #if USING_PWM_FREQUENCY - -void doingSomethingStart(int index) -{ - unsigned long currentMicros = micros(); - - curISR_PWM_Data[index].deltaMicrosStart = currentMicros - curISR_PWM_Data[index].previousMicrosStart; - curISR_PWM_Data[index].previousMicrosStart = currentMicros; -} - -void doingSomethingStop(int index) -{ - unsigned long currentMicros = micros(); - - //curISR_PWM_Data[index].deltaMicrosStop = currentMicros - curISR_PWM_Data[index].previousMicrosStop; - // Count from start to stop PWM pulse - curISR_PWM_Data[index].deltaMicrosStop = currentMicros - curISR_PWM_Data[index].previousMicrosStart; - curISR_PWM_Data[index].previousMicrosStop = currentMicros; -} - -#else // #if USE_COMPLEX_STRUCT - -irqCallback irqCallbackStartFunc[] = -{ - doingSomethingStart0, doingSomethingStart1, doingSomethingStart2, doingSomethingStart3, - doingSomethingStart4, doingSomethingStart5, doingSomethingStart6, doingSomethingStart7, - doingSomethingStart8, doingSomethingStart9, doingSomethingStart10, doingSomethingStart11, - doingSomethingStart12, doingSomethingStart13, doingSomethingStart14, doingSomethingStart15 -}; - -irqCallback irqCallbackStopFunc[] = -{ - doingSomethingStop0, doingSomethingStop1, doingSomethingStop2, doingSomethingStop3, - doingSomethingStop4, doingSomethingStop5, doingSomethingStop6, doingSomethingStop7, - doingSomethingStop8, doingSomethingStop9, doingSomethingStop10, doingSomethingStop11, - doingSomethingStop12, doingSomethingStop13, doingSomethingStop14, doingSomethingStop15 -}; - -#endif // #if USE_COMPLEX_STRUCT - -////////////////////////////////////////////////////// - -#define SIMPLE_TIMER_MS 2000L - -// Init SimpleTimer -SimpleTimer simpleTimer; - -// Here is software Timer, you can do somewhat fancy stuffs without many issues. -// But always avoid -// 1. Long delay() it just doing nothing and pain-without-gain wasting CPU power.Plan and design your code / strategy ahead -// 2. Very long "do", "while", "for" loops without predetermined exit time. -void simpleTimerDoingSomething2s() -{ - static unsigned long previousMicrosStart = startMicros; - - unsigned long currMicros = micros(); - - Serial.print(F("SimpleTimer (ms): ")); Serial.print(SIMPLE_TIMER_MS); - Serial.print(F(", us : ")); Serial.print(currMicros); - Serial.print(F(", Dus : ")); Serial.println(currMicros - previousMicrosStart); - - for (uint16_t i = 0; i < NUMBER_ISR_PWMS; i++) - { -#if USE_COMPLEX_STRUCT - Serial.print(F("PWM Channel : ")); Serial.print(i); - Serial.print(F(", programmed Period (us): ")); - - #if USING_PWM_FREQUENCY - Serial.print(1000000 / curISR_PWM_Data[i].PWM_Freq); - #else - Serial.print(curISR_PWM_Data[i].PWM_Period); - #endif - - Serial.print(F(", actual : ")); Serial.print(curISR_PWM_Data[i].deltaMicrosStart); - - Serial.print(F(", programmed DutyCycle : ")); - - Serial.print(curISR_PWM_Data[i].PWM_DutyCycle); - - Serial.print(F(", actual : ")); Serial.println((float) curISR_PWM_Data[i].deltaMicrosStop * 100.0f / curISR_PWM_Data[i].deltaMicrosStart); - -#else - - Serial.print(F("PWM Channel : ")); Serial.print(i); - - #if USING_PWM_FREQUENCY - Serial.print(1000000 / PWM_Freq[i]); - #else - Serial.print(PWM_Period[i]); - #endif - - Serial.print(F(", programmed Period (us): ")); Serial.print(PWM_Period[i]); - Serial.print(F(", actual : ")); Serial.print(deltaMicrosStart[i]); - - Serial.print(F(", programmed DutyCycle : ")); - - Serial.print(PWM_DutyCycle[i]); - - Serial.print(F(", actual : ")); Serial.println( (float) deltaMicrosStop[i] * 100.0f / deltaMicrosStart[i]); -#endif - } - - previousMicrosStart = currMicros; -} - -void setup() -{ - Serial.begin(115200); - while (!Serial); - - delay(2000); - - Serial.print(F("\nStarting ISR_16_PWMs_Array_Complex on ")); Serial.println(BOARD_NAME); - Serial.println(RP2040_SLOW_PWM_VERSION); - - // Interval in microsecs - if (ITimer.attachInterruptInterval(HW_TIMER_INTERVAL_US, TimerHandler)) - { - startMicros = micros(); - Serial.print(F("Starting ITimer OK, micros() = ")); Serial.println(startMicros); - } - else - Serial.println(F("Can't set ITimer. Select another freq. or timer")); - - startMicros = micros(); - - // Just to demonstrate, don't use too many ISR Timers if not absolutely necessary - // You can use up to 16 timer for each ISR_PWM - - for (uint16_t i = 0; i < NUMBER_ISR_PWMS; i++) - { -#if USE_COMPLEX_STRUCT - curISR_PWM_Data[i].previousMicrosStart = startMicros; - //ISR_PWM.setInterval(curISR_PWM_Data[i].PWM_Period, curISR_PWM_Data[i].irqCallbackStartFunc); - - //void setPWM(uint32_t pin, float frequency, float dutycycle - // , timer_callback_p StartCallback = nullptr, timer_callback_p StopCallback = nullptr) - - #if USING_PWM_FREQUENCY - // You can use this with PWM_Freq in Hz - ISR_PWM.setPWM(curISR_PWM_Data[i].PWM_Pin, curISR_PWM_Data[i].PWM_Freq, curISR_PWM_Data[i].PWM_DutyCycle, - curISR_PWM_Data[i].irqCallbackStartFunc, curISR_PWM_Data[i].irqCallbackStopFunc); - #else - // Or You can use this with PWM_Period in us - ISR_PWM.setPWM_Period(curISR_PWM_Data[i].PWM_Pin, curISR_PWM_Data[i].PWM_Period, curISR_PWM_Data[i].PWM_DutyCycle, - curISR_PWM_Data[i].irqCallbackStartFunc, curISR_PWM_Data[i].irqCallbackStopFunc); - #endif - -#else - previousMicrosStart[i] = micros(); - - #if USING_PWM_FREQUENCY - // You can use this with PWM_Freq in Hz - ISR_PWM.setPWM(PWM_Pin[i], PWM_Freq[i], PWM_DutyCycle[i], irqCallbackStartFunc[i], irqCallbackStopFunc[i]); - #else - // Or You can use this with PWM_Period in us - ISR_PWM.setPWM_Period(PWM_Pin[i], PWM_Period[i], PWM_DutyCycle[i], irqCallbackStartFunc[i], irqCallbackStopFunc[i]); - #endif - -#endif - } - - // You need this timer for non-critical tasks. Avoid abusing ISR if not absolutely necessary. - simpleTimer.setInterval(SIMPLE_TIMER_MS, simpleTimerDoingSomething2s); -} - -#define BLOCKING_TIME_MS 10000L - -void loop() -{ - // This unadvised blocking task is used to demonstrate the blocking effects onto the execution and accuracy to Software timer - // You see the time elapse of ISR_PWM still accurate, whereas very unaccurate for Software Timer - // The time elapse for 2000ms software timer now becomes 3000ms (BLOCKING_TIME_MS) - // While that of ISR_PWM is still prefect. - delay(BLOCKING_TIME_MS); - - // You need this Software timer for non-critical tasks. Avoid abusing ISR if not absolutely necessary - // You don't need to and never call ISR_PWM.run() here in the loop(). It's already handled by ISR timer. - simpleTimer.run(); -} -``` --- --- @@ -861,7 +297,7 @@ The following is the sample terminal output when running example [ISR_16_PWMs_Ar ``` Starting ISR_16_PWMs_Array_Complex on RASPBERRY_PI_PICO -RP2040_Slow_PWM v1.2.0 +RP2040_Slow_PWM v1.2.1 [PWM] _timerNo = 0 , Clock (Hz) = 1000000.00 , _fre (Hz) = 50000.00 [PWM] _count = 0 - 20 [PWM] add_repeating_timer_us = 20 @@ -926,7 +362,7 @@ The following is the sample terminal output when running example [**ISR_16_PWMs_ ``` Starting ISR_16_PWMs_Array on RASPBERRY_PI_PICO -RP2040_Slow_PWM v1.2.0 +RP2040_Slow_PWM v1.2.1 [PWM] _timerNo = 0 , Clock (Hz) = 1000000.00 , _fre (Hz) = 50000.00 [PWM] _count = 0 - 20 [PWM] add_repeating_timer_us = 20 @@ -957,7 +393,7 @@ The following is the sample terminal output when running example [**ISR_16_PWMs_ ``` Starting ISR_16_PWMs_Array_Simple on RASPBERRY_PI_PICO -RP2040_Slow_PWM v1.2.0 +RP2040_Slow_PWM v1.2.1 [PWM] _timerNo = 0 , Clock (Hz) = 1000000.00 , _fre (Hz) = 50000.00 [PWM] _count = 0 - 20 [PWM] add_repeating_timer_us = 20 @@ -988,16 +424,22 @@ The following is the sample terminal output when running example [ISR_Modify_PWM ``` Starting ISR_Modify_PWM on RASPBERRY_PI_PICO -RP2040_Slow_PWM v1.2.0 +RP2040_Slow_PWM v1.2.1 [PWM] _timerNo = 0 , Clock (Hz) = 1000000.00 , _fre (Hz) = 50000.00 [PWM] _count = 0 - 20 [PWM] add_repeating_timer_us = 20 -Starting ITimer OK, micros() = 3340028 -Using PWM Freq = 1.00, PWM DutyCycle = 50.00 -Channel : 0 Period : 1000000 OnTime : 500000 Start_Time : 3341337 -Channel : 0 New Period : 500000 OnTime : 450000 Start_Time : 13341453 -Channel : 0 New Period : 1000000 OnTime : 500000 Start_Time : 23341707 -Channel : 0 New Period : 500000 OnTime : 450000 Start_Time : 32841842 +Starting ITimer OK, micros() = 2791911 +Using PWM Freq = 200.00, PWM DutyCycle = 1.00 +Channel : 0 Period : 5000 OnTime : 50 Start_Time : 2793180 +Channel : 0 Period : 10000 OnTime : 555 Start_Time : 12794374 +Channel : 0 Period : 5000 OnTime : 50 Start_Time : 22794212 +Channel : 0 Period : 10000 OnTime : 555 Start_Time : 32795264 +Channel : 0 Period : 5000 OnTime : 50 Start_Time : 42794822 +Channel : 0 Period : 10000 OnTime : 555 Start_Time : 52796069 +Channel : 0 Period : 5000 OnTime : 50 Start_Time : 62795977 +Channel : 0 Period : 10000 OnTime : 555 Start_Time : 72796753 +Channel : 0 Period : 5000 OnTime : 50 Start_Time : 82796835 +Channel : 0 Period : 10000 OnTime : 555 Start_Time : 92802985 ``` --- @@ -1008,7 +450,7 @@ The following is the sample terminal output when running example [ISR_Changing_P ``` Starting ISR_Changing_PWM on RASPBERRY_PI_PICO -RP2040_Slow_PWM v1.2.0 +RP2040_Slow_PWM v1.2.1 [PWM] _timerNo = 0 , Clock (Hz) = 1000000.00 , _fre (Hz) = 50000.00 [PWM] _count = 0 - 20 [PWM] add_repeating_timer_us = 20 @@ -1072,6 +514,7 @@ Submit issues to: [RP2040_Slow_PWM issues](https://github.com/khoih-prog/RP2040_ 4. Improve accuracy by using `float`, instead of `uint32_t` for `dutycycle` 5. Optimize library code by using `reference-passing` instead of `value-passing` 6. DutyCycle to be optionally updated at the end current PWM period instead of immediately. +7. Display informational warning only when `_PWM_LOGLEVEL_` > 3 --- --- diff --git a/changelog.md b/changelog.md index c12dbf9..03add32 100644 --- a/changelog.md +++ b/changelog.md @@ -12,6 +12,7 @@ ## Table of Contents * [Changelog](#changelog) + * [Releases v1.2.1](#releases-v121) * [Releases v1.2.0](#releases-v120) * [Releases v1.1.0](#releases-v110) * [Releases v1.0.1](#Releases-v101) @@ -22,6 +23,12 @@ ## Changelog +### Releases v1.2.1 + +1. Fix `DutyCycle` bug. Check [float precisison of DutyCycle only sometimes working #3](https://github.com/khoih-prog/SAMD_Slow_PWM/issues/3) +2. Fix `New Period` display bug. Check [random dropouts #4](https://github.com/khoih-prog/SAMD_Slow_PWM/issues/4) +3. Update examples + ### Releases v1.2.0 1. Fix `multiple-definitions` linker error. Drop `src_cpp` and `src_h` directories diff --git a/library.json b/library.json index 8a0fd2a..781523c 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "RP2040_Slow_PWM", - "version": "1.2.0", + "version": "1.2.1", "keywords": "timing, device, control, hardware-timer, pwm, ISR-based-pwm, multi-channel-pwm, low-frequency-pwm, mission-critical, accuracy, non-blocking, arduino-pico, rpi-pico, rp2040, nano-rp2040-connect, raspberry-pi-pico, precise", "description": "This library enables you to use ISR-based PWM channels on RP2040-based boards, such as ADAFRUIT_FEATHER_RP2040, RASPBERRY_PI_PICO, with arduino-pico core to create and output PWM any GPIO pin. The most important feature is they're ISR-based PWM channels, supporting lower PWM frequencies with suitable accuracy. Their executions are not blocked by bad-behaving functions or tasks. This important feature is absolutely necessary for mission-critical tasks. These ISR-based PWMs, still work even if other software functions are blocking. Moreover, they are much more precise (certainly depending on clock frequency accuracy) than other software-based PWM using millis() or micros(). That's necessary if you need to control devices requiring high precision. Now you can change the PWM settings on-the-fly", "authors": diff --git a/library.properties b/library.properties index 301285f..c53b2d3 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=RP2040_Slow_PWM -version=1.2.0 +version=1.2.1 author=Khoi Hoang maintainer=Khoi Hoang sentence=This library enables you to use ISR-based PWM channels on RP2040-based boards, such as ADAFRUIT_FEATHER_RP2040, RASPBERRY_PI_PICO, etc., with arduino-pico core to create and output PWM any GPIO pin. diff --git a/src/PWM_Generic_Debug.h b/src/PWM_Generic_Debug.h index 71c04c7..e1de538 100644 --- a/src/PWM_Generic_Debug.h +++ b/src/PWM_Generic_Debug.h @@ -12,13 +12,15 @@ Therefore, their executions are not blocked by bad-behaving functions / tasks. This important feature is absolutely necessary for mission-critical tasks. - Version: 1.1.0 + Version: 1.2.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K.Hoang 22/09/2021 Initial coding for RP2040-based boards using RP2040 arduino-pico core 1.0.1 K Hoang 22/10/2021 Fix platform in library.json for PIO 1.1.0 K Hoang 10/11/2021 Add functions to modify PWM settings on-the-fly + 1.2.0 K Hoang 02/02/2022 Fix multiple-definitions linker error. Improve accuracy. Optimize code + 1.2.1 K Hoang 03/03/2022 Fix `DutyCycle` and `New Period` display bugs. Display warning only when debug level > 3 *****************************************************************************************************************************/ #pragma once diff --git a/src/RP2040_Slow_PWM.h b/src/RP2040_Slow_PWM.h index 4267e5b..35a6649 100644 --- a/src/RP2040_Slow_PWM.h +++ b/src/RP2040_Slow_PWM.h @@ -12,13 +12,15 @@ Therefore, their executions are not blocked by bad-behaving functions / tasks. This important feature is absolutely necessary for mission-critical tasks. - Version: 1.1.0 + Version: 1.2.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K.Hoang 22/09/2021 Initial coding for RP2040-based boards using RP2040 arduino-pico core 1.0.1 K Hoang 22/10/2021 Fix platform in library.json for PIO 1.1.0 K Hoang 10/11/2021 Add functions to modify PWM settings on-the-fly + 1.2.0 K Hoang 02/02/2022 Fix multiple-definitions linker error. Improve accuracy. Optimize code + 1.2.1 K Hoang 03/03/2022 Fix `DutyCycle` and `New Period` display bugs. Display warning only when debug level > 3 *****************************************************************************************************************************/ #pragma once diff --git a/src/RP2040_Slow_PWM.hpp b/src/RP2040_Slow_PWM.hpp index 66dd333..0376566 100644 --- a/src/RP2040_Slow_PWM.hpp +++ b/src/RP2040_Slow_PWM.hpp @@ -12,7 +12,7 @@ Therefore, their executions are not blocked by bad-behaving functions / tasks. This important feature is absolutely necessary for mission-critical tasks. - Version: 1.2.0 + Version: 1.2.1 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -20,6 +20,7 @@ 1.0.1 K Hoang 22/10/2021 Fix platform in library.json for PIO 1.1.0 K Hoang 10/11/2021 Add functions to modify PWM settings on-the-fly 1.2.0 K Hoang 02/02/2022 Fix multiple-definitions linker error. Improve accuracy. Optimize code + 1.2.1 K Hoang 03/03/2022 Fix `DutyCycle` and `New Period` display bugs. Display warning only when debug level > 3 *****************************************************************************************************************************/ #pragma once @@ -32,13 +33,13 @@ #endif #ifndef RP2040_SLOW_PWM_VERSION - #define RP2040_SLOW_PWM_VERSION "RP2040_Slow_PWM v1.2.0" + #define RP2040_SLOW_PWM_VERSION "RP2040_Slow_PWM v1.2.1" #define RP2040_SLOW_PWM_VERSION_MAJOR 1 #define RP2040_SLOW_PWM_VERSION_MINOR 2 - #define RP2040_SLOW_PWM_VERSION_PATCH 0 + #define RP2040_SLOW_PWM_VERSION_PATCH 1 - #define RP2040_SLOW_PWM_VERSION_INT 1002000 + #define RP2040_SLOW_PWM_VERSION_INT 1002001 #endif #ifndef _PWM_LOGLEVEL_ @@ -46,7 +47,11 @@ #endif #if defined(ARDUINO_ARCH_MBED) - #warning Using MBED RP2040 platform + + #if (_PWM_LOGLEVEL_ > 3) + #warning Using MBED RP2040 platform + #endif + #include "pico.h" #include "pico/time.h" #include "hardware/gpio.h" @@ -55,7 +60,11 @@ #include "hardware/timer.h" #include "hardware/irq.h" #else - #warning Using RP2040 platform + + #if (_PWM_LOGLEVEL_ > 3) + #warning Using RP2040 platform + #endif + #include #include "pico/stdlib.h" #include "hardware/timer.h" diff --git a/src/RP2040_Slow_PWM_ISR.h b/src/RP2040_Slow_PWM_ISR.h index 86979d7..a82edb5 100644 --- a/src/RP2040_Slow_PWM_ISR.h +++ b/src/RP2040_Slow_PWM_ISR.h @@ -12,13 +12,15 @@ Therefore, their executions are not blocked by bad-behaving functions / tasks. This important feature is absolutely necessary for mission-critical tasks. - Version: 1.1.0 + Version: 1.2.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K.Hoang 22/09/2021 Initial coding for RP2040-based boards using RP2040 arduino-pico core 1.0.1 K Hoang 22/10/2021 Fix platform in library.json for PIO 1.1.0 K Hoang 10/11/2021 Add functions to modify PWM settings on-the-fly + 1.2.0 K Hoang 02/02/2022 Fix multiple-definitions linker error. Improve accuracy. Optimize code + 1.2.1 K Hoang 03/03/2022 Fix `DutyCycle` and `New Period` display bugs. Display warning only when debug level > 3 *****************************************************************************************************************************/ #pragma once diff --git a/src/RP2040_Slow_PWM_ISR.hpp b/src/RP2040_Slow_PWM_ISR.hpp index 979f92d..2d13f05 100644 --- a/src/RP2040_Slow_PWM_ISR.hpp +++ b/src/RP2040_Slow_PWM_ISR.hpp @@ -12,13 +12,15 @@ Therefore, their executions are not blocked by bad-behaving functions / tasks. This important feature is absolutely necessary for mission-critical tasks. - Version: 1.1.0 + Version: 1.2.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K.Hoang 22/09/2021 Initial coding for RP2040-based boards using RP2040 arduino-pico core 1.0.1 K Hoang 22/10/2021 Fix platform in library.json for PIO 1.1.0 K Hoang 10/11/2021 Add functions to modify PWM settings on-the-fly + 1.2.0 K Hoang 02/02/2022 Fix multiple-definitions linker error. Improve accuracy. Optimize code + 1.2.1 K Hoang 03/03/2022 Fix `DutyCycle` and `New Period` display bugs. Display warning only when debug level > 3 *****************************************************************************************************************************/ #pragma once @@ -31,13 +33,13 @@ #endif #ifndef RP2040_SLOW_PWM_VERSION - #define RP2040_SLOW_PWM_VERSION "RP2040_Slow_PWM v1.2.0" + #define RP2040_SLOW_PWM_VERSION "RP2040_Slow_PWM v1.2.1" #define RP2040_SLOW_PWM_VERSION_MAJOR 1 #define RP2040_SLOW_PWM_VERSION_MINOR 2 - #define RP2040_SLOW_PWM_VERSION_PATCH 0 + #define RP2040_SLOW_PWM_VERSION_PATCH 1 - #define RP2040_SLOW_PWM_VERSION_INT 1002000 + #define RP2040_SLOW_PWM_VERSION_INT 1002001 #endif #ifndef _PWM_LOGLEVEL_ @@ -64,12 +66,19 @@ typedef void (*timer_callback)(); typedef void (*timer_callback_p)(void *); #if !defined(USING_MICROS_RESOLUTION) - #warning Not USING_MICROS_RESOLUTION, using millis resolution + + #if (_PWM_LOGLEVEL_ > 3) + #warning Not USING_MICROS_RESOLUTION, using millis resolution + #endif + #define USING_MICROS_RESOLUTION false #endif #if !defined(CHANGING_PWM_END_OF_CYCLE) - #warning Using default CHANGING_PWM_END_OF_CYCLE == true + #if (_PWM_LOGLEVEL_ > 3) + #warning Using default CHANGING_PWM_END_OF_CYCLE == true + #endif + #define CHANGING_PWM_END_OF_CYCLE true #endif diff --git a/src/RP2040_Slow_PWM_ISR_Impl.h b/src/RP2040_Slow_PWM_ISR_Impl.h index ac65d9a..60f82a7 100644 --- a/src/RP2040_Slow_PWM_ISR_Impl.h +++ b/src/RP2040_Slow_PWM_ISR_Impl.h @@ -12,13 +12,15 @@ Therefore, their executions are not blocked by bad-behaving functions / tasks. This important feature is absolutely necessary for mission-critical tasks. - Version: 1.1.0 + Version: 1.2.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K.Hoang 22/09/2021 Initial coding for RP2040-based boards using RP2040 arduino-pico core 1.0.1 K Hoang 22/10/2021 Fix platform in library.json for PIO 1.1.0 K Hoang 10/11/2021 Add functions to modify PWM settings on-the-fly + 1.2.0 K Hoang 02/02/2022 Fix multiple-definitions linker error. Improve accuracy. Optimize code + 1.2.1 K Hoang 03/03/2022 Fix `DutyCycle` and `New Period` display bugs. Display warning only when debug level > 3 *****************************************************************************************************************************/ #pragma once @@ -233,7 +235,7 @@ bool RP2040_Slow_PWM_ISR::modifyPWMChannel_Period(const uint8_t& channelNum, con PWM[channelNum].newOnTime = ( period * dutycycle ) / 100; PWM_LOGINFO0("Channel : "); PWM_LOGINFO0(channelNum); - PWM_LOGINFO0("\tNew Period : "); PWM_LOGINFO0(PWM[channelNum].newPeriod); + PWM_LOGINFO0("\t Period : "); PWM_LOGINFO0(period); PWM_LOGINFO0("\t\tOnTime : "); PWM_LOGINFO0(PWM[channelNum].newOnTime); PWM_LOGINFO0("\tStart_Time : "); PWM_LOGINFOLN0(PWM[channelNum].prevTime);