Skip to content

Commit b2773d8

Browse files
author
Richard Unger
committed
due driver refactor (but untested)
1 parent a2344ef commit b2773d8

File tree

1 file changed

+52
-15
lines changed

1 file changed

+52
-15
lines changed

src/drivers/hardware_specific/due_mcu.cpp

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,17 @@ void TC8_Handler()
320320
if(pwm_counter_vals[17]) TC_SetRB(TC2, 2, pwm_counter_vals[17]);
321321
}
322322

323+
324+
325+
326+
323327
// implementation of the hardware_api.cpp
324328
// ---------------------------------------------------------------------------------------------------------------------------------
325329

326330
// function setting the high pwm frequency to the supplied pins
327331
// - BLDC motor - 3PWM setting
328332
// - hardware specific
329-
void _configure3PWM(long pwm_frequency,const int pinA, const int pinB, const int pinC) {
333+
void* _configure3PWM(long pwm_frequency,const int pinA, const int pinB, const int pinC) {
330334
if(!pwm_frequency || !_isset(pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY; // default frequency 50khz
331335
else pwm_frequency = _constrain(pwm_frequency, 0, _PWM_FREQUENCY_MAX); // constrain to 50kHz max
332336
// save the pwm frequency
@@ -337,13 +341,21 @@ void _configure3PWM(long pwm_frequency,const int pinA, const int pinB, const int
337341
initPWM(pinC, _pwm_frequency);
338342
// sync the timers if possible
339343
syncTimers(pinA, pinB, pinC);
344+
345+
GenericDriverParams* params = new GenericDriverParams {
346+
.pins = { pinA, pinB, pinC },
347+
.pwm_frequency = pwm_frequency
348+
};
349+
return params;
340350
}
341351

342352

353+
354+
343355
// Configuring PWM frequency, resolution and alignment
344356
//- Stepper driver - 2PWM setting
345357
// - hardware specific
346-
void _configure2PWM(long pwm_frequency, const int pinA, const int pinB) {
358+
void* _configure2PWM(long pwm_frequency, const int pinA, const int pinB) {
347359
if(!pwm_frequency || !_isset(pwm_frequency)) pwm_frequency = _PWM_FREQUENCY; // default frequency 50khz
348360
else pwm_frequency = _constrain(pwm_frequency, 0, _PWM_FREQUENCY_MAX); // constrain to 50kHz max
349361
// save the pwm frequency
@@ -353,12 +365,21 @@ void _configure2PWM(long pwm_frequency, const int pinA, const int pinB) {
353365
initPWM(pinB, _pwm_frequency);
354366
// sync the timers if possible
355367
syncTimers(pinA, pinB);
368+
369+
GenericDriverParams* params = new GenericDriverParams {
370+
.pins = { pinA, pinB },
371+
.pwm_frequency = pwm_frequency
372+
};
373+
return params;
356374
}
357375

376+
377+
378+
358379
// function setting the high pwm frequency to the supplied pins
359380
// - Stepper motor - 4PWM setting
360381
// - hardware speciffic
361-
void _configure4PWM(long pwm_frequency,const int pinA, const int pinB, const int pinC, const int pinD) {
382+
void* _configure4PWM(long pwm_frequency,const int pinA, const int pinB, const int pinC, const int pinD) {
362383
if(!pwm_frequency || !_isset(pwm_frequency)) pwm_frequency = _PWM_FREQUENCY; // default frequency 50khz
363384
else pwm_frequency = _constrain(pwm_frequency, 0, _PWM_FREQUENCY_MAX); // constrain to 50kHz max
364385
// save the pwm frequency
@@ -370,36 +391,52 @@ void _configure4PWM(long pwm_frequency,const int pinA, const int pinB, const int
370391
initPWM(pinD, _pwm_frequency);
371392
// sync the timers if possible
372393
syncTimers(pinA, pinB, pinC, pinD);
394+
395+
GenericDriverParams* params = new GenericDriverParams {
396+
.pins = { pinA, pinB, pinC, pinD },
397+
.pwm_frequency = pwm_frequency
398+
};
399+
return params;
373400
}
374401

402+
403+
404+
375405
// function setting the pwm duty cycle to the hardware
376406
// - BLDC motor - 3PWM setting
377407
// - hardware speciffic
378-
void _writeDutyCycle3PWM(float dc_a, float dc_b, float dc_c, int pinA, int pinB, int pinC){
408+
void _writeDutyCycle3PWM(float dc_a, float dc_b, float dc_c, void* param){
379409
// transform duty cycle from [0,1] to [0,_max_pwm_value]
380-
setPwm(pinA, _max_pwm_value*dc_a);
381-
setPwm(pinB, _max_pwm_value*dc_b);
382-
setPwm(pinC, _max_pwm_value*dc_c);
410+
GenericDriverParams* p = (GenericDriverParams*)param;
411+
setPwm(p->pins[0], _max_pwm_value*dc_a);
412+
setPwm(p->pins[1], _max_pwm_value*dc_b);
413+
setPwm(p->pins[2], _max_pwm_value*dc_c);
383414
}
384415

416+
417+
385418
// function setting the pwm duty cycle to the hardware
386419
// - Stepper motor - 4PWM setting
387420
// - hardware speciffic
388-
void _writeDutyCycle4PWM(float dc_1a, float dc_1b, float dc_2a, float dc_2b, int pin1A, int pin1B, int pin2A, int pin2B){
421+
void _writeDutyCycle4PWM(float dc_1a, float dc_1b, float dc_2a, float dc_2b, void* param){
389422
// transform duty cycle from [0,1] to [0,_max_pwm_value]
390-
setPwm(pin1A, _max_pwm_value*dc_1a);
391-
setPwm(pin1B, _max_pwm_value*dc_1b);
392-
setPwm(pin2A, _max_pwm_value*dc_2a);
393-
setPwm(pin2B, _max_pwm_value*dc_2b);
423+
GenericDriverParams* p = (GenericDriverParams*)param;
424+
setPwm(p->pins[0], _max_pwm_value*dc_1a);
425+
setPwm(p->pins[1], _max_pwm_value*dc_1b);
426+
setPwm(p->pins[2], _max_pwm_value*dc_2a);
427+
setPwm(p->pins[3], _max_pwm_value*dc_2b);
394428
}
395429

430+
431+
396432
// Function setting the duty cycle to the pwm pin (ex. analogWrite())
397433
// - Stepper driver - 2PWM setting
398434
// - hardware specific
399-
void _writeDutyCycle2PWM(float dc_a, float dc_b, int pinA, int pinB){
435+
void _writeDutyCycle2PWM(float dc_a, float dc_b, void* param){
400436
// transform duty cycle from [0,1] to [0,_max_pwm_value]
401-
setPwm(pinA, _max_pwm_value*dc_a);
402-
setPwm(pinB, _max_pwm_value*dc_b);
437+
GenericDriverParams* p = (GenericDriverParams*)param;
438+
setPwm(p->pins[0], _max_pwm_value*dc_a);
439+
setPwm(p->pins[1], _max_pwm_value*dc_b);
403440
}
404441

405442

0 commit comments

Comments
 (0)