1
1
#include " ../hardware_api.h"
2
2
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) )
4
4
5
5
#include " driver/ledc.h"
6
6
25
25
#define LEDC_CHANNELS (SOC_LEDC_CHANNEL_NUM)
26
26
#endif
27
27
28
+
28
29
// current channel stack index
29
30
// support for multiple motors
30
31
// esp32 has 16 channels
31
32
// esp32s2 has 8 channels
32
33
// esp32c3 has 6 channels
33
34
int channel_index = 0 ;
34
35
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
+
79
47
80
48
// configure High PWM frequency
81
49
void _setHighFrequency (const long freq, const int pin, const int channel){
82
50
ledcSetup (channel, freq, _PWM_RES_BIT );
83
51
ledcAttachPin (pin, channel);
84
52
}
85
53
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) {
87
57
if (!pwm_frequency || !_isset (pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY; // default frequency 25khz
88
58
else pwm_frequency = _constrain (pwm_frequency, 0 , _PWM_FREQUENCY_MAX); // constrain to 50kHz max
89
59
90
60
// 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;
109
73
}
110
74
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) {
112
78
if (!pwm_frequency || !_isset (pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY; // default frequency 25khz
113
79
else pwm_frequency = _constrain (pwm_frequency, 0 , _PWM_FREQUENCY_MAX); // constrain to 50kHz max
114
80
115
81
// 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;
136
96
}
137
97
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) {
139
101
if (!pwm_frequency || !_isset (pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY; // default frequency 25khz
140
102
else pwm_frequency = _constrain (pwm_frequency, 0 , _PWM_FREQUENCY_MAX); // constrain to 50kHz max
141
103
142
104
// 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;
166
121
}
167
122
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));
177
129
}
178
130
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));
189
137
}
190
138
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));
202
146
}
203
147
204
148
#endif
0 commit comments