37
37
int8_t lsm303_setup (lsm303_dev * dev , lsm303_init_param lsm303_params ) {
38
38
int8_t ret = 0 ;
39
39
40
+ if (!i2c_init ()) {
41
+ return LSM303_STATUS_INIT_ERR ;
42
+ }
43
+
40
44
dev -> acc_power_mode = lsm303_params .acc_power_mode ;
41
45
dev -> acc_odr = lsm303_params .acc_odr ;
42
46
dev -> acc_axes_config = lsm303_params .acc_axes_config ;
43
47
dev -> acc_scale = lsm303_params .acc_scale ;
44
48
dev -> acc_resolution = lsm303_params .acc_resolution ;
45
49
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
-
54
50
ret |= lsm303_set_power_mode (dev , dev -> acc_power_mode );
55
51
ret |= lsm303_acc_enable_axes (dev , dev -> acc_axes_config );
56
52
ret |= lsm303_acc_set_odr (dev , dev -> acc_odr );
@@ -83,7 +79,7 @@ int8_t lsm303_set_power_mode(lsm303_dev *device,
83
79
enum lsm303_acc_power_mode mode ) {
84
80
uint8_t val = 0x00 ;
85
81
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 ) !=
87
83
LSM303_STATUS_SUCCESS ) {
88
84
return LSM303_STATUS_API_ERR ;
89
85
}
@@ -97,7 +93,7 @@ int8_t lsm303_set_power_mode(lsm303_dev *device,
97
93
device -> acc_power_mode = mode ;
98
94
99
95
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 );
101
97
}
102
98
103
99
/**
@@ -117,7 +113,7 @@ int8_t lsm303_set_power_mode(lsm303_dev *device,
117
113
int8_t lsm303_acc_enable_axes (lsm303_dev * device , lsm303_acc_axes_config axes ) {
118
114
uint8_t val = 0x00 ;
119
115
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 ) !=
121
117
LSM303_STATUS_SUCCESS ) {
122
118
return LSM303_STATUS_API_ERR ;
123
119
}
@@ -131,7 +127,7 @@ int8_t lsm303_acc_enable_axes(lsm303_dev *device, lsm303_acc_axes_config axes) {
131
127
device -> acc_axes_config = axes ;
132
128
133
129
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 );
135
131
}
136
132
137
133
/**
@@ -150,14 +146,13 @@ int8_t lsm303_acc_enable_axes(lsm303_dev *device, lsm303_acc_axes_config axes) {
150
146
int8_t lsm303_acc_set_odr (lsm303_dev * device , enum lsm303_acc_odr odr ) {
151
147
uint8_t val = 0x00 ;
152
148
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 ) !=
154
150
LSM303_STATUS_SUCCESS ) {
155
151
return LSM303_STATUS_API_ERR ;
156
152
}
157
153
158
154
if (odr == ACC_ODR_1K620HZ || odr == ACC_ODR_5K376HZ ) {
159
155
if (device -> acc_power_mode != ACC_LOW_POWER ) {
160
- printk ("ODR only works in low-power mode\r\n" );
161
156
return LSM303_STATUS_INPUT_ERR ;
162
157
}
163
158
}
@@ -172,7 +167,7 @@ int8_t lsm303_acc_set_odr(lsm303_dev *device, enum lsm303_acc_odr odr) {
172
167
device -> acc_odr = odr ;
173
168
174
169
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 );
176
171
}
177
172
178
173
/**
@@ -192,7 +187,7 @@ int8_t lsm303_acc_set_scale(lsm303_dev *device,
192
187
enum lsm303_acc_full_scale scale ) {
193
188
uint8_t val = 0x00 ;
194
189
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 ) !=
196
191
LSM303_STATUS_SUCCESS ) {
197
192
return LSM303_STATUS_API_ERR ;
198
193
}
@@ -203,7 +198,7 @@ int8_t lsm303_acc_set_scale(lsm303_dev *device,
203
198
device -> acc_scale = scale ;
204
199
205
200
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 );
207
202
}
208
203
209
204
/**
@@ -223,7 +218,7 @@ int8_t lsm303_acc_set_resolution(lsm303_dev *device,
223
218
enum lsm303_acc_resolution resolution ) {
224
219
uint8_t val = 0x00 ;
225
220
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 ) !=
227
222
LSM303_STATUS_SUCCESS ) {
228
223
return LSM303_STATUS_API_ERR ;
229
224
}
@@ -234,7 +229,7 @@ int8_t lsm303_acc_set_resolution(lsm303_dev *device,
234
229
device -> acc_resolution = resolution ;
235
230
236
231
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 );
238
233
}
239
234
240
235
/**
@@ -251,7 +246,7 @@ int8_t lsm303_acc_set_resolution(lsm303_dev *device,
251
246
int8_t lsm303_data_ready (lsm303_dev * device ) {
252
247
uint8_t val = 0x00 ;
253
248
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 ) !=
255
250
LSM303_STATUS_SUCCESS ) {
256
251
return LSM303_STATUS_API_ERR ;
257
252
}
@@ -281,12 +276,12 @@ int8_t lsm303_get_x_raw_data(lsm303_dev *device, lsm303_axes_data *accel_data) {
281
276
uint8_t val_l = 0x00 ;
282
277
uint8_t val_h = 0x00 ;
283
278
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 ) !=
285
280
LSM303_STATUS_SUCCESS ) {
286
281
return LSM303_STATUS_API_ERR ;
287
282
}
288
283
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 ) !=
290
285
LSM303_STATUS_SUCCESS ) {
291
286
return LSM303_STATUS_API_ERR ;
292
287
}
@@ -322,12 +317,12 @@ int8_t lsm303_get_y_raw_data(lsm303_dev *device, lsm303_axes_data *accel_data) {
322
317
uint8_t val_l = 0x00 ;
323
318
uint8_t val_h = 0x00 ;
324
319
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 ) !=
326
321
LSM303_STATUS_SUCCESS ) {
327
322
return LSM303_STATUS_API_ERR ;
328
323
}
329
324
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 ) !=
331
326
LSM303_STATUS_SUCCESS ) {
332
327
return LSM303_STATUS_API_ERR ;
333
328
}
@@ -363,12 +358,12 @@ int8_t lsm303_get_z_raw_data(lsm303_dev *device, lsm303_axes_data *accel_data) {
363
358
uint8_t val_l = 0x00 ;
364
359
uint8_t val_h = 0x00 ;
365
360
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 ) !=
367
362
LSM303_STATUS_SUCCESS ) {
368
363
return LSM303_STATUS_API_ERR ;
369
364
}
370
365
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 ) !=
372
367
LSM303_STATUS_SUCCESS ) {
373
368
return LSM303_STATUS_API_ERR ;
374
369
}
@@ -475,58 +470,3 @@ float lsm303_convert_raw_to_g(lsm303_dev *device, int16_t raw_value) {
475
470
476
471
return (float )raw_value * sensitivity / (float )1000.0 ;
477
472
}
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
- }
0 commit comments