Skip to main content
Graduate
December 11, 2023
Solved

Pin toggling speed on STM32G031

  • December 11, 2023
  • 5 replies
  • 3559 views

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?

 

    This topic has been closed for replies.
    Best answer by cp19

    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.

     

     

    5 replies

    Super User
    December 11, 2023

    > 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

     

    cp19Author
    Graduate
    December 11, 2023

    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.

    Super User
    December 11, 2023

    what optimizer setting you have?  try -O2

    Graduate II
    December 11, 2023

    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

    cp19Author
    Graduate
    December 11, 2023

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

    Graduate
    December 11, 2023

    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.

    Super User
    December 11, 2023

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

    cp19AuthorAnswer
    Graduate
    December 11, 2023

    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.