1
0
Fork 0
InfiniSim/sim/drivers/SpiNorFlash.h
JF 644431cbc4
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>
2022-05-15 22:15:19 +02:00

66 lines
1.7 KiB
C++

#pragma once
#include <cstddef>
#include <cstdint>
#include <fstream>
namespace Pinetime {
namespace Drivers {
class Spi;
class SpiNorFlash {
public:
explicit SpiNorFlash(const std::string& memoryFilePath);
~SpiNorFlash();
SpiNorFlash(const SpiNorFlash&) = delete;
SpiNorFlash& operator=(const SpiNorFlash&) = delete;
SpiNorFlash(SpiNorFlash&&) = delete;
SpiNorFlash& operator=(SpiNorFlash&&) = delete;
struct __attribute__((packed)) Identification {
uint8_t manufacturer = 0;
uint8_t type = 0;
uint8_t density = 0;
};
Identification ReadIdentificaion();
uint8_t ReadStatusRegister();
bool WriteInProgress();
bool WriteEnabled();
uint8_t ReadConfigurationRegister();
void Read(uint32_t address, uint8_t* buffer, size_t size);
void Write(uint32_t address, const uint8_t* buffer, size_t size);
void WriteEnable();
void SectorErase(uint32_t sectorAddress);
uint8_t ReadSecurityRegister();
bool ProgramFailed();
bool EraseFailed();
void Init();
void Uninit();
void Sleep();
void Wakeup();
private:
enum class Commands : uint8_t {
PageProgram = 0x02,
Read = 0x03,
ReadStatusRegister = 0x05,
WriteEnable = 0x06,
ReadConfigurationRegister = 0x15,
SectorErase = 0x20,
ReadSecurityRegister = 0x2B,
ReadIdentification = 0x9F,
ReleaseFromDeepPowerDown = 0xAB,
DeepPowerDown = 0xB9
};
static constexpr uint16_t pageSize = 256;
static constexpr size_t memorySize {0x400000};
const std::string& memoryFilePath;
Identification device_id;
std::fstream memoryFile;
};
}
}