Contiki-NG
bme280.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, Copyright Robert Olsson
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  * This file is part of the Contiki operating system.
30  *
31  *
32  * Author : Robert Olsson rolss@kth.se/robert@radio-sensors.com
33  * Created : 2016-09-14
34  */
35 
36 /**
37  * \file
38  * Basic functions for Bosch BME280 based on datasheet Rev 1.1
39  */
40 
41 #include "contiki.h"
42 #include <string.h>
44 #include "dev/sensor/bme280/bme280-arch.h"
45 #include "lib/sensors.h"
46 
47 static struct {
48  unsigned short dig_t1;
49  signed short dig_t2;
50  signed short dig_t3;
51  unsigned short dig_p1;
52  signed short dig_p2;
53  signed short dig_p3;
54  signed short dig_p4;
55  signed short dig_p5;
56  signed short dig_p6;
57  signed short dig_p7;
58  signed short dig_p8;
59  signed short dig_p9;
60  unsigned char dig_h1;
61  signed short dig_h2;
62  unsigned char dig_h3;
63  signed short dig_h4;
64  signed short dig_h5;
65  signed char dig_h6;
66  int32_t t_fine;
67  uint8_t mode;
68 } bm;
69 
70 int32_t
71 bme280_t_overscale100(int32_t ut)
72 {
73  int32_t v1, v2, t;
74 
75  v1 = ((((ut >> 3) - ((int32_t)bm.dig_t1 << 1))) *
76  ((int32_t)bm.dig_t2)) >> 11;
77 
78  v2 = (((((ut >> 4) - ((int32_t)bm.dig_t1)) * ((ut >> 4) -
79  ((int32_t)bm.dig_t1))) >> 12) * ((int32_t)bm.dig_t3)) >> 14;
80 
81  bm.t_fine = v1 + v2;
82  t = (bm.t_fine * 5 + 128) >> 8;
83  return t;
84 }
85 #ifdef BME280_32BIT
86 static uint32_t
87 bme280_p(int32_t up)
88 {
89  int32_t v1, v2;
90  uint32_t p;
91 
92  v1 = (((int32_t)bm.t_fine) >> 1) - (int32_t)64000;
93  v2 = (((v1 >> 2) * (v1 >> 2)) >> 11) * ((int32_t)bm.dig_p6);
94  v2 = v2 + ((v1 * ((int32_t)bm.dig_p5)) << 1);
95  v2 = (v2 >> 2) + (((int32_t)bm.dig_p4) << 16);
96 
97  v1 = (((bm.dig_p3 * (((v1 >> 2) * (v1 >> 2)) >> 13)) >> 3) +
98  ((((int32_t)bm.dig_p2) * v1) >> 1)) >> 18;
99 
100  v1 = ((((32768 + v1)) * ((int32_t)bm.dig_p1)) >> 15);
101 
102  if(v1 == 0) {
103  return 0;
104  }
105 
106  p = (((uint32_t)(((int32_t)1048576) - up) - (v2 >> 12))) * 3125;
107 
108  if(p < 0x80000000) {
109  p = (p << 1) / ((uint32_t)v1);
110  } else {
111  p = (p / (uint32_t)v1) * 2;
112  }
113 
114  v1 = (((int32_t)bm.dig_p9) * ((int32_t)(((p >> 3) * (p >> 3)) >> 13))) >> 12;
115  v2 = (((int32_t)(p >> 2)) * ((int32_t)bm.dig_p8)) >> 13;
116  p = (uint32_t)((int32_t)p + ((v1 + v2 + bm.dig_p7) >> 4));
117  return p;
118 }
119 #else
120 
121 static uint32_t
122 bme280_p_overscale256(int32_t up)
123 {
124  int64_t v1, v2, p;
125 
126  v1 = ((int64_t)bm.t_fine) - 128000;
127  v2 = v1 * v1 * (int64_t)bm.dig_p6;
128  v2 = v2 + ((v1 * (int64_t)bm.dig_p5) << 17);
129  v2 = v2 + (((int64_t)bm.dig_p4) << 35);
130  v1 = ((v1 * v1 * (int64_t)bm.dig_p3) >> 8) + ((v1 * (int64_t)bm.dig_p2) << 12);
131  v1 = (((((int64_t)1) << 47) + v1)) * ((int64_t)bm.dig_p1) >> 33;
132 
133  if(v1 == 0) {
134  return 0;
135  }
136 
137  p = 1048576 - up;
138  p = (((p << 31) - v2) * 3125) / v1;
139  v1 = (((int64_t)bm.dig_p9) * (p >> 13) * (p >> 13)) >> 25;
140  v2 = (((int64_t)bm.dig_p8) * p) >> 19;
141  p = (((p + v1 + v2) >> 8) + (((int64_t)bm.dig_p7) << 4));
142  return (uint32_t)p;
143 }
144 #endif
145 
146 static uint32_t
147 bme280_h_overscale1024(int32_t uh)
148 {
149  int32_t v1;
150  v1 = (bm.t_fine - ((int32_t)76800));
151  v1 = (((((uh << 14) - (((int32_t)bm.dig_h4) << 20) - (((int32_t)bm.dig_h5) * v1)) + ((int32_t)16384)) >> 15)
152  * (((((((v1 * ((int32_t)bm.dig_h6)) >> 10) * (((v1 * ((int32_t)bm.dig_h3)) >> 11) + ((int32_t)32768)))
153  >> 10) + ((int32_t)2097152)) * ((int32_t)bm.dig_h2) + 8192) >> 14));
154  v1 = (v1 - (((((v1 >> 15) * (v1 >> 15)) >> 7) * ((int32_t)bm.dig_h1)) >> 4));
155  v1 = (v1 < 0 ? 0 : v1);
156  v1 = (v1 > 419430400 ? 419430400 : v1);
157  return (uint32_t)(v1 >> 12);
158 }
159 uint8_t
160 bme280_init(uint8_t mode)
161 {
162  uint16_t i;
163  uint8_t buf[26];
164 
165  bme280_arch_i2c_init();
166 
167  /* Do not mess with other chips */
168  bme280_arch_i2c_read_mem(BME280_ADDR, 0xD0, buf, 1);
169  if(buf[0] != BME280_CHIP_ID) {
170  return 0;
171  }
172 
173  bme280_arch_i2c_write_mem(BME280_ADDR, BME280_CNTL_RESET, 0xB6);
174 
175  for(i = 0; i < BME280_MAX_WAIT; i++) {
176  clock_delay_usec(1000);
177  }
178 
179  memset(buf, 0, sizeof(buf));
180 
181  /* Burst read of all calibration part 1 */
182  bme280_arch_i2c_read_mem(BME280_ADDR, BME280_DIG_T1_ADDR, buf, sizeof(buf));
183  bm.dig_t1 = ((uint16_t)buf[1] << 8) | (uint16_t)buf[0];
184  bm.dig_t2 = ((int16_t)buf[3] << 8) | (uint16_t)buf[2];
185  bm.dig_t3 = ((int16_t)buf[5] << 8) | (uint16_t)buf[4];
186  bm.dig_p1 = ((uint16_t)buf[7] << 8) | (uint16_t)buf[6];
187  bm.dig_p2 = ((int16_t)buf[9] << 8) | (uint16_t)buf[8];
188  bm.dig_p3 = ((int16_t)buf[11] << 8) | (uint16_t)buf[10];
189  bm.dig_p4 = ((int16_t)buf[13] << 8) | (uint16_t)buf[12];
190  bm.dig_p5 = ((int16_t)buf[15] << 8) | (uint16_t)buf[14];
191  bm.dig_p6 = ((int16_t)buf[17] << 8) | (uint16_t)buf[16];
192  bm.dig_p7 = ((int16_t)buf[19] << 8) | (uint16_t)buf[18];
193  bm.dig_p8 = ((int16_t)buf[21] << 8) | (uint16_t)buf[20];
194  bm.dig_p9 = ((int16_t)buf[23] << 8) | (uint16_t)buf[22];
195  /* A0 not used */
196  bm.dig_h1 = (unsigned char)buf[25];
197 
198  /* Burst read of all calibration part 2 */
199  bme280_arch_i2c_read_mem(BME280_ADDR, BME280_DIG_H2_ADDR, buf, 8);
200  bm.dig_h2 = ((int16_t)buf[1] << 8) | (uint16_t)buf[0];
201  bm.dig_h3 = (unsigned char)buf[2];
202  bm.dig_h4 = ((int16_t)buf[3] << 4) | (((uint16_t)buf[4]) & 0xF);
203  bm.dig_h5 = ((int16_t)buf[6] << 4) | (((uint16_t)buf[5]) & 0xF);
204  bm.dig_h6 = (unsigned char)buf[7];
205 
206  bm.mode = mode;
207  return 1;
208 }
209 void
210 bme280_read(uint8_t mode)
211 {
212  int32_t ut, uh, up;
213  uint8_t buf[8], sleep;
214  uint16_t i;
215  memset(buf, 0, sizeof(buf));
216 
217  /* Are we initilized and in the right mode? */
218  if(mode == BME280_MODE_NONE || mode != bm.mode) {
219  return;
220  }
221 
222  ut = uh = up = 0;
223 
224  /* Weather mode. See sectiom 3.5 Datasheet */
225  if(mode == BME280_MODE_WEATHER) {
226  /* Humidity oversampling *1 */
227  bme280_arch_i2c_write_mem(BME280_ADDR, BME280_CNTL_HUM, 0x01);
228 
229  /* 00100111 0x27 oversampling *1 for t and p plus normal mode */
230  /* 0.5 ms -- no filter -- no SPI */
231  bme280_arch_i2c_write_mem(BME280_ADDR, BME280_CONTROL, 0x00);
232 
233  /* 00100110 0x26 oversampling *1 for t and p plus forced mode */
234  /* Trigger measurement needed for every time in forced mode */
235  bme280_arch_i2c_write_mem(BME280_ADDR, BME280_CNTL_MEAS, 0x26);
236  /* Wait to get into sleep mode == measurement done */
237  for(i = 0; i < BME280_MAX_WAIT; i++) {
238  bme280_arch_i2c_read_mem(BME280_ADDR, BME280_CNTL_MEAS, &sleep, 1);
239  sleep = sleep& 0x03;
240  if(sleep== 0) {
241  break;
242  } else {
243  clock_delay_usec(1000);
244  }
245  }
246  if(i == BME280_MAX_WAIT) {
247  return; /* error wait*/
248  }
249  } else { /* if(mode == BME280_MODE_WEATHER) */
250  return; /* error mode*/
251  }
252 
253  /* Burst read of all measurements */
254  bme280_arch_i2c_read_mem(BME280_ADDR, BME280_PRESS, buf, 8);
255  ut = (uint32_t)(buf[3]) << 12 | (uint32_t)(buf[4]) << 4 | (uint32_t)buf[5] >> 4;
256  up = (uint32_t)(buf[0]) << 12 | (uint32_t)(buf[1]) << 4 | (uint32_t)buf[2] >> 4;
257  uh = (uint32_t)(buf[6]) << 8 | (uint32_t)buf[7];
258 
259  bme280_mea.t_overscale100 = bme280_t_overscale100(ut);
260  bme280_mea.h_overscale1024 = bme280_h_overscale1024(uh);
261 #ifdef BME280_64BIT
262  bme280_mea.p_overscale256 = bme280_p_overscale256(up);
263 #else
264  bme280_mea.p = bme280_p(up);
265 #endif
266 
267 #if TEST
268  printf("T_BME280=%5.2f", (double)bme280_mea.t_overscale100 / 100.);
269  printf(" RH_BME280=%5.2f ", (double)bme280_mea.h_overscale1024 / 1024.);
270 #ifdef BME280_64BIT
271  printf(" P_BME280=%5.2f\n", (double)bme280_mea.p_overscale256 / 256.);
272 #else
273  printf(" P_BME280=%5.2f\n", (double)bme280_mea.p);
274 #endif
275 #endif
276 }
void clock_delay_usec(uint16_t dt)
Delay a given number of microseconds.
Definition: clock.c:150
Definitions for the Bosch BME280 based on datasheet Rev 1.1