⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 ST flash driver
💻 C
📖 第 1 页 / 共 3 页
字号:
    if (num<0 || num>100)
    {
        num = 0;
        x = 0;
    }

    printf("num = %d\n", num);

    addr = 4;

    if (num == 0)
    {
        sysFlash->Write(sysFlash, FLASH_PROGRAM_ZIP_DATA_ADDR + addr - FLASH_START_ADDR, 256, (unsigned char *) zipname1);
        addr += 256;
        sysFlash->Write(sysFlash, FLASH_PROGRAM_ZIP_DATA_ADDR + addr - FLASH_START_ADDR, 4, (unsigned char *) &length);
        addr += 4;
        sysFlash->Write(sysFlash, FLASH_PROGRAM_ZIP_DATA_ADDR + addr - FLASH_START_ADDR, (int) length, data);
    }
    else
    {
        for (i = 0; i < num; i++)
        {
            printf("No %d:\n", i);

            addr += 256;
            memcpy((void *) &x, (const void *) (FLASH_PROGRAM_ZIP_DATA_ADDR + addr), 4);

            if (x % 4 != 0)
            {
                x = x + 4 - x % 4;
            }

            addr += 4 + (int) x;

            printf("len=%ld ; addr=%d\n\n", x, addr);
        }

        sysFlash->Write(sysFlash, FLASH_PROGRAM_ZIP_DATA_ADDR + addr - FLASH_START_ADDR, 256, (unsigned char *) zipname1);
        addr += 256;
        sysFlash->Write(sysFlash, FLASH_PROGRAM_ZIP_DATA_ADDR + addr - FLASH_START_ADDR, 4, (unsigned char *) &length);
        addr += 4;
        sysFlash->Write(sysFlash, FLASH_PROGRAM_ZIP_DATA_ADDR + addr - FLASH_START_ADDR, (int) length, data);
    }

    num++;

    sysFlash->Write(sysFlash, FLASH_PROGRAM_ZIP_DATA_ADDR - FLASH_START_ADDR, 4, (unsigned char *) &num);

    return 0;
}

unsigned char * FLoadDataFromFlash(const char *zipname, int *length)
{
    int i, x, num, addr;

    *length = 0;

    memcpy((void *) &num, (const void *) FLASH_PROGRAM_ZIP_DATA_ADDR, 4);

    if (num<0 || num>100)
    {
        print("Read num error.\n");
        return NULL;
    }

    addr = 4;

    for (i = 0; i < num; i++)
    {
        if (!memcmp((const void *) zipname, (const void *) (FLASH_PROGRAM_ZIP_DATA_ADDR + addr), strlen(zipname)))
        {
            addr += 256;

            memcpy((void *) &x, (const void *) (FLASH_PROGRAM_ZIP_DATA_ADDR + addr), 4);

            if (x < 0)
            {
                print("Read data length error.\n");
                return NULL;
            }

            *length = x;
            addr += 4;

            return (unsigned char *) (addr + FLASH_PROGRAM_ZIP_DATA_ADDR);
        }

        addr += 256;
        memcpy((void *) &x, (const void *) (FLASH_PROGRAM_ZIP_DATA_ADDR + addr), 4);

        if (x % 4 != 0)
        {
            x = x + 4 - x % 4;
        }

        addr += 4 + x;
    }

    print("Can not find name : %s\n", zipname);

    return NULL;
}

int GetZipData()
{
    int     len, result;
    char    zipname[256], filename[256];
    unsigned char * buf = NULL;
    FILE   *dfile;

    printf("\nPlease input zipname in flash : ");
    scanf("%s", zipname);
    printf("\n");

    buf = LoadDataFromFlash(zipname, &len);
    if (buf != NULL && len > 0)
    {
        printf("Please input file name to write : ");
        scanf("%s", filename);
        printf("\n");

        dfile = fopen(filename, "wb");

        if (dfile == NULL)
        {
            printf("Open %s for write error.\n", filename);
            return -1;
        }

        result = fwrite(buf, 1, len, dfile);
        if (result != len)
        {
            printf("write to file error.\n");
            return -1;
        }

        fclose(dfile);
    }

    return 0;
}


typedef struct
{
    U16 bfType;
    U32 bfSize;
    U16 bfReserved1;
    U16 bfReserved2;
    U32 bfOffBits;
} BITMAPFILEHEADER;

/*------ bitmapinfoheader ------------------------------------------------------*/
typedef struct
{
    U32 biSize;
    U32 biWidth;
    U32 biHeight;
    U16 biPlanes;
    U16 biBitCount;
    U32 biCompression;
    U32 biSizeImage;
    U32 biXPelsPerMeter;
    U32 biYPelsPerMeter;
    U32 biClrUsed;
    U32 biClrImportant;
} BITMAPINFOHEADER;

int burningZipdata(int compresslevel)
{
    int     result, i, j;
    char    zipname[256], imgfile[256];
    FILE   *dfile;
    unsigned char * buf;
    BITMAPFILEHEADER    BmpHead;
    BITMAPINFOHEADER    BmpInfoHead;
    buf = (unsigned char *) sk_Malloc(800000);

    if (buf == NULL)
    {
        printf("Error happened when call malloc() func.\n");
        return -1;
    }

    printf("\nPlease input the image file name to be writed into flash : ");
    scanf("%s", imgfile);
    printf("\n");

    dfile = fopen(imgfile, "rb");
    if (dfile == NULL)
    {
        printf("Open image file error.\n");
        free(buf);
        return -1;
    }

    //zhujunzhi add at 2003.10.31
    fseek(dfile, 0, 0);
    fread(&BmpHead, 1, 14, dfile);
    fread(&BmpInfoHead, 1, 40, dfile);
    fseek(dfile, 0, 0);
    result = fread(buf, 1, 800000, dfile);
    //fseek(dfile, BmpHead.bfOffBits, SEEK_SET);

    //printf("  %d  %d width is %d heith is %d offset is %d\n",sizeof(BITMAPINFOHEADER),sizeof(BITMAPFILEHEADER),BmpInfoHead.biWidth,BmpInfoHead.biHeight,BmpHead.bfSize);
    if (BmpInfoHead.biWidth % 4)
    {
        j = (4 - BmpInfoHead.biWidth % 4);
        //BmpInfoHead.biWidth+=j;
        printf(" j is %d \n", j);
    }
    else
    {
        j = 0;
    }



    for (i = 0; i <= BmpInfoHead.biHeight - 1; i++)
    {
        fseek(dfile, 1078 + (BmpInfoHead.biHeight - 1 - i) * (BmpInfoHead.biWidth + j), 0);
        fread(buf + i * BmpInfoHead.biWidth, 1, BmpInfoHead.biWidth + j, dfile);
        //printf(" i is %d \n", i);
    }

    if (result <= 0)
    {
        printf("Read image file error.\n");
        free(buf);
        return -1;
    }
    fclose(dfile);
    printf("qqq3\n");
    printf("Please input zipname : ");
    scanf("%s", zipname);
    printf("\n");

    result = Write2FlashZip(buf, result - 1078, zipname, compresslevel, 0);

    free(buf);
    return result;
}

int DeleteZipInFlash()
{
    int i, n, x, num, addr, addr1, addr2, addr3;
    unsigned char * buf;

    printf("\nPlease input which image to be deleted(1-n)? ");
    scanf("%d", &n);
    printf("\n");

    if (n <= 0 || n > 100)
    {
        free(buf);
        return -1;
    }

    memcpy((void *) &num, (const void *) FLASH_PROGRAM_ZIP_DATA_ADDR, 4);

    if (num <= 0 || num > 100)
    {
        printf("Read num error.\n");
        return -1;
    }

    if (n > num)
    {
        printf("There are total %d images in flash.\n", num);
        return -1;
    }


    addr = 4;

    for (i = 0; i < n - 1; i++)
    {
        addr += 256;
        memcpy((void *) &x, (const void *) (FLASH_PROGRAM_ZIP_DATA_ADDR + addr), 4);

        if (x % 4 != 0)
        {
            x = x + 4 - x % 4;
        }

        addr += 4 + x;
    }

    printf("count=%d ; addr=%d\n\n", i, addr);
    addr1 = addr;

    addr = 4;

    for (i = 0; i < n; i++)
    {
        addr += 256;
        memcpy((void *) &x, (const void *) (FLASH_PROGRAM_ZIP_DATA_ADDR + addr), 4);

        if (x % 4 != 0)
        {
            x = x + 4 - x % 4;
        }

        addr += 4 + x;
    }

    printf("count=%d ; addr=%d\n\n", i, addr);
    addr2 = addr;

    addr = 4;
    for (i = 0; i < num; i++)
    {
        addr += 256;
        memcpy((void *) &x, (const void *) (FLASH_PROGRAM_ZIP_DATA_ADDR + addr), 4);

        if (x % 4 != 0)
        {
            x = x + 4 - x % 4;
        }

        addr += 4 + x;
    }

    printf("count=%d ; addr=%d\n\n", i, addr);
    addr3 = addr;

    num--;
    sysFlash->Write(sysFlash, FLASH_PROGRAM_ZIP_DATA_ADDR - FLASH_START_ADDR, 4, (unsigned char *) &num);

    if (addr3 == addr2)
    {
        addr2 = addr1;
    }

    buf = (unsigned char *) sk_Malloc(addr3 - addr2);

    memcpy((void *) buf, (const void *) (FLASH_PROGRAM_ZIP_DATA_ADDR + addr2), addr3 - addr2);
    sysFlash->Write(sysFlash, FLASH_PROGRAM_ZIP_DATA_ADDR - FLASH_START_ADDR + addr1, addr3 - addr2, buf);

    free(buf);
    return 0;
}

int ListZipInFlash()
{
    int     i, x, addr, num;
    char    buf[256];

    memcpy((void *) &num, (const void *) FLASH_PROGRAM_ZIP_DATA_ADDR, 4);

    if (num <= 0 || num > 100)
    {
        printf("Read num error.\n");
        return -1;
    }

    addr = 4;
    for (i = 0; i < num; i++)
    {
        memset(buf, 0, 256);
        sysFlash->Read(sysFlash, FLASH_PROGRAM_ZIP_DATA_ADDR + addr - FLASH_START_ADDR, 256, (unsigned char *) buf);

        addr += 256;
        memcpy((void *) &x, (const void *) (FLASH_PROGRAM_ZIP_DATA_ADDR + addr), 4);

        printf("%d : %s %d\n", i + 1, buf, x);

        if (x % 4 != 0)
        {
            x = x + 4 - x % 4;
        }

        addr += 4 + x;
    }

    return 0;
}

int GetFlashMirrorImage()
{
    int     result, size;
    char    filename[256];
    FILE   *imgfile;

    printf("\nPlease input image file name : ");
    scanf("%s", filename);
    printf("\n");

    //SetVerInfo();

    imgfile = fopen(filename, "wb");
    if (imgfile == NULL)
    {
        printf("Open %s for write error, now exit...\n", filename);
        return -1;
    }

    size = 4 * 1024 * 1024;
    result = fwrite((unsigned char *) FLASH_START_ADDR, 1, size, imgfile);
    if (result != size)
    {
        printf("Write to %s error, now exit...\n", filename);
        fclose(imgfile);
        return -1;
    }

    fclose(imgfile);
    return 0;
}

int WriteFlashMirrorImage()
{
    int         result1, size, blocksize, offset;
    char        filename[256];
    long int    fp, curwadd = 0x7fc00000, curtnum = 0;
    char       *pCache;

    blocksize = 0x40000;
    pCache = malloc(blocksize);
    if (pCache == NULL)
    {
        printf("\nMalloc Cache mem error. exit...\n");
        return -1;
    }

    printf("\nPlease input image file name : ");
    scanf("%s", filename);
    printf("\n");

    if ((fp = debugopen(filename, "rb")) == -1)
    {
        printf("Open %s for read error, now exit...\n", filename);
        free(pCache);
        return -1;
    }

    size = 4 * 1024 * 1024;
    offset = 0;

    while (size > 0)
    {
        curtnum += blocksize;
        //printf("ROMTOOL> Flash 0x%08lx bytes writed! (From 0x%08lx to 0x%08lx)\n", curtnum, curwadd, (curtnum + curwadd));
        printf("ROMTOOL> Writing (bin) to Flash 0x%08lx (%08lx bytes) -> Total= %08lx bytes(%6.2f%%) \n",
                (curwadd + offset),
                blocksize,
                curtnum,
                (curtnum * 1.0 * 100 / 0x400000));

        if (blocksize > size)
        {
            blocksize = size;
        }

        if ((result1 = debugread(fp, pCache, blocksize)) != blocksize)
        {
            printf("read file %s error, now exit...\n", filename);
            debugclose(fp);
            free(pCache);

            return -1;
        }
        size -= blocksize;

        sysFlash->Write(sysFlash, offset, blocksize, (unsigned char *) pCache);

        offset += blocksize;
    }

    debugclose(fp);
    free(pCache);

    return 0;
}


int WriteFlashMirrorImage2M()
{
    int         result1, size, blocksize, offset;
    char        filename[256];
    long int    fp, curwadd = 0x7fe00000, curtnum = 0;
    char       *pCache;

    blocksize = 0x20000;
    pCache = malloc(blocksize);
    if (pCache == NULL)
    {
        printf("\nMalloc Cache mem error. exit...\n");
        return -1;
    }

    printf("\nPlease input image file name : ");
    scanf("%s", filename);
    printf("\n");

    if ((fp = debugopen(filename, "rb")) == -1)
    {
        printf("Open %s for read error, now exit...\n", filename);
        free(pCache);
        return -1;
    }

    size = 2 * 1024 * 1024;
    offset = 0;

    while (size > 0)
    {
        curtnum += blocksize;
        //printf("ROMTOOL> Flash 0x%08lx bytes writed! (From 0x%08lx to 0x%08lx)\n", curtnum, curwadd, (curtnum + curwadd));
        printf("ROMTOOL> Writing (bin) to Flash 0x%08lx (%08lx bytes) -> Total= %08lx bytes(%6.2f%%) \n",
                (curwadd + offset),
                blocksize,
                curtnum,
                (curtnum * 1.0 * 100 / 0x200000));

        if (blocksize > size)
        {
            blocksize = size;
        }

        if ((result1 = debugread(fp, pCache, blocksize)) != blocksize)
        {
            printf("read file %s error, now exit...\n", filename);
            debugclose(fp);
            free(pCache);

            return -1;
        }
        size -= blocksize;

        sysFlash->Write(sysFlash, offset, blocksize, (unsigned char *) pCache);

        offset += blocksize;
    }

    debugclose(fp);
    free(pCache);

    return 0;
}


int WriteArrayToFile()
{
#ifdef ARRAY
    int     result;
    char    filename[256];
    FILE   *dfile;

    printf("Please input filename:");
    scanf("%s", filename);
    printf("\n");

    dfile = fopen(filename, "wb");
    if (dfile == NULL)
    {
        printf("Open %s for write error.\n", filename);
        return -1;
    }

    result = fwrite(imgdata, 1, sizeof(imgdata), dfile);

    if (result != sizeof(imgdata))
    {
        printf("write to file error.\n");
        fclose(dfile);
        return -1;
    }

    fclose(dfile);
#endif

    return 0;
}


int flash_erase(void)
{
    int i;

    for (i = 0; i < 64; i++)
    {
        sysFlash->BlockErase(sysFlash, 65536, i * 65536);
    }

    return 0;
}

int GetDataFromFlash(void)
{
    int i, addr, len, result;
    unsigned char x;
    FILE   *dfile;

    printf("\nPlease input address (hex) : ");
    scanf("%x", &addr);
    printf("\nPlease input length (not more than 256) : ");
    scanf("%d", &len);

    printf("\naddr = %x, len = %x\n", addr, len);

    dfile = fopen("flash.bin", "wb");

    if (dfile == NULL)
    {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -