|
| 1 | +/** |
| 2 | + * BSD 3-Clause License |
| 3 | + * |
| 4 | + * Copyright (c) 2019, STMicroelectronics |
| 5 | + * Copyright (c) 2024, Ferdinand Keil <ferdinand.keil@ies.tu-darmstadt.de> |
| 6 | + * All rights reserved. |
| 7 | + * |
| 8 | + * Redistribution and use in source and binary forms, with or without |
| 9 | + * modification, are permitted provided that the following conditions are met: |
| 10 | + * |
| 11 | + * * Redistributions of source code must retain the above copyright notice, this |
| 12 | + * list of conditions and the following disclaimer. |
| 13 | + * |
| 14 | + * * Redistributions in binary form must reproduce the above copyright notice, |
| 15 | + * this list of conditions and the following disclaimer in the documentation |
| 16 | + * and/or other materials provided with the distribution. |
| 17 | + * |
| 18 | + * * Neither the name of the copyright holder nor the names of its |
| 19 | + * contributors may be used to endorse or promote products derived from |
| 20 | + * this software without specific prior written permission. |
| 21 | + * |
| 22 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 23 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 24 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 25 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 26 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 27 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 28 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 29 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 30 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | + */ |
| 33 | + |
| 34 | +#include <stdio.h> |
| 35 | +#include <freertos/FreeRTOS.h> |
| 36 | +#include <freertos/task.h> |
| 37 | +#include <lis2hh12.h> |
| 38 | +#include <string.h> |
| 39 | +#include "esp_log.h" |
| 40 | + |
| 41 | +#define TAG "example-lis2hh12" |
| 42 | + |
| 43 | +#ifndef CONFIG_EXAMPLE_LIS2HH12_SA0_PIN |
| 44 | +#define CONFIG_EXAMPLE_LIS2HH12_SA0_PIN_LEVEL false |
| 45 | +#else |
| 46 | +#define CONFIG_EXAMPLE_LIS2HH12_SA0_PIN_LEVEL true |
| 47 | +#endif |
| 48 | + |
| 49 | +#define BOOT_TIME 20 // ms |
| 50 | +#define WAIT_TIME 100 // ms |
| 51 | + |
| 52 | +/* Self test limits. */ |
| 53 | +#define MIN_ST_LIMIT_mg 70.0f |
| 54 | +#define MAX_ST_LIMIT_mg 1500.0f |
| 55 | + |
| 56 | +/* Self test results. */ |
| 57 | +#define ST_PASS 1U |
| 58 | +#define ST_FAIL 0U |
| 59 | + |
| 60 | +void lis2hh12_test(void *pvParameters) |
| 61 | +{ |
| 62 | + uint8_t whoamI; |
| 63 | + uint8_t rst; |
| 64 | + uint8_t drdy; |
| 65 | + uint8_t st_result; |
| 66 | + uint8_t i; |
| 67 | + uint8_t j; |
| 68 | + int16_t data_raw[3]; |
| 69 | + float val_st_off[3]; |
| 70 | + float val_st_on[3]; |
| 71 | + float test_val[3]; |
| 72 | + float acceleration_mg[3]; |
| 73 | + |
| 74 | + i2c_dev_t dev; |
| 75 | + memset(&dev, 0, sizeof(i2c_dev_t)); |
| 76 | + |
| 77 | + ESP_ERROR_CHECK(lis2hh12_init_desc(&dev, 0, CONFIG_EXAMPLE_I2C_MASTER_SDA, CONFIG_EXAMPLE_I2C_MASTER_SCL, CONFIG_EXAMPLE_LIS2HH12_SA0_PIN_LEVEL)); |
| 78 | + |
| 79 | + /* Wait sensor boot time */ |
| 80 | + vTaskDelay(pdMS_TO_TICKS(BOOT_TIME)); |
| 81 | + |
| 82 | + ESP_ERROR_CHECK(lis2hh12_dev_id_get(&dev, &whoamI)); |
| 83 | + |
| 84 | + if (whoamI != LIS2HH12_ID) |
| 85 | + { |
| 86 | + ESP_LOGE(TAG, "incorrect device ID (got 0x%02x, expected 0x%02x)", whoamI, LIS2HH12_ID); |
| 87 | + while (1) |
| 88 | + ; |
| 89 | + } |
| 90 | + ESP_LOGI(TAG, "found LIS2HH12 device"); |
| 91 | + |
| 92 | + /* Restore default configuration */ |
| 93 | + ESP_ERROR_CHECK(lis2hh12_dev_reset_set(&dev, PROPERTY_ENABLE)); |
| 94 | + |
| 95 | + do |
| 96 | + { |
| 97 | + ESP_ERROR_CHECK(lis2hh12_dev_reset_get(&dev, &rst)); |
| 98 | + } |
| 99 | + while (rst); |
| 100 | + |
| 101 | + /* Enable Block Data Update */ |
| 102 | + ESP_ERROR_CHECK(lis2hh12_block_data_update_set(&dev, PROPERTY_ENABLE)); |
| 103 | + |
| 104 | + /* |
| 105 | + * Accelerometer Self Test |
| 106 | + */ |
| 107 | + /* Set Output Data Rate */ |
| 108 | + ESP_ERROR_CHECK(lis2hh12_xl_data_rate_set(&dev, LIS2HH12_XL_ODR_50Hz)); |
| 109 | + /* Set full scale */ |
| 110 | + ESP_ERROR_CHECK(lis2hh12_xl_full_scale_set(&dev, LIS2HH12_4g)); |
| 111 | + /* Wait stable output */ |
| 112 | + vTaskDelay(pdMS_TO_TICKS(WAIT_TIME)); |
| 113 | + |
| 114 | + /* Check if new value available */ |
| 115 | + do |
| 116 | + { |
| 117 | + ESP_ERROR_CHECK(lis2hh12_xl_flag_data_ready_get(&dev, &drdy)); |
| 118 | + } |
| 119 | + while (!drdy); |
| 120 | + |
| 121 | + /* Read dummy data and discard it */ |
| 122 | + ESP_ERROR_CHECK(lis2hh12_acceleration_raw_get(&dev, data_raw)); |
| 123 | + |
| 124 | + /* Read 5 sample and get the average vale for each axis */ |
| 125 | + memset(val_st_off, 0x00, 3 * sizeof(float)); |
| 126 | + |
| 127 | + for (i = 0; i < 5; i++) |
| 128 | + { |
| 129 | + /* Check if new value available */ |
| 130 | + do |
| 131 | + { |
| 132 | + ESP_ERROR_CHECK(lis2hh12_xl_flag_data_ready_get(&dev, &drdy)); |
| 133 | + } |
| 134 | + while (!drdy); |
| 135 | + |
| 136 | + /* Read data and accumulate the mg value */ |
| 137 | + ESP_ERROR_CHECK(lis2hh12_acceleration_raw_get(&dev, data_raw)); |
| 138 | + |
| 139 | + for (j = 0; j < 3; j++) |
| 140 | + { |
| 141 | + val_st_off[j] += lis2hh12_from_fs4g_to_mg(data_raw[j]); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + /* Calculate the mg average values */ |
| 146 | + for (i = 0; i < 3; i++) |
| 147 | + { |
| 148 | + val_st_off[i] /= 5.0f; |
| 149 | + } |
| 150 | + |
| 151 | + /* Enable Self Test positive (or negative) */ |
| 152 | + ESP_ERROR_CHECK(lis2hh12_xl_self_test_set(&dev, LIS2HH12_ST_NEGATIVE)); |
| 153 | + /* Wait stable output */ |
| 154 | + vTaskDelay(pdMS_TO_TICKS(WAIT_TIME)); |
| 155 | + |
| 156 | + /* Check if new value available */ |
| 157 | + do |
| 158 | + { |
| 159 | + ESP_ERROR_CHECK(lis2hh12_xl_flag_data_ready_get(&dev, &drdy)); |
| 160 | + } |
| 161 | + while (!drdy); |
| 162 | + |
| 163 | + /* Read dummy data and discard it */ |
| 164 | + ESP_ERROR_CHECK(lis2hh12_acceleration_raw_get(&dev, data_raw)); |
| 165 | + /* Read 5 sample and get the average vale for each axis */ |
| 166 | + memset(val_st_on, 0x00, 3 * sizeof(float)); |
| 167 | + |
| 168 | + for (i = 0; i < 5; i++) |
| 169 | + { |
| 170 | + /* Check if new value available */ |
| 171 | + do |
| 172 | + { |
| 173 | + ESP_ERROR_CHECK(lis2hh12_xl_flag_data_ready_get(&dev, &drdy)); |
| 174 | + } |
| 175 | + while (!drdy); |
| 176 | + |
| 177 | + /* Read data and accumulate the mg value */ |
| 178 | + ESP_ERROR_CHECK(lis2hh12_acceleration_raw_get(&dev, data_raw)); |
| 179 | + |
| 180 | + for (j = 0; j < 3; j++) |
| 181 | + { |
| 182 | + val_st_on[j] += lis2hh12_from_fs4g_to_mg(data_raw[j]); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + /* Calculate the mg average values */ |
| 187 | + for (i = 0; i < 3; i++) |
| 188 | + { |
| 189 | + val_st_on[i] /= 5.0f; |
| 190 | + } |
| 191 | + |
| 192 | + /* Calculate the mg values for self test */ |
| 193 | + for (i = 0; i < 3; i++) |
| 194 | + { |
| 195 | + test_val[i] = fabs((val_st_on[i] - val_st_off[i])); |
| 196 | + } |
| 197 | + |
| 198 | + /* Check self test limit */ |
| 199 | + st_result = ST_PASS; |
| 200 | + |
| 201 | + for (i = 0; i < 3; i++) |
| 202 | + { |
| 203 | + if ((MIN_ST_LIMIT_mg > test_val[i]) || (test_val[i] > MAX_ST_LIMIT_mg)) |
| 204 | + { |
| 205 | + st_result = ST_FAIL; |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + /* Disable Self Test */ |
| 210 | + ESP_ERROR_CHECK(lis2hh12_xl_self_test_set(&dev, LIS2HH12_ST_DISABLE)); |
| 211 | + /* Disable sensor. */ |
| 212 | + ESP_ERROR_CHECK(lis2hh12_xl_data_rate_set(&dev, LIS2HH12_XL_ODR_OFF)); |
| 213 | + |
| 214 | + if (st_result == ST_PASS) |
| 215 | + { |
| 216 | + ESP_LOGI(TAG, "Self Test - PASS"); |
| 217 | + } |
| 218 | + else |
| 219 | + { |
| 220 | + ESP_LOGI(TAG, "Self Test - FAIL"); |
| 221 | + } |
| 222 | + |
| 223 | + /* Enable Block Data Update */ |
| 224 | + ESP_ERROR_CHECK(lis2hh12_block_data_update_set(&dev, PROPERTY_ENABLE)); |
| 225 | + /* Set full scale */ |
| 226 | + ESP_ERROR_CHECK(lis2hh12_xl_full_scale_set(&dev, LIS2HH12_8g)); |
| 227 | + /* Configure filtering chain */ |
| 228 | + /* Accelerometer data output- filter path / bandwidth */ |
| 229 | + ESP_ERROR_CHECK(lis2hh12_xl_filter_aalias_bandwidth_set(&dev, LIS2HH12_AUTO)); |
| 230 | + ESP_ERROR_CHECK(lis2hh12_xl_filter_out_path_set(&dev, LIS2HH12_FILT_LP)); |
| 231 | + ESP_ERROR_CHECK(lis2hh12_xl_filter_low_bandwidth_set(&dev, LIS2HH12_LP_ODR_DIV_400)); |
| 232 | + /* Accelerometer interrupt - filter path / bandwidth */ |
| 233 | + ESP_ERROR_CHECK(lis2hh12_xl_filter_int_path_set(&dev, LIS2HH12_HP_DISABLE)); |
| 234 | + /* Set Output Data Rate */ |
| 235 | + ESP_ERROR_CHECK(lis2hh12_xl_data_rate_set(&dev, LIS2HH12_XL_ODR_100Hz)); |
| 236 | + |
| 237 | + /* Read samples in polling mode (no int) */ |
| 238 | + while (1) |
| 239 | + { |
| 240 | + uint8_t reg; |
| 241 | + /* Read output only if new value is available */ |
| 242 | + ESP_ERROR_CHECK(lis2hh12_xl_flag_data_ready_get(&dev, ®)); |
| 243 | + |
| 244 | + if (reg) |
| 245 | + { |
| 246 | + /* Read acceleration data */ |
| 247 | + memset(data_raw, 0x00, 3 * sizeof(int16_t)); |
| 248 | + ESP_ERROR_CHECK(lis2hh12_acceleration_raw_get(&dev, data_raw)); |
| 249 | + acceleration_mg[0] = lis2hh12_from_fs8g_to_mg(data_raw[0]); |
| 250 | + acceleration_mg[1] = lis2hh12_from_fs8g_to_mg(data_raw[1]); |
| 251 | + acceleration_mg[2] = lis2hh12_from_fs8g_to_mg(data_raw[2]); |
| 252 | + ESP_LOGI(TAG, "Acceleration [mg]:\t%4.2f\t%4.2f\t%4.2f\r\n", acceleration_mg[0], acceleration_mg[1], acceleration_mg[2]); |
| 253 | + } |
| 254 | + |
| 255 | + vTaskDelay(pdMS_TO_TICKS(WAIT_TIME)); |
| 256 | + } |
| 257 | + |
| 258 | + vTaskDelete(NULL); |
| 259 | +} |
| 260 | + |
| 261 | +void app_main() |
| 262 | +{ |
| 263 | + ESP_ERROR_CHECK(i2cdev_init()); |
| 264 | + |
| 265 | + xTaskCreate(lis2hh12_test, "lis2hh12_test", configMINIMAL_STACK_SIZE * 8, NULL, 5, NULL); |
| 266 | +} |
0 commit comments