2024-04-03 06:09 AM
Hi all, I am fairly new in using STM32CubeIDE. I am working with three peripherals (uart1, uart2 and CAN) as also a timer interrupt in the STM32F103C8. I have written all the code in the main.c file. It is working but the coding is a bit jumbled up. For better readability, I want to put different functions of the three peripherals and the timer in different source files and call them from the main function. I am adding new c source files in the src folder but I have trouble in accessing the functions placed in these files from the main.c file. Can anybody please guide me in this matter?
Solved! Go to Solution.
2024-04-03 06:58 AM - edited 2024-09-21 01:54 AM
As @SofLit said, you use header files (.h) to provide the declarations of the things you want to share.
This is standard C stuff - nothing specific or special to CubeIDE:
https://c-faq.com/decl/decldef.html
eg, file.h provides declarations:
extern int variable1;
extern char variable1;
extern float variable1;
void function_1( void );
file.c provides definitions (or "implementations"):
int variable1;
char variable2;
float variable3;
void function_1( void )
{
// your code here
}
Now, for example, your main.c can access things from file.c by #including file.h:
#include "file.h"
int main( void )
{
int local_variable1 = 2 * variable1; // use the variables from file.c
char local_variable2 = 3 + variable2;
float local_variable3 = 2.3 / variable1;
function_1(); // call the function from file.c
:
:
Here is a pictorial summary:
#DeclDef
2024-04-03 06:32 AM - edited 2024-04-03 06:35 AM
2024-04-03 06:35 AM - edited 2024-04-03 06:36 AM
You need to export these functions in .h files.
Then include these .h files in your main.c.
2024-04-03 06:38 AM
Dear SofLit,
Can you please share some example codes or some reading material?
2024-04-03 06:45 AM - edited 2024-04-03 06:46 AM
int sommm(int a, int b){
return(a+b);
}
#ifndef INC_SOM_H_
#define INC_SOM_H_
int sommm(int,int);
#endif /* INC_SOM_H_ */
#include "som.h"
2024-04-03 06:48 AM
One of the examples you can refer to is BSP example in the STM32CubeF1 from here.
If you look at the main.h file already included in main.c, there are many .h files related to the BSP source code:
#include "stm32f1xx_hal.h"
#include "stm3210e_eval.h"
#include "stm3210e_eval_lcd.h"
#include "stm3210e_eval_sd.h"
#include "stm3210e_eval_audio.h"
#include "stm3210e_eval_nor.h"
#include "stm3210e_eval_sram.h"
#include "stm3210e_eval_nand.h"
#include "stm3210e_eval_serialflash.h"
#include "stm3210e_eval_tsensor.h"
The source files are located here.
So the APIs defined in .c files are exported in .h files which in turn included in main.h which finally included in main.c. So all the functions defined in these drives will be seen in main.c file.
Don't forget to add the include path(s) in your IDE.
2024-04-03 06:58 AM - edited 2024-09-21 01:54 AM
As @SofLit said, you use header files (.h) to provide the declarations of the things you want to share.
This is standard C stuff - nothing specific or special to CubeIDE:
https://c-faq.com/decl/decldef.html
eg, file.h provides declarations:
extern int variable1;
extern char variable1;
extern float variable1;
void function_1( void );
file.c provides definitions (or "implementations"):
int variable1;
char variable2;
float variable3;
void function_1( void )
{
// your code here
}
Now, for example, your main.c can access things from file.c by #including file.h:
#include "file.h"
int main( void )
{
int local_variable1 = 2 * variable1; // use the variables from file.c
char local_variable2 = 3 + variable2;
float local_variable3 = 2.3 / variable1;
function_1(); // call the function from file.c
:
:
Here is a pictorial summary:
#DeclDef
2024-04-04 10:37 PM
Thanks everybody. As @Andrew Neil said, it is standard C stuff and I managed to do the coding.
2024-04-05 12:25 AM - edited 2024-04-22 03:00 AM
You're welcome.
One thing I omitted to mention is that it's good to also #include your file.h header in your file.c source file.
So, in file.c:
#include "file.h" // contains the declarations
// here are the definitions:
int variable1;
char variable2;
float variable3;
void function_1( void )
{
// your code here
}
This means that the Compiler, when it compiles file.c, gets to see both the declaration in file.h and the definitions in file.c - and, thus, can warn you if there's any discrepancy.
2024-04-05 04:22 AM
Yeah, I am already doing this.