Browse Source

Replace entire SD library with thin wrapper for SdFat

main
PaulStoffregen 4 years ago
parent
commit
17d22be271
27 changed files with 132 additions and 7959 deletions
  1. +0
    -140
      File.cpp
  2. +0
    -13
      README.txt
  3. +0
    -648
      SD.cpp
  4. +0
    -88
      SD.h
  5. +0
    -248
      SD_t3.h
  6. +0
    -234
      cache_t3.cpp
  7. +0
    -173
      card_t3.cpp
  8. +0
    -255
      dir_t3.cpp
  9. +0
    -63
      fat_t3.cpp
  10. +0
    -211
      file_t3.cpp
  11. +0
    -204
      init_t3.cpp
  12. +5
    -5
      library.properties
  13. +4
    -0
      src/SD.cpp
  14. +123
    -0
      src/SD.h
  15. +0
    -418
      utility/FatStructs.h
  16. +0
    -1441
      utility/NXP_SDHC.cpp
  17. +0
    -16
      utility/NXP_SDHC.h
  18. +0
    -544
      utility/Sd2Card.cpp
  19. +0
    -144
      utility/Sd2Card.h
  20. +0
    -413
      utility/Sd2PinMap.h
  21. +0
    -544
      utility/SdFat.h
  22. +0
    -74
      utility/SdFatUtil.h
  23. +0
    -202
      utility/SdFatmainpage.h
  24. +0
    -1252
      utility/SdFile.cpp
  25. +0
    -232
      utility/SdInfo.h
  26. +0
    -295
      utility/SdVolume.cpp
  27. +0
    -102
      utility/ioreg.h

+ 0
- 140
File.cpp View File

@@ -1,140 +0,0 @@

#include <Arduino.h>
#include <SD.h>
#include <limits.h>

#ifndef __SD_t3_H__

#include <utility/SdFat.h>
#include <utility/SdFatUtil.h>


void SD_File::whoami()
{
//Serial.printf(" SD_File this=%x, f=%x\n", (int)this, (int)f.get());
Serial.printf(" SD_File this=%x, refcount=%u\n",
(int)this, getRefcount());
static int whoamicount = 0;
if (++whoamicount > 20) while (1) ;
}


SD_File::SD_File(const SdFile &file, const char *n)
{
_file = new SdFile();
memcpy(_file, &file, sizeof(SdFile)); // should use a copy constructor?
strncpy(_name, n, 12);
_name[12] = 0;
//f = (File *)this;
}

SD_File::SD_File(void)
{
_file = nullptr;
_name[0] = 0;
//f = (File *)this;
}

SD_File::~SD_File(void)
{
close();
}

// returns a pointer to the file name
const char * SD_File::name(void) {
return _name;
}

// a directory is a special type of file
bool SD_File::isDirectory(void)
{
return (_file && _file->isDir());
}


size_t SD_File::write(uint8_t val)
{
return write(&val, 1);
}

size_t SD_File::write(const void *buf, size_t size)
{
if (_file == nullptr) {
setWriteError();
return 0;
}
_file->clearWriteError();
size_t t = _file->write(buf, size);
if (_file->getWriteError()) {
setWriteError();
return 0;
}
return t;
}

int SD_File::peek()
{
if (_file == nullptr) return 0;
int c = _file->read();
if (c != -1) _file->seekCur(-1);
return c;
}

int SD_File::read()
{
if (_file == nullptr) return -1;
return _file->read();
}

// buffered read for more efficient, high speed reading
size_t SD_File::read(void *buf, size_t nbyte) {
if (_file == nullptr) return 0;
return _file->read(buf, nbyte);
}

int SD_File::available()
{
if (_file == nullptr) return 0;
uint32_t n = size() - position();
if (n > INT_MAX) n = INT_MAX;
return n;
}

void SD_File::flush()
{
if (_file == nullptr) return;
_file->sync();
}

bool SD_File::seek(uint32_t pos, int mode)
{
if (_file == nullptr) return false;
return _file->seekSet(pos);
}

uint32_t SD_File::position() const
{
if (_file == nullptr) return 0;
return _file->curPosition();
}

uint32_t SD_File::size() const
{
if (_file == nullptr) return 0;
return _file->fileSize();
}

void SD_File::close()
{
if (_file == nullptr) return;
_file->close();
//delete _file; // TODO: how to handle this?
_file = nullptr;
}

SD_File::operator bool() const
{
if (_file == nullptr) return false;
return _file->isOpen();
}
#endif

+ 0
- 13
README.txt View File

@@ -1,13 +0,0 @@

** SD - a slightly more friendly wrapper for sdfatlib **

This library aims to expose a subset of SD card functionality in the
form of a higher level "wrapper" object.

License: GNU General Public License V3
(Because sdfatlib is licensed with this.)

(C) Copyright 2010 SparkFun Electronics

Now better than ever with optimization, multiple file support, directory handling, etc - ladyada!


+ 0
- 648
SD.cpp View File

@@ -1,648 +0,0 @@
#include <Arduino.h>
#include "SD.h"

#include <utility/SdFat.h>
#include <utility/SdFatUtil.h>




#if 1
/*

SD - a slightly more friendly wrapper for sdfatlib

This library aims to expose a subset of SD card functionality
in the form of a higher level "wrapper" object.

License: GNU General Public License V3
(Because sdfatlib is licensed with this.)

(C) Copyright 2010 SparkFun Electronics


This library provides four key benefits:

* Including `SD.h` automatically creates a global
`SD` object which can be interacted with in a similar
manner to other standard global objects like `Serial` and `Ethernet`.

* Boilerplate initialisation code is contained in one method named
`begin` and no further objects need to be created in order to access
the SD card.

* Calls to `open` can supply a full path name including parent
directories which simplifies interacting with files in subdirectories.

* Utility methods are provided to determine whether a file exists
and to create a directory heirarchy.


Note however that not all functionality provided by the underlying
sdfatlib library is exposed.

*/

/*

Implementation Notes

In order to handle multi-directory path traversal, functionality that
requires this ability is implemented as callback functions.

Individual methods call the `walkPath` function which performs the actual
directory traversal (swapping between two different directory/file handles
along the way) and at each level calls the supplied callback function.

Some types of functionality will take an action at each level (e.g. exists
or make directory) which others will only take an action at the bottom
level (e.g. open).

*/

#include "SD.h"
#ifndef __SD_t3_H__

// Used by `getNextPathComponent`
#define MAX_COMPONENT_LEN 12 // What is max length?
#define PATH_COMPONENT_BUFFER_LEN MAX_COMPONENT_LEN+1

bool getNextPathComponent(const char *path, unsigned int *p_offset,
char *buffer) {
/*

Parse individual path components from a path.

e.g. after repeated calls '/foo/bar/baz' will be split
into 'foo', 'bar', 'baz'.

This is similar to `strtok()` but copies the component into the
supplied buffer rather than modifying the original string.


`buffer` needs to be PATH_COMPONENT_BUFFER_LEN in size.

`p_offset` needs to point to an integer of the offset at
which the previous path component finished.

Returns `true` if more components remain.

Returns `false` if this is the last component.
(This means path ended with 'foo' or 'foo/'.)

*/

// TODO: Have buffer local to this function, so we know it's the
// correct length?

int bufferOffset = 0;

int offset = *p_offset;

// Skip root or other separator
if (path[offset] == '/') {
offset++;
}

// Copy the next next path segment
while (bufferOffset < MAX_COMPONENT_LEN
&& (path[offset] != '/')
&& (path[offset] != '\0')) {
buffer[bufferOffset++] = path[offset++];
}

buffer[bufferOffset] = '\0';

// Skip trailing separator so we can determine if this
// is the last component in the path or not.
if (path[offset] == '/') {
offset++;
}

*p_offset = offset;

return (path[offset] != '\0');
}



bool walkPath(const char *filepath, SdFile *parentDir,
bool (*callback)(SdFile *parentDir,
char *filePathComponent,
bool isLastComponent,
void *object),
void *object = NULL) {
/*

When given a file path (and parent directory--normally root),
this function traverses the directories in the path and at each
level calls the supplied callback function while also providing
the supplied object for context if required.

e.g. given the path '/foo/bar/baz'
the callback would be called at the equivalent of
'/foo', '/foo/bar' and '/foo/bar/baz'.

The implementation swaps between two different directory/file
handles as it traverses the directories and does not use recursion
in an attempt to use memory efficiently.

If a callback wishes to stop the directory traversal it should
return false--in this case the function will stop the traversal,
tidy up and return false.

If a directory path doesn't exist at some point this function will
also return false and not subsequently call the callback.

If a directory path specified is complete, valid and the callback
did not indicate the traversal should be interrupted then this
function will return true.

*/


SdFile subfile1;
SdFile subfile2;

char buffer[PATH_COMPONENT_BUFFER_LEN];

unsigned int offset = 0;

SdFile *p_parent;
SdFile *p_child;

SdFile *p_tmp_sdfile;

p_child = &subfile1;

p_parent = parentDir;

while (true) {

bool moreComponents = getNextPathComponent(filepath, &offset, buffer);

bool shouldContinue = callback(p_parent, buffer, !moreComponents, object);

if (!shouldContinue) {
// TODO: Don't repeat this code?
// If it's one we've created then we
// don't need the parent handle anymore.
if (p_parent != parentDir) {
(*p_parent).close();
}
return false;
}

if (!moreComponents) {
break;
}

bool exists = (*p_child).open(*p_parent, buffer, O_RDONLY);

// If it's one we've created then we
// don't need the parent handle anymore.
if (p_parent != parentDir) {
(*p_parent).close();
}

// Handle case when it doesn't exist and we can't continue...
if (exists) {
// We alternate between two file handles as we go down
// the path.
if (p_parent == parentDir) {
p_parent = &subfile2;
}

p_tmp_sdfile = p_parent;
p_parent = p_child;
p_child = p_tmp_sdfile;
} else {
return false;
}
}

if (p_parent != parentDir) {
(*p_parent).close(); // TODO: Return/ handle different?
}

return true;
}



/*

The callbacks used to implement various functionality follow.

Each callback is supplied with a parent directory handle,
character string with the name of the current file path component,
a flag indicating if this component is the last in the path and
a pointer to an arbitrary object used for context.

*/

bool callback_pathExists(SdFile *parentDir, char *filePathComponent,
bool isLastComponent, void *object) {
/*

Callback used to determine if a file/directory exists in parent
directory.

Returns true if file path exists.

*/
SdFile child;

bool exists = child.open(parentDir, filePathComponent, O_RDONLY);

if (exists) {
child.close();
}

return exists;
}



bool callback_makeDirPath(SdFile *parentDir, char *filePathComponent,
bool isLastComponent, void *object) {
/*

Callback used to create a directory in the parent directory if
it does not already exist.

Returns true if a directory was created or it already existed.

*/
bool result = false;
SdFile child;

result = callback_pathExists(parentDir, filePathComponent, isLastComponent, object);
if (!result) {
result = child.makeDir(parentDir, filePathComponent);
}

return result;
}


/*

bool callback_openPath(SdFile *parentDir, char *filePathComponent,
bool isLastComponent, void *object) {

Callback used to open a file specified by a filepath that may
specify one or more directories above it.

Expects the context object to be an instance of `SDClass` and
will use the `file` property of the instance to open the requested
file/directory with the associated file open mode property.

Always returns true if the directory traversal hasn't reached the
bottom of the directory heirarchy.

Returns false once the file has been opened--to prevent the traversal
from descending further. (This may be unnecessary.)

if (isLastComponent) {
SDClass *p_SD = static_cast<SDClass*>(object);
p_SD->file.open(parentDir, filePathComponent, p_SD->fileOpenMode);
if (p_SD->fileOpenMode == FILE_WRITE) {
p_SD->file.seekSet(p_SD->file.fileSize());
}
// TODO: Return file open result?
return false;
}
return true;
}
*/



bool callback_remove(SdFile *parentDir, char *filePathComponent,
bool isLastComponent, void *object) {
if (isLastComponent) {
return SdFile::remove(parentDir, filePathComponent);
}
return true;
}

bool callback_rmdir(SdFile *parentDir, char *filePathComponent,
bool isLastComponent, void *object) {
if (isLastComponent) {
SdFile f;
if (!f.open(parentDir, filePathComponent, O_READ)) return false;
return f.rmDir();
}
return true;
}



/* Implementation of class used to create `SDCard` object. */



bool SDClass::begin(uint8_t csPin) {
/*

Performs the initialisation required by the sdfatlib library.

Return true if initialization succeeds, false otherwise.

*/
card = new Sd2Card();
volume = new SdVolume();
root = new SdFile();


if (root->isOpen()) root->close();

return card->init(SPI_HALF_SPEED, csPin) &&
volume->init(card) &&
root->openRoot(volume);
}



// this little helper is used to traverse paths
SdFile SDClass::getParentDir(const char *filepath, int *index) {
// get parent directory
SdFile d1;
SdFile d2;

d1.openRoot(volume); // start with the mostparent, root!

// we'll use the pointers to swap between the two objects
SdFile *parent = &d1;
SdFile *subdir = &d2;

const char *origpath = filepath;

while (strchr(filepath, '/')) {

// get rid of leading /'s
if (filepath[0] == '/') {
filepath++;
continue;
}

if (! strchr(filepath, '/')) {
// it was in the root directory, so leave now
break;
}

// extract just the name of the next subdirectory
uint8_t idx = strchr(filepath, '/') - filepath;
if (idx > 12)
idx = 12; // dont let them specify long names
char subdirname[13];
strncpy(subdirname, filepath, idx);
subdirname[idx] = 0;

// close the subdir (we reuse them) if open
subdir->close();
if (! subdir->open(parent, subdirname, O_READ)) {
// failed to open one of the subdirectories
return SdFile();
}
// move forward to the next subdirectory
filepath += idx;

// we reuse the objects, close it.
parent->close();

// swap the pointers
SdFile *t = parent;
parent = subdir;
subdir = t;
}

*index = (int)(filepath - origpath);
// parent is now the parent diretory of the file!
return *parent;
}


File SDClass::open(const char *filepath, uint8_t mode) {
/*

Open the supplied file path for reading or writing.

The file content can be accessed via the `file` property of
the `SDClass` object--this property is currently
a standard `SdFile` object from `sdfatlib`.

Defaults to read only.

If `write` is true, default action (when `append` is true) is to
append data to the end of the file.

If `append` is false then the file will be truncated first.

If the file does not exist and it is opened for writing the file
will be created.

An attempt to open a file for reading that does not exist is an
error.

*/

int pathidx;

// do the interative search
SdFile parentdir = getParentDir(filepath, &pathidx);
// no more subdirs!

filepath += pathidx;

if (! filepath[0]) {
// it was the directory itself!
Serial.println("it was the directory itself");

File ret = File(new SD_File(parentdir, "/"));
ret.whoami();
return ret;
}

// failed to open a subdir!
if (!parentdir.isOpen()) {
return File();
}

// convert mode to underlying SdFat
if (mode == FILE_READ) {
mode = O_READ;
} else { // FILE_WRITE
mode = O_READ | O_WRITE | O_CREAT;
}

// Open the file itself
SdFile file;
if ( ! file.open(parentdir, filepath, mode)) {
return File();
}
// close the parent
parentdir.close();

if (mode & (O_APPEND | O_WRITE)) {
file.seekSet(file.fileSize());
}

File ret = File(new SD_File(file, filepath));

Serial.println("open() return:");
ret.whoami();
return ret;
//return SD_File(file, filepath);
}


/*
File SDClass::open(char *filepath, uint8_t mode) {
//

Open the supplied file path for reading or writing.

The file content can be accessed via the `file` property of
the `SDClass` object--this property is currently
a standard `SdFile` object from `sdfatlib`.

Defaults to read only.

If `write` is true, default action (when `append` is true) is to
append data to the end of the file.

If `append` is false then the file will be truncated first.

If the file does not exist and it is opened for writing the file
will be created.

An attempt to open a file for reading that does not exist is an
error.

//

// TODO: Allow for read&write? (Possibly not, as it requires seek.)

fileOpenMode = mode;
walkPath(filepath, root, callback_openPath, this);

return File();

}
*/


//bool SDClass::close() {
// /*
//
// Closes the file opened by the `open` method.
//
// */
// file.close();
//}


bool SDClass::exists(const char *filepath) {
/*

Returns true if the supplied file path exists.

*/
return walkPath(filepath, root, callback_pathExists);
}


//bool SDClass::exists(char *filepath, SdFile& parentDir) {
// /*
//
// Returns true if the supplied file path rooted at `parentDir`
// exists.
//
// */
// return walkPath(filepath, parentDir, callback_pathExists);
//}


bool SDClass::mkdir(const char *filepath) {
/*

Makes a single directory or a heirarchy of directories.

A rough equivalent to `mkdir -p`.

*/
return walkPath(filepath, root, callback_makeDirPath);
}

bool SDClass::rmdir(const char *filepath) {
/*

Makes a single directory or a heirarchy of directories.

A rough equivalent to `mkdir -p`.

*/
return walkPath(filepath, root, callback_rmdir);
}

bool SDClass::remove(const char *filepath) {
return walkPath(filepath, root, callback_remove);
}


// allows you to recurse into a directory
File SD_File::openNextFile(uint8_t mode)
{
dir_t p;

//Serial.print("\t\treading dir...");
while (_file->readDir(&p) > 0) {

// done if past last used entry
if (p.name[0] == DIR_NAME_FREE) {
//Serial.println("end");
return File();
}

// skip deleted entry and entries for . and ..
if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.') {
//Serial.println("dots");
continue;
}

// only list subdirectories and files
if (!DIR_IS_FILE_OR_SUBDIR(&p)) {
//Serial.println("notafile");
continue;
}

// print file name with possible blank fill
SdFile f;
char name[13];
_file->dirName(p, name);
//Serial.print("try to open file ");
//Serial.println(name);

if (f.open(_file, name, mode)) {
//Serial.println("OK!");
return File(new SD_File(f, name));
} else {
//Serial.println("ugh");
return File();
}
}

//Serial.println("nothing");
return File();
}

void SD_File::rewindDirectory(void) {
if (isDirectory())
_file->rewind();
}

SDClass SD;
#endif
#endif

+ 0
- 88
SD.h View File

@@ -1,88 +0,0 @@
#ifndef __SD_H__
#define __SD_H__

#include <Arduino.h>
#include <FS.h>

class Sd2Card;
class SdVolume;
class SdFile;

class SD_File : public File {
public:
SD_File(const SdFile &f, const char *name);
SD_File(void);
virtual ~SD_File(void);
virtual size_t write(uint8_t);
virtual size_t write(const void *buf, size_t size);
virtual int read();
virtual int peek();
virtual int available();
virtual void flush();
virtual size_t read(void *buf, size_t nbyte);
virtual bool seek(uint32_t pos, int mode = 0);
virtual uint32_t position() const;
virtual uint32_t size() const;
virtual void close();
virtual operator bool() const;
virtual const char * name();
virtual boolean isDirectory(void);
virtual File openNextFile(uint8_t mode = 0 /*O_RDONLY*/) override;
virtual void rewindDirectory(void);
virtual void whoami();
using Print::write;
private:
char _name[13]; // our name
SdFile *_file; // underlying file pointer
};



class SDClass : public FS {

private:
// These are required for initialisation and use of sdfatlib
Sd2Card *card;
SdVolume *volume;
SdFile *root;
// my quick&dirty iterator, should be replaced
SdFile getParentDir(const char *filepath, int *indx);
public:
// This needs to be called to set up the connection to the SD card
// before other methods are used.
bool begin(uint8_t csPin = 10 /*SD_CHIP_SELECT_PIN*/);
// Open the specified file/directory with the supplied mode (e.g. read or
// write, etc). Returns a File object for interacting with the file.
// Note that currently only one file can be open at a time.
File open(const char *filename, uint8_t mode = FILE_READ);

// Methods to determine if the requested file path exists.
bool exists(const char *filepath);

// Create the requested directory heirarchy--if intermediate directories
// do not exist they will be created.
bool mkdir(const char *filepath);
// Delete the file.
bool remove(const char *filepath);
bool rmdir(const char *filepath);

private:

// This is used to determine the mode used to open a file
// it's here because it's the easiest place to pass the
// information through the directory walking function. But
// it's probably not the best place for it.
// It shouldn't be set directly--it is set via the parameters to `open`.
int fileOpenMode;
friend class SD_File;
friend boolean callback_openPath(SdFile&, char *, boolean, void *);
};

extern SDClass SD;

#endif

+ 0
- 248
SD_t3.h View File

@@ -1,248 +0,0 @@
/* Optimized SD Library for Teensy 3.X
* Copyright (c) 2015, Paul Stoffregen, paul@pjrc.com
*
* Development of this SD library was funded by PJRC.COM, LLC by sales of
* Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
* open source software by purchasing genuine Teensy or other PJRC products.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice, development funding notice, and this permission
* notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

// This Teensy 3.x optimized version is a work-in-progress.
//
// Uncomment this line to use the Teensy version, which completely replaces
// all of the normal Arduino SD library code. The optimized version is
// currently read-only. It CAN NOT WRITE ANYTHING TO YOUR SD CARD. However,
// it is *much* faster for reading more than 1 file at a time, especially for
// the Teensy Audio Library to play and mix multiple sound files.
// On Teensy 3.5 & 3.6, this optimization does NOT SUPPORT the built-in SD
// sockets. It only works with SD cards connected to the SPI pins.
//
//#define USE_TEENSY3_OPTIMIZED_CODE

/* Why reinvent the SD library wheel...
* 1: Allow reading files from within interrupts
* 2: Cache more than one sector for improved performance
* 3: General optimization for 32 bit ARM on Teensy 3.x & Teensy-LC
* 4: Permissive MIT license
*/

#if !defined(__SD_t3_H__) && defined(__arm__) && defined(USE_TEENSY3_OPTIMIZED_CODE)
#define __SD_t3_H__
#define __SD_H__

#include <Arduino.h>
#include <SPI.h>
#include "utility/ioreg.h"

#define SD_CACHE_SIZE 7 // each cache entry uses 520 bytes of RAM

#define SD_SPI_SPEED SPISettings(25000000, MSBFIRST, SPI_MODE0)

#define FILE_READ 0
#define FILE_WRITE 1
#define FILE_DIR 2
#define FILE_DIR_ROOT16 3
#define FILE_INVALID 4

class File;

class SDClass
{
public:
static bool begin(uint8_t csPin = SS);
static File open(const char *path, uint8_t mode = FILE_READ);
static bool exists(const char *path);
static bool mkdir(const char *path);
static bool remove(const char *path);
static bool rmdir(const char *path);
private:
static uint8_t sd_cmd0();
static uint32_t sd_cmd8();
static uint8_t sd_acmd41(uint32_t hcs);
static uint32_t sd_cmd58();
static bool sd_read(uint32_t addr, void * data);
static void send_cmd(uint16_t cmd, uint32_t arg);
static uint8_t recv_r1();
static uint32_t recv_r3_or_r7();
static void end_cmd();
static volatile IO_REG_TYPE * csreg;
static IO_REG_TYPE csmask;
static uint8_t card_type; // 1=SDv1, 2=SDv2, 3=SDHC
static File rootDir;
static uint32_t fat1_begin_lba;
static uint32_t fat2_begin_lba;
static uint32_t data_begin_lba;
static uint32_t max_cluster;
static uint8_t sector2cluster;
static uint8_t fat_type;
friend class SDCache;
friend class File;
typedef struct {
union {
struct { // short 8.3 filename info
char name[11];
uint8_t attrib;
uint8_t reserved;
uint8_t ctime_tenth;
uint16_t ctime;
uint16_t cdate;
uint16_t adate;
uint16_t cluster_high;
uint16_t wtime;
uint16_t wdate;
uint16_t cluster_low;
uint32_t size;
};
struct { // long filename info
uint8_t ord;
uint8_t lname1[10];
uint8_t lattrib;
uint8_t type;
uint8_t cksum;
uint8_t lname2[12];
uint16_t lcluster_low;
uint8_t lname3[4];
};
};
} fatdir_t;
typedef union {
uint8_t u8[512];
uint16_t u16[256];
uint32_t u32[128];
fatdir_t dir[16];
} sector_t;
};


#define ATTR_READ_ONLY 0x01
#define ATTR_HIDDEN 0x02
#define ATTR_SYSTEM 0x04
#define ATTR_VOLUME_ID 0x08
#define ATTR_DIRECTORY 0x10
#define ATTR_ARCHIVE 0x20
#define ATTR_LONG_NAME 0x0F

class File : public Stream
{
public:
File();
~File();
// TODO: copy constructors, needs to be ISR safe
virtual size_t write(uint8_t b);
virtual size_t write(const uint8_t *buf, size_t size);
virtual int read();
virtual int peek();
virtual int available();
virtual void flush();
int read(void *buf, uint32_t size);
bool seek(uint32_t pos);
uint32_t position() {
if (type <= FILE_WRITE) return offset;
return 0;
}
uint32_t size() {
if (type <= FILE_WRITE) return length;
return 0;
}
void close();
operator bool() {
return (type < FILE_INVALID);
}
char * name() {
return namestr;
}
bool isDirectory() {
return (type == FILE_DIR) || (type == FILE_DIR_ROOT16);
}
File openNextFile(uint8_t mode = FILE_READ);
void rewindDirectory() {
rewind();
}
using Print::write;
void rewind() {
offset = 0;
current_cluster = start_cluster;
};
private:
bool find(const char *filename, File *found);
void init(SDClass::fatdir_t *dirent);
bool next_cluster();
uint32_t offset; // position within file (EOF = length)
uint32_t length; // total size of file
uint32_t start_cluster; // first cluster for the file
uint32_t current_cluster; // position (must agree w/ offset)
uint32_t dirent_lba; // dir sector for this file
uint8_t dirent_index; // dir index within sector (0 to 15)
uint8_t type; // file vs dir
char namestr[13];
friend class SDClass;
static inline uint32_t cluster_number(uint32_t n) {
return n >> (SDClass::sector2cluster + 9);
}
static inline uint32_t cluster_offset(uint32_t n) {
return n & ((1 << (SDClass::sector2cluster + 9)) - 1);
}
static inline uint32_t custer_to_sector(uint32_t n) {
return (n - 2) * (1 << SDClass::sector2cluster)
+ SDClass::data_begin_lba;
}
static inline bool is_new_cluster(uint32_t lba) {
return (lba & ((1 << SDClass::sector2cluster) - 1)) == 0;
}
};

class SDCache
{
private:
// SDCache objects should be created with local scope.
// read(), get(), alloc() acquire temporary locks on
// cache buffers, which are automatically released
// by the destructor when the object goes out of scope.
// Pointers returned by those functions must NEVER be
// used after the SDCache object which returned them
// no longer exists.
SDCache(void) { item = NULL; }
~SDCache(void) { release(); }
typedef struct cache_struct {
SDClass::sector_t data;
uint32_t lba;
cache_struct * next;
uint8_t usagecount;
uint8_t flags;
} cache_t;
SDClass::sector_t * read(uint32_t lba, bool is_fat=false);
bool read(uint32_t lba, void *buffer);
cache_t * get(uint32_t lba, bool allocate=true);
void dirty(void);
void flush(void);
void release(void);
cache_t * item;
static cache_t *cache_list;
static cache_t cache[SD_CACHE_SIZE];
static void init(void);
static void print_cache(void);
friend class SDClass;
friend class File;
};


extern SDClass SD;

#endif

+ 0
- 234
cache_t3.cpp View File

@@ -1,234 +0,0 @@
/* Optimized SD Library for Teensy 3.X
* Copyright (c) 2015, Paul Stoffregen, paul@pjrc.com
*
* Development of this SD library was funded by PJRC.COM, LLC by sales of
* Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
* open source software by purchasing genuine Teensy or other PJRC products.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice, development funding notice, and this permission
* notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#if defined(__arm__)
#include "SD_t3.h"
#ifdef USE_TEENSY3_OPTIMIZED_CODE

#define cache_t SDCache::cache_t
#define sector_t SDClass::sector_t

cache_t *SDCache::cache_list = NULL;
cache_t SDCache::cache[SD_CACHE_SIZE];

#define CACHE_FLAG_HAS_DATA 1
#define CACHE_FLAG_IS_DIRTY 2
#define CACHE_FLAG_IS_FAT 4

//#define PRINT_SECTORS

#ifdef PRINT_SECTORS
static void print_sector(const void *data)
{
const uint8_t *p = (const uint8_t *)data;
for (int i=0; i < 512; i++) {
Serial.printf(" %02X", *p++);
if ((i & 31) == 31) Serial.println();
}
}
#endif

void SDCache::print_cache(void)
{
#if 0
const cache_t *end=cache+SD_CACHE_SIZE;
for (cache_t *c = cache; c < end; c++) {
Serial.printf(" cache index %u, lba= %u, ucount=%u, flags=%u\n",
c - cache, c->lba, c->usagecount, c->flags);
}
Serial.print(" cache order:");
for (cache_t *c = cache_list; c; c = c->next) {
Serial.printf(" %u ->", c - cache);
}
Serial.println();
#endif
}

// Read a sector into the cache. If the sector is already cached,
// of course no actual read occurs. This is the primary function
// used to access the SD card.
//
sector_t * SDCache::read(uint32_t lba, bool is_fat)
{
sector_t *ret = NULL;
//uint32_t slot=0, ucount=0;

// the entire read operation, including all cache manipulation,
// needs to be protected with exclusive access to the hardware.
//Serial.printf("cache read: lba = %d\n", lba);
SPI.beginTransaction(SD_SPI_SPEED);
// does the cache already have the sector?
cache_t *c = get(lba);
if (c) {
if (c->flags & CACHE_FLAG_HAS_DATA) {
//Serial.printf(" cache hit, lba=%u\n", lba);
ret = &c->data;
} else {
if (SDClass::sd_read(lba, &c->data)) {
c->flags = CACHE_FLAG_HAS_DATA;
if (is_fat) c->flags |= CACHE_FLAG_IS_FAT;
ret = &c->data;
//Serial.printf(" cache miss, lba=%u\n", lba);
} else {
//Serial.printf(" cache miss: read error, lba=%u\n", lba);
}
}
} else {
//Serial.printf(" cache full & all in use\n", lba);
}
SPI.endTransaction();
//print_cache();
return ret;
}

// Read a whole 512 byte sector directly to memory. If the sector is
// already cached, of course no actual read occurs and data is copied
// from the cache. When the sector is not cached, it's transferred
// directly from SD card to memory, bypassing the cache.
//
bool SDCache::read(uint32_t lba, void *buffer)
{
bool ret = true;

SPI.beginTransaction(SD_SPI_SPEED);
cache_t *c = get(lba, false);
if (!c || !(c->flags & CACHE_FLAG_HAS_DATA)) {
ret = SDClass::sd_read(lba, buffer);
}
SPI.endTransaction();
if (c) {
if ((c->flags & CACHE_FLAG_HAS_DATA)) {
memcpy(buffer, &c->data, 512);
release();
return true;
}
release();
}
return ret;
}


// locate a sector in the cache.
cache_t * SDCache::get(uint32_t lba, bool allocate)
{
cache_t *c, *p=NULL, *last=NULL, *plast=NULL;

// TODO: move initialization to a function called when the SD card is initialized
if (cache_list == NULL) init();
// have we already acquired a cache entry?
if (item) {
// if it's the desired block, use it
if (item->lba == lba) return item;
// if not, release our hold on it
release();
}
__disable_irq();
c = cache_list;
do {
if (c->lba == lba) {
if (p) {
p->next = c->next;
c->next = cache_list;
cache_list = c;
}
c->usagecount++;
__enable_irq();
item = c;
return item;
}
if (c->usagecount == 0) {
plast = p;
last = c;
}
p = c;
c = c->next;
} while (c);
if (allocate && last) {
if (plast) {
plast->next = last->next;
last->next = cache_list;
cache_list = last;
}
last->usagecount = 1;
// TODO: flush if dirty
last->lba = lba;
last->flags = 0;
item = last;
}
__enable_irq();
return item;
}


void SDCache::init(void)
{
cache_t *c = cache;
cache_t *end = c + SD_CACHE_SIZE;
//Serial.println("cache init");
__disable_irq();
do {
c->lba = 0xFFFFFFFF;
c->usagecount = 0;
c->flags = 0;
c->next = c + 1;
c = c + 1;
} while (c < end);
c--;
c->next = NULL;
cache_list = cache;
__enable_irq();
}


void SDCache::dirty(void)
{
__disable_irq();
item->flags |= CACHE_FLAG_IS_DIRTY;
__enable_irq();
}

void SDCache::release(void)
{
//Serial.printf("cache release\n");
if (item) {
__disable_irq();
item->usagecount--;
//uint32_t ucount = item->usagecount;
//uint32_t lba = item->lba;
//Serial.printf("release %d, %d, slot %u\n", item->lba, item->usagecount, item-cache);
__enable_irq();
item = NULL;
}
}







#endif
#endif

+ 0
- 173
card_t3.cpp View File

@@ -1,173 +0,0 @@
/* Optimized SD Library for Teensy 3.X
* Copyright (c) 2015, Paul Stoffregen, paul@pjrc.com
*
* Development of this SD library was funded by PJRC.COM, LLC by sales of
* Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
* open source software by purchasing genuine Teensy or other PJRC products.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice, development funding notice, and this permission
* notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#if defined(__arm__)
#include "SD_t3.h"
#ifdef USE_TEENSY3_OPTIMIZED_CODE

volatile uint8_t * SDClass::csreg;
uint8_t SDClass::csmask;
uint8_t SDClass::card_type;


#define CMD0_GO_IDLE_STATE 0x4095 // arg=0
#define CMD1_SEND_OP_COND 0x41FF
#define CMD8_SEND_IF_COND 0x4887 // arg=0x1AA
#define CMD55_APP_CMD 0x77FF
#define ACMD41_SD_SEND_OP_COND 0x69FF
#define CMD58_READ_OCR 0x7AFF
#define CMD17_READ_SINGLE_BLOCK 0x51FF
#define CMD24_WRITE_BLOCK 0x58FF


uint8_t SDClass::sd_cmd0()
{
send_cmd(CMD0_GO_IDLE_STATE, 0);
uint8_t r1 = recv_r1();
end_cmd();
return r1;
}

uint32_t SDClass::sd_cmd8()
{
send_cmd(CMD8_SEND_IF_COND, 0x1AA);
uint8_t r1 = recv_r1();
uint32_t cond = 0x80000000;
if (r1 == 1) {
cond = recv_r3_or_r7();
//Serial.print(cond, HEX);
}
end_cmd();
return cond;
}

uint8_t SDClass::sd_acmd41(uint32_t hcs)
{
//Serial.print("acmd41:");
send_cmd(CMD55_APP_CMD, 0);
uint8_t r1 = recv_r1();
end_cmd();
send_cmd(ACMD41_SD_SEND_OP_COND, hcs);
r1 = recv_r1();
end_cmd();
return r1;
}

uint32_t SDClass::sd_cmd58(void)
{
send_cmd(CMD58_READ_OCR, 0);
uint8_t r1 = recv_r1();
uint32_t ocr = 0;
if (r1 == 0) ocr = recv_r3_or_r7();
end_cmd();
return ocr;
}

bool SDClass::sd_read(uint32_t addr, void * data)
{
//Serial.printf("sd_read %ld\n", addr);
if (card_type < 2) addr = addr << 9;
send_cmd(CMD17_READ_SINGLE_BLOCK, addr);
uint8_t r1 = recv_r1();
if (r1 != 0) {
end_cmd();
//Serial.println(" sd_read fail r1");
return false;
}
while (1) {
uint8_t token = SPI.transfer(0xFF);
//Serial.printf("t=%02X.", token);
if (token == 0xFE) break;
if (token != 0xFF) {
end_cmd();
//Serial.println(" sd_read fail token");
return false;
}
// TODO: timeout
}
uint8_t *p = (uint8_t *)data;
uint8_t *end = p + 510;
SPI0_PUSHR = 0xFFFF | SPI_PUSHR_CTAS(1);
SPI0_PUSHR = 0xFFFF | SPI_PUSHR_CTAS(1);
while (p < end) {
while (!(SPI0_SR & 0xF0)) ;
SPI0_PUSHR = 0xFFFF | SPI_PUSHR_CTAS(1);
uint32_t in = SPI0_POPR;
*p++ = in >> 8;
*p++ = in;
}
while (!(SPI0_SR & 0xF0)) ;
uint32_t in = SPI0_POPR;
*p++ = in >> 8;
*p++ = in;
while (!(SPI0_SR & 0xF0)) ;
SPI0_POPR; // ignore crc
DIRECT_WRITE_HIGH(csreg, csmask);
SPI.transfer(0xFF);
return true;
// token = 0xFE
// data, 512 bytes
// crc, 2 bytes
}


void SDClass::send_cmd(uint16_t cmd, uint32_t arg)
{
DIRECT_WRITE_LOW(csreg, csmask);
SPI.transfer(cmd >> 8);
SPI.transfer16(arg >> 16);
SPI.transfer16(arg);
SPI.transfer(cmd);
}

uint8_t SDClass::recv_r1(void)
{
uint8_t ret, count=0;
do {
ret = SPI.transfer(0xFF);
//Serial.print(ret);
//Serial.print(".");
if ((ret & 0x80) == 0) break;
} while (++count < 9);
return ret;
}

uint32_t SDClass::recv_r3_or_r7(void)
{
uint32_t r;
r = SPI.transfer16(0xFFFF) << 16;
r |= SPI.transfer16(0xFFFF);
return r;
}

void SDClass::end_cmd(void)
{
DIRECT_WRITE_HIGH(csreg, csmask);
SPI.transfer(0xFF);
}

#endif
#endif

+ 0
- 255
dir_t3.cpp View File

@@ -1,255 +0,0 @@
/* Optimized SD Library for Teensy 3.X
* Copyright (c) 2015, Paul Stoffregen, paul@pjrc.com
*
* Development of this SD library was funded by PJRC.COM, LLC by sales of
* Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
* open source software by purchasing genuine Teensy or other PJRC products.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice, development funding notice, and this permission
* notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#if defined(__arm__)
#include "SD_t3.h"
#ifdef USE_TEENSY3_OPTIMIZED_CODE

#define sector_t SDClass::sector_t
#define fatdir_t SDClass::fatdir_t

File SDClass::open(const char *path, uint8_t mode)
{
File ret, parent = rootDir;

//Serial.print("SD.open: ");
//Serial.println(path);
while (1) {
while (*path == '/') path++;
if (*path == 0) {
// end of pathname is "/", use last subdir
ret = parent;
break;
}
File next;
bool found = parent.find(path, &next);
const char *p = path;
do p++; while (*p != '/' && *p != 0);
if (found) {
//Serial.println(" open: found");
if (*p == 0) {
// found the file
ret = next;
break;
}
// found next subdir
parent = next;
path = p;
} else {
//Serial.print(" open: not found ");
//Serial.println(path);
if (*p == '/') break; // subdir doesn't exist
// file doesn't exist
if (mode == FILE_READ) break;
// TODO: for writing, create the file
break;
}
}
return ret;
}

bool SDClass::exists(const char *path)
{
File f = open(path);
return (bool)f;
}

File File::openNextFile(uint8_t mode)
{
File f;
uint32_t lba, sector_offset, sector_index, sector_count;

//Serial.print("File::openNextFile, offset=");
//Serial.println(offset);
if (mode > FILE_READ) return f; // TODO: writing not yet supported
if (type == FILE_DIR_ROOT16) {
//Serial.print(" fat16 dir");
sector_offset = offset >> 9;
lba = start_cluster + sector_offset;
sector_count = length >> 9;
} else if (type == FILE_DIR) {
//Serial.print(" subdir");
sector_offset = cluster_offset(offset) >> 9;
lba = custer_to_sector(current_cluster) + sector_offset;
sector_count = (1 << SDClass::sector2cluster);
} else {
return f;
}
//Serial.print(" sector_offset=");
//Serial.println(sector_offset);
//Serial.print(" lba=");
//Serial.println(lba);
//Serial.print(" sector_count=");
//Serial.println(sector_count);
sector_index = (offset >> 5) & 15;
//Serial.print(" sector_index=");
//Serial.println(sector_index);
while (1) {
SDCache sector;
sector_t *s = sector.read(lba);
if (!s) return f;
fatdir_t *dirent = s->dir + sector_index;
while (sector_index < 16) {
//Serial.print(" sector_index=");
//Serial.println(sector_index);
if (dirent->attrib != ATTR_LONG_NAME) {
uint8_t b0 = dirent->name[0];
//Serial.print(" b0=");
//Serial.println(b0);
if (b0 == 0) return f;
if (b0 != 0xE5 && memcmp(dirent->name, ". ", 11) != 0
&& memcmp(dirent->name, ".. ", 11) != 0) {
f.init(dirent);
sector.release();
offset += 32;
if (cluster_offset(offset) == 0) {
next_cluster(); // TODO: handle error
}
return f;
}
}
offset += 32;
dirent++;
sector_index++;
}
sector.release();
sector_index = 0;
if (++sector_offset >= sector_count) {
if (type == FILE_DIR_ROOT16) {
break;
} else {
if (!next_cluster()) break;
lba = custer_to_sector(current_cluster);
sector_offset = 0;
}
}
}
return f;
}


bool File::find(const char *filename, File *found)
{
bool find_unused = true;
char name83[11];
uint32_t lba, sector_count;

//Serial.println("File::open");

const char *f = filename;
if (*f == 0) return false;
char *p = name83;
while (p < name83 + 11) {
char c = *f++;
if (c == 0 || c == '/') {
while (p < name83 + 11) *p++ = ' ';
break;
}
if (c == '.') {
while (p < name83 + 8) *p++ = ' ';
continue;
}
if (c > 126) continue;
if (c >= 'a' && c <= 'z') c -= 32;
*p++ = c;
}
//Serial.print("name83 = ");
//for (uint32_t i=0; i < 11; i++) {
//Serial.printf(" %02X", name83[i]);
//}
//Serial.println();

if (type == FILE_DIR_ROOT16) {
lba = start_cluster;
sector_count = length >> 9;
} else if (type == FILE_DIR) {
current_cluster = start_cluster;
lba = custer_to_sector(start_cluster);
sector_count = (1 << SDClass::sector2cluster);
} else {
return false; // not a directory
}
while (1) {
for (uint32_t i=0; i < sector_count; i++) {
SDCache sector;
sector_t *s = sector.read(lba);
if (!s) return false;
fatdir_t *dirent = s->dir;
for (uint32_t j=0; j < 16; j++) {
if (dirent->attrib == ATTR_LONG_NAME) {
// TODO: how to match long names?
}
if (memcmp(dirent->name, name83, 11) == 0) {
//Serial.printf("found 8.3, j=%d\n", j);
found->init(dirent);
return true;
}
uint8_t b0 = dirent->name[0];
if (find_unused && (b0 == 0 || b0 == 0xE5)) {
found->dirent_lba = lba;
found->dirent_index = j;
find_unused = false;
}
if (b0 == 0) return false;
offset += 32;
dirent++;
}
lba++;
}
if (type == FILE_DIR_ROOT16) break;
if (!next_cluster()) break;
lba = custer_to_sector(current_cluster);
//Serial.printf(" next lba = %d\n", lba);
}
return false;
}

void File::init(fatdir_t *dirent)
{
offset = 0;
length = dirent->size;
start_cluster = (dirent->cluster_high << 16) | dirent->cluster_low;
current_cluster = start_cluster;
type = (dirent->attrib & ATTR_DIRECTORY) ? FILE_DIR : FILE_READ;
char *p = namestr;
const char *s = dirent->name;
for (int i=0; i < 8; i++) {
if (*s == ' ') break;
*p++ = *s++;
}
s = dirent->name + 8;
if (*s != ' ') {
*p++ = '.';
*p++ = *s++;
if (*s != ' ') *p++ = *s++;
if (*s != ' ') *p++ = *s++;
}
*p = 0;
//Serial.printf("File::init, cluster = %d, length = %d\n", start_cluster, length);
}

#endif
#endif

+ 0
- 63
fat_t3.cpp View File

@@ -1,63 +0,0 @@
/* Optimized SD Library for Teensy 3.X
* Copyright (c) 2015, Paul Stoffregen, paul@pjrc.com
*
* Development of this SD library was funded by PJRC.COM, LLC by sales of
* Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
* open source software by purchasing genuine Teensy or other PJRC products.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice, development funding notice, and this permission
* notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#if defined(__arm__)
#include "SD_t3.h"
#ifdef USE_TEENSY3_OPTIMIZED_CODE

bool File::next_cluster()
{
SDCache fat;
uint32_t lba, cluster;

lba = SDClass::fat1_begin_lba;
cluster = current_cluster;
//Serial.println();
//Serial.println("****************************************");
//Serial.println();
//Serial.printf(" current_cluster = %d\n", cluster);

if (SDClass::fat_type == 16) {
SDClass::sector_t *s = fat.read(lba + (cluster >> 8), true);
if (!s) return false;
cluster = s->u16[cluster & 255];
} else {
SDClass::sector_t *s = fat.read(lba + (cluster >> 7), true);
if (!s) return false;
cluster = s->u32[cluster & 127];
}

//Serial.printf(" new_cluster = %d\n", cluster);
//Serial.println();
//Serial.println("****************************************");
//Serial.println();
current_cluster = cluster;
if (cluster > SDClass::max_cluster) return false;
return true;
}

#endif
#endif

+ 0
- 211
file_t3.cpp View File

@@ -1,211 +0,0 @@
/* Optimized SD Library for Teensy 3.X
* Copyright (c) 2015, Paul Stoffregen, paul@pjrc.com
*
* Development of this SD library was funded by PJRC.COM, LLC by sales of
* Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
* open source software by purchasing genuine Teensy or other PJRC products.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice, development funding notice, and this permission
* notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#if defined(__arm__)
#include "SD_t3.h"
#ifdef USE_TEENSY3_OPTIMIZED_CODE

#define sector_t SDClass::sector_t

File::File()
{
type = FILE_INVALID;
namestr[0] = 0;
}

File::~File(void)
{
close();
}

size_t File::write(uint8_t b)
{
return write(&b, 1);
}

size_t File::write(const uint8_t *buf, size_t size)
{
if (type != FILE_WRITE) {
setWriteError();
return 0;
}
// TODO: a lot of work....
return 0;
}

int File::read()
{
uint8_t b;
int ret = read(&b, 1);
if (ret != 1) return -1;
return b;
}

int File::peek()
{
uint32_t save_offset = offset;
uint32_t save_cluster = current_cluster;
uint8_t b;
int ret = read(&b, 1);
if (ret != 1) return -1;
offset = save_offset;
current_cluster = save_cluster;
return b;
}

int File::available()
{
if (type > FILE_WRITE) return 0;
uint32_t maxsize = length - offset;
if (maxsize > 0x7FFFFFFF) maxsize = 0x7FFFFFFF;
return maxsize;
}

void File::flush()
{

}

int File::read(void *buf, uint32_t size)
{
if (type > FILE_WRITE) return 0;
uint32_t maxsize = length - offset;
if (size > maxsize) size = maxsize;
if (size == 0) return 0;
uint32_t count = 0;
uint8_t *dest = (uint8_t *)buf;
uint32_t lba = custer_to_sector(current_cluster);
uint32_t sindex = cluster_offset(offset);

//Serial.printf(" read %u at %u (%X)\n", size, offset, offset);

lba += sindex >> 9;
sindex &= 511;
if (sindex) {
// first read starts in the middle of a sector
do {
SDCache cache;
sector_t *sector = cache.read(lba);
if (!sector) {
//Serial.println(" read err1, unable to read");
return 0;
}
uint32_t n = 512 - sindex;
if (size < n) {
// read does not consume all of the sector
memcpy(dest, sector->u8 + sindex, size);
offset += size;
//cache.priority(+1);
return size;
} else {
// read fully consumes this sector
memcpy(dest, sector->u8 + sindex, n);
dest += n;
count = n;
offset += n;
//cache.priority(-1);
}
} while (0);
if (is_new_cluster(++lba)) {
if (!next_cluster()) {
//Serial.print(" read err1, next cluster");
return count;
}
}
if (count >= size) return count;
}
while (1) {
// every read starts from the beginning of a sector
do {
SDCache cache;
uint32_t n = size - count;
if (n < 512) {
// only part of a sector is needed
sector_t *sector = cache.read(lba);
if (!sector) {
//Serial.println(" read err2, unable to read");
return count;
}
memcpy(dest, sector->u8, n);
offset += n;
count += n;
//cache.priority(+1);
return count;
} else {
// a full sector is required
if (!cache.read(lba, dest)) return count;
dest += 512;
offset += 512;
count += 512;
}
} while (0);
if (is_new_cluster(++lba)) {
if (!next_cluster()) {
//Serial.print(" read err2, next cluster");
return count;
}
}
if (count >= size) return count;
}
}

bool File::seek(uint32_t pos)
{
if (type > FILE_WRITE) return false;
if (pos > length) return false;

//Serial.printf(" seek to %u\n", pos);
uint32_t save_cluster = current_cluster;
uint32_t count;
// TODO: if moving to a new lba, lower cache priority
signed int diff = (int)cluster_number(pos) - (int)cluster_number(offset);
if (diff >= 0) {
// seek fowards, 0 or more clusters from current position
count = diff;
} else {
// seek backwards, need to start from beginning of file
current_cluster = start_cluster;
count = cluster_number(pos);
}
while (count > 0) {
if (!next_cluster()) {
current_cluster = save_cluster;
return false;
}
count--;
}
offset = pos;
return true;
}

void File::close()
{
type = FILE_INVALID;
namestr[0] = 0;
}

#endif
#endif

+ 0
- 204
init_t3.cpp View File

@@ -1,204 +0,0 @@
/* Optimized SD Library for Teensy 3.X
* Copyright (c) 2015, Paul Stoffregen, paul@pjrc.com
*
* Development of this SD library was funded by PJRC.COM, LLC by sales of
* Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
* open source software by purchasing genuine Teensy or other PJRC products.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice, development funding notice, and this permission
* notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#if defined(__arm__)
#include "SD_t3.h"
#ifdef USE_TEENSY3_OPTIMIZED_CODE

uint8_t SDClass::fat_type;
uint32_t SDClass::fat1_begin_lba;
uint32_t SDClass::fat2_begin_lba;
uint32_t SDClass::data_begin_lba;
uint32_t SDClass::max_cluster;
uint8_t SDClass::sector2cluster;
File SDClass::rootDir;

static uint32_t unaligned_read32_align16(const void *p)
{
#ifdef KINETISK
return *(const uint32_t *)p;
#else
return *(const uint16_t *)p | (*(const uint16_t *)(p+1) << 16);
#endif
}

static uint32_t unaligned_read16_align8(const void *p)
{
#ifdef KINETISK
return *(const uint16_t *)p;
#else
return *(const uint8_t *)p | (*(const uint8_t *)(p+1) << 8);
#endif
}

#define BPB_BytsPerSec 11 // 2 bytes
#define BPB_SecPerClus 13 // 1 byte
#define BPB_NumFATs 16 // 1 byte
#define BPB_RootEntCnt 17 // 1 byte
#define BPB_TotSec16 19 // 2 bytes
#define BPB_FATSz16 22 // 2 bytes
#define BPB_TotSec32 32 // 4 bytes
#define BPB_FATSz32 36 // 4 bytes
#define BPB_RootClus 44 // 4 bytes

bool SDClass::begin(uint8_t csPin)
{
uint8_t status;
uint32_t cond, hcs, ocr;

// set up the SPI hardware
csreg = PIN_TO_BASEREG(csPin);
csmask = PIN_TO_BITMASK(csPin);
pinMode(csPin, OUTPUT);
DIRECT_WRITE_HIGH(csreg, csmask);
SPI.begin();
// send clocks to initialize hardware
SPI.beginTransaction(SD_SPI_SPEED);
for (uint8_t i=0; i < 5; i++) SPI.transfer16(0xFFFF);
// put the card into idle state
elapsedMillis msec = 0;
while (1) {
status = sd_cmd0();
//Serial.print("cmd0=");
//Serial.println(status);
if (status == 1) break;
SPI.endTransaction();
if (msec > 250) return false;
SPI.beginTransaction(SD_SPI_SPEED);
}
// detect version 1 vs 2 cards
cond = sd_cmd8();
if (cond == 0x80000000) {
// version 1 card
card_type = 1;
hcs = 0;
} else if (cond == 0x1AA) {
// version 2 card
card_type = 2;
hcs = (1<<30);
} else {
SPI.endTransaction();
return false;
}
//Serial.println();
// wait for the card to be ready
msec = 0;
while (1) {
status = sd_acmd41(hcs);
//Serial.println();
if (status == 0) break;
SPI.endTransaction();
if (status > 1) return false;
if (msec > 1500) return false;
SPI.beginTransaction(SD_SPI_SPEED);
}
//Serial.println("card is ready");
// detect high capacity cards
if (card_type == 2) {
ocr = sd_cmd58();
//Serial.print("ocr =");
//Serial.println(ocr, HEX);
if ((ocr >> 30) == 3) card_type = 3;
}
SPI.endTransaction();
//Serial.println("init ok");
// read the MBR (partition table)
SDCache s;
sector_t * mbr = s.read(0);
//Serial.printf(" mbr sig = %04X\n", mbr->u16[255]);
if (mbr->u16[255] != 0xAA55) return false;
uint32_t partition_lba = 0;
uint32_t index = 446;
do {
uint8_t type = mbr->u8[index+4];
//Serial.printf(" partition %d is type %d\n", (index-446)/16+1, type);
if (type == 6 || type == 11 || type == 12) {
partition_lba = unaligned_read32_align16(mbr->u8 + index + 8);
//Serial.printf(" partition lba = %d\n", partition_lba);
break;
}
index += 16;
} while (index < 64);
s.release();

// read the FAT volume ID
sector_t *vol = s.read(partition_lba);
if (vol->u16[255] != 0xAA55) return false;
// BPB_BytsPerSec must be 512 bytes per sector
if (unaligned_read16_align8(vol->u8 + BPB_BytsPerSec) != 512) return false;
// BPB_NumFATs must be 2 copies of the file allocation table
if (vol->u8[BPB_NumFATs] != 2) return false;
uint32_t reserved_sectors = vol->u16[14/2];
if (reserved_sectors == 0) return false;
//Serial.printf(" reserved_sectors = %d\n", reserved_sectors);
uint32_t sectors_per_cluster = vol->u8[BPB_SecPerClus];
//Serial.printf(" sectors_per_cluster = %d\n", sectors_per_cluster);
uint32_t s2c = 31 - __builtin_clz(sectors_per_cluster);
//Serial.printf(" s2c = %d\n", s2c);
sector2cluster = s2c;
uint32_t sectors_per_fat = vol->u16[BPB_FATSz16/2];
if (sectors_per_fat == 0) sectors_per_fat = vol->u32[BPB_FATSz32/4];
//Serial.printf(" sectors_per_fat = %d\n", sectors_per_fat);
uint32_t root_dir_entries = unaligned_read16_align8(vol->u8 + BPB_RootEntCnt);
//Serial.printf(" root_dir_entries = %d\n", root_dir_entries);
uint32_t root_dir_sectors = (root_dir_entries + 15) >> 4;
//Serial.printf(" root_dir_sectors = %d\n", root_dir_sectors);

uint32_t total_sectors = unaligned_read16_align8(vol->u8 + BPB_TotSec16);
if (total_sectors == 0) total_sectors = vol->u32[BPB_TotSec32/4];
//Serial.printf(" total_sectors = %d\n", total_sectors);

fat1_begin_lba = partition_lba + reserved_sectors;
fat2_begin_lba = fat1_begin_lba + sectors_per_fat;
data_begin_lba = fat2_begin_lba + sectors_per_fat + root_dir_sectors;

uint32_t cluster_count = (total_sectors - reserved_sectors
- root_dir_sectors - (sectors_per_fat << 1)) >> s2c;
//Serial.printf(" cluster_count = %d\n", cluster_count);
max_cluster = cluster_count + 1;
if (cluster_count < 4085) {
return false; // FAT12
} else if (cluster_count < 65525) {
fat_type = 16;
rootDir.length = root_dir_entries << 5;
rootDir.start_cluster = partition_lba + reserved_sectors + sectors_per_fat * 2;
rootDir.type = FILE_DIR_ROOT16;
} else {
fat_type = 32;
rootDir.length = 0;
rootDir.start_cluster = vol->u32[BPB_RootClus/4];
//Serial.printf(" root cluster = %d\n", rootDir.start_cluster);
rootDir.type = FILE_DIR;
}
rootDir.current_cluster = rootDir.start_cluster;
rootDir.offset = 0;
s.release();
//Serial.println(sizeof(fatdir_t));
return true;
}

#endif
#endif

+ 5
- 5
library.properties View File

@@ -1,9 +1,9 @@
name=SD
version=1.2.2
author=Arduino, SparkFun
version=2.0.0
author=Paul Stoffregen
maintainer=Paul Stoffregen
sentence=Enables reading and writing on SD cards. This version is optimized for Teensy.
paragraph=Once an SD memory card is connected to the SPI interface of the Arduino or Genuino board you can create files and read/write on them. You can also move through directories on the SD card.
sentence=Arduino SD compatibility layer for SdFat.
paragraph=To access SD cards, we now use Bill Greiman's SdFat library. This library just provides a thin wrapper so programs written for Arduino's SD library can use SdFat. None of the original Arduino SD library code is present in this compatibility library.
category=Data Storage
url=http://www.arduino.cc/en/Reference/SD
url=https://github.com/PaulStoffregen/SD
architectures=*

+ 4
- 0
src/SD.cpp View File

@@ -0,0 +1,4 @@
#include <Arduino.h>
#include <SD.h>

SDClass SD;

+ 123
- 0
src/SD.h View File

@@ -0,0 +1,123 @@
#ifndef __SD_H__
#define __SD_H__

#include <Arduino.h>
#include <FS.h>
#include <SdFat.h>

class SDFile : public File
{
public:
SDFile(const FsFile &file) : sdfatfile(file), filename(nullptr) { }
virtual ~SDFile(void) {
if (sdfatfile) sdfatfile.close();
if (filename) free(filename);
}
#ifdef FILE_WHOAMI
virtual void whoami() {
Serial.printf(" SDFile this=%x, refcount=%u\n",
(int)this, getRefcount());
}
#endif
virtual size_t write(const void *buf, size_t size) {
return sdfatfile.write(buf, size);
}
virtual int peek() {
return sdfatfile.peek();
}
virtual int available() {
return sdfatfile.available();
}
virtual void flush() {
sdfatfile.flush();
}
virtual size_t read(void *buf, size_t nbyte) {
return sdfatfile.read(buf, nbyte);
}
virtual bool seek(uint32_t pos, int mode = SeekSet) {
if (mode == SeekSet) return sdfatfile.seekSet(pos);
if (mode == SeekCur) return sdfatfile.seekCur(pos);
if (mode == SeekEnd) return sdfatfile.seekEnd(pos);
return false;
}
virtual uint32_t position() {
return sdfatfile.curPosition();
}
virtual uint32_t size() {
return sdfatfile.size();
}
virtual void close() {
if (filename) {
free(filename);
filename = nullptr;
}
sdfatfile.close();
}
virtual operator bool() {
return sdfatfile.isOpen();
}
virtual const char * name() {
if (!filename) {
filename = (char *)malloc(256);
if (filename) {
sdfatfile.getName(filename, 256);
} else {
static char zeroterm = 0;
filename = &zeroterm;
}
}
return filename;
}
virtual boolean isDirectory(void) {
return sdfatfile.isDirectory();
}
virtual File openNextFile(uint8_t mode=0) {
FsFile file = sdfatfile.openNextFile();
if (file) return File(new SDFile(file));
return File();
}
virtual void rewindDirectory(void) {
sdfatfile.rewindDirectory();
}
using Print::write;
private:
FsFile sdfatfile;
char *filename;
};



class SDClass : public FS
{
public:
SDClass() { }
bool begin(uint8_t csPin = 10) {
return sdfs.begin(csPin, SD_SCK_MHZ(24));
}
File open(const char *filepath, uint8_t mode = FILE_READ) {
oflag_t flags = O_READ;
if (mode == FILE_WRITE) flags = O_READ | O_WRITE | O_CREAT;
FsFile file = sdfs.open(filepath, flags);
// TODO: Arduino's default to seek to end of writable file
if (file) return File(new SDFile(file));
return File();
}
bool exists(const char *filepath) {
return sdfs.exists(filepath);
}
bool mkdir(const char *filepath) {
return sdfs.mkdir(filepath);
}
bool remove(const char *filepath) {
return sdfs.remove(filepath);
}
bool rmdir(const char *filepath) {
return sdfs.rmdir(filepath);
}
public: // allow access, so users can mix SD & SdFat APIs
SdFs sdfs;
};

extern SDClass SD;

#endif

+ 0
- 418
utility/FatStructs.h View File

@@ -1,418 +0,0 @@
/* Arduino SdFat Library
* Copyright (C) 2009 by William Greiman
*
* This file is part of the Arduino SdFat Library
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Arduino SdFat Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifndef FatStructs_h
#define FatStructs_h
/**
* \file
* FAT file structures
*/
/*
* mostly from Microsoft document fatgen103.doc
* http://www.microsoft.com/whdc/system/platform/firmware/fatgen.mspx
*/
//------------------------------------------------------------------------------
/** Value for byte 510 of boot block or MBR */
uint8_t const BOOTSIG0 = 0X55;
/** Value for byte 511 of boot block or MBR */
uint8_t const BOOTSIG1 = 0XAA;
//------------------------------------------------------------------------------
/**
* \struct partitionTable
* \brief MBR partition table entry
*
* A partition table entry for a MBR formatted storage device.
* The MBR partition table has four entries.
*/
struct partitionTable {
/**
* Boot Indicator . Indicates whether the volume is the active
* partition. Legal values include: 0X00. Do not use for booting.
* 0X80 Active partition.
*/
uint8_t boot;
/**
* Head part of Cylinder-head-sector address of the first block in
* the partition. Legal values are 0-255. Only used in old PC BIOS.
*/
uint8_t beginHead;
/**
* Sector part of Cylinder-head-sector address of the first block in
* the partition. Legal values are 1-63. Only used in old PC BIOS.
*/
unsigned beginSector : 6;
/** High bits cylinder for first block in partition. */
unsigned beginCylinderHigh : 2;
/**
* Combine beginCylinderLow with beginCylinderHigh. Legal values
* are 0-1023. Only used in old PC BIOS.
*/
uint8_t beginCylinderLow;
/**
* Partition type. See defines that begin with PART_TYPE_ for
* some Microsoft partition types.
*/
uint8_t type;
/**
* head part of cylinder-head-sector address of the last sector in the
* partition. Legal values are 0-255. Only used in old PC BIOS.
*/
uint8_t endHead;
/**
* Sector part of cylinder-head-sector address of the last sector in
* the partition. Legal values are 1-63. Only used in old PC BIOS.
*/
unsigned endSector : 6;
/** High bits of end cylinder */
unsigned endCylinderHigh : 2;
/**
* Combine endCylinderLow with endCylinderHigh. Legal values
* are 0-1023. Only used in old PC BIOS.
*/
uint8_t endCylinderLow;
/** Logical block address of the first block in the partition. */
uint32_t firstSector;
/** Length of the partition, in blocks. */
uint32_t totalSectors;
} __attribute__((packed));
/** Type name for partitionTable */
typedef struct partitionTable part_t;
//------------------------------------------------------------------------------
/**
* \struct masterBootRecord
*
* \brief Master Boot Record
*
* The first block of a storage device that is formatted with a MBR.
*/
struct masterBootRecord {
/** Code Area for master boot program. */
uint8_t codeArea[440];
/** Optional WindowsNT disk signature. May contain more boot code. */
uint32_t diskSignature;
/** Usually zero but may be more boot code. */
uint16_t usuallyZero;
/** Partition tables. */
part_t part[4];
/** First MBR signature byte. Must be 0X55 */
uint8_t mbrSig0;
/** Second MBR signature byte. Must be 0XAA */
uint8_t mbrSig1;
} __attribute__((packed));
/** Type name for masterBootRecord */
typedef struct masterBootRecord mbr_t;
//------------------------------------------------------------------------------
/**
* \struct biosParmBlock
*
* \brief BIOS parameter block
*
* The BIOS parameter block describes the physical layout of a FAT volume.
*/
struct biosParmBlock {
/**
* Count of bytes per sector. This value may take on only the
* following values: 512, 1024, 2048 or 4096
*/
uint16_t bytesPerSector;
/**
* Number of sectors per allocation unit. This value must be a
* power of 2 that is greater than 0. The legal values are
* 1, 2, 4, 8, 16, 32, 64, and 128.
*/
uint8_t sectorsPerCluster;
/**
* Number of sectors before the first FAT.
* This value must not be zero.
*/
uint16_t reservedSectorCount;
/** The count of FAT data structures on the volume. This field should
* always contain the value 2 for any FAT volume of any type.
*/
uint8_t fatCount;
/**
* For FAT12 and FAT16 volumes, this field contains the count of
* 32-byte directory entries in the root directory. For FAT32 volumes,
* this field must be set to 0. For FAT12 and FAT16 volumes, this
* value should always specify a count that when multiplied by 32
* results in a multiple of bytesPerSector. FAT16 volumes should
* use the value 512.
*/
uint16_t rootDirEntryCount;
/**
* This field is the old 16-bit total count of sectors on the volume.
* This count includes the count of all sectors in all four regions
* of the volume. This field can be 0; if it is 0, then totalSectors32
* must be non-zero. For FAT32 volumes, this field must be 0. For
* FAT12 and FAT16 volumes, this field contains the sector count, and
* totalSectors32 is 0 if the total sector count fits
* (is less than 0x10000).
*/
uint16_t totalSectors16;
/**
* This dates back to the old MS-DOS 1.x media determination and is
* no longer usually used for anything. 0xF8 is the standard value
* for fixed (non-removable) media. For removable media, 0xF0 is
* frequently used. Legal values are 0xF0 or 0xF8-0xFF.
*/
uint8_t mediaType;
/**
* Count of sectors occupied by one FAT on FAT12/FAT16 volumes.
* On FAT32 volumes this field must be 0, and sectorsPerFat32
* contains the FAT size count.
*/
uint16_t sectorsPerFat16;
/** Sectors per track for interrupt 0x13. Not used otherwise. */
uint16_t sectorsPerTrtack;
/** Number of heads for interrupt 0x13. Not used otherwise. */
uint16_t headCount;
/**
* Count of hidden sectors preceding the partition that contains this
* FAT volume. This field is generally only relevant for media
* visible on interrupt 0x13.
*/
uint32_t hidddenSectors;
/**
* This field is the new 32-bit total count of sectors on the volume.
* This count includes the count of all sectors in all four regions
* of the volume. This field can be 0; if it is 0, then
* totalSectors16 must be non-zero.
*/
uint32_t totalSectors32;
/**
* Count of sectors occupied by one FAT on FAT32 volumes.
*/
uint32_t sectorsPerFat32;
/**
* This field is only defined for FAT32 media and does not exist on
* FAT12 and FAT16 media.
* Bits 0-3 -- Zero-based number of active FAT.
* Only valid if mirroring is disabled.
* Bits 4-6 -- Reserved.
* Bit 7 -- 0 means the FAT is mirrored at runtime into all FATs.
* -- 1 means only one FAT is active; it is the one referenced in bits 0-3.
* Bits 8-15 -- Reserved.
*/
uint16_t fat32Flags;
/**
* FAT32 version. High byte is major revision number.
* Low byte is minor revision number. Only 0.0 define.
*/
uint16_t fat32Version;
/**
* Cluster number of the first cluster of the root directory for FAT32.
* This usually 2 but not required to be 2.
*/
uint32_t fat32RootCluster;
/**
* Sector number of FSINFO structure in the reserved area of the
* FAT32 volume. Usually 1.
*/
uint16_t fat32FSInfo;
/**
* If non-zero, indicates the sector number in the reserved area
* of the volume of a copy of the boot record. Usually 6.
* No value other than 6 is recommended.
*/
uint16_t fat32BackBootBlock;
/**
* Reserved for future expansion. Code that formats FAT32 volumes
* should always set all of the bytes of this field to 0.
*/
uint8_t fat32Reserved[12];
} __attribute__((packed));
/** Type name for biosParmBlock */
typedef struct biosParmBlock bpb_t;
//------------------------------------------------------------------------------
/**
* \struct fat32BootSector
*
* \brief Boot sector for a FAT16 or FAT32 volume.
*
*/
struct fat32BootSector {
/** X86 jmp to boot program */
uint8_t jmpToBootCode[3];
/** informational only - don't depend on it */
char oemName[8];
/** BIOS Parameter Block */
bpb_t bpb;
/** for int0x13 use value 0X80 for hard drive */
uint8_t driveNumber;
/** used by Windows NT - should be zero for FAT */
uint8_t reserved1;
/** 0X29 if next three fields are valid */
uint8_t bootSignature;
/** usually generated by combining date and time */
uint32_t volumeSerialNumber;
/** should match volume label in root dir */
char volumeLabel[11];
/** informational only - don't depend on it */
char fileSystemType[8];
/** X86 boot code */
uint8_t bootCode[420];
/** must be 0X55 */
uint8_t bootSectorSig0;
/** must be 0XAA */
uint8_t bootSectorSig1;
} __attribute__((packed));
//------------------------------------------------------------------------------
// End Of Chain values for FAT entries
/** FAT16 end of chain value used by Microsoft. */
uint16_t const FAT16EOC = 0XFFFF;
/** Minimum value for FAT16 EOC. Use to test for EOC. */
uint16_t const FAT16EOC_MIN = 0XFFF8;
/** FAT32 end of chain value used by Microsoft. */
uint32_t const FAT32EOC = 0X0FFFFFFF;
/** Minimum value for FAT32 EOC. Use to test for EOC. */
uint32_t const FAT32EOC_MIN = 0X0FFFFFF8;
/** Mask a for FAT32 entry. Entries are 28 bits. */
uint32_t const FAT32MASK = 0X0FFFFFFF;
/** Type name for fat32BootSector */
typedef struct fat32BootSector fbs_t;
//------------------------------------------------------------------------------
/**
* \struct directoryEntry
* \brief FAT short directory entry
*
* Short means short 8.3 name, not the entry size.
*
* Date Format. A FAT directory entry date stamp is a 16-bit field that is
* basically a date relative to the MS-DOS epoch of 01/01/1980. Here is the
* format (bit 0 is the LSB of the 16-bit word, bit 15 is the MSB of the
* 16-bit word):
*
* Bits 9-15: Count of years from 1980, valid value range 0-127
* inclusive (1980-2107).
*
* Bits 5-8: Month of year, 1 = January, valid value range 1-12 inclusive.
*
* Bits 0-4: Day of month, valid value range 1-31 inclusive.
*
* Time Format. A FAT directory entry time stamp is a 16-bit field that has
* a granularity of 2 seconds. Here is the format (bit 0 is the LSB of the
* 16-bit word, bit 15 is the MSB of the 16-bit word).
*
* Bits 11-15: Hours, valid value range 0-23 inclusive.
*
* Bits 5-10: Minutes, valid value range 0-59 inclusive.
*
* Bits 0-4: 2-second count, valid value range 0-29 inclusive (0 - 58 seconds).
*
* The valid time range is from Midnight 00:00:00 to 23:59:58.
*/
struct directoryEntry {
/**
* Short 8.3 name.
* The first eight bytes contain the file name with blank fill.
* The last three bytes contain the file extension with blank fill.
*/
uint8_t name[11];
/** Entry attributes.
*
* The upper two bits of the attribute byte are reserved and should
* always be set to 0 when a file is created and never modified or
* looked at after that. See defines that begin with DIR_ATT_.
*/
uint8_t attributes;
/**
* Reserved for use by Windows NT. Set value to 0 when a file is
* created and never modify or look at it after that.
*/
uint8_t reservedNT;
/**
* The granularity of the seconds part of creationTime is 2 seconds
* so this field is a count of tenths of a second and its valid
* value range is 0-199 inclusive. (WHG note - seems to be hundredths)
*/
uint8_t creationTimeTenths;
/** Time file was created. */
uint16_t creationTime;
/** Date file was created. */
uint16_t creationDate;
/**
* Last access date. Note that there is no last access time, only
* a date. This is the date of last read or write. In the case of
* a write, this should be set to the same date as lastWriteDate.
*/
uint16_t lastAccessDate;
/**
* High word of this entry's first cluster number (always 0 for a
* FAT12 or FAT16 volume).
*/
uint16_t firstClusterHigh;
/** Time of last write. File creation is considered a write. */
uint16_t lastWriteTime;
/** Date of last write. File creation is considered a write. */
uint16_t lastWriteDate;
/** Low word of this entry's first cluster number. */
uint16_t firstClusterLow;
/** 32-bit unsigned holding this file's size in bytes. */
uint32_t fileSize;
} __attribute__((packed));
//------------------------------------------------------------------------------
// Definitions for directory entries
//
/** Type name for directoryEntry */
typedef struct directoryEntry dir_t;
/** escape for name[0] = 0XE5 */
uint8_t const DIR_NAME_0XE5 = 0X05;
/** name[0] value for entry that is free after being "deleted" */
uint8_t const DIR_NAME_DELETED = 0XE5;
/** name[0] value for entry that is free and no allocated entries follow */
uint8_t const DIR_NAME_FREE = 0X00;
/** file is read-only */
uint8_t const DIR_ATT_READ_ONLY = 0X01;
/** File should hidden in directory listings */
uint8_t const DIR_ATT_HIDDEN = 0X02;
/** Entry is for a system file */
uint8_t const DIR_ATT_SYSTEM = 0X04;
/** Directory entry contains the volume label */
uint8_t const DIR_ATT_VOLUME_ID = 0X08;
/** Entry is for a directory */
uint8_t const DIR_ATT_DIRECTORY = 0X10;
/** Old DOS archive bit for backup support */
uint8_t const DIR_ATT_ARCHIVE = 0X20;
/** Test value for long name entry. Test is
(d->attributes & DIR_ATT_LONG_NAME_MASK) == DIR_ATT_LONG_NAME. */
uint8_t const DIR_ATT_LONG_NAME = 0X0F;
/** Test mask for long name entry */
uint8_t const DIR_ATT_LONG_NAME_MASK = 0X3F;
/** defined attribute bits */
uint8_t const DIR_ATT_DEFINED_BITS = 0X3F;
/** Directory entry is part of a long name */
static inline uint8_t DIR_IS_LONG_NAME(const dir_t* dir) {
return (dir->attributes & DIR_ATT_LONG_NAME_MASK) == DIR_ATT_LONG_NAME;
}
/** Mask for file/subdirectory tests */
uint8_t const DIR_ATT_FILE_TYPE_MASK = (DIR_ATT_VOLUME_ID | DIR_ATT_DIRECTORY);
/** Directory entry is for a file */
static inline uint8_t DIR_IS_FILE(const dir_t* dir) {
return (dir->attributes & DIR_ATT_FILE_TYPE_MASK) == 0;
}
/** Directory entry is for a subdirectory */
static inline uint8_t DIR_IS_SUBDIR(const dir_t* dir) {
return (dir->attributes & DIR_ATT_FILE_TYPE_MASK) == DIR_ATT_DIRECTORY;
}
/** Directory entry is for a file or subdirectory */
static inline uint8_t DIR_IS_FILE_OR_SUBDIR(const dir_t* dir) {
return (dir->attributes & DIR_ATT_VOLUME_ID) == 0;
}
#endif // FatStructs_h

+ 0
- 1441
utility/NXP_SDHC.cpp
File diff suppressed because it is too large
View File


+ 0
- 16
utility/NXP_SDHC.h View File

@@ -1,16 +0,0 @@
#ifndef _NXP_SDHC_H
#define _NXP_SDHC_H

#define SDHC_STATUS_NOINIT 0x01 /* Drive not initialized */
#define SDHC_STATUS_NODISK 0x02 /* No medium in the drive */
#define SDHC_STATUS_PROTECT 0x04 /* Write protected */

uint8_t SDHC_CardInit(void);
uint8_t SDHC_CardGetType(void);
//uint8_t SDHC_CardGetStatus(void);
//uint32_t SDHC_CardGetBlockCnt(void);

int SDHC_CardReadBlock(void * buff, uint32_t sector);
int SDHC_CardWriteBlock(const void * buff, uint32_t sector);

#endif

+ 0
- 544
utility/Sd2Card.cpp View File

@@ -1,544 +0,0 @@
/* Arduino Sd2Card Library
* Copyright (C) 2009 by William Greiman
*
* This file is part of the Arduino Sd2Card Library
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Arduino Sd2Card Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <Arduino.h>
#include <SPI.h>
#include "Sd2Card.h"
#ifdef SPI_HAS_TRANSACTION
static SPISettings settings;
#endif
#if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
#define USE_TEENSY3_SPI
// Teensy 3.0 functions (copied from sdfatlib20130629)
#include <kinetis.h>
// Limit initial fifo to three entries to avoid fifo overrun
#define SPI_INITIAL_FIFO_DEPTH 3
// define some symbols that are not in mk20dx128.h
#ifndef SPI_SR_RXCTR
#define SPI_SR_RXCTR 0XF0
#endif // SPI_SR_RXCTR
#ifndef SPI_PUSHR_CONT
#define SPI_PUSHR_CONT 0X80000000
#endif // SPI_PUSHR_CONT
#ifndef SPI_PUSHR_CTAS
#define SPI_PUSHR_CTAS(n) (((n) & 7) << 28)
#endif // SPI_PUSHR_CTAS
static void spiBegin() {
SIM_SCGC6 |= SIM_SCGC6_SPI0;
}
static void spiInit(uint8_t spiRate) {
switch (spiRate) {
// the top 2 speeds are set to 24 MHz, for the SD library defaults
case 0: settings = SPISettings(24000000, MSBFIRST, SPI_MODE0); break;
case 1: settings = SPISettings(24000000, MSBFIRST, SPI_MODE0); break;
case 2: settings = SPISettings(8000000, MSBFIRST, SPI_MODE0); break;
case 3: settings = SPISettings(4000000, MSBFIRST, SPI_MODE0); break;
case 4: settings = SPISettings(3000000, MSBFIRST, SPI_MODE0); break;
case 5: settings = SPISettings(2000000, MSBFIRST, SPI_MODE0); break;
default: settings = SPISettings(400000, MSBFIRST, SPI_MODE0);
}
SPI.begin();
}
/** SPI receive a byte */
static uint8_t spiRec() {
SPI0_MCR |= SPI_MCR_CLR_RXF;
SPI0_SR = SPI_SR_TCF;
SPI0_PUSHR = 0xFF;
while (!(SPI0_SR & SPI_SR_TCF)) {}
return SPI0_POPR;
}
/** SPI receive multiple bytes */
static uint8_t spiRec(uint8_t* buf, size_t len) {
// clear any data in RX FIFO
SPI0_MCR = SPI_MCR_MSTR | SPI_MCR_CLR_RXF | SPI_MCR_PCSIS(0x1F);
// use 16 bit frame to avoid TD delay between frames
// get one byte if len is odd
if (len & 1) {
*buf++ = spiRec();
len--;
}
// initial number of words to push into TX FIFO
int nf = len/2 < SPI_INITIAL_FIFO_DEPTH ? len/2 : SPI_INITIAL_FIFO_DEPTH;
for (int i = 0; i < nf; i++) {
SPI0_PUSHR = SPI_PUSHR_CONT | SPI_PUSHR_CTAS(1) | 0XFFFF;
}
uint8_t* limit = buf + len - 2*nf;
while (buf < limit) {
while (!(SPI0_SR & SPI_SR_RXCTR)) {}
SPI0_PUSHR = SPI_PUSHR_CONT | SPI_PUSHR_CTAS(1) | 0XFFFF;
uint16_t w = SPI0_POPR;
*buf++ = w >> 8;
*buf++ = w & 0XFF;
}
// limit for rest of RX data
limit += 2*nf;
while (buf < limit) {
while (!(SPI0_SR & SPI_SR_RXCTR)) {}
uint16_t w = SPI0_POPR;
*buf++ = w >> 8;
*buf++ = w & 0XFF;
}
return 0;
}
static void spiRecIgnore(size_t len) {
// clear any data in RX FIFO
SPI0_MCR = SPI_MCR_MSTR | SPI_MCR_CLR_RXF | SPI_MCR_PCSIS(0x1F);
// use 16 bit frame to avoid TD delay between frames
// get one byte if len is odd
if (len & 1) {
spiRec();
len--;
}
// initial number of words to push into TX FIFO
int nf = len/2 < SPI_INITIAL_FIFO_DEPTH ? len/2 : SPI_INITIAL_FIFO_DEPTH;
for (int i = 0; i < nf; i++) {
SPI0_PUSHR = SPI_PUSHR_CONT | SPI_PUSHR_CTAS(1) | 0XFFFF;
len -= 2;
}
//uint8_t* limit = buf + len - 2*nf;
//while (buf < limit) {
while (len > 0) {
while (!(SPI0_SR & SPI_SR_RXCTR)) {}
SPI0_PUSHR = SPI_PUSHR_CONT | SPI_PUSHR_CTAS(1) | 0XFFFF;
SPI0_POPR;
len -= 2;
}
// limit for rest of RX data
while (nf > 0) {
while (!(SPI0_SR & SPI_SR_RXCTR)) {}
SPI0_POPR;
nf--;
}
}
/** SPI send a byte */
static void spiSend(uint8_t b) {
SPI0_MCR |= SPI_MCR_CLR_RXF;
SPI0_SR = SPI_SR_TCF;
SPI0_PUSHR = b;
while (!(SPI0_SR & SPI_SR_TCF)) {}
}
/** SPI send multiple bytes */
#elif defined(__IMXRT1052__) || defined(__IMXRT1062__) || defined(__MKL26Z64__)
#define USE_SPI_LIB
static void spiInit(uint8_t spiRate) {
switch (spiRate) {
#ifdef __MKL26Z64__
// the top 2 speeds are set to 24 MHz, for the SD library defaults
case 0: settings = SPISettings(24000000, MSBFIRST, SPI_MODE0); break;
#else
// the top 2 speeds are set to 24 MHz, for the SD library defaults
case 0: settings = SPISettings(25200000, MSBFIRST, SPI_MODE0); break;
#endif
case 1: settings = SPISettings(24000000, MSBFIRST, SPI_MODE0); break;
case 2: settings = SPISettings(8000000, MSBFIRST, SPI_MODE0); break;
case 3: settings = SPISettings(4000000, MSBFIRST, SPI_MODE0); break;
case 4: settings = SPISettings(3000000, MSBFIRST, SPI_MODE0); break;
case 5: settings = SPISettings(2000000, MSBFIRST, SPI_MODE0); break;
default: settings = SPISettings(400000, MSBFIRST, SPI_MODE0);
}
SPI.begin();
}
static void spiSend(uint8_t b) {
SPI.transfer(b);
}
static uint8_t spiRec(void) {
return SPI.transfer(0xff);
}
static void spiRec(uint8_t* buf, size_t len) {
memset(buf, 0xFF, len);
SPI.transfer(buf, len);
}
static void spiRecIgnore(size_t len) {
for (size_t i=0; i < len; i++)
SPI.transfer(0xff);
}
//------------------------------------------------------------------------------
#else
// functions for hardware SPI
/** Send a byte to the card */
static void spiSend(uint8_t b) {
SPDR = b;
while (!(SPSR & (1 << SPIF)));
}
/** Receive a byte from the card */
static uint8_t spiRec(void) {
spiSend(0XFF);
return SPDR;
}
#endif
//------------------------------------------------------------------------------
// send command and return error code. Return zero for OK
uint8_t Sd2Card::cardCommand(uint8_t cmd, uint32_t arg)
{
// wait up to 300 ms if busy
waitNotBusy(300);
// send command
spiSend(cmd | 0x40);
// send argument
for (int8_t s = 24; s >= 0; s -= 8) spiSend(arg >> s);
// send CRC
uint8_t crc = 0XFF;
if (cmd == CMD0) crc = 0X95; // correct crc for CMD0 with arg 0
if (cmd == CMD8) crc = 0X87; // correct crc for CMD8 with arg 0X1AA
spiSend(crc);
// wait for response
for (uint8_t i = 0; ((status_ = spiRec()) & 0X80) && i != 0XFF; i++);
return status_;
}
//------------------------------------------------------------------------------
#ifdef SPI_HAS_TRANSACTION
static uint8_t chip_select_asserted = 0;
#endif
void Sd2Card::chipSelectHigh(void) {
digitalWrite(chipSelectPin_, HIGH);
#ifdef SPI_HAS_TRANSACTION
if (chip_select_asserted) {
chip_select_asserted = 0;
SPI.endTransaction();
}
#endif
}
//------------------------------------------------------------------------------
void Sd2Card::chipSelectLow(void) {
#ifdef SPI_HAS_TRANSACTION
if (!chip_select_asserted) {
chip_select_asserted = 1;
SPI.beginTransaction(settings);
}
#endif
digitalWrite(chipSelectPin_, LOW);
}
//------------------------------------------------------------------------------
/**
* Initialize an SD flash memory card.
*
* \param[in] sckRateID SPI clock rate selector. See setSckRate().
* \param[in] chipSelectPin SD chip select pin number.
*
* \return The value one, true, is returned for success and
* the value zero, false, is returned for failure.
*/
uint8_t Sd2Card::SD_init(uint8_t sckRateID, uint8_t chipSelectPin) {
type_ = 0;
chipSelectPin_ = chipSelectPin;
// 16-bit init start time allows over a minute
unsigned int t0 = millis();
uint32_t arg;
digitalWrite(chipSelectPin_, HIGH);
pinMode(chipSelectPin_, OUTPUT);
digitalWrite(chipSelectPin_, HIGH);
#if defined(USE_TEENSY3_SPI)
spiBegin();
spiInit(6);
#elif defined(USE_SPI_LIB)
spiInit(6);
pinMode(SS_PIN, OUTPUT);
digitalWrite(SS_PIN, HIGH); // disable any SPI device using hardware SS pin
#else
// set pin modes
pinMode(SPI_MISO_PIN, INPUT);
pinMode(SPI_MOSI_PIN, OUTPUT);
pinMode(SPI_SCK_PIN, OUTPUT);
// SS must be in output mode even it is not chip select
pinMode(SS_PIN, OUTPUT);
digitalWrite(SS_PIN, HIGH); // disable any SPI device using hardware SS pin
// Enable SPI, Master, clock rate f_osc/128
SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR1) | (1 << SPR0);
// clear double speed
SPSR &= ~(1 << SPI2X);
#ifdef SPI_HAS_TRANSACTION
settings = SPISettings(250000, MSBFIRST, SPI_MODE0);
#endif
#endif // not USE_TEENSY3_SPI
// must supply min of 74 clock cycles with CS high.
#ifdef SPI_HAS_TRANSACTION
SPI.beginTransaction(settings);
#endif
for (uint8_t i = 0; i < 10; i++) spiSend(0XFF);
#ifdef SPI_HAS_TRANSACTION
SPI.endTransaction();
#endif
chipSelectLow();
// command to go idle in SPI mode
while ((status_ = cardCommand(CMD0, 0)) != R1_IDLE_STATE) {
unsigned int d = millis() - t0;
if (d > SD_INIT_TIMEOUT) {
goto fail; // SD_CARD_ERROR_CMD0
}
}
// check SD version
if ((cardCommand(CMD8, 0x1AA) & R1_ILLEGAL_COMMAND)) {
type_ = SD_CARD_TYPE_SD1;
} else {
// only need last byte of r7 response
for (uint8_t i = 0; i < 4; i++) status_ = spiRec();
if (status_ != 0XAA) {
goto fail; // SD_CARD_ERROR_CMD8
}
type_ = SD_CARD_TYPE_SD2;
}
// initialize card and send host supports SDHC if SD2
arg = (type_ == SD_CARD_TYPE_SD2) ? 0X40000000 : 0;
while ((status_ = cardAcmd(ACMD41, arg)) != R1_READY_STATE) {
// check for timeout
unsigned int d = millis() - t0;
if (d > SD_INIT_TIMEOUT) {
goto fail; // SD_CARD_ERROR_ACMD41
}
}
// if SD2 read OCR register to check for SDHC card
if (type_ == SD_CARD_TYPE_SD2) {
if (cardCommand(CMD58, 0)) {
goto fail; // SD_CARD_ERROR_CMD58
}
if ((spiRec() & 0XC0) == 0XC0) type_ = SD_CARD_TYPE_SDHC;
// discard rest of ocr - contains allowed voltage range
for (uint8_t i = 0; i < 3; i++) spiRec();
}
chipSelectHigh();
return setSckRate(sckRateID);
fail:
chipSelectHigh();
return false;
}
//------------------------------------------------------------------------------
/**
* Read a 512 byte block from an SD card device.
*
* \param[in] block Logical block to be read.
* \param[out] dst Pointer to the location that will receive the data.
* \return The value one, true, is returned for success and
* the value zero, false, is returned for failure.
*/
uint8_t Sd2Card::SD_readBlock(uint32_t block, uint8_t* dst)
{
// use address if not SDHC card
if (type_ != SD_CARD_TYPE_SDHC) block <<= 9;
chipSelectLow();
if (cardCommand(CMD17, block)) {
goto fail; // SD_CARD_ERROR_CMD17
}
if (!waitStartBlock()) {
goto fail;
}
#if defined(USE_TEENSY3_SPI) | defined(USE_SPI_LIB)
spiRec(dst, 512);
spiRecIgnore(2);
#else // OPTIMIZE_HARDWARE_SPI
// start first spi transfer
SPDR = 0XFF;
// transfer data
for (uint16_t i = 0; i < 511; i++) {
while (!(SPSR & (1 << SPIF)));
dst[i] = SPDR;
SPDR = 0XFF;
}
// wait for last byte
while (!(SPSR & (1 << SPIF)));
dst[511] = SPDR;
// skip CRC bytes
spiRec();
spiRec();
#endif
chipSelectHigh();
return true;
fail:
chipSelectHigh();
return false;
}
//------------------------------------------------------------------------------
/**
* Set the SPI clock rate.
*
* \param[in] sckRateID A value in the range [0, 6].
*
* 0 = 8 MHz
* 1 = 4 MHz
* 2 = 2 MHz
* 3 = 1 MHz
* 4 = 500 kHz
* 5 = 125 kHz
* 6 = 63 kHz
*
* The SPI clock will be set to F_CPU/pow(2, 1 + sckRateID). The maximum
* SPI rate is F_CPU/2 for \a sckRateID = 0 and the minimum rate is F_CPU/128
* for \a scsRateID = 6.
*
* \return The value one, true, is returned for success and the value zero,
* false, is returned for an invalid value of \a sckRateID.
*/
uint8_t Sd2Card::setSckRate(uint8_t sckRateID) {
#if defined(USE_TEENSY3_SPI) || defined(USE_SPI_LIB)
spiInit(sckRateID);
return true;
#else
if (sckRateID > 6) sckRateID = 6;
// see avr processor datasheet for SPI register bit definitions
if ((sckRateID & 1) || sckRateID == 6) {
SPSR &= ~(1 << SPI2X);
} else {
SPSR |= (1 << SPI2X);
}
SPCR &= ~((1 <<SPR1) | (1 << SPR0));
SPCR |= (sckRateID & 4 ? (1 << SPR1) : 0)
| (sckRateID & 2 ? (1 << SPR0) : 0);
#ifdef SPI_HAS_TRANSACTION
switch (sckRateID) {
case 0: settings = SPISettings(8000000, MSBFIRST, SPI_MODE0); break;
case 1: settings = SPISettings(4000000, MSBFIRST, SPI_MODE0); break;
case 2: settings = SPISettings(2000000, MSBFIRST, SPI_MODE0); break;
case 3: settings = SPISettings(1000000, MSBFIRST, SPI_MODE0); break;
case 4: settings = SPISettings(500000, MSBFIRST, SPI_MODE0); break;
case 5: settings = SPISettings(250000, MSBFIRST, SPI_MODE0); break;
default: settings = SPISettings(125000, MSBFIRST, SPI_MODE0);
}
#endif
return true;
#endif
}
//------------------------------------------------------------------------------
// wait for card to go not busy
uint8_t Sd2Card::waitNotBusy(unsigned int timeoutMillis) {
unsigned int t0 = millis();
unsigned int d;
do {
if (spiRec() == 0XFF) return true;
d = millis() - t0;
}
while (d < timeoutMillis);
return false;
}
//------------------------------------------------------------------------------
/** Wait for start block token */
uint8_t Sd2Card::waitStartBlock(void) {
unsigned int t0 = millis();
while ((status_ = spiRec()) == 0XFF) {
unsigned int d = millis() - t0;
if (d > SD_READ_TIMEOUT) {
return false; // SD_CARD_ERROR_READ_TIMEOUT
}
}
if (status_ != DATA_START_BLOCK) {
return false; // SD_CARD_ERROR_READ
}
return true;
}
//------------------------------------------------------------------------------
/**
* Writes a 512 byte block to an SD card.
*
* \param[in] blockNumber Logical block to be written.
* \param[in] src Pointer to the location of the data to be written.
* \return The value one, true, is returned for success and
* the value zero, false, is returned for failure.
*/
uint8_t Sd2Card::SD_writeBlock(uint32_t blockNumber, const uint8_t* src) {
#if SD_PROTECT_BLOCK_ZERO
// don't allow write to first block
if (blockNumber == 0) {
goto fail; // SD_CARD_ERROR_WRITE_BLOCK_ZERO
}
#endif // SD_PROTECT_BLOCK_ZERO
// use address if not SDHC card
if (type_ != SD_CARD_TYPE_SDHC) blockNumber <<= 9;
chipSelectLow();
if (cardCommand(CMD24, blockNumber)) {
goto fail; // SD_CARD_ERROR_CMD24
}
if (!writeData(DATA_START_BLOCK, src)) goto fail;
// wait for flash programming to complete
if (!waitNotBusy(SD_WRITE_TIMEOUT)) {
goto fail; // SD_CARD_ERROR_WRITE_TIMEOUT
}
// response is r2 so get and check two bytes for nonzero
if (cardCommand(CMD13, 0) || spiRec()) {
goto fail; // SD_CARD_ERROR_WRITE_PROGRAMMING
}
chipSelectHigh();
return true;
fail:
chipSelectHigh();
return false;
}
//------------------------------------------------------------------------------
// send one block of data for write block or write multiple blocks
uint8_t Sd2Card::writeData(uint8_t token, const uint8_t* src) {
#if defined(OPTIMIZE_HARDWARE_SPI) && !defined(USE_SPI_LIB)
// send data - optimized loop
SPDR = token;
// send two byte per iteration
for (uint16_t i = 0; i < 512; i += 2) {
while (!(SPSR & (1 << SPIF)));
SPDR = src[i];
while (!(SPSR & (1 << SPIF)));
SPDR = src[i+1];
}
// wait for last data byte
while (!(SPSR & (1 << SPIF)));
#else // OPTIMIZE_HARDWARE_SPI
spiSend(token);
for (uint16_t i = 0; i < 512; i++) {
spiSend(src[i]);
}
#endif // OPTIMIZE_HARDWARE_SPI
spiSend(0xff); // dummy crc
spiSend(0xff); // dummy crc
status_ = spiRec();
if ((status_ & DATA_RES_MASK) != DATA_RES_ACCEPTED) {
return false; // SD_CARD_ERROR_WRITE
}
return true;
}

+ 0
- 144
utility/Sd2Card.h View File

@@ -1,144 +0,0 @@
/* Arduino Sd2Card Library
* Copyright (C) 2009 by William Greiman
*
* This file is part of the Arduino Sd2Card Library
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Arduino Sd2Card Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifndef Sd2Card_h
#define Sd2Card_h
/**
* \file
* Sd2Card class
*/
#include "Sd2PinMap.h"
#include "SdInfo.h"
/** Set SCK to max rate of F_CPU/2. See Sd2Card::setSckRate(). */
uint8_t const SPI_FULL_SPEED = 0;
/** Set SCK rate to F_CPU/4. See Sd2Card::setSckRate(). */
uint8_t const SPI_HALF_SPEED = 1;
/** Set SCK rate to F_CPU/8. Sd2Card::setSckRate(). */
uint8_t const SPI_QUARTER_SPEED = 2;
//------------------------------------------------------------------------------
// SPI pin definitions
//
// hardware pin defs
/**
* SD Chip Select pin
*
* Warning if this pin is redefined the hardware SS will pin will be enabled
* as an output by init(). An avr processor will not function as an SPI
* master unless SS is set to output mode.
*/
/** The default chip select pin for the SD card is SS. */
uint8_t const SD_CHIP_SELECT_PIN = SS_PIN;
// The following three pins must not be redefined for hardware SPI.
/** SPI Master Out Slave In pin */
uint8_t const SPI_MOSI_PIN = MOSI_PIN;
/** SPI Master In Slave Out pin */
uint8_t const SPI_MISO_PIN = MISO_PIN;
/** SPI Clock pin */
uint8_t const SPI_SCK_PIN = SCK_PIN;
/** optimize loops for hardware SPI */
#define OPTIMIZE_HARDWARE_SPI
//------------------------------------------------------------------------------
/** Protect block zero from write if nonzero */
#define SD_PROTECT_BLOCK_ZERO 1
/** init timeout ms */
const unsigned int SD_INIT_TIMEOUT = 2000;
/** erase timeout ms */
const unsigned int SD_ERASE_TIMEOUT = 10000;
/** read timeout ms */
const unsigned int SD_READ_TIMEOUT = 300;
/** write time out ms */
const unsigned int SD_WRITE_TIMEOUT = 600;
//------------------------------------------------------------------------------
// card types
/** Standard capacity V1 SD card */
uint8_t const SD_CARD_TYPE_SD1 = 1;
/** Standard capacity V2 SD card */
uint8_t const SD_CARD_TYPE_SD2 = 2;
/** High Capacity SD card */
uint8_t const SD_CARD_TYPE_SDHC = 3;
//------------------------------------------------------------------------------
#if defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(__IMXRT1052__) || defined(__IMXRT1062__)
#include "NXP_SDHC.h"
#define BUILTIN_SDCARD 254
#endif
//------------------------------------------------------------------------------
/**
* \class Sd2Card
* \brief Raw access to SD and SDHC flash memory cards.
*/
class Sd2Card {
public:
/** Construct an instance of Sd2Card. */
Sd2Card(void) : type_(0) {}
/* Initialize an SD flash memory card with the selected SPI clock rate
* and the SD chip select pin. */
uint8_t init(uint8_t sckRateID, uint8_t chipSelectPin) {
#if defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(__IMXRT1052__) || defined(__IMXRT1062__)
if (chipSelectPin == BUILTIN_SDCARD) {
chipSelectPin_ = BUILTIN_SDCARD;
uint8_t ret = SDHC_CardInit();
type_ = SDHC_CardGetType();
return (ret == 0) ? true : false;
}
#endif
return SD_init(sckRateID, chipSelectPin);
}
/* return the type of SD card detected during init() */
uint8_t type(void) const {return type_;}
/** Returns the current value, true or false, for partial block read. */
uint8_t readBlock(uint32_t block, uint8_t* dst) {
#if defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(__IMXRT1052__) || defined(__IMXRT1062__)
if (chipSelectPin_ == BUILTIN_SDCARD) {
return (SDHC_CardReadBlock(dst, block) == 0) ? true : false;
}
#endif
return SD_readBlock(block, dst);
}
/** Return the card type: SD V1, SD V2 or SDHC */
uint8_t writeBlock(uint32_t block, const uint8_t* src) {
#if defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(__IMXRT1052__) || defined(__IMXRT1062__)
if (chipSelectPin_ == BUILTIN_SDCARD) {
return (SDHC_CardWriteBlock(src, block) == 0) ? true : false;
}
#endif
return SD_writeBlock(block, src);
}
private:
uint8_t chipSelectPin_;
uint8_t status_;
uint8_t type_;
// private functions
uint8_t SD_init(uint8_t sckRateID, uint8_t chipSelectPin);
uint8_t SD_readBlock(uint32_t block, uint8_t* dst);
uint8_t SD_writeBlock(uint32_t blockNumber, const uint8_t* src);
uint8_t cardAcmd(uint8_t cmd, uint32_t arg) {
cardCommand(CMD55, 0);
return cardCommand(cmd, arg);
}
uint8_t cardCommand(uint8_t cmd, uint32_t arg);
uint8_t sendWriteCommand(uint32_t blockNumber, uint32_t eraseCount);
void chipSelectHigh(void);
void chipSelectLow(void);
uint8_t waitNotBusy(unsigned int timeoutMillis);
uint8_t writeData(uint8_t token, const uint8_t* src);
uint8_t waitStartBlock(void);
uint8_t setSckRate(uint8_t sckRateID);
};
#endif // Sd2Card_h

+ 0
- 413
utility/Sd2PinMap.h View File

@@ -1,413 +0,0 @@
/* Arduino SdFat Library
* Copyright (C) 2010 by William Greiman
*
* This file is part of the Arduino SdFat Library
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Arduino SdFat Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifndef Sd2PinMap_h
#define Sd2PinMap_h
#if defined(__arm__) || defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(__IMXRT1052__) || defined(__IMXRT1062__)
#include <Arduino.h>
uint8_t const SS_PIN = SS;
uint8_t const MOSI_PIN = MOSI;
uint8_t const MISO_PIN = MISO;
uint8_t const SCK_PIN = SCK;
#elif defined(__AVR__)
// Warning this file was generated by a program.
#include <avr/io.h>
//------------------------------------------------------------------------------
/** struct for mapping digital pins */
struct pin_map_t {
volatile uint8_t* ddr;
volatile uint8_t* pin;
volatile uint8_t* port;
uint8_t bit;
};
//------------------------------------------------------------------------------
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
// Mega
// Two Wire (aka I2C) ports
uint8_t const SDA_PIN = 20;
uint8_t const SCL_PIN = 21;
// SPI port
uint8_t const SS_PIN = 53;
uint8_t const MOSI_PIN = 51;
uint8_t const MISO_PIN = 50;
uint8_t const SCK_PIN = 52;
static const pin_map_t digitalPinMap[] = {
{&DDRE, &PINE, &PORTE, 0}, // E0 0
{&DDRE, &PINE, &PORTE, 1}, // E1 1
{&DDRE, &PINE, &PORTE, 4}, // E4 2
{&DDRE, &PINE, &PORTE, 5}, // E5 3
{&DDRG, &PING, &PORTG, 5}, // G5 4
{&DDRE, &PINE, &PORTE, 3}, // E3 5
{&DDRH, &PINH, &PORTH, 3}, // H3 6
{&DDRH, &PINH, &PORTH, 4}, // H4 7
{&DDRH, &PINH, &PORTH, 5}, // H5 8
{&DDRH, &PINH, &PORTH, 6}, // H6 9
{&DDRB, &PINB, &PORTB, 4}, // B4 10
{&DDRB, &PINB, &PORTB, 5}, // B5 11
{&DDRB, &PINB, &PORTB, 6}, // B6 12
{&DDRB, &PINB, &PORTB, 7}, // B7 13
{&DDRJ, &PINJ, &PORTJ, 1}, // J1 14
{&DDRJ, &PINJ, &PORTJ, 0}, // J0 15
{&DDRH, &PINH, &PORTH, 1}, // H1 16
{&DDRH, &PINH, &PORTH, 0}, // H0 17
{&DDRD, &PIND, &PORTD, 3}, // D3 18
{&DDRD, &PIND, &PORTD, 2}, // D2 19
{&DDRD, &PIND, &PORTD, 1}, // D1 20
{&DDRD, &PIND, &PORTD, 0}, // D0 21
{&DDRA, &PINA, &PORTA, 0}, // A0 22
{&DDRA, &PINA, &PORTA, 1}, // A1 23
{&DDRA, &PINA, &PORTA, 2}, // A2 24
{&DDRA, &PINA, &PORTA, 3}, // A3 25
{&DDRA, &PINA, &PORTA, 4}, // A4 26
{&DDRA, &PINA, &PORTA, 5}, // A5 27
{&DDRA, &PINA, &PORTA, 6}, // A6 28
{&DDRA, &PINA, &PORTA, 7}, // A7 29
{&DDRC, &PINC, &PORTC, 7}, // C7 30
{&DDRC, &PINC, &PORTC, 6}, // C6 31
{&DDRC, &PINC, &PORTC, 5}, // C5 32
{&DDRC, &PINC, &PORTC, 4}, // C4 33
{&DDRC, &PINC, &PORTC, 3}, // C3 34
{&DDRC, &PINC, &PORTC, 2}, // C2 35
{&DDRC, &PINC, &PORTC, 1}, // C1 36
{&DDRC, &PINC, &PORTC, 0}, // C0 37
{&DDRD, &PIND, &PORTD, 7}, // D7 38
{&DDRG, &PING, &PORTG, 2}, // G2 39
{&DDRG, &PING, &PORTG, 1}, // G1 40
{&DDRG, &PING, &PORTG, 0}, // G0 41
{&DDRL, &PINL, &PORTL, 7}, // L7 42
{&DDRL, &PINL, &PORTL, 6}, // L6 43
{&DDRL, &PINL, &PORTL, 5}, // L5 44
{&DDRL, &PINL, &PORTL, 4}, // L4 45
{&DDRL, &PINL, &PORTL, 3}, // L3 46
{&DDRL, &PINL, &PORTL, 2}, // L2 47
{&DDRL, &PINL, &PORTL, 1}, // L1 48
{&DDRL, &PINL, &PORTL, 0}, // L0 49
{&DDRB, &PINB, &PORTB, 3}, // B3 50
{&DDRB, &PINB, &PORTB, 2}, // B2 51
{&DDRB, &PINB, &PORTB, 1}, // B1 52
{&DDRB, &PINB, &PORTB, 0}, // B0 53
{&DDRF, &PINF, &PORTF, 0}, // F0 54
{&DDRF, &PINF, &PORTF, 1}, // F1 55
{&DDRF, &PINF, &PORTF, 2}, // F2 56
{&DDRF, &PINF, &PORTF, 3}, // F3 57
{&DDRF, &PINF, &PORTF, 4}, // F4 58
{&DDRF, &PINF, &PORTF, 5}, // F5 59
{&DDRF, &PINF, &PORTF, 6}, // F6 60
{&DDRF, &PINF, &PORTF, 7}, // F7 61
{&DDRK, &PINK, &PORTK, 0}, // K0 62
{&DDRK, &PINK, &PORTK, 1}, // K1 63
{&DDRK, &PINK, &PORTK, 2}, // K2 64
{&DDRK, &PINK, &PORTK, 3}, // K3 65
{&DDRK, &PINK, &PORTK, 4}, // K4 66
{&DDRK, &PINK, &PORTK, 5}, // K5 67
{&DDRK, &PINK, &PORTK, 6}, // K6 68
{&DDRK, &PINK, &PORTK, 7} // K7 69
};
//------------------------------------------------------------------------------
#elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__)
// Sanguino
// Two Wire (aka I2C) ports
uint8_t const SDA_PIN = 17;
uint8_t const SCL_PIN = 18;
// SPI port
uint8_t const SS_PIN = 4;
uint8_t const MOSI_PIN = 5;
uint8_t const MISO_PIN = 6;
uint8_t const SCK_PIN = 7;
static const pin_map_t digitalPinMap[] = {
{&DDRB, &PINB, &PORTB, 0}, // B0 0
{&DDRB, &PINB, &PORTB, 1}, // B1 1
{&DDRB, &PINB, &PORTB, 2}, // B2 2
{&DDRB, &PINB, &PORTB, 3}, // B3 3
{&DDRB, &PINB, &PORTB, 4}, // B4 4
{&DDRB, &PINB, &PORTB, 5}, // B5 5
{&DDRB, &PINB, &PORTB, 6}, // B6 6
{&DDRB, &PINB, &PORTB, 7}, // B7 7
{&DDRD, &PIND, &PORTD, 0}, // D0 8
{&DDRD, &PIND, &PORTD, 1}, // D1 9
{&DDRD, &PIND, &PORTD, 2}, // D2 10
{&DDRD, &PIND, &PORTD, 3}, // D3 11
{&DDRD, &PIND, &PORTD, 4}, // D4 12
{&DDRD, &PIND, &PORTD, 5}, // D5 13
{&DDRD, &PIND, &PORTD, 6}, // D6 14
{&DDRD, &PIND, &PORTD, 7}, // D7 15
{&DDRC, &PINC, &PORTC, 0}, // C0 16
{&DDRC, &PINC, &PORTC, 1}, // C1 17
{&DDRC, &PINC, &PORTC, 2}, // C2 18
{&DDRC, &PINC, &PORTC, 3}, // C3 19
{&DDRC, &PINC, &PORTC, 4}, // C4 20
{&DDRC, &PINC, &PORTC, 5}, // C5 21
{&DDRC, &PINC, &PORTC, 6}, // C6 22
{&DDRC, &PINC, &PORTC, 7}, // C7 23
{&DDRA, &PINA, &PORTA, 7}, // A7 24
{&DDRA, &PINA, &PORTA, 6}, // A6 25
{&DDRA, &PINA, &PORTA, 5}, // A5 26
{&DDRA, &PINA, &PORTA, 4}, // A4 27
{&DDRA, &PINA, &PORTA, 3}, // A3 28
{&DDRA, &PINA, &PORTA, 2}, // A2 29
{&DDRA, &PINA, &PORTA, 1}, // A1 30
{&DDRA, &PINA, &PORTA, 0} // A0 31
};
//------------------------------------------------------------------------------
#elif defined(__AVR_ATmega32U4__)
#if defined(CORE_TEENSY)
// Teensy 2.0
// Two Wire (aka I2C) ports
uint8_t const SDA_PIN = 6;
uint8_t const SCL_PIN = 5;
// SPI port
uint8_t const SS_PIN = 0;
uint8_t const MOSI_PIN = 2;
uint8_t const MISO_PIN = 3;
uint8_t const SCK_PIN = 1;
static const pin_map_t digitalPinMap[] = {
{&DDRB, &PINB, &PORTB, 0}, // B0 0
{&DDRB, &PINB, &PORTB, 1}, // B1 1
{&DDRB, &PINB, &PORTB, 2}, // B2 2
{&DDRB, &PINB, &PORTB, 3}, // B3 3
{&DDRB, &PINB, &PORTB, 7}, // B7 4
{&DDRD, &PIND, &PORTD, 0}, // D0 5
{&DDRD, &PIND, &PORTD, 1}, // D1 6
{&DDRD, &PIND, &PORTD, 2}, // D2 7
{&DDRD, &PIND, &PORTD, 3}, // D3 8
{&DDRC, &PINC, &PORTC, 6}, // C6 9
{&DDRC, &PINC, &PORTC, 7}, // C7 10
{&DDRD, &PIND, &PORTD, 6}, // D6 11
{&DDRD, &PIND, &PORTD, 7}, // D7 12
{&DDRB, &PINB, &PORTB, 4}, // B4 13
{&DDRB, &PINB, &PORTB, 5}, // B5 14
{&DDRB, &PINB, &PORTB, 6}, // B6 15
{&DDRF, &PINF, &PORTF, 7}, // F7 16
{&DDRF, &PINF, &PORTF, 6}, // F6 17
{&DDRF, &PINF, &PORTF, 5}, // F5 18
{&DDRF, &PINF, &PORTF, 4}, // F4 19
{&DDRF, &PINF, &PORTF, 1}, // F1 20
{&DDRF, &PINF, &PORTF, 0}, // F0 21
{&DDRD, &PIND, &PORTD, 4}, // D4 22
{&DDRD, &PIND, &PORTD, 5}, // D5 23
{&DDRE, &PINE, &PORTE, 6} // E6 24
};
#else
// Leonardo
// Two Wire (aka I2C) ports
uint8_t const SDA_PIN = 2;
uint8_t const SCL_PIN = 3;
// SPI port
uint8_t const SS_PIN = 17;
uint8_t const MOSI_PIN = 16;
uint8_t const MISO_PIN = 14;
uint8_t const SCK_PIN = 15;
static const pin_map_t digitalPinMap[] = {
{&DDRD, &PIND, &PORTD, 2}, // D2 0
{&DDRD, &PIND, &PORTD, 3}, // D3 1
{&DDRD, &PIND, &PORTD, 1}, // D1 2
{&DDRD, &PIND, &PORTD, 0}, // D0 3
{&DDRD, &PIND, &PORTD, 4}, // D4 4
{&DDRC, &PINC, &PORTC, 6}, // C6 5
{&DDRD, &PIND, &PORTD, 7}, // D7 6
{&DDRE, &PINE, &PORTE, 6}, // E6 7
{&DDRB, &PINB, &PORTB, 4}, // B4 8
{&DDRB, &PINB, &PORTB, 5}, // B5 9
{&DDRB, &PINB, &PORTB, 6}, // B6 10
{&DDRB, &PINB, &PORTB, 7}, // B7 11
{&DDRD, &PIND, &PORTD, 6}, // D6 12
{&DDRC, &PINC, &PORTC, 7}, // C7 13
{&DDRB, &PINB, &PORTB, 3}, // B3 14
{&DDRB, &PINB, &PORTB, 1}, // B1 15
{&DDRB, &PINB, &PORTB, 2}, // B2 16
{&DDRB, &PINB, &PORTB, 0}, // B0 17
{&DDRF, &PINF, &PORTF, 7}, // F7 18
{&DDRF, &PINF, &PORTF, 6}, // F6 19
{&DDRF, &PINF, &PORTF, 5}, // F5 20
{&DDRF, &PINF, &PORTF, 4}, // F4 21
{&DDRF, &PINF, &PORTF, 1}, // F1 22
{&DDRF, &PINF, &PORTF, 0}, // F0 23
};
#endif
//------------------------------------------------------------------------------
#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
// Teensy++ 1.0 & 2.0
// Two Wire (aka I2C) ports
uint8_t const SDA_PIN = 1;
uint8_t const SCL_PIN = 0;
// SPI port
uint8_t const SS_PIN = 20;
uint8_t const MOSI_PIN = 22;
uint8_t const MISO_PIN = 23;
uint8_t const SCK_PIN = 21;
static const pin_map_t digitalPinMap[] = {
{&DDRD, &PIND, &PORTD, 0}, // D0 0
{&DDRD, &PIND, &PORTD, 1}, // D1 1
{&DDRD, &PIND, &PORTD, 2}, // D2 2
{&DDRD, &PIND, &PORTD, 3}, // D3 3
{&DDRD, &PIND, &PORTD, 4}, // D4 4
{&DDRD, &PIND, &PORTD, 5}, // D5 5
{&DDRD, &PIND, &PORTD, 6}, // D6 6
{&DDRD, &PIND, &PORTD, 7}, // D7 7
{&DDRE, &PINE, &PORTE, 0}, // E0 8
{&DDRE, &PINE, &PORTE, 1}, // E1 9
{&DDRC, &PINC, &PORTC, 0}, // C0 10
{&DDRC, &PINC, &PORTC, 1}, // C1 11
{&DDRC, &PINC, &PORTC, 2}, // C2 12
{&DDRC, &PINC, &PORTC, 3}, // C3 13
{&DDRC, &PINC, &PORTC, 4}, // C4 14
{&DDRC, &PINC, &PORTC, 5}, // C5 15
{&DDRC, &PINC, &PORTC, 6}, // C6 16
{&DDRC, &PINC, &PORTC, 7}, // C7 17
{&DDRE, &PINE, &PORTE, 6}, // E6 18
{&DDRE, &PINE, &PORTE, 7}, // E7 19
{&DDRB, &PINB, &PORTB, 0}, // B0 20
{&DDRB, &PINB, &PORTB, 1}, // B1 21
{&DDRB, &PINB, &PORTB, 2}, // B2 22
{&DDRB, &PINB, &PORTB, 3}, // B3 23
{&DDRB, &PINB, &PORTB, 4}, // B4 24
{&DDRB, &PINB, &PORTB, 5}, // B5 25
{&DDRB, &PINB, &PORTB, 6}, // B6 26
{&DDRB, &PINB, &PORTB, 7}, // B7 27
{&DDRA, &PINA, &PORTA, 0}, // A0 28
{&DDRA, &PINA, &PORTA, 1}, // A1 29
{&DDRA, &PINA, &PORTA, 2}, // A2 30
{&DDRA, &PINA, &PORTA, 3}, // A3 31
{&DDRA, &PINA, &PORTA, 4}, // A4 32
{&DDRA, &PINA, &PORTA, 5}, // A5 33
{&DDRA, &PINA, &PORTA, 6}, // A6 34
{&DDRA, &PINA, &PORTA, 7}, // A7 35
{&DDRE, &PINE, &PORTE, 4}, // E4 36
{&DDRE, &PINE, &PORTE, 5}, // E5 37
{&DDRF, &PINF, &PORTF, 0}, // F0 38
{&DDRF, &PINF, &PORTF, 1}, // F1 39
{&DDRF, &PINF, &PORTF, 2}, // F2 40
{&DDRF, &PINF, &PORTF, 3}, // F3 41
{&DDRF, &PINF, &PORTF, 4}, // F4 42
{&DDRF, &PINF, &PORTF, 5}, // F5 43
{&DDRF, &PINF, &PORTF, 6}, // F6 44
{&DDRF, &PINF, &PORTF, 7} // F7 45
};
//------------------------------------------------------------------------------
#else // defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
// 168 and 328 Arduinos
// Two Wire (aka I2C) ports
uint8_t const SDA_PIN = 18;
uint8_t const SCL_PIN = 19;
// SPI port
uint8_t const SS_PIN = 10;
uint8_t const MOSI_PIN = 11;
uint8_t const MISO_PIN = 12;
uint8_t const SCK_PIN = 13;
static const pin_map_t digitalPinMap[] = {
{&DDRD, &PIND, &PORTD, 0}, // D0 0
{&DDRD, &PIND, &PORTD, 1}, // D1 1
{&DDRD, &PIND, &PORTD, 2}, // D2 2
{&DDRD, &PIND, &PORTD, 3}, // D3 3
{&DDRD, &PIND, &PORTD, 4}, // D4 4
{&DDRD, &PIND, &PORTD, 5}, // D5 5
{&DDRD, &PIND, &PORTD, 6}, // D6 6
{&DDRD, &PIND, &PORTD, 7}, // D7 7
{&DDRB, &PINB, &PORTB, 0}, // B0 8
{&DDRB, &PINB, &PORTB, 1}, // B1 9
{&DDRB, &PINB, &PORTB, 2}, // B2 10
{&DDRB, &PINB, &PORTB, 3}, // B3 11
{&DDRB, &PINB, &PORTB, 4}, // B4 12
{&DDRB, &PINB, &PORTB, 5}, // B5 13
{&DDRC, &PINC, &PORTC, 0}, // C0 14
{&DDRC, &PINC, &PORTC, 1}, // C1 15
{&DDRC, &PINC, &PORTC, 2}, // C2 16
{&DDRC, &PINC, &PORTC, 3}, // C3 17
{&DDRC, &PINC, &PORTC, 4}, // C4 18
{&DDRC, &PINC, &PORTC, 5} // C5 19
};
#endif // defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
//------------------------------------------------------------------------------
static const uint8_t digitalPinCount = sizeof(digitalPinMap)/sizeof(pin_map_t);
uint8_t badPinNumber(void)
__attribute__((error("Pin number is too large or not a constant")));
static inline __attribute__((always_inline))
uint8_t getPinMode(uint8_t pin) {
if (__builtin_constant_p(pin) && pin < digitalPinCount) {
return (*digitalPinMap[pin].ddr >> digitalPinMap[pin].bit) & 1;
} else {
return badPinNumber();
}
}
static inline __attribute__((always_inline))
void setPinMode(uint8_t pin, uint8_t mode) {
if (__builtin_constant_p(pin) && pin < digitalPinCount) {
if (mode) {
*digitalPinMap[pin].ddr |= 1 << digitalPinMap[pin].bit;
} else {
*digitalPinMap[pin].ddr &= ~(1 << digitalPinMap[pin].bit);
}
} else {
badPinNumber();
}
}
static inline __attribute__((always_inline))
uint8_t fastDigitalRead(uint8_t pin) {
if (__builtin_constant_p(pin) && pin < digitalPinCount) {
return (*digitalPinMap[pin].pin >> digitalPinMap[pin].bit) & 1;
} else {
return badPinNumber();
}
}
static inline __attribute__((always_inline))
void fastDigitalWrite(uint8_t pin, uint8_t value) {
if (__builtin_constant_p(pin) && pin < digitalPinCount) {
if (value) {
*digitalPinMap[pin].port |= 1 << digitalPinMap[pin].bit;
} else {
*digitalPinMap[pin].port &= ~(1 << digitalPinMap[pin].bit);
}
} else {
badPinNumber();
}
}
#else
#error Architecture or board not supported.
#endif
#endif // Sd2PinMap_h

+ 0
- 544
utility/SdFat.h View File

@@ -1,544 +0,0 @@
/* Arduino SdFat Library
* Copyright (C) 2009 by William Greiman
*
* This file is part of the Arduino SdFat Library
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Arduino SdFat Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifndef SdFat_h
#define SdFat_h
/**
* \file
* SdFile and SdVolume classes
*/
#include <avr/pgmspace.h>
#include "Sd2Card.h"
#include "FatStructs.h"
#include "Print.h"
//------------------------------------------------------------------------------
/**
* Allow use of deprecated functions if non-zero
*/
#define ALLOW_DEPRECATED_FUNCTIONS 1
//------------------------------------------------------------------------------
// forward declaration since SdVolume is used in SdFile
class SdVolume;
//==============================================================================
// SdFile class
// flags for ls()
/** ls() flag to print modify date */
uint8_t const LS_DATE = 1;
/** ls() flag to print file size */
uint8_t const LS_SIZE = 2;
/** ls() flag for recursive list of subdirectories */
uint8_t const LS_R = 4;
// use the gnu style oflag in open()
/** open() oflag for reading */
uint8_t const O_READ = 0X01;
/** open() oflag - same as O_READ */
uint8_t const O_RDONLY = O_READ;
/** open() oflag for write */
uint8_t const O_WRITE = 0X02;
/** open() oflag - same as O_WRITE */
uint8_t const O_WRONLY = O_WRITE;
/** open() oflag for reading and writing */
uint8_t const O_RDWR = (O_READ | O_WRITE);
/** open() oflag mask for access modes */
uint8_t const O_ACCMODE = (O_READ | O_WRITE);
/** The file offset shall be set to the end of the file prior to each write. */
uint8_t const O_APPEND = 0X04;
/** synchronous writes - call sync() after each write */
uint8_t const O_SYNC = 0X08;
/** create the file if nonexistent */
uint8_t const O_CREAT = 0X10;
/** If O_CREAT and O_EXCL are set, open() shall fail if the file exists */
uint8_t const O_EXCL = 0X20;
/** truncate the file to zero length */
uint8_t const O_TRUNC = 0X40;
// flags for timestamp
/** set the file's last access date */
uint8_t const T_ACCESS = 1;
/** set the file's creation date and time */
uint8_t const T_CREATE = 2;
/** Set the file's write date and time */
uint8_t const T_WRITE = 4;
// values for type_
/** This SdFile has not been opened. */
uint8_t const FAT_FILE_TYPE_CLOSED = 0;
/** SdFile for a file */
uint8_t const FAT_FILE_TYPE_NORMAL = 1;
/** SdFile for a FAT16 root directory */
uint8_t const FAT_FILE_TYPE_ROOT16 = 2;
/** SdFile for a FAT32 root directory */
uint8_t const FAT_FILE_TYPE_ROOT32 = 3;
/** SdFile for a subdirectory */
uint8_t const FAT_FILE_TYPE_SUBDIR = 4;
/** Test value for directory type */
uint8_t const FAT_FILE_TYPE_MIN_DIR = FAT_FILE_TYPE_ROOT16;
/** date field for FAT directory entry */
static inline uint16_t FAT_DATE(uint16_t year, uint8_t month, uint8_t day) {
return (year - 1980) << 9 | month << 5 | day;
}
/** year part of FAT directory date field */
static inline uint16_t FAT_YEAR(uint16_t fatDate) {
return 1980 + (fatDate >> 9);
}
/** month part of FAT directory date field */
static inline uint8_t FAT_MONTH(uint16_t fatDate) {
return (fatDate >> 5) & 0XF;
}
/** day part of FAT directory date field */
static inline uint8_t FAT_DAY(uint16_t fatDate) {
return fatDate & 0X1F;
}
/** time field for FAT directory entry */
static inline uint16_t FAT_TIME(uint8_t hour, uint8_t minute, uint8_t second) {
return hour << 11 | minute << 5 | second >> 1;
}
/** hour part of FAT directory time field */
static inline uint8_t FAT_HOUR(uint16_t fatTime) {
return fatTime >> 11;
}
/** minute part of FAT directory time field */
static inline uint8_t FAT_MINUTE(uint16_t fatTime) {
return(fatTime >> 5) & 0X3F;
}
/** second part of FAT directory time field */
static inline uint8_t FAT_SECOND(uint16_t fatTime) {
return 2*(fatTime & 0X1F);
}
/** Default date for file timestamps is 1 Jan 2000 */
uint16_t const FAT_DEFAULT_DATE = ((2000 - 1980) << 9) | (1 << 5) | 1;
/** Default time for file timestamp is 1 am */
uint16_t const FAT_DEFAULT_TIME = (1 << 11);
//------------------------------------------------------------------------------
/**
* \class SdFile
* \brief Access FAT16 and FAT32 files on SD and SDHC cards.
*/
class SdFile : public Print {
public:
/** Create an instance of SdFile. */
SdFile(void) : type_(FAT_FILE_TYPE_CLOSED) {}
/**
* writeError is set to true if an error occurs during a write().
* Set writeError to false before calling print() and/or write() and check
* for true after calls to print() and/or write().
*/
//bool writeError;
/**
* Cancel unbuffered reads for this file.
* See setUnbufferedRead()
*/
void clearUnbufferedRead(void) {
flags_ &= ~F_FILE_UNBUFFERED_READ;
}
uint8_t close(void);
uint8_t contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock);
uint8_t createContiguous(SdFile* dirFile,
const char* fileName, uint32_t size);
/** \return The current cluster number for a file or directory. */
uint32_t curCluster(void) const {return curCluster_;}
/** \return The current position for a file or directory. */
uint32_t curPosition(void) const {return curPosition_;}
/**
* Set the date/time callback function
*
* \param[in] dateTime The user's call back function. The callback
* function is of the form:
*
* \code
* void dateTime(uint16_t* date, uint16_t* time) {
* uint16_t year;
* uint8_t month, day, hour, minute, second;
*
* // User gets date and time from GPS or real-time clock here
*
* // return date using FAT_DATE macro to format fields
* *date = FAT_DATE(year, month, day);
*
* // return time using FAT_TIME macro to format fields
* *time = FAT_TIME(hour, minute, second);
* }
* \endcode
*
* Sets the function that is called when a file is created or when
* a file's directory entry is modified by sync(). All timestamps,
* access, creation, and modify, are set when a file is created.
* sync() maintains the last access date and last modify date/time.
*
* See the timestamp() function.
*/
static void dateTimeCallback(
void (*dateTime)(uint16_t* date, uint16_t* time)) {
dateTime_ = dateTime;
}
/**
* Cancel the date/time callback function.
*/
static void dateTimeCallbackCancel(void) {
// use explicit zero since NULL is not defined for Sanguino
dateTime_ = 0;
}
/** \return Address of the block that contains this file's directory. */
uint32_t dirBlock(void) const {return dirBlock_;}
uint8_t dirEntry(dir_t* dir);
/** \return Index of this file's directory in the block dirBlock. */
uint8_t dirIndex(void) const {return dirIndex_;}
static void dirName(const dir_t& dir, char* name);
/** \return The total number of bytes in a file or directory. */
uint32_t fileSize(void) const {return fileSize_;}
/** \return The first cluster number for a file or directory. */
uint32_t firstCluster(void) const {return firstCluster_;}
/** \return True if this is a SdFile for a directory else false. */
uint8_t isDir(void) const {return type_ >= FAT_FILE_TYPE_MIN_DIR;}
/** \return True if this is a SdFile for a file else false. */
uint8_t isFile(void) const {return type_ == FAT_FILE_TYPE_NORMAL;}
/** \return True if this is a SdFile for an open file/directory else false. */
uint8_t isOpen(void) const {return type_ != FAT_FILE_TYPE_CLOSED;}
/** \return True if this is a SdFile for a subdirectory else false. */
uint8_t isSubDir(void) const {return type_ == FAT_FILE_TYPE_SUBDIR;}
/** \return True if this is a SdFile for the root directory. */
uint8_t isRoot(void) const {
return type_ == FAT_FILE_TYPE_ROOT16 || type_ == FAT_FILE_TYPE_ROOT32;
}
void ls(uint8_t flags = 0, uint8_t indent = 0);
uint8_t makeDir(SdFile* dir, const char* dirName);
uint8_t open(SdFile* dirFile, uint16_t index, uint8_t oflag);
uint8_t open(SdFile* dirFile, const char* fileName, uint8_t oflag);
uint8_t openRoot(SdVolume* vol);
static void printDirName(const dir_t& dir, uint8_t width);
static void printFatDate(uint16_t fatDate);
static void printFatTime(uint16_t fatTime);
static void printTwoDigits(uint8_t v);
/**
* Read the next byte from a file.
*
* \return For success read returns the next byte in the file as an int.
* If an error occurs or end of file is reached -1 is returned.
*/
int16_t read(void) {
uint8_t b;
return read(&b, 1) == 1 ? b : -1;
}
int32_t read(void* buf, size_t nbyte);
int8_t readDir(dir_t* dir);
static uint8_t remove(SdFile* dirFile, const char* fileName);
uint8_t remove(void);
/** Set the file's current position to zero. */
void rewind(void) {
curPosition_ = curCluster_ = 0;
}
uint8_t rmDir(void);
uint8_t rmRfStar(void);
/** Set the files position to current position + \a pos. See seekSet(). */
uint8_t seekCur(uint32_t pos) {
return seekSet(curPosition_ + pos);
}
/**
* Set the files current position to end of file. Useful to position
* a file for append. See seekSet().
*/
uint8_t seekEnd(void) {return seekSet(fileSize_);}
uint8_t seekSet(uint32_t pos);
/**
* Use unbuffered reads to access this file. Used with Wave
* Shield ISR. Used with Sd2Card::partialBlockRead() in WaveRP.
*
* Not recommended for normal applications.
*/
void setUnbufferedRead(void) {
if (isFile()) flags_ |= F_FILE_UNBUFFERED_READ;
}
uint8_t timestamp(uint8_t flag, uint16_t year, uint8_t month, uint8_t day,
uint8_t hour, uint8_t minute, uint8_t second);
uint8_t sync(void);
/** Type of this SdFile. You should use isFile() or isDir() instead of type()
* if possible.
*
* \return The file or directory type.
*/
uint8_t type(void) const {return type_;}
uint8_t truncate(uint32_t size);
/** \return Unbuffered read flag. */
uint8_t unbufferedRead(void) const {
return flags_ & F_FILE_UNBUFFERED_READ;
}
/** \return SdVolume that contains this file. */
SdVolume* volume(void) const {return vol_;}
size_t write(uint8_t b);
size_t write(const void* buf, size_t nbyte);
size_t write(const char* str);
void write_P(PGM_P str);
void writeln_P(PGM_P str);
//------------------------------------------------------------------------------
#if ALLOW_DEPRECATED_FUNCTIONS
// Deprecated functions - suppress cpplint warnings with NOLINT comment
/** \deprecated Use:
* uint8_t SdFile::contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock);
*/
uint8_t contiguousRange(uint32_t& bgnBlock, uint32_t& endBlock) { // NOLINT
return contiguousRange(&bgnBlock, &endBlock);
}
/** \deprecated Use:
* uint8_t SdFile::createContiguous(SdFile* dirFile,
* const char* fileName, uint32_t size)
*/
uint8_t createContiguous(SdFile& dirFile, // NOLINT
const char* fileName, uint32_t size) {
return createContiguous(&dirFile, fileName, size);
}
/**
* \deprecated Use:
* static void SdFile::dateTimeCallback(
* void (*dateTime)(uint16_t* date, uint16_t* time));
*/
static void dateTimeCallback(
void (*dateTime)(uint16_t& date, uint16_t& time)) { // NOLINT
oldDateTime_ = dateTime;
dateTime_ = dateTime ? oldToNew : 0;
}
/** \deprecated Use: uint8_t SdFile::dirEntry(dir_t* dir); */
uint8_t dirEntry(dir_t& dir) {return dirEntry(&dir);} // NOLINT
/** \deprecated Use:
* uint8_t SdFile::makeDir(SdFile* dir, const char* dirName);
*/
uint8_t makeDir(SdFile& dir, const char* dirName) { // NOLINT
return makeDir(&dir, dirName);
}
/** \deprecated Use:
* uint8_t SdFile::open(SdFile* dirFile, const char* fileName, uint8_t oflag);
*/
uint8_t open(SdFile& dirFile, // NOLINT
const char* fileName, uint8_t oflag) {
return open(&dirFile, fileName, oflag);
}
/** \deprecated Do not use in new apps */
uint8_t open(SdFile& dirFile, const char* fileName) { // NOLINT
return open(dirFile, fileName, O_RDWR);
}
/** \deprecated Use:
* uint8_t SdFile::open(SdFile* dirFile, uint16_t index, uint8_t oflag);
*/
uint8_t open(SdFile& dirFile, uint16_t index, uint8_t oflag) { // NOLINT
return open(&dirFile, index, oflag);
}
/** \deprecated Use: uint8_t SdFile::openRoot(SdVolume* vol); */
uint8_t openRoot(SdVolume& vol) {return openRoot(&vol);} // NOLINT
/** \deprecated Use: int8_t SdFile::readDir(dir_t* dir); */
int8_t readDir(dir_t& dir) {return readDir(&dir);} // NOLINT
/** \deprecated Use:
* static uint8_t SdFile::remove(SdFile* dirFile, const char* fileName);
*/
static uint8_t remove(SdFile& dirFile, const char* fileName) { // NOLINT
return remove(&dirFile, fileName);
}
//------------------------------------------------------------------------------
// rest are private
private:
static void (*oldDateTime_)(uint16_t& date, uint16_t& time); // NOLINT
static void oldToNew(uint16_t* date, uint16_t* time) {
uint16_t d;
uint16_t t;
oldDateTime_(d, t);
*date = d;
*time = t;
}
#endif // ALLOW_DEPRECATED_FUNCTIONS
private:
// bits defined in flags_
// should be 0XF
static uint8_t const F_OFLAG = (O_ACCMODE | O_APPEND | O_SYNC);
// available bits
static uint8_t const F_UNUSED = 0X30;
// use unbuffered SD read
static uint8_t const F_FILE_UNBUFFERED_READ = 0X40;
// sync of directory entry required
static uint8_t const F_FILE_DIR_DIRTY = 0X80;
// make sure F_OFLAG is ok
#if ((F_UNUSED | F_FILE_UNBUFFERED_READ | F_FILE_DIR_DIRTY) & F_OFLAG)
#error flags_ bits conflict
#endif // flags_ bits
// private data
uint8_t flags_; // See above for definition of flags_ bits
uint8_t type_; // type of file see above for values
uint32_t curCluster_; // cluster for current file position
uint32_t curPosition_; // current file position in bytes from beginning
uint32_t dirBlock_; // SD block that contains directory entry for file
uint8_t dirIndex_; // index of entry in dirBlock 0 <= dirIndex_ <= 0XF
uint32_t fileSize_; // file size in bytes
uint32_t firstCluster_; // first cluster of file
SdVolume* vol_; // volume where file is located
// private functions
uint8_t addCluster(void);
uint8_t addDirCluster(void);
dir_t* cacheDirEntry(uint8_t action);
static void (*dateTime_)(uint16_t* date, uint16_t* time);
static uint8_t make83Name(const char* str, uint8_t* name);
uint8_t openCachedEntry(uint8_t cacheIndex, uint8_t oflags);
dir_t* readDirCache(void);
};
//==============================================================================
// SdVolume class
/**
* \brief Cache for an SD data block
*/
union cache_t {
/** Used to access cached file data blocks. */
uint8_t data[512];
/** Used to access cached FAT16 entries. */
uint16_t fat16[256];
/** Used to access cached FAT32 entries. */
uint32_t fat32[128];
/** Used to access cached directory entries. */
dir_t dir[16];
/** Used to access a cached MasterBoot Record. */
mbr_t mbr;
/** Used to access to a cached FAT boot sector. */
fbs_t fbs;
};
//------------------------------------------------------------------------------
/**
* \class SdVolume
* \brief Access FAT16 and FAT32 volumes on SD and SDHC cards.
*/
class SdVolume {
public:
/** Create an instance of SdVolume */
SdVolume(void) :allocSearchStart_(2), fatType_(0) {}
/** Clear the cache and returns a pointer to the cache. Used by the WaveRP
* recorder to do raw write to the SD card. Not for normal apps.
*/
static uint8_t* cacheClear(void) {
cacheFlush();
cacheBlockNumber_ = 0XFFFFFFFF;
return cacheBuffer_.data;
}
/**
* Initialize a FAT volume. Try partition one first then try super
* floppy format.
*
* \param[in] dev The Sd2Card where the volume is located.
*
* \return The value one, true, is returned for success and
* the value zero, false, is returned for failure. Reasons for
* failure include not finding a valid partition, not finding a valid
* FAT file system or an I/O error.
*/
uint8_t init(Sd2Card* dev) { return init(dev, 1) ? true : init(dev, 0);}
uint8_t init(Sd2Card* dev, uint8_t part);
// inline functions that return volume info
/** \return The volume's cluster size in blocks. */
uint8_t blocksPerCluster(void) const {return blocksPerCluster_;}
/** \return The number of blocks in one FAT. */
uint32_t blocksPerFat(void) const {return blocksPerFat_;}
/** \return The total number of clusters in the volume. */
uint32_t clusterCount(void) const {return clusterCount_;}
/** \return The shift count required to multiply by blocksPerCluster. */
uint8_t clusterSizeShift(void) const {return clusterSizeShift_;}
/** \return The logical block number for the start of file data. */
uint32_t dataStartBlock(void) const {return dataStartBlock_;}
/** \return The number of FAT structures on the volume. */
uint8_t fatCount(void) const {return fatCount_;}
/** \return The logical block number for the start of the first FAT. */
uint32_t fatStartBlock(void) const {return fatStartBlock_;}
/** \return The FAT type of the volume. Values are 12, 16 or 32. */
uint8_t fatType(void) const {return fatType_;}
/** \return The number of entries in the root directory for FAT16 volumes. */
uint32_t rootDirEntryCount(void) const {return rootDirEntryCount_;}
/** \return The logical block number for the start of the root directory
on FAT16 volumes or the first cluster number on FAT32 volumes. */
uint32_t rootDirStart(void) const {return rootDirStart_;}
/** return a pointer to the Sd2Card object for this volume */
static Sd2Card* sdCard(void) {return sdCard_;}
//------------------------------------------------------------------------------
#if ALLOW_DEPRECATED_FUNCTIONS
// Deprecated functions - suppress cpplint warnings with NOLINT comment
/** \deprecated Use: uint8_t SdVolume::init(Sd2Card* dev); */
uint8_t init(Sd2Card& dev) {return init(&dev);} // NOLINT
/** \deprecated Use: uint8_t SdVolume::init(Sd2Card* dev, uint8_t vol); */
uint8_t init(Sd2Card& dev, uint8_t part) { // NOLINT
return init(&dev, part);
}
#endif // ALLOW_DEPRECATED_FUNCTIONS
//------------------------------------------------------------------------------
private:
// Allow SdFile access to SdVolume private data.
friend class SdFile;
// value for action argument in cacheRawBlock to indicate read from cache
static uint8_t const CACHE_FOR_READ = 0;
// value for action argument in cacheRawBlock to indicate cache dirty
static uint8_t const CACHE_FOR_WRITE = 1;
static cache_t cacheBuffer_; // 512 byte cache for device blocks
static uint32_t cacheBlockNumber_; // Logical number of block in the cache
static Sd2Card* sdCard_; // Sd2Card object for cache
static uint8_t cacheDirty_; // cacheFlush() will write block if true
static uint32_t cacheMirrorBlock_; // block number for mirror FAT
//
uint32_t allocSearchStart_; // start cluster for alloc search
uint8_t blocksPerCluster_; // cluster size in blocks
uint32_t blocksPerFat_; // FAT size in blocks
uint32_t clusterCount_; // clusters in one FAT
uint8_t clusterSizeShift_; // shift to convert cluster count to block count
uint32_t dataStartBlock_; // first data block number
uint8_t fatCount_; // number of FATs on volume
uint32_t fatStartBlock_; // start block for first FAT
uint8_t fatType_; // volume type (12, 16, OR 32)
uint16_t rootDirEntryCount_; // number of entries in FAT16 root dir
uint32_t rootDirStart_; // root start block for FAT16, cluster for FAT32
//----------------------------------------------------------------------------
uint8_t allocContiguous(uint32_t count, uint32_t* curCluster);
uint8_t blockOfCluster(uint32_t position) const {
return (position >> 9) & (blocksPerCluster_ - 1);}
uint32_t clusterStartBlock(uint32_t cluster) const {
return dataStartBlock_ + ((cluster - 2) << clusterSizeShift_);}
uint32_t blockNumber(uint32_t cluster, uint32_t position) const {
return clusterStartBlock(cluster) + blockOfCluster(position);}
static uint8_t cacheFlush(void);
static uint8_t cacheRawBlock(uint32_t blockNumber, uint8_t action);
static void cacheSetDirty(void) {cacheDirty_ |= CACHE_FOR_WRITE;}
static uint8_t cacheZeroBlock(uint32_t blockNumber);
uint8_t chainSize(uint32_t beginCluster, uint32_t* size) const;
uint8_t fatGet(uint32_t cluster, uint32_t* value) const;
uint8_t fatPut(uint32_t cluster, uint32_t value);
uint8_t fatPutEOC(uint32_t cluster) {
return fatPut(cluster, 0x0FFFFFFF);
}
uint8_t freeChain(uint32_t cluster);
uint8_t isEOC(uint32_t cluster) const {
return cluster >= (fatType_ == 16 ? FAT16EOC_MIN : FAT32EOC_MIN);
}
uint8_t readBlock(uint32_t block, uint8_t* dst) {
return sdCard_->readBlock(block, dst);
}
uint8_t writeBlock(uint32_t block, const uint8_t* dst) {
return sdCard_->writeBlock(block, dst);
}
};
#endif // SdFat_h

+ 0
- 74
utility/SdFatUtil.h View File

@@ -1,74 +0,0 @@
/* Arduino SdFat Library
* Copyright (C) 2008 by William Greiman
*
* This file is part of the Arduino SdFat Library
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with the Arduino SdFat Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifndef SdFatUtil_h
#define SdFatUtil_h
/**
* \file
* Useful utility functions.
*/
#include <Arduino.h>
#include <avr/pgmspace.h>
/** Store and print a string in flash memory.*/
#define PgmPrint(x) SerialPrint_P(PSTR(x))
/** Store and print a string in flash memory followed by a CR/LF.*/
#define PgmPrintln(x) SerialPrintln_P(PSTR(x))
/** Defined so doxygen works for function definitions. */
#define NOINLINE __attribute__((noinline,unused))
#define UNUSEDOK __attribute__((unused))
//------------------------------------------------------------------------------
// this unused FreeRam() function can cause compatibility issues
// when __brkval isn't defined the way it expects
//
/** Return the number of bytes currently free in RAM. */
//static UNUSEDOK int FreeRam(void) {
// extern int __bss_end;
// extern int* __brkval;
// int free_memory;
// if (reinterpret_cast<int>(__brkval) == 0) {
// // if no heap use from end of bss section
// free_memory = reinterpret_cast<int>(&free_memory)
// - reinterpret_cast<int>(&__bss_end);
// } else {
// // use from top of stack to heap
// free_memory = reinterpret_cast<int>(&free_memory)
// - reinterpret_cast<int>(__brkval);
// }
// return free_memory;
//}
//------------------------------------------------------------------------------
/**
* %Print a string in flash memory to the serial port.
*
* \param[in] str Pointer to string stored in flash memory.
*/
static NOINLINE void SerialPrint_P(PGM_P str) {
for (uint8_t c; (c = pgm_read_byte(str)); str++) Serial.write(c);
}
//------------------------------------------------------------------------------
/**
* %Print a string in flash memory followed by a CR/LF.
*
* \param[in] str Pointer to string stored in flash memory.
*/
static NOINLINE void SerialPrintln_P(PGM_P str) {
SerialPrint_P(str);
Serial.println();
}
#endif // #define SdFatUtil_h

+ 0
- 202
utility/SdFatmainpage.h View File

@@ -1,202 +0,0 @@
/* Arduino SdFat Library
* Copyright (C) 2009 by William Greiman
*
* This file is part of the Arduino SdFat Library
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Arduino SdFat Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
/**
\mainpage Arduino SdFat Library
<CENTER>Copyright &copy; 2009 by William Greiman
</CENTER>
\section Intro Introduction
The Arduino SdFat Library is a minimal implementation of FAT16 and FAT32
file systems on SD flash memory cards. Standard SD and high capacity
SDHC cards are supported.
The SdFat only supports short 8.3 names.
The main classes in SdFat are Sd2Card, SdVolume, and SdFile.
The Sd2Card class supports access to standard SD cards and SDHC cards. Most
applications will only need to call the Sd2Card::init() member function.
The SdVolume class supports FAT16 and FAT32 partitions. Most applications
will only need to call the SdVolume::init() member function.
The SdFile class provides file access functions such as open(), read(),
remove(), write(), close() and sync(). This class supports access to the root
directory and subdirectories.
A number of example are provided in the SdFat/examples folder. These were
developed to test SdFat and illustrate its use.
SdFat was developed for high speed data recording. SdFat was used to implement
an audio record/play class, WaveRP, for the Adafruit Wave Shield. This
application uses special Sd2Card calls to write to contiguous files in raw mode.
These functions reduce write latency so that audio can be recorded with the
small amount of RAM in the Arduino.
\section SDcard SD\SDHC Cards
Arduinos access SD cards using the cards SPI protocol. PCs, Macs, and
most consumer devices use the 4-bit parallel SD protocol. A card that
functions well on A PC or Mac may not work well on the Arduino.
Most cards have good SPI read performance but cards vary widely in SPI
write performance. Write performance is limited by how efficiently the
card manages internal erase/remapping operations. The Arduino cannot
optimize writes to reduce erase operations because of its limit RAM.
SanDisk cards generally have good write performance. They seem to have
more internal RAM buffering than other cards and therefore can limit
the number of flash erase operations that the Arduino forces due to its
limited RAM.
\section Hardware Hardware Configuration
SdFat was developed using an
<A HREF = "http://www.adafruit.com/"> Adafruit Industries</A>
<A HREF = "http://www.ladyada.net/make/waveshield/"> Wave Shield</A>.
The hardware interface to the SD card should not use a resistor based level
shifter. SdFat sets the SPI bus frequency to 8 MHz which results in signal
rise times that are too slow for the edge detectors in many newer SD card
controllers when resistor voltage dividers are used.
The 5 to 3.3 V level shifter for 5 V Arduinos should be IC based like the
74HC4050N based circuit shown in the file SdLevel.png. The Adafruit Wave Shield
uses a 74AHC125N. Gravitech sells SD and MicroSD Card Adapters based on the
74LCX245.
If you are using a resistor based level shifter and are having problems try
setting the SPI bus frequency to 4 MHz. This can be done by using
card.init(SPI_HALF_SPEED) to initialize the SD card.
\section comment Bugs and Comments
If you wish to report bugs or have comments, send email to fat16lib@sbcglobal.net.
\section SdFatClass SdFat Usage
SdFat uses a slightly restricted form of short names.
Only printable ASCII characters are supported. No characters with code point
values greater than 127 are allowed. Space is not allowed even though space
was allowed in the API of early versions of DOS.
Short names are limited to 8 characters followed by an optional period (.)
and extension of up to 3 characters. The characters may be any combination
of letters and digits. The following special characters are also allowed:
$ % ' - _ @ ~ ` ! ( ) { } ^ # &
Short names are always converted to upper case and their original case
value is lost.
\note
The Arduino Print class uses character
at a time writes so it was necessary to use a \link SdFile::sync() sync() \endlink
function to control when data is written to the SD card.
\par
An application which writes to a file using \link Print::print() print()\endlink,
\link Print::println() println() \endlink
or \link SdFile::write write() \endlink must call \link SdFile::sync() sync() \endlink
at the appropriate time to force data and directory information to be written
to the SD Card. Data and directory information are also written to the SD card
when \link SdFile::close() close() \endlink is called.
\par
Applications must use care calling \link SdFile::sync() sync() \endlink
since 2048 bytes of I/O is required to update file and
directory information. This includes writing the current data block, reading
the block that contains the directory entry for update, writing the directory
block back and reading back the current data block.
It is possible to open a file with two or more instances of SdFile. A file may
be corrupted if data is written to the file by more than one instance of SdFile.
\section HowTo How to format SD Cards as FAT Volumes
You should use a freshly formatted SD card for best performance. FAT
file systems become slower if many files have been created and deleted.
This is because the directory entry for a deleted file is marked as deleted,
but is not deleted. When a new file is created, these entries must be scanned
before creating the file, a flaw in the FAT design. Also files can become
fragmented which causes reads and writes to be slower.
Microsoft operating systems support removable media formatted with a
Master Boot Record, MBR, or formatted as a super floppy with a FAT Boot Sector
in block zero.
Microsoft operating systems expect MBR formatted removable media
to have only one partition. The first partition should be used.
Microsoft operating systems do not support partitioning SD flash cards.
If you erase an SD card with a program like KillDisk, Most versions of
Windows will format the card as a super floppy.
The best way to restore an SD card's format is to use SDFormatter
which can be downloaded from:
http://www.sdcard.org/consumers/formatter/
SDFormatter aligns flash erase boundaries with file
system structures which reduces write latency and file system overhead.
SDFormatter does not have an option for FAT type so it may format
small cards as FAT12.
After the MBR is restored by SDFormatter you may need to reformat small
cards that have been formatted FAT12 to force the volume type to be FAT16.
If you reformat the SD card with an OS utility, choose a cluster size that
will result in:
4084 < CountOfClusters && CountOfClusters < 65525
The volume will then be FAT16.
If you are formatting an SD card on OS X or Linux, be sure to use the first
partition. Format this partition with a cluster count in above range.
\section References References
Adafruit Industries:
http://www.adafruit.com/
http://www.ladyada.net/make/waveshield/
The Arduino site:
http://www.arduino.cc/
For more information about FAT file systems see:
http://www.microsoft.com/whdc/system/platform/firmware/fatgen.mspx
For information about using SD cards as SPI devices see:
http://www.sdcard.org/developers/tech/sdcard/pls/Simplified_Physical_Layer_Spec.pdf
The ATmega328 datasheet:
http://www.atmel.com/dyn/resources/prod_documents/doc8161.pdf
*/

+ 0
- 1252
utility/SdFile.cpp
File diff suppressed because it is too large
View File


+ 0
- 232
utility/SdInfo.h View File

@@ -1,232 +0,0 @@
/* Arduino Sd2Card Library
* Copyright (C) 2009 by William Greiman
*
* This file is part of the Arduino Sd2Card Library
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Arduino Sd2Card Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifndef SdInfo_h
#define SdInfo_h
#include <stdint.h>
// Based on the document:
//
// SD Specifications
// Part 1
// Physical Layer
// Simplified Specification
// Version 2.00
// September 25, 2006
//
// www.sdcard.org/developers/tech/sdcard/pls/Simplified_Physical_Layer_Spec.pdf
//------------------------------------------------------------------------------
// SD card commands
/** GO_IDLE_STATE - init card in spi mode if CS low */
uint8_t const CMD0 = 0X00;
/** SEND_IF_COND - verify SD Memory Card interface operating condition.*/
uint8_t const CMD8 = 0X08;
/** SEND_CSD - read the Card Specific Data (CSD register) */
uint8_t const CMD9 = 0X09;
/** SEND_CID - read the card identification information (CID register) */
uint8_t const CMD10 = 0X0A;
/** SEND_STATUS - read the card status register */
uint8_t const CMD13 = 0X0D;
/** READ_BLOCK - read a single data block from the card */
uint8_t const CMD17 = 0X11;
/** WRITE_BLOCK - write a single data block to the card */
uint8_t const CMD24 = 0X18;
/** WRITE_MULTIPLE_BLOCK - write blocks of data until a STOP_TRANSMISSION */
uint8_t const CMD25 = 0X19;
/** ERASE_WR_BLK_START - sets the address of the first block to be erased */
uint8_t const CMD32 = 0X20;
/** ERASE_WR_BLK_END - sets the address of the last block of the continuous
range to be erased*/
uint8_t const CMD33 = 0X21;
/** ERASE - erase all previously selected blocks */
uint8_t const CMD38 = 0X26;
/** APP_CMD - escape for application specific command */
uint8_t const CMD55 = 0X37;
/** READ_OCR - read the OCR register of a card */
uint8_t const CMD58 = 0X3A;
/** SET_WR_BLK_ERASE_COUNT - Set the number of write blocks to be
pre-erased before writing */
uint8_t const ACMD23 = 0X17;
/** SD_SEND_OP_COMD - Sends host capacity support information and
activates the card's initialization process */
uint8_t const ACMD41 = 0X29;
//------------------------------------------------------------------------------
/** status for card in the ready state */
uint8_t const R1_READY_STATE = 0X00;
/** status for card in the idle state */
uint8_t const R1_IDLE_STATE = 0X01;
/** status bit for illegal command */
uint8_t const R1_ILLEGAL_COMMAND = 0X04;
/** start data token for read or write single block*/
uint8_t const DATA_START_BLOCK = 0XFE;
/** stop token for write multiple blocks*/
uint8_t const STOP_TRAN_TOKEN = 0XFD;
/** start data token for write multiple blocks*/
uint8_t const WRITE_MULTIPLE_TOKEN = 0XFC;
/** mask for data response tokens after a write block operation */
uint8_t const DATA_RES_MASK = 0X1F;
/** write data accepted token */
uint8_t const DATA_RES_ACCEPTED = 0X05;
//------------------------------------------------------------------------------
typedef struct CID {
// byte 0
uint8_t mid; // Manufacturer ID
// byte 1-2
char oid[2]; // OEM/Application ID
// byte 3-7
char pnm[5]; // Product name
// byte 8
unsigned prv_m : 4; // Product revision n.m
unsigned prv_n : 4;
// byte 9-12
uint32_t psn; // Product serial number
// byte 13
unsigned mdt_year_high : 4; // Manufacturing date
unsigned reserved : 4;
// byte 14
unsigned mdt_month : 4;
unsigned mdt_year_low :4;
// byte 15
unsigned always1 : 1;
unsigned crc : 7;
}cid_t;
//------------------------------------------------------------------------------
// CSD for version 1.00 cards
typedef struct CSDV1 {
// byte 0
unsigned reserved1 : 6;
unsigned csd_ver : 2;
// byte 1
uint8_t taac;
// byte 2
uint8_t nsac;
// byte 3
uint8_t tran_speed;
// byte 4
uint8_t ccc_high;
// byte 5
unsigned read_bl_len : 4;
unsigned ccc_low : 4;
// byte 6
unsigned c_size_high : 2;
unsigned reserved2 : 2;
unsigned dsr_imp : 1;
unsigned read_blk_misalign :1;
unsigned write_blk_misalign : 1;
unsigned read_bl_partial : 1;
// byte 7
uint8_t c_size_mid;
// byte 8
unsigned vdd_r_curr_max : 3;
unsigned vdd_r_curr_min : 3;
unsigned c_size_low :2;
// byte 9
unsigned c_size_mult_high : 2;
unsigned vdd_w_cur_max : 3;
unsigned vdd_w_curr_min : 3;
// byte 10
unsigned sector_size_high : 6;
unsigned erase_blk_en : 1;
unsigned c_size_mult_low : 1;
// byte 11
unsigned wp_grp_size : 7;
unsigned sector_size_low : 1;
// byte 12
unsigned write_bl_len_high : 2;
unsigned r2w_factor : 3;
unsigned reserved3 : 2;
unsigned wp_grp_enable : 1;
// byte 13
unsigned reserved4 : 5;
unsigned write_partial : 1;
unsigned write_bl_len_low : 2;
// byte 14
unsigned reserved5: 2;
unsigned file_format : 2;
unsigned tmp_write_protect : 1;
unsigned perm_write_protect : 1;
unsigned copy : 1;
unsigned file_format_grp : 1;
// byte 15
unsigned always1 : 1;
unsigned crc : 7;
}csd1_t;
//------------------------------------------------------------------------------
// CSD for version 2.00 cards
typedef struct CSDV2 {
// byte 0
unsigned reserved1 : 6;
unsigned csd_ver : 2;
// byte 1
uint8_t taac;
// byte 2
uint8_t nsac;
// byte 3
uint8_t tran_speed;
// byte 4
uint8_t ccc_high;
// byte 5
unsigned read_bl_len : 4;
unsigned ccc_low : 4;
// byte 6
unsigned reserved2 : 4;
unsigned dsr_imp : 1;
unsigned read_blk_misalign :1;
unsigned write_blk_misalign : 1;
unsigned read_bl_partial : 1;
// byte 7
unsigned reserved3 : 2;
unsigned c_size_high : 6;
// byte 8
uint8_t c_size_mid;
// byte 9
uint8_t c_size_low;
// byte 10
unsigned sector_size_high : 6;
unsigned erase_blk_en : 1;
unsigned reserved4 : 1;
// byte 11
unsigned wp_grp_size : 7;
unsigned sector_size_low : 1;
// byte 12
unsigned write_bl_len_high : 2;
unsigned r2w_factor : 3;
unsigned reserved5 : 2;
unsigned wp_grp_enable : 1;
// byte 13
unsigned reserved6 : 5;
unsigned write_partial : 1;
unsigned write_bl_len_low : 2;
// byte 14
unsigned reserved7: 2;
unsigned file_format : 2;
unsigned tmp_write_protect : 1;
unsigned perm_write_protect : 1;
unsigned copy : 1;
unsigned file_format_grp : 1;
// byte 15
unsigned always1 : 1;
unsigned crc : 7;
}csd2_t;
//------------------------------------------------------------------------------
// union of old and new style CSD register
union csd_t {
csd1_t v1;
csd2_t v2;
};
#endif // SdInfo_h

+ 0
- 295
utility/SdVolume.cpp View File

@@ -1,295 +0,0 @@
/* Arduino SdFat Library
* Copyright (C) 2009 by William Greiman
*
* This file is part of the Arduino SdFat Library
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Arduino SdFat Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <SdFat.h>
//------------------------------------------------------------------------------
// raw block cache
// init cacheBlockNumber_to invalid SD block number
uint32_t SdVolume::cacheBlockNumber_ = 0XFFFFFFFF;
cache_t SdVolume::cacheBuffer_; // 512 byte cache for Sd2Card
Sd2Card* SdVolume::sdCard_; // pointer to SD card object
uint8_t SdVolume::cacheDirty_ = 0; // cacheFlush() will write block if true
uint32_t SdVolume::cacheMirrorBlock_ = 0; // mirror block for second FAT
//------------------------------------------------------------------------------
// find a contiguous group of clusters
uint8_t SdVolume::allocContiguous(uint32_t count, uint32_t* curCluster) {
// start of group
uint32_t bgnCluster;
// flag to save place to start next search
uint8_t setStart;
// set search start cluster
if (*curCluster) {
// try to make file contiguous
bgnCluster = *curCluster + 1;
// don't save new start location
setStart = false;
} else {
// start at likely place for free cluster
bgnCluster = allocSearchStart_;
// save next search start if one cluster
setStart = 1 == count;
}
// end of group
uint32_t endCluster = bgnCluster;
// last cluster of FAT
uint32_t fatEnd = clusterCount_ + 1;
// search the FAT for free clusters
for (uint32_t n = 0;; n++, endCluster++) {
// can't find space checked all clusters
if (n >= clusterCount_) return false;
// past end - start from beginning of FAT
if (endCluster > fatEnd) {
bgnCluster = endCluster = 2;
}
uint32_t f;
if (!fatGet(endCluster, &f)) return false;
if (f != 0) {
// cluster in use try next cluster as bgnCluster
bgnCluster = endCluster + 1;
} else if ((endCluster - bgnCluster + 1) == count) {
// done - found space
break;
}
}
// mark end of chain
if (!fatPutEOC(endCluster)) return false;
// link clusters
while (endCluster > bgnCluster) {
if (!fatPut(endCluster - 1, endCluster)) return false;
endCluster--;
}
if (*curCluster != 0) {
// connect chains
if (!fatPut(*curCluster, bgnCluster)) return false;
}
// return first cluster number to caller
*curCluster = bgnCluster;
// remember possible next free cluster
if (setStart) allocSearchStart_ = bgnCluster + 1;
return true;
}
//------------------------------------------------------------------------------
uint8_t SdVolume::cacheFlush(void) {
if (cacheDirty_) {
if (!sdCard_->writeBlock(cacheBlockNumber_, cacheBuffer_.data)) {
return false;
}
// mirror FAT tables
if (cacheMirrorBlock_) {
if (!sdCard_->writeBlock(cacheMirrorBlock_, cacheBuffer_.data)) {
return false;
}
cacheMirrorBlock_ = 0;
}
cacheDirty_ = 0;
}
return true;
}
//------------------------------------------------------------------------------
uint8_t SdVolume::cacheRawBlock(uint32_t blockNumber, uint8_t action) {
if (cacheBlockNumber_ != blockNumber) {
if (!cacheFlush()) return false;
if (!sdCard_->readBlock(blockNumber, cacheBuffer_.data)) return false;
cacheBlockNumber_ = blockNumber;
}
cacheDirty_ |= action;
return true;
}
//------------------------------------------------------------------------------
// cache a zero block for blockNumber
uint8_t SdVolume::cacheZeroBlock(uint32_t blockNumber) {
if (!cacheFlush()) return false;
// loop take less flash than memset(cacheBuffer_.data, 0, 512);
for (uint16_t i = 0; i < 512; i++) {
cacheBuffer_.data[i] = 0;
}
cacheBlockNumber_ = blockNumber;
cacheSetDirty();
return true;
}
//------------------------------------------------------------------------------
// return the size in bytes of a cluster chain
uint8_t SdVolume::chainSize(uint32_t cluster, uint32_t* size) const {
uint32_t s = 0;
do {
if (!fatGet(cluster, &cluster)) return false;
s += 512UL << clusterSizeShift_;
} while (!isEOC(cluster));
*size = s;
return true;
}
//------------------------------------------------------------------------------
// Fetch a FAT entry
uint8_t SdVolume::fatGet(uint32_t cluster, uint32_t* value) const {
if (cluster > (clusterCount_ + 1)) return false;
uint32_t lba = fatStartBlock_;
lba += fatType_ == 16 ? cluster >> 8 : cluster >> 7;
if (lba != cacheBlockNumber_) {
if (!cacheRawBlock(lba, CACHE_FOR_READ)) return false;
}
if (fatType_ == 16) {
*value = cacheBuffer_.fat16[cluster & 0XFF];
} else {
*value = cacheBuffer_.fat32[cluster & 0X7F] & FAT32MASK;
}
return true;
}
//------------------------------------------------------------------------------
// Store a FAT entry
uint8_t SdVolume::fatPut(uint32_t cluster, uint32_t value) {
// error if reserved cluster
if (cluster < 2) return false;
// error if not in FAT
if (cluster > (clusterCount_ + 1)) return false;
// calculate block address for entry
uint32_t lba = fatStartBlock_;
lba += fatType_ == 16 ? cluster >> 8 : cluster >> 7;
if (lba != cacheBlockNumber_) {
if (!cacheRawBlock(lba, CACHE_FOR_READ)) return false;
}
// store entry
if (fatType_ == 16) {
cacheBuffer_.fat16[cluster & 0XFF] = value;
} else {
cacheBuffer_.fat32[cluster & 0X7F] = value;
}
cacheSetDirty();
// mirror second FAT
if (fatCount_ > 1) cacheMirrorBlock_ = lba + blocksPerFat_;
return true;
}
//------------------------------------------------------------------------------
// free a cluster chain
uint8_t SdVolume::freeChain(uint32_t cluster) {
// clear free cluster location
allocSearchStart_ = 2;
do {
uint32_t next;
if (!fatGet(cluster, &next)) return false;
// free cluster
if (!fatPut(cluster, 0)) return false;
cluster = next;
} while (!isEOC(cluster));
return true;
}
//------------------------------------------------------------------------------
/**
* Initialize a FAT volume.
*
* \param[in] dev The SD card where the volume is located.
*
* \param[in] part The partition to be used. Legal values for \a part are
* 1-4 to use the corresponding partition on a device formatted with
* a MBR, Master Boot Record, or zero if the device is formatted as
* a super floppy with the FAT boot sector in block zero.
*
* \return The value one, true, is returned for success and
* the value zero, false, is returned for failure. Reasons for
* failure include not finding a valid partition, not finding a valid
* FAT file system in the specified partition or an I/O error.
*/
uint8_t SdVolume::init(Sd2Card* dev, uint8_t part) {
uint32_t volumeStartBlock = 0;
sdCard_ = dev;
// if part == 0 assume super floppy with FAT boot sector in block zero
// if part > 0 assume mbr volume with partition table
if (part) {
if (part > 4)return false;
if (!cacheRawBlock(volumeStartBlock, CACHE_FOR_READ)) return false;
part_t* p = &cacheBuffer_.mbr.part[part-1];
if ((p->boot & 0X7F) !=0 ||
p->totalSectors < 100 ||
p->firstSector == 0) {
// not a valid partition
return false;
}
volumeStartBlock = p->firstSector;
}
if (!cacheRawBlock(volumeStartBlock, CACHE_FOR_READ)) return false;
bpb_t* bpb = &cacheBuffer_.fbs.bpb;
if (bpb->bytesPerSector != 512 ||
bpb->fatCount == 0 ||
bpb->reservedSectorCount == 0 ||
bpb->sectorsPerCluster == 0) {
// not valid FAT volume
return false;
}
fatCount_ = bpb->fatCount;
blocksPerCluster_ = bpb->sectorsPerCluster;
// determine shift that is same as multiply by blocksPerCluster_
clusterSizeShift_ = 0;
while (blocksPerCluster_ != (1 << clusterSizeShift_)) {
// error if not power of 2
if (clusterSizeShift_++ > 7) return false;
}
blocksPerFat_ = bpb->sectorsPerFat16 ?
bpb->sectorsPerFat16 : bpb->sectorsPerFat32;
fatStartBlock_ = volumeStartBlock + bpb->reservedSectorCount;
// count for FAT16 zero for FAT32
rootDirEntryCount_ = bpb->rootDirEntryCount;
// directory start for FAT16 dataStart for FAT32
rootDirStart_ = fatStartBlock_ + bpb->fatCount * blocksPerFat_;
// data start for FAT16 and FAT32
dataStartBlock_ = rootDirStart_ + ((32 * bpb->rootDirEntryCount + 511)/512);
// total blocks for FAT16 or FAT32
uint32_t totalBlocks = bpb->totalSectors16 ?
bpb->totalSectors16 : bpb->totalSectors32;
// total data blocks
clusterCount_ = totalBlocks - (dataStartBlock_ - volumeStartBlock);
// divide by cluster size to get cluster count
clusterCount_ >>= clusterSizeShift_;
// FAT type is determined by cluster count
if (clusterCount_ < 4085) {
fatType_ = 12;
} else if (clusterCount_ < 65525) {
fatType_ = 16;
} else {
rootDirStart_ = bpb->fat32RootCluster;
fatType_ = 32;
}
return true;
}

+ 0
- 102
utility/ioreg.h View File

@@ -1,102 +0,0 @@
/* Optimized SD Library for Teensy 3.X
* Copyright (c) 2015, Paul Stoffregen, paul@pjrc.com
*
* Development of this SD library was funded by PJRC.COM, LLC by sales of
* Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
* open source software by purchasing genuine Teensy or other PJRC products.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice, development funding notice, and this permission
* notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#ifndef _SD_t3_ioreg_h_
#define _SD_t3_ioreg_h_

// from OneWire.h

#include "Arduino.h"

#if defined(__AVR__)
#define PIN_TO_BASEREG(pin) (portInputRegister(digitalPinToPort(pin)))
#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
#define IO_REG_TYPE uint8_t
#define IO_REG_ASM asm("r30")
#define DIRECT_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0)
#define DIRECT_MODE_INPUT(base, mask) ((*((base)+1)) &= ~(mask))
#define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+1)) |= (mask))
#define DIRECT_WRITE_LOW(base, mask) ((*((base)+2)) &= ~(mask))
#define DIRECT_WRITE_HIGH(base, mask) ((*((base)+2)) |= (mask))

#elif defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
#define PIN_TO_BASEREG(pin) (portOutputRegister(pin))
#define PIN_TO_BITMASK(pin) (1)
#define IO_REG_TYPE uint8_t
#define IO_REG_ASM
#define DIRECT_READ(base, mask) (*((base)+512))
#define DIRECT_MODE_INPUT(base, mask) (*((base)+640) = 0)
#define DIRECT_MODE_OUTPUT(base, mask) (*((base)+640) = 1)
#define DIRECT_WRITE_LOW(base, mask) (*((base)+256) = 1)
#define DIRECT_WRITE_HIGH(base, mask) (*((base)+128) = 1)

#elif defined(__MKL26Z64__)
#define PIN_TO_BASEREG(pin) (portOutputRegister(pin))
#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
#define IO_REG_TYPE uint8_t
#define IO_REG_ASM
#define DIRECT_READ(base, mask) ((*((base)+16) & (mask)) ? 1 : 0)
#define DIRECT_MODE_INPUT(base, mask) (*((base)+20) &= ~(mask))
#define DIRECT_MODE_OUTPUT(base, mask) (*((base)+20) |= (mask))
#define DIRECT_WRITE_LOW(base, mask) (*((base)+8) = (mask))
#define DIRECT_WRITE_HIGH(base, mask) (*((base)+4) = (mask))

#elif defined(__SAM3X8E__)
#define PIN_TO_BASEREG(pin) (&(digitalPinToPort(pin)->PIO_PER))
#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
#define IO_REG_TYPE uint32_t
#define IO_REG_ASM
#define DIRECT_READ(base, mask) (((*((base)+15)) & (mask)) ? 1 : 0)
#define DIRECT_MODE_INPUT(base, mask) ((*((base)+5)) = (mask))
#define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+4)) = (mask))
#define DIRECT_WRITE_LOW(base, mask) ((*((base)+13)) = (mask))
#define DIRECT_WRITE_HIGH(base, mask) ((*((base)+12)) = (mask))

#elif defined(__PIC32MX__)
#define PIN_TO_BASEREG(pin) (portModeRegister(digitalPinToPort(pin)))
#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
#define IO_REG_TYPE uint32_t
#define IO_REG_ASM
#define DIRECT_READ(base, mask) (((*(base+4)) & (mask)) ? 1 : 0) //PORTX + 0x10
#define DIRECT_MODE_INPUT(base, mask) ((*(base+2)) = (mask)) //TRISXSET + 0x08
#define DIRECT_MODE_OUTPUT(base, mask) ((*(base+1)) = (mask)) //TRISXCLR + 0x04
#define DIRECT_WRITE_LOW(base, mask) ((*(base+8+1)) = (mask)) //LATXCLR + 0x24
#define DIRECT_WRITE_HIGH(base, mask) ((*(base+8+2)) = (mask)) //LATXSET + 0x28

#else
#define PIN_TO_BASEREG(pin) (0)
#define PIN_TO_BITMASK(pin) (pin)
#define IO_REG_TYPE unsigned int
#define IO_REG_ASM
#define DIRECT_READ(base, pin) digitalRead(pin)
#define DIRECT_WRITE_LOW(base, pin) digitalWrite(pin, LOW)
#define DIRECT_WRITE_HIGH(base, pin) digitalWrite(pin, HIGH)
#define DIRECT_MODE_INPUT(base, pin) pinMode(pin,INPUT)
#define DIRECT_MODE_OUTPUT(base, pin) pinMode(pin,OUTPUT)

#endif // CPU types

#endif

Loading…
Cancel
Save