Skip to content

Commit

Permalink
[#123] Add new shared buffers for the SD card and FatFS
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidZemon committed Apr 29, 2017
1 parent eb8f688 commit f9e5c94
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 42 deletions.
10 changes: 2 additions & 8 deletions Examples/PropWare_FileReader/FileReader_Demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,9 @@ using PropWare::BlockStorage;
int main() {
const SD driver;
FatFS filesystem(driver);
uint8_t dataBuffer[driver.get_sector_size()];
filesystem.mount(dataBuffer);
filesystem.mount();

BlockStorage::MetaData bufferMetaData;
BlockStorage::Buffer buffer = {
buf: dataBuffer,
meta: &bufferMetaData
};
FatFileReader reader(filesystem, "fat_test.txt", buffer);
FatFileReader reader(filesystem, "fat_test.txt");
reader.open();

while (!reader.eof())
Expand Down
1 change: 0 additions & 1 deletion Examples/PropWare_FileWriter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ cmake_minimum_required(VERSION 3.3)
find_package(PropWare REQUIRED)

set(BOARD activityboard)
set(MODEL cmm)

project(FileWriter_Demo)

Expand Down
10 changes: 2 additions & 8 deletions Examples/PropWare_FileWriter/FileWriter_Demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,16 @@ int main() {
const SD driver;
FatFS filesystem(driver);

uint8_t readBufferData[driver.get_sector_size()];
BlockStorage::MetaData readMetaData;
BlockStorage::Buffer readBuffer = {
buf: readBufferData,
meta: &readMetaData
};
uint8_t writeBufferData[driver.get_sector_size()];
BlockStorage::MetaData writeMetaData;
BlockStorage::Buffer writeBuffer = {
buf: writeBufferData,
meta: &writeMetaData
};

filesystem.mount(readBufferData);
filesystem.mount();

FatFileReader reader(filesystem, "fat_test.txt", readBuffer);
FatFileReader reader(filesystem, "fat_test.txt");
FatFileWriter writer(filesystem, "new2.txt", writeBuffer);

reader.open();
Expand Down
1 change: 1 addition & 0 deletions PropWare/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ set(PROPWARE_SOURCES
${CMAKE_CURRENT_LIST_DIR}/memory/blockstorage.h
${CMAKE_CURRENT_LIST_DIR}/memory/eeprom.h
${CMAKE_CURRENT_LIST_DIR}/memory/sd.h
${CMAKE_CURRENT_LIST_DIR}/memory/sharedbuffers.cpp
${CMAKE_CURRENT_LIST_DIR}/motor/stepper.h
${CMAKE_CURRENT_LIST_DIR}/sensor/analog/mcp3xxx.h
${CMAKE_CURRENT_LIST_DIR}/sensor/analog/pcf8591.h
Expand Down
5 changes: 4 additions & 1 deletion PropWare/filesystem/fat/fatfilereader.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

namespace PropWare {

extern BlockStorage::Buffer SHARED_BUFFER;

/**
* @brief Read a file on a FAT 16 or FAT 32 storage device
*
Expand Down Expand Up @@ -88,7 +90,8 @@ class FatFileReader : virtual public FatFile, virtual public FileReader {
* @param[in] logger This is only used for printing debug statements. Use of the logger is limited
* such that all references will be optimized out in normal application code
*/
FatFileReader (FatFS &fs, const char name[], BlockStorage::Buffer &buffer, const Printer &logger = pwOut)
FatFileReader (FatFS &fs, const char name[], BlockStorage::Buffer &buffer = SHARED_BUFFER,
const Printer &logger = pwOut)
: File(fs, name, buffer, logger),
FatFile(fs, name, buffer, logger),
FileReader(fs, name, buffer, logger) {
Expand Down
5 changes: 4 additions & 1 deletion PropWare/filesystem/fat/fatfilewriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

namespace PropWare {

extern BlockStorage::Buffer SHARED_BUFFER;

/**
* @brief Concrete class for writing or modifying a FAT 16/32 file
*/
Expand All @@ -46,7 +48,8 @@ class FatFileWriter : public virtual FatFile, public virtual FileWriter {
* @param[in] logger This is only used for printing debug statements. Use of the logger is limited
* such that all references will be optimized out in normal application code
*/
FatFileWriter (FatFS &fs, const char name[], BlockStorage::Buffer &buffer, const Printer &logger = pwOut)
FatFileWriter (FatFS &fs, const char name[], BlockStorage::Buffer &buffer = SHARED_BUFFER,
const Printer &logger = pwOut)
: File(fs, name, buffer, logger),
FatFile(fs, name, buffer, logger),
FileWriter(fs, name, buffer, logger) {
Expand Down
48 changes: 25 additions & 23 deletions PropWare/filesystem/fat/fatfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@

namespace PropWare {

extern uint8_t HALF_K_DATA_BUFFER1[];
extern uint8_t HALF_K_DATA_BUFFER2[];

/**
* FAT 16/32 filesystem driver - can be used with SD cards or any other PropWare::BlockStorage device
*/
Expand Down Expand Up @@ -62,16 +65,22 @@ class FatFS : public Filesystem {
/**
* @brief Constructor
*
* @param[in] *driver Address of a the driver which is capable of reading the physical hardware. Commonly,
* this would be an instance of PropWare::SD, but one could potentially write a driver
* for a floppy disk or CD driver or any other block storage device
* @param[in] *logger Useful for debugging, a logger can be given to help determine when something goes
* wrong. All code using the logger will be optimized out by GCC so long as you only
* call public method
* @param[in] *driver Address of a the driver which is capable of reading the physical hardware.
* Commonly, this would be an instance of PropWare::SD, but one could
* potentially write a driver for a floppy disk or CD driver or any other block
* storage device
* @param[in] fatBuffer[] Byte array that can be used to store blocks of the file allocation table (FAT)
* used during mounting, file searching, and file expansion.
* @param[in] *logger Useful for debugging, a logger can be given to help determine when something
* goes wrong. All code using the logger will be optimized out by GCC so long as
* you only call public method
*
* @warning The default value for the `buffer[]` parameter is only valid when using a SD card or other
* `PropWare::BlockStorage` driver with a block size of 512-bytes or less.
*/
FatFS (const BlockStorage &driver, const Printer &logger = pwOut)
FatFS (const BlockStorage &driver, uint8_t fatBuffer[] = HALF_K_DATA_BUFFER1, const Printer &logger = pwOut)
: Filesystem(driver, logger),
m_fat(NULL),
m_fat(fatBuffer),
m_fatMod(false) {
}

Expand All @@ -87,8 +96,11 @@ class FatFS : public Filesystem {

/**
* @see PropWare::Filesystem::mount
*
* @warning The default value for the `buffer[]` parameter is only valid when using a SD card or other
* `PropWare::BlockStorage` driver with a block size of 512-bytes or less.
*/
PropWare::ErrorCode mount (uint8_t buffer[], const uint8_t partition = 0) {
PropWare::ErrorCode mount (uint8_t buffer[] = HALF_K_DATA_BUFFER2, const uint8_t partition = 0) {
PropWare::ErrorCode err;

if (this->m_mounted)
Expand All @@ -101,9 +113,6 @@ class FatFS : public Filesystem {
this->m_fatMod = false;
this->m_nextFileId = 0;

// Allocate buffer for the FAT
if (NULL == this->m_fat)
this->m_fat = (uint8_t *) malloc(this->m_sectorSize);
this->m_dirMeta.name = "Current working directory";

// Excellent information on determining FAT type can be found on page 14 of the following document,
Expand All @@ -125,17 +134,10 @@ class FatFS : public Filesystem {
* @see PropWare::Filesystem::unmount
*/
PropWare::ErrorCode unmount () {
if (this->m_mounted) {
PropWare::ErrorCode err;

if (NULL != this->m_fat) {
check_errors(this->flush_fat());
free(this->m_fat);
this->m_fat = NULL;
}
}

return NO_ERROR;
if (this->m_mounted)
return this->flush_fat();
else
return NO_ERROR;
}

/**
Expand Down
41 changes: 41 additions & 0 deletions PropWare/memory/sharedbuffers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @file PropWare/memory/sd.cpp
*
* @author David Zemon
*
* @copyright
* The MIT License (MIT)<br>
* <br>Copyright (c) 2013 David Zemon<br>
* <br>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:<br>
* <br>The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.<br>
* <br>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.
*/

#include <PropWare/PropWare.h>
#include <PropWare/memory/blockstorage.h>

using PropWare::BlockStorage;

namespace PropWare {

uint8_t HALF_K_DATA_BUFFER1[512];
uint8_t HALF_K_DATA_BUFFER2[512];
BlockStorage::MetaData SHARED_BUFFER_METADATA;
BlockStorage::Buffer SHARED_BUFFER = {
buf: HALF_K_DATA_BUFFER2,
meta: &SHARED_BUFFER_METADATA
};

}

0 comments on commit f9e5c94

Please sign in to comment.