From 7621b0858592e32bdaff7c5b4df870d68390ed07 Mon Sep 17 00:00:00 2001 From: wgzAIIT <820906721@qq.com> Date: Thu, 7 Apr 2022 15:08:27 +0800 Subject: [PATCH] support auto mount sd card on nuttx for imxrt --- .../Applications/sensor_app/pm10_0_ps5308.c | 2 +- .../Applications/sensor_app/pm2_5_ps5308.c | 2 +- .../aiit_board/xidatong/Kconfig | 29 ++ .../xidatong/configs/sdionsh/defconfig | 3 +- .../aiit_board/xidatong/include/board.h | 3 +- .../aiit_board/xidatong/src/Makefile | 4 + .../aiit_board/xidatong/src/imxrt_bringup.c | 7 + .../xidatong/src/imxrt_sdhc_automount.c | 330 ++++++++++++++++++ .../aiit_board/xidatong/src/xidatong.h | 13 + 9 files changed, 388 insertions(+), 5 deletions(-) create mode 100644 Ubiquitous/Nuttx_Fusion_XiUOS/aiit_board/xidatong/src/imxrt_sdhc_automount.c diff --git a/APP_Framework/Applications/sensor_app/pm10_0_ps5308.c b/APP_Framework/Applications/sensor_app/pm10_0_ps5308.c index 7b1559d16..5d6408808 100644 --- a/APP_Framework/Applications/sensor_app/pm10_0_ps5308.c +++ b/APP_Framework/Applications/sensor_app/pm10_0_ps5308.c @@ -11,7 +11,7 @@ */ /** - * @filepm10_0_ps5308 + * @file pm10_0_ps5308.c * @brief PS5308 PM10.0 example * @version 1.0 * @author AIIT XUOS Lab diff --git a/APP_Framework/Applications/sensor_app/pm2_5_ps5308.c b/APP_Framework/Applications/sensor_app/pm2_5_ps5308.c index a2de9ee31..23aaca509 100644 --- a/APP_Framework/Applications/sensor_app/pm2_5_ps5308.c +++ b/APP_Framework/Applications/sensor_app/pm2_5_ps5308.c @@ -11,7 +11,7 @@ */ /** - * @file pm2_5_ps5308 + * @file pm2_5_ps5308.c * @brief PS5308 PM2.5 example * @version 1.0 * @author AIIT XUOS Lab diff --git a/Ubiquitous/Nuttx_Fusion_XiUOS/aiit_board/xidatong/Kconfig b/Ubiquitous/Nuttx_Fusion_XiUOS/aiit_board/xidatong/Kconfig index 27b484b4c..725198eb1 100644 --- a/Ubiquitous/Nuttx_Fusion_XiUOS/aiit_board/xidatong/Kconfig +++ b/Ubiquitous/Nuttx_Fusion_XiUOS/aiit_board/xidatong/Kconfig @@ -24,4 +24,33 @@ config XIDATONG_SDRAM ---help--- Activate DCD configuration of SDRAM +config XIDATONG_SDHC_AUTOMOUNT + bool "SDHC automounter" + default n + depends on FS_AUTOMOUNTER && IMXRT_USDHC + +if XIDATONG_SDHC_AUTOMOUNT + +config XIDATONG_SDHC_AUTOMOUNT_FSTYPE + string "SDHC file system type" + default "vfat" + +config XIDATONG_SDHC_AUTOMOUNT_BLKDEV + string "SDHC block device" + default "/dev/mmcsd0" + +config XIDATONG_SDHC_AUTOMOUNT_MOUNTPOINT + string "SDHC mount point" + default "/mnt/sdcard" + +config XIDATONG_SDHC_AUTOMOUNT_DDELAY + int "SDHC debounce delay (milliseconds)" + default 1000 + +config XIDATONG_SDHC_AUTOMOUNT_UDELAY + int "SDHC unmount retry delay (milliseconds)" + default 2000 + +endif # XIDATONG_SDHC_AUTOMOUNT + endif diff --git a/Ubiquitous/Nuttx_Fusion_XiUOS/aiit_board/xidatong/configs/sdionsh/defconfig b/Ubiquitous/Nuttx_Fusion_XiUOS/aiit_board/xidatong/configs/sdionsh/defconfig index e961fdc9a..2ea5dc4a0 100644 --- a/Ubiquitous/Nuttx_Fusion_XiUOS/aiit_board/xidatong/configs/sdionsh/defconfig +++ b/Ubiquitous/Nuttx_Fusion_XiUOS/aiit_board/xidatong/configs/sdionsh/defconfig @@ -27,7 +27,7 @@ CONFIG_FS_FAT=y CONFIG_FS_PROCFS=y CONFIG_IDLETHREAD_STACKSIZE=2048 CONFIG_EXAMPLES_HELLO=y -CONFIG_IMXRT_GPIO1_0_15_IRQ=y +CONFIG_IMXRT_GPIO2_16_31_IRQ=y CONFIG_IMXRT_GPIO_IRQ=y CONFIG_IMXRT_LPUART1=y CONFIG_IMXRT_USDHC1=y @@ -61,5 +61,6 @@ CONFIG_START_DAY=14 CONFIG_START_MONTH=3 CONFIG_SYSTEM_CLE_CMD_HISTORY=y CONFIG_SYSTEM_COLOR_CLE=y +CONFIG_FS_AUTOMOUNTER=y CONFIG_SYSTEM_NSH=y CONFIG_USER_ENTRYPOINT="nsh_main" diff --git a/Ubiquitous/Nuttx_Fusion_XiUOS/aiit_board/xidatong/include/board.h b/Ubiquitous/Nuttx_Fusion_XiUOS/aiit_board/xidatong/include/board.h index 49181683a..46d73d22a 100644 --- a/Ubiquitous/Nuttx_Fusion_XiUOS/aiit_board/xidatong/include/board.h +++ b/Ubiquitous/Nuttx_Fusion_XiUOS/aiit_board/xidatong/include/board.h @@ -215,9 +215,8 @@ #define PIN_USDHC1_D3 (GPIO_USDHC1_DATA3_1 | IOMUX_USDHC1_DATAX_DEFAULT) /* GPIO_SD_B0_05 */ #define PIN_USDHC1_DCLK (GPIO_USDHC1_CLK_1 | IOMUX_USDHC1_CLK_DEFAULT) /* GPIO_SD_B0_01 */ #define PIN_USDHC1_CMD (GPIO_USDHC1_CMD_1 | IOMUX_USDHC1_CMD_DEFAULT) /* GPIO_SD_B0_00 */ -//#define PIN_USDHC1_CD (GPIO_USDHC1_CD_2 | IOMUX_USDHC1_CLK_DEFAULT) -#define PIN_USDHC1_CD_GPIO (IOMUX_VSD_DEFAULT | GPIO_PORT2 | GPIO_PIN28) /* GPIO_B1_12 */ +#define PIN_USDHC1_CD (GPIO_USDHC1_CD_2 | IOMUX_USDHC1_CLK_DEFAULT) /* GPIO_B1_12 */ /* 386 KHz for initial inquiry stuff */ diff --git a/Ubiquitous/Nuttx_Fusion_XiUOS/aiit_board/xidatong/src/Makefile b/Ubiquitous/Nuttx_Fusion_XiUOS/aiit_board/xidatong/src/Makefile index 053ed09c8..9a45eccc2 100644 --- a/Ubiquitous/Nuttx_Fusion_XiUOS/aiit_board/xidatong/src/Makefile +++ b/Ubiquitous/Nuttx_Fusion_XiUOS/aiit_board/xidatong/src/Makefile @@ -70,4 +70,8 @@ ifeq ($(CONFIG_USBHOST),y) CSRCS += imxrt_usbhost.c endif +ifeq ($(CONFIG_XIDATONG_SDHC_AUTOMOUNT),y) +CSRCS += imxrt_sdhc_automount.c +endif + include $(TOPDIR)/boards/Board.mk diff --git a/Ubiquitous/Nuttx_Fusion_XiUOS/aiit_board/xidatong/src/imxrt_bringup.c b/Ubiquitous/Nuttx_Fusion_XiUOS/aiit_board/xidatong/src/imxrt_bringup.c index d6879f0cc..9b42db76b 100644 --- a/Ubiquitous/Nuttx_Fusion_XiUOS/aiit_board/xidatong/src/imxrt_bringup.c +++ b/Ubiquitous/Nuttx_Fusion_XiUOS/aiit_board/xidatong/src/imxrt_bringup.c @@ -118,6 +118,13 @@ static int nsh_sdmmc_initialize(void) "ERROR: Failed to bind SDIO to the MMC/SD driver: %d\n", ret); } + +#ifdef CONFIG_XIDATONG_SDHC_AUTOMOUNT + imxrt_automount_initialize(); + imxrt_usdhc_set_sdio_card_isr(sdmmc, imxrt_sdhc_automount_event, NULL); +#else + imxrt_usdhc_set_sdio_card_isr(sdmmc, NULL, NULL); +#endif } return OK; diff --git a/Ubiquitous/Nuttx_Fusion_XiUOS/aiit_board/xidatong/src/imxrt_sdhc_automount.c b/Ubiquitous/Nuttx_Fusion_XiUOS/aiit_board/xidatong/src/imxrt_sdhc_automount.c new file mode 100644 index 000000000..18612e7ae --- /dev/null +++ b/Ubiquitous/Nuttx_Fusion_XiUOS/aiit_board/xidatong/src/imxrt_sdhc_automount.c @@ -0,0 +1,330 @@ +/* +* Copyright (c) 2020 AIIT XUOS Lab +* XiOS is licensed under Mulan PSL v2. +* You can use this software according to the terms and conditions of the Mulan PSL v2. +* You may obtain a copy of Mulan PSL v2 at: +* http://license.coscl.org.cn/MulanPSL2 +* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +* See the Mulan PSL v2 for more details. +*/ + +/** + * @file imxrt_sdhc_automount.c + * @brief imxrt board sd card automount + * @version 1.0 + * @author AIIT XUOS Lab + * @date 2022.04.07 + */ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#if defined(CONFIG_FS_AUTOMOUNTER_DEBUG) && !defined(CONFIG_DEBUG_FS) +# define CONFIG_DEBUG_FS 1 +#endif + +#include +#include + +#include +#include +#include +#include "hardware/imxrt_pinmux.h" +#include "xidatong.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifndef NULL +# define NULL (FAR void *)0 +#endif + +#ifndef OK +# define OK 0 +#endif + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/* This structure represents the changeable state of the automounter */ + +struct imxrt_automount_state_s +{ + volatile automount_handler_t handler; /* Upper half handler */ + FAR void *arg; /* Handler argument */ + bool enable; /* Fake interrupt enable */ + bool pending; /* Set if there an event while disabled */ +}; + +/* This structure represents the static configuration of an automounter */ + +struct imxrt_automount_config_s +{ + /* This must be first thing in structure so that we can simply cast from + * struct automount_lower_s to struct imxrt_automount_config_s + */ + + struct automount_lower_s lower; /* Publicly visible part */ + FAR struct imxrt_automount_state_s *state; /* Changeable state */ +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int imxrt_sdhc_attach(FAR const struct automount_lower_s *lower, + automount_handler_t isr, FAR void *arg); +static void imxrt_sdhc_enable(FAR const struct automount_lower_s *lower, + bool enable); +static bool imxrt_sdhc_inserted(FAR const struct automount_lower_s *lower); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static struct imxrt_automount_state_s g_sdhc_state; +static const struct imxrt_automount_config_s g_sdhc_config = +{ + .lower = + { + .fstype = CONFIG_XIDATONG_SDHC_AUTOMOUNT_FSTYPE, + .blockdev = CONFIG_XIDATONG_SDHC_AUTOMOUNT_BLKDEV, + .mountpoint = CONFIG_XIDATONG_SDHC_AUTOMOUNT_MOUNTPOINT, + .ddelay = MSEC2TICK(CONFIG_XIDATONG_SDHC_AUTOMOUNT_DDELAY), + .udelay = MSEC2TICK(CONFIG_XIDATONG_SDHC_AUTOMOUNT_UDELAY), + .attach = imxrt_sdhc_attach, + .enable = imxrt_sdhc_enable, + .inserted = imxrt_sdhc_inserted + }, + .state = &g_sdhc_state +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: imxrt_sdhc_attach + * + * Description: + * Attach a new SDHC event handler + * + * Input Parameters: + * lower - An instance of the auto-mounter lower half state structure + * isr - The new event handler to be attach + * arg - Client data to be provided when the event handler is invoked. + * + * Returned Value: + * Always returns OK + * + ****************************************************************************/ + +static int imxrt_sdhc_attach(FAR const struct automount_lower_s *lower, + automount_handler_t isr, FAR void *arg) +{ + FAR const struct imxrt_automount_config_s *config; + FAR struct imxrt_automount_state_s *state; + + /* Recover references to our structure */ + + config = (FAR struct imxrt_automount_config_s *)lower; + DEBUGASSERT(config != NULL && config->state != NULL); + + state = config->state; + + /* Save the new handler info (clearing the handler first to eliminate race + * conditions). + */ + + state->handler = NULL; + state->pending = false; + state->arg = arg; + state->handler = isr; + return OK; +} + +/**************************************************************************** + * Name: imxrt_sdhc_enable + * + * Description: + * Enable card insertion/removal event detection + * + * Input Parameters: + * lower - An instance of the auto-mounter lower half state structure + * enable - True: enable event detection; False: disable + * + * Returned Value: + * None + * + ****************************************************************************/ + +static void imxrt_sdhc_enable(FAR const struct automount_lower_s *lower, + bool enable) +{ + FAR const struct imxrt_automount_config_s *config; + FAR struct imxrt_automount_state_s *state; + irqstate_t flags; + + /* Recover references to our structure */ + + config = (FAR struct imxrt_automount_config_s *)lower; + DEBUGASSERT(config != NULL && config->state != NULL); + + state = config->state; + + /* Save the fake enable setting */ + + flags = enter_critical_section(); + state->enable = enable; + + /* Did an interrupt occur while interrupts were disabled? */ + + if (enable && state->pending) + { + /* Yes.. perform the fake interrupt if the interrutp is attached */ + + if (state->handler) + { + uint8_t inserted = imxrt_gpio_read(PIN_USDHC1_CD); + if (0 == inserted) + { + state->handler(&config->lower, state->arg, true); + } + else + { + state->handler(&config->lower, state->arg, false); + } + } + + state->pending = false; + } + + leave_critical_section(flags); +} + +/**************************************************************************** + * Name: imxrt_sdhc_inserted + * + * Description: + * Check if a card is inserted into the slot. + * + * Input Parameters: + * lower - An instance of the auto-mounter lower half state structure + * + * Returned Value: + * True if the card is inserted; False otherwise + * + ****************************************************************************/ + +static bool imxrt_sdhc_inserted(FAR const struct automount_lower_s *lower) +{ + uint8_t inserted = imxrt_gpio_read(PIN_USDHC1_CD); + if (0 == inserted) + { + return true; + } + + return false; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: imxrt_sdhc_automount_event + * + * Description: + * The SDHC card detection logic has detected an insertion or removal + * event. + * It has already scheduled the MMC/SD block driver operations. + * Now we need to schedule the auto-mount event which will occur with a + * substantial delay to make sure that everything has settle down. + * + * Input Parameters: + * slotno - Identifies the SDHC0 slot: SDHC0_SLOTNO or SDHC1_SLOTNO. + * There is a terminology problem here: Each SDHC supports two slots, + * slot A and slot B. Only slot A is used. + * So this is not a really a slot, but an HSCMI peripheral number. + * inserted - True if the card is inserted in the slot. False otherwise. + * + * Returned Value: + * None + * + * Assumptions: + * Interrupts are disabled. + * + ****************************************************************************/ + +int imxrt_sdhc_automount_event(void *arg) +{ + FAR const struct imxrt_automount_config_s *config = &g_sdhc_config; + FAR struct imxrt_automount_state_s *state = &g_sdhc_state; + + /* Is the auto-mounter interrupt attached? */ + + if (state->handler) + { + /* Yes.. Have we been asked to hold off interrupts? */ + + if (!state->enable) + { + /* Yes.. just remember that there is a pending interrupt. We will + * deliver the interrupt when interrupts are "re-enabled." + */ + + state->pending = true; + } + else + { + /* No.. forward the event to the handler */ + + uint8_t inserted = imxrt_gpio_read(PIN_USDHC1_CD); + if (0 == inserted) + { + state->handler(&config->lower, state->arg, true); + } + else + { + state->handler(&config->lower, state->arg, false); + } + } + } + return 0; +} + +/**************************************************************************** + * Name: imxrt_automount_initialize + * + * Description: + * Configure auto-mounters for each enable and so configured SDHC + * + * Input Parameters: + * None + * + * Returned Value: + * None + * + ****************************************************************************/ + +void imxrt_automount_initialize(void) +{ + FAR void *handle; + + finfo("Initializing automounter(s)\n"); + + /* Initialize the SDHC0 auto-mounter */ + + handle = automount_initialize(&g_sdhc_config.lower); + if (!handle) + { + ferr("ERROR: Failed to initialize auto-mounter for SDHC0\n"); + } +} diff --git a/Ubiquitous/Nuttx_Fusion_XiUOS/aiit_board/xidatong/src/xidatong.h b/Ubiquitous/Nuttx_Fusion_XiUOS/aiit_board/xidatong/src/xidatong.h index 050d3dc5b..26f11c9f9 100644 --- a/Ubiquitous/Nuttx_Fusion_XiUOS/aiit_board/xidatong/src/xidatong.h +++ b/Ubiquitous/Nuttx_Fusion_XiUOS/aiit_board/xidatong/src/xidatong.h @@ -41,9 +41,13 @@ #include #include +#include +#include #include "imxrt_gpio.h" #include "imxrt_iomuxc.h" +#include "imxrt_usdhc.h" +#include /**************************************************************************** * Pre-processor Definitions @@ -206,5 +210,14 @@ int imxrt_gpio_initialize(void); int imxrt_usbhost_initialize(void); #endif +#if defined(CONFIG_IMXRT_USBOTG) || defined(CONFIG_USBHOST) +int imxrt_mmcsd_initialize(void); +#endif + +#ifdef CONFIG_XIDATONG_SDHC_AUTOMOUNT +int imxrt_sdhc_automount_event(void *arg); +void imxrt_automount_initialize(void); +#endif + #endif /* __ASSEMBLY__ */ #endif /* __BOARDS_ARM_IMXRT_XIDATONG_SRC_XIDATONG_H */