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_i2c.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 
30 #include "peripherals/ms5611_i2c.h"
31 
32 
33 void ms5611_i2c_init(struct Ms5611_I2c *ms, struct i2c_periph *i2c_p, uint8_t addr)
34 {
35  /* set i2c_peripheral */
36  ms->i2c_p = i2c_p;
37 
38  /* slave address */
39  ms->i2c_trans.slave_addr = addr;
40  /* set initial status: Success or Done */
42 
43  ms->data_available = FALSE;
44  ms->initialized = FALSE;
46  ms->prom_cnt = 0;
47 }
48 
50 {
51  if (ms->status == MS5611_STATUS_UNINIT) {
52  ms->initialized = FALSE;
53  ms->prom_cnt = 0;
55  i2c_transmit(ms->i2c_p, &(ms->i2c_trans), ms->i2c_trans.slave_addr, 1);
57  }
58 }
59 
61 {
62  if (ms->status == MS5611_STATUS_IDLE &&
63  ms->i2c_trans.status == I2CTransDone) {
64  /* start D1 conversion */
66  i2c_transmit(ms->i2c_p, &(ms->i2c_trans), ms->i2c_trans.slave_addr, 1);
68  }
69 }
70 
77 {
78  switch (ms->status) {
81  break;
83  if (ms->i2c_trans.status == I2CTransDone) {
84  /* start getting prom data */
85  ms->i2c_trans.buf[0] = MS5611_PROM_READ | (ms->prom_cnt << 1);
86  i2c_transceive(ms->i2c_p, &(ms->i2c_trans), ms->i2c_trans.slave_addr, 1, 2);
88  }
89  break;
92  break;
94  if (ms->i2c_trans.status == I2CTransDone) {
95  /* read D1 adc */
96  ms->i2c_trans.buf[0] = MS5611_ADC_READ;
97  i2c_transceive(ms->i2c_p, &(ms->i2c_trans), ms->i2c_trans.slave_addr, 1, 3);
99  }
100  break;
103  break;
105  if (ms->i2c_trans.status == I2CTransDone) {
106  /* read D2 adc */
107  ms->i2c_trans.buf[0] = MS5611_ADC_READ;
108  i2c_transceive(ms->i2c_p, &(ms->i2c_trans), ms->i2c_trans.slave_addr, 1, 3);
110  }
111  break;
112  default:
113  break;
114  }
115 }
116 
117 void ms5611_i2c_event(struct Ms5611_I2c *ms) {
118  if (ms->initialized) {
119  if (ms->i2c_trans.status == I2CTransFailed) {
122  }
123  else if (ms->i2c_trans.status == I2CTransSuccess) {
124  // Successfull reading
125  switch (ms->status) {
126 
128  /* read D1 (pressure) */
129  ms->data.d1 = (ms->i2c_trans.buf[0] << 16) |
130  (ms->i2c_trans.buf[1] << 8) |
131  ms->i2c_trans.buf[2];
132  if (ms->data.d1 == 0) {
133  /* if value is zero, it was read to soon and is invalid, back to idle */
135  }
136  else {
137  /* start D2 conversion */
139  i2c_transmit(ms->i2c_p, &(ms->i2c_trans), ms->i2c_trans.slave_addr, 1);
141  }
142  break;
143 
145  /* read D2 (temperature) */
146  ms->data.d2 = (ms->i2c_trans.buf[0] << 16) |
147  (ms->i2c_trans.buf[1] << 8) |
148  ms->i2c_trans.buf[2];
149  if (ms->data.d2 == 0) {
150  /* if value is zero, it was read to soon and is invalid, back to idle */
152  }
153  else {
154  /* calculate temp and pressure from measurements and set available if valid */
155  if (ms5611_calc(&(ms->data)))
156  ms->data_available = TRUE;
158  }
159  break;
160 
161  default:
162  break;
163  }
165  }
166  }
167  else if (ms->status != MS5611_STATUS_UNINIT) { // Configuring but not yet initialized
168  switch (ms->i2c_trans.status) {
169 
170  case I2CTransFailed:
171  /* try again */
174  break;
175 
176  case I2CTransSuccess:
177  if (ms->status == MS5611_STATUS_PROM) {
178  /* read prom data */
179  ms->data.c[ms->prom_cnt++] = (ms->i2c_trans.buf[0] << 8) |
180  ms->i2c_trans.buf[1];
181  if (ms->prom_cnt < PROM_NB) {
182  /* get next prom data */
183  ms->i2c_trans.buf[0] = MS5611_PROM_READ | (ms->prom_cnt << 1);
184  i2c_transceive(ms->i2c_p, &(ms->i2c_trans), ms->i2c_trans.slave_addr, 1, 2);
185  }
186  else {
187  /* done reading prom, check prom crc */
188  if (ms5611_prom_crc_ok(ms->data.c)) {
189  ms->initialized = TRUE;
191  }
192  else {
193  /* checksum error, try again */
195  }
196  }
197  }
199  break;
200 
201  default:
202  break;
203  }
204  }
205 }
#define PROM_NB
Definition: ms5611_regs.h:38
uint32_t d1
Definition: ms5611.h:55
transaction successfully finished by I2C driver
Definition: i2c.h:57
volatile bool_t data_available
data ready flag
Definition: ms5611_i2c.h:41
void ms5611_i2c_init(struct Ms5611_I2c *ms, struct i2c_periph *i2c_p, uint8_t addr)
Definition: ms5611_i2c.c:33
struct i2c_transaction i2c_trans
Definition: ms5611_i2c.h:38
bool_t i2c_transceive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len_w, uint16_t len_r)
Submit a write/read transaction.
Definition: i2c.c:258
if(PrimarySpektrumState.SpektrumTimer)--PrimarySpektrumState.SpektrumTimer
bool_t ms5611_prom_crc_ok(uint16_t *prom)
Check if CRC of PROM data is OK.
Definition: ms5611.c:36
uint8_t slave_addr
Slave address.
Definition: i2c.h:104
void ms5611_i2c_start_configure(struct Ms5611_I2c *ms)
Definition: ms5611_i2c.c:49
#define FALSE
Definition: imu_chimu.h:141
#define MS5611_START_CONV_D1
Definition: ms5611_regs.h:69
struct Ms5611Data data
Definition: ms5611_i2c.h:42
#define MS5611_ADC_READ
Definition: ms5611_regs.h:66
transaction set to done by user level
Definition: i2c.h:59
Measurement Specialties (Intersema) MS5611-01BA pressure/temperature sensor interface for I2C...
bool_t i2c_transmit(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len)
Submit a write only transaction.
Definition: i2c.c:238
void ms5611_i2c_event(struct Ms5611_I2c *ms)
Definition: ms5611_i2c.c:117
transaction failed
Definition: i2c.h:58
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
#define TRUE
Definition: imu_chimu.h:144
void ms5611_i2c_start_conversion(struct Ms5611_I2c *ms)
Definition: ms5611_i2c.c:60
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
unsigned char uint8_t
Definition: types.h:14
I2C peripheral structure.
Definition: i2c.h:138
#define MS5611_PROM_READ
Definition: ms5611_regs.h:68
bool_t initialized
config done flag
Definition: ms5611_i2c.h:40
struct i2c_periph * i2c_p
Definition: ms5611_i2c.h:37
uint16_t c[PROM_NB]
Definition: ms5611.h:54
bool_t ms5611_calc(struct Ms5611Data *ms)
Calculate temperature and compensated pressure.
Definition: ms5611.c:63
int32_t prom_cnt
number of bytes read from PROM
Definition: ms5611_i2c.h:43
void ms5611_i2c_periodic_check(struct Ms5611_I2c *ms)
Periodic function to ensure proper delay after triggering reset or conversion.
Definition: ms5611_i2c.c:76
#define MS5611_SOFT_RESET
Definition: ms5611_regs.h:67
#define MS5611_START_CONV_D2
Definition: ms5611_regs.h:70
uint32_t d2
Definition: ms5611.h:56
enum Ms5611Status status
Definition: ms5611_i2c.h:39