Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //************************************************************************************
- //Function: to create a file in FAT32 format in the root directory if given
- // file name does not exist; if the file already exists then append the data
- //Arguments: pointer to the file name
- //return: none
- //************************************************************************************
- void writeFile (unsigned char *fileName, unsigned char *data)
- {
- unsigned char j, error, fileCreatedFlag = 0, start = 0, appendFile = 0, sector;
- unsigned int i, k=0, firstClusterHigh, firstClusterLow;
- struct dir_Structure *dir;
- unsigned long cluster, nextCluster, prevCluster, firstSector, clusterCount, extraMemory;
- j = readFile (VERIFY, fileName);
- if(j == 1)
- {
- //transmitString_F(PSTR(" File already existing, appending data.."));
- appendFile = 1;
- cluster = appendStartCluster;
- clusterCount=0;
- while(1)
- {
- nextCluster = getSetNextCluster (cluster, GET, 0);
- if(nextCluster == EOF) break;
- cluster = nextCluster;
- clusterCount++;
- }
- sector = (fileSize - (clusterCount * sectorPerCluster * bytesPerSector)) / bytesPerSector; //last sector number of the last cluster of the file
- start = 1;
- // appendFile();
- // return;
- }
- else if(j == 2)
- {
- return; //invalid file name
- }
- else
- {
- //TX_NEWLINE;
- //transmitString_F(PSTR(" Creating File.."));
- cluster = getSetFreeCluster (NEXT_FREE, GET, 0);
- if(cluster > totalClusters)
- cluster = rootCluster;
- cluster = searchNextFreeCluster(cluster);
- if(cluster == 0)
- {
- //TX_NEWLINE;
- //transmitString_F(PSTR(" No free cluster!"));
- return;
- }
- getSetNextCluster(cluster, SET, EOF); //last cluster of the file, marked EOF
- firstClusterHigh = (unsigned int) ((cluster & 0xffff0000) >> 16 );
- firstClusterLow = (unsigned int) ( cluster & 0x0000ffff);
- fileSize = 0;
- }
- while(1)
- {
- if(start)
- {
- start = 0;
- startBlock = getFirstSector (cluster) + sector;
- SD_readSingleBlock (startBlock);
- i = fileSize % bytesPerSector;
- j = sector;
- }
- else
- {
- startBlock = getFirstSector (cluster);
- i=0;
- j=0;
- }
- do
- {
- buffer[i++] = data[k++];
- fileSize++;
- if(i == 512)
- {
- i=0;
- error = SD_writeSingleBlock (startBlock);
- j++;
- if(j == sectorPerCluster) {j = 0; break;}
- startBlock++;
- }
- }while(k<strlen(data));
- if (k==strlen(data))
- {
- for(;i<512;i++) //fill the rest of the buffer with 0x00
- buffer[i]= 0x00;
- error = SD_writeSingleBlock (startBlock);
- break;
- }
- prevCluster = cluster;
- cluster = searchNextFreeCluster(prevCluster); //look for a free cluster starting from the current cluster
- if(cluster == 0)
- {
- //TX_NEWLINE;
- //transmitString_F(PSTR(" No free cluster!"));
- return;
- }
- getSetNextCluster(prevCluster, SET, cluster);
- getSetNextCluster(cluster, SET, EOF); //last cluster of the file, marked EOF
- }
- getSetFreeCluster (NEXT_FREE, SET, cluster); //update FSinfo next free cluster entry
- if(appendFile) //executes this loop if file is to be appended
- {
- SD_readSingleBlock (appendFileSector);
- dir = (struct dir_Structure *) &buffer[appendFileLocation];
- extraMemory = fileSize - dir->fileSize;
- dir->fileSize = fileSize;
- SD_writeSingleBlock (appendFileSector);
- freeMemoryUpdate (REMOVE, extraMemory); //updating free memory count in FSinfo sector;
- //TX_NEWLINE;
- //transmitString_F(PSTR(" File appended!"));
- //TX_NEWLINE;
- return;
- }
- //executes following portion when new file is created
- prevCluster = rootCluster; //root cluster
- while(1)
- {
- firstSector = getFirstSector (prevCluster);
- for(sector = 0; sector < sectorPerCluster; sector++)
- {
- SD_readSingleBlock (firstSector + sector);
- for(i=0; i<bytesPerSector; i+=32)
- {
- dir = (struct dir_Structure *) &buffer[i];
- if(fileCreatedFlag) //to mark last directory entry with 0x00 (empty) mark
- { //indicating end of the directory file list
- dir->name[0] = 0x00;
- return;
- }
- if((dir->name[0] == EMPTY) || (dir->name[0] == DELETED)) //looking for an empty slot to enter file info
- {
- for(j=0; j<11; j++)
- dir->name[j] = fileName[j];
- dir->attrib = ATTR_ARCHIVE; //settting file attribute as 'archive'
- dir->NTreserved = 0; //always set to 0
- dir->timeTenth = 0; //always set to 0
- dir->createTime = 0x9684; //fixed time of creation
- dir->createDate = 0x3a37; //fixed date of creation
- dir->lastAccessDate = 0x3a37; //fixed date of last access
- dir->writeTime = 0x9684; //fixed time of last write
- dir->writeDate = 0x3a37; //fixed date of last write
- dir->firstClusterHI = firstClusterHigh;
- dir->firstClusterLO = firstClusterLow;
- dir->fileSize = fileSize;
- SD_writeSingleBlock (firstSector + sector);
- fileCreatedFlag = 1;
- //TX_NEWLINE;
- //TX_NEWLINE;
- //transmitString_F(PSTR(" File Created!"));
- freeMemoryUpdate (REMOVE, fileSize); //updating free memory count in FSinfo sector
- }
- }
- }
- cluster = getSetNextCluster (prevCluster, GET, 0);
- if(cluster > 0x0ffffff6)
- {
- if(cluster == EOF) //this situation will come when total files in root is multiple of (32*sectorPerCluster)
- {
- cluster = searchNextFreeCluster(prevCluster); //find next cluster for root directory entries
- getSetNextCluster(prevCluster, SET, cluster); //link the new cluster of root to the previous cluster
- getSetNextCluster(cluster, SET, EOF); //set the new cluster as end of the root directory
- }
- else
- {
- //transmitString_F(PSTR("End of Cluster Chain"));
- return;
- }
- }
- if(cluster == 0) {
- //transmitString_F(PSTR("Error in getting cluster"));
- return;}
- prevCluster = cluster;
- }
- return;
- }
- //***************************************************************************
- //Function: if flag=READ then to read file from SD card and send contents to UART
- //if flag=VERIFY then functions will verify whether a specified file is already existing
- //Arguments: flag (READ or VERIFY) and pointer to the file name
- //return: 0, if normal operation or flag is READ
- // 1, if file is already existing and flag = VERIFY
- // 2, if file name is incompatible
- //***************************************************************************
- unsigned char readFile (unsigned char flag, unsigned char *fileName)
- {
- struct dir_Structure *dir;
- unsigned long cluster, byteCounter = 0, fileSize, firstSector;
- unsigned int k;
- unsigned char j, error;
- error = convertFileName (fileName); //convert fileName into FAT format
- if(error) return 2;
- dir = findFiles (GET_FILE, fileName); //get the file location
- if(dir == 0)
- return (0);
- if(flag == VERIFY) return (1); //specified file name is already existing
- cluster = (((unsigned long) dir->firstClusterHI) << 16) | dir->firstClusterLO;
- fileSize = dir->fileSize;
- //TX_NEWLINE;
- //TX_NEWLINE;
- while(1)
- {
- firstSector = getFirstSector (cluster);
- for(j=0; j<sectorPerCluster; j++)
- {
- SD_readSingleBlock(firstSector + j);
- for(k=0; k<512; k++)
- {
- //transmitByte(buffer[k]);
- if ((byteCounter++) >= fileSize ) return 0;
- }
- }
- cluster = getSetNextCluster (cluster, GET, 0);
- if(cluster == 0) {
- //transmitString_F(PSTR("Error in getting cluster"));
- return 0;}
- }
- return 0;
- }
- //***************************************************************************
- //Function: to convert normal short file name into FAT format
- //Arguments: pointer to the file name
- //return: 0, if successful else 1.
- //***************************************************************************
- unsigned char convertFileName (unsigned char *fileName)
- {
- unsigned char fileNameFAT[11];
- unsigned char j, k;
- for(j=0; j<12; j++)
- if(fileName[j] == '.') break;
- PORTD = j;
- if(j>8) {
- //transmitString_F(PSTR("Invalid fileName.."));
- return 1;}
- for(k=0; k<j; k++) //setting file name
- fileNameFAT[k] = fileName[k];
- for(k=j; k<=7; k++) //filling file name trail with blanks
- fileNameFAT[k] = ' ';
- j++;
- for(k=8; k<11; k++) //setting file extention
- {
- if(fileName[j] != 0)
- fileNameFAT[k] = fileName[j++];
- else //filling extension trail with blanks
- while(k<11)
- fileNameFAT[k++] = ' ';
- }
- for(j=0; j<11; j++) //converting small letters to caps
- if((fileNameFAT[j] >= 0x61) && (fileNameFAT[j] <= 0x7a))
- fileNameFAT[j] -= 0x20;
- for(j=0; j<11; j++)
- fileName[j] = fileNameFAT[j];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment