Skip to content

Commit 8cdfb60

Browse files
authored
Merge pull request #2412 from adafruit/microsd_bff
adding examples for microsd bff
2 parents 6fc1398 + 38cb803 commit 8cdfb60

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

microSD_BFF_Examples/read_sd/code.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
import os
5+
import busio
6+
import digitalio
7+
import board
8+
import storage
9+
import adafruit_sdcard
10+
11+
# The SD_CS pin is the chip select line.
12+
13+
SD_CS = board.TX # setup for microSD BFF
14+
15+
# Connect to the card and mount the filesystem.
16+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
17+
cs = digitalio.DigitalInOut(SD_CS)
18+
sdcard = adafruit_sdcard.SDCard(spi, cs)
19+
vfs = storage.VfsFat(sdcard)
20+
storage.mount(vfs, "/sd")
21+
22+
# Use the filesystem as normal! Our files are under /sd
23+
24+
# This helper function will print the contents of the SD
25+
def print_directory(path, tabs=0):
26+
for file in os.listdir(path):
27+
stats = os.stat(path + "/" + file)
28+
filesize = stats[6]
29+
isdir = stats[0] & 0x4000
30+
31+
if filesize < 1000:
32+
sizestr = str(filesize) + " bytes"
33+
elif filesize < 1000000:
34+
sizestr = "%0.1f KB" % (filesize / 1000)
35+
else:
36+
sizestr = "%0.1f MB" % (filesize / 1000000)
37+
38+
prettyprintname = ""
39+
for _ in range(tabs):
40+
prettyprintname += " "
41+
prettyprintname += file
42+
if isdir:
43+
prettyprintname += "/"
44+
print("{0:<40} Size: {1:>10}".format(prettyprintname, sizestr))
45+
46+
# recursively print directory contents
47+
if isdir:
48+
print_directory(path + "/" + file, tabs + 1)
49+
50+
51+
print("Files on filesystem:")
52+
print("====================")
53+
print_directory("/sd")
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# SPDX-FileCopyrightText: 2017 Limor Fried for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import adafruit_sdcard
7+
import board
8+
import busio
9+
import digitalio
10+
import microcontroller
11+
import storage
12+
13+
# default CS pin for the microSD bff is TX
14+
SD_CS = board.TX
15+
16+
# Connect to the card and mount the filesystem.
17+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
18+
cs = digitalio.DigitalInOut(SD_CS)
19+
sdcard = adafruit_sdcard.SDCard(spi, cs)
20+
vfs = storage.VfsFat(sdcard)
21+
storage.mount(vfs, "/sd")
22+
23+
# Use the filesystem as normal! Our files are under /sd
24+
25+
print("Logging temperature to filesystem")
26+
# append to the file!
27+
while True:
28+
# open file for append
29+
with open("/sd/temperature.txt", "a") as f:
30+
t = microcontroller.cpu.temperature
31+
print("Temperature = %0.1f" % t)
32+
f.write("%0.1f\n" % t)
33+
# file is saved
34+
time.sleep(1)

0 commit comments

Comments
 (0)