cancel
Showing results for 
Search instead for 
Did you mean: 

How to Write 8 bit value on port in fastest way without disturbing other input/output pins on port

aamirali641989
Associate II
Posted on June 14, 2013 at 14:15

I have write 8 bit value on port PA0-PA7, without disturbing PA8-PA15 in fastest possible way, also if interrupt comes & in that ISR PA8-PA15 are accessed they shouldn't make any error.

I have edited GPIO defs of the stm32f2xx.h file:

#pragma anon_unions

typedef struct

{

  __IO uint32_t MODER;    /*!< GPIO port mode register,               Address offset: 0x00      */

  __IO uint32_t OTYPER;   /*!< GPIO port output type register,        Address offset: 0x04      */

  __IO uint32_t OSPEEDR;  /*!< GPIO port output speed register,       Address offset: 0x08      */

  __IO uint32_t PUPDR;    /*!< GPIO port pull-up/pull-down register,  Address offset: 0x0C      */

  __IO uint32_t IDR;      /*!< GPIO port input data register,         Address offset: 0x10      */

  __IO uint32_t ODR;      /*!< GPIO port output data register,        Address offset: 0x14      */

  union 

  {

    struct

    {  

      __IO uint16_t BSRRL;    /*!< GPIO port bit set/reset low register,  Address offset: 0x18      */

      __IO uint16_t BSRRH;    /*!< GPIO port bit set/reset high register, Address offset: 0x1A      */

    };

    uint32_t BSRR;    

  };    

  __IO uint32_t LCKR;     /*!< GPIO port configuration lock register, Address offset: 0x1C      */

  __IO uint32_t AFR[2];   /*!< GPIO alternate function registers,     Address offset: 0x24-0x28 */

} GPIO_TypeDef;

#pragma no_anon_unions

Now I can write directly to BSRR register. I used this:, is this best way or any method:

        uint32_t i;

        for( i = 8; i < 255; i++ )

        {

             GPIOA->BSRR =  (i & 0x000000ff) | ((~i << 16) & 0x00ff0000); 

        } 

I am using Keil MDK arm
2 REPLIES 2
aamirali641989
Associate II
Posted on June 14, 2013 at 14:19

Also I have started a thread on keil :

http://www.keil.com/forum/23251/

, why there is difference in timing between two.

I have also tried by changing the macro with do{}while(0);, nut no effect

gfuehrer
Associate II
Posted on June 14, 2013 at 19:53

If the 8 bits are aligned to a byte boundary, it is faster to write the byteto the Output Data Register:

for (uint8_t i = 8; i < 
255
; ++i) 
((uint8_t __IO*)&GPIOA->ODR)[0] = i;

-Gary