@@ -183,14 +183,11 @@ static obc_error_code_t initTca6424PinState() {
183183 return OBC_ERR_CODE_SUCCESS ;
184184}
185185
186- // function to get shunt voltage
187- // i2cAddress = ina230 device address
188- // shuntVoltage = pointer to store shuntVoltage
189-
190186/**
191187 * @brief Gets INA230 shunt voltage
192- *
193- * This function reads the value from the shunt voltage register of a device at the specificed I2C address. It converts the raw value to a signed voltage value in volts.
188+ *
189+ * Reads the 16-bit shunt voltage register (MSB first) from the specified INA230 device.
190+ * Converts the raw register value to a signed voltage in volts (LSB = 2.5μV).
194191 *
195192 * @param i2cAddress The I2C address of the INA230 device
196193 * @param shuntVoltage Pointer to store the shunt voltage in volts
@@ -240,20 +237,6 @@ obc_error_code_t getINA230ShuntVoltageForDevice(uint8_t deviceIndex, float* shun
240237 return getINA230ShuntVoltage (i2cAddress , shuntVoltage );
241238}
242239
243- void main_usage () {
244- float shuntVoltage = 0 ;
245-
246- // Call the function for INA230 device 1 (index 0)
247- obc_error_code_t errCode = getINA230ShuntVoltageForDevice (0 , & shuntVoltage );
248- // print shunt volatge
249- printf ("Shunt Voltage for INA230 Device 1: %f V\n" , shuntVoltage );
250-
251- if (errCode == OBC_ERR_CODE_SUCCESS ) {
252- printf ("Shunt Voltage for INA230 Device 1: %f V\n" , shuntVoltage );
253- } else {
254- printf ("Error reading shunt voltage for INA230 Device 1\n" );
255- }
256- }
257240
258241// general disable function for ina230 device
259242
@@ -273,20 +256,17 @@ obc_error_code_t disableNoAlert(ina230_device_t device) {
273256 return OBC_ERR_CODE_SUCCESS ;
274257}
275258
276- // Notes (delete later)
277- // Bus voltage register:
278- // Address: 0x02
279- // Size: 16 bits (2 bytes)
280- // Each bit on the register represents 1.25 mV
281-
282259/**
283- * @brief Gets the bus voltage
260+ * @brief Gets INA230 bus voltage
261+ *
262+ * Reads the 16-bit bus voltage register (MSB first) from the specified INA230 device.
263+ * Converts the raw register value to a signed voltage in volts (LSB = 1.25mV).
284264 *
285265 * @param i2cAddress The I2C address of the INA230 device
266+ * @param busVoltage Pointer to store the bus voltage in volts
286267 * @return OBC_ERR_CODE_SUCCESS if write is successful,
287268 * otherwise return an appropriate error code
288269 */
289- // function to get bus voltage
290270obc_error_code_t getINA230BusVoltage (uint8_t i2cAddress , float * busVoltage ) {
291271 if (busVoltage == NULL ) return OBC_ERR_CODE_INVALID_ARG ;
292272 bool found = false;
@@ -304,7 +284,7 @@ obc_error_code_t getINA230BusVoltage(uint8_t i2cAddress, float* busVoltage) {
304284 RETURN_IF_ERROR_CODE (
305285 i2cReadRegFuncPtr (i2cAddress , INA230_BUS_VOLTAGE_REGISTER_ADDR , busVoltageRaw , 2 , I2C_TRANSFER_TIMEOUT_TICKS ));
306286 uint16_t busVoltageValue = (busVoltageRaw [0 ] << 8 ) | busVoltageRaw [1 ];
307- * busVoltage = busVoltageValue * 0.00125f ;
287+ * busVoltage = busVoltageValue * INA230_BUS_VOLTAGE_LSB ;
308288
309289 return OBC_ERR_CODE_SUCCESS ;
310290}
@@ -316,10 +296,32 @@ obc_error_code_t getINA230BusVoltageForDevice(uint8_t deviceIndex, float* busVol
316296 return getINA230BusVoltage (i2cAddress , busVoltage );
317297}
318298
299+ /**
300+ * @brief Gets INA230 power
301+ *
302+ * Reads the 16-bit power register (MSB first) from the specified INA230 device.
303+ * Converts the raw register value to an unsigned power in watts (LSB = 25mW).
304+ *
305+ * @param i2cAddress The I2C address of the INA230 device
306+ * @param busVoltage Pointer to store the power in watts
307+ * @return OBC_ERR_CODE_SUCCESS if write is successful,
308+ * otherwise return an appropriate error code
309+ */
319310obc_error_code_t getINA230Power (uint8_t i2cAddress , float * power ) {
320311 obc_error_code_t errCode ;
321- if (power == NULL || (i2cAddress != INA230_I2C_ADDRESS_ONE && i2cAddress != INA230_I2C_ADDRESS_TWO ))
312+ if (power == NULL ) {
313+ return OBC_ERR_CODE_INVALID_ARG ;
314+ }
315+ bool found = false;
316+ for (uint8_t i = 0 ; i < INA230_DEVICE_COUNT ; ++ i ) {
317+ if (i2cAddress == ina230Devices [i ].i2cDeviceAddress ) {
318+ found = true;
319+ break ;
320+ }
321+ }
322+ if (!found ) {
322323 return OBC_ERR_CODE_INVALID_ARG ;
324+ }
323325 uint8_t powerRaw [INA_REG_CONF_BUFF_SIZE ] = {};
324326 RETURN_IF_ERROR_CODE (
325327 i2cReadRegFuncPtr (i2cAddress , INA230_POWER_REGISTER_ADDR , powerRaw , 2 , I2C_TRANSFER_TIMEOUT_TICKS ));
@@ -334,11 +336,32 @@ obc_error_code_t getINA230PowerForDevice(uint8_t deviceIndex, float* power) {
334336 return getINA230Power (i2cAddress , power );
335337}
336338
337- // function to get current
339+ /**
340+ * @brief Gets INA230 current
341+ *
342+ * Reads the 16-bit current register (MSB first) from the specified INA230 device.
343+ * Converts the raw register value to a signed current in watts (LSB = 1mA).
344+ *
345+ * @param i2cAddress The I2C address of the INA230 device
346+ * @param busVoltage Pointer to store the current in amperes
347+ * @return OBC_ERR_CODE_SUCCESS if write is successful,
348+ * otherwise return an appropriate error code
349+ */
338350obc_error_code_t getINA230Current (uint8_t i2cAddress , float * current ) {
339351 obc_error_code_t errCode ;
340- if (current == NULL || (i2cAddress != INA230_I2C_ADDRESS_ONE && i2cAddress != INA230_I2C_ADDRESS_TWO ))
352+ if (current == NULL ) {
353+ return OBC_ERR_CODE_INVALID_ARG ;
354+ }
355+ bool found = false;
356+ for (uint8_t i = 0 ; i < INA230_DEVICE_COUNT ; ++ i ) {
357+ if (i2cAddress == ina230Devices [i ].i2cDeviceAddress ) {
358+ found = true;
359+ break ;
360+ }
361+ }
362+ if (!found ) {
341363 return OBC_ERR_CODE_INVALID_ARG ;
364+ }
342365 uint8_t currentRaw [INA_REG_CONF_BUFF_SIZE ] = {};
343366 RETURN_IF_ERROR_CODE (
344367 i2cReadRegFuncPtr (i2cAddress , INA230_CURRENT_REGISTER_ADDR , currentRaw , 2 , I2C_TRANSFER_TIMEOUT_TICKS ));
@@ -353,4 +376,3 @@ obc_error_code_t getINA230CurrentForDevice(uint8_t deviceIndex, float* current)
353376 return getINA230Current (i2cAddress , current );
354377}
355378
356- // allow loop through
0 commit comments