forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples/sensor/lsm303dlhc: Add LSM303DLHC sample
Add sample application for LSM303DLHC, ST MEMS system-in-package featuring a 3D digital linear acceleration sensor and a 3D digital magnetic sensor. Enable sample to build on stm32f3_disco board during sanitycheck. Signed-off-by: Yannis Damigos <[email protected]>
- Loading branch information
1 parent
93e049a
commit 017a351
Showing
6 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ supported: | |
- rtc | ||
- spi | ||
- usb_device | ||
- lsm303dlhc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# | ||
# Copyright (c) 2018, Yannis Damigos | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
|
||
cmake_minimum_required(VERSION 3.8.2) | ||
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE) | ||
project(NONE) | ||
|
||
FILE(GLOB app_sources src/*.c) | ||
target_sources(app PRIVATE ${app_sources}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
.. _lsm303dlhc: | ||
|
||
LSM303DLHC: Magnetometer and Accelerometer data Monitor | ||
####################################################### | ||
|
||
Overview | ||
******** | ||
This sample application periodically reads magnetometer and accelerometer data | ||
from the LSM303DLHC eCompass module's sensors, and displays the sensor data | ||
on the console. | ||
|
||
Requirements | ||
************ | ||
|
||
This sample uses the LSM303DLHC, ST MEMS system-in-package featuring a | ||
3D digital linear acceleration sensor and a 3D digital magnetic sensor, | ||
controlled using the I2C interface. | ||
|
||
References | ||
********** | ||
|
||
For more information about the LSM303DLHC eCompass module, see | ||
http://www.st.com/en/mems-and-sensors/lsm303dlhc.html | ||
|
||
Building and Running | ||
******************** | ||
|
||
This project outputs sensor data to the console. It requires a LSM303DLHC | ||
system-in-package, which is present on the stm32f3_disco board | ||
|
||
.. zephyr-app-commands:: | ||
:zephyr-app: samples/sensors/lsm303dlhc | ||
:board: stm32f3_disco | ||
:goals: build | ||
:compact: | ||
|
||
Sample Output | ||
============= | ||
|
||
.. code-block:: console | ||
Magnetometer data: | ||
( x y z ) = ( 0.531818 -0.435454 -0.089090 ) | ||
Accelerometer data: | ||
( x y z ) = ( -0.078127 -0.347666 1.105502 ) | ||
Magnetometer data: | ||
( x y z ) = ( -0.003636 0.297272 -0.255454 ) | ||
Accelerometer data: | ||
( x y z ) = ( 0.074221 -0.304696 0.972685 ) | ||
<repeats endlessly every 2 seconds> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
CONFIG_STDOUT_CONSOLE=y | ||
CONFIG_I2C=y | ||
CONFIG_SENSOR=y | ||
CONFIG_LSM303DLHC_MAGN=y | ||
CONFIG_LSM303DLHC_ACCEL=y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
sample: | ||
name: LSM303DLHC Magnetometer and Accelerometer data Monitor | ||
tests: | ||
test: | ||
harness: sensor | ||
tags: samples sensor | ||
depends_on: i2c lsm303dlhc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright (c) 2018, Yannis Damigos | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <zephyr.h> | ||
#include <device.h> | ||
#include <sensor.h> | ||
#include <stdio.h> | ||
#include <misc/util.h> | ||
|
||
static s32_t read_sensor(struct device *sensor, enum sensor_channel channel) | ||
{ | ||
struct sensor_value val[3]; | ||
s32_t ret = 0; | ||
|
||
ret = sensor_sample_fetch(sensor); | ||
if (ret < 0) { | ||
printf("Sensor sample update error\n"); | ||
goto end; | ||
} | ||
|
||
ret = sensor_channel_get(sensor, channel, val); | ||
if (ret < 0) { | ||
printf("Cannot read sensor channels\n"); | ||
goto end; | ||
} | ||
|
||
printf("( x y z ) = ( %f %f %f )\n", sensor_value_to_double(&val[0]), | ||
sensor_value_to_double(&val[1]), | ||
sensor_value_to_double(&val[2])); | ||
|
||
end: | ||
return ret; | ||
} | ||
|
||
void main(void) | ||
{ | ||
struct device *accelerometer = device_get_binding( | ||
CONFIG_LSM303DLHC_ACCEL_NAME); | ||
struct device *magnetometer = device_get_binding( | ||
CONFIG_LSM303DLHC_MAGN_NAME); | ||
|
||
if (accelerometer == NULL) { | ||
printf("Could not get %s device\n", | ||
CONFIG_LSM303DLHC_ACCEL_NAME); | ||
return; | ||
} | ||
|
||
if (magnetometer == NULL) { | ||
printf("Could not get %s device\n", | ||
CONFIG_LSM303DLHC_MAGN_NAME); | ||
return; | ||
} | ||
|
||
while (1) { | ||
printf("Magnetometer data:\n"); | ||
if (read_sensor(magnetometer, SENSOR_CHAN_MAGN_XYZ) < 0) { | ||
printf("Failed to read magnetometer data\n"); | ||
} | ||
|
||
printf("Accelerometer data:\n"); | ||
if (read_sensor(accelerometer, SENSOR_CHAN_ACCEL_XYZ) < 0) { | ||
printf("Failed to read accelerometer data\n"); | ||
} | ||
|
||
k_sleep(2000); | ||
} | ||
} |