LibDriver AGS02MA
Loading...
Searching...
No Matches
driver_ags02ma.c
Go to the documentation of this file.
1
36
37#include "driver_ags02ma.h"
38
42#define CHIP_NAME "ASAIR AGS02MA"
43#define MANUFACTURER_NAME "ASAIR"
44#define SUPPLY_VOLTAGE_MIN 3.3f
45#define SUPPLY_VOLTAGE_MAX 5.5f
46#define MAX_CURRENT 33.0f
47#define TEMPERATURE_MIN 0.0f
48#define TEMPERATURE_MAX 50.0f
49#define DRIVER_VERSION 1000
50
54#define AGS02MA_ADDRESS (0x1A << 1)
55
59#define AGS02MA_REG_DATA 0x00
60#define AGS02MA_REG_CALIBRATION 0x01
61#define AGS02MA_REG_VERSION 0x11
62#define AGS02MA_REG_RESISTANCE 0x20
63#define AGS02MA_REG_SLAVE_ADDR 0x21
64
76static uint8_t a_ags02ma_iic_read(ags02ma_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_ags02ma_iic_write(ags02ma_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_ags02ma_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("ags02ma: 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("ags02ma: 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("ags02ma: 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("ags02ma: 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("ags02ma: 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("ags02ma: 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("ags02ma: iic init failed.\n"); /* iic init failed */
204
205 return 1; /* return error */
206 }
207 handle->iic_addr = AGS02MA_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("ags02ma: 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 ags02ma_set_slave_address(ags02ma_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 ags02ma_get_slave_address(ags02ma_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 ags02ma_read_tvoc(ags02ma_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_ags02ma_iic_read(handle, AGS02MA_REG_DATA, buf, 5); /* read tvoc */
326 if (res != 0) /* check the result */
327 {
328 handle->debug_print("ags02ma: read tvoc failed.\n"); /* read tvoc failed */
329
330 return 1; /* return error */
331 }
332 if (a_ags02ma_calc_crc(buf, 4) != buf[4]) /* check the crc */
333 {
334 handle->debug_print("ags02ma: 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("ags02ma: 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
363{
364 uint8_t res;
365 uint8_t buf[5];
366
367 if (handle == NULL) /* check handle */
368 {
369 return 2; /* return error */
370 }
371 if (handle->inited != 1) /* check handle initialization */
372 {
373 return 3; /* return error */
374 }
375
376 buf[0] = 0x00; /* set 0x00 */
377 buf[1] = 0x0C; /* set 0x0C */
378 buf[2] = 0xFF; /* set 0xFF */
379 buf[3] = 0xF3; /* set 0xF3 */
380 buf[4] = a_ags02ma_calc_crc(buf, 4); /* get crc */
381 res = a_ags02ma_iic_write(handle, AGS02MA_REG_CALIBRATION, buf, 5); /* write calibration */
382 if (res != 0) /* check the result */
383 {
384 handle->debug_print("ags02ma: write calibration failed.\n"); /* write calibration failed */
385
386 return 1; /* return error */
387 }
388 handle->delay_ms(30); /* delay 30ms */
389
390 return 0; /* success return 0 */
391}
392
406uint8_t ags02ma_get_resistance(ags02ma_handle_t *handle, uint32_t *raw, double *ohm)
407{
408 uint8_t res;
409 uint8_t buf[5];
410
411 if (handle == NULL) /* check handle */
412 {
413 return 2; /* return error */
414 }
415 if (handle->inited != 1) /* check handle initialization */
416 {
417 return 3; /* return error */
418 }
419
420 res = a_ags02ma_iic_read(handle, AGS02MA_REG_RESISTANCE, buf, 5); /* read resistance */
421 if (res != 0) /* check the result */
422 {
423 handle->debug_print("ags02ma: read resistance failed.\n"); /* read resistance failed */
424
425 return 1; /* return error */
426 }
427 if (a_ags02ma_calc_crc(buf, 4) != buf[4]) /* check the crc */
428 {
429 handle->debug_print("ags02ma: crc is error.\n"); /* crc is error */
430
431 return 4; /* return error */
432 }
433 *raw = (uint32_t)(buf[0]) << 24 |
434 (uint32_t)(buf[1]) << 16 |
435 (uint32_t)(buf[2]) << 8 |
436 (uint32_t)(buf[3]) << 0; /* set raw data */
437 *ohm = (double)(*raw) * 100.0; /* convert to ohm */
438
439 return 0; /* success return 0 */
440}
441
454uint8_t ags02ma_get_version(ags02ma_handle_t *handle, uint8_t *version)
455{
456 uint8_t res;
457 uint8_t buf[5];
458
459 if (handle == NULL) /* check handle */
460 {
461 return 2; /* return error */
462 }
463 if (handle->inited != 1) /* check handle initialization */
464 {
465 return 3; /* return error */
466 }
467
468 res = a_ags02ma_iic_read(handle, AGS02MA_REG_VERSION, buf, 5); /* read version */
469 if (res != 0) /* check the result */
470 {
471 handle->debug_print("ags02ma: read version failed.\n"); /* read version failed */
472
473 return 1; /* return error */
474 }
475 if (a_ags02ma_calc_crc(buf, 4) != buf[4]) /* check the crc */
476 {
477 handle->debug_print("ags02ma: crc is error.\n"); /* crc is error */
478
479 return 4; /* return error */
480 }
481 *version = buf[3]; /* set version */
482 handle->delay_ms(30); /* delay 30ms */
483
484 return 0; /* success return 0 */
485}
486
498uint8_t ags02ma_modify_slave_address(ags02ma_handle_t *handle, uint8_t addr_7bit)
499{
500 uint8_t res;
501 uint8_t buf[5];
502
503 if (handle == NULL) /* check handle */
504 {
505 return 2; /* return error */
506 }
507 if (handle->inited != 1) /* check handle initialization */
508 {
509 return 3; /* return error */
510 }
511
512 buf[0] = addr_7bit; /* new addr */
513 buf[1] = ~addr_7bit; /* rev new addr */
514 buf[2] = addr_7bit; /* new addr */
515 buf[3] = ~addr_7bit; /* rev new addr */
516 buf[4] = a_ags02ma_calc_crc(buf, 4); /* get crc */
517 res = a_ags02ma_iic_write(handle, AGS02MA_REG_SLAVE_ADDR, buf, 5); /* modify slave address */
518 if (res != 0) /* check the result */
519 {
520 handle->debug_print("ags02ma: modify slave address failed.\n"); /* modify slave address failed */
521
522 return 1; /* return error */
523 }
524 handle->iic_addr = addr_7bit << 1; /* set new address */
525 handle->delay_ms(30); /* delay 30ms */
526
527 return 0; /* success return 0 */
528}
529
543uint8_t ags02ma_set_reg(ags02ma_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
544{
545 if (handle == NULL) /* check handle */
546 {
547 return 2; /* return error */
548 }
549 if (handle->inited != 1) /* check handle initialization */
550 {
551 return 3; /* return error */
552 }
553
554 if (a_ags02ma_iic_write(handle, reg, buf, len) != 0) /* write data */
555 {
556 return 1; /* return error */
557 }
558
559 return 0; /* success return 0 */
560}
561
575uint8_t ags02ma_get_reg(ags02ma_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
576{
577 if (handle == NULL) /* check handle */
578 {
579 return 2; /* return error */
580 }
581 if (handle->inited != 1) /* check handle initialization */
582 {
583 return 3; /* return error */
584 }
585
586 if (a_ags02ma_iic_read(handle, reg, buf, len) != 0) /* read data */
587 {
588 return 1; /* return error */
589 }
590
591 return 0; /* success return 0 */
592}
593
603{
604 if (info == NULL) /* check handle */
605 {
606 return 2; /* return error */
607 }
608
609 memset(info, 0, sizeof(ags02ma_info_t)); /* initialize ags02ma info structure */
610 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
611 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
612 strncpy(info->interface, "IIC", 8); /* copy interface name */
613 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
614 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
615 info->max_current_ma = MAX_CURRENT; /* set maximum current */
616 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
617 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
618 info->driver_version = DRIVER_VERSION; /* set driver version */
619
620 return 0; /* success return 0 */
621}
#define MAX_CURRENT
#define AGS02MA_REG_VERSION
#define AGS02MA_REG_DATA
chip register definition
#define SUPPLY_VOLTAGE_MAX
#define AGS02MA_REG_RESISTANCE
#define TEMPERATURE_MAX
#define AGS02MA_ADDRESS
chip address definition
#define AGS02MA_REG_SLAVE_ADDR
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define AGS02MA_REG_CALIBRATION
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
driver ags02ma header file
uint8_t ags02ma_info(ags02ma_info_t *info)
get chip's information
uint8_t ags02ma_set_slave_address(ags02ma_handle_t *handle, uint8_t addr)
set slave address
uint8_t ags02ma_modify_slave_address(ags02ma_handle_t *handle, uint8_t addr_7bit)
modify slave address
uint8_t ags02ma_get_version(ags02ma_handle_t *handle, uint8_t *version)
get version
uint8_t ags02ma_read_tvoc(ags02ma_handle_t *handle, uint32_t *raw, uint32_t *ppb)
read tvoc
uint8_t ags02ma_get_slave_address(ags02ma_handle_t *handle, uint8_t *addr)
get slave address
struct ags02ma_info_s ags02ma_info_t
ags02ma information structure definition
uint8_t ags02ma_deinit(ags02ma_handle_t *handle)
close the chip
uint8_t ags02ma_get_resistance(ags02ma_handle_t *handle, uint32_t *raw, double *ohm)
get resistance
uint8_t ags02ma_init(ags02ma_handle_t *handle)
initialize the chip
uint8_t ags02ma_zero_point_calibration(ags02ma_handle_t *handle)
zero point calibration
struct ags02ma_handle_s ags02ma_handle_t
ags02ma handle structure definition
uint8_t ags02ma_set_reg(ags02ma_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
set the chip register
uint8_t ags02ma_get_reg(ags02ma_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 supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v