-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstts751_reg.c
More file actions
755 lines (631 loc) · 18.5 KB
/
stts751_reg.c
File metadata and controls
755 lines (631 loc) · 18.5 KB
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
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
/**
******************************************************************************
* @file stts751_reg.c
* @author Sensors Software Solution Team
* @brief STTS751 driver file
******************************************************************************
* @attention
*
* Copyright (c) 2021 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
#include "stts751_reg.h"
/**
* @defgroup STTS751
* @brief This file provides a set of functions needed to drive the
* stts751 enhanced inertial module.
* @{
*
*/
/**
* @defgroup STTS751_Interfaces_Functions
* @brief This section provide a set of functions used to read and
* write a generic register of the device.
* MANDATORY: return 0 -> no Error.
* @{
*
*/
/**
* @brief Read generic device register
*
* @param ctx read / write interface definitions(ptr)
* @param reg register to read
* @param data pointer to buffer that store the data read(ptr)
* @param len number of consecutive register to read
* @retval interface status (MANDATORY: return 0 -> no Error)
*
*/
int32_t __weak stts751_read_reg(const stmdev_ctx_t *ctx, uint8_t reg,
uint8_t *data,
uint16_t len)
{
int32_t ret;
if (ctx == NULL)
{
return -1;
}
ret = ctx->read_reg(ctx->handle, reg, data, len);
return ret;
}
/**
* @brief Write generic device register
*
* @param ctx read / write interface definitions(ptr)
* @param reg register to write
* @param data pointer to data to write in register reg(ptr)
* @param len number of consecutive register to write
* @retval interface status (MANDATORY: return 0 -> no Error)
*
*/
int32_t __weak stts751_write_reg(const stmdev_ctx_t *ctx, uint8_t reg,
uint8_t *data,
uint16_t len)
{
int32_t ret;
if (ctx == NULL)
{
return -1;
}
ret = ctx->write_reg(ctx->handle, reg, data, len);
return ret;
}
/**
* @}
*
*/
/**
* @defgroup STTS751_Sensitivity
* @brief These functions convert raw-data into engineering units and
* vice-versa .
* @{
*
*/
float_t stts751_from_lsb_to_celsius(int16_t lsb)
{
return ((float_t)lsb) / 256.0f;
}
/**
* @}
*
*/
/**
* @defgroup STTS751_Sensitivity_Reverse
* @brief This conversion is useful but not need by the driver.
*
* REMOVING this union you are compliant with:
* MISRA-C 2012 [Rule 10.8] -> " Explicit cast of composite
* expression "
*
* @{
*
*/
int16_t stts751_from_celsius_to_lsb(float_t celsius)
{
return (int16_t)(celsius * 256.0f);
}
/**
* @}
*
*/
/**
* @defgroup STTS751_Data_Generation
* @brief This section groups all the functions concerning
* data generation
* @{
*
*/
/**
* @brief Temperature sensor data rate selection.[set]
*
* @param ctx read / write interface definitions
* @param val change the sensor data rate
* @retval interface status (MANDATORY: return 0 -> no Error)
*
*/
int32_t stts751_temp_data_rate_set(const stmdev_ctx_t *ctx,
stts751_odr_t val)
{
stts751_configuration_t configuration;
stts751_conversion_rate_t conversion_rate;
uint8_t dummy_value = 0xAA;
int32_t ret;
ret = stts751_read_reg(ctx, STTS751_CONVERSION_RATE,
(uint8_t *)&conversion_rate, 1);
if (ret == 0)
{
conversion_rate.conv = (uint8_t)val & 0x0FU;
ret = stts751_write_reg(ctx, STTS751_CONVERSION_RATE,
(uint8_t *)&conversion_rate, 1);
}
if (ret == 0)
{
ret = stts751_read_reg(ctx, STTS751_CONFIGURATION,
(uint8_t *)&configuration, 1);
}
if (ret == 0)
{
configuration.stop = ((uint8_t)val & 0x80U) >> 7;
ret = stts751_write_reg(ctx, STTS751_CONFIGURATION,
(uint8_t *)&configuration, 1);
}
if ((ret == 0) && (val == STTS751_TEMP_ODR_ONE_SHOT))
{
ret = stts751_write_reg(ctx, STTS751_ONE_SHOT, &dummy_value, 1);
}
return ret;
}
/**
* @brief Temperature sensor data rate selection.[get]
*
* @param ctx read / write interface definitions
* @param val Get the sensor data rate
* @retval interface status (MANDATORY: return 0 -> no Error)
*
*/
int32_t stts751_temp_data_rate_get(const stmdev_ctx_t *ctx,
stts751_odr_t *val)
{
stts751_conversion_rate_t conversion_rate;
stts751_configuration_t configuration;
int32_t ret;
ret = stts751_read_reg(ctx, STTS751_CONVERSION_RATE,
(uint8_t *)&conversion_rate, 1);
ret += stts751_read_reg(ctx, STTS751_CONFIGURATION,
(uint8_t *)&configuration, 1);
if (ret != 0) { return ret; }
switch ((configuration.stop << 7) + conversion_rate.conv)
{
case STTS751_TEMP_ODR_OFF:
*val = STTS751_TEMP_ODR_OFF;
break;
case STTS751_TEMP_ODR_ONE_SHOT:
*val = STTS751_TEMP_ODR_ONE_SHOT;
break;
case STTS751_TEMP_ODR_62mHz5:
*val = STTS751_TEMP_ODR_62mHz5;
break;
case STTS751_TEMP_ODR_125mHz:
*val = STTS751_TEMP_ODR_125mHz;
break;
case STTS751_TEMP_ODR_250mHz:
*val = STTS751_TEMP_ODR_250mHz;
break;
case STTS751_TEMP_ODR_500mHz:
*val = STTS751_TEMP_ODR_500mHz;
break;
case STTS751_TEMP_ODR_1Hz:
*val = STTS751_TEMP_ODR_1Hz;
break;
case STTS751_TEMP_ODR_2Hz:
*val = STTS751_TEMP_ODR_2Hz;
break;
case STTS751_TEMP_ODR_4Hz:
*val = STTS751_TEMP_ODR_4Hz;
break;
case STTS751_TEMP_ODR_8Hz:
*val = STTS751_TEMP_ODR_8Hz;
break;
case STTS751_TEMP_ODR_16Hz:
*val = STTS751_TEMP_ODR_16Hz;
break;
case STTS751_TEMP_ODR_32Hz:
*val = STTS751_TEMP_ODR_32Hz;
break;
default:
*val = STTS751_TEMP_ODR_OFF;
break;
}
return ret;
}
/**
* @brief Temperature sensor resolution selection.[set]
*
* @param ctx read / write interface definitions
* @param val change the values of tres in reg CONFIGURATION
* @retval interface status (MANDATORY: return 0 -> no Error)
*
*/
int32_t stts751_resolution_set(const stmdev_ctx_t *ctx, stts751_tres_t val)
{
stts751_configuration_t reg;
int32_t ret;
ret = stts751_read_reg(ctx, STTS751_CONFIGURATION, (uint8_t *) ®, 1);
if (ret == 0)
{
reg.tres = (uint8_t) val;
ret = stts751_write_reg(ctx, STTS751_CONFIGURATION, (uint8_t *) ®, 1);
}
return ret;
}
/**
* @brief Temperature sensor resolution selection.[get]
*
* @param ctx read / write interface definitions
* @param val Get the values of tres in reg CONFIGURATION
* @retval interface status (MANDATORY: return 0 -> no Error)
*
*/
int32_t stts751_resolution_get(const stmdev_ctx_t *ctx, stts751_tres_t *val)
{
stts751_configuration_t reg;
int32_t ret;
ret = stts751_read_reg(ctx, STTS751_CONFIGURATION, (uint8_t *) ®, 1);
if (ret != 0) { return ret; }
switch (reg.tres)
{
case STTS751_9bit:
*val = STTS751_9bit;
break;
case STTS751_10bit:
*val = STTS751_10bit;
break;
case STTS751_11bit:
*val = STTS751_11bit;
break;
case STTS751_12bit:
*val = STTS751_12bit;
break;
default:
*val = STTS751_9bit;
break;
}
return ret;
}
/**
* @brief The STATUS_REG register of the device.[get]
*
* @param ctx read / write interface definitions
* @param val union of registers from STATUS to
* @retval interface status (MANDATORY: return 0 -> no Error)
*
*/
int32_t stts751_status_reg_get(const stmdev_ctx_t *ctx,
stts751_status_t *val)
{
int32_t ret;
ret = stts751_read_reg(ctx, STTS751_STATUS, (uint8_t *) val, 1);
return ret;
}
/**
* @brief Temperature sensor "conversion on-going" flag.[get]
*
* @param ctx read / write interface definitions
* @param val get the values of busy in reg STATUS
* @retval interface status (MANDATORY: return 0 -> no Error)
*
*/
int32_t stts751_flag_busy_get(const stmdev_ctx_t *ctx, uint8_t *val)
{
stts751_status_t reg;
int32_t ret;
ret = stts751_read_reg(ctx, STTS751_STATUS, (uint8_t *)®, 1);
if (ret != 0) { return ret; }
*val = reg.busy;
return ret;
}
/**
* @}
*
*/
/**
* @defgroup STTS751_Data_Output
* @brief This section groups all the data output functions.
* @{
*
*/
/**
* @brief Temperature data output register (r). L and H registers
* together express a 16-bit word in two's complement.[get]
*
* @param ctx read / write interface definitions
* @param buff buffer that stores data read
* @retval interface status (MANDATORY: return 0 -> no Error)
*
*/
int32_t stts751_temperature_raw_get(const stmdev_ctx_t *ctx, int16_t *val)
{
uint8_t buff[2];
int32_t ret;
ret = stts751_read_reg(ctx, STTS751_TEMPERATURE_HIGH,
(uint8_t *)&buff[1], 1);
ret += stts751_read_reg(ctx, STTS751_TEMPERATURE_LOW,
&buff[0], 1);
if (ret != 0) { return ret; }
*val = (int16_t)buff[1];
*val = (*val * 256) + (int16_t)buff[0];
return ret;
}
/**
* @}
*
*/
/**
* @defgroup STTS751_Interrupt_Pins
* @brief This section groups all the functions that manage event pin
* @{
*
*/
/**
* @brief Route interrupt signal threshold on event pad.[set]
*
* @param ctx read / write interface definitions
* @param val set mask1 bit in register CONFIGURATION.
* @retval interface status (MANDATORY: return 0 -> no Error)
*
*/
int32_t stts751_pin_event_route_set(const stmdev_ctx_t *ctx, uint8_t val)
{
stts751_configuration_t reg;
int32_t ret;
ret = stts751_read_reg(ctx, STTS751_CONFIGURATION, (uint8_t *)®, 1);
if (ret == 0)
{
reg.mask1 = val;
ret = stts751_write_reg(ctx, STTS751_CONFIGURATION, (uint8_t *)®, 1);
}
return ret;
}
/**
* @brief Route interrupt signal threshold on event pad.[get]
*
* @param ctx read / write interface definitions
* @param val get mask1 bit in register CONFIGURATION.
* @retval interface status (MANDATORY: return 0 -> no Error)
*
*/
int32_t stts751_pin_event_route_get(const stmdev_ctx_t *ctx, uint8_t *val)
{
stts751_configuration_t reg;
int32_t ret;
ret = stts751_read_reg(ctx, STTS751_CONFIGURATION, (uint8_t *)®, 1);
if (ret != 0) { return ret; }
*val = reg.mask1;
return ret;
}
/**
* @}
*
*/
/**
* @defgroup STTS751_Interrupt_on_threshold
* @brief This section groups all the functions that manage interrupt
* on threshold event
* @{
*
*/
/**
* @brief high temperature threshold.[set]
*
* @param ctx read / write interface definitions
* @param buff buffer that contains data to write
* @retval interface status (MANDATORY: return 0 -> no Error)
*
*/
int32_t stts751_high_temperature_threshold_set(const stmdev_ctx_t *ctx,
int16_t val)
{
uint8_t buff[2];
int32_t ret;
buff[0] = (uint8_t)((uint16_t)val / 256U);
buff[1] = (uint8_t)((uint16_t)val - (buff[1] * 256U));
ret = stts751_write_reg(ctx, STTS751_TEMPERATURE_HIGH_LIMIT_HIGH,
buff, 2);
return ret;
}
/**
* @brief high temperature threshold.[get]
*
* @param ctx read / write interface definitions
* @param buff buffer that stores data read
* @retval interface status (MANDATORY: return 0 -> no Error)
*
*/
int32_t stts751_high_temperature_threshold_get(const stmdev_ctx_t *ctx,
int16_t *val)
{
uint8_t buff[2];
int32_t ret;
ret = stts751_read_reg(ctx, STTS751_TEMPERATURE_HIGH_LIMIT_HIGH, buff,
2);
if (ret != 0) { return ret; }
*val = (int16_t)buff[0];
*val = (*val * 256) + (int16_t)buff[1];
return ret;
}
/**
* @brief low temperature threshold.[set]
*
* @param ctx read / write interface definitions
* @param buff buffer that contains data to write
* @retval interface status (MANDATORY: return 0 -> no Error)
*
*/
int32_t stts751_low_temperature_threshold_set(const stmdev_ctx_t *ctx,
int16_t val)
{
uint8_t buff[2];
int32_t ret;
buff[0] = (uint8_t)((uint16_t)val / 256U);
buff[1] = (uint8_t)((uint16_t)val - (buff[1] * 256U));
ret = stts751_write_reg(ctx, STTS751_TEMPERATURE_LOW_LIMIT_HIGH, buff,
2);
return ret;
}
/**
* @brief low temperature threshold.[get]
*
* @param ctx read / write interface definitions
* @param buff buffer that stores data read
* @retval interface status (MANDATORY: return 0 -> no Error)
*
*/
int32_t stts751_low_temperature_threshold_get(const stmdev_ctx_t *ctx,
int16_t *val)
{
uint8_t buff[2];
int32_t ret;
ret = stts751_read_reg(ctx, STTS751_TEMPERATURE_LOW_LIMIT_HIGH, buff,
2);
if (ret != 0) { return ret; }
*val = (int16_t)buff[0];
*val = (*val * 256) + (int16_t)buff[1];
return ret;
}
/**
* @}
*
*/
/**
* @defgroup STTS751 over temperature alarm
* @brief This section groups all the functions that manage
* over temperature alarm functionality.
* @{
*
*/
/**
* @brief Thermal Limit. 1 LSB = 1 degC (max 127 degC min -127 degC ).[set]
*
* @param ctx read / write interface definitions
* @param val change the values of reg THERM_LIMIT
* @retval interface status (MANDATORY: return 0 -> no Error)
*
*/
int32_t stts751_ota_thermal_limit_set(const stmdev_ctx_t *ctx, int8_t val)
{
int32_t ret;
ret = stts751_write_reg(ctx, STTS751_THERM_LIMIT, (uint8_t *)&val, 1);
return ret;
}
/**
* @brief Thermal Limit. 1 LSB = 1 degC (max 127 degC min -127 degC ).[get]
*
* @param ctx read / write interface definitions
* @param val get the values of reg THERM_LIMIT
* @retval interface status (MANDATORY: return 0 -> no Error)
*
*/
int32_t stts751_ota_thermal_limit_get(const stmdev_ctx_t *ctx, int8_t *val)
{
int32_t ret;
ret = stts751_read_reg(ctx, STTS751_THERM_LIMIT, (uint8_t *)val, 1);
return ret;
}
/**
* @brief Thermal hysteresis. 1 LSB = 1 degC.[set]
* max 127 degC min -127 degC.
*
* @param ctx read / write interface definitions
* @param val change the values of reg THERM_HYSTERESIS
* @retval interface status (MANDATORY: return 0 -> no Error)
*
*/
int32_t stts751_ota_thermal_hyst_set(const stmdev_ctx_t *ctx, int8_t val)
{
int32_t ret;
ret = stts751_write_reg(ctx, STTS751_THERM_HYSTERESIS,
(uint8_t *)&val, 1);
return ret;
}
/**
* @brief Thermal hysteresis. 1 LSB = 1 degC.[get]
* max 127 degC min -127 degC.
*
* @param ctx read / write interface definitions
* @param val get the values of reg THERM_HYSTERESIS
* @retval interface status (MANDATORY: return 0 -> no Error)
*
*/
int32_t stts751_ota_thermal_hyst_get(const stmdev_ctx_t *ctx, int8_t *val)
{
int32_t ret;
ret = stts751_read_reg(ctx, STTS751_THERM_HYSTERESIS, (uint8_t *)val, 1);
return ret;
}
/**
* @}
*
*/
/**
* @defgroup STTS751_Common
* @brief This section groups common useful functions.
* @{
*
*/
/**
* @brief SMBus timeout.At power-up, the STTS751 is configured with an
* SMBus timeout of 25 to 35 milliseconds.[set]
*
* @param ctx read / write interface definitions
* @param val set timeout bit in register SMBUS_TIMEOUT.
* @retval interface status (MANDATORY: return 0 -> no Error)
*
*/
int32_t stts751_smbus_timeout_set(const stmdev_ctx_t *ctx, uint8_t val)
{
stts751_smbus_timeout_t reg;
int32_t ret;
ret = stts751_read_reg(ctx, STTS751_SMBUS_TIMEOUT, (uint8_t *)®, 1);
if (ret == 0)
{
reg.timeout = val;
ret = stts751_write_reg(ctx, STTS751_SMBUS_TIMEOUT, (uint8_t *)®, 1);
}
return ret;
}
/**
* @brief SMBus timeout.At power-up, the STTS751 is configured with an
* SMBus timeout of 25 to 35 milliseconds.[get]
*
* @param ctx read / write interface definitions
* @param val get timeout bit in register SMBUS_TIMEOUT.
* @retval interface status (MANDATORY: return 0 -> no Error)
*
*/
int32_t stts751_smbus_timeout_get(const stmdev_ctx_t *ctx, uint8_t *val)
{
stts751_smbus_timeout_t reg;
int32_t ret;
ret = stts751_read_reg(ctx, STTS751_SMBUS_TIMEOUT, (uint8_t *)®, 1);
if (ret != 0) { return ret; }
*val = reg.timeout;
return ret;
}
/**
* @brief Device Who am I.[get]
*
* @param ctx read / write interface definitions
* @param buff buffer that stores data read
* @retval interface status (MANDATORY: return 0 -> no Error)
*
*/
int32_t stts751_device_id_get(const stmdev_ctx_t *ctx, stts751_id_t *buff)
{
int32_t ret;
ret = stts751_read_reg(ctx, STTS751_PRODUCT_ID,
(uint8_t *)&buff->product_id, 1);
if (ret == 0)
{
ret = stts751_read_reg(ctx, STTS751_MANUFACTURER_ID,
(uint8_t *)&buff->manufacturer_id, 1);
}
if (ret == 0)
{
ret = stts751_read_reg(ctx, STTS751_REVISION_ID,
(uint8_t *)&buff->revision_id, 1);
}
return ret;
}
/**
* @}
*
*/
/**
* @}
*
*/