Загрузка данных


#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>

int main(int argc, char *argv[]) {
    if (argc < 3) {
        printf("Usage: %s <i2c-dev-path> <bitstream.bit>\n", argv[0]);
        return 1;
    }

    int fd = open(argv[1], O_RDWR);
    if (fd < 0) { perror("Failed to open I2C bus"); return 1; }
    if (ioctl(fd, I2C_SLAVE_FORCE, 0x40) < 0) { perror("Failed to set slave address"); return 1; }

    FILE *f_bit = fopen(argv[2], "rb");
    if (!f_bit) { perror("Failed to open bit file"); return 1; }

    printf("[RAW_FLASH] Starting execution WITHOUT bit mirroring...\n");

    /* 1. Ключ активации (всегда как есть) */
    unsigned char act_key[] = { 0xA4, 0xC6, 0xF4, 0x8A };
    write(fd, act_key, 4); usleep(5000);

    /* 2. ISC_ENABLE (без зеркалирования) */
    unsigned char isc_enable[] = { 0xC6, 0x00, 0x00, 0x00 };
    write(fd, isc_enable, 4); usleep(10000);

    /* 3. ISC_ERASE (без зеркалирования) */
    unsigned char isc_erase[] = { 0x0E, 0x01, 0x00, 0x00 };
    write(fd, isc_erase, 4); usleep(100000);

    /* 4. LSC_BITSTREAM_BURST (без зеркалирования) */
    unsigned char burst_cmd[] = { 0x7A, 0x00, 0x00, 0x00 };
    write(fd, burst_cmd, 4); usleep(2000);

    /* 5. Потоковая передача файла целиком БЕЗ изменений */
    unsigned char block[4096];
    size_t read_bytes, total_sent = 0;
    while ((read_bytes = fread(block, 1, sizeof(block), f_bit)) > 0) {
        if (write(fd, block, read_bytes) != read_bytes) { perror("Write error"); return 1; }
        total_sent += read_bytes;
    }
    printf("[RAW_FLASH] Sent %zu bytes.\n", total_sent); usleep(5000);

    /* 6. Добивка FIFO */
    unsigned char padding[1024];
    for(int i=0; i<1024; i++) padding[i] = 0xFF;
    write(fd, padding, 1024); usleep(5000);

    /* 7. ISC_DISABLE */
    unsigned char isc_disable[] = { 0x26, 0x00, 0x00, 0x00 };
    write(fd, isc_disable, 4); usleep(5000);

    /* 8. LSC_REFRESH */
    unsigned char refresh[] = { 0x79, 0x00, 0x00, 0x00 };
    write(fd, refresh, 4);

    printf("[RAW_FLASH] Complete! Run i2ctransfer status now.\n");
    fclose(f_bit); close(fd); return 0;
}