Problems with STM32L051C8T6 GPIO Output High Frequency  - Page 1
Forum section: electronics / general

author: Shell Albert
author_rank: Contributor
timestamp: May 30, 2024, 01:50:00 am
content: Hi, guys.I really need help so I come here.I'm using STM32L051C8T6 in my low-power-consumption design.For some peripheral devices accessing, I need to accelerate timing speed.So I test how fast GPIOs can go.I generated a very simple project using STM32CubeIDE, only 4 lines were added by manual.When I measured GPIO with an oscilloscope, it can't reach a high frequency.it's only 250KHz!and I also configure GPIO to Very HIGH output mode.That’s so weird, in my opinion, HAL_GPIO_WritePin() can’t take up much time, and __NOP() only takes up one clock period. But the reality breaks my thought. What’s going on here?And how can I output a very high frequency from GPIO? For example, 2MHz.Does anyone have experience with STM32L051C8T5?

author: ataradov
author_rank: Super Contributor
timestamp: May 30, 2024, 03:51:32 am
content: This toggling frequency has nothing to do with the GPIO speed. The speed configuration is the drive strength. It is useful to limit the EMC and possibly for impedance matching, but it does not affect toggling frequency, it just sets how "soft" your edges would be.Is your input clock actually 32 MHz? Show full schematic.EDIT: I see you are using HSI RC and PLL. In any event verify that the clock is correct.And then look inside HAL_GpioWritePin(). See how much code is there. And then check compiler optimization settings.Attach the resulting ELF file too.

author: Shell Albert
author_rank: Contributor
timestamp: May 30, 2024, 06:20:43 am
content: Thanks for your reply, sir.I follow your guide and check my design carefully.First, the external clock frequency on PCB is 32MHz indeed, I confirmed that with an oscilloscope.And I enabled “Master Clock Output 1” feature, and STM32 output main clock through PA8, I measured this pin with an oscilloscope, it was 32MHz.Whether I use HSE or HSI, I make sure the system runs at 32MHz, I measured “Master Clock Output 1” with an oscilloscope, it was 32MHz, there was no doubt.Then I go to check HAL_GPIO_WritePin() function, it checks parameters validation, then operates registers directly.The last point you mentioned, I checked again, No Optimization Enabled.This is a very tiny code project to output a fixed frequency wave. What’s going on? Did I miss something? This really confused me.Then My colleagues suggested me to update STM32CubeIDE to latest version.The version I’m using is V1.14.1, the latest version on ST.com is V1.15.1,After I eliminated old version and updated to the latest version, nothing changed, I still can’t get a correct consequence.Does anyone do me a favor?

author: ataradov
author_rank: Super Contributor
timestamp: May 30, 2024, 06:25:19 am
content: Well, how big are those asserts?Enable optimization and see if it improves things. Then remove those asserts and check if that improves things.And then look at the disassembly and see how much code there actually is.

author: Shell Albert
author_rank: Contributor
timestamp: May 30, 2024, 06:29:39 am
content: Whether I use HSE(External Clock) or HSI(Internal Clock), the consequence is incorrect.I make sure the system runs at 32MHz,I enable "Master Clock Output 1" feature, So I can measure through PA8.I measured PA8 with an oscilloscope, it was 32MHz indeed.If someone could offer help, no matter how small, it will be appreciated.Got trapped in this problem for nearly a week, I'm going insane. I'm thinking if it's ST company's fault, STM32L051C8T6 is in less fabrication, only few people use it, some bugs can't be revealed and fixed.

author: ataradov
author_rank: Super Contributor
timestamp: May 30, 2024, 06:31:01 am
content: Are you reading my posts? Comment out the asserts and see if it speeds things up. Also enable optimization. Unoptimized code may be big and slow.And then look at the disassembly and see how many instructions are generated in all those cases.

author: Shell Albert
author_rank: Contributor
timestamp: May 30, 2024, 06:38:35 am
content: Thank you very much, sir.I enabled Optimization Level from None to -O1, and now I get an improvement,The output frequency becomes from 250KHz to 750KHz, it was a performance improvement, so proud of you.My expectation is 2MHz, I’m still trying.

author: ataradov
author_rank: Super Contributor
timestamp: May 30, 2024, 06:40:28 am
content: LOOK AT THE DISASSEMBLY.And enable higher levels. If you want fast code, you need to enable optimizations. Have you commented out those asserts?

author: Shell Albert
author_rank: Contributor
timestamp: May 30, 2024, 06:42:26 am
content: I comment asserts, but sadly, nothing happened.Code:void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState){/* Check the parameters *///assert_param(IS_GPIO_PIN_AVAILABLE(GPIOx, GPIO_Pin));//assert_param(IS_GPIO_PIN_ACTION(PinState));if (PinState != GPIO_PIN_RESET){GPIOx->BSRR = GPIO_Pin;}else{GPIOx->BRR = GPIO_Pin ;}}

author: ataradov
author_rank: Super Contributor
timestamp: May 30, 2024, 06:44:17 am
content: I will repeat one last time. Look at the disassembly or attache the ELF file. You will be sitting there another week if you approach things like this.If that function is not getting optimized and it really just passes those pointers and bit numbers around, things will be slow.Throw away that WritePin stuff and write into the register directly.

author: pcprogrammer
author_rank: Super Contributor
timestamp: May 30, 2024, 06:46:51 am
content: Take a look at this thread here:https://www.eevblog.com/forum/microcontrollers/starting-with-stm32-(nucleo-l412kb)/msg5498728/#msg5498728The op there did some tests on the same issue, but a different MCU. Should not make that much of a difference except for the master clock speed.

author: Shell Albert
author_rank: Contributor
timestamp: May 30, 2024, 06:51:21 am
content: I tried to change optimization level to -Ofast, only a little bit improvement, 800KHz.I remember the ST datasheet said the GPIO speed can speed up to maximum of 50MHz.Although I'm not familiar with disassembly language, you inspire me to check it.I'm going to check it.Thanks in advance, sir.

author: ataradov
author_rank: Super Contributor
timestamp: May 30, 2024, 02:02:53 pm
content: Read the linked thread, it has all the advice you need.If you want thing to be fast, then stop using ST HAL and write registers directly.