gowin: Use std::unique_ptr instead of manual RAII

This commit is contained in:
Jean THOMAS 2024-12-11 11:43:07 +01:00
parent 8f88d221dd
commit 91608ca206
2 changed files with 11 additions and 22 deletions

View File

@ -80,12 +80,11 @@ Gowin::Gowin(Jtag *jtag, const string filename, const string &file_type, std::st
bool verify, int8_t verbose): Device(jtag, filename, file_type, bool verify, int8_t verbose): Device(jtag, filename, file_type,
verify, verbose), verify, verbose),
SPIInterface(filename, verbose, 0, verify, false, false), SPIInterface(filename, verbose, 0, verify, false, false),
_fs(NULL), _idcode(0), is_gw1n1(false), is_gw2a(false), _idcode(0), is_gw1n1(false), is_gw2a(false), is_gw1n4(false),
is_gw1n4(false), is_gw5a(false), _external_flash(external_flash), is_gw5a(false), _external_flash(external_flash),
_spi_sck(BSCAN_SPI_SCK), _spi_cs(BSCAN_SPI_CS), _spi_sck(BSCAN_SPI_SCK), _spi_cs(BSCAN_SPI_CS),
_spi_di(BSCAN_SPI_DI), _spi_do(BSCAN_SPI_DO), _spi_di(BSCAN_SPI_DI), _spi_do(BSCAN_SPI_DO),
_spi_msk(BSCAN_SPI_MSK), _spi_msk(BSCAN_SPI_MSK)
_mcufw(NULL)
{ {
detectFamily(); detectFamily();
@ -100,7 +99,7 @@ Gowin::Gowin(Jtag *jtag, const string filename, const string &file_type, std::st
if (!_file_extension.empty() && prg_type != Device::RD_FLASH) { if (!_file_extension.empty() && prg_type != Device::RD_FLASH) {
if (_file_extension == "fs") { if (_file_extension == "fs") {
try { try {
_fs = new FsParser(_filename, _mode == Device::MEM_MODE, _verbose); _fs = std::unique_ptr<ConfigBitstreamParser>(new FsParser(_filename, _mode == Device::MEM_MODE, _verbose));
} catch (std::exception &e) { } catch (std::exception &e) {
throw std::runtime_error(e.what()); throw std::runtime_error(e.what());
} }
@ -109,7 +108,7 @@ Gowin::Gowin(Jtag *jtag, const string filename, const string &file_type, std::st
if (!_external_flash) if (!_external_flash)
throw std::runtime_error("incompatible file format"); throw std::runtime_error("incompatible file format");
try { try {
_fs = new RawParser(_filename, false); _fs = std::unique_ptr<ConfigBitstreamParser>(new RawParser(_filename, false));
} catch (std::exception &e) { } catch (std::exception &e) {
throw std::runtime_error(e.what()); throw std::runtime_error(e.what());
} }
@ -118,7 +117,6 @@ Gowin::Gowin(Jtag *jtag, const string filename, const string &file_type, std::st
printInfo("Parse file ", false); printInfo("Parse file ", false);
if (_fs->parse() == EXIT_FAILURE) { if (_fs->parse() == EXIT_FAILURE) {
printError("FAIL"); printError("FAIL");
delete _fs;
throw std::runtime_error("can't parse file"); throw std::runtime_error("can't parse file");
} else { } else {
printSuccess("DONE"); printSuccess("DONE");
@ -144,11 +142,10 @@ Gowin::Gowin(Jtag *jtag, const string filename, const string &file_type, std::st
if (_idcode != 0x0100981b) if (_idcode != 0x0100981b)
throw std::runtime_error("Microcontroller firmware flashing only supported on GW1NSR-4C"); throw std::runtime_error("Microcontroller firmware flashing only supported on GW1NSR-4C");
_mcufw = new RawParser(mcufw, false); _mcufw = std::unique_ptr<ConfigBitstreamParser>(new RawParser(mcufw, false));
if (_mcufw->parse() == EXIT_FAILURE) { if (_mcufw->parse() == EXIT_FAILURE) {
printError("FAIL"); printError("FAIL");
delete _mcufw;
throw std::runtime_error("can't parse file"); throw std::runtime_error("can't parse file");
} else { } else {
printSuccess("DONE"); printSuccess("DONE");
@ -167,14 +164,6 @@ Gowin::Gowin(Jtag *jtag, const string filename, const string &file_type, std::st
} }
} }
Gowin::~Gowin()
{
if (_fs)
delete _fs;
if (_mcufw)
delete _mcufw;
}
bool Gowin::detectFamily() bool Gowin::detectFamily()
{ {
_idcode = _jtag->get_target_device_id(); _idcode = _jtag->get_target_device_id();
@ -415,7 +404,7 @@ void Gowin::program(unsigned int offset, bool unprotect_flash)
void Gowin::checkCRC() void Gowin::checkCRC()
{ {
uint32_t ucode = readUserCode(); uint32_t ucode = readUserCode();
uint16_t checksum = static_cast<FsParser *>(_fs)->checksum(); uint16_t checksum = static_cast<FsParser *>(_fs.get())->checksum();
if (static_cast<uint16_t>(0xffff & ucode) == checksum) if (static_cast<uint16_t>(0xffff & ucode) == checksum)
goto success; goto success;
/* no match: /* no match:
@ -788,7 +777,7 @@ bool Gowin::writeSRAM(const uint8_t *data, int length)
} }
progress.done(); progress.done();
send_command(0x0a); send_command(0x0a);
uint32_t checksum = static_cast<FsParser *>(_fs)->checksum(); uint32_t checksum = static_cast<FsParser *>(_fs.get())->checksum();
checksum = htole32(checksum); checksum = htole32(checksum);
_jtag->shiftDR((uint8_t *)&checksum, NULL, 32); _jtag->shiftDR((uint8_t *)&checksum, NULL, 32);
send_command(0x08); send_command(0x08);

View File

@ -10,6 +10,7 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <vector> #include <vector>
#include <memory>
#include "configBitstreamParser.hpp" #include "configBitstreamParser.hpp"
#include "device.hpp" #include "device.hpp"
@ -22,7 +23,6 @@ class Gowin: public Device, SPIInterface {
Gowin(Jtag *jtag, std::string filename, const std::string &file_type, Gowin(Jtag *jtag, std::string filename, const std::string &file_type,
std::string mcufw, Device::prog_type_t prg_type, std::string mcufw, Device::prog_type_t prg_type,
bool external_flash, bool verify, int8_t verbose); bool external_flash, bool verify, int8_t verbose);
~Gowin();
uint32_t idCode() override; uint32_t idCode() override;
void reset() override; void reset() override;
void program(unsigned int offset, bool unprotect_flash) override; void program(unsigned int offset, bool unprotect_flash) override;
@ -128,7 +128,8 @@ class Gowin: public Device, SPIInterface {
*/ */
bool gw5a_enable_spi(); bool gw5a_enable_spi();
ConfigBitstreamParser *_fs; std::unique_ptr<ConfigBitstreamParser> _fs;
std::unique_ptr<ConfigBitstreamParser> _mcufw;
uint32_t _idcode; uint32_t _idcode;
bool is_gw1n1; bool is_gw1n1;
bool is_gw2a; bool is_gw2a;
@ -141,7 +142,6 @@ class Gowin: public Device, SPIInterface {
uint8_t _spi_di; /**< di signal (mosi) offset in bscan SPI */ uint8_t _spi_di; /**< di signal (mosi) offset in bscan SPI */
uint8_t _spi_do; /**< do signal (miso) offset in bscan SPI */ uint8_t _spi_do; /**< do signal (miso) offset in bscan SPI */
uint8_t _spi_msk; /** default spi msk with only do out */ uint8_t _spi_msk; /** default spi msk with only do out */
ConfigBitstreamParser *_mcufw;
JtagInterface::tck_edge_t _prev_rd_edge; /**< default probe rd edge cfg */ JtagInterface::tck_edge_t _prev_rd_edge; /**< default probe rd edge cfg */
JtagInterface::tck_edge_t _prev_wr_edge; /**< default probe wr edge cfg */ JtagInterface::tck_edge_t _prev_wr_edge; /**< default probe wr edge cfg */
}; };