1 /**
2 ******************************************************************************
3 * @file stts22h_reg.c
4 * @author Sensors Software Solution Team
5 * @brief STTS22H 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 "stts22h_reg.h"
21
22 /**
23 * @defgroup STTS22H
24 * @brief This file provides a set of functions needed to drive the
25 * stts22h enhanced inertial module.
26 * @{
27 *
28 */
29
30 /**
31 * @defgroup STTS22H_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 */
stts22h_read_reg(const stmdev_ctx_t * ctx,uint8_t reg,uint8_t * data,uint16_t len)49 int32_t __weak stts22h_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) return -1;
56
57 ret = ctx->read_reg(ctx->handle, reg, data, len);
58
59 return ret;
60 }
61
62 /**
63 * @brief Write generic device register
64 *
65 * @param ctx read / write interface definitions(ptr)
66 * @param reg register to write
67 * @param data pointer to data to write in register reg(ptr)
68 * @param len number of consecutive register to write
69 * @retval interface status (MANDATORY: return 0 -> no Error)
70 *
71 */
stts22h_write_reg(const stmdev_ctx_t * ctx,uint8_t reg,uint8_t * data,uint16_t len)72 int32_t __weak stts22h_write_reg(const stmdev_ctx_t *ctx, uint8_t reg,
73 uint8_t *data,
74 uint16_t len)
75 {
76 int32_t ret;
77
78 if (ctx == NULL) return -1;
79
80 ret = ctx->write_reg(ctx->handle, reg, data, len);
81
82 return ret;
83 }
84
85 /**
86 * @}
87 *
88 */
89
90 /**
91 * @defgroup STTS22H_Sensitivity
92 * @brief These functions convert raw-data into engineering units.
93 * @{
94 *
95 */
96
stts22h_from_lsb_to_celsius(int16_t lsb)97 float_t stts22h_from_lsb_to_celsius(int16_t lsb)
98 {
99 return ((float_t)lsb / 100.0f);
100 }
101
102 /**
103 * @}
104 *
105 */
106
107 /**
108 * @defgroup STTS22H_Data_generation
109 * @brief This section groups all the functions concerning
110 * data generation.
111 * @{
112 *
113 */
114
115 /**
116 * @brief Temperature sensor data rate selection..[set]
117 *
118 * @param ctx Read / write interface definitions.(ptr)
119 * @param val Change the values of "one_shot" in reg STTS22H.
120 * @retval Interface status (MANDATORY: return 0 -> no Error).
121 *
122 */
stts22h_temp_data_rate_set(const stmdev_ctx_t * ctx,stts22h_odr_temp_t val)123 int32_t stts22h_temp_data_rate_set(const stmdev_ctx_t *ctx,
124 stts22h_odr_temp_t val)
125 {
126 stts22h_ctrl_t ctrl;
127 int32_t ret;
128
129 ret = stts22h_read_reg(ctx, STTS22H_CTRL, (uint8_t *)&ctrl, 1);
130
131 if (ret == 0)
132 {
133 ctrl.one_shot = (uint8_t)val & 0x01U;
134 ctrl.freerun = ((uint8_t)val & 0x02U) >> 1;
135 ctrl.low_odr_start = ((uint8_t)val & 0x04U) >> 2;
136 ctrl.avg = ((uint8_t)val & 0x30U) >> 4;
137 ret = stts22h_write_reg(ctx, STTS22H_CTRL, (uint8_t *)&ctrl, 1);
138 }
139
140 return ret;
141 }
142
143 /**
144 * @brief Temperature sensor data rate selection..[get]
145 *
146 * @param ctx Read / write interface definitions.(ptr)
147 * @param val Get the values of one_shot in reg CTRL.(ptr)
148 * @retval Interface status (MANDATORY: return 0 -> no Error).
149 *
150 */
stts22h_temp_data_rate_get(const stmdev_ctx_t * ctx,stts22h_odr_temp_t * val)151 int32_t stts22h_temp_data_rate_get(const stmdev_ctx_t *ctx,
152 stts22h_odr_temp_t *val)
153 {
154 stts22h_ctrl_t ctrl;
155 int32_t ret;
156
157 ret = stts22h_read_reg(ctx, STTS22H_CTRL,
158 (uint8_t *)&ctrl, 1);
159
160 switch (ctrl.one_shot | (ctrl.freerun << 1) | (ctrl.low_odr_start <<
161 2) |
162 (ctrl.avg << 4))
163 {
164 case STTS22H_POWER_DOWN:
165 *val = STTS22H_POWER_DOWN;
166 break;
167
168 case STTS22H_ONE_SHOT:
169 *val = STTS22H_ONE_SHOT;
170 break;
171
172 case STTS22H_1Hz:
173 *val = STTS22H_1Hz;
174 break;
175
176 case STTS22H_25Hz:
177 *val = STTS22H_25Hz;
178 break;
179
180 case STTS22H_50Hz:
181 *val = STTS22H_50Hz;
182 break;
183
184 case STTS22H_100Hz:
185 *val = STTS22H_100Hz;
186 break;
187
188 case STTS22H_200Hz:
189 *val = STTS22H_200Hz;
190 break;
191
192 default:
193 *val = STTS22H_POWER_DOWN;
194 break;
195 }
196
197 return ret;
198 }
199
200 /**
201 * @brief Block data update.[set]
202 *
203 * @param ctx Read / write interface definitions.(ptr)
204 * @param val Change the values of bdu in reg CTRL.
205 * @retval Interface status (MANDATORY: return 0 -> no Error).
206 *
207 */
stts22h_block_data_update_set(const stmdev_ctx_t * ctx,uint8_t val)208 int32_t stts22h_block_data_update_set(const stmdev_ctx_t *ctx, uint8_t val)
209 {
210 stts22h_ctrl_t ctrl;
211 int32_t ret;
212
213 ret = stts22h_read_reg(ctx, STTS22H_CTRL, (uint8_t *)&ctrl, 1);
214
215 if (ret == 0)
216 {
217 ctrl.bdu = val;
218 ret = stts22h_write_reg(ctx, STTS22H_CTRL, (uint8_t *)&ctrl, 1);
219 }
220
221 return ret;
222 }
223
224 /**
225 * @brief Block data update.[get]
226 *
227 * @param ctx Read / write interface definitions.(ptr)
228 * @param val Get the values of bdu in reg CTRL.(ptr)
229 * @retval Interface status (MANDATORY: return 0 -> no Error).
230 *
231 */
stts22h_block_data_update_get(const stmdev_ctx_t * ctx,uint8_t * val)232 int32_t stts22h_block_data_update_get(const stmdev_ctx_t *ctx, uint8_t *val)
233 {
234 int32_t ret;
235
236 ret = stts22h_read_reg(ctx, STTS22H_CTRL, (uint8_t *)val, 1);
237
238 return ret;
239 }
240
241 /**
242 * @brief New data available from temperature sensor..[get]
243 *
244 * @param ctx Read / write interface definitions.(ptr)
245 * @param val Return an option of "stts22h_uint8_t".(ptr)
246 * @retval Interface status (MANDATORY: return 0 -> no Error).
247 *
248 */
stts22h_temp_flag_data_ready_get(const stmdev_ctx_t * ctx,uint8_t * val)249 int32_t stts22h_temp_flag_data_ready_get(const stmdev_ctx_t *ctx,
250 uint8_t *val)
251 {
252 stts22h_status_t status;
253 int32_t ret;
254
255 ret = stts22h_read_reg(ctx, STTS22H_STATUS, (uint8_t *)&status, 1);
256
257 if (status.busy == PROPERTY_DISABLE)
258 {
259 *val = PROPERTY_ENABLE;
260 }
261
262 else
263 {
264 *val = PROPERTY_DISABLE;
265 }
266
267 return ret;
268 }
269
270 /**
271 * @}
272 *
273 */
274
275 /**
276 * @defgroup STTS22H_Dataoutput
277 * @brief This section groups all the data output functions.
278 * @{
279 *
280 */
281
282 /**
283 * @brief Temperature data output register(r). L and H registers
284 * together express a 16-bit word in two’s complement..[get]
285 *
286 * @param ctx Read / write interface definitions.(ptr)
287 * @param buff Buffer that stores the data read.(ptr)
288 * @retval Interface status (MANDATORY: return 0 -> no Error).
289 *
290 */
stts22h_temperature_raw_get(const stmdev_ctx_t * ctx,int16_t * val)291 int32_t stts22h_temperature_raw_get(const stmdev_ctx_t *ctx, int16_t *val)
292 {
293 uint8_t buff[2];
294 int32_t ret;
295
296 ret = stts22h_read_reg(ctx, STTS22H_TEMP_L_OUT, buff, 2);
297 *val = (int16_t)buff[1];
298 *val = (*val * 256) + (int16_t)buff[0];
299
300 return ret;
301 }
302
303 /**
304 * @}
305 *
306 */
307
308 /**
309 * @defgroup STTS22H_Common
310 * @brief This section groups common useful functions.
311 * @{
312 *
313 */
314
315 /**
316 * @brief Device Who am I..[get]
317 *
318 * @param ctx Read / write interface definitions.(ptr)
319 * @param buff Buffer that stores the data read.(ptr)
320 * @retval Interface status (MANDATORY: return 0 -> no Error).
321 *
322 */
stts22h_dev_id_get(const stmdev_ctx_t * ctx,uint8_t * buff)323 int32_t stts22h_dev_id_get(const stmdev_ctx_t *ctx, uint8_t *buff)
324 {
325 int32_t ret;
326
327 ret = stts22h_read_reg(ctx, STTS22H_WHOAMI, buff, 1);
328
329 return ret;
330 }
331 /**
332 * @brief Device status register.[get]
333 *
334 * @param ctx Read / write interface definitions.(ptr)
335 * @param val In one-shot mode this bit is high when the
336 * conversion is in progress..(ptr)
337 * @retval Interface status (MANDATORY: return 0 -> no Error).
338 *
339 */
stts22h_dev_status_get(const stmdev_ctx_t * ctx,stts22h_dev_status_t * val)340 int32_t stts22h_dev_status_get(const stmdev_ctx_t *ctx,
341 stts22h_dev_status_t *val)
342 {
343 stts22h_status_t status;
344 int32_t ret;
345
346 ret = stts22h_read_reg(ctx, STTS22H_STATUS, (uint8_t *)&status, 1);
347 val->busy = status.busy;
348
349 return ret;
350 }
351
352 /**
353 * @}
354 *
355 */
356
357 /**
358 * @defgroup STTS22H_Serial_interface
359 * @brief This section groups all the functions concerning main
360 * serial interface management.
361 * @{
362 *
363 */
364
365 /**
366 * @brief SMBus mode..[set]
367 *
368 * @param ctx Read / write interface definitions.(ptr)
369 * @param val Change the values of "time_out_dis" in reg STTS22H.
370 * @retval Interface status (MANDATORY: return 0 -> no Error).
371 *
372 */
stts22h_smbus_interface_set(const stmdev_ctx_t * ctx,stts22h_smbus_md_t val)373 int32_t stts22h_smbus_interface_set(const stmdev_ctx_t *ctx,
374 stts22h_smbus_md_t val)
375 {
376 stts22h_ctrl_t ctrl;
377 int32_t ret;
378
379 ret = stts22h_read_reg(ctx, STTS22H_CTRL, (uint8_t *)&ctrl, 1);
380
381 if (ret == 0)
382 {
383 ctrl.time_out_dis = (uint8_t)val;
384 ret = stts22h_write_reg(ctx, STTS22H_CTRL, (uint8_t *)&ctrl, 1);
385 }
386
387 return ret;
388 }
389
390 /**
391 * @brief SMBus mode..[get]
392 *
393 * @param ctx Read / write interface definitions.(ptr)
394 * @param val Get the values of time_out_dis in reg CTRL.(ptr)
395 * @retval Interface status (MANDATORY: return 0 -> no Error).
396 *
397 */
stts22h_smbus_interface_get(const stmdev_ctx_t * ctx,stts22h_smbus_md_t * val)398 int32_t stts22h_smbus_interface_get(const stmdev_ctx_t *ctx,
399 stts22h_smbus_md_t *val)
400 {
401 stts22h_ctrl_t ctrl;
402 int32_t ret;
403
404 ret = stts22h_read_reg(ctx, STTS22H_CTRL,
405 (uint8_t *)&ctrl, 1);
406
407 switch (ctrl.time_out_dis)
408 {
409 case STTS22H_SMBUS_TIMEOUT_ENABLE:
410 *val = STTS22H_SMBUS_TIMEOUT_ENABLE;
411 break;
412
413 case STTS22H_SMBUS_TIMEOUT_DISABLE:
414 *val = STTS22H_SMBUS_TIMEOUT_DISABLE;
415 break;
416
417 default:
418 *val = STTS22H_SMBUS_TIMEOUT_ENABLE;
419 break;
420 }
421
422 return ret;
423 }
424
425 /**
426 * @brief Register address automatically incremented during a multiple
427 * byte access with a serial interface.[set]
428 *
429 * @param ctx Read / write interface definitions.(ptr)
430 * @param val Change the values of "if_add_inc" in reg STTS22H.
431 * @retval Interface status (MANDATORY: return 0 -> no Error).
432 *
433 */
stts22h_auto_increment_set(const stmdev_ctx_t * ctx,uint8_t val)434 int32_t stts22h_auto_increment_set(const stmdev_ctx_t *ctx, uint8_t val)
435 {
436 stts22h_ctrl_t ctrl;
437 int32_t ret;
438
439 ret = stts22h_read_reg(ctx, STTS22H_CTRL, (uint8_t *)&ctrl, 1);
440
441 if (ret == 0)
442 {
443 ctrl.if_add_inc = (uint8_t)val;
444 ret = stts22h_write_reg(ctx, STTS22H_CTRL, (uint8_t *)&ctrl, 1);
445 }
446
447 return ret;
448 }
449
450 /**
451 * @brief Register address automatically incremented during a multiple
452 * byte access with a serial interface.[get]
453 *
454 * @param ctx Read / write interface definitions.(ptr)
455 * @param val Get the values of if_add_inc in reg CTRL.(ptr)
456 * @retval Interface status (MANDATORY: return 0 -> no Error).
457 *
458 */
stts22h_auto_increment_get(const stmdev_ctx_t * ctx,uint8_t * val)459 int32_t stts22h_auto_increment_get(const stmdev_ctx_t *ctx, uint8_t *val)
460 {
461 int32_t ret;
462
463 ret = stts22h_read_reg(ctx, STTS22H_CTRL, (uint8_t *)&val, 1);
464
465 return ret;
466 }
467
468 /**
469 * @}
470 *
471 */
472
473 /**
474 * @defgroup STTS22H_ Interrupt_on_threshold
475 * @brief This section group all the functions concerning the
476 * interrupt on threshold configuration.
477 * @{
478 *
479 */
480
481 /**
482 * @brief Over temperature interrupt value. ( degC / 0.64 ) + 63.[set]
483 *
484 * @param ctx Read / write interface definitions.(ptr)
485 * @param val Change the values of thl in reg TEMP_H_LIMIT.
486 * @retval Interface status (MANDATORY: return 0 -> no Error).
487 *
488 */
stts22h_temp_trshld_high_set(const stmdev_ctx_t * ctx,uint8_t val)489 int32_t stts22h_temp_trshld_high_set(const stmdev_ctx_t *ctx, uint8_t val)
490 {
491 stts22h_temp_h_limit_t temp_h_limit;
492 int32_t ret;
493
494 ret = stts22h_read_reg(ctx, STTS22H_TEMP_H_LIMIT,
495 (uint8_t *)&temp_h_limit, 1);
496
497 if (ret == 0)
498 {
499 temp_h_limit.thl = val;
500 ret = stts22h_write_reg(ctx, STTS22H_TEMP_H_LIMIT,
501 (uint8_t *)&temp_h_limit, 1);
502 }
503
504 return ret;
505 }
506
507 /**
508 * @brief Over temperature interrupt value. ( degC / 0.64 ) + 63.[get]
509 *
510 * @param ctx Read / write interface definitions.(ptr)
511 * @param val Get the values of thl in reg TEMP_H_LIMIT.(ptr)
512 * @retval Interface status (MANDATORY: return 0 -> no Error).
513 *
514 */
stts22h_temp_trshld_high_get(const stmdev_ctx_t * ctx,uint8_t * val)515 int32_t stts22h_temp_trshld_high_get(const stmdev_ctx_t *ctx, uint8_t *val)
516 {
517 stts22h_temp_h_limit_t temp_h_limit;
518 int32_t ret;
519
520 ret = stts22h_read_reg(ctx, STTS22H_TEMP_H_LIMIT,
521 (uint8_t *)&temp_h_limit, 1);
522 *val = temp_h_limit.thl;
523
524 return ret;
525 }
526
527 /**
528 * @brief Under temperature interrupt value. ( degC / 0.64 ) + 63.[set]
529 *
530 * @param ctx Read / write interface definitions.(ptr)
531 * @param val Change the values of tll in reg TEMP_L_LIMIT.
532 * @retval Interface status (MANDATORY: return 0 -> no Error).
533 *
534 */
stts22h_temp_trshld_low_set(const stmdev_ctx_t * ctx,uint8_t val)535 int32_t stts22h_temp_trshld_low_set(const stmdev_ctx_t *ctx, uint8_t val)
536 {
537 stts22h_temp_l_limit_t temp_l_limit;
538 int32_t ret;
539
540 ret = stts22h_read_reg(ctx, STTS22H_TEMP_L_LIMIT,
541 (uint8_t *)&temp_l_limit, 1);
542
543 if (ret == 0)
544 {
545 temp_l_limit.tll = val;
546 ret = stts22h_write_reg(ctx, STTS22H_TEMP_L_LIMIT,
547 (uint8_t *)&temp_l_limit, 1);
548 }
549
550 return ret;
551 }
552
553 /**
554 * @brief Under temperature interrupt value. ( degC / 0.64 ) + 63.[get]
555 *
556 * @param ctx Read / write interface definitions.(ptr)
557 * @param val Get the values of tll in reg TEMP_L_LIMIT.(ptr)
558 * @retval Interface status (MANDATORY: return 0 -> no Error).
559 *
560 */
stts22h_temp_trshld_low_get(const stmdev_ctx_t * ctx,uint8_t * val)561 int32_t stts22h_temp_trshld_low_get(const stmdev_ctx_t *ctx, uint8_t *val)
562 {
563 stts22h_temp_l_limit_t temp_l_limit;
564 int32_t ret;
565
566 ret = stts22h_read_reg(ctx, STTS22H_TEMP_L_LIMIT,
567 (uint8_t *)&temp_l_limit, 1);
568 *val = temp_l_limit.tll;
569
570 return ret;
571 }
572
573 /**
574 * @brief Temperature interrupt on threshold source.[get]
575 *
576 * @param ctx Read / write interface definitions.(ptr)
577 * @param val Low limit temperature exceeded..(ptr)
578 * @retval Interface status (MANDATORY: return 0 -> no Error).
579 *
580 */
stts22h_temp_trshld_src_get(const stmdev_ctx_t * ctx,stts22h_temp_trlhd_src_t * val)581 int32_t stts22h_temp_trshld_src_get(const stmdev_ctx_t *ctx,
582 stts22h_temp_trlhd_src_t *val)
583 {
584 stts22h_status_t status;
585 int32_t ret;
586
587 ret = stts22h_read_reg(ctx, STTS22H_STATUS, (uint8_t *)&status, 1);
588 val->under_thl = status.under_thl;
589 val->over_thh = status.over_thh;
590
591 return ret;
592 }
593
594 /**
595 * @}
596 *
597 */
598
599 /**
600 * @}
601 *
602 */
603
604 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
605