]> git.itanic.dy.fi Git - BME280_driver/blob - selftest/bme280_selftest.c
9afbc5593b3005f3be044de233763ec1a4278c63
[BME280_driver] / selftest / bme280_selftest.c
1 /**\r
2 * Copyright (c) 2020 Bosch Sensortec GmbH. All rights reserved.\r
3 *\r
4 * BSD-3-Clause\r
5 *\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
8 *\r
9 * 1. Redistributions of source code must retain the above copyright\r
10 *    notice, this list of conditions and the following disclaimer.\r
11 *\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
15 *\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
19 *\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
32 *\r
33 * @file bme280_selftest.c\r
34 * @date 21/01/2020\r
35 * @version  3.4.2\r
36 *\r
37 */\r
38 \r
39 #include "bme280_selftest.h"\r
40 \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
47 \r
48 /*!\r
49  * @brief This API calculates the CRC\r
50  *\r
51  * @param[in] mem_values : reg_data parameter to calculate CRC\r
52  * @param[in] mem_length : Parameter to calculate CRC\r
53  *\r
54  * @return Result of API execution status\r
55  * @retval zero -> Success / +ve value -> Warning / -ve value -> Error\r
56  */\r
57 static uint8_t crc_calculate(uint8_t *mem_values, uint8_t mem_length);\r
58 \r
59 /*!\r
60  * @brief This API reads the stored CRC and then compare with calculated CRC\r
61  *\r
62  * @param[in] dev : Structure instance of bme280_dev.\r
63  *\r
64  * @return Result of API execution status\r
65  * @retval zero -> self test success / +ve value -> warning(self test fail)\r
66  */\r
67 int8_t bme280_selftest_crc(const struct bme280_dev *dev)\r
68 {\r
69     int8_t rslt;\r
70     uint8_t reg_addr;\r
71     uint8_t reg_data[64];\r
72     uint8_t stored_crc = 0;\r
73     uint8_t calculated_crc = 0;\r
74 \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
79     {\r
80         stored_crc = reg_data[0];\r
81 \r
82         /* Calculated CRC value with calibration register */\r
83         reg_addr = BME280_CRC_CALIB1_ADDR;\r
84         rslt = bme280_get_regs(reg_addr, &reg_data[0], BME280_CRC_CALIB1_LEN, dev);\r
85         if (rslt == BME280_OK)\r
86         {\r
87             reg_addr = BME280_CRC_CALIB2_ADDR;\r
88             rslt = bme280_get_regs(reg_addr, &reg_data[BME280_CRC_CALIB1_LEN], BME280_CRC_CALIB2_LEN, dev);\r
89             if (rslt == BME280_OK)\r
90             {\r
91                 calculated_crc = crc_calculate(reg_data, BME280_CRC_CALIB1_LEN + BME280_CRC_CALIB2_LEN);\r
92 \r
93                 /* Validate CRC */\r
94                 if (stored_crc == calculated_crc)\r
95                 {\r
96                     rslt = BME280_OK;\r
97                 }\r
98                 else\r
99                 {\r
100                     rslt = BME280_W_SELF_TEST_FAIL;\r
101                 }\r
102             }\r
103         }\r
104     }\r
105 \r
106     return rslt;\r
107 }\r
108 \r
109 /*!\r
110  * @brief This API calculates the CRC\r
111  *\r
112  * @param[in] mem_values : reg_data parameter to calculate CRC\r
113  * @param[in] mem_length : Parameter to calculate CRC\r
114  *\r
115  * @return Result of API execution status\r
116  * @retval zero -> Success / +ve value -> Warning / -ve value -> Error\r
117  */\r
118 static uint8_t crc_calculate(uint8_t *mem_values, uint8_t mem_length)\r
119 {\r
120     uint32_t crc_reg = 0xFF;\r
121     uint8_t polynomial = 0x1D;\r
122     uint8_t bitno, index;\r
123     uint8_t din = 0;\r
124 \r
125     for (index = 0; index < mem_length; index++)\r
126     {\r
127         for (bitno = 0; bitno < 8; bitno++)\r
128         {\r
129             if ((crc_reg & 0x80) != (mem_values[index] & 0x80))\r
130             {\r
131                 din = 1;\r
132             }\r
133             else\r
134             {\r
135                 din = 0;\r
136             }\r
137 \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
142         }\r
143     }\r
144 \r
145     return (uint8_t)(crc_reg ^ 0xFF);\r
146 }\r