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

Commit 97018ae

Browse files
authored
v1.0.1 to add PWM_StepperControl example
### Releases v1.0.1 1. Add example [PWM_StepperControl](https://github.com/khoih-prog/MBED_RP2040_PWM/examples/PWM_StepperControl) to demo how to control Stepper Motor using PWM.
1 parent 4c44c83 commit 97018ae

9 files changed

+121
-30
lines changed

CONTRIBUTING.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,33 @@ However, before reporting a bug please check through the following:
1010

1111
If you don't find anything, please [open a new issue](https://github.com/khoih-prog/MBED_RP2040_PWM/issues/new).
1212

13+
---
14+
1315
### How to submit a bug report
1416

1517
Please ensure to specify the following:
1618

1719
* Arduino IDE version (e.g. 1.8.19) or Platform.io version
18-
* `ArduinoCore-mbed` Core Version (e.g. `ArduinoCore-mbed` mbed_nano core v3.4.1)
20+
* `ArduinoCore-mbed` Core Version (e.g. `ArduinoCore-mbed` mbed_nano core v3.5.4)
1921
* `RP2040` Board type (e.g. Nano_RP2040_Connect, RaspberryPi Pico,etc.)
2022
* Contextual information (e.g. what you were trying to achieve)
2123
* Simplest possible steps to reproduce
2224
* Anything that might be relevant in your opinion, such as:
2325
* Operating system (Windows, Ubuntu, etc.) and the output of `uname -a`
2426
* Network configuration
2527

28+
Please be educated, civilized and constructive as you've always been. Disrespective posts against [GitHub Code of Conduct](https://docs.github.com/en/site-policy/github-terms/github-event-code-of-conduct) will be ignored and deleted.
29+
30+
---
2631

2732
### Example
2833

2934
```
3035
Arduino IDE version: 1.8.19
31-
`ArduinoCore-mbed` mbed_nano core v3.4.1
36+
`ArduinoCore-mbed` mbed_nano core v3.5.4
3237
Nano_RP2040_Connect
3338
OS: Ubuntu 20.04 LTS
34-
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
39+
Linux xy-Inspiron-3593 5.15.0-58-generic #64~20.04.1-Ubuntu SMP Fri Jan 6 16:42:31 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
3540
3641
Context:
3742
I encountered a crash while using this library
@@ -70,4 +75,6 @@ xy@xy-Inspiron-3593:~/Arduino/xy/MBED_RP2040_PWM_GitHub$
7075

7176
```
7277
xy@xy-Inspiron-3593:~/Arduino/xy/MBED_RP2040_PWM_GitHub$ bash utils/restyle.sh
78+
79+
7380
```

changelog.md

+5
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@
1717
## Table of Contents
1818

1919
* [Changelog](#changelog)
20+
* [Releases v1.0.1](#Releases-v101)
2021
* [Initial Releases v1.0.0](#Initial-Releases-v100)
2122

2223
---
2324
---
2425

2526
## Changelog
2627

28+
### Releases v1.0.1
29+
30+
1. Add example [PWM_StepperControl](https://github.com/khoih-prog/MBED_RP2040_PWM/examples/PWM_StepperControl) to demo how to control Stepper Motor using PWM. Check [Using PWM to step a stepper driver #16](https://github.com/khoih-prog/RP2040_PWM/issues/16)
31+
2732
### Initial Releases v1.0.0
2833

2934
1. Initial coding to support RP2040-based boards such as **Nano_RP2040_Connect and RASPBERRY_PI_PICO**, etc. using [**Arduino-mbed** core](https://github.com/arduino/ArduinoCore-mbed)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/****************************************************************************************************************************
2+
PWM_StepperControl.ino
3+
For RP2040 boards
4+
Written by Khoi Hoang
5+
6+
Built by Khoi Hoang https://github.com/khoih-prog/MBED_RP2040_PWM
7+
Licensed under MIT license
8+
9+
Credits of Paul van Dinther (https://github.com/dinther). Check https://github.com/khoih-prog/RP2040_PWM/issues/16
10+
*****************************************************************************************************************************/
11+
12+
// Use with Stepper-Motor driver, such as TMC2209
13+
14+
#if !( defined(ARDUINO_NANO_RP2040_CONNECT) || defined(ARDUINO_RASPBERRY_PI_PICO) || defined(ARDUINO_ADAFRUIT_FEATHER_RP2040) || \
15+
defined(ARDUINO_GENERIC_RP2040) ) && defined(ARDUINO_ARCH_MBED)
16+
#error This code is intended to run on the MBED RP2040 platform! Please check your Tools->Board setting.
17+
#endif
18+
19+
#define _PWM_LOGLEVEL_ 1
20+
21+
#include <MBED_RP2040_PWM.h>
22+
23+
mbed::PwmOut* stepper = nullptr;
24+
25+
#define STEP_PIN 8
26+
#define DIR_PIN 9
27+
28+
void setSpeed(int speed)
29+
{
30+
if (speed == 0)
31+
{
32+
// Use DC = 0 to stop stepper
33+
setPWM(stepper, STEP_PIN, 500, 0);
34+
}
35+
else
36+
{
37+
// Set the frequency of the PWM output and a duty cycle of 50%
38+
digitalWrite(DIR_PIN, (speed < 0));
39+
setPWM(stepper, STEP_PIN, abs(speed), 50);
40+
}
41+
}
42+
43+
void setup()
44+
{
45+
pinMode(DIR_PIN, OUTPUT);
46+
47+
Serial.begin(115200);
48+
49+
while (!Serial && millis() < 5000);
50+
51+
delay(100);
52+
53+
Serial.print(F("\nStarting PWM_StepperControl on "));
54+
Serial.println(BOARD_NAME);
55+
Serial.println(MBED_RP2040_PWM_VERSION);
56+
57+
//setSpeed(0);
58+
}
59+
60+
void loop()
61+
{
62+
setSpeed(1000);
63+
delay(3000);
64+
65+
// Stop before reversing
66+
setSpeed(0);
67+
delay(3000);
68+
69+
// Reversing
70+
setSpeed(-500);
71+
delay(3000);
72+
73+
// Stop before reversing
74+
setSpeed(0);
75+
delay(3000);
76+
}

library.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "MBED_RP2040_PWM",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"keywords": "timing, device, control, timer, pwm, interrupt, hardware-pwm, hardware-timer, mission-critical, accuracy, non-blocking, mbed, mbed-rp2040, mbed-nano, rp2040, nano-rp2040-connect, raspberry-pi-pico, precise, hardware",
55
"description": "This library enables you to use Hardware Timers on RP2040-based RP2040 board to create and output PWM to pins. These PWM channels, using RP2040 Hardware-PWM channels, still work even if other functions are blocking. Moreover, they are much more precise (certainly depending on clock frequency accuracy) than other software ISR-based PWM, using millis(), micros() or Timer Interrupt. This important feature is absolutely necessary for mission-critical tasks. You can start, stop, change and restore the settings of any PWM channel on-the-fly",
66
"authors":

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=MBED_RP2040_PWM
2-
version=1.0.0
2+
version=1.0.1
33
author=Khoi Hoang <[email protected]>
44
maintainer=Khoi Hoang <[email protected]>
55
sentence=This library enables you to use Hardware-based PWM to create and output PWM to pins on RP2040 board to create and output PWM to pins.

src/MBED_RP2040_PWM.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
Built by Khoi Hoang https://github.com/khoih-prog/MBED_RP2040_PWM
77
Licensed under MIT license
88
9-
Version: 1.0.0
9+
Version: 1.0.1
1010
1111
Version Modified By Date Comments
1212
------- ----------- ---------- -----------
1313
1.0.0 K.Hoang 09/02/2022 Initial coding for RP2040 using ArduinoCore-mbed mbed_rp2040 core
14+
1.0.1 K.Hoang 21/01/2023 Add `PWM_StepperControl` example
1415
*****************************************************************************************************************************/
1516

1617
#pragma once

src/MBED_RP2040_PWM.hpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
Built by Khoi Hoang https://github.com/khoih-prog/MBED_RP2040_PWM
77
Licensed under MIT license
88
9-
Version: 1.0.0
9+
Version: 1.0.1
1010
1111
Version Modified By Date Comments
1212
------- ----------- ---------- -----------
1313
1.0.0 K.Hoang 09/02/2022 Initial coding for RP2040 using ArduinoCore-mbed mbed_rp2040 core
14+
1.0.1 K.Hoang 21/01/2023 Add `PWM_StepperControl` example
1415
*****************************************************************************************************************************/
1516

1617
#pragma once
@@ -24,13 +25,13 @@
2425
#endif
2526

2627
#ifndef MBED_RP2040_PWM_VERSION
27-
#define MBED_RP2040_PWM_VERSION "MBED_RP2040_PWM v1.0.0"
28+
#define MBED_RP2040_PWM_VERSION "MBED_RP2040_PWM v1.0.1"
2829

2930
#define MBED_RP2040_PWM_VERSION_MAJOR 1
3031
#define MBED_RP2040_PWM_VERSION_MINOR 0
31-
#define MBED_RP2040_PWM_VERSION_PATCH 0
32+
#define MBED_RP2040_PWM_VERSION_PATCH 1
3233

33-
#define MBED_RP2040_PWM_VERSION_INT 1000000
34+
#define MBED_RP2040_PWM_VERSION_INT 1000001
3435
#endif
3536

3637

src/MBED_RP2040_PWM_Impl.h

+19-19
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
Built by Khoi Hoang https://github.com/khoih-prog/MBED_RP2040_PWM
77
Licensed under MIT license
88
9-
Version: 1.0.0
9+
Version: 1.0.1
1010
1111
Version Modified By Date Comments
1212
------- ----------- ---------- -----------
1313
1.0.0 K.Hoang 09/02/2022 Initial coding for RP2040 using ArduinoCore-mbed mbed_rp2040 core
14+
1.0.1 K.Hoang 21/01/2023 Add `PWM_StepperControl` example
1415
*****************************************************************************************************************************/
1516

1617
#ifndef MBED_RP2040_PWM_IMPL_H
@@ -32,9 +33,9 @@ bool isValidPWMPin(const pin_size_t& pin)
3233
{
3334
return true;
3435
}
35-
36+
3637
PWM_LOGERROR1("Not PWM pin = ", pin);
37-
38+
3839
return false;
3940
}
4041

@@ -43,7 +44,7 @@ bool isValidPWMDutyCycle(const pin_size_t& pin, const float& dutyCycle)
4344
if ( (dutyCycle < 0.0f) || (dutyCycle > 100.0f) )
4445
{
4546
PWM_LOGERROR3("Bad dutyCycle = ", dutyCycle, ", pin = ", pin);
46-
47+
4748
return false;
4849
}
4950

@@ -59,54 +60,53 @@ bool isValidPWMFreq(const pin_size_t& pin, const float& frequency)
5960

6061
return false;
6162
}
62-
63+
6364
return true;
6465
}
6566

6667
bool isValidPWMSettings(const pin_size_t& pin, const float& frequency, const float& dutyCycle)
6768
{
6869
if ( !isValidPWMPin(pin) || !isValidPWMFreq(pin, frequency) || !isValidPWMDutyCycle(pin, dutyCycle) )
69-
{
70+
{
7071
return false;
71-
}
72-
72+
}
73+
7374
return true;
7475
}
7576

7677
// dutyCycle from 0.0f to 100.0f
7778
mbed::PwmOut* setPWM(mbed::PwmOut* &pwm, const pin_size_t& pin, const float& frequency, const float& dutyCycle)
7879
{
79-
PWM_LOGDEBUG7("Freq = ", frequency, ", \tDutyCycle = ", dutyCycle, ", \tDutyCycle % = ", dutyCycle / 100, ", \tPin = ",
80-
pin);
81-
80+
PWM_LOGDEBUG7("Freq = ", frequency, ", \tDutyCycle = ", dutyCycle, ", \tDutyCycle % = ", dutyCycle / 100, ", \tPin = ", pin);
81+
8282
if ( !isValidPWMSettings(pin, frequency, dutyCycle) )
83-
{
83+
{
8484
return NULL;
8585
}
8686

8787
float percent = dutyCycle / 100.0f;
88-
88+
8989
if (digitalPinToPwm(pin) == NULL)
9090
{
9191
PWM_LOGDEBUG("New pwm");
92-
92+
9393
pwm = new mbed::PwmOut(digitalPinToPinName(pin));
94-
94+
9595
digitalPinToPwm(pin) = pwm;
9696

97-
pwm->period_us( 1000000.0f / frequency );
97+
pwm->period_us( 1000000.0f/frequency );
9898

9999
pwm->write(percent);
100100
}
101101
else if (pwm && (digitalPinToPwm(pin) == pwm) )
102102
{
103103
PWM_LOGDEBUG("Use existing pwm");
104-
105-
pwm->period_us( 1000000.0f / frequency );
104+
105+
pwm->period_us( 1000000.0f/frequency );
106106

107107
pwm->write(percent);
108108
}
109-
109+
110110
return pwm;
111111
}
112112

src/PWM_Generic_Debug.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
Built by Khoi Hoang https://github.com/khoih-prog/RP2040_PWM
77
Licensed under MIT license
88
9-
Version: 1.0.0
9+
Version: 1.0.1
1010
1111
Version Modified By Date Comments
1212
------- ----------- ---------- -----------
1313
1.0.0 K.Hoang 09/02/2022 Initial coding for RP2040 using ArduinoCore-mbed mbed_rp2040 core
14+
1.0.1 K.Hoang 21/01/2023 Add `PWM_StepperControl` example
1415
*****************************************************************************************************************************/
1516

1617
#pragma once

0 commit comments

Comments
 (0)