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