Hello @pierre-haessig
Your question is very interesting and has multiple elements to it.
Why it does not work
The frequency of the Twist and OwnVerter boards is currently set on the dts file:
powershield: power-shield{
compatible = "power-leg";
default-frequency = <200000>;
min-frequency = <50000>;
These values are automatically retrieved by the power_init.cpp file:
uint32_t timer_frequency = DT_PROP(POWER_SHIELD_ID, default_frequency);
uint32_t timer_min_frequency = DT_PROP(POWER_SHIELD_ID, min_frequency);
These variables are used in turn in the PowerAPI::initMode function:
/* Configure PWM frequency */
spin.pwm.initVariableFrequency(timer_frequency, timer_min_frequency);
which is called at the setup_routine()function by:
shield.power.initBuck(ALL);
When we call the spin.pwm.initFixedFrequency(value)function, we update values within a structure of the spin that is found within the hrtim.c file:
void hrtim_frequency_set(uint32_t frequency_set, uint32_t frequency_min)
{
HRTIM_MINIM_FREQUENCY = frequency_min;
timerMaster.pwm_conf.frequency = frequency_set;
tu_channel[PWMA]->pwm_conf.frequency = frequency_set;
tu_channel[PWMB]->pwm_conf.frequency = frequency_set;
tu_channel[PWMC]->pwm_conf.frequency = frequency_set;
tu_channel[PWMD]->pwm_conf.frequency = frequency_set;
tu_channel[PWME]->pwm_conf.frequency = frequency_set;
tu_channel[PWMF]->pwm_conf.frequency = frequency_set;
timerMaster.pwm_conf.min_frequency = frequency_min;
tu_channel[PWMA]->pwm_conf.min_frequency = frequency_min;
tu_channel[PWMB]->pwm_conf.min_frequency = frequency_min;
tu_channel[PWMC]->pwm_conf.min_frequency = frequency_min;
tu_channel[PWMD]->pwm_conf.min_frequency = frequency_min;
tu_channel[PWME]->pwm_conf.min_frequency = frequency_min;
tu_channel[PWMF]->pwm_conf.min_frequency = frequency_min;
}
However, this change is indeed overwritten by the values that are present in the device tree of the twist board.
Thus, if I write on the setup_routine() of the voltage mode buck example:
void setup_routine()
{
/* Buck voltage mode */
spin.pwm.initFixedFrequency(50000); :
shield.power.initBuck(ALL);
shield.sensors.enableDefaultTwistSensors();
pid.init(pid_params);
/* Then declare tasks */
uint32_t app_task_number = task.createBackground(loop_application_task);
uint32_t com_task_number = task.createBackground(loop_communication_task);
task.createCritical(loop_critical_task, 100);
/* Finally, start tasks */
task.startBackground(app_task_number);
task.startBackground(com_task_number);
task.startCritical();
}
I get a frequency of 200kHz regardless:
How to get it working (variable frequency)
However, if you change your communication function to:
void loop_communication_task()
{
received_serial_char = console_getchar();
switch (received_serial_char)
{
case 'h':
/*----------SERIAL INTERFACE MENU----------------------- */
printk(" ________________________________________ \n"
"| ---- MENU buck voltage mode ---- |\n"
"| press i : idle mode |\n"
"| press p : power mode |\n"
"| press u : voltage reference UP |\n"
"| press d : voltage reference DOWN |\n"
"|________________________________________|\n\n");
/*------------------------------------------------------ */
break;
case 'i':
printk("idle mode\n");
mode = IDLEMODE;
break;
case 'p':
printk("power mode\n");
mode = POWERMODE;
break;
case 'u':
frequency += 500;
spin.pwm.setFrequency(frequency);
break;
case 'd':
frequency -= 500;
spin.pwm.setFrequency(frequency);
break;
default:
break;
}
}
You will indeed vary your frequency natively between 200kHz and 50kHz, as it is 144kHzbelow.
So, as a conclusion:
- The variable frequency is supported by default.
- For now, it is defined on the device tree of your power shield
- If you wish to make a different frequency, you will have to change the device tree (as of now)
- If you want us to change this and create a function that overwrites the frequency, please create an issue on the Core and we’ll take care of it.