cancel
Showing results for 
Search instead for 
Did you mean: 

GPIO problem

mmilan9
Associate
Posted on July 10, 2010 at 23:33

GPIO problem

2 REPLIES 2
hpipon957
Associate III
Posted on May 17, 2011 at 13:58

Hi

To configure a port you need to do the following:

1) Enable the port clock in RCC_APB2ENR. In your case

RCC_APB2ENR |= (RCC_APB2ENR_IOPDEN);

2) Configure the port bit as output (you an chose 2MHz, 10MHz or 50MHz speed and open drain or push-pull):

GPIOD_CRL &= ~(0x000f0000);

GPIOD_CRL |= (0x00030000);

This configures Port D-4 to push-pull output with 50MHz operation.

3) To toggle the output value

GPIOD_ODR ^= 0x0010;

It may look rather complicated, but I use the following macro to help doing this. It makes coding simpler:

_CONFIG_PORT_OUTPUT(D, PORTD_BIT4, (OUTPUT_SLOW | OUTPUT_PUSH_PULL));

where the macro is defined as:

#define _CONFIG_PORT_OUTPUT(ref, pins, characteristics) RCC_APB2ENR |= (RCC_APB2ENR_IOP##ref##EN); \

     GPIO##ref##_CRL = ((GPIO##ref##_CRL & \

     ~((0x0001 & pins)        | ((0x0001 & pins) << 1)  | ((0x0001 & pins) << 2)  | ((0x0001 & pins) << 3)    | \

     (((0x0002 & pins) << 3)  | ((0x0002 & pins) << 4)  | ((0x0002 & pins) << 5)  | ((0x0002 & pins) << 6))   | \

     (((0x0004 & pins) << 6)  | ((0x0004 & pins) << 7)  | ((0x0004 & pins) << 8)  | ((0x0004 & pins) << 9))   | \

     (((0x0008 & pins) << 9)  | ((0x0008 & pins) << 10) | ((0x0008 & pins) << 11) | ((0x0008 & pins) << 12))  | \

     (((0x0010 & pins) << 12) | ((0x0010 & pins) << 13) | ((0x0010 & pins) << 14) | ((0x0010 & pins) << 15))  | \

     (((0x0020 & pins) << 15) | ((0x0020 & pins) << 16) | ((0x0020 & pins) << 17) | ((0x0020 & pins) << 18))  | \

     (((0x0040 & pins) << 18) | ((0x0040 & pins) << 19) | ((0x0040 & pins) << 20) | ((0x0040 & pins) << 21))  | \

     (((0x0080 & pins) << 21) | ((0x0080 & pins) << 22) | ((0x0080 & pins) << 23) | ((0x0080 & pins) << 24))))| \

      (((0x0001 & pins)       | ((0x0001 & pins) << 1)  | ((0x0001 & pins) << 2)  | ((0x0001 & pins) << 3))  & (characteristics)) | \

     ((((0x0002 & pins) << 3) | ((0x0002 & pins) << 4)  | ((0x0002 & pins) << 5)  | ((0x0002 & pins) << 6))  & ((characteristics) << 4)) | \

     ((((0x0004 & pins) << 6) | ((0x0004 & pins) << 7)  | ((0x0004 & pins) << 8)  | ((0x0004 & pins) << 9))  & ((characteristics) << 8)) | \

     ((((0x0008 & pins) << 9) | ((0x0008 & pins) << 10) | ((0x0008 & pins) << 11) | ((0x0008 & pins) << 12)) & ((characteristics) << 12)) | \

     ((((0x0010 & pins) << 12)| ((0x0010 & pins) << 13) | ((0x0010 & pins) << 14) | ((0x0010 & pins) << 15)) & ((characteristics) << 16)) | \

     ((((0x0020 & pins) << 15)| ((0x0020 & pins) << 16) | ((0x0020 & pins) << 17) | ((0x0020 & pins) << 18)) & ((characteristics) << 20)) | \

     ((((0x0040 & pins) << 18)| ((0x0040 & pins) << 19) | ((0x0040 & pins) << 20) | ((0x0040 & pins) << 21)) & ((characteristics) << 24)) | \

     ((((0x0080 & pins) << 21)| ((0x0080 & pins) << 22) | ((0x0080 & pins) << 23) | ((0x0080 & pins) << 24)) & ((characteristics) << 28))); \

     GPIO##ref##_CRH = ((GPIO##ref##_CRH & \

     ~(((0x0100 & pins) >> 8) | ((0x0100 & pins) >> 7)  | ((0x0100 & pins) >> 6)  | ((0x0100 & pins) >> 5)    | \

     (((0x0200 & pins) >> 5)  | ((0x0200 & pins) >> 4)  | ((0x0200 & pins) >> 3)  | ((0x0200 & pins) >> 2))   | \

     (((0x0400 & pins) >> 2)  | ((0x0400 & pins) >> 1)  | ((0x0400 & pins))       | ((0x0400 & pins) << 1))   | \

     (((0x0800 & pins) << 1)  | ((0x0800 & pins) << 2)  | ((0x0800 & pins) << 3)  | ((0x0800 & pins) << 4))   | \

     (((0x1000 & pins) << 4)  | ((0x1000 & pins) << 5)  | ((0x1000 & pins) << 6)  | ((0x1000 & pins) << 7))   | \

     (((0x2000 & pins) << 7)  | ((0x2000 & pins) << 8)  | ((0x2000 & pins) << 9)  | ((0x2000 & pins) << 10))  | \

     (((0x4000 & pins) << 10) | ((0x4000 & pins) << 11) | ((0x4000 & pins) << 12) | ((0x4000 & pins) << 13))  | \

     (((0x8000 & pins) << 13) | ((0x8000 & pins) << 14) | ((0x8000 & pins) << 15) | ((0x8000 & pins) << 16))))| \

     ((((0x0100 & pins) >> 8) | ((0x0100 & pins) >> 7)  | ((0x0100 & pins) >> 6)  | ((0x0100 & pins) >> 5))  & (characteristics)) | \

     ((((0x0200 & pins) >> 5) | ((0x0200 & pins) >> 4)  | ((0x0200 & pins) >> 3)  | ((0x0200 & pins) >> 2))  & ((characteristics) << 4)) | \

     ((((0x0400 & pins) >> 2) | ((0x0400 & pins) >> 1)  | ((0x0400 & pins))       | ((0x0400 & pins) << 1))  & ((characteristics) << 8)) | \

     ((((0x0800 & pins) << 1) | ((0x0800 & pins) << 2)  | ((0x0800 & pins) << 3)  | ((0x0800 & pins) << 4))  & ((characteristics) << 12)) | \

     ((((0x1000 & pins) << 4) | ((0x1000 & pins) << 5)  | ((0x1000 & pins) << 6)  | ((0x1000 & pins) << 7))  & ((characteristics) << 16)) | \

     ((((0x2000 & pins) << 7) | ((0x2000 & pins) << 8)  | ((0x2000 & pins) << 9)  | ((0x2000 & pins) << 10)) & ((characteristics) << 20)) | \

     ((((0x4000 & pins) << 10)| ((0x4000 & pins) << 11) | ((0x4000 & pins) << 12) | ((0x4000 & pins) << 13)) & ((characteristics) << 24)) | \

     ((((0x8000 & pins) << 13)| ((0x8000 & pins) << 14) | ((0x8000 & pins) << 15) | ((0x8000 & pins) << 16)) & ((characteristics) << 28))); _SIM_PORT_CHANGE

Regards

Mark

http://www.utasker.com/

drzile86
Associate II
Posted on May 17, 2011 at 13:58

Hi (Pozdrav),

On st.com you can download examples for STM32EVAL-C board which have STM32F107V. You can use std peripherial library also to configure registers.

Cheers,

Dragan