diff options
author | Vinicius Costa Gomes <vinicius.gomes@intel.com> | 2016-01-28 10:43:18 -0200 |
---|---|---|
committer | Anas Nashif <anas.nashif@intel.com> | 2016-02-05 20:25:29 -0500 |
commit | 9442446944b5f594075ae69269dcf91646a52a22 (patch) | |
tree | 605966762eeb6906858fbc2fb43f77de8393e203 /samples | |
parent | eb8ab24ae81e4a3f1500264ad7f55d04b7156969 (diff) |
samples: Add a sample SPI application
This application reads the 'WHO AM I' register from a LSM9DS0
sensor connected over SPI.
Change-Id: I7b0631e9bc783aea60fdb489f40058e0c15275fb
Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@intel.com>
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
Diffstat (limited to 'samples')
-rw-r--r-- | samples/nanokernel/apps/spi_lsm9ds0/Makefile | 5 | ||||
-rw-r--r-- | samples/nanokernel/apps/spi_lsm9ds0/README | 21 | ||||
-rw-r--r-- | samples/nanokernel/apps/spi_lsm9ds0/prj.conf | 1 | ||||
-rw-r--r-- | samples/nanokernel/apps/spi_lsm9ds0/src/Makefile | 1 | ||||
-rw-r--r-- | samples/nanokernel/apps/spi_lsm9ds0/src/main.c | 83 |
5 files changed, 111 insertions, 0 deletions
diff --git a/samples/nanokernel/apps/spi_lsm9ds0/Makefile b/samples/nanokernel/apps/spi_lsm9ds0/Makefile new file mode 100644 index 000000000..f2e25a17f --- /dev/null +++ b/samples/nanokernel/apps/spi_lsm9ds0/Makefile @@ -0,0 +1,5 @@ +KERNEL_TYPE = nano +BOARD ?= qemu_x86 +CONF_FILE = prj.conf + +include ${ZEPHYR_BASE}/Makefile.inc diff --git a/samples/nanokernel/apps/spi_lsm9ds0/README b/samples/nanokernel/apps/spi_lsm9ds0/README new file mode 100644 index 000000000..6fb761bbb --- /dev/null +++ b/samples/nanokernel/apps/spi_lsm9ds0/README @@ -0,0 +1,21 @@ +SPI Test Application + +This application just retrieves the 'WHO AM I' register from the +Accelerometer from a LSM9DS0 connected via API. + +The device datasheet can be found here: +https://www.adafruit.com/datasheets/LSM9DS0.pdf + +Wiring information: + +| LSM9DS0 pin | Quark SE Devboard | +| | pin | +|-------------+--------------------| +| VIN | VDD_HDR_3P3 | +| CS_G | GPIO24_SPI0_M_nCS0 | +| CS_XM | VDD_HDR_3P3 | +| SCL | GPIO24_SPI0_M_SCLK | +| SDO_G | GPIO24_SPI0_M_MISO | +| SDO_XM | Ground | +| SDA | GPIO24_SPI0_M_MISO | +| GND | Ground | diff --git a/samples/nanokernel/apps/spi_lsm9ds0/prj.conf b/samples/nanokernel/apps/spi_lsm9ds0/prj.conf new file mode 100644 index 000000000..5616bfc48 --- /dev/null +++ b/samples/nanokernel/apps/spi_lsm9ds0/prj.conf @@ -0,0 +1 @@ +CONFIG_SPI=y diff --git a/samples/nanokernel/apps/spi_lsm9ds0/src/Makefile b/samples/nanokernel/apps/spi_lsm9ds0/src/Makefile new file mode 100644 index 000000000..b666967fd --- /dev/null +++ b/samples/nanokernel/apps/spi_lsm9ds0/src/Makefile @@ -0,0 +1 @@ +obj-y += main.o diff --git a/samples/nanokernel/apps/spi_lsm9ds0/src/main.c b/samples/nanokernel/apps/spi_lsm9ds0/src/main.c new file mode 100644 index 000000000..5c63f50d1 --- /dev/null +++ b/samples/nanokernel/apps/spi_lsm9ds0/src/main.c @@ -0,0 +1,83 @@ +/* + * 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 <device.h> +#include <spi.h> + +#include <misc/printk.h> + +#if defined(CONFIG_SPI_QMSI_PORT_0_DRV_NAME) +#define SPI_PORT_0 CONFIG_SPI_QMSI_PORT_0_DRV_NAME +#elif defined(CONFIG_SPI_DW_PORT_0_DRV_NAME) +#define SPI_PORT_0 CONFIG_SPI_DW_PORT_0_DRV_NAME +#else +#error "Unknown SPI driver implementation" +#endif + +#define LSM9DS0_WHOAMI_G 0xf +#define LSM9DS0_READ_MASK 0x80 + +static uint8_t rx_buffer[2], tx_buffer[2]; + +static uint8_t lsm9ds0_read_whoami_g(struct device *dev) +{ + int err; + + tx_buffer[1] = LSM9DS0_WHOAMI_G | LSM9DS0_READ_MASK; + + err = spi_transceive(dev, tx_buffer, sizeof(tx_buffer), + rx_buffer, sizeof(rx_buffer)); + if (err) { + printk("Error during SPI transfer\n"); + return 0; + } + + return rx_buffer[0]; +} + +void main(void) +{ + struct spi_config config = { 0 }; + struct device *spi_mst_0 = device_get_binding(SPI_PORT_0); + uint8_t id; + int err; + + printk("SPI Example application\n"); + + if (!spi_mst_0) + return; + + config.config = SPI_MODE_CPOL | SPI_MODE_CPHA | SPI_WORD(16); + config.max_sys_freq = 256; + + err = spi_configure(spi_mst_0, &config); + if (err) { + printk("Could not configure SPI device\n"); + return; + } + + err = spi_slave_select(spi_mst_0, 1); + if (err) { + printk("Could not select SPI slave\n"); + return; + } + + id = lsm9ds0_read_whoami_g(spi_mst_0); + + printk("LSM9DS0 Who Am I: 0x%x\n", id); +} |