Ultra Fast GPIO Access on STM32F0

General discussion on mikroBasic PRO for ARM.
Post Reply
Author
Message
neondale
Posts: 4
Joined: 24 Jan 2017 18:37

Ultra Fast GPIO Access on STM32F0

#1 Post by neondale » 31 Jan 2017 17:34

Hello,

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
However when I use this example for setting individual bits, The maximum i can get is about 1.4Mhz

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
clk setup.PNG
clk setup.PNG (28.31 KiB) Viewed 10440 times

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!

User avatar
nadir.celebic
mikroElektronika team
Posts: 465
Joined: 15 Jun 2016 13:19

Re: Ultra Fast GPIO Access on STM32F0

#2 Post by nadir.celebic » 01 Feb 2017 17:27

Hi,

Can you please attach your whole project in in ZIP, so I could test it?

Regards,
Nadir

neondale
Posts: 4
Joined: 24 Jan 2017 18:37

Re: Ultra Fast GPIO Access on STM32F0

#3 Post by neondale » 01 Feb 2017 18:59

Sure, Here is the attached Zip File.

Basicly all this code does is toggle the entire port in a dead loop at 48mhz. the max FQ i can get from the GPIO toggle is about 2.193Mhz. just seams like something is running terribly slow.
Attachments
fastgpio.zip
(67.59 KiB) Downloaded 386 times

neondale
Posts: 4
Joined: 24 Jan 2017 18:37

Re: Ultra Fast GPIO Access on STM32F0

#4 Post by neondale » 01 Feb 2017 19:01

Sure attached is the Zip File.

The program basicly is a dead loop toggling the entire port. This is running at 48Mhz on a STM32F0 Discovery Dev board.

The max i can get the gpio to clock is 2.193Mhz. Seams terribly slow
Attachments
fastgpio.zip
(67.59 KiB) Downloaded 388 times

User avatar
nadir.celebic
mikroElektronika team
Posts: 465
Joined: 15 Jun 2016 13:19

Re: Ultra Fast GPIO Access on STM32F0

#5 Post by nadir.celebic » 02 Feb 2017 17:36

Hi,

The User Manual for the STM32F0 claims that the the output pins fastest toggle speed is every two clock cycles.
Assuming a maximum operation speed of 48MHz, the fastest toggle speed for the GPIO on the STM32f0 is 24 MHZ,
which means the highest frequency square wave that can be produced by the GPIO is 12MHz.

But I will see what may go wrong here.


Regards,
Nadir

neondale
Posts: 4
Joined: 24 Jan 2017 18:37

Re: Ultra Fast GPIO Access on STM32F0

#6 Post by neondale » 04 Feb 2017 19:14

Nadir,

I appreciate you looking into this for me. I didn't know if i was using the wrong commands (syntax) or an incorrect method or if i needed to write some in-line assembly to speed up the processing time... I look forward to your update.

HardWareMan
Posts: 57
Joined: 09 Oct 2014 17:39

Re: Ultra Fast GPIO Access on STM32F0

#7 Post by HardWareMan » 13 Feb 2017 10:59

neondale wrote:The max i can get the gpio to clock is 2.193Mhz. Seams terribly slow
You should note this: when you use high level language, you always get slowest speed. For example, if you use this pair:

Code: Select all

while true do begin
  GPIOC_ODR.B2 := 0;
  GPIOC_ODR.B2 := 1;
end;
You'll get this kind code (you alway can view itself using ASM view):

Code: Select all

L__main409:
;Test.mpas,1422 :: 		GPIOC_ODR.B2 := 0;
MOVS	R1, #0
MOVW	R0, #lo_addr(GPIOC_ODR+0)
MOVT	R0, #hi_addr(GPIOC_ODR+0)
STR	R1, [R0, #0]
;Test.mpas,1423 :: 		GPIOC_ODR.B2 := 1;
MOVS	R1, #1
MOVW	R0, #lo_addr(GPIOC_ODR+0)
MOVT	R0, #hi_addr(GPIOC_ODR+0)
STR	R1, [R0, #0]
;Test.mpas,1424 :: 		end;
IT	AL
BAL	L__main409
So, every GPIO access make huge overhead of real machine code because of reliability. Obvious you can get faster code, writing in pure ASM, and mE compilers allow you to do it. So, you can optimize this code something like this:

Code: Select all

MOVS	R1, #0
MOVS	R2, #1
MOVW	R0, #lo_addr(GPIOC_ODR+0)
MOVT	R0, #hi_addr(GPIOC_ODR+0)
L__main409:
;Test.mpas,1422 :: 		GPIOC_ODR.B2 := 0;
STR	R1, [R0, #0]
;Test.mpas,1423 :: 		GPIOC_ODR.B2 := 1;
STR	R2, [R0, #0]
;Test.mpas,1424 :: 		end;
IT	AL
BAL	L__main409

rmteo
Posts: 1330
Joined: 19 Oct 2006 17:46
Location: Colorado, USA

Re: Ultra Fast GPIO Access on STM32F0

#8 Post by rmteo » 14 Feb 2017 02:42

Look into MCUs that utilize the Cortex M0+ core:
2.2.2. Single-cycle I/O port

The processor optionally implements a single-cycle I/O port that provides very high speed access to tightly-coupled peripherals, such as general-purpose-I/O (GPIO). The port is accessible both by loads and stores, from the processor and from the debugger.
They are available from NXP (FreeScale & NXP), Silabs (Energy Micro) and others and come with comprehensive Free toolsets (IDE, compiler, etc.) such Simplicity Studio(EM) and LPCExpresso (NXP).
You can get them as standalone MCUs or dualcore M4/M0+ such as the LPC 54000 series.
Why pay for overpriced toys when you can have
professional grade tools for FREE!!! :D :D :D

User avatar
dusan.poluga
mikroElektronika team
Posts: 780
Joined: 02 Feb 2017 14:21

Re: Ultra Fast GPIO Access on STM32F0

#9 Post by dusan.poluga » 15 Feb 2017 13:00

Hi,

Put this setting in your code. It is for setting GPIO speed to the maximum setting.

Code: Select all

GPIOx_OSPEEDR =  0xFFFFFFFF; ' sets all pin speed's to the maximum setting
Replace the 'x' with your port letter.
This register can be found at the reference manual of your microcontroller under GPIO registers section.

Regards,
Dusan Poluga.

Post Reply

Return to “mikroBasic PRO for ARM General”