Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
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 }
spi_transaction::cdiv
enum SPIClockDiv cdiv
prescaler of main clock to use as SPI clock
Definition: spi.h:159
Ms5611_Spi::status
enum Ms5611Status status
Definition: ms5611_spi.h:41
spi_transaction::cpol
enum SPIClockPolarity cpol
clock polarity control
Definition: spi.h:155
ms5611_spi_periodic_check
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
MS5611_STATUS_CONV_D2
@ MS5611_STATUS_CONV_D2
Definition: ms5611.h:46
MS5611_STATUS_UNINIT
@ MS5611_STATUS_UNINIT
Definition: ms5611.h:38
ms5611_prom_crc_ok
bool ms5611_prom_crc_ok(uint16_t *prom)
Check if CRC of PROM data is OK.
Definition: ms5611.c:36
Ms5611_Spi::is_ms5607
bool is_ms5607
TRUE if MS5607, FALSE if MS5611.
Definition: ms5611_spi.h:42
spi_transaction::output_length
uint16_t output_length
number of data words to write
Definition: spi.h:152
MS5611_STATUS_ADC_D1
@ MS5611_STATUS_ADC_D1
Definition: ms5611.h:45
ms5611_spi.h
Ms5611Data::d2
uint32_t d2
Definition: ms5611.h:56
Ms5611_Spi::rx_buf
volatile uint8_t rx_buf[4]
Definition: ms5611_spi.h:40
Ms5611_Spi
Definition: ms5611_spi.h:36
SPISelectUnselect
@ SPISelectUnselect
slave is selected before transaction and unselected after
Definition: spi.h:63
SPITransSuccess
@ SPITransSuccess
Definition: spi.h:99
spi_transaction::bitorder
enum SPIBitOrder bitorder
MSB/LSB order.
Definition: spi.h:158
MS5611_PROM_READ
#define MS5611_PROM_READ
Definition: ms5611_regs.h:68
Ms5611_Spi::data_available
volatile bool data_available
data ready flag
Definition: ms5611_spi.h:44
ms5611_calc
bool ms5611_calc(struct Ms5611Data *ms)
Calculate temperature and compensated pressure for MS5611.
Definition: ms5611.c:81
spi_transaction::output_buf
volatile uint8_t * output_buf
pointer to transmit buffer for DMA
Definition: spi.h:150
spi_periph
SPI peripheral structure.
Definition: spi.h:174
spi_transaction::select
enum SPISlaveSelect select
slave selection behavior
Definition: spi.h:154
SPIMSBFirst
@ SPIMSBFirst
Definition: spi.h:112
MS5611_STATUS_PROM
@ MS5611_STATUS_PROM
Definition: ms5611.h:41
MS5611_STATUS_RESET
@ MS5611_STATUS_RESET
Definition: ms5611.h:39
MS5611_START_CONV_D2
#define MS5611_START_CONV_D2
Definition: ms5611_regs.h:70
spi_transaction::cpha
enum SPIClockPhase cpha
clock phase control
Definition: spi.h:156
spi_transaction::after_cb
SPICallback after_cb
NULL or function called after the transaction.
Definition: spi.h:161
spi_submit
bool spi_submit(struct spi_periph *p, struct spi_transaction *t)
Submit SPI transaction.
Definition: spi_arch.c:511
SPITransFailed
@ SPITransFailed
Definition: spi.h:100
MS5611_STATUS_CONV_D1_OK
@ MS5611_STATUS_CONV_D1_OK
Definition: ms5611.h:44
Ms5611_Spi::prom_cnt
int32_t prom_cnt
number of bytes read from PROM
Definition: ms5611_spi.h:46
PROM_NB
#define PROM_NB
Definition: ms5611_regs.h:38
Ms5611_Spi::initialized
bool initialized
config done flag
Definition: ms5611_spi.h:43
MS5611_SOFT_RESET
#define MS5611_SOFT_RESET
Definition: ms5611_regs.h:67
MS5611_STATUS_IDLE
@ MS5611_STATUS_IDLE
Definition: ms5611.h:42
SPICpolIdleHigh
@ SPICpolIdleHigh
CPOL = 1.
Definition: spi.h:84
uint8_t
unsigned char uint8_t
Definition: types.h:14
Ms5611_Spi::spi_p
struct spi_periph * spi_p
Definition: ms5611_spi.h:37
MS5611_STATUS_CONV_D2_OK
@ MS5611_STATUS_CONV_D2_OK
Definition: ms5611.h:47
ms5607_calc
bool ms5607_calc(struct Ms5611Data *ms)
Calculate temperature and compensated pressure for MS5607.
Definition: ms5611.c:124
if
if(GpsFixValid() &&e_identification_started)
Definition: e_identification_fr.c:159
SPIDss8bit
@ SPIDss8bit
Definition: spi.h:90
MS5611_STATUS_ADC_D2
@ MS5611_STATUS_ADC_D2
Definition: ms5611.h:48
SPIDiv64
@ SPIDiv64
Definition: spi.h:125
Ms5611_Spi::spi_trans
struct spi_transaction spi_trans
Definition: ms5611_spi.h:38
MS5611_STATUS_CONV_D1
@ MS5611_STATUS_CONV_D1
Definition: ms5611.h:43
spi_transaction::input_length
uint16_t input_length
number of data words to read
Definition: spi.h:151
ms5611_spi_start_configure
void ms5611_spi_start_configure(struct Ms5611_Spi *ms)
Definition: ms5611_spi.c:65
spi_transaction::dss
enum SPIDataSizeSelect dss
data transfer word size
Definition: spi.h:157
spi_transaction::slave_idx
uint8_t slave_idx
slave id: SPI_SLAVE0 to SPI_SLAVE4
Definition: spi.h:153
SPITransDone
@ SPITransDone
Definition: spi.h:101
spi_transaction::input_buf
volatile uint8_t * input_buf
pointer to receive buffer for DMA
Definition: spi.h:149
SPICphaEdge2
@ SPICphaEdge2
CPHA = 1.
Definition: spi.h:75
ms5611_spi_start_conversion
void ms5611_spi_start_conversion(struct Ms5611_Spi *ms)
Definition: ms5611_spi.c:76
MS5611_ADC_READ
#define MS5611_ADC_READ
Definition: ms5611_regs.h:66
MS5611_STATUS_RESET_OK
@ MS5611_STATUS_RESET_OK
Definition: ms5611.h:40
spi_transaction::before_cb
SPICallback before_cb
NULL or function called before the transaction.
Definition: spi.h:160
ms5611_spi_event
void ms5611_spi_event(struct Ms5611_Spi *ms)
Definition: ms5611_spi.c:133
MS5611_START_CONV_D1
#define MS5611_START_CONV_D1
Definition: ms5611_regs.h:69
spi_transaction::status
enum SPITransactionStatus status
Definition: spi.h:162
Ms5611Data::d1
uint32_t d1
Definition: ms5611.h:55
Ms5611Data::c
uint16_t c[PROM_NB]
Definition: ms5611.h:54
Ms5611_Spi::data
struct Ms5611Data data
Definition: ms5611_spi.h:45
Ms5611_Spi::tx_buf
volatile uint8_t tx_buf[1]
Definition: ms5611_spi.h:39
ms5611_spi_init
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