Paparazzi UAS  v4.0.4_stable-3-gf39211a
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
baro_ms5611_i2c.c
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Copyright (C) 2011 Martin Mueller <martinmm@pfump.org>
5  *
6  * This file is part of paparazzi.
7  *
8  * paparazzi is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * paparazzi is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with paparazzi; see the file COPYING. If not, write to
20  * the Free Software Foundation, 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  *
23  */
24 
32 
33 #include "mcu_periph/sys_time.h"
34 #include "mcu_periph/i2c.h"
35 #include "mcu_periph/uart.h"
36 #include "messages.h"
38 
39 #ifndef DOWNLINK_DEVICE
40 #define DOWNLINK_DEVICE DOWNLINK_AP_DEVICE
41 #endif
42 
43 #ifndef MS5611_I2C_DEV
44 #define MS5611_I2C_DEV i2c0
45 #endif
46 
47 /* address can be 0xEC or 0xEE (CSB\ high = 0xEC) */
48 #define MS5611_SLAVE_ADDR 0xEC
49 
50 #if PERIODIC_FREQUENCY > 60
51 #error baro_ms5611_i2c assumes a PERIODIC_FREQUENCY of 60Hz
52 #endif
53 
60 
61 
63  int32_t i, j;
64  uint32_t res = 0;
65  uint8_t crc = prom[7] & 0xF;
66  prom[7] &= 0xFF00;
67  for (i = 0; i < 16; i++) {
68  if (i & 1) res ^= ((prom[i>>1]) & 0x00FF);
69  else res ^= (prom[i>>1]>>8);
70  for (j = 8; j > 0; j--) {
71  if (res & 0x8000) res ^= 0x1800;
72  res <<= 1;
73  }
74  }
75  prom[7] |= crc;
76  if (crc == ((res >> 12) & 0xF)) return 0;
77  else return -1;
78 }
79 
80 void baro_ms5611_init(void) {
82  prom_cnt = 0;
83 }
84 
85 void baro_ms5611_periodic( void ) {
86  if (cpu_time_sec > 1) {
87  if (ms5611_status >= MS5611_IDLE) {
88  /* start D1 conversion */
92  RunOnceEvery((4*30), DOWNLINK_SEND_MS5611_COEFF(DefaultChannel, DefaultDevice,
93  &ms5611_c[0], &ms5611_c[1], &ms5611_c[2], &ms5611_c[3],
94  &ms5611_c[4], &ms5611_c[5], &ms5611_c[6], &ms5611_c[7]));
95  }
96  else if (ms5611_status == MS5611_UNINIT) {
97  /* reset sensor */
101  }
102  else if (ms5611_status == MS5611_RESET_OK) {
103  /* start getting prom data */
107  }
108  }
109 }
110 
111 void baro_ms5611_d1( void ) {
112  if (cpu_time_sec > 1) {
114  /* read D1 adc */
118  }
119  }
120 }
121 
122 void baro_ms5611_d2( void ) {
123  if (cpu_time_sec > 1) {
125  /* read D2 adc */
129  }
130  }
131 }
132 
133 void baro_ms5611_event( void ) {
135  switch (ms5611_status) {
136 
137  case MS5611_RESET:
140  break;
141 
142  case MS5611_PROM:
143  /* read prom data */
144  ms5611_c[prom_cnt++] = (ms5611_trans.buf[0] << 8) | ms5611_trans.buf[1];
145  if (prom_cnt < PROM_NB) {
146  /* get next prom data */
149  }
150  else {
151  /* done reading prom */
153  /* check prom crc */
154  if (baro_ms5611_crc(ms5611_c) == 0) {
155  DOWNLINK_SEND_MS5611_COEFF(DefaultChannel, DefaultDevice,
156  &ms5611_c[0], &ms5611_c[1], &ms5611_c[2], &ms5611_c[3],
157  &ms5611_c[4], &ms5611_c[5], &ms5611_c[6], &ms5611_c[7]);
159  }
160  else {
161  /* checksum error, try again */
163  }
164  }
165  break;
166 
167  case MS5611_CONV_D1:
170  break;
171 
172  case MS5611_ADC_D1:
173  /* read D1 (pressure) */
174  ms5611_d1 = (ms5611_trans.buf[0] << 16) |
175  (ms5611_trans.buf[1] << 8) |
176  ms5611_trans.buf[2];
177  /* start D2 conversion */
181  break;
182 
183  case MS5611_CONV_D2:
186  break;
187 
188  case MS5611_ADC_D2: {
189  int64_t dt, baroms, tempms, off, sens, t2, off2, sens2;
190  /* read D2 (temperature) */
191  ms5611_d2 = (ms5611_trans.buf[0] << 16) |
192  (ms5611_trans.buf[1] << 8) |
193  ms5611_trans.buf[2];
196 
197  /* difference between actual and ref temperature */
198  dt = ms5611_d2 - (int64_t)ms5611_c[5] * (1<<8);
199  /* actual temperature */
200  tempms = 2000 + ((int64_t)dt * ms5611_c[6]) / (1<<23);
201  /* offset at actual temperature */
202  off = ((int64_t)ms5611_c[2] * (1<<16)) + ((int64_t)ms5611_c[4] * dt) / (1<<7);
203  /* sensitivity at actual temperature */
204  sens = ((int64_t)ms5611_c[1] * (1<<15)) + ((int64_t)ms5611_c[3] * dt) / (1<<8);
205  /* second order temperature compensation */
206  if (tempms < 2000) {
207  t2 = (dt*dt) / (1<<31);
208  off2 = 5 * ((int64_t)(tempms-2000)*(tempms-2000)) / (1<<1);
209  sens2 = 5 * ((int64_t)(tempms-2000)*(tempms-2000)) / (1<<2);
210  if (tempms < -1500) {
211  off2 = off2 + 7 * (int64_t)(tempms+1500)*(tempms+1500);
212  sens2 = sens2 + 11 * ((int64_t)(tempms+1500)*(tempms+1500)) / (1<<1);
213  }
214  tempms = tempms - t2;
215  off = off - off2;
216  sens = sens - sens2;
217  }
218  /* temperature compensated pressure */
219  baroms = (((int64_t)ms5611_d1 * sens) / (1<<21) - off) / (1<<15);
220 #ifdef SENSOR_SYNC_SEND
221  ftempms = tempms / 100.;
222  fbaroms = baroms / 100.;
223  DOWNLINK_SEND_BARO_MS5611(DefaultChannel, DefaultDevice,
225 #endif
226  break;
227  }
228 
229  default:
231  break;
232  }
233  }
234 }
235 
#define PROM_NB
unsigned short uint16_t
Definition: types.h:16
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
#define MS5611_SOFT_RESET
#define MS5611_START_CONV_D1
void baro_ms5611_init(void)
#define I2CTransmit(_p, _t, _s_addr, _len)
Definition: i2c.h:148
float fbaroms
#define cpu_time_sec
Definition: sys_time.h:64
signed long long int64_t
Definition: types.h:21
struct i2c_transaction ms5611_trans
#define MS5611_START_CONV_D2
void baro_ms5611_event(void)
static int8_t baro_ms5611_crc(uint16_t *prom)
float ftempms
Architecture independent timing functions.
#define MS5611_PROM_READ
unsigned long uint32_t
Definition: types.h:18
#define MS5611_ADC_READ
uint16_t ms5611_c[PROM_NB]
enum I2CTransactionStatus status
Definition: i2c.h:47
signed long int32_t
Definition: types.h:19
volatile uint8_t buf[I2C_BUF_LEN]
Definition: i2c.h:46
uint32_t ms5611_d1
unsigned char uint8_t
Definition: types.h:14
int32_t prom_cnt
#define MS5611_SLAVE_ADDR
#define I2CTransceive(_p, _t, _s_addr, _len_w, _len_r)
Definition: i2c.h:156
void baro_ms5611_periodic(void)
void baro_ms5611_d2(void)
struct i2c_transaction t2
#define MS5611_I2C_DEV
void baro_ms5611_d1(void)
signed char int8_t
Definition: types.h:15
uint32_t ms5611_d2
uint8_t ms5611_status