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