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

Commit 9984229

Browse files
authored
v1.1.0 to modify PWM settings on-the-fly
### Releases v1.1.0 1. Add functions to modify PWM settings on-the-fly 2. Add example to demo how to modify PWM settings on-the-fly
1 parent 270fd04 commit 9984229

24 files changed

+315
-78
lines changed

src/PWM_Generic_Debug.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
Therefore, their executions are not blocked by bad-behaving functions / tasks.
1313
This important feature is absolutely necessary for mission-critical tasks.
1414
15-
Version: 1.0.0
15+
Version: 1.1.0
1616
1717
Version Modified By Date Comments
1818
------- ----------- ---------- -----------
1919
1.0.0 K.Hoang 22/09/2021 Initial coding for Portenta_H7
20+
1.1.0 K Hoang 10/11/2021 Add functions to modify PWM settings on-the-fly
2021
*****************************************************************************************************************************/
2122

2223
#pragma once

src/Portenta_H7_Slow_PWM.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
Therefore, their executions are not blocked by bad-behaving functions / tasks.
1313
This important feature is absolutely necessary for mission-critical tasks.
1414
15-
Version: 1.0.0
15+
Version: 1.1.0
1616
1717
Version Modified By Date Comments
1818
------- ----------- ---------- -----------
1919
1.0.0 K.Hoang 22/09/2021 Initial coding for Portenta_H7
20+
1.1.0 K Hoang 10/11/2021 Add functions to modify PWM settings on-the-fly
2021
*****************************************************************************************************************************/
2122

2223
#pragma once
@@ -44,7 +45,7 @@
4445
#endif
4546

4647
#ifndef PORTENTA_H7_SLOW_PWM_VERSION
47-
#define PORTENTA_H7_SLOW_PWM_VERSION "PORTENTA_H7_SLOW_PWM v1.0.0"
48+
#define PORTENTA_H7_SLOW_PWM_VERSION "PORTENTA_H7_SLOW_PWM v1.1.0"
4849
#endif
4950

5051
///////////////////////////////////////////

src/Portenta_H7_Slow_PWM_ISR.h

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
Therefore, their executions are not blocked by bad-behaving functions / tasks.
1313
This important feature is absolutely necessary for mission-critical tasks.
1414
15-
Version: 1.0.0
15+
Version: 1.1.0
1616
1717
Version Modified By Date Comments
1818
------- ----------- ---------- -----------
1919
1.0.0 K.Hoang 22/09/2021 Initial coding for Portenta_H7
20+
1.1.0 K Hoang 10/11/2021 Add functions to modify PWM settings on-the-fly
2021
*****************************************************************************************************************************/
2122

2223
#pragma once
@@ -44,7 +45,7 @@
4445
#endif
4546

4647
#ifndef PORTENTA_H7_SLOW_PWM_VERSION
47-
#define PORTENTA_H7_SLOW_PWM_VERSION "PORTENTA_H7_SLOW_PWM v1.0.0"
48+
#define PORTENTA_H7_SLOW_PWM_VERSION "PORTENTA_H7_SLOW_PWM v1.1.0"
4849
#endif
4950

5051
#ifndef _PWM_LOGLEVEL_
@@ -95,43 +96,74 @@ class Portenta_H7_Slow_PWM_ISR
9596

9697
//////////////////////////////////////////////////////////////////
9798
// PWM
98-
void setPWM(uint32_t pin, uint32_t frequency, uint32_t dutycycle, timer_callback StartCallback = nullptr,
99+
// Return the channelNum if OK, -1 if error
100+
int setPWM(uint32_t pin, double frequency, uint32_t dutycycle, timer_callback StartCallback = nullptr,
99101
timer_callback StopCallback = nullptr)
102+
//void setPWM(uint32_t pin, uint32_t frequency, uint32_t dutycycle, timer_callback StartCallback = nullptr,
103+
// timer_callback StopCallback = nullptr)
100104
{
101105
uint32_t period = 0;
102106

103107
if ( ( frequency != 0 ) && ( frequency <= 1000 ) )
104108
{
105109
#if USING_MICROS_RESOLUTION
106110
// period in us
107-
period = 1000000 / frequency;
111+
period = 1000000.0f / frequency;
108112
#else
109113
// period in ms
110-
period = 1000 / frequency;
114+
period = 1000.0f / frequency;
111115
#endif
112116
}
113117
else
114118
{
115-
PWM_LOGERROR("Error: Invalid frequency, max is 500Hz");
119+
PWM_LOGERROR("Error: Invalid frequency, max is 1000Hz");
120+
121+
return -1;
116122
}
117123

118-
setupPWMChannel(pin, period, dutycycle, (void *) StartCallback, (void *) StopCallback);
124+
return setupPWMChannel(pin, period, dutycycle, (void *) StartCallback, (void *) StopCallback);
119125
}
120126

121-
#if USING_MICROS_RESOLUTION
122-
//period in us
123-
void setPWM_Period(uint32_t pin, uint32_t period, uint32_t dutycycle, timer_callback StartCallback = nullptr,
124-
timer_callback StopCallback = nullptr)
125-
#else
126-
// PWM
127-
//period in ms
128-
void setPWM_Period(uint32_t pin, uint32_t period, uint32_t dutycycle, timer_callback StartCallback = nullptr,
129-
timer_callback StopCallback = nullptr)
130-
#endif
127+
// period in us
128+
// Return the channelNum if OK, -1 if error
129+
int setPWM_Period(uint32_t pin, uint32_t period, uint32_t dutycycle, timer_callback StartCallback = nullptr,
130+
timer_callback StopCallback = nullptr)
131131
{
132-
setupPWMChannel(pin, period, dutycycle, (void *) StartCallback, (void *) StopCallback);
132+
return setupPWMChannel(pin, period, dutycycle, (void *) StartCallback, (void *) StopCallback);
133133
}
134134

135+
//////////////////////////////////////////////////////////////////
136+
137+
// low level function to modify a PWM channel
138+
// returns the true on success or false on failure
139+
bool modifyPWMChannel(unsigned channelNum, uint32_t pin, double frequency, uint32_t dutycycle)
140+
{
141+
uint32_t period = 0;
142+
143+
if ( ( frequency > 0 ) && ( frequency <= 500 ) )
144+
{
145+
#if USING_MICROS_RESOLUTION
146+
// period in us
147+
period = 1000000.0f / frequency;
148+
#else
149+
// period in ms
150+
period = 1000.0f / frequency;
151+
#endif
152+
}
153+
else
154+
{
155+
PWM_LOGERROR("Error: Invalid frequency, max is 500Hz");
156+
return false;
157+
}
158+
159+
return modifyPWMChannel_Period(channelNum, pin, period, dutycycle);
160+
}
161+
162+
//////////////////////////////////////////////////////////////////
163+
164+
//period in us
165+
bool modifyPWMChannel_Period(unsigned channelNum, uint32_t pin, uint32_t period, uint32_t dutycycle);
166+
135167
//////////////////////////////////////////////////////////////////
136168

137169
// destroy the specified PWM channel

src/Portenta_H7_Slow_PWM_ISR.hpp

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
Therefore, their executions are not blocked by bad-behaving functions / tasks.
1313
This important feature is absolutely necessary for mission-critical tasks.
1414
15-
Version: 1.0.0
15+
Version: 1.1.0
1616
1717
Version Modified By Date Comments
1818
------- ----------- ---------- -----------
1919
1.0.0 K.Hoang 22/09/2021 Initial coding for Portenta_H7
20+
1.1.0 K Hoang 10/11/2021 Add functions to modify PWM settings on-the-fly
2021
*****************************************************************************************************************************/
2122

2223
#pragma once
@@ -170,6 +171,8 @@ int Portenta_H7_Slow_PWM_ISR::setupPWMChannel(uint32_t pin, uint32_t period, uin
170171
digitalWrite(pin, HIGH);
171172
PWM[channelNum].pinHigh = true;
172173

174+
PWM[channelNum].prevTime = timeNow();
175+
173176
PWM[channelNum].callbackStart = cbStartFunc;
174177
PWM[channelNum].callbackStop = cbStopFunc;
175178

@@ -183,6 +186,44 @@ int Portenta_H7_Slow_PWM_ISR::setupPWMChannel(uint32_t pin, uint32_t period, uin
183186
return channelNum;
184187
}
185188

189+
///////////////////////////////////////////////////
190+
191+
bool Portenta_H7_Slow_PWM_ISR::modifyPWMChannel_Period(unsigned channelNum, uint32_t pin, uint32_t period, uint32_t dutycycle)
192+
{
193+
// Invalid input, such as period = 0, etc
194+
if ( (period == 0) || (dutycycle > 100) )
195+
{
196+
PWM_LOGERROR("Error: Invalid period or dutycycle");
197+
return false;
198+
}
199+
200+
if (channelNum > MAX_NUMBER_CHANNELS)
201+
{
202+
PWM_LOGERROR("Error: channelNum > MAX_NUMBER_CHANNELS");
203+
return false;
204+
}
205+
206+
if (PWM[channelNum].pin != pin)
207+
{
208+
PWM_LOGERROR("Error: channelNum and pin mismatched");
209+
return false;
210+
}
211+
212+
PWM[channelNum].period = period;
213+
PWM[channelNum].onTime = ( period * dutycycle ) / 100;
214+
215+
digitalWrite(pin, HIGH);
216+
PWM[channelNum].pinHigh = true;
217+
218+
PWM[channelNum].prevTime = timeNow();
219+
220+
PWM_LOGDEBUG0("Channel : "); PWM_LOGDEBUG0(channelNum); PWM_LOGDEBUG0("\tPeriod : "); PWM_LOGDEBUG0(PWM[channelNum].period);
221+
PWM_LOGDEBUG0("\t\tOnTime : "); PWM_LOGDEBUG0(PWM[channelNum].onTime); PWM_LOGDEBUG0("\tStart_Time : "); PWM_LOGDEBUGLN0(PWM[channelNum].prevTime);
222+
223+
return true;
224+
}
225+
226+
186227
///////////////////////////////////////////////////
187228

188229
void Portenta_H7_Slow_PWM_ISR::deleteChannel(unsigned channelNum)

src/stm32/HardwareTimer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
Therefore, their executions are not blocked by bad-behaving functions / tasks.
1414
This important feature is absolutely necessary for mission-critical tasks.
1515
16-
Version: 1.0.0
16+
Version: 1.1.0
1717
1818
Version Modified By Date Comments
1919
------- ----------- ---------- -----------
2020
1.0.0 K.Hoang 22/09/2021 Initial coding for Portenta_H7
21+
1.1.0 K Hoang 10/11/2021 Add functions to modify PWM settings on-the-fly
2122
*****************************************************************************************************************************/
2223

2324
// Modified from stm32 core v2.0.0

src/stm32/HardwareTimer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
Therefore, their executions are not blocked by bad-behaving functions / tasks.
1414
This important feature is absolutely necessary for mission-critical tasks.
1515
16-
Version: 1.0.0
16+
Version: 1.1.0
1717
1818
Version Modified By Date Comments
1919
------- ----------- ---------- -----------
2020
1.0.0 K.Hoang 22/09/2021 Initial coding for Portenta_H7
21+
1.1.0 K Hoang 10/11/2021 Add functions to modify PWM settings on-the-fly
2122
*****************************************************************************************************************************/
2223

2324
// Modified from stm32 core v2.0.0

src/stm32/timer.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
Therefore, their executions are not blocked by bad-behaving functions / tasks.
1414
This important feature is absolutely necessary for mission-critical tasks.
1515
16-
Version: 1.0.0
16+
Version: 1.1.0
1717
1818
Version Modified By Date Comments
1919
------- ----------- ---------- -----------
2020
1.0.0 K.Hoang 22/09/2021 Initial coding for Portenta_H7
21+
1.1.0 K Hoang 10/11/2021 Add functions to modify PWM settings on-the-fly
2122
*****************************************************************************************************************************/
2223

2324
// Modified from stm32 core v2.0.0

src/stm32/timer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
Therefore, their executions are not blocked by bad-behaving functions / tasks.
1414
This important feature is absolutely necessary for mission-critical tasks.
1515
16-
Version: 1.0.0
16+
Version: 1.1.0
1717
1818
Version Modified By Date Comments
1919
------- ----------- ---------- -----------
2020
1.0.0 K.Hoang 22/09/2021 Initial coding for Portenta_H7
21+
1.1.0 K Hoang 10/11/2021 Add functions to modify PWM settings on-the-fly
2122
*****************************************************************************************************************************/
2223

2324
// Modified from stm32 core v2.0.0

src_cpp/PWM_Generic_Debug.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
Therefore, their executions are not blocked by bad-behaving functions / tasks.
1313
This important feature is absolutely necessary for mission-critical tasks.
1414
15-
Version: 1.0.0
15+
Version: 1.1.0
1616
1717
Version Modified By Date Comments
1818
------- ----------- ---------- -----------
1919
1.0.0 K.Hoang 22/09/2021 Initial coding for Portenta_H7
20+
1.1.0 K Hoang 10/11/2021 Add functions to modify PWM settings on-the-fly
2021
*****************************************************************************************************************************/
2122

2223
#pragma once

src_cpp/Portenta_H7_Slow_PWM.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
Therefore, their executions are not blocked by bad-behaving functions / tasks.
1313
This important feature is absolutely necessary for mission-critical tasks.
1414
15-
Version: 1.0.0
15+
Version: 1.1.0
1616
1717
Version Modified By Date Comments
1818
------- ----------- ---------- -----------
1919
1.0.0 K.Hoang 22/09/2021 Initial coding for Portenta_H7
20+
1.1.0 K Hoang 10/11/2021 Add functions to modify PWM settings on-the-fly
2021
*****************************************************************************************************************************/
2122

2223
#pragma once
@@ -44,7 +45,7 @@
4445
#endif
4546

4647
#ifndef PORTENTA_H7_SLOW_PWM_VERSION
47-
#define PORTENTA_H7_SLOW_PWM_VERSION "PORTENTA_H7_SLOW_PWM v1.0.0"
48+
#define PORTENTA_H7_SLOW_PWM_VERSION "PORTENTA_H7_SLOW_PWM v1.1.0"
4849
#endif
4950

5051
///////////////////////////////////////////

0 commit comments

Comments
 (0)