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