Skip to content

Commit eb5347f

Browse files
author
Richard Unger
committed
added default PWM frequency to RP2040 driver
1 parent 581af18 commit eb5347f

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/drivers/hardware_specific/rp2040_mcu.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66

77
#define SIMPLEFOC_DEBUG_RP2040
88

9-
10-
11-
#include "Arduino.h"
12-
#include "communication/SimpleFOCDebug.h"
9+
#include "../hardware_api.h"
1310

1411

1512
// these defines determine the polarity of the PWM output. Normally, the polarity is active-high,
@@ -31,6 +28,10 @@
3128
#endif
3229

3330

31+
#define _PWM_FREQUENCY 24000
32+
#define _PWM_FREQUENCY_MAX 66000
33+
#define _PWM_FREQUENCY_MIN 5000
34+
3435

3536
typedef struct RP2040DriverParams {
3637
int pins[6];
@@ -59,7 +60,7 @@ void setupPWM(int pin, long pwm_frequency, bool invert, RP2040DriverParams* para
5960
pwm_set_phase_correct(slice, true);
6061
uint16_t wrapvalue = ((125L * 1000L * 1000L) / pwm_frequency) / 2L - 1L;
6162
if (wrapvalue < 999) wrapvalue = 999; // 66kHz, resolution 1000
62-
if (wrapvalue > 3299) wrapvalue = 3299; // 20kHz, resolution 3300
63+
if (wrapvalue > 12499) wrapvalue = 12499; // 20kHz, resolution 12500
6364
#ifdef SIMPLEFOC_DEBUG_RP2040
6465
SimpleFOCDebug::print("Configuring pin ");
6566
SimpleFOCDebug::print(pin);
@@ -96,6 +97,8 @@ void syncSlices() {
9697

9798
void* _configure2PWM(long pwm_frequency, const int pinA, const int pinB) {
9899
RP2040DriverParams* params = new RP2040DriverParams();
100+
if( !pwm_frequency || !_isset(pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY;
101+
else pwm_frequency = _constrain(pwm_frequency, _PWM_FREQUENCY_MIN, _PWM_FREQUENCY_MAX);
99102
params->pwm_frequency = pwm_frequency;
100103
setupPWM(pinA, pwm_frequency, !SIMPLEFOC_PWM_ACTIVE_HIGH, params, 0);
101104
setupPWM(pinB, pwm_frequency, !SIMPLEFOC_PWM_ACTIVE_HIGH, params, 1);
@@ -107,6 +110,8 @@ void* _configure2PWM(long pwm_frequency, const int pinA, const int pinB) {
107110

108111
void* _configure3PWM(long pwm_frequency, const int pinA, const int pinB, const int pinC) {
109112
RP2040DriverParams* params = new RP2040DriverParams();
113+
if( !pwm_frequency || !_isset(pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY;
114+
else pwm_frequency = _constrain(pwm_frequency, _PWM_FREQUENCY_MIN, _PWM_FREQUENCY_MAX);
110115
params->pwm_frequency = pwm_frequency;
111116
setupPWM(pinA, pwm_frequency, !SIMPLEFOC_PWM_ACTIVE_HIGH, params, 0);
112117
setupPWM(pinB, pwm_frequency, !SIMPLEFOC_PWM_ACTIVE_HIGH, params, 1);
@@ -120,6 +125,8 @@ void* _configure3PWM(long pwm_frequency, const int pinA, const int pinB, const i
120125

121126
void* _configure4PWM(long pwm_frequency, const int pin1A, const int pin1B, const int pin2A, const int pin2B) {
122127
RP2040DriverParams* params = new RP2040DriverParams();
128+
if( !pwm_frequency || !_isset(pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY;
129+
else pwm_frequency = _constrain(pwm_frequency, _PWM_FREQUENCY_MIN, _PWM_FREQUENCY_MAX);
123130
params->pwm_frequency = pwm_frequency;
124131
setupPWM(pin1A, pwm_frequency, !SIMPLEFOC_PWM_ACTIVE_HIGH, params, 0);
125132
setupPWM(pin1B, pwm_frequency, !SIMPLEFOC_PWM_ACTIVE_HIGH, params, 1);
@@ -133,6 +140,8 @@ void* _configure4PWM(long pwm_frequency, const int pin1A, const int pin1B, const
133140
void* _configure6PWM(long pwm_frequency, float dead_zone, const int pinA_h, const int pinA_l, const int pinB_h, const int pinB_l, const int pinC_h, const int pinC_l) {
134141
// non-PIO solution...
135142
RP2040DriverParams* params = new RP2040DriverParams();
143+
if( !pwm_frequency || !_isset(pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY;
144+
else pwm_frequency = _constrain(pwm_frequency, _PWM_FREQUENCY_MIN, _PWM_FREQUENCY_MAX);
136145
params->pwm_frequency = pwm_frequency;
137146
params->dead_zone = dead_zone;
138147
setupPWM(pinA_h, pwm_frequency, !SIMPLEFOC_PWM_HIGHSIDE_ACTIVE_HIGH, params, 0);

0 commit comments

Comments
 (0)