2016-06-08 15:52:19 +02:00
|
|
|
/****************************************************************************
|
2019-08-19 17:16:08 +02:00
|
|
|
* boards/arm/stm32f7/nucleo-144/src/stm32_sdio.c
|
2016-06-08 15:52:19 +02:00
|
|
|
*
|
2021-03-19 12:39:00 +01:00
|
|
|
* 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
|
2016-06-08 15:52:19 +02:00
|
|
|
*
|
2021-03-19 12:39:00 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-06-08 15:52:19 +02:00
|
|
|
*
|
2021-03-19 12:39:00 +01:00
|
|
|
* 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.
|
2016-06-08 15:52:19 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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>
|
|
|
|
|
2016-06-24 02:41:51 +02:00
|
|
|
#include "chip.h"
|
2016-06-08 15:52:19 +02:00
|
|
|
#include "nucleo-144.h"
|
2016-06-24 02:41:51 +02:00
|
|
|
#include "stm32_gpio.h"
|
|
|
|
#include "stm32_sdmmc.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_MMCSD
|
2016-06-08 15:52:19 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Configuration ************************************************************/
|
|
|
|
|
|
|
|
/* Card detections requires card support and a card detection GPIO */
|
|
|
|
|
|
|
|
#define HAVE_NCD 1
|
2016-06-24 02:41:51 +02:00
|
|
|
#if !defined(GPIO_SDMMC1_NCD)
|
2016-06-08 15:52:19 +02:00
|
|
|
# undef HAVE_NCD
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-04-17 08:01:48 +02:00
|
|
|
static struct sdio_dev_s *g_sdio_dev;
|
2016-06-08 15:52:19 +02:00
|
|
|
#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
|
2022-04-17 08:01:48 +02:00
|
|
|
static int stm32_ncd_interrupt(int irq, void *context)
|
2016-06-08 15:52:19 +02:00
|
|
|
{
|
|
|
|
bool present;
|
|
|
|
|
2016-06-24 02:41:51 +02:00
|
|
|
present = !stm32_gpioread(GPIO_SDMMC1_NCD);
|
|
|
|
if (g_sdio_dev && present != g_sd_inserted)
|
2016-06-08 15:52:19 +02:00
|
|
|
{
|
|
|
|
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 */
|
|
|
|
|
2016-06-24 02:41:51 +02:00
|
|
|
stm32_configgpio(GPIO_SDMMC1_NCD);
|
2016-06-08 15:52:19 +02:00
|
|
|
|
|
|
|
/* Register an interrupt handler for the card detect pin */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
stm32_gpiosetevent(GPIO_SDMMC1_NCD, true, true, true,
|
|
|
|
stm32_ncd_interrupt, NULL);
|
2016-06-08 15:52:19 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Mount the SDIO-based MMC/SD block driver */
|
2021-03-20 13:01:22 +01:00
|
|
|
|
2016-06-08 15:52:19 +02:00
|
|
|
/* First, get an instance of the SDIO interface */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Initializing SDIO slot %d\n", SDIO_SLOTNO);
|
2016-06-08 15:52:19 +02:00
|
|
|
|
|
|
|
g_sdio_dev = sdio_initialize(SDIO_SLOTNO);
|
|
|
|
if (!g_sdio_dev)
|
|
|
|
{
|
2016-06-13 16:45:54 +02:00
|
|
|
ferr("ERROR: Failed to initialize SDIO slot %d\n", SDIO_SLOTNO);
|
2016-06-08 15:52:19 +02:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now bind the SDIO interface to the MMC/SD driver */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Bind SDIO to the MMC/SD driver, minor=%d\n", SDIO_MINOR);
|
2016-06-08 15:52:19 +02:00
|
|
|
|
|
|
|
ret = mmcsd_slotinitialize(SDIO_MINOR, g_sdio_dev);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2016-06-13 16:45:54 +02:00
|
|
|
ferr("ERROR: Failed to bind SDIO to the MMC/SD driver: %d\n", ret);
|
2016-06-08 15:52:19 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Successfully bound SDIO to the MMC/SD driver\n");
|
2016-06-08 15:52:19 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_NCD
|
|
|
|
/* Use SD card detect pin to check if a card is g_sd_inserted */
|
|
|
|
|
2016-06-24 02:41:51 +02:00
|
|
|
cd_status = !stm32_gpioread(GPIO_SDMMC1_NCD);
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Card detect : %d\n", cd_status);
|
2016-06-08 15:52:19 +02:00
|
|
|
|
|
|
|
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 */
|