6
6
7
7
#define SIMPLEFOC_DEBUG_RP2040
8
8
9
-
10
-
11
- #include " Arduino.h"
12
- #include " communication/SimpleFOCDebug.h"
9
+ #include " ../hardware_api.h"
13
10
14
11
15
12
// these defines determine the polarity of the PWM output. Normally, the polarity is active-high,
31
28
#endif
32
29
33
30
31
+ #define _PWM_FREQUENCY 24000
32
+ #define _PWM_FREQUENCY_MAX 66000
33
+ #define _PWM_FREQUENCY_MIN 5000
34
+
34
35
35
36
typedef struct RP2040DriverParams {
36
37
int pins[6 ];
@@ -59,7 +60,7 @@ void setupPWM(int pin, long pwm_frequency, bool invert, RP2040DriverParams* para
59
60
pwm_set_phase_correct (slice, true );
60
61
uint16_t wrapvalue = ((125L * 1000L * 1000L ) / pwm_frequency) / 2L - 1L ;
61
62
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
63
64
#ifdef SIMPLEFOC_DEBUG_RP2040
64
65
SimpleFOCDebug::print (" Configuring pin " );
65
66
SimpleFOCDebug::print (pin);
@@ -96,6 +97,8 @@ void syncSlices() {
96
97
97
98
void * _configure2PWM (long pwm_frequency, const int pinA, const int pinB) {
98
99
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);
99
102
params->pwm_frequency = pwm_frequency;
100
103
setupPWM (pinA, pwm_frequency, !SIMPLEFOC_PWM_ACTIVE_HIGH, params, 0 );
101
104
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) {
107
110
108
111
void * _configure3PWM (long pwm_frequency, const int pinA, const int pinB, const int pinC) {
109
112
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);
110
115
params->pwm_frequency = pwm_frequency;
111
116
setupPWM (pinA, pwm_frequency, !SIMPLEFOC_PWM_ACTIVE_HIGH, params, 0 );
112
117
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
120
125
121
126
void * _configure4PWM (long pwm_frequency, const int pin1A, const int pin1B, const int pin2A, const int pin2B) {
122
127
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);
123
130
params->pwm_frequency = pwm_frequency;
124
131
setupPWM (pin1A, pwm_frequency, !SIMPLEFOC_PWM_ACTIVE_HIGH, params, 0 );
125
132
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
133
140
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) {
134
141
// non-PIO solution...
135
142
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);
136
145
params->pwm_frequency = pwm_frequency;
137
146
params->dead_zone = dead_zone;
138
147
setupPWM (pinA_h, pwm_frequency, !SIMPLEFOC_PWM_HIGHSIDE_ACTIVE_HIGH, params, 0 );
0 commit comments