LPCOpen Platform  v1.03
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 extern int32_t RootEntries;
59 extern int32_t BytesPerSector;
60 extern int32_t NumFats;
61 extern int32_t SectorsPerFat;
62 extern int32_t NonDataSectors;
63 extern int32_t TotalSectors;
64 extern int32_t StartDataRegion;
65 
66 typedef struct {
67  uint8_t BS_jmpBoot[3]; /* Jump instruction to the boot code */
68  uint8_t BS_OEMName[8]; /* Name of system that formatted the volume */
69  uint16_t BPB_BytsPerSec; /* Bytes per sector (should be 512) */
70  uint8_t BPB_SecPerClus; /* Sectors per cluster (FAT-12 = 1) */
71  uint16_t BPB_RsvdSecCnt; /* Reserved sectors (FAT-12 = 1) */
72  uint8_t BPB_NumFATs; /* FAT tables on the disk (should be 2) */
73  uint16_t BPB_RootEntCnt; /* Max directory entries in root directory */
74  uint16_t BPB_TotSec16; /* Total number of sectors on the disk (FAT-12 and FAT-16) */
75  uint8_t BPB_Media; /* Media type {fixed, removable, etc.} */
76  uint16_t BPB_FATSz16; /* Sector size of FAT table (FAT-12 = 9) */
77  uint16_t BPB_SecPerTrk; /* # of sectors per cylindrical track */
78  uint16_t BPB_NumHeads; /* # of heads per volume (1.4Mb 3.5" = 2) */
79  uint32_t BPB_HiddSec; /* # of preceding hidden sectors (0) */
80  uint32_t BPB_TotSec32; /* # of FAT-32 sectors (0 for FAT-12) */
81  uint8_t BS_DrvNum; /* A drive number for the media (OS specific) */
82  uint8_t BS_Reserved1; /* Reserved space for Windows NT (set to 0) */
83  uint8_t BS_BootSig; /* (0x29) Indicates following: */
84  uint32_t BS_VolID; /* Volume serial # (for tracking this disk) */
85  uint8_t BS_VolLab[11]; /* Volume label (RDL or "NO NAME    ") */
86  uint8_t BS_FilSysType[8]; /* Deceptive FAT type Label */
88 
89 typedef struct {
90  uint8_t FATByte[BYTESPERSECTOR * SECTORSPERFAT];
92 
93 typedef struct { // (total 16 bits--a unsigned short)
94  uint16_t sec : 5; // low-order 5 bits are the seconds
95  uint16_t min : 6; // next 6 bits are the minutes
96  uint16_t hour : 5; // high-order 5 bits are the hour
98 
99 typedef struct { // (total 16 bits--a unsigned short)
100  uint16_t day : 5; // low-order 5 bits are the day
101  uint16_t month : 4; // next 4 bits are the month
102  uint16_t year : 7; // high-order 7 bits are the year
104 
105 typedef struct {
106  uint8_t Name[8]; /* File name (capital letters, padded w/spaces) */
107  uint8_t Extension[3]; /* Extension (same format as name, no '.') */
108  uint8_t Attributes; /* Holds the attributes code */
109  uint8_t Reserved[10]; /* Reserved for Windows NT (Set to zero!) */
110  FATTime Time; /* Time of last write */
111  FATDate Date; /* Date of last write */
112  uint16_t startCluster; /* Pointer to the first cluster of the file */
113  uint32_t fileSize; /* File size in bytes */
115 
116 typedef struct _diskimage {
118  uint8_t BootCode[448];
120  FAT Fat[NUMFATS];
121  DirEntry DirectoryEntries[ROOTENTRIES];
122 } DISKIMAGE;
123 
124 int16_t GetFAT12Entry(DISKIMAGE *DiskImagePtr, int FATindex);
125 
126 void SetFAT12Entry(DISKIMAGE *DiskImagePtr, int FATindex, unsigned short FAT12ClusEntryVal);
127 
129 
130 void CreateDiskImage(DISKIMAGE *DiskImagePtr);
131 
132 void SetDiskMetricsFromDiskImage(DISKIMAGE *DiskImagePtr);
133 
134 void InitializeDiskDiskImage(DISKIMAGE *DiskImagePtr);
135 
136 void InitializeFAT12(DISKIMAGE *DiskImagePtr);
137 
138 #endif /* __FATUTIL_H__ */