Skip to content

Commit c9fd8d0

Browse files
Merge pull request #209 from runger1101001/dev
RP2040 driver improvements, StepDirListener fix
2 parents b300dab + c9e14b6 commit c9fd8d0

File tree

4 files changed

+72
-39
lines changed

4 files changed

+72
-39
lines changed

src/communication/StepDirListener.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void StepDirListener::init(){
1313
}
1414

1515
void StepDirListener::enableInterrupt(void (*doA)()){
16-
attachInterrupt(digitalPinToInterrupt(pin_step), doA, CHANGE);
16+
attachInterrupt(digitalPinToInterrupt(pin_step), doA, polarity);
1717
}
1818

1919
void StepDirListener::attach(float* variable){
@@ -22,15 +22,15 @@ void StepDirListener::attach(float* variable){
2222

2323
void StepDirListener::handle(){
2424
// read step status
25-
bool step = digitalRead(pin_step);
25+
//bool step = digitalRead(pin_step);
2626
// update counter only on rising edge
27-
if(step && step != step_active){
28-
if(digitalRead(pin_dir))
27+
//if(step && step != step_active){
28+
if(digitalRead(pin_dir))
2929
count++;
30-
else
30+
else
3131
count--;
32-
}
33-
step_active = step;
32+
//}
33+
//step_active = step;
3434
// if attached variable update it
3535
if(attached_variable) *attached_variable = getValue();
3636
}

src/communication/StepDirListener.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
#include "Arduino.h"
55
#include "../common/foc_utils.h"
66

7+
8+
#if defined(_STM32_DEF_) || defined(ESP_H) || defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_SAM_DUE)
9+
#define PinStatus int
10+
#endif
11+
12+
713
/**
814
* Step/Dir listenner class for easier interraction with this communication interface.
915
*/
@@ -47,11 +53,13 @@ class StepDirListener
4753
int pin_step; //!< step pin
4854
int pin_dir; //!< direction pin
4955
long count; //!< current counter value - should be set to 0 for homing
56+
PinStatus polarity = RISING; //!< polarity of the step pin
5057

5158
private:
5259
float* attached_variable = nullptr; //!< pointer to the attached variable
5360
float counter_to_value; //!< step counter to value
54-
bool step_active = 0; //!< current step pin status (HIGH/LOW) - debouncing variable
61+
//bool step_active = 0; //!< current step pin status (HIGH/LOW) - debouncing variable
62+
5563
};
5664

5765
#endif

src/drivers/hardware_specific/rp2040_mcu.cpp

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,31 @@
66

77
#define SIMPLEFOC_DEBUG_RP2040
88

9+
#include "../hardware_api.h"
910

10-
#ifdef SIMPLEFOC_DEBUG_RP2040
1111

12-
#ifndef SIMPLEFOC_RP2040_DEBUG_SERIAL
13-
#define SIMPLEFOC_RP2040_DEBUG_SERIAL Serial
14-
#endif
12+
// these defines determine the polarity of the PWM output. Normally, the polarity is active-high,
13+
// i.e. a high-level PWM output is expected to switch on the MOSFET. But should your driver design
14+
// require inverted polarity, you can change the defines below, or set them via your build environment
15+
// or board definition files.
1516

17+
// used for 2-PWM, 3-PWM, and 4-PWM modes
18+
#ifndef SIMPLEFOC_PWM_ACTIVE_HIGH
19+
#define SIMPLEFOC_PWM_ACTIVE_HIGH true
20+
#endif
21+
// used fof 6-PWM mode, high-side
22+
#ifndef SIMPLEFOC_PWM_HIGHSIDE_ACTIVE_HIGH
23+
#define SIMPLEFOC_PWM_HIGHSIDE_ACTIVE_HIGH true
24+
#endif
25+
// used fof 6-PWM mode, low-side
26+
#ifndef SIMPLEFOC_PWM_LOWSIDE_ACTIVE_HIGH
27+
#define SIMPLEFOC_PWM_LOWSIDE_ACTIVE_HIGH true
1628
#endif
1729

18-
#include "Arduino.h"
1930

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

2135

2236
typedef struct RP2040DriverParams {
@@ -46,18 +60,18 @@ void setupPWM(int pin, long pwm_frequency, bool invert, RP2040DriverParams* para
4660
pwm_set_phase_correct(slice, true);
4761
uint16_t wrapvalue = ((125L * 1000L * 1000L) / pwm_frequency) / 2L - 1L;
4862
if (wrapvalue < 999) wrapvalue = 999; // 66kHz, resolution 1000
49-
if (wrapvalue > 3299) wrapvalue = 3299; // 20kHz, resolution 3300
63+
if (wrapvalue > 12499) wrapvalue = 12499; // 20kHz, resolution 12500
5064
#ifdef SIMPLEFOC_DEBUG_RP2040
51-
SIMPLEFOC_RP2040_DEBUG_SERIAL.print("Configuring pin ");
52-
SIMPLEFOC_RP2040_DEBUG_SERIAL.print(pin);
53-
SIMPLEFOC_RP2040_DEBUG_SERIAL.print(" slice ");
54-
SIMPLEFOC_RP2040_DEBUG_SERIAL.print(slice);
55-
SIMPLEFOC_RP2040_DEBUG_SERIAL.print(" channel ");
56-
SIMPLEFOC_RP2040_DEBUG_SERIAL.print(chan);
57-
SIMPLEFOC_RP2040_DEBUG_SERIAL.print(" frequency ");
58-
SIMPLEFOC_RP2040_DEBUG_SERIAL.print(pwm_frequency);
59-
SIMPLEFOC_RP2040_DEBUG_SERIAL.print(" top value ");
60-
SIMPLEFOC_RP2040_DEBUG_SERIAL.println(wrapvalue);
65+
SimpleFOCDebug::print("Configuring pin ");
66+
SimpleFOCDebug::print(pin);
67+
SimpleFOCDebug::print(" slice ");
68+
SimpleFOCDebug::print((int)slice);
69+
SimpleFOCDebug::print(" channel ");
70+
SimpleFOCDebug::print((int)chan);
71+
SimpleFOCDebug::print(" frequency ");
72+
SimpleFOCDebug::print((int)pwm_frequency);
73+
SimpleFOCDebug::print(" top value ");
74+
SimpleFOCDebug::println(wrapvalue);
6175
#endif
6276
pwm_set_wrap(slice, wrapvalue);
6377
wrapvalues[slice] = wrapvalue;
@@ -83,9 +97,11 @@ void syncSlices() {
8397

8498
void* _configure2PWM(long pwm_frequency, const int pinA, const int pinB) {
8599
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);
86102
params->pwm_frequency = pwm_frequency;
87-
setupPWM(pinA, pwm_frequency, false, params, 0);
88-
setupPWM(pinB, pwm_frequency, false, params, 1);
103+
setupPWM(pinA, pwm_frequency, !SIMPLEFOC_PWM_ACTIVE_HIGH, params, 0);
104+
setupPWM(pinB, pwm_frequency, !SIMPLEFOC_PWM_ACTIVE_HIGH, params, 1);
89105
syncSlices();
90106
return params;
91107
}
@@ -94,10 +110,12 @@ void* _configure2PWM(long pwm_frequency, const int pinA, const int pinB) {
94110

95111
void* _configure3PWM(long pwm_frequency, const int pinA, const int pinB, const int pinC) {
96112
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);
97115
params->pwm_frequency = pwm_frequency;
98-
setupPWM(pinA, pwm_frequency, false, params, 0);
99-
setupPWM(pinB, pwm_frequency, false, params, 1);
100-
setupPWM(pinC, pwm_frequency, false, params, 2);
116+
setupPWM(pinA, pwm_frequency, !SIMPLEFOC_PWM_ACTIVE_HIGH, params, 0);
117+
setupPWM(pinB, pwm_frequency, !SIMPLEFOC_PWM_ACTIVE_HIGH, params, 1);
118+
setupPWM(pinC, pwm_frequency, !SIMPLEFOC_PWM_ACTIVE_HIGH, params, 2);
101119
syncSlices();
102120
return params;
103121
}
@@ -107,11 +125,13 @@ void* _configure3PWM(long pwm_frequency, const int pinA, const int pinB, const i
107125

108126
void* _configure4PWM(long pwm_frequency, const int pin1A, const int pin1B, const int pin2A, const int pin2B) {
109127
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);
110130
params->pwm_frequency = pwm_frequency;
111-
setupPWM(pin1A, pwm_frequency, false, params, 0);
112-
setupPWM(pin1B, pwm_frequency, false, params, 1);
113-
setupPWM(pin2A, pwm_frequency, false, params, 2);
114-
setupPWM(pin2B, pwm_frequency, false, params, 3);
131+
setupPWM(pin1A, pwm_frequency, !SIMPLEFOC_PWM_ACTIVE_HIGH, params, 0);
132+
setupPWM(pin1B, pwm_frequency, !SIMPLEFOC_PWM_ACTIVE_HIGH, params, 1);
133+
setupPWM(pin2A, pwm_frequency, !SIMPLEFOC_PWM_ACTIVE_HIGH, params, 2);
134+
setupPWM(pin2B, pwm_frequency, !SIMPLEFOC_PWM_ACTIVE_HIGH, params, 3);
115135
syncSlices();
116136
return params;
117137
}
@@ -120,14 +140,16 @@ void* _configure4PWM(long pwm_frequency, const int pin1A, const int pin1B, const
120140
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) {
121141
// non-PIO solution...
122142
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);
123145
params->pwm_frequency = pwm_frequency;
124146
params->dead_zone = dead_zone;
125-
setupPWM(pinA_h, pwm_frequency, false, params, 0);
126-
setupPWM(pinB_h, pwm_frequency, false, params, 2);
127-
setupPWM(pinC_h, pwm_frequency, false, params, 4);
128-
setupPWM(pinA_l, pwm_frequency, true, params, 1);
129-
setupPWM(pinB_l, pwm_frequency, true, params, 3);
130-
setupPWM(pinC_l, pwm_frequency, true, params, 5);
147+
setupPWM(pinA_h, pwm_frequency, !SIMPLEFOC_PWM_HIGHSIDE_ACTIVE_HIGH, params, 0);
148+
setupPWM(pinB_h, pwm_frequency, !SIMPLEFOC_PWM_HIGHSIDE_ACTIVE_HIGH, params, 2);
149+
setupPWM(pinC_h, pwm_frequency, !SIMPLEFOC_PWM_HIGHSIDE_ACTIVE_HIGH, params, 4);
150+
setupPWM(pinA_l, pwm_frequency, SIMPLEFOC_PWM_LOWSIDE_ACTIVE_HIGH, params, 1);
151+
setupPWM(pinB_l, pwm_frequency, SIMPLEFOC_PWM_LOWSIDE_ACTIVE_HIGH, params, 3);
152+
setupPWM(pinC_l, pwm_frequency, SIMPLEFOC_PWM_LOWSIDE_ACTIVE_HIGH, params, 5);
131153
syncSlices();
132154
return params;
133155
}

src/drivers/hardware_specific/stm32_mcu.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ void _startTimers(HardwareTimer **timers_to_start, int timer_num)
170170
for (int i=0; i < timer_num; i++) {
171171
if(timers_to_start[i] == NP) return;
172172
timers_to_start[i]->resume();
173+
#ifdef SIMPLEFOC_STM32_DEBUG
174+
SIMPLEFOC_DEBUG("STM32-DRV: Starting timer ", getTimerNumber(get_timer_index(timers_to_start[i]->getHandle()->Instance)));
175+
#endif
173176
}
174177
}
175178

0 commit comments

Comments
 (0)