LPCOpen Platform
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
fatutil.h
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------
2  * Name: fatutil.h
3  * Purpose: FAT utilities
4  * Version: V1.00
5  *----------------------------------------------------------------------------
6  * This software is supplied "AS IS" without any warranties, express,
7  * implied or statutory, including but not limited to the implied
8  * warranties of fitness for purpose, satisfactory quality and
9  * noninfringement. NXP extends you a royalty-free right to reproduce
10  * and distribute executable files created using this software for use
11  * on NXP Semiconductors LPC microcontroller devices only. Nothing else
12  * gives you the right to use this software.
13  *
14  * Copyright (c) 2011 NXP Semiconductors. All rights reserved.
15  *---------------------------------------------------------------------------*/
16 
17 #ifndef __FATUTIL_H__
18 #define __FATUTIL_H__
19 
20 #include "usb.h"
21 #include "DataRam.h"
22 #define FAT12 1
23 #define FAT16 2
24 #define FAT32 3
25 
26 #define UDIV_ROUNDUP(a,b) ((a+b-1)/b)
27 #define FATTYPE FAT12
28 
29 //#define DISKSIZE (VIRTUAL_MEMORY_BYTES)
30 #define DISKSIZE (0x100000)
31 #define BYTESPERSECTOR (512)
32 #define RESERVEDSECTORCNT (1)
33 #define ROOTENTRIES (UDIV_ROUNDUP(BYTESPERSECTOR,32))
34 //#define DIRECTORYSECTORCNT ((ROOTENTRIES - 1) / 16 + 1)
35 #define DIRECTORYSECTORCNT (UDIV_ROUNDUP(ROOTENTRIES,16))
36 #define NUMFATS (2)
37 #define SECTORSPERCLUSTER (1)
38 #define DATASECTORS (1024)
39 //#define TOTALSECTORS (NONDATASECTORS + 330)
40 #define TOTALSECTORS (UDIV_ROUNDUP(DISKSIZE,BYTESPERSECTOR))
41 #define FATENTRIESPERSECTOR (UDIV_ROUNDUP(BYTESPERSECTOR*8,12/*FAT12*/))
42 #define SECTORSPERFAT (UDIV_ROUNDUP(UDIV_ROUNDUP((TOTALSECTORS-RESERVEDSECTORCNT),SECTORSPERCLUSTER),\
43  FATENTRIESPERSECTOR))
44 
45 #define NONDATASECTORS (RESERVEDSECTORCNT + NUMFATS * SECTORSPERFAT + DIRECTORYSECTORCNT)
46 #define STARTDATAREGION (NONDATASECTORS * BYTESPERSECTOR)
47 #define BYTESPERCLUSTER (BYTESPERSECTOR*SECTORSPERCLUSTER)
48 #define FAT_MEDIA (0xF0)
49 
50 // Time of date of FIRMWARE.BIN on the USB Stick
51 #define TIME_HOUR 16
52 #define TIME_MIN 00
53 #define TIME_SEC 00
54 #define DATE_YEAR 2011-1980
55 #define DATE_MONTH 8
56 #define DATE_DAY 11
57 
58 
59 extern int32_t RootEntries;
60 extern int32_t BytesPerSector;
61 extern int32_t NumFats;
62 extern int32_t SectorsPerFat;
63 extern int32_t NonDataSectors;
64 extern int32_t TotalSectors;
65 extern int32_t StartDataRegion;
66 
67 typedef struct
68 {
69  uint8_t BS_jmpBoot[3]; /* Jump instruction to the boot code */
70  uint8_t BS_OEMName[8]; /* Name of system that formatted the volume */
71  uint16_t BPB_BytsPerSec; /* Bytes per sector (should be 512) */
72  uint8_t BPB_SecPerClus; /* Sectors per cluster (FAT-12 = 1) */
73  uint16_t BPB_RsvdSecCnt; /* Reserved sectors (FAT-12 = 1) */
74  uint8_t BPB_NumFATs; /* FAT tables on the disk (should be 2) */
75  uint16_t BPB_RootEntCnt; /* Max directory entries in root directory */
76  uint16_t BPB_TotSec16; /* Total number of sectors on the disk (FAT-12 and FAT-16) */
77  uint8_t BPB_Media; /* Media type {fixed, removable, etc.} */
78  uint16_t BPB_FATSz16; /* Sector size of FAT table (FAT-12 = 9) */
79  uint16_t BPB_SecPerTrk; /* # of sectors per cylindrical track */
80  uint16_t BPB_NumHeads; /* # of heads per volume (1.4Mb 3.5" = 2) */
81  uint32_t BPB_HiddSec; /* # of preceding hidden sectors (0) */
82  uint32_t BPB_TotSec32; /* # of FAT-32 sectors (0 for FAT-12) */
83  uint8_t BS_DrvNum; /* A drive number for the media (OS specific) */
84  uint8_t BS_Reserved1; /* Reserved space for Windows NT (set to 0) */
85  uint8_t BS_BootSig; /* (0x29) Indicates following: */
86  uint32_t BS_VolID; /* Volume serial # (for tracking this disk) */
87  uint8_t BS_VolLab[11]; /* Volume label (RDL or "NO NAME    ") */
88  uint8_t BS_FilSysType[8];/* Deceptive FAT type Label */
90 
91 typedef struct
92 {
93  uint8_t FATByte[BYTESPERSECTOR * SECTORSPERFAT];
95 
96 typedef struct
97 { // (total 16 bits--a unsigned short)
98  uint16_t sec: 5; // low-order 5 bits are the seconds
99  uint16_t min: 6; // next 6 bits are the minutes
100  uint16_t hour: 5; // high-order 5 bits are the hour
102 
103 typedef struct
104 { // (total 16 bits--a unsigned short)
105  uint16_t day: 5; // low-order 5 bits are the day
106  uint16_t month: 4; // next 4 bits are the month
107  uint16_t year: 7; // high-order 7 bits are the year
109 
110 typedef struct
111 {
112  uint8_t Name[8]; /* File name (capital letters, padded w/spaces) */
113  uint8_t Extension[3]; /* Extension (same format as name, no '.') */
114  uint8_t Attributes; /* Holds the attributes code */
115  uint8_t Reserved[10]; /* Reserved for Windows NT (Set to zero!) */
116  FATTime Time; /* Time of last write */
117  FATDate Date; /* Date of last write */
118  uint16_t startCluster; /* Pointer to the first cluster of the file */
119  uint32_t fileSize; /* File size in bytes */
121 
122 typedef struct _diskimage
123 {
125  uint8_t BootCode[448];
127  FAT Fat[NUMFATS];
128  DirEntry DirectoryEntries[ROOTENTRIES];
129 }DISKIMAGE;
130 
131 int16_t GetFAT12Entry(DISKIMAGE *DiskImagePtr, int FATindex);
132 void SetFAT12Entry(DISKIMAGE *DiskImagePtr, int FATindex, unsigned short FAT12ClusEntryVal);
134 void CreateDiskImage(DISKIMAGE *DiskImagePtr);
135 void SetDiskMetricsFromDiskImage(DISKIMAGE *DiskImagePtr);
136 void InitializeDiskDiskImage(DISKIMAGE *DiskImagePtr);
137 void InitializeFAT12(DISKIMAGE *DiskImagePtr);
138 
139 
140 #endif /* __FATUTIL_H__ */