2021-10-13 16:08:35 -04:00
|
|
|
#include "drivers/SpiNorFlash.h"
|
2020-05-07 13:53:51 -04:00
|
|
|
#include <hal/nrf_gpio.h>
|
|
|
|
#include <libraries/delay/nrf_delay.h>
|
|
|
|
#include <libraries/log/nrf_log.h>
|
2021-10-13 16:08:35 -04:00
|
|
|
#include "drivers/Spi.h"
|
2020-05-07 13:53:51 -04:00
|
|
|
|
|
|
|
using namespace Pinetime::Drivers;
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
SpiNorFlash::SpiNorFlash(Spi& spi) : spi {spi} {
|
2020-05-07 13:53:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpiNorFlash::Init() {
|
2020-08-22 11:59:59 -04:00
|
|
|
device_id = ReadIdentificaion();
|
2022-05-09 11:14:42 -04:00
|
|
|
NRF_LOG_INFO("[SpiNorFlash] Manufacturer : %d, Memory type : %d, memory density : %d",
|
|
|
|
device_id.manufacturer,
|
|
|
|
device_id.type,
|
|
|
|
device_id.density);
|
2020-05-07 13:53:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpiNorFlash::Uninit() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpiNorFlash::Sleep() {
|
2020-08-22 11:59:59 -04:00
|
|
|
auto cmd = static_cast<uint8_t>(Commands::DeepPowerDown);
|
|
|
|
spi.Write(&cmd, sizeof(uint8_t));
|
2020-10-27 14:38:45 -04:00
|
|
|
NRF_LOG_INFO("[SpiNorFlash] Sleep")
|
2020-05-07 13:53:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpiNorFlash::Wakeup() {
|
2020-08-22 11:59:59 -04:00
|
|
|
// 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;
|
2021-04-18 13:28:14 -04:00
|
|
|
spi.Read(reinterpret_cast<uint8_t*>(&cmd), cmdSize, &id, 1);
|
|
|
|
auto devId = device_id = ReadIdentificaion();
|
|
|
|
if (devId.type != device_id.type) {
|
2020-08-22 11:59:59 -04:00
|
|
|
NRF_LOG_INFO("[SpiNorFlash] ID on Wakeup: Failed");
|
2021-04-18 13:28:14 -04:00
|
|
|
} else {
|
2020-08-22 11:59:59 -04:00
|
|
|
NRF_LOG_INFO("[SpiNorFlash] ID on Wakeup: %d", id);
|
|
|
|
}
|
2020-10-27 14:38:45 -04:00
|
|
|
NRF_LOG_INFO("[SpiNorFlash] Wakeup")
|
2020-05-07 13:53:51 -04:00
|
|
|
}
|
2020-05-11 12:50:37 -04:00
|
|
|
|
|
|
|
SpiNorFlash::Identification SpiNorFlash::ReadIdentificaion() {
|
|
|
|
auto cmd = static_cast<uint8_t>(Commands::ReadIdentification);
|
|
|
|
Identification identification;
|
2021-04-18 13:28:14 -04:00
|
|
|
spi.Read(&cmd, 1, reinterpret_cast<uint8_t*>(&identification), sizeof(Identification));
|
2020-05-11 12:50:37 -04:00
|
|
|
return identification;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SpiNorFlash::WriteInProgress() {
|
|
|
|
return (ReadStatusRegister() & 0x01u) == 0x01u;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SpiNorFlash::WriteEnabled() {
|
|
|
|
return (ReadStatusRegister() & 0x02u) == 0x02u;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
void SpiNorFlash::Read(uint32_t address, uint8_t* buffer, size_t size) {
|
2020-05-11 12:50:37 -04:00
|
|
|
static constexpr uint8_t cmdSize = 4;
|
2022-05-09 11:14:42 -04:00
|
|
|
uint8_t cmd[cmdSize] = {static_cast<uint8_t>(Commands::Read),
|
|
|
|
static_cast<uint8_t>(address >> 16U),
|
|
|
|
static_cast<uint8_t>(address >> 8U),
|
|
|
|
static_cast<uint8_t>(address)};
|
2021-04-18 13:28:14 -04:00
|
|
|
spi.Read(reinterpret_cast<uint8_t*>(&cmd), cmdSize, buffer, size);
|
2020-05-11 12:50:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2021-04-18 13:28:14 -04:00
|
|
|
uint8_t cmd[cmdSize] = {static_cast<uint8_t>(Commands::SectorErase),
|
2022-05-09 11:14:42 -04:00
|
|
|
static_cast<uint8_t>(sectorAddress >> 16U),
|
|
|
|
static_cast<uint8_t>(sectorAddress >> 8U),
|
|
|
|
static_cast<uint8_t>(sectorAddress)};
|
2020-05-11 12:50:37 -04:00
|
|
|
|
|
|
|
WriteEnable();
|
2021-04-18 13:28:14 -04:00
|
|
|
while (!WriteEnabled())
|
|
|
|
vTaskDelay(1);
|
2020-05-11 12:50:37 -04:00
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
spi.Read(reinterpret_cast<uint8_t*>(&cmd), cmdSize, nullptr, 0);
|
2020-05-11 12:50:37 -04:00
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
while (WriteInProgress())
|
|
|
|
vTaskDelay(1);
|
2020-05-11 12:50:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SpiNorFlash::ProgramFailed() {
|
|
|
|
return (ReadSecurityRegister() & 0x20u) == 0x20u;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SpiNorFlash::EraseFailed() {
|
|
|
|
return (ReadSecurityRegister() & 0x40u) == 0x40u;
|
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
void SpiNorFlash::Write(uint32_t address, const uint8_t* buffer, size_t size) {
|
2020-05-11 12:50:37 -04:00
|
|
|
static constexpr uint8_t cmdSize = 4;
|
|
|
|
|
|
|
|
size_t len = size;
|
|
|
|
uint32_t addr = address;
|
2020-06-01 12:32:46 -04:00
|
|
|
const uint8_t* b = buffer;
|
2021-04-18 13:28:14 -04:00
|
|
|
while (len > 0) {
|
2020-05-11 12:50:37 -04:00
|
|
|
uint32_t pageLimit = (addr & ~(pageSize - 1u)) + pageSize;
|
2021-04-18 13:28:14 -04:00
|
|
|
uint32_t toWrite = pageLimit - addr > len ? len : pageLimit - addr;
|
2020-05-11 12:50:37 -04:00
|
|
|
|
2022-05-09 11:14:42 -04:00
|
|
|
uint8_t cmd[cmdSize] = {static_cast<uint8_t>(Commands::PageProgram),
|
|
|
|
static_cast<uint8_t>(addr >> 16U),
|
|
|
|
static_cast<uint8_t>(addr >> 8U),
|
|
|
|
static_cast<uint8_t>(addr)};
|
2020-05-11 12:50:37 -04:00
|
|
|
|
|
|
|
WriteEnable();
|
2021-04-18 13:28:14 -04:00
|
|
|
while (!WriteEnabled())
|
|
|
|
vTaskDelay(1);
|
2020-05-11 12:50:37 -04:00
|
|
|
|
|
|
|
spi.WriteCmdAndBuffer(cmd, cmdSize, b, toWrite);
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
while (WriteInProgress())
|
|
|
|
vTaskDelay(1);
|
2020-05-11 12:50:37 -04:00
|
|
|
|
|
|
|
addr += toWrite;
|
|
|
|
b += toWrite;
|
|
|
|
len -= toWrite;
|
|
|
|
}
|
|
|
|
}
|