ChibiOS  19.1.4
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  /* Retrieving rH values from Calibration registers */
118  msg = hts221I2CReadRegister(devp->config->i2cp,
119  HTS221_AD_CALIB_0, calib, 16);
120 
121  H0_rH_x2 = calib[0];
122  H1_rH_x2 = calib[1];
123  H0_T0_OUT = calib[6];
124  H0_T0_OUT += calib[7] << 8;
125  H1_T0_OUT = calib[10];
126  H1_T0_OUT += calib[11] << 8;
127 
128  T0_degC_x8 = calib[2];
129 
130  /* Completing T0_degC_x8 value */
131  msb = (calib[5] & HTS221_SEL(0x03, 0));
132  if (msb & HTS221_SEL(0x01, 1)) {
133  msb |= HTS221_SEL(0x3F, 2);
134  }
135  T0_degC_x8 += msb << 8;
136 
137  T1_degC_x8 = calib[3];
138  /* Completing T1_degC_x8 value */
139  msb = ((calib[5] & HTS221_SEL(0x03, 2)) >> 2);
140  if (msb & HTS221_SEL(0x01, 1)) {
141  msb |= HTS221_SEL(0x3F, 2);
142  }
143  T1_degC_x8 += msb << 8;
144 
145  T0_OUT = calib[12];
146  T0_OUT += calib[13] << 8;
147  T1_OUT = calib[14];
148  T1_OUT += calib[15] << 8;
149 
150  devp->hygrofactorysensitivity = ((float)H1_rH_x2 - (float)H0_rH_x2) /
151  (((float)H1_T0_OUT - (float)H0_T0_OUT) * 2.0f);
152 
153 
154  devp->hygrofactorybias = (devp->hygrofactorysensitivity * (float)H0_T0_OUT) -
155  ((float)H0_rH_x2 / 2.0f);
156 
157  devp->thermofactorysensitivity = ((float)T1_degC_x8 - (float)T0_degC_x8) /
158  (((float)T1_OUT - (float)T0_OUT) * 8.0f);
159 
160  devp->thermofactorybias = (devp->thermofactorysensitivity * (float)T0_OUT) -
161  ((float)T0_degC_x8 / 8.0f);
162 
163  return msg;
164 }
165 
166 /**
167  * @brief Return the number of axes of the BaseHygrometer.
168  *
169  * @param[in] ip pointer to @p BaseHygrometer interface.
170  *
171  * @return the number of axes.
172  */
173 static size_t hygro_get_axes_number(void *ip) {
174  (void)ip;
175 
177 }
178 
179 /**
180  * @brief Retrieves raw data from the BaseHygrometer.
181  * @note This data is retrieved from MEMS register without any algebraical
182  * manipulation.
183  * @note The axes array must be at least the same size of the
184  * BaseHygrometer axes number.
185  *
186  * @param[in] ip pointer to @p BaseHygrometer interface.
187  * @param[out] axes a buffer which would be filled with raw data.
188  *
189  * @return The operation status.
190  * @retval MSG_OK if the function succeeded.
191  * @retval MSG_RESET if one or more I2C errors occurred, the errors can
192  * be retrieved using @p i2cGetErrors().
193  * @retval MSG_TIMEOUT if a timeout occurred before operation end.
194  */
195 static msg_t hygro_read_raw(void *ip, int32_t axes[]) {
196  HTS221Driver* devp;
197  uint8_t buff[2];
198  int16_t tmp;
199  msg_t msg;
200 
201  osalDbgCheck((ip != NULL) && (axes != NULL));
202 
203  /* Getting parent instance pointer.*/
205 
206  osalDbgAssert((devp->state == HTS221_READY),
207  "hygro_read_raw(), invalid state");
208 
209  osalDbgAssert((devp->config->i2cp->state == I2C_READY),
210  "hygro_read_raw(), channel not ready");
211 
212 #if HTS221_SHARED_I2C
213  i2cAcquireBus(devp->config->i2cp);
214  i2cStart(devp->config->i2cp,
215  devp->config->i2ccfg);
216 #endif /* HTS221_SHARED_I2C */
217 
218  msg = hts221I2CReadRegister(devp->config->i2cp, HTS221_AD_HUMIDITY_OUT_L,
219  buff, 2);
220 
221 #if HTS221_SHARED_I2C
222  i2cReleaseBus(devp->config->i2cp);
223 #endif /* HTS221_SHARED_I2C */
224 
225  if (msg == MSG_OK) {
226  tmp = buff[0] + (buff[1] << 8);
227  *axes = (int32_t)tmp;
228  }
229  return msg;
230 }
231 
232 /**
233  * @brief Retrieves cooked data from the BaseHygrometer.
234  * @note This data is manipulated according to the formula
235  * cooked = (raw * sensitivity) - bias.
236  * @note Final data is expressed as %rH.
237  * @note The axes array must be at least the same size of the
238  * BaseHygrometer axes number.
239  *
240  * @param[in] ip pointer to @p BaseHygrometer interface.
241  * @param[out] axes a buffer which would be filled with cooked data.
242  *
243  * @return The operation status.
244  * @retval MSG_OK if the function succeeded.
245  * @retval MSG_RESET if one or more I2C errors occurred, the errors can
246  * be retrieved using @p i2cGetErrors().
247  * @retval MSG_TIMEOUT if a timeout occurred before operation end.
248  */
249 static msg_t hygro_read_cooked(void *ip, float axes[]) {
250  HTS221Driver* devp;
251  int32_t raw;
252  msg_t msg;
253 
254  osalDbgCheck((ip != NULL) && (axes != NULL));
255 
256  /* Getting parent instance pointer.*/
258 
259  osalDbgAssert((devp->state == HTS221_READY),
260  "hygro_read_cooked(), invalid state");
261 
262  msg = hygro_read_raw(ip, &raw);
263 
264  *axes = (raw * devp->hygrosensitivity) - devp->hygrobias;
265 
266  return msg;
267 }
268 
269 /**
270  * @brief Set bias values for the BaseHygrometer.
271  * @note Bias must be expressed as %rH.
272  * @note The bias buffer must be at least the same size of the
273  * BaseHygrometer axes number.
274  *
275  * @param[in] ip pointer to @p BaseHygrometer interface.
276  * @param[in] bp a buffer which contains biases.
277  *
278  * @return The operation status.
279  * @retval MSG_OK if the function succeeded.
280  * @retval MSG_RESET if one or more I2C errors occurred, the errors can
281  * be retrieved using @p i2cGetErrors().
282  * @retval MSG_TIMEOUT if a timeout occurred before operation end.
283  */
284 static msg_t hygro_set_bias(void *ip, float *bp) {
285  HTS221Driver* devp;
286  msg_t msg = MSG_OK;
287 
288  osalDbgCheck((ip != NULL) && (bp != NULL));
289 
290  /* Getting parent instance pointer.*/
292 
293  osalDbgAssert((devp->state == HTS221_READY),
294  "hygro_set_bias(), invalid state");
295 
296  devp->hygrobias = *bp;
297  return msg;
298 }
299 
300 /**
301  * @brief Reset bias values for the BaseHygrometer.
302  * @note Default biases value are obtained from device datasheet when
303  * available otherwise they are considered zero.
304  *
305  * @param[in] ip pointer to @p BaseHygrometer interface.
306  *
307  * @return The operation status.
308  * @retval MSG_OK if the function succeeded.
309  */
310 static msg_t hygro_reset_bias(void *ip) {
311  HTS221Driver* devp;
312  msg_t msg = MSG_OK;
313 
314  osalDbgCheck(ip != NULL);
315 
316  /* Getting parent instance pointer.*/
318 
319  osalDbgAssert((devp->state == HTS221_READY),
320  "hygro_reset_bias(), invalid state");
321 
322  devp->hygrobias = devp->hygrofactorybias;
323  return msg;
324 }
325 
326 /**
327  * @brief Set sensitivity values for the BaseHygrometer.
328  * @note Sensitivity must be expressed as %rH/LSB.
329  * @note The sensitivity buffer must be at least the same size of the
330  * BaseHygrometer axes number.
331  *
332  * @param[in] ip pointer to @p BaseHygrometer interface.
333  * @param[in] sp a buffer which contains sensitivities.
334  *
335  * @return The operation status.
336  * @retval MSG_OK if the function succeeded.
337  */
338 static msg_t hygro_set_sensitivity(void *ip, float *sp) {
339  HTS221Driver* devp;
340  msg_t msg = MSG_OK;
341 
342  osalDbgCheck((ip != NULL) && (sp != NULL));
343 
344  /* Getting parent instance pointer.*/
346 
347  osalDbgAssert((devp->state == HTS221_READY),
348  "hygro_set_sensitivity(), invalid state");
349 
350  devp->hygrosensitivity = *sp;
351  return msg;
352 }
353 
354 /**
355  * @brief Reset sensitivity values for the BaseHygrometer.
356  * @note Default sensitivities value are obtained from device datasheet.
357  *
358  * @param[in] ip pointer to @p BaseHygrometer interface.
359  *
360  * @return The operation status.
361  * @retval MSG_OK if the function succeeded.
362  */
363 static msg_t hygro_reset_sensitivity(void *ip) {
364  HTS221Driver* devp;
365  msg_t msg = MSG_OK;
366 
367  osalDbgCheck(ip != NULL);
368 
369  /* Getting parent instance pointer.*/
371 
372  osalDbgAssert((devp->state == HTS221_READY),
373  "hygro_reset_sensitivity(), invalid state");
374 
375  devp->hygrosensitivity = devp->hygrofactorysensitivity;
376  return msg;
377 }
378 
379 /**
380  * @brief Return the number of axes of the BaseThermometer.
381  *
382  * @param[in] ip pointer to @p BaseThermometer interface.
383  *
384  * @return the number of axes.
385  */
386 static size_t thermo_get_axes_number(void *ip) {
387  (void)ip;
388 
390 }
391 
392 /**
393  * @brief Retrieves raw data from the BaseThermometer.
394  * @note This data is retrieved from MEMS register without any algebraical
395  * manipulation.
396  * @note The axes array must be at least the same size of the
397  * BaseThermometer axes number.
398  *
399  * @param[in] ip pointer to @p BaseThermometer interface.
400  * @param[out] axes a buffer which would be filled with raw data.
401  *
402  * @return The operation status.
403  * @retval MSG_OK if the function succeeded.
404  * @retval MSG_RESET if one or more I2C errors occurred, the errors can
405  * be retrieved using @p i2cGetErrors().
406  * @retval MSG_TIMEOUT if a timeout occurred before operation end.
407  */
408 static msg_t thermo_read_raw(void *ip, int32_t axes[]) {
409  HTS221Driver* devp;
410  int16_t tmp;
411  uint8_t buff[2];
412  msg_t msg;
413 
414  osalDbgCheck((ip != NULL) && (axes != NULL));
415 
416  /* Getting parent instance pointer.*/
418 
419  osalDbgAssert((devp->state == HTS221_READY),
420  "thermo_read_raw(), invalid state");
421 
422  osalDbgAssert((devp->config->i2cp->state == I2C_READY),
423  "thermo_read_raw(), channel not ready");
424 
425 #if HTS221_SHARED_I2C
426  i2cAcquireBus(devp->config->i2cp);
427  i2cStart(devp->config->i2cp,
428  devp->config->i2ccfg);
429 #endif /* HTS221_SHARED_I2C */
430 
431  msg = hts221I2CReadRegister(devp->config->i2cp, HTS221_AD_TEMP_OUT_L,
432  buff, 2);
433 
434 #if HTS221_SHARED_I2C
435  i2cReleaseBus(devp->config->i2cp);
436 #endif /* HTS221_SHARED_I2C */
437 
438  if (msg == MSG_OK) {
439  tmp = buff[0] + (buff[1] << 8);
440  *axes = (int32_t)tmp;
441  }
442  return msg;
443 }
444 
445 /**
446  * @brief Retrieves cooked data from the BaseThermometer.
447  * @note This data is manipulated according to the formula
448  * cooked = (raw * sensitivity) - bias.
449  * @note Final data is expressed as °C.
450  * @note The axes array must be at least the same size of the
451  * BaseThermometer axes number.
452  *
453  * @param[in] ip pointer to @p BaseThermometer interface.
454  * @param[out] axis a buffer which would be filled with cooked data.
455  *
456  * @return The operation status.
457  * @retval MSG_OK if the function succeeded.
458  * @retval MSG_RESET if one or more I2C errors occurred, the errors can
459  * be retrieved using @p i2cGetErrors().
460  * @retval MSG_TIMEOUT if a timeout occurred before operation end.
461  */
462 static msg_t thermo_read_cooked(void *ip, float* axis) {
463  HTS221Driver* devp;
464  int32_t raw;
465  msg_t msg;
466 
467  osalDbgCheck((ip != NULL) && (axis != NULL));
468 
469  /* Getting parent instance pointer.*/
471 
472  osalDbgAssert((devp->state == HTS221_READY),
473  "thermo_read_cooked(), invalid state");
474 
475  msg = thermo_read_raw(devp, &raw);
476 
477  *axis = (raw * devp->thermosensitivity) - devp->thermobias;
478 
479  return msg;
480 }
481 
482 /**
483  * @brief Set bias values for the BaseThermometer.
484  * @note Bias must be expressed as °C.
485  * @note The bias buffer must be at least the same size of the
486  * BaseThermometer axes number.
487  *
488  * @param[in] ip pointer to @p BaseThermometer interface.
489  * @param[in] bp a buffer which contains biases.
490  *
491  * @return The operation status.
492  * @retval MSG_OK if the function succeeded.
493  */
494 static msg_t thermo_set_bias(void *ip, float *bp) {
495  HTS221Driver* devp;
496  msg_t msg = MSG_OK;
497 
498  osalDbgCheck((ip != NULL) && (bp != NULL));
499 
500  /* Getting parent instance pointer.*/
502 
503  osalDbgAssert((devp->state == HTS221_READY),
504  "thermo_set_bias(), invalid state");
505 
506  devp->thermobias = *bp;
507 
508  return msg;
509 }
510 
511 /**
512  * @brief Reset bias values for the BaseThermometer.
513  * @note Default biases value are obtained from device datasheet when
514  * available otherwise they are considered zero.
515  *
516  * @param[in] ip pointer to @p BaseThermometer interface.
517  *
518  * @return The operation status.
519  * @retval MSG_OK if the function succeeded.
520  */
521 static msg_t thermo_reset_bias(void *ip) {
522  HTS221Driver* devp;
523  msg_t msg = MSG_OK;
524 
525  osalDbgCheck(ip != NULL);
526 
527  /* Getting parent instance pointer.*/
529 
530  osalDbgAssert((devp->state == HTS221_READY),
531  "thermo_reset_bias(), invalid state");
532 
533  devp->thermobias = devp->thermofactorybias;
534 
535  return msg;
536 }
537 
538 /**
539  * @brief Set sensitivity values for the BaseThermometer.
540  * @note Sensitivity must be expressed as °C/LSB.
541  * @note The sensitivity buffer must be at least the same size of the
542  * BaseThermometer axes number.
543  *
544  * @param[in] ip pointer to @p BaseThermometer interface.
545  * @param[in] sp a buffer which contains sensitivities.
546  *
547  * @return The operation status.
548  * @retval MSG_OK if the function succeeded.
549  */
550 static msg_t thermo_set_sensitivity(void *ip, float *sp) {
551  HTS221Driver* devp;
552  msg_t msg = MSG_OK;
553 
554  osalDbgCheck((ip != NULL) && (sp != NULL));
555 
556  /* Getting parent instance pointer.*/
558 
559  osalDbgAssert((devp->state == HTS221_READY),
560  "thermo_set_sensitivity(), invalid state");
561 
562  devp->thermosensitivity = *sp;
563 
564  return msg;
565 }
566 
567 /**
568  * @brief Reset sensitivity values for the BaseThermometer.
569  * @note Default sensitivities value are obtained from device datasheet.
570  *
571  * @param[in] ip pointer to @p BaseThermometer interface.
572  *
573  * @return The operation status.
574  * @retval MSG_OK if the function succeeded.
575  */
576 static msg_t thermo_reset_sensitivity(void *ip) {
577  HTS221Driver* devp;
578  msg_t msg = MSG_OK;
579 
580  osalDbgCheck(ip != NULL);
581 
582  /* Getting parent instance pointer.*/
584 
585  osalDbgAssert((devp->state == HTS221_READY),
586  "thermo_reset_sensitivity(), invalid state");
587 
588  devp->thermosensitivity = devp->thermofactorysensitivity;
589 
590  return msg;
591 }
592 
593 static const struct HTS221VMT vmt_device = {
594  (size_t)0
595 };
596 
597 static const struct BaseHygrometerVMT vmt_hygrometer = {
598  sizeof(struct HTS221VMT*),
602 };
603 
604 static const struct BaseThermometerVMT vmt_thermometer = {
605  sizeof(struct HTS221VMT*) + sizeof(BaseHygrometer),
609 };
610 
611 /*===========================================================================*/
612 /* Driver exported functions. */
613 /*===========================================================================*/
614 
615 /**
616  * @brief Initializes an instance.
617  *
618  * @param[out] devp pointer to the @p HTS221Driver object
619  *
620  * @init
621  */
623 
624  devp->vmt = &vmt_device;
625  devp->hygro_if.vmt = &vmt_hygrometer;
626  devp->thermo_if.vmt = &vmt_thermometer;
627 
628  devp->config = NULL;
629 
630  devp->hygroaxes = HTS221_HYGRO_NUMBER_OF_AXES;
631  devp->thermoaxes = HTS221_THERMO_NUMBER_OF_AXES;
632 
633  devp->hygrobias = 0.0f;
634  devp->thermobias = 0.0f;
635 
636  devp->state = HTS221_STOP;
637 }
638 
639 /**
640  * @brief Configures and activates HTS221 Complex Driver peripheral.
641  *
642  * @param[in] devp pointer to the @p HTS221Driver object
643  * @param[in] config pointer to the @p HTS221Config object
644  *
645  * @api
646  */
647 void hts221Start(HTS221Driver *devp, const HTS221Config *config) {
648  uint8_t cr[2];
649  osalDbgCheck((devp != NULL) && (config != NULL));
650 
651  osalDbgAssert((devp->state == HTS221_STOP) || (devp->state == HTS221_READY),
652  "hts221Start(), invalid state");
653 
654  devp->config = config;
655 
656 #if HTS221_SHARED_I2C
657  i2cAcquireBus(devp->config->i2cp);
658 #endif /* HTS221_SHARED_I2C */
659 
660  /* Intializing the I2C. */
661  i2cStart(devp->config->i2cp, devp->config->i2ccfg);
662 
663  hts221Calibrate(devp);
664 
665 #if HTS221_SHARED_I2C
666  i2cReleaseBus(devp->config->i2cp);
667 #endif /* HTS221_SHARED_I2C */
668 
669 
670  if(devp->config->hygrosensitivity == NULL) {
671  devp->hygrosensitivity = devp->hygrofactorysensitivity;
672  }
673  else{
674  /* Taking hygrometer sensitivity from user configurations */
675  devp->hygrosensitivity = *(devp->config->hygrosensitivity);
676  }
677 
678  if(devp->config->hygrobias == NULL) {
679  devp->hygrobias = devp->hygrofactorybias;
680  }
681  else{
682  /* Taking hygrometer bias from user configurations */
683  devp->hygrobias = *(devp->config->hygrobias);
684  }
685 
686  if(devp->config->thermosensitivity == NULL) {
687  devp->thermosensitivity = devp->thermofactorysensitivity;
688  }
689  else{
690  /* Taking thermometer sensitivity from user configurations */
691  devp->thermosensitivity = *(devp->config->thermosensitivity);
692  }
693 
694  if(devp->config->thermobias == NULL) {
695  devp->thermobias = devp->thermofactorybias;
696  }
697  else{
698  /* Taking thermometer bias from user configurations */
699  devp->thermobias = *(devp->config->thermobias);
700  }
701 
702  /* Control register 1 configuration block.*/
703  {
704  cr[0] = HTS221_AD_CTRL_REG1;
705  cr[1] = devp->config->outputdatarate | HTS221_CTRL_REG1_PD;
706 #if HTS221_USE_ADVANCED || defined(__DOXYGEN__)
707  cr[1] |= devp->config->blockdataupdate;
708 #endif
709 
710 #if HTS221_SHARED_I2C
711  i2cAcquireBus(devp->config->i2cp);
712  i2cStart(devp->config->i2cp, devp->config->i2ccfg);
713 #endif /* HTS221_SHARED_I2C */
714 
715  hts221I2CWriteRegister(devp->config->i2cp, cr, 1);
716 
717 #if HTS221_SHARED_I2C
718  i2cReleaseBus(devp->config->i2cp);
719 #endif /* HTS221_SHARED_I2C */
720  }
721 
722  /* Average register configuration block.*/
723  {
724  cr[0] = HTS221_AD_AV_CONF;
725  cr[1] = 0x05;
726 #if HTS221_USE_ADVANCED || defined(__DOXYGEN__)
727  cr[1] = devp->config->hygroresolution | devp->config->thermoresolution;
728 #endif
729 
730 #if HTS221_SHARED_I2C
731  i2cAcquireBus(devp->config->i2cp);
732  i2cStart(devp->config->i2cp, devp->config->i2ccfg);
733 #endif /* HTS221_SHARED_I2C */
734 
735  hts221I2CWriteRegister(devp->config->i2cp, cr, 1);
736 
737 #if HTS221_SHARED_I2C
738  i2cReleaseBus(devp->config->i2cp);
739 #endif /* HTS221_SHARED_I2C */
740  }
741 
742  /* This is the MEMS transient recovery time */
744 
745  devp->state = HTS221_READY;
746 }
747 
748 /**
749  * @brief Deactivates the HTS221 Complex Driver peripheral.
750  *
751  * @param[in] devp pointer to the @p HTS221Driver object
752  *
753  * @api
754  */
756  uint8_t cr[2];
757 
758  osalDbgCheck(devp != NULL);
759 
760  osalDbgAssert((devp->state == HTS221_STOP) || (devp->state == HTS221_READY),
761  "hts221Stop(), invalid state");
762 
763  if (devp->state == HTS221_READY) {
764 
765 #if HTS221_SHARED_I2C
766  i2cAcquireBus(devp->config->i2cp);
767  i2cStart(devp->config->i2cp, devp->config->i2ccfg);
768 #endif /* HTS221_SHARED_I2C */
769 
770  cr[0] = HTS221_AD_CTRL_REG1;
771  cr[1] = 0;
772  hts221I2CWriteRegister(devp->config->i2cp, cr, 1);
773 
774  i2cStop(devp->config->i2cp);
775 #if HTS221_SHARED_I2C
776  i2cReleaseBus(devp->config->i2cp);
777 #endif /* HTS221_SHARED_I2C */
778  }
779  devp->state = HTS221_STOP;
780 }
781 /** @} */
static msg_t thermo_read_raw(void *ip, int32_t axes[])
Retrieves raw data from the BaseThermometer.
Definition: hts221.c:408
static msg_t thermo_set_sensitivity(void *ip, float *sp)
Set sensitivity values for the BaseThermometer.
Definition: hts221.c:550
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:462
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:249
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:521
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:195
BaseThermometer virtual methods table.
void hts221ObjectInit(HTS221Driver *devp)
Initializes an instance.
Definition: hts221.c:622
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:576
static msg_t hygro_set_bias(void *ip, float *bp)
Set bias values for the BaseHygrometer.
Definition: hts221.c:284
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:494
static msg_t hygro_set_sensitivity(void *ip, float *sp)
Set sensitivity values for the BaseHygrometer.
Definition: hts221.c:338
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:310
#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:363
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:173
static size_t thermo_get_axes_number(void *ip)
Return the number of axes of the BaseThermometer.
Definition: hts221.c:386
void hts221Stop(HTS221Driver *devp)
Deactivates the HTS221 Complex Driver peripheral.
Definition: hts221.c:755
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:647
#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