LibDriver MS5837
Loading...
Searching...
No Matches
driver_ms5837.c
Go to the documentation of this file.
1
36
37#include "driver_ms5837.h"
38
42#define CHIP_NAME "TE MS5837"
43#define MANUFACTURER_NAME "TE"
44#define SUPPLY_VOLTAGE_MIN 1.5f
45#define SUPPLY_VOLTAGE_MAX 3.6f
46#define MAX_CURRENT 1.25f
47#define TEMPERATURE_MIN -20.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
50
54#define MS5837_ADDRESS 0xEC
55
59#define MS5837_CMD_RESET 0x1E
60#define MS5837_CMD_D1 0x40
61#define MS5837_CMD_D2 0x50
62#define MS5837_CMD_ADC_READ 0x00
63#define MS5837_CMD_PROM_READ 0xA0
64
76static uint8_t a_ms5837_iic_read(ms5837_handle_t *handle, uint8_t reg, uint8_t *data, uint16_t len)
77{
78 if (handle->iic_read(MS5837_ADDRESS, reg, data, len) != 0) /* read the register */
79 {
80 return 1; /* return error */
81 }
82 else
83 {
84 return 0; /* success return 0 */
85 }
86}
87
99static uint8_t a_ms5837_iic_write(ms5837_handle_t *handle, uint8_t reg, uint8_t *data, uint16_t len)
100{
101 if (handle->iic_write(MS5837_ADDRESS, reg, data, len) != 0) /* write the register */
102 {
103 return 1; /* return error */
104 }
105 else
106 {
107 return 0; /* success return 0 */
108 }
109}
110
117static uint8_t a_ms5837_crc4(uint16_t n_prom[8])
118{
119 uint16_t cnt;
120 uint16_t n_rem = 0;
121 uint16_t n_bit;
122
123 n_prom[0] = ((n_prom[0]) & 0x0FFF); /* crc byte is replaced by 0 */
124 n_prom[7] = 0; /* subsidiary value and set to 0 */
125 for (cnt = 0; cnt < 16; cnt++) /* operation is performed on bytes */
126 {
127 if ((cnt% 2) == 1) /* 16 bits progress */
128 {
129 n_rem ^= (uint16_t)((n_prom[cnt >> 1]) & 0x00FF); /* low part */
130 }
131 else
132 {
133 n_rem ^= (uint16_t)(n_prom[cnt >> 1] >> 8); /* high part */
134 }
135 for (n_bit = 8; n_bit > 0; n_bit--) /* 8 times */
136 {
137 if ((n_rem & (0x8000U)) != 0) /* check msb */
138 {
139 n_rem = (n_rem << 1) ^ 0x3000; /* xor */
140 }
141 else
142 {
143 n_rem = (n_rem << 1); /* do nothing */
144 }
145 }
146 }
147 n_rem= ((n_rem >> 12) & 0x000F); /* final 4-bit remainder is crc code */
148
149 return (n_rem ^ 0x00); /* return the crc */
150}
151
161static void a_ms5837_calculate_temperature_pressure(ms5837_handle_t *handle, uint32_t d2_temp, float *temperature_c,
162 uint32_t d1_press, float *pressure_mbar)
163{
164 int32_t dt = 0;
165 int64_t sens = 0;
166 int64_t off = 0;
167 int32_t sensi = 0;
168 int32_t offi = 0;
169 int32_t ti = 0;
170 int64_t off2 = 0;
171 int64_t sens2 = 0;
172 int32_t p;
173 int32_t temp;
174
175 dt = d2_temp - (uint32_t)(handle->c[4]) * 256; /* get the dt */
176 if ((handle->type == MS5837_TYPE_02BA01) || (handle->type == MS5837_TYPE_02BA21)) /* 02ba01 and 02ba21 */
177 {
178 sens = (int64_t)(handle->c[0]) * 65536 + ((int64_t)(handle->c[2]) * dt) / 128; /* get the sens */
179 off = (int64_t)(handle->c[1]) * 131072 + ((int64_t)(handle->c[3]) * dt) / 64; /* get the off */
180 p = (int32_t)((d1_press * sens / 2097152 - off) / 32768); /* get the p */
181 }
182 else /* 30ba26 */
183 {
184 sens = (int64_t)(handle->c[0]) * 32768 + ((int64_t)(handle->c[2]) * dt) / 256; /* get the sens */
185 off = (int64_t)(handle->c[1]) * 65536 + ((int64_t)(handle->c[3]) * dt) / 128; /* get the off */
186 p = (int32_t)((d1_press * sens / 2097152 - off) / 8192); /* get the p */
187 }
188 temp = 2000 + (int64_t)(dt) * handle->c[5] / 8388608; /* get the temp */
189 if ((handle->type == MS5837_TYPE_02BA01) || (handle->type == MS5837_TYPE_02BA21)) /* 02ba01 and 02ba21 */
190 {
191 if ((temp / 100) < 20) /* if < 20 */
192 {
193 ti = (int32_t)((11 * (int64_t)(dt) * (int64_t)(dt)) / 34359738368U); /* get the ti */
194 offi = (31 * (temp - 2000) * (temp - 2000)) / 8; /* get the offi */
195 sensi = (63 * (temp - 2000) * (temp - 2000)) / 32; /* get the sensi */
196 }
197 }
198 else
199 {
200 if ((temp / 100) < 20) /* if < 20 */
201 {
202 ti = (int32_t)((3 * (int64_t)(dt) * (int64_t)(dt)) / 8589934592U); /* get the ti */
203 offi = (3 * (temp - 2000) * (temp - 2000)) / 2; /* get the offi */
204 sensi = (5 * (temp - 2000) * (temp - 2000)) / 8; /* get the sensi */
205 if ((temp / 100) < -15) /* if < -15 */
206 {
207 offi = offi + 7 * (temp + 1500) * (temp + 1500); /* get the offi */
208 sensi = sensi + 4 * (temp + 1500) * (temp + 1500); /* get the sensi */
209 }
210 }
211 else
212 {
213 ti = (int32_t)(2 * (dt * dt) / 137438953472U); /* get the ti */
214 offi = (1 * (temp - 2000) * (temp - 2000)) / 16; /* get the offi */
215 sensi = 0; /* get the sensi */
216 }
217 }
218 off2 = off - offi; /* get the off2 */
219 sens2 = sens - sensi; /* get the sens2 */
220 temp = (temp - ti); /* get the temp */
221 *temperature_c = (float)(temp) / 100.0f; /* set the temperature */
222 if ((handle->type == MS5837_TYPE_02BA01) || (handle->type == MS5837_TYPE_02BA21)) /* 02ba01 and 02ba21 */
223 {
224 p = (int32_t)((((d1_press * sens2) / 2097152 - off2) / 32768)); /* get the p */
225 *pressure_mbar = (float)(p) / 100.0f; /* set the pressure */
226 }
227 else
228 {
229 p = (int32_t)((((d1_press * sens2) / 2097152 - off2) / 8192)); /* get the p */
230 *pressure_mbar = (float)(p) / 10.0f; /* set the pressure */
231 }
232}
233
241static void a_ms5837_calculate_temperature(ms5837_handle_t *handle, uint32_t d2_temp, float *temperature_c)
242{
243 int32_t dt = 0;
244 int32_t ti = 0;
245 int32_t temp;
246
247 dt = d2_temp - (uint32_t)(handle->c[4]) * 256; /* get the dt */
248 temp = 2000 + (int64_t)(dt) * handle->c[5] / 8388608; /* get the temp */
249 if ((handle->type == MS5837_TYPE_02BA01) || (handle->type == MS5837_TYPE_02BA21)) /* 02ba01 and 02ba21 */
250 {
251 if ((temp / 100) < 20) /* if < 20 */
252 {
253 ti = (int32_t)((11 * (int64_t)(dt) * (int64_t)(dt)) / 34359738368U); /* get the ti */
254 }
255 }
256 else
257 {
258 if ((temp / 100) < 20) /* if < 20 */
259 {
260 ti = (int32_t)((3 * (int64_t)(dt) * (int64_t)(dt)) / 8589934592U); /* get the ti */
261 }
262 else
263 {
264 ti = (int32_t)((2 * (dt * dt)) / 137438953472U); /* get the ti */
265 }
266 }
267 temp = (temp - ti); /* get the temp */
268 *temperature_c = (float)(temp) / 100.0f; /* set the temperature */
269}
270
286{
287 uint8_t i;
288 uint8_t crc;
289 uint8_t crc_check;
290 uint8_t type;
291 uint16_t c[8];
292
293 if (handle == NULL) /* check handle */
294 {
295 return 2; /* return error */
296 }
297 if (handle->debug_print == NULL) /* check debug_print */
298 {
299 return 3; /* return error */
300 }
301 if (handle->iic_init == NULL) /* check iic_init */
302 {
303 handle->debug_print("ms5837: iic_init is null.\n"); /* iic_init is null */
304
305 return 3; /* return error */
306 }
307 if (handle->iic_deinit == NULL) /* check iic_init */
308 {
309 handle->debug_print("ms5837: iic_deinit is null.\n"); /* iic_deinit is null */
310
311 return 3; /* return error */
312 }
313 if (handle->iic_read == NULL) /* check iic_read */
314 {
315 handle->debug_print("ms5837: iic_read is null.\n"); /* iic_read is null */
316
317 return 3; /* return error */
318 }
319 if (handle->iic_write == NULL) /* check iic_write */
320 {
321 handle->debug_print("ms5837: iic_write is null.\n"); /* iic_write is null */
322
323 return 3; /* return error */
324 }
325 if (handle->delay_ms == NULL) /* check delay_ms */
326 {
327 handle->debug_print("ms5837: delay_ms is null.\n"); /* delay_ms is null */
328
329 return 3; /* return error */
330 }
331
332 if (handle->iic_init() != 0) /* iic init */
333 {
334 handle->debug_print("ms5837: iic init failed.\n"); /* iic init failed */
335
336 return 1; /* return error */
337 }
338 if (a_ms5837_iic_write(handle, MS5837_CMD_RESET,
339 NULL, 0) != 0) /* reset the device */
340 {
341 handle->debug_print("ms5837: reset failed.\n"); /* reset failed */
342
343 return 4; /* return error */
344 }
345 handle->delay_ms(10); /* delay 10 ms */
346 memset(c, 0, sizeof(uint16_t) * 8); /* clear the c */
347 for (i = 0; i < 7; i++) /* 7 times */
348 {
349 if (a_ms5837_iic_read(handle, MS5837_CMD_PROM_READ + i * 2,
350 handle->prom + i * 2, 2) != 0) /* read the prom */
351 {
352 handle->debug_print("ms5837: read prom failed.\n"); /* read prom failed */
353
354 return 5; /* return error */
355 }
356 c[i] = ((uint16_t)(handle->prom[i * 2]) << 8) |
357 (uint16_t)(handle->prom[i * 2 + 1]); /* set c[i] */
358 }
359
360 crc_check = (c[0] >> 12) & 0xF; /* get the crc check */
361 type = (c[0] >> 5) & 0x7F; /* set the type */
362 crc = a_ms5837_crc4(c); /* calculate the crc */
363 if (crc != crc_check) /* check the crc */
364 {
365 handle->debug_print("ms5837: crc is error.\n"); /* crc is error */
366
367 return 6; /* return error */
368 }
369 handle->c[0] = c[1]; /* set c0 */
370 handle->c[1] = c[2]; /* set c1 */
371 handle->c[2] = c[3]; /* set c2 */
372 handle->c[3] = c[4]; /* set c3 */
373 handle->c[4] = c[5]; /* set c4 */
374 handle->c[5] = c[6]; /* set c5 */
375 if (type == 0x00) /* check the type */
376 {
377 handle->type = MS5837_TYPE_02BA01; /* ms5837 02ba01 */
378 }
379 else if (type == 0x15) /* check the type */
380 {
381 handle->type = MS5837_TYPE_02BA21; /* ms5837 02ba21 */
382 }
383 else if (type == 0x1A) /* check the type */
384 {
385 handle->type = MS5837_TYPE_30BA26; /* ms5837 30ba26 */
386 }
387 else
388 {
389 handle->debug_print("ms5837: type is invalid.\n"); /* type is invalid */
390
391 return 7; /* return error */
392 }
393 handle->temp_osr = MS5837_OSR_256; /* set 256 temperature osr */
394 handle->press_osr = MS5837_OSR_256; /* set 256 pressure osr */
395 handle->inited = 1; /* flag finish initialization */
396
397 return 0; /* success return 0 */
398}
399
412{
413 if (handle == NULL) /* check handle */
414 {
415 return 2; /* return error */
416 }
417 if (handle->inited != 1) /* check handle initialization */
418 {
419 return 3; /* return error */
420 }
421
422 if (a_ms5837_iic_write(handle, MS5837_CMD_RESET,
423 NULL, 0) != 0) /* reset the device */
424 {
425 handle->debug_print("ms5837: reset failed.\n"); /* reset failed */
426
427 return 4; /* return error */
428 }
429 handle->delay_ms(10); /* delay 10 ms */
430 if (handle->iic_deinit() != 0) /* iic deinit */
431 {
432 handle->debug_print("ms5837: iic deinit failed.\n"); /* iic deinit failed */
433
434 return 1; /* return error */
435 }
436 handle->inited = 0; /* flag close */
437
438 return 0; /* success return 0 */
439}
440
452{
453 if (handle == NULL) /* check handle */
454 {
455 return 2; /* return error */
456 }
457 if (handle->inited != 1) /* check handle initialization */
458 {
459 return 3; /* return error */
460 }
461
462 *type = (ms5837_type_t)(handle->type); /* get the type */
463
464 return 0; /* success return 0 */
465}
466
478{
479 if (handle == NULL) /* check handle */
480 {
481 return 2; /* return error */
482 }
483 if (handle->inited != 1) /* check handle initialization */
484 {
485 return 3; /* return error */
486 }
487
488 handle->type = (uint8_t)(type); /* set the type */
489
490 return 0; /* success return 0 */
491}
492
504{
505 if (handle == NULL) /* check handle */
506 {
507 return 2; /* return error */
508 }
509 if (handle->inited != 1) /* check handle initialization */
510 {
511 return 3; /* return error */
512 }
513
514 *osr = (ms5837_osr_t)(handle->temp_osr); /* get the osr */
515
516 return 0; /* success return 0 */
517}
518
530{
531 if (handle == NULL) /* check handle */
532 {
533 return 2; /* return error */
534 }
535 if (handle->inited != 1) /* check handle initialization */
536 {
537 return 3; /* return error */
538 }
539
540 handle->temp_osr = (uint8_t)(osr); /* set the osr */
541
542 return 0; /* success return 0 */
543}
544
556{
557 if (handle == NULL) /* check handle */
558 {
559 return 2; /* return error */
560 }
561 if (handle->inited != 1) /* check handle initialization */
562 {
563 return 3; /* return error */
564 }
565
566 *osr = (ms5837_osr_t)(handle->press_osr); /* get the osr */
567
568 return 0; /* success return 0 */
569}
570
582{
583 if (handle == NULL) /* check handle */
584 {
585 return 2; /* return error */
586 }
587 if (handle->inited != 1) /* check handle initialization */
588 {
589 return 3; /* return error */
590 }
591
592 handle->press_osr = (uint8_t)(osr); /* set the osr */
593
594 return 0; /* success return 0 */
595}
596
611uint8_t ms5837_read_temperature_pressure(ms5837_handle_t *handle, uint32_t *temperature_raw, float *temperature_c,
612 uint32_t *pressure_raw, float *pressure_mbar)
613{
614 uint8_t buf[3];
615
616 if (handle == NULL) /* check handle */
617 {
618 return 2; /* return error */
619 }
620 if (handle->inited != 1) /* check handle initialization */
621 {
622 return 3; /* return error */
623 }
624
625 if (a_ms5837_iic_write(handle, MS5837_CMD_D2 + (handle->temp_osr << 1), NULL, 0) != 0) /* sent d2 */
626 {
627 handle->debug_print("ms5837: sent d2 failed.\n"); /* sent d2 failed */
628
629 return 1; /* return error */
630 }
631 if (handle->temp_osr == MS5837_OSR_256) /* osr 256 */
632 {
633 handle->delay_ms(1); /* delay 1ms */
634 }
635 else if (handle->temp_osr == MS5837_OSR_512) /* osr 512 */
636 {
637 handle->delay_ms(2); /* delay 2ms */
638 }
639 else if (handle->temp_osr == MS5837_OSR_1024) /* osr 1024 */
640 {
641 handle->delay_ms(3); /* delay 3ms */
642 }
643 else if (handle->temp_osr == MS5837_OSR_2048) /* osr 2048 */
644 {
645 handle->delay_ms(5); /* delay 5ms */
646 }
647 else if (handle->temp_osr == MS5837_OSR_4096) /* osr 4096 */
648 {
649 handle->delay_ms(10); /* delay 10ms */
650 }
651 else /* osr 8192 */
652 {
653 handle->delay_ms(20); /* delay 20ms */
654 }
655 if (a_ms5837_iic_read(handle, MS5837_CMD_ADC_READ, buf, 3) != 0) /* read adc */
656 {
657 handle->debug_print("ms5837: read adc failed.\n"); /* read adc failed */
658
659 return 1; /* return error */
660 }
661 *temperature_raw = (((uint32_t)buf[0]) << 16) | (((uint32_t)buf[1]) << 8) | buf[2]; /* set the temperature raw */
662
663 if (a_ms5837_iic_write(handle, MS5837_CMD_D1 + (handle->press_osr << 1), NULL, 0) != 0) /* sent d1 */
664 {
665 handle->debug_print("ms5837: sent d1 failed.\n"); /* sent d1 failed */
666
667 return 1; /* return error */
668 }
669 if (handle->press_osr == MS5837_OSR_256) /* osr 256 */
670 {
671 handle->delay_ms(1); /* delay 1ms */
672 }
673 else if (handle->press_osr == MS5837_OSR_512) /* osr 512 */
674 {
675 handle->delay_ms(2); /* delay 2ms */
676 }
677 else if (handle->press_osr == MS5837_OSR_1024) /* osr 1024 */
678 {
679 handle->delay_ms(3); /* delay 3ms */
680 }
681 else if (handle->press_osr == MS5837_OSR_2048) /* osr 2048 */
682 {
683 handle->delay_ms(5); /* delay 5ms */
684 }
685 else if (handle->press_osr == MS5837_OSR_4096) /* osr 4096 */
686 {
687 handle->delay_ms(10); /* delay 10ms */
688 }
689 else /* osr 8192 */
690 {
691 handle->delay_ms(20); /* delay 20ms */
692 }
693 if (a_ms5837_iic_read(handle, MS5837_CMD_ADC_READ, buf, 3) != 0) /* read adc */
694 {
695 handle->debug_print("ms5837: read adc failed.\n"); /* read adc failed */
696
697 return 1; /* return error */
698 }
699 *pressure_raw = (((uint32_t)buf[0]) << 16) | (((uint32_t)buf[1]) << 8) | buf[2]; /* set the pressure raw */
700 a_ms5837_calculate_temperature_pressure(handle, *temperature_raw, temperature_c,
701 *pressure_raw, pressure_mbar); /* calculate temperature and pressure */
702
703 return 0; /* success return 0 */
704}
705
718uint8_t ms5837_read_pressure(ms5837_handle_t *handle, uint32_t *pressure_raw, float *pressure_mbar)
719{
720 uint8_t buf[3];
721 uint32_t temperature_raw;
722 float temperature_c;
723
724 if (handle == NULL) /* check handle */
725 {
726 return 2; /* return error */
727 }
728 if (handle->inited != 1) /* check handle initialization */
729 {
730 return 3; /* return error */
731 }
732
733 if (a_ms5837_iic_write(handle, MS5837_CMD_D2 + (handle->temp_osr << 1), NULL, 0) != 0) /* sent d2 */
734 {
735 handle->debug_print("ms5837: sent d2 failed.\n"); /* sent d2 failed */
736
737 return 1; /* return error */
738 }
739 if (handle->temp_osr == MS5837_OSR_256) /* osr 256 */
740 {
741 handle->delay_ms(1); /* delay 1ms */
742 }
743 else if (handle->temp_osr == MS5837_OSR_512) /* osr 512 */
744 {
745 handle->delay_ms(2); /* delay 2ms */
746 }
747 else if (handle->temp_osr == MS5837_OSR_1024) /* osr 1024 */
748 {
749 handle->delay_ms(3); /* delay 3ms */
750 }
751 else if (handle->temp_osr == MS5837_OSR_2048) /* osr 2048 */
752 {
753 handle->delay_ms(5); /* delay 5ms */
754 }
755 else if (handle->temp_osr == MS5837_OSR_4096) /* osr 4096 */
756 {
757 handle->delay_ms(10); /* delay 10ms */
758 }
759 else /* osr 8192 */
760 {
761 handle->delay_ms(20); /* delay 20ms */
762 }
763 if (a_ms5837_iic_read(handle, MS5837_CMD_ADC_READ, buf, 3) != 0) /* read adc */
764 {
765 handle->debug_print("ms5837: read adc failed.\n"); /* read adc failed */
766
767 return 1; /* return error */
768 }
769 temperature_raw = (((uint32_t)buf[0]) << 16) | (((uint32_t)buf[1]) << 8) | buf[2]; /* set the temperature raw */
770
771 if (a_ms5837_iic_write(handle, MS5837_CMD_D1 + (handle->press_osr << 1), NULL, 0) != 0) /* sent d1 */
772 {
773 handle->debug_print("ms5837: sent d1 failed.\n"); /* sent d1 failed */
774
775 return 1; /* return error */
776 }
777 if (handle->press_osr == MS5837_OSR_256) /* osr 256 */
778 {
779 handle->delay_ms(1); /* delay 1ms */
780 }
781 else if (handle->press_osr == MS5837_OSR_512) /* osr 512 */
782 {
783 handle->delay_ms(2); /* delay 2ms */
784 }
785 else if (handle->press_osr == MS5837_OSR_1024) /* osr 1024 */
786 {
787 handle->delay_ms(3); /* delay 3ms */
788 }
789 else if (handle->press_osr == MS5837_OSR_2048) /* osr 2048 */
790 {
791 handle->delay_ms(5); /* delay 5ms */
792 }
793 else if (handle->press_osr == MS5837_OSR_4096) /* osr 4096 */
794 {
795 handle->delay_ms(10); /* delay 10ms */
796 }
797 else /* osr 8192 */
798 {
799 handle->delay_ms(20); /* delay 20ms */
800 }
801 if (a_ms5837_iic_read(handle, MS5837_CMD_ADC_READ, buf, 3) != 0) /* read adc */
802 {
803 handle->debug_print("ms5837: read adc failed.\n"); /* read adc failed */
804
805 return 1; /* return error */
806 }
807 *pressure_raw = (((uint32_t)buf[0]) << 16) | (((uint32_t)buf[1]) << 8) | buf[2]; /* set the pressure raw */
808 a_ms5837_calculate_temperature_pressure(handle, temperature_raw, &temperature_c,
809 *pressure_raw, pressure_mbar); /* calculate temperature and pressure */
810
811 return 0; /* success return 0 */
812}
813
826uint8_t ms5837_read_temperature(ms5837_handle_t *handle, uint32_t *temperature_raw, float *temperature_c)
827{
828 uint8_t buf[3];
829
830 if (handle == NULL) /* check handle */
831 {
832 return 2; /* return error */
833 }
834 if (handle->inited != 1) /* check handle initialization */
835 {
836 return 3; /* return error */
837 }
838
839 if (a_ms5837_iic_write(handle, MS5837_CMD_D2 + (handle->temp_osr << 1), NULL, 0) != 0) /* sent d2 */
840 {
841 handle->debug_print("ms5837: sent d2 failed.\n"); /* sent d2 failed */
842
843 return 1; /* return error */
844 }
845 if (handle->temp_osr == MS5837_OSR_256) /* osr 256 */
846 {
847 handle->delay_ms(1); /* delay 1ms */
848 }
849 else if (handle->temp_osr == MS5837_OSR_512) /* osr 512 */
850 {
851 handle->delay_ms(2); /* delay 2ms */
852 }
853 else if (handle->temp_osr == MS5837_OSR_1024) /* osr 1024 */
854 {
855 handle->delay_ms(3); /* delay 3ms */
856 }
857 else if (handle->temp_osr == MS5837_OSR_2048) /* osr 2048 */
858 {
859 handle->delay_ms(5); /* delay 5ms */
860 }
861 else if (handle->temp_osr == MS5837_OSR_4096) /* osr 4096 */
862 {
863 handle->delay_ms(10); /* delay 10ms */
864 }
865 else /* osr 8192 */
866 {
867 handle->delay_ms(20); /* delay 20ms */
868 }
869 if (a_ms5837_iic_read(handle, MS5837_CMD_ADC_READ, buf, 3) != 0) /* read adc */
870 {
871 handle->debug_print("ms5837: read adc failed.\n"); /* read adc failed */
872
873 return 1; /* return error */
874 }
875 *temperature_raw = (((uint32_t)buf[0]) << 16) | (((uint32_t)buf[1]) << 8) | buf[2]; /* set the temperature raw */
876 a_ms5837_calculate_temperature(handle, *temperature_raw, temperature_c); /* calculate temperature and pressure */
877
878 return 0; /* success return 0 */
879}
880
892{
893 if (handle == NULL) /* check handle */
894 {
895 return 2; /* return error */
896 }
897 if (handle->inited != 1) /* check handle initialization */
898 {
899 return 3; /* return error */
900 }
901
902 if (a_ms5837_iic_write(handle, MS5837_CMD_RESET, NULL, 0) != 0) /* reset the device */
903 {
904 handle->debug_print("ms5837: reset failed.\n"); /* reset failed */
905
906 return 1; /* return error */
907 }
908 handle->delay_ms(10); /* delay 10 ms */
909
910 return 0; /* success return 0 */
911}
912
926uint8_t ms5837_set_reg(ms5837_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
927{
928 uint8_t res;
929
930 if (handle == NULL) /* check handle */
931 {
932 return 2; /* return error */
933 }
934 if (handle->inited != 1) /* check handle initialization */
935 {
936 return 3; /* return error */
937 }
938
939 res = a_ms5837_iic_write(handle, reg, buf, len); /* write data */
940 if (res != 0) /* check result */
941 {
942 handle->debug_print("ms5837: write register failed.\n"); /* write register failed */
943
944 return 1; /* return error */
945 }
946
947 return 0; /* success return 0 */
948}
949
963uint8_t ms5837_get_reg(ms5837_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
964{
965 uint8_t res;
966
967 if (handle == NULL) /* check handle */
968 {
969 return 2; /* return error */
970 }
971 if (handle->inited != 1) /* check handle initialization */
972 {
973 return 3; /* return error */
974 }
975
976 res = a_ms5837_iic_read(handle, reg, buf, len); /* read data */
977 if (res != 0) /* check result */
978 {
979 handle->debug_print("ms5837: read register failed.\n"); /* read register failed */
980
981 return 1; /* return error */
982 }
983
984 return 0; /* success return 0 */
985}
986
996{
997 if (info == NULL) /* check handle */
998 {
999 return 2; /* return error */
1000 }
1001
1002 memset(info, 0, sizeof(ms5837_info_t)); /* initialize ms5837 info structure */
1003 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
1004 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
1005 strncpy(info->interface, "IIC", 8); /* copy interface name */
1006 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
1007 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
1008 info->max_current_ma = MAX_CURRENT; /* set maximum current */
1009 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
1010 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
1011 info->driver_version = DRIVER_VERSION; /* set driver version */
1012
1013 return 0; /* success return 0 */
1014}
#define MS5837_CMD_PROM_READ
#define MS5837_CMD_ADC_READ
#define MAX_CURRENT
#define MS5837_ADDRESS
chip address definition
#define SUPPLY_VOLTAGE_MAX
#define MS5837_CMD_D1
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define MS5837_CMD_D2
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
#define MS5837_CMD_RESET
chip command definition
driver ms5837 header file
ms5837_type_t
ms5837 type enumeration definition
uint8_t ms5837_read_temperature_pressure(ms5837_handle_t *handle, uint32_t *temperature_raw, float *temperature_c, uint32_t *pressure_raw, float *pressure_mbar)
read the temperature and pressure
uint8_t ms5837_deinit(ms5837_handle_t *handle)
close the chip
uint8_t ms5837_get_type(ms5837_handle_t *handle, ms5837_type_t *type)
get the device type
uint8_t ms5837_reset(ms5837_handle_t *handle)
reset the device
uint8_t ms5837_set_pressure_osr(ms5837_handle_t *handle, ms5837_osr_t osr)
set the adc pressure osr
struct ms5837_handle_s ms5837_handle_t
ms5837 handle structure definition
uint8_t ms5837_get_temperature_osr(ms5837_handle_t *handle, ms5837_osr_t *osr)
get the adc temperature osr
uint8_t ms5837_info(ms5837_info_t *info)
get chip's information
uint8_t ms5837_set_type(ms5837_handle_t *handle, ms5837_type_t type)
set the device type
uint8_t ms5837_read_temperature(ms5837_handle_t *handle, uint32_t *temperature_raw, float *temperature_c)
read the temperature
ms5837_osr_t
ms5837 osr enumeration definition
uint8_t ms5837_get_pressure_osr(ms5837_handle_t *handle, ms5837_osr_t *osr)
get the adc pressure osr
uint8_t ms5837_read_pressure(ms5837_handle_t *handle, uint32_t *pressure_raw, float *pressure_mbar)
read the pressure
struct ms5837_info_s ms5837_info_t
ms5837 information structure definition
uint8_t ms5837_init(ms5837_handle_t *handle)
initialize the chip
uint8_t ms5837_set_temperature_osr(ms5837_handle_t *handle, ms5837_osr_t osr)
set the adc temperature osr
@ MS5837_TYPE_02BA21
@ MS5837_TYPE_02BA01
@ MS5837_TYPE_30BA26
@ MS5837_OSR_256
@ MS5837_OSR_1024
@ MS5837_OSR_4096
@ MS5837_OSR_512
@ MS5837_OSR_2048
uint8_t ms5837_get_reg(ms5837_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
get the chip register
uint8_t ms5837_set_reg(ms5837_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
set the chip register
void(* delay_ms)(uint32_t ms)
uint16_t c[6]
uint8_t prom[16]
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
uint8_t(* iic_write)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_read)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_deinit)(void)
float supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v
char chip_name[32]