Paparazzi UAS  v5.2.2_stable-0-gd6b9f29
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
ms5611.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011 Martin Mueller <martinmm@pfump.org>
3  * Copyright (C) 2013 Felix Ruess <felix.ruess@gmail.com>
4  *
5  * This file is part of paparazzi.
6  *
7  * paparazzi is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * paparazzi is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with paparazzi; see the file COPYING. If not, write to
19  * the Free Software Foundation, 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22 
29 #include "peripherals/ms5611.h"
30 #include "std.h"
31 
37  int32_t i, j;
38  uint32_t res = 0;
39  uint8_t crc = prom[7] & 0xF;
40  prom[7] &= 0xFF00;
41  for (i = 0; i < 16; i++) {
42  if (i & 1)
43  res ^= ((prom[i>>1]) & 0x00FF);
44  else
45  res ^= (prom[i>>1]>>8);
46  for (j = 8; j > 0; j--) {
47  if (res & 0x8000)
48  res ^= 0x1800;
49  res <<= 1;
50  }
51  }
52  prom[7] |= crc;
53  if (crc == ((res >> 12) & 0xF))
54  return TRUE;
55  else
56  return FALSE;
57 }
58 
63 bool_t ms5611_calc(struct Ms5611Data* ms) {
64  int64_t dt, tempms, off, sens, t2, off2, sens2;
65 
66  /* difference between actual and ref temperature */
67  dt = ms->d2 - (int64_t)ms->c[5] * (1<<8);
68  /* actual temperature */
69  tempms = 2000 + ((int64_t)dt * ms->c[6]) / (1<<23);
70  /* offset at actual temperature */
71  off = ((int64_t)ms->c[2] * (1<<16)) + ((int64_t)ms->c[4] * dt) / (1<<7);
72  /* sensitivity at actual temperature */
73  sens = ((int64_t)ms->c[1] * (1<<15)) + ((int64_t)ms->c[3] * dt) / (1<<8);
74  /* second order temperature compensation */
75  if (tempms < 2000) {
76  t2 = (dt*dt) / (1<<31);
77  off2 = 5 * ((int64_t)(tempms-2000)*(tempms-2000)) / (1<<1);
78  sens2 = 5 * ((int64_t)(tempms-2000)*(tempms-2000)) / (1<<2);
79  if (tempms < -1500) {
80  off2 = off2 + 7 * (int64_t)(tempms+1500)*(tempms+1500);
81  sens2 = sens2 + 11 * ((int64_t)(tempms+1500)*(tempms+1500)) / (1<<1);
82  }
83  tempms = tempms - t2;
84  off = off - off2;
85  sens = sens - sens2;
86  }
87 
88  /* temperature compensated pressure in Pascal (0.01mbar) */
89  uint32_t p = (((int64_t)ms->d1 * sens) / (1<<21) - off) / (1<<15);
90  /* if temp and pressare are in valid bounds, copy and return TRUE (valid) */
91  if ((tempms > -4000) && (tempms < 8500) && (p > 1000 ) && (p < 120000)) {
92  /* temperature in deg Celsius with 0.01 degC resolultion */
93  ms->temperature = (int32_t)tempms;
94  ms->pressure = p;
95  return TRUE;
96  }
97  return FALSE;
98 }
unsigned short uint16_t
Definition: types.h:16
uint32_t d1
Definition: ms5611.h:55
int32_t temperature
temperature with 0.01 degrees Celsius resolution
Definition: ms5611.h:53
static const float dt
signed long long int64_t
Definition: types.h:21
bool_t ms5611_prom_crc_ok(uint16_t *prom)
Check if CRC of PROM data is OK.
Definition: ms5611.c:36
#define FALSE
Definition: imu_chimu.h:141
MS5611 barometer driver common interface (I2C and SPI).
unsigned long uint32_t
Definition: types.h:18
signed long int32_t
Definition: types.h:19
#define TRUE
Definition: imu_chimu.h:144
unsigned char uint8_t
Definition: types.h:14
static float p[2][2]
uint16_t c[PROM_NB]
Definition: ms5611.h:54
uint32_t pressure
pressure in Pascal (0.01mbar)
Definition: ms5611.h:52
bool_t ms5611_calc(struct Ms5611Data *ms)
Calculate temperature and compensated pressure.
Definition: ms5611.c:63
uint32_t d2
Definition: ms5611.h:56