cancel
Showing results for 
Search instead for 
Did you mean: 

[SOLVED FOR NOW] HAL I2C and HAL SPI not initialising correctly when using a custom static library that houses HAL Library

JNoon.1
Associate III

Hi,

I have multiple projects that use the same code but use different processors (STM32F4xx). To try and solve the common code problem I have created a static library, this has the HAL driver code inside and the main applications for each processor link to this library.

Now for some reason I2C and SPI do not initialise correctly. For example I2C, it will get into the HAL_I2C_Init function, but when it tries to set the struct attribute 'instance' it always gets set to 0. The value that it should be set to is 788 but it never works. I have tried to set it when in debug mode but it always goes back to 0.

I know this code work fine as before I separated out the code into a library, it worked fine with no problems when it was all in one project. The struct 'instance' attribute would be set correctly.

Has anyone had any success in separating out the HAL library into a static library? and have I2C or SPI etc working?

1 ACCEPTED SOLUTION

Accepted Solutions

Update 2:

Problem solved. Not in the best way but it is solved. It was simply moving the stm32f4xx_hal_msp.c file form the library into the main project, for some reason in the HAL_I2C_Init function it couldn't see the overridden function in stm32f4xx_hal_msp.c of HAL_I2C_MspInit().

It would be nice if there was more support for multi processor libraries or projects. The way I have done it works and has no problems there are just some "gotchas" or manual steps needed, especially if the code is regenerated. If there is more support from STM to allow this in STM32CubeIDE it would make things much easier for managing multiple projects with different hardware in the same family of processors.

View solution in original post

4 REPLIES 4
TDK
Guru

> Now for some reason I2C and SPI do not initialize correctly. For example I2C, it will get into the HAL_I2C_Init function, but when it tries to set the struct attribute 'instance' it always gets set to 0. The value that it should be set to is 788 but it never works. I have tried to set it when in debug mode but it always goes back to 0.

The Instance attribute should be set prior to calling HAL_I2C_Init. If you use CubeMX generated code, it is set within MX_I2C_Init. Why should it be 788?

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

Hi,

It does get set before hand and passed through into the HAL_I2C_Init function. Apologies that is the wrong number I got confused with something else.

This code does work and everything gets initialised correctly, it's only since I created a static library with the HAL Drivers in that SPI and I2C seem to not initialise properly.

I step through HAL_I2C_Init and when it tries to assign values, E.G.

hi2c->Instance->TIMINGR = hi2c->Init.Timing & TIMING_CLEAR_MASK;

This never gets set and stays as 0. When I have everything inside one project, this gets set successfully along with everything else.

JNoon.1
Associate III

Update:

After stepping through the debugger again, when it went through HAL_I2C_Init it got the function call HAL_I2C_MspInit(hi2c). Now this actually calls the weak version in stm32l4xx_hal_i2c.c and not the user defined version in stm32l4xx_hal_msp.c. And as it doesn't do this it never enables the clock and initialise the correct pins for I2C.

Any thoughts on how to force it to go to the user defined version at all?

Update 2:

Problem solved. Not in the best way but it is solved. It was simply moving the stm32f4xx_hal_msp.c file form the library into the main project, for some reason in the HAL_I2C_Init function it couldn't see the overridden function in stm32f4xx_hal_msp.c of HAL_I2C_MspInit().

It would be nice if there was more support for multi processor libraries or projects. The way I have done it works and has no problems there are just some "gotchas" or manual steps needed, especially if the code is regenerated. If there is more support from STM to allow this in STM32CubeIDE it would make things much easier for managing multiple projects with different hardware in the same family of processors.