Permalink
Browse files

Updated C example

  • Loading branch information...
1 parent 1358b70 commit d558c061de6fa56eca6b9af2d48092769a4a47ab makestuff committed Mar 21, 2013
Showing with 64 additions and 78 deletions.
  1. +64 −78 examples/c/main.c
View
@@ -19,13 +19,7 @@
#include <libfpgalink.h>
#include "args.h"
-#define CHECK(x) \
- if ( status != FL_SUCCESS ) { \
- returnCode = x; \
- fprintf(stderr, "%s\n", error); \
- flFreeError(error); \
- goto cleanup; \
- }
+#define CHECK(x) if ( status != FL_SUCCESS ) { FAIL(x); }
int main(int argc, const char *argv[]) {
int returnCode;
@@ -39,11 +33,10 @@ int main(int argc, const char *argv[]) {
uint32 fileLen;
uint8 *buffer = NULL;
uint32 numDevices, scanChain[16], i;
- const char *vp = NULL, *ivp = NULL, *jtagPort = NULL, *xsvfFile = NULL, *dataFile = NULL;
- bool scan = false, usbPower = false;
+ const char *vp = NULL, *ivp = NULL, *queryPort = NULL, *portConfig = NULL, *progConfig = NULL, *dataFile = NULL;
const char *const prog = argv[0];
- printf("FPGALink \"C\" Example Copyright (C) 2011 Chris McClelland\n\n");
+ printf("FPGALink \"C\" Example Copyright (C) 2011-2013 Chris McClelland\n\n");
argv++;
argc--;
while ( argc ) {
@@ -56,46 +49,35 @@ int main(int argc, const char *argv[]) {
usage(prog);
FAIL(0);
break;
- case 's':
- scan = true;
+ case 'q':
+ GET_ARG("q", queryPort, 2);
break;
- case 'p':
- usbPower = true;
+ case 'd':
+ GET_ARG("d", portConfig, 3);
break;
case 'v':
- GET_ARG("v", vp, 2);
+ GET_ARG("v", vp, 4);
break;
case 'i':
- GET_ARG("i", ivp, 3);
+ GET_ARG("i", ivp, 5);
break;
- case 'j':
- GET_ARG("j", jtagPort, 4);
- break;
- case 'x':
- GET_ARG("x", xsvfFile, 5);
+ case 'p':
+ GET_ARG("p", progConfig, 6);
break;
case 'f':
- GET_ARG("f", dataFile, 6);
+ GET_ARG("f", dataFile, 7);
break;
default:
invalid(prog, argv[0][1]);
- FAIL(7);
+ FAIL(8);
}
argv++;
argc--;
}
if ( !vp ) {
missing(prog, "v <VID:PID>");
- FAIL(8);
- }
-
- if ( !ivp && jtagPort ) {
- fprintf(stderr, "You can't specify --j without -i");
FAIL(9);
}
- if ( !jtagPort ) {
- jtagPort = "D0234";
- }
status = flInitialise(0, &error);
CHECK(10);
@@ -106,7 +88,7 @@ int main(int argc, const char *argv[]) {
if ( ivp ) {
int count = 60;
printf("Loading firmware into %s...\n", ivp);
- status = flLoadStandardFirmware(ivp, vp, jtagPort, &error);
+ status = flLoadStandardFirmware(ivp, vp, &error);
CHECK(11);
printf("Awaiting renumeration");
@@ -134,18 +116,18 @@ int main(int argc, const char *argv[]) {
}
}
- if ( usbPower ) {
- printf("Connecting USB power to FPGA...\n");
- status = flPortAccess(handle, 3, 0x80, 0x80, 0x80, NULL, &error);
+ if ( portConfig ) {
+ printf("Configuring ports...\n");
+ status = flPortConfig(handle, portConfig, &error);
CHECK(16);
flSleep(100);
}
isNeroCapable = flIsNeroCapable(handle);
isCommCapable = flIsCommCapable(handle);
- if ( scan ) {
+ if ( queryPort ) {
if ( isNeroCapable ) {
- status = flScanChain(handle, &numDevices, scanChain, 16, &error);
+ status = jtagScanChain(handle, queryPort, &numDevices, scanChain, 16, &error);
CHECK(17);
if ( numDevices ) {
printf("The FPGALink device at %s scanned its JTAG chain, yielding:\n", vp);
@@ -161,31 +143,29 @@ int main(int argc, const char *argv[]) {
}
}
- if ( xsvfFile ) {
- printf("Playing \"%s\" into the JTAG chain on FPGALink device %s...\n", xsvfFile, vp);
+ if ( progConfig ) {
+ printf("Executing programming configuration \"%s\" on FPGALink device %s...\n", progConfig, vp);
if ( isNeroCapable ) {
- status = flPlayXSVF(handle, xsvfFile, &error);
+ status = flProgram(handle, progConfig, NULL, &error);
CHECK(19);
} else {
- fprintf(stderr, "XSVF play requested but device at %s does not support NeroJTAG\n", vp);
+ fprintf(stderr, "Program operation requested but device at %s does not support NeroProg\n", vp);
FAIL(20);
}
}
- if ( dataFile && !isCommCapable ) {
- fprintf(stderr, "Data file load requested but device at %s does not support CommFPGA\n", vp);
- FAIL(21);
- }
-
- if ( isCommCapable ) {
- printf("Zeroing registers 1 & 2...\n");
- byte = 0x00;
- status = flWriteChannel(handle, 1000, 0x01, 1, &byte, &error);
- CHECK(22);
- status = flWriteChannel(handle, 1000, 0x02, 1, &byte, &error);
- CHECK(23);
-
- if ( dataFile ) {
+ if ( dataFile ) {
+ if ( isCommCapable ) {
+ printf("Enabling FIFO mode...\n");
+ status = flFifoMode(handle, true, &error);
+ CHECK(21);
+ printf("Zeroing registers 1 & 2...\n");
+ byte = 0x00;
+ status = flWriteChannel(handle, 1000, 0x01, 1, &byte, &error);
+ CHECK(22);
+ status = flWriteChannel(handle, 1000, 0x02, 1, &byte, &error);
+ CHECK(23);
+
buffer = flLoadFile(dataFile, &fileLen);
if ( buffer ) {
uint16 checksum = 0x0000;
@@ -202,38 +182,44 @@ int main(int argc, const char *argv[]) {
fprintf(stderr, "Unable to load file %s!\n", dataFile);
FAIL(25);
}
- }
- printf("Reading channel 0...");
- status = flReadChannel(handle, 1000, 0x00, 1, buf, &error);
- CHECK(26);
- printf("got 0x%02X\n", buf[0]);
- printf("Reading channel 1...");
- status = flReadChannel(handle, 1000, 0x01, 1, buf, &error);
- CHECK(27);
- printf("got 0x%02X\n", buf[0]);
- printf("Reading channel 2...");
- status = flReadChannel(handle, 1000, 0x02, 1, buf, &error);
- CHECK(28);
- printf("got 0x%02X\n", buf[0]);
+ printf("Reading channel 0...");
+ status = flReadChannel(handle, 1000, 0x00, 1, buf, &error);
+ CHECK(26);
+ printf("got 0x%02X\n", buf[0]);
+ printf("Reading channel 1...");
+ status = flReadChannel(handle, 1000, 0x01, 1, buf, &error);
+ CHECK(27);
+ printf("got 0x%02X\n", buf[0]);
+ printf("Reading channel 2...");
+ status = flReadChannel(handle, 1000, 0x02, 1, buf, &error);
+ CHECK(28);
+ printf("got 0x%02X\n", buf[0]);
+ } else {
+ fprintf(stderr, "Data file load requested but device at %s does not support CommFPGA\n", vp);
+ FAIL(29);
+ }
}
-
returnCode = 0;
cleanup:
+ if ( error ) {
+ fprintf(stderr, "%s\n", error);
+ flFreeError(error);
+ }
flFreeFile(buffer);
flClose(handle);
return returnCode;
}
void usage(const char *prog) {
- printf("Usage: %s [-hps] -v <VID:PID> [-i <VID:PID>] [-x <xsvfFile>] [-f <dataFile>]\n\n", prog);
+ printf("Usage: %s [-h] [-i <VID:PID>] -v <VID:PID> [-d <portConfig>]\n", prog);
+ printf(" [-q <jtagPort>] [-p <progConfig>] [-f <dataFile>]\n\n");
printf("Load FX2LP firmware, load the FPGA, interact with the FPGA.\n\n");
- printf(" -h print this help and exit\n");
- printf(" -p FPGA is powered from USB (Nexys2 only!)\n");
- printf(" -s scan the JTAG chain\n");
- printf(" -v <VID:PID> renumerated vendor and product ID of the FPGALink device\n");
- printf(" -i <VID:PID> initial vendor and product ID of the FPGALink device\n");
- printf(" -j <jtagPort> JTAG port config (e.g D0234)\n");
- printf(" -x <xsvfFile> SVF, XSVF or CSVF file to play into the JTAG chain\n");
- printf(" -f <dataFile> binary data to write to channel 0\n");
+ printf(" -i <VID:PID> initial vendor and product ID of the FPGALink device\n");
+ printf(" -v <VID:PID> renumerated vendor and product ID of the FPGALink device\n");
+ printf(" -d <portConfig> configure the ports\n");
+ printf(" -q <jtagPort> scan the JTAG chain\n");
+ printf(" -p <progConfig> configuration and programming file\n");
+ printf(" -f <dataFile> binary data to write to channel 0\n");
+ printf(" -h print this help and exit\n");
}

0 comments on commit d558c06

Please sign in to comment.