Can anyone please advise me how it would be possible to toggle an MCU's GPIO(s) using the fastest available method.
Currently i have a STM32F051 Discovery that has been setup to run at 48Mhz: My problem is that i am only able to toggle the GPIO at maximum 2Mhz in a dead loop. That seams crazy to me since the chip is running at 48mhz and i should be able to get at least 24Mhz with instruction cycles...? I am trying to switch GPIO's at very high frequency but it seams that the Basic Language Instructions take too many clock cycles to execute or i am using the wrong method. I want to be able to set individual bits of my desired GPIO Ports and i need the switching to be FAST!
Below Example yields the fastest possible PortC GPIO Toggle of the entire port @ around 2Mhz from STM32 @ 48Mhz
Code: Select all
while TRUE
GPIOC_ODR = 0xFFFF
GPIOC_ODR = 0x0000
WEND
Code: Select all
while TRUE
GPIOC_ODR.B8 = 1
GPIOC_ODR.B8 = 0
WEND
I was reading that these above operations of individual bit setting may be done through a Read/Modify/Write Sequance that takes too many instruction cycles.
I read that i may need to write directly to the BSRR Register? or use assembly?
Here is my Entire Code with CLK Setup
MCU Clock Setup should be correct (MCU operating at 48Mhz
Code: Select all
main:
' Main program
GPIO_Digital_Output(@GPIOA_BASE, _GPIO_PINMASK_ALL) ' Set PORTA as digital output
GPIO_Digital_Output(@GPIOB_BASE, _GPIO_PINMASK_ALL) ' Set PORTB as digital output
GPIO_Digital_Output(@GPIOC_BASE, _GPIO_PINMASK_ALL) ' Set PORTC as digital output
GPIO_Digital_Output(@GPIOD_BASE, _GPIO_PINMASK_ALL) ' Set PORTD as digital output
GPIO_Digital_Output(@GPIOF_BASE, _GPIO_PINMASK_ALL) ' Set PORTE as digital output
GPIO_Config(@GPIOC_BASE,
_GPIO_PINMASK_ALL,
_GPIO_CFG_MODE_OUTPUT or _GPIO_CFG_SPEED_MAX)
GPIOA_ODR = 0
GPIOB_ODR = 0
GPIOC_ODR = 0
GPIOD_ODR = 0
GPIOF_ODR = 0
' pa0 = user button
' pc9 = green led
' pc8 = blue led
while TRUE
GPIOC_ODR = 0xFFFF
GPIOC_ODR = 0x0000
WEND
end.
Please offer you advice! Thanks So much!