1 /**
2   ******************************************************************************
3   * @file    hts221_reg.c
4   * @author  Sensors Software Solution Team
5   * @brief   HTS221 driver file
6   ******************************************************************************
7   * @attention
8   *
9   * <h2><center>&copy; 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 "hts221_reg.h"
21 
22 /**
23   * @defgroup  HTS221
24   * @brief     This file provides a set of functions needed to drive the
25   *            hts221 enhanced inertial module.
26   * @{
27   *
28   */
29 
30 /**
31   * @defgroup  HTS221_interfaces_functions
32   * @brief     This section provide a set of functions used to read and write
33   *            a generic register of the device.
34   * @{
35   *
36   */
37 
38 /**
39   * @brief  Read generic device register
40   *
41   * @param  ctx   read / write interface definitions(ptr)
42   * @param  reg   register to read
43   * @param  data  pointer to buffer that store the data read(ptr)
44   * @param  len   number of consecutive register to read
45   * @retval       interface status (MANDATORY: return 0 -> no Error)
46   *
47   */
hts221_read_reg(const stmdev_ctx_t * ctx,uint8_t reg,uint8_t * data,uint16_t len)48 int32_t __weak hts221_read_reg(const stmdev_ctx_t *ctx, uint8_t reg, uint8_t *data,
49                                uint16_t len)
50 {
51   int32_t ret;
52 
53   if (ctx == NULL) return -1;
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   */
hts221_write_reg(const stmdev_ctx_t * ctx,uint8_t reg,uint8_t * data,uint16_t len)70 int32_t __weak hts221_write_reg(const stmdev_ctx_t *ctx, uint8_t reg,
71                                 uint8_t *data,
72                                 uint16_t len)
73 {
74   int32_t ret;
75 
76   if (ctx == NULL) return -1;
77 
78   ret = ctx->write_reg(ctx->handle, reg, data, len);
79 
80   return ret;
81 }
82 
83 /**
84   * @}
85   *
86   */
87 
88 /**
89   * @defgroup  HTS221_Data_Generation
90   * @brief     This section group all the functions concerning data generation
91   * @{
92   *
93   */
94 
95 /**
96   * @brief  The numbers of averaged humidity samples.[set]
97   *
98   * @param  ctx     read / write interface definitions
99   * @param  val     change the values of avgh in reg AV_CONF
100   * @retval         interface status (MANDATORY: return 0 -> no Error)
101   *
102   */
hts221_humidity_avg_set(const stmdev_ctx_t * ctx,hts221_avgh_t val)103 int32_t hts221_humidity_avg_set(const stmdev_ctx_t *ctx, hts221_avgh_t val)
104 {
105   hts221_av_conf_t reg;
106   int32_t ret;
107 
108   ret = hts221_read_reg(ctx, HTS221_AV_CONF, (uint8_t *) &reg, 1);
109 
110   if (ret == 0)
111   {
112     reg.avgh = (uint8_t)val;
113     ret = hts221_write_reg(ctx, HTS221_AV_CONF, (uint8_t *) &reg, 1);
114   }
115 
116   return ret;
117 }
118 
119 /**
120   * @brief  The numbers of averaged humidity samples.[get]
121   *
122   * @param  ctx     read / write interface definitions
123   * @param  val     Get the values of avgh in reg AV_CONF
124   * @retval         interface status (MANDATORY: return 0 -> no Error)
125   *
126   */
hts221_humidity_avg_get(const stmdev_ctx_t * ctx,hts221_avgh_t * val)127 int32_t hts221_humidity_avg_get(const stmdev_ctx_t *ctx, hts221_avgh_t *val)
128 {
129   hts221_av_conf_t reg;
130   int32_t ret;
131 
132   ret = hts221_read_reg(ctx, HTS221_AV_CONF, (uint8_t *) &reg, 1);
133 
134   switch (reg.avgh)
135   {
136     case HTS221_H_AVG_4:
137       *val = HTS221_H_AVG_4;
138       break;
139 
140     case HTS221_H_AVG_8:
141       *val = HTS221_H_AVG_8;
142       break;
143 
144     case HTS221_H_AVG_16:
145       *val = HTS221_H_AVG_16;
146       break;
147 
148     case HTS221_H_AVG_32:
149       *val = HTS221_H_AVG_32;
150       break;
151 
152     case HTS221_H_AVG_64:
153       *val = HTS221_H_AVG_64;
154       break;
155 
156     case HTS221_H_AVG_128:
157       *val = HTS221_H_AVG_128;
158       break;
159 
160     case HTS221_H_AVG_256:
161       *val = HTS221_H_AVG_256;
162       break;
163 
164     case HTS221_H_AVG_512:
165       *val = HTS221_H_AVG_512;
166       break;
167 
168     default:
169       *val = HTS221_H_AVG_ND;
170       break;
171   }
172 
173   return ret;
174 }
175 
176 /**
177   * @brief  The numbers of averaged temperature samples.[set]
178   *
179   * @param  ctx     read / write interface definitions
180   * @param  val     change the values of avgt in reg AV_CONF
181   * @retval         interface status (MANDATORY: return 0 -> no Error)
182   *
183   */
hts221_temperature_avg_set(const stmdev_ctx_t * ctx,hts221_avgt_t val)184 int32_t hts221_temperature_avg_set(const stmdev_ctx_t *ctx,
185                                    hts221_avgt_t val)
186 {
187   hts221_av_conf_t reg;
188   int32_t ret;
189 
190   ret = hts221_read_reg(ctx, HTS221_AV_CONF, (uint8_t *) &reg, 1);
191 
192   if (ret == 0)
193   {
194     reg.avgt = (uint8_t)val;
195     ret = hts221_write_reg(ctx, HTS221_AV_CONF, (uint8_t *) &reg, 1);
196   }
197 
198   return ret;
199 }
200 
201 /**
202   * @brief  The numbers of averaged temperature samples.[get]
203   *
204   * @param  ctx     read / write interface definitions
205   * @param  val     Get the values of avgt in reg AV_CONF
206   * @retval         interface status (MANDATORY: return 0 -> no Error)
207   *
208   */
hts221_temperature_avg_get(const stmdev_ctx_t * ctx,hts221_avgt_t * val)209 int32_t hts221_temperature_avg_get(const stmdev_ctx_t *ctx,
210                                    hts221_avgt_t *val)
211 {
212   hts221_av_conf_t reg;
213   int32_t ret;
214 
215   ret = hts221_read_reg(ctx, HTS221_AV_CONF, (uint8_t *) &reg, 1);
216 
217   switch (reg.avgh)
218   {
219     case HTS221_T_AVG_2:
220       *val = HTS221_T_AVG_2;
221       break;
222 
223     case HTS221_T_AVG_4:
224       *val = HTS221_T_AVG_4;
225       break;
226 
227     case HTS221_T_AVG_8:
228       *val = HTS221_T_AVG_8;
229       break;
230 
231     case HTS221_T_AVG_16:
232       *val = HTS221_T_AVG_16;
233       break;
234 
235     case HTS221_T_AVG_32:
236       *val = HTS221_T_AVG_32;
237       break;
238 
239     case HTS221_T_AVG_64:
240       *val = HTS221_T_AVG_64;
241       break;
242 
243     case HTS221_T_AVG_128:
244       *val = HTS221_T_AVG_128;
245       break;
246 
247     case HTS221_T_AVG_256:
248       *val = HTS221_T_AVG_256;
249       break;
250 
251     default:
252       *val = HTS221_T_AVG_ND;
253       break;
254   }
255 
256   return ret;
257 }
258 
259 /**
260   * @brief  Output data rate selection.[set]
261   *
262   * @param  ctx     read / write interface definitions
263   * @param  val     change the values of odr in reg CTRL_REG1
264   * @retval         interface status (MANDATORY: return 0 -> no Error)
265   *
266   */
hts221_data_rate_set(const stmdev_ctx_t * ctx,hts221_odr_t val)267 int32_t hts221_data_rate_set(const stmdev_ctx_t *ctx, hts221_odr_t val)
268 {
269   hts221_ctrl_reg1_t reg;
270   int32_t ret;
271 
272   ret = hts221_read_reg(ctx, HTS221_CTRL_REG1, (uint8_t *) &reg, 1);
273 
274   if (ret == 0)
275   {
276     reg.odr = (uint8_t)val;
277     ret = hts221_write_reg(ctx, HTS221_CTRL_REG1, (uint8_t *) &reg, 1);
278   }
279 
280   return ret;
281 }
282 
283 /**
284   * @brief  Output data rate selection.[get]
285   *
286   * @param  ctx     read / write interface definitions
287   * @param  val     Get the values of odr in reg CTRL_REG1
288   * @retval         interface status (MANDATORY: return 0 -> no Error)
289   *
290   */
hts221_data_rate_get(const stmdev_ctx_t * ctx,hts221_odr_t * val)291 int32_t hts221_data_rate_get(const stmdev_ctx_t *ctx, hts221_odr_t *val)
292 {
293   hts221_ctrl_reg1_t reg;
294   int32_t ret;
295 
296   ret = hts221_read_reg(ctx, HTS221_CTRL_REG1, (uint8_t *) &reg, 1);
297 
298   switch (reg.odr)
299   {
300     case HTS221_ONE_SHOT:
301       *val = HTS221_ONE_SHOT;
302       break;
303 
304     case HTS221_ODR_1Hz:
305       *val = HTS221_ODR_1Hz;
306       break;
307 
308     case HTS221_ODR_7Hz:
309       *val = HTS221_ODR_7Hz;
310       break;
311 
312     case HTS221_ODR_12Hz5:
313       *val = HTS221_ODR_12Hz5;
314       break;
315 
316     default:
317       *val = HTS221_ODR_ND;
318       break;
319   }
320 
321   return ret;
322 }
323 
324 /**
325   * @brief  Block data update.[set]
326   *
327   * @param  ctx     read / write interface definitions
328   * @param  val     change the values of bdu in reg CTRL_REG1
329   * @retval         interface status (MANDATORY: return 0 -> no Error)
330   *
331   */
hts221_block_data_update_set(const stmdev_ctx_t * ctx,uint8_t val)332 int32_t hts221_block_data_update_set(const stmdev_ctx_t *ctx, uint8_t val)
333 {
334   hts221_ctrl_reg1_t reg;
335   int32_t ret;
336 
337   ret = hts221_read_reg(ctx, HTS221_CTRL_REG1, (uint8_t *) &reg, 1);
338 
339   if (ret == 0)
340   {
341     reg.bdu = val;
342     ret = hts221_write_reg(ctx, HTS221_CTRL_REG1, (uint8_t *) &reg, 1);
343   }
344 
345   return ret;
346 }
347 
348 /**
349   * @brief  Block data update.[get]
350   *
351   * @param  ctx     read / write interface definitions
352   * @param  val     change the values of bdu in reg CTRL_REG1
353   * @retval         interface status (MANDATORY: return 0 -> no Error)
354   *
355   */
hts221_block_data_update_get(const stmdev_ctx_t * ctx,uint8_t * val)356 int32_t hts221_block_data_update_get(const stmdev_ctx_t *ctx, uint8_t *val)
357 {
358   hts221_ctrl_reg1_t reg;
359   int32_t ret;
360 
361   ret = hts221_read_reg(ctx, HTS221_CTRL_REG1, (uint8_t *) &reg, 1);
362   *val = reg.bdu;
363 
364   return ret;
365 }
366 
367 /**
368   * @brief  One-shot mode. Device perform a single measure.[set]
369   *
370   * @param  ctx     read / write interface definitions
371   * @param  val     change the values of one_shot in reg CTRL_REG2
372   * @retval         interface status (MANDATORY: return 0 -> no Error)
373   *
374   */
hts221_one_shoot_trigger_set(const stmdev_ctx_t * ctx,uint8_t val)375 int32_t hts221_one_shoot_trigger_set(const stmdev_ctx_t *ctx, uint8_t val)
376 {
377   hts221_ctrl_reg2_t reg;
378   int32_t ret;
379 
380   ret = hts221_read_reg(ctx, HTS221_CTRL_REG2, (uint8_t *) &reg, 1);
381 
382   if (ret == 0)
383   {
384     reg.one_shot = val;
385     ret = hts221_write_reg(ctx, HTS221_CTRL_REG2, (uint8_t *) &reg, 1);
386   }
387 
388   return ret;
389 }
390 
391 /**
392   * @brief  One-shot mode. Device perform a single measure.[get]
393   *
394   * @param  ctx     read / write interface definitions
395   * @param  val     change the values of one_shot in reg CTRL_REG2
396   * @retval         interface status (MANDATORY: return 0 -> no Error)
397   *
398   */
hts221_one_shoot_trigger_get(const stmdev_ctx_t * ctx,uint8_t * val)399 int32_t hts221_one_shoot_trigger_get(const stmdev_ctx_t *ctx, uint8_t *val)
400 {
401   hts221_ctrl_reg2_t reg;
402   int32_t ret;
403 
404   ret = hts221_read_reg(ctx, HTS221_CTRL_REG2, (uint8_t *) &reg, 1);
405   *val = reg.one_shot;
406 
407   return ret;
408 }
409 
410 /**
411   * @brief  Temperature data available.[get]
412   *
413   * @param  ctx     read / write interface definitions
414   * @param  val     change the values of t_da in reg STATUS_REG
415   * @retval         interface status (MANDATORY: return 0 -> no Error)
416   *
417   */
hts221_temp_data_ready_get(const stmdev_ctx_t * ctx,uint8_t * val)418 int32_t hts221_temp_data_ready_get(const stmdev_ctx_t *ctx, uint8_t *val)
419 {
420   hts221_status_reg_t reg;
421   int32_t ret;
422 
423   ret = hts221_read_reg(ctx, HTS221_STATUS_REG, (uint8_t *) &reg, 1);
424   *val = reg.t_da;
425 
426   return ret;
427 }
428 
429 /**
430   * @brief  Humidity data available.[get]
431   *
432   * @param  ctx     read / write interface definitions
433   * @param  val     change the values of h_da in reg STATUS_REG
434   * @retval         interface status (MANDATORY: return 0 -> no Error)
435   *
436   */
hts221_hum_data_ready_get(const stmdev_ctx_t * ctx,uint8_t * val)437 int32_t hts221_hum_data_ready_get(const stmdev_ctx_t *ctx, uint8_t *val)
438 {
439   hts221_status_reg_t reg;
440   int32_t ret;
441 
442   ret = hts221_read_reg(ctx, HTS221_STATUS_REG, (uint8_t *) &reg, 1);
443   *val = reg.h_da;
444 
445   return ret;
446 }
447 
448 /**
449   * @brief  Humidity output value[get]
450   *
451   * @param  ctx     read / write interface definitions
452   * @param  buff    buffer that stores data read
453   * @retval         interface status (MANDATORY: return 0 -> no Error)
454   *
455   */
hts221_humidity_raw_get(const stmdev_ctx_t * ctx,int16_t * val)456 int32_t hts221_humidity_raw_get(const stmdev_ctx_t *ctx, int16_t *val)
457 {
458   uint8_t buff[2];
459   int32_t ret;
460 
461   ret = hts221_read_reg(ctx, HTS221_HUMIDITY_OUT_L, buff, 2);
462   *val = (int16_t)buff[1];
463   *val = (*val * 256) + (int16_t)buff[0];
464 
465   return ret;
466 }
467 
468 /**
469   * @brief  Temperature output value[get]
470   *
471   * @param  ctx     read / write interface definitions
472   * @param  buff    buffer that stores data read
473   * @retval         interface status (MANDATORY: return 0 -> no Error)
474   *
475   */
hts221_temperature_raw_get(const stmdev_ctx_t * ctx,int16_t * val)476 int32_t hts221_temperature_raw_get(const stmdev_ctx_t *ctx, int16_t *val)
477 {
478   uint8_t buff[2];
479   int32_t ret;
480 
481   ret = hts221_read_reg(ctx, HTS221_TEMP_OUT_L, buff, 2);
482   *val = (int16_t)buff[1];
483   *val = (*val * 256) + (int16_t)buff[0];
484 
485   return ret;
486 }
487 
488 /**
489   * @}
490   *
491   */
492 
493 /**
494   * @defgroup  HTS221_common
495   * @brief     This section group common useful functions
496   * @{
497   *
498   */
499 
500 /**
501   * @brief  Device Who amI.[get]
502   *
503   * @param  ctx     read / write interface definitions
504   * @param  buff    buffer that stores data read
505   * @retval         interface status (MANDATORY: return 0 -> no Error)
506   *
507   */
hts221_device_id_get(const stmdev_ctx_t * ctx,uint8_t * buff)508 int32_t hts221_device_id_get(const stmdev_ctx_t *ctx, uint8_t *buff)
509 {
510   int32_t ret;
511 
512   ret = hts221_read_reg(ctx, HTS221_WHO_AM_I, buff, 1);
513 
514   return ret;
515 }
516 
517 /**
518   * @brief  Switch device on/off.[set]
519   *
520   * @param  ctx     read / write interface definitions
521   * @param  val     change the values of pd in reg CTRL_REG1
522   * @retval         interface status (MANDATORY: return 0 -> no Error)
523   *
524   */
hts221_power_on_set(const stmdev_ctx_t * ctx,uint8_t val)525 int32_t hts221_power_on_set(const stmdev_ctx_t *ctx, uint8_t val)
526 {
527   hts221_ctrl_reg1_t reg;
528   int32_t ret;
529 
530   ret = hts221_read_reg(ctx, HTS221_CTRL_REG1, (uint8_t *) &reg, 1);
531 
532   if (ret == 0)
533   {
534     reg.pd = val;
535     ret = hts221_write_reg(ctx, HTS221_CTRL_REG1, (uint8_t *) &reg, 1);
536   }
537 
538   return ret;
539 }
540 
541 /**
542   * @brief  Switch device on/off.[get]
543   *
544   * @param  ctx     read / write interface definitions
545   * @param  val     change the values of pd in reg CTRL_REG1
546   * @retval         interface status (MANDATORY: return 0 -> no Error)
547   *
548   */
hts221_power_on_get(const stmdev_ctx_t * ctx,uint8_t * val)549 int32_t hts221_power_on_get(const stmdev_ctx_t *ctx, uint8_t *val)
550 {
551   hts221_ctrl_reg1_t reg;
552   int32_t ret;
553 
554   ret = hts221_read_reg(ctx, HTS221_CTRL_REG1, (uint8_t *) &reg, 1);
555   *val = reg.pd;
556 
557   return ret;
558 }
559 
560 /**
561   * @brief  Heater enable / disable.[set]
562   *
563   * @param  ctx     read / write interface definitions
564   * @param  val     change the values of heater in reg CTRL_REG2
565   * @retval         interface status (MANDATORY: return 0 -> no Error)
566   *
567   */
hts221_heater_set(const stmdev_ctx_t * ctx,uint8_t val)568 int32_t hts221_heater_set(const stmdev_ctx_t *ctx, uint8_t val)
569 {
570   hts221_ctrl_reg2_t reg;
571   int32_t ret;
572 
573   ret = hts221_read_reg(ctx, HTS221_CTRL_REG2, (uint8_t *) &reg, 1);
574 
575   if (ret == 0)
576   {
577     reg.heater = val;
578     ret = hts221_write_reg(ctx, HTS221_CTRL_REG2, (uint8_t *) &reg, 1);
579   }
580 
581   return ret;
582 }
583 
584 /**
585   * @brief  Heater enable / disable.[get]
586   *
587   * @param  ctx     read / write interface definitions
588   * @param  val     change the values of heater in reg CTRL_REG2
589   * @retval         interface status (MANDATORY: return 0 -> no Error)
590   *
591   */
hts221_heater_get(const stmdev_ctx_t * ctx,uint8_t * val)592 int32_t hts221_heater_get(const stmdev_ctx_t *ctx, uint8_t *val)
593 {
594   hts221_ctrl_reg2_t reg;
595   int32_t ret;
596 
597   ret = hts221_read_reg(ctx, HTS221_CTRL_REG2, (uint8_t *) &reg, 1);
598   *val = reg.heater;
599 
600   return ret;
601 }
602 
603 /**
604   * @brief  Reboot memory content. Reload the calibration parameters.[set]
605   *
606   * @param  ctx     read / write interface definitions
607   * @param  val     change the values of boot in reg CTRL_REG2
608   * @retval         interface status (MANDATORY: return 0 -> no Error)
609   *
610   */
hts221_boot_set(const stmdev_ctx_t * ctx,uint8_t val)611 int32_t hts221_boot_set(const stmdev_ctx_t *ctx, uint8_t val)
612 {
613   hts221_ctrl_reg2_t reg;
614   int32_t ret;
615 
616   ret = hts221_read_reg(ctx, HTS221_CTRL_REG2, (uint8_t *) &reg, 1);
617 
618   if (ret == 0)
619   {
620     reg.boot = val;
621     ret = hts221_write_reg(ctx, HTS221_CTRL_REG2, (uint8_t *) &reg, 1);
622   }
623 
624   return ret;
625 }
626 
627 /**
628   * @brief  Reboot memory content. Reload the calibration parameters.[get]
629   *
630   * @param  ctx     read / write interface definitions
631   * @param  val     change the values of boot in reg CTRL_REG2
632   * @retval         interface status (MANDATORY: return 0 -> no Error)
633   *
634   */
hts221_boot_get(const stmdev_ctx_t * ctx,uint8_t * val)635 int32_t hts221_boot_get(const stmdev_ctx_t *ctx, uint8_t *val)
636 {
637   hts221_ctrl_reg2_t reg;
638   int32_t ret;
639 
640   ret = hts221_read_reg(ctx, HTS221_CTRL_REG2, (uint8_t *) &reg, 1);
641   *val = reg.boot;
642 
643   return ret;
644 }
645 
646 /**
647   * @brief  Info about device status.[get]
648   *
649   * @param  ctx     read / write interface definitions
650   * @param  val     Registers STATUS_REG
651   * @retval         interface status (MANDATORY: return 0 -> no Error)
652   *
653   */
hts221_status_get(const stmdev_ctx_t * ctx,hts221_status_reg_t * val)654 int32_t hts221_status_get(const stmdev_ctx_t *ctx, hts221_status_reg_t *val)
655 {
656   int32_t ret;
657 
658   ret = hts221_read_reg(ctx, HTS221_STATUS_REG, (uint8_t *) val, 1);
659 
660   return ret;
661 }
662 
663 /**
664   * @}
665   *
666   */
667 
668 /**
669   * @defgroup  HTS221_interrupts
670   * @brief   This section group all the functions that manage interrupts
671   * @{
672   *
673   */
674 
675 /**
676   * @brief  Data-ready signal on INT_DRDY pin.[set]
677   *
678   * @param  ctx     read / write interface definitions
679   * @param  val     change the values of drdy in reg CTRL_REG3
680   * @retval         interface status (MANDATORY: return 0 -> no Error)
681   *
682   */
hts221_drdy_on_int_set(const stmdev_ctx_t * ctx,uint8_t val)683 int32_t hts221_drdy_on_int_set(const stmdev_ctx_t *ctx, uint8_t val)
684 {
685   hts221_ctrl_reg3_t reg;
686   int32_t ret;
687 
688   ret = hts221_read_reg(ctx, HTS221_CTRL_REG3, (uint8_t *) &reg, 1);
689 
690   if (ret == 0)
691   {
692     reg.drdy = val;
693     ret = hts221_write_reg(ctx, HTS221_CTRL_REG3, (uint8_t *) &reg, 1);
694   }
695 
696   return ret;
697 }
698 
699 /**
700   * @brief  Data-ready signal on INT_DRDY pin.[get]
701   *
702   * @param  ctx     read / write interface definitions
703   * @param  val     change the values of drdy in reg CTRL_REG3
704   * @retval         interface status (MANDATORY: return 0 -> no Error)
705   *
706   */
hts221_drdy_on_int_get(const stmdev_ctx_t * ctx,uint8_t * val)707 int32_t hts221_drdy_on_int_get(const stmdev_ctx_t *ctx, uint8_t *val)
708 {
709   hts221_ctrl_reg3_t reg;
710   int32_t ret;
711 
712   ret = hts221_read_reg(ctx, HTS221_CTRL_REG3, (uint8_t *) &reg, 1);
713   *val = reg.drdy;
714 
715   return ret;
716 }
717 
718 /**
719   * @brief  Push-pull/open drain selection on interrupt pads.[set]
720   *
721   * @param  ctx     read / write interface definitions
722   * @param  val     change the values of pp_od in reg CTRL_REG3
723   *
724   */
hts221_pin_mode_set(const stmdev_ctx_t * ctx,hts221_pp_od_t val)725 int32_t hts221_pin_mode_set(const stmdev_ctx_t *ctx, hts221_pp_od_t val)
726 {
727   hts221_ctrl_reg3_t reg;
728   int32_t ret;
729 
730   ret = hts221_read_reg(ctx, HTS221_CTRL_REG3, (uint8_t *) &reg, 1);
731 
732   if (ret == 0)
733   {
734     reg.pp_od = (uint8_t)val;
735     ret = hts221_write_reg(ctx, HTS221_CTRL_REG3, (uint8_t *) &reg, 1);
736   }
737 
738   return ret;
739 }
740 
741 /**
742   * @brief  Push-pull/open drain selection on interrupt pads.[get]
743   *
744   * @param  ctx     read / write interface definitions
745   * @param  val     Get the values of pp_od in reg CTRL_REG3
746   * @retval         interface status (MANDATORY: return 0 -> no Error)
747   *
748   */
hts221_pin_mode_get(const stmdev_ctx_t * ctx,hts221_pp_od_t * val)749 int32_t hts221_pin_mode_get(const stmdev_ctx_t *ctx, hts221_pp_od_t *val)
750 {
751   hts221_ctrl_reg3_t reg;
752   int32_t ret;
753 
754   ret = hts221_read_reg(ctx, HTS221_CTRL_REG3, (uint8_t *) &reg, 1);
755 
756   switch (reg.pp_od)
757   {
758     case HTS221_PUSH_PULL:
759       *val = HTS221_PUSH_PULL;
760       break;
761 
762     case HTS221_OPEN_DRAIN:
763       *val = HTS221_OPEN_DRAIN;
764       break;
765 
766     default:
767       *val = HTS221_PIN_MODE_ND;
768       break;
769   }
770 
771   return ret;
772 }
773 
774 /**
775   * @brief  Interrupt active-high/low.[set]
776   *
777   * @param  ctx     read / write interface definitions
778   * @param  val     change the values of drdy_h_l in reg CTRL_REG3
779   * @retval         interface status (MANDATORY: return 0 -> no Error)
780   *
781   */
hts221_int_polarity_set(const stmdev_ctx_t * ctx,hts221_drdy_h_l_t val)782 int32_t hts221_int_polarity_set(const stmdev_ctx_t *ctx,
783                                 hts221_drdy_h_l_t val)
784 {
785   hts221_ctrl_reg3_t reg;
786   int32_t ret;
787 
788   ret = hts221_read_reg(ctx, HTS221_CTRL_REG3, (uint8_t *) &reg, 1);
789 
790   if (ret == 0)
791   {
792     reg.drdy_h_l = (uint8_t)val;
793     ret = hts221_write_reg(ctx, HTS221_CTRL_REG3, (uint8_t *) &reg, 1);
794   }
795 
796   return ret;
797 }
798 
799 /**
800   * @brief  Interrupt active-high/low.[get]
801   *
802   * @param  ctx     read / write interface definitions
803   * @param  val     Get the values of drdy_h_l in reg CTRL_REG3
804   * @retval         interface status (MANDATORY: return 0 -> no Error)
805   *
806   */
hts221_int_polarity_get(const stmdev_ctx_t * ctx,hts221_drdy_h_l_t * val)807 int32_t hts221_int_polarity_get(const stmdev_ctx_t *ctx,
808                                 hts221_drdy_h_l_t *val)
809 {
810   hts221_ctrl_reg3_t reg;
811   int32_t ret;
812 
813   ret = hts221_read_reg(ctx, HTS221_CTRL_REG3, (uint8_t *) &reg, 1);
814 
815   switch (reg.drdy_h_l)
816   {
817     case HTS221_ACTIVE_HIGH:
818       *val = HTS221_ACTIVE_HIGH;
819       break;
820 
821     case HTS221_ACTIVE_LOW:
822       *val = HTS221_ACTIVE_LOW;
823       break;
824 
825     default:
826       *val = HTS221_ACTIVE_ND;
827       break;
828   }
829 
830   return ret;
831 }
832 
833 /**
834   * @}
835   *
836   */
837 
838 /**
839   * @defgroup  HTS221_calibration
840   * @brief     This section group all the calibration coefficients need
841   *            for reading data
842   * @{
843   *
844   */
845 
846 /**
847   * @brief  First calibration point for Rh Humidity.[get]
848   *
849   * @param  ctx     read / write interface definitions
850   * @param  val     buffer that stores data read
851   * @retval         interface status (MANDATORY: return 0 -> no Error)
852   *
853   */
hts221_hum_rh_point_0_get(const stmdev_ctx_t * ctx,float_t * val)854 int32_t hts221_hum_rh_point_0_get(const stmdev_ctx_t *ctx, float_t *val)
855 {
856   uint8_t coeff;
857   int32_t ret;
858 
859   ret = hts221_read_reg(ctx, HTS221_H0_RH_X2, &coeff, 1);
860   *val = coeff / 2.0f;
861 
862   return ret;
863 }
864 
865 /**
866   * @brief  Second calibration point for Rh Humidity.[get]
867   *
868   * @param  ctx     read / write interface definitions
869   * @param  val     buffer that stores data read
870   * @retval         interface status (MANDATORY: return 0 -> no Error)
871   *
872   */
hts221_hum_rh_point_1_get(const stmdev_ctx_t * ctx,float_t * val)873 int32_t hts221_hum_rh_point_1_get(const stmdev_ctx_t *ctx, float_t *val)
874 {
875   uint8_t coeff;
876   int32_t ret;
877 
878   ret = hts221_read_reg(ctx, HTS221_H1_RH_X2, &coeff, 1);
879   *val = coeff / 2.0f;
880 
881   return ret;
882 }
883 
884 /**
885   * @brief  First calibration point for degC temperature.[get]
886   *
887   * @param  ctx     read / write interface definitions
888   * @param  buff    buffer that stores data read
889   * @retval         interface status (MANDATORY: return 0 -> no Error)
890   *
891   */
hts221_temp_deg_point_0_get(const stmdev_ctx_t * ctx,float_t * val)892 int32_t hts221_temp_deg_point_0_get(const stmdev_ctx_t *ctx, float_t *val)
893 {
894   hts221_t1_t0_msb_t reg;
895   uint8_t coeff_h;
896   uint8_t coeff_l;
897   int32_t ret;
898 
899   ret = hts221_read_reg(ctx, HTS221_T0_DEGC_X8, &coeff_l, 1);
900 
901   if (ret == 0)
902   {
903     ret = hts221_read_reg(ctx, HTS221_T1_T0_MSB, (uint8_t *) &reg, 1);
904     coeff_h = reg.t0_msb;
905     *val = ((coeff_h * 256) + coeff_l) / 8.0f;
906   }
907 
908   return ret;
909 }
910 
911 /**
912   * @brief  Second calibration point for degC temperature.[get]
913   *
914   * @param  ctx     read / write interface definitions
915   * @param  val     buffer that stores data read
916   * @retval         interface status (MANDATORY: return 0 -> no Error)
917   *
918   */
hts221_temp_deg_point_1_get(const stmdev_ctx_t * ctx,float_t * val)919 int32_t hts221_temp_deg_point_1_get(const stmdev_ctx_t *ctx, float_t *val)
920 {
921   hts221_t1_t0_msb_t reg;
922   uint8_t coeff_h;
923   uint8_t coeff_l;
924   int32_t ret;
925 
926   ret = hts221_read_reg(ctx, HTS221_T1_DEGC_X8, &coeff_l, 1);
927 
928   if (ret == 0)
929   {
930     ret = hts221_read_reg(ctx, HTS221_T1_T0_MSB, (uint8_t *) &reg, 1);
931     coeff_h = reg.t1_msb;
932     *val = ((coeff_h * 256) + coeff_l) / 8.0f;
933   }
934 
935   return ret;
936 }
937 
938 /**
939   * @brief  First calibration point for humidity in LSB.[get]
940   *
941   * @param  ctx     read / write interface definitions
942   * @param  val     buffer that stores data read
943   * @retval         interface status (MANDATORY: return 0 -> no Error)
944   *
945   */
hts221_hum_adc_point_0_get(const stmdev_ctx_t * ctx,float_t * val)946 int32_t hts221_hum_adc_point_0_get(const stmdev_ctx_t *ctx, float_t *val)
947 {
948   uint8_t coeff_p[2];
949   int16_t coeff;
950   int32_t ret;
951 
952   ret = hts221_read_reg(ctx, HTS221_H0_T0_OUT_L, coeff_p, 2);
953   coeff = (coeff_p[1] * 256) + coeff_p[0];
954   *val = coeff * 1.0f;
955 
956   return ret;
957 }
958 
959 /**
960   * @brief  Second calibration point for humidity in LSB.[get]
961   *
962   * @param  ctx     read / write interface definitions
963   * @param  val     buffer that stores data read
964   * @retval         interface status (MANDATORY: return 0 -> no Error)
965   *
966   */
hts221_hum_adc_point_1_get(const stmdev_ctx_t * ctx,float_t * val)967 int32_t hts221_hum_adc_point_1_get(const stmdev_ctx_t *ctx, float_t *val)
968 {
969   uint8_t coeff_p[2];
970   int16_t coeff;
971   int32_t ret;
972 
973   ret = hts221_read_reg(ctx, HTS221_H1_T0_OUT_L, coeff_p, 2);
974   coeff = (coeff_p[1] * 256) + coeff_p[0];
975   *val = coeff * 1.0f;
976 
977   return ret;
978 }
979 
980 /**
981   * @brief  First calibration point for temperature in LSB.[get]
982   *
983   * @param  ctx     read / write interface definitions
984   * @param  val     buffer that stores data read
985   * @retval         interface status (MANDATORY: return 0 -> no Error)
986   *
987   */
hts221_temp_adc_point_0_get(const stmdev_ctx_t * ctx,float_t * val)988 int32_t hts221_temp_adc_point_0_get(const stmdev_ctx_t *ctx, float_t *val)
989 {
990   uint8_t coeff_p[2];
991   int16_t coeff;
992   int32_t ret;
993 
994   ret = hts221_read_reg(ctx, HTS221_T0_OUT_L, coeff_p, 2);
995   coeff = (coeff_p[1] * 256) + coeff_p[0];
996   *val = coeff * 1.0f;
997 
998   return ret;
999 }
1000 
1001 /**
1002   * @brief  Second calibration point for temperature in LSB.[get]
1003   *
1004   * @param  ctx     read / write interface definitions
1005   * @param  val     buffer that stores data read
1006   * @retval         interface status (MANDATORY: return 0 -> no Error)
1007   *
1008   */
hts221_temp_adc_point_1_get(const stmdev_ctx_t * ctx,float_t * val)1009 int32_t hts221_temp_adc_point_1_get(const stmdev_ctx_t *ctx, float_t *val)
1010 {
1011   uint8_t coeff_p[2];
1012   int16_t coeff;
1013   int32_t ret;
1014 
1015   ret = hts221_read_reg(ctx, HTS221_T1_OUT_L, coeff_p, 2);
1016   coeff = (coeff_p[1] * 256) + coeff_p[0];
1017   *val = coeff * 1.0f;
1018 
1019   return ret;
1020 }
1021 
1022 /**
1023   * @}
1024   *
1025   */
1026 
1027 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
1028