1
\$\begingroup\$

I'd like to use the GPIO_TypeDef an GPIO_Init to create a bidirectional Port on the STM32F407, some input pins, some output pins.

Do I just run GPIO_Init twice, once for the input pins and once for the output pins, after setting up the GPIO_Typedef structure, or does the second run undo everything the first run did?

I know (at least I'm pretty sure) that this can be coded at the register level without problems, but I'd like the CMSIS way of doing things.

\$\endgroup\$
2
\$\begingroup\$

As long as you specify different pins in the GPIO_Typedef structure, you can run GPIO_Init as many times as you like without un-doing the other inits. You can group all the inputs (or outputs) together and bitwise OR the pin designations (such as GPIO_Pin_0 | GPIO_Pin_1), or you can do them all separately, calling GPIO_Init for each one.

Don't forget to enable the GPIO AHB clock using: RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOx, ENABLE); before configuring the pins.

\$\endgroup\$
  • 1
    \$\begingroup\$ @ScottSeidman ... If you haven't already done so, I would recommend downloading the source code for the Standard Peripherals Library. Not only can you then see how functions such as GPIO_Init() work, but (oddly enough) they include instructions for setting up and using the peripherals in the source files. It seems to me that it would be better if the instructions were in the header files, but not so. \$\endgroup\$ – Tut Aug 18 '15 at 19:31
  • \$\begingroup\$ Good advice. I did check out the "How to Use this Driver" section, but that wasn't clear on this question. Digging a bit deeper, it's clear that GPIO_Init loops over the bits specified in the GPIO_Pin field, and doesn't touch anything else. I guess I have to be meticulous about the GPIO_InitStruct housekeeping if I don't want to do stuff accidentally (or just use two separate function calls to set it up). Is there a good way to clear that structure? \$\endgroup\$ – Scott Seidman Aug 18 '15 at 19:49
  • 1
    \$\begingroup\$ @ScottSeidman ... I don't know of a good way to clear the structure. They do provide GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct), but that just fills in the struct with default values so you still have to fill in everything that is different and the default for GPIO_Pin is GPIO_Pin_All so you need to be careful with that. I prefer to explicitly setup all the struct values immediately before each call to GPIO_Init(). \$\endgroup\$ – Tut Aug 18 '15 at 20:07
  • \$\begingroup\$ @ScottSeidman ... Of course, that may be a bit tedious if you will be changing ports more than once such as changing inputs to outputs and back again. In that case I would probably use pre-loaded structures. \$\endgroup\$ – Tut Aug 18 '15 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.