LibDriver AGS10
Loading...
Searching...
No Matches
driver_ags10.c
Go to the documentation of this file.
1
36
37#include "driver_ags10.h"
38
42#define CHIP_NAME "ASAIR AGS10"
43#define MANUFACTURER_NAME "ASAIR"
44#define SUPPLY_VOLTAGE_MIN 2.9f
45#define SUPPLY_VOLTAGE_MAX 3.1f
46#define MAX_CURRENT 25.0f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
50
54#define AGS10_ADDRESS (0x1A << 1)
55
59#define AGS10_REG_DATA 0x00
60#define AGS10_REG_CALIBRATION 0x01
61#define AGS10_REG_VERSION 0x11
62#define AGS10_REG_RESISTANCE 0x20
63#define AGS10_REG_SLAVE_ADDR 0x21
64
76static uint8_t a_ags10_iic_read(ags10_handle_t *handle, uint8_t reg, uint8_t *data, uint16_t len)
77{
78 if (handle->iic_write_cmd(handle->iic_addr, &reg, 1) != 0) /* write the register */
79 {
80 return 1; /* return error */
81 }
82 handle->delay_ms(30); /* delay 30ms */
83 if (handle->iic_read_cmd(handle->iic_addr, data, len) != 0) /* read the register */
84 {
85 return 1; /* return error */
86 }
87
88 return 0; /* success return 0 */
89}
90
102static uint8_t a_ags10_iic_write(ags10_handle_t *handle, uint8_t reg, uint8_t *data, uint16_t len)
103{
104 if (handle->iic_write(handle->iic_addr, reg, data, len) != 0) /* write the register */
105 {
106 return 1; /* return error */
107 }
108
109 return 0; /* success return 0 */
110}
111
119static uint8_t a_ags10_calc_crc(uint8_t *data, uint8_t len)
120{
121 uint8_t i;
122 uint8_t byte;
123 uint8_t crc = 0xFF;
124
125 for (byte = 0; byte < len; byte++) /* len times */
126 {
127 crc ^= data[byte]; /* xor byte */
128 for (i = 0; i < 8; i++) /* one byte */
129 {
130 if ((crc & 0x80) != 0) /* if high*/
131 {
132 crc = (crc << 1) ^ 0x31; /* xor 0x31 */
133 }
134 else
135 {
136 crc = crc << 1; /* skip */
137 }
138 }
139 }
140
141 return crc; /* return crc */
142}
143
155{
156 if (handle == NULL) /* check handle */
157 {
158 return 2; /* return error */
159 }
160 if (handle->debug_print == NULL) /* check debug_print */
161 {
162 return 3; /* return error */
163 }
164 if (handle->iic_init == NULL) /* check iic_init */
165 {
166 handle->debug_print("ags10: iic_init is null.\n"); /* iic_init is null */
167
168 return 3; /* return error */
169 }
170 if (handle->iic_deinit == NULL) /* check iic_deinit */
171 {
172 handle->debug_print("ags10: iic_deinit is null.\n"); /* iic_deinit is null */
173
174 return 3; /* return error */
175 }
176 if (handle->iic_read_cmd == NULL) /* check iic_read_cmd */
177 {
178 handle->debug_print("ags10: iic_read_cmd is null.\n"); /* iic_read_cmd is null */
179
180 return 3; /* return error */
181 }
182 if (handle->iic_write_cmd == NULL) /* check iic_write_cmd */
183 {
184 handle->debug_print("ags10: iic_write_cmd is null.\n"); /* iic_write_cmd is null */
185
186 return 3; /* return error */
187 }
188 if (handle->iic_write == NULL) /* check iic_write */
189 {
190 handle->debug_print("ags10: iic_write is null.\n"); /* iic_write is null */
191
192 return 3; /* return error */
193 }
194 if (handle->delay_ms == NULL) /* check delay_ms */
195 {
196 handle->debug_print("ags10: delay_ms is null.\n"); /* delay_ms is null */
197
198 return 3; /* return error */
199 }
200
201 if (handle->iic_init() != 0) /* iic init */
202 {
203 handle->debug_print("ags10: iic init failed.\n"); /* iic init failed */
204
205 return 1; /* return error */
206 }
207 handle->iic_addr = AGS10_ADDRESS; /* set default slave address */
208 handle->inited = 1; /* flag finish initialization */
209
210 return 0; /* success return 0 */
211}
212
224{
225 if (handle == NULL) /* check handle */
226 {
227 return 2; /* return error */
228 }
229 if (handle->inited != 1) /* check handle initialization */
230 {
231 return 3; /* return error */
232 }
233
234 if (handle->iic_deinit() != 0) /* iic deinit */
235 {
236 handle->debug_print("ags10: iic deinit failed.\n"); /* iic deinit failed */
237
238 return 1; /* return error */
239 }
240 handle->inited = 0; /* set closed flag */
241
242 return 0; /* success return 0 */
243}
244
255uint8_t ags10_set_slave_address(ags10_handle_t *handle, uint8_t addr)
256{
257 if (handle == NULL) /* check handle */
258 {
259 return 2; /* return error */
260 }
261 if (handle->inited != 1) /* check handle initialization */
262 {
263 return 3; /* return error */
264 }
265
266 handle->iic_addr = (uint8_t)addr; /* set address */
267
268 return 0; /* success return 0 */
269}
270
281uint8_t ags10_get_slave_address(ags10_handle_t *handle, uint8_t *addr)
282{
283 if (handle == NULL) /* check handle */
284 {
285 return 2; /* return error */
286 }
287 if (handle->inited != 1) /* check handle initialization */
288 {
289 return 3; /* return error */
290 }
291
292 *addr = handle->iic_addr ; /* get address */
293
294 return 0; /* success return 0 */
295}
296
311uint8_t ags10_read_tvoc(ags10_handle_t *handle, uint32_t *raw, uint32_t *ppb)
312{
313 uint8_t res;
314 uint8_t buf[5];
315
316 if (handle == NULL) /* check handle */
317 {
318 return 2; /* return error */
319 }
320 if (handle->inited != 1) /* check handle initialization */
321 {
322 return 3; /* return error */
323 }
324
325 res = a_ags10_iic_read(handle, AGS10_REG_DATA, buf, 5); /* read tvoc */
326 if (res != 0) /* check the result */
327 {
328 handle->debug_print("ags10: read tvoc failed.\n"); /* read tvoc failed */
329
330 return 1; /* return error */
331 }
332 if (a_ags10_calc_crc(buf, 4) != buf[4]) /* check the crc */
333 {
334 handle->debug_print("ags10: crc is error.\n"); /* crc is error */
335
336 return 4; /* return error */
337 }
338 if ((buf[0] & 0x01) != 0) /* check status */
339 {
340 handle->debug_print("ags10: chip is busy.\n"); /* chip is busy */
341
342 return 5; /* return error */
343 }
344 *raw = (uint32_t)(buf[1]) << 16 |
345 (uint32_t)(buf[2]) << 8 |
346 (uint32_t)(buf[3]) << 0; /* set raw data */
347 *ppb = *raw; /* copy to ppb */
348
349 return 0; /* success return 0 */
350}
351
363uint8_t ags10_zero_point_calibration(ags10_handle_t *handle, uint16_t raw)
364{
365 uint8_t res;
366 uint8_t buf[5];
367
368 if (handle == NULL) /* check handle */
369 {
370 return 2; /* return error */
371 }
372 if (handle->inited != 1) /* check handle initialization */
373 {
374 return 3; /* return error */
375 }
376
377 buf[0] = 0x00; /* set 0x00 */
378 buf[1] = 0x0C; /* set 0x0C */
379 buf[2] = (raw >> 8) & 0xFF; /* set msb */
380 buf[3] = (raw >> 0) & 0xFF; /* set lsb */
381 buf[4] = a_ags10_calc_crc(buf, 4); /* get crc */
382 res = a_ags10_iic_write(handle, AGS10_REG_CALIBRATION, buf, 5); /* write calibration */
383 if (res != 0) /* check the result */
384 {
385 handle->debug_print("ags10: write calibration failed.\n"); /* write calibration failed */
386
387 return 1; /* return error */
388 }
389 handle->delay_ms(30); /* delay 30ms */
390
391 return 0; /* success return 0 */
392}
393
405{
406 uint8_t res;
407 uint8_t buf[5];
408
409 if (handle == NULL) /* check handle */
410 {
411 return 2; /* return error */
412 }
413 if (handle->inited != 1) /* check handle initialization */
414 {
415 return 3; /* return error */
416 }
417
418 buf[0] = 0x00; /* set 0x00 */
419 buf[1] = 0x0C; /* set 0x0C */
420 buf[2] = 0xFF; /* set 0xFF */
421 buf[3] = 0xFF; /* set 0xFF */
422 buf[4] = a_ags10_calc_crc(buf, 4); /* get crc */
423 res = a_ags10_iic_write(handle, AGS10_REG_CALIBRATION, buf, 5); /* write calibration */
424 if (res != 0) /* check the result */
425 {
426 handle->debug_print("ags10: write calibration failed.\n"); /* write calibration failed */
427
428 return 1; /* return error */
429 }
430 handle->delay_ms(30); /* delay 30ms */
431
432 return 0; /* success return 0 */
433}
434
446{
447 uint8_t res;
448 uint8_t buf[5];
449
450 if (handle == NULL) /* check handle */
451 {
452 return 2; /* return error */
453 }
454 if (handle->inited != 1) /* check handle initialization */
455 {
456 return 3; /* return error */
457 }
458
459 buf[0] = 0x00; /* set 0x00 */
460 buf[1] = 0x0C; /* set 0x0C */
461 buf[2] = 0x00; /* set 0x00 */
462 buf[3] = 0x00; /* set 0x00 */
463 buf[4] = a_ags10_calc_crc(buf, 4); /* get crc */
464 res = a_ags10_iic_write(handle, AGS10_REG_CALIBRATION, buf, 5); /* write calibration */
465 if (res != 0) /* check the result */
466 {
467 handle->debug_print("ags10: write calibration failed.\n"); /* write calibration failed */
468
469 return 1; /* return error */
470 }
471 handle->delay_ms(30); /* delay 30ms */
472
473 return 0; /* success return 0 */
474}
475
489uint8_t ags10_get_resistance(ags10_handle_t *handle, uint32_t *raw, double *ohm)
490{
491 uint8_t res;
492 uint8_t buf[5];
493
494 if (handle == NULL) /* check handle */
495 {
496 return 2; /* return error */
497 }
498 if (handle->inited != 1) /* check handle initialization */
499 {
500 return 3; /* return error */
501 }
502
503 res = a_ags10_iic_read(handle, AGS10_REG_RESISTANCE, buf, 5); /* read resistance */
504 if (res != 0) /* check the result */
505 {
506 handle->debug_print("ags10: read resistance failed.\n"); /* read resistance failed */
507
508 return 1; /* return error */
509 }
510 if (a_ags10_calc_crc(buf, 4) != buf[4]) /* check the crc */
511 {
512 handle->debug_print("ags10: crc is error.\n"); /* crc is error */
513
514 return 4; /* return error */
515 }
516 *raw = (uint32_t)(buf[0]) << 24 |
517 (uint32_t)(buf[1]) << 16 |
518 (uint32_t)(buf[2]) << 8 |
519 (uint32_t)(buf[3]) << 0; /* set raw data */
520 *ohm = (double)(*raw) * 100.0; /* convert to ohm */
521
522 return 0; /* success return 0 */
523}
524
537uint8_t ags10_get_version(ags10_handle_t *handle, uint8_t *version)
538{
539 uint8_t res;
540 uint8_t buf[5];
541
542 if (handle == NULL) /* check handle */
543 {
544 return 2; /* return error */
545 }
546 if (handle->inited != 1) /* check handle initialization */
547 {
548 return 3; /* return error */
549 }
550
551 res = a_ags10_iic_read(handle, AGS10_REG_VERSION, buf, 5); /* read version */
552 if (res != 0) /* check the result */
553 {
554 handle->debug_print("ags10: read version failed.\n"); /* read version failed */
555
556 return 1; /* return error */
557 }
558 if (a_ags10_calc_crc(buf, 4) != buf[4]) /* check the crc */
559 {
560 handle->debug_print("ags10: crc is error.\n"); /* crc is error */
561
562 return 4; /* return error */
563 }
564 *version = buf[3]; /* set version */
565 handle->delay_ms(30); /* delay 30ms */
566
567 return 0; /* success return 0 */
568}
569
581uint8_t ags10_modify_slave_address(ags10_handle_t *handle, uint8_t addr_7bit)
582{
583 uint8_t res;
584 uint8_t buf[5];
585
586 if (handle == NULL) /* check handle */
587 {
588 return 2; /* return error */
589 }
590 if (handle->inited != 1) /* check handle initialization */
591 {
592 return 3; /* return error */
593 }
594
595 buf[0] = addr_7bit; /* new addr */
596 buf[1] = ~addr_7bit; /* rev new addr */
597 buf[2] = addr_7bit; /* new addr */
598 buf[3] = ~addr_7bit; /* rev new addr */
599 buf[4] = a_ags10_calc_crc(buf, 4); /* get crc */
600 res = a_ags10_iic_write(handle, AGS10_REG_SLAVE_ADDR, buf, 5); /* modify slave address */
601 if (res != 0) /* check the result */
602 {
603 handle->debug_print("ags10: modify slave address failed.\n"); /* modify slave address failed */
604
605 return 1; /* return error */
606 }
607 handle->iic_addr = addr_7bit << 1; /* set new address */
608 handle->delay_ms(30); /* delay 30ms */
609
610 return 0; /* success return 0 */
611}
612
626uint8_t ags10_set_reg(ags10_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
627{
628 if (handle == NULL) /* check handle */
629 {
630 return 2; /* return error */
631 }
632 if (handle->inited != 1) /* check handle initialization */
633 {
634 return 3; /* return error */
635 }
636
637 if (a_ags10_iic_write(handle, reg, buf, len) != 0) /* write data */
638 {
639 return 1; /* return error */
640 }
641
642 return 0; /* success return 0 */
643}
644
658uint8_t ags10_get_reg(ags10_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
659{
660 if (handle == NULL) /* check handle */
661 {
662 return 2; /* return error */
663 }
664 if (handle->inited != 1) /* check handle initialization */
665 {
666 return 3; /* return error */
667 }
668
669 if (a_ags10_iic_read(handle, reg, buf, len) != 0) /* read data */
670 {
671 return 1; /* return error */
672 }
673
674 return 0; /* success return 0 */
675}
676
686{
687 if (info == NULL) /* check handle */
688 {
689 return 2; /* return error */
690 }
691
692 memset(info, 0, sizeof(ags10_info_t)); /* initialize ags10 info structure */
693 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
694 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
695 strncpy(info->interface, "IIC", 8); /* copy interface name */
696 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
697 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
698 info->max_current_ma = MAX_CURRENT; /* set maximum current */
699 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
700 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
701 info->driver_version = DRIVER_VERSION; /* set driver version */
702
703 return 0; /* success return 0 */
704}
#define AGS10_REG_DATA
chip register definition
#define MAX_CURRENT
#define SUPPLY_VOLTAGE_MAX
#define AGS10_REG_VERSION
#define TEMPERATURE_MAX
#define AGS10_REG_RESISTANCE
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define AGS10_ADDRESS
chip address definition
#define AGS10_REG_SLAVE_ADDR
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
#define AGS10_REG_CALIBRATION
driver ags10 header file
uint8_t ags10_deinit(ags10_handle_t *handle)
close the chip
uint8_t ags10_info(ags10_info_t *info)
get chip's information
uint8_t ags10_current_resistance_zero_point_calibration(ags10_handle_t *handle)
zero point calibration using current resistance
uint8_t ags10_zero_point_calibration(ags10_handle_t *handle, uint16_t raw)
zero point calibration
struct ags10_info_s ags10_info_t
ags10 information structure definition
uint8_t ags10_get_resistance(ags10_handle_t *handle, uint32_t *raw, double *ohm)
get resistance
uint8_t ags10_get_slave_address(ags10_handle_t *handle, uint8_t *addr)
get slave address
uint8_t ags10_read_tvoc(ags10_handle_t *handle, uint32_t *raw, uint32_t *ppb)
read tvoc
uint8_t ags10_modify_slave_address(ags10_handle_t *handle, uint8_t addr_7bit)
modify slave address
uint8_t ags10_get_version(ags10_handle_t *handle, uint8_t *version)
get version
uint8_t ags10_reset_zero_point_calibration(ags10_handle_t *handle)
reset zero point calibration
struct ags10_handle_s ags10_handle_t
ags10 handle structure definition
uint8_t ags10_set_slave_address(ags10_handle_t *handle, uint8_t addr)
set slave address
uint8_t ags10_init(ags10_handle_t *handle)
initialize the chip
uint8_t ags10_set_reg(ags10_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
set the chip register
uint8_t ags10_get_reg(ags10_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
get the chip register
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_write)(uint8_t addr, uint8_t reg, 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]