Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
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
31
32
33void 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 */
40 ms->spi_trans.cpol = SPICpolIdleHigh;
41 ms->spi_trans.cpha = SPICphaEdge2;
42 ms->spi_trans.dss = SPIDss8bit;
43 ms->spi_trans.bitorder = SPIMSBFirst;
44 ms->spi_trans.cdiv = SPIDiv64;
45
46 ms->spi_trans.select = SPISelectUnselect;
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 */
56 ms->spi_trans.status = SPITransDone;
57
58 ms->data_available = false;
59 ms->initialized = false;
60 ms->status = MS5611_STATUS_UNINIT;
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));
72 ms->status = MS5611_STATUS_RESET;
73 }
74}
75
77{
78 if (ms->status == MS5611_STATUS_IDLE &&
79 ms->spi_trans.status == SPITransDone) {
80 /* start D1 conversion */
81 ms->tx_buf[0] = MS5611_START_CONV_D1;
82 spi_submit(ms->spi_p, &(ms->spi_trans));
83 ms->status = MS5611_STATUS_CONV_D1;
84 }
85}
86
93{
94 switch (ms->status) {
96 ms->status = MS5611_STATUS_RESET_OK;
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));
103 ms->status = MS5611_STATUS_PROM;
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));
114 ms->status = MS5611_STATUS_ADC_D1;
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));
125 ms->status = MS5611_STATUS_ADC_D2;
126 }
127 break;
128 default:
129 break;
130 }
131}
132
134{
135 if (ms->initialized) {
136 if (ms->spi_trans.status == SPITransFailed) {
137 ms->status = MS5611_STATUS_IDLE;
138 ms->spi_trans.status = SPITransDone;
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 */
150 ms->status = MS5611_STATUS_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));
155 ms->status = MS5611_STATUS_CONV_D2;
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 */
166 ms->status = MS5611_STATUS_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 }
175 ms->status = MS5611_STATUS_IDLE;
176 }
177 break;
178
179 default:
180 break;
181 }
182 ms->spi_trans.status = SPITransDone;
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 */
189 ms->status = MS5611_STATUS_UNINIT;
190 ms->spi_trans.status = SPITransDone;
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;
206 ms->status = MS5611_STATUS_IDLE;
207 } else {
208 /* checksum error, try again */
209 ms->status = MS5611_STATUS_UNINIT;
210 }
211 }
212 }
213 ms->spi_trans.status = SPITransDone;
214 break;
215
216 default:
217 break;
218 }
219 }
220}
enum SPIStatus status
internal state of the peripheral
Definition spi.h:180
bool spi_submit(struct spi_periph *p, struct spi_transaction *t)
Submit SPI transaction.
Definition spi_arch.c:533
@ SPICphaEdge2
CPHA = 1.
Definition spi.h:75
@ SPITransFailed
Definition spi.h:100
@ SPITransSuccess
Definition spi.h:99
@ SPITransDone
Definition spi.h:101
@ SPICpolIdleHigh
CPOL = 1.
Definition spi.h:84
@ SPISelectUnselect
slave is selected before transaction and unselected after
Definition spi.h:63
@ SPIMSBFirst
Definition spi.h:112
@ SPIDiv64
Definition spi.h:125
@ SPIDss8bit
Definition spi.h:90
SPI peripheral structure.
Definition spi.h:174
uint16_t foo
Definition main_demo5.c:58
bool ms5607_calc(struct Ms5611Data *ms)
Calculate temperature and compensated pressure for MS5607.
Definition ms5611.c:124
bool ms5611_prom_crc_ok(uint16_t *prom)
Check if CRC of PROM data is OK.
Definition ms5611.c:36
bool ms5611_calc(struct Ms5611Data *ms)
Calculate temperature and compensated pressure for MS5611.
Definition ms5611.c:81
@ MS5611_STATUS_ADC_D2
Definition ms5611.h:48
@ MS5611_STATUS_IDLE
Definition ms5611.h:42
@ MS5611_STATUS_PROM
Definition ms5611.h:41
@ MS5611_STATUS_ADC_D1
Definition ms5611.h:45
@ MS5611_STATUS_CONV_D1
Definition ms5611.h:43
@ MS5611_STATUS_CONV_D2_OK
Definition ms5611.h:47
@ MS5611_STATUS_RESET
Definition ms5611.h:39
@ MS5611_STATUS_CONV_D1_OK
Definition ms5611.h:44
@ MS5611_STATUS_RESET_OK
Definition ms5611.h:40
@ MS5611_STATUS_CONV_D2
Definition ms5611.h:46
@ MS5611_STATUS_UNINIT
Definition ms5611.h:38
#define MS5611_START_CONV_D1
Definition ms5611_regs.h:69
#define PROM_NB
Definition ms5611_regs.h:38
#define MS5611_ADC_READ
Definition ms5611_regs.h:66
#define MS5611_SOFT_RESET
Definition ms5611_regs.h:67
#define MS5611_START_CONV_D2
Definition ms5611_regs.h:70
#define MS5611_PROM_READ
Definition ms5611_regs.h:68
void ms5611_spi_start_configure(struct Ms5611_Spi *ms)
Definition ms5611_spi.c:65
void ms5611_spi_event(struct Ms5611_Spi *ms)
Definition ms5611_spi.c:133
void ms5611_spi_start_conversion(struct Ms5611_Spi *ms)
Definition ms5611_spi.c:76
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
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
Measurement Specialties (Intersema) MS5611-01BA and MS5607-02BA03 pressure/temperature sensor interfa...
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.