cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with initialising I2C bus in different files

SLuit.1
Associate III

Hi,

I am working with STM32H747-Disco and I am currently working with I2C bus. My I2C bus is working perfectly fine and I can see the communication. I just have main.c file and everything is there.

I am planning to have different file for the initialisation and I will just call the function from main.c file.

I defined handletypedef in main.c file and passed it as a prameter to another file and I2C stopped working. The code is below:

I2C_HandleTypeDef hi2c4 ;

I have called initialise from main.c passing hi2c4 as a parameter:

Initialise_I2C4(hi2c4);

I have defined initialise function in another file copying the working code from main.c file which is below:

void MX_I2C4_Init(I2C_HandleTypeDef i2c4){

i2c4.Instance       = I2C4;

i2c4.Mode         = HAL_I2C_MODE_MASTER;

i2c4.Init.Timing      = 0x10707DBC;        //I2C frequency at 100 KHz. Value generated from CubeMX

i2c4.Init.AddressingMode  = I2C_ADDRESSINGMODE_7BIT;

i2c4.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;  //Needed only in I2C slave features

i2c4.Init.OwnAddress2Masks = I2C_OA2_NOMASK;

i2c4.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;

i2c4.Init.NoStretchMode  = I2C_NOSTRETCH_DISABLE;

//hi2c4.Init.OwnAddress1   = 0;      //Needed only in I2C slave features

//hi2c4.Init.OwnAddress2   = 0;          //Needed only in I2C slave features

HAL_I2C_Init(&i2c4);

/* if(HAL_I2C_Init(&I2C) == HAL_OK)

{

return 0;

}*/

}

IF I put everything in other file, that also works fine but if I call the function from main.c and pass the typedef as parameter, it does not work.

Could you please help me out with this as I am new to STM32 and I need to add multiple drivers and busses in the prject. I want to keep everthing in other files and call the functions only from main.

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

> void MX_I2C4_Init(I2C_HandleTypeDef i2c4){

You should be passing a pointer and not by value.

void MX_I2C4_Init(I2C_HandleTypeDef * hi2c) {

CubeMX will separate peripheral initialization if you select Project Manager -> Code Generator -> Generate peripheral initialization as a pair of...

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

3 REPLIES 3
SLuit.1
Associate III

sorry the function is

void Initialise_I2C4(I2C_HandleTypeDef i2c4){

i2c4.Instance       = I2C4;

i2c4.Mode         = HAL_I2C_MODE_MASTER;

i2c4.Init.Timing      = 0x10707DBC;        //I2C frequency at 100 KHz. Value generated from CubeMX

i2c4.Init.AddressingMode  = I2C_ADDRESSINGMODE_7BIT;

i2c4.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;  //Needed only in I2C slave features

i2c4.Init.OwnAddress2Masks = I2C_OA2_NOMASK;

i2c4.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;

i2c4.Init.NoStretchMode  = I2C_NOSTRETCH_DISABLE;

//hi2c4.Init.OwnAddress1   = 0;      //Needed only in I2C slave features

//hi2c4.Init.OwnAddress2   = 0;          //Needed only in I2C slave features

HAL_I2C_Init(&i2c4);

/* if(HAL_I2C_Init(&I2C) == HAL_OK)

{

return 0;

}*/

}

But not void MX_I2C4_Init(I2C_HandleTypeDef i2c4). I made a mistake while copying. I do not know where the problem is.

TDK
Guru

> void MX_I2C4_Init(I2C_HandleTypeDef i2c4){

You should be passing a pointer and not by value.

void MX_I2C4_Init(I2C_HandleTypeDef * hi2c) {

CubeMX will separate peripheral initialization if you select Project Manager -> Code Generator -> Generate peripheral initialization as a pair of...

If you feel a post has answered your question, please click "Accept as Solution".
SLuit.1
Associate III

Thank you. That was the problem. Now I can pass handletypedef as variable and I don't need to have everything in one file.