Add support for SpiNorFlash and FS (#30)
The external SPI flash is implemented as a 4MB on the local filesystem. This allows the FS (littleFS) and settings to work properly. Remove the simulated `FS.h` and `FS.cpp`, because we can now use the files from InfiniTime directly as the heavy lifting is done in the simulated `SpiNorFlash.h` and cpp files. `SpiNorFlash.h` provides read and write functions with `uint8_t` buffer, but `fs::fstream` expects `char` buffer. Use `reinterpret_cast` and check if by any chance the `char` type on a platform is implemented with more than one byte. Then the `reinterpret_cast<char *>(buffer)` would change the meaning of the `size` parameter, which could lead to garbage data. Co-authored-by: Reinhold Gschweicher <pyro4hell@gmail.com>
This commit is contained in:
@@ -1,12 +1,27 @@
|
||||
#include "drivers/SpiNorFlash.h"
|
||||
#include <hal/nrf_gpio.h>
|
||||
#include <libraries/delay/nrf_delay.h>
|
||||
#include <libraries/log/nrf_log.h>
|
||||
#include "drivers/Spi.h"
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
|
||||
using namespace Pinetime::Drivers;
|
||||
|
||||
SpiNorFlash::SpiNorFlash(Spi& spi) : spi {spi} {
|
||||
SpiNorFlash::SpiNorFlash(const std::string& memoryFilePath) : memoryFilePath{memoryFilePath} {
|
||||
namespace fs = std::filesystem;
|
||||
fs::path f{ memoryFilePath };
|
||||
if (fs::exists(f)) {
|
||||
memoryFile = std::fstream(memoryFilePath, std::ios::binary | std::fstream::in | std::fstream::out);
|
||||
} else {
|
||||
memoryFile = std::fstream(memoryFilePath, std::ios::trunc | std::ios::binary | std::fstream::in | std::fstream::out);
|
||||
memoryFile.seekp(memorySize - 1);
|
||||
memoryFile.write("", 1);
|
||||
}
|
||||
}
|
||||
SpiNorFlash::~SpiNorFlash() {
|
||||
if (memoryFile.is_open()) {
|
||||
memoryFile.close();
|
||||
}
|
||||
}
|
||||
|
||||
void SpiNorFlash::Init() {
|
||||
@@ -19,126 +34,61 @@ void SpiNorFlash::Uninit() {
|
||||
}
|
||||
|
||||
void SpiNorFlash::Sleep() {
|
||||
auto cmd = static_cast<uint8_t>(Commands::DeepPowerDown);
|
||||
spi.Write(&cmd, sizeof(uint8_t));
|
||||
NRF_LOG_INFO("[SpiNorFlash] Sleep")
|
||||
}
|
||||
|
||||
void SpiNorFlash::Wakeup() {
|
||||
// send Commands::ReleaseFromDeepPowerDown then 3 dummy bytes before reading Device ID
|
||||
// static constexpr uint8_t cmdSize = 4;
|
||||
// uint8_t cmd[cmdSize] = {static_cast<uint8_t>(Commands::ReleaseFromDeepPowerDown), 0x01, 0x02, 0x03};
|
||||
// uint8_t id = 0;
|
||||
// spi.Read(reinterpret_cast<uint8_t*>(&cmd), cmdSize, &id, 1);
|
||||
// auto devId = device_id = ReadIdentificaion();
|
||||
// if (devId.type != device_id.type) {
|
||||
// NRF_LOG_INFO("[SpiNorFlash] ID on Wakeup: Failed");
|
||||
// } else {
|
||||
// NRF_LOG_INFO("[SpiNorFlash] ID on Wakeup: %d", id);
|
||||
// }
|
||||
NRF_LOG_INFO("[SpiNorFlash] Wakeup")
|
||||
}
|
||||
|
||||
SpiNorFlash::Identification SpiNorFlash::ReadIdentificaion() {
|
||||
// auto cmd = static_cast<uint8_t>(Commands::ReadIdentification);
|
||||
// Identification identification;
|
||||
// spi.Read(&cmd, 1, reinterpret_cast<uint8_t*>(&identification), sizeof(Identification));
|
||||
// return identification;
|
||||
return {};
|
||||
}
|
||||
|
||||
uint8_t SpiNorFlash::ReadStatusRegister() {
|
||||
auto cmd = static_cast<uint8_t>(Commands::ReadStatusRegister);
|
||||
uint8_t status;
|
||||
spi.Read(&cmd, sizeof(cmd), &status, sizeof(uint8_t));
|
||||
return status;
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool SpiNorFlash::WriteInProgress() {
|
||||
// return (ReadStatusRegister() & 0x01u) == 0x01u;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool SpiNorFlash::WriteEnabled() {
|
||||
// return (ReadStatusRegister() & 0x02u) == 0x02u;
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t SpiNorFlash::ReadConfigurationRegister() {
|
||||
auto cmd = static_cast<uint8_t>(Commands::ReadConfigurationRegister);
|
||||
uint8_t status;
|
||||
spi.Read(&cmd, sizeof(cmd), &status, sizeof(uint8_t));
|
||||
return status;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void SpiNorFlash::Read(uint32_t address, uint8_t* buffer, size_t size) {
|
||||
static constexpr uint8_t cmdSize = 4;
|
||||
uint8_t cmd[cmdSize] = {static_cast<uint8_t>(Commands::Read), (uint8_t) (address >> 16U), (uint8_t) (address >> 8U), (uint8_t) address};
|
||||
spi.Read(reinterpret_cast<uint8_t*>(&cmd), cmdSize, buffer, size);
|
||||
static_assert(sizeof(uint8_t) == sizeof(char));
|
||||
memoryFile.seekp(address);
|
||||
memoryFile.read(reinterpret_cast<char *>(buffer), size);
|
||||
}
|
||||
|
||||
void SpiNorFlash::WriteEnable() {
|
||||
auto cmd = static_cast<uint8_t>(Commands::WriteEnable);
|
||||
spi.Read(&cmd, sizeof(cmd), nullptr, 0);
|
||||
|
||||
}
|
||||
|
||||
void SpiNorFlash::SectorErase(uint32_t sectorAddress) {
|
||||
// static constexpr uint8_t cmdSize = 4;
|
||||
// uint8_t cmd[cmdSize] = {static_cast<uint8_t>(Commands::SectorErase),
|
||||
// (uint8_t) (sectorAddress >> 16U),
|
||||
// (uint8_t) (sectorAddress >> 8U),
|
||||
// (uint8_t) sectorAddress};
|
||||
//
|
||||
// WriteEnable();
|
||||
// while (!WriteEnabled())
|
||||
// vTaskDelay(1);
|
||||
//
|
||||
// spi.Read(reinterpret_cast<uint8_t*>(&cmd), cmdSize, nullptr, 0);
|
||||
//
|
||||
// while (WriteInProgress())
|
||||
// vTaskDelay(1);
|
||||
|
||||
}
|
||||
|
||||
uint8_t SpiNorFlash::ReadSecurityRegister() {
|
||||
auto cmd = static_cast<uint8_t>(Commands::ReadSecurityRegister);
|
||||
uint8_t status;
|
||||
spi.Read(&cmd, sizeof(cmd), &status, sizeof(uint8_t));
|
||||
return status;
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool SpiNorFlash::ProgramFailed() {
|
||||
// return (ReadSecurityRegister() & 0x20u) == 0x20u;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool SpiNorFlash::EraseFailed() {
|
||||
// return (ReadSecurityRegister() & 0x40u) == 0x40u;
|
||||
return false;
|
||||
}
|
||||
|
||||
void SpiNorFlash::Write(uint32_t address, const uint8_t* buffer, size_t size) {
|
||||
// static constexpr uint8_t cmdSize = 4;
|
||||
//
|
||||
// size_t len = size;
|
||||
// uint32_t addr = address;
|
||||
// const uint8_t* b = buffer;
|
||||
// while (len > 0) {
|
||||
// uint32_t pageLimit = (addr & ~(pageSize - 1u)) + pageSize;
|
||||
// uint32_t toWrite = pageLimit - addr > len ? len : pageLimit - addr;
|
||||
//
|
||||
// uint8_t cmd[cmdSize] = {static_cast<uint8_t>(Commands::PageProgram), (uint8_t) (addr >> 16U), (uint8_t) (addr >> 8U), (uint8_t) addr};
|
||||
//
|
||||
// WriteEnable();
|
||||
// while (!WriteEnabled())
|
||||
// vTaskDelay(1);
|
||||
//
|
||||
// spi.WriteCmdAndBuffer(cmd, cmdSize, b, toWrite);
|
||||
//
|
||||
// while (WriteInProgress())
|
||||
// vTaskDelay(1);
|
||||
//
|
||||
// addr += toWrite;
|
||||
// b += toWrite;
|
||||
// len -= toWrite;
|
||||
// }
|
||||
memoryFile.seekp(address);
|
||||
memoryFile.write(reinterpret_cast<const char *>(buffer), size);
|
||||
memoryFile.flush();
|
||||
}
|
||||
|
Reference in New Issue
Block a user