This commit is contained in:
Alexey Starikovskiy 2023-09-12 19:38:05 +03:00
parent 3e3aea8357
commit df2fb3a5aa
4 changed files with 67 additions and 19 deletions

View File

@ -208,6 +208,8 @@ add_executable(openFPGALoader
${OPENFPGALOADER_HEADERS}
)
add_executable(fsread src/fsread.cpp src/configBitstreamParser.cpp src/fsparser.cpp src/display.cpp)
include_directories(
${LIBUSB_INCLUDE_DIRS}
${LIBFTDI_INCLUDE_DIRS}
@ -300,6 +302,7 @@ endif()
if (ZLIB_FOUND)
include_directories(${ZLIB_INCLUDE_DIRS})
target_link_libraries(openFPGALoader ${ZLIB_LIBRARIES})
target_link_libraries(fsread ${ZLIB_LIBRARIES})
add_definitions(-DHAS_ZLIB=1)
else()
message("zlib library not found: can't flash intel/altera devices")

View File

@ -76,13 +76,24 @@ int FsParser::parseHeader()
case 0x0B: /* only present when bit_security is set */
_hdr["SecurityBit"] = "ON";
break;
case 0x10:
case 0x10: {
/* unknown conversion */
_hdr["loading_rate"] = to_string(0xff & (val >> 16));
_compressed = 0x01 & (val >> 13);
unsigned rate = (val >> 16) & 0xff;
if (rate) {
rate &= 0x7f;
rate ^= 0x44;
rate = (rate >> 2) + 1;
rate = 125000000/rate;
} else {
rate = 2500000; // default
}
_hdr["LoadingRate"] = to_string(rate);
_compressed = (val >> 13) & 1;
_hdr["Compress"] = (_compressed) ? "ON" : "OFF";
_hdr["ProgramDoneBypass"] = (0x01 & (val >> 12))?"ON":"OFF";
_hdr["ProgramDoneBypass"] = ((val >> 12) & 1) ? "ON" : "OFF";
printf("Loading Rate: %f MHz\n", rate*1e-6);
break;
}
case 0x12: /* unknown */
break;
case 0x51:
@ -245,9 +256,10 @@ int FsParser::parse()
_checksum = 0;
for (uint32_t pos = 0; pos < tmp.size(); pos+=16)
_checksum += (uint16_t)bitToVal(&tmp[pos], 16);
if (_verbose)
if (_verbose) {
printf("checksum 0x%04x\n", _checksum);
}
printSuccess("Done");

7
src/fsread.cpp Normal file
View File

@ -0,0 +1,7 @@
#include "fsparser.hpp"
int main(int argc, char *argv[]) {
FsParser fs(argv[1], true, 1);
fs.parse();
return 0;
}

View File

@ -200,8 +200,9 @@ Gowin::~Gowin()
bool Gowin::send_command(uint8_t cmd)
{
_jtag->shiftIR(&cmd, nullptr, 8);
_jtag->toggleClk(8);
_jtag->toggleClk(6);
_jtag->flush();
usleep(250);
return true;
}
@ -224,9 +225,11 @@ bool Gowin::send_command(uint8_t cmd)
uint32_t Gowin::readReg32(uint8_t cmd)
{
uint32_t reg = 0, tmp = 0xff;
uint32_t reg = 0, tmp = 0xffffffffU;
send_command(cmd);
_jtag->shiftDR((uint8_t *)&tmp, (uint8_t *)&reg, 32);
_jtag->flush();
usleep(200);
return le32toh(reg);
}
@ -241,15 +244,15 @@ void Gowin::programFlash()
const uint8_t *data = _fs->getData();
int length = _fs->getLength();
_jtag->setClkFreq(2500000);
_jtag->setClkFreq(3000000);
send_command(CONFIG_DISABLE);
send_command(NOOP);
send_command(0);
_jtag->go_test_logic_reset();
_jtag->flush();
usleep(500*1000);
//_jtag->flush();
//usleep(500*1000);
eraseSRAM();
//eraseSRAM();
if (!eraseFLASH())
return;
/* test status a faire */
@ -481,6 +484,7 @@ inline uint32_t bswap_32(uint32_t x)
bool Gowin::writeFLASH(uint32_t page, const uint8_t *data, int length)
{
printInfo("Write FLASH ", false);
//return true;
uint8_t xpage[256];
length /= 8;
for (int off = 0; off < length; off += 256) {
@ -565,6 +569,15 @@ bool Gowin::writeSRAM(const uint8_t *data, int length)
bool Gowin::eraseFLASH()
{
printInfo("Erase Flash ", false);
send_command(CONFIG_DISABLE);
send_command(0);
send_command(CONFIG_DISABLE);
send_command(0);
readStatusReg();
readStatusReg();
readStatusReg();
readReg32(0x11);
usleep(500);
send_command(CONFIG_ENABLE);
send_command(EFLASH_ERASE);
uint32_t tx = 0;
@ -574,20 +587,20 @@ bool Gowin::eraseFLASH()
_jtag->shiftDR((uint8_t *)&tx, NULL, 32);
}
sendClkUs(150*1000);
if (_verbose)
displayReadReg(readStatusReg());
send_command(CONFIG_DISABLE);
send_command(NOOP);
send_command(0x11);
send_command(CONFIG_DISABLE);
send_command(NOOP);
send_command(RELOAD);
send_command(NOOP);
_jtag->flush();
usleep(500*1000);
readReg32(0x11);
readReg32(0x11);
if (_verbose)
displayReadReg(readStatusReg());
_jtag->flush();
usleep(500*1000);
if (_verbose)
displayReadReg(readStatusReg());
printSuccess("Done");
return true;
}
@ -606,6 +619,19 @@ void Gowin::sendClkUs(unsigned us)
bool Gowin::eraseSRAM()
{
printInfo("Erase SRAM ", false);
send_command(CONFIG_DISABLE);
send_command(0);
_jtag->go_test_logic_reset();
readReg32(0x11);
readReg32(0x41);
_jtag->go_test_logic_reset();
send_command(CONFIG_DISABLE);
send_command(0);
_jtag->go_test_logic_reset();
readReg32(0x41);
readReg32(0x11);
send_command(CONFIG_ENABLE);
send_command(ERASE_SRAM);
send_command(NOOP);