Skip to content
This repository has been archived by the owner on Feb 4, 2023. It is now read-only.

Commit

Permalink
v1.0.2 fixes dynamic-frequency bug
Browse files Browse the repository at this point in the history
### Releases v1.0.2

1. Fix bug not changing frequency dynamically. See [Change the PWM frequency #2](#2)
  • Loading branch information
khoih-prog authored Oct 4, 2021
1 parent a8e5cff commit bad37e1
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 41 deletions.
55 changes: 49 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@
* [2. Initizlize PWM Instance](#1-Initizlize-PWM-Instance)
* [Examples](#examples)
* [ 1. PWM_Multi](examples/PWM_Multi)
* [ 2. PWM_Multi](examples/PWM_DynamicFreq)
* [Example PWM_Multi](#example-PWM_Multi)
* [Debug Terminal Output Samples](#debug-terminal-output-samples)
* [1. PWM_Multi on MBED RaspberryPi Pico](#1-PWM_Multi-on-MBED-RaspberryPi-Pico)
* [2. PWM_Multi on RASPBERRY_PI_PICO](#2-PWM_Multi-on-RASPBERRY_PI_PICO)
* [3. PWM_DynamicFreq on Nano RP2040 Connect](#3-PWM_DynamicFreq-on-Nano-RP2040-Connect)
* [Debug](#debug)
* [Troubleshooting](#troubleshooting)
* [Issues](#issues)
Expand Down Expand Up @@ -207,6 +209,7 @@ if (PWM_Instance)
### Examples:

1. [PWM_Multi](examples/PWM_Multi)
2. [PWM_DynamicFreq](examples/PWM_DynamicFreq). **New**

---
---
Expand Down Expand Up @@ -289,7 +292,7 @@ void setup()
Serial.print("\t\t"); Serial.println(PWM_Instance[index]->getActualFreq());
PWM_LOGDEBUG5("\nTOP = ", top, ", DIV = ", div, ", CPU_freq = ", PWM_Instance[index]->get_freq_CPU());
PWM_LOGDEBUG5("\nTOP =", top, ", DIV =", div, ", CPU_freq =", PWM_Instance[index]->get_freq_CPU());
}
else
{
Expand Down Expand Up @@ -318,7 +321,7 @@ The following is the sample terminal output when running example [PWM_Multi](exa

```
Starting PWM_Multi on RaspberryPi Pico
RP2040_PWM v1.0.1
RP2040_PWM v1.0.2
=============================================================
Index Pin PWM_freq DutyCycle Actual Freq
=============================================================
Expand All @@ -337,11 +340,11 @@ Index Pin PWM_freq DutyCycle Actual Freq

### 2. PWM_Multi on RASPBERRY_PI_PICO

The following is the sample terminal output when running example [**PWM_Multi**](examples/PWM_Multi) on **RASPBERRY_PI_PICO**, running [`Earle Philhower's arduino-pico core`](https://github.com/earlephilhower/arduino-pico), to demonstrate the ability to provide high PWM frequencies and the accuracy of Hardware-based PWM, **especially when system is very busy**.
The following is the sample terminal output when running example [**PWM_Multi**](examples/PWM_Multi) on **RASPBERRY_PI_PICO**, running [`Earle Philhower's arduino-pico core`](https://github.com/earlephilhower/arduino-pico), to demonstrate the ability to provide high PWM frequencies and the accuracy of Hardware-based PWM, **especially when system is very busy**.

```
Starting PWM_Multi on RASPBERRY_PI_PICO
RP2040_PWM v1.0.1
RP2040_PWM v1.0.2
=============================================================
Index Pin PWM_freq DutyCycle Actual Freq
=============================================================
Expand All @@ -356,6 +359,43 @@ Index Pin PWM_freq DutyCycle Actual Freq
=============================================================
```

---

### 3. PWM_DynamicFreq on Nano RP2040 Connect

The following is the sample terminal output when running example [**PWM_DynamicFreq**](examples/PWM_DynamicFreq) on **Nano RP2040 Connect**, running running [`ArduinoCore-mbed mbed_rp2040 core`](https://github.com/arduino/ArduinoCore-mbed), to demonstrate the ability to change dynamically PWM frequencies and the accuracy of Hardware-based PWM.

```
Starting PWM_DynamicFreq on Nano RP2040 Connect
RP2040_PWM v1.0.2
[PWM] _PWM_config.top = 12499 , _actualFrequency = 1000.00
[PWM] No change, same PWM frequency = 1000.00
=============================================================
Change PWM Freq to 2000.00
[PWM] _PWM_config.top = 62499 , _actualFrequency = 2000.00
[PWM] Changing PWM frequency to 2000.00
Actual PWM Frequency = 2000.00
[PWM] TOP = 62499 , DIV = 1 , CPU_freq = 125000000
Change PWM Freq to 1000.00
[PWM] _PWM_config.top = 12499 , _actualFrequency = 1000.00
[PWM] Changing PWM frequency to 1000.00
Actual PWM Frequency = 1000.00
[PWM] TOP = 12499 , DIV = 10 , CPU_freq = 125000000
Change PWM Freq to 2000.00
[PWM] _PWM_config.top = 62499 , _actualFrequency = 2000.00
[PWM] Changing PWM frequency to 2000.00
Actual PWM Frequency = 2000.00
[PWM] TOP = 62499 , DIV = 1 , CPU_freq = 125000000
Change PWM Freq to 1000.00
[PWM] _PWM_config.top = 12499 , _actualFrequency = 1000.00
[PWM] Changing PWM frequency to 1000.00
Actual PWM Frequency = 1000.00
[PWM] TOP = 12499 , DIV = 10 , CPU_freq = 125000000
Change PWM Freq to 2000.00
[PWM] _PWM_config.top = 62499 , _actualFrequency = 2000.00
[PWM] Changing PWM frequency to 2000.00
Actual PWM Frequency = 2000.00
```

---
---
Expand Down Expand Up @@ -409,11 +449,14 @@ Submit issues to: [RP2040_PWM issues](https://github.com/khoih-prog/RP2040_PWM/i

Many thanks for everyone for bug reporting, new feature suggesting, testing and contributing to the development of this library.

1. Thanks to [americodias](https://github.com/americodias) to report bugs in [Wrong frequency #1](https://github.com/khoih-prog/RP2040_PWM/issues/1) leading to v1.0.1
1. Thanks to [americodias](https://github.com/americodias) to report bugs in

- [Wrong frequency #1](https://github.com/khoih-prog/RP2040_PWM/issues/1) leading to v1.0.1
- [Change the PWM frequency #2](https://github.com/khoih-prog/RP2040_PWM/issues/2) leading to v1.0.2

<table>
<tr>
<td align="center"><a href="https://github.com/americodias"><img src="https://github.com/americodias.png" width="100px;" alt="americodias"/><br /><sub><b>Américo Dias</b></sub></a><br /></td>
<td align="center"><a href="https://github.com/americodias"><img src="https://github.com/americodias.png" width="100px;" alt="americodias"/><br /><sub><b>⭐️ Américo Dias</b></sub></a><br /></td>
</tr>
</table>

Expand Down
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
## Table of Contents

* [Changelog](#changelog)
* [Releases v1.0.2](#Releases-v102)
* [Releases v1.0.1](#Releases-v101)
* [Initial Releases v1.0.0](#Initial-Releases-v100)

Expand All @@ -20,6 +21,10 @@

## Changelog

### Releases v1.0.2

1. Fix bug not changing frequency dynamically. See [Change the PWM frequency #2](https://github.com/khoih-prog/RP2040_PWM/issues/2)

### Releases v1.0.1

1. Fix bug generating wrong frequency. See [Wrong frequency #1](https://github.com/khoih-prog/RP2040_PWM/issues/1)
Expand Down
96 changes: 96 additions & 0 deletions examples/PWM_DynamicFreq/PWM_DynamicFreq.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/****************************************************************************************************************************
PWM_DynamicFreq.ino
For RP2040 boards
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/RP2040_PWM
Licensed under MIT license
The RP2040 PWM block has 8 identical slices. Each slice can drive two PWM output signals, or measure the frequency
or duty cycle of an input signal. This gives a total of up to 16 controllable PWM outputs. All 30 GPIO pins can be driven
by the PWM block
Version: 1.0.2
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K.Hoang 21/09/2021 Initial coding for RP2040 using ArduinoCore-mbed or arduino-pico core
1.0.1 K.Hoang 24/09/2021 Fix bug generating wrong frequency
1.0.2 K.Hoang 04/10/2021 Fix bug not changing frequency dynamically
*****************************************************************************************************************************/

#if ( defined(ARDUINO_NANO_RP2040_CONNECT) || defined(ARDUINO_RASPBERRY_PI_PICO) || defined(ARDUINO_ADAFRUIT_FEATHER_RP2040) || \
defined(ARDUINO_GENERIC_RP2040) ) && defined(ARDUINO_ARCH_MBED)

#warning USING_MBED_RP2040_PWM

#elif ( defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_RASPBERRY_PI_PICO) || defined(ARDUINO_ADAFRUIT_FEATHER_RP2040) || \
defined(ARDUINO_GENERIC_RP2040) ) && !defined(ARDUINO_ARCH_MBED)

#warning USING_MBED_RP2040_PWM
#else
#error This code is intended to run on the RP2040 mbed_nano, mbed_rp2040 or arduino-pico platform! Please check your Tools->Board setting.
#endif

#define _PWM_LOGLEVEL_ 4

#include "RP2040_PWM.h"

#define LED_ON LOW
#define LED_OFF HIGH

#define pin0 16 // PWM channel 4B (D2)

RP2040_PWM* PWM_Instance;

double frequency;

char dashLine[] = "=============================================================";

void setup()
{
Serial.begin(115200);
while (!Serial);
delay(100);

Serial.print(F("\nStarting PWM_DynamicFreq on ")); Serial.println(BOARD_NAME);
Serial.println(RP2040_PWM_VERSION);

frequency = 1000;
PWM_Instance = new RP2040_PWM(pin0, frequency, 50);

if (PWM_Instance)
{
PWM_Instance->setPWM();
}

Serial.println(dashLine);
}

void printPWMInfo(RP2040_PWM* PWM_Instance)
{
uint32_t div = PWM_Instance->get_DIV();
uint32_t top = PWM_Instance->get_TOP();

Serial.print("Actual PWM Frequency = "); Serial.println(PWM_Instance->getActualFreq());

PWM_LOGDEBUG5("TOP =", top, ", DIV =", div, ", CPU_freq =", PWM_Instance->get_freq_CPU());
}



void loop()
{
delay(5000);
frequency = 2000;
Serial.print(F("Change PWM Freq to ")); Serial.println(frequency);
PWM_Instance->setPWM(pin0, frequency, 50, true);

printPWMInfo(PWM_Instance);

delay(5000);
frequency = 1000;
Serial.print(F("Change PWM Freq to ")); Serial.println(frequency);
PWM_Instance->setPWM(pin0, frequency, 50, true);
printPWMInfo(PWM_Instance);
}
5 changes: 3 additions & 2 deletions examples/PWM_Multi/PWM_Multi.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
or duty cycle of an input signal. This gives a total of up to 16 controllable PWM outputs. All 30 GPIO pins can be driven
by the PWM block
Version: 1.0.1
Version: 1.0.2
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K.Hoang 21/09/2021 Initial coding for RP2040 using ArduinoCore-mbed or arduino-pico core
1.0.1 K.Hoang 24/09/2021 Fix bug generating wrong frequency
1.0.2 K.Hoang 04/10/2021 Fix bug not changing frequency dynamically
*****************************************************************************************************************************/

#if ( defined(ARDUINO_NANO_RP2040_CONNECT) || defined(ARDUINO_RASPBERRY_PI_PICO) || defined(ARDUINO_ADAFRUIT_FEATHER_RP2040) || \
Expand Down Expand Up @@ -93,7 +94,7 @@ void setup()

Serial.print("\t\t"); Serial.println(PWM_Instance[index]->getActualFreq());

PWM_LOGDEBUG5("TOP = ", top, ", DIV = ", div, ", CPU_freq = ", PWM_Instance[index]->get_freq_CPU());
PWM_LOGDEBUG5("TOP =", top, ", DIV =", div, ", CPU_freq =", PWM_Instance[index]->get_freq_CPU());
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "RP2040_PWM",
"version": "1.0.1",
"version": "1.0.2",
"keywords": "timing, device, control, timer, pwm, pwm-slice, hardware-based-pwm, high-frequency-pwm, hardware-pwm, mission-critical, accuracy, non-blocking, mbed, mbed-nano, mbed-rp2040, rpi-pico, rp2040, nano-rp2040-connect, precise, hardware",
"description": "This library enables you to use Hardware-based PWM channels on RP2040-based boards, such as Nano_RP2040_Connect, RASPBERRY_PI_PICO, with either Arduino-mbed (mbed_nano or mbed_rp2040) or arduino-pico core to create and output PWM any GPIO pin. The most important feature is they're purely hardware-based PWM channels, supporting very high PWM frequencies. Therefore, their executions are not blocked by bad-behaving functions or tasks. This important feature is absolutely necessary for mission-critical tasks. These hardware-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 ISR, millis() or micros(). That's necessary if you need to control devices requiring high precision",
"authors":
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=RP2040_PWM
version=1.0.1
version=1.0.2
author=Khoi Hoang <khoih.prog@gmail.com>
maintainer=Khoi Hoang <khoih.prog@gmail.com>
sentence=his library enables you to use Hardware-based PWM channels on RP2040-based boards, such as Nano_RP2040_Connect, RASPBERRY_PI_PICO, with either Arduino-mbed (mbed_nano or mbed_rp2040) or arduino-pico core to create and output PWM to any GPIO pin.
Expand Down
Loading

0 comments on commit bad37e1

Please sign in to comment.