Skip to content

Commit 36a7d92

Browse files
committed
lattice: simplify code for ECP3 family
1 parent e1d0749 commit 36a7d92

File tree

2 files changed

+41
-30
lines changed

2 files changed

+41
-30
lines changed

src/lattice.cpp

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -428,30 +428,8 @@ bool Lattice::program_mem()
428428
}
429429

430430
if (_fpga_family == ECP3_FAMILY) {
431-
// ! Erase the device
432-
433-
// ! Shift in ISC PROGRAM USERCODE(0x1A) instruction
434-
// SIR 8 TDI (1A);
435-
// SDR 32 TDI (FFFFFFFF);
436-
// RUNTEST IDLE 5 TCK 2,00E-03 SEC;
437-
// ! Shift in READ USERCODE(0x17) instruction
438-
// SIR 8 TDI (17);
439-
// SDR 32 TDI (FFFFFFFF)
440-
// TDO (FFFFFFFF);
441-
uint32_t dummy = 0xffffffff;
442-
wr_rd(ECP3_ISC_PROGRAM_USERCODE, (uint8_t *)&dummy, 4, NULL, 0);
443-
_jtag->set_state(Jtag::RUN_TEST_IDLE);
444-
_jtag->toggleClk(5, 1);
445-
usleep_ecp3(2000); // 2ms
446-
447-
uint32_t rx = 0;
448-
wr_rd(ECP3_READ_USERCODE, (uint8_t *)&dummy, 4, (uint8_t *)&rx, 4);
449-
if (rx != 0xffffffff) {
450-
char message[256];
451-
snprintf(message, 256, "failed: 0x%08x instead of 0xffffffff", rx);
452-
printError(message);
431+
if (!write_userCode(0xffffffff))
453432
return false;
454-
}
455433
}
456434

457435
/* ISC ERASE
@@ -526,7 +504,7 @@ bool Lattice::program_mem()
526504
for (int ii = 0; ii < size; ii++)
527505
tmp[ii] = ConfigBitstreamParser::reverseByte(data[i+ii]);
528506

529-
_jtag->shiftDR(tmp, NULL, size*8, next_state);
507+
_jtag->shiftDR(tmp, NULL, size * 8, next_state);
530508
}
531509

532510
_jtag->set_state(Jtag::RUN_TEST_IDLE);
@@ -536,15 +514,15 @@ bool Lattice::program_mem()
536514
usleep_ecp3(2000);
537515

538516
/* Verifiy User Code register */
539-
uint32_t rx = 0;
540-
uint8_t dummy[] = {0xff, 0xff, 0xff, 0xff};
541-
wr_rd(ECP3_READ_USERCODE, dummy, 4, (uint8_t *)&rx, 4);
517+
uint32_t rx = userCode();
542518
if (rx != 0x00000000) {
519+
progress.fail();
543520
char message[256];
544521
snprintf(message, 256, "failed: 0x%08x instead of 0x00000000", rx);
545522
printError(message);
546523
return false;
547524
}
525+
progress.done();
548526
} else {
549527
_jtag->toggleClk(1000);
550528
}
@@ -590,9 +568,14 @@ bool Lattice::program_mem()
590568

591569
// Verify STATUS Register
592570
uint64_t status = readStatusReg();
593-
printf("status %08x %08x\n", status, status & 0x60007);
594-
displayReadReg(status);
595571
_jtag->toggleClk(5, 1);
572+
if (status & 0x60007 != 0x20000) {
573+
char message[256];
574+
snprintf(message, 256, "Programming failed: status 0x%08x instead of 0x20000", status);
575+
printError(message);
576+
displayReadReg(status);
577+
return false;
578+
}
596579
}
597580

598581
if (_verbose)
@@ -1086,13 +1069,40 @@ uint32_t Lattice::idCode()
10861069
int Lattice::userCode()
10871070
{
10881071
uint8_t usercode[4];
1089-
wr_rd(0xC0, NULL, 0, usercode, 4);
1072+
wr_rd((_fpga_family == ECP3_FAMILY) ? ECP3_READ_USERCODE : 0xC0,
1073+
NULL, 0, usercode, 4);
10901074
return usercode[3] << 24 |
10911075
usercode[2] << 16 |
10921076
usercode[1] << 8 |
10931077
usercode[0];
10941078
}
10951079

1080+
bool Lattice::write_userCode(uint32_t usercode)
1081+
{
1082+
// ! Shift in ISC PROGRAM USERCODE(0x1A) instruction
1083+
// SIR 8 TDI (1A);
1084+
// SDR 32 TDI (FFFFFFFF);
1085+
// RUNTEST IDLE 5 TCK 2,00E-03 SEC;
1086+
// ! Shift in READ USERCODE(0x17) instruction
1087+
// SIR 8 TDI (17);
1088+
// SDR 32 TDI (FFFFFFFF)
1089+
// TDO (FFFFFFFF);
1090+
wr_rd(ECP3_ISC_PROGRAM_USERCODE, (uint8_t *)&usercode, 4, NULL, 0);
1091+
_jtag->set_state(Jtag::RUN_TEST_IDLE);
1092+
_jtag->toggleClk(5, 1);
1093+
usleep_ecp3(2000); // 2ms
1094+
1095+
uint32_t rd_usercode = userCode();
1096+
if (rd_usercode != usercode) {
1097+
char message[256];
1098+
snprintf(message, 256, "failed: 0x%08x instead of 0x%08x",
1099+
rd_usercode, usercode);
1100+
printError(message);
1101+
return false;
1102+
}
1103+
return true;
1104+
}
1105+
10961106
bool Lattice::checkID()
10971107
{
10981108
printf("\n");

src/lattice.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Lattice: public Device, SPIInterface {
2525
int8_t verbose, bool skip_load_bridge, bool skip_reset);
2626
uint32_t idCode() override;
2727
int userCode();
28+
bool write_userCode(uint32_t usercode);
2829
void reset() override {}
2930
void program(unsigned int offset, bool unprotect_flash) override;
3031
bool program_mem();

0 commit comments

Comments
 (0)