briantdavis / EAS_BBB_DataAcquisition

EAS data acquisition using BeagleBone Black
GNU General Public License v2.0
6 stars 10 forks source link

BME280 calibration Coefficents are not being generated properly #26

Open marsfan opened 5 years ago

marsfan commented 5 years ago

The following for loop is supposed to output the calibration coefficients raw dump, which would be 66 bits. However, it is actually dumping out 72 characters for unknown reasons. We need to fix this so that the calibration data can be applied to the BME280 sensor.

https://github.com/briantdavis/EAS_BBB_DataAcquisition/blob/a15f0708c49f5480c779cbd16f24938798e29457/source/bme280.cpp#L257-L261

marsfan commented 5 years ago

I might be wrong here, but I may have found the problem. I wrote some new code that just has the for loop and needed variables.

#include <errno.h>
#include <unistd.h>

#include <fcntl.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <array>

#include <iostream>
#include <cstring>
#include <fstream>
#include <iomanip>  // setw() setfill()

#include "bme280.h"

typedef struct _compParams_ts {
  uint16_t dig_T1;
  int16_t  dig_T2;
  int16_t  dig_T3;

  uint16_t dig_P1;
  int16_t  dig_P2;
  int16_t  dig_P3;
  int16_t  dig_P4;
  int16_t  dig_P5;
  int16_t  dig_P6;
  int16_t  dig_P7;
  int16_t  dig_P8;
  int16_t  dig_P9;

  uint8_t  dig_H1;
  int16_t  dig_H2;
  uint8_t  dig_H3;
  int16_t  dig_H4;
  int16_t  dig_H5;
  uint8_t  dig_H6;

} compParams_ts;

union compParams_u
{
   uint8_t        compArray[24+9];
   compParams_ts  compStruct;
};

compParams_u cal_data;

int main(){
  std::cout << "Sensor ID [" << std::hex << (int)5 << "] Calibration Data ||" ;
  {
    cal_data.compStruct.dig_T1 = (uint16_t)65535;
    cal_data.compStruct.dig_T2 = (int16_t)65535;
    cal_data.compStruct.dig_T3 = (int16_t)65535;

    cal_data.compStruct.dig_P1 = (uint16_t)65535;
    cal_data.compStruct.dig_P2 = (int16_t)65535;
    cal_data.compStruct.dig_P3 = (int16_t)65535;
    cal_data.compStruct.dig_P4 = (int16_t)65535;
    cal_data.compStruct.dig_P5 = (int16_t)65535;
    cal_data.compStruct.dig_P6 = (int16_t)65535;
    cal_data.compStruct.dig_P7 = (int16_t)65535;
    cal_data.compStruct.dig_P8 = (int16_t)65535;
    cal_data.compStruct.dig_P9 = (int16_t)65535;
    cal_data.compStruct.dig_H1 = (uint8_t)255;
    cal_data.compStruct.dig_H2 = (int16_t)65535;
    cal_data.compStruct.dig_H3 = (uint8_t)255;
    cal_data.compStruct.dig_H4 = (int16_t)65535;
    cal_data.compStruct.dig_H5 = (int16_t)65535;
    cal_data.compStruct.dig_H6 = (uint8_t)255;

    // uint8_t *ptr = (void *) cal_data;
    //FIXME: Outputting the wrong number of characters
    for (unsigned int i = 0 ; i < sizeof(cal_data) ; i++)
    {
        std::cout << std::hex << std::setfill('0') << std::setw(2) << (int)cal_data.compArray[i] << ' ';
    }
  }

  std::cout << "||" << std::endl ;

  //test.logPartBSensorCal();
}

When I run this, the output I get is ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff 00 ff ff ff 00 ff ff ff ff ff 00

Note the three clusters of zeros for a total of 6 zeros. This brings the number of characters from the expected 66 to the actual 72. I believe that casting each element of the array to int is the problem.

Note that three of the calibration coefficients are of type uint8_t. Casting these to a int converts them to a uint16_t, adding the two leading zeros. We need to figure out how to dynamically adjust this to output the proper size. I have an idea based on an if statement that I can try. I will report in then

marsfan commented 5 years ago

Posting an update. I cannot figure out how to dynamically adjust output to proper size, but as the three 2char variables are all unsigned, I do not believe that it will matter for now, and will write my code accordingly to adapt to this.