Skip to content

Commit ad7fd54

Browse files
committed
Merge branch 'feature/add_fatfs' into 'master'
feat(fatfs): add FATFS for ESP8266 See merge request sdk/ESP8266_RTOS_SDK!1319
2 parents d20cf8f + 3690eab commit ad7fd54

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+28876
-3
lines changed
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/*
2+
* Copyright (c) 2006 Uwe Stuehler <[email protected]>
3+
* Adaptations to ESP-IDF Copyright (c) 2016 Espressif Systems (Shanghai) PTE LTD
4+
*
5+
* Permission to use, copy, modify, and distribute this software for any
6+
* purpose with or without fee is hereby granted, provided that the above
7+
* copyright notice and this permission notice appear in all copies.
8+
*
9+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16+
*/
17+
18+
#ifndef _SDMMC_TYPES_H_
19+
#define _SDMMC_TYPES_H_
20+
21+
#include <stdint.h>
22+
#include <stddef.h>
23+
#include "esp_err.h"
24+
#include "freertos/FreeRTOS.h"
25+
26+
/**
27+
* Decoded values from SD card Card Specific Data register
28+
*/
29+
typedef struct {
30+
int csd_ver; /*!< CSD structure format */
31+
int mmc_ver; /*!< MMC version (for CID format) */
32+
int capacity; /*!< total number of sectors */
33+
int sector_size; /*!< sector size in bytes */
34+
int read_block_len; /*!< block length for reads */
35+
int card_command_class; /*!< Card Command Class for SD */
36+
int tr_speed; /*!< Max transfer speed */
37+
} sdmmc_csd_t;
38+
39+
/**
40+
* Decoded values from SD card Card IDentification register
41+
*/
42+
typedef struct {
43+
int mfg_id; /*!< manufacturer identification number */
44+
int oem_id; /*!< OEM/product identification number */
45+
char name[8]; /*!< product name (MMC v1 has the longest) */
46+
int revision; /*!< product revision */
47+
int serial; /*!< product serial number */
48+
int date; /*!< manufacturing date */
49+
} sdmmc_cid_t;
50+
51+
/**
52+
* Decoded values from SD Configuration Register
53+
*/
54+
typedef struct {
55+
int sd_spec; /*!< SD Physical layer specification version, reported by card */
56+
int bus_width; /*!< bus widths supported by card: BIT(0) — 1-bit bus, BIT(2) — 4-bit bus */
57+
} sdmmc_scr_t;
58+
59+
/**
60+
* Decoded values of Extended Card Specific Data
61+
*/
62+
typedef struct {
63+
uint8_t power_class; /*!< Power class used by the card */
64+
} sdmmc_ext_csd_t;
65+
66+
/**
67+
* SD/MMC command response buffer
68+
*/
69+
typedef uint32_t sdmmc_response_t[4];
70+
71+
/**
72+
* SD SWITCH_FUNC response buffer
73+
*/
74+
typedef struct {
75+
uint32_t data[512 / 8 / sizeof(uint32_t)]; /*!< response data */
76+
} sdmmc_switch_func_rsp_t;
77+
78+
/**
79+
* SD/MMC command information
80+
*/
81+
typedef struct {
82+
uint32_t opcode; /*!< SD or MMC command index */
83+
uint32_t arg; /*!< SD/MMC command argument */
84+
sdmmc_response_t response; /*!< response buffer */
85+
void* data; /*!< buffer to send or read into */
86+
size_t datalen; /*!< length of data buffer */
87+
size_t blklen; /*!< block length */
88+
int flags; /*!< see below */
89+
/** @cond */
90+
#define SCF_ITSDONE 0x0001 /*!< command is complete */
91+
#define SCF_CMD(flags) ((flags) & 0x00f0)
92+
#define SCF_CMD_AC 0x0000
93+
#define SCF_CMD_ADTC 0x0010
94+
#define SCF_CMD_BC 0x0020
95+
#define SCF_CMD_BCR 0x0030
96+
#define SCF_CMD_READ 0x0040 /*!< read command (data expected) */
97+
#define SCF_RSP_BSY 0x0100
98+
#define SCF_RSP_136 0x0200
99+
#define SCF_RSP_CRC 0x0400
100+
#define SCF_RSP_IDX 0x0800
101+
#define SCF_RSP_PRESENT 0x1000
102+
/* response types */
103+
#define SCF_RSP_R0 0 /*!< none */
104+
#define SCF_RSP_R1 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX)
105+
#define SCF_RSP_R1B (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX|SCF_RSP_BSY)
106+
#define SCF_RSP_R2 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_136)
107+
#define SCF_RSP_R3 (SCF_RSP_PRESENT)
108+
#define SCF_RSP_R4 (SCF_RSP_PRESENT)
109+
#define SCF_RSP_R5 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX)
110+
#define SCF_RSP_R5B (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX|SCF_RSP_BSY)
111+
#define SCF_RSP_R6 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX)
112+
#define SCF_RSP_R7 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX)
113+
/* special flags */
114+
#define SCF_WAIT_BUSY 0x2000 /*!< Wait for completion of card busy signal before returning */
115+
/** @endcond */
116+
esp_err_t error; /*!< error returned from transfer */
117+
int timeout_ms; /*!< response timeout, in milliseconds */
118+
} sdmmc_command_t;
119+
120+
/**
121+
* SD/MMC Host description
122+
*
123+
* This structure defines properties of SD/MMC host and functions
124+
* of SD/MMC host which can be used by upper layers.
125+
*/
126+
typedef struct {
127+
uint32_t flags; /*!< flags defining host properties */
128+
#define SDMMC_HOST_FLAG_1BIT BIT(0) /*!< host supports 1-line SD and MMC protocol */
129+
#define SDMMC_HOST_FLAG_4BIT BIT(1) /*!< host supports 4-line SD and MMC protocol */
130+
#define SDMMC_HOST_FLAG_8BIT BIT(2) /*!< host supports 8-line MMC protocol */
131+
#define SDMMC_HOST_FLAG_SPI BIT(3) /*!< host supports SPI protocol */
132+
#define SDMMC_HOST_FLAG_DDR BIT(4) /*!< host supports DDR mode for SD/MMC */
133+
int slot; /*!< slot number, to be passed to host functions */
134+
int max_freq_khz; /*!< max frequency supported by the host */
135+
#define SDMMC_FREQ_DEFAULT 20000 /*!< SD/MMC Default speed (limited by clock divider) */
136+
#define SDMMC_FREQ_HIGHSPEED 40000 /*!< SD High speed (limited by clock divider) */
137+
#define SDMMC_FREQ_PROBING 400 /*!< SD/MMC probing speed */
138+
#define SDMMC_FREQ_52M 52000 /*!< MMC 52MHz speed */
139+
#define SDMMC_FREQ_26M 26000 /*!< MMC 26MHz speed */
140+
float io_voltage; /*!< I/O voltage used by the controller (voltage switching is not supported) */
141+
esp_err_t (*init)(void); /*!< Host function to initialize the driver */
142+
esp_err_t (*set_bus_width)(int slot, size_t width); /*!< host function to set bus width */
143+
size_t (*get_bus_width)(int slot); /*!< host function to get bus width */
144+
esp_err_t (*set_bus_ddr_mode)(int slot, bool ddr_enable); /*!< host function to set DDR mode */
145+
esp_err_t (*set_card_clk)(int slot, uint32_t freq_khz); /*!< host function to set card clock frequency */
146+
esp_err_t (*do_transaction)(int slot, sdmmc_command_t* cmdinfo); /*!< host function to do a transaction */
147+
esp_err_t (*deinit)(void); /*!< host function to deinitialize the driver */
148+
esp_err_t (*io_int_enable)(int slot); /*!< Host function to enable SDIO interrupt line */
149+
esp_err_t (*io_int_wait)(int slot, TickType_t timeout_ticks); /*!< Host function to wait for SDIO interrupt line to be active */
150+
int command_timeout_ms; /*!< timeout, in milliseconds, of a single command. Set to 0 to use the default value. */
151+
} sdmmc_host_t;
152+
153+
/**
154+
* SD/MMC card information structure
155+
*/
156+
typedef struct {
157+
sdmmc_host_t host; /*!< Host with which the card is associated */
158+
uint32_t ocr; /*!< OCR (Operation Conditions Register) value */
159+
union {
160+
sdmmc_cid_t cid; /*!< decoded CID (Card IDentification) register value */
161+
sdmmc_response_t raw_cid; /*!< raw CID of MMC card to be decoded
162+
after the CSD is fetched in the data transfer mode*/
163+
};
164+
sdmmc_csd_t csd; /*!< decoded CSD (Card-Specific Data) register value */
165+
sdmmc_scr_t scr; /*!< decoded SCR (SD card Configuration Register) value */
166+
sdmmc_ext_csd_t ext_csd; /*!< decoded EXT_CSD (Extended Card Specific Data) register value */
167+
uint16_t rca; /*!< RCA (Relative Card Address) */
168+
uint16_t max_freq_khz; /*!< Maximum frequency, in kHz, supported by the card */
169+
uint32_t is_mem : 1; /*!< Bit indicates if the card is a memory card */
170+
uint32_t is_sdio : 1; /*!< Bit indicates if the card is an IO card */
171+
uint32_t is_mmc : 1; /*!< Bit indicates if the card is MMC */
172+
uint32_t num_io_functions : 3; /*!< If is_sdio is 1, contains the number of IO functions on the card */
173+
uint32_t log_bus_width : 2; /*!< log2(bus width supported by card) */
174+
uint32_t is_ddr : 1; /*!< Card supports DDR mode */
175+
uint32_t reserved : 23; /*!< Reserved for future expansion */
176+
} sdmmc_card_t;
177+
178+
179+
#endif // _SDMMC_TYPES_H_
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
// Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
#include <stdint.h>
18+
#include <stddef.h>
19+
#include "esp_err.h"
20+
#include "sdmmc_types.h"
21+
#include "driver/gpio.h"
22+
// #include "driver/spi_master.h"
23+
24+
#ifdef __cplusplus
25+
extern "C" {
26+
#endif
27+
28+
/**
29+
* @brief Default sdmmc_host_t structure initializer for SD over SPI driver
30+
*
31+
* Uses SPI mode and max frequency set to 20MHz
32+
*
33+
* 'slot' can be set to one of HSPI_HOST, VSPI_HOST.
34+
*/
35+
/*#define SDSPI_HOST_DEFAULT() {\
36+
.flags = SDMMC_HOST_FLAG_SPI, \
37+
.slot = HSPI_HOST, \
38+
.max_freq_khz = SDMMC_FREQ_DEFAULT, \
39+
.io_voltage = 3.3f, \
40+
.init = &sdspi_host_init, \
41+
.set_bus_width = NULL, \
42+
.get_bus_width = NULL, \
43+
.set_bus_ddr_mode = NULL, \
44+
.set_card_clk = &sdspi_host_set_card_clk, \
45+
.do_transaction = &sdspi_host_do_transaction, \
46+
.deinit = &sdspi_host_deinit, \
47+
.io_int_enable = &sdspi_host_io_int_enable, \
48+
.io_int_wait = &sdspi_host_io_int_wait, \
49+
.command_timeout_ms = 0, \
50+
}*/
51+
52+
/**
53+
* Extra configuration for SPI host
54+
*/
55+
typedef struct {
56+
gpio_num_t gpio_miso; ///< GPIO number of MISO signal
57+
gpio_num_t gpio_mosi; ///< GPIO number of MOSI signal
58+
gpio_num_t gpio_sck; ///< GPIO number of SCK signal
59+
gpio_num_t gpio_cs; ///< GPIO number of CS signal
60+
gpio_num_t gpio_cd; ///< GPIO number of card detect signal
61+
gpio_num_t gpio_wp; ///< GPIO number of write protect signal
62+
gpio_num_t gpio_int; ///< GPIO number of interrupt line (input) for SDIO card.
63+
int dma_channel; ///< DMA channel to be used by SPI driver (1 or 2)
64+
} sdspi_slot_config_t;
65+
66+
#define SDSPI_SLOT_NO_CD GPIO_NUM_NC ///< indicates that card detect line is not used
67+
#define SDSPI_SLOT_NO_WP GPIO_NUM_NC ///< indicates that write protect line is not used
68+
#define SDSPI_SLOT_NO_INT GPIO_NUM_NC ///< indicates that interrupt line is not used
69+
70+
/**
71+
* Macro defining default configuration of SPI host
72+
*/
73+
#define SDSPI_SLOT_CONFIG_DEFAULT() {\
74+
.gpio_miso = GPIO_NUM_2, \
75+
.gpio_mosi = GPIO_NUM_15, \
76+
.gpio_sck = GPIO_NUM_14, \
77+
.gpio_cs = GPIO_NUM_13, \
78+
.gpio_cd = SDSPI_SLOT_NO_CD, \
79+
.gpio_wp = SDSPI_SLOT_NO_WP, \
80+
.gpio_int = GPIO_NUM_NC, \
81+
.dma_channel = 1 \
82+
}
83+
84+
/**
85+
* @brief Initialize SD SPI driver
86+
*
87+
* @note This function is not thread safe
88+
*
89+
* @return
90+
* - ESP_OK on success
91+
* - other error codes may be returned in future versions
92+
*/
93+
esp_err_t sdspi_host_init(void);
94+
95+
/**
96+
* @brief Initialize SD SPI driver for the specific SPI controller
97+
*
98+
* @note This function is not thread safe
99+
*
100+
* @note The SDIO over sdspi needs an extra interrupt line. Call ``gpio_install_isr_service()`` before this function.
101+
*
102+
* @param slot SPI controller to use (HSPI_HOST or VSPI_HOST)
103+
* @param slot_config pointer to slot configuration structure
104+
*
105+
* @return
106+
* - ESP_OK on success
107+
* - ESP_ERR_INVALID_ARG if sdspi_init_slot has invalid arguments
108+
* - ESP_ERR_NO_MEM if memory can not be allocated
109+
* - other errors from the underlying spi_master and gpio drivers
110+
*/
111+
esp_err_t sdspi_host_init_slot(int slot, const sdspi_slot_config_t* slot_config);
112+
113+
/**
114+
* @brief Send command to the card and get response
115+
*
116+
* This function returns when command is sent and response is received,
117+
* or data is transferred, or timeout occurs.
118+
*
119+
* @note This function is not thread safe w.r.t. init/deinit functions,
120+
* and bus width/clock speed configuration functions. Multiple tasks
121+
* can call sdspi_host_do_transaction as long as other sdspi_host_*
122+
* functions are not called.
123+
*
124+
* @param slot SPI controller (HSPI_HOST or VSPI_HOST)
125+
* @param cmdinfo pointer to structure describing command and data to transfer
126+
* @return
127+
* - ESP_OK on success
128+
* - ESP_ERR_TIMEOUT if response or data transfer has timed out
129+
* - ESP_ERR_INVALID_CRC if response or data transfer CRC check has failed
130+
* - ESP_ERR_INVALID_RESPONSE if the card has sent an invalid response
131+
*/
132+
esp_err_t sdspi_host_do_transaction(int slot, sdmmc_command_t *cmdinfo);
133+
134+
/**
135+
* @brief Set card clock frequency
136+
*
137+
* Currently only integer fractions of 40MHz clock can be used.
138+
* For High Speed cards, 40MHz can be used.
139+
* For Default Speed cards, 20MHz can be used.
140+
*
141+
* @note This function is not thread safe
142+
*
143+
* @param slot SPI controller (HSPI_HOST or VSPI_HOST)
144+
* @param freq_khz card clock frequency, in kHz
145+
* @return
146+
* - ESP_OK on success
147+
* - other error codes may be returned in the future
148+
*/
149+
esp_err_t sdspi_host_set_card_clk(int slot, uint32_t freq_khz);
150+
151+
152+
/**
153+
* @brief Release resources allocated using sdspi_host_init
154+
*
155+
* @note This function is not thread safe
156+
*
157+
* @return
158+
* - ESP_OK on success
159+
* - ESP_ERR_INVALID_STATE if sdspi_host_init function has not been called
160+
*/
161+
esp_err_t sdspi_host_deinit(void);
162+
163+
/**
164+
* @brief Enable SDIO interrupt.
165+
*
166+
* @param slot SPI controller to use (HSPI_HOST or VSPI_HOST)
167+
*
168+
* @return
169+
* - ESP_OK on success
170+
*/
171+
esp_err_t sdspi_host_io_int_enable(int slot);
172+
173+
/**
174+
* @brief Wait for SDIO interrupt until timeout.
175+
*
176+
* @param slot SPI controller to use (HSPI_HOST or VSPI_HOST)
177+
* @param timeout_ticks Ticks to wait before timeout.
178+
*
179+
* @return
180+
* - ESP_OK on success
181+
*/
182+
esp_err_t sdspi_host_io_int_wait(int slot, TickType_t timeout_ticks);
183+
184+
#ifdef __cplusplus
185+
}
186+
#endif

0 commit comments

Comments
 (0)