Advertisement
Guest User

FAT32.h

a guest
Nov 5th, 2010
858
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.96 KB | None | 0 0
  1. //********************************************************
  2. // **** ROUTINES FOR FAT32 IMPLEMATATION OF SD CARD *****
  3. //********************************************************
  4. //Controller: ATmega168 (Clock: 8 Mhz-internal)
  5. //Compiler: AVR-GCC
  6. //Version : 2.1
  7. //Author: CC Dharmani, Chennai (India)
  8. // www.dharmanitech.com
  9. // With some changes by Mujda
  10. //Date: 26 Feb 2009 - updated 13 Sep 2009
  11. //********************************************************
  12.  
  13. //Link to the Post: http://www.dharmanitech.com/2009/01/sd-card-interfacing-with-atmega8-fat32.html
  14.  
  15.  
  16. //**************************************************
  17. // ***** HEADER FILE : FAT32.h ******
  18. //**************************************************
  19. #ifndef _FAT32_H_
  20. #define _FAT32_H_
  21.  
  22. //Structure to access Master Boot Record for getting info about partioions
  23. struct MBRinfo_Structure{
  24. unsigned char     nothing[446];      //ignore, placed here to fill the gap in the structure
  25. unsigned char     partitionData[64]; //partition records (16x4)
  26. unsigned int      signature;         //0xaa55
  27. };
  28.  
  29. //Structure to access info of the first partioion of the disk
  30. struct partitionInfo_Structure{                      
  31. unsigned char     status;                 //0x80 - active partition
  32. unsigned char     headStart;              //starting head
  33. unsigned int      cylSectStart;           //starting cylinder and sector
  34. unsigned char     type;                   //partition type
  35. unsigned char     headEnd;                //ending head of the partition
  36. unsigned int      cylSectEnd;             //ending cylinder and sector
  37. unsigned long     firstSector;            //total sectors between MBR & the first sector of the partition
  38. unsigned long     sectorsTotal;           //size of this partition in sectors
  39. };
  40.  
  41. //Structure to access boot sector data
  42. struct BS_Structure{
  43. unsigned char jumpBoot[3];      //default: 0x009000EB
  44. unsigned char OEMName[8];
  45. unsigned int  bytesPerSector;   //deafault: 512
  46. unsigned char sectorPerCluster;
  47. unsigned int  reservedSectorCount;
  48. unsigned char numberofFATs;
  49. unsigned int  rootEntryCount;
  50. unsigned int  totalSectors_F16; //must be 0 for FAT32
  51. unsigned char mediaType;
  52. unsigned int  FATsize_F16;      //must be 0 for FAT32
  53. unsigned int  sectorsPerTrack;
  54. unsigned int  numberofHeads;
  55. unsigned long hiddenSectors;
  56. unsigned long totalSectors_F32;
  57. unsigned long FATsize_F32;      //count of sectors occupied by one FAT
  58. unsigned int  extFlags;
  59. unsigned int  FSversion;        //0x0000 (defines version 0.0)
  60. unsigned long rootCluster;      //first cluster of root directory (=2)
  61. unsigned int  FSinfo;           //sector number of FSinfo structure (=1)
  62. unsigned int  BackupBootSector;
  63. unsigned char reserved[12];
  64. unsigned char driveNumber;
  65. unsigned char reserved1;
  66. unsigned char bootSignature;
  67. unsigned long volumeID;
  68. unsigned char volumeLabel[11];   //"NO NAME "
  69. unsigned char fileSystemType[8]; //"FAT32"
  70. unsigned char bootData[420];
  71. unsigned int  bootEndSignature;  //0xaa55
  72. };
  73.  
  74.  
  75. //Structure to access FSinfo sector data
  76. struct FSInfo_Structure
  77. {
  78. unsigned long leadSignature;      //0x41615252
  79. unsigned char reserved1[480];
  80. unsigned long structureSignature; //0x61417272
  81. unsigned long freeClusterCount;   //initial: 0xffffffff
  82. unsigned long nextFreeCluster;    //initial: 0xffffffff
  83. unsigned char reserved2[12];
  84. unsigned long trailSignature;     //0xaa550000
  85. };
  86.  
  87. //Structure to access Directory Entry in the FAT
  88. struct dir_Structure{
  89. unsigned char name[11];
  90. unsigned char attrib;         //file attributes
  91. unsigned char NTreserved;     //always 0
  92. unsigned char timeTenth;      //tenths of seconds, set to 0 here
  93. unsigned int  createTime;     //time file was created
  94. unsigned int  createDate;     //date file was created
  95. unsigned int  lastAccessDate;
  96. unsigned int  firstClusterHI; //higher word of the first cluster number
  97. unsigned int  writeTime;      //time of last write
  98. unsigned int  writeDate;      //date of last write
  99. unsigned int  firstClusterLO; //lower word of the first cluster number
  100. unsigned long fileSize;       //size of file in bytes
  101. };
  102.  
  103. //Attribute definitions for file/directory
  104. #define ATTR_READ_ONLY     0x01
  105. #define ATTR_HIDDEN        0x02
  106. #define ATTR_SYSTEM        0x04
  107. #define ATTR_VOLUME_ID     0x08
  108. #define ATTR_DIRECTORY     0x10
  109. #define ATTR_ARCHIVE       0x20
  110. #define ATTR_LONG_NAME     0x0f
  111.  
  112.  
  113. #define DIR_ENTRY_SIZE     0x32
  114. #define EMPTY              0x00
  115. #define DELETED            0xe5
  116. #define GET         0
  117. #define SET         1
  118. #define READ        0
  119. #define VERIFY      1
  120. #define ADD         0
  121. #define REMOVE      1
  122. #define TOTAL_FREE  1
  123. #define NEXT_FREE   2
  124. #define GET_LIST    0
  125. #define GET_FILE    1
  126. #define DELETE          2
  127. #define EOF         0x0fffffff
  128.  
  129.  
  130. //************* external variables *************
  131. extern volatile unsigned char buffer[512];
  132. extern volatile unsigned long startBlock;
  133. extern volatile unsigned long firstDataSector, rootCluster, totalClusters;
  134. extern volatile unsigned int  bytesPerSector, sectorPerCluster, reservedSectorCount;
  135.  
  136. //global flag to keep track of free cluster count updating in FSinfo sector
  137. unsigned char freeClusterCountUpdated;
  138. unsigned long unusedSectors;
  139.  
  140. //************* functions *************
  141. unsigned char getBootSectorData (void);
  142.  
  143. unsigned long getFirstSector(unsigned long clusterNumber);
  144. unsigned long getSetFreeCluster(unsigned char totOrNext, unsigned char get_set, unsigned long FSEntry);
  145. struct dir_Structure* findFiles (unsigned char flag, unsigned char *fileName);
  146. unsigned long getSetNextCluster (unsigned long clusterNumber,unsigned char get_set,unsigned long clusterEntry);
  147. unsigned char readFile (unsigned char flag, unsigned char *fileName);
  148. unsigned char convertFileName (unsigned char *fileName);
  149. void createFile (unsigned char *fileName);
  150. unsigned long searchNextFreeCluster (unsigned long startCluster);
  151. void memoryStatistics (void);
  152. void displayMemory (unsigned long memory);
  153. void deleteFile (unsigned char *fileName);
  154. void freeMemoryUpdate (unsigned char flag, unsigned long size);
  155. void sampleData(unsigned char *line);
  156. void convertToAscii(unsigned char *line, unsigned int start, int val, int places);
  157. void adc_init();
  158.  
  159. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement