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

📄 flash.c

📁 flash 的驱动, 很不错的! 可以看一看.
💻 C
📖 第 1 页 / 共 3 页
字号:
               fs_index = fs_index % 256;
          }

          read_block = (fs_temp[fs_index] & 0x3FFF);//最高两位没用到
          state = read_flash_block(read_temp, read_block);

          if(state != 1)
          {
               return (0);
          }

          if(len >= (512 - start_addr))
          {
               memcpy(write_pos,(read_temp + start_addr), (512 - start_addr));
               len -= (512 - start_addr);
               write_pos += (512 - start_addr);
          }
          else
          {
               memcpy(write_pos,(read_temp + start_addr),len);
               len = 0;
               write_pos += len;
          }

          start_addr = 0;
          fs_index++;
     }


     return (1);
}


/*******************************************************************************/
//功能:    写flash
//成功返回 1 ,否则返回0
/*******************************************************************************/
int flash_write(void *buf, int len,  unsigned int BaseAddr)
{
     unsigned int fs_block;
     unsigned int write_block;          //当前要写的扁区
     unsigned int start_addr;          //要写的数据所在扇区的起始地址
     unsigned int state;
     unsigned int fs_index;
     unsigned char * write_pos;
     unsigned char write_temp[528];
     unsigned short fs_temp[264];

     if((BaseAddr + len) >= (7680 * 512))
     {
         return (0);
     }

     fs_block = (BaseAddr / 512) /256;  //要写的扁区的索引项在文件索引表中的扇区号
     fs_index = (BaseAddr / 512) % 256; //要写的扇区的索引项在文件索引表中的扇区号的对应的位置
     start_addr = BaseAddr % 512;       //要写的数据所在扇区的位置

      state = read_fs_index((unsigned char *)fs_temp, fs_block);//Closed by ChengDong Lu at 04/20/2006
     // state = read_fs_index((unsigned char *)fs_temp, fs_block - 8128);

     if(state != 1)
     {
         return (0);
     }

     write_pos = (unsigned char *)buf;//Added by ChengDong Lu at 04/19/2006

      while(len > 0)
     {
          if(fs_index >=256)
          {
               fs_block++;
               state = read_fs_index((unsigned char *)fs_temp, fs_block);//Closed by ChengDong Lu at 04/20/2006
               //state = read_fs_index((unsigned char *)fs_temp, fs_block - 8128);


               if(state != 1)
               {
                   return (0);
               }

               fs_index = fs_index % 256;
          }

          write_block = (fs_temp[fs_index] & 0x3FFF);//最高两位没用到

          if(state != 1)
          {
               return (0);
          }

          if(len >= (512 - start_addr))
          {

                if(start_addr > 0 || len < 512)
                {
                      state = read_flash_block(write_temp, write_block);

                      if(state != 1)
                      {
                            return (0);
                      }
                }

              //  memcpy((write_temp + start_addr),(write_pos + start_addr), (512 - start_addr));
                memcpy((write_temp + start_addr),write_pos, (512 - start_addr));
                len -= (512 - start_addr);
                write_pos += (512 - start_addr);


               state = write_flash_block(write_temp, write_block);

               if(state != 1)
               {
                       /*    //如果还不成功,则找一个没用过的扇区来替换上次要写的扇区,再往里面写
                           write_block = find_empty_block();
                           if(write_block == 0)
                           {
                                 return (0);
                           }
                           else
                           {
                                 fs_temp[fs_index] = write_block;
                                 //在写之前还得重新算一下cs
                                 state = write_fs_index((unsigned char *)fs_temp, fs_block);//修改文件索引表
                                 if(state != 1)
                                 {
                                     return (0);
                                 }
                           }

                           state = write_flash_block_ext(write_temp, write_block, start_addr, (512 - start_addr));
                           if(state != 1)//如果没写成功 ,再写一次
                           {

                           }
                 */
                 return (0);
               }
          }
          else
          {
                state = read_flash_block(write_temp, write_block);

                 if(state != 1)
                {
                     return (0);
                }

               memcpy((write_temp + start_addr), write_pos, len);
               write_flash_block(write_temp, write_block);


               if(state != 1)
               {
                           //如果还不成功,则找一个没用过的扇区来替换上次要写的扇区,再往里面写
                        /*   write_block = find_empty_block();
                           if(write_block == 0)
                           {
                                 return (0);
                           }
                           else
                           {
                                 fs_temp[fs_index] = write_block;
                                 //在写之前还得重新算一下cs
                                 state = write_fs_index((unsigned char *)fs_temp, fs_block);//修改文件索引表
                                 if(state != 1)
                                 {
                                     return (0);
                                 }
                           }

                           state = write_flash_block_ext(write_temp, write_block, start_addr, len);
                           if(state != 1)//如果没写成功 ,再写一次
                           {
                                 return (0);
                           }
                 */
                 return (0);
               }

               len = 0;
               write_pos += len;
          }

          start_addr = 0;
          fs_index++;
     }


     return (1);

}



void test(void)
{
     unsigned int opr_block;
   //  unsigned char write_temp[512];
     unsigned char read_temp[512];
     unsigned char write_buf[512 * 4];
     unsigned char read_buf[512 * 4];
     unsigned int state;
     unsigned int i,j;
     unsigned int error = 8;

     for(i = 0; i < 512; i++)
     {
       //  write_temp[i] = 'E';
         write_buf[i] = 0x35;
         write_buf[512 + i] = 0x36;
         write_buf[512 * 2 + i] = 0x37;
         write_buf[512 * 3 + i] = 0x38;

         read_temp[i] = 0x0;
         read_buf[i] = 0x0;
         read_buf[512 + i] = 0x0;
         read_buf[512 * 2 + i] = 0x0;
         read_buf[512 * 3 + i] = 0x0;

     }

     //当 j=0xeb  i = 457; 出错(文件索引表的第2个扇区的被覆盖,)
     //当 j=0x8c1  i = 457; 出错(文件索引表的第19个扇区的被覆盖)
      //当 j=0x90b  i = 457; 出错(文件索引表的第26个扇区的被覆盖)
      //当 j=0xc71  i = 457; 出错(文件索引表的第个扇区的被覆盖)
     for(j = 0x0; j < 3538; j++)
     {
           for(j = 0; j < 1111; j++)
           {
              // write_buf[j] =  i;//colsed by ChengDong Lu at 04/20/2006
              write_buf[j] =  j;
              read_buf[j] = 0x0;
           }


         state = flash_write(write_buf, 1111, 1111*j);
         if(state != 1)
         {
             // return ;
           error = 0;
           break;
         }

          state = flash_read(read_buf, 1111 ,  1111*j);
          if(state != 1)
          {
                //return ;
             error = 2;
            break;
          }

         for(i = 0; i < 1111; i++)
         {
             if( write_buf[i] != read_buf[i])
             {
                 //return;
                error = 3;
               break;
              }
         }
     }

 error = error;

   state = flash_read(read_buf, 512 * 4, 255*512 + 7);
     if(state != 1)
     {
        return ;
     }

    opr_block =255;
    while(1)
    {
         state =  read_flash_block(read_temp,opr_block );
         if(state != 1)
         {
            return ;
         }
         opr_block++;
    }

}





/*
void init_string(unsigned char *mystring, unsigned int len)
{
    unsigned int i;

    for(i = 0; i < len; i++)
    {
        mystring[i] = 0x0;
    }
}


void write_string(unsigned char *mystring, unsigned int len, unsigned char letter)
{
    unsigned int i;

    for(i = 0; i < len; i++)
    {
        mystring[i] = letter;
    }
}


void test2(void)
{
       unsigned int i;
       unsigned int state;
       unsigned char test8128_buf[528];
       unsigned char test8160_buf[528];

       init_string(test8128_buf, 528);
       init_string(test8160_buf, 528);

       for(i = 0; i < 32; i++)
       {
           state = read_flash_block(test8128_buf, 8128 + i );
           if(state != 1)
           {
                return ;
           }

            state = read_flash_block(test8160_buf, 8160 + i);
            if(state != 1)
            {
                return ;
            }


            init_string(test8128_buf, 528);
            init_string(test8160_buf, 528);


       }


}

*/

void test_write(void)
{
  unsigned int i,j;
  unsigned char write_temp[528];
  unsigned char write_buf[1111];
  unsigned char read_buf[1111];
  unsigned int error = 0;
  unsigned int state;

  write_temp[0]= 0;

  for(i = 0; i < 3538; i++)
  {
       for(j = 0; j < 1111; j++)
       {
              //write_buf[j] =  i;//colsed by ChengDong Lu at 04/20/2006
             write_buf[j] =  j;
             read_buf[j] = 0x0;
       }


       state = flash_write(write_buf, 1111, 1111*i);

        if(state != 1)
        {
                //return ;
             error = 1;
            break;
        }


  }

  error = error;
  read_buf[0] = read_buf[0];
  write_temp[0] = write_temp[0];
}


void test_read(void)
{
  unsigned int i,j;
 // unsigned int write_number;
  unsigned char write_buf[1111];
  unsigned char read_buf[1111];
  unsigned char write_temp[528];
  unsigned int error = 0;
  unsigned int state;

  state = read_flash_block(write_temp, 2);

  for(i = 0; i < 3538; i++)
  {
       for(j = 0; j < 1111; j++)
       {
           // write_buf[j] =  i;//closed by ChengDong Lu at 04/20/2006
            write_buf[j] =  j;
       }

       state = flash_read(read_buf, 1111, 1111*i);

        if(state != 1)
        {
                //return ;
            error = 1;
            break;
        }

        for(j = 0; j < 1111; j++)
        {
            if(write_buf[j] != read_buf[j])
            {
                error = 2;
                break;
            }
       }
  }

  error = error;

}

⌨️ 快捷键说明

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