Paparazzi UAS  v5.15_devel-110-g5cf27ee
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
ms5611_spi.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_spi.h"
31 
32 
33 void ms5611_spi_init(struct Ms5611_Spi *ms, struct spi_periph *spi_p, uint8_t slave_idx,
34  bool is_ms5607)
35 {
36  /* set spi_peripheral */
37  ms->spi_p = spi_p;
38 
39  /* configure spi transaction */
42  ms->spi_trans.dss = SPIDss8bit;
44  ms->spi_trans.cdiv = SPIDiv64;
45 
47  ms->spi_trans.slave_idx = slave_idx;
48  ms->spi_trans.output_length = 1;
49  ms->spi_trans.input_length = 4;
50  ms->spi_trans.before_cb = NULL;
51  ms->spi_trans.after_cb = NULL;
52  ms->spi_trans.input_buf = ms->rx_buf;
53  ms->spi_trans.output_buf = ms->tx_buf;
54 
55  /* set initial status: Success or Done */
57 
58  ms->data_available = false;
59  ms->initialized = false;
61  ms->prom_cnt = 0;
62  ms->is_ms5607 = is_ms5607;
63 }
64 
66 {
67  if (ms->status == MS5611_STATUS_UNINIT) {
68  ms->initialized = false;
69  ms->prom_cnt = 0;
70  ms->tx_buf[0] = MS5611_SOFT_RESET;
71  spi_submit(ms->spi_p, &(ms->spi_trans));
73  }
74 }
75 
77 {
78  if (ms->status == MS5611_STATUS_IDLE &&
79  ms->spi_trans.status == SPITransDone) {
80  /* start D1 conversion */
82  spi_submit(ms->spi_p, &(ms->spi_trans));
84  }
85 }
86 
93 {
94  switch (ms->status) {
97  break;
99  if (ms->spi_trans.status == SPITransDone) {
100  /* start getting prom data */
101  ms->tx_buf[0] = MS5611_PROM_READ | (ms->prom_cnt << 1);
102  spi_submit(ms->spi_p, &(ms->spi_trans));
104  }
105  break;
108  break;
110  if (ms->spi_trans.status == SPITransDone) {
111  /* read D1 adc */
112  ms->tx_buf[0] = MS5611_ADC_READ;
113  spi_submit(ms->spi_p, &(ms->spi_trans));
115  }
116  break;
119  break;
121  if (ms->spi_trans.status == SPITransDone) {
122  /* read D2 adc */
123  ms->tx_buf[0] = MS5611_ADC_READ;
124  spi_submit(ms->spi_p, &(ms->spi_trans));
126  }
127  break;
128  default:
129  break;
130  }
131 }
132 
133 void ms5611_spi_event(struct Ms5611_Spi *ms)
134 {
135  if (ms->initialized) {
136  if (ms->spi_trans.status == SPITransFailed) {
139  } else if (ms->spi_trans.status == SPITransSuccess) {
140  // Successfull reading
141  switch (ms->status) {
142 
144  /* read D1 (pressure) */
145  ms->data.d1 = (ms->rx_buf[1] << 16) |
146  (ms->rx_buf[2] << 8) |
147  ms->rx_buf[3];
148  if (ms->data.d1 == 0) {
149  /* if value is zero, it was read to soon and is invalid, back to idle */
151  } else {
152  /* start D2 conversion */
153  ms->tx_buf[0] = MS5611_START_CONV_D2;
154  spi_submit(ms->spi_p, &(ms->spi_trans));
156  }
157  break;
158 
160  /* read D2 (temperature) */
161  ms->data.d2 = (ms->rx_buf[1] << 16) |
162  (ms->rx_buf[2] << 8) |
163  ms->rx_buf[3];
164  if (ms->data.d2 == 0) {
165  /* if value is zero, it was read to soon and is invalid, back to idle */
167  } else {
168  /* calculate temp and pressure from measurements and set available if valid */
169  if (ms->is_ms5607) {
170  ms->data_available = ms5607_calc(&(ms->data));
171  }
172  else {
173  ms->data_available = ms5611_calc(&(ms->data));
174  }
176  }
177  break;
178 
179  default:
180  break;
181  }
183  }
184  } else if (ms->status != MS5611_STATUS_UNINIT) { // Configuring but not yet initialized
185  switch (ms->spi_trans.status) {
186 
187  case SPITransFailed:
188  /* try again */
191  break;
192 
193  case SPITransSuccess:
194  if (ms->status == MS5611_STATUS_PROM) {
195  /* read prom data */
196  ms->data.c[ms->prom_cnt++] = (ms->rx_buf[1] << 8) |
197  ms->rx_buf[2];
198  if (ms->prom_cnt < PROM_NB) {
199  /* get next prom data */
200  ms->tx_buf[0] = MS5611_PROM_READ | (ms->prom_cnt << 1);
201  spi_submit(ms->spi_p, &(ms->spi_trans));
202  } else {
203  /* done reading prom, check prom crc */
204  if (ms5611_prom_crc_ok(ms->data.c)) {
205  ms->initialized = true;
207  } else {
208  /* checksum error, try again */
210  }
211  }
212  }
214  break;
215 
216  default:
217  break;
218  }
219  }
220 }
uint32_t d1
Definition: ms5611.h:55
enum SPIClockPolarity cpol
clock polarity control
Definition: spi.h:149
#define PROM_NB
Definition: ms5611_regs.h:38
bool ms5611_calc(struct Ms5611Data *ms)
Calculate temperature and compensated pressure for MS5611.
Definition: ms5611.c:81
enum SPIClockDiv cdiv
prescaler of main clock to use as SPI clock
Definition: spi.h:153
uint16_t c[PROM_NB]
Definition: ms5611.h:54
uint16_t output_length
number of data words to write
Definition: spi.h:146
Measurement Specialties (Intersema) MS5611-01BA and MS5607-02BA03 pressure/temperature sensor interfa...
bool ms5607_calc(struct Ms5611Data *ms)
Calculate temperature and compensated pressure for MS5607.
Definition: ms5611.c:124
volatile bool data_available
data ready flag
Definition: ms5611_spi.h:44
volatile uint8_t rx_buf[4]
Definition: ms5611_spi.h:40
CPHA = 1.
Definition: spi.h:69
struct Ms5611Data data
Definition: ms5611_spi.h:45
volatile uint8_t tx_buf[1]
Definition: ms5611_spi.h:39
bool spi_submit(struct spi_periph *p, struct spi_transaction *t)
Submit SPI transaction.
Definition: spi_arch.c:505
void ms5611_spi_start_conversion(struct Ms5611_Spi *ms)
Definition: ms5611_spi.c:76
uint32_t d2
Definition: ms5611.h:56
CPOL = 1.
Definition: spi.h:78
enum SPIBitOrder bitorder
MSB/LSB order.
Definition: spi.h:152
volatile uint8_t * output_buf
pointer to transmit buffer for DMA
Definition: spi.h:144
enum SPISlaveSelect select
slave selection behavior
Definition: spi.h:148
bool ms5611_prom_crc_ok(uint16_t *prom)
Check if CRC of PROM data is OK.
Definition: ms5611.c:36
#define MS5611_START_CONV_D1
Definition: ms5611_regs.h:69
#define MS5611_ADC_READ
Definition: ms5611_regs.h:66
enum SPIClockPhase cpha
clock phase control
Definition: spi.h:150
SPI peripheral structure.
Definition: spi.h:168
struct spi_periph * spi_p
Definition: ms5611_spi.h:37
Definition: spi.h:84
void ms5611_spi_event(struct Ms5611_Spi *ms)
Definition: ms5611_spi.c:133
int32_t prom_cnt
number of bytes read from PROM
Definition: ms5611_spi.h:46
void ms5611_spi_start_configure(struct Ms5611_Spi *ms)
Definition: ms5611_spi.c:65
SPICallback after_cb
NULL or function called after the transaction.
Definition: spi.h:155
bool initialized
config done flag
Definition: ms5611_spi.h:43
uint16_t input_length
number of data words to read
Definition: spi.h:145
bool is_ms5607
TRUE if MS5607, FALSE if MS5611.
Definition: ms5611_spi.h:42
struct spi_transaction spi_trans
Definition: ms5611_spi.h:38
enum Ms5611Status status
Definition: ms5611_spi.h:41
void ms5611_spi_periodic_check(struct Ms5611_Spi *ms)
Periodic function to ensure proper delay after triggering reset or conversion.
Definition: ms5611_spi.c:92
unsigned char uint8_t
Definition: types.h:14
void ms5611_spi_init(struct Ms5611_Spi *ms, struct spi_periph *spi_p, uint8_t slave_idx, bool is_ms5607)
Definition: ms5611_spi.c:33
slave is selected before transaction and unselected after
Definition: spi.h:57
enum SPIDataSizeSelect dss
data transfer word size
Definition: spi.h:151
uint8_t slave_idx
slave id: SPI_SLAVE0 to SPI_SLAVE4
Definition: spi.h:147
#define MS5611_PROM_READ
Definition: ms5611_regs.h:68
Definition: spi.h:119
volatile uint8_t * input_buf
pointer to receive buffer for DMA
Definition: spi.h:143
SPICallback before_cb
NULL or function called before the transaction.
Definition: spi.h:154
#define MS5611_SOFT_RESET
Definition: ms5611_regs.h:67
#define MS5611_START_CONV_D2
Definition: ms5611_regs.h:70
enum SPITransactionStatus status
Definition: spi.h:156