LibDriver SHT4X
Loading...
Searching...
No Matches
driver_sht4x.c
Go to the documentation of this file.
1
36
37#include "driver_sht4x.h"
38
42#define CHIP_NAME "Sensirion SHT4X"
43#define MANUFACTURER_NAME "Sensirion"
44#define SUPPLY_VOLTAGE_MIN 1.08f
45#define SUPPLY_VOLTAGE_MAX 3.6f
46#define MAX_CURRENT 100.0f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 125.0f
49#define DRIVER_VERSION 1000
50
54#define SHT4X_COMMAND_SOFT_RESET 0x94
55#define SHT4X_COMMAND_READ_SERIAL_NUMBER 0x89
56
69static uint8_t a_sht4x_write_read(sht4x_handle_t *handle, uint8_t cmd, uint16_t delay, uint8_t *data, uint16_t len)
70{
71 if (handle->iic_write_cmd(handle->iic_addr, &cmd, 1) != 0) /* write command */
72 {
73 return 1; /* return error */
74 }
75 if (delay != 0) /* if not 0 */
76 {
77 handle->delay_ms(delay); /* delay */
78 }
79 if (len != 0) /* check length */
80 {
81 if (handle->iic_read_cmd(handle->iic_addr, data, len) != 0) /* read data */
82 {
83 return 1; /* return error */
84 }
85 }
86
87 return 0; /* success return 0 */
88}
89
97static uint8_t a_sht4x_crc(uint8_t *data, uint16_t len)
98{
99 const uint8_t POLYNOMIAL = 0x31;
100 uint8_t crc = 0xFF;
101 uint16_t i;
102 uint16_t j;
103
104 for (j = len; j != 0; --j) /* length-- */
105 {
106 crc ^= *data++; /* xor */
107 for (i = 8; i != 0; --i) /* 8 times */
108 {
109 crc = (crc & 0x80) ? (crc << 1) ^ POLYNOMIAL : (crc<<1); /* calculate crc */
110 }
111 }
112
113 return crc; /* return crc */
114}
115
128{
129 uint8_t res;
130
131 if (handle == NULL) /* check handle */
132 {
133 return 2; /* return error */
134 }
135 if (handle->debug_print == NULL) /* check debug_print */
136 {
137 return 3; /* return error */
138 }
139 if (handle->iic_init == NULL) /* check iic_init */
140 {
141 handle->debug_print("sht4x: iic_init is null.\n"); /* iic_init is null */
142
143 return 3; /* return error */
144 }
145 if (handle->iic_deinit == NULL) /* check iic_deinit */
146 {
147 handle->debug_print("sht4x: iic_deinit is null.\n"); /* iic_deinit is null */
148
149 return 3; /* return error */
150 }
151 if (handle->iic_read_cmd == NULL) /* check iic_read_cmd */
152 {
153 handle->debug_print("sht4x: iic_read_cmd is null.\n"); /* iic_read_cmd is null */
154
155 return 3; /* return error */
156 }
157 if (handle->iic_write_cmd == NULL) /* check iic_write_cmd */
158 {
159 handle->debug_print("sht4x: iic_write_cmd is null.\n"); /* iic_write_cmd is null */
160
161 return 3; /* return error */
162 }
163 if (handle->delay_ms == NULL) /* check delay_ms */
164 {
165 handle->debug_print("sht4x: delay_ms is null.\n"); /* delay_ms is null */
166
167 return 3; /* return error */
168 }
169
170 if (handle->iic_init() != 0) /* iic init */
171 {
172 handle->debug_print("sht4x: iic init failed.\n"); /* iic init failed */
173
174 return 1; /* return error */
175 }
176 res = a_sht4x_write_read(handle, SHT4X_COMMAND_SOFT_RESET,
177 10, NULL, 0); /* soft reset */
178 if (res != 0) /* check result */
179 {
180 handle->debug_print("sht4x: write command failed.\n"); /* write command failed */
181 (void)handle->iic_deinit(); /* close iic */
182
183 return 4; /* return error */
184 }
185 handle->inited = 1; /* flag finish initialization */
186
187 return 0; /* success return 0 */
188}
189
202{
203 uint8_t res;
204
205 if (handle == NULL) /* check handle */
206 {
207 return 2; /* return error */
208 }
209 if (handle->inited != 1) /* check handle initialization */
210 {
211 return 3; /* return error */
212 }
213
214 res = a_sht4x_write_read(handle, SHT4X_COMMAND_SOFT_RESET,
215 10, NULL, 0); /* soft reset */
216 if (res != 0) /* check result */
217 {
218 handle->debug_print("sht4x: write command failed.\n"); /* write command failed */
219
220 return 4; /* return error */
221 }
222 if (handle->iic_deinit() != 0) /* iic deinit */
223 {
224 handle->debug_print("sht4x: iic deinit failed.\n"); /* iic deinit failed */
225
226 return 1; /* return error */
227 }
228 handle->inited = 0; /* flag close */
229
230 return 0; /* success return 0 */
231}
232
244{
245 if (handle == NULL) /* check handle */
246 {
247 return 2; /* return error */
248 }
249
250 handle->iic_addr = (uint8_t)addr; /* set address */
251
252 return 0; /* success return 0 */
253}
254
266{
267 if (handle == NULL) /* check handle */
268 {
269 return 2; /* return error */
270 }
271
272 *addr = (sht4x_address_t)(handle->iic_addr); /* get address */
273
274 return 0; /* success return 0 */
275}
276
294 uint16_t *temperature_raw, float *temperature_s,
295 uint16_t *humidity_raw, float *humidity_s)
296{
297 uint8_t res;
298 uint8_t buf[6];
299
300 if (handle == NULL) /* check handle */
301 {
302 return 2; /* return error */
303 }
304 if (handle->inited != 1) /* check handle initialization */
305 {
306 return 3; /* return error */
307 }
308
309 switch (mode) /* select mode */
310 {
311 case SHT4X_MODE_HIGH_PRECISION_WITH_NO_HEATER : /* measure T & RH with high precision */
312 {
313 res = a_sht4x_write_read(handle,
315 10, buf, 6); /* read data */
316 if (res != 0) /* check result */
317 {
318 handle->debug_print("sht4x: write command failed.\n"); /* write command failed */
319
320 return 1; /* return error */
321 }
322
323 break; /* break */
324 }
325 case SHT4X_MODE_MEDIUM_PRECISION_WITH_NO_HEATER : /* measure T & RH with medium precision */
326 {
327 res = a_sht4x_write_read(handle,
329 5, buf, 6); /* read data */
330 if (res != 0) /* check result */
331 {
332 handle->debug_print("sht4x: write command failed.\n"); /* write command failed */
333
334 return 1; /* return error */
335 }
336
337 break; /* break */
338 }
339 case SHT4X_MODE_LOWEST_PRECISION_WITH_NO_HEATER : /* measure T & RH with the lowest precision */
340 {
341 res = a_sht4x_write_read(handle,
343 2, buf, 6); /* read data */
344 if (res != 0) /* check result */
345 {
346 handle->debug_print("sht4x: write command failed.\n"); /* write command failed */
347
348 return 1; /* return error */
349 }
350
351 break; /* break */
352 }
353 case SHT4X_MODE_HIGH_PRECISION_WITH_HEATER_200MW_1S : /* activate heater with 200mW for 1s, high precision */
354 {
355 res = a_sht4x_write_read(handle,
357 1100, buf, 6); /* read data */
358 if (res != 0) /* check result */
359 {
360 handle->debug_print("sht4x: write command failed.\n"); /* write command failed */
361
362 return 1; /* return error */
363 }
364
365 break; /* break */
366 }
367 case SHT4X_MODE_HIGH_PRECISION_WITH_HEATER_200MW_0P1S : /* activate heater with 200mW for 0.1s, high precision */
368 {
369 res = a_sht4x_write_read(handle,
371 110, buf, 6); /* read data */
372 if (res != 0) /* check result */
373 {
374 handle->debug_print("sht4x: write command failed.\n"); /* write command failed */
375
376 return 1; /* return error */
377 }
378
379 break; /* break */
380 }
381 case SHT4X_MODE_HIGH_PRECISION_WITH_HEATER_110MW_1S : /* activate heater with 110mW for 1s, high precision */
382 {
383 res = a_sht4x_write_read(handle,
385 1100, buf, 6); /* read data */
386 if (res != 0) /* check result */
387 {
388 handle->debug_print("sht4x: write command failed.\n"); /* write command failed */
389
390 return 1; /* return error */
391 }
392
393 break; /* break */
394 }
395 case SHT4X_MODE_HIGH_PRECISION_WITH_HEATER_110MW_0P1S : /* activate heater with 110mW for 0.1s, high precision */
396 {
397 res = a_sht4x_write_read(handle,
399 110, buf, 6); /* read data */
400 if (res != 0) /* check result */
401 {
402 handle->debug_print("sht4x: write command failed.\n"); /* write command failed */
403
404 return 1; /* return error */
405 }
406
407 break; /* break */
408 }
409 case SHT4X_MODE_HIGH_PRECISION_WITH_HEATER_20MW_1S : /* activate heater with 20mW for 1s, high precision */
410 {
411 res = a_sht4x_write_read(handle,
413 1100, buf, 6); /* read data */
414 if (res != 0) /* check result */
415 {
416 handle->debug_print("sht4x: write command failed.\n"); /* write command failed */
417
418 return 1; /* return error */
419 }
420
421 break; /* break */
422 }
423 case SHT4X_MODE_HIGH_PRECISION_WITH_HEATER_20MW_0P1S : /* activate heater with 20mW for 0.1s, high precision */
424 {
425 res = a_sht4x_write_read(handle,
427 110, buf, 6); /* read data */
428 if (res != 0) /* check result */
429 {
430 handle->debug_print("sht4x: write command failed.\n"); /* write command failed */
431
432 return 1; /* return error */
433 }
434
435 break; /* break */
436 }
437 default : /* invalid */
438 {
439 memset(buf, 0, sizeof(uint8_t) * 6); /* clear buffer */
440
441 break; /* break */
442 }
443 }
444 if (a_sht4x_crc(buf + 0, 2) != buf[2]) /* check crc */
445 {
446 handle->debug_print("sht4x: crc is error.\n"); /* crc is error */
447
448 return 4; /* return error */
449 }
450 if (a_sht4x_crc(buf + 3, 2) != buf[5]) /* check crc */
451 {
452 handle->debug_print("sht4x: crc is error.\n"); /* crc is error */
453
454 return 4; /* return error */
455 }
456 *temperature_raw = (uint16_t)((((uint16_t)buf[0]) << 8) | buf[1]); /* get raw temperature */
457 *humidity_raw = (uint16_t)((((uint16_t)buf[3]) << 8) | buf[4]); /* get raw humidity */
458 *temperature_s = (float)(*temperature_raw) / 65535.0f * 175.0f - 45.0f; /* convert raw temperature */
459 *humidity_s = (((float)(*humidity_raw) / 65535.0f) * 125.0f - 6.0f); /* convert raw humidity */
460 if ((*humidity_s) > 100.0f) /* check humidity range */
461 {
462 *humidity_s = 100.0f; /* max humidity is 100% */
463 }
464 if ((*humidity_s) < 0.0f) /* check humidity range */
465 {
466 *humidity_s = 0.0f; /* min humidity is 0% */
467 }
468
469 return 0; /* success return 0 */
470}
471
484uint8_t sht4x_get_serial_number(sht4x_handle_t *handle, uint8_t num[4])
485{
486 uint8_t res;
487 uint8_t buf[6];
488
489 if (handle == NULL) /* check handle */
490 {
491 return 2; /* return error */
492 }
493 if (handle->inited != 1) /* check handle initialization */
494 {
495 return 3; /* return error */
496 }
497
498 res = a_sht4x_write_read(handle, SHT4X_COMMAND_READ_SERIAL_NUMBER,
499 10, buf, 6); /* read serial number */
500 if (res != 0) /* check result */
501 {
502 handle->debug_print("sht4x: write command failed.\n"); /* write command failed */
503
504 return 1; /* return error */
505 }
506
507 if (a_sht4x_crc(buf + 0, 2) != buf[2]) /* check crc */
508 {
509 handle->debug_print("sht4x: crc is error.\n"); /* crc is error */
510
511 return 4; /* return error */
512 }
513 if (a_sht4x_crc(buf + 3, 2) != buf[5]) /* check crc */
514 {
515 handle->debug_print("sht4x: crc is error.\n"); /* crc is error */
516
517 return 4; /* return error */
518 }
519 num[0] = buf[0]; /* set number 0 */
520 num[1] = buf[1]; /* set number 1 */
521 num[2] = buf[3]; /* set number 2 */
522 num[3] = buf[4]; /* set number 3 */
523
524 return 0; /* success return 0 */
525}
526
538{
539 uint8_t res;
540
541 if (handle == NULL) /* check handle */
542 {
543 return 2; /* return error */
544 }
545 if (handle->inited != 1) /* check handle initialization */
546 {
547 return 3; /* return error */
548 }
549
550 res = a_sht4x_write_read(handle, SHT4X_COMMAND_SOFT_RESET,
551 10, NULL, 0); /* soft reset */
552 if (res != 0) /* check result */
553 {
554 handle->debug_print("sht4x: write command failed.\n"); /* write command failed */
555
556 return 1; /* return error */
557 }
558
559 return 0; /* success return 0 */
560}
561
574uint8_t sht4x_write_read(sht4x_handle_t *handle, uint8_t cmd, uint16_t delay_ms, uint8_t *data, uint16_t len)
575{
576 if (handle == NULL) /* check handle */
577 {
578 return 2; /* return error */
579 }
580 if (handle->inited != 1) /* check handle initialization */
581 {
582 return 3; /* return error */
583 }
584
585 return a_sht4x_write_read(handle, cmd, delay_ms, data, len); /* write and read */
586}
587
597{
598 if (info == NULL) /* check handle */
599 {
600 return 2; /* return error */
601 }
602
603 memset(info, 0, sizeof(sht4x_info_t)); /* initialize sht4x info structure */
604 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
605 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
606 strncpy(info->interface, "IIC", 8); /* copy interface name */
607 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
608 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
609 info->max_current_ma = MAX_CURRENT; /* set maximum current */
610 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
611 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
612 info->driver_version = DRIVER_VERSION; /* set driver version */
613
614 return 0; /* success return 0 */
615}
#define MAX_CURRENT
#define SUPPLY_VOLTAGE_MAX
#define SHT4X_COMMAND_SOFT_RESET
chip command definition
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define SHT4X_COMMAND_READ_SERIAL_NUMBER
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
driver sht4x header file
uint8_t sht4x_read(sht4x_handle_t *handle, sht4x_mode_t mode, uint16_t *temperature_raw, float *temperature_s, uint16_t *humidity_raw, float *humidity_s)
read temperature and humidity
struct sht4x_handle_s sht4x_handle_t
sht4x handle structure definition
uint8_t sht4x_get_serial_number(sht4x_handle_t *handle, uint8_t num[4])
get serial number
sht4x_mode_t
sht4x mode enumeration definition
struct sht4x_info_s sht4x_info_t
sht4x information structure definition
sht4x_address_t
sht4x address enumeration definition
uint8_t sht4x_set_addr(sht4x_handle_t *handle, sht4x_address_t addr)
set the iic address
uint8_t sht4x_get_addr(sht4x_handle_t *handle, sht4x_address_t *addr)
get the iic address
uint8_t sht4x_init(sht4x_handle_t *handle)
initialize the chip
uint8_t sht4x_soft_reset(sht4x_handle_t *handle)
soft reset the chip
uint8_t sht4x_info(sht4x_info_t *info)
get chip's information
uint8_t sht4x_deinit(sht4x_handle_t *handle)
close the chip
@ SHT4X_MODE_HIGH_PRECISION_WITH_NO_HEATER
@ SHT4X_MODE_HIGH_PRECISION_WITH_HEATER_110MW_1S
@ SHT4X_MODE_MEDIUM_PRECISION_WITH_NO_HEATER
@ SHT4X_MODE_HIGH_PRECISION_WITH_HEATER_20MW_0P1S
@ SHT4X_MODE_HIGH_PRECISION_WITH_HEATER_200MW_1S
@ SHT4X_MODE_HIGH_PRECISION_WITH_HEATER_200MW_0P1S
@ SHT4X_MODE_LOWEST_PRECISION_WITH_NO_HEATER
@ SHT4X_MODE_HIGH_PRECISION_WITH_HEATER_110MW_0P1S
@ SHT4X_MODE_HIGH_PRECISION_WITH_HEATER_20MW_1S
uint8_t sht4x_write_read(sht4x_handle_t *handle, uint8_t cmd, uint16_t delay_ms, uint8_t *data, uint16_t len)
write and read bytes
void(* delay_ms)(uint32_t ms)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
uint8_t(* iic_read_cmd)(uint8_t addr, uint8_t *buf, uint16_t len)
uint8_t(* iic_deinit)(void)
uint8_t(* iic_write_cmd)(uint8_t addr, uint8_t *buf, uint16_t len)
float temperature_max
float supply_voltage_max_v
uint32_t driver_version
float temperature_min
float max_current_ma
char manufacturer_name[32]
float supply_voltage_min_v
char interface[8]
char chip_name[32]