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;
}
示例#3
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;
}