cancel
Showing results for 
Search instead for 
Did you mean: 

GPIO toggle, uneven high and low.

arunashok
Associate
Posted on December 09, 2011 at 14:39

Hi,

 I want to measure the clock of my stm32f225rg chip. I am doing it by toggling the bit of gpio port B and checking using scope.  Rather than using the routines in the standard peripheral library, I am programming the registers directly.  The routine for toggling is below.

while (1)

      {

        GPIOB->BSRRL = GPIO_Pin_15;     // Set

        GPIOB->BSRRH = GPIO_Pin_15;      // ReSet

        GPIOB->BSRRL = GPIO_Pin_15;     // Set

        GPIOB->BSRRH = GPIO_Pin_15;      // ReSet

        GPIOB->BSRRL = GPIO_Pin_15;     // Set

        GPIOB->BSRRH = GPIO_Pin_15;      // ReSet

        GPIOB->BSRRL = GPIO_Pin_15;     // Set

        GPIOB->BSRRH = GPIO_Pin_15;      // ReSet

        GPIOB->BSRRL = GPIO_Pin_15;     // Set

        GPIOB->BSRRH = GPIO_Pin_15;      // ReSet

        GPIOB->BSRRL = GPIO_Pin_15;     // Set

        Delay(200);

        GPIOB->BSRRH = GPIO_Pin_15;     // ReSet

        Delay(200);

      }

The toggling can be observed in the scope, but I find the on and off times different. Though the number of instructions for the bitset and bitreset are 4 it seems bitset is using 5 clock cycles and bitreset 4. Any help  would be appreciated.
1 REPLY 1
Posted on December 09, 2011 at 16:11

The GPIO unit is on the APB bus, and behind a write buffer, figure at least 4 cycles. Toggling the pins in this fashion is awfully inefficient. Performance might be different if you use 32-bit writes, and code this in assembler.

Use a TIMer to generate signals with predictable duty cycles.

Use the MCO pin to measure CPU frequency.

Run code from RAM to avoid unpredictable behaviour of the flash controller/cache.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..