Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set STM32 timer prescaler based on timer clock frequency #388

Merged
merged 2 commits into from Apr 19, 2024

Conversation

Copper280z
Copy link
Contributor

This commit adds a function which calculates the correct timer prescaler value based on the actual timer clock frequency and sets it. This is necessary because some stm32duino clock configs, specifically the nucleo-f746zg, set things up such that the timers have an input frequency that differs by 2x. Families other than F7 also allow for this, but don't seem to have the same problem with the default configs.
image

HardwareTimer->setOverflow(num, HERTZ_FORMAT) attempts to deal with this, but it does so by setting TIMx->ARR to a value that differs by 2x, instead of setting the prescaler. For some PWM frequencies this leads to an off-by-one error in ARR between the different timers(with the 2x factor taken into account). The values I observed with the aforementioned f746 were

TIM1->ARR = 1349 (2698)
TIM4->ARR = 2699

This new function finds the lowest timer frequency for all timers, then calculates the prescaler value for each timer that's required to make the counter frequency match. It also calculates the correct overflow value for this frequency and sets it with HT->setOverflow(num, TICK_FORMAT), which does not reset the prescaler value. This method does require that no further calls to setOverflow are made with HERTZ_FORMAT, otherwise the prescaler value will be overwritten.

Additionally this change also adds the _getPwmState function, in anticipation of merging HFI support, as well as adding the STM32F7 family to the list of defines in _getInternalSourceTrigger. The F7 family timer interconnections appear to mirror the F4 family.
pg713 of RM0385 rev8 (STM32F74xx and F75xx reference manual)
image
pg794
image

So far I've tested this on the F746 nucleo + SFOC shield and it works as expected. I haven't tested further on other targets.

@runger1101001
Copy link
Member

Hey, this may also explain this issue? #166

May I ask, is this tested in combination with Antun's new timer-synchronisation based on triggers?

@Copper280z
Copy link
Contributor Author

I think it might explain #166, at least based on a quick read. I'd need to check the various docs to confirm though. I'd expect this to resolve it, in any case, so long as HardwareTimer handles those timers correctly.

Yes, this is tested with Antun's timer sync code, which, to be clear, only syncs the start of the timers. It does not do any synchronization once they're running, that requires reset mode along with an update trigger, instead of an enable trigger. Using update/reset mode breaks current sensing in a difficult to fix way, at least without this change. I didn't test it with this change.

I ran into this whole trying to port current sensing and HFI to the F7, which strongly depend on having sync'd timers. I found that the effected timer was not sync'd even though it had nominally the same frequency on the scope.

@askuric
Copy link
Member

askuric commented Apr 19, 2024

Thanks for this. LGTM.

@askuric askuric merged commit 57ec89d into simplefoc:dev Apr 19, 2024
16 checks passed
@askuric
Copy link
Member

askuric commented Apr 19, 2024

Hi @Copper280z,
Did you maybe use f7 board with low-side current sensing or was it always inline?
I was wondering if you know is some of the architectures f4/l4/g4 has the same low-side ADC configuration as f7.

@askuric askuric added this to the 2.3.3_Release milestone Apr 19, 2024
@Copper280z Copper280z deleted the timer_prescaler branch April 20, 2024 14:12
@Copper280z
Copy link
Contributor Author

I used it with lowside current sensing.

#394 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants