Driver Development with STM32
To test my STM32 breakout board, I used it with an I2C temperature sensor, LM75B by NXP, and wrote a driver for it in C with ST’s HAL.
The driver contains low-level functions that read/write from device registers as well high-level functions that let the user set/read temperatures directly.
I particularly enjoyed reading the datasheet and programming at low-level which involved interacting with hardware.
Code summary:
A struct is used to hold configuration data and temperature reading. The sensor sends a warning signal if T exceeds Tos, and then stops the warning when T drops back below Thyst.
The ReadRegister and WriteRegister functions wrap the I2C communication functions for ease of use.
The functions LM75B_ReadTemperature, LM75B_WriteTos and LM75B_WriteThyst write to or read from the relevant registers.
When using the driver, initialise the relevant ports in STM32CubeMX and initialise the struct. Then simply use the high-level functions to achieve the desired goals.