BME280 Combined humidity and pressure sensor library with SPI interface

Dependents:   BME280_SPI_Hello TYBLE16_simple_data_logger mpl115a2_display_local GS_final

Fork of BME280 by Toyomasa Watarai

Revision:
7:dfd6107f1f92
Parent:
6:b91c721722d2
Child:
8:f0faf51d3d4a
--- a/BME280_SPI.cpp	Sun Mar 12 03:02:05 2017 +0000
+++ b/BME280_SPI.cpp	Mon Mar 13 07:23:21 2017 +0000
@@ -8,23 +8,17 @@
  ******************************************************************************
  * @attention
  *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
  *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
  */
 
 #include "mbed.h"
@@ -50,36 +44,32 @@
 
     _cs = 1;
     _spi.format(8, 0); // 8-bit, mode=0
-    _spi.frequency(10000000); // 10MHZ
+    _spi.frequency(1000000); // 1MHZ
 
     _cs = 0;
-    cmd[0] = 0xf2; // ctrl_hum
-    cmd[1] = 0x01; // Humidity oversampling x1
-    _spi.write(0xf2); // ctrl_hum
+    _spi.write(0xd0); // chip_id
+    cmd[0] = _spi.write(0); // read chip_id
+    _cs = 1;
+    
+    DEBUG_PRINT("chip_id = 0x%x\n", cmd[0]);
+
+    _cs = 0;
+    _spi.write(0xf2 & BME280_SPI_MASK); // ctrl_hum
     _spi.write(0x01); // Humidity oversampling x1
     _cs = 1;
 
     _cs = 0;
-    cmd[0] = 0xf4; // ctrl_meas
-    cmd[1] = 0x27; // Temparature oversampling x1, Pressure oversampling x1, Normal mode
-    //i2c.write(address, cmd, 2);
-    _spi.write(0xf4); // ctrl_meas
+    _spi.write(0xf4 & BME280_SPI_MASK); // ctrl_meas
     _spi.write(0x27); // Temparature oversampling x1, Pressure oversampling x1, Normal mode
     _cs = 1;
 
     _cs = 0;
-    cmd[0] = 0xf5; // config
-    cmd[1] = 0xa0; // Standby 1000ms, Filter off
-    //i2c.write(address, cmd, 2);
-    _spi.write(0xf5); // config
-    _spi.write(0xa0); // Standby 1000ms, Filter off
+    _spi.write(0xf5 & BME280_SPI_MASK); // config
+    _spi.write(0xa0); // Standby 1000ms, Filter off, 4-wire SPI interface
     _cs = 1;
 
     _cs = 0;
-    cmd[0] = 0x88; // read dig_T regs
-    //i2c.write(address, cmd, 1);
     _spi.write(0x88); // read dig_T regs
-    //i2c.read(address, cmd, 6);
     for(int i = 0; i < 6; i++)
         cmd[i] = _spi.write(0);
     _cs = 1;
@@ -89,12 +79,10 @@
     dig_T3 = (cmd[5] << 8) | cmd[4];
 
     DEBUG_PRINT("dig_T = 0x%x, 0x%x, 0x%x\n", dig_T1, dig_T2, dig_T3);
+    DEBUG_PRINT("dig_T = %d, %d, %d\n", dig_T1, dig_T2, dig_T3);
 
     _cs = 0;
-    cmd[0] = 0x8E; // read dig_P regs
-    //i2c.write(address, cmd, 1);
     _spi.write(0x8e);
-    //i2c.read(address, cmd, 18);
     for(int i = 0; i < 18; i++)
         cmd[i] = _spi.write(0);
     _cs = 1;
@@ -110,20 +98,15 @@
     dig_P9 = (cmd[17] << 8) | cmd[16];
 
     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);
+    DEBUG_PRINT("dig_P = %d, %d, %d, %d, %d, %d, %d, %d, %d\n", dig_P1, dig_P2, dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9);
 
     _cs = 0;
-    cmd[0] = 0xA1; // read dig_H regs
-    //i2c.write(address, cmd, 1);
     _spi.write(0xA1); // read dig_H regs
-    //i2c.read(address, cmd, 1);
     cmd[0] = _spi.write(0);
     _cs = 1;
 
     _cs = 0;
-    cmd[1] = 0xE1; // read dig_H regs
-    //i2c.write(address, &cmd[1], 1);
     _spi.write(0xE1); // read dig_H regs
-    //i2c.read(address, &cmd[1], 7);
     for(int i = 0; i < 7; i++)
         cmd[1+i] = _spi.write(0);
     _cs = 1;
@@ -136,6 +119,7 @@
     dig_H6 = cmd[7];
 
     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);
+    DEBUG_PRINT("dig_H = %d, %d, %d, %d, %d, %d\n", dig_H1, dig_H2, dig_H3, dig_H4, dig_H5, dig_H6);
 }
 
 float BME280_SPI::getTemperature()
@@ -145,10 +129,7 @@
     char cmd[4];
 
     _cs = 0;
-    cmd[0] = 0xfa; // temp_msb
-    //i2c.write(address, cmd, 1);
     _spi.write(0xfa);
-    //i2c.read(address, &cmd[1], 3);
     for(int i = 0; i < 3; i++)
         cmd[1+i] = _spi.write(0);
     _cs = 1;
@@ -175,10 +156,7 @@
     char cmd[4];
 
     _cs = 0;
-    cmd[0] = 0xf7; // press_msb
-    //i2c.write(address, cmd, 1);
     _spi.write(0xf7); // press_msb
-    //i2c.read(address, &cmd[1], 3);
     for(int i = 0; i < 3; i++)
         cmd[1+i] = _spi.write(0);
     _cs = 1;
@@ -218,10 +196,7 @@
     char cmd[4];
 
     _cs = 0;
-    cmd[0] = 0xfd; // hum_msb
-    //i2c.write(address, cmd, 1);
     _spi.write(0xfd); // hum_msb
-    //i2c.read(address, &cmd[1], 2);
     for(int i = 0; i < 2; i++)
         cmd[1+i] = _spi.write(0);
     _cs = 1;