Waspmote Sdcard Programming Guide
Waspmote Sdcard Programming Guide
Programming Guide
Index
INDEX
1. Introduction........................................................................................................................... 4
1.1. Waspmote Libraries........................................................................................................................... 4
1.1.1. Waspmote SD files.................................................................................................................4
1.1.2. Constructor.............................................................................................................................4
1.1.3. Flag...........................................................................................................................................5
1.1.4. Error messages.......................................................................................................................5
1.1.5. Buffer.......................................................................................................................................5
1.2. Formatting SD card............................................................................................................................ 5
1.3. Short filename format....................................................................................................................... 5
2. Initialization........................................................................................................................... 7
2.1. Initializing a card................................................................................................................................ 7
2.2. Closing a card..................................................................................................................................... 7
2.3. SD present.......................................................................................................................................... 7
3. Disk operations...................................................................................................................... 8
3.1. Disk information................................................................................................................................ 8
3.2. Disk Size.............................................................................................................................................. 8
4. Directory operations............................................................................................................. 9
4.1. Creating a directory .......................................................................................................................... 9
4.2. Deleting a directory .......................................................................................................................... 9
4.3. Directory listing ............................................................................................................................... 10
4.4. Finding a directory .......................................................................................................................... 11
4.5. Number of files ................................................................................................................................ 12
4.6. Changing directory .......................................................................................................................... 12
5. File operations...................................................................................................................... 13
5.1. Creating files..................................................................................................................................... 13
5.2. Deleting files..................................................................................................................................... 13
5.3. Opening files..................................................................................................................................... 14
5.4. Closing files....................................................................................................................................... 14
5.5. Finding files....................................................................................................................................... 15
5.6. Reading data..................................................................................................................................... 15
5.7. Writing data...................................................................................................................................... 16
5.8. Number of lines............................................................................................................................... 17
5.9. Getting file size................................................................................................................................. 18
5.10. Finding patterns............................................................................................................................. 18
-2- v7.1
Index
7. API changelog....................................................................................................................... 20
8. Certifications........................................................................................................................ 21
9. Documentation changelog................................................................................................. 22
-3- v7.1
Introduction
Note: There are many SD card models. Any of them has defective blocks, which are ignored when using the
Waspmote’s SD library. However, when using OTA, those SD blocks cannot be avoided, so that the execution could
crash. Libelium implements a special process to ensure the SD cards we provide will work fine with OTA. The only SD
cards that Libelium can assure that work correctly with Waspmote are the SD cards we distribute officially.
Note: Make sure Waspmote is switched off before inserting or removing the SD card. Otherwise, the SD card could
be damaged.
Note: Waspmote must not be switched off or reseted while there are ongoing read or write operations in the SD
card. Otherwise, the SD card could be damaged and data could be lost. If you suspect that there may be some
ongoing SD operations, wait a while until they are completed.
Tip: you can use one programmable LED to signal when the SD card is being processed.
1. Introduction
This guide explains the SD card features and functions. There are no great variations in this library for our new
product lines Waspmote v15 and Plug & Sense! v15, released on October 2016.
Anyway, if you are using previous versions of our products, please use the corresponding guides, available on our
Development website.
You can get more information about the generation change on the document “New generation of Libelium product
lines”.
Other utilities inside the sd_utilities subdirectory: ufstream.h, ostream.cpp, SdBaseFile.h, SdFile.cpp, SdSpi.h , ios.h,
ostream.h, SdFatConfig.h, SdFile.h, SdStream.cpp , iostream.h, Sd2Card.cpp, SdFat.cpp, SdInfo.h, SdStream.h ,
istream.cpp, Sd2Card.h, SdFat.h, SdSpiArduino.cpp, SdVolume.cpp , istream.h, SdBaseFile.cpp, SdFatStructs.h,
SdSpiAVR.cpp, SdVolume.h
1.1.2. Constructor
To start using Waspmote SD library, an object from class ‘WaspSD’ must be created. This object, called ‘SD’, is
created inside Waspmote SD library and it is public to all libraries. It is used through the guide to show how
Waspmote SD library works.
-4- v7.1
Introduction
1.1.3. Flag
A flag to indicate if there have been any problem during execution of a function has been created. This flag shows
the state of the SD card during initialization and operation. Possible values are:
1.1.5. Buffer
Due to memory restrictions, a buffer has been created to limit the length of the data managed by Waspmote
API libraries when working with SD cards. Buffer size has been set to 256Bytes due to it is a sufficient value to
manage strings and it occupies little memory.
This limit must be considered when developing applications, using the flag previously explained to know when
data has been truncated. Obviously this does not mean you can handle 256B only, but you have to make writings
and readings of this size.
Only upper-case letters A–Z are valid. When using lower-case letters a-z, these are converted to upper-case letter.
Besides, illegal characters for directories and filenames include the following:
|<>^+=?/[];,*\“\\
-5- v7.1
Introduction
FILE.TXT → valid
FILENAME → valid
FILENAME.TXT → valid
FOLDER → valid
SUBFOLDER → invalid (more than 8 characters)
FILENAME1.TXT → invalid (more than 8 characters before ‘.’)
FILENAME.AAAA → invalid (more than 3 characters after ‘.’)
file.txt → valid (but it will be interpreted as FILE.TXT)
-6- v7.1
Initialization
2. Initialization
Before start using the SD card, it needs to be initialized. This process checks if an SD card is present on the slot,
initializes SPI bus, opens partition and opens root directory.
Example of use:
{
SD.ON(); // Set SD card on
}
Available Information
SD.flag → stores the error code indicating the state of SD initialization process.
The SD card cannot be removed or inserted without powering off Waspmote. If a SD card is removed, the
initialization function should be called to initialize the card again. Before that, the SD Card should be closed using
functions explained in section “Closing a card”.
Example of use:
{
SD.OFF(); // Powers SD card down
}
2.3. SD present
It reads the associated pin to know if there is an SD in card slot.
Example of use:
{
uint8_t present;
// Reads associated pin to know if there is a SD in card slot
present=SD.isSD();
}
Available Information
-7- v7.1
Disk operations
3. Disk operations
When SD has been initialized properly, pointers can be used to access partition, file system and root directory.
There are some functions that return information about the SD card.
{
// Gets disk info, returning it and storing this info in ‘SD.buffer’
SD.print_disk_info();
USB.println(SD.buffer);
}
Available Information
SD.buffer → stores the data received as a human-readable encoded string.
diskInfo → pointer to SD.buffer
An example of the output by this system would be:
manuf: 0x1b
oem: SM
prod: 21e7
rev: 1.0
serial: 0xedb6c604
date: 11/10
It returns ‘diskSize’ variable and updates its value. Size is stored and returned in Bytes.
Example of use:
{
// Get total size of SD card
diskSize = SD.getDiskSize();
USB.println(SD.diskSize);
}
Related Variables:
-8- v7.1
Directory operations
4. Directory operations
To organize an SD card, it is possible to create and manage directories. There are some functions related with
directories.
It returns ‘1’ on creation and ‘0’ on error, activating the flag too.
If a directory name already exists, it will occur an error and the flag will be activated.
Example of use:
boolean dirCreation;
char name[] = ”FOLDER1”;
char path[] = ”FOLDER3/FOLDER4/FOLDER5”;
Note 1: All directory names must be defined according to 8.3 short filename format (see section “Short filename format”)
Note 2: Be careful when calling this function to create a directory. If it is interrupted, the directory results damaged and
it is necessary to delete it as a regular file using SD.del()
The rmdir() function deletes the empty directory specified as input. The directory file will be removed only if it is
empty and is not the root directory.
It returns ‘1’ if the directory has been erased properly and ‘0’ if error.
Note: It allows erasing a complete path of directories always they are empty.
-9- v7.1
Directory operations
Example of use:
Non-empty directories
The rmRfDir() function deletes the non-empty directory specified as input and all contained files. It returns ‘1’ if
the directory has been erased properly and ‘0’ if error.
Example of use:
{
const char* name = ”FOLDER”;
char* path = ”FOLDER3/FOLDER4/FOLDER5”;
boolean delState;
-10- v7.1
Directory operations
Example of use:
{
// lists the name of all the files of the directory indicating the size of the files
SD.ls();
// lists the name of all files of the directory indicating the size of the files
SD.ls(LS_SIZE);
// lists the name of all files of the directory indicating the date of the files
SD.ls(LS_DATE);
// lists the name of all files of the directory and all subdirectories
SD.ls(LS_R);
//lists the name of the files recursively indicating size and date
SD.ls(LS_R|LS_DATE|LS_SIZE);
}
Example of use:
{
uint8_t isdir;
const char name[] = ”FOLDER”;
-11- v7.1
Directory operations
Example of use:
{
int8_t numfiles;
Note: In root directory it has no sense changing directory to ‘..’, so function will return error when doing that.
Example of use
uint8_t cdState;
const char command[] = ”FOLDER”;
// Go one directory up
cdState = SD.cd(“..”);
}
The goRoot() function permits to go to root directory directly. It returns ‘1’ when ok, ‘0’ when error.
Example of use:
{
uint8_t cdState;
// Go to root directory
cdState = SD.goRoot();
}
-12- v7.1
File operations
5. File operations
To store data, files can be created and managed. There are some functions related to files operations.
It returns ‘1’ on file creation and ‘0’ if error and it will mark the flag too.
Example of use:
{
const char name[] = ”FILE.TXT”;
boolean fileCreation;
Note 1:All file names must be defined according to 8.3 short filename format (see section “Short filename format”)
Note 2: The maximum number of files which can be created in the root directory are 341, while a 2nd level directory can
store 1.000+ files. Thus, in the case the user needs to create hundreds of files in an SD card, it is highly advised to create
them inside a 2nd level directory.
Example of use:
{
const char name[] = ”FILE.TXT”;
boolean fileDelete;
-13- v7.1
File operations
Example of use:
{
const char filepath[] = ”FILE.TXT”;
Note: All file names must be defined according to 8.3 short filename format (see section “Short filename format”)
Note: If a file is opened with previous function and it is not closed before using another file function, it will not work
properly. Only one file pointer can be managed at the same time.
Example of use:
{
// previously declared file
SdFile file;
-14- v7.1
File operations
If it exists and it is a file ‘1’ will be returned, ‘0’ will be returned if it exists but it is not a file and ‘-1’ will be returned
if it does not exist.
Example of use:
{
const char* name = ”FILE.TXT”;
int8_t fileFound;
The catln() function stores into the buffer the amount of lines indicated as input. This function needs three input
parameters:
The information is returned as a string where each one of the characters are printed one after the next, EOL (‘\n’)
will be encoded as EOL, and will be accounted as one byte.
Note: There is a limitation in size, due to buffer size. If the data read was bigger than that, the function will include the
characters “>>” at the end and activate the TRUNCATED_DATA value in the flag. It is recommended to check this value to
ensure data integrity.
If ‘offset’ or ‘scope’, or both of them, are greater than file size, there will be no error but only possible data will be copied
into buffer.
-15- v7.1
File operations
Example of use
{
const char* name=”FILE.TXT”;
-16- v7.1
File operations
-17- v7.1
File operations
Example of use:
{
const char name[] = ”FILE.TXT”;
int32_t sizeFile;
Available Information
A possible value would be: sizeFile=16, indicating the size file is 16 Bytes.
It will return the amount of bytes to the pattern from the offset.
The special characters like ‘\n’ (EOL) are accounted as one byte and files are indexed from 0.
Example of use:
{
const char name[] = ”FILE.TXT”;
int32_t pattern;
// It returns position at which “11” appears on file jumping over first 17 positions
pattern = SD.indexOf(name,”11”,17);
The following table shows the results from searching different patterns:
Function Answer
SD.indexOf(“FILE.TXT”, “hola”, 0); 0
SD.indexOf(“FILE.TXT”, “hej”, 3) 11
-18- v7.1
Code examples and extended information
-19- v7.1
API changelog
7. API changelog
Keep track of the software changes on this link:
www.libelium.com/development/waspmote/documentation/changelog/#SDcard
-20- v7.1
Certifications
8. Certifications
Libelium offers 2 types of IoT sensor platforms, Waspmote OEM and Plug & Sense!:
•• Waspmote OEM is intended to be used for research purposes or as part of a major product so it needs final
certification on the client side. More info at: www.libelium.com/products/waspmote
•• Plug & Sense! is the line ready to be used out-of-the-box. It includes market certifications. See below the
specific list of regulations passed. More info at: www.libelium.com/products/plug-sense
Besides, Meshlium, our multiprotocol router for the IoT, is also certified with the certifications below. Get more
info at:
www.libelium.com/products/meshlium
•• CE (Europe)
•• FCC (US)
•• IC (Canada)
•• ANATEL (Brazil)
•• RCM (Australia)
•• PTCRB (cellular certification for the US)
•• AT&T (cellular certification for the US)
www.libelium.com/certifications
-21- v7.1
Documentation changelog
9. Documentation changelog
-22- v7.1