@@ -51,13 +51,36 @@ typedef struct {
5151
5252static const ina230_config_t ina230Devices [] = {[INA230_DEVICE_ONE ] = {.i2cDeviceAddress = INA230_I2C_ADDRESS_ONE ,
5353 .tcaAlertPort = INA230_ONE_ALERT_PIN ,
54- .tcaEnablePort = INA230_ONE_ENABLE_PIN },
55-
54+ .tcaEnablePort = INA230_ONE_ENABLE_PIN ,
55+ .configurationMode = INA230_DEFAULT_MODE ,
56+ .configurationShunt = INA230_DEFAULT_SHUNT_CONV_TIME ,
57+ .configurationBus = INA230_DEFAULT_BUS_CONV_TIME ,
58+ .configurationAvg = INA230_DEFAULT_AVERAGING ,
59+ .calibrationRegister = INA230_CALIBRATION_VALUE ,
60+ .maskEnableRegister = INA230_MASK_ENABLE_NONE ,
61+ .alertRegister = INA230_ALERT_LIMIT_NONE },
5662 [INA230_DEVICE_TWO ] = {.i2cDeviceAddress = INA230_I2C_ADDRESS_TWO ,
5763 .tcaAlertPort = INA230_TWO_ALERT_PIN ,
58- .tcaEnablePort = INA230_TWO_ENABLE_PIN }};
59-
60- static obc_error_code_t writeINA230Register (uint8_t regAddress , uint8_t * data , uint8_t size , uint8_t i2cAddress );
64+ .tcaEnablePort = INA230_TWO_ENABLE_PIN ,
65+ .configurationMode = INA230_DEFAULT_MODE ,
66+ .configurationShunt = INA230_DEFAULT_SHUNT_CONV_TIME ,
67+ .configurationBus = INA230_DEFAULT_BUS_CONV_TIME ,
68+ .configurationAvg = INA230_DEFAULT_AVERAGING , .calibrationRegister = INA230_CALIBRATION_VALUE ,
69+ .maskEnableRegister = INA230_MASK_ENABLE_NONE ,
70+ .alertRegister = INA230_ALERT_LIMIT_NONE },
71+ // initialized only 2 devices for now; initialize the rest if used
72+ [INA230_DEVICE_THREE ] = {0 },
73+ [INA230_DEVICE_FOUR ] = {0 },
74+ [INA230_DEVICE_FIVE ] = {0 },
75+ [INA230_DEVICE_SIX ] = {0 },
76+ [INA230_DEVICE_SEVEN ] = {0 },
77+ [INA230_DEVICE_EIGHT ] = {0 },
78+ [INA230_DEVICE_NINE ] = {0 },
79+ [INA230_DEVICE_TEN ] = {0 },
80+ [INA230_DEVICE_ELEVEN ] = {0 }
81+ };
82+
83+ static obc_error_code_t writeINA230Register (uint8_t regAddress , uint8_t * data , uint8_t size , ina230_device_t device );
6184static obc_error_code_t initTca6424PinState ();
6285
6386/**
@@ -90,19 +113,16 @@ obc_error_code_t initINA230() {
90113 uint8_t deviceAddress = device .i2cDeviceAddress ;
91114 RETURN_IF_ERROR_CODE (writeINA230Register (INA230_CONFIG_REGISTER_ADDR , configRegisterUnpacked ,
92115 sizeof (configRegisterUnpacked ) / sizeof (configRegisterUnpacked [0 ]),
93- deviceAddress ));
116+ i ));
94117 RETURN_IF_ERROR_CODE (writeINA230Register (INA230_MASK_ENABLE_REGISTER_ADDR , maskEnRegisterUnpacked ,
95118 sizeof (maskEnRegisterUnpacked ) / sizeof (maskEnRegisterUnpacked [0 ]),
96- deviceAddress ));
119+ i ));
97120 RETURN_IF_ERROR_CODE (writeINA230Register (INA230_ALERT_LIMIT_REGISTER_ADDR , alertRegisterUnpacked ,
98121 sizeof (alertRegisterUnpacked ) / sizeof (alertRegisterUnpacked [0 ]),
99- deviceAddress ));
100- // uint16_t calibrationValue = INA230_CALIBRATION_VALUE;
101- // uint8_t calibrationRegisterUnpacked[] = {calibrationValue & 0xFF, calibrationValue >> 8};
102-
122+ i ));
103123 RETURN_IF_ERROR_CODE (writeINA230Register (
104124 INA230_CALIBRATION_REGISTER_ADDR , calibrationRegisterUnpacked ,
105- sizeof (calibrationRegisterUnpacked ) / sizeof (calibrationRegisterUnpacked [0 ]), deviceAddress ));
125+ sizeof (calibrationRegisterUnpacked ) / sizeof (calibrationRegisterUnpacked [0 ]), i ));
106126 }
107127
108128 RETURN_IF_ERROR_CODE (initTca6424PinState ());
@@ -115,7 +135,7 @@ obc_error_code_t initINA230() {
115135 * This function reads the complete input from the TCA6424 and checks for alerts
116136 * on the INA230 devices. If an alert is detected, it disables the corresponding device.
117137 *
118- * @param device The INA230 device to check
138+ * @param device INA230 device enum
119139 * @return OBC_ERR_CODE_SUCCESS if operation is successful,
120140 * otherwise returns an appropriate error code
121141 */
@@ -139,25 +159,17 @@ inline obc_error_code_t readAndDisableIfAlert(ina230_device_t device) {
139159 * @param regAddress The address of the register to write to
140160 * @param data Pointer to the data to be written
141161 * @param size The size of the data to be written
142- * @param i2cAddress The I2C address of the INA230 device
162+ * @param device INA230 device index
143163 * @return OBC_ERR_CODE_SUCCESS if write is successful,
144164 * otherwise returns an appropriate error code
145165 */
146166
147- static obc_error_code_t writeINA230Register (uint8_t regAddress , uint8_t * data , uint8_t size , uint8_t i2cAddress ) {
148- if (data == NULL ) return OBC_ERR_CODE_INVALID_ARG ;
149- bool found = false;
150- for (uint8_t i = 0 ; i < INA230_DEVICE_COUNT ; ++ i ) {
151- if (i2cAddress == ina230Devices [i ].i2cDeviceAddress ) {
152- found = true;
153- break ;
154- }
155- }
156- if (!found ) {
167+ static obc_error_code_t writeINA230Register (uint8_t regAddress , uint8_t * data , uint8_t size , ina230_device_t device ) {
168+ if (data == NULL || device >= INA230_DEVICE_COUNT ) {
157169 return OBC_ERR_CODE_INVALID_ARG ;
158170 }
159171 obc_error_code_t errCode ;
160- RETURN_IF_ERROR_CODE (i2cWriteRegFuncPtr (i2cAddress , regAddress , data , size ));
172+ RETURN_IF_ERROR_CODE (i2cWriteRegFuncPtr (ina230Devices [ device ]. i2cDeviceAddress , regAddress , data , size ));
161173 return OBC_ERR_CODE_SUCCESS ;
162174}
163175/**
@@ -189,30 +201,22 @@ static obc_error_code_t initTca6424PinState() {
189201 * Reads the 16-bit shunt voltage register (MSB first) from the specified INA230 device.
190202 * Converts the raw register value to a signed voltage in volts (LSB = 2.5μV).
191203 *
192- * @param i2cAddress The I2C address of the INA230 device
204+ * @param device INA230 device enum
193205 * @param shuntVoltage Pointer to store the shunt voltage in volts
194206 * @return OBC_ERR_CODE_SUCCESS if operation is successful,
195207 * otherwise returns an appropriate error code
196208 */
197- obc_error_code_t getINA230ShuntVoltage (uint8_t i2cAddress , float * shuntVoltage ) {
198- if (shuntVoltage == NULL ) return OBC_ERR_CODE_INVALID_ARG ;
209+ obc_error_code_t getINA230ShuntVoltage (ina230_device_t device , float * shuntVoltage ) {
199210
200- uint8_t shuntVoltageRaw [INA_REG_CONF_BUFF_SIZE ] = {0 }; // store 2 bytes of shunt voltage
201- obc_error_code_t errCode ;
202-
203- bool found = false;
204- for (uint8_t i = 0 ; i < INA230_DEVICE_COUNT ; ++ i ) {
205- if (i2cAddress == ina230Devices [i ].i2cDeviceAddress ) {
206- found = true;
207- break ;
208- }
209- }
210- if (!found ) {
211+ if (shuntVoltage == NULL || device >= INA230_DEVICE_COUNT ) {
211212 return OBC_ERR_CODE_INVALID_ARG ;
212213 }
213214
215+ uint8_t shuntVoltageRaw [INA_REG_CONF_BUFF_SIZE ] = {0 }; // store 2 bytes of shunt voltage
216+ obc_error_code_t errCode ;
217+
214218 // Read the 16-bit shunt voltage register
215- errCode = i2cReadRegFuncPtr (i2cAddress , INA230_SHUNT_VOLTAGE_REGISTER_ADDR , shuntVoltageRaw , 2 ,
219+ errCode = i2cReadRegFuncPtr (ina230Devices [ device ]. i2cDeviceAddress , INA230_SHUNT_VOLTAGE_REGISTER_ADDR , shuntVoltageRaw , 2 ,
216220 I2C_TRANSFER_TIMEOUT_TICKS ); // last param not sure
217221 if (errCode != OBC_ERR_CODE_SUCCESS ) return errCode ;
218222
@@ -224,18 +228,7 @@ obc_error_code_t getINA230ShuntVoltage(uint8_t i2cAddress, float* shuntVoltage)
224228
225229 return OBC_ERR_CODE_SUCCESS ;
226230}
227-
228- obc_error_code_t getINA230ShuntVoltageForDevice (uint8_t deviceIndex , float * shuntVoltage ) {
229- if (shuntVoltage == NULL ) return OBC_ERR_CODE_INVALID_ARG ;
230-
231- if (deviceIndex >= INA230_DEVICE_COUNT ) return OBC_ERR_CODE_INVALID_ARG ; // Check valid device index
232-
233- // Retrieve the I2C address for the specified INA230 device
234- uint8_t i2cAddress = ina230Devices [deviceIndex ].i2cDeviceAddress ;
235-
236- // Get the shunt voltage
237- return getINA230ShuntVoltage (i2cAddress , shuntVoltage );
238- }
231+
239232
240233
241234// general disable function for ina230 device
@@ -245,7 +238,7 @@ obc_error_code_t disableNoAlert(ina230_device_t device) {
245238 obc_error_code_t errCode ;
246239
247240 for (uint8_t i = 0 ; i < INA230_DEVICE_COUNT ; ++ i ) {
248- uint8_t pinLocation =
241+ uint8_t pinLocation =
249242 ina230Devices [i ].tcaEnablePort ; // specific pin on TCA that this ina230 controls, should this be alertPort?
250243 uint8_t index = ((pinLocation & 0x0F ) +
251244 ((pinLocation >> 1 ) & 0x18 )); // converts the pinLocation to an index in the 24 bit IOPortValue
@@ -262,117 +255,71 @@ obc_error_code_t disableNoAlert(ina230_device_t device) {
262255 * Reads the 16-bit bus voltage register (MSB first) from the specified INA230 device.
263256 * Converts the raw register value to a signed voltage in volts (LSB = 1.25mV).
264257 *
265- * @param i2cAddress The I2C address of the INA230 device
258+ * @param device INA230 device enum
266259 * @param busVoltage Pointer to store the bus voltage in volts
267260 * @return OBC_ERR_CODE_SUCCESS if write is successful,
268261 * otherwise return an appropriate error code
269262 */
270- obc_error_code_t getINA230BusVoltage (uint8_t i2cAddress , float * busVoltage ) {
271- if (busVoltage == NULL ) return OBC_ERR_CODE_INVALID_ARG ;
272- bool found = false;
273- for (uint8_t i = 0 ; i < INA230_DEVICE_COUNT ; ++ i ) {
274- if (i2cAddress == ina230Devices [i ].i2cDeviceAddress ) {
275- found = true;
276- break ;
277- }
278- }
279- if (!found ) {
263+ obc_error_code_t getINA230BusVoltage (ina230_device_t device , float * busVoltage ) {
264+ if (busVoltage == NULL || device >= INA230_DEVICE_COUNT ) {
280265 return OBC_ERR_CODE_INVALID_ARG ;
281266 }
282267 obc_error_code_t errCode ;
283268 uint8_t busVoltageRaw [2 ] = {};
284269 RETURN_IF_ERROR_CODE (
285- i2cReadRegFuncPtr (i2cAddress , INA230_BUS_VOLTAGE_REGISTER_ADDR , busVoltageRaw , 2 , I2C_TRANSFER_TIMEOUT_TICKS ));
270+ i2cReadRegFuncPtr (ina230Devices [ device ]. i2cDeviceAddress , INA230_BUS_VOLTAGE_REGISTER_ADDR , busVoltageRaw , 2 , I2C_TRANSFER_TIMEOUT_TICKS ));
286271 uint16_t busVoltageValue = (busVoltageRaw [0 ] << 8 ) | busVoltageRaw [1 ];
287272 * busVoltage = busVoltageValue * INA230_BUS_VOLTAGE_LSB ;
288273
289274 return OBC_ERR_CODE_SUCCESS ;
290275}
291276
292277
293- obc_error_code_t getINA230BusVoltageForDevice (uint8_t deviceIndex , float * busVoltage ) {
294- if (busVoltage == NULL || deviceIndex >= INA230_DEVICE_COUNT ) return OBC_ERR_CODE_INVALID_ARG ;
295- uint8_t i2cAddress = ina230Devices [deviceIndex ].i2cDeviceAddress ;
296- return getINA230BusVoltage (i2cAddress , busVoltage );
297- }
298-
299278/**
300279 * @brief Gets INA230 power
301280 *
302281 * Reads the 16-bit power register (MSB first) from the specified INA230 device.
303282 * Converts the raw register value to an unsigned power in watts (LSB = 25mW).
304283 *
305- * @param i2cAddress The I2C address of the INA230 device
284+ * @param ina230_device_t device enum
306285 * @param busVoltage Pointer to store the power in watts
307286 * @return OBC_ERR_CODE_SUCCESS if write is successful,
308287 * otherwise return an appropriate error code
309288 */
310- obc_error_code_t getINA230Power (uint8_t i2cAddress , float * power ) {
289+ obc_error_code_t getINA230Power (ina230_device_t device , float * power ) {
311290 obc_error_code_t errCode ;
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 ) {
291+ if (power == NULL || device >= INA230_DEVICE_COUNT ) {
323292 return OBC_ERR_CODE_INVALID_ARG ;
324293 }
325294 uint8_t powerRaw [INA_REG_CONF_BUFF_SIZE ] = {};
326295 RETURN_IF_ERROR_CODE (
327- i2cReadRegFuncPtr (i2cAddress , INA230_POWER_REGISTER_ADDR , powerRaw , 2 , I2C_TRANSFER_TIMEOUT_TICKS ));
296+ i2cReadRegFuncPtr (ina230Devices [ device ]. i2cDeviceAddress , INA230_POWER_REGISTER_ADDR , powerRaw , 2 , I2C_TRANSFER_TIMEOUT_TICKS ));
328297 uint16_t powerValue = (powerRaw [0 ] << 8 ) | powerRaw [1 ];
329298 * power = powerValue * (INA230_CURRENT_LSB * INA230_POWER_LSB_MULTIPLIER );
330299 return OBC_ERR_CODE_SUCCESS ;
331300}
332301
333- obc_error_code_t getINA230PowerForDevice (uint8_t deviceIndex , float * power ) {
334- if (power == NULL || deviceIndex >= INA230_DEVICE_COUNT ) return OBC_ERR_CODE_INVALID_ARG ;
335- uint8_t i2cAddress = ina230Devices [deviceIndex ].i2cDeviceAddress ;
336- return getINA230Power (i2cAddress , power );
337- }
338-
339302/**
340303 * @brief Gets INA230 current
341304 *
342305 * Reads the 16-bit current register (MSB first) from the specified INA230 device.
343306 * Converts the raw register value to a signed current in watts (LSB = 1mA).
344307 *
345- * @param i2cAddress The I2C address of the INA230 device
308+ * @param device INA230 device enum
346309 * @param busVoltage Pointer to store the current in amperes
347310 * @return OBC_ERR_CODE_SUCCESS if write is successful,
348311 * otherwise return an appropriate error code
349312 */
350- obc_error_code_t getINA230Current (uint8_t i2cAddress , float * current ) {
313+ obc_error_code_t getINA230Current (ina230_device_t device , float * current ) {
351314 obc_error_code_t errCode ;
352- if (current == NULL ) {
315+ if (current == NULL || device >= INA230_DEVICE_COUNT ) {
353316 return OBC_ERR_CODE_INVALID_ARG ;
354317 }
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 ) {
363- return OBC_ERR_CODE_INVALID_ARG ;
364- }
318+
365319 uint8_t currentRaw [INA_REG_CONF_BUFF_SIZE ] = {};
366320 RETURN_IF_ERROR_CODE (
367- i2cReadRegFuncPtr (i2cAddress , INA230_CURRENT_REGISTER_ADDR , currentRaw , 2 , I2C_TRANSFER_TIMEOUT_TICKS ));
321+ i2cReadRegFuncPtr (ina230Devices [ device ]. i2cDeviceAddress , INA230_CURRENT_REGISTER_ADDR , currentRaw , 2 , I2C_TRANSFER_TIMEOUT_TICKS ));
368322 int16_t currentValue = (currentRaw [0 ] << 8 ) | currentRaw [1 ];
369323 * current = currentValue * INA230_CURRENT_LSB ;
370324 return OBC_ERR_CODE_SUCCESS ;
371- }
372-
373- obc_error_code_t getINA230CurrentForDevice (uint8_t deviceIndex , float * current ) {
374- if (current == NULL || deviceIndex >= INA230_DEVICE_COUNT ) return OBC_ERR_CODE_INVALID_ARG ;
375- uint8_t i2cAddress = ina230Devices [deviceIndex ].i2cDeviceAddress ;
376- return getINA230Current (i2cAddress , current );
377- }
378-
325+ }
0 commit comments