Skip to content

Commit aa152ab

Browse files
committed
Remove function pointers
1 parent 2e2a645 commit aa152ab

File tree

2 files changed

+13
-44
lines changed

2 files changed

+13
-44
lines changed

obc/app/drivers/ina230/ina230.c

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,9 @@
1919
#define INA230_DISABLE_LOAD TCA6424A_GPIO_LOW
2020
#define INA230_ALERT_HIGH TCA6424A_GPIO_HIGH
2121

22-
// buffer sizes
2322
#define INA_REG_CONF_BUFF_SIZE 2
24-
2523
#define I2C_TRANSFER_TIMEOUT_TICKS pdMS_TO_TICKS(100) // 100 ms in RTOS ticks
2624

27-
// function pointers to switch between mock and real data
28-
obc_error_code_t (*i2cReadRegFuncPtr)(uint8_t, uint8_t, uint8_t*, uint16_t, TickType_t) = NULL;
29-
obc_error_code_t (*i2cWriteRegFuncPtr)(uint8_t, uint8_t, uint8_t*, uint16_t) = NULL;
30-
3125
// ------------------ INA230 Device Configuration ------------- //
3226
typedef struct {
3327
uint8_t i2cDeviceAddress;
@@ -93,10 +87,6 @@ static obc_error_code_t initTca6424PinState();
9387
* otherwise returns an appropriate error code
9488
*/
9589
obc_error_code_t initINA230() {
96-
#ifndef USE_MOCK_I2C
97-
i2cReadRegFuncPtr = i2cReadReg;
98-
i2cWriteRegFuncPtr = i2cWriteReg;
99-
#endif
10090
obc_error_code_t errCode;
10191
for (uint8_t i = 0; i < INA230_DEVICE_COUNT; ++i) {
10292
const ina230_config_t device = ina230Devices[i];
@@ -166,7 +156,7 @@ static obc_error_code_t writeINA230Register(uint8_t regAddress, uint8_t* data, u
166156
return OBC_ERR_CODE_INVALID_ARG;
167157
}
168158
obc_error_code_t errCode;
169-
RETURN_IF_ERROR_CODE(i2cWriteRegFuncPtr(ina230Devices[device].i2cDeviceAddress, regAddress, data, size));
159+
RETURN_IF_ERROR_CODE(i2cWriteReg(ina230Devices[device].i2cDeviceAddress, regAddress, data, size));
170160
return OBC_ERR_CODE_SUCCESS;
171161
}
172162
/**
@@ -212,9 +202,8 @@ obc_error_code_t getINA230ShuntVoltage(ina230_device_t device, float* shuntVolta
212202
obc_error_code_t errCode;
213203

214204
// Read the 16-bit shunt voltage register
215-
errCode =
216-
i2cReadRegFuncPtr(ina230Devices[device].i2cDeviceAddress, INA230_SHUNT_VOLTAGE_REGISTER_ADDR, shuntVoltageRaw, 2,
217-
I2C_TRANSFER_TIMEOUT_TICKS); // last param not sure
205+
errCode = i2cReadReg(ina230Devices[device].i2cDeviceAddress, INA230_SHUNT_VOLTAGE_REGISTER_ADDR,
206+
shuntVoltageRaw, 2, I2C_TRANSFER_TIMEOUT_TICKS);
218207
if (errCode != OBC_ERR_CODE_SUCCESS) return errCode;
219208

220209
// Combine the two bytes into a 16-bit value
@@ -231,16 +220,13 @@ obc_error_code_t getINA230ShuntVoltage(ina230_device_t device, float* shuntVolta
231220
obc_error_code_t disableNoAlert(ina230_device_t device) {
232221
uint32_t IOPortValue = 0;
233222
obc_error_code_t errCode;
234-
235-
for (uint8_t i = 0; i < INA230_DEVICE_COUNT; ++i) {
236-
uint8_t pinLocation =
237-
ina230Devices[i].tcaEnablePort; // specific pin on TCA that this ina230 controls, should this be alertPort?
238-
uint8_t index = ((pinLocation & 0x0F) +
239-
((pinLocation >> 1) & 0x18)); // converts the pinLocation to an index in the 24 bit IOPortValue
240-
// disbale
241-
uint8_t drivePort = INA230_DISABLE_LOAD;
242-
RETURN_IF_ERROR_CODE(driveTCA6424APinOutput(pinLocation, drivePort));
243-
}
223+
uint8_t pinLocation =
224+
ina230Devices[device].tcaEnablePort; // specific pin on TCA that this ina230 controls, should this be alertPort?
225+
uint8_t index = ((pinLocation & 0x0F) +
226+
((pinLocation >> 1) & 0x18)); // converts the pinLocation to an index in the 24 bit IOPortValue
227+
// disbale
228+
uint8_t drivePort = INA230_DISABLE_LOAD;
229+
RETURN_IF_ERROR_CODE(driveTCA6424APinOutput(pinLocation, drivePort));
244230
return OBC_ERR_CODE_SUCCESS;
245231
}
246232

@@ -261,7 +247,7 @@ obc_error_code_t getINA230BusVoltage(ina230_device_t device, float* busVoltage)
261247
}
262248
obc_error_code_t errCode;
263249
uint8_t busVoltageRaw[2] = {};
264-
RETURN_IF_ERROR_CODE(i2cReadRegFuncPtr(ina230Devices[device].i2cDeviceAddress, INA230_BUS_VOLTAGE_REGISTER_ADDR,
250+
RETURN_IF_ERROR_CODE(i2cReadReg(ina230Devices[device].i2cDeviceAddress, INA230_BUS_VOLTAGE_REGISTER_ADDR,
265251
busVoltageRaw, 2, I2C_TRANSFER_TIMEOUT_TICKS));
266252
uint16_t busVoltageValue = (busVoltageRaw[0] << 8) | busVoltageRaw[1];
267253
*busVoltage = busVoltageValue * INA230_BUS_VOLTAGE_LSB;
@@ -286,7 +272,7 @@ obc_error_code_t getINA230Power(ina230_device_t device, float* power) {
286272
return OBC_ERR_CODE_INVALID_ARG;
287273
}
288274
uint8_t powerRaw[INA_REG_CONF_BUFF_SIZE] = {};
289-
RETURN_IF_ERROR_CODE(i2cReadRegFuncPtr(ina230Devices[device].i2cDeviceAddress, INA230_POWER_REGISTER_ADDR, powerRaw,
275+
RETURN_IF_ERROR_CODE(i2cReadReg(ina230Devices[device].i2cDeviceAddress, INA230_POWER_REGISTER_ADDR, powerRaw,
290276
2, I2C_TRANSFER_TIMEOUT_TICKS));
291277
uint16_t powerValue = (powerRaw[0] << 8) | powerRaw[1];
292278
*power = powerValue * (INA230_CURRENT_LSB * INA230_POWER_LSB_MULTIPLIER);
@@ -311,7 +297,7 @@ obc_error_code_t getINA230Current(ina230_device_t device, float* current) {
311297
}
312298

313299
uint8_t currentRaw[INA_REG_CONF_BUFF_SIZE] = {};
314-
RETURN_IF_ERROR_CODE(i2cReadRegFuncPtr(ina230Devices[device].i2cDeviceAddress, INA230_CURRENT_REGISTER_ADDR,
300+
RETURN_IF_ERROR_CODE(i2cReadReg(ina230Devices[device].i2cDeviceAddress, INA230_CURRENT_REGISTER_ADDR,
315301
currentRaw, 2, I2C_TRANSFER_TIMEOUT_TICKS));
316302
int16_t currentValue = (currentRaw[0] << 8) | currentRaw[1];
317303
*current = currentValue * INA230_CURRENT_LSB;

test/test_obc/unit/test_ina230.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,6 @@
66
#include <assert.h>
77
#include <gtest/gtest.h>
88

9-
// include C functions in C++ file
10-
extern "C" {
11-
obc_error_code_t i2cReadReg(uint8_t, uint8_t, uint8_t*, uint16_t, TickType_t);
12-
obc_error_code_t i2cWriteReg(uint8_t, uint8_t, uint8_t*, uint16_t);
13-
}
14-
15-
// Google Test test environment class configuration
16-
class INA230TestEnvironment : public ::testing::Environment {
17-
public:
18-
void SetUp() override {
19-
i2cReadRegFuncPtr = i2cReadReg;
20-
i2cWriteRegFuncPtr = i2cWriteReg;
21-
}
22-
};
23-
24-
static ::testing::Environment* const ina230_env = ::testing::AddGlobalTestEnvironment(new INA230TestEnvironment());
25-
269
#define INA230_TEST_FLOAT_TOLERANCE 0.01f
2710

2811
// for reference:

0 commit comments

Comments
 (0)