1 /*
2  * Copyright (c) 2023 Wuerth Elektronik eiSos GmbH & Co. KG
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief Driver file for the WSEN-HIDS-2525020210001 sensor.
10  */
11 
12 #include "WSEN_HIDS_2525020210001_hal.h"
13 
14 #include <stdio.h>
15 
16 #include <weplatform.h>
17 
18 /**
19  * @brief Default sensor interface configuration.
20  */
21 static const WE_sensorInterface_t hidsDefaultSensorInterface = {
22     .sensorType = WE_HIDS,
23     .interfaceType = WE_i2c,
24     .options = {.i2c = {.address = HIDS_ADDRESS_I2C_0, .burstMode = 1, .protocol = WE_i2cProtocol_RegisterBased, .useRegAddrMsbForMultiBytesRead = 1, .reserved = 0},
25                 .spi = {.chipSelectPort = 0, .chipSelectPin = 0, .burstMode = 0, .reserved = 0},
26                 .readTimeout = 1000,
27                 .writeTimeout = 1000},
28     .handle = 0};
29 
30 /**
31  * @brief HIDS calibration data.
32  */
33 typedef struct
34 {
35   /* Is set to true when the sensor's calibration data has been read */
36   uint8_t calibrationPresent;
37 
38   /* Humidity linear interpolation point H0 */
39   uint8_t H0_rh;      /* H0 RH calibration data*/
40   int16_t H0_T0_out;  /* H0_T0 calibration data */
41 
42   /* Humidity linear interpolation point H1 */
43   uint8_t H1_rh;      /* H1 RH calibration data */
44   int16_t H1_T0_out;  /* H1_T0 calibration data */
45 
46   /* Temperature linear interpolation point T0 */
47   uint16_t T0_degC;
48   int16_t T0_out;
49 
50   /* Temperature linear interpolation point T1 */
51   uint16_t T1_degC;
52   int16_t T1_out;
53 } HIDS_calibrationData_t;
54 
55 /**
56  * @brief Stores HIDS calibration data. Can be read from sensor using HIDS_readCalibrationData().
57  */
58 static HIDS_calibrationData_t hidsCalibrationData = {0};
59 
60 /* Get the calibration parameters for humidity and store the data in hidsCalibrationData */
61 static int8_t HIDS_get_H0_T0_out(WE_sensorInterface_t* sensorInterface);
62 static int8_t HIDS_get_H1_T0_out(WE_sensorInterface_t* sensorInterface);
63 static int8_t HIDS_get_H0_rh(WE_sensorInterface_t* sensorInterface);
64 static int8_t HIDS_get_H1_rh(WE_sensorInterface_t* sensorInterface);
65 
66 /* Get the calibration parameters for temperature and store the data in hidsCalibrationData */
67 static int8_t HIDS_get_T1_OUT(WE_sensorInterface_t* sensorInterface);
68 static int8_t HIDS_get_T0_OUT(WE_sensorInterface_t* sensorInterface);
69 static int8_t HIDS_get_T0_degC(WE_sensorInterface_t* sensorInterface);
70 static int8_t HIDS_get_T1_degC(WE_sensorInterface_t* sensorInterface);
71 
72 
73 /**
74  * @brief Read data from sensor.
75  *
76  * @param[in] sensorInterface Pointer to sensor interface
77  * @param[in] regAdr Address of register to read from
78  * @param[in] numBytesToRead Number of bytes to be read
79  * @param[out] data Target buffer
80  * @return Error Code
81  */
HIDS_ReadReg(WE_sensorInterface_t * sensorInterface,uint8_t regAdr,uint16_t numBytesToRead,uint8_t * data)82 static inline int8_t HIDS_ReadReg(WE_sensorInterface_t* sensorInterface,
83                                   uint8_t regAdr,
84                                   uint16_t numBytesToRead,
85                                   uint8_t *data)
86 {
87   return WE_ReadReg(sensorInterface, regAdr, numBytesToRead, data);
88 }
89 
90 /**
91  * @brief Write data to sensor.
92  *
93  * @param[in] sensorInterface Pointer to sensor interface
94  * @param[in] regAdr Address of register to write to
95  * @param[in] numBytesToWrite Number of bytes to be written
96  * @param[in] data Source buffer
97  * @return Error Code
98  */
HIDS_WriteReg(WE_sensorInterface_t * sensorInterface,uint8_t regAdr,uint16_t numBytesToWrite,uint8_t * data)99 static inline int8_t HIDS_WriteReg(WE_sensorInterface_t* sensorInterface,
100                                    uint8_t regAdr,
101                                    uint16_t numBytesToWrite,
102                                    uint8_t *data)
103 {
104   return WE_WriteReg(sensorInterface, regAdr, numBytesToWrite, data);
105 }
106 
107 /**
108  * @brief Returns the default sensor interface configuration.
109  * @param[out] sensorInterface Sensor interface configuration (output parameter)
110  * @return Error code
111  */
HIDS_getDefaultInterface(WE_sensorInterface_t * sensorInterface)112 int8_t HIDS_getDefaultInterface(WE_sensorInterface_t* sensorInterface)
113 {
114   *sensorInterface = hidsDefaultSensorInterface;
115   return WE_SUCCESS;
116 }
117 
118 /**
119  * @brief Read the device ID
120  *
121  * The expected value is HIDS_DEVICE_ID_VALUE.
122  *
123  * @param[in] sensorInterface Pointer to sensor interface
124  * @param[out] deviceID The returned device ID.
125  * @return Error code
126  */
HIDS_getDeviceID(WE_sensorInterface_t * sensorInterface,uint8_t * deviceID)127 int8_t HIDS_getDeviceID(WE_sensorInterface_t* sensorInterface, uint8_t *deviceID)
128 {
129   return HIDS_ReadReg(sensorInterface, HIDS_DEVICE_ID_REG, 1, deviceID);
130 }
131 
132 /**
133  * @brief Set the humidity average configuration
134  * @param[in] sensorInterface Pointer to sensor interface
135  * @param[in] avgHum Humidity average parameter
136  * @return Error code
137  */
HIDS_setHumidityAverageConfig(WE_sensorInterface_t * sensorInterface,HIDS_humidityAverageConfig_t avgHum)138 uint8_t HIDS_setHumidityAverageConfig(WE_sensorInterface_t* sensorInterface, HIDS_humidityAverageConfig_t avgHum)
139 {
140   HIDS_averageConfig_t averageReg;
141 
142   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_AVERAGE_REG, 1, (uint8_t *) &averageReg))
143   {
144     return WE_FAIL;
145   }
146 
147   averageReg.avgHum = avgHum;
148 
149   return HIDS_WriteReg(sensorInterface, HIDS_AVERAGE_REG, 1, (uint8_t *) &averageReg);
150 }
151 
152 /**
153  * @brief Read the humidity average configuration
154  * @param[in] sensorInterface Pointer to sensor interface
155  * @param[out] avgHum The returned humidity average configuration
156  * @return Error code
157  */
HIDS_getHumidityAverageConfig(WE_sensorInterface_t * sensorInterface,HIDS_humidityAverageConfig_t * avgHum)158 uint8_t HIDS_getHumidityAverageConfig(WE_sensorInterface_t* sensorInterface, HIDS_humidityAverageConfig_t *avgHum)
159 {
160   HIDS_averageConfig_t averageReg;
161 
162   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_AVERAGE_REG, 1, (uint8_t *) &averageReg))
163   {
164     return WE_FAIL;
165   }
166 
167   *avgHum = (HIDS_humidityAverageConfig_t) averageReg.avgHum;
168 
169   return WE_SUCCESS;
170 }
171 
172 /**
173  * @brief Set the temperature average configuration
174  * @param[in] sensorInterface Pointer to sensor interface
175  * @param[in] avgTemp Temperature average parameter
176  * @return Error code
177  */
HIDS_setTemperatureAverageConfig(WE_sensorInterface_t * sensorInterface,HIDS_temperatureAverageConfig_t avgTemp)178 uint8_t HIDS_setTemperatureAverageConfig(WE_sensorInterface_t* sensorInterface, HIDS_temperatureAverageConfig_t avgTemp)
179 {
180   HIDS_averageConfig_t averageReg;
181 
182   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_AVERAGE_REG, 1, (uint8_t *) &averageReg))
183   {
184     return WE_FAIL;
185   }
186 
187   averageReg.avgTemp = avgTemp;
188 
189   return HIDS_WriteReg(sensorInterface, HIDS_AVERAGE_REG, 1, (uint8_t *) &averageReg);
190 }
191 
192 /**
193  * @brief Read the temperature average configuration
194  * @param[in] sensorInterface Pointer to sensor interface
195  * @param[out] avgTemp The returned temperature average configuration
196  * @return Error code
197  */
HIDS_getTemperatureAverageConfig(WE_sensorInterface_t * sensorInterface,HIDS_temperatureAverageConfig_t * avgTemp)198 uint8_t HIDS_getTemperatureAverageConfig(WE_sensorInterface_t* sensorInterface, HIDS_temperatureAverageConfig_t *avgTemp)
199 {
200   HIDS_averageConfig_t averageReg;
201 
202   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_AVERAGE_REG, 1, (uint8_t *) &averageReg))
203   {
204     return WE_FAIL;
205   }
206 
207   *avgTemp = (HIDS_temperatureAverageConfig_t) averageReg.avgTemp;
208 
209   return WE_SUCCESS;
210 }
211 
212 /**
213  * @brief Set the output data rate of the sensor
214  * @param[in] sensorInterface Pointer to sensor interface
215  * @param[in] odr Output data rate
216  * @return Error code
217  */
HIDS_setOutputDataRate(WE_sensorInterface_t * sensorInterface,HIDS_outputDataRate_t odr)218 int8_t HIDS_setOutputDataRate(WE_sensorInterface_t* sensorInterface, HIDS_outputDataRate_t odr)
219 {
220   HIDS_ctrl1_t ctrlReg1;
221 
222   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_CTRL_REG_1, 1, (uint8_t *) &ctrlReg1))
223   {
224     return WE_FAIL;
225   }
226 
227   ctrlReg1.odr = odr;
228 
229   return HIDS_WriteReg(sensorInterface, HIDS_CTRL_REG_1, 1, (uint8_t *) &ctrlReg1);
230 }
231 
232 /**
233  * @brief Read the output data rate of the sensor
234  * @param[in] sensorInterface Pointer to sensor interface
235  * @param[out] odr The returned output data rate
236  * @return Error code
237  */
HIDS_getOutputDataRate(WE_sensorInterface_t * sensorInterface,HIDS_outputDataRate_t * odr)238 int8_t HIDS_getOutputDataRate(WE_sensorInterface_t* sensorInterface, HIDS_outputDataRate_t *odr)
239 {
240   HIDS_ctrl1_t ctrlReg1;
241 
242   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_CTRL_REG_1, 1, (uint8_t *) &ctrlReg1))
243   {
244     return WE_FAIL;
245   }
246 
247   *odr = (HIDS_outputDataRate_t) ctrlReg1.odr;
248 
249   return WE_SUCCESS;
250 }
251 
252 /**
253  * @brief Enable/disable block data update mode
254  * @param[in] sensorInterface Pointer to sensor interface
255  * @param[in] bdu Block data update state
256  * @retval Error code
257  */
HIDS_enableBlockDataUpdate(WE_sensorInterface_t * sensorInterface,HIDS_state_t bdu)258 int8_t HIDS_enableBlockDataUpdate(WE_sensorInterface_t* sensorInterface, HIDS_state_t bdu)
259 {
260   HIDS_ctrl1_t ctrlReg1;
261 
262   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_CTRL_REG_1, 1, (uint8_t *) &ctrlReg1))
263   {
264     return WE_FAIL;
265   }
266 
267   ctrlReg1.bdu = bdu;
268 
269   return HIDS_WriteReg(sensorInterface, HIDS_CTRL_REG_1, 1, (uint8_t *) &ctrlReg1);
270 }
271 
272 /**
273  * @brief Read the block data update state
274  * @param[in] sensorInterface Pointer to sensor interface
275  * @param[out] bdu The returned block data update state
276  * @return Error code
277  */
HIDS_isBlockDataUpdateEnabled(WE_sensorInterface_t * sensorInterface,HIDS_state_t * bdu)278 int8_t HIDS_isBlockDataUpdateEnabled(WE_sensorInterface_t* sensorInterface, HIDS_state_t *bdu)
279 {
280   HIDS_ctrl1_t ctrlReg1;
281 
282   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_CTRL_REG_1, 1, (uint8_t *) &ctrlReg1))
283   {
284     return WE_FAIL;
285   }
286 
287   *bdu = (HIDS_state_t) ctrlReg1.bdu;
288 
289   return WE_SUCCESS;
290 }
291 
292 /**
293  * @brief Set the power control mode
294  * @param[in] sensorInterface Pointer to sensor interface
295  * @param[in] pd Power control mode
296  * @return Error code
297  */
HIDS_setPowerMode(WE_sensorInterface_t * sensorInterface,HIDS_powerMode_t pd)298 int8_t HIDS_setPowerMode(WE_sensorInterface_t* sensorInterface, HIDS_powerMode_t pd)
299 {
300   HIDS_ctrl1_t ctrlReg1;
301 
302   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_CTRL_REG_1, 1, (uint8_t *) &ctrlReg1))
303   {
304     return WE_FAIL;
305   }
306 
307   ctrlReg1.powerControlMode = pd;
308 
309   return HIDS_WriteReg(sensorInterface, HIDS_CTRL_REG_1, 1, (uint8_t *) &ctrlReg1);
310 }
311 
312 /**
313  * @brief Read the power control mode
314  * @param[in] sensorInterface Pointer to sensor interface
315  * @param[out] pd The returned power control mode
316  * @return Error code
317  */
HIDS_getPowerMode(WE_sensorInterface_t * sensorInterface,HIDS_powerMode_t * pd)318 int8_t HIDS_getPowerMode(WE_sensorInterface_t* sensorInterface, HIDS_powerMode_t *pd)
319 {
320   HIDS_ctrl1_t ctrlReg1;
321 
322   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_CTRL_REG_1, 1, (uint8_t *) &ctrlReg1))
323   {
324     return WE_FAIL;
325   }
326 
327   *pd = (HIDS_powerMode_t) ctrlReg1.powerControlMode;
328 
329   return WE_SUCCESS;
330 }
331 
332 /**
333  * @brief Trigger capturing of a new value in one-shot mode.
334  *
335  * Note: Depends on ctrl_reg_1.ODR = '00' (one-shot mode)
336  *
337  * @param[in] sensorInterface Pointer to sensor interface
338  * @param[in] oneShot One shot bit state
339  * @return Error code
340  */
HIDS_enableOneShot(WE_sensorInterface_t * sensorInterface,HIDS_state_t oneShot)341 int8_t HIDS_enableOneShot(WE_sensorInterface_t* sensorInterface, HIDS_state_t oneShot)
342 {
343   HIDS_ctrl2_t ctrlReg2;
344 
345   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_CTRL_REG_2, 1, (uint8_t *) &ctrlReg2))
346   {
347     return WE_FAIL;
348   }
349 
350   ctrlReg2.oneShotBit = oneShot;
351 
352   return HIDS_WriteReg(sensorInterface, HIDS_CTRL_REG_2, 1, (uint8_t *) &ctrlReg2);
353 }
354 
355 /**
356  * @brief Read the one shot bit state
357  * @param[in] sensorInterface Pointer to sensor interface
358  * @param[out] oneShot The returned one shot bit state
359  * @return Error code
360  */
HIDS_isOneShotEnabled(WE_sensorInterface_t * sensorInterface,HIDS_state_t * oneShot)361 int8_t HIDS_isOneShotEnabled(WE_sensorInterface_t* sensorInterface, HIDS_state_t *oneShot)
362 {
363   HIDS_ctrl2_t ctrlReg2;
364 
365   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_CTRL_REG_2, 1, (uint8_t *) &ctrlReg2))
366   {
367     return WE_FAIL;
368   }
369 
370   *oneShot = (HIDS_state_t) ctrlReg2.oneShotBit;
371 
372   return WE_SUCCESS;
373 }
374 
375 /**
376  * @brief Enable/disable the heater
377  * @param[in] sensorInterface Pointer to sensor interface
378  * @param[in] heater Heater state
379  * @return Error code
380  */
HIDS_enableHeater(WE_sensorInterface_t * sensorInterface,HIDS_state_t heater)381 int8_t HIDS_enableHeater(WE_sensorInterface_t* sensorInterface, HIDS_state_t heater)
382 {
383   HIDS_ctrl2_t ctrlReg2;
384 
385   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_CTRL_REG_2, 1, (uint8_t *) &ctrlReg2))
386   {
387     return WE_FAIL;
388   }
389 
390   ctrlReg2.heater = heater;
391 
392   return HIDS_WriteReg(sensorInterface, HIDS_CTRL_REG_2, 1, (uint8_t *) &ctrlReg2);
393 }
394 
395 /**
396  * @brief Read the heater state
397  * @param[in] sensorInterface Pointer to sensor interface
398  * @param[out] heater The returned heater state
399  * @return Error code
400  */
HIDS_isHeaterEnabled(WE_sensorInterface_t * sensorInterface,HIDS_state_t * heater)401 int8_t HIDS_isHeaterEnabled(WE_sensorInterface_t* sensorInterface, HIDS_state_t *heater)
402 {
403   HIDS_ctrl2_t ctrlReg2;
404 
405   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_CTRL_REG_2, 1, (uint8_t *) &ctrlReg2))
406   {
407     return WE_FAIL;
408   }
409 
410   *heater = (HIDS_state_t) ctrlReg2.heater;
411 
412   return WE_SUCCESS;
413 }
414 
415 /**
416  * @brief Enable the memory reboot
417  * @param[in] sensorInterface Pointer to sensor interface
418  * @param[in] reboot Reboot state
419  * @return Error code
420  */
HIDS_reboot(WE_sensorInterface_t * sensorInterface,HIDS_state_t reboot)421 int8_t HIDS_reboot(WE_sensorInterface_t* sensorInterface, HIDS_state_t reboot)
422 {
423   HIDS_ctrl2_t ctrlReg2;
424 
425   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_CTRL_REG_2, 1, (uint8_t *) &ctrlReg2))
426   {
427     return WE_FAIL;
428   }
429 
430   ctrlReg2.rebootMemory = reboot;
431 
432   return HIDS_WriteReg(sensorInterface, HIDS_CTRL_REG_2, 1, (uint8_t *) &ctrlReg2);
433 }
434 
435 /**
436  * @brief Read the reboot state
437  * @param[in] sensorInterface Pointer to sensor interface
438  * @param[out] rebooting The returned reboot state
439  * @return Error code
440  */
HIDS_isRebooting(WE_sensorInterface_t * sensorInterface,HIDS_state_t * rebooting)441 int8_t HIDS_isRebooting(WE_sensorInterface_t* sensorInterface, HIDS_state_t *rebooting)
442 {
443   HIDS_ctrl2_t ctrlReg2;
444 
445   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_CTRL_REG_2, 1, (uint8_t *) &ctrlReg2))
446   {
447     return WE_FAIL;
448   }
449 
450   *rebooting = (HIDS_state_t) ctrlReg2.rebootMemory;
451 
452   return WE_SUCCESS;
453 }
454 
455 /**
456  * @brief Enable/disable the data ready interrupt
457  * @param[in] sensorInterface Pointer to sensor interface
458  * @param[in] drdy Data ready interrupt enabled/disabled
459  * @return Error code
460  */
HIDS_enableDataReadyInterrupt(WE_sensorInterface_t * sensorInterface,HIDS_state_t drdy)461 int8_t HIDS_enableDataReadyInterrupt(WE_sensorInterface_t* sensorInterface, HIDS_state_t drdy)
462 {
463   HIDS_ctrl3_t ctrlReg3;
464 
465   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_CTRL_REG_3, 1, (uint8_t *) &ctrlReg3))
466   {
467     return WE_FAIL;
468   }
469 
470   ctrlReg3.enDataReady = drdy;
471 
472   return HIDS_WriteReg(sensorInterface, HIDS_CTRL_REG_3, 1, (uint8_t *) &ctrlReg3);
473 }
474 
475 /**
476  * @brief Read the data ready interrupt enable state
477  * @param[in] sensorInterface Pointer to sensor interface
478  * @param[out] drdy The returned data ready enable state
479  * @return Error code
480  */
HIDS_isDataReadyInterruptEnabled(WE_sensorInterface_t * sensorInterface,HIDS_state_t * drdy)481 int8_t HIDS_isDataReadyInterruptEnabled(WE_sensorInterface_t* sensorInterface, HIDS_state_t *drdy)
482 {
483   HIDS_ctrl3_t ctrlReg3;
484 
485   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_CTRL_REG_3, 1, (uint8_t *) &ctrlReg3))
486   {
487     return WE_FAIL;
488   }
489 
490   *drdy = (HIDS_state_t) ctrlReg3.enDataReady;
491 
492   return WE_SUCCESS;
493 }
494 
495 /**
496  * @brief Set the (data ready) interrupt pin type
497  * @param[in] sensorInterface Pointer to sensor interface
498  * @param[in] pinConfig Interrupt pin type (push-pull / open drain)
499  * @return Error code
500  */
HIDS_setInterruptPinType(WE_sensorInterface_t * sensorInterface,HIDS_interruptPinConfig_t pinType)501 int8_t HIDS_setInterruptPinType(WE_sensorInterface_t* sensorInterface, HIDS_interruptPinConfig_t pinType)
502 {
503   HIDS_ctrl3_t ctrlReg3;
504 
505   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_CTRL_REG_3, 1, (uint8_t *) &ctrlReg3))
506   {
507     return WE_FAIL;
508   }
509 
510   ctrlReg3.interruptPinConfig = pinType;
511 
512   return HIDS_WriteReg(sensorInterface, HIDS_CTRL_REG_3, 1, (uint8_t *) &ctrlReg3);
513 }
514 
515 /**
516  * @brief Read the (data ready) interrupt pin type
517  * @param[in] sensorInterface Pointer to sensor interface
518  * @param[out] pinConfig The returned interrupt pin type (push-pull / open drain)
519  * @return Error code
520  */
HIDS_getInterruptPinType(WE_sensorInterface_t * sensorInterface,HIDS_interruptPinConfig_t * pinType)521 int8_t HIDS_getInterruptPinType(WE_sensorInterface_t* sensorInterface, HIDS_interruptPinConfig_t *pinType)
522 {
523   HIDS_ctrl3_t ctrlReg3;
524 
525   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_CTRL_REG_3, 1, (uint8_t *) &ctrlReg3))
526   {
527     return WE_FAIL;
528   }
529 
530   *pinType = (HIDS_interruptPinConfig_t) ctrlReg3.interruptPinConfig;
531 
532   return WE_SUCCESS;
533 }
534 
535 /**
536  * @brief Set the (data ready) output interrupt pin level
537  * @param[in] sensorInterface Pointer to sensor interface
538  * @param[in] level Level of output interrupt pin
539  * @return Error code
540  */
HIDS_setInterruptActiveLevel(WE_sensorInterface_t * sensorInterface,HIDS_interruptActiveLevel_t level)541 int8_t HIDS_setInterruptActiveLevel(WE_sensorInterface_t* sensorInterface, HIDS_interruptActiveLevel_t level)
542 {
543   HIDS_ctrl3_t ctrlReg3;
544 
545   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_CTRL_REG_3, 1, (uint8_t *) &ctrlReg3))
546   {
547     return WE_FAIL;
548   }
549 
550   ctrlReg3.drdyOutputLevel = level;
551 
552   return HIDS_WriteReg(sensorInterface, HIDS_CTRL_REG_3, 1, (uint8_t *) &ctrlReg3);
553 }
554 
555 /**
556  * @brief Read the (data ready) output interrupt pin level
557  * @param[in] sensorInterface Pointer to sensor interface
558  * @param[out] level The returned output interrupt pin level
559  * @return Error code
560  */
HIDS_getInterruptActiveLevel(WE_sensorInterface_t * sensorInterface,HIDS_interruptActiveLevel_t * level)561 int8_t HIDS_getInterruptActiveLevel(WE_sensorInterface_t* sensorInterface, HIDS_interruptActiveLevel_t *level)
562 {
563   HIDS_ctrl3_t ctrlReg3;
564 
565   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_CTRL_REG_3, 1, (uint8_t *) &ctrlReg3))
566   {
567     return WE_FAIL;
568   }
569 
570   *level = (HIDS_interruptActiveLevel_t) ctrlReg3.drdyOutputLevel;
571 
572   return WE_SUCCESS;
573 }
574 
575 /**
576  * @brief Check if a new humidity data sample is available
577  * since upon read of HIDS_STATUS_REG register both data ready flags will be reset to '0' they shall only be read in one register access.
578  * @param[in] sensorInterface Pointer to sensor interface
579  * @param[out] state Is set to true if a new sample is available
580  * @return Error code
581  */
HIDS_isHumidityDataAvailable(WE_sensorInterface_t * sensorInterface,HIDS_state_t * state)582 int8_t HIDS_isHumidityDataAvailable(WE_sensorInterface_t* sensorInterface, HIDS_state_t *state)
583 {
584   HIDS_status_t statusReg;
585 
586   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_STATUS_REG, 1, (uint8_t *) &statusReg))
587   {
588     return WE_FAIL;
589   }
590 
591   *state = (HIDS_state_t) statusReg.humDataAvailable;
592 
593   return WE_SUCCESS;
594 }
595 
596 /**
597  * @brief Check if a new temperature data sample is available
598  * @param[in] sensorInterface Pointer to sensor interface
599  * @param[out] state Is set to true if a new sample is available
600  * @return Error code
601  */
HIDS_isTemperatureDataAvailable(WE_sensorInterface_t * sensorInterface,HIDS_state_t * state)602 int8_t HIDS_isTemperatureDataAvailable(WE_sensorInterface_t* sensorInterface, HIDS_state_t *state)
603 {
604   HIDS_status_t statusReg;
605 
606   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_STATUS_REG, 1, (uint8_t *) &statusReg))
607   {
608     return WE_FAIL;
609   }
610 
611   *state = (HIDS_state_t) statusReg.tempDataAvailable;
612 
613   return WE_SUCCESS;
614 }
615 
616 /**
617  * @brief Check if a new temperature and humidity data sample is available
618  * @param[in] sensorInterface Pointer to sensor interface
619  * @param[out] temp_state Is set to true if a new temperature sample is available
620  * @param[out] hum_state Is set to true if a new humidity sample is available
621  * @return Error code
622  */
HIDS_isDataAvailable(WE_sensorInterface_t * sensorInterface,HIDS_state_t * temp_state,HIDS_state_t * hum_state)623 int8_t HIDS_isDataAvailable(WE_sensorInterface_t* sensorInterface, HIDS_state_t *temp_state, HIDS_state_t *hum_state)
624 {
625   HIDS_status_t statusReg;
626 
627   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_STATUS_REG, 1, (uint8_t *) &statusReg))
628   {
629     return WE_FAIL;
630   }
631 
632   if(temp_state != NULL)
633   {
634 	  *temp_state = (HIDS_state_t) statusReg.tempDataAvailable;
635   }
636   if(hum_state != NULL)
637   {
638   	  *hum_state = (HIDS_state_t) statusReg.humDataAvailable;
639   }
640 
641   return WE_SUCCESS;
642 }
643 
644 /**
645  * @brief Read a raw humidity value
646  * @param[in] sensorInterface Pointer to sensor interface
647  * @param[out] rawHumidity The returned raw humidity
648  * @return Error code
649  */
HIDS_getRawHumidity(WE_sensorInterface_t * sensorInterface,int16_t * rawHumidity)650 int8_t HIDS_getRawHumidity(WE_sensorInterface_t* sensorInterface, int16_t *rawHumidity)
651 {
652   uint8_t buffer[2];
653 
654   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_H_OUT_L_REG, 2, buffer))
655   {
656     *rawHumidity = 0;
657     return WE_FAIL;
658   }
659 
660   *rawHumidity = (int16_t) (buffer[1] << 8);
661   *rawHumidity |= (int16_t) buffer[0];
662 
663   return WE_SUCCESS;
664 }
665 
666 /**
667  * @brief Read a raw temperature value
668  * @param[in] sensorInterface Pointer to sensor interface
669  * @param[out] rawTemp The returned raw temperature
670  * @return Error code
671  */
HIDS_getRawTemperature(WE_sensorInterface_t * sensorInterface,int16_t * rawTemp)672 int8_t HIDS_getRawTemperature(WE_sensorInterface_t* sensorInterface, int16_t *rawTemp)
673 {
674   uint8_t buffer[2];
675 
676   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_T_OUT_L_REG, 2, buffer))
677   {
678     *rawTemp = 0;
679     return WE_FAIL;
680   }
681 
682   *rawTemp = (int16_t) (buffer[1] << 8);
683   *rawTemp |= (int16_t) buffer[0];
684 
685   return WE_SUCCESS;
686 }
687 
688 /**
689  * @brief Read raw temperature and humidity values
690  * @param[in] sensorInterface Pointer to sensor interface
691  * @param[out] rawHumidity The returned raw humidity
692  * @param[out] rawTemp The returned raw temperature
693  * @return Error code
694  */
HIDS_getRawValues(WE_sensorInterface_t * sensorInterface,int16_t * rawHumidity,int16_t * rawTemp)695 int8_t HIDS_getRawValues(WE_sensorInterface_t* sensorInterface, int16_t *rawHumidity, int16_t *rawTemp)
696 {
697   uint8_t buffer[4];
698 
699   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_H_OUT_L_REG, 4, buffer))
700   {
701     *rawHumidity = 0;
702     *rawTemp = 0;
703     return WE_FAIL;
704   }
705 
706   *rawHumidity = (int16_t) (buffer[1] << 8);
707   *rawHumidity |= (int16_t) buffer[0];
708 
709   *rawTemp = (int16_t) (buffer[3] << 8);
710   *rawTemp |= (int16_t) buffer[2];
711 
712   return WE_SUCCESS;
713 }
714 
715 #ifdef WE_USE_FLOAT
716 
717 /**
718  * @brief Read humidity
719  *
720  * Note: Architecture must support float
721  *
722  * @param[in] sensorInterface Pointer to sensor interface
723  * @param[out] humidity The returned humidity in %
724  * @return Error code
725  */
HIDS_getHumidity_float(WE_sensorInterface_t * sensorInterface,float * humidity)726 int8_t HIDS_getHumidity_float(WE_sensorInterface_t* sensorInterface, float *humidity)
727 {
728   int16_t rawHumidity;
729   if (WE_FAIL == HIDS_getRawHumidity(sensorInterface, &rawHumidity))
730   {
731     *humidity = 0;
732     return WE_FAIL;
733   }
734   return HIDS_convertHumidity_float(sensorInterface, rawHumidity, humidity);
735 }
736 
737 /**
738  * @brief Read the temperature
739  *
740  * Note: Architecture must support float
741  *
742  * @param[in] sensorInterface Pointer to sensor interface
743  * @param[out] tempDegC The returned temperature in °C
744  * @return Error code
745  */
HIDS_getTemperature_float(WE_sensorInterface_t * sensorInterface,float * tempDegC)746 int8_t HIDS_getTemperature_float(WE_sensorInterface_t* sensorInterface, float *tempDegC)
747 {
748   int16_t tempRaw;
749   if (WE_FAIL == HIDS_getRawTemperature(sensorInterface, &tempRaw))
750   {
751     *tempDegC = 0;
752     return WE_FAIL;
753   }
754   return HIDS_convertTemperature_float(sensorInterface, tempRaw, tempDegC);
755 }
756 
757 /**
758  * @brief Convert raw humidity to humidity in %
759  *
760  * Note: Architecture must support float
761  *
762  * @param[in] sensorInterface Pointer to sensor interface
763  * @param[in] rawHumidity The raw humidity to be converted
764  * @param[out] humidity The returned humidity in %
765  * @return Error code
766  */
HIDS_convertHumidity_float(WE_sensorInterface_t * sensorInterface,int16_t rawHumidity,float * humidity)767 int8_t HIDS_convertHumidity_float(WE_sensorInterface_t* sensorInterface, int16_t rawHumidity, float *humidity)
768 {
769   if (hidsCalibrationData.calibrationPresent == 0)
770   {
771     if (WE_FAIL == HIDS_readCalibrationData(sensorInterface))
772     {
773       *humidity = 0;
774       return WE_FAIL;
775     }
776   }
777 
778   *humidity = ((((float) hidsCalibrationData.H1_rh - (float) hidsCalibrationData.H0_rh) * ((float) rawHumidity - (float) hidsCalibrationData.H0_T0_out))) /
779       ((float) hidsCalibrationData.H1_T0_out - (float) hidsCalibrationData.H0_T0_out) + (float) hidsCalibrationData.H0_rh;
780 
781   if (*humidity > 100)
782   {
783     *humidity = 100;
784   }
785   else if (*humidity < 0)
786   {
787     *humidity = 0;
788   }
789 
790   return WE_SUCCESS;
791 }
792 
793 /**
794  * @brief Convert raw temperature to temperature in °C
795  *
796  * Note: Architecture must support float
797  *
798  * @param[in] sensorInterface Pointer to sensor interface
799  * @param[in] rawTemp The raw temperature to be converted
800  * @param[out] tempDegC The returned temperature in °C
801  * @return Error code
802  */
HIDS_convertTemperature_float(WE_sensorInterface_t * sensorInterface,int16_t rawTemp,float * tempDegC)803 int8_t HIDS_convertTemperature_float(WE_sensorInterface_t* sensorInterface, int16_t rawTemp, float *tempDegC)
804 {
805   if (hidsCalibrationData.calibrationPresent == 0)
806   {
807     if (WE_FAIL == HIDS_readCalibrationData(sensorInterface))
808     {
809       *tempDegC = 0;
810       return WE_FAIL;
811     }
812   }
813 
814   // Decode temperature
815   // Calculate temperature in degrees Celsius
816   // Provide signed Celsius measurement unit
817 
818   *tempDegC = (float) (((int16_t) rawTemp - (int16_t) hidsCalibrationData.T0_out) * (float) ((int16_t) hidsCalibrationData.T1_degC - (int16_t) hidsCalibrationData.T0_degC)) /
819       (float) ((int16_t) hidsCalibrationData.T1_out - (int16_t) hidsCalibrationData.T0_out) + (float) ((int16_t) hidsCalibrationData.T0_degC);
820 
821   return WE_SUCCESS;
822 }
823 
824 #endif /* WE_USE_FLOAT */
825 
826 
827 
828 /**
829  * @brief Read the humidity
830  * @param[in] sensorInterface Pointer to sensor interface
831  * @param[out] humidity The returned humidity in 0...100 % RH
832  * @return Error code
833  */
HIDS_getHumidity_int8(WE_sensorInterface_t * sensorInterface,int8_t * humidity)834 int8_t HIDS_getHumidity_int8(WE_sensorInterface_t* sensorInterface, int8_t *humidity)
835 {
836   int16_t rawHumidity;
837   if (WE_FAIL == HIDS_getRawHumidity(sensorInterface, &rawHumidity))
838   {
839     *humidity = 0;
840     return WE_FAIL;
841   }
842   return HIDS_convertHumidity_int8(sensorInterface, rawHumidity, humidity);
843 }
844 
845 /**
846  * @brief Read the temperature
847  * @param[in] sensorInterface Pointer to sensor interface
848  * @param[out] tempDegC The returned temperature in -40...+85 °C
849  * @return Error code
850  */
HIDS_getTemperature_int8(WE_sensorInterface_t * sensorInterface,int8_t * tempDegC)851 int8_t HIDS_getTemperature_int8(WE_sensorInterface_t* sensorInterface, int8_t *tempDegC)
852 {
853   int16_t tempRaw;
854   if (WE_FAIL == HIDS_getRawTemperature(sensorInterface, &tempRaw))
855   {
856     *tempDegC = 0;
857     return WE_FAIL;
858   }
859   return HIDS_convertTemperature_int8(sensorInterface, tempRaw, tempDegC);
860 }
861 
862 /**
863  * @brief Convert raw humidity to 0...100 % RH
864  * @param[in] sensorInterface Pointer to sensor interface
865  * @param[in] rawHumidity The raw humidity to be converted
866  * @param[out] humidity The returned humidity in 0...100 % RH
867  * @return Error code
868  */
HIDS_convertHumidity_int8(WE_sensorInterface_t * sensorInterface,int16_t rawHumidity,int8_t * humidity)869 int8_t HIDS_convertHumidity_int8(WE_sensorInterface_t* sensorInterface, int16_t rawHumidity, int8_t *humidity)
870 {
871   int32_t relHum;
872 
873   if (hidsCalibrationData.calibrationPresent == 0)
874   {
875     if (WE_FAIL == HIDS_readCalibrationData(sensorInterface))
876     {
877       return WE_FAIL;
878     }
879   }
880 
881   relHum = (((int32_t) hidsCalibrationData.H1_rh - (int32_t) hidsCalibrationData.H0_rh) * ((int32_t) rawHumidity - (int32_t) hidsCalibrationData.H0_T0_out)) /
882       ((int32_t) hidsCalibrationData.H1_T0_out - (int32_t) hidsCalibrationData.H0_T0_out) + (int32_t) hidsCalibrationData.H0_rh;
883 
884   if (relHum > 100)
885   {
886     relHum = 100;
887   }
888   else if (relHum < 0)
889   {
890     relHum = 0;
891   }
892 
893   *humidity = (int8_t) relHum; // provide signed % measurement unit
894 
895   return WE_SUCCESS;
896 }
897 
898 /**
899  * @brief Convert raw temperature to °C
900  * @param[in] sensorInterface Pointer to sensor interface
901  * @param[in] rawTemp The raw temperature to be converted
902  * @param[out] tempDegC The returned temperature in -40...+85 °C
903  * @return Error code
904  */
HIDS_convertTemperature_int8(WE_sensorInterface_t * sensorInterface,int16_t rawTemp,int8_t * tempDegC)905 int8_t HIDS_convertTemperature_int8(WE_sensorInterface_t* sensorInterface, int16_t rawTemp, int8_t *tempDegC)
906 {
907   int32_t tTemp;
908 
909   if (hidsCalibrationData.calibrationPresent == 0)
910   {
911     if (WE_FAIL == HIDS_readCalibrationData(sensorInterface))
912     {
913       *tempDegC = 0;
914       return WE_FAIL;
915     }
916   }
917 
918   // Calculate temperature in full degrees
919   tTemp = (((int32_t) rawTemp - (int32_t) hidsCalibrationData.T0_out) * ((int32_t) hidsCalibrationData.T1_degC - (int32_t) hidsCalibrationData.T0_degC)) /
920       ((int32_t) hidsCalibrationData.T1_out - (int32_t) hidsCalibrationData.T0_out) + (int32_t) hidsCalibrationData.T0_degC;
921 
922   if (tTemp > 85)
923   {
924     tTemp = 85;
925   }
926   else if (tTemp < -40)
927   {
928     tTemp = -40;
929   }
930 
931   *tempDegC = (int8_t) tTemp;
932 
933   return WE_SUCCESS;
934 }
935 
936 /**
937  * @brief Read the humidity
938  * @param[in] sensorInterface Pointer to sensor interface
939  * @param[out] humidity The returned humidity in 0.01%
940  * @return Error code
941  */
HIDS_getHumidity_uint16(WE_sensorInterface_t * sensorInterface,uint16_t * humidity)942 int8_t HIDS_getHumidity_uint16(WE_sensorInterface_t* sensorInterface, uint16_t *humidity)
943 {
944   int16_t rawHumidity;
945   if (WE_FAIL == HIDS_getRawHumidity(sensorInterface, &rawHumidity))
946   {
947     *humidity = 0;
948     return WE_FAIL;
949   }
950   return HIDS_convertHumidity_uint16(sensorInterface, rawHumidity, humidity);
951 }
952 
953 /**
954  * @brief Read the temperature
955  * @param[in] sensorInterface Pointer to sensor interface
956  * @param[out] temperature The returned temperature in 0.01°C
957  * @return Error code
958  */
HIDS_getTemperature_int16(WE_sensorInterface_t * sensorInterface,int16_t * temperature)959 int8_t HIDS_getTemperature_int16(WE_sensorInterface_t* sensorInterface, int16_t *temperature)
960 {
961   int16_t tempRaw;
962   if (WE_FAIL == HIDS_getRawTemperature(sensorInterface, &tempRaw))
963   {
964     *temperature = 0;
965     return WE_FAIL;
966   }
967   return HIDS_convertTemperature_int16(sensorInterface, tempRaw, temperature);
968 }
969 
970 /**
971  * @brief Convert raw humidity to 0.01%
972  * @param[in] sensorInterface Pointer to sensor interface
973  * @param[in] rawHumidity The raw humidity to be converted
974  * @param[out] humidity The returned humidity in 0.01%
975  * @return Error code
976  */
HIDS_convertHumidity_uint16(WE_sensorInterface_t * sensorInterface,int16_t rawHumidity,uint16_t * humidity)977 int8_t HIDS_convertHumidity_uint16(WE_sensorInterface_t* sensorInterface, int16_t rawHumidity, uint16_t *humidity)
978 {
979   int32_t relHum;
980 
981   if (hidsCalibrationData.calibrationPresent == 0)
982   {
983     if (WE_FAIL == HIDS_readCalibrationData(sensorInterface))
984     {
985       *humidity = 0;
986       return WE_FAIL;
987     }
988   }
989 
990   // Decode Humidity
991   // Calculate humidity in decimal i.e. 15.0 = 1500.
992 
993   relHum = ((((int32_t) hidsCalibrationData.H1_rh - (int32_t) hidsCalibrationData.H0_rh) * ((int32_t) rawHumidity - (int32_t) hidsCalibrationData.H0_T0_out)) * 100) /
994       ((int32_t) hidsCalibrationData.H1_T0_out - (int32_t) hidsCalibrationData.H0_T0_out) + (((int32_t) hidsCalibrationData.H0_rh) * 100);
995 
996   if (relHum > 100 * 100)
997   {
998     *humidity = 100 * 100;
999   }
1000   else if (relHum < 0)
1001   {
1002     *humidity = 0;
1003   }
1004   else
1005   {
1006     *humidity = (uint16_t) relHum; // provide unsigned % measurement unit
1007   }
1008 
1009   return WE_SUCCESS;
1010 }
1011 
1012 /**
1013  * @brief Convert raw temperature to 0.01°C
1014  * @param[in] sensorInterface Pointer to sensor interface
1015  * @param[in] rawTemp The raw temperature to be converted
1016  * @param[out] temperature The returned temperature in 0.01°C
1017  * @return Error code
1018  */
HIDS_convertTemperature_int16(WE_sensorInterface_t * sensorInterface,int16_t rawTemp,int16_t * temperature)1019 int8_t HIDS_convertTemperature_int16(WE_sensorInterface_t* sensorInterface, int16_t rawTemp, int16_t *temperature)
1020 {
1021   int32_t tTemp;
1022 
1023   if (hidsCalibrationData.calibrationPresent == 0)
1024   {
1025     if (WE_FAIL == HIDS_readCalibrationData(sensorInterface))
1026     {
1027       *temperature = 0;
1028       return WE_FAIL;
1029     }
1030   }
1031 
1032   // Decode temperature
1033   // Calculate temperature in decimal of degree
1034   // centigrade i.e. 15.0 = 1500.
1035 
1036   tTemp = ((((int32_t) rawTemp - (int32_t) hidsCalibrationData.T0_out) * ((int32_t) hidsCalibrationData.T1_degC - (int32_t) hidsCalibrationData.T0_degC)) * 100) /
1037       ((int32_t) hidsCalibrationData.T1_out - (int32_t) hidsCalibrationData.T0_out) + (((int32_t) hidsCalibrationData.T0_degC) * 100);
1038 
1039   // provide signed celsius*100 measurement unit
1040 
1041   *temperature = (int16_t) tTemp;
1042 
1043   return WE_SUCCESS;
1044 }
1045 
1046 
1047 /* ********************************************************* */
1048 
1049 /**
1050  * @brief Get the sensor's calibration data for re-use
1051  * @param[in] sensorInterface Pointer to sensor interface
1052  * @return Error code
1053  */
HIDS_readCalibrationData(WE_sensorInterface_t * sensorInterface)1054 int8_t HIDS_readCalibrationData(WE_sensorInterface_t* sensorInterface)
1055 {
1056   /* Temperature calibration data for T0 and T1 points */
1057   if (WE_FAIL == HIDS_get_T0_degC(sensorInterface))
1058   {
1059     return WE_FAIL;
1060   }
1061 
1062   if (WE_FAIL == HIDS_get_T1_degC(sensorInterface))
1063   {
1064     return WE_FAIL;
1065   }
1066 
1067   if (WE_FAIL == HIDS_get_T0_OUT(sensorInterface))
1068   {
1069     return WE_FAIL;
1070   }
1071 
1072   if (WE_FAIL == HIDS_get_T1_OUT(sensorInterface))
1073   {
1074     return WE_FAIL;
1075   }
1076 
1077   /* Relative humidity calibration data for H0 and H1 points */
1078   if (WE_FAIL == HIDS_get_H0_rh(sensorInterface))
1079   {
1080     return WE_FAIL;
1081   }
1082 
1083   if (WE_FAIL == HIDS_get_H1_rh(sensorInterface))
1084   {
1085     return WE_FAIL;
1086   }
1087 
1088   if (WE_FAIL == HIDS_get_H0_T0_out(sensorInterface))
1089   {
1090     return WE_FAIL;
1091   }
1092 
1093   if (WE_FAIL == HIDS_get_H1_T0_out(sensorInterface))
1094   {
1095     return WE_FAIL;
1096   }
1097 
1098   hidsCalibrationData.calibrationPresent = 1;
1099 
1100   return WE_SUCCESS;
1101 }
1102 
1103 /**
1104  * @brief Read H0_T0_out (calibration data) and store the value in hidsCalibrationData.
1105  * @param[in] sensorInterface Pointer to sensor interface
1106  * @return Error code
1107  */
HIDS_get_H0_T0_out(WE_sensorInterface_t * sensorInterface)1108 static int8_t HIDS_get_H0_T0_out(WE_sensorInterface_t* sensorInterface)
1109 {
1110   uint8_t buffer;
1111   int16_t temp;
1112 
1113   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_H0_T0_OUT_H, 1, &buffer))
1114   {
1115     return WE_FAIL;
1116   }
1117 
1118   temp = (((int16_t) buffer) << 8);
1119 
1120   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_H0_T0_OUT_L, 1, &buffer))
1121   {
1122     return WE_FAIL;
1123   }
1124 
1125   hidsCalibrationData.H0_T0_out = temp | buffer;
1126 
1127   return WE_SUCCESS;
1128 }
1129 
1130 /**
1131  * @brief  Read H1_T0_out (calibration data) and store the value in hidsCalibrationData.
1132  * @param[in] sensorInterface Pointer to sensor interface
1133  * @return Error code
1134  */
HIDS_get_H1_T0_out(WE_sensorInterface_t * sensorInterface)1135 static int8_t HIDS_get_H1_T0_out(WE_sensorInterface_t* sensorInterface)
1136 {
1137   uint8_t buffer;
1138   int16_t temp;
1139 
1140   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_H1_T0_OUT_H, 1, &buffer))
1141   {
1142     return WE_FAIL;
1143   }
1144 
1145   temp = (((int16_t) buffer) << 8);
1146 
1147   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_H1_T0_OUT_L, 1, &buffer))
1148   {
1149     return WE_FAIL;
1150   }
1151 
1152   hidsCalibrationData.H1_T0_out = temp | buffer;
1153 
1154   return WE_SUCCESS;
1155 }
1156 
1157 /**
1158  * @brief  Read H0_rh (calibration data) and store the value in hidsCalibrationData.
1159  * @param[in] sensorInterface Pointer to sensor interface
1160  * @return Error code
1161  */
HIDS_get_H0_rh(WE_sensorInterface_t * sensorInterface)1162 static int8_t HIDS_get_H0_rh(WE_sensorInterface_t* sensorInterface)
1163 {
1164   uint8_t buffer;
1165 
1166   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_H0_RH_X2, 1, &buffer))
1167   {
1168     return WE_FAIL;
1169   }
1170 
1171   hidsCalibrationData.H0_rh = buffer >> 1;
1172 
1173   return WE_SUCCESS;
1174 }
1175 
1176 /**
1177  * @brief Read H1_rh (calibration data) and store the value in hidsCalibrationData.
1178  * @param[in] sensorInterface Pointer to sensor interface
1179  * @return Error code
1180  */
HIDS_get_H1_rh(WE_sensorInterface_t * sensorInterface)1181 static int8_t HIDS_get_H1_rh(WE_sensorInterface_t* sensorInterface)
1182 {
1183   uint8_t buffer;
1184 
1185   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_H1_RH_X2, 1, &buffer))
1186   {
1187     return WE_FAIL;
1188   }
1189 
1190   hidsCalibrationData.H1_rh = buffer >> 1;
1191 
1192   return WE_SUCCESS;
1193 }
1194 
1195 /**
1196  * @brief Read T0_OUT (calibration data) and store the value in hidsCalibrationData.
1197  * @param[in] sensorInterface Pointer to sensor interface
1198  * @return Error code
1199  */
HIDS_get_T0_OUT(WE_sensorInterface_t * sensorInterface)1200 static int8_t HIDS_get_T0_OUT(WE_sensorInterface_t* sensorInterface)
1201 {
1202   uint8_t buffer;
1203   int16_t temp;
1204 
1205   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_T0_OUT_H, 1, &buffer))
1206   {
1207     return WE_FAIL;
1208   }
1209 
1210   temp = (((int16_t) buffer) << 8);
1211 
1212   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_T0_OUT_L, 1, &buffer))
1213   {
1214     return WE_FAIL;
1215   }
1216 
1217   hidsCalibrationData.T0_out = temp | buffer;
1218 
1219   return WE_SUCCESS;
1220 }
1221 
1222 /**
1223  * @brief Read T1_OUT (calibration data) and store the value in hidsCalibrationData.
1224  * @param[in] sensorInterface Pointer to sensor interface
1225  * @return Error code
1226  */
HIDS_get_T1_OUT(WE_sensorInterface_t * sensorInterface)1227 static int8_t HIDS_get_T1_OUT(WE_sensorInterface_t* sensorInterface)
1228 {
1229   uint8_t buffer;
1230   int16_t temp;
1231 
1232   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_T1_OUT_H, 1, &buffer))
1233   {
1234     return WE_FAIL;
1235   }
1236 
1237   temp = (((int16_t) buffer) << 8);
1238 
1239   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_T1_OUT_L, 1, &buffer))
1240   {
1241     return WE_FAIL;
1242   }
1243 
1244   hidsCalibrationData.T1_out = temp | buffer;
1245 
1246   return WE_SUCCESS;
1247 }
1248 
1249 
1250 /**
1251  * @brief Read T0_degC (calibration data) and store the value in hidsCalibrationData.
1252  * @param[in] sensorInterface Pointer to sensor interface
1253  * @return Error code
1254  */
HIDS_get_T0_degC(WE_sensorInterface_t * sensorInterface)1255 static int8_t HIDS_get_T0_degC(WE_sensorInterface_t* sensorInterface)
1256 {
1257   uint16_t T0_degC_x8_u16;
1258   uint8_t lsb, msb;
1259 
1260   /* Temperature calibration data for T0 and T1 - 2 MSBs for T0 and T1, where [0+1] = T0 MSBs and [2+3] = T1 MSBs */
1261   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_T0_T1_DEGC_H2, 1, &msb))
1262   {
1263     return WE_FAIL;
1264   }
1265 
1266   /* Get LSBs for T0 */
1267   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_T0_DEGC_X8, 1, &lsb))
1268   {
1269     return WE_FAIL;
1270   }
1271 
1272   /* Calc T0 using 8 LSBs + 2 MSBs */
1273   T0_degC_x8_u16 = (((uint16_t) (msb & 0x03)) << 8) | ((uint16_t) lsb);
1274 
1275   // Divide by 8 (=3LSBs)
1276   hidsCalibrationData.T0_degC = T0_degC_x8_u16 >> 3;
1277 
1278 
1279   return WE_SUCCESS;
1280 }
1281 
1282 /**
1283  * @brief Read T1_degC (calibration data) and store the value in hidsCalibrationData.
1284  * @param[in] sensorInterface Pointer to sensor interface
1285  * @return Error code
1286  */
HIDS_get_T1_degC(WE_sensorInterface_t * sensorInterface)1287 static int8_t HIDS_get_T1_degC(WE_sensorInterface_t* sensorInterface)
1288 {
1289   uint16_t T1_degC_x8_u16;
1290   uint8_t lsb, msb;
1291 
1292   /* Temperature calibration data for T0 and T1 - 2 MSBs for T0 and T1, where [0+1] = T0 MSBs and [2+3] = T1 MSBs */
1293   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_T0_T1_DEGC_H2, 1, &msb))
1294   {
1295     return WE_FAIL;
1296   }
1297 
1298   /* Get LSBs for T1 */
1299 
1300   if (WE_FAIL == HIDS_ReadReg(sensorInterface, HIDS_T1_DEGC_X8, 1, &lsb))
1301   {
1302     return WE_FAIL;
1303   }
1304 
1305   /* Calc T1 using 8 LSBs + 2 MSBs */
1306   T1_degC_x8_u16 = (((uint16_t) (msb & 0x0C)) << 6) | ((uint16_t) lsb);
1307 
1308   // Divide by 8 (=3LSBs)
1309   hidsCalibrationData.T1_degC = T1_degC_x8_u16 >> 3;
1310 
1311   return WE_SUCCESS;
1312 }
1313