From d45b731fbd093a61ffaa051c0674a1eda4d1d6dd Mon Sep 17 00:00:00 2001 From: raiden00 Date: Sun, 19 Feb 2017 17:42:25 +0100 Subject: [PATCH] config/stm32f429i-disco: add support for the L3GD20 driver --- configs/stm32f429i-disco/README.txt | 2 +- configs/stm32f429i-disco/src/Makefile | 4 + configs/stm32f429i-disco/src/stm32_appinit.c | 11 ++ configs/stm32f429i-disco/src/stm32_l3gd20.c | 144 ++++++++++++++++++ .../stm32f429i-disco/src/stm32f429i-disco.h | 24 +++ 5 files changed, 184 insertions(+), 1 deletion(-) create mode 100644 configs/stm32f429i-disco/src/stm32_l3gd20.c diff --git a/configs/stm32f429i-disco/README.txt b/configs/stm32f429i-disco/README.txt index 13ba9b52ca..2b406e035c 100644 --- a/configs/stm32f429i-disco/README.txt +++ b/configs/stm32f429i-disco/README.txt @@ -8,7 +8,7 @@ memory and 256kbytes. The board features: - On-board ST-LINK/V2 for programming and debugging, - On-board 64 Mbits (8 Mbytes) External SDRAM (1 Mbit x 16-bit x 4-bank) - - LIS302DL, ST MEMS motion sensor, 3-axis digital output accelerometer, + - L3GD20, ST MEMS motion sensor, 3-axis digital output gyroscope, - TFT 2.4" LCD, 262K color RGB, 240 x 320 pixels - Touchscreen controller - Two user LEDs and two push-buttons, diff --git a/configs/stm32f429i-disco/src/Makefile b/configs/stm32f429i-disco/src/Makefile index 9dcff952a2..2ff0cb8d23 100644 --- a/configs/stm32f429i-disco/src/Makefile +++ b/configs/stm32f429i-disco/src/Makefile @@ -72,6 +72,10 @@ ifeq ($(CONFIG_STM32F429I_DISCO_ILI9341),y) CSRCS += stm32_ili93414ws.c endif +ifeq ($(CONFIG_SENSORS_L3GD20),y) +CSRCS += stm32_l3gd20.c +endif + ifeq ($(and \ $(CONFIG_STM32F429I_DISCO_ILI9341_LCDIFACE), \ $(CONFIG_STM32F429I_DISCO_ILI9341_FBIFACE), \ diff --git a/configs/stm32f429i-disco/src/stm32_appinit.c b/configs/stm32f429i-disco/src/stm32_appinit.c index 26b92f3e4c..215452a228 100644 --- a/configs/stm32f429i-disco/src/stm32_appinit.c +++ b/configs/stm32f429i-disco/src/stm32_appinit.c @@ -165,6 +165,8 @@ int board_app_initialize(uintptr_t arg) int ret; #elif defined(HAVE_USBHOST) || defined(HAVE_USBMONITOR) int ret; +#elif defined(CONFIG_SENSORS_L3GD20) + int ret; #endif /* Configure SPI-based devices */ @@ -378,5 +380,14 @@ int board_app_initialize(uintptr_t arg) } #endif +#ifdef CONFIG_SENSORS_L3GD20 + ret = stm32_l3gd20initialize("/dev/gyr0"); + if (ret != OK) + { + syslog(LOG_ERR, "ERROR: Failed to initialize l3gd20 sensor: %d\n", ret); + } + +#endif + return OK; } diff --git a/configs/stm32f429i-disco/src/stm32_l3gd20.c b/configs/stm32f429i-disco/src/stm32_l3gd20.c new file mode 100644 index 0000000000..5f433d3c4e --- /dev/null +++ b/configs/stm32f429i-disco/src/stm32_l3gd20.c @@ -0,0 +1,144 @@ +/**************************************************************************** + * configs/stm32f429i-disco/src/stm32_l3gd20.c + * + * Authors: Mateusz Szafoni + * + * 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 "stm32_spi.h" +#include "stm32f429i-disco.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#if defined(CONFIG_SPI) & defined(CONFIG_SENSORS_L3GD20) + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int l3gd20_attach(FAR struct l3gd20_config_s * cfg, xcpt_t irq); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* Only one L3GD20 device on board */ + +static struct l3gd20_config_s g_l3gd20_config = + { + .attach = l3gd20_attach, + .irq = L3GD20_IRQ, + .spi_devid = SPIDEV_ACCELEROMETER + }; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: l3gd20_attach() + * + * Description: Attach the l3gd20 interrupt handler to the GPIO interrupt + * + ****************************************************************************/ + +static int l3gd20_attach(FAR struct l3gd20_config_s * cfg, xcpt_t irq) +{ + stm32_gpiosetevent(GPIO_L3GD20_DREADY, true, false, true, irq); + + return OK; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: stm32_l3gd20initialize() + * + * Description: + * Initialize and register the L3GD20 3 axis gyroscope sensor driver. + * + * Input parameters: + * devpath - The full path to the driver to register. E.g., "/dev/gyro0" + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + ****************************************************************************/ + +int stm32_l3gd20initialize(FAR const char *devpath) +{ + int ret = 0; + struct spi_dev_s *spi; + + /* Configure DREADY IRQ input */ + + stm32_configgpio(GPIO_L3GD20_DREADY); + + /* Initialize SPI */ + + spi = stm32_spi5initialize(); + + if (!spi) + { + ret = -ENODEV; + goto errout; + } + + /* Then register the gyro */ + + ret = l3gd20_register(devpath, spi, &g_l3gd20_config); + if (ret != OK) + { + goto errout; + } + + errout: + return ret; +} + +#endif /* CONFIG_SPI && CONFIG_SENSORS_L3GD20 */ diff --git a/configs/stm32f429i-disco/src/stm32f429i-disco.h b/configs/stm32f429i-disco/src/stm32f429i-disco.h index 1b309a7200..a45afd0b39 100644 --- a/configs/stm32f429i-disco/src/stm32f429i-disco.h +++ b/configs/stm32f429i-disco/src/stm32f429i-disco.h @@ -114,6 +114,11 @@ #define GPIO_CS_SST25 (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_50MHz|\ GPIO_OUTPUT_SET|GPIO_PORTE|GPIO_PIN4) +/* L3GD20 MEMS*/ + +#define GPIO_L3GD20_DREADY (GPIO_INPUT|GPIO_FLOAT|GPIO_EXTI|GPIO_PORTA|GPIO_PIN2) +#define L3GD20_IRQ (2 + STM32_IRQ_EXTI0) + /* USB OTG HS * * PA9 OTG_HS_VBUS VBUS sensing (also connected to the green LED) @@ -275,6 +280,25 @@ FAR struct ili9341_lcd_s *stm32_ili93414ws_initialize(void); FAR struct spi_dev_s *stm32_spi5initialize(void); #endif + +#if defined(CONFIG_SPI) & defined(CONFIG_SENSORS_L3GD20) +/**************************************************************************** + * Name: stm32_l3gd20initialize() + * + * Description: + * Initialize and register the L3GD20 3 axis gyroscope sensor driver. + * + * Input parameters: + * devpath - The full path to the driver to register. E.g., "/dev/gyro0" + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + ****************************************************************************/ + +int stm32_l3gd20initialize(FAR const char *devpath); +#endif + #endif /* __ASSEMBLY__ */ #endif /* __CONFIGS_STM32F429I_DISCO_SRC_STM32F429I_DISCO_H */