static int fx3_ram_write ( cyusb_handle *h, unsigned char *buf, unsigned int ramAddress, int len) { int r; int index = 0; int size; while (len > 0) { size = (len > MAX_WRITE_SIZE) ? MAX_WRITE_SIZE : len; r = cyusb_control_transfer (h, 0x40, 0xA0, GET_LSW(ramAddress), GET_MSW(ramAddress), &buf[index], size, VENDORCMD_TIMEOUT); if (r != size) { fprintf (stderr, "Error: Vendor write to FX3 RAM failed\n"); return -1; } ramAddress += size; index += size; len -= size; } return 0; }
GLOBAL VOID SMSW IFN1( IU32 *, pop1 ) { *pop1 = GET_MSW(); }
int SPI_DOWNLOAD_THREAD::ram_write(unsigned char *buf, unsigned int ramAddress, int len) { int r; int index = 0; int size; while ( len > 0 ) { size = (len > MAX_WRITE_SIZE) ? MAX_WRITE_SIZE : len; r = cyusb_control_transfer(h, 0x40, 0xA0, GET_LSW(ramAddress), GET_MSW(ramAddress), &buf[index], size, VENDORCMD_TIMEOUT); if ( r != size ) { printf("Vendor write to FX3 RAM failed\n"); return -1; } ramAddress += size; index += size; len -= size; } return 0; }
int SPI_DOWNLOAD_THREAD::fx3_usbboot_download(const char *filename) { unsigned char *fwBuf; unsigned int *data_p; unsigned int i, checksum; unsigned int address, length; int r, index; fwBuf = (unsigned char *)calloc (1, MAX_FWIMG_SIZE); if ( fwBuf == 0 ) { printf("Failed to allocate buffer to store firmware binary\n"); sb->showMessage("Error: Failed to get memory for download\n", 5000); return -1; } // Read the firmware image into the local RAM buffer. r = read_firmware_image(filename, fwBuf, NULL); if ( r != 0 ) { printf("Failed to read firmware file %s\n", filename); sb->showMessage("Error: Failed to read firmware binary\n", 5000); free(fwBuf); return -2; } // Run through each section of code, and use vendor commands to download them to RAM. index = 4; checksum = 0; while ( index < filesize ) { data_p = (unsigned int *)(fwBuf + index); length = data_p[0]; address = data_p[1]; if (length != 0) { for (i = 0; i < length; i++) checksum += data_p[2 + i]; r = ram_write(fwBuf + index + 8, address, length * 4); if (r != 0) { printf("Failed to download data to FX3 RAM\n"); sb->showMessage("Error: Write to FX3 RAM failed", 5000); free(fwBuf); return -3; } } else { if (checksum != data_p[2]) { printf ("Checksum error in firmware binary\n"); sb->showMessage("Error: Firmware checksum error", 5000); free(fwBuf); return -4; } r = cyusb_control_transfer(h, 0x40, 0xA0, GET_LSW(address), GET_MSW(address), NULL, 0, VENDORCMD_TIMEOUT); if ( r != 0 ) printf("Ignored error in control transfer: %d\n", r); break; } index += (8 + length * 4); } free(fwBuf); return 0; }