2022-02-16 21:42:29 +01:00
|
|
|
#pragma once
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
2022-05-15 22:15:19 +02:00
|
|
|
#include <fstream>
|
2022-02-16 21:42:29 +01:00
|
|
|
|
|
|
|
namespace Pinetime {
|
|
|
|
namespace Drivers {
|
|
|
|
class Spi;
|
|
|
|
class SpiNorFlash {
|
|
|
|
public:
|
2022-05-15 22:15:19 +02:00
|
|
|
explicit SpiNorFlash(const std::string& memoryFilePath);
|
|
|
|
~SpiNorFlash();
|
2022-02-16 21:42:29 +01:00
|
|
|
SpiNorFlash(const SpiNorFlash&) = delete;
|
|
|
|
SpiNorFlash& operator=(const SpiNorFlash&) = delete;
|
|
|
|
SpiNorFlash(SpiNorFlash&&) = delete;
|
|
|
|
SpiNorFlash& operator=(SpiNorFlash&&) = delete;
|
|
|
|
|
2022-04-12 15:14:55 +02:00
|
|
|
struct __attribute__((packed)) Identification {
|
2022-02-16 21:42:29 +01:00
|
|
|
uint8_t manufacturer = 0;
|
|
|
|
uint8_t type = 0;
|
|
|
|
uint8_t density = 0;
|
2022-03-20 22:49:30 +01:00
|
|
|
};
|
2022-02-16 21:42:29 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2022-05-15 22:15:19 +02:00
|
|
|
static constexpr size_t memorySize {0x400000};
|
|
|
|
const std::string& memoryFilePath;
|
|
|
|
|
|
|
|
|
2022-02-16 21:42:29 +01:00
|
|
|
Identification device_id;
|
2022-05-15 22:15:19 +02:00
|
|
|
std::fstream memoryFile;
|
2022-02-16 21:42:29 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|