Skip to content

Commit f081032

Browse files
committed
First release of STM32SD library
Signed-off-by: Frederic.Pillon <[email protected]>
1 parent 3a3c77c commit f081032

19 files changed

+2352
-1
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.swp

Diff for: README.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,18 @@
11
# STM32SD
2-
Enables reading and writing on SD card using SD card slot of the STM32 Board.
2+
3+
## SD library for Arduino
4+
5+
With an STM32 board with SD card slot availability, this library enables
6+
reading and writing on SD card using SD card slot of a STM32 board (NUCLEO, DISCOVERY, ...).
7+
8+
This library follow Arduin API.
9+
10+
For more information about it, please visit:
11+
http://www.arduino.cc/en/Reference/SD
12+
13+
## Note
14+
15+
The library is based on FatFs, a generic FAT file system module for small embedded systems.
16+
[http://elm-chan.org/fsw/ff](http://elm-chan.org/fsw/ff/00index_e.html)
17+
18+
The FatFs has been ported as Arduino library [here](https://github.com/stm32duino/FatFs). The STM32SD library depends on it.

Diff for: examples/CardInfo/CardInfo.ino

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
* SD card attached
9+
10+
*/
11+
// include the SD library:
12+
#include <STM32SD.h>
13+
14+
Sd2Card card;
15+
SdFatFs fatFs;
16+
17+
void setup()
18+
{
19+
bool disp = false;
20+
// Open serial communications and wait for port to open:
21+
Serial.begin(9600);
22+
23+
while (!Serial);
24+
Serial.print("\nInitializing SD card...");
25+
while(!card.init(SD_DETECT_PIN)) {
26+
if (!disp) {
27+
Serial.println("initialization failed. Is a card inserted?");
28+
disp = true;
29+
}
30+
delay(10);
31+
}
32+
33+
Serial.println("A card is present.");
34+
35+
// print the type of card
36+
Serial.print("\nCard type: ");
37+
switch (card.type()) {
38+
case SD_CARD_TYPE_SD1:
39+
Serial.println("SD1");
40+
break;
41+
case SD_CARD_TYPE_SD2:
42+
Serial.println("SD2");
43+
break;
44+
case SD_CARD_TYPE_SDHC:
45+
Serial.println("SDHC");
46+
break;
47+
default:
48+
Serial.println("Unknown");
49+
}
50+
51+
// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
52+
if (!fatFs.init()) {
53+
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
54+
return;
55+
}
56+
57+
// print the type and size of the first FAT-type volume
58+
uint64_t volumesize;
59+
Serial.print("\nVolume type is FAT");
60+
Serial.println(fatFs.fatType(), DEC);
61+
Serial.println();
62+
63+
volumesize = fatFs.blocksPerCluster(); // clusters are collections of blocks
64+
volumesize *= fatFs.clusterCount(); // we'll have a lot of clusters
65+
volumesize *= 512; // SD card blocks are always 512 bytes
66+
Serial.print("Volume size (bytes): ");
67+
Serial.println(volumesize);
68+
Serial.print("Volume size (Kbytes): ");
69+
volumesize /= 1024;
70+
Serial.println(volumesize);
71+
Serial.print("Volume size (Mbytes): ");
72+
volumesize /= 1024;
73+
Serial.println(volumesize);
74+
75+
76+
Serial.println("\nFiles found on the card (name, date and size in bytes): ");
77+
File root = SD.openRoot();
78+
79+
// list all files in the card with date and size
80+
root.ls(LS_R | LS_DATE | LS_SIZE);
81+
Serial.println("###### End of the SD tests ######");
82+
}
83+
84+
void loop(void) {
85+
// do nothing
86+
}

Diff for: examples/Datalogger/Datalogger.ino

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 A0, A1, and A2
9+
* SD card
10+
11+
*/
12+
13+
#include <STM32SD.h>
14+
15+
uint32_t A[] = { A0, A1, A2};
16+
17+
void setup()
18+
{
19+
// Open serial communications and wait for port to open:
20+
Serial.begin(9600);
21+
while (!Serial) {
22+
; // wait for serial port to connect. Needed for Leonardo only
23+
}
24+
25+
Serial.print("Initializing SD card...");
26+
// see if the card is present and can be initialized:
27+
while (SD.begin(SD_DETECT_PIN) != TRUE)
28+
{
29+
delay(10);
30+
}
31+
delay(100);
32+
Serial.println("card initialized.");
33+
}
34+
35+
void loop()
36+
{
37+
// make a string for assembling the data to log:
38+
String dataString = "";
39+
40+
// read three sensors and append to the string:
41+
for (int analogPin = 0; analogPin < 3; analogPin++) {
42+
int sensor = analogRead(A[analogPin]);
43+
dataString += String(sensor);
44+
if (analogPin < 2) {
45+
dataString += ",";
46+
}
47+
}
48+
49+
// open the file. note that only one file can be open at a time,
50+
// so you have to close this one before opening another.
51+
File dataFile = SD.open("datalog.txt", FILE_WRITE);
52+
53+
// if the file is available, write to it:
54+
if (dataFile) {
55+
dataFile.seek(dataFile.size());
56+
dataFile.println(dataString);
57+
dataFile.close();
58+
// print to the serial port too:
59+
Serial.println(dataString);
60+
}
61+
// if the file isn't open, pop up an error:
62+
else {
63+
Serial.println("error opening datalog.txt");
64+
}
65+
delay(100);
66+
}

Diff for: examples/DumpFile/DumpFile.ino

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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
9+
10+
This example code is in the public domain.
11+
12+
*/
13+
14+
#include <STM32SD.h>
15+
16+
void setup()
17+
{
18+
// Open serial communications and wait for port to open:
19+
Serial.begin(9600);
20+
while (!Serial) {
21+
; // wait for serial port to connect. Needed for Leonardo only
22+
}
23+
24+
25+
Serial.print("Initializing SD card...");
26+
// see if the card is present and can be initialized:
27+
while (SD.begin(SD_DETECT_PIN) != TRUE)
28+
{
29+
delay(10);
30+
}
31+
delay(100);
32+
Serial.println("card initialized.");
33+
34+
// open the file. note that only one file can be open at a time,
35+
// so you have to close this one before opening another.
36+
File dataFile = SD.open("datalog.txt");
37+
38+
// if the file is available, write to it:
39+
if (dataFile) {
40+
while (dataFile.available()) {
41+
Serial.write(dataFile.read());
42+
}
43+
dataFile.close();
44+
}
45+
// if the file isn't open, pop up an error:
46+
else {
47+
Serial.println("error opening datalog.txt");
48+
}
49+
Serial.println("###### End of the SD tests ######");
50+
}
51+
52+
void loop()
53+
{
54+
}

Diff for: examples/Files/Files.ino

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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
7+
8+
This example code is in the public domain.
9+
10+
*/
11+
#include <STM32SD.h>
12+
13+
File myFile;
14+
15+
void setup()
16+
{
17+
// Open serial communications and wait for port to open:
18+
Serial.begin(9600);
19+
while (!Serial) {
20+
; // wait for serial port to connect. Needed for Leonardo only
21+
}
22+
23+
24+
Serial.print("Initializing SD card...");
25+
26+
while (SD.begin(SD_DETECT_PIN) != TRUE)
27+
{
28+
delay(10);
29+
}
30+
Serial.println("initialization done.");
31+
32+
if (SD.exists("example.txt")) {
33+
Serial.println("example.txt exists.");
34+
}
35+
else {
36+
Serial.println("example.txt doesn't exist.");
37+
}
38+
39+
// open a new file and immediately close it:
40+
Serial.println("Creating example.txt...");
41+
myFile = SD.open("example.txt", FILE_WRITE);
42+
myFile.close();
43+
44+
// Check to see if the file exists:
45+
if (SD.exists("example.txt")) {
46+
Serial.println("example.txt exists.");
47+
}
48+
else {
49+
Serial.println("example.txt doesn't exist.");
50+
}
51+
52+
// delete the file:
53+
Serial.println("Removing example.txt...");
54+
SD.remove("example.txt");
55+
56+
if (SD.exists("example.txt")) {
57+
Serial.println("example.txt exists.");
58+
}
59+
else {
60+
Serial.println("example.txt doesn't exist.");
61+
}
62+
Serial.println("###### End of the SD tests ######");
63+
}
64+
65+
void loop()
66+
{
67+
// nothing happens after setup finishes.
68+
}

0 commit comments

Comments
 (0)