bme280_selftest.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**\mainpage
  2. * Copyright (C) 2016 - 2017 Bosch Sensortec GmbH
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. *
  10. * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * Neither the name of the copyright holder nor the names of the
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  19. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
  23. * OR CONTRIBUTORS BE LIABLE FOR ANY
  24. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  25. * OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  33. *
  34. * The information provided is believed to be accurate and reliable.
  35. * The copyright holder assumes no responsibility
  36. * for the consequences of use
  37. * of such information nor for any infringement of patents or
  38. * other rights of third parties which may result from its use.
  39. * No license is granted by implication or otherwise under any patent or
  40. * patent rights of the copyright holder.
  41. *
  42. * File bme280_selftest.c
  43. * Date 21 Nov 2017
  44. * Version 1.0.0
  45. *
  46. */
  47. #include "bme280_selftest.h"
  48. #define BME280_CRC_DATA_ADDR UINT8_C(0xE8)
  49. #define BME280_CRC_DATA_LEN UINT8_C(1)
  50. #define BME280_CRC_CALIB1_ADDR UINT8_C(0x88)
  51. #define BME280_CRC_CALIB1_LEN UINT8_C(26)
  52. #define BME280_CRC_CALIB2_ADDR UINT8_C(0xE1)
  53. #define BME280_CRC_CALIB2_LEN UINT8_C(7)
  54. /*!
  55. * @brief This API calculates the CRC
  56. *
  57. * @param[in] mem_values : reg_data parameter to calculate CRC
  58. * @param[in] mem_length : Parameter to calculate CRC
  59. *
  60. * @return Result of API execution status
  61. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  62. */
  63. static uint8_t crc_calculate(uint8_t *mem_values, uint8_t mem_length);
  64. /*!
  65. * @brief This API reads the stored CRC and then compare with calculated CRC
  66. *
  67. * @param[in] dev : Structure instance of bme280_dev.
  68. *
  69. * @return Result of API execution status
  70. * @retval zero -> self test success / +ve value -> warning(self test fail)
  71. */
  72. int8_t bme280_crc_selftest(const struct bme280_dev *dev)
  73. {
  74. int8_t rslt;
  75. uint8_t reg_addr;
  76. uint8_t reg_data[64];
  77. uint8_t stored_crc = 0;
  78. uint8_t calculated_crc = 0;
  79. /* Read stored crc value from register */
  80. reg_addr = BME280_CRC_DATA_ADDR;
  81. rslt = bme280_get_regs(reg_addr, reg_data, BME280_CRC_DATA_LEN, dev);
  82. if (rslt == BME280_OK) {
  83. stored_crc = reg_data[0];
  84. /* Calculated CRC value with calibration register */
  85. reg_addr = BME280_CRC_CALIB1_ADDR;
  86. rslt = bme280_get_regs(reg_addr, &reg_data[0], BME280_CRC_CALIB1_LEN, dev);
  87. if (rslt == BME280_OK) {
  88. reg_addr = BME280_CRC_CALIB2_ADDR;
  89. rslt = bme280_get_regs(reg_addr, &reg_data[BME280_CRC_CALIB1_LEN], BME280_CRC_CALIB2_LEN, dev);
  90. if (rslt == BME280_OK) {
  91. calculated_crc = crc_calculate(reg_data, BME280_CRC_CALIB1_LEN + BME280_CRC_CALIB2_LEN);
  92. /* Validate CRC */
  93. if (stored_crc == calculated_crc)
  94. rslt = BME280_OK;
  95. else
  96. rslt = BME280_W_SELF_TEST_FAIL;
  97. }
  98. }
  99. }
  100. return rslt;
  101. }
  102. /*!
  103. * @brief This API calculates the CRC
  104. *
  105. * @param[in] mem_values : reg_data parameter to calculate CRC
  106. * @param[in] mem_length : Parameter to calculate CRC
  107. *
  108. * @return Result of API execution status
  109. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  110. */
  111. static uint8_t crc_calculate(uint8_t *mem_values, uint8_t mem_length)
  112. {
  113. uint32_t crc_reg = 0xFF;
  114. uint8_t polynomial = 0x1D;
  115. uint8_t bitNo, index;
  116. uint8_t din = 0;
  117. for (index = 0; index < mem_length; index++) {
  118. for (bitNo = 0; bitNo < 8; bitNo++) {
  119. if (((crc_reg & 0x80) > 0) ^ ((mem_values[index] & 0x80) > 0))
  120. din = 1;
  121. else
  122. din = 0;
  123. /* Truncate 8th bit for crc_reg and mem_values */
  124. crc_reg = (uint32_t)((crc_reg & 0x7F) << 1);
  125. mem_values[index] = (uint8_t)((mem_values[index] & 0x7F) << 1);
  126. crc_reg = (uint32_t)(crc_reg ^ (polynomial * din));
  127. }
  128. }
  129. return (uint8_t)(crc_reg ^ 0xFF);
  130. }