應用程式操作NorFlash範例程式碼分享(norflash介面使用方法)_C 語言

來源:互聯網
上載者:User

複製代碼 代碼如下:

int dealwithnor()
{

//    glob_t mtdbuf;
    struct mtd_info_user mtd;
    struct erase_info_user erase;
    int blocks = 0;
    int i = 0;  //用於控制擦除的塊的個數
    int k = 0;
    int written = 0;  //已寫入的位元組數,只初始化一次
    unsigned int size = StateOfImage.st_size;  //應該是鏡像的實際大小,因為記憶體中大於鏡像的空間的內容不可預知
    unsigned int result = 0;
    unsigned int DevNum = 0;    //裝置的數量
    unsigned int StartDev = 0;  //從第startDev開始擦除
    char DevName[20] = {0};
    unsigned int AllSize = 0;
    #define MAXPARTITIONS 40
    struct DeviceInfo
    {
        int fd;
        char dir[20];
        uint32_t size;     // Total size of the MTD
        uint32_t erasesize;

    }DevInfo[MAXPARTITIONS];//用來存放裝置資訊

    bzero(DevInfo, sizeof(struct DeviceInfo));


/* 這是一種方法,但是有一個缺點,當mtd裝置大於10個是,通過glob搜尋出來的結果
   會出問題,下面採用會採用第二種方法 */
//    if(searchmtd(&mtdbuf) != 0){

//        DEBUG("Sorry! Can not find mtd device\n");
//        return 1;   //返回大於零的數,表示升級失敗
//    }
//    else
//    {
//        int fd;
//       
//        DEBUG("find %d mtd devices \n",mtdbuf.gl_pathc);
//        DevNum = mtdbuf.gl_pathc;

//        for(i=0; i<DevNum; i++)
//        {
//            fd = safeOpen (mtdbuf.gl_pathv[i],O_SYNC | O_RDWR);
//            if(fd < 0)
//            {
//                printf("failt to open\n");
//                return 1;
//            }
//            bzero(&mtd, sizeof(struct mtd_info_user));
//            if (ioctl(fd, MEMGETINFO,&mtd) < 0)
//            {
//                DEBUG("ioctl(): %m\n");
//                DEBUG("This doesn't seem to be a valid MTD flash device!\n");
//                return 1;
//            }
//           
//            strcpy(DevInfo[i].dir, mtdbuf.gl_pathv[i]);
//            DevInfo[i].fd = fd;
//            DevInfo[i].size = mtd.size;
//            DevInfo[i].erasesize = mtd.erasesize;
//        }

//        globfree(&mtdbuf);
//        for(i=0; i<DevNum; i++)
//        {
//            printf("\n\tinfo of %s\n",DevInfo[i].dir);
//            printf("%s.fd:            %d\n",DevInfo[i].dir, DevInfo[i].fd);
//            printf("%s.size:          %d\n",DevInfo[i].dir,DevInfo[i].size);
//            printf("%s.erasesize:     %d\n",DevInfo[i].dir,DevInfo[i].erasesize);
//        }
//    }


/* 下面是第二種方法,這種方法克服了第一種方法的缺陷,不受mtd裝置數量的限制 */

 

    for(i=0; i<MAXPARTITIONS; i++)
    {
        int fd;
        sprintf(DevName, "%s%d", "/dev/mtd",i);

        if((fd = open (DevName,O_SYNC | O_RDWR)) > 0)
        {
            bzero(&mtd, sizeof(struct mtd_info_user));
            if (ioctl(fd, MEMGETINFO,&mtd) < 0)
            {
                DEBUG("ioctl(): %m\n");
                DEBUG("This doesn't seem to be a valid MTD flash device!\n");
                return 1;
            }
            strcpy(DevInfo[i].dir, DevName);
            DevInfo[i].fd = fd;
            DevInfo[i].size = mtd.size;
            DevInfo[i].erasesize = mtd.erasesize;
        }
        else
        {
            DevNum = i;
            break;
        }

    }

    for(i=0; i<DevNum; i++)
    {
        printf("\n\tinfo of %s\n",DevInfo[i].dir);
        printf("%s.fd:            %d\n",DevInfo[i].dir, DevInfo[i].fd);
        printf("%s.size:          %d\n",DevInfo[i].dir,DevInfo[i].size);
        printf("%s.erasesize:     %d\n",DevInfo[i].dir,DevInfo[i].erasesize);
        AllSize += DevInfo[i].size;
     }

    if(AllSize < StateOfImage.st_size)
    {
        DEBUG("ERROR!! all device size is less than ImageSize\n");
        return 1;
    }

    for(i=StartDev; i<DevNum; i++)
    {

      /**
       * 先進行擦除操作
       */
        int j = 0;
        g_AllImgSize = DevInfo[i].size;
        g_AllImgWrite = 0;
        erase.start = 0;
        blocks = DevInfo[i].size / mtd.erasesize;  //計算要擦除的塊的個數
        erase.length = mtd.erasesize;
        printf ("\nbegin to erase block %s\n", DevInfo[i].dir);
        for (j= 1; j <= blocks; j++)
        {
            fprintf(stderr, "\rErasing blocks: %d/%d (%d%%)", j, blocks, (int)PERCENTAGE (j, blocks));
            g_percentage = 100 * ((float)g_AllImgWrite / g_AllImgSize);
              if (ioctl(DevInfo[i].fd, MEMERASE, &erase) < 0)
              {
                DEBUG("\n");
                DEBUG("While erasing blocks 0x%.8x-0x%.8x on %s\n",\
                (unsigned int) erase.start, (unsigned int) (erase.start + erase.length), DevInfo[i].dir);
                /*return "Error while erasing blocks";*/
                return 1;
            }
            g_AllImgWrite += erase.length;
            erase.start += mtd.erasesize;
        }
        printf("\n\rErased blocks: %d/%d (100%%)\n", blocks, blocks);

        /**
         * 再進行寫操作
         */
        printf ("\nbegin to write block %s\n\n", DevInfo[i].dir);
        g_AllImgWrite = 0;
        k = BUFSIZE;
        while (size)
        {
            if (size < BUFSIZE)
            {
                k = size;
            }
            printf("\033[1A");
            printf("\r%s usage: %dk/%dk (%d%%)\n",\
            DevInfo[i].dir, KB (g_AllImgWrite + k), KB (DevInfo[i].size), (int)PERCENTAGE (g_AllImgWrite + k, DevInfo[i].size));
            fprintf(stderr, "Writing data: %dk/%ldk (%d%%)", KB (written + k), KB (StateOfImage.st_size), (int)PERCENTAGE (written + k, StateOfImage.st_size));
            result = write(DevInfo[i].fd, &upPack[written], k);
            if (k != result)
            {
                DEBUG ("\n");
                if (result < 0)
                {
                    DEBUG("While writing data to 0x%.8x-0x%.8x on %s\n", written, written + k, DevInfo[i].dir);
                      return 1;
                }
                DEBUG("Short write count returned while writing to x%.8x-0x%.8x on %s: %d/%d bytes written to flash\n", \
                        written,written + k, DevInfo[i].dir, written + result, DevInfo[i].size);
                return 1;
            }

            written += k;
            size -= k;

             g_AllImgWrite += k;
            if(g_AllImgWrite >= DevInfo[i].size)
            {  
                g_AllImgWrite = 0;
                printf("\n");
                break;
            }

        }
        printf("Wrote %d / %ldk bytes\n", written, (unsigned long int)(StateOfImage.st_size));       

    }

    munmap(upPack, UPGRADE_SHM_SIZE);
    for(i=0; i<DevNum; i++)
    {
        close (DevInfo[i].fd);
        printf("%s is closed!\n",DevInfo[i].dir);
    }

    return 0;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.