Author Topic: Problems with STM32L051C8T6 GPIO Output High Frequency  (Read 805 times)

0 Members and 1 Guest are viewing this topic.

Offline Shell AlbertTopic starter

  • Contributor
  • Posts: 16
  • Country: us
Problems with STM32L051C8T6 GPIO Output High Frequency
« on: May 30, 2024, 01:50:00 am »
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? 
« Last Edit: May 30, 2024, 01:59:42 am by Shell Albert »
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11652
  • Country: us
    • Personal site
Re: Problems with STM32L051C8T6 GPIO Output High Frequency
« Reply #1 on: May 30, 2024, 03:51:32 am »
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.
« Last Edit: May 30, 2024, 03:53:59 am by ataradov »
Alex
 

Offline Shell AlbertTopic starter

  • Contributor
  • Posts: 16
  • Country: us
Re: Problems with STM32L051C8T6 GPIO Output High Frequency
« Reply #2 on: May 30, 2024, 06:20:43 am »
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?
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11652
  • Country: us
    • Personal site
Re: Problems with STM32L051C8T6 GPIO Output High Frequency
« Reply #3 on: May 30, 2024, 06:25:19 am »
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.
Alex
 

Offline Shell AlbertTopic starter

  • Contributor
  • Posts: 16
  • Country: us
Re: Problems with STM32L051C8T6 GPIO Output High Frequency
« Reply #4 on: May 30, 2024, 06:29:39 am »
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.
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11652
  • Country: us
    • Personal site
Re: Problems with STM32L051C8T6 GPIO Output High Frequency
« Reply #5 on: May 30, 2024, 06:31:01 am »
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.
Alex
 

Offline Shell AlbertTopic starter

  • Contributor
  • Posts: 16
  • Country: us
Re: Problems with STM32L051C8T6 GPIO Output High Frequency
« Reply #6 on: May 30, 2024, 06:38:35 am »
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.
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11652
  • Country: us
    • Personal site
Re: Problems with STM32L051C8T6 GPIO Output High Frequency
« Reply #7 on: May 30, 2024, 06:40:28 am »
LOOK AT THE DISASSEMBLY.

And enable higher levels. If you want fast code, you need to enable optimizations. Have you commented out those asserts?
Alex
 

Offline Shell AlbertTopic starter

  • Contributor
  • Posts: 16
  • Country: us
Re: Problems with STM32L051C8T6 GPIO Output High Frequency
« Reply #8 on: May 30, 2024, 06:42:26 am »
I comment asserts, but sadly, nothing happened.

Code: [Select]

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 ;
  }
}
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11652
  • Country: us
    • Personal site
Re: Problems with STM32L051C8T6 GPIO Output High Frequency
« Reply #9 on: May 30, 2024, 06:44:17 am »
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.
« Last Edit: May 30, 2024, 06:47:21 am by ataradov »
Alex
 

Offline pcprogrammer

  • Super Contributor
  • ***
  • Posts: 4242
  • Country: nl
Re: Problems with STM32L051C8T6 GPIO Output High Frequency
« Reply #10 on: May 30, 2024, 06:46:51 am »
Take a look at this thread here: https://www.eevblog.com/forum/microcontrollers/starting-with-stm32-(nucleo-l412kb)/msg5498728/#msg5498728

The 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.

Offline Shell AlbertTopic starter

  • Contributor
  • Posts: 16
  • Country: us
Re: Problems with STM32L051C8T6 GPIO Output High Frequency
« Reply #11 on: May 30, 2024, 06:51:21 am »
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.
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11652
  • Country: us
    • Personal site
Re: Problems with STM32L051C8T6 GPIO Output High Frequency
« Reply #12 on: May 30, 2024, 02:02:53 pm »
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.
Alex
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf