Skip to content

Commit 0cb5faf

Browse files
committed
Transfer functions and updated unit-test fixtures
1 parent 62e5bc5 commit 0cb5faf

10 files changed

+326
-104
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "src/utils"]
2+
path = src/utils
3+
url = https://github.com/mcu-dev/dev-utils.git

lsm303.code-workspace

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,11 @@
44
"path": "."
55
}
66
],
7-
"settings": {}
7+
"settings": {
8+
"files.associations": {
9+
"lsm303.h": "c",
10+
"stdbool.h": "c",
11+
"i2c.h": "c"
12+
}
13+
}
814
}

project.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
:paths:
3333
:test:
3434
- +:test/**
35-
- -:test/support
3635
:source:
3736
- src/**
3837
:support:
@@ -55,8 +54,12 @@
5554
:mock_prefix: mock_
5655
:when_no_prototypes: :warn
5756
:enforce_strict_ordering: TRUE
57+
:verbosity: 2
5858
:plugins:
5959
- :ignore
60+
- :array
61+
- :ignore_arg
62+
- :return_thru_ptr
6063
- :callback
6164
:treat_as:
6265
uint8: HEX8

src/lsm303.c

+21-81
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,16 @@
3737
int8_t lsm303_setup(lsm303_dev *dev, lsm303_init_param lsm303_params) {
3838
int8_t ret = 0;
3939

40+
if (!i2c_init()) {
41+
return LSM303_STATUS_INIT_ERR;
42+
}
43+
4044
dev->acc_power_mode = lsm303_params.acc_power_mode;
4145
dev->acc_odr = lsm303_params.acc_odr;
4246
dev->acc_axes_config = lsm303_params.acc_axes_config;
4347
dev->acc_scale = lsm303_params.acc_scale;
4448
dev->acc_resolution = lsm303_params.acc_resolution;
4549

46-
#ifdef PLATFORM_ZEPHYR
47-
dev->i2c0_dev = (struct device *)DEVICE_DT_GET(i2c0_master);
48-
49-
if (!device_is_ready(dev->i2c0_dev)) {
50-
return LSM303_STATUS_API_ERR;
51-
}
52-
#endif
53-
5450
ret |= lsm303_set_power_mode(dev, dev->acc_power_mode);
5551
ret |= lsm303_acc_enable_axes(dev, dev->acc_axes_config);
5652
ret |= lsm303_acc_set_odr(dev, dev->acc_odr);
@@ -83,7 +79,7 @@ int8_t lsm303_set_power_mode(lsm303_dev *device,
8379
enum lsm303_acc_power_mode mode) {
8480
uint8_t val = 0x00;
8581

86-
if (lsm303_i2c_read(device, ACC_I2C_ADDRESS, CTRL_REG1_A, &val) !=
82+
if (i2c_read_byte(ACC_I2C_ADDRESS, CTRL_REG1_A, &val) !=
8783
LSM303_STATUS_SUCCESS) {
8884
return LSM303_STATUS_API_ERR;
8985
}
@@ -97,7 +93,7 @@ int8_t lsm303_set_power_mode(lsm303_dev *device,
9793
device->acc_power_mode = mode;
9894

9995
uint8_t data_buffer[] = {CTRL_REG1_A, val};
100-
return lsm303_i2c_write(device, ACC_I2C_ADDRESS, data_buffer);
96+
return i2c_write_bytes(ACC_I2C_ADDRESS, data_buffer);
10197
}
10298

10399
/**
@@ -117,7 +113,7 @@ int8_t lsm303_set_power_mode(lsm303_dev *device,
117113
int8_t lsm303_acc_enable_axes(lsm303_dev *device, lsm303_acc_axes_config axes) {
118114
uint8_t val = 0x00;
119115

120-
if (lsm303_i2c_read(device, ACC_I2C_ADDRESS, CTRL_REG1_A, &val) !=
116+
if (i2c_read_byte(ACC_I2C_ADDRESS, CTRL_REG1_A, &val) !=
121117
LSM303_STATUS_SUCCESS) {
122118
return LSM303_STATUS_API_ERR;
123119
}
@@ -131,7 +127,7 @@ int8_t lsm303_acc_enable_axes(lsm303_dev *device, lsm303_acc_axes_config axes) {
131127
device->acc_axes_config = axes;
132128

133129
uint8_t data_buffer[] = {CTRL_REG1_A, val};
134-
return lsm303_i2c_write(device, ACC_I2C_ADDRESS, data_buffer);
130+
return i2c_write_bytes(ACC_I2C_ADDRESS, data_buffer);
135131
}
136132

137133
/**
@@ -150,14 +146,13 @@ int8_t lsm303_acc_enable_axes(lsm303_dev *device, lsm303_acc_axes_config axes) {
150146
int8_t lsm303_acc_set_odr(lsm303_dev *device, enum lsm303_acc_odr odr) {
151147
uint8_t val = 0x00;
152148

153-
if (lsm303_i2c_read(device, ACC_I2C_ADDRESS, CTRL_REG1_A, &val) !=
149+
if (i2c_read_byte(ACC_I2C_ADDRESS, CTRL_REG1_A, &val) !=
154150
LSM303_STATUS_SUCCESS) {
155151
return LSM303_STATUS_API_ERR;
156152
}
157153

158154
if (odr == ACC_ODR_1K620HZ || odr == ACC_ODR_5K376HZ) {
159155
if (device->acc_power_mode != ACC_LOW_POWER) {
160-
printk("ODR only works in low-power mode\r\n");
161156
return LSM303_STATUS_INPUT_ERR;
162157
}
163158
}
@@ -172,7 +167,7 @@ int8_t lsm303_acc_set_odr(lsm303_dev *device, enum lsm303_acc_odr odr) {
172167
device->acc_odr = odr;
173168

174169
uint8_t data_buffer[] = {CTRL_REG1_A, val};
175-
return lsm303_i2c_write(device, ACC_I2C_ADDRESS, data_buffer);
170+
return i2c_write_bytes(ACC_I2C_ADDRESS, data_buffer);
176171
}
177172

178173
/**
@@ -192,7 +187,7 @@ int8_t lsm303_acc_set_scale(lsm303_dev *device,
192187
enum lsm303_acc_full_scale scale) {
193188
uint8_t val = 0x00;
194189

195-
if (lsm303_i2c_read(device, ACC_I2C_ADDRESS, CTRL_REG4_A, &val) !=
190+
if (i2c_read_byte(ACC_I2C_ADDRESS, CTRL_REG4_A, &val) !=
196191
LSM303_STATUS_SUCCESS) {
197192
return LSM303_STATUS_API_ERR;
198193
}
@@ -203,7 +198,7 @@ int8_t lsm303_acc_set_scale(lsm303_dev *device,
203198
device->acc_scale = scale;
204199

205200
uint8_t data_buffer[] = {CTRL_REG4_A, val};
206-
return lsm303_i2c_write(device, ACC_I2C_ADDRESS, data_buffer);
201+
return i2c_write_bytes(ACC_I2C_ADDRESS, data_buffer);
207202
}
208203

209204
/**
@@ -223,7 +218,7 @@ int8_t lsm303_acc_set_resolution(lsm303_dev *device,
223218
enum lsm303_acc_resolution resolution) {
224219
uint8_t val = 0x00;
225220

226-
if (lsm303_i2c_read(device, ACC_I2C_ADDRESS, CTRL_REG4_A, &val) !=
221+
if (i2c_read_byte(ACC_I2C_ADDRESS, CTRL_REG4_A, &val) !=
227222
LSM303_STATUS_SUCCESS) {
228223
return LSM303_STATUS_API_ERR;
229224
}
@@ -234,7 +229,7 @@ int8_t lsm303_acc_set_resolution(lsm303_dev *device,
234229
device->acc_resolution = resolution;
235230

236231
uint8_t data_buffer[] = {CTRL_REG4_A, val};
237-
return lsm303_i2c_write(device, ACC_I2C_ADDRESS, data_buffer);
232+
return i2c_write_bytes(ACC_I2C_ADDRESS, data_buffer);
238233
}
239234

240235
/**
@@ -251,7 +246,7 @@ int8_t lsm303_acc_set_resolution(lsm303_dev *device,
251246
int8_t lsm303_data_ready(lsm303_dev *device) {
252247
uint8_t val = 0x00;
253248

254-
if (lsm303_i2c_read(device, ACC_I2C_ADDRESS, STATUS_REG_A, &val) !=
249+
if (i2c_read_byte(ACC_I2C_ADDRESS, STATUS_REG_A, &val) !=
255250
LSM303_STATUS_SUCCESS) {
256251
return LSM303_STATUS_API_ERR;
257252
}
@@ -281,12 +276,12 @@ int8_t lsm303_get_x_raw_data(lsm303_dev *device, lsm303_axes_data *accel_data) {
281276
uint8_t val_l = 0x00;
282277
uint8_t val_h = 0x00;
283278

284-
if (lsm303_i2c_read(device, ACC_I2C_ADDRESS, OUT_X_H_A, &val_h) !=
279+
if (i2c_read_byte(ACC_I2C_ADDRESS, OUT_X_H_A, &val_h) !=
285280
LSM303_STATUS_SUCCESS) {
286281
return LSM303_STATUS_API_ERR;
287282
}
288283

289-
if (lsm303_i2c_read(device, ACC_I2C_ADDRESS, OUT_X_L_A, &val_l) !=
284+
if (i2c_read_byte(ACC_I2C_ADDRESS, OUT_X_L_A, &val_l) !=
290285
LSM303_STATUS_SUCCESS) {
291286
return LSM303_STATUS_API_ERR;
292287
}
@@ -322,12 +317,12 @@ int8_t lsm303_get_y_raw_data(lsm303_dev *device, lsm303_axes_data *accel_data) {
322317
uint8_t val_l = 0x00;
323318
uint8_t val_h = 0x00;
324319

325-
if (lsm303_i2c_read(device, ACC_I2C_ADDRESS, OUT_Y_H_A, &val_h) !=
320+
if (i2c_read_byte(ACC_I2C_ADDRESS, OUT_Y_H_A, &val_h) !=
326321
LSM303_STATUS_SUCCESS) {
327322
return LSM303_STATUS_API_ERR;
328323
}
329324

330-
if (lsm303_i2c_read(device, ACC_I2C_ADDRESS, OUT_Y_L_A, &val_l) !=
325+
if (i2c_read_byte(ACC_I2C_ADDRESS, OUT_Y_L_A, &val_l) !=
331326
LSM303_STATUS_SUCCESS) {
332327
return LSM303_STATUS_API_ERR;
333328
}
@@ -363,12 +358,12 @@ int8_t lsm303_get_z_raw_data(lsm303_dev *device, lsm303_axes_data *accel_data) {
363358
uint8_t val_l = 0x00;
364359
uint8_t val_h = 0x00;
365360

366-
if (lsm303_i2c_read(device, ACC_I2C_ADDRESS, OUT_Z_H_A, &val_h) !=
361+
if (i2c_read_byte(ACC_I2C_ADDRESS, OUT_Z_H_A, &val_h) !=
367362
LSM303_STATUS_SUCCESS) {
368363
return LSM303_STATUS_API_ERR;
369364
}
370365

371-
if (lsm303_i2c_read(device, ACC_I2C_ADDRESS, OUT_Z_L_A, &val_l) !=
366+
if (i2c_read_byte(ACC_I2C_ADDRESS, OUT_Z_L_A, &val_l) !=
372367
LSM303_STATUS_SUCCESS) {
373368
return LSM303_STATUS_API_ERR;
374369
}
@@ -475,58 +470,3 @@ float lsm303_convert_raw_to_g(lsm303_dev *device, int16_t raw_value) {
475470

476471
return (float)raw_value * sensitivity / (float)1000.0;
477472
}
478-
479-
/**
480-
* @brief Reads a register value from the LSM303 device via I2C.
481-
*
482-
* Reads a single register from the LSM303 device and stores the retrieved value
483-
* in the specified buffer.
484-
*
485-
* @param device Pointer to the LSM303 device structure.
486-
* @param address I2C address of the LSM303 device.
487-
* @param reg Register address to read from.
488-
* @param read_data Pointer to the buffer where the read value will be stored.
489-
*
490-
* @return
491-
* - 0 on success.
492-
* - Non-zero error code on failure.
493-
*/
494-
int8_t lsm303_i2c_read(lsm303_dev *device, uint8_t address, uint8_t reg,
495-
uint8_t *read_data) {
496-
#ifdef PLATFORM_ZEPHYR
497-
uint32_t bytecount = 1;
498-
499-
if (i2c_write(device->i2c0_dev, &reg, bytecount, address) !=
500-
LSM303_STATUS_SUCCESS) {
501-
return LSM303_STATUS_API_ERR;
502-
}
503-
504-
if (i2c_read(device->i2c0_dev, read_data, sizeof(*read_data), address) != 0) {
505-
return LSM303_STATUS_API_ERR;
506-
}
507-
508-
return LSM303_STATUS_SUCCESS;
509-
#endif
510-
}
511-
512-
/**
513-
* @brief Writes data to a register of the LSM303 device over I2C.
514-
*
515-
* This function writes data to a specified register of the LSM303 device
516-
* using the provided data buffer.
517-
*
518-
* @param device Pointer to the LSM303 device structure.
519-
* @param address I2C address of the LSM303 device.
520-
* @param data_buffer Pointer to the buffer containing the data to be written.
521-
*
522-
* @return
523-
* - 0 on success.
524-
* - Non-zero error code on failure.
525-
*/
526-
int8_t lsm303_i2c_write(lsm303_dev *device, uint8_t address,
527-
uint8_t *data_buffer) {
528-
#ifdef PLATFORM_ZEPHYR
529-
uint32_t bytecount = 2;
530-
return i2c_write(device->i2c0_dev, data_buffer, bytecount, address);
531-
#endif
532-
}

src/lsm303.h

+2-21
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,7 @@
2626
#include <stdint.h>
2727
#include <stdio.h>
2828

29-
#ifdef PLATFORM_ZEPHYR
30-
#include <zephyr/device.h>
31-
#include <zephyr/devicetree.h>
32-
#include <zephyr/drivers/i2c.h>
33-
#include <zephyr/kernel.h>
34-
35-
#define i2c0_master DT_NODELABEL(i2c0)
36-
#endif
29+
#include "utils/i2c.h"
3730

3831
/* SA0 pin connection status */
3932
#define SA0 1
@@ -49,7 +42,7 @@ typedef enum {
4942
LSM303_STATUS_SUCCESS = 0,
5043
LSM303_STATUS_API_ERR = -1,
5144
LSM303_STATUS_INPUT_ERR = -2,
52-
LSM303_STATUS_ALLOC_ERR = -3,
45+
LSM303_STATUS_INIT_ERR = -3,
5346
} LSM303_RETURN_STATUS;
5447

5548
/*****************************ID REGISTERS*************************************/
@@ -218,9 +211,6 @@ typedef struct {
218211
enum lsm303_mag_full_scale mag_scale;
219212
enum lsm303_acc_resolution acc_resolution;
220213
lsm303_acc_axes_config acc_axes_config;
221-
#ifdef PLATFORM_ZEPHYR
222-
struct device *i2c0_dev;
223-
#endif
224214
bool is_Setup;
225215
} lsm303_dev;
226216

@@ -233,9 +223,6 @@ typedef struct {
233223
enum lsm303_mag_full_scale mag_scale;
234224
enum lsm303_acc_resolution acc_resolution;
235225
lsm303_acc_axes_config acc_axes_config;
236-
#ifdef PLATFORM_ZEPHYR
237-
struct device *i2c0_dev;
238-
#endif
239226
bool is_Setup;
240227
} lsm303_init_param;
241228

@@ -266,10 +253,4 @@ int8_t lsm303_get_z_raw_data(lsm303_dev *device, lsm303_axes_data *accel_data);
266253

267254
float lsm303_convert_raw_to_g(lsm303_dev *device, int16_t raw_value);
268255

269-
int8_t lsm303_i2c_read(lsm303_dev *device, uint8_t address, uint8_t reg,
270-
uint8_t *read_data);
271-
272-
int8_t lsm303_i2c_write(lsm303_dev *device, uint8_t address,
273-
uint8_t *data_buffer);
274-
275256
#endif /* LSM303_H */

src/utils

Submodule utils added at 3cf2c30

test/support/.gitkeep

Whitespace-only changes.

test/support/test_helper.c

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "test_helper.h"
2+
#include "unity.h"
3+
4+
static uint8_t *expected_data = NULL;
5+
static size_t expected_data_size = 0;
6+
7+
void set_expected_i2c_write_data(uint8_t *expected_data_param, size_t size) {
8+
expected_data = expected_data_param;
9+
expected_data_size = size;
10+
}
11+
12+
signed char i2c_write_bytes_callback(uint8_t address, uint8_t *data_buffer,
13+
int cmock_num_calls) {
14+
// Validate the address
15+
TEST_ASSERT_EQUAL(ACC_I2C_ADDRESS, address);
16+
17+
// Validate the data buffer
18+
TEST_ASSERT_EQUAL_UINT8_ARRAY(expected_data, data_buffer, expected_data_size);
19+
20+
return LSM303_STATUS_SUCCESS;
21+
}

test/support/test_helper.h

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef TEST_HELPERS_H
2+
#define TEST_HELPERS_H
3+
4+
#include <stddef.h>
5+
#include <stdint.h>
6+
7+
#include "lsm303.h"
8+
9+
// Declare the generalized callback function
10+
void set_expected_i2c_write_data(uint8_t *expected_data, size_t size);
11+
signed char i2c_write_bytes_callback(uint8_t address, uint8_t *data_buffer,
12+
int cmock_num_calls);
13+
14+
#endif // TEST_HELPERS_H

0 commit comments

Comments
 (0)