Skip to content

Commit 96e1d9f

Browse files
committed
added a tested example
1 parent 9e16509 commit 96e1d9f

File tree

9 files changed

+335
-3
lines changed

9 files changed

+335
-3
lines changed

components/lis2hh12/lis2hh12.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
* @retval interface status
5858
*
5959
*/
60-
static esp_err_t lis2hh12_read_reg(i2c_dev_t *dev, uint8_t reg, uint8_t *data, uint16_t len)
60+
esp_err_t lis2hh12_read_reg(i2c_dev_t *dev, uint8_t reg, uint8_t *data, uint16_t len)
6161
{
6262
CHECK_ARG(dev);
6363

components/lis2hh12/lis2hh12.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,9 +593,11 @@ esp_err_t lis2hh12_temperature_raw_get(i2c_dev_t *dev, int16_t *val);
593593

594594
esp_err_t lis2hh12_acceleration_raw_get(i2c_dev_t *dev, int16_t *val);
595595

596-
esp_err_t lis2hh12_init_desc(i2c_dev_t *dev, i2c_port_t port, gpio_num_t sda_gpio, gpio_num_t scl_gpio, bool pin_sa0)
596+
esp_err_t lis2hh12_init_desc(i2c_dev_t *dev, i2c_port_t port, gpio_num_t sda_gpio, gpio_num_t scl_gpio, bool pin_sa0);
597597

598-
esp_err_t lis2hh12_dev_id_get(i2c_dev_t *dev, uint8_t *buff);
598+
esp_err_t lis2hh12_free_desc(i2c_dev_t *dev);
599+
600+
esp_err_t lis2hh12_dev_id_get(i2c_dev_t *dev, uint8_t *buff);
599601

600602
esp_err_t lis2hh12_dev_reset_set(i2c_dev_t *dev, uint8_t val);
601603
esp_err_t lis2hh12_dev_reset_get(i2c_dev_t *dev, uint8_t *val);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# The following four lines of boilerplate have to be in your project's CMakeLists
2+
# in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.5)
4+
5+
set(EXTRA_COMPONENT_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/../../../components)
6+
7+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
8+
project(example-lis2hh12)

examples/lis2hh12/default/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#V := 1
2+
PROJECT_NAME := example-lis2hh12
3+
4+
EXTRA_COMPONENT_DIRS := $(CURDIR)/../../../components
5+
6+
include $(IDF_PATH)/make/project.mk

examples/lis2hh12/default/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Example for `lis2hh12` driver
2+
3+
## What it does
4+
5+
First, runs the device self test procedure.
6+
7+
If successful, starts reading the acceleration data in polling mode.
8+
9+
## Wiring
10+
11+
Connect `SCL` and `SDA` pins to the following pins with appropriate pull-up
12+
resistors.
13+
14+
| Name | Description | Defaults |
15+
|------|-------------|----------|
16+
| `CONFIG_EXAMPLE_I2C_MASTER_SCL` | GPIO number for `SCL` | "5" for `esp8266`, "6" for `esp32c3`, "19" for `esp32`, `esp32s2`, and `esp32s3` |
17+
| `CONFIG_EXAMPLE_I2C_MASTER_SDA` | GPIO number for `SDA` | "4" for `esp8266`, "5" for `esp32c3`, "18" for `esp32`, `esp32s2`, and `esp32s3` |
18+
| `CONFIG_EXAMPLE_LIS2HH12_SA0_PIN` | Level of the pin `SA0` on the LIS2HH12 | not selected (LOW) |
19+
20+
## Based on
21+
22+
This code is based on the examples `lis2hh12_self_test.c` and
23+
`lis2hh12_data_polling.c` by ST Microelectronics. They can be found here:
24+
https://github.com/STMicroelectronics/STMems_Standard_C_drivers/tree/master/lis2hh12_STdC/examples
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
idf_component_register(SRCS "main.c"
2+
INCLUDE_DIRS ".")
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
menu "Example configuration"
2+
config EXAMPLE_I2C_MASTER_SCL
3+
int "SCL GPIO Number"
4+
default 5 if IDF_TARGET_ESP8266
5+
default 6 if IDF_TARGET_ESP32C3
6+
default 19 if IDF_TARGET_ESP32 || IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32S3
7+
help
8+
GPIO number for I2C Master clock line.
9+
10+
config EXAMPLE_I2C_MASTER_SDA
11+
int "SDA GPIO Number"
12+
default 4 if IDF_TARGET_ESP8266
13+
default 5 if IDF_TARGET_ESP32C3
14+
default 18 if IDF_TARGET_ESP32 || IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32S3
15+
help
16+
GPIO number for I2C Master data line.
17+
18+
config EXAMPLE_LIS2HH12_SA0_PIN
19+
bool "SA0 pin level"
20+
default n
21+
help
22+
Level of the SA0 pin (true = HIGH).
23+
endmenu
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
COMPONENT_ADD_INCLUDEDIRS = . include/

examples/lis2hh12/default/main/main.c

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
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, &reg));
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

Comments
 (0)