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 -40.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
531{
532 if (handle == NULL) /* check handle */
533 {
534 return 2; /* return error */
535 }
536 if (handle->inited != 1) /* check handle initialization */
537 {
538 return 3; /* return error */
539 }
540 if ((handle->type == MS5837_TYPE_30BA26)
541 && (osr == MS5837_OSR_8192) /* check the osr */
542 )
543 {
544 handle->debug_print("ms5837: 30ba26 can't support osr 8192.\n"); /* 30ba26 can't support osr 8192 */
545
546 return 4; /* return error */
547 }
548
549 handle->temp_osr = (uint8_t)(osr); /* set the osr */
550
551 return 0; /* success return 0 */
552}
553
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 *osr = (ms5837_osr_t)(handle->press_osr); /* get the osr */
576
577 return 0; /* success return 0 */
578}
579
592{
593 if (handle == NULL) /* check handle */
594 {
595 return 2; /* return error */
596 }
597 if (handle->inited != 1) /* check handle initialization */
598 {
599 return 3; /* return error */
600 }
601 if ((handle->type == MS5837_TYPE_30BA26)
602 && (osr == MS5837_OSR_8192) /* check the osr */
603 )
604 {
605 handle->debug_print("ms5837: 30ba26 can't support osr 8192.\n"); /* 30ba26 can't support osr 8192 */
606
607 return 4; /* return error */
608 }
609
610 handle->press_osr = (uint8_t)(osr); /* set the osr */
611
612 return 0; /* success return 0 */
613}
614
629uint8_t ms5837_read_temperature_pressure(ms5837_handle_t *handle, uint32_t *temperature_raw, float *temperature_c,
630 uint32_t *pressure_raw, float *pressure_mbar)
631{
632 uint8_t buf[3];
633
634 if (handle == NULL) /* check handle */
635 {
636 return 2; /* return error */
637 }
638 if (handle->inited != 1) /* check handle initialization */
639 {
640 return 3; /* return error */
641 }
642
643 if (a_ms5837_iic_write(handle, MS5837_CMD_D2 + handle->temp_osr, NULL, 0) != 0) /* sent d2 */
644 {
645 handle->debug_print("ms5837: sent d2 failed.\n"); /* sent d2 failed */
646
647 return 1; /* return error */
648 }
649 if (handle->temp_osr == MS5837_OSR_256) /* osr 256 */
650 {
651 handle->delay_ms(1); /* delay 1ms */
652 }
653 else if (handle->temp_osr == MS5837_OSR_512) /* osr 512 */
654 {
655 handle->delay_ms(2); /* delay 2ms */
656 }
657 else if (handle->temp_osr == MS5837_OSR_1024) /* osr 1024 */
658 {
659 handle->delay_ms(3); /* delay 3ms */
660 }
661 else if (handle->temp_osr == MS5837_OSR_2048) /* osr 2048 */
662 {
663 handle->delay_ms(5); /* delay 5ms */
664 }
665 else if (handle->temp_osr == MS5837_OSR_4096) /* osr 4096 */
666 {
667 handle->delay_ms(9); /* delay 9ms */
668 }
669 else /* osr 8192 */
670 {
671 handle->delay_ms(18); /* delay 18ms */
672 }
673 if (a_ms5837_iic_read(handle, MS5837_CMD_ADC_READ, buf, 3) != 0) /* read adc */
674 {
675 handle->debug_print("ms5837: read adc failed.\n"); /* read adc failed */
676
677 return 1; /* return error */
678 }
679 *temperature_raw = (((uint32_t)buf[0]) << 16) | (((uint32_t)buf[1]) << 8) | buf[2]; /* set the temperature raw */
680
681 if (a_ms5837_iic_write(handle, MS5837_CMD_D1 + handle->press_osr, NULL, 0) != 0) /* sent d1 */
682 {
683 handle->debug_print("ms5837: sent d1 failed.\n"); /* sent d1 failed */
684
685 return 1; /* return error */
686 }
687 if (handle->press_osr == MS5837_OSR_256) /* osr 256 */
688 {
689 handle->delay_ms(1); /* delay 1ms */
690 }
691 else if (handle->press_osr == MS5837_OSR_512) /* osr 512 */
692 {
693 handle->delay_ms(2); /* delay 2ms */
694 }
695 else if (handle->press_osr == MS5837_OSR_1024) /* osr 1024 */
696 {
697 handle->delay_ms(3); /* delay 3ms */
698 }
699 else if (handle->press_osr == MS5837_OSR_2048) /* osr 2048 */
700 {
701 handle->delay_ms(5); /* delay 5ms */
702 }
703 else if (handle->press_osr == MS5837_OSR_4096) /* osr 4096 */
704 {
705 handle->delay_ms(9); /* delay 9ms */
706 }
707 else /* osr 8192 */
708 {
709 handle->delay_ms(18); /* delay 18ms */
710 }
711 if (a_ms5837_iic_read(handle, MS5837_CMD_ADC_READ, buf, 3) != 0) /* read adc */
712 {
713 handle->debug_print("ms5837: read adc failed.\n"); /* read adc failed */
714
715 return 1; /* return error */
716 }
717 *pressure_raw = (((uint32_t)buf[0]) << 16) | (((uint32_t)buf[1]) << 8) | buf[2]; /* set the temperature raw */
718 a_ms5837_calculate_temperature_pressure(handle, *temperature_raw, temperature_c,
719 *pressure_raw, pressure_mbar); /* calculate temperature and pressure */
720
721 return 0; /* success return 0 */
722}
723
736uint8_t ms5837_read_pressure(ms5837_handle_t *handle, uint32_t *pressure_raw, float *pressure_mbar)
737{
738 uint8_t buf[3];
739 uint32_t temperature_raw;
740 float temperature_c;
741
742 if (handle == NULL) /* check handle */
743 {
744 return 2; /* return error */
745 }
746 if (handle->inited != 1) /* check handle initialization */
747 {
748 return 3; /* return error */
749 }
750
751 if (a_ms5837_iic_write(handle, MS5837_CMD_D2 + handle->temp_osr, NULL, 0) != 0) /* sent d2 */
752 {
753 handle->debug_print("ms5837: sent d2 failed.\n"); /* sent d2 failed */
754
755 return 1; /* return error */
756 }
757 if (handle->temp_osr == MS5837_OSR_256) /* osr 256 */
758 {
759 handle->delay_ms(1); /* delay 1ms */
760 }
761 else if (handle->temp_osr == MS5837_OSR_512) /* osr 512 */
762 {
763 handle->delay_ms(2); /* delay 2ms */
764 }
765 else if (handle->temp_osr == MS5837_OSR_1024) /* osr 1024 */
766 {
767 handle->delay_ms(3); /* delay 3ms */
768 }
769 else if (handle->temp_osr == MS5837_OSR_2048) /* osr 2048 */
770 {
771 handle->delay_ms(5); /* delay 5ms */
772 }
773 else if (handle->temp_osr == MS5837_OSR_4096) /* osr 4096 */
774 {
775 handle->delay_ms(9); /* delay 9ms */
776 }
777 else /* osr 8192 */
778 {
779 handle->delay_ms(18); /* delay 18ms */
780 }
781 if (a_ms5837_iic_read(handle, MS5837_CMD_ADC_READ, buf, 3) != 0) /* read adc */
782 {
783 handle->debug_print("ms5837: read adc failed.\n"); /* read adc failed */
784
785 return 1; /* return error */
786 }
787 temperature_raw = (((uint32_t)buf[0]) << 16) | (((uint32_t)buf[1]) << 8) | buf[2]; /* set the temperature raw */
788
789 if (a_ms5837_iic_write(handle, MS5837_CMD_D1 + handle->press_osr, NULL, 0) != 0) /* sent d1 */
790 {
791 handle->debug_print("ms5837: sent d1 failed.\n"); /* sent d1 failed */
792
793 return 1; /* return error */
794 }
795 if (handle->press_osr == MS5837_OSR_256) /* osr 256 */
796 {
797 handle->delay_ms(1); /* delay 1ms */
798 }
799 else if (handle->press_osr == MS5837_OSR_512) /* osr 512 */
800 {
801 handle->delay_ms(2); /* delay 2ms */
802 }
803 else if (handle->press_osr == MS5837_OSR_1024) /* osr 1024 */
804 {
805 handle->delay_ms(3); /* delay 3ms */
806 }
807 else if (handle->press_osr == MS5837_OSR_2048) /* osr 2048 */
808 {
809 handle->delay_ms(5); /* delay 5ms */
810 }
811 else if (handle->press_osr == MS5837_OSR_4096) /* osr 4096 */
812 {
813 handle->delay_ms(9); /* delay 9ms */
814 }
815 else /* osr 8192 */
816 {
817 handle->delay_ms(18); /* delay 18ms */
818 }
819 if (a_ms5837_iic_read(handle, MS5837_CMD_ADC_READ, buf, 3) != 0) /* read adc */
820 {
821 handle->debug_print("ms5837: read adc failed.\n"); /* read adc failed */
822
823 return 1; /* return error */
824 }
825 *pressure_raw = (((uint32_t)buf[0]) << 16) | (((uint32_t)buf[1]) << 8) | buf[2]; /* set the temperature raw */
826 a_ms5837_calculate_temperature_pressure(handle, temperature_raw, &temperature_c,
827 *pressure_raw, pressure_mbar); /* calculate temperature and pressure */
828
829 return 0; /* success return 0 */
830}
831
844uint8_t ms5837_read_temperature(ms5837_handle_t *handle, uint32_t *temperature_raw, float *temperature_c)
845{
846 uint8_t buf[3];
847
848 if (handle == NULL) /* check handle */
849 {
850 return 2; /* return error */
851 }
852 if (handle->inited != 1) /* check handle initialization */
853 {
854 return 3; /* return error */
855 }
856
857 if (a_ms5837_iic_write(handle, MS5837_CMD_D2 + handle->temp_osr, NULL, 0) != 0) /* sent d2 */
858 {
859 handle->debug_print("ms5837: sent d2 failed.\n"); /* sent d2 failed */
860
861 return 1; /* return error */
862 }
863 if (handle->temp_osr == MS5837_OSR_256) /* osr 256 */
864 {
865 handle->delay_ms(1); /* delay 1ms */
866 }
867 else if (handle->temp_osr == MS5837_OSR_512) /* osr 512 */
868 {
869 handle->delay_ms(2); /* delay 2ms */
870 }
871 else if (handle->temp_osr == MS5837_OSR_1024) /* osr 1024 */
872 {
873 handle->delay_ms(3); /* delay 3ms */
874 }
875 else if (handle->temp_osr == MS5837_OSR_2048) /* osr 2048 */
876 {
877 handle->delay_ms(5); /* delay 5ms */
878 }
879 else if (handle->temp_osr == MS5837_OSR_4096) /* osr 4096 */
880 {
881 handle->delay_ms(9); /* delay 9ms */
882 }
883 else /* osr 8192 */
884 {
885 handle->delay_ms(18); /* delay 18ms */
886 }
887 if (a_ms5837_iic_read(handle, MS5837_CMD_ADC_READ, buf, 3) != 0) /* read adc */
888 {
889 handle->debug_print("ms5837: read adc failed.\n"); /* read adc failed */
890
891 return 1; /* return error */
892 }
893 *temperature_raw = (((uint32_t)buf[0]) << 16) | (((uint32_t)buf[1]) << 8) | buf[2]; /* set the temperature raw */
894 a_ms5837_calculate_temperature(handle, *temperature_raw, temperature_c); /* calculate temperature and pressure */
895
896 return 0; /* success return 0 */
897}
898
910{
911 if (handle == NULL) /* check handle */
912 {
913 return 2; /* return error */
914 }
915 if (handle->inited != 1) /* check handle initialization */
916 {
917 return 3; /* return error */
918 }
919
920 if (a_ms5837_iic_write(handle, MS5837_CMD_RESET, NULL, 0) != 0) /* reset the device */
921 {
922 handle->debug_print("ms5837: reset failed.\n"); /* reset failed */
923
924 return 1; /* return error */
925 }
926 handle->delay_ms(10); /* delay 10 ms */
927
928 return 0; /* success return 0 */
929}
930
944uint8_t ms5837_set_reg(ms5837_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
945{
946 uint8_t res;
947
948 if (handle == NULL) /* check handle */
949 {
950 return 2; /* return error */
951 }
952 if (handle->inited != 1) /* check handle initialization */
953 {
954 return 3; /* return error */
955 }
956
957 res = a_ms5837_iic_write(handle, reg, buf, len); /* write data */
958 if (res != 0) /* check result */
959 {
960 handle->debug_print("ms5837: write register failed.\n"); /* write register failed */
961
962 return 1; /* return error */
963 }
964
965 return 0; /* success return 0 */
966}
967
981uint8_t ms5837_get_reg(ms5837_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
982{
983 uint8_t res;
984
985 if (handle == NULL) /* check handle */
986 {
987 return 2; /* return error */
988 }
989 if (handle->inited != 1) /* check handle initialization */
990 {
991 return 3; /* return error */
992 }
993
994 res = a_ms5837_iic_read(handle, reg, buf, len); /* read data */
995 if (res != 0) /* check result */
996 {
997 handle->debug_print("ms5837: read register failed.\n"); /* read register failed */
998
999 return 1; /* return error */
1000 }
1001
1002 return 0; /* success return 0 */
1003}
1004
1014{
1015 if (info == NULL) /* check handle */
1016 {
1017 return 2; /* return error */
1018 }
1019
1020 memset(info, 0, sizeof(ms5837_info_t)); /* initialize ms5837 info structure */
1021 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
1022 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
1023 strncpy(info->interface, "IIC", 8); /* copy interface name */
1024 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
1025 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
1026 info->max_current_ma = MAX_CURRENT; /* set maximum current */
1027 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
1028 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
1029 info->driver_version = DRIVER_VERSION; /* set driver version */
1030
1031 return 0; /* success return 0 */
1032}
#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_8192
@ 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]