1 /**
2 ******************************************************************************
3 * @file stts751_reg.c
4 * @author Sensors Software Solution Team
5 * @brief STTS751 driver file
6 ******************************************************************************
7 * @attention
8 *
9 * <h2><center>© Copyright (c) 2021 STMicroelectronics.
10 * All rights reserved.</center></h2>
11 *
12 * This software component is licensed by ST under BSD 3-Clause license,
13 * the "License"; You may not use this file except in compliance with the
14 * License. You may obtain a copy of the License at:
15 * opensource.org/licenses/BSD-3-Clause
16 *
17 ******************************************************************************
18 */
19
20 #include "stts751_reg.h"
21
22 /**
23 * @defgroup STTS751
24 * @brief This file provides a set of functions needed to drive the
25 * stts751 enhanced inertial module.
26 * @{
27 *
28 */
29
30 /**
31 * @defgroup STTS751_Interfaces_Functions
32 * @brief This section provide a set of functions used to read and
33 * write a generic register of the device.
34 * MANDATORY: return 0 -> no Error.
35 * @{
36 *
37 */
38
39 /**
40 * @brief Read generic device register
41 *
42 * @param ctx read / write interface definitions(ptr)
43 * @param reg register to read
44 * @param data pointer to buffer that store the data read(ptr)
45 * @param len number of consecutive register to read
46 * @retval interface status (MANDATORY: return 0 -> no Error)
47 *
48 */
stts751_read_reg(const stmdev_ctx_t * ctx,uint8_t reg,uint8_t * data,uint16_t len)49 int32_t __weak stts751_read_reg(const stmdev_ctx_t *ctx, uint8_t reg,
50 uint8_t *data,
51 uint16_t len)
52 {
53 int32_t ret;
54
55 if (ctx == NULL)
56 {
57 return -1;
58 }
59
60 ret = ctx->read_reg(ctx->handle, reg, data, len);
61
62 return ret;
63 }
64
65 /**
66 * @brief Write generic device register
67 *
68 * @param ctx read / write interface definitions(ptr)
69 * @param reg register to write
70 * @param data pointer to data to write in register reg(ptr)
71 * @param len number of consecutive register to write
72 * @retval interface status (MANDATORY: return 0 -> no Error)
73 *
74 */
stts751_write_reg(const stmdev_ctx_t * ctx,uint8_t reg,uint8_t * data,uint16_t len)75 int32_t __weak stts751_write_reg(const stmdev_ctx_t *ctx, uint8_t reg,
76 uint8_t *data,
77 uint16_t len)
78 {
79 int32_t ret;
80
81 if (ctx == NULL)
82 {
83 return -1;
84 }
85
86 ret = ctx->write_reg(ctx->handle, reg, data, len);
87
88 return ret;
89 }
90
91 /**
92 * @}
93 *
94 */
95
96 /**
97 * @defgroup STTS751_Sensitivity
98 * @brief These functions convert raw-data into engineering units and
99 * vice-versa .
100 * @{
101 *
102 */
103
stts751_from_lsb_to_celsius(int16_t lsb)104 float_t stts751_from_lsb_to_celsius(int16_t lsb)
105 {
106 return ((float_t)lsb) / 256.0f;
107 }
108
109 /**
110 * @}
111 *
112 */
113
114 /**
115 * @defgroup STTS751_Sensitivity_Reverse
116 * @brief This conversion is useful but not need by the driver.
117 *
118 * REMOVING this union you are compliant with:
119 * MISRA-C 2012 [Rule 10.8] -> " Explicit cast of composite
120 * expression "
121 *
122 * @{
123 *
124 */
125
stts751_from_celsius_to_lsb(float_t celsius)126 int16_t stts751_from_celsius_to_lsb(float_t celsius)
127 {
128 return (int16_t)(celsius * 256.0f);
129 }
130
131 /**
132 * @}
133 *
134 */
135
136 /**
137 * @defgroup STTS751_Data_Generation
138 * @brief This section groups all the functions concerning
139 * data generation
140 * @{
141 *
142 */
143
144 /**
145 * @brief Temperature sensor data rate selection.[set]
146 *
147 * @param ctx read / write interface definitions
148 * @param val change the sensor data rate
149 * @retval interface status (MANDATORY: return 0 -> no Error)
150 *
151 */
stts751_temp_data_rate_set(const stmdev_ctx_t * ctx,stts751_odr_t val)152 int32_t stts751_temp_data_rate_set(const stmdev_ctx_t *ctx,
153 stts751_odr_t val)
154 {
155 stts751_configuration_t configuration;
156 stts751_conversion_rate_t conversion_rate;
157 uint8_t dummy_value = 0xAA;
158 int32_t ret;
159
160 ret = stts751_read_reg(ctx, STTS751_CONVERSION_RATE,
161 (uint8_t *)&conversion_rate, 1);
162
163 if (ret == 0)
164 {
165 conversion_rate.conv = (uint8_t)val & 0x0FU;
166 ret = stts751_write_reg(ctx, STTS751_CONVERSION_RATE,
167 (uint8_t *)&conversion_rate, 1);
168 }
169
170 if (ret == 0)
171 {
172 ret = stts751_read_reg(ctx, STTS751_CONFIGURATION,
173 (uint8_t *)&configuration, 1);
174 }
175
176 if (ret == 0)
177 {
178 configuration.stop = ((uint8_t)val & 0x80U) >> 7;
179 ret = stts751_write_reg(ctx, STTS751_CONFIGURATION,
180 (uint8_t *)&configuration, 1);
181 }
182
183 if ((ret == 0) && (val == STTS751_TEMP_ODR_ONE_SHOT))
184 {
185 ret = stts751_write_reg(ctx, STTS751_ONE_SHOT, &dummy_value, 1);
186 }
187
188 return ret;
189 }
190
191 /**
192 * @brief Temperature sensor data rate selection.[get]
193 *
194 * @param ctx read / write interface definitions
195 * @param val Get the sensor data rate
196 * @retval interface status (MANDATORY: return 0 -> no Error)
197 *
198 */
stts751_temp_data_rate_get(const stmdev_ctx_t * ctx,stts751_odr_t * val)199 int32_t stts751_temp_data_rate_get(const stmdev_ctx_t *ctx,
200 stts751_odr_t *val)
201 {
202 stts751_conversion_rate_t conversion_rate;
203 stts751_configuration_t configuration;
204 int32_t ret;
205
206 ret = stts751_read_reg(ctx, STTS751_CONVERSION_RATE,
207 (uint8_t *)&conversion_rate, 1);
208
209 if (ret == 0)
210 {
211 ret = stts751_read_reg(ctx, STTS751_CONFIGURATION,
212 (uint8_t *)&configuration, 1);
213 }
214
215 switch ((configuration.stop << 7) + conversion_rate.conv)
216 {
217 case STTS751_TEMP_ODR_OFF:
218 *val = STTS751_TEMP_ODR_OFF;
219 break;
220
221 case STTS751_TEMP_ODR_ONE_SHOT:
222 *val = STTS751_TEMP_ODR_ONE_SHOT;
223 break;
224
225 case STTS751_TEMP_ODR_62mHz5:
226 *val = STTS751_TEMP_ODR_62mHz5;
227 break;
228
229 case STTS751_TEMP_ODR_125mHz:
230 *val = STTS751_TEMP_ODR_125mHz;
231 break;
232
233 case STTS751_TEMP_ODR_250mHz:
234 *val = STTS751_TEMP_ODR_250mHz;
235 break;
236
237 case STTS751_TEMP_ODR_500mHz:
238 *val = STTS751_TEMP_ODR_500mHz;
239 break;
240
241 case STTS751_TEMP_ODR_1Hz:
242 *val = STTS751_TEMP_ODR_1Hz;
243 break;
244
245 case STTS751_TEMP_ODR_2Hz:
246 *val = STTS751_TEMP_ODR_2Hz;
247 break;
248
249 case STTS751_TEMP_ODR_4Hz:
250 *val = STTS751_TEMP_ODR_4Hz;
251 break;
252
253 case STTS751_TEMP_ODR_8Hz:
254 *val = STTS751_TEMP_ODR_8Hz;
255 break;
256
257 case STTS751_TEMP_ODR_16Hz:
258 *val = STTS751_TEMP_ODR_16Hz;
259 break;
260
261 case STTS751_TEMP_ODR_32Hz:
262 *val = STTS751_TEMP_ODR_32Hz;
263 break;
264
265 default:
266 *val = STTS751_TEMP_ODR_OFF;
267 break;
268 }
269
270 return ret;
271 }
272
273 /**
274 * @brief Temperature sensor resolution selection.[set]
275 *
276 * @param ctx read / write interface definitions
277 * @param val change the values of tres in reg CONFIGURATION
278 * @retval interface status (MANDATORY: return 0 -> no Error)
279 *
280 */
stts751_resolution_set(const stmdev_ctx_t * ctx,stts751_tres_t val)281 int32_t stts751_resolution_set(const stmdev_ctx_t *ctx, stts751_tres_t val)
282 {
283 stts751_configuration_t reg;
284 int32_t ret;
285
286 ret = stts751_read_reg(ctx, STTS751_CONFIGURATION, (uint8_t *) ®, 1);
287
288 if (ret == 0)
289 {
290 reg.tres = (uint8_t) val;
291 ret = stts751_write_reg(ctx, STTS751_CONFIGURATION, (uint8_t *) ®, 1);
292 }
293
294 return ret;
295 }
296
297 /**
298 * @brief Temperature sensor resolution selection.[get]
299 *
300 * @param ctx read / write interface definitions
301 * @param val Get the values of tres in reg CONFIGURATION
302 * @retval interface status (MANDATORY: return 0 -> no Error)
303 *
304 */
stts751_resolution_get(const stmdev_ctx_t * ctx,stts751_tres_t * val)305 int32_t stts751_resolution_get(const stmdev_ctx_t *ctx, stts751_tres_t *val)
306 {
307 stts751_configuration_t reg;
308 int32_t ret;
309
310 ret = stts751_read_reg(ctx, STTS751_CONFIGURATION, (uint8_t *) ®, 1);
311
312 switch (reg.tres)
313 {
314 case STTS751_9bit:
315 *val = STTS751_9bit;
316 break;
317
318 case STTS751_10bit:
319 *val = STTS751_10bit;
320 break;
321
322 case STTS751_11bit:
323 *val = STTS751_11bit;
324 break;
325
326 case STTS751_12bit:
327 *val = STTS751_12bit;
328 break;
329
330 default:
331 *val = STTS751_9bit;
332 break;
333 }
334
335 return ret;
336 }
337
338 /**
339 * @brief The STATUS_REG register of the device.[get]
340 *
341 * @param ctx read / write interface definitions
342 * @param val union of registers from STATUS to
343 * @retval interface status (MANDATORY: return 0 -> no Error)
344 *
345 */
stts751_status_reg_get(const stmdev_ctx_t * ctx,stts751_status_t * val)346 int32_t stts751_status_reg_get(const stmdev_ctx_t *ctx,
347 stts751_status_t *val)
348 {
349 int32_t ret;
350
351 ret = stts751_read_reg(ctx, STTS751_STATUS, (uint8_t *) val, 1);
352
353 return ret;
354 }
355
356 /**
357 * @brief Temperature sensor "conversion on-going" flag.[get]
358 *
359 * @param ctx read / write interface definitions
360 * @param val get the values of busy in reg STATUS
361 * @retval interface status (MANDATORY: return 0 -> no Error)
362 *
363 */
stts751_flag_busy_get(const stmdev_ctx_t * ctx,uint8_t * val)364 int32_t stts751_flag_busy_get(const stmdev_ctx_t *ctx, uint8_t *val)
365 {
366 stts751_status_t reg;
367 int32_t ret;
368
369 ret = stts751_read_reg(ctx, STTS751_STATUS, (uint8_t *)®, 1);
370 *val = reg.busy;
371
372 return ret;
373 }
374
375 /**
376 * @}
377 *
378 */
379
380 /**
381 * @defgroup STTS751_Data_Output
382 * @brief This section groups all the data output functions.
383 * @{
384 *
385 */
386
387 /**
388 * @brief Temperature data output register (r). L and H registers
389 * together express a 16-bit word in two’s complement.[get]
390 *
391 * @param ctx read / write interface definitions
392 * @param buff buffer that stores data read
393 * @retval interface status (MANDATORY: return 0 -> no Error)
394 *
395 */
stts751_temperature_raw_get(const stmdev_ctx_t * ctx,int16_t * val)396 int32_t stts751_temperature_raw_get(const stmdev_ctx_t *ctx, int16_t *val)
397 {
398 uint8_t buff[2];
399 int32_t ret;
400
401 ret = stts751_read_reg(ctx, STTS751_TEMPERATURE_HIGH,
402 (uint8_t *)&buff[1], 1);
403
404 if (ret == 0)
405 {
406 ret = stts751_read_reg(ctx, STTS751_TEMPERATURE_LOW,
407 &buff[0], 1);
408 *val = (int16_t)buff[1];
409 *val = (*val * 256) + (int16_t)buff[0];
410 }
411
412 return ret;
413 }
414
415 /**
416 * @}
417 *
418 */
419
420 /**
421 * @defgroup STTS751_Interrupt_Pins
422 * @brief This section groups all the functions that manage event pin
423 * @{
424 *
425 */
426
427 /**
428 * @brief Route interrupt signal threshold on event pad.[set]
429 *
430 * @param ctx read / write interface definitions
431 * @param val set mask1 bit in register CONFIGURATION.
432 * @retval interface status (MANDATORY: return 0 -> no Error)
433 *
434 */
stts751_pin_event_route_set(const stmdev_ctx_t * ctx,uint8_t val)435 int32_t stts751_pin_event_route_set(const stmdev_ctx_t *ctx, uint8_t val)
436 {
437 stts751_configuration_t reg;
438 int32_t ret;
439
440 ret = stts751_read_reg(ctx, STTS751_CONFIGURATION, (uint8_t *)®, 1);
441
442 if (ret == 0)
443 {
444 reg.mask1 = val;
445 ret = stts751_write_reg(ctx, STTS751_CONFIGURATION, (uint8_t *)®, 1);
446 }
447
448 return ret;
449 }
450
451 /**
452 * @brief Route interrupt signal threshold on event pad.[get]
453 *
454 * @param ctx read / write interface definitions
455 * @param val get mask1 bit in register CONFIGURATION.
456 * @retval interface status (MANDATORY: return 0 -> no Error)
457 *
458 */
stts751_pin_event_route_get(const stmdev_ctx_t * ctx,uint8_t * val)459 int32_t stts751_pin_event_route_get(const stmdev_ctx_t *ctx, uint8_t *val)
460 {
461 stts751_configuration_t reg;
462 int32_t ret;
463
464 ret = stts751_read_reg(ctx, STTS751_CONFIGURATION, (uint8_t *)®, 1);
465 *val = reg.mask1;
466
467 return ret;
468 }
469
470 /**
471 * @}
472 *
473 */
474
475 /**
476 * @defgroup STTS751_Interrupt_on_threshold
477 * @brief This section groups all the functions that manage interrupt
478 * on threshold event
479 * @{
480 *
481 */
482
483 /**
484 * @brief high temperature threshold.[set]
485 *
486 * @param ctx read / write interface definitions
487 * @param buff buffer that contains data to write
488 * @retval interface status (MANDATORY: return 0 -> no Error)
489 *
490 */
stts751_high_temperature_threshold_set(const stmdev_ctx_t * ctx,int16_t val)491 int32_t stts751_high_temperature_threshold_set(const stmdev_ctx_t *ctx,
492 int16_t val)
493 {
494 uint8_t buff[2];
495 int32_t ret;
496
497 buff[0] = (uint8_t)((uint16_t)val / 256U);
498 buff[1] = (uint8_t)((uint16_t)val - (buff[1] * 256U));
499 ret = stts751_write_reg(ctx, STTS751_TEMPERATURE_HIGH_LIMIT_HIGH,
500 buff, 2);
501
502 return ret;
503 }
504
505 /**
506 * @brief high temperature threshold.[get]
507 *
508 * @param ctx read / write interface definitions
509 * @param buff buffer that stores data read
510 * @retval interface status (MANDATORY: return 0 -> no Error)
511 *
512 */
stts751_high_temperature_threshold_get(const stmdev_ctx_t * ctx,int16_t * val)513 int32_t stts751_high_temperature_threshold_get(const stmdev_ctx_t *ctx,
514 int16_t *val)
515 {
516 uint8_t buff[2];
517 int32_t ret;
518
519 ret = stts751_read_reg(ctx, STTS751_TEMPERATURE_HIGH_LIMIT_HIGH, buff,
520 2);
521 *val = (int16_t)buff[0];
522 *val = (*val * 256) + (int16_t)buff[1];
523
524 return ret;
525 }
526
527 /**
528 * @brief low temperature threshold.[set]
529 *
530 * @param ctx read / write interface definitions
531 * @param buff buffer that contains data to write
532 * @retval interface status (MANDATORY: return 0 -> no Error)
533 *
534 */
stts751_low_temperature_threshold_set(const stmdev_ctx_t * ctx,int16_t val)535 int32_t stts751_low_temperature_threshold_set(const stmdev_ctx_t *ctx,
536 int16_t val)
537 {
538 uint8_t buff[2];
539 int32_t ret;
540
541 buff[0] = (uint8_t)((uint16_t)val / 256U);
542 buff[1] = (uint8_t)((uint16_t)val - (buff[1] * 256U));
543 ret = stts751_write_reg(ctx, STTS751_TEMPERATURE_LOW_LIMIT_HIGH, buff,
544 2);
545
546 return ret;
547 }
548
549 /**
550 * @brief low temperature threshold.[get]
551 *
552 * @param ctx read / write interface definitions
553 * @param buff buffer that stores data read
554 * @retval interface status (MANDATORY: return 0 -> no Error)
555 *
556 */
stts751_low_temperature_threshold_get(const stmdev_ctx_t * ctx,int16_t * val)557 int32_t stts751_low_temperature_threshold_get(const stmdev_ctx_t *ctx,
558 int16_t *val)
559 {
560 uint8_t buff[2];
561 int32_t ret;
562
563 ret = stts751_read_reg(ctx, STTS751_TEMPERATURE_LOW_LIMIT_HIGH, buff,
564 2);
565 *val = (int16_t)buff[0];
566 *val = (*val * 256) + (int16_t)buff[1];
567
568 return ret;
569 }
570
571 /**
572 * @}
573 *
574 */
575
576 /**
577 * @defgroup STTS751 over temperature alarm
578 * @brief This section groups all the functions that manage
579 * over temperature alarm functionality.
580 * @{
581 *
582 */
583
584 /**
585 * @brief Thermal Limit. 1 LSB = 1 degC (max 127 degC min -127 degC ).[set]
586 *
587 * @param ctx read / write interface definitions
588 * @param val change the values of reg THERM_LIMIT
589 * @retval interface status (MANDATORY: return 0 -> no Error)
590 *
591 */
stts751_ota_thermal_limit_set(const stmdev_ctx_t * ctx,int8_t val)592 int32_t stts751_ota_thermal_limit_set(const stmdev_ctx_t *ctx, int8_t val)
593 {
594 int32_t ret;
595
596 ret = stts751_write_reg(ctx, STTS751_THERM_LIMIT, (uint8_t *)&val, 1);
597
598 return ret;
599 }
600
601 /**
602 * @brief Thermal Limit. 1 LSB = 1 degC (max 127 degC min -127 degC ).[get]
603 *
604 * @param ctx read / write interface definitions
605 * @param val get the values of reg THERM_LIMIT
606 * @retval interface status (MANDATORY: return 0 -> no Error)
607 *
608 */
stts751_ota_thermal_limit_get(const stmdev_ctx_t * ctx,int8_t * val)609 int32_t stts751_ota_thermal_limit_get(const stmdev_ctx_t *ctx, int8_t *val)
610 {
611 int32_t ret;
612
613 ret = stts751_read_reg(ctx, STTS751_THERM_LIMIT, (uint8_t *)val, 1);
614
615 return ret;
616 }
617
618 /**
619 * @brief Thermal hysteresis. 1 LSB = 1 degC.[set]
620 * max 127 degC min -127 degC.
621 *
622 * @param ctx read / write interface definitions
623 * @param val change the values of reg THERM_HYSTERESIS
624 * @retval interface status (MANDATORY: return 0 -> no Error)
625 *
626 */
stts751_ota_thermal_hyst_set(const stmdev_ctx_t * ctx,int8_t val)627 int32_t stts751_ota_thermal_hyst_set(const stmdev_ctx_t *ctx, int8_t val)
628 {
629 int32_t ret;
630
631 ret = stts751_write_reg(ctx, STTS751_THERM_HYSTERESIS,
632 (uint8_t *)&val, 1);
633
634 return ret;
635 }
636
637 /**
638 * @brief Thermal hysteresis. 1 LSB = 1 degC.[get]
639 * max 127 degC min -127 degC.
640 *
641 * @param ctx read / write interface definitions
642 * @param val get the values of reg THERM_HYSTERESIS
643 * @retval interface status (MANDATORY: return 0 -> no Error)
644 *
645 */
stts751_ota_thermal_hyst_get(const stmdev_ctx_t * ctx,int8_t * val)646 int32_t stts751_ota_thermal_hyst_get(const stmdev_ctx_t *ctx, int8_t *val)
647 {
648 int32_t ret;
649
650 ret = stts751_read_reg(ctx, STTS751_THERM_HYSTERESIS, (uint8_t *)val, 1);
651
652 return ret;
653 }
654
655 /**
656 * @}
657 *
658 */
659
660 /**
661 * @defgroup STTS751_Common
662 * @brief This section groups common useful functions.
663 * @{
664 *
665 */
666
667 /**
668 * @brief SMBus timeout.At power-up, the STTS751 is configured with an
669 * SMBus timeout of 25 to 35 milliseconds.[set]
670 *
671 * @param ctx read / write interface definitions
672 * @param val set timeout bit in register SMBUS_TIMEOUT.
673 * @retval interface status (MANDATORY: return 0 -> no Error)
674 *
675 */
stts751_smbus_timeout_set(const stmdev_ctx_t * ctx,uint8_t val)676 int32_t stts751_smbus_timeout_set(const stmdev_ctx_t *ctx, uint8_t val)
677 {
678 stts751_smbus_timeout_t reg;
679 int32_t ret;
680
681 ret = stts751_read_reg(ctx, STTS751_SMBUS_TIMEOUT, (uint8_t *)®, 1);
682
683 if (ret == 0)
684 {
685 reg.timeout = val;
686 ret = stts751_write_reg(ctx, STTS751_SMBUS_TIMEOUT, (uint8_t *)®, 1);
687 }
688
689 return ret;
690 }
691
692 /**
693 * @brief SMBus timeout.At power-up, the STTS751 is configured with an
694 * SMBus timeout of 25 to 35 milliseconds.[get]
695 *
696 * @param ctx read / write interface definitions
697 * @param val get timeout bit in register SMBUS_TIMEOUT.
698 * @retval interface status (MANDATORY: return 0 -> no Error)
699 *
700 */
stts751_smbus_timeout_get(const stmdev_ctx_t * ctx,uint8_t * val)701 int32_t stts751_smbus_timeout_get(const stmdev_ctx_t *ctx, uint8_t *val)
702 {
703 stts751_smbus_timeout_t reg;
704 int32_t ret;
705
706 ret = stts751_read_reg(ctx, STTS751_SMBUS_TIMEOUT, (uint8_t *)®, 1);
707 *val = reg.timeout;
708
709 return ret;
710 }
711
712 /**
713 * @brief Device Who am I.[get]
714 *
715 * @param ctx read / write interface definitions
716 * @param buff buffer that stores data read
717 * @retval interface status (MANDATORY: return 0 -> no Error)
718 *
719 */
stts751_device_id_get(const stmdev_ctx_t * ctx,stts751_id_t * buff)720 int32_t stts751_device_id_get(const stmdev_ctx_t *ctx, stts751_id_t *buff)
721 {
722 int32_t ret;
723
724 ret = stts751_read_reg(ctx, STTS751_PRODUCT_ID,
725 (uint8_t *)&buff->product_id, 1);
726
727 if (ret == 0)
728 {
729 ret = stts751_read_reg(ctx, STTS751_MANUFACTURER_ID,
730 (uint8_t *)&buff->manufacturer_id, 1);
731 }
732
733 if (ret == 0)
734 {
735 ret = stts751_read_reg(ctx, STTS751_REVISION_ID,
736 (uint8_t *)&buff->revision_id, 1);
737 }
738
739 return ret;
740 }
741
742 /**
743 * @}
744 *
745 */
746
747 /**
748 * @}
749 *
750 */
751
752 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
753