fc10ef2e10
GD32F470ZGT6 based project with full peripheral support: - SDRAM (W9825G6KH) via EXMC - LCD NT35510 (480x800) via EXMC NOR/PSRAM - Flash (W25Q64) via SPI - UART (USART0) debug console - LED indicators - CMSIS-DAP debug interface - CMake + ARM GCC toolchain build system
94 lines
2.5 KiB
C++
94 lines
2.5 KiB
C++
#include "flash_manager.h"
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
|
|
FlashManager &FlashManager::instance()
|
|
{
|
|
static FlashManager mgr;
|
|
return mgr;
|
|
}
|
|
|
|
FlashManager::FlashManager()
|
|
: handle_{
|
|
.type = FLASH_TYPE_INTERNAL,
|
|
.config = {
|
|
.type = FLASH_TYPE_INTERNAL,
|
|
.base_address = flash_get_internal_base_address(),
|
|
.clock_freq = 0,
|
|
.cs_pin = 0,
|
|
.spi_port = 0,
|
|
.exmc_bank = 0},
|
|
.info = {
|
|
.total_size = flash_get_internal_total_size(),
|
|
.sector_size = flash_get_internal_sector_size(),
|
|
.page_size = 256,
|
|
.block_size = 64 * 1024,
|
|
.manufacturer_id = 0x20,
|
|
.device_id = 0x470},
|
|
.initialized = false,
|
|
.private_data = nullptr
|
|
},
|
|
info_{
|
|
.total_size = flash_get_internal_total_size(),
|
|
.sector_size = flash_get_internal_sector_size(),
|
|
.page_size = 256,
|
|
.block_size = 64 * 1024,
|
|
.manufacturer_id = 0x20,
|
|
.device_id = 0x470}
|
|
{
|
|
}
|
|
|
|
RetCode FlashManager::init()
|
|
{
|
|
if (initialized_) {
|
|
return RET_OK;
|
|
}
|
|
|
|
printf("[FlashManager] Initializing...\r\n");
|
|
RetCode ret = flash_init(&handle_);
|
|
if (ret == RET_OK) {
|
|
flash_get_info(&handle_, &info_);
|
|
initialized_ = true;
|
|
printf("[FlashManager] Init OK: %lu KB, sector=%lu KB\r\n",
|
|
info_.total_size / 1024, info_.sector_size / 1024);
|
|
} else {
|
|
printf("[FlashManager] Init failed: %d\r\n", ret);
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
RetCode FlashManager::erase(uint32_t address, uint32_t size)
|
|
{
|
|
if (!initialized_) return RET_NOT_INITIALIZED;
|
|
return flash_erase(&handle_, address, size);
|
|
}
|
|
|
|
RetCode FlashManager::write(uint32_t address, const void *data, uint32_t length)
|
|
{
|
|
if (!initialized_) return RET_NOT_INITIALIZED;
|
|
return flash_write(&handle_, address, data, length);
|
|
}
|
|
|
|
RetCode FlashManager::read(uint32_t address, void *buffer, uint32_t length)
|
|
{
|
|
if (!initialized_) return RET_NOT_INITIALIZED;
|
|
return flash_read(&handle_, address, buffer, length);
|
|
}
|
|
|
|
RetCode FlashManager::self_test(uint32_t test_size)
|
|
{
|
|
if (!initialized_) return RET_NOT_INITIALIZED;
|
|
printf("[FlashManager] Running self-test (%lu bytes)...\r\n", test_size);
|
|
return flash_self_test(&handle_, test_size);
|
|
}
|
|
|
|
uint32_t FlashManager::total_size() const
|
|
{
|
|
return info_.total_size;
|
|
}
|
|
|
|
uint32_t FlashManager::sector_size() const
|
|
{
|
|
return info_.sector_size;
|
|
}
|