/** * @file BSP_INA.c * @brief Driver for the BSP INA219 module * * This file provides a driver for the BSP INA219 module, which is used to measure voltage and current values. * The driver includes functions for initializing the module, reading the current and voltage values, and calculating the power value. * All of these functions are wrapped in a mutex lock to ensure consistent reading of the values. */ #include "BSP_INA.h" #include "i2c.h" #include "INA219.h" #include "cmsis_os2.h" #define BSP_INA_I2C &hi2c1 #define BSP_INA_ADDRESS INA219_ADDRESS | 0x05 #define BSP_INA_MUTEX_TIMEOUT osWaitForever static INA219_t hina = {0}; static osMutexId_t ina_lock = NULL; /** * @brief Initializes the INA219 module and sets up the I2C bus. */ void BSP_INA_Init(void) { INA219_Init(&hina, BSP_INA_I2C, BSP_INA_ADDRESS); ina_lock = osMutexNew(NULL); // default done by Init // INA219_Reset(ina219); // INA219_setCalibration_32V_2A(&hina); } /** * @brief Reads the voltage value from the INA219. * * This function acquires a mutex to ensure that the voltage value is read consistently. * The mutex is released after the value has been read. * * @return The voltage value in milli Volts (mV) */ uint16_t BSP_INA_Voltage() { osMutexAcquire(ina_lock,BSP_INA_MUTEX_TIMEOUT); uint16_t result = INA219_ReadBusVoltage(&hina); osMutexRelease(ina_lock); return result; } /** * @brief Reads the current value from the INA219. * * This function acquires a mutex to ensure that the current value is read consistently. * The mutex is released after the value has been read. * * @return The current value in 10 milli Amperes (10mA) */ uint16_t BSP_INA_Current() { osMutexAcquire(ina_lock,BSP_INA_MUTEX_TIMEOUT); uint16_t result = INA219_ReadCurrent(&hina); osMutexRelease(ina_lock); return result; } /** * @brief Reads the power value from the INA219. * * This function acquires a mutex to ensure that the power value is read consistently. * The mutex is released after the value has been read. * * @return The power value in micro Watts (uW) */ uint32_t BSP_INA_Power() { osMutexAcquire(ina_lock,BSP_INA_MUTEX_TIMEOUT); uint16_t result_current = INA219_ReadCurrent(&hina); uint16_t result_voltage = INA219_ReadBusVoltage(&hina); osMutexRelease(ina_lock); return (result_current*10) * result_voltage; }