Skip to content

Commit f667748

Browse files
committed
Modified ODR settings to handle power-mode restrictions
1 parent 2d77973 commit f667748

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

lsm303.c

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@ int8_t lsm303_setup(lsm303_dev *dev, lsm303_init_param lsm303_params) {
4242
dev->acc_axes_config = lsm303_params.acc_axes_config;
4343
dev->acc_scale = lsm303_params.acc_scale;
4444
dev->acc_resolution = lsm303_params.acc_resolution;
45-
// dev->i2c0_dev = DEVICE_DT_GET(i2c0_master);
45+
46+
#ifdef PLATFORM_ZEPHYR
4647
dev->i2c0_dev = (struct device *)DEVICE_DT_GET(i2c0_master);
4748

4849
if (!device_is_ready(dev->i2c0_dev)) {
49-
printk("I2C bus is not ready!\n\r");
5050
return LSM303_STATUS_API_ERR;
5151
}
52+
#endif
5253

5354
ret |= lsm303_set_power_mode(dev, dev->acc_power_mode);
5455
ret |= lsm303_acc_enable_axes(dev, dev->acc_axes_config);
@@ -122,10 +123,11 @@ int8_t lsm303_acc_enable_axes(lsm303_dev *device, lsm303_acc_axes_config axes) {
122123
}
123124

124125
val &= ~0x07;
125-
val = val | axes.acc_axes << ACC_AXES_MASK;
126-
axes.enable.z = (val & (1 << 2)) >> 2;
127-
axes.enable.y = (val & (1 << 1)) >> 1;
128-
axes.enable.x = (val & 1);
126+
val = val | axes.acc_axes << ACC_AXES_MASK;
127+
axes.enable.z = (val & (1 << 2)) >> 2;
128+
axes.enable.y = (val & (1 << 1)) >> 1;
129+
axes.enable.x = (val & 1);
130+
129131
device->acc_axes_config = axes;
130132

131133
uint8_t data_buffer[] = {CTRL_REG1_A, val};
@@ -153,8 +155,20 @@ int8_t lsm303_acc_set_odr(lsm303_dev *device, enum lsm303_acc_odr odr) {
153155
return LSM303_STATUS_API_ERR;
154156
}
155157

158+
if (odr == ACC_ODR_1K620HZ || odr == ACC_ODR_5K376HZ) {
159+
if (device->acc_power_mode != ACC_LOW_POWER) {
160+
printk("ODR only works in low-power mode\r\n");
161+
return LSM303_STATUS_INPUT_ERR;
162+
}
163+
}
164+
156165
val &= ~0xF0;
157-
val = val | odr << ACC_ODR_MASK;
166+
if (odr == ACC_ODR_5K376HZ) {
167+
val = val | (odr - 1) << ACC_ODR_MASK;
168+
} else {
169+
val = val | odr << ACC_ODR_MASK;
170+
}
171+
158172
device->acc_odr = odr;
159173

160174
uint8_t data_buffer[] = {CTRL_REG1_A, val};
@@ -184,7 +198,8 @@ int8_t lsm303_acc_set_scale(lsm303_dev *device,
184198
}
185199

186200
val &= ~0x30;
187-
val = val | scale << ACC_SCALE_MASK;
201+
val = val | scale << ACC_SCALE_MASK;
202+
188203
device->acc_scale = scale;
189204

190205
uint8_t data_buffer[] = {CTRL_REG4_A, val};
@@ -214,7 +229,8 @@ int8_t lsm303_acc_set_resolution(lsm303_dev *device,
214229
}
215230

216231
val &= ~0x08;
217-
val = val | resolution << ACC_RESOLUTION_MASK;
232+
val = val | resolution << ACC_RESOLUTION_MASK;
233+
218234
device->acc_resolution = resolution;
219235

220236
uint8_t data_buffer[] = {CTRL_REG4_A, val};
@@ -370,7 +386,21 @@ int8_t lsm303_get_z_raw_data(lsm303_dev *device, lsm303_axes_data *accel_data) {
370386
return LSM303_STATUS_SUCCESS;
371387
}
372388

373-
float convert_raw_to_g(lsm303_dev *device, int16_t raw_value) {
389+
/**
390+
* @brief Converts raw accelerometer data to acceleration in g units.
391+
*
392+
* This function converts a raw accelerometer value from the LSM303 device
393+
* to acceleration in g units, considering the device's sensitivity
394+
* and configuration settings.
395+
*
396+
* @param device Pointer to the LSM303 device structure containing
397+
* configuration details such as sensitivity.
398+
* @param raw_value The raw accelerometer data to be converted.
399+
*
400+
* @return
401+
* The acceleration value in g units as a float.
402+
*/
403+
float lsm303_convert_raw_to_g(lsm303_dev *device, int16_t raw_value) {
374404
float sensitivity = 0.0;
375405

376406
if (device->acc_resolution == ACC_RESOLUTION_LOW) {
@@ -443,7 +473,7 @@ float convert_raw_to_g(lsm303_dev *device, int16_t raw_value) {
443473
}
444474
}
445475

446-
return (float)raw_value * sensitivity / 1000.0;
476+
return (float)raw_value * sensitivity / (float)1000.0;
447477
}
448478

449479
/**

lsm303.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ enum lsm303_acc_odr {
140140
ACC_ODR_400HZ = 0x07,
141141
ACC_ODR_1K620HZ = 0x08,
142142
ACC_ODR_1K344HZ = 0x09,
143-
ACC_ODR_5K376HZ = 0x09
143+
ACC_ODR_5K376HZ = 0x0A
144144
};
145145

146146
enum lsm303_acc_axes_enable {
@@ -264,7 +264,7 @@ int8_t lsm303_get_y_raw_data(lsm303_dev *device, lsm303_axes_data *accel_data);
264264

265265
int8_t lsm303_get_z_raw_data(lsm303_dev *device, lsm303_axes_data *accel_data);
266266

267-
float convert_raw_to_g(lsm303_dev *device, int16_t raw_value);
267+
float lsm303_convert_raw_to_g(lsm303_dev *device, int16_t raw_value);
268268

269269
int8_t lsm303_i2c_read(lsm303_dev *device, uint8_t address, uint8_t reg,
270270
uint8_t *read_data);

0 commit comments

Comments
 (0)