Generating SPWM - code optimization
Hi,
I'm working on a project to generate a 3phase sinusoidal pwm.
F_fundemnetal = 0.1 - 100 Hz
F_sw = 1-100 kHz.
I would like to calculate the PWM values on the fly if possible instead of a lookup table. I am generating an ISR every time TIM1 overflows and am doing my calculations in there.
I'm using STM32F302R8.
IT works fine at 10 kHz, however I have been struggling to get it to work at 100 kHz with more than one phase calculation. I think there is not enough time for the calculations of the following code.
As far as I'm aware, the FPU should do each of these calculations in 14 clock cycles (running at 72 MHz) so that should be plenty of time to do it even at 100 kHz.
In keil I have enabled the floating point hardware to use single precision in the options for target 1 menu. Then when I am initializing the clocks, I should be enabling the FPU with the following command.
SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */At this point I'm guessing the FPU is doing the calculations but I am not sure on how to verify this.
Am I missing something? Is the uC/FPU not fast enough for these calculations? Can I somehow optimize the code to make it execute more quickly?
// interrupt occurs at switching frequency
void TIM1_UP_TIM16_IRQHandler(void)
{
TIM1->SR = ~TIM_SR_UIF; // Clear interrupt
if (PWM_index >= nsamples)
{
PWM_index = 1;
}
PWM_U = MaxPWM_value*sinf((6.28315/nsamples)*PWM_index);
PWM_V = MaxPWM_value*sinf((6.28315/nsamples)*PWM_index);
PWM_W = MaxPWM_value*sinf((6.28315/nsamples)*PWM_index);
if (PWM_U <= 0)
{
PWM_U = 0;
}
if (PWM_V <= 0)
{
PWM_V = 0;
}
if (PWM_W <= 0)
{
PWM_W = 0;
}
// //Duty Cycles
TIM1->CCR1 = PWM_U;
TIM1->CCR2 = PWM_V;
TIM1->CCR3 = PWM_W;
PWM_index++;
}