2 * Copyright (c) 2020 Bosch Sensortec GmbH. All rights reserved.
\r
6 * Redistribution and use in source and binary forms, with or without
\r
7 * modification, are permitted provided that the following conditions are met:
\r
9 * 1. Redistributions of source code must retain the above copyright
\r
10 * notice, this list of conditions and the following disclaimer.
\r
12 * 2. Redistributions in binary form must reproduce the above copyright
\r
13 * notice, this list of conditions and the following disclaimer in the
\r
14 * documentation and/or other materials provided with the distribution.
\r
16 * 3. Neither the name of the copyright holder nor the names of its
\r
17 * contributors may be used to endorse or promote products derived from
\r
18 * this software without specific prior written permission.
\r
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
\r
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
\r
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
\r
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
\r
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
\r
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
\r
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
\r
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
\r
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
\r
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
\r
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
\r
31 * POSSIBILITY OF SUCH DAMAGE.
\r
33 * @file bme280_selftest.c
\r
39 #include "bme280_selftest.h"
\r
41 #define BME280_CRC_DATA_ADDR UINT8_C(0xE8)
\r
42 #define BME280_CRC_DATA_LEN UINT8_C(1)
\r
43 #define BME280_CRC_CALIB1_ADDR UINT8_C(0x88)
\r
44 #define BME280_CRC_CALIB1_LEN UINT8_C(26)
\r
45 #define BME280_CRC_CALIB2_ADDR UINT8_C(0xE1)
\r
46 #define BME280_CRC_CALIB2_LEN UINT8_C(7)
\r
49 * @brief This API calculates the CRC
\r
51 * @param[in] mem_values : reg_data parameter to calculate CRC
\r
52 * @param[in] mem_length : Parameter to calculate CRC
\r
54 * @return Result of API execution status
\r
55 * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
\r
57 static uint8_t crc_calculate(uint8_t *mem_values, uint8_t mem_length);
\r
60 * @brief This API reads the stored CRC and then compare with calculated CRC
\r
62 * @param[in] dev : Structure instance of bme280_dev.
\r
64 * @return Result of API execution status
\r
65 * @retval zero -> self test success / +ve value -> warning(self test fail)
\r
67 int8_t bme280_selftest_crc(const struct bme280_dev *dev)
\r
71 uint8_t reg_data[64];
\r
72 uint8_t stored_crc = 0;
\r
73 uint8_t calculated_crc = 0;
\r
75 /* Read stored crc value from register */
\r
76 reg_addr = BME280_CRC_DATA_ADDR;
\r
77 rslt = bme280_get_regs(reg_addr, reg_data, BME280_CRC_DATA_LEN, dev);
\r
78 if (rslt == BME280_OK)
\r
80 stored_crc = reg_data[0];
\r
82 /* Calculated CRC value with calibration register */
\r
83 reg_addr = BME280_CRC_CALIB1_ADDR;
\r
84 rslt = bme280_get_regs(reg_addr, ®_data[0], BME280_CRC_CALIB1_LEN, dev);
\r
85 if (rslt == BME280_OK)
\r
87 reg_addr = BME280_CRC_CALIB2_ADDR;
\r
88 rslt = bme280_get_regs(reg_addr, ®_data[BME280_CRC_CALIB1_LEN], BME280_CRC_CALIB2_LEN, dev);
\r
89 if (rslt == BME280_OK)
\r
91 calculated_crc = crc_calculate(reg_data, BME280_CRC_CALIB1_LEN + BME280_CRC_CALIB2_LEN);
\r
94 if (stored_crc == calculated_crc)
\r
100 rslt = BME280_W_SELF_TEST_FAIL;
\r
110 * @brief This API calculates the CRC
\r
112 * @param[in] mem_values : reg_data parameter to calculate CRC
\r
113 * @param[in] mem_length : Parameter to calculate CRC
\r
115 * @return Result of API execution status
\r
116 * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
\r
118 static uint8_t crc_calculate(uint8_t *mem_values, uint8_t mem_length)
\r
120 uint32_t crc_reg = 0xFF;
\r
121 uint8_t polynomial = 0x1D;
\r
122 uint8_t bitno, index;
\r
125 for (index = 0; index < mem_length; index++)
\r
127 for (bitno = 0; bitno < 8; bitno++)
\r
129 if ((crc_reg & 0x80) != (mem_values[index] & 0x80))
\r
138 /* Truncate 8th bit for crc_reg and mem_values */
\r
139 crc_reg = (uint32_t)((crc_reg & 0x7F) << 1);
\r
140 mem_values[index] = (uint8_t)((mem_values[index] & 0x7F) << 1);
\r
141 crc_reg = (uint32_t)(crc_reg ^ (polynomial * din));
\r
145 return (uint8_t)(crc_reg ^ 0xFF);
\r