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