AnsweredAssumed Answered

Instruction cycles and gpio access

Question asked by de_craemer.klaas on Jan 13, 2013
Latest reply on Jan 16, 2013 by de_craemer.klaas
I wanted to see how fast the gpio pins can be 'pulsed', so I created the following piece of code:

01./* Switch to 16Mhz Crystal Osc clock */
02.CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSE, DISABLE, DISABLE);
03./* Output Fcpu on CLK_CCO pin */
04.CLK_CCOConfig(CLK_OUTPUT_CPU);
05. 
06.GPIO_DeInit(GPIOD);
07.GPIO_Init(GPIOD, GPIO_PIN_3, GPIO_MODE_OUT_PP_LOW_FAST)
08. 
09.while(1){
10.   GPIOD->ODR |= (uint8_t)(GPIO_PIN_3);
11.   GPIOD->ODR &= (uint8_t)(~GPIO_PIN_3);
12.}

Then I connected a scope on the output of GPIO D3 and the CCO output:

gpio-pulse_vs_cco.png

As you can see, about 6 cycles are needed to turn a GPIO on and off, resulting in 16Mhz/6 = 2.6667 Mhz for the GPIO signal.

I found this a bit slow, as the code translates to the following instructions (I have used writeHigh and writeLow as macros for the GPIO->ODR access):

main.c:107          writeHigh(GPIOD, GPIO_PIN_3);
0x81bb <main+40>            0x7216500F      BSET  0x500f,#3           BSET  0x500f,#3
main.c:108          writeLow(GPIOD, GPIO_PIN_3);
0x81bf <main+44>            0x7217500F      BRES  0x500f,#3           BRES  0x500f,#3
0x81c3 <main+48>            0x20F6          JRT   0x81bb              JRT   0x81bb

So in fact only 1 instruction (BSET or BRES) is needed to control a GPIO pin. According to the CPU programming manual PM0044 p.70, these instructions have a 4 byte length and execute in 1 cycle.
Why would it take 2 cycles in the above example? I don't understand...

Outcomes