Skip to main content
Graduate
December 16, 2024
Solved

Generated private variables in main.c

  • December 16, 2024
  • 3 replies
  • 1298 views

In the generated code in main.c file, there are variables that are declared as "private variables" by the software, such as "ADC_HandleTypeDef hadc1"; "TIM_HandleTypeDef htim1". Since these variables are declared only in main.c and not in main.h, does that mean that the use of these variables in other modules is not encouraged? 
If I need to use them in other modules, can I declare these variable as extern in main.h or is there a better way to handle this?

Thank you in advance!

    This topic has been closed for replies.
    Best answer by Andrew Neil

    @neel1311 wrote:

    In the generated code in main.c file, there are variables that are declared as "private variables" by the software, such as "ADC_HandleTypeDef hadc1"; "TIM_HandleTypeDef htim1". !


    Please show that code.

    See: How to insert source code.

    In the C language, variables (and functions) defined at file scope are public unless specifically qualified as static

     

    EDIT:

    You mean stuff like this:

    /* Private includes ----------------------------------------------------------*/
    /* USER CODE BEGIN Includes */
    #include "stm32g0xx_nucleo_32.h"
    /* USER CODE END Includes */
    
    /* Private typedef -----------------------------------------------------------*/
    /* USER CODE BEGIN PTD */
    
    /* USER CODE END PTD */
    
    /* Private define ------------------------------------------------------------*/
    /* USER CODE BEGIN PD */
    
    /* USER CODE END PD */
    
    /* Private macro -------------------------------------------------------------*/
    /* USER CODE BEGIN PM */
    
    /* USER CODE END PM */
    
    /* Private variables ---------------------------------------------------------*/
    ADC_HandleTypeDef hadc1;

    That comment is actually misleading.

    As noted above, the hadc1 definition there is public - because it is not qualified as static.

    So, as @Karl Yamashita said, you can provide your own extern declarations anywhere you want to use it - either in your own header file, or in a USER section of one of the auto-generated headers.

    If you configure the project to generate separate .c & .h files for peripheral initialisation:

    AndrewNeil_0-1734432078943.png

    The extern declarations for the peripheral handles will be in the generated headers; eg,

    /* Includes ------------------------------------------------------------------*/
    #include "main.h"
    
    /* USER CODE BEGIN Includes */
    
    /* USER CODE END Includes */
    
    extern ADC_HandleTypeDef hadc;
    
    /* USER CODE BEGIN Private defines */
    
    /* USER CODE END Private defines */

     

     

     

     

     

    3 replies

    Graduate II
    December 16, 2024

    You can use them as extern. If the variables are not meant to be used outside of the file then it would be declared static.

    Graduate II
    December 16, 2024

    You typically don't define variables in .H files, you provide prototypes, extern for those you wish to pull into multiple source files, so as not to get "multiple definition" errors.

    You can define and share these in ONE .C file, or make them static to hide them from the global name-space.

    Often you'd want to share peripheral instances with stm32xyz_it.c so you could keep all the IRQHandler over there, but HAL/CUBE generally just does what it wants.

     

    Super User
    December 17, 2024

    @neel1311 wrote:

    In the generated code in main.c file, there are variables that are declared as "private variables" by the software, such as "ADC_HandleTypeDef hadc1"; "TIM_HandleTypeDef htim1". !


    Please show that code.

    See: How to insert source code.

    In the C language, variables (and functions) defined at file scope are public unless specifically qualified as static

     

    EDIT:

    You mean stuff like this:

    /* Private includes ----------------------------------------------------------*/
    /* USER CODE BEGIN Includes */
    #include "stm32g0xx_nucleo_32.h"
    /* USER CODE END Includes */
    
    /* Private typedef -----------------------------------------------------------*/
    /* USER CODE BEGIN PTD */
    
    /* USER CODE END PTD */
    
    /* Private define ------------------------------------------------------------*/
    /* USER CODE BEGIN PD */
    
    /* USER CODE END PD */
    
    /* Private macro -------------------------------------------------------------*/
    /* USER CODE BEGIN PM */
    
    /* USER CODE END PM */
    
    /* Private variables ---------------------------------------------------------*/
    ADC_HandleTypeDef hadc1;

    That comment is actually misleading.

    As noted above, the hadc1 definition there is public - because it is not qualified as static.

    So, as @Karl Yamashita said, you can provide your own extern declarations anywhere you want to use it - either in your own header file, or in a USER section of one of the auto-generated headers.

    If you configure the project to generate separate .c & .h files for peripheral initialisation:

    AndrewNeil_0-1734432078943.png

    The extern declarations for the peripheral handles will be in the generated headers; eg,

    /* Includes ------------------------------------------------------------------*/
    #include "main.h"
    
    /* USER CODE BEGIN Includes */
    
    /* USER CODE END Includes */
    
    extern ADC_HandleTypeDef hadc;
    
    /* USER CODE BEGIN Private defines */
    
    /* USER CODE END Private defines */