Skip to content

Commit b9257bc

Browse files
author
Richard Unger
committed
nrf52 driver refactored (but untested)
1 parent e433f95 commit b9257bc

File tree

1 file changed

+82
-56
lines changed

1 file changed

+82
-56
lines changed

src/drivers/hardware_specific/nrf52_mcu.cpp

Lines changed: 82 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#define _TAKEN_SLOT (-0x55)
2323

2424
int pwm_range;
25-
float dead_time;
25+
2626

2727
static NRF_PWM_Type* pwms[PWM_COUNT] = {
2828
NRF_PWM0,
@@ -72,7 +72,22 @@ stepper_motor_slots_t nrf52_stepper_motor_slots[4] = {
7272
bldc_6pwm_motor_slots_t nrf52_bldc_6pwm_motor_slots[2] = {
7373
{_EMPTY_SLOT, pwms[0], pwms[1], {0,0,0,0,0,0,0,0}},// 1st motor will be on PWM0 & PWM1
7474
{_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+
7691

7792
// configuring high frequency pwm timer
7893
void _configureHwPwm(NRF_PWM_Type* mcpwm1, NRF_PWM_Type* mcpwm2){
@@ -103,7 +118,7 @@ void _configureHwPwm(NRF_PWM_Type* mcpwm1, NRF_PWM_Type* mcpwm2){
103118
// function setting the high pwm frequency to the supplied pins
104119
// - BLDC motor - 3PWM setting
105120
// - 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) {
107122

108123
if( !pwm_frequency || pwm_frequency == NOT_SET) pwm_frequency = PWM_FREQ; // default frequency 20khz for a resolution of 800
109124
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
123138
break;
124139
}
125140
}
141+
// if no slots available
142+
if(slot_num >= 4) return SIMPLEFOC_DRIVER_INIT_FAILED;
143+
126144
// disable all the slots with the same MCPWM
127145
if(slot_num < 2){
128146
// slot 0 of the stepper
@@ -149,12 +167,20 @@ void _configure3PWM(long pwm_frequency,const int pinA, const int pinB, const int
149167

150168
// configure the pwm
151169
_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;
152175
}
153176

177+
178+
179+
154180
// function setting the high pwm frequency to the supplied pins
155181
// - Stepper motor - 4PWM setting
156182
// - 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) {
158184

159185
if( !pwm_frequency || pwm_frequency == NOT_SET) pwm_frequency = PWM_FREQ; // default frequency 20khz for a resolution of 800
160186
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
175201
break;
176202
}
177203
}
204+
// if no slots available
205+
if (slot_num >= 4) return SIMPLEFOC_DRIVER_INIT_FAILED;
206+
178207
// disable all the slots with the same MCPWM
179-
if( slot_num < 2 ){
208+
if (slot_num < 2){
180209
// slots 0 and 1 of the 3pwm bldc
181210
nrf52_bldc_3pwm_motor_slots[slot_num].pinA = _TAKEN_SLOT;
182211
// slot 0 of the 6pwm bldc
@@ -202,61 +231,59 @@ void _configure4PWM(long pwm_frequency,const int pinA, const int pinB, const int
202231

203232
// configure the pwm
204233
_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;
205239
}
206240

241+
242+
243+
207244
// function setting the pwm duty cycle to the hardware
208245
// - BLDC motor - 3PWM setting
209246
// - 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;
225255
}
226256

257+
258+
259+
227260
// function setting the pwm duty cycle to the hardware
228261
// - Stepper motor - 4PWM setting
229262
// - 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;
246272
}
247273

248274
/* Configuring PWM frequency, resolution and alignment
249275
// - BLDC driver - 6PWM setting
250276
// - hardware specific
251277
*/
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){
253279

254280
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
255281
else pwm_frequency = _constrain(pwm_frequency*2, 0, PWM_MAX_FREQ); // constrain to 62.5kHz max => 31.25kHz for a resolution of 256
256282

257283
pwm_range = (PWM_CLK / pwm_frequency);
258284
pwm_range /= 2; // scale the frequency (centered PWM)
259285

286+
float dead_time;
260287
if (dead_zone != NOT_SET){
261288
dead_time = dead_zone/2;
262289
}else{
@@ -281,7 +308,7 @@ int _configure6PWM(long pwm_frequency, float dead_zone, const int pinA_h, const
281308
}
282309
}
283310
// if no slots available
284-
if(slot_num >= 2) return -1;
311+
if(slot_num >= 2) return SIMPLEFOC_DRIVER_INIT_FAILED;
285312

286313
// disable all the slots with the same MCPWM
287314
if( slot_num == 0 ){
@@ -324,31 +351,30 @@ int _configure6PWM(long pwm_frequency, float dead_zone, const int pinA_h, const
324351
// configure the pwm type
325352
_configureHwPwm(nrf52_bldc_6pwm_motor_slots[slot_num].mcpwm1, nrf52_bldc_6pwm_motor_slots[slot_num].mcpwm2);
326353

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;
329359
}
330360

361+
362+
363+
331364
/* Function setting the duty cycle to the pwm pin
332365
// - BLDC driver - 6PWM setting
333366
// - hardware specific
334367
*/
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);
348377
NRF_EGU0->TASKS_TRIGGER[0] = 1;
349-
break;
350-
}
351-
}
352378
}
353379

354380

0 commit comments

Comments
 (0)