SD file updates

This commit is contained in:
Scott Lahteine 2017-09-06 06:28:32 -05:00
parent 82fff87e16
commit 614a86a380
15 changed files with 233 additions and 226 deletions

View file

@ -26,11 +26,15 @@
*
* This file is part of the Arduino Sd2Card Library
*/
#include "Marlin.h"
#include "../inc/MarlinConfig.h"
#if ENABLED(SDSUPPORT)
#include "Sd2Card.h"
#include "../Marlin.h"
//------------------------------------------------------------------------------
// send command and return error code. Return zero for OK
uint8_t Sd2Card::cardCommand(uint8_t cmd, uint32_t arg) {

View file

@ -27,17 +27,18 @@
* This file is part of the Arduino Sd2Card Library
*/
#include "Marlin.h"
#if ENABLED(SDSUPPORT)
#ifndef SD2CARD_H
#define SD2CARD_H
#ifndef Sd2Card_h
#define Sd2Card_h
/**
* \file
* \brief Sd2Card class for V2 SD/SDHC cards
*/
#include "SdFatConfig.h"
#include "SdInfo.h"
#include <stdint.h>
//------------------------------------------------------------------------------
/** init timeout ms */
uint16_t const SD_INIT_TIMEOUT = 2000;
@ -239,7 +240,5 @@ class Sd2Card {
bool waitNotBusy(uint16_t timeoutMillis);
bool writeData(uint8_t token, const uint8_t* src);
};
#endif // Sd2Card_h
#endif
#endif // SD2CARD_H

View file

@ -27,10 +27,14 @@
* This file is part of the Arduino Sd2Card Library
*/
#include "Marlin.h"
#include "../inc/MarlinConfig.h"
#if ENABLED(SDSUPPORT)
#include "SdBaseFile.h"
#include "../Marlin.h"
//------------------------------------------------------------------------------
// pointer to cwd directory
SdBaseFile* SdBaseFile::cwd_ = 0;
@ -1822,5 +1826,4 @@ int16_t SdBaseFile::write(const void* buf, uint16_t nbyte) {
void (*SdBaseFile::oldDateTime_)(uint16_t &date, uint16_t &time) = 0; // NOLINT
#endif // ALLOW_DEPRECATED_FUNCTIONS
#endif
#endif // SDSUPPORT

View file

@ -26,18 +26,19 @@
*
* This file is part of the Arduino Sd2Card Library
*/
#include "Marlin.h"
#if ENABLED(SDSUPPORT)
#ifndef SdBaseFile_h
#define SdBaseFile_h
#ifndef SDBASEFILE_H
#define SDBASEFILE_H
/**
* \file
* \brief SdBaseFile class
*/
#include "Marlin.h"
#include "SdFatConfig.h"
#include "SdVolume.h"
#include <stdint.h>
//------------------------------------------------------------------------------
/**
* \struct filepos_t
@ -488,5 +489,4 @@ class SdBaseFile {
#endif // ALLOW_DEPRECATED_FUNCTIONS
};
#endif // SdBaseFile_h
#endif
#endif // SDBASEFILE_H

View file

@ -26,109 +26,105 @@
*
* This file is part of the Arduino Sd2Card Library
*/
/**
* \file
* \brief configuration definitions
*/
#include "Marlin.h"
#if ENABLED(SDSUPPORT)
#ifndef SDFATCONFIG_H
#define SDFATCONFIG_H
#ifndef SdFatConfig_h
#define SdFatConfig_h
#include <stdint.h>
//------------------------------------------------------------------------------
/**
* To use multiple SD cards set USE_MULTIPLE_CARDS nonzero.
*
* Using multiple cards costs 400 - 500 bytes of flash.
*
* Each card requires about 550 bytes of SRAM so use of a Mega is recommended.
*/
#define USE_MULTIPLE_CARDS 0
//------------------------------------------------------------------------------
/**
* Call flush for endl if ENDL_CALLS_FLUSH is nonzero
*
* The standard for iostreams is to call flush. This is very costly for
* SdFat. Each call to flush causes 2048 bytes of I/O to the SD.
*
* SdFat has a single 512 byte buffer for SD I/O so it must write the current
* data block to the SD, read the directory block from the SD, update the
* directory entry, write the directory block to the SD and read the data
* block back into the buffer.
*
* The SD flash memory controller is not designed for this many rewrites
* so performance may be reduced by more than a factor of 100.
*
* If ENDL_CALLS_FLUSH is zero, you must call flush and/or close to force
* all data to be written to the SD.
*/
#define ENDL_CALLS_FLUSH 0
//------------------------------------------------------------------------------
/**
* Allow use of deprecated functions if ALLOW_DEPRECATED_FUNCTIONS is nonzero
*/
#define ALLOW_DEPRECATED_FUNCTIONS 1
//------------------------------------------------------------------------------
/**
* Allow FAT12 volumes if FAT12_SUPPORT is nonzero.
* FAT12 has not been well tested.
*/
#define FAT12_SUPPORT 0
//------------------------------------------------------------------------------
/**
* SPI init rate for SD initialization commands. Must be 5 (F_CPU/64)
* or 6 (F_CPU/128).
*/
#define SPI_SD_INIT_RATE 5
//------------------------------------------------------------------------------
/**
* Set the SS pin high for hardware SPI. If SS is chip select for another SPI
* device this will disable that device during the SD init phase.
*/
#define SET_SPI_SS_HIGH 1
//------------------------------------------------------------------------------
/**
* Define MEGA_SOFT_SPI nonzero to use software SPI on Mega Arduinos.
* Pins used are SS 10, MOSI 11, MISO 12, and SCK 13.
*
* MEGA_SOFT_SPI allows an unmodified Adafruit GPS Shield to be used
* on Mega Arduinos. Software SPI works well with GPS Shield V1.1
* but many SD cards will fail with GPS Shield V1.0.
*/
#define MEGA_SOFT_SPI 0
//------------------------------------------------------------------------------
/**
* Set USE_SOFTWARE_SPI nonzero to always use software SPI.
*/
#define USE_SOFTWARE_SPI 0
// define software SPI pins so Mega can use unmodified 168/328 shields
/** Software SPI chip select pin for the SD */
#define SOFT_SPI_CS_PIN 10
/** Software SPI Master Out Slave In pin */
#define SOFT_SPI_MOSI_PIN 11
/** Software SPI Master In Slave Out pin */
#define SOFT_SPI_MISO_PIN 12
/** Software SPI Clock pin */
#define SOFT_SPI_SCK_PIN 13
//------------------------------------------------------------------------------
/**
* The __cxa_pure_virtual function is an error handler that is invoked when
* a pure virtual function is called.
*/
#define USE_CXA_PURE_VIRTUAL 1
//------------------------------------------------------------------------------
/**
* To use multiple SD cards set USE_MULTIPLE_CARDS nonzero.
*
* Using multiple cards costs 400 - 500 bytes of flash.
*
* Each card requires about 550 bytes of SRAM so use of a Mega is recommended.
*/
#define USE_MULTIPLE_CARDS 0
//------------------------------------------------------------------------------
/**
* Call flush for endl if ENDL_CALLS_FLUSH is nonzero
*
* The standard for iostreams is to call flush. This is very costly for
* SdFat. Each call to flush causes 2048 bytes of I/O to the SD.
*
* SdFat has a single 512 byte buffer for SD I/O so it must write the current
* data block to the SD, read the directory block from the SD, update the
* directory entry, write the directory block to the SD and read the data
* block back into the buffer.
*
* The SD flash memory controller is not designed for this many rewrites
* so performance may be reduced by more than a factor of 100.
*
* If ENDL_CALLS_FLUSH is zero, you must call flush and/or close to force
* all data to be written to the SD.
*/
#define ENDL_CALLS_FLUSH 0
//------------------------------------------------------------------------------
/**
* Allow use of deprecated functions if ALLOW_DEPRECATED_FUNCTIONS is nonzero
*/
#define ALLOW_DEPRECATED_FUNCTIONS 1
//------------------------------------------------------------------------------
/**
* Allow FAT12 volumes if FAT12_SUPPORT is nonzero.
* FAT12 has not been well tested.
*/
#define FAT12_SUPPORT 0
//------------------------------------------------------------------------------
/**
* SPI init rate for SD initialization commands. Must be 5 (F_CPU/64)
* or 6 (F_CPU/128).
*/
#define SPI_SD_INIT_RATE 5
//------------------------------------------------------------------------------
/**
* Set the SS pin high for hardware SPI. If SS is chip select for another SPI
* device this will disable that device during the SD init phase.
*/
#define SET_SPI_SS_HIGH 1
//------------------------------------------------------------------------------
/**
* Define MEGA_SOFT_SPI nonzero to use software SPI on Mega Arduinos.
* Pins used are SS 10, MOSI 11, MISO 12, and SCK 13.
*
* MEGA_SOFT_SPI allows an unmodified Adafruit GPS Shield to be used
* on Mega Arduinos. Software SPI works well with GPS Shield V1.1
* but many SD cards will fail with GPS Shield V1.0.
*/
#define MEGA_SOFT_SPI 0
//------------------------------------------------------------------------------
/**
* Set USE_SOFTWARE_SPI nonzero to always use software SPI.
*/
#define USE_SOFTWARE_SPI 0
// define software SPI pins so Mega can use unmodified 168/328 shields
/** Software SPI chip select pin for the SD */
#define SOFT_SPI_CS_PIN 10
/** Software SPI Master Out Slave In pin */
#define SOFT_SPI_MOSI_PIN 11
/** Software SPI Master In Slave Out pin */
#define SOFT_SPI_MISO_PIN 12
/** Software SPI Clock pin */
#define SOFT_SPI_SCK_PIN 13
//------------------------------------------------------------------------------
/**
* The __cxa_pure_virtual function is an error handler that is invoked when
* a pure virtual function is called.
*/
#define USE_CXA_PURE_VIRTUAL 1
/** Number of UTF-16 characters per entry */
#define FILENAME_LENGTH 13
/** Number of UTF-16 characters per entry */
#define FILENAME_LENGTH 13
/**
* Defines for long (vfat) filenames
*/
/** Number of VFAT entries used. Every entry has 13 UTF-16 characters */
#define MAX_VFAT_ENTRIES (2)
/** Total size of the buffer used to store the long filenames */
#define LONG_FILENAME_LENGTH (FILENAME_LENGTH*MAX_VFAT_ENTRIES+1)
#endif // SdFatConfig_h
/**
* Defines for long (vfat) filenames
*/
/** Number of VFAT entries used. Every entry has 13 UTF-16 characters */
#define MAX_VFAT_ENTRIES (2)
/** Total size of the buffer used to store the long filenames */
#define LONG_FILENAME_LENGTH (FILENAME_LENGTH*MAX_VFAT_ENTRIES+1)
#endif
#endif // SDFATCONFIG_H

View file

@ -26,11 +26,11 @@
*
* This file is part of the Arduino Sd2Card Library
*/
#include "Marlin.h"
#if ENABLED(SDSUPPORT)
#ifndef SdFatStructs_h
#define SdFatStructs_h
#ifndef SDFATSTRUCTS_H
#define SDFATSTRUCTS_H
#include <stdint.h>
#define PACKED __attribute__((__packed__))
/**
@ -649,7 +649,5 @@ static inline uint8_t DIR_IS_SUBDIR(const dir_t* dir) {
static inline uint8_t DIR_IS_FILE_OR_SUBDIR(const dir_t* dir) {
return (dir->attributes & DIR_ATT_VOLUME_ID) == 0;
}
#endif // SdFatStructs_h
#endif
#endif // SDFATSTRUCTS_H

View file

@ -26,9 +26,11 @@
*
* This file is part of the Arduino Sd2Card Library
*/
#include "Marlin.h"
#include "../inc/MarlinConfig.h"
#if ENABLED(SDSUPPORT)
#include "SdFatUtil.h"
//------------------------------------------------------------------------------
@ -88,4 +90,5 @@ void SdFatUtil::SerialPrint_P(PGM_P str) {
void SdFatUtil::SerialPrintln_P(PGM_P str) {
println_P(str);
}
#endif
#endif // SDSUPPORT

View file

@ -26,11 +26,10 @@
*
* This file is part of the Arduino Sd2Card Library
*/
#ifndef SdFatUtil_h
#define SdFatUtil_h
#ifndef SDFATUTIL_H
#define SDFATUTIL_H
#include "Marlin.h"
#if ENABLED(SDSUPPORT)
#include <string.h>
/**
* \file
@ -51,6 +50,4 @@ namespace SdFatUtil {
using namespace SdFatUtil; // NOLINT
#endif // SDSUPPORT
#endif // SdFatUtil_h
#endif // SDFATUTIL_H

View file

@ -26,10 +26,13 @@
*
* This file is part of the Arduino Sd2Card Library
*/
#include "Marlin.h"
#include "../inc/MarlinConfig.h"
#if ENABLED(SDSUPPORT)
#include "SdFile.h"
/** Create a file object and open it in the current working directory.
*
* \param[in] path A path with a valid 8.3 DOS name for a file to be opened.

View file

@ -26,18 +26,20 @@
*
* This file is part of the Arduino Sd2Card Library
*/
/**
* \file
* \brief SdFile class
*/
#include "Marlin.h"
#if ENABLED(SDSUPPORT)
#ifndef SDFILE_H
#define SDFILE_H
#include "SdBaseFile.h"
//todo: HAL: create wrapper for Print?
//#include <Print.h>
#ifndef SdFile_h
#define SdFile_h
#include <stdint.h>
#include <string.h>
//------------------------------------------------------------------------------
/**
* \class SdFile
@ -58,7 +60,5 @@ class SdFile : public SdBaseFile/*, public Print*/ {
void write_P(PGM_P str);
void writeln_P(PGM_P str);
};
#endif // SdFile_h
#endif
#endif // SDFILE_H

View file

@ -26,12 +26,11 @@
*
* This file is part of the Arduino Sd2Card Library
*/
#include "Marlin.h"
#if ENABLED(SDSUPPORT)
#ifndef SDINFO_H
#define SDINFO_H
#ifndef SdInfo_h
#define SdInfo_h
#include <stdint.h>
// Based on the document:
//
// SD Specifications
@ -284,6 +283,5 @@ union csd_t {
csd1_t v1;
csd2_t v2;
};
#endif // SdInfo_h
#endif
#endif // SDINFO_H

View file

@ -26,10 +26,15 @@
*
* This file is part of the Arduino Sd2Card Library
*/
#include "Marlin.h"
#include "../inc/MarlinConfig.h"
#if ENABLED(SDSUPPORT)
#include "SdVolume.h"
#include "../Marlin.h"
//------------------------------------------------------------------------------
#if !USE_MULTIPLE_CARDS
// raw block cache
@ -417,4 +422,5 @@ bool SdVolume::init(Sd2Card* dev, uint8_t part) {
FAIL:
return false;
}
#endif
#endif // SDSUPPORT

View file

@ -26,10 +26,9 @@
*
* This file is part of the Arduino Sd2Card Library
*/
#include "Marlin.h"
#if ENABLED(SDSUPPORT)
#ifndef SdVolume_h
#define SdVolume_h
#ifndef SDVOLUME_H
#define SDVOLUME_H
/**
* \file
* \brief SdVolume class
@ -38,6 +37,8 @@
#include "Sd2Card.h"
#include "SdFatStructs.h"
#include <stdint.h>
//==============================================================================
// SdVolume class
/**
@ -136,19 +137,19 @@ class SdVolume {
// value for dirty argument in cacheRawBlock to indicate write to cache
static bool const CACHE_FOR_WRITE = true;
#if USE_MULTIPLE_CARDS
cache_t cacheBuffer_; // 512 byte cache for device blocks
uint32_t cacheBlockNumber_; // Logical number of block in the cache
Sd2Card* sdCard_; // Sd2Card object for cache
bool cacheDirty_; // cacheFlush() will write block if true
uint32_t cacheMirrorBlock_; // block number for mirror FAT
#else // USE_MULTIPLE_CARDS
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 bool cacheDirty_; // cacheFlush() will write block if true
static uint32_t cacheMirrorBlock_; // block number for mirror FAT
#endif // USE_MULTIPLE_CARDS
#if USE_MULTIPLE_CARDS
cache_t cacheBuffer_; // 512 byte cache for device blocks
uint32_t cacheBlockNumber_; // Logical number of block in the cache
Sd2Card* sdCard_; // Sd2Card object for cache
bool cacheDirty_; // cacheFlush() will write block if true
uint32_t cacheMirrorBlock_; // block number for mirror FAT
#else // USE_MULTIPLE_CARDS
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 bool cacheDirty_; // cacheFlush() will write block if true
static uint32_t cacheMirrorBlock_; // block number for mirror FAT
#endif // USE_MULTIPLE_CARDS
uint32_t allocSearchStart_; // start cluster for alloc search
uint8_t blocksPerCluster_; // cluster size in blocks
uint32_t blocksPerFat_; // FAT size in blocks
@ -173,13 +174,13 @@ class SdVolume {
}
cache_t* cache() {return &cacheBuffer_;}
uint32_t cacheBlockNumber() {return cacheBlockNumber_;}
#if USE_MULTIPLE_CARDS
bool cacheFlush();
bool cacheRawBlock(uint32_t blockNumber, bool dirty);
#else // USE_MULTIPLE_CARDS
static bool cacheFlush();
static bool cacheRawBlock(uint32_t blockNumber, bool dirty);
#endif // USE_MULTIPLE_CARDS
#if USE_MULTIPLE_CARDS
bool cacheFlush();
bool cacheRawBlock(uint32_t blockNumber, bool dirty);
#else // USE_MULTIPLE_CARDS
static bool cacheFlush();
static bool cacheRawBlock(uint32_t blockNumber, bool dirty);
#endif // USE_MULTIPLE_CARDS
// used by SdBaseFile write to assign cache to SD location
void cacheSetBlockNumber(uint32_t blockNumber, bool dirty) {
cacheDirty_ = dirty;
@ -206,22 +207,22 @@ class SdVolume {
}
//------------------------------------------------------------------------------
// Deprecated functions - suppress cpplint warnings with NOLINT comment
#if ALLOW_DEPRECATED_FUNCTIONS && !defined(DOXYGEN)
public:
/** \deprecated Use: bool SdVolume::init(Sd2Card* dev);
* \param[in] dev The SD card where the volume is located.
* \return true for success or false for failure.
*/
bool init(Sd2Card& dev) {return init(&dev);} // NOLINT
/** \deprecated Use: bool SdVolume::init(Sd2Card* dev, uint8_t vol);
* \param[in] dev The SD card where the volume is located.
* \param[in] part The partition to be used.
* \return true for success or false for failure.
*/
bool init(Sd2Card& dev, uint8_t part) { // NOLINT
return init(&dev, part);
}
#endif // ALLOW_DEPRECATED_FUNCTIONS
#if ALLOW_DEPRECATED_FUNCTIONS && !defined(DOXYGEN)
public:
/** \deprecated Use: bool SdVolume::init(Sd2Card* dev);
* \param[in] dev The SD card where the volume is located.
* \return true for success or false for failure.
*/
bool init(Sd2Card& dev) {return init(&dev);} // NOLINT
/** \deprecated Use: bool SdVolume::init(Sd2Card* dev, uint8_t vol);
* \param[in] dev The SD card where the volume is located.
* \param[in] part The partition to be used.
* \return true for success or false for failure.
*/
bool init(Sd2Card& dev, uint8_t part) { // NOLINT
return init(&dev, part);
}
#endif // ALLOW_DEPRECATED_FUNCTIONS
};
#endif // SdVolume
#endif
#endif // SDVOLUME_H

View file

@ -20,17 +20,20 @@
*
*/
#include <ctype.h>
#include "cardreader.h"
#include "ultralcd.h"
#include "stepper.h"
#include "language.h"
#include "Marlin.h"
#include "../inc/MarlinConfig.h"
#if ENABLED(SDSUPPORT)
#include "cardreader.h"
#include "../Marlin.h"
#include "../lcd/ultralcd.h"
#include "../module/stepper.h"
#include "../module/printcounter.h"
#include "../core/language.h"
#include <ctype.h>
#define LONGEST_FILENAME (longFilename[0] ? longFilename : filename)
CardReader::CardReader() {
@ -77,7 +80,7 @@ char *createFilename(char *buffer, const dir_t &p) { //buffer > 12characters
* LS_GetFilename - Get the filename of the file indexed by nrFile_index
* LS_SerialPrint - Print the full path and size of each file to serial output
*/
uint16_t nrFile_index;
void CardReader::lsDive(const char *prepend, SdFile parent, const char * const match/*=NULL*/) {
@ -864,12 +867,15 @@ void CardReader::updir() {
#endif // SDCARD_SORT_ALPHA
#if (ENABLED(SDCARD_SORT_ALPHA) && SDSORT_USES_RAM && SDSORT_CACHE_NAMES)
// if true - don't need to access the SD card for file names
uint16_t CardReader::get_num_Files() {return nrFiles;}
#else
uint16_t CardReader::get_num_Files() {return getnrfilenames(); }
#endif
uint16_t CardReader::get_num_Files() {
return
#if ENABLED(SDCARD_SORT_ALPHA) && SDSORT_USES_RAM && SDSORT_CACHE_NAMES
nrFiles // no need to access the SD card for filenames
#else
getnrfilenames()
#endif
;
}
void CardReader::printingHasFinished() {
stepper.synchronize();

View file

@ -23,16 +23,11 @@
#ifndef CARDREADER_H
#define CARDREADER_H
#include "MarlinConfig.h"
#if ENABLED(SDSUPPORT)
#define MAX_DIR_DEPTH 10 // Maximum folder depth
#include "SdFile.h"
#include "types.h"
#include "enum.h"
#include "../inc/MarlinConfig.h"
class CardReader {
public:
@ -70,7 +65,7 @@ public:
void setroot();
uint16_t get_num_Files();
#if ENABLED(SDCARD_SORT_ALPHA)
void presort();
void getfilename_sorted(const uint16_t nr);
@ -166,27 +161,25 @@ private:
#endif
};
extern CardReader card;
#define IS_SD_PRINTING (card.sdprinting)
#define IS_SD_FILE_OPEN (card.isFileOpen())
#if PIN_EXISTS(SD_DETECT)
#if ENABLED(SD_DETECT_INVERTED)
#define IS_SD_INSERTED (READ(SD_DETECT_PIN) != 0)
#define IS_SD_INSERTED (READ(SD_DETECT_PIN) == HIGH)
#else
#define IS_SD_INSERTED (READ(SD_DETECT_PIN) == 0)
#define IS_SD_INSERTED (READ(SD_DETECT_PIN) == LOW)
#endif
#else
//No card detect line? Assume the card is inserted.
#define IS_SD_INSERTED true
#endif
#if ENABLED(SDSUPPORT)
#define IS_SD_PRINTING (card.sdprinting)
#define IS_SD_FILE_OPEN (card.isFileOpen())
#else
#define IS_SD_PRINTING (false)
#define IS_SD_FILE_OPEN (false)
#endif
#define IS_SD_PRINTING (false)
#define IS_SD_FILE_OPEN (false)
extern CardReader card;
#endif // SDSUPPORT
#endif // __CARDREADER_H
#endif // CARDREADER_H