# main.py - mounts and lists content of sd card. High speed (HSPI) Pins are 12, 13, 14, 15. No inner nor outer pullups are used.
# One can try to reduce time.sleep delays. Not yet tried by myself. Waveshare sd card module is used.
# When formatting sd card, remember to perform over write format as one of the SD Card's option is.
# I am using SD Card Formatter 5.0.1 from Tuxera Inc.
# My esp32 is Joy-IT esp32 wroom-32 dual processor version :
https://joy-it.net/en/products/SBC-NodeMCU-ESP32
# Prints out :
"""
1
2
['System Volume Information', 'hello.txt']
Files on filesystem:
====================
System Volume Information/ Size: 0 by
IndexerVolumeGuid Size: 76 by
WPSettings.dat Size: 12 by
hello.txt Size: 17 by
"""
def print_directory(path, tabs = 0):
import os
for file in os.listdir(path):
stats = os.stat(path+"/"+file)
filesize = stats[6]
isdir = stats[0] & 0x4000
if filesize < 1000:
sizestr = str(filesize) + " by"
elif filesize < 1000000:
sizestr = "%0.1f KB" % (filesize/1000)
else:
sizestr = "%0.1f MB" % (filesize/1000000)
prettyprintname = ""
for i in range(tabs):
prettyprintname += " "
prettyprintname += file
if isdir:
prettyprintname += "/"
print('{0:<40} Size: {1:>10}'.format(prettyprintname, sizestr))
# recursively print directory contents
if isdir:
print_directory(path+"/"+file, tabs+1)
def f1():
import uos
from machine import SDCard, Pin
import machine
import time
import gc
#gc.enable()
#gc.collect()
#pins = [5, 18, 19, 23]
#pins = [12, 13, 14, 15]
#for pin in pins:
# machine.Pin(pin).init(-1, machine.Pin.PULL_UP)
print(str(1))
time.sleep(0.5)
print(str(2))
#sd = machine.SDCard(slot=2, width=1, cd=None, wp=None, sck=Pin(18), miso=Pin(19), mosi=Pin(23), cs=Pin(5), freq=20000000)
sd = machine.SDCard(slot=3, width=1, cd=None, wp=None, sck=Pin(14), miso=Pin(12), mosi=Pin(13), cs=Pin(15), freq=20000000)
time.sleep(0.5)
uos.mount(sd, "/sd2")
time.sleep(0.5)
print(uos.listdir("/sd2"))
#calling function
print("Files on filesystem:")
print("====================")
print_directory("/sd2")
time.sleep(0.5)
uos.umount("/sd2")
#gc.collect()
#gc.disable()
# end of f1():
f1()
Code: Select all
# main.py - mounts and lists content of sd card. High speed (HSPI) Pins are 12, 13, 14, 15. No inner nor outer pullups are used.
# One can try to reduce time.sleep delays. Not yet tried by myself. Waveshare sd card module is used.
# When formatting sd card, remember to perform over write format as one of the SD Card's option is.
# I am using SD Card Formatter 5.0.1 from Tuxera Inc.
# My esp32 is Joy-IT esp32 wroom-32 dual processor version : https://joy-it.net/en/products/SBC-NodeMCU-ESP32
# Prints out :
"""
1
2
['System Volume Information', 'hello.txt']
Files on filesystem:
====================
System Volume Information/ Size: 0 by
IndexerVolumeGuid Size: 76 by
WPSettings.dat Size: 12 by
hello.txt Size: 17 by
"""
def print_directory(path, tabs = 0):
import os
for file in os.listdir(path):
stats = os.stat(path+"/"+file)
filesize = stats[6]
isdir = stats[0] & 0x4000
if filesize < 1000:
sizestr = str(filesize) + " by"
elif filesize < 1000000:
sizestr = "%0.1f KB" % (filesize/1000)
else:
sizestr = "%0.1f MB" % (filesize/1000000)
prettyprintname = ""
for i in range(tabs):
prettyprintname += " "
prettyprintname += file
if isdir:
prettyprintname += "/"
print('{0:<40} Size: {1:>10}'.format(prettyprintname, sizestr))
# recursively print directory contents
if isdir:
print_directory(path+"/"+file, tabs+1)
def f1():
import uos
from machine import SDCard, Pin
import machine
import time
import gc
#gc.enable()
#gc.collect()
#pins = [5, 18, 19, 23]
#pins = [12, 13, 14, 15]
#for pin in pins:
# machine.Pin(pin).init(-1, machine.Pin.PULL_UP)
print(str(1))
time.sleep(0.5)
print(str(2))
#sd = machine.SDCard(slot=2, width=1, cd=None, wp=None, sck=Pin(18), miso=Pin(19), mosi=Pin(23), cs=Pin(5), freq=20000000)
sd = machine.SDCard(slot=3, width=1, cd=None, wp=None, sck=Pin(14), miso=Pin(12), mosi=Pin(13), cs=Pin(15), freq=20000000)
time.sleep(0.5)
uos.mount(sd, "/sd2")
time.sleep(0.5)
print(uos.listdir("/sd2"))
#calling function
print("Files on filesystem:")
print("====================")
print_directory("/sd2")
time.sleep(0.5)
uos.umount("/sd2")
#gc.collect()
#gc.disable()
# end of f1():
f1()