Browse Source

changing the bitstream read and push strategy.

It seems that thread heap space is neither large nor consistent across OSs. Specifically on windows I couldn't get a large enough buffer for the whole bitstream in one go, so I'm reading incrementally now
nfactor-troky
nelisky 13 years ago
parent
commit
c5897683be
  1. 97
      libztex.c

97
libztex.c

@ -118,10 +118,10 @@ static int libztex_getFpgaState (struct libztex_device *ztex, struct libztex_fpg
static int libztex_configureFpgaLS (struct libztex_device *ztex, const char* firmware, bool force, char bs) { static int libztex_configureFpgaLS (struct libztex_device *ztex, const char* firmware, bool force, char bs) {
struct libztex_fpgastate state; struct libztex_fpgastate state;
unsigned char buf[8*1024*1024], cs;
ssize_t pos=0; ssize_t pos=0;
int transactionBytes = 2048; int transactionBytes = 2048;
int tries, cnt, i, j; unsigned char buf[transactionBytes], cs;
int tries, cnt, buf_p, i;
FILE *fp; FILE *fp;
if (!libztex_checkCapability(ztex, CAPABILITY_FPGA)) { if (!libztex_checkCapability(ztex, CAPABILITY_FPGA)) {
@ -135,55 +135,70 @@ static int libztex_configureFpgaLS (struct libztex_device *ztex, const char* fir
} }
} }
fp = fopen(firmware, "rb"); for (tries=10; tries>0; tries--) {
if (!fp) {
applog(LOG_ERR, "%s: failed to read firmware '%s'", ztex->repr, firmware);
return -2;
}
while (!feof(fp)) { fp = fopen(firmware, "rb");
buf[pos++] = getc(fp); if (!fp) {
}; applog(LOG_ERR, "%s: failed to read firmware '%s'", ztex->repr, firmware);
pos--; return -2;
applog(LOG_ERR, "%s: read firmware, %d bytes", ztex->repr, pos); }
fclose(fp); cs = 0;
while (pos < transactionBytes && !feof(fp)) {
buf[pos] = getc(fp);
cs += buf[pos++];
};
if (feof(fp))
pos--;
if ( bs<0 || bs>1 ) if ( bs<0 || bs>1 )
bs = libztex_detectBitstreamBitOrder(buf, transactionBytes<pos ? transactionBytes : pos); bs = libztex_detectBitstreamBitOrder(buf, transactionBytes<pos ? transactionBytes : pos);
if ( bs == 1 )
libztex_swapBits(buf, pos);
for (tries=10; tries>0; tries--) { //* Reset fpga
//* Reset fpga
cnt = libztex_resetFpga(ztex); cnt = libztex_resetFpga(ztex);
if (unlikely(cnt < 0)) { if (unlikely(cnt < 0)) {
applog(LOG_ERR, "%s: Failed reset fpga with err %d", ztex->repr, cnt); applog(LOG_ERR, "%s: Failed reset fpga with err %d", ztex->repr, cnt);
continue; continue;
} }
cs = 0;
i = 0; if ( bs == 1 )
while (i < pos) { libztex_swapBits(buf, pos);
j = (i+transactionBytes) > pos ? pos-i : transactionBytes;
cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x32, 0, 0, &buf[i], j, 5000); buf_p = pos;
if (unlikely(cnt < 0)) { while (1) {
applog(LOG_ERR, "%s: Failed send fpga data with err %d", ztex->repr, cnt); i = 0;
break; while (i < buf_p) {
} cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x32, 0, 0, &buf[i], buf_p-i, 5000);
for (j=0; j<cnt; j++) { if (unlikely(cnt < 0)) {
cs = (cs + (buf[i+j] & 0xFF)) & 0xFF; applog(LOG_ERR, "%s: Failed send fpga data with err %d", ztex->repr, cnt);
break;
}
i += cnt;
} }
i += cnt; if (i < buf_p || buf_p < transactionBytes)
} break;
if (i < pos) { buf_p = 0;
continue; while (buf_p < transactionBytes && !feof(fp)) {
} buf[buf_p] = getc(fp);
tries = 0; cs += buf[buf_p++];
libztex_getFpgaState(ztex, &state); };
if (!state.fpgaConfigured) { if (feof(fp))
applog(LOG_ERR, "%s: FPGA configuration failed: DONE pin does not go high", ztex->repr); buf_p--;
return 3; pos += buf_p;
if (buf_p == 0)
break;
if ( bs == 1 )
libztex_swapBits(buf, buf_p);
} }
if (cnt >= 0)
tries = 0;
fclose(fp);
}
libztex_getFpgaState(ztex, &state);
if (!state.fpgaConfigured) {
applog(LOG_ERR, "%s: FPGA configuration failed: DONE pin does not go high", ztex->repr);
return 3;
} }
usleep(200000); usleep(200000);
applog(LOG_ERR, "%s: FPGA configuration done", ztex->repr); applog(LOG_ERR, "%s: FPGA configuration done", ztex->repr);
@ -323,7 +338,7 @@ int libztex_prepare_device (struct libusb_device *dev, struct libztex_device** z
newdev->maxErrorRate[cnt] = 0; newdev->maxErrorRate[cnt] = 0;
} }
cnt = strlen(&buf[buf[0]==4?10:8]); cnt = strlen((char *)&buf[buf[0]==4?10:8]);
newdev->bitFileName = malloc(sizeof(char)*(cnt+1)); newdev->bitFileName = malloc(sizeof(char)*(cnt+1));
memcpy(newdev->bitFileName, &buf[buf[0]==4?10:8], cnt+1); memcpy(newdev->bitFileName, &buf[buf[0]==4?10:8], cnt+1);

Loading…
Cancel
Save