samples: Add i2c_lsm9ds0 application
This patch adds a new sample application to demonstrate how to use I2C APIs. The application is very simple. It simply reads the 'WHO AM I' register from the accelerometer in the LSM9DS0 chip and check if it matches with the value described in the datasheet. It also adds a README file which provides the wiring information required to get the sample application working on Quark SE DevBoard. Change-Id: I9162c030874c2718506b76519b255c9c11631802 Signed-off-by: Andre Guedes <andre.guedes@intel.com>
This commit is contained in:
parent
8b10e6d382
commit
0c5b7f7d22
5 changed files with 135 additions and 0 deletions
6
samples/drivers/i2c_lsm9ds0/Makefile
Normal file
6
samples/drivers/i2c_lsm9ds0/Makefile
Normal file
|
@ -0,0 +1,6 @@
|
|||
BOARD ?= quark_se_devboard
|
||||
ARCH ?= x86
|
||||
KERNEL_TYPE = nano
|
||||
CONF_FILE = prj.conf
|
||||
|
||||
include ${ZEPHYR_BASE}/Makefile.inc
|
26
samples/drivers/i2c_lsm9ds0/README
Normal file
26
samples/drivers/i2c_lsm9ds0/README
Normal file
|
@ -0,0 +1,26 @@
|
|||
I2C LSM9DS0 Sample Application
|
||||
==============================
|
||||
|
||||
This sample application illustrates how to use I2C APIs from Zephyr.
|
||||
It requires an external device in order to work. The external device
|
||||
is the accelerometer from LSM9DS0. The datasheet can be found in:
|
||||
https://www.adafruit.com/datasheets/LSM9DS0.pdf
|
||||
|
||||
The application is very simple. It simply reads the 'WHO AM I' register
|
||||
from the accelerometer and check if matches with the value described in
|
||||
the datasheet.
|
||||
|
||||
Below follows the wiring information for each board this samples has been
|
||||
tested.
|
||||
|
||||
--------------+--------------------
|
||||
| LSM9DS0 pin | Quark SE Devboard |
|
||||
| | pin |
|
||||
|-------------+-------------------|
|
||||
| VIN | P4 pin 1 |
|
||||
| GND | P4 pin 43 |
|
||||
| SCL | P4 pin 31 |
|
||||
| SDA | P4 pin 33 |
|
||||
| CSG | P4 pin 29 |
|
||||
| SDOG | P4 pin 17 |
|
||||
--------------+--------------------
|
3
samples/drivers/i2c_lsm9ds0/prj.conf
Normal file
3
samples/drivers/i2c_lsm9ds0/prj.conf
Normal file
|
@ -0,0 +1,3 @@
|
|||
CONFIG_STDOUT_CONSOLE=y
|
||||
CONFIG_PRINTK=y
|
||||
CONFIG_I2C=y
|
1
samples/drivers/i2c_lsm9ds0/src/Makefile
Normal file
1
samples/drivers/i2c_lsm9ds0/src/Makefile
Normal file
|
@ -0,0 +1 @@
|
|||
obj-y = main.o
|
99
samples/drivers/i2c_lsm9ds0/src/main.c
Normal file
99
samples/drivers/i2c_lsm9ds0/src/main.c
Normal file
|
@ -0,0 +1,99 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Intel Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <zephyr.h>
|
||||
|
||||
#include <misc/printk.h>
|
||||
|
||||
#include <device.h>
|
||||
#include <i2c.h>
|
||||
|
||||
#define GYRO_I2C_ADDR 0x6A
|
||||
#define WHO_AM_I_REG 0x0F
|
||||
|
||||
static void read_who_am_i(struct device *dev)
|
||||
{
|
||||
uint8_t data;
|
||||
|
||||
data = WHO_AM_I_REG;
|
||||
if (i2c_write(dev, &data, sizeof(data), GYRO_I2C_ADDR) != DEV_OK) {
|
||||
printk("Error on i2c_write()\n");
|
||||
return;
|
||||
}
|
||||
|
||||
data = 0;
|
||||
if (i2c_read(dev, &data, sizeof(data), GYRO_I2C_ADDR) != DEV_OK) {
|
||||
printk("Error on i2c_read()\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (data == 0xd4)
|
||||
printk("Register Who am I read successfully\n");
|
||||
else
|
||||
printk("Register Who am I read FAILED\n");
|
||||
}
|
||||
|
||||
static void read_who_am_i_by_transfer(struct device *dev)
|
||||
{
|
||||
struct i2c_msg msg[2];
|
||||
uint8_t write_data = WHO_AM_I_REG;
|
||||
uint8_t read_data = 0;
|
||||
|
||||
msg[0].flags = I2C_MSG_WRITE | I2C_MSG_RESTART;
|
||||
msg[0].buf = &write_data;
|
||||
msg[0].len = sizeof(write_data);
|
||||
|
||||
msg[1].flags = I2C_MSG_READ | I2C_MSG_STOP;
|
||||
msg[1].buf = &read_data;
|
||||
msg[1].len = sizeof(read_data);
|
||||
|
||||
if (i2c_transfer(dev, msg, 2, GYRO_I2C_ADDR) != DEV_OK) {
|
||||
printk("Error on i2c_transfer()\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (read_data == 0xd4)
|
||||
printk("Register Who am I read successfully\n");
|
||||
else
|
||||
printk("Register Who am I read FAILED\n");
|
||||
}
|
||||
|
||||
void main(void)
|
||||
{
|
||||
union dev_config cfg;
|
||||
struct device *dev;
|
||||
|
||||
printk("Start I2C LSM9DS0 sample\n");
|
||||
|
||||
cfg.raw = 0;
|
||||
cfg.bits.use_10_bit_addr = 0;
|
||||
cfg.bits.speed = I2C_SPEED_STANDARD;
|
||||
cfg.bits.is_master_device = 1;
|
||||
|
||||
dev = device_get_binding("I2C0");
|
||||
if (!dev) {
|
||||
printk("I2C0: Device not found.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (i2c_configure(dev, cfg.raw) != DEV_OK) {
|
||||
printk("Error on i2c_configure()\n");
|
||||
return;
|
||||
}
|
||||
|
||||
read_who_am_i(dev);
|
||||
read_who_am_i_by_transfer(dev);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue