aboutsummaryrefslogtreecommitdiff
path: root/circuitpython/lib/sdmmc/include
diff options
context:
space:
mode:
Diffstat (limited to 'circuitpython/lib/sdmmc/include')
-rw-r--r--circuitpython/lib/sdmmc/include/sdmmc_cmd.h274
-rw-r--r--circuitpython/lib/sdmmc/include/sdmmc_defs.h498
-rw-r--r--circuitpython/lib/sdmmc/include/sdmmc_types.h199
3 files changed, 971 insertions, 0 deletions
diff --git a/circuitpython/lib/sdmmc/include/sdmmc_cmd.h b/circuitpython/lib/sdmmc/include/sdmmc_cmd.h
new file mode 100644
index 0000000..7952ada
--- /dev/null
+++ b/circuitpython/lib/sdmmc/include/sdmmc_cmd.h
@@ -0,0 +1,274 @@
+// Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD
+//
+// Licensed 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.
+
+#pragma once
+
+#include <stdio.h>
+#include "sdmmc_defs.h"
+#include "sdmmc_types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * Probe and initialize SD/MMC card using given host
+ *
+ * @note Only SD cards (SDSC and SDHC/SDXC) are supported now.
+ * Support for MMC/eMMC cards will be added later.
+ *
+ * @param host pointer to structure defining host controller
+ * @param out_card pointer to structure which will receive information
+ * about the card when the function completes
+ * @return
+ * - SDMMC_OK on success
+ * - One of the error codes from SDMMC host controller
+ */
+sdmmc_err_t sdmmc_card_init(const sdmmc_host_t* host,
+ sdmmc_card_t* out_card);
+
+/**
+ * @brief Print information about the card to a stream
+ * @param stream stream obtained using fopen or fdopen
+ * @param card card information structure initialized using sdmmc_card_init
+ */
+void sdmmc_card_print_info(FILE* stream, const sdmmc_card_t* card);
+
+/**
+ * Write given number of sectors to SD/MMC card
+ *
+ * @param card pointer to card information structure previously initialized
+ * using sdmmc_card_init
+ * @param src pointer to data buffer to read data from; data size must be
+ * equal to sector_count * card->csd.sector_size
+ * @param start_sector sector where to start writing
+ * @param sector_count number of sectors to write
+ * @return
+ * - SDMMC_OK on success
+ * - One of the error codes from SDMMC host controller
+ */
+sdmmc_err_t sdmmc_write_sectors(sdmmc_card_t* card, const void* src,
+ size_t start_sector, size_t sector_count);
+
+/**
+ * Read given number of sectors from the SD/MMC card
+ *
+ * @param card pointer to card information structure previously initialized
+ * using sdmmc_card_init
+ * @param dst pointer to data buffer to write into; buffer size must be
+ * at least sector_count * card->csd.sector_size
+ * @param start_sector sector where to start reading
+ * @param sector_count number of sectors to read
+ * @return
+ * - SDMMC_OK on success
+ * - One of the error codes from SDMMC host controller
+ */
+sdmmc_err_t sdmmc_read_sectors(sdmmc_card_t* card, void* dst,
+ size_t start_sector, size_t sector_count);
+
+/**
+ * Read one byte from an SDIO card using IO_RW_DIRECT (CMD52)
+ *
+ * @param card pointer to card information structure previously initialized
+ * using sdmmc_card_init
+ * @param function IO function number
+ * @param reg byte address within IO function
+ * @param[out] out_byte output, receives the value read from the card
+ * @return
+ * - SDMMC_OK on success
+ * - One of the error codes from SDMMC host controller
+ */
+sdmmc_err_t sdmmc_io_read_byte(sdmmc_card_t* card, uint32_t function,
+ uint32_t reg, uint8_t *out_byte);
+
+/**
+ * Write one byte to an SDIO card using IO_RW_DIRECT (CMD52)
+ *
+ * @param card pointer to card information structure previously initialized
+ * using sdmmc_card_init
+ * @param function IO function number
+ * @param reg byte address within IO function
+ * @param in_byte value to be written
+ * @param[out] out_byte if not NULL, receives new byte value read
+ * from the card (read-after-write).
+ * @return
+ * - SDMMC_OK on success
+ * - One of the error codes from SDMMC host controller
+ */
+sdmmc_err_t sdmmc_io_write_byte(sdmmc_card_t* card, uint32_t function,
+ uint32_t reg, uint8_t in_byte, uint8_t* out_byte);
+
+/**
+ * Read multiple bytes from an SDIO card using IO_RW_EXTENDED (CMD53)
+ *
+ * This function performs read operation using CMD53 in byte mode.
+ * For block mode, see sdmmc_io_read_blocks.
+ *
+ * @param card pointer to card information structure previously initialized
+ * using sdmmc_card_init
+ * @param function IO function number
+ * @param addr byte address within IO function where reading starts
+ * @param dst buffer which receives the data read from card
+ * @param size number of bytes to read
+ * @return
+ * - SDMMC_OK on success
+ * - SDMMC_ERR_INVALID_SIZE if size exceeds 512 bytes
+ * - One of the error codes from SDMMC host controller
+ */
+sdmmc_err_t sdmmc_io_read_bytes(sdmmc_card_t* card, uint32_t function,
+ uint32_t addr, void* dst, size_t size);
+
+/**
+ * Write multiple bytes to an SDIO card using IO_RW_EXTENDED (CMD53)
+ *
+ * This function performs write operation using CMD53 in byte mode.
+ * For block mode, see sdmmc_io_write_blocks.
+ *
+ * @param card pointer to card information structure previously initialized
+ * using sdmmc_card_init
+ * @param function IO function number
+ * @param addr byte address within IO function where writing starts
+ * @param src data to be written
+ * @param size number of bytes to write
+ * @return
+ * - SDMMC_OK on success
+ * - SDMMC_ERR_INVALID_SIZE if size exceeds 512 bytes
+ * - One of the error codes from SDMMC host controller
+ */
+sdmmc_err_t sdmmc_io_write_bytes(sdmmc_card_t* card, uint32_t function,
+ uint32_t addr, const void* src, size_t size);
+
+/**
+ * Read blocks of data from an SDIO card using IO_RW_EXTENDED (CMD53)
+ *
+ * This function performs read operation using CMD53 in block mode.
+ * For byte mode, see sdmmc_io_read_bytes.
+ *
+ * @param card pointer to card information structure previously initialized
+ * using sdmmc_card_init
+ * @param function IO function number
+ * @param addr byte address within IO function where writing starts
+ * @param dst buffer which receives the data read from card
+ * @param size number of bytes to read, must be divisible by the card block
+ * size.
+ * @return
+ * - SDMMC_OK on success
+ * - SDMMC_ERR_INVALID_SIZE if size is not divisible by 512 bytes
+ * - One of the error codes from SDMMC host controller
+ */
+sdmmc_err_t sdmmc_io_read_blocks(sdmmc_card_t* card, uint32_t function,
+ uint32_t addr, void* dst, size_t size);
+
+/**
+ * Write blocks of data to an SDIO card using IO_RW_EXTENDED (CMD53)
+ *
+ * This function performs write operation using CMD53 in block mode.
+ * For byte mode, see sdmmc_io_write_bytes.
+ *
+ * @param card pointer to card information structure previously initialized
+ * using sdmmc_card_init
+ * @param function IO function number
+ * @param addr byte address within IO function where writing starts
+ * @param src data to be written
+ * @param size number of bytes to read, must be divisible by the card block
+ * size.
+ * @return
+ * - SDMMC_OK on success
+ * - SDMMC_ERR_INVALID_SIZE if size is not divisible by 512 bytes
+ * - One of the error codes from SDMMC host controller
+ */
+sdmmc_err_t sdmmc_io_write_blocks(sdmmc_card_t* card, uint32_t function,
+ uint32_t addr, const void* src, size_t size);
+
+/**
+ * Enable SDIO interrupt in the SDMMC host
+ *
+ * @param card pointer to card information structure previously initialized
+ * using sdmmc_card_init
+ * @return
+ * - SDMMC_OK on success
+ * - SDMMC_ERR_NOT_SUPPORTED if the host controller does not support
+ * IO interrupts
+ */
+sdmmc_err_t sdmmc_io_enable_int(sdmmc_card_t* card);
+
+/**
+ * Block until an SDIO interrupt is received
+ *
+ * Slave uses D1 line to signal interrupt condition to the host.
+ * This function can be used to wait for the interrupt.
+ *
+ * @param card pointer to card information structure previously initialized
+ * using sdmmc_card_init
+ * @param timeout_ticks time to wait for the interrupt, in RTOS ticks
+ * @return
+ * - SDMMC_OK if the interrupt is received
+ * - SDMMC_ERR_NOT_SUPPORTED if the host controller does not support
+ * IO interrupts
+ * - SDMMC_ERR_TIMEOUT if the interrupt does not happen in timeout_ticks
+ */
+sdmmc_err_t sdmmc_io_wait_int(sdmmc_card_t* card, int timeout_ms);
+
+/**
+ * Get the data of CIS region of a SDIO card.
+ *
+ * You may provide a buffer not sufficient to store all the CIS data. In this
+ * case, this functions store as much data into your buffer as possible. Also,
+ * this function will try to get and return the size required for you.
+ *
+ * @param card pointer to card information structure previously initialized
+ * using sdmmc_card_init
+ * @param out_buffer Output buffer of the CIS data
+ * @param buffer_size Size of the buffer.
+ * @param inout_cis_size Mandatory, pointer to a size, input and output.
+ * - input: Limitation of maximum searching range, should be 0 or larger than
+ * buffer_size. The function searches for CIS_CODE_END until this range. Set to
+ * 0 to search infinitely.
+ * - output: The size required to store all the CIS data, if CIS_CODE_END is found.
+ *
+ * @return
+ * - SDMMC_OK: on success
+ * - SDMMC_ERR_INVALID_RESPONSE: if the card does not (correctly) support CIS.
+ * - SDMMC_ERR_INVALID_SIZE: CIS_CODE_END found, but buffer_size is less than
+ * required size, which is stored in the inout_cis_size then.
+ * - SDMMC_ERR_NOT_FOUND: if the CIS_CODE_END not found. Increase input value of
+ * inout_cis_size or set it to 0, if you still want to search for the end;
+ * output value of inout_cis_size is invalid in this case.
+ * - and other error code return from sdmmc_io_read_bytes
+ */
+sdmmc_err_t sdmmc_io_get_cis_data(sdmmc_card_t* card, uint8_t* out_buffer, size_t buffer_size, size_t* inout_cis_size);
+
+/**
+ * Parse and print the CIS information of a SDIO card.
+ *
+ * @note Not all the CIS codes and all kinds of tuples are supported. If you
+ * see some unresolved code, you can add the parsing of these code in
+ * sdmmc_io.c and contribute to the IDF through the Github repository.
+ *
+ * using sdmmc_card_init
+ * @param buffer Buffer to parse
+ * @param buffer_size Size of the buffer.
+ * @param fp File pointer to print to, set to NULL to print to stdout.
+ *
+ * @return
+ * - SDMMC_OK: on success
+ * - SDMMC_ERR_NOT_SUPPORTED: if the value from the card is not supported to be parsed.
+ * - SDMMC_ERR_INVALID_SIZE: if the CIS size fields are not correct.
+ */
+sdmmc_err_t sdmmc_io_print_cis_info(uint8_t* buffer, size_t buffer_size, FILE* fp);
+
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/circuitpython/lib/sdmmc/include/sdmmc_defs.h b/circuitpython/lib/sdmmc/include/sdmmc_defs.h
new file mode 100644
index 0000000..7ac6ae1
--- /dev/null
+++ b/circuitpython/lib/sdmmc/include/sdmmc_defs.h
@@ -0,0 +1,498 @@
+/*
+ * Copyright (c) 2006 Uwe Stuehler <uwe@openbsd.org>
+ * Adaptations to ESP-IDF Copyright (c) 2016 Espressif Systems (Shanghai) PTE LTD
+ *
+ * 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.
+ */
+
+#ifndef _SDMMC_DEFS_H_
+#define _SDMMC_DEFS_H_
+
+#include <stdint.h>
+#include <limits.h>
+
+/* MMC commands */ /* response type */
+#define MMC_GO_IDLE_STATE 0 /* R0 */
+#define MMC_SEND_OP_COND 1 /* R3 */
+#define MMC_ALL_SEND_CID 2 /* R2 */
+#define MMC_SET_RELATIVE_ADDR 3 /* R1 */
+#define MMC_SWITCH 6 /* R1B */
+#define MMC_SELECT_CARD 7 /* R1 */
+#define MMC_SEND_EXT_CSD 8 /* R1 */
+#define MMC_SEND_CSD 9 /* R2 */
+#define MMC_SEND_CID 10 /* R1 */
+#define MMC_READ_DAT_UNTIL_STOP 11 /* R1 */
+#define MMC_STOP_TRANSMISSION 12 /* R1B */
+#define MMC_SEND_STATUS 13 /* R1 */
+#define MMC_SET_BLOCKLEN 16 /* R1 */
+#define MMC_READ_BLOCK_SINGLE 17 /* R1 */
+#define MMC_READ_BLOCK_MULTIPLE 18 /* R1 */
+#define MMC_WRITE_DAT_UNTIL_STOP 20 /* R1 */
+#define MMC_SET_BLOCK_COUNT 23 /* R1 */
+#define MMC_WRITE_BLOCK_SINGLE 24 /* R1 */
+#define MMC_WRITE_BLOCK_MULTIPLE 25 /* R1 */
+#define MMC_APP_CMD 55 /* R1 */
+
+/* SD commands */ /* response type */
+#define SD_SEND_RELATIVE_ADDR 3 /* R6 */
+#define SD_SEND_SWITCH_FUNC 6 /* R1 */
+#define SD_SEND_IF_COND 8 /* R7 */
+#define SD_READ_OCR 58 /* R3 */
+#define SD_CRC_ON_OFF 59 /* R1 */
+
+/* SD application commands */ /* response type */
+#define SD_APP_SET_BUS_WIDTH 6 /* R1 */
+#define SD_APP_SD_STATUS 13 /* R2 */
+#define SD_APP_OP_COND 41 /* R3 */
+#define SD_APP_SEND_SCR 51 /* R1 */
+
+/* SD IO commands */
+#define SD_IO_SEND_OP_COND 5 /* R4 */
+#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 */
+#define MMC_OCR_SECTOR_MODE (1<<30)
+#define MMC_OCR_BYTE_MODE (1<<29)
+#define MMC_OCR_3_5V_3_6V (1<<23)
+#define MMC_OCR_3_4V_3_5V (1<<22)
+#define MMC_OCR_3_3V_3_4V (1<<21)
+#define MMC_OCR_3_2V_3_3V (1<<20)
+#define MMC_OCR_3_1V_3_2V (1<<19)
+#define MMC_OCR_3_0V_3_1V (1<<18)
+#define MMC_OCR_2_9V_3_0V (1<<17)
+#define MMC_OCR_2_8V_2_9V (1<<16)
+#define MMC_OCR_2_7V_2_8V (1<<15)
+#define MMC_OCR_2_6V_2_7V (1<<14)
+#define MMC_OCR_2_5V_2_6V (1<<13)
+#define MMC_OCR_2_4V_2_5V (1<<12)
+#define MMC_OCR_2_3V_2_4V (1<<11)
+#define MMC_OCR_2_2V_2_3V (1<<10)
+#define MMC_OCR_2_1V_2_2V (1<<9)
+#define MMC_OCR_2_0V_2_1V (1<<8)
+#define MMC_OCR_1_65V_1_95V (1<<7)
+
+#define SD_OCR_SDHC_CAP (1<<30)
+#define SD_OCR_VOL_MASK 0xFF8000 /* bits 23:15 */
+
+/* SD mode R1 response type bits */
+#define MMC_R1_READY_FOR_DATA (1<<8) /* ready for next transfer */
+#define MMC_R1_APP_CMD (1<<5) /* app. commands supported */
+#define MMC_R1_SWITCH_ERROR (1<<7) /* switch command did not succeed */
+
+/* SPI mode R1 response type bits */
+#define SD_SPI_R1_IDLE_STATE (1<<0)
+#define SD_SPI_R1_ERASE_RST (1<<1)
+#define SD_SPI_R1_ILLEGAL_CMD (1<<2)
+#define SD_SPI_R1_CMD_CRC_ERR (1<<3)
+#define SD_SPI_R1_ERASE_SEQ_ERR (1<<4)
+#define SD_SPI_R1_ADDR_ERR (1<<5)
+#define SD_SPI_R1_PARAM_ERR (1<<6)
+#define SD_SPI_R1_NO_RESPONSE (1<<7)
+
+#define SDIO_R1_FUNC_NUM_ERR (1<<4)
+
+/* 48-bit response decoding (32 bits w/o CRC) */
+#define MMC_R1(resp) ((resp)[0])
+#define MMC_R3(resp) ((resp)[0])
+#define MMC_R4(resp) ((resp)[0])
+#define MMC_R5(resp) ((resp)[0])
+#define SD_R6(resp) ((resp)[0])
+#define MMC_R1_CURRENT_STATE(resp) (((resp)[0] >> 9) & 0xf)
+
+/* SPI mode response decoding */
+#define SD_SPI_R1(resp) ((resp)[0] & 0xff)
+#define SD_SPI_R2(resp) ((resp)[0] & 0xffff)
+#define SD_SPI_R3(resp) ((resp)[0])
+#define SD_SPI_R7(resp) ((resp)[0])
+
+/* SPI mode data response decoding */
+#define SD_SPI_DATA_RSP_VALID(resp_byte) (((resp_byte)&0x11)==0x1)
+#define SD_SPI_DATA_RSP(resp_byte) (((resp_byte)>>1)&0x7)
+#define SD_SPI_DATA_ACCEPTED 0x2
+#define SD_SPI_DATA_CRC_ERROR 0x5
+#define SD_SPI_DATA_WR_ERROR 0x6
+
+/* RCA argument and response */
+#define MMC_ARG_RCA(rca) ((rca) << 16)
+#define SD_R6_RCA(resp) (SD_R6((resp)) >> 16)
+
+/* bus width argument */
+#define SD_ARG_BUS_WIDTH_1 0
+#define SD_ARG_BUS_WIDTH_4 2
+
+/* EXT_CSD fields */
+#define EXT_CSD_BUS_WIDTH 183 /* WO */
+#define EXT_CSD_HS_TIMING 185 /* R/W */
+#define EXT_CSD_REV 192 /* RO */
+#define EXT_CSD_STRUCTURE 194 /* RO */
+#define EXT_CSD_CARD_TYPE 196 /* RO */
+#define EXT_CSD_SEC_COUNT 212 /* RO */
+#define EXT_CSD_PWR_CL_26_360 203 /* RO */
+#define EXT_CSD_PWR_CL_52_360 202 /* RO */
+#define EXT_CSD_PWR_CL_26_195 201 /* RO */
+#define EXT_CSD_PWR_CL_52_195 200 /* RO */
+#define EXT_CSD_POWER_CLASS 187 /* R/W */
+#define EXT_CSD_CMD_SET 191 /* R/W */
+#define EXT_CSD_S_CMD_SET 504 /* RO */
+
+/* EXT_CSD field definitions */
+#define EXT_CSD_CMD_SET_NORMAL (1U << 0)
+#define EXT_CSD_CMD_SET_SECURE (1U << 1)
+#define EXT_CSD_CMD_SET_CPSECURE (1U << 2)
+
+/* EXT_CSD_HS_TIMING */
+#define EXT_CSD_HS_TIMING_BC 0
+#define EXT_CSD_HS_TIMING_HS 1
+#define EXT_CSD_HS_TIMING_HS200 2
+#define EXT_CSD_HS_TIMING_HS400 3
+
+/* EXT_CSD_BUS_WIDTH */
+#define EXT_CSD_BUS_WIDTH_1 0
+#define EXT_CSD_BUS_WIDTH_4 1
+#define EXT_CSD_BUS_WIDTH_8 2
+#define EXT_CSD_BUS_WIDTH_4_DDR 5
+#define EXT_CSD_BUS_WIDTH_8_DDR 6
+
+/* EXT_CSD_CARD_TYPE */
+/* The only currently valid values for this field are 0x01, 0x03, 0x07,
+ * 0x0B and 0x0F. */
+#define EXT_CSD_CARD_TYPE_F_26M (1 << 0) /* SDR at "rated voltages */
+#define EXT_CSD_CARD_TYPE_F_52M (1 << 1) /* SDR at "rated voltages */
+#define EXT_CSD_CARD_TYPE_F_52M_1_8V (1 << 2) /* DDR, 1.8V or 3.3V I/O */
+#define EXT_CSD_CARD_TYPE_F_52M_1_2V (1 << 3) /* DDR, 1.2V I/O */
+#define EXT_CSD_CARD_TYPE_26M 0x01
+#define EXT_CSD_CARD_TYPE_52M 0x03
+#define EXT_CSD_CARD_TYPE_52M_V18 0x07
+#define EXT_CSD_CARD_TYPE_52M_V12 0x0b
+#define EXT_CSD_CARD_TYPE_52M_V12_18 0x0f
+
+/* EXT_CSD MMC */
+#define EXT_CSD_MMC_SIZE 512
+
+/* MMC_SWITCH access mode */
+#define MMC_SWITCH_MODE_CMD_SET 0x00 /* Change the command set */
+#define MMC_SWITCH_MODE_SET_BITS 0x01 /* Set bits in value */
+#define MMC_SWITCH_MODE_CLEAR_BITS 0x02 /* Clear bits in value */
+#define MMC_SWITCH_MODE_WRITE_BYTE 0x03 /* Set target to value */
+
+/* MMC R2 response (CSD) */
+#define MMC_CSD_CSDVER(resp) MMC_RSP_BITS((resp), 126, 2)
+#define MMC_CSD_CSDVER_1_0 1
+#define MMC_CSD_CSDVER_2_0 2
+#define MMC_CSD_CSDVER_EXT_CSD 3
+#define MMC_CSD_MMCVER(resp) MMC_RSP_BITS((resp), 122, 4)
+#define MMC_CSD_MMCVER_1_0 0 /* MMC 1.0 - 1.2 */
+#define MMC_CSD_MMCVER_1_4 1 /* MMC 1.4 */
+#define MMC_CSD_MMCVER_2_0 2 /* MMC 2.0 - 2.2 */
+#define MMC_CSD_MMCVER_3_1 3 /* MMC 3.1 - 3.3 */
+#define MMC_CSD_MMCVER_4_0 4 /* MMC 4 */
+#define MMC_CSD_READ_BL_LEN(resp) MMC_RSP_BITS((resp), 80, 4)
+#define MMC_CSD_C_SIZE(resp) MMC_RSP_BITS((resp), 62, 12)
+#define MMC_CSD_CAPACITY(resp) ((MMC_CSD_C_SIZE((resp))+1) << \
+ (MMC_CSD_C_SIZE_MULT((resp))+2))
+#define MMC_CSD_C_SIZE_MULT(resp) MMC_RSP_BITS((resp), 47, 3)
+
+/* MMC v1 R2 response (CID) */
+#define MMC_CID_MID_V1(resp) MMC_RSP_BITS((resp), 104, 24)
+#define MMC_CID_PNM_V1_CPY(resp, pnm) \
+ do { \
+ (pnm)[0] = MMC_RSP_BITS((resp), 96, 8); \
+ (pnm)[1] = MMC_RSP_BITS((resp), 88, 8); \
+ (pnm)[2] = MMC_RSP_BITS((resp), 80, 8); \
+ (pnm)[3] = MMC_RSP_BITS((resp), 72, 8); \
+ (pnm)[4] = MMC_RSP_BITS((resp), 64, 8); \
+ (pnm)[5] = MMC_RSP_BITS((resp), 56, 8); \
+ (pnm)[6] = MMC_RSP_BITS((resp), 48, 8); \
+ (pnm)[7] = '\0'; \
+ } while (0)
+#define MMC_CID_REV_V1(resp) MMC_RSP_BITS((resp), 40, 8)
+#define MMC_CID_PSN_V1(resp) MMC_RSP_BITS((resp), 16, 24)
+#define MMC_CID_MDT_V1(resp) MMC_RSP_BITS((resp), 8, 8)
+
+/* MMC v2 R2 response (CID) */
+#define MMC_CID_MID_V2(resp) MMC_RSP_BITS((resp), 120, 8)
+#define MMC_CID_OID_V2(resp) MMC_RSP_BITS((resp), 104, 16)
+#define MMC_CID_PNM_V2_CPY(resp, pnm) \
+ do { \
+ (pnm)[0] = MMC_RSP_BITS((resp), 96, 8); \
+ (pnm)[1] = MMC_RSP_BITS((resp), 88, 8); \
+ (pnm)[2] = MMC_RSP_BITS((resp), 80, 8); \
+ (pnm)[3] = MMC_RSP_BITS((resp), 72, 8); \
+ (pnm)[4] = MMC_RSP_BITS((resp), 64, 8); \
+ (pnm)[5] = MMC_RSP_BITS((resp), 56, 8); \
+ (pnm)[6] = '\0'; \
+ } while (0)
+#define MMC_CID_PSN_V2(resp) MMC_RSP_BITS((resp), 16, 32)
+
+/* SD R2 response (CSD) */
+#define SD_CSD_CSDVER(resp) MMC_RSP_BITS((resp), 126, 2)
+#define SD_CSD_CSDVER_1_0 0
+#define SD_CSD_CSDVER_2_0 1
+#define SD_CSD_TAAC(resp) MMC_RSP_BITS((resp), 112, 8)
+#define SD_CSD_TAAC_1_5_MSEC 0x26
+#define SD_CSD_NSAC(resp) MMC_RSP_BITS((resp), 104, 8)
+#define SD_CSD_SPEED(resp) MMC_RSP_BITS((resp), 96, 8)
+#define SD_CSD_SPEED_25_MHZ 0x32
+#define SD_CSD_SPEED_50_MHZ 0x5a
+#define SD_CSD_CCC(resp) MMC_RSP_BITS((resp), 84, 12)
+#define SD_CSD_CCC_BASIC (1 << 0) /* basic */
+#define SD_CSD_CCC_BR (1 << 2) /* block read */
+#define SD_CSD_CCC_BW (1 << 4) /* block write */
+#define SD_CSD_CCC_ERASE (1 << 5) /* erase */
+#define SD_CSD_CCC_WP (1 << 6) /* write protection */
+#define SD_CSD_CCC_LC (1 << 7) /* lock card */
+#define SD_CSD_CCC_AS (1 << 8) /*application specific*/
+#define SD_CSD_CCC_IOM (1 << 9) /* I/O mode */
+#define SD_CSD_CCC_SWITCH (1 << 10) /* switch */
+#define SD_CSD_READ_BL_LEN(resp) MMC_RSP_BITS((resp), 80, 4)
+#define SD_CSD_READ_BL_PARTIAL(resp) MMC_RSP_BITS((resp), 79, 1)
+#define SD_CSD_WRITE_BLK_MISALIGN(resp) MMC_RSP_BITS((resp), 78, 1)
+#define SD_CSD_READ_BLK_MISALIGN(resp) MMC_RSP_BITS((resp), 77, 1)
+#define SD_CSD_DSR_IMP(resp) MMC_RSP_BITS((resp), 76, 1)
+#define SD_CSD_C_SIZE(resp) MMC_RSP_BITS((resp), 62, 12)
+#define SD_CSD_CAPACITY(resp) ((SD_CSD_C_SIZE((resp))+1) << \
+ (SD_CSD_C_SIZE_MULT((resp))+2))
+#define SD_CSD_V2_C_SIZE(resp) MMC_RSP_BITS((resp), 48, 22)
+#define SD_CSD_V2_CAPACITY(resp) ((SD_CSD_V2_C_SIZE((resp))+1) << 10)
+#define SD_CSD_V2_BL_LEN 0x9 /* 512 */
+#define SD_CSD_VDD_R_CURR_MIN(resp) MMC_RSP_BITS((resp), 59, 3)
+#define SD_CSD_VDD_R_CURR_MAX(resp) MMC_RSP_BITS((resp), 56, 3)
+#define SD_CSD_VDD_W_CURR_MIN(resp) MMC_RSP_BITS((resp), 53, 3)
+#define SD_CSD_VDD_W_CURR_MAX(resp) MMC_RSP_BITS((resp), 50, 3)
+#define SD_CSD_VDD_RW_CURR_100mA 0x7
+#define SD_CSD_VDD_RW_CURR_80mA 0x6
+#define SD_CSD_C_SIZE_MULT(resp) MMC_RSP_BITS((resp), 47, 3)
+#define SD_CSD_ERASE_BLK_EN(resp) MMC_RSP_BITS((resp), 46, 1)
+#define SD_CSD_SECTOR_SIZE(resp) MMC_RSP_BITS((resp), 39, 7) /* +1 */
+#define SD_CSD_WP_GRP_SIZE(resp) MMC_RSP_BITS((resp), 32, 7) /* +1 */
+#define SD_CSD_WP_GRP_ENABLE(resp) MMC_RSP_BITS((resp), 31, 1)
+#define SD_CSD_R2W_FACTOR(resp) MMC_RSP_BITS((resp), 26, 3)
+#define SD_CSD_WRITE_BL_LEN(resp) MMC_RSP_BITS((resp), 22, 4)
+#define SD_CSD_RW_BL_LEN_2G 0xa
+#define SD_CSD_RW_BL_LEN_1G 0x9
+#define SD_CSD_WRITE_BL_PARTIAL(resp) MMC_RSP_BITS((resp), 21, 1)
+#define SD_CSD_FILE_FORMAT_GRP(resp) MMC_RSP_BITS((resp), 15, 1)
+#define SD_CSD_COPY(resp) MMC_RSP_BITS((resp), 14, 1)
+#define SD_CSD_PERM_WRITE_PROTECT(resp) MMC_RSP_BITS((resp), 13, 1)
+#define SD_CSD_TMP_WRITE_PROTECT(resp) MMC_RSP_BITS((resp), 12, 1)
+#define SD_CSD_FILE_FORMAT(resp) MMC_RSP_BITS((resp), 10, 2)
+
+/* SD R2 response (CID) */
+#define SD_CID_MID(resp) MMC_RSP_BITS((resp), 120, 8)
+#define SD_CID_OID(resp) MMC_RSP_BITS((resp), 104, 16)
+#define SD_CID_PNM_CPY(resp, pnm) \
+ do { \
+ (pnm)[0] = MMC_RSP_BITS((resp), 96, 8); \
+ (pnm)[1] = MMC_RSP_BITS((resp), 88, 8); \
+ (pnm)[2] = MMC_RSP_BITS((resp), 80, 8); \
+ (pnm)[3] = MMC_RSP_BITS((resp), 72, 8); \
+ (pnm)[4] = MMC_RSP_BITS((resp), 64, 8); \
+ (pnm)[5] = '\0'; \
+ } while (0)
+#define SD_CID_REV(resp) MMC_RSP_BITS((resp), 56, 8)
+#define SD_CID_PSN(resp) MMC_RSP_BITS((resp), 24, 32)
+#define SD_CID_MDT(resp) MMC_RSP_BITS((resp), 8, 12)
+
+/* SCR (SD Configuration Register) */
+#define SCR_STRUCTURE(scr) MMC_RSP_BITS((scr), 60, 4)
+#define SCR_STRUCTURE_VER_1_0 0 /* Version 1.0 */
+#define SCR_SD_SPEC(scr) MMC_RSP_BITS((scr), 56, 4)
+#define SCR_SD_SPEC_VER_1_0 0 /* Version 1.0 and 1.01 */
+#define SCR_SD_SPEC_VER_1_10 1 /* Version 1.10 */
+#define SCR_SD_SPEC_VER_2 2 /* Version 2.00 or Version 3.0X */
+#define SCR_DATA_STAT_AFTER_ERASE(scr) MMC_RSP_BITS((scr), 55, 1)
+#define SCR_SD_SECURITY(scr) MMC_RSP_BITS((scr), 52, 3)
+#define SCR_SD_SECURITY_NONE 0 /* no security */
+#define SCR_SD_SECURITY_1_0 1 /* security protocol 1.0 */
+#define SCR_SD_SECURITY_1_0_2 2 /* security protocol 1.0 */
+#define SCR_SD_BUS_WIDTHS(scr) MMC_RSP_BITS((scr), 48, 4)
+#define SCR_SD_BUS_WIDTHS_1BIT (1 << 0) /* 1bit (DAT0) */
+#define SCR_SD_BUS_WIDTHS_4BIT (1 << 2) /* 4bit (DAT0-3) */
+#define SCR_SD_SPEC3(scr) MMC_RSP_BITS((scr), 47, 1)
+#define SCR_EX_SECURITY(scr) MMC_RSP_BITS((scr), 43, 4)
+#define SCR_SD_SPEC4(scr) MMC_RSP_BITS((scr), 42, 1)
+#define SCR_RESERVED(scr) MMC_RSP_BITS((scr), 34, 8)
+#define SCR_CMD_SUPPORT_CMD23(scr) MMC_RSP_BITS((scr), 33, 1)
+#define SCR_CMD_SUPPORT_CMD20(scr) MMC_RSP_BITS((scr), 32, 1)
+#define SCR_RESERVED2(scr) MMC_RSP_BITS((scr), 0, 32)
+
+/* Max supply current in SWITCH_FUNC response (in mA) */
+#define SD_SFUNC_I_MAX(status) (MMC_RSP_BITS((uint32_t *)(status), 496, 16))
+
+/* Supported flags in SWITCH_FUNC response */
+#define SD_SFUNC_SUPPORTED(status, group) \
+ (MMC_RSP_BITS((uint32_t *)(status), 400 + (group - 1) * 16, 16))
+
+/* Selected function in SWITCH_FUNC response */
+#define SD_SFUNC_SELECTED(status, group) \
+ (MMC_RSP_BITS((uint32_t *)(status), 376 + (group - 1) * 4, 4))
+
+/* Busy flags in SWITCH_FUNC response */
+#define SD_SFUNC_BUSY(status, group) \
+ (MMC_RSP_BITS((uint32_t *)(status), 272 + (group - 1) * 16, 16))
+
+/* Version of SWITCH_FUNC response */
+#define SD_SFUNC_VER(status) (MMC_RSP_BITS((uint32_t *)(status), 368, 8))
+
+#define SD_SFUNC_GROUP_MAX 6
+#define SD_SFUNC_FUNC_MAX 15
+
+#define SD_ACCESS_MODE 1 /* Function group 1, Access Mode */
+
+#define SD_ACCESS_MODE_SDR12 0 /* 25 MHz clock */
+#define SD_ACCESS_MODE_SDR25 1 /* 50 MHz clock */
+#define SD_ACCESS_MODE_SDR50 2 /* UHS-I, 100 MHz clock */
+#define SD_ACCESS_MODE_SDR104 3 /* UHS-I, 208 MHz clock */
+#define SD_ACCESS_MODE_DDR50 4 /* UHS-I, 50 MHz clock, DDR */
+
+/**
+ * @brief Extract up to 32 sequential bits from an array of 32-bit words
+ *
+ * Bits within the word are numbered in the increasing order from LSB to MSB.
+ *
+ * As an example, consider 2 32-bit words:
+ *
+ * 0x01234567 0x89abcdef
+ *
+ * On a little-endian system, the bytes are stored in memory as follows:
+ *
+ * 67 45 23 01 ef cd ab 89
+ *
+ * MMC_RSP_BITS will extact bits as follows:
+ *
+ * start=0 len=4 -> result=0x00000007
+ * start=0 len=12 -> result=0x00000567
+ * start=28 len=8 -> result=0x000000f0
+ * start=59 len=5 -> result=0x00000011
+ *
+ * @param src array of words to extract bits from
+ * @param start index of the first bit to extract
+ * @param len number of bits to extract, 1 to 32
+ * @return 32-bit word where requested bits start from LSB
+ */
+static inline uint32_t MMC_RSP_BITS(uint32_t *src, int start, int len)
+{
+ uint32_t mask = (len % 32 == 0) ? UINT_MAX : UINT_MAX >> (32 - (len % 32));
+ size_t word = start / 32;
+ size_t shift = start % 32;
+ uint32_t right = src[word] >> shift;
+ uint32_t left = (len + shift <= 32) ? 0 : src[word + 1] << ((32 - shift) % 32);
+ return (left | right) & mask;
+}
+
+/* SD R4 response (IO OCR) */
+#define SD_IO_OCR_MEM_READY (1<<31)
+#define SD_IO_OCR_NUM_FUNCTIONS(ocr) (((ocr) >> 28) & 0x7)
+#define SD_IO_OCR_MEM_PRESENT (1<<27)
+#define SD_IO_OCR_MASK 0x00fffff0
+
+/* CMD52 arguments */
+#define SD_ARG_CMD52_READ (0<<31)
+#define SD_ARG_CMD52_WRITE (1<<31)
+#define SD_ARG_CMD52_FUNC_SHIFT 28
+#define SD_ARG_CMD52_FUNC_MASK 0x7
+#define SD_ARG_CMD52_EXCHANGE (1<<27)
+#define SD_ARG_CMD52_REG_SHIFT 9
+#define SD_ARG_CMD52_REG_MASK 0x1ffff
+#define SD_ARG_CMD52_DATA_SHIFT 0
+#define SD_ARG_CMD52_DATA_MASK 0xff
+#define SD_R5_DATA(resp) ((resp)[0] & 0xff)
+
+/* CMD53 arguments */
+#define SD_ARG_CMD53_READ (0<<31)
+#define SD_ARG_CMD53_WRITE (1<<31)
+#define SD_ARG_CMD53_FUNC_SHIFT 28
+#define SD_ARG_CMD53_FUNC_MASK 0x7
+#define SD_ARG_CMD53_BLOCK_MODE (1<<27)
+#define SD_ARG_CMD53_INCREMENT (1<<26)
+#define SD_ARG_CMD53_REG_SHIFT 9
+#define SD_ARG_CMD53_REG_MASK 0x1ffff
+#define SD_ARG_CMD53_LENGTH_SHIFT 0
+#define SD_ARG_CMD53_LENGTH_MASK 0x1ff
+#define SD_ARG_CMD53_LENGTH_MAX 512
+
+/* Card Common Control Registers (CCCR) */
+#define SD_IO_CCCR_START 0x00000
+#define SD_IO_CCCR_SIZE 0x100
+#define SD_IO_CCCR_FN_ENABLE 0x02
+#define SD_IO_CCCR_FN_READY 0x03
+#define SD_IO_CCCR_INT_ENABLE 0x04
+#define SD_IO_CCCR_INT_PENDING 0x05
+#define SD_IO_CCCR_CTL 0x06
+#define CCCR_CTL_RES (1<<3)
+#define SD_IO_CCCR_BUS_WIDTH 0x07
+#define CCCR_BUS_WIDTH_1 (0<<0)
+#define CCCR_BUS_WIDTH_4 (2<<0)
+#define CCCR_BUS_WIDTH_8 (3<<0)
+#define CCCR_BUS_WIDTH_ECSI (1<<5)
+#define SD_IO_CCCR_CARD_CAP 0x08
+#define CCCR_CARD_CAP_LSC BIT(6)
+#define CCCR_CARD_CAP_4BLS BIT(7)
+#define SD_IO_CCCR_CISPTR 0x09
+#define SD_IO_CCCR_BLKSIZEL 0x10
+#define SD_IO_CCCR_BLKSIZEH 0x11
+#define SD_IO_CCCR_HIGHSPEED 0x13
+#define CCCR_HIGHSPEED_SUPPORT BIT(0)
+#define CCCR_HIGHSPEED_ENABLE BIT(1)
+
+/* Function Basic Registers (FBR) */
+#define SD_IO_FBR_START 0x00100
+#define SD_IO_FBR_SIZE 0x00700
+
+/* Card Information Structure (CIS) */
+#define SD_IO_CIS_START 0x01000
+#define SD_IO_CIS_SIZE 0x17000
+
+/* CIS tuple codes (based on PC Card 16) */
+#define CISTPL_CODE_NULL 0x00
+#define CISTPL_CODE_DEVICE 0x01
+#define CISTPL_CODE_CHKSUM 0x10
+#define CISTPL_CODE_VERS1 0x15
+#define CISTPL_CODE_ALTSTR 0x16
+#define CISTPL_CODE_CONFIG 0x1A
+#define CISTPL_CODE_CFTABLE_ENTRY 0x1B
+#define CISTPL_CODE_MANFID 0x20
+#define CISTPL_CODE_FUNCID 0x21
+#define TPLFID_FUNCTION_SDIO 0x0c
+#define CISTPL_CODE_FUNCE 0x22
+#define CISTPL_CODE_VENDER_BEGIN 0x80
+#define CISTPL_CODE_VENDER_END 0x8F
+#define CISTPL_CODE_SDIO_STD 0x91
+#define CISTPL_CODE_SDIO_EXT 0x92
+#define CISTPL_CODE_END 0xFF
+
+
+/* Timing */
+#define SDMMC_TIMING_LEGACY 0
+#define SDMMC_TIMING_HIGHSPEED 1
+#define SDMMC_TIMING_MMC_DDR52 2
+
+#include "py/runtime.h"
+
+// Logging macros
+// #define ESP_LOGD(tag, string, ...) mp_printf(&mp_plat_print, string "\n" __VA_OPT__(,) __VA_ARGS__)
+// #define ESP_LOGV(tag, string, ...) mp_printf(&mp_plat_print, string "\n" __VA_OPT__(,) __VA_ARGS__)
+// #define ESP_LOGW(tag, string, ...) mp_printf(&mp_plat_print, string "\n" __VA_OPT__(,) __VA_ARGS__)
+// #define ESP_LOGE(tag, string, ...) mp_printf(&mp_plat_print, string "\n" __VA_OPT__(,) __VA_ARGS__)
+
+#define ESP_LOGD(tag, string, ...)
+#define ESP_LOGV(tag, string, ...)
+#define ESP_LOGW(tag, string, ...)
+#define ESP_LOGE(tag, string, ...)
+#define TSD_MIN(_x, _y) ( ( (_x) < (_y) ) ? (_x) : (_y) )
+
+#endif //_SDMMC_DEFS_H_
diff --git a/circuitpython/lib/sdmmc/include/sdmmc_types.h b/circuitpython/lib/sdmmc/include/sdmmc_types.h
new file mode 100644
index 0000000..79a15e2
--- /dev/null
+++ b/circuitpython/lib/sdmmc/include/sdmmc_types.h
@@ -0,0 +1,199 @@
+/*
+ * Copyright (c) 2006 Uwe Stuehler <uwe@openbsd.org>
+ * Adaptations to ESP-IDF Copyright (c) 2016 Espressif Systems (Shanghai) PTE LTD
+ *
+ * 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.
+ */
+
+#ifndef _SDMMC_TYPES_H_
+#define _SDMMC_TYPES_H_
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <stddef.h>
+
+/**
+ * Decoded values from SD card Card Specific Data register
+ */
+typedef struct {
+ int csd_ver; /*!< CSD structure format */
+ int mmc_ver; /*!< MMC version (for CID format) */
+ int capacity; /*!< total number of sectors */
+ int sector_size; /*!< sector size in bytes */
+ int read_block_len; /*!< block length for reads */
+ int card_command_class; /*!< Card Command Class for SD */
+ int tr_speed; /*!< Max transfer speed */
+} sdmmc_csd_t;
+
+/**
+ * Decoded values from SD card Card IDentification register
+ */
+typedef struct {
+ int mfg_id; /*!< manufacturer identification number */
+ int oem_id; /*!< OEM/product identification number */
+ char name[8]; /*!< product name (MMC v1 has the longest) */
+ int revision; /*!< product revision */
+ int serial; /*!< product serial number */
+ int date; /*!< manufacturing date */
+} sdmmc_cid_t;
+
+/**
+ * Decoded values from SD Configuration Register
+ */
+typedef struct {
+ int sd_spec; /*!< SD Physical layer specification version, reported by card */
+ int bus_width; /*!< bus widths supported by card: BIT(0) — 1-bit bus, BIT(2) — 4-bit bus */
+} sdmmc_scr_t;
+
+/**
+ * Decoded values of Extended Card Specific Data
+ */
+typedef struct {
+ uint8_t power_class; /*!< Power class used by the card */
+} sdmmc_ext_csd_t;
+
+/**
+ * SD/MMC command response buffer
+ */
+typedef uint32_t sdmmc_response_t[4];
+
+/**
+ * SD SWITCH_FUNC response buffer
+ */
+typedef struct {
+ uint32_t data[512 / 8 / sizeof(uint32_t)]; /*!< response data */
+} sdmmc_switch_func_rsp_t;
+
+typedef enum {
+ SDMMC_OK = 0,
+ SDMMC_ERR_NOT_SUPPORTED = 1,
+ SDMMC_ERR_INVALID_RESPONSE = 2,
+ SDMMC_ERR_TIMEOUT = 3,
+ SDMMC_ERR_NO_MEM = 4,
+ SDMMC_ERR_INVALID_SIZE = 5,
+ SDMMC_ERR_NO_CARD = 6,
+ SDMMC_ERR_INVALID_ARG = 7,
+ SDMMC_ERR_BUSY = 8,
+} sdmmc_err_t;
+
+/**
+ * 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 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
+#define SCF_CMD_ADTC 0x0010
+#define SCF_CMD_BC 0x0020
+#define SCF_CMD_BCR 0x0030
+#define SCF_CMD_READ 0x0040 /*!< read command (data expected) */
+#define SCF_RSP_BSY 0x0100
+#define SCF_RSP_136 0x0200
+#define SCF_RSP_CRC 0x0400
+#define SCF_RSP_IDX 0x0800
+#define SCF_RSP_PRESENT 0x1000
+/* 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)
+#define SCF_RSP_R2 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_136)
+#define SCF_RSP_R3 (SCF_RSP_PRESENT)
+#define SCF_RSP_R4 (SCF_RSP_PRESENT)
+#define SCF_RSP_R5 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX)
+#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 */
+#define SCF_WAIT_BUSY 0x2000 /*!< Wait for completion of card busy signal before returning */
+#define SCF_AUTO_STOP 0x4000 /*!< Auto stop with command 12 */
+/** @endcond */
+ sdmmc_err_t error; /*!< error returned from transfer */
+ int timeout_ms; /*!< response timeout, in milliseconds */
+} sdmmc_command_t;
+
+/**
+ * SD/MMC Host description
+ *
+ * This structure defines properties of SD/MMC host and functions
+ * of SD/MMC host which can be used by upper layers.
+ */
+#define BIT(x) (1 << x)
+
+typedef struct {
+ uint32_t flags; /*!< flags defining host properties */
+#define SDMMC_HOST_FLAG_1BIT BIT(0) /*!< host supports 1-line SD and MMC protocol */
+#define SDMMC_HOST_FLAG_4BIT BIT(1) /*!< host supports 4-line SD and MMC protocol */
+#define SDMMC_HOST_FLAG_8BIT BIT(2) /*!< host supports 8-line MMC protocol */
+#define SDMMC_HOST_FLAG_SPI BIT(3) /*!< host supports SPI protocol */
+#define SDMMC_HOST_FLAG_DDR BIT(4) /*!< host supports DDR mode for SD/MMC */
+#define SDMMC_HOST_FLAG_DEINIT_ARG BIT(5) /*!< host `deinit` function called with the slot argument */
+ int slot; /*!< slot number, to be passed to host functions */
+ int max_freq_khz; /*!< max frequency supported by the host */
+#define SDMMC_FREQ_DEFAULT 20000 /*!< SD/MMC Default speed (limited by clock divider) */
+#define SDMMC_FREQ_HIGHSPEED 40000 /*!< SD High speed (limited by clock divider) */
+#define SDMMC_FREQ_PROBING 400 /*!< SD/MMC probing speed */
+#define SDMMC_FREQ_52M 52000 /*!< MMC 52MHz speed */
+#define SDMMC_FREQ_26M 26000 /*!< MMC 26MHz speed */
+ float io_voltage; /*!< I/O voltage used by the controller (voltage switching is not supported) */
+ sdmmc_err_t (*init)(void); /*!< Host function to initialize the driver */
+ sdmmc_err_t (*set_bus_width)(int slot, size_t width); /*!< host function to set bus width */
+ size_t (*get_bus_width)(int slot); /*!< host function to get bus width */
+ sdmmc_err_t (*set_bus_ddr_mode)(int slot, bool ddr_enable); /*!< host function to set DDR mode */
+ sdmmc_err_t (*set_card_clk)(int slot, uint32_t freq_khz); /*!< host function to set card clock frequency */
+ sdmmc_err_t (*do_transaction)(int slot, sdmmc_command_t* cmdinfo); /*!< host function to do a transaction */
+ union {
+ sdmmc_err_t (*deinit)(void); /*!< host function to deinitialize the driver */
+ sdmmc_err_t (*deinit_p)(int slot); /*!< host function to deinitialize the driver, called with the `slot` */
+ };
+ sdmmc_err_t (*io_int_enable)(int slot); /*!< Host function to enable SDIO interrupt line */
+ sdmmc_err_t (*io_int_wait)(int slot, int timeout_ms); /*!< Host function to wait for SDIO interrupt line to be active */
+ int command_timeout_ms; /*!< timeout, in milliseconds, of a single command. Set to 0 to use the default value. */
+} sdmmc_host_t;
+
+/**
+ * SD/MMC card information structure
+ */
+typedef struct {
+ sdmmc_host_t host; /*!< Host with which the card is associated */
+ uint32_t ocr; /*!< OCR (Operation Conditions Register) value */
+ union {
+ sdmmc_cid_t cid; /*!< decoded CID (Card IDentification) register value */
+ sdmmc_response_t raw_cid; /*!< raw CID of MMC card to be decoded
+ after the CSD is fetched in the data transfer mode*/
+ };
+ sdmmc_csd_t csd; /*!< decoded CSD (Card-Specific Data) register value */
+ sdmmc_scr_t scr; /*!< decoded SCR (SD card Configuration Register) value */
+ sdmmc_ext_csd_t ext_csd; /*!< decoded EXT_CSD (Extended Card Specific Data) register value */
+ uint16_t rca; /*!< RCA (Relative Card Address) */
+ uint16_t max_freq_khz; /*!< Maximum frequency, in kHz, supported by the card */
+ uint32_t is_mem : 1; /*!< Bit indicates if the card is a memory card */
+ uint32_t is_sdio : 1; /*!< Bit indicates if the card is an IO card */
+ uint32_t is_mmc : 1; /*!< Bit indicates if the card is MMC */
+ uint32_t num_io_functions : 3; /*!< If is_sdio is 1, contains the number of IO functions on the card */
+ uint32_t log_bus_width : 2; /*!< log2(bus width supported by card) */
+ uint32_t is_ddr : 1; /*!< Card supports DDR mode */
+ uint32_t reserved : 23; /*!< Reserved for future expansion */
+} sdmmc_card_t;
+
+// OS abstraction layer (copied from TinyUSB)
+void osal_task_delay(uint32_t msec);
+
+#endif // _SDMMC_TYPES_H_