forked from mpgiii/MSP432-Weather-Station
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbmp280.c
More file actions
311 lines (221 loc) · 8.14 KB
/
bmp280.c
File metadata and controls
311 lines (221 loc) · 8.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/* bmp280.c
* Written by Connor McKee and Michael Georgariou
* CPE 316 - Spring 2020
*
* For use with the MSP432. */
#include "bmp280.h"
#include "msp.h"
int8_t calibration_data[CALIB_DATA_WIDTH];
int32_t t_fine;
/* SPI_init
* initializes SPI communication on the MSP432. */
void SPI_init(void) {
/* first reset the EUSCI */
EUSCI_B0 -> CTLW0 |= EUSCI_B_CTLW0_SWRST;
/* next, set bits to do the following:
* polarity as active low
* enable synchronous mode
* use the SMCLK as the clock source
* set the MSB first selector
* set as the master */
EUSCI_B0 -> CTLW0 |= EUSCI_B_CTLW0_SWRST
| EUSCI_B_CTLW0_CKPL
| EUSCI_B_CTLW0_SYNC
| EUSCI_B_CTLW0_UCSSEL_2
| EUSCI_B_CTLW0_MSB
| EUSCI_B_CTLW0_MST;
/* set the CS bit as an output */
CS_PORT -> SEL0 &= ~CS_BIT;
CS_PORT -> SEL1 &= ~CS_BIT;
CS_PORT -> DIR |= CS_BIT;
CS_PORT -> OUT |= CS_BIT;
/* set the appropriate SPI port bits to run in primary mode */
SPI_PORT -> SEL0 |= (CLK_BIT | MOSI_BIT | MISO_BIT);
SPI_PORT -> SEL1 &= ~(CLK_BIT | MOSI_BIT | MISO_BIT);
/* set CLK and MOSI as outputs */
SPI_PORT -> DIR |= (CLK_BIT | MOSI_BIT);
SPI_PORT -> OUT |= (CLK_BIT | MOSI_BIT);
/* set MISO as input */
SPI_PORT -> DIR &= ~MISO_BIT;
SPI_PORT -> REN &= ~MISO_BIT;
/* clear reset */
EUSCI_B0 -> CTLW0 &= ~(EUSCI_B_CTLW0_SWRST);
return;
}
void BMP_init(){
/* configure BMP280 with the following settings:
* Temperature Oversampling x2
* Pressure Oversampling x2
* Normal Power Mode
*/
send_to_BMP(CTRL_ADDR, LOW_RES);
/* Get calibration data for temp and pressure compensation */
fetch_calibration_data();
return;
}
void fetch_calibration_data(){
int i;
int8_t calibration_value;
uint8_t addr = CALIB_START_ADDR;
/* Populate the calibration table */
for(i = 0; i < CALIB_DATA_WIDTH; i++){
calibration_value = read_calibration_value(addr);
calibration_data[i] = calibration_value;
/* increment addr by two for next LSB/MSB pair */
addr += 2;
}
}
uint16_t read_calibration_value(uint8_t addr_lsb){
uint8_t lsb, msb;
uint16_t dig;
/* Data format on chip: sequential 8bit LSB/MSB pairs */
/* set the CS bit low for reading/writing to BMP */
CS_PORT -> OUT &= ~(CS_BIT);
/* wait until transmit buffer is empty */
while (!(EUSCI_B0 -> IFG & EUSCI_B_IFG_TXIFG));
/* Transmit control byte */
EUSCI_B0 -> TXBUF = addr_lsb;
/* wait until transmit buffer is empty */
while (!(EUSCI_B0 -> IFG & EUSCI_B_IFG_TXIFG));
/* Wait for input buffer to fill */
while (!(EUSCI_B0 -> IFG & EUSCI_B_IFG_RXIFG));
/* Store LSB input */
lsb = EUSCI_B0 -> RXBUF;
while (!(EUSCI_B0 -> IFG & EUSCI_B_IFG_TXIFG));
/* Transmit control byte. Add one for MSB address */
EUSCI_B0 -> TXBUF = addr_lsb + 1;
while (!(EUSCI_B0 -> IFG & EUSCI_B_IFG_TXIFG));
while (!(EUSCI_B0 -> IFG & EUSCI_B_IFG_RXIFG));
/* Store MSB input */
msb = EUSCI_B0 -> RXBUF;
/* End transmission */
CS_PORT -> OUT |= CS_BIT;
dig = ((msb << 8) | lsb);
return dig;
}
void send_to_BMP(uint8_t addr, uint8_t data){
/* Set control for writing BIT7 = 0 */
addr &= ~BIT7;
/* set the CS bit low for writing to BMP */
CS_PORT -> OUT &= ~(CS_BIT);
/* wait until transmit buffer is empty */
while (!(EUSCI_B0 -> IFG & EUSCI_B_IFG_TXIFG));
/* Transmit control byte */
EUSCI_B0 -> TXBUF = addr;
/* wait until transmit buffer is empty */
while (!(EUSCI_B0 -> IFG & EUSCI_B_IFG_TXIFG));
/* Transmit data/config byte */
EUSCI_B0 -> TXBUF = data;
/* wait for transmit to complete */
while(!(EUSCI_B0 -> IFG & EUSCI_B_IFG_RXIFG));
/* set the CS bit high to end write*/
CS_PORT -> OUT |= CS_BIT;
return;
}
int32_t read_from_BMP(uint8_t mode){
uint8_t msb, lsb, xlsb, addr;
int32_t input = 0;
/* Configure for reading 8 bits MSB Pressure/Temp data BIT7 = 1 */
if(mode == PRESSURE_READ){
addr = PRESS_MSB_ADDR | BIT7;
} else {
addr = TEMP_MSB_ADDR | BIT7;
}
/* set the CS bit low for reading/writing to BMP */
CS_PORT -> OUT &= ~(CS_BIT);
/* wait until transmit buffer is empty */
while (!(EUSCI_B0 -> IFG & EUSCI_B_IFG_TXIFG));
/* Transmit control byte */
EUSCI_B0 -> TXBUF = addr;
/* wait until transmit buffer is empty */
while (!(EUSCI_B0 -> IFG & EUSCI_B_IFG_TXIFG));
/* Wait for input buffer to fill */
while (!(EUSCI_B0 -> IFG & EUSCI_B_IFG_RXIFG));
/* Store MSB input */
msb = EUSCI_B0 -> RXBUF;
/* Configure next address for LSB */
if(mode == PRESSURE_READ){
addr = PRESS_LSB_ADDR | BIT7;
} else {
addr = TEMP_LSB_ADDR | BIT7;
}
/* wait until transmit buffer is empty */
while (!(EUSCI_B0 -> IFG & EUSCI_B_IFG_TXIFG));
/* Transmit control byte */
EUSCI_B0 -> TXBUF = addr;
/* wait until transmit buffer is empty */
while (!(EUSCI_B0 -> IFG & EUSCI_B_IFG_TXIFG));
/* Wait for input buffer to fill */
while (!(EUSCI_B0 -> IFG & EUSCI_B_IFG_RXIFG));
/* Store LSB input */
lsb = EUSCI_B0 -> RXBUF;
/* Configure next address for XLSB */
if(mode == PRESSURE_READ){
addr = PRESS_XLSB_ADDR | BIT7;
} else {
addr = TEMP_XLSB_ADDR | BIT7;
}
/* wait until transmit buffer is empty */
while (!(EUSCI_B0 -> IFG & EUSCI_B_IFG_TXIFG));
/* Transmit control byte */
EUSCI_B0 -> TXBUF = addr;
/* wait until transmit buffer is empty */
while (!(EUSCI_B0 -> IFG & EUSCI_B_IFG_TXIFG));
/* Wait for input buffer to fill */
while (!(EUSCI_B0 -> IFG & EUSCI_B_IFG_RXIFG));
/* Store MSB input */
xlsb = EUSCI_B0 -> RXBUF;
/* set the CS bit high to end transmission */
CS_PORT -> OUT |= CS_BIT;
/* Combine input */
input = ((msb << 16) | (lsb << 8) | xlsb);
input >>= 4;
return input;
}
/* Following the temperature conversion outlined by Bosch */
//int32_t compensate_temp(int32_t uncomp_temp){
// int32_t var1, var2, T;
//
// var1 = ((uncomp_temp>>3) - ((int32_t)calibration_data[DIG_T1]<<1)) *
// ((int32_t) calibration_data[DIG_T2]) >> 11;
// var2 = ((((uncomp_temp>>4) - ((int32_t)calibration_data[DIG_T1])) *
// ((uncomp_temp>>4) - ((int32_t)calibration_data[DIG_T1]))) >> 12) *
// ((int32_t)calibration_data[DIG_T3]) >> 14;
// t_fine = var1 + var2;
// T = (t_fine * 5 + 128) >> 8;
// //T = (var1 + var2)/5120;
// return T;
//}
int32_t compensate_temp(int32_t uncomp_temp){
int32_t var1, var2, T;
var1 = (((double)uncomp_temp)/16384.0 -
((double)calibration_data[DIG_T1])/1024.0) *
((double)calibration_data[DIG_T2]);
var2 = ((((double)uncomp_temp)/131072.0 -
((double)calibration_data[DIG_T1])/8192.0) *
(((double)uncomp_temp)/131072.0 -
((double)calibration_data[DIG_T1])/8192.0)) *
((double)calibration_data[DIG_T3]);
t_fine = ((int32_t)(var1 + var2));
T = ((var1 + var2)/5120.0);
return T;
}
uint32_t compensate_pressure(int32_t uncomp_press){
int64_t var1, var2, p;
var1 = ((int64_t) t_fine) -128000;
var2 = var1 * var1 * (int64_t)calibration_data[DIG_P6];
var2 = var2 + ((var1 * (int64_t)calibration_data[DIG_P5]) << 17);
var2 = var2 + (((int64_t)calibration_data[DIG_P4]) << 35);
var1 = ((var1 * var1 * (int64_t)calibration_data[DIG_P3]) >> 8) +
((var1 * (int64_t)calibration_data[DIG_P2]) << 12);
var1 = (((((int64_t)1) << 47) + var1)) * ((int64_t)calibration_data[DIG_P1]) >> 33;
if(var1 == 0){
return 0;
}
p = 1048576 - uncomp_press;
p = (((p << 31) - var2) * 3125) / var1;
var1 = (((int64_t)calibration_data[DIG_P9]) * (p >> 13) * (p >> 13)) >> 25;
var2 = (((int64_t)calibration_data[DIG_P8]) * p) >> 19;
p = ((p + var1 + var2) >> 8) + (((int64_t)calibration_data[DIG_P7]) << 4);
return (uint32_t) p;
}