2025-05-20 10:18 AM - last edited on 2025-05-20 10:35 AM by Andrew Neil
I wrote this code on my Nucelo-STM32F4x MCU on Windows to play with the LED and the user button:
bool f = true;
while (1) {
if(f) {
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
HAL_Delay(300);
}
if(!HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13))
f = false;
if(!f) {
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
HAL_Delay(2000);
}
}
It works (however, I had to use #include <stdbool.h> to be able to use bool f = true; :|)
I like to use C++ for embedded. So the first thing to do is renaming main.c to main.cpp (still works even after removing #include <stdbool.h>) But as I have tested earlier, any change in the .ioc file makes another main.c file (while at the beginning of the project I chose C++ as the target language).
Any idea to make the IDE stick to C++ or any workaround?
2025-05-20 11:20 AM
Better dont rename the main.c , its doing the init of the chip and the HAL libs are in C, so just let it do its job, as it is.
Just after init call "your main" program, myprog.cpp , or so, included in the core src directory .
So init, generate code, HAL etc are working (with C code) and your program can be c++ .
2025-05-20 1:02 PM
>> Just after init
You mean after HAL_Init();?
>> call "your main" program, myprog.cpp , or so, included in the core src directory .
Call myprog? If that's a C++ file then how to call it!? That's not a function to call.
It's possible to add a new file named myProg.cpp into the Src directory, but what should it contain?
2025-05-20 1:24 PM
Header file cpp_file.h:
#ifndef CPP_FILE_H
#define CPP_FILE_H
#ifdef __cplusplus
extern "C" {
#endif
void my_func();
#ifdef __cplusplus
}
#endif
#endif // CPP_FILE_H
CPP source file ccp_file.cpp:
#include "cpp_file.h
#include "main.h" // this includes your peripheral definitions and pin names indirectly
void my_func()
{
// add your code here
}
main.c:
#include "cpp_file.h"
int main()
{
...
while(1)
{
my_func();
}
}
2025-05-20 1:40 PM - edited 2025-05-20 1:45 PM
>You mean after HAL_Init();?
No, after all init is done...
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_SDMMC1_SD_Init();
MX_FATFS_Init();
MX_USB_HOST_Init();
/* USER CODE BEGIN 2 */
my_cpp_prog();
// We should never get here as control is now taken by your cpp program
Its exactly same way, a RTOS is included , here Azure "start" :
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_GPDMA1_Init();
MX_ICACHE_Init();
MX_ADC1_Init();
MX_ETH_Init();
MX_LPUART1_UART_Init();
MX_SDMMC1_SD_Init();
MX_USB_HCD_Init();
MX_USART3_UART_Init();
MX_SAI1_Init();
MX_SPI2_Init();
MX_SPI4_Init();
MX_I2C2_Init();
MX_I3C1_Init();
MX_SAI2_Init();
MX_TIM1_Init();
MX_TIM5_Init();
MX_SDMMC2_SD_Init();
MX_TIM2_Init();
MX_CORDIC_Init();
MX_RTC_Init();
/* USER CODE BEGIN 2 */
MX_USB_HCD_Init();
printf("************** RTOS start ****************\r\n");
/* USER CODE END 2 */
MX_ThreadX_Init();
/* We should never get here as control is now taken by the scheduler */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
+
>If that's a C++ file then how to call it!? That's not a function to call.
Sure, you do your "while loop" there.
You just write your basic file , xxx.cpp , then import in IDE , (import->file system ->..) , just right click on the target directory you want it to be, core/src , then select there your xxx.cpp file; IDE "knows" then, its part of project and compiles and links it. (btw you start as a cpp project , or right click on project -> convert to c++ , might help :) )
Then your "my_cpp_prog()" is just a function , thats called as part of this project.
+ The optimizer will delete the unused/useless code after the branch to your cpp program, so its then :
a C++ program with an cpu-setup/init in C .