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