From 93c23a2c03ee336b5c680f24c66758b180161f8a Mon Sep 17 00:00:00 2001 From: Ouss4 Date: Sat, 1 Sep 2018 07:36:01 -0600 Subject: [PATCH] configs/olimex-stm32-p407: Add board support for the DHTxx sensor. --- configs/olimex-stm32-p407/dhtxx/defconfig | 65 +++++++ configs/olimex-stm32-p407/src/Makefile | 20 +- .../olimex-stm32-p407/src/olimex-stm32-p407.h | 18 ++ configs/olimex-stm32-p407/src/stm32_bringup.c | 8 + configs/olimex-stm32-p407/src/stm32_dhtxx.c | 173 ++++++++++++++++++ 5 files changed, 276 insertions(+), 8 deletions(-) create mode 100644 configs/olimex-stm32-p407/dhtxx/defconfig create mode 100644 configs/olimex-stm32-p407/src/stm32_dhtxx.c diff --git a/configs/olimex-stm32-p407/dhtxx/defconfig b/configs/olimex-stm32-p407/dhtxx/defconfig new file mode 100644 index 0000000000..fc6abb0b80 --- /dev/null +++ b/configs/olimex-stm32-p407/dhtxx/defconfig @@ -0,0 +1,65 @@ +CONFIG_ARCH="arm" +CONFIG_ARCH_BOARD="olimex-stm32-p407" +CONFIG_ARCH_BOARD_OLIMEX_STM32P407=y +CONFIG_ARCH_BUTTONS=y +CONFIG_ARCH_CHIP_STM32=y +CONFIG_ARCH_CHIP_STM32F407ZG=y +CONFIG_ARCH_IRQBUTTONS=y +CONFIG_ARCH_STACKDUMP=y +CONFIG_BOARDCTL_USBDEVCTRL=y +CONFIG_BOARD_INITIALIZE=y +CONFIG_BOARD_LOOPSPERMSEC=16717 +CONFIG_BUILTIN=y +CONFIG_CDCACM=y +CONFIG_CDCACM_CONSOLE=y +CONFIG_DISABLE_POLL=y +CONFIG_EXAMPLES_DHTXX=y +CONFIG_EXAMPLES_NSH=y +CONFIG_EXAMPLES_NSH_CXXINITIALIZE=y +CONFIG_FAT_LCNAMES=y +CONFIG_FAT_LFN=y +CONFIG_FS_FAT=y +CONFIG_FS_PROCFS=y +CONFIG_HAVE_CXX=y +CONFIG_HAVE_CXXINITIALIZE=y +CONFIG_INTELHEX_BINARY=y +CONFIG_LIBC_FLOATINGPOINT=y +CONFIG_LIB_BOARDCTL=y +CONFIG_MAX_TASKS=16 +CONFIG_MAX_WDOGPARMS=2 +CONFIG_NFILE_DESCRIPTORS=8 +CONFIG_NFILE_STREAMS=8 +CONFIG_NSH_BUILTIN_APPS=y +CONFIG_NSH_DISABLE_GET=y +CONFIG_NSH_DISABLE_IFUPDOWN=y +CONFIG_NSH_DISABLE_PUT=y +CONFIG_NSH_DISABLE_WGET=y +CONFIG_NSH_FILEIOSIZE=512 +CONFIG_NSH_LINELEN=64 +CONFIG_NSH_READLINE=y +CONFIG_PREALLOC_MQ_MSGS=4 +CONFIG_PREALLOC_TIMERS=4 +CONFIG_PREALLOC_WDOGS=8 +CONFIG_RAM_SIZE=131072 +CONFIG_RAM_START=0x20000000 +CONFIG_RAW_BINARY=y +CONFIG_RR_INTERVAL=200 +CONFIG_SCHED_HPWORK=y +CONFIG_SCHED_HPWORKPRIORITY=192 +CONFIG_SCHED_WAITPID=y +CONFIG_SDCLONE_DISABLE=y +CONFIG_SENSORS=y +CONFIG_SENSORS_DHTXX=y +CONFIG_START_YEAR=2013 +CONFIG_STM32_CCMEXCLUDE=y +CONFIG_STM32_DISABLE_IDLE_SLEEP_DURING_DEBUG=y +CONFIG_STM32_FREERUN=y +CONFIG_STM32_JTAG_SW_ENABLE=y +CONFIG_STM32_OTGFS=y +CONFIG_STM32_PWR=y +CONFIG_STM32_TIM1=y +CONFIG_STM32_USART3=y +CONFIG_TASK_NAME_SIZE=32 +CONFIG_USBDEV=y +CONFIG_USER_ENTRYPOINT="nsh_main" +CONFIG_WDOG_INTRESERVE=1 diff --git a/configs/olimex-stm32-p407/src/Makefile b/configs/olimex-stm32-p407/src/Makefile index a19f5f58aa..b5cad654d8 100644 --- a/configs/olimex-stm32-p407/src/Makefile +++ b/configs/olimex-stm32-p407/src/Makefile @@ -39,33 +39,37 @@ ASRCS = CSRCS = stm32_boot.c stm32_bringup.c ifeq ($(CONFIG_ARCH_LEDS),y) -CSRCS += stm32_autoleds.c + CSRCS += stm32_autoleds.c else -CSRCS += stm32_userleds.c + CSRCS += stm32_userleds.c endif ifeq ($(CONFIG_ARCH_BUTTONS),y) -CSRCS += stm32_buttons.c + CSRCS += stm32_buttons.c endif ifeq ($(CONFIG_STM32_FSMC),y) -CSRCS += stm32_sram.c + CSRCS += stm32_sram.c endif ifeq ($(CONFIG_STM32_OTGFS),y) -CSRCS += stm32_usb.c + CSRCS += stm32_usb.c endif ifeq ($(CONFIG_LIB_BOARDCTL),y) -CSRCS += stm32_appinit.c + CSRCS += stm32_appinit.c endif ifeq ($(CONFIG_ADC),y) -CSRCS += stm32_adc.c + CSRCS += stm32_adc.c endif ifeq ($(CONFIG_CAN),y) -CSRCS += stm32_can.c + CSRCS += stm32_can.c +endif + +ifeq ($(CONFIG_SENSORS_DHTXX),y) + CSRCS += stm32_dhtxx.c endif include $(TOPDIR)/configs/Board.mk diff --git a/configs/olimex-stm32-p407/src/olimex-stm32-p407.h b/configs/olimex-stm32-p407/src/olimex-stm32-p407.h index 44f6c8ddde..622adf0a1d 100644 --- a/configs/olimex-stm32-p407/src/olimex-stm32-p407.h +++ b/configs/olimex-stm32-p407/src/olimex-stm32-p407.h @@ -169,6 +169,12 @@ #define GPIO_BTN_DOWN (GPIO_INPUT|GPIO_FLOAT|GPIO_EXTI|GPIO_PORTG|GPIO_PIN8) #define GPIO_BTN_CENTER (GPIO_INPUT|GPIO_FLOAT|GPIO_EXTI|GPIO_PORTG|GPIO_PIN15) +/* DHTxx confing pin. */ + +#define GPIO_DHTXX_PIN (GPIO_PORTG|GPIO_PIN9) +#define GPIO_DHTXX_PIN_OUTPUT (GPIO_OUTPUT|GPIO_FLOAT|GPIO_SPEED_100MHz|GPIO_DHTXX_PIN) +#define GPIO_DHTXX_PIN_INPUT (GPIO_INPUT|GPIO_FLOAT|GPIO_DHTXX_PIN) + /* USB OTG FS * * PA9 OTG_FS_VBUS VBUS sensing (also connected to the green LED) @@ -289,5 +295,17 @@ int stm32_adc_setup(void); int stm32_can_setup(void); #endif +/************************************************************************************ + * Name: stm32_dhtxx_initialize + * + * Description: + * Called to initialize the DHTxx sensor + * + ************************************************************************************/ + +#ifdef CONFIG_SENSORS_DHTXX +int stm32_dhtxx_initialize(FAR const char *devpath); +#endif + #endif /* __ASSEMBLY__ */ #endif /* __CONFIGS_OLIMEX_STM32_P407_SRC_H */ diff --git a/configs/olimex-stm32-p407/src/stm32_bringup.c b/configs/olimex-stm32-p407/src/stm32_bringup.c index c57f62f785..a937cf07d7 100644 --- a/configs/olimex-stm32-p407/src/stm32_bringup.c +++ b/configs/olimex-stm32-p407/src/stm32_bringup.c @@ -200,6 +200,14 @@ int stm32_bringup(void) } #endif +#ifdef CONFIG_SENSORS_DHTXX + ret = stm32_dhtxx_initialize("/dev/dht0"); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: stm32_dhtxx_initialize() failed: %d\n", ret); + } +#endif + UNUSED(ret); return OK; } diff --git a/configs/olimex-stm32-p407/src/stm32_dhtxx.c b/configs/olimex-stm32-p407/src/stm32_dhtxx.c new file mode 100644 index 0000000000..8e16bba265 --- /dev/null +++ b/configs/olimex-stm32-p407/src/stm32_dhtxx.c @@ -0,0 +1,173 @@ +/**************************************************************************** + * configs/olimex-stm32-p407/src/stm32_dhtxx.c + * + * Copyright (C) 2014 Abdelatif GUETTOUCHE. All rights reserved. + * Author: Abdelatif GUETTOUCHE + * + * 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 +#include + +#include "stm32.h" +#include "stm32_freerun.h" +#include "olimex-stm32-p407.h" + +#if defined(CONFIG_SENSORS_DHTXX) + +/************************************************************************************ + * Pre-processor Definitions + ************************************************************************************/ + +/* Use TIM1 as free running timer for the DHTXX sensor. */ + +#define DHTXX_FREE_TIMER 1 + +/************************************************************************************ + * Private Function Prototypes + ************************************************************************************/ + +static void dhtxx_config_data_pin(FAR struct dhtxx_config_s *state, bool mode); +static void dhtxx_set_data_pin(FAR struct dhtxx_config_s *state, bool value); +static bool dhtxx_read_data_pin(FAR struct dhtxx_config_s *state); +static int64_t dhtxx_get_clock(FAR struct dhtxx_config_s *state); + +/************************************************************************************ + * Private Data + ************************************************************************************/ + +static struct dhtxx_config_s g_dhtxx_config = +{ + .config_data_pin = dhtxx_config_data_pin, + .set_data_pin = dhtxx_set_data_pin, + .read_data_pin = dhtxx_read_data_pin, + .get_clock = dhtxx_get_clock, + .type = DHTXX_DHT11 +}; + +struct stm32_freerun_s g_freerun; +struct timespec ts; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static void dhtxx_config_data_pin(FAR struct dhtxx_config_s *state, bool mode) +{ + if (mode) + { + stm32_configgpio(GPIO_DHTXX_PIN_INPUT); + } + else + { + stm32_configgpio(GPIO_DHTXX_PIN_OUTPUT); + } +} + +static void dhtxx_set_data_pin(FAR struct dhtxx_config_s *state, bool value) +{ + stm32_gpiowrite(GPIO_DHTXX_PIN_OUTPUT, value); +} + +static bool dhtxx_read_data_pin(FAR struct dhtxx_config_s *state) +{ + return stm32_gpioread(GPIO_DHTXX_PIN_INPUT); +} + +static int64_t dhtxx_get_clock(FAR struct dhtxx_config_s *state) +{ + /* Get the time from free running timer */ + + stm32_freerun_counter(&g_freerun, &ts); + + /* Return time in microseconds */ + + return ((ts.tv_sec * 1000000) + (ts.tv_nsec / 1000)); +} + +/************************************************************************************ + * Public Functions + ************************************************************************************/ + +/**************************************************************************** + * Name: stm32_dhtxx_initialize + * + * Description: + * This function is called by application-specific, setup logic to + * configure the DHTxx sensor. This function will register the driver + * with the name passed at *devpath. + * + * Input Parameters: + * devpath - The device name to register. + * + * Returned Value: + * Zero (OK) returned on success; + * a negated errno value is returned on failure. + * + ****************************************************************************/ + +int stm32_dhtxx_initialize(FAR const char *devpath) +{ + int ret; + + stm32_configgpio(GPIO_DHTXX_PIN); + + stm32_gpiowrite(GPIO_DHTXX_PIN, false); + + /* Initialize the free-running timer with 1uS resolution */ + + ret = stm32_freerun_initialize(&g_freerun, DHTXX_FREE_TIMER, 1); + if (ret < 0) + { + snerr("Failed to initialize the free running timer! Err = %d\n", ret); + return -ENODEV; + } + + ret = dhtxx_register(devpath, &g_dhtxx_config); + if (ret < 0) + { + snerr("ERROR: Error registering Dhtxx\n"); + } + + return ret; +} + +#endif /* CONFIG_SENSORS_DHTXX */