2025-04-15 5:01 AM - last edited on 2025-04-15 5:57 AM by Andrew Neil
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.
Solved! Go to Solution.
2025-04-15 7:44 AM
Cruel programming world. Thank you for your reminders and tips.
2025-04-15 7:50 AM - edited 2025-04-15 7:51 AM
@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 );
2025-04-15 12:13 PM
If you use STM32CubeIDE or STM32CubeMX, you can use Enter User Label to name the pin. In this case, PA5 is named LED_GREEN.
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
2025-04-15 10:21 PM
I just do them in "definitions.h" header file, not under the Private Define. It still works. Thank you all for your help.