add sdio driver for GD32F4

This commit is contained in:
GD32-MCU
2023-06-03 14:47:40 +08:00
committed by Xiang Xiao
parent 0a9279f672
commit fdfc25cf56
12 changed files with 3921 additions and 8 deletions
@@ -0,0 +1,95 @@
#
# This file is autogenerated: PLEASE DO NOT EDIT IT.
#
# You can use "make menuconfig" to make any modifications to the installed .config file.
# You can then do "make savedefconfig" to generate a new defconfig file that includes your
# modifications.
#
# CONFIG_ARCH_FPU is not set
CONFIG_ARCH="arm"
CONFIG_ARCH_BOARD="gd32f450zk-eval"
CONFIG_ARCH_BOARD_GD32F450ZK_EVAL=y
CONFIG_ARCH_BUTTONS=y
CONFIG_ARCH_CHIP="gd32f4"
CONFIG_ARCH_CHIP_GD32F450ZK=y
CONFIG_ARCH_CHIP_GD32F4=y
CONFIG_ARCH_STACKDUMP=y
CONFIG_BOARD_LOOPSPERMSEC=16717
CONFIG_BUILTIN=y
CONFIG_DEBUG_CUSTOMOPT=y
CONFIG_DEBUG_OPTLEVEL="-O0"
CONFIG_DEBUG_SYMBOLS=y
CONFIG_ETH0_PHY_DP83848C=y
CONFIG_FAT_COMPUTE_FSINFO=y
CONFIG_FAT_FORCE_INDIRECT=y
CONFIG_FAT_LCNAMES=y
CONFIG_FAT_LFN=y
CONFIG_FS_FAT=y
CONFIG_FS_LARGEFILE=y
CONFIG_FS_PROCFS=y
CONFIG_FS_PROCFS_REGISTER=y
CONFIG_FS_TMPFS=y
CONFIG_GD32F4_DISABLE_IDLE_SLEEP_DURING_DEBUG=y
CONFIG_GD32F4_ENETMAC=y
CONFIG_GD32F4_FLASH_CONFIG_K=y
CONFIG_GD32F4_PHY_SR=16
CONFIG_GD32F4_PHY_SR_100FD=0x0004
CONFIG_GD32F4_PHY_SR_100HD=0x0000
CONFIG_GD32F4_PHY_SR_10FD=0x0006
CONFIG_GD32F4_PHY_SR_10HD=0x0002
CONFIG_GD32F4_PHY_SR_ALTCONFIG=y
CONFIG_GD32F4_PHY_SR_ALTMODE=0x006
CONFIG_GD32F4_RMII_EXTCLK=y
CONFIG_GD32F4_SDIO=y
CONFIG_GD32F4_SDIO_PULLUP=y
CONFIG_HAVE_CXX=y
CONFIG_HAVE_CXXINITIALIZE=y
CONFIG_INIT_ENTRYPOINT="nsh_main"
CONFIG_INTELHEX_BINARY=y
CONFIG_MKFATFS_BUFFER_ALIGNMENT=10
CONFIG_MMCSD=y
CONFIG_MMCSD_SDIO=y
CONFIG_MM_REGIONS=2
CONFIG_NET=y
CONFIG_NETDB_DNSCLIENT=y
CONFIG_NETINIT_DRIPADDR=0x0a320301
CONFIG_NETINIT_IPADDR=0x0a320336
CONFIG_NETINIT_MACADDR_1=0x20304050
CONFIG_NETINIT_MACADDR_2=0x6080
CONFIG_NETINIT_NOMAC=y
CONFIG_NETUTILS_DISCOVER=y
CONFIG_NETUTILS_WEBCLIENT=y
CONFIG_NET_ARP_IPIN=y
CONFIG_NET_ARP_SEND=y
CONFIG_NET_BROADCAST=y
CONFIG_NET_ETH_PKTSIZE=1500
CONFIG_NET_ICMP=y
CONFIG_NET_ICMP_SOCKET=y
CONFIG_NET_IGMP=y
CONFIG_NET_LOOPBACK=y
CONFIG_NET_ROUTE=y
CONFIG_NET_STATISTICS=y
CONFIG_NET_TCP=y
CONFIG_NET_UDP=y
CONFIG_NET_UDP_CHECKSUMS=y
CONFIG_NSH_ARCHINIT=y
CONFIG_NSH_BUILTIN_APPS=y
CONFIG_NSH_FILEIOSIZE=512
CONFIG_NSH_LINELEN=64
CONFIG_NSH_READLINE=y
CONFIG_PREALLOC_TIMERS=4
CONFIG_RAM_SIZE=114688
CONFIG_RAM_START=0x20000000
CONFIG_RAW_BINARY=y
CONFIG_RR_INTERVAL=200
CONFIG_SCHED_HPWORK=y
CONFIG_SCHED_LPWORK=y
CONFIG_SCHED_WAITPID=y
CONFIG_START_DAY=6
CONFIG_START_MONTH=12
CONFIG_START_YEAR=2011
CONFIG_SYSTEM_DHCPC_RENEW=y
CONFIG_SYSTEM_NSH=y
CONFIG_SYSTEM_PING=y
CONFIG_TASK_NAME_SIZE=0
CONFIG_USART0_SERIAL_CONSOLE=y
@@ -358,4 +358,26 @@ typedef enum
# define GPIO_ENET_PPS_OUT GPIO_ENET_PPS_OUT_1
#endif
/* SDIO gpios
*
* PD2 SDIO_CMD
* PC12 SDIO_CLK
* PC8 SDIO_DAT0
* PC9 SDIO_DAT1
* PC10 SDIO_DAT2
* PC11 SDIO_DAT3
*
*/
#define GPIO_SDIO_CMD_PIN GPIO_SDIO_CMD_2
#define GPIO_SDIO_CLK_PIN GPIO_SDIO_CK_2
#define GPIO_SDIO_DAT0_PIN GPIO_SDIO_D0_2
#define GPIO_SDIO_DAT1_PIN GPIO_SDIO_D1_3
#define GPIO_SDIO_DAT2_PIN GPIO_SDIO_D2_3
#define GPIO_SDIO_DAT3_PIN GPIO_SDIO_D3
#ifdef CONFIG_GD32F4_SDIO_DMA
# define SDIO_DMA_INTEN (DMA_CHXCTL_SDEIE | DMA_CHXCTL_TAEIE | DMA_CHXCTL_FTFIE)
#endif
#endif /* __BOARDS_ARM_GD32F450ZK_EVAL_INCLUDE_BOARD_H */
@@ -42,11 +42,15 @@ CSRCS += gd32f4xx_gpio.c
endif
ifeq ($(CONFIG_ARCH_BUTTONS),y)
CSRCS += gd32f4xx_buttons.c
CSRCS += gd32f4xx_buttons.c
endif
ifeq ($(CONFIG_SPI),y)
CSRCS += gd32f4xx_spi.c
CSRCS += gd32f4xx_spi.c
endif
ifeq ($(CONFIG_MMCSD),y)
CSRCS += gd32f4xx_sdio.c
endif
ifeq ($(CONFIG_MTD_GD25),y)
@@ -147,6 +147,33 @@
GPIO_CFG_PORT_B | GPIO_CFG_PIN_1)
#define GPIO_INT1 (GPIO_CFG_MODE_INPUT | GPIO_CFG_PUPD_NONE | GPIO_CFG_PORT_B | GPIO_CFG_PIN_2)
/* Can't support MMC/SD features if mountpoints are disabled or if SDIO
* support is not enabled.
*/
#if defined(CONFIG_DISABLE_MOUNTPOINT) || !defined(CONFIG_GD32_SDIO)
# undef HAVE_SDIO
#endif
#define SDIO_MINOR 0 /* Any minor number, default 0 */
#define SDIO_SLOTNO 0 /* Only one slot */
#ifdef HAVE_SDIO
# if !defined(CONFIG_NSH_MMCSDSLOTNO)
# define CONFIG_NSH_MMCSDSLOTNO SDIO_SLOTNO
# elif 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
#endif
/****************************************************************************
* Public Data
****************************************************************************/
@@ -204,12 +204,12 @@ int board_app_initialize(uintptr_t arg)
#ifdef CONFIG_INPUT_BUTTONS_LOWER
/* Register the BUTTON driver */
ret = btn_lower_initialize("/dev/buttons");
if (ret != OK)
{
syslog(LOG_ERR, "ERROR: btn_lower_initialize() failed: %d\n", ret);
return ret;
}
ret = btn_lower_initialize("/dev/buttons");
if (ret != OK)
{
syslog(LOG_ERR, "ERROR: btn_lower_initialize() failed: %d\n", ret);
return ret;
}
#else
/* Enable BUTTON support for some other purpose */
@@ -228,6 +228,34 @@ int board_app_initialize(uintptr_t arg)
}
#endif
/* Configure SDIO chip selects */
#ifdef CONFIG_ARCH_HAVE_SDIO
ret = gd32_sdio_initialize();
if (ret != OK)
{
syslog(LOG_ERR, "ERROR: gd32_sdio_initialize() failed: %d\n", ret);
return ret;
}
/* Mount the file system at /mnt/sd */
ret = nx_mount("/dev/mmcsd0", "/mnt/sd", "vfat", 0, NULL);
if (ret < 0)
{
ret = nx_mount("/dev/mmcsd0", "/mnt/sd", "vfat", 0,
"forceformat");
if (ret < 0)
{
ferr("ERROR: Failed to mount the SD card: %d\n", ret);
return ret;
}
}
syslog(LOG_INFO, "INFO: FAT volume /mnt/sd mount " \
"sd card success: %d\n", ret);
#endif
/* Now we are initialized */
initialized = true;
@@ -0,0 +1,156 @@
/****************************************************************************
* boards/arm/gd32f4/gd32f450zk-eval/src/gd32f4xx_sdio.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdbool.h>
#include <stdio.h>
#include <debug.h>
#include <errno.h>
#include <nuttx/sdio.h>
#include <nuttx/mmcsd.h>
#include "chip.h"
#include "gd32f4xx.h"
#include "gd32f450z_eval.h"
#ifdef CONFIG_MMCSD
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Configuration ************************************************************/
/* Card detections requires card support and a card detection GPIO */
#define HAVE_NCD 1
#if !defined(GPIO_SDMMC1_NCD)
# undef HAVE_NCD
#endif
/****************************************************************************
* Private Data
****************************************************************************/
static struct sdio_dev_s *g_sdio_dev;
#ifdef HAVE_NCD
static bool g_sd_inserted = 0xff; /* Impossible value */
#endif
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: gd32_ncd_interrupt
*
* Description:
* Card detect interrupt handler.
*
****************************************************************************/
#ifdef HAVE_NCD
static int gd32_ncd_interrupt(int irq, void *context)
{
bool present;
present = !gd32_gpio_read(GPIO_SDMMC1_NCD);
if (g_sdio_dev && present != g_sd_inserted)
{
sdio_mediachange(g_sdio_dev, present);
g_sd_inserted = present;
}
return OK;
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: gd32_sdio_initialize
*
* Description:
* Initialize SDIO-based MMC/SD card support
*
****************************************************************************/
int gd32_sdio_initialize(void)
{
int ret;
#ifdef HAVE_NCD
/* Card detect */
bool cd_status;
#endif
/* Mount the SDIO-based MMC/SD block driver
* First, get an instance of the SDIO interface
*/
finfo("Initializing SDIO slot %d\n", SDIO_SLOTNO);
g_sdio_dev = sdio_initialize(SDIO_SLOTNO);
if (!g_sdio_dev)
{
ferr("ERROR: Failed to initialize SDIO slot %d\n", SDIO_SLOTNO);
return -ENODEV;
}
/* Now bind the SDIO interface to the MMC/SD driver */
finfo("Bind SDIO to the MMC/SD driver, minor=%d\n", SDIO_MINOR);
ret = mmcsd_slotinitialize(SDIO_MINOR, g_sdio_dev);
if (ret != OK)
{
ferr("ERROR: Failed to bind SDIO to the MMC/SD driver: %d\n", ret);
return ret;
}
finfo("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 = !gd32_gpio_read(GPIO_SDMMC1_NCD);
finfo("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 /* CONFIG_MMCSD */