LibDriver MCP3421
Loading...
Searching...
No Matches
driver_mcp3421.c
Go to the documentation of this file.
1
36
37#include "driver_mcp3421.h"
38#include <math.h>
39
43#define CHIP_NAME "Microchip MCP3421"
44#define MANUFACTURER_NAME "Microchip"
45#define SUPPLY_VOLTAGE_MIN 2.7f
46#define SUPPLY_VOLTAGE_MAX 5.5f
47#define MAX_CURRENT 10.0f
48#define TEMPERATURE_MIN -40.0f
49#define TEMPERATURE_MAX 125.0f
50#define DRIVER_VERSION 1000
51
55#define MCP3421_ADDRESS (0xD0)
56
67static uint8_t a_mcp3421_iic_read(mcp3421_handle_t *handle, uint8_t *buf, uint16_t len)
68{
69 if (handle->iic_read_cmd(MCP3421_ADDRESS, (uint8_t *)buf, len) == 0) /* read data */
70 {
71 return 0; /* success return 0 */
72 }
73 else
74 {
75 return 1; /* return error */
76 }
77}
78
89static uint8_t a_mcp3421_iic_write(mcp3421_handle_t *handle, uint8_t *buf, uint16_t len)
90{
91 if (handle->iic_write_cmd(MCP3421_ADDRESS, (uint8_t *)buf, len) == 0) /* write data */
92 {
93 return 0; /* success return 0 */
94 }
95 else
96 {
97 return 1; /* return error */
98 }
99}
100
112{
113 if (handle == NULL) /* check handle */
114 {
115 return 2; /* return error */
116 }
117 if (handle->debug_print == NULL) /* check debug_print */
118 {
119 return 3; /* return error */
120 }
121 if (handle->iic_init == NULL) /* check iic_init */
122 {
123 handle->debug_print("mcp3421: iic_init is null.\n"); /* iic_init is null */
124
125 return 3; /* return error */
126 }
127 if (handle->iic_deinit == NULL) /* check iic_deinit */
128 {
129 handle->debug_print("mcp3421: iic_deinit is null.\n"); /* iic_deinit is null */
130
131 return 3; /* return error */
132 }
133 if (handle->iic_read_cmd == NULL) /* check iic_read_cmd */
134 {
135 handle->debug_print("mcp3421: iic_read_cmd is null.\n"); /* iic_read_cmd is null */
136
137 return 3; /* return error */
138 }
139 if (handle->iic_write_cmd == NULL) /* check iic_write_cmd */
140 {
141 handle->debug_print("mcp3421: iic_write_cmd is null.\n"); /* iic_write_cmd is null */
142
143 return 3; /* return error */
144 }
145 if (handle->delay_ms == NULL) /* check delay_ms */
146 {
147 handle->debug_print("mcp3421: delay_ms is null.\n"); /* delay_ms is null */
148
149 return 3; /* return error */
150 }
151
152 if (handle->iic_init() != 0) /* iic init */
153 {
154 handle->debug_print("mcp3421: iic init failed.\n"); /* iic init failed */
155
156 return 1; /* return error */
157 }
158 handle->inited = 1; /* flag inited */
159
160 return 0; /* success return 0 */
161}
162
175{
176 uint8_t res;
177 uint8_t buf[4];
178
179 if (handle == NULL) /* check handle */
180 {
181 return 2; /* return error */
182 }
183 if (handle->inited != 1) /* check handle initialization */
184 {
185 return 3; /* return error */
186 }
187
188 res = a_mcp3421_iic_read(handle, buf, 4); /* read config */
189 if (res != 0) /* check error */
190 {
191 handle->debug_print("mcp3421: read config failed.\n"); /* read config failed */
192
193 return 4; /* return error */
194 }
195 buf[3] &= ~(1 << 4); /* clear mode */
196 res = a_mcp3421_iic_write(handle, &buf[3], 1); /* write config */
197 if (res != 0) /* check error */
198 {
199 handle->debug_print("mcp3421: write config failed.\n"); /* write config failed */
200
201 return 4; /* return error */
202 }
203 res = handle->iic_deinit(); /* close iic */
204 if (res != 0) /* check the result */
205 {
206 handle->debug_print("mcp3421: iic deinit failed.\n"); /* iic deinit failed */
207
208 return 1; /* return error */
209 }
210 handle->inited = 0; /* flag close */
211
212 return 0; /* success return 0 */
213}
214
227{
228 uint8_t res;
229 uint8_t buf[4];
230
231 if (handle == NULL) /* check handle */
232 {
233 return 2; /* return error */
234 }
235 if (handle->inited != 1) /* check handle initialization */
236 {
237 return 3; /* return error */
238 }
239
240 res = a_mcp3421_iic_read(handle, buf, 4); /* read config */
241 if (res != 0) /* check error */
242 {
243 handle->debug_print("mcp3421: read config failed.\n"); /* read config failed */
244
245 return 1; /* return error */
246 }
247 buf[3] &= ~(0x03 << 2); /* clear settings */
248 buf[3] |= adc_bit << 2; /* set adc bit */
249 res = a_mcp3421_iic_write(handle, &buf[3], 1); /* write config */
250 if (res != 0) /* check error */
251 {
252 handle->debug_print("mcp3421: write config failed.\n"); /* write config failed */
253
254 return 1; /* return error */
255 }
256
257 return 0; /* success return 0 */
258}
259
272{
273 uint8_t res;
274 uint8_t buf[4];
275
276 if (handle == NULL) /* check handle */
277 {
278 return 2; /* return error */
279 }
280 if (handle->inited != 1) /* check handle initialization */
281 {
282 return 3; /* return error */
283 }
284
285 res = a_mcp3421_iic_read(handle, buf, 4); /* read config */
286 if (res != 0) /* check error */
287 {
288 handle->debug_print("mcp3421: read config failed.\n"); /* read config failed */
289
290 return 1; /* return error */
291 }
292 *adc_bit = (mcp3421_bit_t)((buf[3] >> 2) & 0x03); /* get adc bit */
293
294 return 0; /* success return 0 */
295}
296
309{
310 uint8_t res;
311 uint8_t buf[4];
312
313 if (handle == NULL) /* check handle */
314 {
315 return 2; /* return error */
316 }
317 if (handle->inited != 1) /* check handle initialization */
318 {
319 return 3; /* return error */
320 }
321
322 res = a_mcp3421_iic_read(handle, buf, 4); /* read config */
323 if (res != 0) /* check error */
324 {
325 handle->debug_print("mcp3421: read config failed.\n"); /* read config failed */
326
327 return 1; /* return error */
328 }
329 buf[3] &= ~(0x03 << 0); /* clear settings */
330 buf[3] |= pga << 0; /* set pga */
331 res = a_mcp3421_iic_write(handle, &buf[3], 1); /* write config */
332 if (res != 0) /* check error */
333 {
334 handle->debug_print("mcp3421: write config failed.\n"); /* write config failed */
335
336 return 1; /* return error */
337 }
338
339 return 0; /* success return 0 */
340}
341
354{
355 uint8_t res;
356 uint8_t buf[4];
357
358 if (handle == NULL) /* check handle */
359 {
360 return 2; /* return error */
361 }
362 if (handle->inited != 1) /* check handle initialization */
363 {
364 return 3; /* return error */
365 }
366
367 res = a_mcp3421_iic_read(handle, buf, 4); /* read config */
368 if (res != 0) /* check error */
369 {
370 handle->debug_print("mcp3421: read config failed.\n"); /* read config failed */
371
372 return 1; /* return error */
373 }
374 *pga = (mcp3421_pga_t)((buf[3] >> 0) & 0x03); /* get pga */
375
376 return 0; /* success return 0 */
377}
378
392uint8_t mcp3421_single_read(mcp3421_handle_t *handle, int32_t *raw, double *v)
393{
394 uint8_t res;
395 uint8_t adc_bit;
396 uint8_t pga;
397 uint8_t buf[4];
398 uint32_t timeout = 500;
399
400 if (handle == NULL) /* check handle */
401 {
402 return 2; /* return error */
403 }
404 if (handle->inited != 1) /* check handle initialization */
405 {
406 return 3; /* return error */
407 }
408
409 res = a_mcp3421_iic_read(handle, buf, 4); /* read config */
410 if (res != 0) /* check error */
411 {
412 handle->debug_print("mcp3421: read config failed.\n"); /* read config failed */
413
414 return 1; /* return error */
415 }
416 buf[3] &= ~(1 << 4); /* shot mode */
417 buf[3] |= 1 << 7; /* set start bit */
418 res = a_mcp3421_iic_write(handle, &buf[3], 1); /* write config */
419 if (res != 0) /* check error */
420 {
421 handle->debug_print("mcp3421: write config failed.\n"); /* write config failed */
422
423 return 1; /* return error */
424 }
425 adc_bit = buf[3]; /* get adc bit */
426 pga = buf[3] & 0x3; /* get pga */
427 while (timeout != 0) /* loop */
428 {
429 handle->delay_ms(10); /* delay 10ms */
430 timeout--; /* timeout */
431 if (((adc_bit >> 2) & 0x3) == 3) /* 18bit */
432 {
433 res = a_mcp3421_iic_read(handle, buf, 4); /* read config */
434 if (res != 0) /* check error */
435 {
436 handle->debug_print("mcp3421: read config failed.\n"); /* read config failed */
437
438 return 1; /* return error */
439 }
440 if (((buf[3] >> 7) & 0x1) == 0) /* check updated bit */
441 {
442 break; /* break */
443 }
444 }
445 else
446 {
447 res = a_mcp3421_iic_read(handle, buf, 3); /* read config */
448 if (res != 0) /* check error */
449 {
450 handle->debug_print("mcp3421: read config failed.\n"); /* read config failed */
451
452 return 1; /* return error */
453 }
454 if (((buf[2] >> 7) & 0x01) == 0) /* check updated bit */
455 {
456 break; /* break */
457 }
458 }
459 }
460 if (timeout == 0) /* check timeout */
461 {
462 handle->debug_print("mcp3421: read timeout.\n"); /* read timeout */
463
464 return 4; /* return error */
465 }
466 if (((adc_bit >> 2) & 0x3) == 3) /* 18bit */
467 {
468 if (((buf[0] >> 1) & 0x01) != 0) /* check msb */
469 {
470 *raw = (int32_t)(((uint32_t)(buf[0] & 0x3) << 16) |
471 ((uint32_t)buf[1] << 8) |
472 ((uint32_t)buf[2] << 0) |
473 ((uint32_t)0xFF << 24) |
474 ((uint32_t)0xFC << 16)); /* set raw */
475 }
476 else
477 {
478 *raw = (int32_t)(((uint32_t)(buf[0] & 0x3) << 16) |
479 ((uint32_t)buf[1] << 8) |
480 ((uint32_t)buf[2] << 0)); /* set raw */
481 }
482 *v = (double)(*raw) * 2.048 / (pow(2, 17) * pow(2, pga)); /* convert */
483 }
484 else if (((adc_bit >> 2) & 0x3) == 2) /* 16bit */
485 {
486 if (((buf[0] >> 7) & 0x01) != 0) /* check msb */
487 {
488 *raw = (int32_t)(((uint32_t)buf[0] << 8) |
489 ((uint32_t)buf[1] << 0) |
490 ((uint32_t)0xFF << 24) |
491 ((uint32_t)0xFF << 16)); /* set raw */
492 }
493 else
494 {
495 *raw = (int32_t)(((uint32_t)buf[0] << 8) |
496 ((uint32_t)buf[1] << 0)); /* set raw */
497 }
498 *v = (double)(*raw) * 2.048 / (pow(2, 15) * pow(2, pga)); /* convert */
499 }
500 else if (((adc_bit >> 2) & 0x3) == 1) /* 14bit */
501 {
502 if (((buf[0] >> 6) & 0x01) != 0) /* check msb */
503 {
504 *raw = (int32_t)(((uint32_t)buf[0] << 8) |
505 ((uint32_t)buf[1] << 0) |
506 ((uint32_t)0xFF << 24) |
507 ((uint32_t)0xFF << 16)); /* set raw */
508 }
509 else
510 {
511 *raw = (int32_t)(((uint32_t)buf[0] << 8) |
512 ((uint32_t)buf[1] << 0)); /* set raw */
513 }
514 *v = (double)(*raw) * 2.048 / (pow(2, 13) * pow(2, pga)); /* convert */
515 }
516 else /* 12bit */
517 {
518 if (((buf[0] >> 5) & 0x01) != 0) /* check msb */
519 {
520 *raw = (int32_t)(((uint32_t)buf[0] << 8) |
521 ((uint32_t)buf[1] << 0) |
522 ((uint32_t)0xFF << 24) |
523 ((uint32_t)0xFF << 16)); /* set raw */
524 }
525 else
526 {
527 *raw = (int32_t)(((uint32_t)buf[0] << 8) |
528 ((uint32_t)buf[1] << 0)); /* set raw */
529 }
530 *v = (double)(*raw) * 2.048 / (pow(2, 11) * pow(2, pga)); /* convert */
531 }
532
533 return 0; /* success return 0 */
534}
535
549uint8_t mcp3421_continuous_read(mcp3421_handle_t *handle, int32_t *raw, double *v)
550{
551 uint8_t res;
552 uint8_t adc_bit;
553 uint8_t pga;
554 uint8_t buf[4];
555
556 if (handle == NULL) /* check handle */
557 {
558 return 2; /* return error */
559 }
560 if (handle->inited != 1) /* check handle initialization */
561 {
562 return 3; /* return error */
563 }
564
565 adc_bit = handle->config; /* get adc bit */
566 pga = handle->config & 0x3; /* get pga */
567 if (((adc_bit >> 2) & 0x3) == 3) /* 18bit */
568 {
569 res = a_mcp3421_iic_read(handle, buf, 4); /* read config */
570 if (res != 0) /* check error */
571 {
572 handle->debug_print("mcp3421: read config failed.\n"); /* read config failed */
573
574 return 1; /* return error */
575 }
576 }
577 else
578 {
579 res = a_mcp3421_iic_read(handle, buf, 3); /* read config */
580 if (res != 0) /* check error */
581 {
582 handle->debug_print("mcp3421: read config failed.\n"); /* read config failed */
583
584 return 1; /* return error */
585 }
586 }
587
588 if (((adc_bit >> 2) & 0x3) == 3) /* 18bit */
589 {
590 if (((buf[0] >> 1) & 0x01) != 0) /* check msb */
591 {
592 *raw = (int32_t)(((uint32_t)(buf[0] & 0x3) << 16) |
593 ((uint32_t)buf[1] << 8) |
594 ((uint32_t)buf[2] << 0) |
595 ((uint32_t)0xFF << 24) |
596 ((uint32_t)0xFC << 16)); /* set raw */
597 }
598 else
599 {
600 *raw = (int32_t)(((uint32_t)(buf[0] & 0x3) << 16) |
601 ((uint32_t)buf[1] << 8) |
602 ((uint32_t)buf[2] << 0)); /* set raw */
603 }
604 *v = (double)(*raw) * 2.048 / (pow(2, 17) * pow(2, pga)); /* convert */
605 }
606 else if (((adc_bit >> 2) & 0x3) == 2) /* 16bit */
607 {
608 if (((buf[0] >> 7) & 0x01) != 0) /* check msb */
609 {
610 *raw = (int32_t)(((uint32_t)buf[0] << 8) |
611 ((uint32_t)buf[1] << 0) |
612 ((uint32_t)0xFF << 24) |
613 ((uint32_t)0xFF << 16)); /* set raw */
614 }
615 else
616 {
617 *raw = (int32_t)(((uint32_t)buf[0] << 8) |
618 ((uint32_t)buf[1] << 0)); /* set raw */
619 }
620 *v = (double)(*raw) * 2.048 / (pow(2, 15) * pow(2, pga)); /* convert */
621 }
622 else if (((adc_bit >> 2) & 0x3) == 1) /* 14bit */
623 {
624 if (((buf[0] >> 6) & 0x01) != 0) /* check msb */
625 {
626 *raw = (int32_t)(((uint32_t)buf[0] << 8) |
627 ((uint32_t)buf[1] << 0) |
628 ((uint32_t)0xFF << 24) |
629 ((uint32_t)0xFF << 16)); /* set raw */
630 }
631 else
632 {
633 *raw = (int32_t)(((uint32_t)buf[0] << 8) |
634 ((uint32_t)buf[1] << 0)); /* set raw */
635 }
636 *v = (double)(*raw) * 2.048 / (pow(2, 13) * pow(2, pga)); /* convert */
637 }
638 else /* 12bit */
639 {
640 if (((buf[0] >> 5) & 0x01) != 0) /* check msb */
641 {
642 *raw = (int32_t)(((uint32_t)buf[0] << 8) |
643 ((uint32_t)buf[1] << 0) |
644 ((uint32_t)0xFF << 24) |
645 ((uint32_t)0xFF << 16)); /* set raw */
646 }
647 else
648 {
649 *raw = (int32_t)(((uint32_t)buf[0] << 8) |
650 ((uint32_t)buf[1] << 0)); /* set raw */
651 }
652 *v = (double)(*raw) * 2.048 / (pow(2, 11) * pow(2, pga)); /* convert */
653 }
654
655 return 0; /* success return 0 */
656}
657
669{
670 uint8_t res;
671 uint8_t buf[4];
672
673 if (handle == NULL) /* check handle */
674 {
675 return 2; /* return error */
676 }
677 if (handle->inited != 1) /* check handle initialization */
678 {
679 return 3; /* return error */
680 }
681
682 res = a_mcp3421_iic_read(handle, buf, 4); /* read config */
683 if (res != 0) /* check error */
684 {
685 handle->debug_print("mcp3421: read config failed.\n"); /* read config failed */
686
687 return 1; /* return error */
688 }
689 buf[3] &= ~(1 << 4); /* clear mode */
690 buf[3] |= 1 << 4; /* continuous mode */
691 res = a_mcp3421_iic_write(handle, &buf[3], 1); /* write config */
692 if (res != 0) /* check error */
693 {
694 handle->debug_print("mcp3421: write config failed.\n"); /* write config failed */
695
696 return 1; /* return error */
697 }
698 handle->config = buf[3]; /* save config */
699
700 return 0; /* success return 0 */
701}
702
714{
715 uint8_t res;
716 uint8_t buf[4];
717
718 if (handle == NULL) /* check handle */
719 {
720 return 2; /* return error */
721 }
722 if (handle->inited != 1) /* check handle initialization */
723 {
724 return 3; /* return error */
725 }
726
727 res = a_mcp3421_iic_read(handle, buf, 4); /* read config */
728 if (res != 0) /* check error */
729 {
730 handle->debug_print("mcp3421: read config failed.\n"); /* read config failed */
731
732 return 1; /* return error */
733 }
734 buf[3] &= ~(1 << 4); /* clear mode */
735 res = a_mcp3421_iic_write(handle, &buf[3], 1); /* write config */
736 if (res != 0) /* check error */
737 {
738 handle->debug_print("mcp3421: write config failed.\n"); /* write config failed */
739
740 return 1; /* return error */
741 }
742 handle->config = buf[3]; /* save config */
743
744 return 0; /* success return 0 */
745}
746
759uint8_t mcp3421_set_reg(mcp3421_handle_t *handle, uint8_t *buf, uint16_t len)
760{
761 if (handle == NULL) /* check handle */
762 {
763 return 2; /* return error */
764 }
765 if (handle->inited != 1) /* check handle initialization */
766 {
767 return 3; /* return error */
768 }
769
770 return a_mcp3421_iic_write(handle, buf, len); /* write reg */
771}
772
785uint8_t mcp3421_get_reg(mcp3421_handle_t *handle, uint8_t *buf, uint16_t len)
786{
787 if (handle == NULL) /* check handle */
788 {
789 return 2; /* return error */
790 }
791 if (handle->inited != 1) /* check handle initialization */
792 {
793 return 3; /* return error */
794 }
795
796 return a_mcp3421_iic_read(handle, buf, len); /* read reg */
797}
798
808{
809 if (info == NULL) /* check handle */
810 {
811 return 2; /* return error */
812 }
813
814 memset(info, 0, sizeof(mcp3421_info_t)); /* initialize mcp3421 info structure */
815 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
816 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
817 strncpy(info->interface, "IIC", 8); /* copy interface name */
818 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
819 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
820 info->max_current_ma = MAX_CURRENT; /* set maximum current */
821 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
822 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
823 info->driver_version = DRIVER_VERSION; /* set driver version */
824
825 return 0; /* success return 0 */
826}
#define MAX_CURRENT
#define SUPPLY_VOLTAGE_MAX
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define MCP3421_ADDRESS
iic address definition
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
driver mcp3421 header file
uint8_t mcp3421_get_adc_bit(mcp3421_handle_t *handle, mcp3421_bit_t *adc_bit)
get the adc bit
uint8_t mcp3421_set_pga(mcp3421_handle_t *handle, mcp3421_pga_t pga)
set the adc pga
uint8_t mcp3421_set_adc_bit(mcp3421_handle_t *handle, mcp3421_bit_t adc_bit)
set the adc bit
uint8_t mcp3421_start_continuous_read(mcp3421_handle_t *handle)
start the chip reading
mcp3421_pga_t
mcp3421 pga enumeration definition
uint8_t mcp3421_single_read(mcp3421_handle_t *handle, int32_t *raw, double *v)
read data from the chip once
uint8_t mcp3421_init(mcp3421_handle_t *handle)
initialize the chip
uint8_t mcp3421_deinit(mcp3421_handle_t *handle)
close the chip
uint8_t mcp3421_stop_continuous_read(mcp3421_handle_t *handle)
stop the chip reading
uint8_t mcp3421_continuous_read(mcp3421_handle_t *handle, int32_t *raw, double *v)
read data from the chip continuously
uint8_t mcp3421_get_pga(mcp3421_handle_t *handle, mcp3421_pga_t *pga)
get the adc pga
mcp3421_bit_t
mcp3421 bit enumeration definition
struct mcp3421_handle_s mcp3421_handle_t
mcp3421 handle structure definition
struct mcp3421_info_s mcp3421_info_t
mcp3421 information structure definition
uint8_t mcp3421_info(mcp3421_info_t *info)
get chip's information
uint8_t mcp3421_set_reg(mcp3421_handle_t *handle, uint8_t *buf, uint16_t len)
set the chip register
uint8_t mcp3421_get_reg(mcp3421_handle_t *handle, 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_deinit)(void)
uint8_t(* iic_write_cmd)(uint8_t addr, uint8_t *buf, uint16_t len)
uint32_t driver_version
char manufacturer_name[32]