2022-04-09 01:24 PM
I'm working on test code for a bare metal STM32G0C1 project. I'm using STM32CubeIDE 1.9.0 and created the project with the included STMCubeMX tool.
When digging around in the generated code to understand I2C I found that the function HAL_I2C_MspInit(hi2c) is only weakly defined in stm32g0xx_hal_i2c.c. For other peripherals, such as UARTs, there's a HAL_***_MspInit() where the GPIO are configured. Even supplied sample projects have a HAL_I2C_MspInit(). Instead I find this heart stopping note in stm32g0xx_hal_i2c.c:
"(+) User must Implement HAL_I2C_MspInit() function in which he configures
all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC )."
Whoa horse!! What's up with burying such an important little comment deep in the generated source code (that I'm not suppose to muck with and assume is good to go right out of the chute)?
I hope you can tell me the error of my ways. Otherwise, I'm going to assign the dreaded "b" word (as in insect) to this tidy little underhanded toss.
My test code, by the way, didn't work. I'm pretty sure I now know why....
Thanks for any comments!!
doug
Solved! Go to Solution.
2022-04-09 02:31 PM
Ah, ha! I found it! You can stand down.
HAL_I2C_MspInit() is defined. It is in Core/Src/stm32g0xx_hal_msp.c. The brute-force searches I was doing earlier seem to only show the Drivers/STM32G0XX_HAL_Driver/Src/STM32g0xx_hal_i2c.c. That's where the __weak, do-nothing function is defined. Both of those MspInit and MspDeInit have user code spaces where I can handle defining the necessary pins.
My apologies. Clearly I need to take a break from the keyboard for a while....
2022-04-09 01:30 PM
This function should be created by CubeMX/IDE in one of generated files.
Please make sure that one or more I2C controllers is configured in your CubeMX/IDE project (,ioc) and re-generate the code.
Note: the HAL (or LL) drivers are NOT "generated code". They are copied into your project verbatim from the Cube repository. Or they can be used by reference in the repository instead of being copied.
2022-04-09 01:51 PM
I've already got I2C2 defined, pins allocated, and all that via the embedded STMCubeMX. I discovered all of the above because I've got to switch pin definitions at run time. I need to temporarily define I2C1 as alternate functions on PA10 / PA9. It's when trying to figure out how to do that (which will require modification to HAL_I2C_MspInit()) that I discovered HAL_I2C_MspInit() is delinquent from my STM32CubeMX generated project. All I've got, and I've re-built the project many times, is the kindly note not to forget to add my own HAL_I2C_MspInit(). I check some earlier test I2C code, that just happen to have failed to identify an I2C device, and that project has the same same status as the one I'm currently working on: HAL_I2C_MspInit() is MIA. I'm going to have to write my own. I'm okay with that. I just find it cheap to have to STM32CubeMX not give me a little warning.
Say... STMCubeMX was upgraded along with STM32CubeIDE 1.9.0. Every time I fiddle with an old project I get a "do you want to migrate your .ioc file?" message. Is it possible something's gotten lost in translation?
2022-04-09 02:14 PM
> Is it possible something's gotten lost in translation?
Yes, things happen. Can you attach your ioc file?
2022-04-09 02:31 PM
Ah, ha! I found it! You can stand down.
HAL_I2C_MspInit() is defined. It is in Core/Src/stm32g0xx_hal_msp.c. The brute-force searches I was doing earlier seem to only show the Drivers/STM32G0XX_HAL_Driver/Src/STM32g0xx_hal_i2c.c. That's where the __weak, do-nothing function is defined. Both of those MspInit and MspDeInit have user code spaces where I can handle defining the necessary pins.
My apologies. Clearly I need to take a break from the keyboard for a while....
2022-04-10 02:47 PM
Search -> File... can be used to find all instances of a text string in the project, or workspace, or directory etc.