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_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 {
35  /* set spi_peripheral */
36  ms->spi_p = spi_p;
37 
38  /* configure spi transaction */
41  ms->spi_trans.dss = SPIDss8bit;
43  ms->spi_trans.cdiv = SPIDiv64;
44 
46  ms->spi_trans.slave_idx = slave_idx;
47  ms->spi_trans.output_length = 1;
48  ms->spi_trans.input_length = 4;
49  ms->spi_trans.before_cb = NULL;
50  ms->spi_trans.after_cb = NULL;
51  ms->spi_trans.input_buf = ms->rx_buf;
52  ms->spi_trans.output_buf = ms->tx_buf;
53 
54  /* set initial status: Success or Done */
56 
57  ms->data_available = FALSE;
58  ms->initialized = FALSE;
60  ms->prom_cnt = 0;
61 }
62 
64 {
65  if (ms->status == MS5611_STATUS_UNINIT) {
66  ms->initialized = FALSE;
67  ms->prom_cnt = 0;
68  ms->tx_buf[0] = MS5611_SOFT_RESET;
69  spi_submit(ms->spi_p, &(ms->spi_trans));
71  }
72 }
73 
75 {
76  if (ms->status == MS5611_STATUS_IDLE &&
77  ms->spi_trans.status == SPITransDone) {
78  /* start D1 conversion */
80  spi_submit(ms->spi_p, &(ms->spi_trans));
82  }
83 }
84 
91 {
92  switch (ms->status) {
95  break;
97  if (ms->spi_trans.status == SPITransDone) {
98  /* start getting prom data */
99  ms->tx_buf[0] = MS5611_PROM_READ | (ms->prom_cnt << 1);
100  spi_submit(ms->spi_p, &(ms->spi_trans));
102  }
103  break;
106  break;
108  if (ms->spi_trans.status == SPITransDone) {
109  /* read D1 adc */
110  ms->tx_buf[0] = MS5611_ADC_READ;
111  spi_submit(ms->spi_p, &(ms->spi_trans));
113  }
114  break;
117  break;
119  if (ms->spi_trans.status == SPITransDone) {
120  /* read D2 adc */
121  ms->tx_buf[0] = MS5611_ADC_READ;
122  spi_submit(ms->spi_p, &(ms->spi_trans));
124  }
125  break;
126  default:
127  break;
128  }
129 }
130 
131 void ms5611_spi_event(struct Ms5611_Spi *ms) {
132  if (ms->initialized) {
133  if (ms->spi_trans.status == SPITransFailed) {
136  }
137  else if (ms->spi_trans.status == SPITransSuccess) {
138  // Successfull reading
139  switch (ms->status) {
140 
142  /* read D1 (pressure) */
143  ms->data.d1 = (ms->rx_buf[1] << 16) |
144  (ms->rx_buf[2] << 8) |
145  ms->rx_buf[3];
146  if (ms->data.d1 == 0) {
147  /* if value is zero, it was read to soon and is invalid, back to idle */
149  }
150  else {
151  /* start D2 conversion */
152  ms->tx_buf[0] = MS5611_START_CONV_D2;
153  spi_submit(ms->spi_p, &(ms->spi_trans));
155  }
156  break;
157 
159  /* read D2 (temperature) */
160  ms->data.d2 = (ms->rx_buf[1] << 16) |
161  (ms->rx_buf[2] << 8) |
162  ms->rx_buf[3];
163  if (ms->data.d2 == 0) {
164  /* if value is zero, it was read to soon and is invalid, back to idle */
166  }
167  else {
168  /* calculate temp and pressure from measurements and set available if valid */
169  if (ms5611_calc(&(ms->data)))
170  ms->data_available = TRUE;
172  }
173  break;
174 
175  default:
176  break;
177  }
179  }
180  }
181  else if (ms->status != MS5611_STATUS_UNINIT) { // Configuring but not yet initialized
182  switch (ms->spi_trans.status) {
183 
184  case SPITransFailed:
185  /* try again */
188  break;
189 
190  case SPITransSuccess:
191  if (ms->status == MS5611_STATUS_PROM) {
192  /* read prom data */
193  ms->data.c[ms->prom_cnt++] = (ms->rx_buf[1] << 8) |
194  ms->rx_buf[2];
195  if (ms->prom_cnt < PROM_NB) {
196  /* get next prom data */
197  ms->tx_buf[0] = MS5611_PROM_READ | (ms->prom_cnt << 1);
198  spi_submit(ms->spi_p, &(ms->spi_trans));
199  }
200  else {
201  /* done reading prom, check prom crc */
202  if (ms5611_prom_crc_ok(ms->data.c)) {
203  ms->initialized = TRUE;
205  }
206  else {
207  /* checksum error, try again */
209  }
210  }
211  }
213  break;
214 
215  default:
216  break;
217  }
218  }
219 }
enum SPIBitOrder bitorder
MSB/LSB order.
Definition: spi.h:152
#define PROM_NB
Definition: ms5611_regs.h:38
struct spi_transaction spi_trans
Definition: ms5611_spi.h:38
enum SPIClockPolarity cpol
clock polarity control
Definition: spi.h:149
uint32_t d1
Definition: ms5611.h:55
struct Ms5611Data data
Definition: ms5611_spi.h:44
uint8_t input_length
number of data words to read
Definition: spi.h:145
Measurement Specialties (Intersema) MS5611-01BA pressure/temperature sensor interface for SPI...
enum SPIClockPhase cpha
clock phase control
Definition: spi.h:150
CPHA = 1.
Definition: spi.h:69
volatile uint8_t tx_buf[1]
Definition: ms5611_spi.h:39
enum Ms5611Status status
Definition: ms5611_spi.h:41
void ms5611_spi_start_conversion(struct Ms5611_Spi *ms)
Definition: ms5611_spi.c:74
volatile uint8_t * input_buf
pointer to receive buffer for DMA
Definition: spi.h:143
CPOL = 1.
Definition: spi.h:78
struct spi_periph * spi_p
Definition: ms5611_spi.h:37
if(PrimarySpektrumState.SpektrumTimer)--PrimarySpektrumState.SpektrumTimer
enum SPISlaveSelect select
slave selection behavior
Definition: spi.h:148
bool_t ms5611_prom_crc_ok(uint16_t *prom)
Check if CRC of PROM data is OK.
Definition: ms5611.c:36
int32_t prom_cnt
number of bytes read from PROM
Definition: ms5611_spi.h:45
#define FALSE
Definition: imu_chimu.h:141
enum SPITransactionStatus status
Definition: spi.h:156
#define MS5611_START_CONV_D1
Definition: ms5611_regs.h:69
volatile uint8_t rx_buf[4]
Definition: ms5611_spi.h:40
#define MS5611_ADC_READ
Definition: ms5611_regs.h:66
volatile bool_t data_available
data ready flag
Definition: ms5611_spi.h:43
Definition: spi.h:84
void ms5611_spi_event(struct Ms5611_Spi *ms)
Definition: ms5611_spi.c:131
enum SPIClockDiv cdiv
prescaler of main clock to use as SPI clock
Definition: spi.h:153
bool_t spi_submit(struct spi_periph *p, struct spi_transaction *t)
Submit a spi transaction.
Definition: spi_arch.c:470
void ms5611_spi_start_configure(struct Ms5611_Spi *ms)
Definition: ms5611_spi.c:63
enum SPIDataSizeSelect dss
data transfer word size
Definition: spi.h:151
uint8_t output_length
number of data words to write
Definition: spi.h:146
void ms5611_spi_init(struct Ms5611_Spi *ms, struct spi_periph *spi_p, uint8_t slave_idx)
Definition: ms5611_spi.c:33
#define TRUE
Definition: imu_chimu.h:144
SPI peripheral structure.
Definition: spi.h:168
void ms5611_spi_periodic_check(struct Ms5611_Spi *ms)
Periodic function to ensure proper delay after triggering reset or conversion.
Definition: ms5611_spi.c:90
unsigned char uint8_t
Definition: types.h:14
bool_t initialized
config done flag
Definition: ms5611_spi.h:42
slave is selected before transaction and unselected after
Definition: spi.h:57
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
uint16_t c[PROM_NB]
Definition: ms5611.h:54
volatile uint8_t * output_buf
pointer to transmit buffer for DMA
Definition: spi.h:144
SPICallback before_cb
NULL or function called before the transaction.
Definition: spi.h:154
bool_t ms5611_calc(struct Ms5611Data *ms)
Calculate temperature and compensated pressure.
Definition: ms5611.c:63
#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
SPICallback after_cb
NULL or function called after the transaction.
Definition: spi.h:155