drivers/sensors: Add drvier for the LIS3MDL 3 axis magnetometer
This commit is contained in:
parent
3b6befcd16
commit
338bf8c9e3
@ -173,6 +173,13 @@ config I2C_LM75
|
||||
bool
|
||||
default y if LM75
|
||||
|
||||
config LIS3MDL
|
||||
bool "STMicro LIS3MDL 3-Axis magnetometer support"
|
||||
default n
|
||||
select SPI
|
||||
---help---
|
||||
Enable driver support for the ST LIS3MDL 3-axis magnetometer.
|
||||
|
||||
config LM75
|
||||
bool "STMicro LM-75 Temperature Sensor support"
|
||||
default n
|
||||
|
@ -113,6 +113,11 @@ endif
|
||||
ifeq ($(CONFIG_MPL115A),y)
|
||||
CSRCS += mpl115a.c
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_LIS3MDL),y)
|
||||
CSRCS += lis3mdl.c
|
||||
endif
|
||||
|
||||
endif # CONFIG_SPI
|
||||
|
||||
# Quadrature encoder upper half
|
||||
|
630
drivers/sensors/lis3mdl.c
Normal file
630
drivers/sensors/lis3mdl.c
Normal file
@ -0,0 +1,630 @@
|
||||
/****************************************************************************
|
||||
* drivers/sensors/lis3mdl.c
|
||||
* Character driver for the LIS3MDL 3-Axis magnetometer.
|
||||
*
|
||||
* Copyright (C) 2016 DS-Automotion GmbH. All rights reserved.
|
||||
* Author: Alexander Entinger <a.entinger@ds-automotion.com>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name NuttX nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <debug.h>
|
||||
#include <string.h>
|
||||
#include <semaphore.h>
|
||||
|
||||
#include <nuttx/kmalloc.h>
|
||||
#include <nuttx/wqueue.h>
|
||||
|
||||
#include <nuttx/fs/fs.h>
|
||||
#include <nuttx/sensors/lis3mdl.h>
|
||||
|
||||
#if defined(CONFIG_SPI) && defined(CONFIG_LIS3MDL)
|
||||
|
||||
/****************************************************************************
|
||||
* Private
|
||||
****************************************************************************/
|
||||
|
||||
struct lis3mdl_sensor_data_s
|
||||
{
|
||||
int16_t x_mag; /* Measurement result for x axis */
|
||||
int16_t y_mag; /* Measurement result for y axis */
|
||||
int16_t z_mag; /* Measurement result for z axis */
|
||||
int16_t temperature; /* Measurement result for temperature sensor */
|
||||
};
|
||||
|
||||
struct lis3mdl_dev_s
|
||||
{
|
||||
FAR struct lis3mdl_dev_s *flink; /* Supports a singly linked list of
|
||||
* drivers */
|
||||
FAR struct spi_dev_s *spi; /* Pointer to the SPI instance */
|
||||
FAR struct lis3mdl_config_s *config; /* Pointer to the configuration
|
||||
* of the LIS3MDL sensor */
|
||||
sem_t datasem; /* Manages exclusive access to this
|
||||
* structure */
|
||||
struct lis3mdl_sensor_data_s data; /* The data as measured by the sensor */
|
||||
struct work_s work; /* The work queue is responsible for
|
||||
* retrieving the data from the
|
||||
* sensor after the arrival of new
|
||||
* data was signalled in an interrupt */
|
||||
};
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
static void lis3mdl_read_register(FAR struct lis3mdl_dev_s *dev,
|
||||
uint8_t const reg_addr, uint8_t * reg_data);
|
||||
static void lis3mdl_write_register(FAR struct lis3mdl_dev_s *dev,
|
||||
uint8_t const reg_addr,
|
||||
uint8_t const reg_data);
|
||||
static void lis3mdl_reset(FAR struct lis3mdl_dev_s *dev);
|
||||
static void lis3mdl_read_measurement_data(FAR struct lis3mdl_dev_s *dev);
|
||||
static void lis3mdl_read_magnetic_data(FAR struct lis3mdl_dev_s *dev,
|
||||
uint16_t * x_mag, uint16_t * y_mag,
|
||||
uint16_t * z_mag);
|
||||
static void lis3mdl_read_temperature(FAR struct lis3mdl_dev_s *dev,
|
||||
uint16_t * temperature);
|
||||
static int lis3mdl_interrupt_handler(int irq, FAR void *context);
|
||||
static void lis3mdl_worker(FAR void *arg);
|
||||
|
||||
static int lis3mdl_open(FAR struct file *filep);
|
||||
static int lis3mdl_close(FAR struct file *filep);
|
||||
static ssize_t lis3mdl_read(FAR struct file *, FAR char *, size_t);
|
||||
static ssize_t lis3mdl_write(FAR struct file *filep, FAR const char *buffer,
|
||||
size_t buflen);
|
||||
static int lis3mdl_ioctl(FAR struct file *filep, int cmd, unsigned long arg);
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
static const struct file_operations g_lis3mdl_fops =
|
||||
{
|
||||
lis3mdl_open,
|
||||
lis3mdl_close,
|
||||
lis3mdl_read,
|
||||
lis3mdl_write,
|
||||
NULL,
|
||||
lis3mdl_ioctl
|
||||
#ifndef CONFIG_DISABLE_POLL
|
||||
, NULL
|
||||
#endif
|
||||
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
||||
, NULL
|
||||
#endif
|
||||
};
|
||||
|
||||
/* Single linked list to store instances of drivers */
|
||||
|
||||
static struct lis3mdl_dev_s *g_lis3mdl_list = 0;
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lis3mdl_read_register
|
||||
****************************************************************************/
|
||||
|
||||
static void lis3mdl_read_register(FAR struct lis3mdl_dev_s *dev,
|
||||
uint8_t const reg_addr, uint8_t * reg_data)
|
||||
{
|
||||
/* Lock the SPI bus so that only one device can access it at the same time */
|
||||
|
||||
SPI_LOCK(dev->spi, true);
|
||||
|
||||
/* Set CS to low which selects the LIS3MDL */
|
||||
|
||||
SPI_SELECT(dev->spi, dev->config->spi_devid, true);
|
||||
|
||||
/* Transmit the register address from where we want to read - the MSB needs
|
||||
* to be set to indicate the read indication.
|
||||
*/
|
||||
|
||||
SPI_SEND(dev->spi, reg_addr | 0x80);
|
||||
|
||||
/* Write an idle byte while receiving the required data */
|
||||
|
||||
*reg_data = (uint8_t) (SPI_SEND(dev->spi, 0));
|
||||
|
||||
/* Set CS to high which deselects the LIS3MDL */
|
||||
|
||||
SPI_SELECT(dev->spi, dev->config->spi_devid, false);
|
||||
|
||||
/* Unlock the SPI bus */
|
||||
|
||||
SPI_LOCK(dev->spi, false);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lis3mdl_write_register
|
||||
****************************************************************************/
|
||||
|
||||
static void lis3mdl_write_register(FAR struct lis3mdl_dev_s *dev,
|
||||
uint8_t const reg_addr,
|
||||
uint8_t const reg_data)
|
||||
{
|
||||
/* Lock the SPI bus so that only one device can access it at the same time */
|
||||
|
||||
SPI_LOCK(dev->spi, true);
|
||||
|
||||
/* Set CS to low which selects the LIS3MDL */
|
||||
|
||||
SPI_SELECT(dev->spi, dev->config->spi_devid, true);
|
||||
|
||||
/* Transmit the register address from where we want to read */
|
||||
|
||||
SPI_SEND(dev->spi, reg_addr);
|
||||
|
||||
/* Transmit the content which should be written in the register */
|
||||
|
||||
SPI_SEND(dev->spi, reg_data);
|
||||
|
||||
/* Set CS to high which deselects the LIS3MDL */
|
||||
|
||||
SPI_SELECT(dev->spi, dev->config->spi_devid, false);
|
||||
|
||||
/* Unlock the SPI bus */
|
||||
|
||||
SPI_LOCK(dev->spi, false);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lis3mdl_reset
|
||||
****************************************************************************/
|
||||
|
||||
static void lis3mdl_reset(FAR struct lis3mdl_dev_s *dev)
|
||||
{
|
||||
lis3mdl_write_register(dev,
|
||||
LIS3MDL_CTRL_REG_2, LIS3MDL_CTRL_REG_2_SOFT_RST_bm);
|
||||
|
||||
up_mdelay(100);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lis3mdl_interrupt_handler
|
||||
****************************************************************************/
|
||||
|
||||
static void lis3mdl_read_measurement_data(FAR struct lis3mdl_dev_s *dev)
|
||||
{
|
||||
/* Magnetic data */
|
||||
|
||||
uint16_t x_mag = 0, y_mag = 0, z_mag = 0;
|
||||
|
||||
lis3mdl_read_magnetic_data(dev, &x_mag, &y_mag, &z_mag);
|
||||
|
||||
/* Temperature */
|
||||
|
||||
uint16_t temperature = 0;
|
||||
|
||||
lis3mdl_read_temperature(dev, &temperature);
|
||||
|
||||
/* Aquire the semaphore before the data is copied */
|
||||
|
||||
int ret = sem_wait(&dev->datasem);
|
||||
if (ret != OK)
|
||||
{
|
||||
snerr("Could not aquire dev->datasem: %d\n", ret);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Copy retrieve data to internal data structure */
|
||||
|
||||
dev->data.x_mag = (int16_t) (x_mag);
|
||||
dev->data.y_mag = (int16_t) (y_mag);
|
||||
dev->data.z_mag = (int16_t) (z_mag);
|
||||
dev->data.temperature = (int16_t) (temperature);
|
||||
|
||||
/* Give back the semaphore */
|
||||
|
||||
sem_post(&dev->datasem);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lis3mdl_read_magnetic_data
|
||||
****************************************************************************/
|
||||
|
||||
static void lis3mdl_read_magnetic_data(FAR struct lis3mdl_dev_s *dev,
|
||||
uint16_t * x_mag, uint16_t * y_mag,
|
||||
uint16_t * z_mag)
|
||||
{
|
||||
/* Lock the SPI bus so that only one device can access it at the same time */
|
||||
|
||||
SPI_LOCK(dev->spi, true);
|
||||
|
||||
/* Set CS to low which selects the LIS3MDL */
|
||||
|
||||
SPI_SELECT(dev->spi, dev->config->spi_devid, true);
|
||||
|
||||
/* Transmit the register address from where we want to start reading 0x80 ->
|
||||
* MSB is set -> Read Indication 0x40 -> MSB-1 (MS-Bit) is set -> auto
|
||||
* increment of address when reading multiple bytes.
|
||||
*/
|
||||
|
||||
SPI_SEND(dev->spi, (LIS3MDL_OUT_X_L_REG | 0x80 | 0x40)); /* RX */
|
||||
*x_mag = ((uint16_t) (SPI_SEND(dev->spi, 0)) << 0); /* LSB */
|
||||
*x_mag |= ((uint16_t) (SPI_SEND(dev->spi, 0)) << 8); /* MSB */
|
||||
|
||||
*y_mag = ((uint16_t) (SPI_SEND(dev->spi, 0)) << 0); /* LSB */
|
||||
*y_mag |= ((uint16_t) (SPI_SEND(dev->spi, 0)) << 8); /* MSB */
|
||||
|
||||
*z_mag = ((uint16_t) (SPI_SEND(dev->spi, 0)) << 0); /* LSB */
|
||||
*z_mag |= ((uint16_t) (SPI_SEND(dev->spi, 0)) << 8); /* MSB */
|
||||
|
||||
/* Set CS to high which deselects the LIS3MDL */
|
||||
|
||||
SPI_SELECT(dev->spi, dev->config->spi_devid, false);
|
||||
|
||||
/* Unlock the SPI bus */
|
||||
|
||||
SPI_LOCK(dev->spi, false);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lis3mdl_read_temperature
|
||||
****************************************************************************/
|
||||
|
||||
static void lis3mdl_read_temperature(FAR struct lis3mdl_dev_s *dev,
|
||||
uint16_t * temperature)
|
||||
{
|
||||
/* Lock the SPI bus so that only one device can access it at the same time */
|
||||
|
||||
SPI_LOCK(dev->spi, true);
|
||||
|
||||
/* Set CS to low which selects the LIS3MDL */
|
||||
|
||||
SPI_SELECT(dev->spi, dev->config->spi_devid, true);
|
||||
|
||||
/* Transmit the register address from where we want to start reading 0x80 ->
|
||||
* MSB is set -> Read Indication 0x40 -> MSB-1 (MS-Bit) is set -> auto
|
||||
* increment of address when reading multiple bytes */
|
||||
|
||||
SPI_SEND(dev->spi, (LIS3MDL_TEMP_OUT_L_REG | 0x80 | 0x40));
|
||||
|
||||
/* RX */
|
||||
|
||||
*temperature = ((uint16_t) (SPI_SEND(dev->spi, 0)) << 0); /* LSB */
|
||||
*temperature |= ((uint16_t) (SPI_SEND(dev->spi, 0)) << 8); /* MSB */
|
||||
|
||||
/* Set CS to high which deselects the LIS3MDL */
|
||||
|
||||
SPI_SELECT(dev->spi, dev->config->spi_devid, false);
|
||||
|
||||
/* Unlock the SPI bus */
|
||||
|
||||
SPI_LOCK(dev->spi, false);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lis3mdl_interrupt_handler
|
||||
****************************************************************************/
|
||||
|
||||
static int lis3mdl_interrupt_handler(int irq, FAR void *context)
|
||||
{
|
||||
/* This function should be called upon a rising edge on the LIS3MDL DRDY pin
|
||||
* since it signals that new data has been measured. */
|
||||
|
||||
FAR struct lis3mdl_dev_s *priv = 0;
|
||||
int ret;
|
||||
|
||||
/* Find out which LIS3MDL device caused the interrupt */
|
||||
|
||||
for (priv = g_lis3mdl_list; priv && priv->config->irq != irq;
|
||||
priv = priv->flink);
|
||||
DEBUGASSERT(priv != NULL);
|
||||
|
||||
/* Task the worker with retrieving the latest sensor data. We should not do
|
||||
* this in a interrupt since it might take too long. Also we cannot lock the
|
||||
* SPI bus from within an interrupt. */
|
||||
|
||||
DEBUGASSERT(priv->work.worker == NULL);
|
||||
ret = work_queue(HPWORK, &priv->work, lis3mdl_worker, priv, 0);
|
||||
if (ret != 0)
|
||||
{
|
||||
snerr("Failed to queue work: %d\n", ret);
|
||||
return ERROR;
|
||||
}
|
||||
else
|
||||
{
|
||||
return OK;
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lis3mdl_worker
|
||||
****************************************************************************/
|
||||
|
||||
static void lis3mdl_worker(FAR void *arg)
|
||||
{
|
||||
FAR struct lis3mdl_dev_s *priv = (FAR struct lis3mdl_dev_s *)(arg);
|
||||
DEBUGASSERT(priv != NULL);
|
||||
|
||||
/* Read out the latest sensor data */
|
||||
|
||||
lis3mdl_read_measurement_data(priv);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lis3mdl_open
|
||||
****************************************************************************/
|
||||
static int lis3mdl_open(FAR struct file *filep)
|
||||
{
|
||||
FAR struct inode *inode = filep->f_inode;
|
||||
FAR struct lis3mdl_dev_s *priv = inode->i_private;
|
||||
uint8_t reg_content;
|
||||
uint8_t reg_addr;
|
||||
|
||||
DEBUGASSERT(priv != NULL);
|
||||
|
||||
/* Perform a reset */
|
||||
|
||||
lis3mdl_reset(priv);
|
||||
|
||||
/* Enable * - the maximum full scale mode. * Full scale = +/- 1.6 mT (16
|
||||
* Gauss) */
|
||||
|
||||
lis3mdl_write_register(priv,
|
||||
LIS3MDL_CTRL_REG_2,
|
||||
LIS3MDL_CTRL_REG_2_FS_1_bm |
|
||||
LIS3MDL_CTRL_REG_2_FS_0_bm);
|
||||
|
||||
/* Enable - temperature sensor - ultra high performance mode (UMP) for X and
|
||||
* Y - fast output data rates This results in a output data rate of 155 Hz
|
||||
* for X and Y */
|
||||
|
||||
lis3mdl_write_register(priv,
|
||||
LIS3MDL_CTRL_REG_1,
|
||||
LIS3MDL_CTRL_REG_1_TEMP_EN_bm |
|
||||
LIS3MDL_CTRL_REG_1_OM_1_bm | LIS3MDL_CTRL_REG_1_OM_0_bm
|
||||
| LIS3MDL_CTRL_REG_1_FAST_ODR_bm);
|
||||
|
||||
/* Enable * - ultra high performance mode (UMP) for Z * This should result to
|
||||
* the same output data rate as for X and Y. */
|
||||
|
||||
lis3mdl_write_register(priv,
|
||||
LIS3MDL_CTRL_REG_4,
|
||||
LIS3MDL_CTRL_REG_4_OMZ_1_bm |
|
||||
LIS3MDL_CTRL_REG_4_OMZ_0_bm);
|
||||
|
||||
/* Enable * - block data update for magnetic sensor data * This should
|
||||
* prevent race conditions when reading sensor data. */
|
||||
|
||||
lis3mdl_write_register(priv, LIS3MDL_CTRL_REG_5, LIS3MDL_CTRL_REG_5_BDU_bm);
|
||||
|
||||
/* Enable continous conversion mode - the device starts measuring now. */
|
||||
|
||||
lis3mdl_write_register(priv, LIS3MDL_CTRL_REG_3, 0);
|
||||
|
||||
/* Read measurement data to ensure DRDY is low */
|
||||
|
||||
lis3mdl_read_measurement_data(priv);
|
||||
|
||||
/* Read back the content of all control registers for debug purposes */
|
||||
|
||||
reg_content = 0;
|
||||
eg_addr = LIS3MDL_CTRL_REG_1;
|
||||
for (; reg_addr <= LIS3MDL_CTRL_REG_5; reg_addr++)
|
||||
{
|
||||
lis3mdl_read_register(priv, reg_addr, ®_content);
|
||||
snerr("R#%04x = %04x\n", reg_addr, reg_content);
|
||||
}
|
||||
|
||||
lis3mdl_read_register(priv, LIS3MDL_STATUS_REG, ®_content);
|
||||
snerr("STATUS_REG = %04x\n", reg_content);
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lis3mdl_close
|
||||
****************************************************************************/
|
||||
|
||||
static int lis3mdl_close(FAR struct file *filep)
|
||||
{
|
||||
FAR struct inode *inode = filep->f_inode;
|
||||
FAR struct lis3mdl_dev_s *priv = inode->i_private;
|
||||
|
||||
DEBUGASSERT(priv != NULL);
|
||||
|
||||
/* Perform a reset */
|
||||
|
||||
lis3mdl_reset(priv);
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lis3mdl_read
|
||||
****************************************************************************/
|
||||
static ssize_t lis3mdl_read(FAR struct file *filep, FAR char *buffer,
|
||||
size_t buflen)
|
||||
{
|
||||
FAR struct inode *inode = filep->f_inode;
|
||||
FAR struct lis3mdl_dev_s *priv = inode->i_private;
|
||||
FAR struct lis3mdl_sensor_data_s *data;
|
||||
int ret;
|
||||
|
||||
DEBUGASSERT(priv != NULL);
|
||||
|
||||
/* Check if enough memory was provided for the read call */
|
||||
|
||||
if (buflen < sizeof(FAR struct lis3mdl_sensor_data_s))
|
||||
{
|
||||
snerr("Not enough memory for reading out a sensor data sample\n");
|
||||
return -ENOSYS;
|
||||
}
|
||||
|
||||
/* Aquire the semaphore before the data is copied */
|
||||
|
||||
ret = sem_wait(&priv->datasem);
|
||||
if (ret != OK)
|
||||
{
|
||||
snerr("Could not aquire priv->datasem: %d\n", ret);
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
/* Copy the sensor data into the buffer */
|
||||
|
||||
data = (FAR struct lis3mdl_sensor_data_s *)buffer;
|
||||
memset(data, 0, sizeof(FAR struct lis3mdl_sensor_data_s));
|
||||
|
||||
data->x_mag = priv->data.x_mag;
|
||||
data->y_mag = priv->data.y_mag;
|
||||
data->z_mag = priv->data.z_mag;
|
||||
data->temperature = priv->data.temperature;
|
||||
|
||||
/* Give back the semaphore */
|
||||
|
||||
sem_post(&priv->datasem);
|
||||
|
||||
return sizeof(FAR struct lis3mdl_sensor_data_s);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lis3mdl_write
|
||||
****************************************************************************/
|
||||
|
||||
static ssize_t lis3mdl_write(FAR struct file *filep, FAR const char *buffer,
|
||||
size_t buflen)
|
||||
{
|
||||
return -ENOSYS;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lis3mdl_ioctl
|
||||
****************************************************************************/
|
||||
|
||||
static int lis3mdl_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
||||
{
|
||||
int ret = OK;
|
||||
|
||||
switch (cmd)
|
||||
{
|
||||
/* Command was not recognized */
|
||||
|
||||
default:
|
||||
snerr("Unrecognized cmd: %d\n", cmd);
|
||||
ret = -ENOTTY;
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lis3mdl_register
|
||||
*
|
||||
* Description:
|
||||
* Register the LIS3MDL character device as 'devpath'
|
||||
*
|
||||
* Input Parameters:
|
||||
* devpath - The full path to the driver to register. E.g., "/dev/mag0"
|
||||
* spi - An instance of the SPI interface to use to communicate with LIS3MDL
|
||||
* config - configuration for the LIS3MDL driver. For details see description above.
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero (OK) on success; a negated errno value on failure.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int lis3mdl_register(FAR const char *devpath, FAR struct spi_dev_s *spi,
|
||||
FAR struct lis3mdl_config_s const *config)
|
||||
{
|
||||
FAR struct lis3mdl_dev_s *priv;
|
||||
int ret;
|
||||
|
||||
/* Sanity check */
|
||||
|
||||
DEBUGASSERT(spi != NULL);
|
||||
DEBUGASSERT(config != NULL);
|
||||
|
||||
/* Initialize the LIS3MDL device structure */
|
||||
|
||||
priv = (FAR struct lis3mdl_dev_s *)kmm_malloc(sizeof(struct lis3mdl_dev_s));
|
||||
if (priv == NULL)
|
||||
{
|
||||
snerr("Failed to allocate instance\n");
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
priv->spi = spi;
|
||||
priv->config = config;
|
||||
|
||||
priv->work.worker = NULL;
|
||||
|
||||
sem_init(&priv->datasem, 0, 1); /* Initialize sensor data access
|
||||
* semaphore */
|
||||
|
||||
/* Setup SPI frequency and mode */
|
||||
|
||||
SPI_SETFREQUENCY(spi, LIS3MDL_SPI_FREQUENCY);
|
||||
SPI_SETMODE(spi, LIS3MDL_SPI_MODE);
|
||||
|
||||
/* Attach the interrupt handler */
|
||||
|
||||
ret = priv->config->attach(priv->config, &lis3mdl_interrupt_handler);
|
||||
if (ret < 0)
|
||||
{
|
||||
snerr("Failed to attach interrupt\n");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
/* Register the character driver */
|
||||
|
||||
ret = register_driver(devpath, &g_lis3mdl_fops, 0666, priv);
|
||||
if (ret < 0)
|
||||
{
|
||||
snerr("Failed to register driver: %d\n", ret);
|
||||
kmm_free(priv);
|
||||
sem_destroy(&priv->datasem);
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
/* Since we support multiple LIS3MDL devices are supported, we will need to
|
||||
* add this new instance to a list of device instances so that it can be
|
||||
* found by the interrupt handler based on the received IRQ number. */
|
||||
|
||||
priv->flink = g_lis3mdl_list;
|
||||
g_lis3mdl_list = priv;
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_SPI && CONFIG_LIS3MDL */
|
178
include/nuttx/sensors/lis3mdl.h
Normal file
178
include/nuttx/sensors/lis3mdl.h
Normal file
@ -0,0 +1,178 @@
|
||||
/****************************************************************************
|
||||
* include/nuttx/sensors/lis3mdl.h
|
||||
*
|
||||
* Copyright (C) 2016 DS-Automotion GmbH. All rights reserved.
|
||||
* Author: Alexander Entinger <a.entinger@ds-automotion.com>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name NuttX nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef NUTTX_INCLUDE_NUTTX_SENSORS_LIS3MDL_H_
|
||||
#define NUTTX_INCLUDE_NUTTX_SENSORS_LIS3MDL_H_
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/irq.h>
|
||||
#include <nuttx/config.h>
|
||||
#include <nuttx/fs/ioctl.h>
|
||||
#include <nuttx/spi/spi.h>
|
||||
|
||||
#if defined(CONFIG_SPI) && defined(CONFIG_LIS3MDL)
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/* LIS3MDL Register Definitions *********************************************/
|
||||
|
||||
#define LIS3MDL_WHO_AM_I_REG (0x0F)
|
||||
#define LIS3MDL_CTRL_REG_1 (0x20)
|
||||
#define LIS3MDL_CTRL_REG_2 (0x21)
|
||||
#define LIS3MDL_CTRL_REG_3 (0x22)
|
||||
#define LIS3MDL_CTRL_REG_4 (0x23)
|
||||
#define LIS3MDL_CTRL_REG_5 (0x24)
|
||||
#define LIS3MDL_STATUS_REG (0x27)
|
||||
#define LIS3MDL_OUT_X_L_REG (0x28)
|
||||
#define LIS3MDL_OUT_X_H_REG (0x29)
|
||||
#define LIS3MDL_OUT_Y_L_REG (0x2A)
|
||||
#define LIS3MDL_OUT_Y_H_REG (0x2B)
|
||||
#define LIS3MDL_OUT_Z_L_REG (0x2C)
|
||||
#define LIS3MDL_OUT_Z_H_REG (0x2D)
|
||||
#define LIS3MDL_TEMP_OUT_L_REG (0x2E)
|
||||
#define LIS3MDL_TEMP_OUT_H_REG (0x2F)
|
||||
#define LIS3MDL_INT_CFG_REG (0x30)
|
||||
#define LIS3MDL_INT_SRC_REG (0x31)
|
||||
#define LIS3MDL_INT_THS_L_REG (0x32)
|
||||
#define LIS3MDL_INT_THS_H_REG (0x33)
|
||||
|
||||
/* LIS3MDL CTRL_REG_1 Bit Definitions ***************************************/
|
||||
|
||||
#define LIS3MDL_CTRL_REG_1_TEMP_EN_bm (1<<7) /* Enable the temperature sensor */
|
||||
#define LIS3MDL_CTRL_REG_1_OM_1_bm (1<<6) /* Select the operating mode of X and Y axis bit 1 */
|
||||
#define LIS3MDL_CTRL_REG_1_OM_0_bm (1<<5) /* Select the operating mode of X and Y axis bit 0 */
|
||||
#define LIS3MDL_CTRL_REG_1_DO2_bm (1<<4) /* Output data rate selection bit 2 */
|
||||
#define LIS3MDL_CTRL_REG_1_DO1_bm (1<<3) /* Output data rate selection bit 1 */
|
||||
#define LIS3MDL_CTRL_REG_1_DO0_bm (1<<2) /* Output data rate selection bit 2 */
|
||||
#define LIS3MDL_CTRL_REG_1_FAST_ODR_bm (1<<1) /* Enable higher output data rates */
|
||||
|
||||
/* LIS3MDL CTRL_REG_2 Bit Definitions ***************************************/
|
||||
|
||||
#define LIS3MDL_CTRL_REG_2_FS_1_bm (1<<6) /* Full scale selection bit 1 */
|
||||
#define LIS3MDL_CTRL_REG_2_FS_0_bm (1<<5) /* Full scale selection bit 0 */
|
||||
#define LIS3MDL_CTRL_REG_2_REBOOT_bm (1<<3) /* Reboot Memory Content */
|
||||
#define LIS3MDL_CTRL_REG_2_SOFT_RST_bm (1<<2) /* Soft Reset */
|
||||
|
||||
/* LIS3MDL CTRL_REG_4 Bit Definitions ***************************************/
|
||||
|
||||
#define LIS3MDL_CTRL_REG_4_OMZ_1_bm (1<<3) /* Select the operating mode of Z axis bit 1 */
|
||||
#define LIS3MDL_CTRL_REG_4_OMZ_0_bm (1<<2) /* Select the operating mode of Z axis bit 0 */
|
||||
|
||||
/* LIS3MDL CTRL_REG_5 Bit Definitions ***************************************/
|
||||
|
||||
#define LIS3MDL_CTRL_REG_5_BDU_bm (1<<6) /* Enable block data update for magnetic data (prevent race conditions while reading) */
|
||||
|
||||
/* SPI BUS PARAMETERS *******************************************************/
|
||||
|
||||
#define LIS3MDL_SPI_FREQUENCY (1000000) /* 1 MHz */
|
||||
#define LIS3MDL_SPI_MODE (SPIDEV_MODE3) /* Device uses SPI Mode 3: CPOL=1, CPHA=1 */
|
||||
|
||||
/****************************************************************************
|
||||
* Public Types
|
||||
****************************************************************************/
|
||||
|
||||
/* A reference to a structure of this type must be passed to the LIS3MDL
|
||||
* driver. This structure provides information about the configuration
|
||||
* of the sensor and provides some board-specific hooks.
|
||||
*
|
||||
* Memory for this structure is provided by the caller. It is not copied
|
||||
* by the driver and is presumed to persist while the driver is active.
|
||||
*/
|
||||
|
||||
struct lis3mdl_config_s
|
||||
{
|
||||
/* Since multiple LIS3MDL can be connected to the same SPI bus we need
|
||||
* to use multiple spi device ids which are employed by NuttX to select/
|
||||
* deselect the desired LIS3MDL chip via their chip select inputs.
|
||||
*/
|
||||
|
||||
int spi_devid;
|
||||
|
||||
/* The IRQ number must be provided for each so LIS3MDL device so that
|
||||
* their interrupts can be distinguished.
|
||||
*/
|
||||
|
||||
int irq;
|
||||
|
||||
/* Attach the LIS3MDL interrupt handler to the GPIO interrupt of the
|
||||
* concrete LIS3MDL instance.
|
||||
*/
|
||||
|
||||
int (*attach)(FAR struct lis3mdl_config_s *, xcpt_t);
|
||||
};
|
||||
|
||||
/****************************************************************************
|
||||
* Public Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
#define EXTERN extern "C"
|
||||
extern "C"
|
||||
{
|
||||
#else
|
||||
#define EXTERN extern
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lis3mdl_register
|
||||
*
|
||||
* Description:
|
||||
* Register the LIS3MDL character device as 'devpath'
|
||||
*
|
||||
* Input Parameters:
|
||||
* devpath - The full path to the driver to register. E.g., "/dev/mag0"
|
||||
* spi - An instance of the SPI interface to use to communicate with LIS3MDL
|
||||
* config - configuration for the LIS3MDL driver. For details see description above.
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero (OK) on success; a negated errno value on failure.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int lis3mdl_register(FAR const char *devpath, FAR struct spi_dev_s *spi,
|
||||
FAR struct lis3mdl_config_s const *config);
|
||||
|
||||
#undef EXTERN
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* CONFIG_SPI && CONFIG_LIS3MDL */
|
||||
#endif /* NUTTX_INCLUDE_NUTTX_SENSORS_LIS3MDL_H_ */
|
Loading…
Reference in New Issue
Block a user