Skip to content

Commit bfa0b36

Browse files
committed
add SPI SD card support
1 parent 8809757 commit bfa0b36

File tree

25 files changed

+6279
-2
lines changed

25 files changed

+6279
-2
lines changed

libraries/SD/README.adoc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
= SD Library for Arduino =
2+
3+
The SD library allows for reading from and writing to SD cards.
4+
5+
For more information about this library please visit us at
6+
http://www.arduino.cc/en/Reference/SD
7+
8+
== License ==
9+
10+
Copyright (C) 2009 by William Greiman
11+
Copyright (c) 2010 SparkFun Electronics
12+
Copyright (c) 2019 Sipeed
13+
14+
This program is free software: you can redistribute it and/or modify
15+
it under the terms of the GNU General Public License as published by
16+
the Free Software Foundation, either version 3 of the License, or
17+
(at your option) any later version.
18+
19+
This program is distributed in the hope that it will be useful,
20+
but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
GNU General Public License for more details.
23+
24+
You should have received a copy of the GNU General Public License
25+
along with this program. If not, see <http://www.gnu.org/licenses/>.
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
SD card test
3+
4+
This example shows how use the utility libraries on which the'
5+
SD library is based in order to get info about your SD card.
6+
Very useful for testing a card when you're not sure whether its working or not.
7+
8+
The circuit:
9+
SD card attached to SPI bus as follows:
10+
** MOSI - pin 28
11+
** MISO - pin 26
12+
** CLK - pin 27
13+
** CS - pin 29
14+
Pin 4 used here for consistency with other Arduino examples
15+
16+
17+
created 28 Mar 2011
18+
by Limor Fried
19+
modified 9 Apr 2012
20+
by Tom Igoe
21+
modified for k210 30 Mar 2019
22+
by Neucrack
23+
*/
24+
// include the SD library:
25+
#include <SPI.h>
26+
#include <SD.h>
27+
28+
// set up variables using the SD utility library functions:
29+
Sd2Card card; // use object SPI (use SPI1) by default,
30+
// you can change it by:
31+
// SPIClass spi(SPI1, 27, 26, 28, -1);
32+
// Sd2Card card(spi);
33+
SdVolume volume;
34+
SdFile root;
35+
36+
// change this to match your SD shield or module;
37+
// pin 29 on Maix Go or Maix One Dock or Maix Bit
38+
const int chipSelect = 29;
39+
40+
void setup() {
41+
// Open serial communications and wait for port to open:
42+
Serial.begin(9600);
43+
while (!Serial) {
44+
; // wait for serial port to connect. Needed for native USB port only
45+
}
46+
47+
48+
Serial.print("\nInitializing SD card...");
49+
50+
// we'll use the initialization code from the utility libraries
51+
// since we're just testing if the card is working!
52+
if (!card.init(SPI_HALF_SPEED, chipSelect)) {
53+
Serial.println("initialization failed. Things to check:");
54+
Serial.println("* is a card inserted?");
55+
Serial.println("* is your wiring correct?");
56+
Serial.println("* did you change the chipSelect pin to match your shield or module?");
57+
while (1);
58+
} else {
59+
Serial.println("Wiring is correct and a card is present.");
60+
}
61+
62+
// print the type of card
63+
Serial.println();
64+
Serial.print("Card type: ");
65+
switch (card.type()) {
66+
case SD_CARD_TYPE_SD1:
67+
Serial.println("SD1");
68+
break;
69+
case SD_CARD_TYPE_SD2:
70+
Serial.println("SD2");
71+
break;
72+
case SD_CARD_TYPE_SDHC:
73+
Serial.println("SDHC");
74+
break;
75+
default:
76+
Serial.println("Unknown");
77+
}
78+
79+
// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
80+
if (!volume.init(card)) {
81+
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
82+
while (1);
83+
}
84+
85+
Serial.print("Clusters: ");
86+
Serial.println(volume.clusterCount());
87+
Serial.print("Blocks x Cluster: ");
88+
Serial.println(volume.blocksPerCluster());
89+
90+
Serial.print("Total Blocks: ");
91+
Serial.println(volume.blocksPerCluster() * volume.clusterCount());
92+
Serial.println();
93+
94+
// print the type and size of the first FAT-type volume
95+
uint32_t volumesize;
96+
Serial.print("Volume type is: FAT");
97+
Serial.println(volume.fatType(), DEC);
98+
99+
volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
100+
volumesize *= volume.clusterCount(); // we'll have a lot of clusters
101+
volumesize /= 2; // SD card blocks are always 512 bytes (2 blocks are 1KB)
102+
Serial.print("Volume size (Kb): ");
103+
Serial.println(volumesize);
104+
Serial.print("Volume size (Mb): ");
105+
volumesize /= 1024;
106+
Serial.println(volumesize);
107+
Serial.print("Volume size (Gb): ");
108+
Serial.println((float)volumesize / 1024.0);
109+
110+
Serial.println("\nFiles found on the card (name, date and size in bytes): ");
111+
root.openRoot(volume);
112+
113+
// list all files in the card with date and size
114+
root.ls(LS_R | LS_DATE | LS_SIZE);
115+
}
116+
117+
void loop(void) {
118+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
SD card datalogger
3+
4+
This example shows how to log data from three analog sensors
5+
to an SD card using the SD library.
6+
7+
The circuit:
8+
* analog sensors on analog ins 0, 1, and 2
9+
* SD card attached to SPI bus as follows:
10+
** MOSI - pin 28
11+
** MISO - pin 26
12+
** CLK - pin 27
13+
** CS - pin 29
14+
15+
created 24 Nov 2010
16+
modified 9 Apr 2012
17+
by Tom Igoe
18+
modified for k210 30 Mar 2019
19+
by Neucrack
20+
21+
This example code is in the public domain.
22+
23+
*/
24+
25+
#include <SPI.h>
26+
#include <SD.h>
27+
28+
const int chipSelect = 29;
29+
30+
void setup() {
31+
// Open serial communications and wait for port to open:
32+
Serial.begin(9600);
33+
while (!Serial) {
34+
; // wait for serial port to connect. Needed for native USB port only
35+
}
36+
37+
38+
Serial.print("Initializing SD card...");
39+
40+
// see if the card is present and can be initialized:
41+
// SD object used default SPI object( use SPI1 ),
42+
// if you want to modify, as follows:
43+
// SPIClass spi(SPI1, 27, 26, 28, -1);
44+
// SDClass sd(spi);
45+
// sd.begin(29)
46+
if (!SD.begin(chipSelect)) {
47+
Serial.println("Card failed, or not present");
48+
// don't do anything more:
49+
while (1);
50+
}
51+
Serial.println("card initialized.");
52+
}
53+
54+
void loop() {
55+
// make a string for assembling the data to log:
56+
String dataString = "";
57+
58+
// read three sensors and append to the string:
59+
for (int analogPin = 0; analogPin < 3; analogPin++) {
60+
int sensor = analogRead(analogPin);
61+
dataString += String(sensor);
62+
if (analogPin < 2) {
63+
dataString += ",";
64+
}
65+
}
66+
67+
// open the file. note that only one file can be open at a time,
68+
// so you have to close this one before opening another.
69+
File dataFile = SD.open("datalog.txt", FILE_WRITE);
70+
71+
// if the file is available, write to it:
72+
if (dataFile) {
73+
dataFile.println(dataString);
74+
dataFile.close();
75+
// print to the serial port too:
76+
Serial.println(dataString);
77+
}
78+
// if the file isn't open, pop up an error:
79+
else {
80+
Serial.println("error opening datalog.txt");
81+
}
82+
}
83+
84+
85+
86+
87+
88+
89+
90+
91+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
SD card file dump
3+
4+
This example shows how to read a file from the SD card using the
5+
SD library and send it over the serial port.
6+
7+
The circuit:
8+
* SD card attached to SPI bus as follows:
9+
** MOSI - pin 28
10+
** MISO - pin 26
11+
** CLK - pin 27
12+
** CS - pin 29
13+
14+
created 22 December 2010
15+
by Limor Fried
16+
modified 9 Apr 2012
17+
by Tom Igoe
18+
modified for k210 30 Mar 2019
19+
by Neucrack
20+
21+
This example code is in the public domain.
22+
23+
*/
24+
25+
#include <SPI.h>
26+
#include <SD.h>
27+
28+
const int chipSelect = 29;
29+
30+
void setup() {
31+
// Open serial communications and wait for port to open:
32+
Serial.begin(9600);
33+
while (!Serial) {
34+
; // wait for serial port to connect. Needed for native USB port only
35+
}
36+
37+
38+
Serial.print("Initializing SD card...");
39+
40+
// see if the card is present and can be initialized:
41+
if (!SD.begin(chipSelect)) {
42+
Serial.println("Card failed, or not present");
43+
// don't do anything more:
44+
while (1);
45+
}
46+
Serial.println("card initialized.");
47+
48+
// open the file. note that only one file can be open at a time,
49+
// so you have to close this one before opening another.
50+
File dataFile = SD.open("datalog.txt");
51+
52+
// if the file is available, write to it:
53+
if (dataFile) {
54+
while (dataFile.available()) {
55+
Serial.write(dataFile.read());
56+
}
57+
dataFile.close();
58+
}
59+
// if the file isn't open, pop up an error:
60+
else {
61+
Serial.println("error opening datalog.txt");
62+
}
63+
}
64+
65+
void loop() {
66+
}
67+
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
SD card basic file example
3+
4+
This example shows how to create and destroy an SD card file
5+
The circuit:
6+
* SD card attached to SPI bus as follows:
7+
** MOSI - pin 28
8+
** MISO - pin 26
9+
** CLK - pin 27
10+
** CS - pin 29
11+
12+
created Nov 2010
13+
by David A. Mellis
14+
modified 9 Apr 2012
15+
by Tom Igoe
16+
modified for k210 30 Mar 2019
17+
by Neucrack
18+
19+
This example code is in the public domain.
20+
21+
*/
22+
#include <SPI.h>
23+
#include <SD.h>
24+
25+
File myFile;
26+
27+
void setup() {
28+
// Open serial communications and wait for port to open:
29+
Serial.begin(9600);
30+
while (!Serial) {
31+
; // wait for serial port to connect. Needed for native USB port only
32+
}
33+
34+
35+
Serial.print("Initializing SD card...");
36+
37+
if (!SD.begin(29)) {
38+
Serial.println("initialization failed!");
39+
while (1);
40+
}
41+
Serial.println("initialization done.");
42+
43+
if (SD.exists("example.txt")) {
44+
Serial.println("example.txt exists.");
45+
} else {
46+
Serial.println("example.txt doesn't exist.");
47+
}
48+
49+
// open a new file and immediately close it:
50+
Serial.println("Creating example.txt...");
51+
myFile = SD.open("example.txt", FILE_WRITE);
52+
myFile.close();
53+
54+
// Check to see if the file exists:
55+
if (SD.exists("example.txt")) {
56+
Serial.println("example.txt exists.");
57+
} else {
58+
Serial.println("example.txt doesn't exist.");
59+
}
60+
61+
// delete the file:
62+
Serial.println("Removing example.txt...");
63+
SD.remove("example.txt");
64+
65+
if (SD.exists("example.txt")) {
66+
Serial.println("example.txt exists.");
67+
} else {
68+
Serial.println("example.txt doesn't exist.");
69+
}
70+
}
71+
72+
void loop() {
73+
// nothing happens after setup finishes.
74+
}
75+
76+
77+

0 commit comments

Comments
 (0)