Skip to content

Commit

Permalink
First release of STM32SD library
Browse files Browse the repository at this point in the history
Signed-off-by: Frederic.Pillon <frederic.pillon@st.com>
  • Loading branch information
fpistm committed Aug 30, 2017
1 parent 3a3c77c commit f081032
Show file tree
Hide file tree
Showing 19 changed files with 2,352 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.swp
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
# STM32SD
Enables reading and writing on SD card using SD card slot of the STM32 Board.

## SD library for Arduino

With an STM32 board with SD card slot availability, this library enables
reading and writing on SD card using SD card slot of a STM32 board (NUCLEO, DISCOVERY, ...).

This library follow Arduin API.

For more information about it, please visit:
http://www.arduino.cc/en/Reference/SD

## Note

The library is based on FatFs, a generic FAT file system module for small embedded systems.
[http://elm-chan.org/fsw/ff](http://elm-chan.org/fsw/ff/00index_e.html)

The FatFs has been ported as Arduino library [here](https://github.com/stm32duino/FatFs). The STM32SD library depends on it.
86 changes: 86 additions & 0 deletions examples/CardInfo/CardInfo.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
SD card test
This example shows how use the utility libraries on which the'
SD library is based in order to get info about your SD card.
Very useful for testing a card when you're not sure whether its working or not.
* SD card attached
*/
// include the SD library:
#include <STM32SD.h>

Sd2Card card;
SdFatFs fatFs;

void setup()
{
bool disp = false;
// Open serial communications and wait for port to open:
Serial.begin(9600);

while (!Serial);
Serial.print("\nInitializing SD card...");
while(!card.init(SD_DETECT_PIN)) {
if (!disp) {
Serial.println("initialization failed. Is a card inserted?");
disp = true;
}
delay(10);
}

Serial.println("A card is present.");

// print the type of card
Serial.print("\nCard type: ");
switch (card.type()) {
case SD_CARD_TYPE_SD1:
Serial.println("SD1");
break;
case SD_CARD_TYPE_SD2:
Serial.println("SD2");
break;
case SD_CARD_TYPE_SDHC:
Serial.println("SDHC");
break;
default:
Serial.println("Unknown");
}

// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
if (!fatFs.init()) {
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
return;
}

// print the type and size of the first FAT-type volume
uint64_t volumesize;
Serial.print("\nVolume type is FAT");
Serial.println(fatFs.fatType(), DEC);
Serial.println();

volumesize = fatFs.blocksPerCluster(); // clusters are collections of blocks
volumesize *= fatFs.clusterCount(); // we'll have a lot of clusters
volumesize *= 512; // SD card blocks are always 512 bytes
Serial.print("Volume size (bytes): ");
Serial.println(volumesize);
Serial.print("Volume size (Kbytes): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.print("Volume size (Mbytes): ");
volumesize /= 1024;
Serial.println(volumesize);


Serial.println("\nFiles found on the card (name, date and size in bytes): ");
File root = SD.openRoot();

// list all files in the card with date and size
root.ls(LS_R | LS_DATE | LS_SIZE);
Serial.println("###### End of the SD tests ######");
}

void loop(void) {
// do nothing
}
66 changes: 66 additions & 0 deletions examples/Datalogger/Datalogger.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
SD card datalogger
This example shows how to log data from three analog sensors
to an SD card using the SD library.
The circuit:
* analog sensors on analog ins A0, A1, and A2
* SD card
*/

#include <STM32SD.h>

uint32_t A[] = { A0, A1, A2};

void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}

Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
while (SD.begin(SD_DETECT_PIN) != TRUE)
{
delay(10);
}
delay(100);
Serial.println("card initialized.");
}

void loop()
{
// make a string for assembling the data to log:
String dataString = "";

// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 3; analogPin++) {
int sensor = analogRead(A[analogPin]);
dataString += String(sensor);
if (analogPin < 2) {
dataString += ",";
}
}

// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);

// if the file is available, write to it:
if (dataFile) {
dataFile.seek(dataFile.size());
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
delay(100);
}
54 changes: 54 additions & 0 deletions examples/DumpFile/DumpFile.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
SD card file dump
This example shows how to read a file from the SD card using the
SD library and send it over the serial port.
The circuit:
* SD card attached
This example code is in the public domain.
*/

#include <STM32SD.h>

void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}


Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
while (SD.begin(SD_DETECT_PIN) != TRUE)
{
delay(10);
}
delay(100);
Serial.println("card initialized.");

// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt");

// if the file is available, write to it:
if (dataFile) {
while (dataFile.available()) {
Serial.write(dataFile.read());
}
dataFile.close();
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
Serial.println("###### End of the SD tests ######");
}

void loop()
{
}
68 changes: 68 additions & 0 deletions examples/Files/Files.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
SD card basic file example
This example shows how to create and destroy an SD card file
The circuit:
* SD card attached
This example code is in the public domain.
*/
#include <STM32SD.h>

File myFile;

void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}


Serial.print("Initializing SD card...");

while (SD.begin(SD_DETECT_PIN) != TRUE)
{
delay(10);
}
Serial.println("initialization done.");

if (SD.exists("example.txt")) {
Serial.println("example.txt exists.");
}
else {
Serial.println("example.txt doesn't exist.");
}

// open a new file and immediately close it:
Serial.println("Creating example.txt...");
myFile = SD.open("example.txt", FILE_WRITE);
myFile.close();

// Check to see if the file exists:
if (SD.exists("example.txt")) {
Serial.println("example.txt exists.");
}
else {
Serial.println("example.txt doesn't exist.");
}

// delete the file:
Serial.println("Removing example.txt...");
SD.remove("example.txt");

if (SD.exists("example.txt")) {
Serial.println("example.txt exists.");
}
else {
Serial.println("example.txt doesn't exist.");
}
Serial.println("###### End of the SD tests ######");
}

void loop()
{
// nothing happens after setup finishes.
}
Loading

0 comments on commit f081032

Please sign in to comment.