Paparazzi UAS  v5.0.5_stable-7-g4b8bbb7
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
baro_MS5534A.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007 ENAC
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  */
22 
31 #include "mcu_periph/spi.h"
32 #include "mcu_periph/uart.h"
33 #ifndef BARO_NO_DOWNLINK
34 #include "ap_downlink.h"
35 #endif
36 #include "subsystems/nav.h"
37 #include "state.h"
38 
48 
49 
50 #define STATUS_INIT1 0
51 #define STATUS_INIT2 1
52 #define STATUS_INIT3 2
53 #define STATUS_INIT4 3
54 #define STATUS_MEASURE_PRESSURE 4
55 #define STATUS_MEASURE_TEMPERATURE 5
56 #define STATUS_RESET 6
57 
58 static uint8_t status;
59 static bool_t status_read_data;
60 static uint16_t words[4];
61 
62 #define InitStatus() (status <= STATUS_INIT4)
63 
64 #define NextStatus() { \
65  if (status_read_data) { \
66  status++; if (status > STATUS_MEASURE_TEMPERATURE) status = STATUS_MEASURE_PRESSURE; \
67  }; \
68  status_read_data = !status_read_data; \
69 }
70 
71 #define CMD_INIT 0x1D
72 #define CMD_MEASUREMENT 0x0F
73 
74 #define CMD_W1 0x50
75 #define CMD_W2 0x60
76 #define CMD_W3 0x90
77 #define CMD_W4 0xA0
78 #define CMD_PRESSURE 0x40
79 #define CMD_TEMPERATURE 0x20
81 static uint8_t reset[3] = {0x15, 0x55, 0x40};
82 
83 
84 
85 static uint8_t buf_input[3];
86 static uint8_t buf_output[3];
87 
88 #define Uint16(buf_input) (buf_input[0] << 8 | buf_input[1])
89 
90 /* PWM prescaler, set PWM input clock to 15MHz, PWM_CLK = PCLK / PWM_PRESCALER */
91 
92 #if (PCLK == 15000000)
93 #define PWM_PRESCALER 1
94 #else
95 
96 #if (PCLK == 30000000)
97 #define PWM_PRESCALER 2
98 #else
99 
100 #if (PCLK == 60000000)
101 #define PWM_PRESCALER 4
102 #else
103 
104 #error unknown PCLK frequency
105 #endif
106 #endif
107 #endif
108 
109 #define MS5534A_MCLK 32768
110 #define PWM_PERIOD ((PCLK / PWM_PRESCALER) / MS5534A_MCLK)
111 #define PWM_DUTY (PWM_PERIOD / 2)
112 
113 
114 static void calibration( void );
115 
116 void baro_MS5534A_init( void ) {
117  /* 32768Hz on PWM2 */
118  /* Configure P0.7 pin as PWM */
119  PINSEL0 &= ~(_BV(14));
120  PINSEL0 |= _BV(15);
121 
122  /* No prescaler */
123  PWMPR = PWM_PRESCALER-1;
124 
125  /* To get 32768Hz from a base frequency of 15MHz */
126  PWMMR0 = PWM_PERIOD;
127  PWMMR2 = PWM_DUTY;
128 
132 
133 #ifdef BARO_MS5534A_W1
134  words[0] = BARO_MS5534A_W1;
135  words[1] = BARO_MS5534A_W2;
136  words[2] = BARO_MS5534A_W3;
137  words[3] = BARO_MS5534A_W4;
138 
141 
142  calibration();
143 #else
146 #endif
147 
148 
149 
152 
154  baro_MS5534A_r = 20.;
157 }
158 
159 void baro_MS5534A_reset( void ) {
162 }
163 
164 /* To be called not faster than 30Hz */
165 void baro_MS5534A_send(void) {
166  if (!SpiCheckAvailable()) {
167  SpiOverRun();
168  return;
169  }
170 
171  if (status == STATUS_RESET) {
172  uint8_t i;
173  for(i = 0; i < 3; i++)
174  buf_output[i] = reset[i];
175  spi_buffer_length = 3;
176  } else {
177  if (status_read_data) {
178  buf_output[0] = buf_output[1] = 0;
179  } else {
181  buf_output[1] = cmds[status];
182  }
183  spi_buffer_length = 2;
184  }
185 
186  spi_buffer_input = (uint8_t*)&buf_input;
187  spi_buffer_output = (uint8_t*)&buf_output;
188  if (status_read_data)
189  SpiSetCPHA();
190  else
191  SpiClrCPHA();
192  SpiStart();
193 }
194 
195 static uint16_t d1, d2;
196 static uint16_t c1, c2, c3, c4, ut1, c6;
197 
198 
199 static void calibration( void ) {
200  /* End of init, configuration (page 11) */
201  c1 = words[0] >> 1;
202  c2 = ((words[2] & 0x3f) << 6) | (words[3] & 0x3f);
203  c3 = words[3] >> 6;
204  c4 = words[2] >> 6;
205  uint16_t c5 = ((words[0] & 0x1) << 10) | (words[1] >> 6);
206  c6 = words[1] & 0x3f;
207 
208  ut1 = (c5 << 3) + 20224;
209 
210 #ifndef BARO_NO_DOWNLINK
211  DOWNLINK_SEND_BARO_WORDS(DefaultChannel, DefaultDevice, &words[0], &words[1], &words[2], &words[3]);
212 #endif
213 }
214 
215 
216 
217 /* Handle the SPI message, i.e. store the received values in variables */
219  if (status_read_data) {
220  switch (status) {
222  d1 = Uint16(buf_input);
223  break;
225  d2 = Uint16(buf_input);
226  /* Compute pressure and temp (page 10) */
227  int16_t dT = d2 - ut1;
228  baro_MS5534A_temp = 200 + (dT*(c6+50)) / (1 << 10);
229  int16_t off = c2*4 + ((c4-512)*dT)/(1 << 12);
230  uint32_t sens = c1 + (c3*dT)/(1<<10) + 24576;
231  uint16_t x = (sens*(d1-7168))/(1<<14) - off;
232  // baro_MS5534A = ((x*10)>>5) + 250*10;
233  baro_MS5534A_pressure = ((x*100)>>5) + 250*100;
235 
236  break;
237  case STATUS_RESET:
238  break;
239  default: /* Init status */
241  if (status == STATUS_INIT4) {
242  calibration();
243  }
244  }
245  } /* else nothing to read */
246 
247  NextStatus();
248  if (!status_read_data) {
249  /* Ask next conversion now */
251  }
252 }
253 
254 void baro_MS5534A_event( void ) {
255  if (spi_message_received) {
256  /* Got a message on SPI. */
260  // baro_MS5534A_available = FALSE; // Checked by INS
262  // if (alt_baro_enabled) {
263  // EstimatorSetAlt(baro_MS5534A_z); // Updated by INS
264  // }
265  }
266  }
267 }
Handling of the MS5534a pressure sensor.
unsigned short uint16_t
Definition: types.h:16
static uint16_t d2
Definition: baro_MS5534A.c:195
#define PWM_DUTY
Definition: baro_MS5534A.c:111
status
Definition: anemotaxis.c:10
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
bool_t spi_message_received
Definition: sim_baro.c:8
static uint8_t buf_output[3]
Definition: baro_MS5534A.c:86
#define CMD_W3
Definition: baro_MS5534A.c:76
static void SpiStart(struct spi_periph *p, struct spi_transaction *t)
Definition: spi_arch.c:233
#define PWMMR0
Definition: LPC21xx.h:100
float baro_MS5534A_r
Definition: baro_MS5534A.c:45
static void SpiSetCPHA(struct spi_periph *p)
Definition: spi_arch.c:103
bool_t baro_MS5534A_do_reset
Definition: baro_MS5534A.c:39
#define PWMTCR
Definition: LPC21xx.h:95
#define PWMPCR
Definition: LPC21xx.h:107
static uint16_t d1
Definition: baro_MS5534A.c:195
#define CMD_W2
Definition: baro_MS5534A.c:75
void baro_MS5534A_event(void)
Definition: baro_MS5534A.c:254
#define PWMMR2
Definition: LPC21xx.h:102
static uint16_t words[4]
Definition: baro_MS5534A.c:60
#define Uint16(buf_input)
Definition: baro_MS5534A.c:88
#define STATUS_MEASURE_TEMPERATURE
Definition: baro_MS5534A.c:55
static void calibration(void)
Definition: baro_MS5534A.c:199
#define FALSE
Definition: imu_chimu.h:141
#define PWMTCR_COUNTER_ENABLE
Definition: lpcTMR.h:83
#define STATUS_INIT4
Definition: baro_MS5534A.c:53
Architecture independent SPI (Serial Peripheral Interface) API.
static uint8_t reset[3]
Definition: baro_MS5534A.c:81
static bool_t status_read_data
Definition: baro_MS5534A.c:59
#define PWMPCR_ENA2
Definition: lpcTMR.h:117
static uint8_t cmds[6]
Definition: baro_MS5534A.c:80
#define InitStatus()
Definition: baro_MS5534A.c:62
bool_t alt_baro_enabled
Definition: baro_MS5534A.c:43
static uint8_t buf_input[3]
Definition: baro_MS5534A.c:85
static uint16_t c4
Definition: baro_MS5534A.c:196
#define CMD_INIT
Definition: baro_MS5534A.c:71
#define PWMLER
Definition: LPC21xx.h:108
void baro_MS5534A_send(void)
Definition: baro_MS5534A.c:165
unsigned long uint32_t
Definition: types.h:18
bool_t baro_MS5534A_available
Definition: baro_MS5534A.c:42
#define PWM_PERIOD
Definition: baro_MS5534A.c:110
signed short int16_t
Definition: types.h:17
#define PWMLER_LATCH2
Definition: lpcTMR.h:126
static uint16_t c1
Definition: baro_MS5534A.c:196
#define STATUS_MEASURE_PRESSURE
Definition: baro_MS5534A.c:54
#define CMD_W1
Definition: baro_MS5534A.c:74
#define CMD_W4
Definition: baro_MS5534A.c:77
static uint16_t c6
Definition: baro_MS5534A.c:196
float baro_MS5534A_sigma2
Definition: baro_MS5534A.c:46
static uint16_t c3
Definition: baro_MS5534A.c:196
uint32_t baro_MS5534A_ground_pressure
Definition: baro_MS5534A.c:44
#define TRUE
Definition: imu_chimu.h:144
#define PWMPR
Definition: LPC21xx.h:97
#define PINSEL0
Definition: LPC21xx.h:347
#define PWMTCR_PWM_ENABLE
Definition: lpcTMR.h:85
unsigned char uint8_t
Definition: types.h:14
API to get/set the generic vehicle states.
#define PWMLER_LATCH0
Definition: lpcTMR.h:124
#define CMD_MEASUREMENT
Definition: baro_MS5534A.c:72
static uint16_t ut1
Definition: baro_MS5534A.c:196
void baro_MS5534A_event_task(void)
Definition: baro_MS5534A.c:218
static uint16_t c2
Definition: baro_MS5534A.c:196
#define NextStatus()
Definition: baro_MS5534A.c:64
static uint8_t status
Definition: baro_MS5534A.c:58
#define CMD_PRESSURE
Definition: baro_MS5534A.c:78
void baro_MS5534A_reset(void)
Definition: baro_MS5534A.c:159
#define STATUS_INIT1
Definition: baro_MS5534A.c:50
#define STATUS_RESET
Definition: baro_MS5534A.c:56
float baro_MS5534A_z
Definition: baro_MS5534A.c:47
uint16_t baro_MS5534A_temp
Definition: baro_MS5534A.c:41
#define CMD_TEMPERATURE
Definition: baro_MS5534A.c:79
void baro_MS5534A_init(void)
Definition: baro_MS5534A.c:116
uint32_t baro_MS5534A_pressure
Definition: baro_MS5534A.c:40