cancel
Showing results for 
Search instead for 
Did you mean: 

Use of .c or .h files

tensaisakuragi06
Associate III

Hi, I am curious one thing when using the HAL libraries. Just as an basic exapmle i assign LED1 to PB12 pin of STM32F070CBT6 as an GPIO_output. And i want to use HAL_GPIO_WritePin(GPIOB, LED1, LED1_SET); instead of HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET); . Do you think it is weird that i want to use my variables like that with HAL libraries? Second thing is (if it is not weir or inappropriate), should i define my definitions as "definitions.c" or "definitions.h"? When i make some search, .c file is not recommended. I am not sure if i do all these steps with my ST microcontroller and it works well.

13 REPLIES 13
tensaisakuragi06
Associate III

Cruel programming world. Thank you for your reminders and tips.


@tensaisakuragi06 wrote:

 If i can improve my logic, please do not hesitate to tell me.


You have only got #defines for the pins - you should also have them for the ports!

eg,

#define LED1_PIN  GPIO_PIN_12
#define LED1_PORT GPIOB

(Note that these are all "macros")

 

 Then you can write:

HAL_GPIO_WritePin( LED1_PORT, LED1_PIN, LED1_ON );

 

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.
Karl Yamashita
Principal

If you use STM32CubeIDE or STM32CubeMX, you can use Enter User Label to name the pin. In this case, PA5 is named LED_GREEN.

KarlYamashita_0-1744744179992.png

 

When you generate the code, it'll create the Port and pin defines in main.h

/* Private defines -----------------------------------------------------------*/
#define MCO_Pin GPIO_PIN_0
#define MCO_GPIO_Port GPIOF
#define LED_GREEN_Pin GPIO_PIN_5
#define LED_GREEN_GPIO_Port GPIOA
#define TMS_Pin GPIO_PIN_13
#define TMS_GPIO_Port GPIOA
#define TCK_Pin GPIO_PIN_14
#define TCK_GPIO_Port GPIOA

 

 

Don't worry, I won't byte.
TimerCallback tutorial! | UART and DMA Idle tutorial!

If you find my solution useful, please click the Accept as Solution so others see the solution.

I just do them in "definitions.h" header file, not under the Private Define. It still works. Thank you all for your help.