cancel
Showing results for 
Search instead for 
Did you mean: 

Basic GPIO_init() question

jhanc.11
Senior

I am using for instance, ADC, UARTS, Timers, external interrupts, etc., all generated with cube. But I notice that cube doesn't always generate GPIO settings for certain pins. For instance, ADC1_IN4, when I select it using my board, an stm32f769i-disco, PIN PA4 is populated and not marked as modified in the GPIO section. When I look at the code generated by cube in the GPIO init section, there is no mention of that pin. This is also the case for TIM3_CH3 where PA6 is again preselected by cube and no GPIO entries are setup (in the generated code). lastly, usart1 follows the same pattern, where PA9 and PA10 are predetermined and no GPIO settings are generated in the resulting code. Does this this have to do with me selecting the stm32f769i-disco board and it defaulting the pins to specific functions? This is driving me crazy because certain pins can be used on more than one function (alternate functions) and with the board limitations, I am needing other pins.

Lastly, I went through the GPIO section port by port and made a map of the PINs that are called out (In GPIO init) and the Timer3, ADC and Usart pins are not coded (in GPIOO) but clearly the functions work. What am I missing here?

Thank

Jerry

1 ACCEPTED SOLUTION

Accepted Solutions
jhanc.11
Senior

Go figure, I told cubemx to generate separate files (c & h for each) and it added all the GPIO initialization in each file. there is clearly something wrong with the mac version I'm running. I thought I was going crazy because simple things just weren't working. The files look ok now, thanks

View solution in original post

15 REPLIES 15
gbm
Lead III

What you are missing is the beauty of HAL+CubeMX code, configuring the pins one by one in dozens of different routines instead of doing it at once in a single place. For a start, look for all MX_something_Init() routines and everything with "msp" in its name.

Peter BENSCH
ST Employee

Some more details: First of all, you will have noticed that the DISCOVERY boards are not so much intended for your own developments as for evaluating the possibilities of the respective target MCU. For this reason, many external sensors etc. are connected, which already occupy many peripheral modules.

The NUCLEO boards, on the other hand, are largely unoccupied and therefore offer considerably more freedom for developing your own projects.

But to your question and confusion: the pins mentioned (TIM3_CH3, PA6 is probably a typo, it is PA8) are already pre-assigned for the above reasons and have therefore also been given their own "user labels", which can be found in CubeMX under the tab GPIO Settings.

The TIM3_CH3 mentioned was named "ARD_D5/PWM", the ADC1_IN4 "ARD_A1" and for USART1 the pins PA9/PA10 the labels "VCP_TX [STM32F103CBT6_PA3]" and "VCP_RX".

Exactly these labels can be found in the main.h as #define, so that the respective pins do not have to be addressed as e.g. GPIOA, GPIO_PIN_9, but as "VCP_TX_GPIO_Port" and "VCP_TX_Pin".

Does it answer your question?

Regards

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
jhanc.11
Senior

Peter, I've looked at every define and those pins, exactly the ones you mention, are not addressed in the GPIO section in main.c, where I would expect them to be. I went thru the GPIO section, too big to post here and checked every define within every GPIO_InitStruct and the pins aren't defined. Their functions must be defined someplace, for instance the tx and rx on usart1 that maps to the VCP on the stlink, those pins are complete opposites so it's not like they would default to input or output. I remember seeing some very specific includes like stm32f769disco.x, I'm looking for that now, maybe they are in there.

The #defines are in main.h, i.e. the header file, not in main.c.

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
jhanc.11
Senior

Yes, the defines are in main.h. What I am referring to is this:

 /*Configure GPIO pins : usart rx PA9 */
  GPIO_InitStruct.Pin = GPIO_PIN_9;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
  /*Configure GPIO pins : usart tx PA10 */
  GPIO_InitStruct.Pin = GPIO_PIN_10;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

PA9 and PA10, before I added the code above, weren't configured anywhere. I shouldn't have used the term 'define' as that has a specific context in 'c' and 'c++'; I should have written 'gpio configured' . I would think pins PA9 and PA10 would be configured in the  static void MX_GPIO_Init sub. With all the modules I'm using, there must be 20 structures as above. I went through each of them, resolved the defines to ports and pins numbers and the pins for usart1, ADC, EXTI and timer interrupts aren't there. So I must be missing some basic understanding of the coding or something. I wrote a few thousand lines of USB code about 5yrs ago or longer on STM32 boards but its been a while.

Thanks for the help and sorry for the confusion.

Jerry

jhanc.11
Senior

Peter, I just noticed you are an STM employee. If you are interested in recreating the problem, I am using the latest cubemx, start a project with the stm32f769i-disco board. Double click on usart1 and you'll see in the GPIO section PA9 and PA10 listed. Generate the code with initialization functions, then check the GPIO section. Shouldn't there be GPIO structure-based definitions and calls to HAL_GPIO_Init for those two pins? I can't find them.

Thanks again

Jerry

Peter BENSCH
ST Employee

They are there, just under the alias:

main.h:

#define VCP_RX_Pin GPIO_PIN_10
#define VCP_RX_GPIO_Port GPIOA
...
#define VCP_TX_Pin GPIO_PIN_9
#define VCP_TX_GPIO_Port GPIOA

HAL_UART_MspInit (depending on the CubeMX setting in main.c or in usart.c):

/**USART1 GPIO Configuration
PA10     ------> USART1_RX
PA9     ------> USART1_TX
*/
GPIO_InitStruct.Pin = VCP_RX_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
HAL_GPIO_Init(VCP_RX_GPIO_Port, &GPIO_InitStruct);
 
GPIO_InitStruct.Pin = VCP_TX_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
HAL_GPIO_Init(VCP_TX_GPIO_Port, &GPIO_InitStruct);

Regards

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
jhanc.11
Senior

I wonder what the heck is going on. They are missing in my projects. I have 'generate code' checked on the advanced settings page. This is clearly why I am having so many strange problems with things not working. I am generating the projects on a mac. I'll try a windows machine.

jhanc.11
Senior

I just tried it again. Nothing. All my functions begin with Mx, for instance MX_USART1_UART_Init. Also, the GPIO function calls would be in a large GPIO routine. What did you use to generate the code? I am using Stm32cubemx v6.6.1. Is there another code generator? It sounds like there is something very basically wrong with my workflow. Thanks, it's late here, I'm going to take another look at pick it up tomorrow.