Paparazzi UAS v7.1_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
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
29
31#include "mcu_periph/spi.h"
32#include "modules/core/abi.h"
33#include "mcu_periph/sys_time.h"
35#include "state.h"
36
37
47
48
49#define STATUS_INIT1 0
50#define STATUS_INIT2 1
51#define STATUS_INIT3 2
52#define STATUS_INIT4 3
53#define STATUS_MEASURE_PRESSURE 4
54#define STATUS_MEASURE_TEMPERATURE 5
55#define STATUS_RESET 6
56
58static bool status_read_data;
59static uint16_t words[4];
60
61#define InitStatus() (status <= STATUS_INIT4)
62
63#define NextStatus() { \
64 if (status_read_data) { \
65 status++; if (status > STATUS_MEASURE_TEMPERATURE) status = STATUS_MEASURE_PRESSURE; \
66 }; \
67 status_read_data = !status_read_data; \
68 }
69
70#define CMD_INIT 0x1D
71#define CMD_MEASUREMENT 0x0F
72
73#define CMD_W1 0x50
74#define CMD_W2 0x60
75#define CMD_W3 0x90
76#define CMD_W4 0xA0
77#define CMD_PRESSURE 0x40
78#define CMD_TEMPERATURE 0x20
80static uint8_t reset[3] = {0x15, 0x55, 0x40};
81
82
83
86
87#define Uint16(buf_input) (buf_input[0] << 8 | buf_input[1])
88
89/* PWM prescaler, set PWM input clock to 15MHz, PWM_CLK = PCLK / PWM_PRESCALER */
90
91#if (PCLK == 15000000)
92#define PWM_PRESCALER 1
93#else
94
95#if (PCLK == 30000000)
96#define PWM_PRESCALER 2
97#else
98
99#if (PCLK == 60000000)
100#define PWM_PRESCALER 4
101#else
102
103#error unknown PCLK frequency
104#endif
105#endif
106#endif
107
108#define MS5534A_MCLK 32768
109#define PWM_PERIOD ((PCLK / PWM_PRESCALER) / MS5534A_MCLK)
110#define PWM_DUTY (PWM_PERIOD / 2)
111
112
113static void calibration(void);
114
116{
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
129 PWMLER = PWMLER_LATCH0 | PWMLER_LATCH2;
130 PWMTCR = PWMTCR_COUNTER_ENABLE | PWMTCR_PWM_ENABLE;
131 PWMPCR = PWMPCR_ENA2;
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
140 status_read_data = false;
141
142 calibration();
143#else
145 status_read_data = false;
146#endif
147
148
149
151 alt_baro_enabled = false;
152
154 baro_MS5534A_r = 20.;
156 baro_MS5534A_do_reset = false;
157}
158
160{
162 status_read_data = false;
163}
164
165/* To be called not faster than 30Hz */
167{
168 if (!SpiCheckAvailable()) {
169 SpiOverRun();
170 return;
171 }
172
173 if (status == STATUS_RESET) {
174 uint8_t i;
175 for (i = 0; i < 3; i++) {
176 buf_output[i] = reset[i];
177 }
178 spi_buffer_length = 3;
179 } else {
180 if (status_read_data) {
181 buf_output[0] = buf_output[1] = 0;
182 } else {
184 buf_output[1] = cmds[status];
185 }
186 spi_buffer_length = 2;
187 }
188
189 spi_buffer_input = (uint8_t *)&buf_input;
190 spi_buffer_output = (uint8_t *)&buf_output;
191 if (status_read_data) {
192 SpiSetCPHA();
193 } else {
194 SpiClrCPHA();
195 }
196 SpiStart();
197}
198
199static uint16_t d1, d2;
200static uint16_t c1, c2, c3, c4, ut1, c6;
201
202
203static void calibration(void)
204{
205 /* End of init, configuration (page 11) */
206 c1 = words[0] >> 1;
207 c2 = ((words[2] & 0x3f) << 6) | (words[3] & 0x3f);
208 c3 = words[3] >> 6;
209 c4 = words[2] >> 6;
210 uint16_t c5 = ((words[0] & 0x1) << 10) | (words[1] >> 6);
211 c6 = words[1] & 0x3f;
212
213 ut1 = (c5 << 3) + 20224;
214}
215
216
217
218/* Handle the SPI message, i.e. store the received values in variables */
220{
221 if (status_read_data) {
222 switch (status) {
225 break;
228 /* Compute pressure and temp (page 10) */
229 int16_t dT = d2 - ut1;
230 baro_MS5534A_temp = 200 + (dT * (c6 + 50)) / (1 << 10);
231 int16_t off = c2 * 4 + ((c4 - 512) * dT) / (1 << 12);
232 uint32_t sens = c1 + (c3 * dT) / (1 << 10) + 24576;
233 uint16_t x = (sens * (d1 - 7168)) / (1 << 14) - off;
234 // baro_MS5534A = ((x*10)>>5) + 250*10;
235 baro_MS5534A_pressure = ((x * 100) >> 5) + 250 * 100;
237
238 break;
239 case STATUS_RESET:
240 break;
241 default: /* Init status */
243 if (status == STATUS_INIT4) {
244 calibration();
245 }
246 }
247 } /* else nothing to read */
248
249 NextStatus();
250 if (!status_read_data) {
251 /* Ask next conversion now */
253 }
254}
255
257{
258 if (spi_message_received) {
259 /* Got a message on SPI. */
260 spi_message_received = false;
263 uint32_t now_ts = get_sys_time_usec();
266 float pressure = (float)baro_MS5534A_pressure;
267 AbiSendMsgBARO_ABS_RAW(BARO_MS5534A_SENDER_ID, now_ts, pressure);
268 }
269 }
270}
Main include for ABI (AirBorneInterface).
#define BARO_MS5534A_SENDER_ID
static uint16_t d1
void baro_MS5534A_event_task(void)
void baro_MS5534A_event(void)
#define CMD_PRESSURE
static uint16_t c4
#define STATUS_INIT4
float baro_MS5534A_z
uint16_t baro_MS5534A_temp
#define STATUS_INIT1
#define CMD_INIT
void baro_MS5534A_send(void)
#define NextStatus()
static uint16_t ut1
#define CMD_W2
static uint16_t c6
static uint8_t buf_input[3]
static uint16_t d2
void baro_MS5534A_init(void)
float baro_MS5534A_sigma2
#define STATUS_MEASURE_TEMPERATURE
bool alt_baro_enabled
bool baro_MS5534A_do_reset
static uint16_t c1
#define CMD_MEASUREMENT
uint32_t baro_MS5534A_pressure
#define Uint16(buf_input)
void baro_MS5534A_reset(void)
#define InitStatus()
float baro_MS5534A_r
static uint16_t words[4]
#define CMD_W4
#define PWM_PERIOD
static uint16_t c3
static void calibration(void)
#define CMD_W3
static uint16_t c2
#define CMD_W1
#define STATUS_RESET
static uint8_t cmds[6]
bool baro_MS5534A_available
static bool status_read_data
static uint8_t status
static uint8_t buf_output[3]
#define STATUS_MEASURE_PRESSURE
static uint8_t reset[3]
#define CMD_TEMPERATURE
uint32_t baro_MS5534A_ground_pressure
#define PWM_DUTY
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
float ground_alt
size == nb_waypoint, waypoint 0 is a dummy waypoint
Definition common_nav.c:46
Handling of the MS5534a pressure sensor.
Fixedwing Navigation library.
Architecture independent SPI (Serial Peripheral Interface) API.
API to get/set the generic vehicle states.
Architecture independent timing functions.
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
short int16_t
Typedef defining 16 bit short type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.