LibDriver ADS1110
Loading...
Searching...
No Matches
driver_ads1110.c
Go to the documentation of this file.
1
36
37#include "driver_ads1110.h"
38
42#define CHIP_NAME "Texas Instruments ADS1110"
43#define MANUFACTURER_NAME "Texas Instruments"
44#define SUPPLY_VOLTAGE_MIN 2.7f
45#define SUPPLY_VOLTAGE_MAX 5.5f
46#define MAX_CURRENT 0.35f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
50
61static uint8_t a_ads1110_iic_read(ads1110_handle_t *handle, uint8_t *buf, uint16_t len)
62{
63 if (handle->iic_read_cmd(handle->iic_addr, (uint8_t *)buf, len) != 0) /* read data */
64 {
65 return 1; /* return error */
66 }
67
68 return 0; /* success return 0 */
69}
70
81static uint8_t a_ads1110_iic_write(ads1110_handle_t *handle, uint8_t *buf, uint16_t len)
82{
83 if (handle->iic_write_cmd(handle->iic_addr, (uint8_t *)buf, len) != 0) /* write data */
84 {
85 return 1; /* return error */
86 }
87
88 return 0; /* success return 0 */
89}
90
102{
103 uint8_t res;
104 uint8_t buf[3];
105
106 if (handle == NULL) /* check handle */
107 {
108 return 2; /* return error */
109 }
110 if (handle->debug_print == NULL) /* check debug_print */
111 {
112 return 3; /* return error */
113 }
114 if (handle->iic_init == NULL) /* check iic_init */
115 {
116 handle->debug_print("ads1110: iic_init is null.\n"); /* iic_init is null */
117
118 return 3; /* return error */
119 }
120 if (handle->iic_deinit == NULL) /* check iic_deinit */
121 {
122 handle->debug_print("ads1110: iic_deinit is null.\n"); /* iic_deinit is null */
123
124 return 3; /* return error */
125 }
126 if (handle->iic_read_cmd == NULL) /* check iic_read_cmd */
127 {
128 handle->debug_print("ads1110: iic_read_cmd is null.\n"); /* iic_read_cmd is null */
129
130 return 3; /* return error */
131 }
132 if (handle->iic_write_cmd == NULL) /* check iic_write_cmd */
133 {
134 handle->debug_print("ads1110: iic_write_cmd is null.\n"); /* iic_write_cmd is null */
135
136 return 3; /* return error */
137 }
138 if (handle->delay_ms == NULL) /* check delay_ms */
139 {
140 handle->debug_print("ads1110: delay_ms is null.\n"); /* delay_ms is null */
141
142 return 3; /* return error */
143 }
144
145 if (handle->iic_init() != 0) /* iic init */
146 {
147 handle->debug_print("ads1110: iic init failed.\n"); /* iic init failed */
148
149 return 1; /* return error */
150 }
151
152 res = a_ads1110_iic_read(handle, buf, 3); /* read config */
153 if (res != 0) /* check the result */
154 {
155 handle->debug_print("ads1110: read failed.\n"); /* read failed */
156 (void)handle->iic_deinit(); /* iic deinit */
157
158 return 1; /* return error */
159 }
160 buf[2] &= ~(1 << 4); /* clear settings */
161 buf[2] |= (1 << 4); /* set single conversion mode */
162 res = a_ads1110_iic_write(handle, &buf[2], 1); /* write config */
163 if (res != 0) /* check the result */
164 {
165 handle->debug_print("ads1110: write failed.\n"); /* write failed */
166 (void)handle->iic_deinit(); /* iic deinit */
167
168 return 1; /* return error */
169 }
170 handle->inited = 1; /* flag inited */
171
172 return 0; /* success return 0 */
173}
174
187{
188 uint8_t res;
189 uint8_t buf[3];
190
191 if (handle == NULL) /* check handle */
192 {
193 return 2; /* return error */
194 }
195 if (handle->inited != 1) /* check handle initialization */
196 {
197 return 3; /* return error */
198 }
199
200 res = a_ads1110_iic_read(handle, buf, 3); /* read config */
201 if (res != 0) /* check the result */
202 {
203 handle->debug_print("ads1110: power down failed.\n"); /* power down failed */
204
205 return 4; /* return error */
206 }
207 buf[2] &= ~(1 << 4); /* clear settings */
208 buf[2] |= (1 << 4); /* set single conversion mode */
209 res = a_ads1110_iic_write(handle, &buf[2], 1); /* write config */
210 if (res != 0) /* check the result */
211 {
212 handle->debug_print("ads1110: power down failed.\n"); /* power down failed */
213
214 return 4; /* return error */
215 }
216 res = handle->iic_deinit(); /* close iic */
217 if (res != 0) /* check the result */
218 {
219 handle->debug_print("ads1110: iic deinit failed.\n"); /* iic deinit failed */
220
221 return 1; /* return error */
222 }
223 handle->inited = 0; /* flag close */
224
225 return 0; /* success return 0 */
226}
227
240{
241 uint8_t res;
242 uint8_t buf[3];
243
244 if (handle == NULL) /* check handle */
245 {
246 return 2; /* return error */
247 }
248 if (handle->inited != 1) /* check handle initialization */
249 {
250 return 3; /* return error */
251 }
252
253 res = a_ads1110_iic_read(handle, buf, 3); /* read config */
254 if (res != 0) /* check the result */
255 {
256 handle->debug_print("ads1110: read config failed.\n"); /* read config failed */
257
258 return 1; /* return error */
259 }
260 buf[2] &= ~(3 << 2); /* clear settings */
261 buf[2] |= rate << 2; /* set rate */
262 res = a_ads1110_iic_write(handle, &buf[2], 1); /* write config */
263 if (res != 0) /* check the result */
264 {
265 handle->debug_print("ads1110: write config failed.\n"); /* write config failed */
266
267 return 1; /* return error */
268 }
269
270 return 0; /* success return */
271}
272
285{
286 uint8_t res;
287 uint8_t buf[3];
288
289 if (handle == NULL) /* check handle */
290 {
291 return 2; /* return error */
292 }
293 if (handle->inited != 1) /* check handle initialization */
294 {
295 return 3; /* return error */
296 }
297
298 res = a_ads1110_iic_read(handle, buf, 3); /* read config */
299 if (res != 0) /* check the result */
300 {
301 handle->debug_print("ads1110: read config failed.\n"); /* read config failed */
302
303 return 1; /* return error */
304 }
305 *rate = (ads1110_rate_t)((buf[2] >> 2) & 0x03); /* set rate */
306
307 return 0; /* success return 0 */
308}
309
322{
323 uint8_t res;
324 uint8_t buf[3];
325
326 if (handle == NULL) /* check handle */
327 {
328 return 2; /* return error */
329 }
330 if (handle->inited != 1) /* check handle initialization */
331 {
332 return 3; /* return error */
333 }
334
335 res = a_ads1110_iic_read(handle, buf, 3); /* read config */
336 if (res != 0) /* check the result */
337 {
338 handle->debug_print("ads1110: read config failed.\n"); /* read config failed */
339
340 return 1; /* return error */
341 }
342 buf[2] &= ~(3 << 0); /* clear settings */
343 buf[2] |= gain << 0; /* set gain */
344 res = a_ads1110_iic_write(handle, &buf[2], 1); /* write config */
345 if (res != 0) /* check the result */
346 {
347 handle->debug_print("ads1110: write config failed.\n"); /* write config failed */
348
349 return 1; /* return error */
350 }
351
352 return 0; /* success return */
353}
354
367{
368 uint8_t res;
369 uint8_t buf[3];
370
371 if (handle == NULL) /* check handle */
372 {
373 return 2; /* return error */
374 }
375 if (handle->inited != 1) /* check handle initialization */
376 {
377 return 3; /* return error */
378 }
379
380 res = a_ads1110_iic_read(handle, buf, 3); /* read config */
381 if (res != 0) /* check the result */
382 {
383 handle->debug_print("ads1110: read config failed.\n"); /* read config failed */
384
385 return 1; /* return error */
386 }
387 *gain = (ads1110_gain_t)((buf[2] >> 0) & 0x03); /* set gain */
388
389 return 0; /* success return 0 */
390}
391
403{
404 if (handle == NULL) /* check handle */
405 {
406 return 2; /* return error */
407 }
408
409 handle->iic_addr = (uint8_t)(addr_pin); /* set address pin */
410
411 return 0; /* success return 0 */
412}
413
425{
426 if (handle == NULL) /* check handle */
427 {
428 return 2; /* return error */
429 }
430
431 *addr_pin = (ads1110_address_t)(handle->iic_addr); /* set address pin */
432
433 return 0; /* success return 0 */
434}
435
448uint8_t ads1110_single_read(ads1110_handle_t *handle, int16_t *raw, float *v)
449{
450 uint8_t res;
451 uint8_t pga;
452 uint8_t rate;
453 uint8_t n;
454 uint8_t buf[3];
455 uint16_t timeout;
456
457 if (handle == NULL) /* check handle */
458 {
459 return 2; /* return error */
460 }
461 if (handle->inited != 1) /* check handle initialization */
462 {
463 return 3; /* return error */
464 }
465
466 res = a_ads1110_iic_read(handle, buf, 3); /* read config */
467 if (res != 0) /* check the result */
468 {
469 handle->debug_print("ads1110: read config failed.\n"); /* read config failed */
470
471 return 1; /* return error */
472 }
473 buf[2] &= ~(1 << 4); /* clear settings */
474 buf[2] |= 1 << 4; /* set single config */
475 buf[2] |= 1 << 7; /* set start */
476 res = a_ads1110_iic_write(handle, &buf[2], 1); /* write config */
477 if (res != 0) /* check the result */
478 {
479 handle->debug_print("ads1110: write config failed.\n"); /* write config failed */
480
481 return 1; /* return error */
482 }
483 timeout = 5 * 1000; /* max 5 seconds */
484 while (timeout != 0) /* check timeout */
485 {
486 handle->delay_ms(1); /* wait 1 ms */
487 res = a_ads1110_iic_read(handle, buf, 3); /* read config */
488 if (res != 0) /* check the result */
489 {
490 handle->debug_print("ads1110: read config failed.\n"); /* read config failed */
491
492 return 1; /* return error */
493 }
494 if ((buf[2] & (1 << 7)) == 0) /* check finished */
495 {
496 break; /* break */
497 }
498 timeout--; /* timeout-- */
499 }
500 if (timeout == 0) /* check timeout */
501 {
502 handle->debug_print("ads1110: read timeout.\n"); /* timeout */
503
504 return 1; /* return error */
505 }
506 *raw = (int16_t)(((uint16_t)buf[0] << 8) | buf[1]); /* set raw */
507 rate = (buf[2] >> 2) & 0x03; /* set rate */
508 pga = buf[2] & 0x03; /* set pga */
509 if (pga == 0) /* 0 */
510 {
511 n = 1; /* gain 1 */
512 }
513 else if (pga == 1) /* 1 */
514 {
515 n = 2; /* gain 2 */
516 }
517 else if (pga == 2) /* 2 */
518 {
519 n = 4; /* gain 4 */
520 }
521 else /* 3 */
522 {
523 n = 8; /* gain 8 */
524 }
525 if (rate == 0) /* 240 sps */
526 {
527 *v = (float)(*raw) / (2048.0f * (float)(n)) * 2.048f; /* convert */
528 }
529 else if (rate == 1) /* 60 sps */
530 {
531 *v = (float)(*raw) / (8192.0f * (float)(n)) * 2.048f; /* convert */
532 }
533 else if (rate == 2) /* 30 sps */
534 {
535 *v = (float)(*raw) / (16384.0f * (float)(n)) * 2.048f; /* convert */
536 }
537 else /* 15 sps */
538 {
539 *v = (float)(*raw) / (32768.0f * (float)(n)) * 2.048f; /* convert */
540 }
541
542 return 0; /* success return 0 */
543}
544
558uint8_t ads1110_continuous_read(ads1110_handle_t *handle,int16_t *raw, float *v)
559{
560 uint8_t res;
561 uint8_t pga;
562 uint8_t rate;
563 uint8_t n;
564 uint8_t buf[3];
565
566 if (handle == NULL) /* check handle */
567 {
568 return 2; /* return error */
569 }
570 if (handle->inited != 1) /* check handle initialization */
571 {
572 return 3; /* return error */
573 }
574
575 res = a_ads1110_iic_read(handle, buf, 3); /* read config */
576 if (res != 0) /* check the result */
577 {
578 handle->debug_print("ads1110: read config failed.\n"); /* read config failed */
579
580 return 1; /* return error */
581 }
582 *raw = (int16_t)(((uint16_t)buf[0] << 8) | buf[1]); /* set raw */
583 rate = (buf[2] >> 2) & 0x03; /* set rate */
584 pga = buf[2] & 0x03; /* set pga */
585 if (pga == 0) /* 0 */
586 {
587 n = 1; /* gain 1 */
588 }
589 else if (pga == 1) /* 1 */
590 {
591 n = 2; /* gain 2 */
592 }
593 else if (pga == 2) /* 2 */
594 {
595 n = 4; /* gain 4 */
596 }
597 else /* 3 */
598 {
599 n = 8; /* gain 8 */
600 }
601 if (rate == 0) /* 240 sps */
602 {
603 *v = (float)(*raw) / (2048.0f * (float)(n)) * 2.048f; /* convert */
604 }
605 else if (rate == 1) /* 60 sps */
606 {
607 *v = (float)(*raw) / (8192.0f * (float)(n)) * 2.048f; /* convert */
608 }
609 else if (rate == 2) /* 30 sps */
610 {
611 *v = (float)(*raw) / (16384.0f * (float)(n)) * 2.048f; /* convert */
612 }
613 else /* 15 sps */
614 {
615 *v = (float)(*raw) / (32768.0f * (float)(n)) * 2.048f; /* convert */
616 }
617
618 return 0; /* success return 0 */
619}
620
632{
633 uint8_t res;
634 uint8_t buf[3];
635
636 if (handle == NULL) /* check handle */
637 {
638 return 2; /* return error */
639 }
640 if (handle->inited != 1) /* check handle initialization */
641 {
642 return 3; /* return error */
643 }
644
645 res = a_ads1110_iic_read(handle, buf, 3); /* read config */
646 if (res != 0) /* check the result */
647 {
648 handle->debug_print("ads1110: read config failed.\n"); /* read config failed */
649
650 return 1; /* return error */
651 }
652 buf[2] &= ~(1 << 4); /* clear settings */
653 res = a_ads1110_iic_write(handle, &buf[2], 1); /* write config */
654 if (res != 0) /* check the result */
655 {
656 handle->debug_print("ads1110: write config failed.\n"); /* write config failed */
657
658 return 1; /* return error */
659 }
660
661 return 0; /* success return 0 */
662}
663
675{
676 uint8_t res;
677 uint8_t buf[3];
678
679 if (handle == NULL) /* check handle */
680 {
681 return 2; /* return error */
682 }
683 if (handle->inited != 1) /* check handle initialization */
684 {
685 return 3; /* return error */
686 }
687
688 res = a_ads1110_iic_read(handle, buf, 3); /* read config */
689 if (res != 0) /* check the result */
690 {
691 handle->debug_print("ads1110: read config failed.\n"); /* read config failed */
692
693 return 1; /* return error */
694 }
695 buf[2] &= ~(1 << 4); /* clear settings */
696 buf[2] |= 1 << 4; /* set single config */
697 res = a_ads1110_iic_write(handle, &buf[2], 1); /* write config */
698 if (res != 0) /* check the result */
699 {
700 handle->debug_print("ads1110: write config failed.\n"); /* write config failed */
701
702 return 1; /* return error */
703 }
704
705 return 0; /* success return 0 */
706}
707
720uint8_t ads1110_set_reg(ads1110_handle_t *handle, uint8_t *buf, uint16_t len)
721{
722 if (handle == NULL) /* check handle */
723 {
724 return 2; /* return error */
725 }
726 if (handle->inited != 1) /* check handle initialization */
727 {
728 return 3; /* return error */
729 }
730
731 return a_ads1110_iic_write(handle, buf, len); /* write reg */
732}
733
746uint8_t ads1110_get_reg(ads1110_handle_t *handle, uint8_t *buf, uint16_t len)
747{
748 if (handle == NULL) /* check handle */
749 {
750 return 2; /* return error */
751 }
752 if (handle->inited != 1) /* check handle initialization */
753 {
754 return 3; /* return error */
755 }
756
757 return a_ads1110_iic_read(handle, buf, len); /* read reg */
758}
759
769{
770 if (info == NULL) /* check handle */
771 {
772 return 2; /* return error */
773 }
774
775 memset(info, 0, sizeof(ads1110_info_t)); /* initialize ads1110 info structure */
776 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
777 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
778 strncpy(info->interface, "IIC", 8); /* copy interface name */
779 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
780 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
781 info->max_current_ma = MAX_CURRENT; /* set maximum current */
782 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
783 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
784 info->driver_version = DRIVER_VERSION; /* set driver version */
785
786 return 0; /* success return 0 */
787}
#define MAX_CURRENT
#define SUPPLY_VOLTAGE_MAX
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
driver ads1110 header file
uint8_t ads1110_set_rate(ads1110_handle_t *handle, ads1110_rate_t rate)
set the sample rate
uint8_t ads1110_single_read(ads1110_handle_t *handle, int16_t *raw, float *v)
read data from the chip once
uint8_t ads1110_deinit(ads1110_handle_t *handle)
close the chip
uint8_t ads1110_set_addr_pin(ads1110_handle_t *handle, ads1110_address_t addr_pin)
set the iic address pin
uint8_t ads1110_info(ads1110_info_t *info)
get chip's information
struct ads1110_handle_s ads1110_handle_t
ads1110 handle structure definition
uint8_t ads1110_stop_continuous_read(ads1110_handle_t *handle)
stop the chip reading
ads1110_rate_t
ads1110 rate enumeration definition
uint8_t ads1110_get_gain(ads1110_handle_t *handle, ads1110_gain_t *gain)
get adc gain
uint8_t ads1110_get_rate(ads1110_handle_t *handle, ads1110_rate_t *rate)
get the sample rate
uint8_t ads1110_init(ads1110_handle_t *handle)
initialize the chip
uint8_t ads1110_continuous_read(ads1110_handle_t *handle, int16_t *raw, float *v)
read data from the chip continuously
ads1110_address_t
ads1110 address enumeration definition
uint8_t ads1110_get_addr_pin(ads1110_handle_t *handle, ads1110_address_t *addr_pin)
get the iic address pin
struct ads1110_info_s ads1110_info_t
ads1110 information structure definition
uint8_t ads1110_set_gain(ads1110_handle_t *handle, ads1110_gain_t gain)
set adc gain
uint8_t ads1110_start_continuous_read(ads1110_handle_t *handle)
start the chip reading
ads1110_gain_t
ads1110 gain enumeration definition
uint8_t ads1110_get_reg(ads1110_handle_t *handle, uint8_t *buf, uint16_t len)
get the chip register
uint8_t ads1110_set_reg(ads1110_handle_t *handle, uint8_t *buf, uint16_t len)
set 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_deinit)(void)
uint8_t(* iic_write_cmd)(uint8_t addr, uint8_t *buf, uint16_t len)
uint32_t driver_version
char manufacturer_name[32]