Contiki 2.5
pressure-mpl115a.c
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  * \addtogroup mpl115a_interface Pressure Sensor (Barometer) MPL115A
30  * @{
31  */
32 
33 /**
34  * \file
35  * MPL115A Pressure Sensor (Barometer) interface implementation
36  * \author
37  * Ulf Kulau <kulau@ibr.cs.tu-bs.de>
38  */
39 
40 #include "pressure-mpl115a.h"
41 
42 void mpl115a_init(void) {
44  /*init mspi in mode0, at chip select pin 4 and max baud rate*/
46 }
47 
48 uint16_t mpl115a_get_pressure(void) {
49  uint8_t press_msb;
50  uint8_t press_lsb;
51  uint16_t pressure = 0x0000;
52  /*start pressure measurement*/
54  _delay_ms(6);
57  pressure = (uint16_t) (press_msb << 8);
58  pressure &= (0xFF00 | ((uint16_t) (press_lsb)));
59  pressure = 0x03FF & (pressure >> 6);
60  return pressure;
61 }
62 
63 uint16_t mpl115a_get_temp(void) {
64  uint8_t temp_msb;
65  uint8_t temp_lsb;
66  uint16_t temperature = 0x0000;
67  /*start temperature measurement*/
69  _delay_ms(6);
72 
73  temperature = (uint16_t) (temp_msb << 8);
74  temperature &= (0xFF00 | ((uint16_t) (temp_lsb)));
75  temperature = 0x03FF & (temperature >> 6);
76  return temperature;
77 }
78 
79 void mpl115a_read_coefficients(void) {
80  uint8_t i;
81  int8_t coeff_tmp_msb;
82  int8_t coeff_tmp_lsb;
83 
84  for (i = 0; i < 6; i++) {
85  coeff_tmp_msb = mpl115a_cmd(coefficients[i].addr[0]);
86  coeff_tmp_lsb = mpl115a_cmd(coefficients[i].addr[1]);
87  uart_TXchar(coeff_tmp_msb);
88  uart_TXchar(coeff_tmp_lsb);
89 
90 
91  coefficients[i].value = (int16_t) (coeff_tmp_msb << 8);
92  coefficients[i].value &= (0xFF00 | ((int16_t) (coeff_tmp_lsb)));
93  }
94  uart_TXchar('\n');
95 }
96 
97 int16_t mpl115a_get_Pcomp(void) {
98  int32_t tmp1_val, tmp2_val, tmp3_val;
99  int32_t tmp_help;
100  uint8_t msb, lsb;
101  uint16_t temperature = 0x0000;
102  uint16_t pressure = 0x0000;
103 
104  /*start both, temperature and pressure measurement*/
106  /*wait 3 ms*/
107  _delay_ms(6);
108  /*get temperature value*/
111  temperature = (uint16_t) (msb << 8);
112  temperature &= (0xFF00 | ((uint16_t) (lsb)));
113  temperature = 0x03FF & (temperature >> 6);
114  /*get pressure value*/
117  pressure = (uint16_t) (msb << 8);
118  pressure &= (0xFF00 | ((uint16_t) (lsb)));
119  pressure = 0x03FF & (pressure >> 6);
120 
121  /* calculate the compensated pressure value with the coefficients
122  * and temperature value*/
123  tmp1_val = (int32_t) coefficients[MPL115A_C11].value;
124  tmp2_val = (int32_t) pressure;
125  /*c11x1 = tmp3_val = c11 * pressure*/
126  tmp3_val = tmp1_val * tmp2_val;
127 
128  tmp1_val = (int32_t) (coefficients[MPL115A_B1].value << 14);
129  tmp2_val = (int32_t) tmp3_val;
130  tmp3_val = tmp1_val + tmp2_val;
131  /*a11 = tmp_help = b1 + c11x1*/
132  tmp_help = (int32_t) (tmp3_val >> 14);
133 
134  tmp1_val = (int32_t) coefficients[MPL115A_C12].value;
135  tmp2_val = (int32_t) temperature;
136  /*c12x2 = tmp3_val = c12 * temperature*/
137  tmp3_val = tmp1_val * tmp2_val;
138 
139  tmp1_val = (int32_t) (tmp_help << 11);
140  tmp2_val = (int32_t) tmp3_val;
141  tmp3_val = tmp1_val + tmp2_val;
142  /*tmp_help = a1 = a11 + c12x2*/
143  tmp_help = (int32_t) (tmp3_val >> 11);
144 
145  tmp1_val = (int32_t) coefficients[MPL115A_C22].value;
146  tmp2_val = (int32_t) temperature;
147  /*c22x2 = tmp3_val = c22 * temperature*/
148  tmp3_val = tmp1_val * tmp2_val;
149 
150  tmp1_val = (int32_t) (coefficients[MPL115A_B2].value << 15);
151  tmp2_val = (int32_t) (tmp3_val >> 1);
152  tmp3_val = tmp1_val + tmp2_val;
153 
154  tmp1_val = (int32_t) tmp_help;
155  /*a2 = b2 + c22x2*/
156  tmp_help = (int32_t) (tmp3_val >> 16);
157  tmp2_val = (int32_t) pressure;
158  /*a1x1 = tmp3_val = a1 * pressure*/
159  tmp3_val = tmp1_val * tmp2_val;
160 
161  tmp1_val = (int32_t) (coefficients[MPL115A_A0].value << 10);
162  tmp2_val = (int32_t) tmp3_val;
163  tmp3_val = tmp1_val + tmp2_val;
164 
165  tmp1_val = (int32_t) tmp_help;
166  /*y1 = tmp_help = a0 + a1x1*/
167  tmp_help = (int32_t) (tmp3_val >> 10);
168  tmp2_val = (int32_t) temperature;
169  /*a2x2 = tmp3_val = a2 * temperature*/
170  tmp3_val = tmp1_val * tmp2_val;
171 
172  tmp1_val = (int32_t) (tmp_help << 10);
173  tmp2_val = (int32_t) tmp3_val;
174  tmp3_val = tmp1_val + tmp2_val;
175 
176  return (int16_t) (tmp3_val >> 13);
177 }
178 
179 uint8_t mpl115a_cmd(uint8_t reg) {
180  uint8_t data;
182  mspi_transceive(reg);
183  data = mspi_transceive(0x00);
185  return data;
186 }