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

Stm32 adc no interrupt #341

Merged
merged 2 commits into from
Dec 1, 2023
Merged

Conversation

Candas1
Copy link
Collaborator

@Candas1 Candas1 commented Nov 19, 2023

On stm32, when injected adc is used, the adc samples are saved in specific register.
The implementations for F1/F4/G4/L4 use an interrupt to save injected adc values in an array.
The way simplefoc works now, this is not required as those values are accessible anytime from the registers.

I introduced a SIMPLEFOC_STM32_ADC_INTERRUPT build flag.
By default, it will remove any code related to the adc interrupt.
When SIMPLEFOC_STM32_ADC_INTERRUPT is declared, code will work as before.
The reason why I kept this code as optional is that someone my want to use this interrupt for different reasons (e.g. running loopfoc, triggering an output for the scope).

I could test this on stm32f1 only, here are the results:
Without interrupt
RAM: [= ] 8.9% (used 4376 bytes from 49152 bytes)
Flash: [=== ] 26.7% (used 69916 bytes from 262144 bytes)
loopfoc = 270us

With interrupt
RAM: [= ] 9.0% (used 4428 bytes from 49152 bytes)
Flash: [=== ] 26.8% (used 70284 bytes from 262144 bytes)
loopfoc = 290us

It is slightly reducing the code size and reducing loopfoc duration as this interrupt was stopping the loopfoc execution several times unnecessarily.
Because of the priority of this interrupt, it might also delay the hall sensor interrupt.

Additionally this PR has the adc calibration for G4/L4 ( this was already merged for F1 and G431-ESC1).

@runger1101001
Copy link
Member

A 7% speed improvement as well!

I think in a few releases we can probably drop the interrupt code. I'm not aware that anyone is using it for anything...

@Candas1
Copy link
Collaborator Author

Candas1 commented Nov 19, 2023

If a chip can run loopfoc close to pwm frequency, it should have less impact.

@Candas1
Copy link
Collaborator Author

Candas1 commented Nov 20, 2023

Forgot that I am running the pwm at 'only' 16khz.
The higher the pwm frequency the more the adc interrupts would impact loopfoc

@Candas1
Copy link
Collaborator Author

Candas1 commented Nov 20, 2023

Loopfoc duration decreased from 360us to 240us on a STM32F1 if I compare the master branch and this PR 🚀​🚀​🚀​

@runger1101001
Copy link
Member

runger1101001 commented Nov 28, 2023

I think we'll make this part of the release following 2.3.2?

@runger1101001 runger1101001 added this to the 2.3.3_Release milestone Nov 28, 2023
@Candas1
Copy link
Collaborator Author

Candas1 commented Nov 28, 2023

It's OK. This is a small performance improvement for a particular use case (stm32 - low side current sensing).
This release already has a lot of improvements.

@runger1101001 runger1101001 merged commit cb822dc into simplefoc:dev Dec 1, 2023
16 checks passed
@Candas1
Copy link
Collaborator Author

Candas1 commented Dec 9, 2023

I received a SimpleFOC so I hope I can do more tests now on F4 and L4.

I was thinking about this change again and I have a small doubt.
When not using the interrupt, it can be we read the data from the injected adc register while it's being written.
So phase A could be a new sample, and phase B could be from the previous sample.
But I think this can also happen with DMA without interrupt for the B431-ESC1 current sense driver.

I need to do more tests to check if it happens and what is the impact.

@Candas1 Candas1 deleted the stm32_adc_no_interrupt branch June 10, 2024 21:46
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

2 participants