Contiki 2.5
pressure-bmp085.c
1 /*
2  * pressure-bmp085.c
3  *
4  * Created on: 04.07.2011
5  * Author: root
6  */
7 
8 #include "pressure-bmp085.h"
9 
10 
11 int8_t bmp085_init(void) {
12  uint8_t i = 0;
13  i2c_init();
14  while (bmp085_read16bit_data(BMP085_AC1_ADDR) == 0x00) {
15  _delay_ms(10);
16  if (i++ > 10) {
17  return -1;
18  }
19  }
21  return 0;
22 }
23 
24 int32_t bmp085_read_temperature(void) {
25  i2c_start(BMP085_DEV_ADDR_W);
26  i2c_write(BMP085_CTRL_REG_ADDR);
27  i2c_write(BMP085_CTRL_REG_TEMP);
28  i2c_stop();
29  _delay_ms(5);
30  return (int32_t) (bmp085_read16bit_data(BMP085_DATA_REG_N));
31 }
32 
33 int32_t bmp085_read_pressure(uint8_t mode) {
34  int32_t pressure;
35  i2c_start(BMP085_DEV_ADDR_W);
36  i2c_write(BMP085_CTRL_REG_ADDR);
37  switch (mode) {
38  case 0:
39  i2c_write(BMP085_CTRL_REG_PRESS_0);
40  i2c_stop();
41  _delay_ms(5);
42  break;
43  case 1:
44  i2c_write(BMP085_CTRL_REG_PRESS_1);
45  i2c_stop();
46  _delay_ms(8);
47  break;
48  case 2:
49  i2c_write(BMP085_CTRL_REG_PRESS_2);
50  i2c_stop();
51  _delay_ms(14);
52  break;
53  case 3:
54  i2c_write(BMP085_CTRL_REG_PRESS_3);
55  i2c_stop();
56  _delay_ms(26);
57  break;
58  }
60  pressure = pressure << 8;
62  return (pressure >> (8 - mode));
63 }
64 
66  int32_t ut = 0, compt = 0;
67 
68  int32_t x1, x2, b5;
69 
71 
72  x1 = ((int32_t) ut - (int32_t) bmp085_coeff.ac6)
73  * (int32_t) bmp085_coeff.ac5 >> 15;
74  x2 = ((int32_t) bmp085_coeff.mc << 11) / (x1 + bmp085_coeff.md);
75  b5 = x1 + x2;
76  compt = (b5 + 8) >> 4;
77 
78  return compt;
79 }
80 
81 int32_t bmp085_read_comp_pressure(uint8_t mode) {
82  int32_t ut = 0, compt = 0;
83  int32_t up = 0, compp = 0;
84 
85  int32_t x1, x2, b5, b6, x3, b3, p;
86  uint32_t b4, b7;
87 
89  up = bmp085_read_pressure(mode);
90 
91  x1 = ((int32_t) ut - (int32_t) bmp085_coeff.ac6)
92  * (int32_t) bmp085_coeff.ac5 >> 15;
93  x2 = ((int32_t) bmp085_coeff.mc << 11) / (x1 + bmp085_coeff.md);
94  b5 = x1 + x2;
95  compt = (b5 + 8) >> 4;
96 
97  b6 = b5 - 4000;
98  x1 = (bmp085_coeff.b2 * ((b6 * b6) >> 12)) >> 11;
99  x2 = (bmp085_coeff.ac2 * b6) >> 11;
100  x3 = x1 + x2;
101  b3 = (((((int32_t) bmp085_coeff.ac1) * 4 + x3) << mode) + 2) >> 2;
102  x1 = (bmp085_coeff.ac3 * b6) >> 13;
103  x2 = (bmp085_coeff.b1 * ((b6 * b6) >> 12)) >> 16;
104  x3 = ((x1 + x2) + 2) >> 2;
105  b4 = (bmp085_coeff.ac4 * (uint32_t) (x3 + 32768)) >> 15;
106  b7 = ((uint32_t) (up - b3) * (50000 >> mode));
107 
108  if (b7 < 0x80000000) {
109  p = (b7 << 1) / b4;
110  } else {
111  p = (b7 / b4) << 1;
112  }
113 
114  x1 = (p >> 8) * (p >> 8);
115  x1 = (x1 * 3038) >> 16;
116  x2 = (-7357 * p) >> 16;
117  compp = p + ((x1 + x2 + 3791) >> 4);
118  return compp;
119 }
120 
122  bmp085_coeff.ac1 = bmp085_read16bit_data(BMP085_AC1_ADDR);
123  bmp085_coeff.ac2 = bmp085_read16bit_data(BMP085_AC2_ADDR);
124  bmp085_coeff.ac3 = bmp085_read16bit_data(BMP085_AC3_ADDR);
125  bmp085_coeff.ac4 = bmp085_read16bit_data(BMP085_AC4_ADDR);
126  bmp085_coeff.ac5 = bmp085_read16bit_data(BMP085_AC5_ADDR);
127  bmp085_coeff.ac6 = bmp085_read16bit_data(BMP085_AC6_ADDR);
128  bmp085_coeff.b1 = bmp085_read16bit_data(BMP085_B1_ADDR);
129  bmp085_coeff.b2 = bmp085_read16bit_data(BMP085_B2_ADDR);
130  bmp085_coeff.mb = bmp085_read16bit_data(BMP085_MB_ADDR);
131  bmp085_coeff.mc = bmp085_read16bit_data(BMP085_MC_ADDR);
132  bmp085_coeff.md = bmp085_read16bit_data(BMP085_MD_ADDR);
133 }
134 
135 uint16_t bmp085_read16bit_data(uint8_t addr) {
136  uint8_t msb = 0, lsb = 0;
137  i2c_start(BMP085_DEV_ADDR_W);
138  i2c_write(addr);
139  i2c_rep_start(BMP085_DEV_ADDR_R);
140  i2c_read_ack(&msb);
141  i2c_read_nack(&lsb);
142  i2c_stop();
143  return (uint16_t) ((msb << 8) | lsb);
144 }
145 
146 uint8_t bmp085_read8bit_data(uint8_t addr) {
147  uint8_t lsb = 0;
148  i2c_start(BMP085_DEV_ADDR_W);
149  i2c_write(addr);
150  i2c_rep_start(BMP085_DEV_ADDR_R);
151  i2c_read_nack(&lsb);
152  i2c_stop();
153  return lsb;
154 }