ChibiOS  0.0.0
hts221.c
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2016..2018 Rocco Marco Guglielmi
3 
4  This file is part of ChibiOS.
5 
6  ChibiOS is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 3 of the License, or
9  (at your option) any later version.
10 
11  ChibiOS is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
19 */
20 
21 /**
22  * @file hts221.c
23  * @brief HTS221 MEMS interface module code.
24  *
25  * @addtogroup HTS221
26  * @ingroup EX_ST
27  * @{
28  */
29 
30 #include "hal.h"
31 #include "hts221.h"
32 
33 /*===========================================================================*/
34 /* Driver local definitions. */
35 /*===========================================================================*/
36 
37 #define HTS221_SEL(mask, offset) (int16_t)(mask << offset)
38 
39 #define HTS221_FLAG_HYGRO_BIAS 0x01
40 #define HTS221_FLAG_HYGRO_SENS 0x02
41 #define HTS221_FLAG_THERMO_BIAS 0x04
42 #define HTS221_FLAG_THERMO_SENS 0x08
43 
44 /*===========================================================================*/
45 /* Driver exported variables. */
46 /*===========================================================================*/
47 
48 /*===========================================================================*/
49 /* Driver local variables and types. */
50 /*===========================================================================*/
51 
52 /*===========================================================================*/
53 /* Driver local functions. */
54 /*===========================================================================*/
55 
56 #if (HTS221_USE_I2C) || defined(__DOXYGEN__)
57 /**
58  * @brief Reads registers value using I2C.
59  * @pre The I2C interface must be initialized and the driver started.
60  *
61  * @param[in] i2cp pointer to the I2C interface
62  * @param[in] reg first sub-register address
63  * @param[out] rxbuf pointer to an output buffer
64  * @param[in] n number of consecutive register to read
65  * @return the operation status.
66  *
67  * @notapi
68  */
69 static msg_t hts221I2CReadRegister(I2CDriver *i2cp, uint8_t reg, uint8_t* rxbuf,
70  size_t n) {
71  uint8_t txbuf = reg;
72  if (n > 1)
73  txbuf |= HTS221_SUB_MS;
74 
75  return i2cMasterTransmitTimeout(i2cp, HTS221_SAD, &txbuf, 1, rxbuf, n,
77 }
78 
79 /**
80  * @brief Writes a value into a register using I2C.
81  * @pre The I2C interface must be initialized and the driver started.
82  *
83  * @param[in] i2cp pointer to the I2C interface
84  * @param[in] txbuf buffer containing sub-address value in first position
85  * and values to write
86  * @param[in] n size of txbuf less one (not considering the first
87  * element)
88  * @return the operation status.
89  *
90  * @notapi
91  */
92 static msg_t hts221I2CWriteRegister(I2CDriver *i2cp, uint8_t* txbuf, size_t n) {
93  if (n > 1)
94  (*txbuf) |= HTS221_SUB_MS;
95 
96  return i2cMasterTransmitTimeout(i2cp, HTS221_SAD, txbuf, n + 1, NULL, 0,
98 }
99 #endif /* HTS221_USE_I2C */
100 
101 /**
102  * @brief Computes biases and sensitivities starting from data stored in
103  * calibration registers.
104  * @note Factory bias and sensitivity values are stored into the driver
105  * structure.
106  *
107  * @param[in] devp pointer to the HTS221 interface
108  * @return the operation status.
109  *
110  * @notapi
111  */
113  msg_t msg;
114  uint8_t calib[16], H0_rH_x2, H1_rH_x2, msb;
115  int16_t H0_T0_OUT, H1_T0_OUT, T0_degC_x8, T1_degC_x8, T0_OUT, T1_OUT;
116 
117 #if HTS221_SHARED_I2C
118  i2cAcquireBus(devp->config->i2cp);
119  i2cStart(devp->config->i2cp, devp->config->i2ccfg);
120 #endif /* HTS221_SHARED_I2C */
121 
122  /* Retrieving rH values from Calibration registers */
123  msg = hts221I2CReadRegister(devp->config->i2cp,
124  HTS221_AD_CALIB_0, calib, 16);
125 
126 #if HTS221_SHARED_I2C
127  i2cReleaseBus(devp->config->i2cp);
128 #endif /* HTS221_SHARED_I2C */
129 
130  H0_rH_x2 = calib[0];
131  H1_rH_x2 = calib[1];
132  H0_T0_OUT = calib[6];
133  H0_T0_OUT += calib[7] << 8;
134  H1_T0_OUT = calib[10];
135  H1_T0_OUT += calib[11] << 8;
136 
137  T0_degC_x8 = calib[2];
138 
139  /* Completing T0_degC_x8 value */
140  msb = (calib[5] & HTS221_SEL(0x03, 0));
141  if (msb & HTS221_SEL(0x01, 1)) {
142  msb |= HTS221_SEL(0x3F, 2);
143  }
144  T0_degC_x8 += msb << 8;
145 
146  T1_degC_x8 = calib[3];
147  /* Completing T1_degC_x8 value */
148  msb = ((calib[5] & HTS221_SEL(0x03, 2)) >> 2);
149  if (msb & HTS221_SEL(0x01, 1)) {
150  msb |= HTS221_SEL(0x3F, 2);
151  }
152  T1_degC_x8 += msb << 8;
153 
154  T0_OUT = calib[12];
155  T0_OUT += calib[13] << 8;
156  T1_OUT = calib[14];
157  T1_OUT += calib[15] << 8;
158 
159  devp->hygrofactorysensitivity = ((float)H1_rH_x2 - (float)H0_rH_x2) /
160  (((float)H1_T0_OUT - (float)H0_T0_OUT) * 2.0f);
161 
162 
163  devp->hygrofactorybias = (devp->hygrofactorysensitivity * (float)H0_T0_OUT) -
164  ((float)H0_rH_x2 / 2.0f);
165 
166  devp->thermofactorysensitivity = ((float)T1_degC_x8 - (float)T0_degC_x8) /
167  (((float)T1_OUT - (float)T0_OUT) * 8.0f);
168 
169  devp->thermofactorybias = (devp->thermofactorysensitivity * (float)T0_OUT) -
170  ((float)T0_degC_x8 / 8.0f);
171 
172  return msg;
173 }
174 
175 /**
176  * @brief Return the number of axes of the BaseHygrometer.
177  *
178  * @param[in] ip pointer to @p BaseHygrometer interface.
179  *
180  * @return the number of axes.
181  */
182 static size_t hygro_get_axes_number(void *ip) {
183  (void)ip;
184 
186 }
187 
188 /**
189  * @brief Retrieves raw data from the BaseHygrometer.
190  * @note This data is retrieved from MEMS register without any algebraical
191  * manipulation.
192  * @note The axes array must be at least the same size of the
193  * BaseHygrometer axes number.
194  *
195  * @param[in] ip pointer to @p BaseHygrometer interface.
196  * @param[out] axes a buffer which would be filled with raw data.
197  *
198  * @return The operation status.
199  * @retval MSG_OK if the function succeeded.
200  * @retval MSG_RESET if one or more I2C errors occurred, the errors can
201  * be retrieved using @p i2cGetErrors().
202  * @retval MSG_TIMEOUT if a timeout occurred before operation end.
203  */
204 static msg_t hygro_read_raw(void *ip, int32_t axes[]) {
205  HTS221Driver* devp;
206  uint8_t buff[2];
207  int16_t tmp;
208  msg_t msg;
209 
210  osalDbgCheck((ip != NULL) && (axes != NULL));
211 
212  /* Getting parent instance pointer.*/
214 
215  osalDbgAssert((devp->state == HTS221_READY),
216  "hygro_read_raw(), invalid state");
217 
218  osalDbgAssert((devp->config->i2cp->state == I2C_READY),
219  "hygro_read_raw(), channel not ready");
220 
221 #if HTS221_SHARED_I2C
222  i2cAcquireBus(devp->config->i2cp);
223  i2cStart(devp->config->i2cp,
224  devp->config->i2ccfg);
225 #endif /* HTS221_SHARED_I2C */
226 
227  msg = hts221I2CReadRegister(devp->config->i2cp, HTS221_AD_HUMIDITY_OUT_L,
228  buff, 2);
229 
230 #if HTS221_SHARED_I2C
231  i2cReleaseBus(devp->config->i2cp);
232 #endif /* HTS221_SHARED_I2C */
233 
234  if (msg == MSG_OK) {
235  tmp = buff[0] + (buff[1] << 8);
236  *axes = (int32_t)tmp;
237  }
238  return msg;
239 }
240 
241 /**
242  * @brief Retrieves cooked data from the BaseHygrometer.
243  * @note This data is manipulated according to the formula
244  * cooked = (raw * sensitivity) - bias.
245  * @note Final data is expressed as %rH.
246  * @note The axes array must be at least the same size of the
247  * BaseHygrometer axes number.
248  *
249  * @param[in] ip pointer to @p BaseHygrometer interface.
250  * @param[out] axes a buffer which would be filled with cooked data.
251  *
252  * @return The operation status.
253  * @retval MSG_OK if the function succeeded.
254  * @retval MSG_RESET if one or more I2C errors occurred, the errors can
255  * be retrieved using @p i2cGetErrors().
256  * @retval MSG_TIMEOUT if a timeout occurred before operation end.
257  */
258 static msg_t hygro_read_cooked(void *ip, float axes[]) {
259  HTS221Driver* devp;
260  int32_t raw;
261  msg_t msg;
262 
263  osalDbgCheck((ip != NULL) && (axes != NULL));
264 
265  /* Getting parent instance pointer.*/
267 
268  osalDbgAssert((devp->state == HTS221_READY),
269  "hygro_read_cooked(), invalid state");
270 
271  msg = hygro_read_raw(ip, &raw);
272 
273  *axes = (raw * devp->hygrosensitivity) - devp->hygrobias;
274 
275  return msg;
276 }
277 
278 /**
279  * @brief Set bias values for the BaseHygrometer.
280  * @note Bias must be expressed as %rH.
281  * @note The bias buffer must be at least the same size of the
282  * BaseHygrometer axes number.
283  *
284  * @param[in] ip pointer to @p BaseHygrometer interface.
285  * @param[in] bp a buffer which contains biases.
286  *
287  * @return The operation status.
288  * @retval MSG_OK if the function succeeded.
289  * @retval MSG_RESET if one or more I2C errors occurred, the errors can
290  * be retrieved using @p i2cGetErrors().
291  * @retval MSG_TIMEOUT if a timeout occurred before operation end.
292  */
293 static msg_t hygro_set_bias(void *ip, float *bp) {
294  HTS221Driver* devp;
295  msg_t msg = MSG_OK;
296 
297  osalDbgCheck((ip != NULL) && (bp != NULL));
298 
299  /* Getting parent instance pointer.*/
301 
302  osalDbgAssert((devp->state == HTS221_READY),
303  "hygro_set_bias(), invalid state");
304 
305  devp->hygrobias = *bp;
306  return msg;
307 }
308 
309 /**
310  * @brief Reset bias values for the BaseHygrometer.
311  * @note Default biases value are obtained from device datasheet when
312  * available otherwise they are considered zero.
313  *
314  * @param[in] ip pointer to @p BaseHygrometer interface.
315  *
316  * @return The operation status.
317  * @retval MSG_OK if the function succeeded.
318  */
319 static msg_t hygro_reset_bias(void *ip) {
320  HTS221Driver* devp;
321  msg_t msg = MSG_OK;
322 
323  osalDbgCheck(ip != NULL);
324 
325  /* Getting parent instance pointer.*/
327 
328  osalDbgAssert((devp->state == HTS221_READY),
329  "hygro_reset_bias(), invalid state");
330 
331  devp->hygrobias = devp->hygrofactorybias;
332  return msg;
333 }
334 
335 /**
336  * @brief Set sensitivity values for the BaseHygrometer.
337  * @note Sensitivity must be expressed as %rH/LSB.
338  * @note The sensitivity buffer must be at least the same size of the
339  * BaseHygrometer axes number.
340  *
341  * @param[in] ip pointer to @p BaseHygrometer interface.
342  * @param[in] sp a buffer which contains sensitivities.
343  *
344  * @return The operation status.
345  * @retval MSG_OK if the function succeeded.
346  */
347 static msg_t hygro_set_sensitivity(void *ip, float *sp) {
348  HTS221Driver* devp;
349  msg_t msg = MSG_OK;
350 
351  osalDbgCheck((ip != NULL) && (sp != NULL));
352 
353  /* Getting parent instance pointer.*/
355 
356  osalDbgAssert((devp->state == HTS221_READY),
357  "hygro_set_sensitivity(), invalid state");
358 
359  devp->hygrosensitivity = *sp;
360  return msg;
361 }
362 
363 /**
364  * @brief Reset sensitivity values for the BaseHygrometer.
365  * @note Default sensitivities value are obtained from device datasheet.
366  *
367  * @param[in] ip pointer to @p BaseHygrometer interface.
368  *
369  * @return The operation status.
370  * @retval MSG_OK if the function succeeded.
371  */
372 static msg_t hygro_reset_sensitivity(void *ip) {
373  HTS221Driver* devp;
374  msg_t msg = MSG_OK;
375 
376  osalDbgCheck(ip != NULL);
377 
378  /* Getting parent instance pointer.*/
380 
381  osalDbgAssert((devp->state == HTS221_READY),
382  "hygro_reset_sensitivity(), invalid state");
383 
384  devp->hygrosensitivity = devp->hygrofactorysensitivity;
385  return msg;
386 }
387 
388 /**
389  * @brief Return the number of axes of the BaseThermometer.
390  *
391  * @param[in] ip pointer to @p BaseThermometer interface.
392  *
393  * @return the number of axes.
394  */
395 static size_t thermo_get_axes_number(void *ip) {
396  (void)ip;
397 
399 }
400 
401 /**
402  * @brief Retrieves raw data from the BaseThermometer.
403  * @note This data is retrieved from MEMS register without any algebraical
404  * manipulation.
405  * @note The axes array must be at least the same size of the
406  * BaseThermometer axes number.
407  *
408  * @param[in] ip pointer to @p BaseThermometer interface.
409  * @param[out] axes a buffer which would be filled with raw data.
410  *
411  * @return The operation status.
412  * @retval MSG_OK if the function succeeded.
413  * @retval MSG_RESET if one or more I2C errors occurred, the errors can
414  * be retrieved using @p i2cGetErrors().
415  * @retval MSG_TIMEOUT if a timeout occurred before operation end.
416  */
417 static msg_t thermo_read_raw(void *ip, int32_t axes[]) {
418  HTS221Driver* devp;
419  int16_t tmp;
420  uint8_t buff[2];
421  msg_t msg;
422 
423  osalDbgCheck((ip != NULL) && (axes != NULL));
424 
425  /* Getting parent instance pointer.*/
427 
428  osalDbgAssert((devp->state == HTS221_READY),
429  "thermo_read_raw(), invalid state");
430 
431  osalDbgAssert((devp->config->i2cp->state == I2C_READY),
432  "thermo_read_raw(), channel not ready");
433 
434 #if HTS221_SHARED_I2C
435  i2cAcquireBus(devp->config->i2cp);
436  i2cStart(devp->config->i2cp,
437  devp->config->i2ccfg);
438 #endif /* HTS221_SHARED_I2C */
439 
440  msg = hts221I2CReadRegister(devp->config->i2cp, HTS221_AD_TEMP_OUT_L,
441  buff, 2);
442 
443 #if HTS221_SHARED_I2C
444  i2cReleaseBus(devp->config->i2cp);
445 #endif /* HTS221_SHARED_I2C */
446 
447  if (msg == MSG_OK) {
448  tmp = buff[0] + (buff[1] << 8);
449  *axes = (int32_t)tmp;
450  }
451  return msg;
452 }
453 
454 /**
455  * @brief Retrieves cooked data from the BaseThermometer.
456  * @note This data is manipulated according to the formula
457  * cooked = (raw * sensitivity) - bias.
458  * @note Final data is expressed as °C.
459  * @note The axes array must be at least the same size of the
460  * BaseThermometer axes number.
461  *
462  * @param[in] ip pointer to @p BaseThermometer interface.
463  * @param[out] axis a buffer which would be filled with cooked data.
464  *
465  * @return The operation status.
466  * @retval MSG_OK if the function succeeded.
467  * @retval MSG_RESET if one or more I2C errors occurred, the errors can
468  * be retrieved using @p i2cGetErrors().
469  * @retval MSG_TIMEOUT if a timeout occurred before operation end.
470  */
471 static msg_t thermo_read_cooked(void *ip, float* axis) {
472  HTS221Driver* devp;
473  int32_t raw;
474  msg_t msg;
475 
476  osalDbgCheck((ip != NULL) && (axis != NULL));
477 
478  /* Getting parent instance pointer.*/
480 
481  osalDbgAssert((devp->state == HTS221_READY),
482  "thermo_read_cooked(), invalid state");
483 
484  msg = thermo_read_raw(devp, &raw);
485 
486  *axis = (raw * devp->thermosensitivity) - devp->thermobias;
487 
488  return msg;
489 }
490 
491 /**
492  * @brief Set bias values for the BaseThermometer.
493  * @note Bias must be expressed as °C.
494  * @note The bias buffer must be at least the same size of the
495  * BaseThermometer axes number.
496  *
497  * @param[in] ip pointer to @p BaseThermometer interface.
498  * @param[in] bp a buffer which contains biases.
499  *
500  * @return The operation status.
501  * @retval MSG_OK if the function succeeded.
502  */
503 static msg_t thermo_set_bias(void *ip, float *bp) {
504  HTS221Driver* devp;
505  msg_t msg = MSG_OK;
506 
507  osalDbgCheck((ip != NULL) && (bp != NULL));
508 
509  /* Getting parent instance pointer.*/
511 
512  osalDbgAssert((devp->state == HTS221_READY),
513  "thermo_set_bias(), invalid state");
514 
515  devp->thermobias = *bp;
516 
517  return msg;
518 }
519 
520 /**
521  * @brief Reset bias values for the BaseThermometer.
522  * @note Default biases value are obtained from device datasheet when
523  * available otherwise they are considered zero.
524  *
525  * @param[in] ip pointer to @p BaseThermometer interface.
526  *
527  * @return The operation status.
528  * @retval MSG_OK if the function succeeded.
529  */
530 static msg_t thermo_reset_bias(void *ip) {
531  HTS221Driver* devp;
532  msg_t msg = MSG_OK;
533 
534  osalDbgCheck(ip != NULL);
535 
536  /* Getting parent instance pointer.*/
538 
539  osalDbgAssert((devp->state == HTS221_READY),
540  "thermo_reset_bias(), invalid state");
541 
542  devp->thermobias = devp->thermofactorybias;
543 
544  return msg;
545 }
546 
547 /**
548  * @brief Set sensitivity values for the BaseThermometer.
549  * @note Sensitivity must be expressed as °C/LSB.
550  * @note The sensitivity buffer must be at least the same size of the
551  * BaseThermometer axes number.
552  *
553  * @param[in] ip pointer to @p BaseThermometer interface.
554  * @param[in] sp a buffer which contains sensitivities.
555  *
556  * @return The operation status.
557  * @retval MSG_OK if the function succeeded.
558  */
559 static msg_t thermo_set_sensitivity(void *ip, float *sp) {
560  HTS221Driver* devp;
561  msg_t msg = MSG_OK;
562 
563  osalDbgCheck((ip != NULL) && (sp != NULL));
564 
565  /* Getting parent instance pointer.*/
567 
568  osalDbgAssert((devp->state == HTS221_READY),
569  "thermo_set_sensitivity(), invalid state");
570 
571  devp->thermosensitivity = *sp;
572 
573  return msg;
574 }
575 
576 /**
577  * @brief Reset sensitivity values for the BaseThermometer.
578  * @note Default sensitivities value are obtained from device datasheet.
579  *
580  * @param[in] ip pointer to @p BaseThermometer interface.
581  *
582  * @return The operation status.
583  * @retval MSG_OK if the function succeeded.
584  */
585 static msg_t thermo_reset_sensitivity(void *ip) {
586  HTS221Driver* devp;
587  msg_t msg = MSG_OK;
588 
589  osalDbgCheck(ip != NULL);
590 
591  /* Getting parent instance pointer.*/
593 
594  osalDbgAssert((devp->state == HTS221_READY),
595  "thermo_reset_sensitivity(), invalid state");
596 
597  devp->thermosensitivity = devp->thermofactorysensitivity;
598 
599  return msg;
600 }
601 
602 static const struct HTS221VMT vmt_device = {
603  (size_t)0
604 };
605 
606 static const struct BaseHygrometerVMT vmt_hygrometer = {
607  sizeof(struct HTS221VMT*),
611 };
612 
613 static const struct BaseThermometerVMT vmt_thermometer = {
614  sizeof(struct HTS221VMT*) + sizeof(BaseHygrometer),
618 };
619 
620 /*===========================================================================*/
621 /* Driver exported functions. */
622 /*===========================================================================*/
623 
624 /**
625  * @brief Initializes an instance.
626  *
627  * @param[out] devp pointer to the @p HTS221Driver object
628  *
629  * @init
630  */
632 
633  devp->vmt = &vmt_device;
634  devp->hygro_if.vmt = &vmt_hygrometer;
635  devp->thermo_if.vmt = &vmt_thermometer;
636 
637  devp->config = NULL;
638 
639  devp->hygroaxes = HTS221_HYGRO_NUMBER_OF_AXES;
640  devp->thermoaxes = HTS221_THERMO_NUMBER_OF_AXES;
641 
642  devp->hygrobias = 0.0f;
643  devp->thermobias = 0.0f;
644 
645  devp->state = HTS221_STOP;
646 }
647 
648 /**
649  * @brief Configures and activates HTS221 Complex Driver peripheral.
650  *
651  * @param[in] devp pointer to the @p HTS221Driver object
652  * @param[in] config pointer to the @p HTS221Config object
653  *
654  * @api
655  */
656 void hts221Start(HTS221Driver *devp, const HTS221Config *config) {
657  uint8_t cr[2];
658  osalDbgCheck((devp != NULL) && (config != NULL));
659 
660  osalDbgAssert((devp->state == HTS221_STOP) || (devp->state == HTS221_READY),
661  "hts221Start(), invalid state");
662 
663  devp->config = config;
664 
665 #if HTS221_SHARED_I2C
666  i2cAcquireBus(devp->config->i2cp);
667 #endif /* HTS221_SHARED_I2C */
668 
669  i2cStart(devp->config->i2cp, devp->config->i2ccfg);
670  hts221Calibrate(devp);
671 
672 #if HTS221_SHARED_I2C
673  i2cReleaseBus(devp->config->i2cp);
674 #endif /* HTS221_SHARED_I2C */
675 
676  if(devp->config->hygrosensitivity == NULL) {
677  devp->hygrosensitivity = devp->hygrofactorysensitivity;
678  }
679  else{
680  /* Taking hygrometer sensitivity from user configurations */
681  devp->hygrosensitivity = *(devp->config->hygrosensitivity);
682  }
683 
684  if(devp->config->hygrobias == NULL) {
685  devp->hygrobias = devp->hygrofactorybias;
686  }
687  else{
688  /* Taking hygrometer bias from user configurations */
689  devp->hygrobias = *(devp->config->hygrobias);
690  }
691 
692  if(devp->config->thermosensitivity == NULL) {
693  devp->thermosensitivity = devp->thermofactorysensitivity;
694  }
695  else{
696  /* Taking thermometer sensitivity from user configurations */
697  devp->thermosensitivity = *(devp->config->thermosensitivity);
698  }
699 
700  if(devp->config->thermobias == NULL) {
701  devp->thermobias = devp->thermofactorybias;
702  }
703  else{
704  /* Taking thermometer bias from user configurations */
705  devp->thermobias = *(devp->config->thermobias);
706  }
707 
708  /* Control register 1 configuration block.*/
709  {
710  cr[0] = HTS221_AD_CTRL_REG1;
711  cr[1] = devp->config->outputdatarate | HTS221_CTRL_REG1_PD;
712 #if HTS221_USE_ADVANCED || defined(__DOXYGEN__)
713  cr[1] |= devp->config->blockdataupdate;
714 #endif
715 
716 #if HTS221_SHARED_I2C
717  i2cAcquireBus(devp->config->i2cp);
718  i2cStart(devp->config->i2cp, devp->config->i2ccfg);
719 #endif /* HTS221_SHARED_I2C */
720 
721  hts221I2CWriteRegister(devp->config->i2cp, cr, 1);
722 
723 #if HTS221_SHARED_I2C
724  i2cReleaseBus(devp->config->i2cp);
725 #endif /* HTS221_SHARED_I2C */
726  }
727 
728  /* Average register configuration block.*/
729  {
730  cr[0] = HTS221_AD_AV_CONF;
731  cr[1] = 0x05;
732 #if HTS221_USE_ADVANCED || defined(__DOXYGEN__)
733  cr[1] = devp->config->hygroresolution | devp->config->thermoresolution;
734 #endif
735 
736 #if HTS221_SHARED_I2C
737  i2cAcquireBus(devp->config->i2cp);
738  i2cStart(devp->config->i2cp, devp->config->i2ccfg);
739 #endif /* HTS221_SHARED_I2C */
740 
741  hts221I2CWriteRegister(devp->config->i2cp, cr, 1);
742 
743 #if HTS221_SHARED_I2C
744  i2cReleaseBus(devp->config->i2cp);
745 #endif /* HTS221_SHARED_I2C */
746  }
747 
748  /* This is the MEMS transient recovery time */
750 
751  devp->state = HTS221_READY;
752 }
753 
754 /**
755  * @brief Deactivates the HTS221 Complex Driver peripheral.
756  *
757  * @param[in] devp pointer to the @p HTS221Driver object
758  *
759  * @api
760  */
762  uint8_t cr[2];
763 
764  osalDbgCheck(devp != NULL);
765 
766  osalDbgAssert((devp->state == HTS221_STOP) || (devp->state == HTS221_READY),
767  "hts221Stop(), invalid state");
768 
769  if (devp->state == HTS221_READY) {
770 
771 #if HTS221_SHARED_I2C
772  i2cAcquireBus(devp->config->i2cp);
773  i2cStart(devp->config->i2cp, devp->config->i2ccfg);
774 #endif /* HTS221_SHARED_I2C */
775 
776  cr[0] = HTS221_AD_CTRL_REG1;
777  cr[1] = 0;
778  hts221I2CWriteRegister(devp->config->i2cp, cr, 1);
779 
780  i2cStop(devp->config->i2cp);
781 #if HTS221_SHARED_I2C
782  i2cReleaseBus(devp->config->i2cp);
783 #endif /* HTS221_SHARED_I2C */
784  }
785  devp->state = HTS221_STOP;
786 }
787 /** @} */
static msg_t thermo_read_raw(void *ip, int32_t axes[])
Retrieves raw data from the BaseThermometer.
Definition: hts221.c:417
static msg_t thermo_set_sensitivity(void *ip, float *sp)
Set sensitivity values for the BaseThermometer.
Definition: hts221.c:559
msg_t i2cMasterTransmitTimeout(I2CDriver *i2cp, i2caddr_t addr, const uint8_t *txbuf, size_t txbytes, uint8_t *rxbuf, size_t rxbytes, sysinterval_t timeout)
Sends data via the I2C bus.
Definition: hal_i2c.c:170
static msg_t thermo_read_cooked(void *ip, float *axis)
Retrieves cooked data from the BaseThermometer.
Definition: hts221.c:471
void i2cStart(I2CDriver *i2cp, const I2CConfig *config)
Configures and activates the I2C peripheral.
Definition: hal_i2c.c:93
static msg_t hygro_read_cooked(void *ip, float axes[])
Retrieves cooked data from the BaseHygrometer.
Definition: hts221.c:258
Base thermometer class.
const struct HTS221VMT * vmt
Virtual Methods Table.
Definition: hts221.h:442
HAL subsystem header.
void i2cAcquireBus(I2CDriver *i2cp)
Gains exclusive access to the I2C bus.
Definition: hal_i2c.c:261
static msg_t thermo_reset_bias(void *ip)
Reset bias values for the BaseThermometer.
Definition: hts221.c:530
BaseHygrometer hygro_if
Base hygrometer interface.
Definition: hts221.h:444
void i2cReleaseBus(I2CDriver *i2cp)
Releases exclusive access to the I2C bus.
Definition: hal_i2c.c:277
static msg_t hygro_read_raw(void *ip, int32_t axes[])
Retrieves raw data from the BaseHygrometer.
Definition: hts221.c:204
BaseThermometer virtual methods table.
void hts221ObjectInit(HTS221Driver *devp)
Initializes an instance.
Definition: hts221.c:631
HTS221 2-axis hygrometer/thermometer class.
Definition: hts221.h:440
static msg_t thermo_reset_sensitivity(void *ip)
Reset sensitivity values for the BaseThermometer.
Definition: hts221.c:585
static msg_t hygro_set_bias(void *ip, float *bp)
Set bias values for the BaseHygrometer.
Definition: hts221.c:293
BaseThermometer thermo_if
Base thermometer interface.
Definition: hts221.h:446
#define objGetInstance(type, ip)
Returns the instance pointer starting from an interface pointer.
Definition: hal_objects.h:80
#define HTS221_HYGRO_NUMBER_OF_AXES
HTS221 hygrometer subsystem characteristics.
Definition: hts221.h:72
static msg_t hts221Calibrate(HTS221Driver *devp)
Computes biases and sensitivities starting from data stored in calibration registers.
Definition: hts221.c:112
HTS221 virtual methods table.
Definition: hts221.h:404
#define osalThreadSleepMilliseconds(msecs)
Delays the invoking thread for the specified number of milliseconds.
Definition: osal.h:451
static msg_t thermo_set_bias(void *ip, float *bp)
Set bias values for the BaseThermometer.
Definition: hts221.c:503
static msg_t hygro_set_sensitivity(void *ip, float *sp)
Set sensitivity values for the BaseHygrometer.
Definition: hts221.c:347
void i2cStop(I2CDriver *i2cp)
Deactivates the I2C peripheral.
Definition: hal_i2c.c:113
Base hygrometer class.
Structure representing an I2C driver.
Definition: hal_i2c_lld.h:88
static msg_t hygro_reset_bias(void *ip)
Reset bias values for the BaseHygrometer.
Definition: hts221.c:319
#define TIME_INFINITE
Infinite interval specification for all functions with a timeout specification.
Definition: chtime.h:55
#define osalDbgCheck(c)
Function parameters check.
Definition: osal.h:278
static msg_t hygro_reset_sensitivity(void *ip)
Reset sensitivity values for the BaseHygrometer.
Definition: hts221.c:372
HTS221 MEMS interface module header.
#define MSG_OK
Normal wakeup message.
Definition: chschd.h:39
static size_t hygro_get_axes_number(void *ip)
Return the number of axes of the BaseHygrometer.
Definition: hts221.c:182
static size_t thermo_get_axes_number(void *ip)
Return the number of axes of the BaseThermometer.
Definition: hts221.c:395
void hts221Stop(HTS221Driver *devp)
Deactivates the HTS221 Complex Driver peripheral.
Definition: hts221.c:761
static msg_t hts221I2CReadRegister(I2CDriver *i2cp, uint8_t reg, uint8_t *rxbuf, size_t n)
Reads registers value using I2C.
Definition: hts221.c:69
static msg_t hts221I2CWriteRegister(I2CDriver *i2cp, uint8_t *txbuf, size_t n)
Writes a value into a register using I2C.
Definition: hts221.c:92
#define osalDbgAssert(c, remark)
Condition assertion.
Definition: osal.h:258
void hts221Start(HTS221Driver *devp, const HTS221Config *config)
Configures and activates HTS221 Complex Driver peripheral.
Definition: hts221.c:656
#define HTS221_THERMO_NUMBER_OF_AXES
HTS221 thermometer subsystem characteristics.
Definition: hts221.h:85
const struct BaseHygrometerVMT * vmt
Virtual Methods Table.
BaseHygrometer virtual methods table.
const struct BaseThermometerVMT * vmt
Virtual Methods Table.
int32_t msg_t
Definition: chtypes.h:51
HTS221 configuration structure.
Definition: hts221.h:328