Skip to content

Commit e433f95

Browse files
author
Richard Unger
committed
refactor and simplify LEDC driver (untested)
1 parent d243cbf commit e433f95

File tree

1 file changed

+83
-139
lines changed

1 file changed

+83
-139
lines changed
Lines changed: 83 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "../hardware_api.h"
22

3-
#if defined(ESP_H) && defined(ARDUINO_ARCH_ESP32) && !defined(SOC_MCPWM_SUPPORTED)
3+
#if defined(ESP_H) && defined(ARDUINO_ARCH_ESP32) && ( !defined(SOC_MCPWM_SUPPORTED) || defined(SIMPLEFOC_ESP32_USELEDC) )
44

55
#include "driver/ledc.h"
66

@@ -25,180 +25,124 @@
2525
#define LEDC_CHANNELS (SOC_LEDC_CHANNEL_NUM)
2626
#endif
2727

28+
2829
// current channel stack index
2930
// support for multiple motors
3031
// esp32 has 16 channels
3132
// esp32s2 has 8 channels
3233
// esp32c3 has 6 channels
3334
int channel_index = 0;
3435

35-
// slot for the 3pwm bldc motors
36-
typedef struct {
37-
int pinID;
38-
int ch1;
39-
int ch2;
40-
int ch3;
41-
} bldc_3pwm_motor_slots_t;
42-
43-
// slot for the 2pwm stepper motors
44-
typedef struct {
45-
int pinID;
46-
int ch1;
47-
int ch2;
48-
} stepper_2pwm_motor_slots_t;
49-
50-
// slot for the 4pwm stepper motors
51-
typedef struct {
52-
int pinID;
53-
int ch1;
54-
int ch2;
55-
int ch3;
56-
int ch4;
57-
} stepper_4pwm_motor_slots_t;
58-
59-
60-
// define motor slots array
61-
bldc_3pwm_motor_slots_t esp32_bldc_3pwm_motor_slots[4] = {
62-
{_EMPTY_SLOT, 0,0,0}, // 1st motor
63-
{_EMPTY_SLOT, 0,0,0}, // 2nd motor
64-
{_EMPTY_SLOT, 0,0,0}, // 3st motor // esp32s2 & esp32
65-
{_EMPTY_SLOT, 0,0,0}, // 4nd motor // esp32 only
66-
};
67-
stepper_2pwm_motor_slots_t esp32_stepper_2pwm_motor_slots[4]={
68-
{_EMPTY_SLOT, 0,0}, // 1st motor
69-
{_EMPTY_SLOT, 0,0}, // 2nd motor
70-
{_EMPTY_SLOT, 0,0}, // 3rd motor
71-
{_EMPTY_SLOT, 0,0} // 4th motor - esp32s2 and esp32
72-
};
73-
stepper_4pwm_motor_slots_t esp32_stepper_4pwm_motor_slots[4]={
74-
{_EMPTY_SLOT, 0,0,0,0}, // 1st motor
75-
{_EMPTY_SLOT, 0,0,0,0}, // 2nd motor - esp32s2 and esp32
76-
{_EMPTY_SLOT, 0,0,0,0}, // 3st motor - only esp32
77-
{_EMPTY_SLOT, 0,0,0,0}, // 4st motor - only esp32
78-
};
36+
37+
38+
39+
typedef struct ESP32LEDCDriverParams {
40+
int channels[6];
41+
long pwm_frequency;
42+
} ESP32LEDCDriverParams;
43+
44+
45+
46+
7947

8048
// configure High PWM frequency
8149
void _setHighFrequency(const long freq, const int pin, const int channel){
8250
ledcSetup(channel, freq, _PWM_RES_BIT );
8351
ledcAttachPin(pin, channel);
8452
}
8553

86-
void _configure2PWM(long pwm_frequency, const int pinA, const int pinB) {
54+
55+
56+
void* _configure2PWM(long pwm_frequency, const int pinA, const int pinB) {
8757
if(!pwm_frequency || !_isset(pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY; // default frequency 25khz
8858
else pwm_frequency = _constrain(pwm_frequency, 0, _PWM_FREQUENCY_MAX); // constrain to 50kHz max
8959

9060
// check if enough channels available
91-
if ( channel_index + 2 >= LEDC_CHANNELS ) return;
92-
93-
stepper_2pwm_motor_slots_t m_slot = {};
94-
95-
// determine which motor are we connecting
96-
// and set the appropriate configuration parameters
97-
for(int slot_num = 0; slot_num < 4; slot_num++){
98-
if(esp32_stepper_2pwm_motor_slots[slot_num].pinID == _EMPTY_SLOT){ // put the new motor in the first empty slot
99-
esp32_stepper_2pwm_motor_slots[slot_num].pinID = pinA;
100-
esp32_stepper_2pwm_motor_slots[slot_num].ch1 = channel_index++;
101-
esp32_stepper_2pwm_motor_slots[slot_num].ch2 = channel_index++;
102-
m_slot = esp32_stepper_2pwm_motor_slots[slot_num];
103-
break;
104-
}
105-
}
106-
107-
_setHighFrequency(pwm_frequency, pinA, m_slot.ch1);
108-
_setHighFrequency(pwm_frequency, pinB, m_slot.ch2);
61+
if ( channel_index + 2 >= LEDC_CHANNELS ) return SIMPLEFOC_DRIVER_INIT_FAILED;
62+
63+
int ch1 = channel_index++;
64+
int ch2 = channel_index++;
65+
_setHighFrequency(pwm_frequency, pinA, ch1);
66+
_setHighFrequency(pwm_frequency, pinB, ch2);
67+
68+
ESP32LEDCDriverParams* params = new ESP32LEDCDriverParams {
69+
.channels = { ch1, ch2 },
70+
.pwm_frequency = pwm_frequency
71+
};
72+
return params;
10973
}
11074

111-
void _configure3PWM(long pwm_frequency,const int pinA, const int pinB, const int pinC) {
75+
76+
77+
void* _configure3PWM(long pwm_frequency,const int pinA, const int pinB, const int pinC) {
11278
if(!pwm_frequency || !_isset(pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY; // default frequency 25khz
11379
else pwm_frequency = _constrain(pwm_frequency, 0, _PWM_FREQUENCY_MAX); // constrain to 50kHz max
11480

11581
// check if enough channels available
116-
if ( channel_index + 3 >= LEDC_CHANNELS ) return;
117-
118-
bldc_3pwm_motor_slots_t m_slot = {};
119-
120-
// determine which motor are we connecting
121-
// and set the appropriate configuration parameters
122-
for(int slot_num = 0; slot_num < 4; slot_num++){
123-
if(esp32_bldc_3pwm_motor_slots[slot_num].pinID == _EMPTY_SLOT){ // put the new motor in the first empty slot
124-
esp32_bldc_3pwm_motor_slots[slot_num].pinID = pinA;
125-
esp32_bldc_3pwm_motor_slots[slot_num].ch1 = channel_index++;
126-
esp32_bldc_3pwm_motor_slots[slot_num].ch2 = channel_index++;
127-
esp32_bldc_3pwm_motor_slots[slot_num].ch3 = channel_index++;
128-
m_slot = esp32_bldc_3pwm_motor_slots[slot_num];
129-
break;
130-
}
131-
}
132-
133-
_setHighFrequency(pwm_frequency, pinA, m_slot.ch1);
134-
_setHighFrequency(pwm_frequency, pinB, m_slot.ch2);
135-
_setHighFrequency(pwm_frequency, pinC, m_slot.ch3);
82+
if ( channel_index + 3 >= LEDC_CHANNELS ) return SIMPLEFOC_DRIVER_INIT_FAILED;
83+
84+
int ch1 = channel_index++;
85+
int ch2 = channel_index++;
86+
int ch3 = channel_index++;
87+
_setHighFrequency(pwm_frequency, pinA, ch1);
88+
_setHighFrequency(pwm_frequency, pinB, ch2);
89+
_setHighFrequency(pwm_frequency, pinC, ch3);
90+
91+
ESP32LEDCDriverParams* params = new ESP32LEDCDriverParams {
92+
.channels = { ch1, ch2, ch3 },
93+
.pwm_frequency = pwm_frequency
94+
};
95+
return params;
13696
}
13797

138-
void _configure4PWM(long pwm_frequency,const int pinA, const int pinB, const int pinC, const int pinD) {
98+
99+
100+
void* _configure4PWM(long pwm_frequency,const int pinA, const int pinB, const int pinC, const int pinD) {
139101
if(!pwm_frequency || !_isset(pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY; // default frequency 25khz
140102
else pwm_frequency = _constrain(pwm_frequency, 0, _PWM_FREQUENCY_MAX); // constrain to 50kHz max
141103

142104
// check if enough channels available
143-
if ( channel_index + 4 >= LEDC_CHANNELS ) return;
144-
145-
146-
stepper_4pwm_motor_slots_t m_slot = {};
147-
148-
// determine which motor are we connecting
149-
// and set the appropriate configuration parameters
150-
for(int slot_num = 0; slot_num < 4; slot_num++){
151-
if(esp32_stepper_4pwm_motor_slots[slot_num].pinID == _EMPTY_SLOT){ // put the new motor in the first empty slot
152-
esp32_stepper_4pwm_motor_slots[slot_num].pinID = pinA;
153-
esp32_stepper_4pwm_motor_slots[slot_num].ch1 = channel_index++;
154-
esp32_stepper_4pwm_motor_slots[slot_num].ch2 = channel_index++;
155-
esp32_stepper_4pwm_motor_slots[slot_num].ch3 = channel_index++;
156-
esp32_stepper_4pwm_motor_slots[slot_num].ch4 = channel_index++;
157-
m_slot = esp32_stepper_4pwm_motor_slots[slot_num];
158-
break;
159-
}
160-
}
161-
162-
_setHighFrequency(pwm_frequency, pinA, m_slot.ch1);
163-
_setHighFrequency(pwm_frequency, pinB, m_slot.ch2);
164-
_setHighFrequency(pwm_frequency, pinC, m_slot.ch3);
165-
_setHighFrequency(pwm_frequency, pinD, m_slot.ch4);
105+
if ( channel_index + 4 >= LEDC_CHANNELS ) return SIMPLEFOC_DRIVER_INIT_FAILED;
106+
107+
int ch1 = channel_index++;
108+
int ch2 = channel_index++;
109+
int ch3 = channel_index++;
110+
int ch4 = channel_index++;
111+
_setHighFrequency(pwm_frequency, pinA, ch1);
112+
_setHighFrequency(pwm_frequency, pinB, ch2);
113+
_setHighFrequency(pwm_frequency, pinC, ch3);
114+
_setHighFrequency(pwm_frequency, pinD, ch4);
115+
116+
ESP32LEDCDriverParams* params = new ESP32LEDCDriverParams {
117+
.channels = { ch1, ch2, ch3, ch4 },
118+
.pwm_frequency = pwm_frequency
119+
};
120+
return params;
166121
}
167122

168-
void _writeDutyCycle2PWM(float dc_a, float dc_b, int pinA, int pinB){
169-
// determine which motor slot is the motor connected to
170-
for(int i = 0; i < 4; i++){
171-
if(esp32_stepper_2pwm_motor_slots[i].pinID == pinA){ // if motor slot found
172-
ledcWrite(esp32_stepper_2pwm_motor_slots[i].ch1, _constrain(_PWM_RES*dc_a, 0, _PWM_RES));
173-
ledcWrite(esp32_stepper_2pwm_motor_slots[i].ch2, _constrain(_PWM_RES*dc_b, 0, _PWM_RES));
174-
break;
175-
}
176-
}
123+
124+
125+
126+
void _writeDutyCycle2PWM(float dc_a, float dc_b, void* params){
127+
ledcWrite(((ESP32LEDCDriverParams*)params)->channels[0], _constrain(_PWM_RES*dc_a, 0, _PWM_RES));
128+
ledcWrite(((ESP32LEDCDriverParams*)params)->channels[1], _constrain(_PWM_RES*dc_b, 0, _PWM_RES));
177129
}
178130

179-
void _writeDutyCycle3PWM(float dc_a, float dc_b, float dc_c, int pinA, int pinB, int pinC){
180-
// determine which motor slot is the motor connected to
181-
for(int i = 0; i < 4; i++){
182-
if(esp32_bldc_3pwm_motor_slots[i].pinID == pinA){ // if motor slot found
183-
ledcWrite(esp32_bldc_3pwm_motor_slots[i].ch1, _constrain(_PWM_RES*dc_a, 0, _PWM_RES));
184-
ledcWrite(esp32_bldc_3pwm_motor_slots[i].ch2, _constrain(_PWM_RES*dc_b, 0, _PWM_RES));
185-
ledcWrite(esp32_bldc_3pwm_motor_slots[i].ch3, _constrain(_PWM_RES*dc_c, 0, _PWM_RES));
186-
break;
187-
}
188-
}
131+
132+
133+
void _writeDutyCycle3PWM(float dc_a, float dc_b, float dc_c, void* params){
134+
ledcWrite(((ESP32LEDCDriverParams*)params)->channels[0], _constrain(_PWM_RES*dc_a, 0, _PWM_RES));
135+
ledcWrite(((ESP32LEDCDriverParams*)params)->channels[1], _constrain(_PWM_RES*dc_b, 0, _PWM_RES));
136+
ledcWrite(((ESP32LEDCDriverParams*)params)->channels[2], _constrain(_PWM_RES*dc_c, 0, _PWM_RES));
189137
}
190138

191-
void _writeDutyCycle4PWM(float dc_1a, float dc_1b, float dc_2a, float dc_2b, int pin1A, int pin1B, int pin2A, int pin2B){
192-
// determine which motor slot is the motor connected to
193-
for(int i = 0; i < 4; i++){
194-
if(esp32_stepper_4pwm_motor_slots[i].pinID == pin1A){ // if motor slot found
195-
ledcWrite(esp32_stepper_4pwm_motor_slots[i].ch1, _constrain(_PWM_RES*dc_1a, 0, _PWM_RES));
196-
ledcWrite(esp32_stepper_4pwm_motor_slots[i].ch2, _constrain(_PWM_RES*dc_1b, 0, _PWM_RES));
197-
ledcWrite(esp32_stepper_4pwm_motor_slots[i].ch3, _constrain(_PWM_RES*dc_2a, 0, _PWM_RES));
198-
ledcWrite(esp32_stepper_4pwm_motor_slots[i].ch4, _constrain(_PWM_RES*dc_2b, 0, _PWM_RES));
199-
break;
200-
}
201-
}
139+
140+
141+
void _writeDutyCycle4PWM(float dc_1a, float dc_1b, float dc_2a, float dc_2b, void* params){
142+
ledcWrite(((ESP32LEDCDriverParams*)params)->channels[0], _constrain(_PWM_RES*dc_1a, 0, _PWM_RES));
143+
ledcWrite(((ESP32LEDCDriverParams*)params)->channels[1], _constrain(_PWM_RES*dc_1b, 0, _PWM_RES));
144+
ledcWrite(((ESP32LEDCDriverParams*)params)->channels[2], _constrain(_PWM_RES*dc_2a, 0, _PWM_RES));
145+
ledcWrite(((ESP32LEDCDriverParams*)params)->channels[3], _constrain(_PWM_RES*dc_2b, 0, _PWM_RES));
202146
}
203147

204148
#endif

0 commit comments

Comments
 (0)