mirror of
https://github.com/0xFEEDC0DE64/arduino-esp32.git
synced 2025-07-13 02:36:31 +02:00
Add FS, SD and SD_MMC (#256)
This commit is contained in:
97
libraries/SD_MMC/src/SD_MMC.cpp
Normal file
97
libraries/SD_MMC/src/SD_MMC.cpp
Normal file
@ -0,0 +1,97 @@
|
||||
// Copyright 2015-2016 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.
|
||||
|
||||
#include "vfs_api.h"
|
||||
|
||||
extern "C" {
|
||||
#include <sys/unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <dirent.h>
|
||||
#include "esp_vfs_fat.h"
|
||||
#include "driver/sdmmc_host.h"
|
||||
#include "driver/sdmmc_defs.h"
|
||||
#include "sdmmc_cmd.h"
|
||||
}
|
||||
#include "SD_MMC.h"
|
||||
|
||||
using namespace fs;
|
||||
/*
|
||||
|
||||
*/
|
||||
|
||||
SDMMCFS::SDMMCFS(FSImplPtr impl)
|
||||
: FS(impl), _card(NULL)
|
||||
{}
|
||||
|
||||
bool SDMMCFS::begin(const char * mountpoint)
|
||||
{
|
||||
if(_card) {
|
||||
return true;
|
||||
}
|
||||
//mount
|
||||
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
|
||||
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
|
||||
host.max_freq_khz = SDMMC_FREQ_HIGHSPEED;
|
||||
//host.flags = SDMMC_HOST_FLAG_1BIT; //use 1-line SD mode
|
||||
|
||||
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
|
||||
.format_if_mount_failed = false,
|
||||
.max_files = 5
|
||||
};
|
||||
|
||||
esp_err_t ret = esp_vfs_fat_sdmmc_mount(mountpoint, &host, &slot_config, &mount_config, &_card);
|
||||
if (ret != ESP_OK) {
|
||||
if (ret == ESP_FAIL) {
|
||||
log_e("Failed to mount filesystem. If you want the card to be formatted, set format_if_mount_failed = true.");
|
||||
} else if (ret == ESP_ERR_INVALID_STATE) {
|
||||
_impl->mountpoint(mountpoint);
|
||||
log_w("SD Already mounted");
|
||||
return true;
|
||||
} else {
|
||||
log_e("Failed to initialize the card (%d). Make sure SD card lines have pull-up resistors in place.", ret);
|
||||
}
|
||||
_card = NULL;
|
||||
return false;
|
||||
}
|
||||
_impl->mountpoint(mountpoint);
|
||||
return true;
|
||||
}
|
||||
|
||||
void SDMMCFS::end()
|
||||
{
|
||||
if(_card) {
|
||||
esp_vfs_fat_sdmmc_unmount();
|
||||
_impl->mountpoint(NULL);
|
||||
_card = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
sdcard_type_t SDMMCFS::cardType()
|
||||
{
|
||||
if(!_card) {
|
||||
return CARD_NONE;
|
||||
}
|
||||
return (_card->ocr & SD_OCR_SDHC_CAP)?CARD_SDHC:CARD_SD;
|
||||
}
|
||||
|
||||
uint64_t SDMMCFS::cardSize()
|
||||
{
|
||||
if(!_card) {
|
||||
return 0;
|
||||
}
|
||||
return (uint64_t)_card->csd.capacity * _card->csd.sector_size;
|
||||
}
|
||||
|
||||
|
||||
SDMMCFS SD_MMC = SDMMCFS(FSImplPtr(new VFSImpl()));
|
41
libraries/SD_MMC/src/SD_MMC.h
Normal file
41
libraries/SD_MMC/src/SD_MMC.h
Normal file
@ -0,0 +1,41 @@
|
||||
// Copyright 2015-2016 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.
|
||||
#ifndef _SDMMC_H_
|
||||
#define _SDMMC_H_
|
||||
|
||||
#include "FS.h"
|
||||
#include "driver/sdmmc_types.h"
|
||||
#include "sd_defines.h"
|
||||
|
||||
namespace fs
|
||||
{
|
||||
|
||||
class SDMMCFS : public FS
|
||||
{
|
||||
protected:
|
||||
sdmmc_card_t* _card;
|
||||
|
||||
public:
|
||||
SDMMCFS(FSImplPtr impl);
|
||||
bool begin(const char * mountpoint="/sdcard");
|
||||
void end();
|
||||
sdcard_type_t cardType();
|
||||
uint64_t cardSize();
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
extern fs::SDMMCFS SD_MMC;
|
||||
|
||||
#endif /* _SDMMC_H_ */
|
25
libraries/SD_MMC/src/sd_defines.h
Normal file
25
libraries/SD_MMC/src/sd_defines.h
Normal file
@ -0,0 +1,25 @@
|
||||
// Copyright 2015-2016 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.
|
||||
#ifndef _SD_DEFINES_H_
|
||||
#define _SD_DEFINES_H_
|
||||
|
||||
typedef enum {
|
||||
CARD_NONE,
|
||||
CARD_MMC,
|
||||
CARD_SD,
|
||||
CARD_SDHC,
|
||||
CARD_UNKNOWN
|
||||
} sdcard_type_t;
|
||||
|
||||
#endif /* _SD_DISKIO_H_ */
|
Reference in New Issue
Block a user