1
0
mirror of https://github.com/preble/libpinproc synced 2026-02-24 18:25:23 +01:00

pinprocfw: code cleanup

This commit is contained in:
Tom Collins
2020-06-25 23:28:45 -07:00
committed by Gerry Stellenberg
parent 954084d348
commit 83f8f116f9

View File

@@ -32,12 +32,18 @@
#endif /* DEBUG_MODE */ #endif /* DEBUG_MODE */
#include "pinprocfw.h" #include "pinprocfw.h"
#include "../../src/PRHardware.h"
#include "lenval.h" #include "lenval.h"
#ifndef _MSC_VER #ifndef _MSC_VER
#include <unistd.h> #include <unistd.h>
#endif #endif
#if defined(__WIN32__) || defined(_WIN32)
#include <windows.h>
#define PRSleep(milliseconds) Sleep(milliseconds)
#else
#define PRSleep(milliseconds) usleep(milliseconds*1000)
#endif
#include "pinproc.h" // Include libpinproc's header. #include "pinproc.h" // Include libpinproc's header.
PRMachineType machineType = kPRMachineCustom; // Should work with all machines. PRMachineType machineType = kPRMachineCustom; // Should work with all machines.
@@ -514,7 +520,7 @@ void setPort(short p,short val)
// Toggle TCK. // Toggle TCK.
void pulseClock() void pulseClock(void)
{ {
setPort(TCK,0); /* set the TCK port to low */ setPort(TCK,0); /* set the TCK port to low */
setPort(TCK,1); /* set the TCK port to high */ setPort(TCK,1); /* set the TCK port to high */
@@ -540,7 +546,7 @@ void readByte(unsigned char *data)
long int bytesPer200th = numBytesTotal / 200; long int bytesPer200th = numBytesTotal / 200;
numBytesCurrent++; numBytesCurrent++;
if (numBytesCurrent % bytesPerTenth == 0) { if (numBytesCurrent % bytesPerTenth == 0) {
printf("\n%ld0%% ",numBytesCurrent/bytesPerTenth); printf("\n%d0%% ",(int) (numBytesCurrent/bytesPerTenth));
fflush(stdout); fflush(stdout);
} }
else if (numBytesCurrent % bytesPer200th == 0) { else if (numBytesCurrent % bytesPer200th == 0) {
@@ -549,7 +555,7 @@ void readByte(unsigned char *data)
} }
} }
unsigned char readTDOBit() unsigned char readTDOBit(void)
{ {
PRJTAGStatus jtagStatus; PRJTAGStatus jtagStatus;
@@ -1853,7 +1859,7 @@ void xsvfCleanup( SXsvfInfo* pXsvfInfo )
* Parameters: none. * Parameters: none.
* Returns: int - Legacy result values: 1 == success; 0 == failed. * Returns: int - Legacy result values: 1 == success; 0 == failed.
*****************************************************************************/ *****************************************************************************/
int xsvfExecute() int xsvfExecute(void)
{ {
SXsvfInfo xsvfInfo; SXsvfInfo xsvfInfo;
@@ -1874,7 +1880,7 @@ int xsvfExecute()
} }
else else
{ {
XSVFDBG_PRINTF( 0, "SUCCESS - Operation completed successfully. Cycle P-ROC power to activate any changes.\n" ); XSVFDBG_PRINTF( 0, "SUCCESS - Operation completed successfully.\nCycle P-ROC power to activate any changes.\n" );
} }
xsvfCleanup( &xsvfInfo ); xsvfCleanup( &xsvfInfo );
@@ -1882,7 +1888,7 @@ int xsvfExecute()
return( XSVF_ERRORCODE(xsvfInfo.iErrorCode) ); return( XSVF_ERRORCODE(xsvfInfo.iErrorCode) );
} }
int openPROC() int openPROC(void)
{ {
// Instantiate the P-ROC device: // Instantiate the P-ROC device:
XSVFDBG_PRINTF( 1, "Opening P-ROC.\n"); XSVFDBG_PRINTF( 1, "Opening P-ROC.\n");
@@ -1898,35 +1904,27 @@ int openPROC()
void printUsage(char * name) void printUsage(char * name)
{ {
fprintf(stderr, "\n%s: Version 1.1", name ); fprintf(stderr, "%s: Version 1.1\n", name );
fprintf(stderr, "\nUSAGE: %s <filename>\n", name ); fprintf(stderr, "USAGE: %s <filename>\n", name );
fprintf(stderr, " filename = the .xsvf or .p-roc file to execute.\n" ); fprintf(stderr, " filename = the .xsvf or .p-roc file to execute.\n" );
} }
int getNumFileBytes() {
unsigned char data;
int i=0;
while (!feof(in))
{
data = (unsigned char)fgetc( in );
i++;
}
return i;
}
// Move file pointer to beginning of XSVF data. // Move file pointer to beginning of XSVF data.
void preparePROCFile() { void preparePROCFile(void) {
int temp, num_header_words; int temp, num_header_words;
int i; int i;
rewind(in);
fscanf(in, "%x\n", &temp); fscanf(in, "%x\n", &temp);
num_header_words = (int)(0x012345678 - temp); num_header_words = (int)(0x012345678 - temp);
for (i=0; i<num_header_words; i++) fscanf(in, "%x\n", &temp); for (i = 0; i < num_header_words; i++) {
fscanf(in, "%x\n", &temp);
}
} }
uint32_t P3ROC_SPIWaitForReady() uint32_t P3ROC_SPIWaitForReady(void)
{ {
uint32_t dataBuffer[1]; uint32_t dataBuffer[1];
uint32_t addr = 0; uint32_t addr = 0;
@@ -1952,7 +1950,7 @@ uint32_t P3ROC_SPIWaitForReady()
return 1; return 1;
} }
void P3ROC_SPISendWEL() void P3ROC_SPISendWEL(void)
{ {
uint32_t dataBuffer[512]; uint32_t dataBuffer[512];
uint32_t addr = 0; uint32_t addr = 0;
@@ -1961,7 +1959,7 @@ void P3ROC_SPISendWEL()
PRWriteData (proc, P3_ROC_BUS_SPI_SELECT, addr, 1, dataBuffer); PRWriteData (proc, P3_ROC_BUS_SPI_SELECT, addr, 1, dataBuffer);
} }
void P3ROC_SPIBulkErase() void P3ROC_SPIBulkErase(void)
{ {
uint32_t dataBuffer[512]; uint32_t dataBuffer[512];
uint32_t addr = 0; uint32_t addr = 0;
@@ -2001,10 +1999,10 @@ void P3ROC_SPIWritePage(uint32_t page_addr, uint32_t * writeDataBuffer)
P3ROC_SPIWaitForReady(); P3ROC_SPIWaitForReady();
} }
int verifyP3ROCImage() int verifyP3ROCImage(void)
{ {
unsigned char inChars [4]; unsigned char inChars [4];
uint32_t dataBuffer[512]; uint32_t dataBuffer[64];
uint32_t readBuffer[64]; uint32_t readBuffer[64];
int pageAddr = 0; int pageAddr = 0;
@@ -2025,7 +2023,7 @@ int verifyP3ROCImage()
inChars[j] = fgetc(in); inChars[j] = fgetc(in);
numBytesCurrent++; numBytesCurrent++;
if (numBytesCurrent % bytesPerTenth == 0) { if (numBytesCurrent % bytesPerTenth == 0) {
printf("\n%ld0%% ",numBytesCurrent/bytesPerTenth); printf("\n%d0%% ", (int)(numBytesCurrent / bytesPerTenth));
fflush(stdout); fflush(stdout);
} }
else if (numBytesCurrent % bytesPer200th == 0) { else if (numBytesCurrent % bytesPer200th == 0) {
@@ -2042,20 +2040,19 @@ int verifyP3ROCImage()
//fprintf(stderr, "\nWriting Page: %x", pageAddr); //fprintf(stderr, "\nWriting Page: %x", pageAddr);
P3ROC_SPIReadPage(pageAddr, &readBuffer[0]); P3ROC_SPIReadPage(pageAddr, &readBuffer[0]);
for (int i=0; i<64; i++) if (memcmp(readBuffer, dataBuffer, sizeof readBuffer) != 0) {
{ return 0;
if (readBuffer[i] != dataBuffer[i]) return 0;
} }
pageAddr++; pageAddr++;
} }
XSVFDBG_PRINTF( 0, "\n\nSUCCESS - Operation completed successfully. Cycle P3-ROC power to activate any changes.\n" ); XSVFDBG_PRINTF( 0, "\n\nSUCCESS - Operation completed successfully.\nCycle P3-ROC power to activate any changes.\n" );
return 1; return 1;
} }
void writeP3ROCImage() void writeP3ROCImage(void)
{ {
unsigned char inChars [4]; unsigned char inChars [4];
uint32_t dataBuffer[512]; uint32_t dataBuffer[512];
@@ -2069,16 +2066,13 @@ void writeP3ROCImage()
printf("\nProgramming new image:\n0%% "); printf("\nProgramming new image:\n0%% ");
fflush(stdout); fflush(stdout);
while (!feof(in)) { while (!feof(in)) {
for (int i=0; i<64; i++) for (int i=0; i<64; i++) {
{ if (!feof(in)) {
if (!feof(in)) for (int j=0; j<4; j++) {
{
for (int j=0; j<4; j++)
{
inChars[j] = fgetc(in); inChars[j] = fgetc(in);
numBytesCurrent++; numBytesCurrent++;
if (numBytesCurrent % bytesPerTenth == 0) { if (numBytesCurrent % bytesPerTenth == 0) {
printf("\n%ld0%% ",numBytesCurrent/bytesPerTenth); printf("\n%d0%% ", (int)(numBytesCurrent / bytesPerTenth));
fflush(stdout); fflush(stdout);
} }
else if (numBytesCurrent % bytesPer200th == 0) { else if (numBytesCurrent % bytesPer200th == 0) {
@@ -2105,7 +2099,7 @@ void writeP3ROCImage()
} }
void processP3ROCFile() void processP3ROCFile(void)
{ {
fprintf(stderr, "\n\nUpdating P3-ROC. This may take a couple of minutes.\n"); fprintf(stderr, "\n\nUpdating P3-ROC. This may take a couple of minutes.\n");
fprintf(stderr, "WARNING: DO NOT POWER CYCLE UNTIL COMPLETE!\n"); fprintf(stderr, "WARNING: DO NOT POWER CYCLE UNTIL COMPLETE!\n");
@@ -2113,22 +2107,18 @@ void processP3ROCFile()
P3ROC_SPIBulkErase(); P3ROC_SPIBulkErase();
fprintf(stderr, "\nFlash erased.\n"); fprintf(stderr, "\nFlash erased.\n");
writeP3ROCImage(); writeP3ROCImage();
rewind(in);
preparePROCFile(); preparePROCFile();
if (verifyP3ROCImage() == 0) if (verifyP3ROCImage() == 0)
fprintf(stderr, "\nERROR: Verification failed. Please retry. DO NOT CYCLE POWER.\n\n"); fprintf(stderr, "\nERROR: Verification failed. Please retry. DO NOT CYCLE POWER.\n\n");
} }
int processFile() int processFile(void)
{ {
clock_t startClock; clock_t startClock;
clock_t endClock; clock_t endClock;
int iErrorCode; int iErrorCode;
//fseek(in, 0L, SEEK_END);
//numBytesTotal = ftell(in);
//fseek(in, 0L, SEEK_SET);
numBytesCurrent = 0;
numBytesCurrent = 0;
/* Execute the XSVF in the file */ /* Execute the XSVF in the file */
startClock = clock(); startClock = clock();
@@ -2139,91 +2129,92 @@ int processFile()
return iErrorCode; return iErrorCode;
} }
uint32_t checkPROCFile() { uint32_t checkPROCFile(void) {
uint32_t checksum=0, file_checksum, file_board_id, header_checksum; uint32_t checksum=0, file_checksum, file_board_id, header_checksum;
unsigned char data; unsigned char data;
int i=0,file_i=0; int i=0,file_i=0;
int min_board_rev, max_board_rev; int min_board_rev, max_board_rev;
int temp, num_header_words, proc_file_version; int temp[8], num_header_words, proc_file_version;
if (!fscanf(in, "%x\n", &temp)) return 0; // Read 8 hex values from start of file.
num_header_words = (int)(0x012345678 - temp); for (i = 0; i < 8; i++) {
//fprintf(stderr, "\nproc_file_version: %x, %x", num_header_words, temp); if (!fscanf(in, "%x\n", &temp[i])) {
if (!fscanf(in, "%x\n", &temp)) return 0; return 0;
proc_file_version = (int)(-1 - temp); }
//fprintf(stderr, "\nproc_file_version: %x, %x", proc_file_version, temp); }
if (!fscanf(in, "%x\n", &temp)) return 0; num_header_words = (int)(0x012345678 - temp[0]);
file_i = (int)(-1 - temp); proc_file_version = (int)(-1 - temp[1]);
//fprintf(stderr, "\nbyte count: %d, %x", file_i, temp); file_i = (int)(-1 - temp[2]);
if (!fscanf(in, "%x\n", &temp)) return 0; file_board_id = (uint32_t)(-1 - temp[3]);
file_board_id = (uint32_t)(-1 - temp); min_board_rev = (int)(-1 - temp[4]);
//fprintf(stderr, "\nid: %x, %x", file_board_id, temp); max_board_rev = (int)(-1 - temp[5]);
if (!fscanf(in, "%x\n", &temp)) return 0; file_checksum = (uint32_t)(-1 - temp[6]);
min_board_rev = (int)(-1 - temp); header_checksum = (uint32_t)(-1 - temp[7]);
//fprintf(stderr, "\nmin_board: %d, %x", min_board_rev, temp);
if (!fscanf(in, "%x\n", &temp)) return 0;
max_board_rev = (int)(-1 - temp);
//fprintf(stderr, "\nmax_board: %d, %x", max_board_rev, temp);
if (!fscanf(in, "%x\n", &temp)) return 0;
file_checksum = (uint32_t)(-1 - temp);
//fprintf(stderr, "\nchecksum: %d, %x", file_checksum, temp);
if (!fscanf(in, "%x\n", &temp)) return 0;
header_checksum = (uint32_t)(-1 - temp);
//fprintf(stderr, "\nheader_checksum: %d, %x", header_checksum, temp);
uint32_t readdata[4], board_rev, board_id; fprintf(stderr, "File version %d, for board_id 0x%08X (board revs %d to %d)\n",
PRReadData(proc, 0, 0, 4, readdata); proc_file_version, file_board_id, min_board_rev, max_board_rev);
fprintf(stderr, "%d-byte data checksum 0x%08X, header checksum 0x%08X\n",
file_i, file_checksum, header_checksum);
uint32_t readdata[4], board_id;
PRReadData(proc, P_ROC_MANAGER_SELECT, P_ROC_REG_CHIP_ID_ADDR, 4, readdata);
board_id = readdata[0]; board_id = readdata[0];
board_rev = readdata[3]; int board_rev = 0;
if (board_id == P3_ROC_CHIP_ID) { if (board_id == P3_ROC_CHIP_ID) {
board_rev = (board_rev & 0x800) >> 11 | board_rev = (readdata[3] & 0x800) >> 11 |
(board_rev & 0x400) >> 10 | (readdata[3] & 0x400) >> 10 |
(board_rev & 0x200) >> 9 | (readdata[3] & 0x200) >> 9 |
(board_rev & 0x100) >> 8; (readdata[3] & 0x100) >> 8;
fprintf(stderr, "\nReading P3-ROC board_rev: %d", board_rev); fprintf(stderr, "Connected to P3-ROC (board rev %d).\n", board_rev);
}
else if (board_id == P_ROC_CHIP_ID) {
board_rev = (readdata[3] & 0x80) >> 7 |
(readdata[3] & 0x40) >> 6 |
(readdata[3] & 0x20) >> 5 |
(readdata[3] & 0x10) >> 4;
fprintf(stderr, "Connected to P-ROC (board rev %d).\n", board_rev);
} }
else { else {
board_rev = (board_rev & 0x80) >> 7 | fprintf(stderr, "Connected to unrecognized hardware (0x%08X).\n",
(board_rev & 0x40) >> 6 | board_id);
(board_rev & 0x20) >> 5 | return 0;
(board_rev & 0x10) >> 4;
fprintf(stderr, "\nReading P-ROC board_rev: %d", board_rev);
} }
fprintf(stderr, "Current FPGA version: %u.%u\n",
readdata[1] >> 16, readdata[1] & 0xffff);
if (proc_file_version != 0) { if (proc_file_version != 0) {
fprintf(stderr, "\nERROR: Unsupported .p-roc file version: %x. Check for an updated version of this tool.\n\n", proc_file_version); fprintf(stderr, "ERROR: Unsupported .p-roc file version: 0x%x. Check for an updated version of this tool.\n\n",
proc_file_version);
return 0; return 0;
} }
// Check for valid board ID and rev // Check for valid board ID and rev
if (board_id != file_board_id) { if (board_id == file_board_id) {
fprintf(stderr, "\nERROR: board type mismatch."); fprintf(stderr, "Board ID verified\n");
if (board_id == P_ROC_CHIP_ID && file_board_id == P3_ROC_CHIP_ID) }
fprintf(stderr, "\nCannot program a P3-ROC image onto a P-ROC\n\n"); else {
else if (board_id == P3_ROC_CHIP_ID && file_board_id == P_ROC_CHIP_ID) if (board_id == P_ROC_CHIP_ID && file_board_id == P3_ROC_CHIP_ID) {
fprintf(stderr, "\nCannot program a P-ROC image onto a P3-ROC\n\n"); fprintf(stderr, "ERROR: Cannot program a P3-ROC image onto a P-ROC\n\n");
else fprintf(stderr, "\nImage (0x%08X) and board (0x%08X) are incompatible\n\n", file_board_id, board_id); }
else if (board_id == P3_ROC_CHIP_ID && file_board_id == P_ROC_CHIP_ID) {
fprintf(stderr, "ERROR: Cannot program a P-ROC image onto a P3-ROC\n\n");
}
else {
fprintf(stderr, "ERROR: Image (0x%08X) and board (0x%08X) are incompatible\n\n",
file_board_id, board_id);
}
return 0; return 0;
} }
else fprintf(stderr, "\nBoard ID verified");
if (board_rev > max_board_rev) {
fprintf(stderr, "\nERROR: board_rev %d > max_board_rev %d", board_rev, max_board_rev);
}
if (board_rev < min_board_rev) {
fprintf(stderr, "\nERROR: board_rev < min_board_rev");
}
if (board_rev > max_board_rev || board_rev < min_board_rev) { if (board_rev > max_board_rev || board_rev < min_board_rev) {
fprintf(stderr, "\nERROR: This image is not compatible with the P-ROC board (rev: %x)", board_id); fprintf(stderr, "ERROR: Board rev %d not between %d and %d\n",
board_rev, min_board_rev, max_board_rev);
return 0; return 0;
} }
else fprintf(stderr, "\nBoard rev verified");
checksum = 0; checksum = 0;
i = 0; i = 0;
while (!feof(in)) while (!feof(in)) {
{
data = (unsigned char)fgetc( in ); data = (unsigned char)fgetc( in );
checksum += data; checksum += data;
i++; i++;
@@ -2235,7 +2226,7 @@ uint32_t checkPROCFile() {
if ((i != file_i) || if ((i != file_i) ||
(checksum != file_checksum) || (checksum != file_checksum) ||
(header_checksum != new_header_checksum)) { (header_checksum != new_header_checksum)) {
fprintf(stderr, "\nFPGA data verification failure!\n\n"); fprintf(stderr, "FPGA data verification failure!\n\n");
return 0; return 0;
} }
@@ -2264,11 +2255,8 @@ int main( int argc, char** argv )
int i; int i;
int iErrorCode; int iErrorCode;
// Set a signal handler so that we can exit gracefully on Ctrl-C:
//signal(SIGINT, sigint);
iErrorCode = XSVF_ERRORCODE( XSVF_ERROR_NONE ); iErrorCode = XSVF_ERRORCODE( XSVF_ERROR_NONE );
pzXsvfFileName = 0; pzXsvfFileName = NULL;
//printf( "XSVF Player v%s, Xilinx, Inc.\n", XSVF_VERSION ); //printf( "XSVF Player v%s, Xilinx, Inc.\n", XSVF_VERSION );
@@ -2296,26 +2284,22 @@ int main( int argc, char** argv )
// Check for .p-roc file // Check for .p-roc file
if (strstr(pzXsvfFileName, ".p-roc")) { if (strstr(pzXsvfFileName, ".p-roc")) {
fprintf(stderr, "\nP-ROC file format detected\n."); fprintf(stderr, "P-ROC file format detected.\n");
in = fopen( pzXsvfFileName, "rb" ); in = fopen( pzXsvfFileName, "rb" );
if ( !in ) { if ( !in ) {
fprintf(stderr, "ERROR: Cannot open file %s\n.", pzXsvfFileName ); fprintf(stderr, "ERROR: Cannot open file '%s'.\n", pzXsvfFileName );
iErrorCode = XSVF_ERRORCODE( XSVF_ERROR_UNKNOWN ); iErrorCode = XSVF_ERRORCODE( XSVF_ERROR_UNKNOWN );
} }
else { else {
if (openPROC()) { if (openPROC()) {
fprintf(stderr, "\nVerifying file contents and board compatibility..."); fprintf(stderr, "Verifying file contents and board compatibility...\n");
switch (checkPROCFile()) switch (checkPROCFile()) {
{
case P_ROC_CHIP_ID: case P_ROC_CHIP_ID:
rewind(in);
preparePROCFile(); preparePROCFile();
processFile(); processFile();
break; break;
case P3_ROC_CHIP_ID: case P3_ROC_CHIP_ID:
rewind(in);
preparePROCFile(); preparePROCFile();
processP3ROCFile(); processP3ROCFile();
break; break;
@@ -2323,35 +2307,38 @@ int main( int argc, char** argv )
fprintf(stderr, "Failed to parse file.\n"); fprintf(stderr, "Failed to parse file.\n");
break; break;
} }
// Destroy the P-ROC device handle created by openPROC()
PRDelete(proc);
proc = kPRHandleInvalid;
} }
} }
} }
// Check for .p-roc file // Check for .xsvf file
else if (strstr(pzXsvfFileName, ".xsvf")) { else if (strstr(pzXsvfFileName, ".xsvf")) {
fprintf(stderr, "\nXSVF file format detected.\n"); fprintf(stderr, "XSVF file format detected.\n");
in = fopen( pzXsvfFileName, "rb" ); in = fopen( pzXsvfFileName, "rb" );
if ( !in ) { if ( !in ) {
fprintf(stderr, "ERROR: Cannot open file %s\n.", pzXsvfFileName ); fprintf(stderr, "ERROR: Cannot open file '%s'.\n", pzXsvfFileName );
iErrorCode = XSVF_ERRORCODE( XSVF_ERROR_UNKNOWN ); iErrorCode = XSVF_ERRORCODE( XSVF_ERROR_UNKNOWN );
} }
else { else {
numBytesTotal = getNumFileBytes();
rewind(in);
if (openPROC()) { if (openPROC()) {
fseek(in, 0, SEEK_END);
numBytesTotal = ftell(in);
rewind(in);
processFile(); processFile();
} }
} }
} }
else { else {
fprintf(stderr, "\nERROR: Unsupported file format.\n"); fprintf(stderr, "ERROR: Unsupported file format.\n");
printUsage(argv[0]); printUsage(argv[0]);
} }
} }
if (proc != kPRHandleInvalid) {
// Destroy the P-ROC device handle created by openPROC()
PRDelete(proc);
proc = kPRHandleInvalid;
}
return( iErrorCode ); return( iErrorCode );
} }
#endif /* XSVF_MAIN */ #endif /* XSVF_MAIN */