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


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

/* Побитовый разворот одного байта (Bit Mirroring) */
unsigned char mirror_byte(unsigned char b) {
    b = ((b & 0xF0) >> 4) | ((b & 0x0F) << 4);
    b = ((b & 0xCC) >> 2) | ((b & 0x33) << 2);
    b = ((b & 0xAA) >> 1) | ((b & 0x55) << 1);
    return b;
}

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; }

    /* Принудительно заставляем работать с адресом 0x40 (даже если он занят драйвером) */
    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("[USER_FLASH] Connected to 0x40. Starting direct execution...\n");

    /* 1. Отправляем Ключ Активации (Activation Key) - ПЛИС требует его в оригинальном виде */
    unsigned char act_key[] = { 0xA4, 0xC6, 0xF4, 0x8A };
    if (write(fd, act_key, 4) != 4) { perror("Activation key failed"); return 1; }
    usleep(5000);

    /* 2. Отправляем команду ISC_ENABLE (0xC6 0x00 0x00 0x00) с зеркалированием бит */
    unsigned char isc_enable[] = { mirror_byte(0xC6), 0x00, 0x00, 0x00 };
    if (write(fd, isc_enable, 4) != 4) { perror("ISC_ENABLE failed"); return 1; }
    usleep(10000);

    /* 3. Отправляем команду ISC_ERASE (0x0E 0x01 0x00 0x00) с зеркалированием бит */
    unsigned char isc_erase[] = { mirror_byte(0x0E), mirror_byte(0x01), 0x00, 0x00 };
    if (write(fd, isc_erase, 4) != 4) { perror("ISC_ERASE failed"); return 1; }
    printf("[USER_FLASH] Memory erasing... waiting 100ms\n");
    usleep(100000);

    /* 4. Отправляем команду начала записи LSC_BITSTREAM_BURST (0x7A 0x00 0x00 0x00) */
    unsigned char burst_cmd[] = { mirror_byte(0x7A), 0x00, 0x00, 0x00 };
    if (write(fd, burst_cmd, 4) != 4) { perror("Burst command failed"); return 1; }
    usleep(2000);

    /* 5. Читаем файл .bit, отрезаем текстовый заголовок Radiant (359 байт) и льем данные */
    fseek(f_bit, 359, SEEK_SET);
    unsigned char block[512];
    size_t read_bytes;
    size_t total_sent = 0;

    printf("[USER_FLASH] Streaming payload data directly into SCL/SDA wires...\n");
    while ((read_bytes = fread(block, 1, sizeof(block), f_bit)) > 0) {
        for (size_t i = 0; i < read_bytes; i++) {
            block[i] = mirror_byte(block[i]);
        }
        if (write(fd, block, read_bytes) != read_bytes) {
            perror("Write payload error");
            return 1;
        }
        total_sent += read_bytes;
    }
    printf("[USER_FLASH] Sent %zu bytes of bitstream.\n", total_sent);
    usleep(5000);

    /* 6. Добивка тактов заполнения (Padding) для очистки FIFO */
    unsigned char padding[1024];
    for(int i=0; i<1024; i++) padding[i] = 0xFF;
    write(fd, padding, 1024);

    /* 7. Команда финализации ISC_DISABLE (0x26 0x00 0x00 0x00) */
    unsigned char isc_disable[] = { mirror_byte(0x26), 0x00, 0x00, 0x00 };
    write(fd, isc_disable, 4);
    usleep(5000);

    /* 8. Мягкий перезапуск LSC_REFRESH (0x79 0x00 0x00 0x00) */
    unsigned char refresh[] = { mirror_byte(0x79), 0x00, 0x00, 0x00 };
    write(fd, refresh, 4);

    printf("[USER_FLASH] Execution complete. Check the LED! Run i2ctransfer status now.\n");
    fclose(f_bit);
    close(fd);
    return 0;
}