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>
This commit is contained in:
Vinicius Costa Gomes 2016-01-28 10:43:18 -02:00 committed by Anas Nashif
commit 9442446944
5 changed files with 111 additions and 0 deletions

View file

@ -0,0 +1,5 @@
KERNEL_TYPE = nano
BOARD ?= qemu_x86
CONF_FILE = prj.conf
include ${ZEPHYR_BASE}/Makefile.inc

View file

@ -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 |

View file

@ -0,0 +1 @@
CONFIG_SPI=y

View file

@ -0,0 +1 @@
obj-y += main.o

View file

@ -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);
}