LibDriver SHTC3
Loading...
Searching...
No Matches
driver_shtc3.c
Go to the documentation of this file.
1
36
37#include "driver_shtc3.h"
38
42#define CHIP_NAME "Sensirion SHTC3"
43#define MANUFACTURER_NAME "Sensirion"
44#define SUPPLY_VOLTAGE_MIN 1.62f
45#define SUPPLY_VOLTAGE_MAX 3.6f
46#define MAX_CURRENT 0.9f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 125.0f
49#define DRIVER_VERSION 1000
50
54#define SHTC3_ADDRESS (0x70 << 1)
55
59#define SHTC3_COMMAND_READ_ID 0xEFC8U
60#define SHTC3_COMMAND_SOFT_RESET 0x805DU
61#define SHTC3_COMMAND_SLEEP 0xB098U
62#define SHTC3_COMMAND_WAKEUP 0x3517U
63#define SHTC3_COMMAND_MEAS_T_POLLING 0x7866U
64#define SHTC3_COMMAND_MEAS_T_CLOCKSTR 0x7CA2U
65#define SHTC3_COMMAND_MEAS_RH_POLLING 0x58E0U
66#define SHTC3_COMMAND_MEAS_RH_CLOCKSTR 0x5C24U
67#define SHTC3_COMMAND_LP_MEAS_T_POLLING 0x609CU
68#define SHTC3_COMMAND_LP_MEAS_T_CLOCKSTR 0x6458U
69#define SHTC3_COMMAND_LP_MEAS_RH_POLLING 0x401AU
70#define SHTC3_COMMAND_LP_MEAS_RH_CLOCKSTR 0x44DEU
71
81static uint8_t a_shtc3_write(shtc3_handle_t *handle, uint16_t cmd)
82{
83 if (handle->iic_write_address16(SHTC3_ADDRESS, cmd, NULL, 0) != 0) /* iic write */
84 {
85 return 1; /* return error */
86 }
87
88 return 0; /* success return 0 */
89}
90
102static uint8_t a_shtc3_read(shtc3_handle_t *handle, uint16_t reg, uint8_t *data, uint16_t len)
103{
104 if (handle->iic_read_address16(SHTC3_ADDRESS, reg, data, len) != 0) /* iic read */
105 {
106 return 1; /* return error */
107 }
108
109 return 0; /* success return 0 */
110}
111
119static uint8_t a_shtc3_crc(uint8_t *data, uint16_t len)
120{
121 const uint8_t POLYNOMIAL = 0x31;
122 uint8_t crc = 0xFF;
123 uint16_t i, j;
124
125 for (j = len; j != 0; --j) /* length-- */
126 {
127 crc ^= *data++; /* xor */
128 for (i = 8; i != 0; --i) /* 8 times */
129 {
130 crc = (crc & 0x80) ? (crc << 1) ^ POLYNOMIAL : (crc<<1); /* calculate crc */
131 }
132 }
133
134 return crc; /* return crc */
135}
136
150{
151 uint8_t res;
152 uint8_t data[3];
153 uint16_t command;
154
155 if (handle == NULL) /* check handle */
156 {
157 return 2; /* return error */
158 }
159 if (handle->debug_print == NULL) /* check debug_print */
160 {
161 return 3; /* return error */
162 }
163 if (handle->iic_init == NULL) /* check iic_init */
164 {
165 handle->debug_print("shtc3: iic_init is null.\n"); /* iic_init is null */
166
167 return 3; /* return error */
168 }
169 if (handle->iic_deinit == NULL) /* check iic_deinit */
170 {
171 handle->debug_print("shtc3: iic_deinit is null.\n"); /* iic_deinit is null */
172
173 return 3; /* return error */
174 }
175 if (handle->iic_read_address16 == NULL) /* check iic_read_address16 */
176 {
177 handle->debug_print("shtc3: iic_read_address16 is null.\n"); /* iic_read_address16 is null */
178
179 return 3; /* return error */
180 }
181 if (handle->iic_write_address16 == NULL) /* check iic_write_address16 */
182 {
183 handle->debug_print("shtc3: iic_write_address16 is null.\n"); /* iic_write_address16 is null */
184
185 return 3; /* return error */
186 }
187 if (handle->delay_ms == NULL) /* check delay_ms */
188 {
189 handle->debug_print("shtc3: delay_ms is null.\n"); /* delay_ms is null */
190
191 return 3; /* return error */
192 }
193
194 if (handle->iic_init() != 0) /* iic init */
195 {
196 handle->debug_print("shtc3: iic init failed.\n"); /* iic init failed */
197
198 return 1; /* return error */
199 }
200
201 command = SHTC3_COMMAND_SOFT_RESET; /* set command */
202 res = a_shtc3_write(handle, command); /* write command */
203 if (res != 0) /* check result */
204 {
205 handle->debug_print("shtc3: write command failed.\n"); /* write command failed */
206 (void)handle->iic_deinit(); /* iic deinit */
207
208 return 1; /* return error */
209 }
210 handle->delay_ms(1); /* delay 1ms */
211
212 command = SHTC3_COMMAND_READ_ID; /* read id command */
213 res = a_shtc3_read(handle, command, (uint8_t *)data, 3); /* read data */
214 if (res != 0) /* check result */
215 {
216 handle->debug_print("shtc3: read data failed.\n"); /* read data failed */
217 (void)handle->iic_deinit(); /* iic deinit */
218
219 return 4; /* return error */
220 }
221 if (a_shtc3_crc((uint8_t *)data, 2) != data[2]) /* check crc */
222 {
223 handle->debug_print("shtc3: crc check failed.\n"); /* crc check failed */
224 (void)handle->iic_deinit(); /* iic deinit */
225
226 return 4; /* return error */
227 }
228 if (((data[0] & 0x08) != 0x08) || ((data[1] & 0x3F) != 0x7)) /* check id */
229 {
230 handle->debug_print("shtc3: id is invalid.\n"); /* id is invalid */
231 (void)handle->iic_deinit(); /* iic deinit */
232
233 return 5; /* return error */
234 }
235 handle->inited = 1; /* flag finish initialization */
236
237 return 0; /* success return 0 */
238}
239
252{
253 uint8_t res;
254 uint16_t command;
255
256 if (handle == NULL) /* check handle */
257 {
258 return 2; /* return error */
259 }
260 if (handle->inited != 1) /* check handle initialization */
261 {
262 return 3; /* return error */
263 }
264
265 command = SHTC3_COMMAND_SOFT_RESET; /* set command */
266 res = a_shtc3_write(handle, command); /* write command */
267 if (res != 0) /* check result */
268 {
269 handle->debug_print("shtc3: write command failed.\n"); /* write command failed */
270
271 return 4; /* return error */
272 }
273 handle->delay_ms(1); /* delay 1ms */
274
275 if (handle->iic_deinit() != 0) /* iic deinit */
276 {
277 handle->debug_print("shtc3: iic deinit failed.\n"); /* iic deinit failed */
278
279 return 1; /* return error */
280 }
281 handle->inited = 0; /* flag close */
282
283 return 0; /* success return 0 */
284}
285
301uint8_t shtc3_read(shtc3_handle_t *handle, shtc3_bool_t clock_stretching_enable,
302 uint16_t *temperature_raw, float *temperature_s,
303 uint16_t *humidity_raw, float *humidity_s)
304{
305 uint8_t res;
306 uint16_t command;
307 uint8_t data[3];
308
309 if (handle == NULL) /* check handle */
310 {
311 return 2; /* return error */
312 }
313 if (handle->inited != 1) /* check handle initialization */
314 {
315 return 3; /* return error */
316 }
317
318 if (clock_stretching_enable == SHTC3_BOOL_FALSE) /* if disable clock stretching */
319 {
320 command = SHTC3_COMMAND_MEAS_T_POLLING; /* set disable low */
321 }
322 else /* if enable clock stretching */
323 {
324 command = SHTC3_COMMAND_MEAS_T_CLOCKSTR; /* set enable low */
325 }
326 res = a_shtc3_read(handle, command, (uint8_t *)data, 3); /* read data */
327 if (res != 0) /* check result */
328 {
329 handle->debug_print("shtc3: read data failed.\n"); /* read data failed */
330
331 return 1; /* return error */
332 }
333 if (a_shtc3_crc((uint8_t *)data, 2) != data[2]) /* check crc */
334 {
335 handle->debug_print("shtc3: crc check failed.\n"); /* crc check failed */
336
337 return 1; /* return error */
338 }
339 *temperature_raw = (uint16_t)((((uint16_t)data[0]) << 8) | data[1]); /* get raw temperature */
340 *temperature_s = (float)(*temperature_raw) / 65536.0f * 175.0f - 45.0f; /* convert raw temperature */
341
342 if (clock_stretching_enable == SHTC3_BOOL_FALSE) /* if disable clock stretching */
343 {
344 command = SHTC3_COMMAND_MEAS_RH_POLLING; /* set disable low */
345 }
346 else /* if enable clock stretching */
347 {
348 command = SHTC3_COMMAND_MEAS_RH_CLOCKSTR; /* set enable low */
349 }
350 res = a_shtc3_read(handle, command, (uint8_t *)data, 3); /* read data */
351 if (res != 0) /* check result */
352 {
353 handle->debug_print("shtc3: read data failed.\n"); /* read data failed */
354
355 return 1; /* return error */
356 }
357 if (a_shtc3_crc((uint8_t *)data, 2) != data[2]) /* check crc */
358 {
359 handle->debug_print("shtc3: crc check failed.\n"); /* crc check failed */
360
361 return 1; /* return error */
362 }
363 *humidity_raw = (uint16_t)((((uint16_t)data[0]) << 8) | data[1]); /* get raw humidity */
364 *humidity_s = (float)(*humidity_raw) / 65536.0f * 100.0f; /* convert raw humidity */
365
366 return 0; /* success return 0 */
367}
368
384uint8_t shtc3_read_low_power(shtc3_handle_t *handle, shtc3_bool_t clock_stretching_enable,
385 uint16_t *temperature_raw, float *temperature_s,
386 uint16_t *humidity_raw, float *humidity_s)
387{
388 uint8_t res;
389 uint16_t command;
390 uint8_t data[3];
391
392 if (handle == NULL) /* check handle */
393 {
394 return 2; /* return error */
395 }
396 if (handle->inited != 1) /* check handle initialization */
397 {
398 return 3; /* return error */
399 }
400
401 if (clock_stretching_enable == SHTC3_BOOL_FALSE) /* if disable clock stretching */
402 {
403 command = SHTC3_COMMAND_LP_MEAS_T_POLLING; /* set disable low */
404 }
405 else /* if enable clock stretching */
406 {
407 command = SHTC3_COMMAND_LP_MEAS_T_CLOCKSTR; /* set enable low */
408 }
409 res = a_shtc3_read(handle, command, (uint8_t *)data, 3); /* read data */
410 if (res != 0) /* check result */
411 {
412 handle->debug_print("shtc3: read data failed.\n"); /* read data failed */
413
414 return 1; /* return error */
415 }
416 if (a_shtc3_crc((uint8_t *)data, 2) != data[2]) /* check crc */
417 {
418 handle->debug_print("shtc3: crc check failed.\n"); /* crc check failed */
419
420 return 1; /* return error */
421 }
422 *temperature_raw = (uint16_t)((((uint16_t)data[0]) << 8) | data[1]); /* get raw temperature */
423 *temperature_s = (float)(*temperature_raw) / 65536.0f * 175.0f - 45.0f; /* convert raw temperature */
424
425 if (clock_stretching_enable == SHTC3_BOOL_FALSE) /* if disable clock stretching */
426 {
427 command = SHTC3_COMMAND_LP_MEAS_RH_POLLING; /* set disable low */
428 }
429 else /* if enable clock stretching */
430 {
431 command = SHTC3_COMMAND_LP_MEAS_RH_CLOCKSTR; /* set enable low */
432 }
433 res = a_shtc3_read(handle, command, (uint8_t *)data, 3); /* read data */
434 if (res != 0) /* check result */
435 {
436 handle->debug_print("shtc3: read data failed.\n"); /* read data failed */
437
438 return 1; /* return error */
439 }
440 if (a_shtc3_crc((uint8_t *)data, 2) != data[2]) /* check crc */
441 {
442 handle->debug_print("shtc3: crc check failed.\n"); /* crc check failed */
443
444 return 1; /* return error */
445 }
446 *humidity_raw = (uint16_t)((((uint16_t)data[0]) << 8) | data[1]); /* get raw humidity */
447 *humidity_s = (float)(*humidity_raw) / 65536.0f * 100.0f; /* convert raw humidity */
448
449 return 0; /* success return 0 */
450}
451
463{
464 uint8_t res;
465 uint16_t command;
466
467 if (handle == NULL) /* check handle */
468 {
469 return 2; /* return error */
470 }
471 if (handle->inited != 1) /* check handle initialization */
472 {
473 return 3; /* return error */
474 }
475
476 command = SHTC3_COMMAND_SOFT_RESET; /* set command */
477 res = a_shtc3_write(handle, command); /* write command */
478 if (res != 0) /* check result */
479 {
480 handle->debug_print("shtc3: write command failed.\n"); /* write command failed */
481
482 return 1; /* return error */
483 }
484 handle->delay_ms(1); /* delay 1ms */
485
486 return 0; /* success return 0 */
487}
488
500{
501 uint8_t res;
502 uint16_t command;
503
504 if (handle == NULL) /* check handle */
505 {
506 return 2; /* return error */
507 }
508 if (handle->inited != 1) /* check handle initialization */
509 {
510 return 3; /* return error */
511 }
512
513 command = SHTC3_COMMAND_SLEEP; /* set command */
514 res = a_shtc3_write(handle, command); /* write command */
515 if (res != 0) /* check result */
516 {
517 handle->debug_print("shtc3: write command failed.\n"); /* write command failed */
518
519 return 1; /* return error */
520 }
521
522 return 0; /* success return 0 */
523}
524
536{
537 uint8_t res;
538 uint16_t command;
539
540 if (handle == NULL) /* check handle */
541 {
542 return 2; /* return error */
543 }
544 if (handle->inited != 1) /* check handle initialization */
545 {
546 return 3; /* return error */
547 }
548
549 command = SHTC3_COMMAND_WAKEUP; /* set command */
550 res = a_shtc3_write(handle, command); /* write command */
551 if (res != 0) /* check result */
552 {
553 handle->debug_print("shtc3: write command failed.\n"); /* write command failed */
554
555 return 1; /* return error */
556 }
557 handle->delay_ms(1); /* delay 1ms */
558
559 return 0; /* success return 0 */
560}
561
573uint8_t shtc3_set_reg(shtc3_handle_t *handle, uint16_t command)
574{
575 if (handle == NULL) /* check handle */
576 {
577 return 2; /* return error */
578 }
579 if (handle->inited != 1) /* check handle initialization */
580 {
581 return 3; /* return error */
582 }
583
584 return a_shtc3_write(handle, command); /* write command */
585}
586
600uint8_t shtc3_get_reg(shtc3_handle_t *handle, uint16_t command, uint8_t *buf, uint16_t len)
601{
602 if (handle == NULL) /* check handle */
603 {
604 return 2; /* return error */
605 }
606 if (handle->inited != 1) /* check handle initialization */
607 {
608 return 3; /* return error */
609 }
610
611 return a_shtc3_read(handle, command, buf, len); /* read data */
612}
613
623{
624 if (info == NULL) /* check handle */
625 {
626 return 2; /* return error */
627 }
628
629 memset(info, 0, sizeof(shtc3_info_t)); /* initialize shtc3 info structure */
630 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
631 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
632 strncpy(info->interface, "IIC", 8); /* copy interface name */
633 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
634 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
635 info->max_current_ma = MAX_CURRENT; /* set maximum current */
636 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
637 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
638 info->driver_version = DRIVER_VERSION; /* set driver version */
639
640 return 0; /* success return 0 */
641}
#define SHTC3_COMMAND_SOFT_RESET
#define MAX_CURRENT
#define SHTC3_COMMAND_LP_MEAS_RH_POLLING
#define SHTC3_COMMAND_MEAS_T_POLLING
#define SHTC3_COMMAND_LP_MEAS_T_CLOCKSTR
#define SHTC3_COMMAND_READ_ID
chip command definition
#define SHTC3_COMMAND_MEAS_RH_CLOCKSTR
#define SHTC3_COMMAND_MEAS_RH_POLLING
#define SUPPLY_VOLTAGE_MAX
#define SHTC3_ADDRESS
chip address definition
#define TEMPERATURE_MAX
#define SHTC3_COMMAND_MEAS_T_CLOCKSTR
#define SHTC3_COMMAND_SLEEP
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define SHTC3_COMMAND_WAKEUP
#define SHTC3_COMMAND_LP_MEAS_RH_CLOCKSTR
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
#define SHTC3_COMMAND_LP_MEAS_T_POLLING
driver shtc3 header file
shtc3_bool_t
shtc3 bool enumeration definition
uint8_t shtc3_init(shtc3_handle_t *handle)
initialize the chip
uint8_t shtc3_wakeup(shtc3_handle_t *handle)
wakeup
uint8_t shtc3_info(shtc3_info_t *info)
get chip's information
uint8_t shtc3_read(shtc3_handle_t *handle, shtc3_bool_t clock_stretching_enable, uint16_t *temperature_raw, float *temperature_s, uint16_t *humidity_raw, float *humidity_s)
normal read
uint8_t shtc3_deinit(shtc3_handle_t *handle)
close the chip
uint8_t shtc3_soft_reset(shtc3_handle_t *handle)
soft reset the chip
struct shtc3_handle_s shtc3_handle_t
shtc3 handle structure definition
uint8_t shtc3_sleep(shtc3_handle_t *handle)
sleep
uint8_t shtc3_read_low_power(shtc3_handle_t *handle, shtc3_bool_t clock_stretching_enable, uint16_t *temperature_raw, float *temperature_s, uint16_t *humidity_raw, float *humidity_s)
read in low power mode
struct shtc3_info_s shtc3_info_t
shtc3 information structure definition
@ SHTC3_BOOL_FALSE
uint8_t shtc3_set_reg(shtc3_handle_t *handle, uint16_t command)
set the chip register
uint8_t shtc3_get_reg(shtc3_handle_t *handle, uint16_t command, uint8_t *buf, uint16_t len)
get the chip register
void(* delay_ms)(uint32_t ms)
uint8_t(* iic_read_address16)(uint8_t addr, uint16_t reg, uint8_t *buf, uint16_t len)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
uint8_t(* iic_write_address16)(uint8_t addr, uint16_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_deinit)(void)
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]