-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathi2cDevice.c
executable file
·669 lines (539 loc) · 21.3 KB
/
i2cDevice.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/*
* Some of the code in this file was copied from ST Micro. Below is their required information.
*
* @attention
*
* <h2><center>© COPYRIGHT(c) 2018 STMicroelectronics</center></h2>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <errno.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdarg.h>
#include <applibs/log.h>
#include <applibs/i2c.h>
#include <hw/sample_hardware.h>
#include "i2cDevice.h"
#include "lsm6dso_reg.h"
#include "lps22hh_reg.h"
// Azure IoT SDK
#include <iothub_client_core_common.h>
#include <iothub_device_client_ll.h>
#include <iothub_client_options.h>
#include <iothubtransportmqtt.h>
#include <iothub.h>
#include <azure_sphere_provisioning.h>
#define IOT_CENTRAL_APPLICATION
#define JSON_BUFFER_SIZE 256
/* Private variables ---------------------------------------------------------*/
static axis3bit16_t data_raw_acceleration;
static axis3bit16_t data_raw_angular_rate;
static axis3bit16_t raw_angular_rate_calibration;
static axis1bit32_t data_raw_pressure;
static axis1bit16_t data_raw_temperature;
static float acceleration_mg[3];
static float angular_rate_dps[3];
static float lsm6dsoTemperature_degC;
static float pressure_hPa;
static float lps22hhTemperature_degC;
static uint8_t whoamI, rst;
int accelTimerFd;
const uint8_t lsm6dsOAddress = LSM6DSO_ADDRESS; // Addr = 0x6A
lsm6dso_ctx_t dev_ctx;
lps22hh_ctx_t pressure_ctx;
bool lps22hhDetected;
int i2cFd = -1;
//Extern variables
extern IOTHUB_DEVICE_CLIENT_LL_HANDLE iothubClientHandle;
// Publilc functions
void SensorHub_SendData(void);
//Private functions
// Routines to read/write to the LSM6DSO device
static int32_t platform_write(int *fD, uint8_t reg, uint8_t *bufp, uint16_t len);
static int32_t platform_read(int *fD, uint8_t reg, uint8_t *bufp, uint16_t len);
// Routines to read/write to the LPS22HH device connected to the LSM6DSO sensor hub
static int32_t lsm6dso_write_lps22hh_cx(void* ctx, uint8_t reg, uint8_t* data, uint16_t len);
static int32_t lsm6dso_read_lps22hh_cx(void* ctx, uint8_t reg, uint8_t* data, uint16_t len);
extern void CloseFdAndPrintError(int fd, const char* fdName);
/// <summary>
/// Sleep for delayTime ms
/// </summary>
void HAL_Delay(int delayTime) {
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = delayTime * 10000;
nanosleep(&ts, NULL);
}
static void sendMessageCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* context)
{
Log_Debug("INFO: Message received by IoT Hub. Result is: %d\n", result);
}
static void sendMessage(const char* messagePayload)
{
if (iothubClientHandle == NULL) {
Log_Debug("WARNING: IoT Hub client not initialized\n");
return;
}
IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromString(messagePayload);
if (messageHandle == 0) {
Log_Debug("WARNING: unable to create a new IoTHubMessage\n");
return;
}
if (IoTHubDeviceClient_LL_SendEventAsync(iothubClientHandle, messageHandle, sendMessageCallback,
/*&callback_param*/ 0) != IOTHUB_CLIENT_OK) {
Log_Debug("WARNING: failed to hand over the message to IoTHubClient\n");
}
else {
Log_Debug("INFO: IoTHubClient accepted the message for delivery\n");
}
IoTHubMessage_Destroy(messageHandle);
}
/// <summary>
/// Print latest data from on-board sensors.
/// </summary>
void SensorHub_SendData(void)
{
uint8_t reg;
lps22hh_reg_t lps22hhReg;
#if (defined(IOT_CENTRAL_APPLICATION) || defined(IOT_HUB_APPLICATION))
static bool firstPass = true;
#endif
// Read the sensors on the lsm6dso device
//Read output only if new xl value is available
lsm6dso_xl_flag_data_ready_get(&dev_ctx, ®);
if (reg)
{
// Read acceleration field data
memset(data_raw_acceleration.u8bit, 0x00, 3 * sizeof(int16_t));
lsm6dso_acceleration_raw_get(&dev_ctx, data_raw_acceleration.u8bit);
acceleration_mg[0] = lsm6dso_from_fs4_to_mg(data_raw_acceleration.i16bit[0]);
acceleration_mg[1] = lsm6dso_from_fs4_to_mg(data_raw_acceleration.i16bit[1]);
acceleration_mg[2] = lsm6dso_from_fs4_to_mg(data_raw_acceleration.i16bit[2]);
Log_Debug("\nLSM6DSO: Acceleration [mg] : %.4lf, %.4lf, %.4lf\n",
acceleration_mg[0], acceleration_mg[1], acceleration_mg[2]);
}
lsm6dso_gy_flag_data_ready_get(&dev_ctx, ®);
if (reg)
{
// Read angular rate field data
memset(data_raw_angular_rate.u8bit, 0x00, 3 * sizeof(int16_t));
lsm6dso_angular_rate_raw_get(&dev_ctx, data_raw_angular_rate.u8bit);
// Before we store the mdps values subtract the calibration data we captured at startup.
angular_rate_dps[0] = (lsm6dso_from_fs2000_to_mdps(data_raw_angular_rate.i16bit[0] - raw_angular_rate_calibration.i16bit[0])) / 1000.0;
angular_rate_dps[1] = (lsm6dso_from_fs2000_to_mdps(data_raw_angular_rate.i16bit[1] - raw_angular_rate_calibration.i16bit[1])) / 1000.0;
angular_rate_dps[2] = (lsm6dso_from_fs2000_to_mdps(data_raw_angular_rate.i16bit[2] - raw_angular_rate_calibration.i16bit[2])) / 1000.0;
Log_Debug("LSM6DSO: Angular rate [dps] : %4.2f, %4.2f, %4.2f\r\n",
angular_rate_dps[0], angular_rate_dps[1], angular_rate_dps[2]);
}
lsm6dso_temp_flag_data_ready_get(&dev_ctx, ®);
if (reg)
{
// Read temperature data
memset(data_raw_temperature.u8bit, 0x00, sizeof(int16_t));
lsm6dso_temperature_raw_get(&dev_ctx, data_raw_temperature.u8bit);
lsm6dsoTemperature_degC = lsm6dso_from_lsb_to_celsius(data_raw_temperature.i16bit);
Log_Debug("LSM6DSO: Temperature [degC]: %.2f\r\n", lsm6dsoTemperature_degC);
}
// Read the lps22hh sensor on the lsm6dso device
// Initialize the data structures to 0s.
memset(data_raw_pressure.u8bit, 0x00, sizeof(int32_t));
memset(data_raw_temperature.u8bit, 0x00, sizeof(int16_t));
if (lps22hhDetected) {
lps22hh_read_reg(&pressure_ctx, LPS22HH_STATUS, (uint8_t *)&lps22hhReg, 1);
//Read output only if new value is available
if ((lps22hhReg.status.p_da == 1) && (lps22hhReg.status.t_da == 1))
{
lps22hh_pressure_raw_get(&pressure_ctx, data_raw_pressure.u8bit);
pressure_hPa = lps22hh_from_lsb_to_hpa(data_raw_pressure.i32bit);
lps22hh_temperature_raw_get(&pressure_ctx, data_raw_temperature.u8bit);
lps22hhTemperature_degC = lps22hh_from_lsb_to_celsius(data_raw_temperature.i16bit);
Log_Debug("LPS22HH: Pressure [hPa] : %.2f\r\n", pressure_hPa);
Log_Debug("LPS22HH: Temperature [degC]: %.2f\r\n", lps22hhTemperature_degC);
}
}
// LPS22HH was not detected
else {
Log_Debug("LPS22HH: Pressure [hPa] : Not read!\r\n");
Log_Debug("LPS22HH: Temperature [degC]: Not read!\r\n");
}
#if (defined(IOT_CENTRAL_APPLICATION) || defined(IOT_HUB_APPLICATION))
// We've seen that the first read of the Accelerometer data is garbage. If this is the first pass
// reading data, don't report it to Azure. Since we're graphing data in Azure, this data point
// will skew the data.
if (!firstPass)
{
// Allocate memory for a telemetry message to Azure
char *pjsonBuffer = (char *)malloc(JSON_BUFFER_SIZE);
if (pjsonBuffer == NULL) {
Log_Debug("ERROR: not enough memory to send telemetry");
}
// construct the telemetry message
snprintf(pjsonBuffer, JSON_BUFFER_SIZE, "{\"aX\":\"%.4lf\", \"aY\":\"%.4lf\", \"aZ\":\"%.4lf\", \"pressure\": \"%.2f\", \"gX\": \"%4.2f\", \"gY\": \"%4.2f\", \"gZ\": \"%4.2f\", \"Temperature\": \"%.2f\"}",
acceleration_mg[0], acceleration_mg[1], acceleration_mg[2], pressure_hPa, angular_rate_dps[0], angular_rate_dps[1], angular_rate_dps[2], lps22hhTemperature_degC);
Log_Debug("\n[Info] Sending telemetry: %s\n", pjsonBuffer);
sendMessage(pjsonBuffer);
free(pjsonBuffer);
}
firstPass = false;
#endif
}
int initI2cDevice(void) {
i2cFd = I2CMaster_Open(AVNET_MT3620_SK_ISU2_I2C);
if (i2cFd < 0) {
Log_Debug("ERROR: I2CMaster_Open: errno=%d (%s)\n", errno, strerror(errno));
return -1;
}
int result = I2CMaster_SetBusSpeed(i2cFd, I2C_BUS_SPEED_STANDARD);
if (result != 0) {
Log_Debug("ERROR: I2CMaster_SetBusSpeed: errno=%d (%s)\n", errno, strerror(errno));
return -1;
}
result = I2CMaster_SetTimeout(i2cFd, 100);
if (result != 0) {
Log_Debug("ERROR: I2CMaster_SetTimeout: errno=%d (%s)\n", errno, strerror(errno));
return -1;
}
// Start lsm6dso specific init
// Initialize lsm6dso mems driver interface
dev_ctx.write_reg = platform_write;
dev_ctx.read_reg = platform_read;
dev_ctx.handle = &i2cFd;
// Check device ID
lsm6dso_device_id_get(&dev_ctx, &whoamI);
if (whoamI != LSM6DSO_ID) {
Log_Debug("LSM6DSO not found!\n");
return -1;
}
else {
Log_Debug("LSM6DSO Found!\n");
}
// Restore default configuration
lsm6dso_reset_set(&dev_ctx, PROPERTY_ENABLE);
do {
lsm6dso_reset_get(&dev_ctx, &rst);
} while (rst);
// Disable I3C interface
lsm6dso_i3c_disable_set(&dev_ctx, LSM6DSO_I3C_DISABLE);
// Enable Block Data Update
lsm6dso_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
// Set Output Data Rate
lsm6dso_xl_data_rate_set(&dev_ctx, LSM6DSO_XL_ODR_12Hz5);
lsm6dso_gy_data_rate_set(&dev_ctx, LSM6DSO_GY_ODR_12Hz5);
// Set full scale
lsm6dso_xl_full_scale_set(&dev_ctx, LSM6DSO_4g);
lsm6dso_gy_full_scale_set(&dev_ctx, LSM6DSO_2000dps);
// Configure filtering chain(No aux interface)
// Accelerometer - LPF1 + LPF2 path
lsm6dso_xl_hp_path_on_out_set(&dev_ctx, LSM6DSO_LP_ODR_DIV_100);
lsm6dso_xl_filter_lp2_set(&dev_ctx, PROPERTY_ENABLE);
// lps22hh specific init
// Default the flag to false. If we fail to communicate with the LPS22HH device, this flag
// will cause application execution to skip over LPS22HH specific code.
lps22hhDetected = false;
// Initialize lps22hh mems driver interface
pressure_ctx.read_reg = lsm6dso_read_lps22hh_cx;
pressure_ctx.write_reg = lsm6dso_write_lps22hh_cx;
pressure_ctx.handle = &i2cFd;
int failCount = 10;
while (!lps22hhDetected) {
// Enable pull up on master I2C interface.
lsm6dso_sh_pin_mode_set(&dev_ctx, LSM6DSO_INTERNAL_PULL_UP);
// Check if LPS22HH is connected to Sensor Hub
lps22hh_device_id_get(&pressure_ctx, &whoamI);
if (whoamI != LPS22HH_ID) {
Log_Debug("LPS22HH not found!\n");
}
else {
lps22hhDetected = true;
Log_Debug("LPS22HH Found!\n");
}
// Restore the default configuration
lps22hh_reset_set(&pressure_ctx, PROPERTY_ENABLE);
do {
lps22hh_reset_get(&pressure_ctx, &rst);
} while (rst);
// Enable Block Data Update
lps22hh_block_data_update_set(&pressure_ctx, PROPERTY_ENABLE);
//Set Output Data Rate
lps22hh_data_rate_set(&pressure_ctx, LPS22HH_10_Hz_LOW_NOISE);
// If we failed to detect the lps22hh device, then pause before trying again.
if (!lps22hhDetected) {
HAL_Delay(100);
}
if (failCount-- == 0) {
bool lps22hhDetected = false;
Log_Debug("Failed to read LPS22HH device ID, disabling all access to LPS22HH device!\n");
Log_Debug("Usually a power cycle will correct this issue\n");
break;
}
}
// Read the raw angular rate data from the device to use as offsets. We're making the assumption that the device
// is stationary.
uint8_t reg;
Log_Debug("LSM6DSO: Calibrating angular rate . . .\n");
Log_Debug("LSM6DSO: Please make sure the device is stationary.\n");
do {
// Delay and read the device until we have data!
do {
// Read the calibration values
HAL_Delay(5000);
lsm6dso_gy_flag_data_ready_get(&dev_ctx, ®);
} while (!reg);
if (reg)
{
// Read angular rate field data to use for calibration offsets
memset(data_raw_angular_rate.u8bit, 0x00, 3 * sizeof(int16_t));
lsm6dso_angular_rate_raw_get(&dev_ctx, raw_angular_rate_calibration.u8bit);
}
// Delay and read the device until we have data!
do {
// Read the calibration values
HAL_Delay(5000);
lsm6dso_gy_flag_data_ready_get(&dev_ctx, ®);
} while (!reg);
// Read the angular data rate again and verify that after applying the calibration, we have 0 angular rate in all directions
if (reg)
{
// Read angular rate field data
memset(data_raw_angular_rate.u8bit, 0x00, 3 * sizeof(int16_t));
lsm6dso_angular_rate_raw_get(&dev_ctx, data_raw_angular_rate.u8bit);
// Before we store the mdps values subtract the calibration data we captured at startup.
angular_rate_dps[0] = lsm6dso_from_fs2000_to_mdps(data_raw_angular_rate.i16bit[0] - raw_angular_rate_calibration.i16bit[0]);
angular_rate_dps[1] = lsm6dso_from_fs2000_to_mdps(data_raw_angular_rate.i16bit[1] - raw_angular_rate_calibration.i16bit[1]);
angular_rate_dps[2] = lsm6dso_from_fs2000_to_mdps(data_raw_angular_rate.i16bit[2] - raw_angular_rate_calibration.i16bit[2]);
}
// If the angular values after applying the offset are not all 0.0s, then do it again!
} while ((angular_rate_dps[0] != 0.0) || (angular_rate_dps[1] != 0.0) || (angular_rate_dps[2] != 0.0));
Log_Debug("LSM6DSO: Calibrating angular rate complete!\n");
return 0;
}
/// <summary>
/// Closes the I2C interface File Descriptors.
/// </summary>
void closeI2cDevice(void) {
CloseFdAndPrintError(i2cFd, "i2c");
}
/// <summary>
/// Writes data to the lsm6dso i2c device
/// </summary>
/// <returns>0</returns>
static int32_t platform_write(int *fD, uint8_t reg, uint8_t *bufp,
uint16_t len)
{
#ifdef ENABLE_READ_WRITE_DEBUG
Log_Debug("platform_write()\n");
Log_Debug("reg: %0x\n", reg);
Log_Debug("len: %0x\n", len);
Log_Debug("bufp contents: ");
for (int i = 0; i < len; i++) {
Log_Debug("%0x: ", bufp[i]);
}
Log_Debug("\n");
#endif
// Construct a new command buffer that contains the register to write to, then the data to write
uint8_t cmdBuffer[len + 1];
cmdBuffer[0] = reg;
for (int i = 0; i < len; i++) {
cmdBuffer[i + 1] = bufp[i];
}
#ifdef ENABLE_READ_WRITE_DEBUG
Log_Debug("cmdBuffer contents: ");
for (int i = 0; i < len + 1; i++) {
Log_Debug("%0x: ", cmdBuffer[i]);
}
Log_Debug("\n");
#endif
// Write the data to the device
int32_t retVal = I2CMaster_Write(*fD, lsm6dsOAddress, cmdBuffer, (size_t)len + 1);
if (retVal < 0) {
Log_Debug("ERROR: platform_write: errno=%d (%s)\n", errno, strerror(errno));
return -1;
}
#ifdef ENABLE_READ_WRITE_DEBUG
Log_Debug("Wrote %d bytes to device.\n\n", retVal);
#endif
return 0;
}
/// <summary>
/// Reads generic device register from the i2c interface
/// </summary>
/// <returns>0</returns>
/*
* @brief Read generic device register (platform dependent)
*
* @param handle customizable argument. In this examples is used in
* order to select the correct sensor bus handler.
* @param reg register to read
* @param bufp pointer to buffer that store the data read
* @param len number of consecutive register to read
*
*/
static int32_t platform_read(int *fD, uint8_t reg, uint8_t *bufp,
uint16_t len)
{
#ifdef ENABLE_READ_WRITE_DEBUG
Log_Debug("platform_read()\n");
Log_Debug("reg: %0x\n", reg);
Log_Debug("len: %d\n", len);
;
#endif
// Set the register address to read
int32_t retVal = I2CMaster_Write(i2cFd, lsm6dsOAddress, ®, 1);
if (retVal < 0) {
Log_Debug("ERROR: platform_read(write step): errno=%d (%s)\n", errno, strerror(errno));
return -1;
}
// Read the data into the provided buffer
retVal = I2CMaster_Read(i2cFd, lsm6dsOAddress, bufp, len);
if (retVal < 0) {
Log_Debug("ERROR: platform_read(read step): errno=%d (%s)\n", errno, strerror(errno));
return -1;
}
#ifdef ENABLE_READ_WRITE_DEBUG
Log_Debug("Read returned: ");
for (int i = 0; i < len; i++) {
Log_Debug("%0x: ", bufp[i]);
}
Log_Debug("\n\n");
#endif
return 0;
}
/*
* @brief Write lsm2mdl device register (used by configuration functions)
*
* @param handle customizable argument. In this examples is used in
* order to select the correct sensor bus handler.
* @param reg register to write
* @param bufp pointer to data to write in register reg
* @param len number of consecutive register to write
*
*/
static int32_t lsm6dso_write_lps22hh_cx(void* ctx, uint8_t reg, uint8_t* data,
uint16_t len)
{
axis3bit16_t data_raw_acceleration;
int32_t ret;
uint8_t drdy;
lsm6dso_status_master_t master_status;
lsm6dso_sh_cfg_write_t sh_cfg_write;
// Configure Sensor Hub to write to the LPS22HH, and send the write data
sh_cfg_write.slv0_add = (LPS22HH_I2C_ADD_L & 0xFEU) >> 1; // 7bit I2C address
sh_cfg_write.slv0_subadd = reg,
sh_cfg_write.slv0_data = *data,
ret = lsm6dso_sh_cfg_write(&dev_ctx, &sh_cfg_write);
/* Disable accelerometer. */
lsm6dso_xl_data_rate_set(&dev_ctx, LSM6DSO_XL_ODR_OFF);
/* Enable I2C Master. */
lsm6dso_sh_master_set(&dev_ctx, PROPERTY_ENABLE);
/* Enable accelerometer to trigger Sensor Hub operation. */
lsm6dso_xl_data_rate_set(&dev_ctx, LSM6DSO_XL_ODR_104Hz);
/* Wait Sensor Hub operation flag set. */
lsm6dso_acceleration_raw_get(&dev_ctx, data_raw_acceleration.u8bit);
do
{
HAL_Delay(20);
lsm6dso_xl_flag_data_ready_get(&dev_ctx, &drdy);
} while (!drdy);
do
{
HAL_Delay(20);
lsm6dso_sh_status_get(&dev_ctx, &master_status);
} while (!master_status.sens_hub_endop);
/* Disable I2C master and XL (trigger). */
lsm6dso_sh_master_set(&dev_ctx, PROPERTY_DISABLE);
lsm6dso_xl_data_rate_set(&dev_ctx, LSM6DSO_XL_ODR_OFF);
return ret;
}
/*
* @brief Read lsm2mdl device register (used by configuration functions)
*
* @param handle customizable argument. In this examples is used in
* order to select the correct sensor bus handler.
* @param reg register to read
* @param bufp pointer to buffer that store the data read
* @param len number of consecutive register to read
*
*/
static int32_t lsm6dso_read_lps22hh_cx(void* ctx, uint8_t reg, uint8_t* data, uint16_t len)
{
lsm6dso_sh_cfg_read_t sh_cfg_read;
axis3bit16_t data_raw_acceleration;
int32_t ret;
uint8_t drdy;
lsm6dso_status_master_t master_status;
/* Disable accelerometer. */
lsm6dso_xl_data_rate_set(&dev_ctx, LSM6DSO_XL_ODR_OFF);
// For each byte we need to read from the lps22hh
for (int i = 0; i < len; i++) {
/* Configure Sensor Hub to read LPS22HH. */
sh_cfg_read.slv_add = (LPS22HH_I2C_ADD_L &0xFEU) >> 1; /* 7bit I2C address */
sh_cfg_read.slv_subadd = reg+i;
sh_cfg_read.slv_len = 1;
// Call the command to read the data from the sensor hub.
// This data will be read from the device connected to the
// sensor hub, and saved into a register for us to read.
ret = lsm6dso_sh_slv0_cfg_read(&dev_ctx, &sh_cfg_read);
lsm6dso_sh_slave_connected_set(&dev_ctx, LSM6DSO_SLV_0);
/* Enable I2C Master and I2C master. */
lsm6dso_sh_master_set(&dev_ctx, PROPERTY_ENABLE);
/* Enable accelerometer to trigger Sensor Hub operation. */
lsm6dso_xl_data_rate_set(&dev_ctx, LSM6DSO_XL_ODR_104Hz);
/* Wait Sensor Hub operation flag set. */
lsm6dso_acceleration_raw_get(&dev_ctx, data_raw_acceleration.u8bit);
do {
HAL_Delay(20);
lsm6dso_xl_flag_data_ready_get(&dev_ctx, &drdy);
} while (!drdy);
do {
HAL_Delay(20);
lsm6dso_sh_status_get(&dev_ctx, &master_status);
} while (!master_status.sens_hub_endop);
/* Disable I2C master and XL(trigger). */
lsm6dso_sh_master_set(&dev_ctx, PROPERTY_DISABLE);
lsm6dso_xl_data_rate_set(&dev_ctx, LSM6DSO_XL_ODR_OFF);
// Read the data from the device. The call below reads
// all 18 sensor hub data. We just need the data from
// sensor hub 1, so copy that into our data array.
uint8_t buffer[18];
lsm6dso_sh_read_data_raw_get(&dev_ctx, buffer);
data[i] = buffer[0];
#ifdef ENABLE_READ_WRITE_DEBUG
Log_Debug("Read %d bytes: ", len);
for (int i = 0; i < len; i++) {
Log_Debug("[%0x] ", data[i]);
}
Log_Debug("\n", len);
#endif
}
/* Re-enable accelerometer */
lsm6dso_xl_data_rate_set(&dev_ctx, LSM6DSO_XL_ODR_104Hz);
return ret;
}