22
22
#define _TAKEN_SLOT (-0x55 )
23
23
24
24
int pwm_range;
25
- float dead_time;
25
+
26
26
27
27
static NRF_PWM_Type* pwms[PWM_COUNT] = {
28
28
NRF_PWM0,
@@ -72,7 +72,22 @@ stepper_motor_slots_t nrf52_stepper_motor_slots[4] = {
72
72
bldc_6pwm_motor_slots_t nrf52_bldc_6pwm_motor_slots[2 ] = {
73
73
{_EMPTY_SLOT, pwms[0 ], pwms[1 ], {0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 }},// 1st motor will be on PWM0 & PWM1
74
74
{_EMPTY_SLOT, pwms[2 ], pwms[3 ], {0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 }} // 2nd motor will be on PWM1 & PWM2
75
- };
75
+ };
76
+
77
+
78
+
79
+ typedef struct NRF52DriverParams {
80
+ union {
81
+ bldc_3pwm_motor_slots_t * slot3pwm;
82
+ bldc_6pwm_motor_slots_t * slot6pwm;
83
+ stepper_motor_slots_t * slotstep;
84
+ } slot;
85
+ long pwm_frequency;
86
+ float dead_time;
87
+ } NRF52DriverParams;
88
+
89
+
90
+
76
91
77
92
// configuring high frequency pwm timer
78
93
void _configureHwPwm (NRF_PWM_Type* mcpwm1, NRF_PWM_Type* mcpwm2){
@@ -103,7 +118,7 @@ void _configureHwPwm(NRF_PWM_Type* mcpwm1, NRF_PWM_Type* mcpwm2){
103
118
// function setting the high pwm frequency to the supplied pins
104
119
// - BLDC motor - 3PWM setting
105
120
// - hardware speciffic
106
- void _configure3PWM (long pwm_frequency,const int pinA, const int pinB, const int pinC) {
121
+ void * _configure3PWM (long pwm_frequency,const int pinA, const int pinB, const int pinC) {
107
122
108
123
if ( !pwm_frequency || pwm_frequency == NOT_SET) pwm_frequency = PWM_FREQ; // default frequency 20khz for a resolution of 800
109
124
else pwm_frequency = _constrain (pwm_frequency, 0 , PWM_MAX_FREQ); // constrain to 62.5kHz max for a resolution of 256
@@ -123,6 +138,9 @@ void _configure3PWM(long pwm_frequency,const int pinA, const int pinB, const int
123
138
break ;
124
139
}
125
140
}
141
+ // if no slots available
142
+ if (slot_num >= 4 ) return SIMPLEFOC_DRIVER_INIT_FAILED;
143
+
126
144
// disable all the slots with the same MCPWM
127
145
if (slot_num < 2 ){
128
146
// slot 0 of the stepper
@@ -149,12 +167,20 @@ void _configure3PWM(long pwm_frequency,const int pinA, const int pinB, const int
149
167
150
168
// configure the pwm
151
169
_configureHwPwm (nrf52_bldc_3pwm_motor_slots[slot_num].mcpwm , nrf52_bldc_3pwm_motor_slots[slot_num].mcpwm );
170
+
171
+ NRF52DriverParams* params = new NRF52DriverParams ();
172
+ params->slot .slot3pwm = &(nrf52_bldc_3pwm_motor_slots[slot_num]);
173
+ params->pwm_frequency = pwm_frequency;
174
+ return params;
152
175
}
153
176
177
+
178
+
179
+
154
180
// function setting the high pwm frequency to the supplied pins
155
181
// - Stepper motor - 4PWM setting
156
182
// - hardware speciffic
157
- void _configure4PWM (long pwm_frequency,const int pinA, const int pinB, const int pinC, const int pinD) {
183
+ void * _configure4PWM (long pwm_frequency, const int pinA, const int pinB, const int pinC, const int pinD) {
158
184
159
185
if ( !pwm_frequency || pwm_frequency == NOT_SET) pwm_frequency = PWM_FREQ; // default frequency 20khz for a resolution of 800
160
186
else pwm_frequency = _constrain (pwm_frequency, 0 , PWM_MAX_FREQ); // constrain to 62.5kHz max for a resolution of 256
@@ -175,8 +201,11 @@ void _configure4PWM(long pwm_frequency,const int pinA, const int pinB, const int
175
201
break ;
176
202
}
177
203
}
204
+ // if no slots available
205
+ if (slot_num >= 4 ) return SIMPLEFOC_DRIVER_INIT_FAILED;
206
+
178
207
// disable all the slots with the same MCPWM
179
- if ( slot_num < 2 ){
208
+ if ( slot_num < 2 ){
180
209
// slots 0 and 1 of the 3pwm bldc
181
210
nrf52_bldc_3pwm_motor_slots[slot_num].pinA = _TAKEN_SLOT;
182
211
// slot 0 of the 6pwm bldc
@@ -202,61 +231,59 @@ void _configure4PWM(long pwm_frequency,const int pinA, const int pinB, const int
202
231
203
232
// configure the pwm
204
233
_configureHwPwm (nrf52_stepper_motor_slots[slot_num].mcpwm , nrf52_stepper_motor_slots[slot_num].mcpwm );
234
+
235
+ NRF52DriverParams* params = new NRF52DriverParams ();
236
+ params->slot .slotstep = &(nrf52_stepper_motor_slots[slot_num]);
237
+ params->pwm_frequency = pwm_frequency;
238
+ return params;
205
239
}
206
240
241
+
242
+
243
+
207
244
// function setting the pwm duty cycle to the hardware
208
245
// - BLDC motor - 3PWM setting
209
246
// - hardware speciffic
210
- void _writeDutyCycle3PWM (float dc_a, float dc_b, float dc_c, int pinA, int pinB, int pinC){
211
- // determine which motor slot is the motor connected to
212
- for (int i = 0 ; i < 4 ; i++){
213
- if (nrf52_bldc_3pwm_motor_slots[i].pinA == pinA){ // if motor slot found
214
- // se the PWM on the slot timers
215
- // transform duty cycle from [0,1] to [0,range]
216
-
217
- nrf52_bldc_3pwm_motor_slots[i].mcpwm_channel_sequence [0 ] = (int )(dc_a * pwm_range) | 0x8000 ;
218
- nrf52_bldc_3pwm_motor_slots[i].mcpwm_channel_sequence [1 ] = (int )(dc_b * pwm_range) | 0x8000 ;
219
- nrf52_bldc_3pwm_motor_slots[i].mcpwm_channel_sequence [2 ] = (int )(dc_c * pwm_range) | 0x8000 ;
220
-
221
- nrf52_bldc_3pwm_motor_slots[i].mcpwm ->TASKS_SEQSTART [0 ] = 1 ;
222
- break ;
223
- }
224
- }
247
+ void _writeDutyCycle3PWM (float dc_a, float dc_b, float dc_c, void * params){
248
+ // transform duty cycle from [0,1] to [0,range]
249
+ bldc_3pwm_motor_slots_t * p = ((NRF52DriverParams*)params)->slot .slot3pwm ;
250
+ p->mcpwm_channel_sequence [0 ] = (int )(dc_a * pwm_range) | 0x8000 ;
251
+ p->mcpwm_channel_sequence [1 ] = (int )(dc_b * pwm_range) | 0x8000 ;
252
+ p->mcpwm_channel_sequence [2 ] = (int )(dc_c * pwm_range) | 0x8000 ;
253
+
254
+ p->mcpwm ->TASKS_SEQSTART [0 ] = 1 ;
225
255
}
226
256
257
+
258
+
259
+
227
260
// function setting the pwm duty cycle to the hardware
228
261
// - Stepper motor - 4PWM setting
229
262
// - hardware speciffic
230
- void _writeDutyCycle4PWM (float dc_1a, float dc_1b, float dc_2a, float dc_2b, int pin1A){
231
- // determine which motor slot is the motor connected to
232
- for (int i = 0 ; i < 4 ; i++){
233
- if (nrf52_stepper_motor_slots[i].pin1A == pin1A){ // if motor slot found
234
- // se the PWM on the slot timers
235
- // transform duty cycle from [0,1] to [0,range]
236
-
237
- nrf52_stepper_motor_slots[i].mcpwm_channel_sequence [0 ] = (int )(dc_1a * pwm_range) | 0x8000 ;
238
- nrf52_stepper_motor_slots[i].mcpwm_channel_sequence [1 ] = (int )(dc_1b * pwm_range) | 0x8000 ;
239
- nrf52_stepper_motor_slots[i].mcpwm_channel_sequence [2 ] = (int )(dc_2a * pwm_range) | 0x8000 ;
240
- nrf52_stepper_motor_slots[i].mcpwm_channel_sequence [3 ] = (int )(dc_2b * pwm_range) | 0x8000 ;
241
-
242
- nrf52_stepper_motor_slots[i].mcpwm ->TASKS_SEQSTART [0 ] = 1 ;
243
- break ;
244
- }
245
- }
263
+ void _writeDutyCycle4PWM (float dc_1a, float dc_1b, float dc_2a, float dc_2b, void * params){
264
+
265
+ stepper_motor_slots_t * p = ((NRF52DriverParams*)params)->slot .slotstep ;
266
+ p->mcpwm_channel_sequence [0 ] = (int )(dc_1a * pwm_range) | 0x8000 ;
267
+ p->mcpwm_channel_sequence [1 ] = (int )(dc_1b * pwm_range) | 0x8000 ;
268
+ p->mcpwm_channel_sequence [2 ] = (int )(dc_2a * pwm_range) | 0x8000 ;
269
+ p->mcpwm_channel_sequence [3 ] = (int )(dc_2b * pwm_range) | 0x8000 ;
270
+
271
+ p->mcpwm ->TASKS_SEQSTART [0 ] = 1 ;
246
272
}
247
273
248
274
/* Configuring PWM frequency, resolution and alignment
249
275
// - BLDC driver - 6PWM setting
250
276
// - hardware specific
251
277
*/
252
- int _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){
278
+ 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){
253
279
254
280
if ( !pwm_frequency || pwm_frequency == NOT_SET) pwm_frequency = PWM_FREQ; // default frequency 20khz - centered pwm has twice lower frequency for a resolution of 400
255
281
else pwm_frequency = _constrain (pwm_frequency*2 , 0 , PWM_MAX_FREQ); // constrain to 62.5kHz max => 31.25kHz for a resolution of 256
256
282
257
283
pwm_range = (PWM_CLK / pwm_frequency);
258
284
pwm_range /= 2 ; // scale the frequency (centered PWM)
259
285
286
+ float dead_time;
260
287
if (dead_zone != NOT_SET){
261
288
dead_time = dead_zone/2 ;
262
289
}else {
@@ -281,7 +308,7 @@ int _configure6PWM(long pwm_frequency, float dead_zone, const int pinA_h, const
281
308
}
282
309
}
283
310
// if no slots available
284
- if (slot_num >= 2 ) return - 1 ;
311
+ if (slot_num >= 2 ) return SIMPLEFOC_DRIVER_INIT_FAILED ;
285
312
286
313
// disable all the slots with the same MCPWM
287
314
if ( slot_num == 0 ){
@@ -324,31 +351,30 @@ int _configure6PWM(long pwm_frequency, float dead_zone, const int pinA_h, const
324
351
// configure the pwm type
325
352
_configureHwPwm (nrf52_bldc_6pwm_motor_slots[slot_num].mcpwm1 , nrf52_bldc_6pwm_motor_slots[slot_num].mcpwm2 );
326
353
327
- // return
328
- return 0 ;
354
+ NRF52DriverParams* params = new NRF52DriverParams ();
355
+ params->slot .slot6pwm = &(nrf52_bldc_6pwm_motor_slots[slot_num]);
356
+ params->pwm_frequency = pwm_frequency;
357
+ params->dead_time = dead_time;
358
+ return params;
329
359
}
330
360
361
+
362
+
363
+
331
364
/* Function setting the duty cycle to the pwm pin
332
365
// - BLDC driver - 6PWM setting
333
366
// - hardware specific
334
367
*/
335
- void _writeDutyCycle6PWM (float dc_a, float dc_b, float dc_c, float dead_zone, const int pinA_h, const int , const int , const int , const int , const int ){
336
- for (int i = 0 ; i < 2 ; i++){
337
- if (nrf52_bldc_6pwm_motor_slots[i].pinAH == pinA_h){ // if motor slot found
338
- // se the PWM on the slot timers
339
- // transform duty cycle from [0,1] to [0,range]
340
-
341
- nrf52_bldc_6pwm_motor_slots[i].mcpwm_channel_sequence [0 ] = (int )(_constrain (dc_a-dead_time,0 ,1 )*pwm_range) | 0x8000 ;
342
- nrf52_bldc_6pwm_motor_slots[i].mcpwm_channel_sequence [1 ] = (int )(_constrain (dc_a+dead_time,0 ,1 )*pwm_range);
343
- nrf52_bldc_6pwm_motor_slots[i].mcpwm_channel_sequence [2 ] = (int )(_constrain (dc_b-dead_time,0 ,1 )*pwm_range) | 0x8000 ;
344
- nrf52_bldc_6pwm_motor_slots[i].mcpwm_channel_sequence [3 ] = (int )(_constrain (dc_b+dead_time,0 ,1 )*pwm_range);
345
- nrf52_bldc_6pwm_motor_slots[i].mcpwm_channel_sequence [4 ] = (int )(_constrain (dc_c-dead_time,0 ,1 )*pwm_range) | 0x8000 ;
346
- nrf52_bldc_6pwm_motor_slots[i].mcpwm_channel_sequence [5 ] = (int )(_constrain (dc_c+dead_time,0 ,1 )*pwm_range);
347
-
368
+ void _writeDutyCycle6PWM (float dc_a, float dc_b, float dc_c, void * params){
369
+ bldc_6pwm_motor_slots_t * p = ((NRF52DriverParams*)params)->slot .slot6pwm ;
370
+ float dead_time = ((NRF52DriverParams*)params)->dead_time ;
371
+ p->mcpwm_channel_sequence [0 ] = (int )(_constrain (dc_a-dead_time,0 ,1 )*pwm_range) | 0x8000 ;
372
+ p->mcpwm_channel_sequence [1 ] = (int )(_constrain (dc_a+dead_time,0 ,1 )*pwm_range);
373
+ p->mcpwm_channel_sequence [2 ] = (int )(_constrain (dc_b-dead_time,0 ,1 )*pwm_range) | 0x8000 ;
374
+ p->mcpwm_channel_sequence [3 ] = (int )(_constrain (dc_b+dead_time,0 ,1 )*pwm_range);
375
+ p->mcpwm_channel_sequence [4 ] = (int )(_constrain (dc_c-dead_time,0 ,1 )*pwm_range) | 0x8000 ;
376
+ p->mcpwm_channel_sequence [5 ] = (int )(_constrain (dc_c+dead_time,0 ,1 )*pwm_range);
348
377
NRF_EGU0->TASKS_TRIGGER [0 ] = 1 ;
349
- break ;
350
- }
351
- }
352
378
}
353
379
354
380
0 commit comments