int SPI_DOWNLOAD_THREAD::fx3_spiboot_download(const char *filename)
{
    unsigned char *fwBuf;
    int r, i;

    // Check if we have a handle to the FX3 flash programmer.
    r = get_fx3_prog_handle();
    if ( r != 0 ) {
        printf("FX3 flash programmer not found\n");
        emit sendDownloadFailStatus(QString("Error: Could not find target device"));
        return -1;
    }

    // Allocate memory for holding the firmware binary.
    fwBuf = (unsigned char *)calloc (1, MAX_FWIMG_SIZE);
    if ( fwBuf == 0 ) {
        printf("Failed to allocate buffer to store firmware binary\n");
        emit sendDownloadFailStatus(QString("Error: Failed to get memory for download\n"));
        //sb->showMessage("Error: Failed to get memory for download\n", 5000);
        return -2;
    }

    if ( read_firmware_image(filename, fwBuf, NULL) ) {
        printf("File %s does not contain valid FX3 firmware image\n", filename);
        emit sendDownloadFailStatus(QString("Error: Failed to find valid FX3 firmware image\n"));
        //sb->showMessage("Error: Failed to find valid FX3 firmware image", 5000);
        free(fwBuf);
        return -3;
    }

    filesize = ROUND_UP(filesize, SPI_PAGE_SIZE);

    int number_setor = (filesize + SPI_SECTOR_SIZE - 1) / SPI_SECTOR_SIZE;
    // Erase as many SPI sectors as are required to hold the firmware binary.
    for (i = 0; i < ((filesize + SPI_SECTOR_SIZE - 1) / SPI_SECTOR_SIZE); i++) {
        r = spi_erase_sector(i,number_setor);
        if (r != 0) {
            printf("Failed to erase SPI flash\n");
            emit sendDownloadFailStatus(QString("Error: Failed to erase SPI flash device"));
            //sb->showMessage("Error: Failed to erase SPI flash device", 5000);
            free(fwBuf);
            return -4;
        }
    }
    r = spi_write(fwBuf, filesize);
    if (r != 0) {
        printf("SPI write failed\n");
        emit sendDownloadFailStatus(QString("SPI Flash programming failed"));
    //	sb->showMessage("SPI Flash programming failed", 5000);
    } else {
        emit sendDownloadFailStatus(QString("Completed writing into SPI FLASH"));
        //sb->showMessage("Completed writing into SPI FLASH", 5000);
    }

    free(fwBuf);
    return r;
}
示例#2
0
int
fx3_spiboot_download (
		cyusb_handle *h,
		const char   *filename)
{
	unsigned char *fwBuf;
	int r, i, filesize;

	// Check if we have a handle to the FX3 flash programmer.
	r = get_fx3_prog_handle (&h);
	if (r != 0) {
		fprintf (stderr, "Error: FX3 flash programmer not found\n");
		return -1;
	}

	// Allocate memory for holding the firmware binary.
	fwBuf = (unsigned char *)calloc (1, MAX_FWIMG_SIZE);
	if (fwBuf == 0) {
		fprintf (stderr, "Error: Failed to allocate buffer to store firmware binary\n");
		return -2;
	}

	if (read_firmware_image (filename, fwBuf, NULL, &filesize)) {
		fprintf (stderr, "Error: File %s does not contain valid FX3 firmware image\n", filename);
		free (fwBuf);
		return -3;
	}

	filesize = ROUND_UP(filesize, SPI_PAGE_SIZE);

	// Erase as many SPI sectors as are required to hold the firmware binary.
	for (i = 0; i < ((filesize + SPI_SECTOR_SIZE - 1) / SPI_SECTOR_SIZE); i++) {
		r = fx3_spi_erase_sector (h, i);
		if (r != 0) {
			fprintf (stderr, "Error: Failed to erase SPI flash\n");
			free (fwBuf);
			return -4;
		}
	}

	r = fx3_spi_write (h, fwBuf, filesize);
	if (r != 0) {
		fprintf (stderr, "Error: SPI write failed\n");
	} else {
		printf ("Info: SPI flash programming completed\n");
	}

	free (fwBuf);
	return r;
}
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;
}
示例#4
0
int
fx3_i2cboot_download (
		cyusb_handle *h,
		const char   *filename)
{
	int romsize, size;
	int address = 0, offset = 0;
	int r, filesize;
	unsigned char *fwBuf = 0;

	// Check if we have a handle to the FX3 flash programmer.
	r = get_fx3_prog_handle (&h);
	if (r != 0) {
		fprintf (stderr, "Error: FX3 flash programmer not found\n");
		return -1;
	}

	// Allocate memory for holding the firmware binary.
	fwBuf = (unsigned char *)calloc (1, MAX_FWIMG_SIZE);

	if (read_firmware_image (filename, fwBuf, &romsize, &filesize)) {
		fprintf (stderr, "Error: File %s does not contain valid FX3 firmware image\n", filename);
		free (fwBuf);
		return -2;
	}

        printf ("Info: Writing firmware image to I2C EEPROM\n");

	filesize = ROUND_UP(filesize, I2C_PAGE_SIZE);
	while (filesize != 0) {

		size = (filesize <= romsize) ? filesize : romsize;
		if (size > I2C_SLAVE_SIZE) {
			r = fx3_i2c_write (h, fwBuf, address, offset, I2C_SLAVE_SIZE);
			if (r == 0) {
				r = fx3_i2c_read_verify (h, fwBuf + offset, address, I2C_SLAVE_SIZE);
				if (r != 0) {
					fprintf (stderr, "Error: Read-verify from I2C EEPROM failed\n");
					free (fwBuf);
					return -3;
				}

				r = fx3_i2c_write (h, fwBuf, address + 4, offset + I2C_SLAVE_SIZE, size - I2C_SLAVE_SIZE);
				if (r == 0) {
					r = fx3_i2c_read_verify (h, fwBuf + offset + I2C_SLAVE_SIZE,
							address + 4, size - I2C_SLAVE_SIZE);
					if (r != 0) {
						fprintf (stderr, "Error: Read-verify from I2C EEPROM failed\n");
						free (fwBuf);
						return -3;
					}
				}
			}
		} else {
			r = fx3_i2c_write (h, fwBuf, address, offset, size);
			if (r == 0) {
				r = fx3_i2c_read_verify (h, fwBuf + offset, address, size);
				if (r != 0) {
					fprintf (stderr, "Error: Read-verify from I2C EEPROM failed\n");
					free (fwBuf);
					return -3;
				}
			}
		}

		if (r != 0) {
			fprintf (stderr, "Error: Write to I2C EEPROM failed\n");
			free (fwBuf);
			return -4;
		}

		/* Move to the next slave address. */
		offset += size;
		filesize -= size;
		address++;
	}

	free (fwBuf);
        printf ("Info: I2C programming completed\n");
	return 0;
}