diff options
-rw-r--r-- | src/Configuration.h | 2 | ||||
-rw-r--r-- | src/GCodes/GCodes.cpp | 17 | ||||
-rw-r--r-- | src/GCodes/GCodes2.cpp | 17 | ||||
-rw-r--r-- | src/Linux/DataTransfer.cpp | 58 | ||||
-rw-r--r-- | src/Linux/DataTransfer.h | 2 | ||||
-rw-r--r-- | src/Storage/CRC16.cpp | 72 | ||||
-rw-r--r-- | src/Storage/CRC16.h | 37 | ||||
-rw-r--r-- | src/Storage/CRC32.cpp | 13 | ||||
-rw-r--r-- | src/Storage/CRC32.h | 10 | ||||
-rw-r--r-- | src/Storage/FileStore.cpp | 417 | ||||
-rw-r--r-- | src/bossa/Flasher.cpp | 15 |
11 files changed, 343 insertions, 317 deletions
diff --git a/src/Configuration.h b/src/Configuration.h index 127c30a8..66d58da0 100644 --- a/src/Configuration.h +++ b/src/Configuration.h @@ -371,7 +371,5 @@ static_assert(MaxExpectedWebDirFilenameLength + strlen(WEB_DIR) + strlen(".gz") // List defaults constexpr char LIST_SEPARATOR = ':'; -constexpr char FILE_LIST_SEPARATOR = ','; -constexpr char FILE_LIST_BRACKET = '"'; #endif diff --git a/src/GCodes/GCodes.cpp b/src/GCodes/GCodes.cpp index f2db64c1..a7d9800b 100644 --- a/src/GCodes/GCodes.cpp +++ b/src/GCodes/GCodes.cpp @@ -3593,24 +3593,20 @@ void GCodes::HandleReply(GCodeBuffer& gb, OutputBuffer *reply) noexcept { platform.Message(type, "Begin file list\n"); platform.Message(type, reply); - platform.Message(type, "End file list\n"); - platform.Message(type, response); - platform.Message(type, "\n"); + platform.MessageF(type, "End file list\n%s\n", response); return; } if (gb.GetCommandLetter() == 'M' && gb.GetCommandNumber() == 28) { - platform.Message(type, response); - platform.Message(type, "\n"); + platform.MessageF(type, "%s\n", response); platform.Message(type, reply); return; } if (gb.GetCommandLetter() =='M' && (gb.GetCommandNumber() == 105 || gb.GetCommandNumber() == 998)) { - platform.Message(type, response); - platform.Message(type, " "); + platform.MessageF(type, "%s ", response); platform.Message(type, reply); return; } @@ -3618,9 +3614,7 @@ void GCodes::HandleReply(GCodeBuffer& gb, OutputBuffer *reply) noexcept if (reply->Length() != 0 && !gb.IsDoingFileMacro()) { platform.Message(type, reply); - platform.Message(type, "\n"); - platform.Message(type, response); - platform.Message(type, "\n"); + platform.MessageF(type, "\n%s\n", response); } else if (reply->Length() != 0) { @@ -3629,8 +3623,7 @@ void GCodes::HandleReply(GCodeBuffer& gb, OutputBuffer *reply) noexcept else { OutputBuffer::ReleaseAll(reply); - platform.Message(type, response); - platform.Message(type, "\n"); + platform.MessageF(type, "%s\n", response); } return; diff --git a/src/GCodes/GCodes2.cpp b/src/GCodes/GCodes2.cpp index 00f27e79..603bf693 100644 --- a/src/GCodes/GCodes2.cpp +++ b/src/GCodes/GCodes2.cpp @@ -749,25 +749,22 @@ bool GCodes::HandleMcode(GCodeBuffer& gb, const StringRef& reply) THROWS(GCodeEx FileInfo fileInfo; if (MassStorage::FindFirst(dir.c_str(), fileInfo)) { - // iterate through all entries and append each file name - do { + // Iterate through all entries and append each file name + bool first = true; + do + { if (encapsulateList) { - outBuf->catf("%c%s%c%c", FILE_LIST_BRACKET, fileInfo.fileName.c_str(), FILE_LIST_BRACKET, FILE_LIST_SEPARATOR); + outBuf->catf((first) ? "\"%s\"" : ",\"%s\"", fileInfo.fileName.c_str()); + first = false; } else { outBuf->catf("%s\n", fileInfo.fileName.c_str()); } } while (MassStorage::FindNext(fileInfo)); - - if (encapsulateList) - { - // remove the last separator - (*outBuf)[outBuf->Length() - 1] = 0; - } } - else + else if (!encapsulateList) { outBuf->cat("NONE\n"); } diff --git a/src/Linux/DataTransfer.cpp b/src/Linux/DataTransfer.cpp index 4ab2b8a4..1de31012 100644 --- a/src/Linux/DataTransfer.cpp +++ b/src/Linux/DataTransfer.cpp @@ -9,6 +9,7 @@ #if HAS_LINUX_INTERFACE +#include <Storage/CRC16.h> #include <algorithm> #if defined(DUET_NG) && defined(USE_SBC) @@ -762,7 +763,7 @@ bool DataTransfer::IsReady() noexcept break; } - const uint16_t checksum = CRC16(reinterpret_cast<const char *>(&rxHeader), sizeof(TransferHeader) - sizeof(uint16_t)); + const uint16_t checksum = CalcCRC16(reinterpret_cast<const char *>(&rxHeader), sizeof(TransferHeader) - sizeof(uint16_t)); if (rxHeader.checksumHeader != checksum) { if (reprap.Debug(moduleLinuxInterface)) @@ -847,7 +848,7 @@ bool DataTransfer::IsReady() noexcept break; } - const uint16_t checksum = CRC16(rxBuffer, rxHeader.dataLength); + const uint16_t checksum = CalcCRC16(rxBuffer, rxHeader.dataLength); if (rxHeader.checksumData != checksum) { if (reprap.Debug(moduleLinuxInterface)) @@ -943,8 +944,8 @@ void DataTransfer::StartNextTransfer() noexcept txHeader.numPackets = packetId; txHeader.sequenceNumber++; txHeader.dataLength = txPointer; - txHeader.checksumData = CRC16(txBuffer, txPointer); - txHeader.checksumHeader = CRC16(reinterpret_cast<const char *>(&txHeader), sizeof(TransferHeader) - sizeof(uint16_t)); + txHeader.checksumData = CalcCRC16(txBuffer, txPointer); + txHeader.checksumHeader = CalcCRC16(reinterpret_cast<const char *>(&txHeader), sizeof(TransferHeader) - sizeof(uint16_t)); // Begin SPI transfer ExchangeHeader(); @@ -1398,52 +1399,11 @@ template<typename T> T *DataTransfer::WriteDataHeader() noexcept return header; } -uint16_t DataTransfer::CRC16(const char *buffer, size_t length) const noexcept +uint16_t DataTransfer::CalcCRC16(const char *buffer, size_t length) const noexcept { - static const uint16_t crc16_table[] = - { - 0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241, - 0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440, - 0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40, - 0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841, - 0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40, - 0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41, - 0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641, - 0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040, - 0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240, - 0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441, - 0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41, - 0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840, - 0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41, - 0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40, - 0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640, - 0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041, - 0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240, - 0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441, - 0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41, - 0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840, - 0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41, - 0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40, - 0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640, - 0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041, - 0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241, - 0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440, - 0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40, - 0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841, - 0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40, - 0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41, - 0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641, - 0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040 - }; - - uint16_t Crc = 65535; - for (size_t i = 0; i < length; i++) - { - const uint16_t x = (uint16_t)(Crc ^ buffer[i]); - Crc = (uint16_t)((Crc >> 8) ^ crc16_table[x & 0x00FF]); - } - - return Crc; + CRC16 crc; + crc.Update(buffer, length); + return crc.Get(); } #endif diff --git a/src/Linux/DataTransfer.h b/src/Linux/DataTransfer.h index 1b7964e5..ee0fd628 100644 --- a/src/Linux/DataTransfer.h +++ b/src/Linux/DataTransfer.h @@ -118,7 +118,7 @@ private: void ExchangeResponse(uint32_t response) noexcept; void ExchangeData() noexcept; void ResetTransfer(bool ownRequest) noexcept; - uint16_t CRC16(const char *buffer, size_t length) const noexcept; + uint16_t CalcCRC16(const char *buffer, size_t length) const noexcept; template<typename T> const T *ReadDataHeader() noexcept; diff --git a/src/Storage/CRC16.cpp b/src/Storage/CRC16.cpp new file mode 100644 index 00000000..876064a0 --- /dev/null +++ b/src/Storage/CRC16.cpp @@ -0,0 +1,72 @@ +/* + * CRC16.cpp + * + * Created on: 4 Dec 2020 + * Author: David + */ + +#include "CRC16.h" + +static const uint16_t crc16_table[] = +{ + 0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241, + 0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440, + 0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40, + 0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841, + 0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40, + 0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41, + 0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641, + 0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040, + 0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240, + 0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441, + 0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41, + 0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840, + 0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41, + 0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40, + 0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640, + 0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041, + 0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240, + 0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441, + 0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41, + 0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840, + 0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41, + 0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40, + 0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640, + 0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041, + 0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241, + 0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440, + 0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40, + 0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841, + 0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40, + 0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41, + 0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641, + 0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040 +}; + +CRC16::CRC16() noexcept +{ + Reset(); +} + +CRC16::~CRC16() +{ +} + +void CRC16::Update(char c) noexcept +{ + crc = (uint16_t)((crc >> 8) ^ crc16_table[(crc ^ c) & 0x00FF]); +} + +void CRC16::Update(const char *c, size_t len) noexcept +{ + //TODO speed this up, by unrolling the loop and/or using slicing-by-4 and/or using hardware CRC on the SAME5x + uint16_t locCrc = crc; + while (len != 0) + { + locCrc = (uint16_t)((locCrc >> 8) ^ crc16_table[(locCrc ^ *c++) & 0x00FF]); + --len; + } + crc = locCrc; +} + +// End diff --git a/src/Storage/CRC16.h b/src/Storage/CRC16.h new file mode 100644 index 00000000..b74a7b6b --- /dev/null +++ b/src/Storage/CRC16.h @@ -0,0 +1,37 @@ +/* + * CRC16.h + * + * Created on: 4 Dec 2020 + * Author: David + */ + +#ifndef SRC_STORAGE_CRC16_H_ +#define SRC_STORAGE_CRC16_H_ + +#include <RepRapFirmware.h> + +class CRC16 +{ +private: + uint16_t crc; + +public: + CRC16() noexcept; + ~CRC16(); + void Update(char c) noexcept; + void Update(const char *c, size_t len) noexcept; + void Reset(uint16_t initialValue = 0xFFFF) noexcept; + uint16_t Get() const noexcept; +}; + +inline uint16_t CRC16::Get() const noexcept +{ + return crc; +} + +inline void CRC16::Reset(uint16_t initialValue) noexcept +{ + crc = initialValue; +} + +#endif /* SRC_STORAGE_CRC16_H_ */ diff --git a/src/Storage/CRC32.cpp b/src/Storage/CRC32.cpp index a3760fff..c2f6f43b 100644 --- a/src/Storage/CRC32.cpp +++ b/src/Storage/CRC32.cpp @@ -166,6 +166,8 @@ CRC32::CRC32() noexcept Reset(); } +CRC32::~CRC32() { } + void CRC32::Update(char c) noexcept { crc = (CRC_32_TAB[(crc ^ c) & 0xFF] ^ (crc >> 8)); @@ -173,7 +175,7 @@ void CRC32::Update(char c) noexcept // A note on CRC algorithms on ARM: // Original algorithm (1 byte per loop iteration, 1K table): 7 instructions, 11 clocks (11 clocks/byte) -// Algorithm currently used on non-SAME70 processors (4 bytes per loop iteration, 1K table): 19 instructions, 26 clocks (6.5 clocks/byte) +// Algorithm currently used on non-SAME70/SAME5x processors (4 bytes per loop iteration, 1K table): 19 instructions, 26 clocks (6.5 clocks/byte) // Slicing-by-4 using 1 dword per loop iteration: 15 instructions, 18 clocks (4.5 clocks/byte) // Slicing-by-4 using 1 quadword per loop iteration: 28 instructions, 31 clocks (3.875 clocks/byte) void CRC32::Update(const char *s, size_t len) noexcept @@ -193,7 +195,7 @@ void CRC32::Update(const char *s, size_t len) noexcept const char * const endAligned = s + ((end - s) & ~7); while (s != endAligned) { - // Slicing-by-4 algorithm, 2 quadwords at a time + // Slicing-by-4 algorithm, 2 dwords at a time const uint32_t data0 = *reinterpret_cast<const uint32_t*>(s) ^ locCrc; locCrc = CRC_32_TAB[(data0 >> 24) & 0xFF] ^ CRC_32_TAB1[(data0 >> 16) & 0xFF] ^ CRC_32_TAB2[(data0 >> 8) & 0xFF] ^ CRC_32_TAB3[data0 & 0xFF]; const uint32_t data1 = *reinterpret_cast<const uint32_t*>(s + 4) ^ locCrc; @@ -213,7 +215,7 @@ void CRC32::Update(const char *s, size_t len) noexcept } #endif - // Process up to 7 (SAME70) or 3 (others) bytes at the end + // Process up to 7 (SAME70/SAME5x) or 3 (others) bytes at the end while (s != end) { locCrc = (CRC_32_TAB[(locCrc ^ *s++) & 0xFF] ^ (locCrc >> 8)); @@ -222,7 +224,4 @@ void CRC32::Update(const char *s, size_t len) noexcept crc = locCrc; } -void CRC32::Reset() noexcept -{ - crc = 0xffffffff; -} +// End diff --git a/src/Storage/CRC32.h b/src/Storage/CRC32.h index 1c5fed8c..892f9c5a 100644 --- a/src/Storage/CRC32.h +++ b/src/Storage/CRC32.h @@ -10,9 +10,10 @@ private: public: CRC32() noexcept; + ~CRC32(); void Update(char c) noexcept; - void Update(const char *c, size_t len) noexcept; - void Reset() noexcept; + void Update(const char *s, size_t len) noexcept; + void Reset(uint32_t initialValue = 0xFFFFFFFF) noexcept; uint32_t Get() const noexcept; }; @@ -21,4 +22,9 @@ inline uint32_t CRC32::Get() const noexcept return ~crc; } +inline void CRC32::Reset(uint32_t initialValue) noexcept +{ + crc = initialValue; +} + #endif diff --git a/src/Storage/FileStore.cpp b/src/Storage/FileStore.cpp index 11bfa09e..516065cf 100644 --- a/src/Storage/FileStore.cpp +++ b/src/Storage/FileStore.cpp @@ -14,9 +14,11 @@ # include "Libraries/Fatfs/diskio.h" # include "Movement/StepTimer.h" #endif + #if HAS_LINUX_INTERFACE # include "Linux/LinuxInterface.h" #endif + FileStore::FileStore() noexcept : #if HAS_MASS_STORAGE @@ -48,48 +50,36 @@ void FileStore::Init() noexcept #endif } -#if HAS_MASS_STORAGE - -// Invalidate the file if it uses the specified FATFS object -bool FileStore::Invalidate(const FATFS *fs, bool doClose) noexcept +// Open a local file (for example on an SD card). +// This is protected - only Platform can access it. +bool FileStore::Open(const char* filePath, OpenMode mode, uint32_t preAllocSize) noexcept { - if (file.obj.fs == fs) +#if HAS_LINUX_INTERFACE + if (reprap.UsingLinuxInterface()) { - if (doClose) + if (mode != OpenMode::read) { - (void)ForceClose(); + REPORT_INTERNAL_ERROR; + return false; } - else + //TODO allocating dynamic memory here isn't nice because of possible memory fragmentation. Try to avoid it in future. + absoluteFilename = new char[strlen(filePath)+1]; + strcpy(absoluteFilename, filePath); + char dummyBuf[1]; + uint32_t dummyLen = 0; + if (!reprap.GetLinuxInterface().GetFileChunk(absoluteFilename, 0, dummyBuf, dummyLen, length)) { - file.obj.fs = nullptr; - if (writeBuffer != nullptr) - { - MassStorage::ReleaseWriteBuffer(writeBuffer); - writeBuffer = nullptr; - } + delete absoluteFilename; + absoluteFilename = nullptr; + return false; } - usageMode = FileUseMode::invalidated; + usageMode = FileUseMode::readOnly; + offset = 0; return true; } - return false; -} - -// Return true if the file is open on the specified file system -bool FileStore::IsOpenOn(const FATFS *fs) const noexcept -{ - return openCount != 0 && file.obj.fs == fs; -} -#endif - -// Open a local file (for example on an SD card). -// This is protected - only Platform can access it. -bool FileStore::Open(const char* filePath, OpenMode mode, uint32_t preAllocSize) noexcept -{ -#if HAS_LINUX_INTERFACE - if (!reprap.UsingLinuxInterface()) #endif +#if HAS_MASS_STORAGE { -# if HAS_MASS_STORAGE const bool writing = (mode == OpenMode::write || mode == OpenMode::writeWithCrc || mode == OpenMode::append); writeBuffer = nullptr; @@ -147,157 +137,80 @@ bool FileStore::Open(const char* filePath, OpenMode mode, uint32_t preAllocSize) # endif reprap.VolumesUpdated(); return true; + } # else - return false; + return false; # endif - } +} + +// This may be called from an ISR, in which case we need to defer the close +bool FileStore::Close() noexcept +{ #if HAS_LINUX_INTERFACE - else + if (reprap.UsingLinuxInterface()) { - if (mode != OpenMode::read) - { - reprap.GetPlatform().Message(WarningMessage, "When using LinuxInterface files can only be opened for read access.\n"); - return false; - } - absoluteFilename = new char[strlen(filePath)+1]; - strcpy(absoluteFilename, filePath); - char dummyBuf[1]; - uint32_t dummyLen = 0; - if (!reprap.GetLinuxInterface().GetFileChunk(absoluteFilename, 0, dummyBuf, dummyLen, length)) - { - delete absoluteFilename; - absoluteFilename = nullptr; - return false; - } - usageMode = FileUseMode::readOnly; offset = 0; + length = 0; + delete absoluteFilename; + absoluteFilename = nullptr; + usageMode = FileUseMode::free; return true; } #endif -} - #if HAS_MASS_STORAGE -void FileStore::Duplicate() noexcept -{ switch (usageMode) { case FileUseMode::free: - REPORT_INTERNAL_ERROR; - break; + if (!inInterrupt()) + { + REPORT_INTERNAL_ERROR; + } + return false; case FileUseMode::readOnly: case FileUseMode::readWrite: { const irqflags_t flags = cpu_irq_save(); - ++openCount; - cpu_irq_restore(flags); + if (openCount > 1) + { + --openCount; + cpu_irq_restore(flags); + return true; + } + else if (inInterrupt()) + { + closeRequested = true; + cpu_irq_restore(flags); + return true; + } + else + { + cpu_irq_restore(flags); + return ForceClose(); + } } - break; case FileUseMode::invalidated: default: - break; - } -} -#endif - -// This may be called from an ISR, in which case we need to defer the close -bool FileStore::Close() noexcept -{ -#if HAS_LINUX_INTERFACE - if (!reprap.UsingLinuxInterface()) -#endif - { -#if HAS_MASS_STORAGE - switch (usageMode) { - case FileUseMode::free: - if (!inInterrupt()) + const irqflags_t flags = cpu_irq_save(); + if (openCount > 1) { - REPORT_INTERNAL_ERROR; + --openCount; } - return false; - - case FileUseMode::readOnly: - case FileUseMode::readWrite: - { - const irqflags_t flags = cpu_irq_save(); - if (openCount > 1) - { - --openCount; - cpu_irq_restore(flags); - return true; - } - else if (inInterrupt()) - { - closeRequested = true; - cpu_irq_restore(flags); - return true; - } - else - { - cpu_irq_restore(flags); - return ForceClose(); - } - } - - case FileUseMode::invalidated: - default: + else { - const irqflags_t flags = cpu_irq_save(); - if (openCount > 1) - { - --openCount; - } - else - { - usageMode = FileUseMode::free; - } - cpu_irq_restore(flags); -#endif - return true; -#if HAS_MASS_STORAGE + usageMode = FileUseMode::free; } + cpu_irq_restore(flags); + return true; } -#endif - } -#if HAS_LINUX_INTERFACE - else - { - offset = 0; - length = 0; - delete absoluteFilename; - absoluteFilename = nullptr; - usageMode = FileUseMode::free; - return true; } +#else + return true; #endif } -#if HAS_MASS_STORAGE -bool FileStore::ForceClose() noexcept -{ - bool ok = true; - if (usageMode == FileUseMode::readWrite) - { - ok = Flush(); - } - - if (writeBuffer != nullptr) - { - MassStorage::ReleaseWriteBuffer(writeBuffer); - writeBuffer = nullptr; - } - - const FRESULT fr = f_close(&file); - usageMode = FileUseMode::free; - closeRequested = false; - openCount = 0; - reprap.VolumesUpdated(); - return ok && fr == FR_OK; -} -#endif - bool FileStore::Seek(FilePosition pos) noexcept { switch (usageMode) @@ -309,17 +222,7 @@ bool FileStore::Seek(FilePosition pos) noexcept case FileUseMode::readOnly: case FileUseMode::readWrite: #if HAS_LINUX_INTERFACE - if (!reprap.UsingLinuxInterface()) -#endif - { -#if HAS_MASS_STORAGE - return f_lseek(&file, pos) == FR_OK; -#else - return false; -#endif - } -#if HAS_LINUX_INTERFACE - else + if (reprap.UsingLinuxInterface()) { const bool validTarget = offset < length; if (validTarget) @@ -329,6 +232,11 @@ bool FileStore::Seek(FilePosition pos) noexcept return validTarget; } #endif +#if HAS_MASS_STORAGE + return f_lseek(&file, pos) == FR_OK; +#else + return false; +#endif case FileUseMode::invalidated: default: @@ -339,36 +247,17 @@ bool FileStore::Seek(FilePosition pos) noexcept FilePosition FileStore::Position() const noexcept { #if HAS_LINUX_INTERFACE - if (!reprap.UsingLinuxInterface()) -#endif - { - return -#if HAS_MASS_STORAGE - (usageMode == FileUseMode::readOnly || usageMode == FileUseMode::readWrite) ? file.fptr : -#endif - 0; - } -#if HAS_LINUX_INTERFACE - else + if (reprap.UsingLinuxInterface()) { return offset; } #endif -} - #if HAS_MASS_STORAGE -uint32_t FileStore::ClusterSize() const noexcept -{ - return (usageMode == FileUseMode::readOnly || usageMode == FileUseMode::readWrite) ? file.obj.fs->csize * 512u : 1; // we divide by the cluster size so return 1 not 0 if there is an error -} + return (usageMode == FileUseMode::readOnly || usageMode == FileUseMode::readWrite) ? file.fptr : 0; +#else + return 0; #endif - -#if 0 // not currently used -bool FileStore::GoToEnd() -{ - return Seek(Length()); } -#endif FilePosition FileStore::Length() const noexcept { @@ -380,27 +269,22 @@ FilePosition FileStore::Length() const noexcept case FileUseMode::readOnly: #if HAS_LINUX_INTERFACE - if (!reprap.UsingLinuxInterface()) -#endif - { -#if HAS_MASS_STORAGE - return f_size(&file); -#else - return 0; -#endif - } -#if HAS_LINUX_INTERFACE - else + if (reprap.UsingLinuxInterface()) { return length; } #endif +#if HAS_MASS_STORAGE + return f_size(&file); +#else + return 0; +#endif case FileUseMode::readWrite: #if HAS_MASS_STORAGE return (writeBuffer != nullptr) ? f_size(&file) + writeBuffer->BytesStored() : f_size(&file); #else - return 0; + return 0; #endif case FileUseMode::invalidated: @@ -426,37 +310,33 @@ int FileStore::Read(char* extBuf, size_t nBytes) noexcept case FileUseMode::readOnly: case FileUseMode::readWrite: - { #if HAS_LINUX_INTERFACE - if (!reprap.UsingLinuxInterface()) -#endif + if (reprap.UsingLinuxInterface()) + { + uint32_t read = nBytes; + const bool success = reprap.GetLinuxInterface().GetFileChunk(absoluteFilename, offset, extBuf, read, length); + if (!success) { -#if HAS_MASS_STORAGE - UINT bytes_read; - FRESULT readStatus = f_read(&file, extBuf, nBytes, &bytes_read); - if (readStatus != FR_OK) - { - reprap.GetPlatform().MessageF(ErrorMessage, "Cannot read file, error code %d\n", (int)readStatus); + return -1; + } + offset += read; + return (int)read; + } #endif - return -1; #if HAS_MASS_STORAGE - } - return (int)bytes_read; -#endif - } -#if HAS_LINUX_INTERFACE - else + { + UINT bytes_read; + FRESULT readStatus = f_read(&file, extBuf, nBytes, &bytes_read); + if (readStatus != FR_OK) { - uint32_t read = nBytes; - const bool success = reprap.GetLinuxInterface().GetFileChunk(absoluteFilename, offset, extBuf, read, length); - if (!success) { - return -1; - } - offset += read; - return (int)read; + reprap.GetPlatform().MessageF(ErrorMessage, "Cannot read file, error code %d\n", (int)readStatus); + return -1; } -#endif + return (int)bytes_read; } +#else + return -1; +#endif case FileUseMode::invalidated: default: @@ -499,7 +379,83 @@ int FileStore::ReadLine(char* buf, size_t nBytes) noexcept return i; } -#if HAS_MASS_STORAGE +#if HAS_MASS_STORAGE // the remaining functions are only supported on local storage + +// Invalidate the file if it uses the specified FATFS object +bool FileStore::Invalidate(const FATFS *fs, bool doClose) noexcept +{ + if (file.obj.fs == fs) + { + if (doClose) + { + (void)ForceClose(); + } + else + { + file.obj.fs = nullptr; + if (writeBuffer != nullptr) + { + MassStorage::ReleaseWriteBuffer(writeBuffer); + writeBuffer = nullptr; + } + } + usageMode = FileUseMode::invalidated; + return true; + } + return false; +} + +// Return true if the file is open on the specified file system +bool FileStore::IsOpenOn(const FATFS *fs) const noexcept +{ + return openCount != 0 && file.obj.fs == fs; +} + +void FileStore::Duplicate() noexcept +{ + switch (usageMode) + { + case FileUseMode::free: + REPORT_INTERNAL_ERROR; + break; + + case FileUseMode::readOnly: + case FileUseMode::readWrite: + { + const irqflags_t flags = cpu_irq_save(); + ++openCount; + cpu_irq_restore(flags); + } + break; + + case FileUseMode::invalidated: + default: + break; + } +} + +bool FileStore::ForceClose() noexcept +{ + bool ok = true; + if (usageMode == FileUseMode::readWrite) + { + ok = Flush(); + } + + if (writeBuffer != nullptr) + { + MassStorage::ReleaseWriteBuffer(writeBuffer); + writeBuffer = nullptr; + } + + const FRESULT fr = f_close(&file); + usageMode = FileUseMode::free; + closeRequested = false; + openCount = 0; + reprap.VolumesUpdated(); + return ok && fr == FR_OK; +} + FRESULT FileStore::Store(const char *s, size_t len, size_t *bytesWritten) noexcept { if (calcCrc) @@ -638,9 +594,20 @@ bool FileStore::IsSameFile(const FIL& otherFile) const noexcept { return file.obj.fs == otherFile.obj.fs && file.dir_sect == otherFile.dir_sect && file.dir_ptr == otherFile.dir_ptr; } -#endif -#if 0 // not currently used +uint32_t FileStore::ClusterSize() const noexcept +{ + return (usageMode == FileUseMode::readOnly || usageMode == FileUseMode::readWrite) ? file.obj.fs->csize * 512u : 1; // we divide by the cluster size so return 1 not 0 if there is an error +} + +#endif // HAS_MASS_STORAGE + +#if 0 // these are not currently used + +bool FileStore::GoToEnd() +{ + return Seek(Length()); +} // Provide a cluster map for fast seeking. Needs _USE_FASTSEEK defined as 1 in conf_fatfs to make any difference. // The first element of the table must be set to the total number of 32-bit entries in the table before calling this. diff --git a/src/bossa/Flasher.cpp b/src/bossa/Flasher.cpp index fd192bf0..011849b9 100644 --- a/src/bossa/Flasher.cpp +++ b/src/bossa/Flasher.cpp @@ -35,8 +35,7 @@ # include "Linux/LinuxInterface.h" #endif -void -Flasher::erase(uint32_t foffset) THROWS(GCodeException) +void Flasher::erase(uint32_t foffset) THROWS(GCodeException) { #if ORIGINAL_BOSSA_CODE _observer.onStatus("Erase flash\n"); @@ -45,9 +44,7 @@ Flasher::erase(uint32_t foffset) THROWS(GCodeException) _flash->eraseAuto(false); } - -bool -Flasher::write(const char* filename, uint32_t& foffset) THROWS(GCodeException) +bool Flasher::write(const char* filename, uint32_t& foffset) THROWS(GCodeException) { uint32_t pageSize = _flash->pageSize(); uint32_t numPages; @@ -114,8 +111,7 @@ Flasher::write(const char* filename, uint32_t& foffset) THROWS(GCodeException) return true; } -bool -Flasher::verify(const char* filename, uint32_t& pageErrors, uint32_t& totalErrors, uint32_t& foffset) THROWS(GCodeException) +bool Flasher::verify(const char* filename, uint32_t& pageErrors, uint32_t& totalErrors, uint32_t& foffset) THROWS(GCodeException) { uint32_t pageSize = _flash->pageSize(); uint8_t bufferA[pageSize]; @@ -201,8 +197,7 @@ Flasher::verify(const char* filename, uint32_t& pageErrors, uint32_t& totalError return true; } -void -Flasher::lock(/* string& regionArg, */ bool enable) THROWS(GCodeException) +void Flasher::lock(/* string& regionArg, */ bool enable) THROWS(GCodeException) { #if ORIGINAL_BOSSA_CODE if (regionArg.empty()) @@ -235,3 +230,5 @@ Flasher::lock(/* string& regionArg, */ bool enable) THROWS(GCodeException) } #endif } + +// End |