Code: Select all
float period = (myfloat)CH_FREQUENCY) / 200;
start = chTimeNow();
while (TRUE) {
for (int phaseIndex = 0; phaseIndex < state->phaseCount; phaseIndex++) {
systime_t timeToSwitch = (systime_t) ((iteration + state->switchTimes[phaseIndex]) * period);
chThdSleepUntil(start + timeToSwitch);
int value = state->waves.pinStates[phaseIndex];
setPinValue(outputPin, value);
}
iteration++;
}
Unless there is a better algorithm, this approach is as precise as big CH_FREQUENCY could be. With 168MHz stm32f4 I see that everything works with CH_FREQUENCY=200K but the board hangs with CH_FREQUENCY=500K (That's in Debug configuration with most of CH_DBG settings set to TRUE)
Looking at a logic analyzer I see that my 50% duty 200Hz software pulses are 2.49ms, 2.5ms or 2.51ms wide. All hardware PWM pulses are 2.5000x wide.
So, I guess my question is how much would things change with 3.0 tickless kernel and when is that happening?