Skip to main content
chrisevans3
Associate
August 5, 2011
Question

GPIO struct parameters not initialized

  • August 5, 2011
  • 5 replies
  • 1709 views
Posted on August 05, 2011 at 12:26

The GPIO struct is defined but it is not initialized before it is used in main.  Everything works but I am stuck with trying to find a value that I can use in GPIO_Init.  GPIOx->CR2 is OR'd with another value and I don't know where to get the initial value GPIOx->CR2.  The only thing I can think is that a random number is in that address.

This is in the given program that comes with the firmware.  I am new to C and this is probably a simple problem. 

Thanks,

Chris

In the main function that comes with the Discovery microcontroller.  The first thing that main does is to call GPIO_Init.   I have included it here.

void GPIO_Init(GPIO_TypeDef* GPIOx,

               uint8_t GPIO_Pin,

               GPIO_Mode_TypeDef GPIO_Mode)

{

 

 

  /*----------------------*/

  /* Check the parameters */

  /*----------------------*/

  assert_param(IS_GPIO_MODE(GPIO_Mode));

  assert_param(IS_GPIO_PIN(GPIO_Pin));

  /* Reset crresponding bit to GPIO_Pin in CR2 register */

  GPIOx->CR2 &= (uint8_t)(~(GPIO_Pin));

  /*-----------------------------*/

  /* Input/Output mode selection */

  /*-----------------------------*/

  if ((((uint8_t)(GPIO_Mode)) & (uint8_t)0x80) != (uint8_t)0x00) /* Output mode */

  {

    if ((((uint8_t)(GPIO_Mode)) & (uint8_t)0x10) != (uint8_t)0x00) /* High level */

    {

      GPIOx->ODR |= GPIO_Pin;

    } else /* Low level */

    {

      GPIOx->ODR &= (uint8_t)(~(GPIO_Pin));

    }

    /* Set Output mode */

    GPIOx->DDR |= GPIO_Pin;

  } else /* Input mode */

  {

    /* Set Input mode */

    GPIOx->DDR &= (uint8_t)(~(GPIO_Pin));

  }

I think you can see that GPIOx->CR2 and GPIOx->ODR and GPIOx->DDR are being used to set bits.(Outlined in RED)  My thinking is that they are not initialized by the function GPIO_Init yet so any number that happens to be in that address could be used.

I am a beginner in both uC's and C.  Thanks for your help.

Chris

#initialize-a-structure #memory-mapped-peripherals
    This topic has been closed for replies.

    5 replies

    Andrew Neil
    Super User
    August 6, 2011
    Posted on August 06, 2011 at 09:00

    Can you show an example to illustrate what you're talking about?

    ''I am new to C''

    Are you also new to programming in general, and/or to microcontrollers, and/or digital electronics?

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    chrisevans3
    Associate
    August 8, 2011
    Posted on August 08, 2011 at 18:27

    Andrew Neil
    Super User
    August 9, 2011
    Posted on August 09, 2011 at 08:09

    ''Thanks for responding to my post.''

    You're welcome. 

    ''I am new to C and uC's''

     

    Don't try to learn too much new stuff all at once!

    It might be easier to learn the 'C' programming language on a PC or other ''normal'' platform before moving on to apply it to microcontrollers...

    Some 'C' starting tips here: 

    http://blog.antronics.co.uk/2011/08/08

    ''have some experience in digital electronics''

    So are you familiar with the concept of memory-mapped IO?

    It means that the IO registers appear in the CPU's normal memory map as normal memory locations - so the CPU accesses the peripherals just with normal memory reads & writes.

    Therefore, by locating 'C' variables at these locations, your 'C' program can access the peripheral registers by just accessing the variables.

    This is what the code you showed is doing - using a struct to group all the registers of a port together.

    Therefore you do not initialise the structure - reading the structure just gives the values from the hardware registers!

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    Andrew Neil
    Super User
    August 9, 2011
    Posted on August 09, 2011 at 10:36

    ''memory-mapped IO ... using a struct to group all the registers of a port together''

    Note that this is a very common & widely used technique.

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    chrisevans3
    Associate
    August 9, 2011
    Posted on August 09, 2011 at 12:15