Skip to content

Commit

Permalink
gowin: Implement user flash programming for GW1N9
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean THOMAS committed Dec 11, 2024
1 parent ffa006d commit 46ff6cb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
32 changes: 31 additions & 1 deletion src/gowin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,22 @@ Gowin::Gowin(Jtag *jtag, const string filename, const string &file_type, std::st
}
}

if (user_flash.size() > 0) {
if (!is_gw1n9)
throw std::runtime_error("Unsupported FPGA model (only GW1N(R)-9(C) is supported at the moment)");
if (mcufw.size() > 0)
throw std::runtime_error("Microcontroller firmware and user flash can't be specified simultaneously");

_userflash = std::unique_ptr<ConfigBitstreamParser>(new RawParser(user_flash, false));

if (_userflash->parse() == EXIT_FAILURE) {
printError("FAIL");
throw std::runtime_error("can't parse file");
} else {
printSuccess("DONE");
}
}

if (is_gw5a && _mode == Device::FLASH_MODE) {
_jtag->setClkFreq(2500000);
_jtag->set_state(Jtag::TEST_LOGIC_RESET);
Expand Down Expand Up @@ -293,6 +309,13 @@ void Gowin::programFlash()
return;
}

if (_userflash) {
const uint8_t *userflash_data = _userflash->getData();
int userflash_length = _userflash->getLength();
if (!writeFLASH(0x6D0, userflash_data, userflash_length, true))
return;
}

if (_verify)
printWarn("writing verification not supported");

Expand Down Expand Up @@ -567,7 +590,7 @@ inline uint32_t bswap_32(uint32_t x)
}

/* TN653 p. 17-21 */
bool Gowin::writeFLASH(uint32_t page, const uint8_t *data, int length)
bool Gowin::writeFLASH(uint32_t page, const uint8_t *data, int length, bool invert_bits)
{

#if 1
Expand Down Expand Up @@ -652,6 +675,13 @@ bool Gowin::writeFLASH(uint32_t page, const uint8_t *data, int length)
else
tx[x] = t[x];
}

if (invert_bits) {
for (int x = 0; x < 4; x++) {
tx[x] ^= 0xFF;
}
}

_jtag->shiftDR(tx, NULL, 32);

if (!is_gw1n1)
Expand Down
3 changes: 2 additions & 1 deletion src/gowin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class Gowin: public Device, SPIInterface {
void programExtFlash(unsigned int offset, bool unprotect_flash);
void programSRAM();
bool writeSRAM(const uint8_t *data, int length);
bool writeFLASH(uint32_t page, const uint8_t *data, int length);
bool writeFLASH(uint32_t page, const uint8_t *data, int length, bool invert_bits = false);
void displayReadReg(const char *, uint32_t dev);
uint32_t readStatusReg();
uint32_t readUserCode();
Expand All @@ -131,6 +131,7 @@ class Gowin: public Device, SPIInterface {

std::unique_ptr<ConfigBitstreamParser> _fs;
std::unique_ptr<ConfigBitstreamParser> _mcufw;
std::unique_ptr<ConfigBitstreamParser> _userflash;
uint32_t _idcode;
bool is_gw1n1;
bool is_gw1n4;
Expand Down

0 comments on commit 46ff6cb

Please sign in to comment.