forked from gschorcht/lis3mdl-esp-idf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lis3mdl.c
738 lines (576 loc) · 22.8 KB
/
lis3mdl.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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
/*
* Driver for LIS3MDL 3-axes digital magnetometer connected to I2C or SPI.
*
* This driver is for the usage with the ESP8266 and FreeRTOS (esp-open-rtos)
* [https://github.com/SuperHouse/esp-open-rtos]. It is also working with ESP32
* and ESP-IDF [https://github.com/espressif/esp-idf.git] as well as Linux
* based systems using a wrapper library for ESP8266 functions.
*
* ---------------------------------------------------------------------------
*
* The BSD License (3-clause license)
*
* Copyright (c) 2017 Gunar Schorcht (https://github.com/gschorcht)
* All rights reserved.
*
* 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 the copyright holder 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
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* 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.
*
* The information provided is believed to be accurate and reliable. The
* copyright holder assumes no responsibility for the consequences of use
* of such information nor for any infringement of patents or other rights
* of third parties which may result from its use. No license is granted by
* implication or otherwise under any patent or patent rights of the copyright
* holder.
*/
#include <string.h>
#include <stdlib.h>
#include "lis3mdl.h"
#if defined(LIS3MDL_DEBUG_LEVEL_2)
#define debug(s, f, ...) printf("%s %s: " s "\n", "LIS3MDL", f, ## __VA_ARGS__)
#define debug_dev(s, f, d, ...) printf("%s %s: bus %d, addr %02x - " s "\n", "LIS3MDL", f, d->bus, d->addr, ## __VA_ARGS__)
#else
#define debug(s, f, ...)
#define debug_dev(s, f, d, ...)
#endif
#if defined(LIS3MDL_DEBUG_LEVEL_1) || defined(LIS3MDL_DEBUG_LEVEL_2)
#define error(s, f, ...) printf("%s %s: " s "\n", "LIS3MDL", f, ## __VA_ARGS__)
#define error_dev(s, f, d, ...) printf("%s %s: bus %d, addr %02x - " s "\n", "LIS3MDL", f, d->bus, d->addr, ## __VA_ARGS__)
#else
#define error(s, f, ...)
#define error_dev(s, f, d, ...)
#endif
// register addresses
#define LIS3MDL_REG_WHO_AM_I 0x0f
#define LIS3MDL_REG_CTRL1 0x20
#define LIS3MDL_REG_CTRL2 0x21
#define LIS3MDL_REG_CTRL3 0x22
#define LIS3MDL_REG_CTRL4 0x23
#define LIS3MDL_REG_CTRL5 0x24
#define LIS3MDL_REG_STATUS 0x27
#define LIS3MDL_REG_OUT_X_L 0x28
#define LIS3MDL_REG_OUT_X_H 0x29
#define LIS3MDL_REG_OUT_Y_L 0x2a
#define LIS3MDL_REG_OUT_Y_H 0x2b
#define LIS3MDL_REG_OUT_Z_L 0x2c
#define LIS3MDL_REG_OUT_Z_H 0x2d
#define LIS3MDL_REG_TEMP_OUT_L 0x2e
#define LIS3MDL_REG_TEMP_OUT_H 0x2f
#define LIS3MDL_REG_INT_CFG 0x30
#define LIS3MDL_REG_INT_SRC 0x31
#define LIS3MDL_REG_INT_THS_L 0x32
#define LIS3MDL_REG_INT_THS_H 0x33
// register structure definitions
struct lis3mdl_reg_status
{
uint8_t XDA :1; // STATUS<0> X axis new data available
uint8_t YDA :1; // STATUS<1> Y axis new data available
uint8_t ZDA :1; // STATUS<2> Z axis new data available
uint8_t ZYXDA :1; // STATUS<3> X, Y and Z axis new data available
uint8_t XOR :1; // STATUS<4> X axis data overrun
uint8_t YOR :1; // STATUS<5> Y axis data overrun
uint8_t ZOR :1; // STATUS<6> Z axis data overrun
uint8_t ZYXOR :1; // STATUS<7> X, Y and Z axis data overrun
};
#define LIS3MDL_ANY_DATA_READY 0x0f // LIS3MDL_REG_STATUS<3:0>
struct lis3mdl_reg_ctrl1
{
uint8_t ST :1; // CTRL1<0> Self-test enable
uint8_t FAST_ODR :1; // CTRL1<1> Data rates higher 80 Hz enabled
uint8_t DO :3; // CTRL1<4:2> Output data rate
uint8_t OM :2; // CTRL1<6:5> X and Y axes operative mode
uint8_t TEMP_EN :1; // CTRL1<7> Temperature sensor enabled
};
struct lis3mdl_reg_ctrl2
{
uint8_t unused1 :2; // CTRL2<1:0> unused
uint8_t SOFT_RST :1; // CTRL2<2> configuration and user regs reset
uint8_t REBOOT :1; // CTRL2<3> Reboot memory content
uint8_t unused2 :1; // CTRL2<4> unused
uint8_t FS :2; // CTRL2<6:5>
uint8_t unused3 :1; // CTRL2<7> unused
};
struct lis3mdl_reg_ctrl3
{
uint8_t MD :2; // CTRL3<1:0> Operation mode selection
uint8_t SIM :1; // CTRL3<2> SPI serial interface mode selection
uint8_t unused1 :2; // CTRL3<4:3> unused
uint8_t LP :1; // CTRL3<5> Low power mode configuration
uint8_t unused2 :2; // CTRL3<7:6> unused
};
struct lis3mdl_reg_ctrl4
{
uint8_t unused1 :1; // CTRL4<0> unused
uint8_t BLE :1; // CTRL4<1> Big/litle endian data selection
uint8_t OMZ :2; // CTRL4<3:2> Z axis operative mode
uint8_t unused2 :4; // CTRL4<7:4> unused
};
struct lis3mdl_reg_ctrl5
{
uint8_t unused :6; // CTRL5<5:0> unused
uint8_t BDU :1; // CTRL5<6> Block data update
uint8_t FAST_READ:1; // CTRL5<7> Fast read enabled
};
struct lis3mdl_reg_int_cfg
{
uint8_t IEN :1; // INT_CFG<0> Interrupt enabled
uint8_t LIR :1; // INT_CFG<1> Latch interrupt request
uint8_t IEA :1; // INT_CFG<2> Interrupt active
uint8_t unused :2; // INT_CFG<4:3> unused
uint8_t ZIEN :1; // INT_CFG<5> Z axis threshold interrupt enabled
uint8_t YIEN :1; // INT_CFG<6> Y axis threshold interrupt enabled
uint8_t XIEN :1; // INT_CFG<7> X axis threshold interrupt enabled
};
struct lis3mdl_reg_int_src
{
uint8_t PTH_X :1; // INT_SRC<0> X exceeds threshold on positive side
uint8_t PTH_Y :1; // INT_SRC<1> Y exceeds threshold on positive side
uint8_t PTH_Z :1; // INT_SRC<2> Z exceeds threshold on positive side
uint8_t NTH_X :1; // INT_SRC<3> X exceeds threshold on negative side
uint8_t NTH_Y :1; // INT_SRC<4> Y exceeds threshold on negative side
uint8_t NTH_Z :1; // INT_SRC<5> Z exceeds threshold on negative side
uint8_t MROI :1; // INT_SRC<6> Internal measurement range overflow
uint8_t INT :1; // INT_SRC<7> Interrupt event occurs
};
/** Forward declaration of functions for internal use */
static bool lis3mdl_reset (lis3mdl_sensor_t* dev);
static bool lis3mdl_is_available(lis3mdl_sensor_t* dev);
static bool lis3mdl_i2c_read (lis3mdl_sensor_t* dev, uint8_t reg, uint8_t *data, uint16_t len);
static bool lis3mdl_i2c_write (lis3mdl_sensor_t* dev, uint8_t reg, uint8_t *data, uint16_t len);
static bool lis3mdl_spi_read (lis3mdl_sensor_t* dev, uint8_t reg, uint8_t *data, uint16_t len);
static bool lis3mdl_spi_write (lis3mdl_sensor_t* dev, uint8_t reg, uint8_t *data, uint16_t len);
#define msb_lsb_to_type(t,b,o) (t)(((t)b[o] << 8) | b[o+1])
#define lsb_msb_to_type(t,b,o) (t)(((t)b[o+1] << 8) | b[o])
#define lsb_to_type(t,b,o) (t)(b[o])
#define lis3mdl_update_reg(dev,addr,type,elem,value) \
{ \
struct type __reg; \
if (!lis3mdl_reg_read (dev, (addr), (uint8_t*)&__reg, 1)) \
return false; \
__reg.elem = (value); \
if (!lis3mdl_reg_write (dev, (addr), (uint8_t*)&__reg, 1)) \
return false; \
}
lis3mdl_sensor_t* lis3mdl_init_sensor (uint8_t bus, uint8_t addr, uint8_t cs, bool reset)
{
lis3mdl_sensor_t* dev;
if ((dev = malloc (sizeof(lis3mdl_sensor_t))) == NULL)
return NULL;
// init sensor data structure
dev->bus = bus;
dev->addr = addr;
dev->cs = cs;
dev->error_code = LIS3MDL_OK;
dev->scale = lis3mdl_scale_4_Gs;
// if addr==0 then SPI is used and has to be initialized
if (!addr && !spi_device_init (bus, cs))
{
error_dev ("Could not initialize SPI interface.", __FUNCTION__, dev);
free (dev);
return NULL;
}
// check availability of the sensor
if (!lis3mdl_is_available (dev))
{
error_dev ("Sensor is not available.", __FUNCTION__, dev);
free (dev);
return NULL;
}
// Reset the sensor only if reset flag is true
if (reset && !lis3mdl_reset(dev))
{
error_dev ("Could not reset the sensor device.", __FUNCTION__, dev);
free (dev);
return NULL;
}
lis3mdl_update_reg (dev, LIS3MDL_REG_CTRL2, lis3mdl_reg_ctrl2, FS, lis3mdl_scale_4_Gs);
lis3mdl_update_reg (dev, LIS3MDL_REG_CTRL5, lis3mdl_reg_ctrl5, BDU, 1);
return dev;
}
// switching times
// LP 0.90
// MP 1.65
// HP 3.23
// UHP 6.40
bool lis3mdl_set_mode (lis3mdl_sensor_t* dev, lis3mdl_mode_t mode)
{
if (!dev) return false;
dev->error_code = LIS3MDL_OK;
struct lis3mdl_reg_ctrl1 ctrl1;
struct lis3mdl_reg_ctrl3 ctrl3;
struct lis3mdl_reg_ctrl4 ctrl4;
// read current register values
if (!lis3mdl_reg_read (dev, LIS3MDL_REG_CTRL1, (uint8_t*)&ctrl1, 1) ||
!lis3mdl_reg_read (dev, LIS3MDL_REG_CTRL3, (uint8_t*)&ctrl3, 1) ||
!lis3mdl_reg_read (dev, LIS3MDL_REG_CTRL4, (uint8_t*)&ctrl4, 1))
return false;
if (mode < lis3mdl_lpm_1000)
{
ctrl1.FAST_ODR = 0;
ctrl3.MD = 0; // continuous measurement
ctrl3.LP = 0;
ctrl1.DO = mode;
ctrl1.OM = 0;
ctrl4.OMZ = ctrl1.OM;
}
else if (mode < lis3mdl_low_power)
{
ctrl1.FAST_ODR = 1;
ctrl3.MD = 0; // continuous measurement
ctrl3.LP = 0;
ctrl1.DO = 0;
ctrl1.OM = mode - lis3mdl_lpm_1000;
ctrl4.OMZ = ctrl1.OM;
}
else if (mode == lis3mdl_low_power)
{
ctrl1.FAST_ODR = 0;
ctrl3.MD = 0; // continuous measurement
ctrl3.LP = 1; // at lowest data rate 0.625 Hz
ctrl1.DO = 0;
ctrl1.OM = 0;
ctrl4.OMZ = ctrl1.OM;
}
else // lis3mdl_power_down
{
ctrl3.MD = 3;
}
if (!lis3mdl_reg_write (dev, LIS3MDL_REG_CTRL1, (uint8_t*)&ctrl1, 1) ||
!lis3mdl_reg_write (dev, LIS3MDL_REG_CTRL3, (uint8_t*)&ctrl3, 1) ||
!lis3mdl_reg_write (dev, LIS3MDL_REG_CTRL4, (uint8_t*)&ctrl4, 1))
return false;
// wait until mode switch happened
vTaskDelay (50/portTICK_PERIOD_MS);
// dummy read last data register set
lis3mdl_raw_data_t raw;
lis3mdl_get_raw_data (dev, &raw);
return false;
}
bool lis3mdl_set_scale (lis3mdl_sensor_t* dev, lis3mdl_scale_t scale)
{
if (!dev) return false;
dev->error_code = LIS3MDL_OK;
dev->scale = scale;
// read CTRL2 register and write scale
lis3mdl_update_reg (dev, LIS3MDL_REG_CTRL2, lis3mdl_reg_ctrl2, FS, scale);
return true;
}
bool lis3mdl_new_data (lis3mdl_sensor_t* dev)
{
if (!dev) return false;
dev->error_code = LIS3MDL_OK;
struct lis3mdl_reg_status status;
if (!lis3mdl_reg_read (dev, LIS3MDL_REG_STATUS, (uint8_t*)&status, 1))
{
error_dev ("Could not get sensor status", __FUNCTION__, dev);
return false;
}
return status.ZYXDA;
}
/**
* Scaling factors for the conversion of raw sensor data to floating point _Gs
* values. Scaling factors are from mechanical characteristics in datasheet.
*
* scale/sensitivity resolution sensitivity
* +-4 gauss 6842 LSB/gauss 1.461561e-4
* +-8 gauss 3421 LSB/gauss 2,923122e-4
* +-12 gauss 2281 LSB/gauss 4,384042e-4
* +-16 gauss 1711 LSB/gauss 5,844535e-4
*/
const static double LIS3MDL_SCALES[4] = { 1.0/6842, 1.0/3421, 1.0/2281, 1.0/1711 };
bool lis3mdl_get_float_data (lis3mdl_sensor_t* dev, lis3mdl_float_data_t* data)
{
if (!dev || !data) return false;
lis3mdl_raw_data_t raw;
if (!lis3mdl_get_raw_data (dev, &raw))
return false;
data->mx = LIS3MDL_SCALES[dev->scale] * raw.mx;
data->my = LIS3MDL_SCALES[dev->scale] * raw.my;
data->mz = LIS3MDL_SCALES[dev->scale] * raw.mz;
return true;
}
bool lis3mdl_get_raw_data (lis3mdl_sensor_t* dev, lis3mdl_raw_data_t* raw)
{
if (!dev || !raw) return false;
dev->error_code = LIS3MDL_OK;
uint8_t regs[6];
// read raw data sample
if (!lis3mdl_reg_read (dev, LIS3MDL_REG_OUT_X_L, regs, 6))
{
error_dev ("Could not get raw data sample", __FUNCTION__, dev);
dev->error_code |= LIS3MDL_GET_RAW_DATA_FAILED;
return false;
}
raw->mx = ((uint16_t)regs[1] << 8) | regs[0];
raw->my = ((uint16_t)regs[3] << 8) | regs[2];
raw->mz = ((uint16_t)regs[5] << 8) | regs[4];
return true;
}
bool lis3mdl_set_int_config (lis3mdl_sensor_t* dev,
lis3mdl_int_config_t* cfg)
{
if (!dev || !cfg) return false;
dev->error_code = LIS3MDL_OK;
struct lis3mdl_reg_int_cfg int_cfg;
int_cfg.unused = 0;
int_cfg.XIEN = cfg->x_enabled;
int_cfg.YIEN = cfg->y_enabled;
int_cfg.ZIEN = cfg->z_enabled;
int_cfg.LIR = cfg->latch;
int_cfg.IEA = cfg->signal_level;
int_cfg.IEN = cfg->x_enabled | cfg->y_enabled | cfg->z_enabled;
if (// write the threshold to registers INT_THS_*
!lis3mdl_reg_write (dev, LIS3MDL_REG_INT_THS_L, (uint8_t*)&cfg->threshold, 2) ||
// write configuration to INT_CFG
!lis3mdl_reg_write (dev, LIS3MDL_REG_INT_CFG, (uint8_t*)&int_cfg, 1))
{
error_dev ("Could not configure interrupt INT", __FUNCTION__, dev);
dev->error_code |= LIS3MDL_CONFIG_INT_FAILED;
return false;
}
return true;
}
bool lis3mdl_get_int_config (lis3mdl_sensor_t* dev,
lis3mdl_int_config_t* cfg)
{
if (!dev || !cfg) return false;
dev->error_code = LIS3MDL_OK;
struct lis3mdl_reg_int_cfg int_cfg;
if (!lis3mdl_reg_read (dev, LIS3MDL_REG_INT_THS_L, (uint8_t*)&cfg->threshold, 2) ||
!lis3mdl_reg_read (dev, LIS3MDL_REG_INT_CFG , (uint8_t*)&int_cfg, 1))
{
error_dev ("Could not read configuration of interrupt INT from sensor", __FUNCTION__, dev);
dev->error_code |= LIS3MDL_CONFIG_INT_FAILED;
return false;
}
cfg->x_enabled = int_cfg.XIEN;
cfg->y_enabled = int_cfg.YIEN;
cfg->z_enabled = int_cfg.ZIEN;
cfg->latch = int_cfg.LIR;
cfg->signal_level = int_cfg.IEA;
return true;
}
bool lis3mdl_get_int_source (lis3mdl_sensor_t* dev,
lis3mdl_int_source_t* src)
{
if (!dev || !src) return false;
dev->error_code = LIS3MDL_OK;
struct lis3mdl_reg_int_src int_src;
struct lis3mdl_reg_int_cfg int_cfg;
if (!lis3mdl_reg_read (dev, LIS3MDL_REG_INT_SRC, (uint8_t*)&int_src, 1) ||
!lis3mdl_reg_read (dev, LIS3MDL_REG_INT_CFG, (uint8_t*)&int_cfg, 1))
{
error_dev ("Could not read source of interrupt INT from sensor", __FUNCTION__, dev);
dev->error_code |= LIS3MDL_INT_SOURCE_FAILED;
return false;
}
src->active = int_src.INT;
src->x_pos = int_src.PTH_X & int_cfg.XIEN;
src->x_neg = int_src.NTH_X & int_cfg.XIEN;
src->y_pos = int_src.PTH_Y & int_cfg.YIEN;
src->y_neg = int_src.NTH_Y & int_cfg.YIEN;
src->z_pos = int_src.PTH_Z & int_cfg.ZIEN;
src->z_neg = int_src.NTH_Z & int_cfg.ZIEN;
return true;
}
bool lis3mdl_enable_temperature (lis3mdl_sensor_t* dev, bool enable)
{
lis3mdl_update_reg (dev, LIS3MDL_REG_CTRL1, lis3mdl_reg_ctrl1, TEMP_EN, enable);
return true;
}
float lis3mdl_get_temperature (lis3mdl_sensor_t* dev)
{
uint8_t regs[2];
// read raw data sample
if (!lis3mdl_reg_read (dev, LIS3MDL_REG_TEMP_OUT_L, regs, 2))
{
error_dev ("Could not get temperature data sample", __FUNCTION__, dev);
dev->error_code |= LIS3MDL_GET_RAW_DATA_FAILED;
return false;
}
return (((int16_t)((regs[1] << 8) | regs[0])) >> 3) + 25.0;
}
/** Functions for internal use only */
/**
* @brief Check the chip ID to test whether sensor is available
*/
static bool lis3mdl_is_available (lis3mdl_sensor_t* dev)
{
uint8_t chip_id;
if (!dev) return false;
dev->error_code = LIS3MDL_OK;
if (!lis3mdl_reg_read (dev, LIS3MDL_REG_WHO_AM_I, &chip_id, 1))
return false;
if (chip_id != LIS3MDL_CHIP_ID)
{
error_dev ("Chip id %02x is wrong, should be %02x.",
__FUNCTION__, dev, chip_id, LIS3MDL_CHIP_ID);
dev->error_code = LIS3MDL_WRONG_CHIP_ID;
return false;
}
return true;
}
static bool lis3mdl_reset (lis3mdl_sensor_t* dev)
{
if (!dev) return false;
dev->error_code = LIS3MDL_OK;
uint8_t ctrl_regs[5] = { 0x10, 0x00, 0x03, 0x00, 0x00 };
uint8_t int_cfg = 0x00;
// initialize sensor completely including setting in power down mode
lis3mdl_reg_write (dev, LIS3MDL_REG_CTRL1 , ctrl_regs, 5);
lis3mdl_reg_write (dev, LIS3MDL_REG_INT_CFG, &int_cfg , 1);
return true;
}
bool lis3mdl_reg_read(lis3mdl_sensor_t* dev, uint8_t reg, uint8_t *data, uint16_t len)
{
if (!dev || !data) return false;
return (dev->addr) ? lis3mdl_i2c_read (dev, reg, data, len)
: lis3mdl_spi_read (dev, reg, data, len);
}
bool lis3mdl_reg_write(lis3mdl_sensor_t* dev, uint8_t reg, uint8_t *data, uint16_t len)
{
if (!dev || !data) return false;
return (dev->addr) ? lis3mdl_i2c_write (dev, reg, data, len)
: lis3mdl_spi_write (dev, reg, data, len);
}
#define LIS3MDL_SPI_BUF_SIZE 64 // SPI register data buffer size of ESP866
#define LIS3MDL_SPI_READ_FLAG 0x80
#define LIS3MDL_SPI_WRITE_FLAG 0x00
#define LIS3MDL_SPI_AUTO_INC_FLAG 0x40
static bool lis3mdl_spi_read(lis3mdl_sensor_t* dev, uint8_t reg, uint8_t *data, uint16_t len)
{
if (!dev || !data) return false;
if (len >= LIS3MDL_SPI_BUF_SIZE)
{
dev->error_code |= LIS3MDL_SPI_BUFFER_OVERFLOW;
error_dev ("Error on read from SPI slave on bus 1. Tried to transfer "
"more than %d byte in one read operation.",
__FUNCTION__, dev, LIS3MDL_SPI_BUF_SIZE);
return false;
}
uint8_t addr = (reg & 0x3f) | LIS3MDL_SPI_READ_FLAG | LIS3MDL_SPI_AUTO_INC_FLAG;
static uint8_t mosi[LIS3MDL_SPI_BUF_SIZE];
static uint8_t miso[LIS3MDL_SPI_BUF_SIZE];
memset (mosi, 0xff, LIS3MDL_SPI_BUF_SIZE);
memset (miso, 0xff, LIS3MDL_SPI_BUF_SIZE);
mosi[0] = addr;
if (!spi_transfer_pf (dev->bus, dev->cs, mosi, miso, len+1))
{
error_dev ("Could not read data from SPI", __FUNCTION__, dev);
dev->error_code |= LIS3MDL_SPI_READ_FAILED;
return false;
}
// shift data one by left, first byte received while sending register address is invalid
for (int i=0; i < len; i++)
data[i] = miso[i+1];
#ifdef LIS3MDL_DEBUG_LEVEL_2
printf("LIS3MDL %s: read the following bytes from reg %02x: ", __FUNCTION__, reg);
for (int i=0; i < len; i++)
printf("%02x ", data[i]);
printf("\n");
#endif
return true;
}
static bool lis3mdl_spi_write(lis3mdl_sensor_t* dev, uint8_t reg, uint8_t *data, uint16_t len)
{
if (!dev || !data) return false;
uint8_t addr = (reg & 0x3f) | LIS3MDL_SPI_WRITE_FLAG | LIS3MDL_SPI_AUTO_INC_FLAG;
static uint8_t mosi[LIS3MDL_SPI_BUF_SIZE];
if (len >= LIS3MDL_SPI_BUF_SIZE)
{
dev->error_code |= LIS3MDL_SPI_BUFFER_OVERFLOW;
error_dev ("Error on write to SPI slave on bus 1. Tried to transfer more"
"than %d byte in one write operation.",
__FUNCTION__, dev, LIS3MDL_SPI_BUF_SIZE);
return false;
}
reg &= 0x7f;
// first byte in output is the register address
mosi[0] = addr;
// shift data one byte right, first byte in output is the register address
for (int i = 0; i < len; i++)
mosi[i+1] = data[i];
#ifdef LIS3MDL_DEBUG_LEVEL_2
printf("LIS3MDL %s: Write the following bytes to reg %02x: ", __FUNCTION__, reg);
for (int i = 1; i < len+1; i++)
printf("%02x ", mosi[i]);
printf("\n");
#endif
if (!spi_transfer_pf (dev->bus, dev->cs, mosi, NULL, len+1))
{
error_dev ("Could not write data to SPI.", __FUNCTION__, dev);
dev->error_code |= LIS3MDL_SPI_WRITE_FAILED;
return false;
}
return true;
}
#define I2C_AUTO_INCREMENT (0x80)
static bool lis3mdl_i2c_read(lis3mdl_sensor_t* dev, uint8_t reg, uint8_t *data, uint16_t len)
{
if (!dev || !data) return false;
debug_dev ("Read %d byte from i2c slave register %02x.", __FUNCTION__, dev, len, reg);
if (len > 1)
reg |= I2C_AUTO_INCREMENT;
int result = i2c_slave_read(dev->bus, dev->addr, ®, data, len);
if (result)
{
dev->error_code |= (result == -EBUSY) ? LIS3MDL_I2C_BUSY : LIS3MDL_I2C_READ_FAILED;
error_dev ("Error %d on read %d byte from I2C slave register %02x.",
__FUNCTION__, dev, result, len, reg);
return false;
}
# ifdef LIS3MDL_DEBUG_LEVEL_2
printf("LIS3MDL %s: Read following bytes: ", __FUNCTION__);
printf("%02x: ", reg & 0x7f);
for (int i=0; i < len; i++)
printf("%02x ", data[i]);
printf("\n");
# endif
return true;
}
static bool lis3mdl_i2c_write(lis3mdl_sensor_t* dev, uint8_t reg, uint8_t *data, uint16_t len)
{
if (!dev || !data) return false;
debug_dev ("Write %d byte to i2c slave register %02x.", __FUNCTION__, dev, len, reg);
if (len > 1)
reg |= I2C_AUTO_INCREMENT;
int result = i2c_slave_write(dev->bus, dev->addr, ®, data, len);
if (result)
{
dev->error_code |= (result == -EBUSY) ? LIS3MDL_I2C_BUSY : LIS3MDL_I2C_WRITE_FAILED;
error_dev ("Error %d on write %d byte to i2c slave register %02x.",
__FUNCTION__, dev, result, len, reg);
return false;
}
# ifdef LIS3MDL_DEBUG_LEVEL_2
printf("LIS3MDL %s: Wrote the following bytes: ", __FUNCTION__);
printf("%02x: ", reg & 0x7f);
for (int i=0; i < len; i++)
printf("%02x ", data[i]);
printf("\n");
# endif
return true;
}