Skip to content

Commit

Permalink
refactor(sdmmc): place sdmmc driver into a new component
Browse files Browse the repository at this point in the history
  • Loading branch information
Icarus113 committed Nov 16, 2023
1 parent c455919 commit c7c38b7
Show file tree
Hide file tree
Showing 20 changed files with 191 additions and 185 deletions.
8 changes: 1 addition & 7 deletions components/driver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ set(includes "include"
"parlio/include"
"rmt/include"
"sdio_slave/include"
"sdmmc/include"
"sdspi/include"
"sigma_delta/include"
"temperature_sensor/include"
Expand Down Expand Up @@ -127,12 +126,6 @@ if(CONFIG_SOC_SDIO_SLAVE_SUPPORTED)
list(APPEND srcs "sdio_slave/sdio_slave.c")
endif()

# SDMMC related source files
if(CONFIG_SOC_SDMMC_HOST_SUPPORTED)
list(APPEND srcs "sdmmc/sdmmc_transaction.c"
"sdmmc/sdmmc_host.c")
endif()

# Sigma-Delta Modulation related source files
if(CONFIG_SOC_SDM_SUPPORTED)
list(APPEND srcs "sigma_delta/sdm.c"
Expand Down Expand Up @@ -199,6 +192,7 @@ else()
# for backward compatibility, the driver component needs to
# have a public dependency on other "esp_driver_foo" components
esp_driver_gpio esp_driver_pcnt esp_driver_gptimer esp_driver_spi esp_driver_mcpwm
esp_driver_sd_common esp_driver_sdmmc
LDFRAGMENTS ${ldfragments}
)
endif()
Expand Down
6 changes: 6 additions & 0 deletions components/esp_driver_sd_common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
set(srcs)
set(public_include "include")

idf_component_register(SRCS ${srcs}
INCLUDE_DIRS ${public_include}
)
10 changes: 10 additions & 0 deletions components/esp_driver_sd_common/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Common Component for SD Related Drivers

This component contains driver layer common files:
- `esp_sd_defs.h`
- `esp_sd_types.h`

These files will be used by SD related drivers, including:

- `esp_driver_sdmmc`
- `esp_driver_sdspi`
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
/*
* SPDX-FileCopyrightText: 2006 Uwe Stuehler <uwe@openbsd.org>
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: ISC
*
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
*/
/*
* Copyright (c) 2006 Uwe Stuehler <uwe@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once
Expand Down Expand Up @@ -75,7 +58,6 @@ extern "C" {
#define SD_IO_RW_DIRECT 52 /* R5 */
#define SD_IO_RW_EXTENDED 53 /* R5 */


/* OCR bits */
#define MMC_OCR_MEM_READY (1<<31) /* memory power-up status bit */
#define MMC_OCR_ACCESS_MODE_MASK 0x60000000 /* bits 30:29 */
Expand Down Expand Up @@ -528,7 +510,6 @@ static inline uint32_t MMC_RSP_BITS(uint32_t *src, int start, int len)
#define CISTPL_CODE_SDIO_EXT 0x92
#define CISTPL_CODE_END 0xFF


/* Timing */
#define SDMMC_TIMING_LEGACY 0
#define SDMMC_TIMING_HIGHSPEED 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
/*
* SPDX-FileCopyrightText: 2006 Uwe Stuehler <uwe@openbsd.org>
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: ISC
*
* SPDX-FileContributor: 2016-2023 Espressif Systems (Shanghai) CO LTD
*/
/*
* Copyright (c) 2006 Uwe Stuehler <uwe@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once
Expand Down Expand Up @@ -110,15 +93,15 @@ typedef struct {
* SD/MMC command information
*/
typedef struct {
uint32_t opcode; /*!< SD or MMC command index */
uint32_t arg; /*!< SD/MMC command argument */
sdmmc_response_t response; /*!< response buffer */
void* data; /*!< buffer to send or read into */
size_t datalen; /*!< length of data in the buffer */
size_t buflen; /*!< length of the buffer */
size_t blklen; /*!< block length */
int flags; /*!< see below */
/** @cond */
uint32_t opcode; /*!< SD or MMC command index */
uint32_t arg; /*!< SD/MMC command argument */
sdmmc_response_t response; /*!< response buffer */
void* data; /*!< buffer to send or read into */
size_t datalen; /*!< length of data in the buffer */
size_t buflen; /*!< length of the buffer */
size_t blklen; /*!< block length */
int flags; /*!< see below */
/** @cond */
#define SCF_ITSDONE 0x0001 /*!< command is complete */
#define SCF_CMD(flags) ((flags) & 0x00f0)
#define SCF_CMD_AC 0x0000
Expand All @@ -131,7 +114,7 @@ typedef struct {
#define SCF_RSP_CRC 0x0400
#define SCF_RSP_IDX 0x0800
#define SCF_RSP_PRESENT 0x1000
/* response types */
/* response types */
#define SCF_RSP_R0 0 /*!< none */
#define SCF_RSP_R1 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX)
#define SCF_RSP_R1B (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX|SCF_RSP_BSY)
Expand All @@ -142,11 +125,11 @@ typedef struct {
#define SCF_RSP_R5B (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX|SCF_RSP_BSY)
#define SCF_RSP_R6 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX)
#define SCF_RSP_R7 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX)
/* special flags */
/* special flags */
#define SCF_WAIT_BUSY 0x2000 /*!< Wait for completion of card busy signal before returning */
/** @endcond */
esp_err_t error; /*!< error returned from transfer */
uint32_t timeout_ms; /*!< response timeout, in milliseconds */
/** @endcond */
esp_err_t error; /*!< error returned from transfer */
uint32_t timeout_ms; /*!< response timeout, in milliseconds */
} sdmmc_command_t;

/**
Expand Down
15 changes: 15 additions & 0 deletions components/esp_driver_sdmmc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
set(srcs)

set(public_include "include")

# SDMMC related source files
if(CONFIG_SOC_SDMMC_HOST_SUPPORTED)
list(APPEND srcs "src/sdmmc_transaction.c"
"src/sdmmc_host.c")
endif()

idf_component_register(SRCS ${srcs}
INCLUDE_DIRS ${public_include}
REQUIRES
PRIV_REQUIRES esp_timer esp_pm esp_mm esp_driver_gpio esp_driver_sd_common
)
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ extern "C" {
#define SDMMC_SLOT_NO_WP GPIO_NUM_NC ///< indicates that write protect line is not used
#define SDMMC_SLOT_WIDTH_DEFAULT 0 ///< use the maximum possible width for the slot



#if SOC_SDMMC_USE_IOMUX && !SOC_SDMMC_USE_GPIO_MATRIX
/**
* Macro defining default configuration of SDMMC host slot
Expand Down
9 changes: 9 additions & 0 deletions components/esp_driver_sdmmc/include/driver/sdmmc_defs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include "driver/esp_sd_defs.h"
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ typedef struct {
uint8_t width; ///< Bus width used by the slot (might be less than the max width supported)
uint32_t flags; ///< Features used by this slot
#define SDMMC_SLOT_FLAG_INTERNAL_PULLUP BIT(0)
/**< Enable internal pullups on enabled pins. The internal pullups
are insufficient however, please make sure external pullups are
connected on the bus. This is for debug / example purpose only.
*/
/**< Enable internal pullups on enabled pins. The internal pullups
are insufficient however, please make sure external pullups are
connected on the bus. This is for debug / example purpose only.
*/
#define SDMMC_SLOT_FLAG_WP_ACTIVE_HIGH BIT(1)
/**< GPIO write protect polarity.
* 0 means "active low", i.e. card is protected when the GPIO is low;
* 1 means "active high", i.e. card is protected when GPIO is high.
*/
/**< GPIO write protect polarity.
* 0 means "active low", i.e. card is protected when the GPIO is low;
* 1 means "active high", i.e. card is protected when GPIO is high.
*/
} sdmmc_slot_config_t;

/**
Expand Down
9 changes: 9 additions & 0 deletions components/esp_driver_sdmmc/include/driver/sdmmc_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include "driver/esp_sd_types.h"
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
#define SDMMC_CLK_SRC_ATOMIC()
#endif


static const char *TAG = "sdmmc_periph";

/**
Expand All @@ -70,7 +69,6 @@ typedef struct host_ctx_t {

static host_ctx_t s_host_ctx;


static void sdmmc_isr(void *arg);
static void sdmmc_host_dma_init(void);
static esp_err_t sdmmc_host_pullup_en_internal(int slot, int width);
Expand Down Expand Up @@ -209,10 +207,10 @@ void sdmmc_host_get_clk_dividers(uint32_t freq_khz, int *host_div, int *card_div
* effective frequency range: 400 kHz - 32 MHz (32.1 - 39.9 MHz cannot be covered with given divider scheme)
*/
*host_div = (clk_src_freq_hz) / (freq_khz * 1000);
if (*host_div > 15 ) {
if (*host_div > 15) {
*host_div = 2;
*card_div = (clk_src_freq_hz / 2) / (2 * freq_khz * 1000);
if ( ((clk_src_freq_hz / 2) % (2 * freq_khz * 1000)) > 0 ) {
if (((clk_src_freq_hz / 2) % (2 * freq_khz * 1000)) > 0) {
(*card_div)++;
}
} else if ((clk_src_freq_hz % (freq_khz * 1000)) > 0) {
Expand Down Expand Up @@ -315,22 +313,22 @@ esp_err_t sdmmc_host_set_input_delay(int slot, sdmmc_delay_phase_t delay_phase)
int delay_phase_num = 0;
sdmmc_ll_delay_phase_t phase = SDMMC_LL_DELAY_PHASE_0;
switch (delay_phase) {
case SDMMC_DELAY_PHASE_1:
phase = SDMMC_LL_DELAY_PHASE_1;
delay_phase_num = 1;
break;
case SDMMC_DELAY_PHASE_2:
phase = SDMMC_LL_DELAY_PHASE_2;
delay_phase_num = 2;
break;
case SDMMC_DELAY_PHASE_3:
phase = SDMMC_LL_DELAY_PHASE_3;
delay_phase_num = 3;
break;
default:
phase = SDMMC_LL_DELAY_PHASE_0;
delay_phase_num = 0;
break;
case SDMMC_DELAY_PHASE_1:
phase = SDMMC_LL_DELAY_PHASE_1;
delay_phase_num = 1;
break;
case SDMMC_DELAY_PHASE_2:
phase = SDMMC_LL_DELAY_PHASE_2;
delay_phase_num = 2;
break;
case SDMMC_DELAY_PHASE_3:
phase = SDMMC_LL_DELAY_PHASE_3;
delay_phase_num = 3;
break;
default:
phase = SDMMC_LL_DELAY_PHASE_0;
delay_phase_num = 0;
break;
}
SDMMC_CLK_SRC_ATOMIC() {
sdmmc_ll_set_din_delay(s_host_ctx.hal.dev, phase);
Expand Down Expand Up @@ -433,13 +431,13 @@ esp_err_t sdmmc_host_init(void)
}
// Enable interrupts
SDMMC.intmask.val =
SDMMC_INTMASK_CD |
SDMMC_INTMASK_CMD_DONE |
SDMMC_INTMASK_DATA_OVER |
SDMMC_INTMASK_RCRC | SDMMC_INTMASK_DCRC |
SDMMC_INTMASK_RTO | SDMMC_INTMASK_DTO | SDMMC_INTMASK_HTO |
SDMMC_INTMASK_SBE | SDMMC_INTMASK_EBE |
SDMMC_INTMASK_RESP_ERR | SDMMC_INTMASK_HLE; //sdio is enabled only when use.
SDMMC_INTMASK_CD |
SDMMC_INTMASK_CMD_DONE |
SDMMC_INTMASK_DATA_OVER |
SDMMC_INTMASK_RCRC | SDMMC_INTMASK_DCRC |
SDMMC_INTMASK_RTO | SDMMC_INTMASK_DTO | SDMMC_INTMASK_HTO |
SDMMC_INTMASK_SBE | SDMMC_INTMASK_EBE |
SDMMC_INTMASK_RESP_ERR | SDMMC_INTMASK_HLE; //sdio is enabled only when use.
SDMMC.ctrl.int_enable = 1;

// Disable generation of Busy Clear Interrupt
Expand Down Expand Up @@ -479,7 +477,7 @@ static void configure_pin_iomux(uint8_t gpio_num)

static void configure_pin_gpio_matrix(uint8_t gpio_num, uint8_t gpio_matrix_sig, gpio_mode_t mode, const char *name)
{
assert (gpio_num != (uint8_t) GPIO_NUM_NC);
assert(gpio_num != (uint8_t) GPIO_NUM_NC);
ESP_LOGD(TAG, "using GPIO%d as %s pin", gpio_num, name);
gpio_reset_pin(gpio_num);
gpio_set_direction(gpio_num, mode);
Expand Down Expand Up @@ -651,7 +649,7 @@ esp_err_t sdmmc_host_init_slot(int slot, const sdmmc_slot_config_t *slot_config)
// As hardware expects an active-high signal,
// if WP signal is active low, then invert it in GPIO matrix,
// else keep it in its default state
esp_rom_gpio_connect_in_signal(matrix_in_wp, slot_info->write_protect, (gpio_wp_polarity? false : true));
esp_rom_gpio_connect_in_signal(matrix_in_wp, slot_info->write_protect, (gpio_wp_polarity ? false : true));

// By default, set probing frequency (400kHz) and 1-bit bus
esp_err_t ret = sdmmc_host_set_card_clk(slot, 400);
Expand Down Expand Up @@ -733,7 +731,7 @@ esp_err_t sdmmc_host_set_bus_width(int slot, size_t width)

size_t sdmmc_host_get_slot_width(int slot)
{
assert( slot == 0 || slot == 1 );
assert(slot == 0 || slot == 1);
return s_host_ctx.slot_ctx[slot].slot_width;
}

Expand Down
File renamed without changes.
Loading

0 comments on commit c7c38b7

Please sign in to comment.