Skip to content

Commit 8b30d11

Browse files
ubiedakartben
authored andcommitted
sensor: icm4568x: Address existing code issues by static analysis
Addressing low-hanging fruits. Put in a separate commit in order to make it easier to keep track of changes. Signed-off-by: Luis Ubieda <[email protected]>
1 parent 8db851f commit 8b30d11

File tree

4 files changed

+32
-13
lines changed

4 files changed

+32
-13
lines changed

drivers/sensor/tdk/icm4268x/icm4268x.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ static inline void icm4268x_accel_reg_to_hz(uint8_t odr, struct sensor_value *ou
200200
out->val1 = 1;
201201
out->val2 = 562500;
202202
return;
203+
default:
204+
CODE_UNREACHABLE;
205+
return;
203206
}
204207
}
205208

@@ -283,6 +286,8 @@ static inline void icm4268x_gyro_reg_to_odr(uint8_t odr, struct sensor_value *ou
283286
out->val1 = 12;
284287
out->val2 = 500000;
285288
return;
289+
default:
290+
CODE_UNREACHABLE;
286291
}
287292
}
288293

drivers/sensor/tdk/icm4268x/icm4268x_common.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,19 @@ static uint16_t icm4268x_compute_fifo_wm(const struct icm4268x_cfg *cfg)
6767
{
6868
const bool accel_enabled = cfg->accel_pwr_mode != ICM42688_DT_ACCEL_OFF;
6969
const bool gyro_enabled = cfg->gyro_pwr_mode != ICM42688_DT_GYRO_OFF;
70-
const int pkt_size = cfg->fifo_hires ? 20 : (accel_enabled && gyro_enabled ? 16 : 8);
7170
int accel_modr = 0;
7271
int gyro_modr = 0;
72+
uint8_t pkt_size;
7373
int64_t modr;
7474

75+
if (cfg->fifo_hires) {
76+
pkt_size = 20;
77+
} else if (accel_enabled && gyro_enabled) {
78+
pkt_size = 16;
79+
} else {
80+
pkt_size = 8;
81+
}
82+
7583
if (cfg->batch_ticks == 0 || (!accel_enabled && !gyro_enabled)) {
7684
return 0;
7785
}

drivers/sensor/tdk/icm4268x/icm4268x_decoder.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ int icm4268x_convert_raw_to_q31(struct icm4268x_cfg *cfg, enum sensor_channel ch
116116
if (shift < 0) {
117117
intermediate =
118118
intermediate * ((int64_t)INT32_MAX + 1) * (1 << -shift) / INT64_C(1000000);
119-
} else if (shift > 0) {
119+
} else {
120120
intermediate =
121121
intermediate * ((int64_t)INT32_MAX + 1) / ((1 << shift) * INT64_C(1000000));
122122
}
@@ -513,6 +513,8 @@ static int icm4268x_fifo_decode(const uint8_t *buffer, struct sensor_chan_spec c
513513
buffer = frame_end;
514514
continue;
515515
}
516+
} else {
517+
CODE_UNREACHABLE;
516518
}
517519
buffer = frame_end;
518520
*fit = (uintptr_t)frame_end;

drivers/sensor/tdk/icm4268x/icm4268x_emul.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,16 @@ struct icm4268x_emul_data {
2424
};
2525

2626
struct icm4268x_emul_cfg {
27+
#if defined(CONFIG_CPP)
28+
/* Empty struct has size 0 in C, size 1 in C++. Force them to be the same. */
29+
uint8_t unused_cpp_size_compatibility;
30+
#endif
2731
};
2832

33+
#if defined(CONFIG_CPP)
34+
BUILD_ASSERT(sizeof(struct icm4268x_emul_cfg) >= 1);
35+
#endif
36+
2937
void icm4268x_emul_set_reg(const struct emul *target, uint8_t reg_addr, const uint8_t *val,
3038
size_t count)
3139
{
@@ -47,17 +55,13 @@ static void icm4268x_emul_handle_write(const struct emul *target, uint8_t regn,
4755
{
4856
struct icm4268x_emul_data *data = target->data;
4957

50-
switch (regn) {
51-
case REG_DEVICE_CONFIG:
52-
if (FIELD_GET(BIT_SOFT_RESET_CONFIG, value) == 1) {
53-
/* Perform a soft reset */
54-
memset(data->reg, 0, NUM_REGS);
55-
/* Initialized the who-am-i register */
56-
data->reg[REG_WHO_AM_I] = WHO_AM_I_ICM42688;
57-
/* Set the bit for the reset being done */
58-
data->reg[REG_INT_STATUS] |= BIT_RESET_DONE_INT;
59-
}
60-
break;
58+
if (regn == REG_DEVICE_CONFIG && FIELD_GET(BIT_SOFT_RESET_CONFIG, value) == 1) {
59+
/* Perform a soft reset */
60+
memset(data->reg, 0, NUM_REGS);
61+
/* Initialized the who-am-i register */
62+
data->reg[REG_WHO_AM_I] = WHO_AM_I_ICM42688;
63+
/* Set the bit for the reset being done */
64+
data->reg[REG_INT_STATUS] |= BIT_RESET_DONE_INT;
6165
}
6266
}
6367

0 commit comments

Comments
 (0)