Question
Instruction cycles and gpio access
Posted on January 13, 2013 at 18:58
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):
I wanted to see how fast the gpio pins can be 'pulsed', so I created the following piece of code:
/* Switch to 16Mhz Crystal Osc clock */
CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSE, DISABLE, DISABLE);
/* Output Fcpu on CLK_CCO pin */
CLK_CCOConfig(CLK_OUTPUT_CPU);
GPIO_DeInit(GPIOD);
GPIO_Init(GPIOD, GPIO_PIN_3, GPIO_MODE_OUT_PP_LOW_FAST)
while
(1){
GPIOD->ODR |= (uint8_t)(GPIO_PIN_3);
GPIOD->ODR &= (uint8_t)(~GPIO_PIN_3);
}
Then I connected a scope on the output of GPIO D3 and the CCO output:
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...