From b1e625eacbc8ae31823571ce421ef1b0ea813246 Mon Sep 17 00:00:00 2001 From: Florian Olbrich Date: Thu, 14 Sep 2017 20:03:28 +0200 Subject: [PATCH 1/4] Added initialization code and Kconfig entries to set up the LIS3DSH accelerometer driver on STM32F4Discovery rev. C boards and attach the associated interrupt callback. Added the argument parameter (FAR void *arg) to the interrupt handler provided by the LIS3DSH driver to fit the definition for ISRs in xcpt_t. Changed the check for working queue availability in lis3dsh interrupt handler to use work_available() and not crash in case of an overrun. --- configs/stm32f4discovery/Kconfig | 12 ++ configs/stm32f4discovery/src/Makefile | 4 + configs/stm32f4discovery/src/stm32_bringup.c | 12 ++ configs/stm32f4discovery/src/stm32_lis3dsh.c | 125 ++++++++++++++++++ .../stm32f4discovery/src/stm32f4discovery.h | 13 ++ drivers/sensors/lis3dsh.c | 16 +-- 6 files changed, 174 insertions(+), 8 deletions(-) create mode 100644 configs/stm32f4discovery/src/stm32_lis3dsh.c diff --git a/configs/stm32f4discovery/Kconfig b/configs/stm32f4discovery/Kconfig index badfd635f9..ea355379a9 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 5c26078c68..060e8bc4ac 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 79d7f00330..8c18f7bac0 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 0000000000..cfaf09140d --- /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 bad8918fda..d3e135975e 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 80db21c3f4..39bba148d3 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; } From 81b599e8ae84ec21c58ed60762e865b0e17a3c44 Mon Sep 17 00:00:00 2001 From: Florian Olbrich Date: Thu, 14 Sep 2017 20:11:20 +0000 Subject: [PATCH 2/4] stm32_lis3dsh.c edited to obey nuttx coding standards --- configs/stm32f4discovery/src/stm32_lis3dsh.c | 26 +++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/configs/stm32f4discovery/src/stm32_lis3dsh.c b/configs/stm32f4discovery/src/stm32_lis3dsh.c index cfaf09140d..70f1c597c7 100644 --- a/configs/stm32f4discovery/src/stm32_lis3dsh.c +++ b/configs/stm32f4discovery/src/stm32_lis3dsh.c @@ -56,7 +56,8 @@ * Pre-processor Definitions ************************************************************************************/ -#define GPIO_STM32F4DISCO_LIS3DSH_EXT0 (GPIO_INPUT|GPIO_FLOAT|GPIO_AF0|GPIO_SPEED_50MHz|GPIO_PORTE|GPIO_PIN0) +#define GPIO_STM32F4DISCO_LIS3DSH_EXT0 \ + (GPIO_INPUT|GPIO_FLOAT|GPIO_AF0|GPIO_SPEED_50MHz|GPIO_PORTE|GPIO_PIN0) /************************************************************************************ * Private Functions @@ -66,10 +67,12 @@ * Name: attach_disc_lis3dsh * * Description: - * Attach the lis3dsh interrupt handler to PE0/EXT0 on the STM32F4 as wired on STM32F4Discovery + * 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 + * *config - The lis3dsh instance configuration data containing the IRQ number, + * device ID and interrupt handler * interrupt_handler - The interrupt handler to attach * arg - * @@ -77,8 +80,10 @@ * 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 ); +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 ); } /************************************************************************************ @@ -98,6 +103,7 @@ int attach_disc_lis3dsh(FAR struct lis3dsh_config_s *config, xcpt_t interrupt_ha * Zero (OK) on success; a negated errno value on failure. * ************************************************************************************/ + int stm32_lis3dshinitialize(FAR const char *devpath) { static struct lis3dsh_config_s acc0_config; @@ -105,17 +111,19 @@ int stm32_lis3dshinitialize(FAR const char *devpath) int ret; sninfo("Initializing LIS3DSH\n"); - - acc0_config.irq=22; // Interrupt no. for PE0 (INT1 on LIS3DSH) + + acc0_config.irq=22; acc0_config.spi_devid=0; acc0_config.attach = &attach_disc_lis3dsh; spi = stm32_spibus_initialize(1); - if( !spi ) { + if (!spi) + { spiinfo("Failed to initialize SPI port\n"); ret = -ENODEV; } - else { + else + { ret = lis3dsh_register(devpath, spi, &acc0_config); } From 6f57d1d47268fe5103cf6f2cac1c769725f49589 Mon Sep 17 00:00:00 2001 From: Florian Olbrich Date: Thu, 14 Sep 2017 20:29:41 +0000 Subject: [PATCH 3/4] lis3dsh.c edited to obey the nuttx coding standard. --- drivers/sensors/lis3dsh.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/drivers/sensors/lis3dsh.c b/drivers/sensors/lis3dsh.c index 39bba148d3..1452a53337 100644 --- a/drivers/sensors/lis3dsh.c +++ b/drivers/sensors/lis3dsh.c @@ -318,13 +318,15 @@ static int lis3dsh_interrupt_handler(int irq, FAR void *context) * SPI bus from within an interrupt. */ - 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; + 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; } From cd584265ca901ad2658a68565e660ce118dec512 Mon Sep 17 00:00:00 2001 From: Florian Olbrich Date: Thu, 14 Sep 2017 20:33:47 +0000 Subject: [PATCH 4/4] stm32_lis3dsh.c edited to obey nuttx coding standard. --- configs/stm32f4discovery/src/stm32_lis3dsh.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/configs/stm32f4discovery/src/stm32_lis3dsh.c b/configs/stm32f4discovery/src/stm32_lis3dsh.c index 70f1c597c7..e6c6de1645 100644 --- a/configs/stm32f4discovery/src/stm32_lis3dsh.c +++ b/configs/stm32f4discovery/src/stm32_lis3dsh.c @@ -83,7 +83,11 @@ 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 ); + true, + false, + false, + interrupt_handler, + NULL ); } /************************************************************************************ @@ -118,14 +122,14 @@ int stm32_lis3dshinitialize(FAR const char *devpath) spi = stm32_spibus_initialize(1); if (!spi) - { - spiinfo("Failed to initialize SPI port\n"); - ret = -ENODEV; - } + { + spiinfo("Failed to initialize SPI port\n"); + ret = -ENODEV; + } else - { - ret = lis3dsh_register(devpath, spi, &acc0_config); - } + { + ret = lis3dsh_register(devpath, spi, &acc0_config); + } return ret; }