-
Notifications
You must be signed in to change notification settings - Fork 978
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
Initialize each ADC only once in Analogread #2133
base: main
Are you sure you want to change the base?
Conversation
Hi @Candas1 |
Bonjour Frederic, Thanks for considering this. Thanks in advance. |
It was used but as you change the code it do not match anymore the format. We provide a script to apply change. in CI/astyle and the rules are defined in the astylerc file: https://github.com/stm32duino/Arduino_Core_STM32/tree/main/CI/astyle |
But it's even reporting code that I haven't changed. |
I got the style fixed locally by astyle. Do you trust this tool or it requires testing again ? |
You can force push. |
This is normal as you changed code block, so some indent are required 😉 |
No worries, I pushed the fixes. |
@Candas1 |
Thanks. I've made a test with a Nucleo-L476RG with your PR and it breaks the analogRead. |
Any details what is happening ? |
I guess the ADC channel conf moved not help and probably more. |
Do you have a table of all models and the ADCs it has ? But do you think it's not worth trying ? |
List of ADC is not an issue,it is quite easy to do something generic. |
Initializing the ADC for each adc sampling is also not the standard way to proceed to be honest. Maybe some families don't like changing the channel configuration while the ADC is on, that could be fixed as well. |
Well it was implemented like this to be arduino compatible as we made only one conversion.
Unfortunately, no, this would requires some investigations and I have no free bandwidth for this.
Probably if you read the comment it seems it depends of the ADC state. I guess using some LL would help on this but probably not simple and fast to do. |
I have some B-G431B-ESC1 laying around, I can maybe try my implementation on those. Do you mind keeping this open and we decide later if it's too complicated ? |
Hello, can you upload your optimized code for a look? |
Hi, I did an obvious mistake that I didn't upload yet in this PR. Now I got F3/F4/L4/G4/H5 nucleos I can test with. |
Thank you very much for your reply. I am looking forward to your sharing. Please call me when you share. |
Use LL functions instead Signed-off-by: Candas1 <candascraig@hotmail.com>
modify for F1/F2/F4/F7/L1 Signed-off-by: Candas1 <candascraig@hotmail.com>
Even after synching some changes were missing somehow Signed-off-by: Candas1 <candascraig@hotmail.com>
Sorry for the late reply. The only doubt I have now is that it's not possible to change the resolution between reads if the adc is not reinitialized, need to check how to deal about it. |
HAL_ADC_Start is ok afterall Signed-off-by: Candas1 <candascraig@hotmail.com>
Signed-off-by: Candas1 <candascraig@hotmail.com>
Signed-off-by: Candas1 <candascraig@hotmail.com>
That's all right, I'm looking forward to your effect. Finally, I have a suggestion: I have done a long experiment to improve the speed of ADC in Arduino development environment, for example DMA, but the Arduinol development environment can not call interrupt, I also consulted fpistm, he replied me that Arduino' official does not have DMA API, so he did not do it Method, so I hope the final optimized code can be a library like stevstrongl's [Arduino_.STM32] |
The scope of this PR is only to optimize analogRead without impacting it's behavior and the projects using it. I have idea about an ADC library as part of SimpleFOC but it would require a much more complicated API, it's out of scope here. |
Ok, thank you for your reply. |
Summary
This PR improves the speed of analogread by initializing each ADC only once(mentioned in this issue)
Currently when using analogread, the following steps are executed:
With this PR, if the ADC wasn't initialized yet by analogread, the following steps are executed:
stop adcdeinitialize adcIf the ADC was initialized by analogread, only the following steps are executed:
initialize adccalibrate adcstop adcdeinitialize adcI tested my changes on a STM32F103RCT6.
I ran analogread in a loop:
With this change, I was also able to run injected adc in parallel with analogread without interfering, the only prerequisite is that analogread should run first.
[EDIT] It was late so I forgot some details.
I tried reading the same pin from 3 different ADCs (PC2_ALT0, PC2_ALT1, PC2_ALT2) consecutively in a loop, it was working well, the init happened only once for each ADC. The same example wasn't working before my change, I was getting readings for one of the analogread calls only.
If you think users were using the previous behavior as a feature, I could also #ifdef the new behavior to make it optional.