expert_vision

fat32

Aug 23rd, 2013
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.58 KB | None | 0 0
  1. //************************************************************************************
  2. //Function: to create a file in FAT32 format in the root directory if given
  3. //          file name does not exist; if the file already exists then append the data
  4. //Arguments: pointer to the file name
  5. //return: none
  6. //************************************************************************************
  7. void writeFile (unsigned char *fileName, unsigned char *data)
  8. {
  9. unsigned char j, error, fileCreatedFlag = 0, start = 0, appendFile = 0, sector;
  10. unsigned int i, k=0, firstClusterHigh, firstClusterLow;
  11. struct dir_Structure *dir;
  12. unsigned long cluster, nextCluster, prevCluster, firstSector, clusterCount, extraMemory;
  13.  
  14. j = readFile (VERIFY, fileName);
  15.  
  16. if(j == 1)
  17. {
  18.   //transmitString_F(PSTR("  File already existing, appending data.."));
  19.   appendFile = 1;
  20.   cluster = appendStartCluster;
  21.   clusterCount=0;
  22.   while(1)
  23.   {
  24.     nextCluster = getSetNextCluster (cluster, GET, 0);
  25.     if(nextCluster == EOF) break;
  26.     cluster = nextCluster;
  27.     clusterCount++;
  28.   }
  29.  
  30.   sector = (fileSize - (clusterCount * sectorPerCluster * bytesPerSector)) / bytesPerSector; //last sector number of the last cluster of the file
  31.   start = 1;
  32. //  appendFile();
  33. //  return;
  34. }
  35. else if(j == 2)
  36. {
  37.    return; //invalid file name
  38. }
  39. else
  40. {
  41.   //TX_NEWLINE;
  42.   //transmitString_F(PSTR(" Creating File.."));
  43.  
  44.   cluster = getSetFreeCluster (NEXT_FREE, GET, 0);
  45.   if(cluster > totalClusters)
  46.      cluster = rootCluster;
  47.  
  48.   cluster = searchNextFreeCluster(cluster);
  49.    if(cluster == 0)
  50.    {
  51.       //TX_NEWLINE;
  52.       //transmitString_F(PSTR(" No free cluster!"));
  53.       return;
  54.    }
  55.   getSetNextCluster(cluster, SET, EOF);   //last cluster of the file, marked EOF
  56.    
  57.   firstClusterHigh = (unsigned int) ((cluster & 0xffff0000) >> 16 );
  58.   firstClusterLow = (unsigned int) ( cluster & 0x0000ffff);
  59.   fileSize = 0;
  60. }
  61.  
  62. while(1)
  63. {
  64.    if(start)
  65.    {
  66.       start = 0;
  67.       startBlock = getFirstSector (cluster) + sector;
  68.       SD_readSingleBlock (startBlock);
  69.       i = fileSize % bytesPerSector;
  70.       j = sector;
  71.    }
  72.    else
  73.    {
  74.       startBlock = getFirstSector (cluster);
  75.       i=0;
  76.       j=0;
  77.    }
  78.    
  79.    do
  80.    {
  81.        buffer[i++] = data[k++];
  82.        fileSize++;
  83.        if(i == 512)
  84.        {
  85.            i=0;
  86.            error = SD_writeSingleBlock (startBlock);
  87.            j++;
  88.            if(j == sectorPerCluster) {j = 0; break;}
  89.            startBlock++;
  90.        }
  91.    }while(k<strlen(data));
  92.    
  93.    if (k==strlen(data))
  94.    {
  95.        for(;i<512;i++)  //fill the rest of the buffer with 0x00
  96.        buffer[i]= 0x00;
  97.        error = SD_writeSingleBlock (startBlock);
  98.        break;
  99.    }
  100.    
  101.    prevCluster = cluster;
  102.    
  103.    cluster = searchNextFreeCluster(prevCluster); //look for a free cluster starting from the current cluster
  104.    
  105.    if(cluster == 0)
  106.    {
  107.    //TX_NEWLINE;
  108.    //transmitString_F(PSTR(" No free cluster!"));
  109.    return;
  110.    }
  111.    
  112.    getSetNextCluster(prevCluster, SET, cluster);
  113.    getSetNextCluster(cluster, SET, EOF);   //last cluster of the file, marked EOF        
  114. }
  115.  
  116. getSetFreeCluster (NEXT_FREE, SET, cluster); //update FSinfo next free cluster entry
  117.  
  118. if(appendFile)  //executes this loop if file is to be appended
  119. {
  120.   SD_readSingleBlock (appendFileSector);    
  121.   dir = (struct dir_Structure *) &buffer[appendFileLocation];
  122.   extraMemory = fileSize - dir->fileSize;
  123.   dir->fileSize = fileSize;
  124.   SD_writeSingleBlock (appendFileSector);
  125.   freeMemoryUpdate (REMOVE, extraMemory); //updating free memory count in FSinfo sector;
  126.  
  127.  
  128.   //TX_NEWLINE;
  129.   //transmitString_F(PSTR(" File appended!"));
  130.   //TX_NEWLINE;
  131.   return;
  132. }
  133.  
  134. //executes following portion when new file is created
  135.  
  136. prevCluster = rootCluster; //root cluster
  137.  
  138. while(1)
  139. {
  140.    firstSector = getFirstSector (prevCluster);
  141.  
  142.    for(sector = 0; sector < sectorPerCluster; sector++)
  143.    {
  144.      SD_readSingleBlock (firstSector + sector);
  145.    
  146.  
  147.      for(i=0; i<bytesPerSector; i+=32)
  148.      {
  149.         dir = (struct dir_Structure *) &buffer[i];
  150.  
  151.         if(fileCreatedFlag)   //to mark last directory entry with 0x00 (empty) mark
  152.          {                    //indicating end of the directory file list
  153.            dir->name[0] = 0x00;
  154.            return;
  155.          }
  156.  
  157.         if((dir->name[0] == EMPTY) || (dir->name[0] == DELETED))  //looking for an empty slot to enter file info
  158.         {
  159.           for(j=0; j<11; j++)
  160.             dir->name[j] = fileName[j];
  161.           dir->attrib = ATTR_ARCHIVE;   //settting file attribute as 'archive'
  162.           dir->NTreserved = 0;          //always set to 0
  163.           dir->timeTenth = 0;           //always set to 0
  164.           dir->createTime = 0x9684;     //fixed time of creation
  165.           dir->createDate = 0x3a37;     //fixed date of creation
  166.           dir->lastAccessDate = 0x3a37; //fixed date of last access
  167.           dir->writeTime = 0x9684;      //fixed time of last write
  168.           dir->writeDate = 0x3a37;      //fixed date of last write
  169.           dir->firstClusterHI = firstClusterHigh;
  170.           dir->firstClusterLO = firstClusterLow;
  171.           dir->fileSize = fileSize;
  172.  
  173.           SD_writeSingleBlock (firstSector + sector);
  174.           fileCreatedFlag = 1;
  175.  
  176.           //TX_NEWLINE;
  177.           //TX_NEWLINE;
  178.           //transmitString_F(PSTR(" File Created!"));
  179.  
  180.           freeMemoryUpdate (REMOVE, fileSize); //updating free memory count in FSinfo sector
  181.          
  182.         }
  183.      }
  184.    }
  185.  
  186.    cluster = getSetNextCluster (prevCluster, GET, 0);
  187.  
  188.    if(cluster > 0x0ffffff6)
  189.    {
  190.       if(cluster == EOF)   //this situation will come when total files in root is multiple of (32*sectorPerCluster)
  191.       {  
  192.         cluster = searchNextFreeCluster(prevCluster); //find next cluster for root directory entries
  193.         getSetNextCluster(prevCluster, SET, cluster); //link the new cluster of root to the previous cluster
  194.         getSetNextCluster(cluster, SET, EOF);  //set the new cluster as end of the root directory
  195.       }
  196.  
  197.       else
  198.       {
  199.         //transmitString_F(PSTR("End of Cluster Chain"));
  200.         return;
  201.       }
  202.    }
  203.    if(cluster == 0) {
  204.        //transmitString_F(PSTR("Error in getting cluster"));
  205.        return;}
  206.    
  207.    prevCluster = cluster;
  208.  }
  209.  
  210.  return;
  211. }
  212.  
  213. //***************************************************************************
  214. //Function: if flag=READ then to read file from SD card and send contents to UART
  215. //if flag=VERIFY then functions will verify whether a specified file is already existing
  216. //Arguments: flag (READ or VERIFY) and pointer to the file name
  217. //return: 0, if normal operation or flag is READ
  218. //        1, if file is already existing and flag = VERIFY
  219. //        2, if file name is incompatible
  220. //***************************************************************************
  221. unsigned char readFile (unsigned char flag, unsigned char *fileName)
  222. {
  223. struct dir_Structure *dir;
  224. unsigned long cluster, byteCounter = 0, fileSize, firstSector;
  225. unsigned int k;
  226. unsigned char j, error;
  227.  
  228. error = convertFileName (fileName); //convert fileName into FAT format
  229. if(error) return 2;
  230.  
  231. dir = findFiles (GET_FILE, fileName); //get the file location
  232. if(dir == 0)
  233.   return (0);
  234.  
  235. if(flag == VERIFY) return (1);  //specified file name is already existing
  236.  
  237. cluster = (((unsigned long) dir->firstClusterHI) << 16) | dir->firstClusterLO;
  238.  
  239. fileSize = dir->fileSize;
  240.  
  241. //TX_NEWLINE;
  242. //TX_NEWLINE;
  243.  
  244. while(1)
  245. {
  246.   firstSector = getFirstSector (cluster);
  247.  
  248.   for(j=0; j<sectorPerCluster; j++)
  249.   {
  250.     SD_readSingleBlock(firstSector + j);
  251.    
  252.     for(k=0; k<512; k++)
  253.     {
  254.       //transmitByte(buffer[k]);
  255.       if ((byteCounter++) >= fileSize ) return 0;
  256.     }
  257.   }
  258.   cluster = getSetNextCluster (cluster, GET, 0);
  259.   if(cluster == 0) {
  260.       //transmitString_F(PSTR("Error in getting cluster"));
  261.        return 0;}
  262. }
  263. return 0;
  264. }
  265.  
  266. //***************************************************************************
  267. //Function: to convert normal short file name into FAT format
  268. //Arguments: pointer to the file name
  269. //return: 0, if successful else 1.
  270. //***************************************************************************
  271. unsigned char convertFileName (unsigned char *fileName)
  272. {
  273. unsigned char fileNameFAT[11];
  274. unsigned char j, k;
  275.  
  276. for(j=0; j<12; j++)
  277. if(fileName[j] == '.') break;
  278. PORTD = j;
  279. if(j>8) {
  280.     //transmitString_F(PSTR("Invalid fileName.."));
  281.     return 1;}
  282.  
  283. for(k=0; k<j; k++) //setting file name
  284.   fileNameFAT[k] = fileName[k];
  285.  
  286. for(k=j; k<=7; k++) //filling file name trail with blanks
  287.   fileNameFAT[k] = ' ';
  288.  
  289. j++;
  290. for(k=8; k<11; k++) //setting file extention
  291. {
  292.   if(fileName[j] != 0)
  293.     fileNameFAT[k] = fileName[j++];
  294.   else //filling extension trail with blanks
  295.     while(k<11)
  296.       fileNameFAT[k++] = ' ';
  297. }
  298.  
  299. for(j=0; j<11; j++) //converting small letters to caps
  300.   if((fileNameFAT[j] >= 0x61) && (fileNameFAT[j] <= 0x7a))
  301.     fileNameFAT[j] -= 0x20;
  302.  
  303. for(j=0; j<11; j++)
  304.   fileName[j] = fileNameFAT[j];
  305.  
  306. return 0;
  307. }
Advertisement
Add Comment
Please, Sign In to add comment