public Bme280 Initialize() { Logger.Log("BME280::Initialize"); try { var settings = new I2cConnectionSettings(Bme280Address); settings.BusSpeed = I2cBusSpeed.StandardMode; settings.SharingMode = I2cSharingMode.Shared; string aqs = I2cDevice.GetDeviceSelector(_i2CControllerName); _bme280 = I2cDevice.FromId(_i2CControllerName, settings); if (_bme280 == null) { Logger.Log("BME280 device not found", Logger.LogLevel.Warning); } } catch (Exception e) { Logger.Log("BME 280 exception: " + e.Message + "\n" + e.StackTrace, Logger.LogLevel.Error); throw; } byte[] readChipId = new byte[] { (byte)ERegisters.BME280_REGISTER_CHIPID }; byte[] readBuffer = new byte[] { 0xFF }; //Read the device signature _bme280.WriteReadPartial(readChipId, readBuffer); Logger.Log("BME280 Signature: " + readBuffer[0], Logger.LogLevel.Info); //Verify the device signature if (readBuffer[0] != Bme280Signature) { Logger.Log("BME280::Begin Signature Mismatch.", Logger.LogLevel.Warning); return(this); } //Set configuration registers WriteConfigRegister(); WriteControlMeasurementRegister(); WriteControlRegisterHumidity(); //Set configuration registers again to ensure configuration of humidity WriteConfigRegister(); WriteControlMeasurementRegister(); WriteControlRegisterHumidity(); //Read the coefficients table _calibrationData = ReadCoefficeints(); //Dummy read temp to setup t_fine ReadTemperature(); return(this); }
//Method to read the caliberation data from the registers private Bme280CalibrationData ReadCoefficeints() { // 16 bit calibration data is stored as Little Endian, the helper method will do the byte swap. _calibrationData = new Bme280CalibrationData(); // Read temperature calibration data _calibrationData.DigT1 = (ushort)_bme280.ReadValue((byte)ERegisters.BME280_REGISTER_DIG_T1, 2); _calibrationData.DigT2 = (short)_bme280.ReadValue((byte)ERegisters.BME280_REGISTER_DIG_T2, 2); _calibrationData.DigT3 = (short)_bme280.ReadValue((byte)ERegisters.BME280_REGISTER_DIG_T3, 2); // Read presure calibration data _calibrationData.DigP9 = (short)_bme280.ReadValue((byte)ERegisters.BME280_REGISTER_DIG_P9, 2); _calibrationData.DigP8 = (short)_bme280.ReadValue((byte)ERegisters.BME280_REGISTER_DIG_P8, 2); _calibrationData.DigP7 = (short)_bme280.ReadValue((byte)ERegisters.BME280_REGISTER_DIG_P7, 2); _calibrationData.DigP6 = (short)_bme280.ReadValue((byte)ERegisters.BME280_REGISTER_DIG_P6, 2); _calibrationData.DigP5 = (short)_bme280.ReadValue((byte)ERegisters.BME280_REGISTER_DIG_P5, 2); _calibrationData.DigP4 = (short)_bme280.ReadValue((byte)ERegisters.BME280_REGISTER_DIG_P4, 2); _calibrationData.DigP3 = (short)_bme280.ReadValue((byte)ERegisters.BME280_REGISTER_DIG_P3, 2); _calibrationData.DigP2 = (short)_bme280.ReadValue((byte)ERegisters.BME280_REGISTER_DIG_P2, 2); _calibrationData.DigP1 = (ushort)_bme280.ReadValue((byte)ERegisters.BME280_REGISTER_DIG_P1, 2); // Read humidity calibration data _calibrationData.DigH1 = (byte)_bme280.ReadValue((byte)ERegisters.BME280_REGISTER_DIG_H1); _calibrationData.DigH2 = (short)_bme280.ReadValue((byte)ERegisters.BME280_REGISTER_DIG_H2, 2); _calibrationData.DigH3 = (byte)_bme280.ReadValue((byte)ERegisters.BME280_REGISTER_DIG_H3); short e4 = (short)_bme280.ReadValue((byte)ERegisters.BME280_REGISTER_DIG_H4_L); // Read 0xE4 short e5 = (short)_bme280.ReadValue((byte)ERegisters.BME280_REGISTER_DIG_H4_H); // Read 0xE5 short e6 = (short)_bme280.ReadValue((byte)ERegisters.BME280_REGISTER_DIG_H5_H); // Read 0xE6 _calibrationData.DigH6 = (sbyte)_bme280.ReadValue((byte)ERegisters.BME280_REGISTER_DIG_H6); _calibrationData.DigH4 = (short)((e4 << 4) + (e5 & 0x0F)); _calibrationData.DigH5 = (short)((e5 >> 4) + (e6 << 4)); return(_calibrationData); }