Contiki-Inga 3.x
mpl115a.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, TU Braunschweig.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /**
31  * \file
32  * MPL115A Pressure Sensor (Barometer) interface definitions
33  * \author
34  * Ulf Kulau <kulau@ibr.cs.tu-bs.de>
35  */
36 
37 /**
38  * \addtogroup inga_sensors_driver
39  * @{
40  */
41 
42 /**
43  * \defgroup mpl115a_interface MPL115A Pressure Sensor (Barometer)
44  *
45  * <p> A pressure sensor sounds a little bit strange in conjunction with
46  * a health care sensor node, but it exists an interesting task. With the help
47  * of the differential pressure value, it is possible to recognize an up and
48  * down movement of a test person.
49  * Therefore the Freescale MPL115A (SPI) was integrated onto the sensor node. This
50  * pressure sensor has a very small design size and furthermore an independent
51  * temperature sensor. In Combination with the temperature value and a few coefficients,
52  * the raw pressure value can be compensated to get the exact result. The coefficients
53  * are stored in the internal ROM of the MPL115A and can be read out via SPI. The
54  * compensation can be done by the MCU (this option is implemented), or by an external
55  * application (e.g. server application)
56  * \note The pressure compensation costs about 6 32Bit multiplications
57  * </p>
58  * @{
59  */
60 
61 #include "../dev/mspi.h"
62 #include <util/delay.h>
63 
64 #ifndef PRESSUREMPL115A_H_
65 #define PRESSUREMPL115A_H_
66 
67 /*!
68  * SPI device order. The chip select number where the
69  * pressure sensor mpl115a is connected to the BCD-decimal
70  * decoder
71  */
72 #define MPL115A_CS 4
73 
74 /*!
75  * Pressure value high byte
76  * <table border="1">
77  * <tr>
78  * <th> D7 </th>
79  * <th> D6 </th>
80  * <th> D5 </th>
81  * <th> D4 </th>
82  * <th> D3 </th>
83  * <th> D2 </th>
84  * <th> D1 </th>
85  * <th> D0 </th>
86  * </tr>
87  * <tr>
88  * <td> Bit9 </td>
89  * <td> Bit8 </td>
90  * <td> Bit7 </td>
91  * <td> Bit6 </td>
92  * <td> Bit5 </td>
93  * <td> Bit4 </td>
94  * <td> Bit3 </td>
95  * <td> Bit2 </td>
96  * </tr>
97  * </table>
98  */
99 #define MPL115A_PRESSURE_OUT_MSB 0x80 | (0x00 << 1)
100 /*!
101  * Pressure value low byte
102  * <table border="1">
103  * <tr>
104  * <th> D7 </th>
105  * <th> D6 </th>
106  * <th> D5 </th>
107  * <th> D4 </th>
108  * <th> D3 </th>
109  * <th> D2 </th>
110  * <th> D1 </th>
111  * <th> D0 </th>
112  * </tr>
113  * <tr>
114  * <td> Bit1 </td>
115  * <td> Bit0 </td>
116  * <td> x </td>
117  * <td> x </td>
118  * <td> x </td>
119  * <td> x </td>
120  * <td> x </td>
121  * <td> x </td>
122  * </tr>
123  * </table>
124  */
125 #define MPL115A_PRESSURE_OUT_LSB 0x80 | (0x01 << 1)
126 
127 /*!
128  * Temperature value high byte
129  * <table border="1">
130  * <tr>
131  * <th> D7 </th>
132  * <th> D6 </th>
133  * <th> D5 </th>
134  * <th> D4 </th>
135  * <th> D3 </th>
136  * <th> D2 </th>
137  * <th> D1 </th>
138  * <th> D0 </th>
139  * </tr>
140  * <tr>
141  * <td> Bit9 </td>
142  * <td> Bit8 </td>
143  * <td> Bit7 </td>
144  * <td> Bit6 </td>
145  * <td> Bit5 </td>
146  * <td> Bit4 </td>
147  * <td> Bit3 </td>
148  * <td> Bit2 </td>
149  * </tr>
150  * </table>
151  */
152 #define MPL115A_TEMP_OUT_MSB 0x80 | (0x02 << 1)
153 /*!
154  * Temperature value low byte
155  * <table border="1">
156  * <tr>
157  * <th> D7 </th>
158  * <th> D6 </th>
159  * <th> D5 </th>
160  * <th> D4 </th>
161  * <th> D3 </th>
162  * <th> D2 </th>
163  * <th> D1 </th>
164  * <th> D0 </th>
165  * </tr>
166  * <tr>
167  * <td> Bit1 </td>
168  * <td> Bit0 </td>
169  * <td> x </td>
170  * <td> x </td>
171  * <td> x </td>
172  * <td> x </td>
173  * <td> x </td>
174  * <td> x </td>
175  * </tr>
176  * </table>
177  */
178 #define MPL115A_TEMP_OUT_LSB 0x80 | (0x03 << 1)
179 
180 /*!
181  * Pressure compensate coefficient A0 (high byte)
182  */
183 #define MPL115A_COEFF_ADDR_A0_M 0x80 | (0x04 << 1)
184 /*!
185  * Pressure compensate coefficient A0 (low byte)
186  */
187 #define MPL115A_COEFF_ADDR_A0_L 0x80 | (0x05 << 1)
188 
189 /*!
190  * Pressure compensate coefficient B1 (high byte)
191  */
192 #define MPL115A_COEFF_ADDR_B1_M 0x80 | (0x06 << 1)
193 /*!
194  * Pressure compensate coefficient B1 (low byte)
195  */
196 #define MPL115A_COEFF_ADDR_B1_L 0x80 | (0x07 << 1)
197 
198 /*!
199  * Pressure compensate coefficient B2 (high byte)
200  */
201 #define MPL115A_COEFF_ADDR_B2_M 0x80 | (0x08 << 1)
202 /*!
203  * Pressure compensate coefficient B2 (low byte)
204  */
205 #define MPL115A_COEFF_ADDR_B2_L 0x80 | (0x09 << 1)
206 
207 /*!
208  * Pressure compensate coefficient C12 (high byte)
209  */
210 #define MPL115A_COEFF_ADDR_C12_M 0x80 | (0x0A << 1)
211 /*!
212  * Pressure compensate coefficient C12 (low byte)
213  */
214 #define MPL115A_COEFF_ADDR_C12_L 0x80 | (0x0B << 1)
215 
216 /*!
217  * Pressure compensate coefficient C11 (high byte)
218  */
219 #define MPL115A_COEFF_ADDR_C11_M 0x80 | (0x0C << 1)
220 /*!
221  * Pressure compensate coefficient C11 (low byte)
222  */
223 #define MPL115A_COEFF_ADDR_C11_L 0x80 | (0x0D << 1)
224 
225 /*!
226  * Pressure compensate coefficient C22 (high byte)
227  */
228 #define MPL115A_COEFF_ADDR_C22_M 0x80 | (0x0E << 1)
229 /*!
230  * Pressure compensate coefficient C22 (low byte)
231  */
232 #define MPL115A_COEFF_ADDR_C22_L 0x80 | (0x0F << 1)
233 
234 /*!
235  * Start pressure measurement command
236  */
237 #define MPL115A_START_P_CONV (0x10 << 1)
238 /*!
239  * Start temperature measurement command
240  */
241 #define MPL115A_START_T_CONV (0x11 << 1)
242 /*!
243  * Start both, pressure and temperature measurement
244  * command
245  */
246 #define MPL115A_START_B_CONV (0x12 << 1)
247 
248 #define MPL115A_A0 0
249 #define MPL115A_B1 1
250 #define MPL115A_B2 2
251 #define MPL115A_C12 3
252 #define MPL115A_C11 4
253 #define MPL115A_C22 5
254 
255 
256 
257 typedef struct{
258  /*!
259  * Pressure compensate coefficient value
260  */
261  int16_t value;
262  /*!
263  * coefficient addresses
264  */
265  uint8_t addr[2];
266 }coeff_t;
267 
268 /*!
269  * Hold all coefficients with their belonging addresses
270  */
271 static coeff_t coefficients[6] = {
278 };
279 
280 
281 /**
282  * \brief Initialize the MPL115A Pressure Sensor
283  *
284  */
285 void mpl115a_init(void);
286 
287 /**
288  * \brief Get raw pressure value (10Bit)
289  *
290  * \return Pressure value
291  *
292  * \note The overall pressure measurement takes approximately
293  * 3ms
294  */
295 uint16_t mpl115a_get_pressure(void);
296 
297 /**
298  * \brief Get raw temperature value (10Bit)
299  *
300  * \return Temperature value
301  *
302  * \note The overall temperature measurement takes approximately
303  * 3ms
304  */
305 uint16_t mpl115a_get_temp(void);
306 
307 /*
308  * \brief This function calculates and returns the compensated
309  * pressure value
310  *
311  * \return Compensated pressure value
312  *
313  */
314 int16_t mpl115a_get_Pcomp(void);
315 
316 /*
317  * \brief This function reads out all coefficients from the
318  * MPL115A ROM
319  *
320  * \note It is not clear, if these values will change or if
321  * they are fix, please check that
322  *
323  */
324 void mpl115a_read_coefficients(void);
325 
326 /**
327  * \brief This function reads and writes from the given
328  * register of the MPL115A
329  *
330  * \param reg The register address
331  *
332  * \return The data value
333  */
334 uint8_t mpl115a_cmd(uint8_t reg);
335 
336 /** @} */ // mpl115a_interface
337 /** @} */ // inga_sensors_driver
338 
339 #endif /* PRESSUREMPL115A_H_ */