cancel
Showing results for 
Search instead for 
Did you mean: 

Pin toggling speed on STM32G031

cp19
Associate II

The MCU is STM32G031J6 on a STM32G0316-DISCO board.

The maximum software pin toggling speed I am able to achieve is about 0.7 us.

It looks a bit slow for a M0+ running at 48 mhz.

Looking at listing file, there is no library overhead (I am using LL).

Can someone shed some light on this?

 

1 ACCEPTED SOLUTION

Accepted Solutions
cp19
Associate II

Solved, thank you all.

My bad. Inexperienced with Stm32Cube, I was always flashing the MCU with the Debug build.

Attached there are the listing of the main loop and the output.

 

 

View solution in original post

8 REPLIES 8

> running at 48 mhz

Milli-hecto-z? 😉

Does it indeed run at 48MHz? Read out and check/post content of relevant RCC registers.

> Looking at listing file

Show.

JW

 

Yes, it does indeed run at 48 mhz, I checked it with a scope on pin 5, configured as MCO.

I have not the listing at hand, I'll post it as soon as I can.

AScha.3
Chief

what optimizer setting you have?  try -O2

If you feel a post has answered your question, please click "Accept as Solution".

Speed depends on how efficiently you write to registers, whether you RMW to ODR or write patterns to BSRR

Normally for high speed you'd use a TIM in toggle mode, rather than saturate the MCU with bus traffic to the AHB/APB, which realistically is going to take at least 4 machine cycles.

For speed critical output perhaps a RAM based pattern buffer and TIM+DMA+GPIO to drive between 1 and 16 pins in a single GPIO Bank

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

>the MCU with bus traffic to the AHB/APB, which realistically is going to take at least 4 machine cycles.

As far as I know, this MCU features the 'single cycle' IOPORT.

gbm
Lead III

Not sure what you mean by "toggling" since you didn't show the actual code. The HAL_GPIO_Toggle routine has some errors/inefficiencies. The optimal routine should be this:

 

 

static inline void TogglePin(GPIO_TypeDef *port, uint16_t msk)
{
    port->BSRR = msk << 16 | (~port->ODR & msk);
}

 

In contrast, the HAL routine is not declared as static inline, so it must be really called (which takes few instruction cycles) and there is an extra, unnecessary logic AND operation in it.

btw

i just tested an H563 , 250MHz , -O2 ; APB bus at 250M , pin speed : very high; 

makes 4 ns at the pin !  (with:  GPIOB->BSRR = GPIO_PIN_0; )

If you feel a post has answered your question, please click "Accept as Solution".
cp19
Associate II

Solved, thank you all.

My bad. Inexperienced with Stm32Cube, I was always flashing the MCU with the Debug build.

Attached there are the listing of the main loop and the output.