diff --git a/configs/stm32f4discovery/Kconfig b/configs/stm32f4discovery/Kconfig index badfd635f94..ea355379a99 100644 --- a/configs/stm32f4discovery/Kconfig +++ b/configs/stm32f4discovery/Kconfig @@ -27,6 +27,18 @@ config STM32F4DISCO_QETIMER default 2 depends on QENCODER +config STM32F4DISCO_LIS3DSH + bool "Enable LIS3DSH driver for the IMU on STM32F4Discovery (rev. MB997C)" + default n + depends on SPI + depends on LIS3DSH + default n + ---help--- + Select to create a LIS3DSH driver instance for the builtin accelerometer of + STM32F4Discovery. Provides /dev/acc0 device file. + Also see apps/examples/lis3dsh_reader. + The LIS3DSH is available on the STM32F4Discovery rev. MB997C (see the board manual). + config PM_BUTTONS bool "PM button support" default n diff --git a/configs/stm32f4discovery/src/Makefile b/configs/stm32f4discovery/src/Makefile index 5c26078c68a..060e8bc4acb 100644 --- a/configs/stm32f4discovery/src/Makefile +++ b/configs/stm32f4discovery/src/Makefile @@ -72,6 +72,10 @@ ifeq ($(CONFIG_SENSORS_MAX31855),y) CSRCS += stm32_max31855.c endif +ifeq ($(CONFIG_STM32F4DISCO_LIS3DSH),y) +CSRCS += stm32_lis3dsh.c +endif + ifeq ($(CONFIG_SENSORS_MAX6675),y) CSRCS += stm32_max6675.c endif diff --git a/configs/stm32f4discovery/src/stm32_bringup.c b/configs/stm32f4discovery/src/stm32_bringup.c index 79d7f003301..8c18f7bac01 100644 --- a/configs/stm32f4discovery/src/stm32_bringup.c +++ b/configs/stm32f4discovery/src/stm32_bringup.c @@ -287,5 +287,17 @@ int stm32_bringup(void) ret = xen1210_archinitialize(0); #endif + +#ifdef CONFIG_STM32F4DISCO_LIS3DSH + /* Create a lis3dsh driver instance fitting the chip built into stm32f4discovery */ + + ret = stm32_lis3dshinitialize("/dev/acc0"); + if (ret < 0) + { + serr("ERROR: Failed to initialize LIS3DSH driver: %d\n", ret); + } +#endif + + return ret; } diff --git a/configs/stm32f4discovery/src/stm32_lis3dsh.c b/configs/stm32f4discovery/src/stm32_lis3dsh.c new file mode 100644 index 00000000000..cfaf09140de --- /dev/null +++ b/configs/stm32f4discovery/src/stm32_lis3dsh.c @@ -0,0 +1,125 @@ +/************************************************************************************ + * configs/stm32f4discovery/src/stm32_lis3dsh.c + * + * Copyright (C) 2017 Florian Olbrich. All rights reserved. + * Author: Florian Olbrich + * + * 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 + +#include +#include + +#include +#include + +#include "stm32.h" +#include "stm32f4discovery.h" +#include +#include + +#if defined(CONFIG_STM32F4DISCO_LIS3DSH) && defined(CONFIG_LIS3DSH) + +/************************************************************************************ + * Pre-processor Definitions + ************************************************************************************/ + +#define GPIO_STM32F4DISCO_LIS3DSH_EXT0 (GPIO_INPUT|GPIO_FLOAT|GPIO_AF0|GPIO_SPEED_50MHz|GPIO_PORTE|GPIO_PIN0) + +/************************************************************************************ + * Private Functions + ************************************************************************************/ + +/************************************************************************************ + * Name: attach_disc_lis3dsh + * + * Description: + * Attach the lis3dsh interrupt handler to PE0/EXT0 on the STM32F4 as wired on STM32F4Discovery + * + * Input parameters: + * *config - The lis3dsh instance configuration data containing the IRQ number, device ID and interrupt handler + * interrupt_handler - The interrupt handler to attach + * arg - + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + ************************************************************************************/ +int attach_disc_lis3dsh(FAR struct lis3dsh_config_s *config, xcpt_t interrupt_handler) { + return stm32_gpiosetevent(GPIO_STM32F4DISCO_LIS3DSH_EXT0, true, false, false, interrupt_handler, NULL ); +} + +/************************************************************************************ + * Public Functions + ************************************************************************************/ + +/************************************************************************************ + * Name: stm32_lis3dshinitialize + * + * Description: + * Initialize and register the LIS3DSH 3-axis accelerometer. + * + * Input parameters: + * devpath - The full path to the driver to register. E.g., "/dev/acc0" + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + ************************************************************************************/ +int stm32_lis3dshinitialize(FAR const char *devpath) +{ + static struct lis3dsh_config_s acc0_config; + struct spi_dev_s *spi; + int ret; + + sninfo("Initializing LIS3DSH\n"); + + acc0_config.irq=22; // Interrupt no. for PE0 (INT1 on LIS3DSH) + acc0_config.spi_devid=0; + acc0_config.attach = &attach_disc_lis3dsh; + + spi = stm32_spibus_initialize(1); + if( !spi ) { + spiinfo("Failed to initialize SPI port\n"); + ret = -ENODEV; + } + else { + ret = lis3dsh_register(devpath, spi, &acc0_config); + } + + return ret; +} + +#endif /* CONFIG_STM32F4DISCO_LIS3DSH && CONFIG_LIS3DSH */ diff --git a/configs/stm32f4discovery/src/stm32f4discovery.h b/configs/stm32f4discovery/src/stm32f4discovery.h index bad8918fdad..d3e135975e9 100644 --- a/configs/stm32f4discovery/src/stm32f4discovery.h +++ b/configs/stm32f4discovery/src/stm32f4discovery.h @@ -429,6 +429,19 @@ int stm32_bh1750initialize(FAR const char *devpath); int stm32_bmp180initialize(FAR const char *devpath); #endif +/**************************************************************************** + * Name: stm32_lis3dshinitialize + * + * Description: + * Called to configure SPI 1, and to register LIS3DSH and its external interrupt + * for the stm32f4discovery board. + * + ****************************************************************************/ + +#ifdef CONFIG_STM32F4DISCO_LIS3DSH +int stm32_lis3dshinitialize(FAR const char *devpath); +#endif + /**************************************************************************** * Name: stm32_bringup * diff --git a/drivers/sensors/lis3dsh.c b/drivers/sensors/lis3dsh.c index 80db21c3f47..39bba148d32 100644 --- a/drivers/sensors/lis3dsh.c +++ b/drivers/sensors/lis3dsh.c @@ -5,6 +5,7 @@ * Copyright (C) 2016 DS-Automotion GmbH. All rights reserved. * Author: Alexander Entinger * Thomas Ilk + * Florian Olbrich * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -309,8 +310,7 @@ static int lis3dsh_interrupt_handler(int irq, FAR void *context) /* Find out which device caused the interrupt */ - for (priv = g_lis3dsh_list; priv && priv->config->irq != irq; - priv = priv->flink); + for (priv = g_lis3dsh_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 @@ -318,13 +318,13 @@ static int lis3dsh_interrupt_handler(int irq, FAR void *context) * SPI bus from within an interrupt. */ - DEBUGASSERT(priv->work.worker == NULL); - ret = work_queue(HPWORK, &priv->work, lis3dsh_worker, priv, 0); - if (ret < 0) - { - snerr("ERROR: Failed to queue work: %d\n", ret); - return ret; + if(work_available(&priv->work)) { + ret = work_queue(HPWORK, &priv->work, lis3dsh_worker, priv, 0); + if (ret < 0) { + snerr("ERROR: Failed to queue work: %d\n", ret); + return ret; } + } return OK; }