Test BME and TLC59711 BLE Nano 1.5

Fork of BME280_SPI by Toyomasa Watarai

Committer:
MACRUM
Date:
Sun Mar 12 03:02:05 2017 +0000
Revision:
6:b91c721722d2
Parent:
BME280.cpp@5:c1f1647004c4
Child:
7:dfd6107f1f92
SPI version prototyping

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MACRUM 1:763a4018aaec 1 /**
MACRUM 5:c1f1647004c4 2 ******************************************************************************
MACRUM 6:b91c721722d2 3 * @file BME280_SPI.cpp
MACRUM 5:c1f1647004c4 4 * @author Toyomasa Watarai
MACRUM 5:c1f1647004c4 5 * @version V1.0.0
MACRUM 5:c1f1647004c4 6 * @date 11 March 2017
MACRUM 6:b91c721722d2 7 * @brief BME280_SPI class implementation
MACRUM 5:c1f1647004c4 8 ******************************************************************************
MACRUM 5:c1f1647004c4 9 * @attention
MACRUM 1:763a4018aaec 10 *
MACRUM 5:c1f1647004c4 11 * Permission is hereby granted, free of charge, to any person obtaining a copy
MACRUM 5:c1f1647004c4 12 * of this software and associated documentation files (the "Software"), to deal
MACRUM 5:c1f1647004c4 13 * in the Software without restriction, including without limitation the rights
MACRUM 5:c1f1647004c4 14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
MACRUM 5:c1f1647004c4 15 * copies of the Software, and to permit persons to whom the Software is
MACRUM 5:c1f1647004c4 16 * furnished to do so, subject to the following conditions:
MACRUM 1:763a4018aaec 17 *
MACRUM 5:c1f1647004c4 18 * The above copyright notice and this permission notice shall be included in
MACRUM 5:c1f1647004c4 19 * all copies or substantial portions of the Software.
MACRUM 1:763a4018aaec 20 *
MACRUM 5:c1f1647004c4 21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
MACRUM 5:c1f1647004c4 22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
MACRUM 5:c1f1647004c4 23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
MACRUM 5:c1f1647004c4 24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
MACRUM 5:c1f1647004c4 25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
MACRUM 5:c1f1647004c4 26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
MACRUM 5:c1f1647004c4 27 * THE SOFTWARE.
MACRUM 1:763a4018aaec 28 */
MACRUM 1:763a4018aaec 29
MACRUM 0:ade9be832910 30 #include "mbed.h"
MACRUM 6:b91c721722d2 31 #include "BME280_SPI.h"
MACRUM 0:ade9be832910 32
MACRUM 6:b91c721722d2 33 BME280_SPI::BME280_SPI(PinName mosi, PinName miso, PinName sclk, PinName cs)
MACRUM 0:ade9be832910 34 :
MACRUM 6:b91c721722d2 35 _spi(mosi, miso, sclk),
MACRUM 6:b91c721722d2 36 _cs(cs),
MACRUM 0:ade9be832910 37 t_fine(0)
MACRUM 0:ade9be832910 38 {
MACRUM 0:ade9be832910 39 initialize();
MACRUM 0:ade9be832910 40 }
MACRUM 0:ade9be832910 41
MACRUM 6:b91c721722d2 42
MACRUM 6:b91c721722d2 43 BME280_SPI::~BME280_SPI()
MACRUM 0:ade9be832910 44 {
MACRUM 0:ade9be832910 45 }
MACRUM 6:b91c721722d2 46
MACRUM 6:b91c721722d2 47 void BME280_SPI::initialize()
MACRUM 0:ade9be832910 48 {
MACRUM 0:ade9be832910 49 char cmd[18];
MACRUM 6:b91c721722d2 50
MACRUM 6:b91c721722d2 51 _cs = 1;
MACRUM 6:b91c721722d2 52 _spi.format(8, 0); // 8-bit, mode=0
MACRUM 6:b91c721722d2 53 _spi.frequency(10000000); // 10MHZ
MACRUM 6:b91c721722d2 54
MACRUM 6:b91c721722d2 55 _cs = 0;
MACRUM 0:ade9be832910 56 cmd[0] = 0xf2; // ctrl_hum
MACRUM 0:ade9be832910 57 cmd[1] = 0x01; // Humidity oversampling x1
MACRUM 6:b91c721722d2 58 _spi.write(0xf2); // ctrl_hum
MACRUM 6:b91c721722d2 59 _spi.write(0x01); // Humidity oversampling x1
MACRUM 6:b91c721722d2 60 _cs = 1;
MACRUM 6:b91c721722d2 61
MACRUM 6:b91c721722d2 62 _cs = 0;
MACRUM 0:ade9be832910 63 cmd[0] = 0xf4; // ctrl_meas
MACRUM 0:ade9be832910 64 cmd[1] = 0x27; // Temparature oversampling x1, Pressure oversampling x1, Normal mode
MACRUM 6:b91c721722d2 65 //i2c.write(address, cmd, 2);
MACRUM 6:b91c721722d2 66 _spi.write(0xf4); // ctrl_meas
MACRUM 6:b91c721722d2 67 _spi.write(0x27); // Temparature oversampling x1, Pressure oversampling x1, Normal mode
MACRUM 6:b91c721722d2 68 _cs = 1;
MACRUM 6:b91c721722d2 69
MACRUM 6:b91c721722d2 70 _cs = 0;
MACRUM 0:ade9be832910 71 cmd[0] = 0xf5; // config
MACRUM 0:ade9be832910 72 cmd[1] = 0xa0; // Standby 1000ms, Filter off
MACRUM 6:b91c721722d2 73 //i2c.write(address, cmd, 2);
MACRUM 6:b91c721722d2 74 _spi.write(0xf5); // config
MACRUM 6:b91c721722d2 75 _spi.write(0xa0); // Standby 1000ms, Filter off
MACRUM 6:b91c721722d2 76 _cs = 1;
MACRUM 6:b91c721722d2 77
MACRUM 6:b91c721722d2 78 _cs = 0;
MACRUM 0:ade9be832910 79 cmd[0] = 0x88; // read dig_T regs
MACRUM 6:b91c721722d2 80 //i2c.write(address, cmd, 1);
MACRUM 6:b91c721722d2 81 _spi.write(0x88); // read dig_T regs
MACRUM 6:b91c721722d2 82 //i2c.read(address, cmd, 6);
MACRUM 6:b91c721722d2 83 for(int i = 0; i < 6; i++)
MACRUM 6:b91c721722d2 84 cmd[i] = _spi.write(0);
MACRUM 6:b91c721722d2 85 _cs = 1;
MACRUM 6:b91c721722d2 86
MACRUM 0:ade9be832910 87 dig_T1 = (cmd[1] << 8) | cmd[0];
MACRUM 0:ade9be832910 88 dig_T2 = (cmd[3] << 8) | cmd[2];
MACRUM 0:ade9be832910 89 dig_T3 = (cmd[5] << 8) | cmd[4];
MACRUM 6:b91c721722d2 90
MACRUM 0:ade9be832910 91 DEBUG_PRINT("dig_T = 0x%x, 0x%x, 0x%x\n", dig_T1, dig_T2, dig_T3);
MACRUM 6:b91c721722d2 92
MACRUM 6:b91c721722d2 93 _cs = 0;
MACRUM 0:ade9be832910 94 cmd[0] = 0x8E; // read dig_P regs
MACRUM 6:b91c721722d2 95 //i2c.write(address, cmd, 1);
MACRUM 6:b91c721722d2 96 _spi.write(0x8e);
MACRUM 6:b91c721722d2 97 //i2c.read(address, cmd, 18);
MACRUM 6:b91c721722d2 98 for(int i = 0; i < 18; i++)
MACRUM 6:b91c721722d2 99 cmd[i] = _spi.write(0);
MACRUM 6:b91c721722d2 100 _cs = 1;
MACRUM 6:b91c721722d2 101
MACRUM 0:ade9be832910 102 dig_P1 = (cmd[ 1] << 8) | cmd[ 0];
MACRUM 0:ade9be832910 103 dig_P2 = (cmd[ 3] << 8) | cmd[ 2];
MACRUM 0:ade9be832910 104 dig_P3 = (cmd[ 5] << 8) | cmd[ 4];
MACRUM 0:ade9be832910 105 dig_P4 = (cmd[ 7] << 8) | cmd[ 6];
MACRUM 0:ade9be832910 106 dig_P5 = (cmd[ 9] << 8) | cmd[ 8];
MACRUM 0:ade9be832910 107 dig_P6 = (cmd[11] << 8) | cmd[10];
MACRUM 0:ade9be832910 108 dig_P7 = (cmd[13] << 8) | cmd[12];
MACRUM 0:ade9be832910 109 dig_P8 = (cmd[15] << 8) | cmd[14];
MACRUM 0:ade9be832910 110 dig_P9 = (cmd[17] << 8) | cmd[16];
MACRUM 6:b91c721722d2 111
MACRUM 0:ade9be832910 112 DEBUG_PRINT("dig_P = 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", dig_P1, dig_P2, dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9);
MACRUM 6:b91c721722d2 113
MACRUM 6:b91c721722d2 114 _cs = 0;
MACRUM 0:ade9be832910 115 cmd[0] = 0xA1; // read dig_H regs
MACRUM 6:b91c721722d2 116 //i2c.write(address, cmd, 1);
MACRUM 6:b91c721722d2 117 _spi.write(0xA1); // read dig_H regs
MACRUM 6:b91c721722d2 118 //i2c.read(address, cmd, 1);
MACRUM 6:b91c721722d2 119 cmd[0] = _spi.write(0);
MACRUM 6:b91c721722d2 120 _cs = 1;
MACRUM 6:b91c721722d2 121
MACRUM 6:b91c721722d2 122 _cs = 0;
MACRUM 6:b91c721722d2 123 cmd[1] = 0xE1; // read dig_H regs
MACRUM 6:b91c721722d2 124 //i2c.write(address, &cmd[1], 1);
MACRUM 6:b91c721722d2 125 _spi.write(0xE1); // read dig_H regs
MACRUM 6:b91c721722d2 126 //i2c.read(address, &cmd[1], 7);
MACRUM 6:b91c721722d2 127 for(int i = 0; i < 7; i++)
MACRUM 6:b91c721722d2 128 cmd[1+i] = _spi.write(0);
MACRUM 6:b91c721722d2 129 _cs = 1;
MACRUM 2:c35f637c28ef 130
MACRUM 0:ade9be832910 131 dig_H1 = cmd[0];
MACRUM 0:ade9be832910 132 dig_H2 = (cmd[2] << 8) | cmd[1];
MACRUM 0:ade9be832910 133 dig_H3 = cmd[3];
MACRUM 0:ade9be832910 134 dig_H4 = (cmd[4] << 4) | (cmd[5] & 0x0f);
MACRUM 2:c35f637c28ef 135 dig_H5 = (cmd[6] << 4) | ((cmd[5]>>4) & 0x0f);
MACRUM 2:c35f637c28ef 136 dig_H6 = cmd[7];
MACRUM 6:b91c721722d2 137
MACRUM 0:ade9be832910 138 DEBUG_PRINT("dig_H = 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", dig_H1, dig_H2, dig_H3, dig_H4, dig_H5, dig_H6);
MACRUM 0:ade9be832910 139 }
MACRUM 6:b91c721722d2 140
MACRUM 6:b91c721722d2 141 float BME280_SPI::getTemperature()
MACRUM 0:ade9be832910 142 {
MACRUM 0:ade9be832910 143 uint32_t temp_raw;
MACRUM 0:ade9be832910 144 float tempf;
MACRUM 0:ade9be832910 145 char cmd[4];
MACRUM 6:b91c721722d2 146
MACRUM 6:b91c721722d2 147 _cs = 0;
MACRUM 0:ade9be832910 148 cmd[0] = 0xfa; // temp_msb
MACRUM 6:b91c721722d2 149 //i2c.write(address, cmd, 1);
MACRUM 6:b91c721722d2 150 _spi.write(0xfa);
MACRUM 6:b91c721722d2 151 //i2c.read(address, &cmd[1], 3);
MACRUM 6:b91c721722d2 152 for(int i = 0; i < 3; i++)
MACRUM 6:b91c721722d2 153 cmd[1+i] = _spi.write(0);
MACRUM 6:b91c721722d2 154 _cs = 1;
MACRUM 6:b91c721722d2 155
MACRUM 0:ade9be832910 156 temp_raw = (cmd[1] << 12) | (cmd[2] << 4) | (cmd[3] >> 4);
MACRUM 6:b91c721722d2 157
MACRUM 0:ade9be832910 158 int32_t temp;
MACRUM 6:b91c721722d2 159
MACRUM 0:ade9be832910 160 temp =
MACRUM 0:ade9be832910 161 (((((temp_raw >> 3) - (dig_T1 << 1))) * dig_T2) >> 11) +
MACRUM 0:ade9be832910 162 ((((((temp_raw >> 4) - dig_T1) * ((temp_raw >> 4) - dig_T1)) >> 12) * dig_T3) >> 14);
MACRUM 6:b91c721722d2 163
MACRUM 0:ade9be832910 164 t_fine = temp;
MACRUM 0:ade9be832910 165 temp = (temp * 5 + 128) >> 8;
MACRUM 0:ade9be832910 166 tempf = (float)temp;
MACRUM 6:b91c721722d2 167
MACRUM 0:ade9be832910 168 return (tempf/100.0f);
MACRUM 0:ade9be832910 169 }
MACRUM 6:b91c721722d2 170
MACRUM 6:b91c721722d2 171 float BME280_SPI::getPressure()
MACRUM 0:ade9be832910 172 {
MACRUM 0:ade9be832910 173 uint32_t press_raw;
MACRUM 0:ade9be832910 174 float pressf;
MACRUM 0:ade9be832910 175 char cmd[4];
MACRUM 6:b91c721722d2 176
MACRUM 6:b91c721722d2 177 _cs = 0;
MACRUM 0:ade9be832910 178 cmd[0] = 0xf7; // press_msb
MACRUM 6:b91c721722d2 179 //i2c.write(address, cmd, 1);
MACRUM 6:b91c721722d2 180 _spi.write(0xf7); // press_msb
MACRUM 6:b91c721722d2 181 //i2c.read(address, &cmd[1], 3);
MACRUM 6:b91c721722d2 182 for(int i = 0; i < 3; i++)
MACRUM 6:b91c721722d2 183 cmd[1+i] = _spi.write(0);
MACRUM 6:b91c721722d2 184 _cs = 1;
MACRUM 6:b91c721722d2 185
MACRUM 0:ade9be832910 186 press_raw = (cmd[1] << 12) | (cmd[2] << 4) | (cmd[3] >> 4);
MACRUM 6:b91c721722d2 187
MACRUM 0:ade9be832910 188 int32_t var1, var2;
MACRUM 0:ade9be832910 189 uint32_t press;
MACRUM 6:b91c721722d2 190
MACRUM 0:ade9be832910 191 var1 = (t_fine >> 1) - 64000;
MACRUM 0:ade9be832910 192 var2 = (((var1 >> 2) * (var1 >> 2)) >> 11) * dig_P6;
MACRUM 0:ade9be832910 193 var2 = var2 + ((var1 * dig_P5) << 1);
MACRUM 0:ade9be832910 194 var2 = (var2 >> 2) + (dig_P4 << 16);
MACRUM 0:ade9be832910 195 var1 = (((dig_P3 * (((var1 >> 2)*(var1 >> 2)) >> 13)) >> 3) + ((dig_P2 * var1) >> 1)) >> 18;
MACRUM 0:ade9be832910 196 var1 = ((32768 + var1) * dig_P1) >> 15;
MACRUM 0:ade9be832910 197 if (var1 == 0) {
MACRUM 0:ade9be832910 198 return 0;
MACRUM 0:ade9be832910 199 }
MACRUM 0:ade9be832910 200 press = (((1048576 - press_raw) - (var2 >> 12))) * 3125;
MACRUM 0:ade9be832910 201 if(press < 0x80000000) {
MACRUM 0:ade9be832910 202 press = (press << 1) / var1;
MACRUM 0:ade9be832910 203 } else {
MACRUM 0:ade9be832910 204 press = (press / var1) * 2;
MACRUM 0:ade9be832910 205 }
MACRUM 0:ade9be832910 206 var1 = ((int32_t)dig_P9 * ((int32_t)(((press >> 3) * (press >> 3)) >> 13))) >> 12;
MACRUM 0:ade9be832910 207 var2 = (((int32_t)(press >> 2)) * (int32_t)dig_P8) >> 13;
MACRUM 0:ade9be832910 208 press = (press + ((var1 + var2 + dig_P7) >> 4));
MACRUM 6:b91c721722d2 209
MACRUM 0:ade9be832910 210 pressf = (float)press;
MACRUM 0:ade9be832910 211 return (pressf/100.0f);
MACRUM 0:ade9be832910 212 }
MACRUM 6:b91c721722d2 213
MACRUM 6:b91c721722d2 214 float BME280_SPI::getHumidity()
MACRUM 0:ade9be832910 215 {
MACRUM 0:ade9be832910 216 uint32_t hum_raw;
MACRUM 0:ade9be832910 217 float humf;
MACRUM 0:ade9be832910 218 char cmd[4];
MACRUM 6:b91c721722d2 219
MACRUM 6:b91c721722d2 220 _cs = 0;
MACRUM 0:ade9be832910 221 cmd[0] = 0xfd; // hum_msb
MACRUM 6:b91c721722d2 222 //i2c.write(address, cmd, 1);
MACRUM 6:b91c721722d2 223 _spi.write(0xfd); // hum_msb
MACRUM 6:b91c721722d2 224 //i2c.read(address, &cmd[1], 2);
MACRUM 6:b91c721722d2 225 for(int i = 0; i < 2; i++)
MACRUM 6:b91c721722d2 226 cmd[1+i] = _spi.write(0);
MACRUM 6:b91c721722d2 227 _cs = 1;
MACRUM 6:b91c721722d2 228
MACRUM 0:ade9be832910 229 hum_raw = (cmd[1] << 8) | cmd[2];
MACRUM 6:b91c721722d2 230
MACRUM 0:ade9be832910 231 int32_t v_x1;
MACRUM 6:b91c721722d2 232
MACRUM 0:ade9be832910 233 v_x1 = t_fine - 76800;
MACRUM 0:ade9be832910 234 v_x1 = (((((hum_raw << 14) -(((int32_t)dig_H4) << 20) - (((int32_t)dig_H5) * v_x1)) +
MACRUM 0:ade9be832910 235 ((int32_t)16384)) >> 15) * (((((((v_x1 * (int32_t)dig_H6) >> 10) *
MACRUM 0:ade9be832910 236 (((v_x1 * ((int32_t)dig_H3)) >> 11) + 32768)) >> 10) + 2097152) *
MACRUM 0:ade9be832910 237 (int32_t)dig_H2 + 8192) >> 14));
MACRUM 0:ade9be832910 238 v_x1 = (v_x1 - (((((v_x1 >> 15) * (v_x1 >> 15)) >> 7) * (int32_t)dig_H1) >> 4));
MACRUM 0:ade9be832910 239 v_x1 = (v_x1 < 0 ? 0 : v_x1);
MACRUM 0:ade9be832910 240 v_x1 = (v_x1 > 419430400 ? 419430400 : v_x1);
MACRUM 6:b91c721722d2 241
MACRUM 0:ade9be832910 242 humf = (float)(v_x1 >> 12);
MACRUM 6:b91c721722d2 243
MACRUM 0:ade9be832910 244 return (humf/1024.0f);
MACRUM 0:ade9be832910 245 }