Note : In the image above, the default pins of the SD module are connected, but to show you that you can use your own custom pins, other pins are used in the examples. (We will talk about it later)
Location : ModdableSDK\modules\files
Change folder name (sd-module) to (sd)
Location : ModdableSDK\examples\files
Change folder name (sd-example) to (sd)
This type of import is suggested by STC1998
"include": [
...
{
"git":"/~https://github.com/salarizadi/moddable-sdk.git",
"include":"Modules/SD/sd-module/manifest_spi.json"
}
]
- If you have a display or SPI host other than the SD card and there is an interference with the setup process, you can change your SD card host via your project manifest definitions, for example:
"defines": {
...
"sd_spi_host": 2 // Note: HSPI_HOST is equal to 1 and VSPI_HOST is equal to 2. Default is HSPI_HOST.
}
5. And now you can setup SD card in your project Take a complete look at the sample code It's very simple It's a complete sample
class SD {
mount ( config )
unmount ( )
busFree ( )
disable ( )
info ( )
format ( ) // Need ESP-IDF version 5 or higher
}
import SD from "sd"
const $SD = new SD();
const root = "/sdcard/";
$SD.mount({
miso: 15, // Default 19
mosi: 12, // Default 23
clk: 14, // Default 18
cs: 13, // Default 5
ff: true, // Format if mount failed
transfer_sz: 4000,
max_files: 5,
aus: 16 * 1024 // Allocation unit size
});
$SD.info()
This returns a JSON Object :
{
"name",
"csd_ver",
"speed",
"storage", // Storage unit : MB
"sector_size",
"capacity",
"sd_spec",
"bus_width",
"spi_host"
}
$SD.format() // Need ESP-IDF version 5 or higher
$SD.disable()
Notes : When the $SD.info() function is executed, the output of my memory card information is different from yours
SD initialize : true
This is a test.
We can write multiple values.
This is the end of the test.
test.txt renamed to test2.txt
name: Brian, city: Del Mar, state: CA
File length: 20
Last five shorts: 5 6 7 8 9
System Volume Information directory
test.bin file 20 bytes
test2.txt file 75 bytes
preferences.json file 46 bytes
Info : {"name":1073430784,"csd_ver":0,"speed":25000000,"storage":1898,"sector_size":512,"capacity":3887104,"sd_spec":2,"bus_width":5,"spi_host":2}
Disable : {"unmount":true,"free":true}
- If you want to use the (format) function, you must use ESP-IDF version 5 or higher and uncomment the xs_sd_format function in the (sdcardspi.c) file and the (format) function in the (sd.js) file.
sdcardspi.c
void xs_sd_format ( xsMachine *the ) {
xsmcSetBoolean(xsResult, sdmmc_full_erase(xs_sdmmc_card) == ESP_OK);
}
sd.js
format ( ) @ "xs_sd_format";
-
1.5.0 :
- Fix bugs
- Added definition (sd_spi_host) to customize SPI host in manifest definitions
- (spi_host) was added in the (info) function
-
1.0.0 :
- spi sd card for esp32