From 0e9d155c763e52239a9320a28fd96993d47d8eef Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Sat, 20 Sep 2014 12:55:23 -0600 Subject: [PATCH] STM32F4Discovery: Add support for the microSD card slot on the STM32F4DIS-BB base board --- configs/stm3240g-eval/include/board.h | 4 +- configs/stm32f4discovery/README.txt | 28 +++ configs/stm32f4discovery/include/board.h | 41 ++++ configs/stm32f4discovery/netnsh/defconfig | 47 +++- configs/stm32f4discovery/src/Makefile | 6 +- configs/stm32f4discovery/src/stm32_boot.c | 10 +- configs/stm32f4discovery/src/stm32_bringup.c | 119 ++++++++++ configs/stm32f4discovery/src/stm32_nsh.c | 102 +------- configs/stm32f4discovery/src/stm32_sdio.c | 175 ++++++++++++++ .../stm32f4discovery/src/stm32f4discovery.h | 220 +++++++++++++----- 10 files changed, 580 insertions(+), 172 deletions(-) create mode 100644 configs/stm32f4discovery/src/stm32_bringup.c create mode 100644 configs/stm32f4discovery/src/stm32_sdio.c diff --git a/configs/stm3240g-eval/include/board.h b/configs/stm3240g-eval/include/board.h index 965a536870..a531c220c7 100644 --- a/configs/stm3240g-eval/include/board.h +++ b/configs/stm3240g-eval/include/board.h @@ -1,4 +1,4 @@ -/************************************************************************************ + * configs/stm3240g-eval/include/board.h * include/arch/board/board.h * @@ -444,7 +444,7 @@ /* DMA Channl/Stream Selections *****************************************************/ /* Stream selections are arbitrary for now but might become important in the future - * is we set aside more DMA channels/streams. + * if we set aside more DMA channels/streams. * * SDIO DMA * DMAMAP_SDIO_1 = Channel 4, Stream 3 diff --git a/configs/stm32f4discovery/README.txt b/configs/stm32f4discovery/README.txt index afddf10068..7d292b1dd3 100644 --- a/configs/stm32f4discovery/README.txt +++ b/configs/stm32f4discovery/README.txt @@ -1261,6 +1261,34 @@ Where is one of the following: and RJ5 network connector. Support is enabled for ICMP, TCP/IP, UDP, and ARP. + 3. SD card support is enabled. The STM32F4DIS-BB has an on-board + microSD slot that should be automatically registered as the block + device /dev/mmcsd0 when an SD card is present. The SD card can + then be mounted by the NSH command: + + nsh> mount -t /dev/mmcsd0 /mnt/sdcard + + 4. CCM memory is not included in the heap in this configuration. That + is because the SD card uses DMA and if DMA memory is allocated from + the CCM memory, the DMA will failure. This is an STM32 hardware + limitation. + + If you want to get the CCM memory back in the heap, then you can + + a) Disable microSD support (and DMAC2 which is then no longer + needed). If you reduce the clocking by a huge amount, it might + be possible to use microSD without DMA. This, however, may + not be possible. + b) Develop a strategy to manage CCM memory and DMA memory. Look + at this discussion on the NuttX Wiki: + http://www.nuttx.org/doku.php?id=wiki:howtos:stm32-ccm-alloc + + To put the CCM memory back into the heap you would need to change + the following in the NuttX configuration: + + CONFIG_STM32_CCMEXCLUDE=n : Don't exclude CCM memory from the heap + CONFIG_MM_REGIONS=2 : With CCM, there will be two memory regions + nsh: --- Configures the NuttShell (nsh) located at apps/examples/nsh. The diff --git a/configs/stm32f4discovery/include/board.h b/configs/stm32f4discovery/include/board.h index ba481d6191..069281fa1c 100644 --- a/configs/stm32f4discovery/include/board.h +++ b/configs/stm32f4discovery/include/board.h @@ -156,6 +156,36 @@ #define STM32_TIM18_FREQUENCY STM32_HCLK_FREQUENCY #define STM32_TIM27_FREQUENCY (STM32_HCLK_FREQUENCY/2) +/* SDIO dividers. Note that slower clocking is required when DMA is disabled + * in order to avoid RX overrun/TX underrun errors due to delayed responses + * to service FIFOs in interrupt driven mode. These values have not been + * tuned!!! + * + * SDIOCLK=48MHz, SDIO_CK=SDIOCLK/(118+2)=400 KHz + */ + +#define SDIO_INIT_CLKDIV (118 << SDIO_CLKCR_CLKDIV_SHIFT) + +/* DMA ON: SDIOCLK=48MHz, SDIO_CK=SDIOCLK/(1+2)=16 MHz + * DMA OFF: SDIOCLK=48MHz, SDIO_CK=SDIOCLK/(2+2)=12 MHz + */ + +#ifdef CONFIG_SDIO_DMA +# define SDIO_MMCXFR_CLKDIV (1 << SDIO_CLKCR_CLKDIV_SHIFT) +#else +# define SDIO_MMCXFR_CLKDIV (2 << SDIO_CLKCR_CLKDIV_SHIFT) +#endif + +/* DMA ON: SDIOCLK=48MHz, SDIO_CK=SDIOCLK/(1+2)=16 MHz + * DMA OFF: SDIOCLK=48MHz, SDIO_CK=SDIOCLK/(2+2)=12 MHz + */ + +#ifdef CONFIG_SDIO_DMA +# define SDIO_SDXFR_CLKDIV (1 << SDIO_CLKCR_CLKDIV_SHIFT) +#else +# define SDIO_SDXFR_CLKDIV (2 << SDIO_CLKCR_CLKDIV_SHIFT) +#endif + /* LED definitions ******************************************************************/ /* If CONFIG_ARCH_LEDS is not defined, then the user can control the LEDs in any * way. The following definitions are used to access individual LEDs. @@ -276,6 +306,17 @@ #endif +/* DMA Channl/Stream Selections *****************************************************/ +/* Stream selections are arbitrary for now but might become important in the future + * if we set aside more DMA channels/streams. + * + * SDIO DMA + * DMAMAP_SDIO_1 = Channel 4, Stream 3 + * DMAMAP_SDIO_2 = Channel 4, Stream 6 + */ + +#define DMAMAP_SDIO DMAMAP_SDIO_1 + /************************************************************************************ * Public Data ************************************************************************************/ diff --git a/configs/stm32f4discovery/netnsh/defconfig b/configs/stm32f4discovery/netnsh/defconfig index ec7d84f5af..00bdd5cb6d 100644 --- a/configs/stm32f4discovery/netnsh/defconfig +++ b/configs/stm32f4discovery/netnsh/defconfig @@ -121,6 +121,9 @@ CONFIG_ARMV7M_TOOLCHAIN_CODESOURCERYW=y # CONFIG_ARMV7M_TOOLCHAIN_GNU_EABIW is not set # CONFIG_ARMV7M_TOOLCHAIN_RAISONANCE is not set # CONFIG_SERIAL_TERMIOS is not set +CONFIG_SDIO_DMA=y +CONFIG_SDIO_DMAPRIO=0x00010000 +# CONFIG_SDIO_WIDTH_D1_ONLY is not set # CONFIG_NET_MULTICAST is not set # @@ -270,7 +273,7 @@ CONFIG_STM32_HAVE_ETHMAC=y # CONFIG_STM32_CRC is not set # CONFIG_STM32_CRYP is not set # CONFIG_STM32_DMA1 is not set -# CONFIG_STM32_DMA2 is not set +CONFIG_STM32_DMA2=y # CONFIG_STM32_DAC1 is not set # CONFIG_STM32_DAC2 is not set # CONFIG_STM32_DCMI is not set @@ -284,7 +287,7 @@ CONFIG_STM32_ETHMAC=y # CONFIG_STM32_OTGHS is not set CONFIG_STM32_PWR=y # CONFIG_STM32_RNG is not set -# CONFIG_STM32_SDIO is not set +CONFIG_STM32_SDIO=y CONFIG_STM32_SPI1=y # CONFIG_STM32_SPI2 is not set # CONFIG_STM32_SPI3 is not set @@ -324,13 +327,15 @@ CONFIG_STM32_JTAG_SW_ENABLE=y # CONFIG_STM32_DISABLE_IDLE_SLEEP_DURING_DEBUG is not set # CONFIG_STM32_FORCEPOWER is not set # CONFIG_ARCH_BOARD_STM32_CUSTOM_CLOCKCONFIG is not set -# CONFIG_STM32_CCMEXCLUDE is not set +CONFIG_STM32_CCMEXCLUDE=y +CONFIG_STM32_DMACAPABLE=y CONFIG_STM32_USART=y # # U[S]ART Configuration # # CONFIG_USART6_RS485 is not set +# CONFIG_USART6_RXDMA is not set # CONFIG_SERIAL_DISABLE_REORDERING is not set # CONFIG_STM32_USART_SINGLEWIRE is not set @@ -340,6 +345,10 @@ CONFIG_STM32_USART=y # CONFIG_STM32_SPI_INTERRUPTS is not set # CONFIG_STM32_SPI_DMA is not set +# +# SDIO Configuration +# + # # Ethernet MAC configuration # @@ -373,7 +382,7 @@ CONFIG_STM32_RMII_EXTCLK=y # # CONFIG_ARCH_NOINTC is not set # CONFIG_ARCH_VECNOTIRQ is not set -# CONFIG_ARCH_DMA is not set +CONFIG_ARCH_DMA=y CONFIG_ARCH_HAVE_IRQPRIO=y # CONFIG_ARCH_L2CACHE is not set # CONFIG_ARCH_HAVE_COHERENT_DCACHE is not set @@ -441,6 +450,7 @@ CONFIG_ARCH_BUTTONS=y CONFIG_ARCH_HAVE_IRQBUTTONS=y # CONFIG_ARCH_IRQBUTTONS is not set CONFIG_NSH_MMCSDMINOR=0 +CONFIG_NSH_MMCSDSLOTNO=0 # # Board-Specific Options @@ -576,7 +586,18 @@ CONFIG_SPI_EXCHANGE=y # CONFIG_BCH is not set # CONFIG_INPUT is not set # CONFIG_LCD is not set -# CONFIG_MMCSD is not set +CONFIG_MMCSD=y +CONFIG_MMCSD_NSLOTS=1 +# CONFIG_MMCSD_READONLY is not set +CONFIG_MMCSD_MULTIBLOCK_DISABLE=y +# CONFIG_MMCSD_MMCSUPPORT is not set +CONFIG_MMCSD_HAVECARDDETECT=y +# CONFIG_MMCSD_SPI is not set +CONFIG_ARCH_HAVE_SDIO=y +CONFIG_MMCSD_SDIO=y +CONFIG_SDIO_PREFLIGHT=y +# CONFIG_SDIO_MUXBUS is not set +# CONFIG_SDIO_BLOCKSETUP is not set # CONFIG_MTD is not set CONFIG_NETDEVICES=y @@ -783,10 +804,15 @@ CONFIG_NET_ETHERNET=y # CONFIG_DISABLE_MOUNTPOINT is not set # CONFIG_FS_AUTOMOUNTER is not set # CONFIG_DISABLE_PSEUDOFS_OPERATIONS is not set -# CONFIG_FS_READABLE is not set -# CONFIG_FS_WRITABLE is not set +CONFIG_FS_READABLE=y +CONFIG_FS_WRITABLE=y # CONFIG_FS_RAMMAP is not set -# CONFIG_FS_FAT is not set +CONFIG_FS_FAT=y +CONFIG_FAT_LCNAMES=y +CONFIG_FAT_LFN=y +CONFIG_FAT_MAXFNAME=32 +# CONFIG_FS_FATTIME is not set +# CONFIG_FAT_DMAMEMORY is not set # CONFIG_NFS is not set # CONFIG_FS_NXFFS is not set # CONFIG_FS_ROMFS is not set @@ -809,7 +835,7 @@ CONFIG_NET_ETHERNET=y # Memory Management # # CONFIG_MM_SMALL is not set -CONFIG_MM_REGIONS=2 +CONFIG_MM_REGIONS=1 # CONFIG_ARCH_HAVE_HEAP2 is not set # CONFIG_GRAN is not set @@ -1039,6 +1065,7 @@ CONFIG_NSH_BUILTIN_APPS=y # CONFIG_NSH_DISABLE_LS is not set # CONFIG_NSH_DISABLE_MB is not set # CONFIG_NSH_DISABLE_MKDIR is not set +# CONFIG_NSH_DISABLE_MKFATFS is not set # CONFIG_NSH_DISABLE_MKFIFO is not set # CONFIG_NSH_DISABLE_MKRD is not set # CONFIG_NSH_DISABLE_MH is not set @@ -1079,7 +1106,7 @@ CONFIG_NSH_FILEIOSIZE=512 # Console Configuration # CONFIG_NSH_CONSOLE=y -# CONFIG_NSH_ARCHINIT is not set +CONFIG_NSH_ARCHINIT=y # # Networking Configuration diff --git a/configs/stm32f4discovery/src/Makefile b/configs/stm32f4discovery/src/Makefile index e43601dbc3..f015866ff0 100644 --- a/configs/stm32f4discovery/src/Makefile +++ b/configs/stm32f4discovery/src/Makefile @@ -40,7 +40,7 @@ CFLAGS += -I$(TOPDIR)/sched ASRCS = AOBJS = $(ASRCS:.S=$(OBJEXT)) -CSRCS = stm32_boot.c stm32_spi.c +CSRCS = stm32_boot.c stm32_bringup.c stm32_spi.c ifeq ($(CONFIG_HAVE_CXX),y) CSRCS += stm32_cxxinitialize.c @@ -60,6 +60,10 @@ ifeq ($(CONFIG_STM32_OTGFS),y) CSRCS += stm32_usb.c endif +ifeq ($(CONFIG_STM32_SDIO),y) +CSRCS += stm32_sdio.c +endif + ifeq ($(CONFIG_PWM),y) CSRCS += stm32_pwm.c endif diff --git a/configs/stm32f4discovery/src/stm32_boot.c b/configs/stm32f4discovery/src/stm32_boot.c index b28713cf45..1a8e33e09a 100644 --- a/configs/stm32f4discovery/src/stm32_boot.c +++ b/configs/stm32f4discovery/src/stm32_boot.c @@ -117,14 +117,8 @@ void stm32_boardinitialize(void) #ifdef CONFIG_BOARD_INITIALIZE void board_initialize(void) { - /* Perform NSH initialization here instead of from the NSH. This - * alternative NSH initialization is necessary when NSH is ran in user-space - * but the initialization function must run in kernel space. - */ + /* Perform board-specific initialization */ -#if defined(CONFIG_NSH_LIBRARY) && !defined(CONFIG_NSH_ARCHINIT) - (void)nsh_archinitialize(); -#endif + (void)stm32_bringup(); } #endif - diff --git a/configs/stm32f4discovery/src/stm32_bringup.c b/configs/stm32f4discovery/src/stm32_bringup.c new file mode 100644 index 0000000000..0b7f7adb1e --- /dev/null +++ b/configs/stm32f4discovery/src/stm32_bringup.c @@ -0,0 +1,119 @@ +/**************************************************************************** + * config/stm32f4discovery/src/stm32_bringup.c + * + * Copyright (C) 2012, 2014 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#ifdef CONFIG_SYSTEM_USBMONITOR +# include +#endif + +#ifdef CONFIG_STM32_OTGFS +# include "stm32_usbhost.h" +#endif + +#include "stm32.h" +#include "stm32f4discovery.h" + +/**************************************************************************** + * Pre-Processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: stm32_bringup + * + * Description: + * Perform architecture-specific initialization + * + * CONFIG_BOARD_INITIALIZE=y : + * Called from board_initialize(). + * + * CONFIG_BOARD_INITIALIZE=n && CONFIG_NSH_ARCHINIT=y : + * Called from the NSH library + * + ****************************************************************************/ + +int stm32_bringup(void) +{ + int ret = OK; + +#ifdef HAVE_SDIO + /* Initialize the SDIO block driver */ + + ret = stm32_sdio_initialize(); + if (ret != OK) + { + fdbg("Failed to initialize MMC/SD driver: %d\n", ret); + return ret; + } +#endif + +#ifdef HAVE_USBHOST + /* Initialize USB host operation. stm32_usbhost_initialize() starts a thread + * will monitor for USB connection and disconnection events. + */ + + ret = stm32_usbhost_initialize(); + if (ret != OK) + { + udbg("Failed to initialize USB host: %d\n", ret); + return ret; + } +#endif + +#ifdef HAVE_USBMONITOR + /* Start the USB Monitor */ + + ret = usbmonitor_start(0, NULL); + if (ret != OK) + { + udbg("Start USB monitor: %d\n", ret); + } +#endif + + return ret; +} diff --git a/configs/stm32f4discovery/src/stm32_nsh.c b/configs/stm32f4discovery/src/stm32_nsh.c index 296615da8b..3692071bb0 100644 --- a/configs/stm32f4discovery/src/stm32_nsh.c +++ b/configs/stm32f4discovery/src/stm32_nsh.c @@ -1,7 +1,7 @@ /**************************************************************************** * config/stm32f4discovery/src/stm32_nsh.c * - * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Copyright (C) 2012, 2014 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -39,73 +39,14 @@ #include -#include -#include -#include -#include - -#ifdef CONFIG_SYSTEM_USBMONITOR -# include -#endif - -#ifdef CONFIG_STM32_OTGFS -# include "stm32_usbhost.h" -#endif - -#include "stm32.h" #include "stm32f4discovery.h" /**************************************************************************** * Pre-Processor Definitions ****************************************************************************/ -/* Configuration ************************************************************/ - -#define HAVE_USBDEV 1 -#define HAVE_USBHOST 1 -#define HAVE_USBMONITOR 1 - -/* Can't support USB host or device features if USB OTG FS is not enabled */ - -#ifndef CONFIG_STM32_OTGFS -# undef HAVE_USBDEV -# undef HAVE_USBHOST -# undef HAVE_USBMONITOR -#endif - -/* Can't support USB device monitor if USB device is not enabled */ - -#ifndef CONFIG_USBDEV -# undef HAVE_USBDEV -# undef HAVE_USBMONITOR -#endif - -/* Can't support USB host is USB host is not enabled */ - -#ifndef CONFIG_USBHOST -# undef HAVE_USBHOST -#endif - -/* Check if we should enable the USB monitor before starting NSH */ - -#if !defined(CONFIG_USBDEV_TRACE) || !defined(CONFIG_SYSTEM_USBMONITOR) -# undef HAVE_USBMONITOR -#endif - -/* Debug ********************************************************************/ - -#ifdef CONFIG_CPP_HAVE_VARARGS -# if defined(CONFIG_DEBUG) || !defined(CONFIG_NSH_ARCHINIT) -# define message(...) lowsyslog(__VA_ARGS__) -# else -# define message(...) printf(__VA_ARGS__) -# endif -#else -# if defined(CONFIG_DEBUG) || !defined(CONFIG_NSH_ARCHINIT) -# define message lowsyslog -# else -# define message printf -# endif +#ifndef OK +# define OK 0 #endif /**************************************************************************** @@ -116,44 +57,17 @@ * Name: nsh_archinitialize * * Description: - * Perform architecture specific initialization - * - * CONFIG_NSH_ARCHINIT=y : - * Called from the NSH library - * - * CONFIG_BOARD_INITIALIZE=y, CONFIG_NSH_LIBRARY=y, && - * CONFIG_NSH_ARCHINIT=n : - * Called from board_initialize(). + * Perform architecture-specific initialization (if this was not already + * done by board_initialize(); * ****************************************************************************/ int nsh_archinitialize(void) { -#if defined(HAVE_USBHOST) || defined(HAVE_USBMONITOR) - int ret; -#endif +#ifndef CONFIG_BOARD_INITIALIZE + /* Perform board-specific initialization */ -#ifdef HAVE_USBHOST - /* Initialize USB host operation. stm32_usbhost_initialize() starts a thread - * will monitor for USB connection and disconnection events. - */ - - ret = stm32_usbhost_initialize(); - if (ret != OK) - { - message("nsh_archinitialize: Failed to initialize USB host: %d\n", ret); - return ret; - } -#endif - -#ifdef HAVE_USBMONITOR - /* Start the USB Monitor */ - - ret = usbmonitor_start(0, NULL); - if (ret != OK) - { - message("nsh_archinitialize: Start USB monitor: %d\n", ret); - } + (void)stm32_bringup(); #endif return OK; diff --git a/configs/stm32f4discovery/src/stm32_sdio.c b/configs/stm32f4discovery/src/stm32_sdio.c new file mode 100644 index 0000000000..4148caf795 --- /dev/null +++ b/configs/stm32f4discovery/src/stm32_sdio.c @@ -0,0 +1,175 @@ +/**************************************************************************** + * config/stm32f4discovery/src/stm32_sdio.c + * + * Copyright (C) 2014 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 "stm32f4discovery.h" + +#ifdef HAVE_SDIO + +/**************************************************************************** + * Pre-Processor Definitions + ****************************************************************************/ +/* Configuration ************************************************************/ + +/* Card detections requires card support and a card detection GPIO */ + +#define HAVE_NCD 1 +#if !defined(HAVE_SDIO) || !defined(GPIO_SDIO_NCD) +# undef HAVE_NCD +#endif + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static FAR struct sdio_dev_s *g_sdio_dev; +#ifdef HAVE_NCD +static bool g_sd_inserted = 0xff; /* Impossible value */ +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: stm32_ncd_interrupt + * + * Description: + * Card detect interrupt handler. + * + ****************************************************************************/ + +#ifdef HAVE_NCD +static int stm32_ncd_interrupt(int irq, FAR void *context) +{ + bool present; + + present = !stm32_gpioread(GPIO_SDIO_NCD); + if (present != g_sd_inserted) + { + sdio_mediachange(g_sdio_dev, present); + g_sd_inserted = present; + } + + return OK; +} +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: stm32_sdio_initialize + * + * Description: + * Initialize SDIO-based MMC/SD card support + * + ****************************************************************************/ + +int stm32_sdio_initialize(void) +{ + int ret; + +#ifdef HAVE_NCD + /* Card detect */ + + bool cd_status; + + /* Configure the card detect GPIO */ + + stm32_configgpio(GPIO_SDIO_NCD); + + /* Register an interrupt handler for the card detect pin */ + + stm32_gpiosetevent(GPIO_SDIO_NCD, true, true, true, stm32_ncd_interrupt); +#endif + + /* Mount the SDIO-based MMC/SD block driver */ + /* First, get an instance of the SDIO interface */ + + fvdbg("Initializing SDIO slot %d\n", SDIO_SLOTNO); + + g_sdio_dev = sdio_initialize(SDIO_SLOTNO); + if (!g_sdio_dev) + { + fdbg("Failed to initialize SDIO slot %d\n", SDIO_SLOTNO); + return -ENODEV; + } + + /* Now bind the SDIO interface to the MMC/SD driver */ + + fvdbg("Bind SDIO to the MMC/SD driver, minor=%d\n", SDIO_MINOR); + + ret = mmcsd_slotinitialize(SDIO_MINOR, g_sdio_dev); + if (ret != OK) + { + fdbg("Failed to bind SDIO to the MMC/SD driver: %d\n", ret); + return ret; + } + + fvdbg("Successfully bound SDIO to the MMC/SD driver\n"); + +#ifdef HAVE_NCD + /* Use SD card detect pin to check if a card is g_sd_inserted */ + + cd_status = !stm32_gpioread(GPIO_SDIO_NCD); + fvdbg("Card detect : %d\n", cd_status); + + sdio_mediachange(g_sdio_dev, cd_status); +#else + /* Assume that the SD card is inserted. What choice do we have? */ + + sdio_mediachange(g_sdio_dev, true); +#endif + + return OK; +} + +#endif /* HAVE_SDIO */ diff --git a/configs/stm32f4discovery/src/stm32f4discovery.h b/configs/stm32f4discovery/src/stm32f4discovery.h index dc0a17b6ac..0992f1f4d3 100644 --- a/configs/stm32f4discovery/src/stm32f4discovery.h +++ b/configs/stm32f4discovery/src/stm32f4discovery.h @@ -1,4 +1,4 @@ -/**************************************************************************************************** +/**************************************************************************** * configs/stm32f4discovery/src/stm32f4discovery.h * * Copyright (C) 2011-2012 Gregory Nutt. All rights reserved. @@ -31,23 +31,23 @@ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - ****************************************************************************************************/ + ****************************************************************************/ #ifndef __CONFIGS_STM32F4DISCOVERY_SRC_STM32F4DISCOVERY_H #define __CONFIGS_STM32F4DISCOVERY_SRC_STM32F4DISCOVERY_H -/**************************************************************************************************** +/**************************************************************************** * Included Files - ****************************************************************************************************/ + ****************************************************************************/ #include #include #include -/**************************************************************************************************** - * Definitions - ****************************************************************************************************/ -/* Configuration ************************************************************************************/ +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ +/* Configuration *************************************************************/ /* How many SPI modules does this chip support? */ #if STM32_NSPI < 1 @@ -61,7 +61,79 @@ # undef CONFIG_STM32_SPI3 #endif -/* STM32F4 Discovery GPIOs **************************************************************************/ +/* Assume that we have everything */ + +#define HAVE_USBDEV 1 +#define HAVE_USBHOST 1 +#define HAVE_USBMONITOR 1 +#define HAVE_SDIO 1 + +/* Can't support USB host or device features if USB OTG FS is not enabled */ + +#ifndef CONFIG_STM32_OTGFS +# undef HAVE_USBDEV +# undef HAVE_USBHOST +# undef HAVE_USBMONITOR +#endif + +/* Can't support USB device monitor if USB device is not enabled */ + +#ifndef CONFIG_USBDEV +# undef HAVE_USBDEV +# undef HAVE_USBMONITOR +#endif + +/* Can't support USB host is USB host is not enabled */ + +#ifndef CONFIG_USBHOST +# undef HAVE_USBHOST +#endif + +/* Check if we should enable the USB monitor before starting NSH */ + +#if !defined(CONFIG_USBDEV_TRACE) || !defined(CONFIG_SYSTEM_USBMONITOR) +# undef HAVE_USBMONITOR +#endif + +/* Can't support MMC/SD features if mountpoints are disabled or if SDIO support + * is not enabled. + */ + +#if defined(CONFIG_DISABLE_MOUNTPOINT) || !defined(CONFIG_STM32_SDIO) +# undef HAVE_SDIO +#endif + +#undef SDIO_MINOR /* Any minor number, default 0 */ +#define SDIO_SLOTNO 0 /* Only one slot */ + +#ifdef HAVE_SDIO + +# if defined(CONFIG_NSH_MMCSDSLOTNO) && CONFIG_NSH_MMCSDSLOTNO != 0 +# warning "Only one MMC/SD slot, slot 0" +# undef CONFIG_NSH_MMCSDSLOTNO +# define CONFIG_NSH_MMCSDSLOTNO SDIO_SLOTNO +# endif + +# if defined(CONFIG_NSH_MMCSDMINOR) +# define SDIO_MINOR CONFIG_NSH_MMCSDMINOR +# else +# define SDIO_MINOR 0 +# endif + + /* SD card bringup does not work if performed on the IDLE thread because it + * will cause waiting. Use either: + * + * CONFIG_NSH_ARCHINIT=y, OR + * CONFIG_BOARD_INITIALIZE=y && CONFIG_BOARD_INITTHREAD=y + */ + +# if defined(CONFIG_BOARD_INITIALIZE) && !defined(CONFIG_BOARD_INITTHREAD) +# warning "SDIO initialization cannot be perfomed on the IDLE thread" +# undef HAVE_SDIO +# endif +#endif + +/* STM32F4 Discovery GPIOs **************************************************/ /* LEDs */ #define GPIO_LED1 (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_50MHz|\ @@ -92,8 +164,8 @@ /* SPI chip selects */ -#define GPIO_CS_MEMS (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_50MHz|\ - GPIO_OUTPUT_SET|GPIO_PORTE|GPIO_PIN3) +#define GPIO_CS_MEMS (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_50MHz|\ + GPIO_OUTPUT_SET|GPIO_PORTE|GPIO_PIN3) /* USB OTG FS * @@ -102,14 +174,19 @@ * PD5 OTG_FS_Overcurrent */ -#define GPIO_OTGFS_VBUS (GPIO_INPUT|GPIO_FLOAT|GPIO_SPEED_100MHz|GPIO_OPENDRAIN|GPIO_PORTA|GPIO_PIN9) -#define GPIO_OTGFS_PWRON (GPIO_OUTPUT|GPIO_FLOAT|GPIO_SPEED_100MHz|GPIO_PUSHPULL|GPIO_PORTC|GPIO_PIN0) +#define GPIO_OTGFS_VBUS (GPIO_INPUT|GPIO_FLOAT|GPIO_SPEED_100MHz|\ + GPIO_OPENDRAIN|GPIO_PORTA|GPIO_PIN9) +#define GPIO_OTGFS_PWRON (GPIO_OUTPUT|GPIO_FLOAT|GPIO_SPEED_100MHz|\ + GPIO_PUSHPULL|GPIO_PORTC|GPIO_PIN0) #ifdef CONFIG_USBHOST -# define GPIO_OTGFS_OVER (GPIO_INPUT|GPIO_EXTI|GPIO_FLOAT|GPIO_SPEED_100MHz|GPIO_PUSHPULL|GPIO_PORTD|GPIO_PIN5) +# define GPIO_OTGFS_OVER (GPIO_INPUT|GPIO_EXTI|GPIO_FLOAT|\ + GPIO_SPEED_100MHz|GPIO_PUSHPULL|\ + GPIO_PORTD|GPIO_PIN5) #else -# define GPIO_OTGFS_OVER (GPIO_INPUT|GPIO_FLOAT|GPIO_SPEED_100MHz|GPIO_PUSHPULL|GPIO_PORTD|GPIO_PIN5) +# define GPIO_OTGFS_OVER (GPIO_INPUT|GPIO_FLOAT|GPIO_SPEED_100MHz|\ + GPIO_PUSHPULL|GPIO_PORTD|GPIO_PIN5) #endif /* UG-2864AMBAG01 or UG-2864HSWEG01 OLED Display (SPI 4-wire): @@ -160,161 +237,190 @@ */ #if defined(CONFIG_STM32F4DISBB) && defined(CONFIG_STM32_SDIO) -# define GPIO_SDIO_NCD (GPIO_INPUT|GPIO_FLOAT|GPIO_EXTI|GPIO_PORTB|GPIO_PIN15) +# define GPIO_SDIO_NCD (GPIO_INPUT|GPIO_FLOAT|GPIO_EXTI|\ + GPIO_PORTB|GPIO_PIN15) #endif -/**************************************************************************************************** +/**************************************************************************** * Public Types - ****************************************************************************************************/ + ****************************************************************************/ -/**************************************************************************************************** +/**************************************************************************** * Public data - ****************************************************************************************************/ + ****************************************************************************/ #ifndef __ASSEMBLY__ -/**************************************************************************************************** +/**************************************************************************** * Public Functions - ****************************************************************************************************/ + ****************************************************************************/ -/**************************************************************************************************** +/**************************************************************************** * Name: stm32_spiinitialize * * Description: - * Called to configure SPI chip select GPIO pins for the stm32f4discovery board. + * Called to configure SPI chip select GPIO pins for the stm32f4discovery + * board. * - ****************************************************************************************************/ + ****************************************************************************/ void weak_function stm32_spiinitialize(void); -/**************************************************************************************************** +/**************************************************************************** + * Name: stm32_bringup + * + * Description: + * Perform architecture-specific initialization + * + * CONFIG_BOARD_INITIALIZE=y : + * Called from board_initialize(). + * + * CONFIG_BOARD_INITIALIZE=y && CONFIG_NSH_ARCHINIT=y : + * Called from the NSH library + * + ****************************************************************************/ + +int stm32_bringup(void); + +/**************************************************************************** * Name: stm32_usbinitialize * * Description: - * Called from stm32_usbinitialize very early in inialization to setup USB-related - * GPIO pins for the STM32F4Discovery board. + * Called from stm32_usbinitialize very early in initialization to setup + * USB-related GPIO pins for the STM32F4Discovery board. * - ****************************************************************************************************/ + ****************************************************************************/ #ifdef CONFIG_STM32_OTGFS void weak_function stm32_usbinitialize(void); #endif -/**************************************************************************************************** +/**************************************************************************** * Name: stm32_usbhost_initialize * * Description: - * Called at application startup time to initialize the USB host functionality. This function will - * start a thread that will monitor for device connection/disconnection events. + * Called at application startup time to initialize the USB host + * functionality. This function will start a thread that will monitor for + * device connection/disconnection events. * - ****************************************************************************************************/ + ****************************************************************************/ #if defined(CONFIG_STM32_OTGFS) && defined(CONFIG_USBHOST) int stm32_usbhost_initialize(void); #endif -/**************************************************************************************************** +/**************************************************************************** * Name: stm32_extmemgpios * * Description: * Initialize GPIOs for external memory usage * - ****************************************************************************************************/ + ****************************************************************************/ #ifdef CONFIG_STM32_FSMC void stm32_extmemgpios(const uint32_t *gpios, int ngpios); #endif -/**************************************************************************************************** +/**************************************************************************** * Name: stm32_extmemaddr * * Description: * Initialize adress line GPIOs for external memory access * - ****************************************************************************************************/ + ****************************************************************************/ #ifdef CONFIG_STM32_FSMC void stm32_extmemaddr(int naddrs); #endif -/**************************************************************************************************** +/**************************************************************************** * Name: stm32_extmemdata * * Description: * Initialize data line GPIOs for external memory access * - ****************************************************************************************************/ + ****************************************************************************/ #ifdef CONFIG_STM32_FSMC void stm32_extmemdata(int ndata); #endif -/**************************************************************************************************** +/**************************************************************************** * Name: stm32_enablefsmc * * Description: * enable clocking to the FSMC module * - ****************************************************************************************************/ + ****************************************************************************/ #ifdef CONFIG_STM32_FSMC void stm32_enablefsmc(void); #endif -/**************************************************************************************************** +/**************************************************************************** * Name: stm32_disablefsmc * * Description: * enable clocking to the FSMC module * - ****************************************************************************************************/ + ****************************************************************************/ #ifdef CONFIG_STM32_FSMC void stm32_disablefsmc(void); #endif -/**************************************************************************************************** +/**************************************************************************** * Name: stm32_led_pminitialize * * Description: - * Enable logic to use the LEDs on the STM32F4Discovery to support power management testing + * Enable logic to use the LEDs on the STM32F4Discovery to support power + * management testing * - ****************************************************************************************************/ + ****************************************************************************/ #ifdef CONFIG_PM void stm32_led_pminitialize(void); #endif -/**************************************************************************************************** +/**************************************************************************** * Name: stm32_pm_buttons * * Description: * Configure the user button of the STM32f4discovery board as EXTI, * so it is able to wakeup the MCU from the PM_STANDBY mode * - ****************************************************************************************************/ + ****************************************************************************/ #if defined(CONFIG_PM) && defined(CONFIG_ARCH_IDLE_CUSTOM) && defined(CONFIG_PM_BUTTONS) void stm32_pm_buttons(void); #endif /**************************************************************************** - * Name: nsh_archinitialize + * Name: stm32_bringup * * Description: - * Perform architecture specific initialization for NSH. + * Perform architecture-specific initialization * - * CONFIG_NSH_ARCHINIT=y : - * Called from the NSH library - * - * CONFIG_BOARD_INITIALIZE=y, CONFIG_NSH_LIBRARY=y, && - * CONFIG_NSH_ARCHINIT=n : + * CONFIG_BOARD_INITIALIZE=y : * Called from board_initialize(). * + * CONFIG_BOARD_INITIALIZE=n && CONFIG_NSH_ARCHINIT=y : + * Called from the NSH library + * ****************************************************************************/ -#ifdef CONFIG_NSH_LIBRARY -int nsh_archinitialize(void); +int stm32_bringup(void); + +/**************************************************************************** + * Name: stm32_sdio_initialize + * + * Description: + * Initialize SDIO-based MMC/SD card support + * + ****************************************************************************/ + +#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_STM32_SDIO) +int stm32_sdio_initialize(void); #endif #endif /* __ASSEMBLY__ */