Skip to content

Commit 9b92851

Browse files
committed
adding sd card demos
Adding sd card demos for the RP2040 Adalogger
1 parent 67eccd1 commit 9b92851

File tree

4 files changed

+177
-0
lines changed

4 files changed

+177
-0
lines changed

Feather_RP2040_Adalogger/Arduino_RP2040_Adalogger_microSD/.feather_rp2040_adalogger.test.only

Whitespace-only changes.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
/*
5+
SD card read/write
6+
7+
This example shows how to read and write data to and from an SD card file
8+
The circuit:
9+
* SD card attached to SPI bus as follows:
10+
** MOSI - pin 11
11+
** MISO - pin 12
12+
** CLK - pin 13
13+
14+
created Nov 2010
15+
by David A. Mellis
16+
modified 9 Apr 2012
17+
by Tom Igoe
18+
modified 14 Feb 2023
19+
by Liz Clark
20+
modified 25 Aug 2023
21+
by Kattni Rembor
22+
23+
This example code is in the public domain.
24+
25+
*/
26+
27+
#include <SPI.h>
28+
#include "SdFat.h"
29+
30+
#define SD_CS_PIN 23
31+
32+
SdFat SD;
33+
File32 myFile;
34+
SdSpiConfig config(SD_CS_PIN, DEDICATED_SPI, SD_SCK_MHZ(16), &SPI1);
35+
36+
void setup() {
37+
// Open serial communications and wait for port to open:
38+
Serial.begin(115200);
39+
while (!Serial) { yield(); delay(10); } // wait till serial port is opened
40+
delay(100); // RP2040 delay is not a bad idea
41+
42+
Serial.print("Initializing SD card...");
43+
44+
// Retry mechanism for SD card initialization
45+
while (!SD.begin(config)) {
46+
Serial.println("initialization failed! Retrying...");
47+
delay(1000); // Wait for a second before retrying
48+
}
49+
Serial.println("initialization done.");
50+
51+
// open the file. note that only one file can be open at a time,
52+
// so you have to close this one before opening another.
53+
myFile = SD.open("test.txt", FILE_WRITE);
54+
55+
// if the file opened okay, write to it:
56+
if (myFile) {
57+
Serial.print("Writing to test.txt...");
58+
myFile.println("testing 1, 2, 3.");
59+
myFile.println("hello world!");
60+
// close the file:
61+
myFile.close();
62+
Serial.println("done.");
63+
} else {
64+
// if the file didn't open, print an error:
65+
Serial.println("error opening test.txt");
66+
}
67+
68+
// re-open the file for reading:
69+
myFile = SD.open("test.txt");
70+
if (myFile) {
71+
Serial.println("test.txt:");
72+
73+
// read from the file until there's nothing else in it:
74+
while (myFile.available()) {
75+
Serial.write(myFile.read());
76+
}
77+
// close the file:
78+
myFile.close();
79+
} else {
80+
// if the file didn't open, print an error:
81+
Serial.println("error opening test.txt");
82+
}
83+
}
84+
85+
void loop() {
86+
// nothing happens after setup
87+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
"""
5+
CircuitPython Essentials SD Card Read Demo
6+
Feather RP2040 Adalogger
7+
"""
8+
9+
import os
10+
import busio
11+
import digitalio
12+
import board
13+
import storage
14+
import adafruit_sdcard
15+
16+
# Connect to the card and mount the filesystem.
17+
cs = digitalio.DigitalInOut(board.SD_CS)
18+
sd_spi = busio.SPI(board.SD_CLK, board.SD_MOSI, board.SD_MISO)
19+
sdcard = adafruit_sdcard.SDCard(sd_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+
# This helper function will print the contents of the SD
26+
def print_directory(path, tabs=0):
27+
for file in os.listdir(path):
28+
stats = os.stat(path + "/" + file)
29+
filesize = stats[6]
30+
isdir = stats[0] & 0x4000
31+
32+
if filesize < 1000:
33+
sizestr = str(filesize) + " bytes"
34+
elif filesize < 1000000:
35+
sizestr = "%0.1f KB" % (filesize / 1000)
36+
else:
37+
sizestr = "%0.1f MB" % (filesize / 1000000)
38+
39+
prettyprintname = ""
40+
for _ in range(tabs):
41+
prettyprintname += " "
42+
prettyprintname += file
43+
if isdir:
44+
prettyprintname += "/"
45+
print("{0:<40} Size: {1:>10}".format(prettyprintname, sizestr))
46+
47+
# recursively print directory contents
48+
if isdir:
49+
print_directory(path + "/" + file, tabs + 1)
50+
51+
52+
print("Files on filesystem:")
53+
print("====================")
54+
print_directory("/sd")
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# SPDX-FileCopyrightText: 2017 Limor Fried for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
CircuitPython Essentials SD Card Write Demo
7+
Feather RP2040 Adalogger
8+
"""
9+
10+
import time
11+
import busio
12+
import board
13+
import digitalio
14+
import microcontroller
15+
import storage
16+
import adafruit_sdcard
17+
18+
# Connect to the card and mount the filesystem.
19+
cs = digitalio.DigitalInOut(board.SD_CS)
20+
sd_spi = busio.SPI(board.SD_CLK, board.SD_MOSI, board.SD_MISO)
21+
sdcard = adafruit_sdcard.SDCard(sd_spi, cs)
22+
vfs = storage.VfsFat(sdcard)
23+
storage.mount(vfs, "/sd")
24+
25+
# Use the filesystem as normal! Our files are under /sd
26+
27+
print("Logging temperature to filesystem")
28+
# append to the file!
29+
while True:
30+
# open file for append
31+
with open("/sd/temperature.txt", "a") as f:
32+
t = microcontroller.cpu.temperature
33+
print("Temperature = %0.1f" % t)
34+
f.write("%0.1f\n" % t)
35+
# file is saved
36+
time.sleep(1)

0 commit comments

Comments
 (0)