C/ FS TM The Embedded File System User's Manual V4.04 Micrim 1290 Weston Road, Suite 306 Weston, FL 33326 USA www.micrium.com Designations used by companies to distinguish their products are often claimed as trademarks. In all instances where Micrim Press is aware of a trademark claim, the product name appears in initial capital letters, in all capital letters, or in accordance with the vendor's capatilization preference. Readers should contact the appropriate companies for more complete information on trademarks and trademark registrations. All trademarks and registerd trademarks in this book are the property of their respective holders. Copyright (c) 2010 by Micrim except where noted otherwise. All rights reserved. Printed in the United States of America. No part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the publisher; with the exception that the program listings may be entered, stored, and executed in a computer system, but they may not be reproduced for publication. The programs and code examples in this book are presented for instructional value. The programs and examples have been carefully tested, but are not guaranteed to any particular purpose. The publisher does not offer any warranties and does not guarantee the accuracy, adequacy, or completeness of any information herein and is not responsible for any errors and ommissions. The publisher assumes no liability for damages resulting from the use of the information in this book or for any infringement of the intellectual property rights of third parties that would result from the use of this information. 600-uC-FS-001 Table of Contents Chapter 1 1-1 1-2 1-3 1-4 1-5 Introduction .......................................................................................... 15 What Is a File System? ........................................................................ 15 C/FS .................................................................................................... 16 Typical Usages ..................................................................................... 17 Why FAT? ............................................................................................. 18 Chapter Contents ................................................................................. 18 Chapter 2 2-1 2-1-1 2-1-2 2-1-3 2-1-4 2-1-5 2-1-6 2-1-7 2-1-8 C/FS Architecture .............................................................................. 22 Architecture Components ................................................................... 24 Your Application ................................................................................... 24 LIB (Libraries) ....................................................................................... 24 POSIX API Layer .................................................................................. 24 FS Layer ............................................................................................... 24 File System Driver Layer ...................................................................... 26 Device Driver Layer .............................................................................. 26 CPU Layer ............................................................................................ 26 RTOS Layer .......................................................................................... 27 Chapter 3 3-1 3-2 3-3 3-4 3-5 3-6 3-7 3-8 3-9 3-10 Directories and Files ............................................................................ 28 Application Code ................................................................................. 31 CPU ....................................................................................................... 33 Board Support Package (BSP) ............................................................ 34 C/CPU, CPU Specific Source Code .................................................. 35 C/LIB, Portable Library Functions ..................................................... 37 C/Clk, Time/Calendar Management .................................................. 38 C/CRC, Checksums and Error Correction Codes ............................ 40 C/FS Platform-Independent Source Code ........................................ 42 C/FS FAT Filesystem Source Code ................................................... 45 C/FS Memory Device Drivers ............................................................ 46 3 3-11 3-12 3-13 C/FS Platform-Specific Source Code ............................................... 50 C/FS OS Abstraction Layer ............................................................... 51 Summary .............................................................................................. 52 Chapter 4 4-1 4-2 4-3 4-4 4-5 Miscellaneous ...................................................................................... 59 Nomenclature ....................................................................................... 59 C/FS Device and Volume Names ...................................................... 61 C/FS File and Directory Names and Paths ....................................... 62 C/FS Name Lengths ........................................................................... 63 Resource Usage ................................................................................... 65 Chapter 5 5-1 5-2 5-3 5-4 5-5 5-6 5-7 5-7-1 5-7-2 Devices and Volumes .......................................................................... 67 Device Operations ............................................................................... 68 Using Devices ...................................................................................... 69 Using Removable Devices ................................................................... 71 Partitions .............................................................................................. 72 Volume Operations .............................................................................. 76 Using Volumes ..................................................................................... 77 Using Volume Cache ............................................................................ 79 Choosing Cache Parameters ............................................................... 80 Other Caching & Buffering Mechanisms ............................................. 82 Chapter 6 6-1 6-2 6-3 6-3-1 6-3-2 6-3-3 6-3-4 6-3-5 6-4 6-5 POSIX API ............................................................................................. 83 Supported Functions ........................................................................... 84 Working Directory Functions ............................................................... 85 File Access Functions .......................................................................... 86 Opening, Reading & Writing Files ....................................................... 87 Getting or Setting the File Position ..................................................... 90 Configuring a File Buffer ...................................................................... 91 Diagnosing a File Error ........................................................................ 93 Atomic File Operations Using File Lock .............................................. 93 Directory Access Functions ................................................................ 94 Entry Access Functions ....................................................................... 96 Chapter 7 7-1 7-1-1 Files ...................................................................................................... 97 File Access Functions .......................................................................... 98 Opening Files ....................................................................................... 99 4 7-1-2 7-1-3 7-1-4 7-1-5 7-2 7-2-1 7-2-2 7-2-3 Getting Information About a File ....................................................... 100 Configuring a File Buffer .................................................................... 101 File Error Functions ............................................................................ 102 Atomic File Operations Using File Lock ............................................ 102 Entry Access Functions ..................................................................... 103 File and Directory Attributes .............................................................. 104 Creating New Files and Directories ................................................... 105 Deleting Files and Directories ........................................................... 106 Chapter 8 8-1 Directories .......................................................................................... 107 Directory Access Functions .............................................................. 108 Chapter 9 9-1 9-1-1 9-1-2 9-1-3 9-1-4 9-2 9-2-1 9-2-2 9-2-3 File Systems: FAT .............................................................................. 109 FAT Architecture ................................................................................ 110 FAT12 / FAT16 / FAT32 ...................................................................... 111 Short and Long File Names ............................................................... 111 Directories and Directory Entries ...................................................... 112 FAT System Driver Architecture ........................................................ 114 Operations .......................................................................................... 115 Formatting .......................................................................................... 115 Disk Check ......................................................................................... 116 Journaling ........................................................................................... 117 Chapter 10 10-1 10-1-1 Device Drivers .................................................................................... 119 Provided Device Drivers .................................................................... 120 Driver Characterization ...................................................................... 121 Chapter 11 11-1 11-2 11-2-1 11-2-2 IDE/CF Driver ..................................................................................... 124 Files and Directories .......................................................................... 124 Using the IDE/CF Driver ..................................................................... 125 ATA (True IDE) Communication ......................................................... 128 IDE BSP Overview .............................................................................. 131 Chapter 12 Logical Device Driver ......................................................................... 133 Chapter 13 MSC Driver ......................................................................................... 134 5 13-1 13-2 Files and Directories .......................................................................... 134 Using the MSC Driver ........................................................................ 135 Chapter 14 14-1 14-2 14-3 14-3-1 14-3-2 14-3-3 14-4 14-4-1 14-4-2 14-4-3 NAND Flash Driver ............................................................................. 137 Files and Directories .......................................................................... 138 Driver & Device Characteristics ......................................................... 139 Using a NAND Device (Software ECC) .............................................. 140 Driver Architecture ............................................................................. 146 Hardware ............................................................................................ 146 NAND BSP Overview ......................................................................... 148 Physical-Layer Drivers ....................................................................... 148 FSDev_NAND_0512x08 ...................................................................... 149 FSDev_NAND_2048x08, FSDev_NAND_2048x16 ............................. 149 FSDev_NAND_AT45 ........................................................................... 150 Chapter 15 15-1 15-2 15-3 15-3-1 15-3-2 15-3-3 15-4 15-4-1 15-4-2 15-5 15-5-1 15-5-2 15-5-3 15-5-4 15-5-5 NOR Flash Driver ............................................................................... 151 Files and Directories .......................................................................... 152 Driver & Device Characteristics ......................................................... 154 Using a Parallel NOR Device ............................................................. 156 Driver Architecture ............................................................................. 160 Hardware ............................................................................................ 160 NOR BSP Overview ............................................................................ 162 Using a Serial NOR Device ................................................................ 163 Hardware ............................................................................................ 164 NOR SPI BSP Overview ..................................................................... 165 Physical-Layer Drivers ....................................................................... 166 FSDev_NOR_AMD_1x08, FSDev_NOR_AMD_1x16 .......................... 167 FSDev_NOR_Intel_1x16 ..................................................................... 167 FSDev_NOR_SST39 ........................................................................... 168 FSDev_NOR_STM25 .......................................................................... 168 FSDev_NOR_SST25 ........................................................................... 169 Chapter 16 16-1 16-2 RAM Disk Driver ................................................................................. 170 Files and Directories .......................................................................... 170 Using the RAM Disk Driver ................................................................ 171 Chapter 17 SD/MMC Drivers ................................................................................ 174 6 17-1 17-2 17-2-1 17-2-2 17-2-3 17-3 17-3-1 17-3-2 17-3-3 Files and Directories .......................................................................... 176 Using the SD/MMC CardMode Driver ............................................... 177 SD/MMC CardMode Communication ............................................... 180 SD/MMC CardMode Communication Debugging ............................ 182 SD/MMC CardMode BSP Overview .................................................. 187 Using the SD/MMC SPI Driver ........................................................... 189 SD/MMC SPI Communication ........................................................... 193 SD/MMC SPI Communication Debugging ........................................ 194 SD/MMC SPI BSP Overview .............................................................. 197 Appendix A A-1 A-1-1 A-1-2 A-1-3 A-1-4 A-1-5 A-2 A-2-1 A-2-2 A-2-3 A-2-4 A-2-5 A-2-6 A-2-7 A-2-8 A-2-9 A-2-10 A-2-11 A-2-12 A-2-13 A-2-14 A-2-15 A-2-16 A-2-17 A-2-18 A-2-19 A-2-20 C/FS API Reference Manual ............................................................ 198 General File System Functions .......................................................... 200 FS_DevDrvAdd() ................................................................................. 201 FS_Init() ............................................................................................... 203 FS_VersionGet() .................................................................................. 204 FS_WorkingDirGet() ........................................................................... 205 FS_WorkingDirSet() ............................................................................ 206 Posix API Functions ........................................................................... 207 fs_asctime_r() ..................................................................................... 210 fs_chdir() ............................................................................................. 211 fs_clearerr() ......................................................................................... 212 fs_closedir() ........................................................................................ 213 fs_ctime_r() ......................................................................................... 214 fs_fclose() ........................................................................................... 215 fs_feof() ............................................................................................... 216 fs_ferror() ............................................................................................ 217 fs_fflush() ............................................................................................ 218 fs_fgetpos() ......................................................................................... 219 fs_flockfile() ........................................................................................ 220 fs_fopen() ............................................................................................ 221 fs_fread() ............................................................................................. 222 fs_fseek() ............................................................................................ 223 fs_fsetpos() ......................................................................................... 225 fs_ftell() ............................................................................................... 226 fs_ftruncate() ...................................................................................... 227 fs_ftrylockfile() .................................................................................... 228 fs_funlockfile() .................................................................................... 229 fs_fwrite() ............................................................................................ 230 7 A-2-21 A-2-22 A-2-23 A-2-24 A-2-25 A-2-26 A-2-27 A-2-28 A-2-29 A-2-30 A-2-31 A-2-32 A-3 A-3-1 A-3-2 A-3-3 A-3-4 A-3-5 A-3-6 A-3-7 A-3-8 A-3-9 A-3-10 A-3-11 A-3-12 A-3-13 A-4 A-4-1 A-4-2 A-4-3 A-4-4 A-5 A-5-1 A-5-2 A-5-3 A-5-4 A-5-5 A-5-6 fs_getcwd() ......................................................................................... 231 fs_localtime_r() ................................................................................... 232 fs_mkdir() ............................................................................................ 233 fs_mktime() ......................................................................................... 234 fs_opendir() ......................................................................................... 235 fs_readdir_r() ...................................................................................... 236 fs_remove() ......................................................................................... 237 fs_rename() ......................................................................................... 239 fs_rewind() .......................................................................................... 241 fs_rmdir() ............................................................................................. 242 fs_setbuf() ........................................................................................... 244 fs_setvbuf() ......................................................................................... 245 Device Functions ............................................................................... 247 FSDev_Close() .................................................................................... 249 FSDev_GetDevName() ....................................................................... 250 FSDev_GetDevCnt() ........................................................................... 251 FSDev_GetDevCntMax() .................................................................... 252 FSDev_GetNbrPartitions() .................................................................. 253 FSDev_Open() .................................................................................... 254 FSDev_PartitionAdd() ......................................................................... 256 FSDev_PartitionFind() ........................................................................ 257 FSDev_PartitionInit() .......................................................................... 259 FSDev_Query() ................................................................................... 261 FSDev_Rd() ......................................................................................... 262 FSDev_Refresh() ................................................................................. 264 FSDev_Wr() ......................................................................................... 266 Directory Access Functions .............................................................. 267 FSDir_Close() ...................................................................................... 268 FSDir_IsOpen() ................................................................................... 269 FSDir_Open() ...................................................................................... 270 FSDir_Rd() .......................................................................................... 272 Entry Access Functions ..................................................................... 273 FSEntry_AttribSet() ............................................................................. 274 FSEntry_Copy() .................................................................................. 276 FSEntry_Create() ................................................................................ 278 FSEntry_Del() ...................................................................................... 280 FSEntry_Query() ................................................................................. 282 FSEntry_Rename() .............................................................................. 284 8 A-5-7 A-6 A-6-1 A-6-2 A-6-3 A-6-4 A-6-5 A-6-6 A-6-7 A-6-8 A-6-9 A-6-10 A-6-11 A-6-12 A-6-13 A-6-14 A-6-15 A-6-16 A-6-17 A-7 A-7-1 A-7-2 A-7-3 A-7-4 A-7-5 A-7-6 A-7-7 A-7-8 A-7-9 A-7-10 A-7-11 A-7-12 A-7-13 A-7-14 A-8 A-8-1 A-8-2 A-8-3 FSEntry_TimeSet() .............................................................................. 286 File Functions ..................................................................................... 288 FSFile_BufAssign() ............................................................................. 290 FSFile_BufFlush() ............................................................................... 292 FSFile_Close() ..................................................................................... 293 FSFile_ClrErr() .................................................................................... 294 FSFile_IsEOF() .................................................................................... 295 FSFile_IsErr() ...................................................................................... 296 FSFile_IsOpen() .................................................................................. 297 FSFile_LockAccept() .......................................................................... 299 FSFile_LockGet() ................................................................................ 300 FSFile_LockSet() ................................................................................ 301 FSFile_Open() ..................................................................................... 302 FSFile_PosGet() .................................................................................. 305 FSFile_PosSet() .................................................................................. 306 FSFile_Query() .................................................................................... 308 FSFile_Rd() ......................................................................................... 309 FSFile_Truncate() ............................................................................... 311 FSFile_Wr() ......................................................................................... 312 Volume Functions .............................................................................. 314 FSVol_Close() ..................................................................................... 316 FSVol_Fmt() ........................................................................................ 317 FSVol_GetDfltVolName() .................................................................... 319 FSVol_GetVolCnt() .............................................................................. 320 FSVol_GetVolCntMax() ....................................................................... 321 FSVol_GetVolName() .......................................................................... 322 FSVol_IsDflt() ...................................................................................... 323 FSVol_IsMounted() ............................................................................. 324 FSVol_LabelGet() ................................................................................ 325 FSVol_LabelSet() ................................................................................ 327 FSVol_Open() ...................................................................................... 329 FSVol_Query() ..................................................................................... 331 FSVol_Rd() .......................................................................................... 332 FSVol_Wr() .......................................................................................... 334 Volume Cache Functions ................................................................... 335 FSVol_CacheAssign () ........................................................................ 336 FSVol_CacheInvalidate () ................................................................... 338 FSVol_CacheFlush () .......................................................................... 339 9 A-9 A-9-1 A-9-2 A-9-3 A-9-4 A-9-5 A-9-6 A-10 A-10-1 A-10-2 A-10-3 A-10-4 A-10-5 A-10-6 A-10-7 A-10-8 A-10-9 A-11 A-11-1 A-11-2 A-11-3 A-12 A-12-1 A-12-2 A-12-3 A-12-4 A-12-5 NAND Driver Functions ...................................................................... 340 FSDev_NAND_LowFmt() .................................................................... 341 FSDev_NAND_LowMount() ................................................................ 342 FSDev_NAND_LowUnmount() ........................................................... 343 FSDev_NAND_PhyRdSec() ................................................................ 344 FSDev_NAND_PhyWrSec() ................................................................ 346 FSDev_NAND_PhyEraseBlk() ............................................................ 348 NOR Driver Functions ........................................................................ 350 FSDev_NOR_LowFmt() ...................................................................... 352 FSDev_NOR_LowMount() .................................................................. 353 FSDev_NOR_LowUnmount() .............................................................. 354 FSDev_NOR_LowCompact() .............................................................. 355 FSDev_NOR_LowDefrag() .................................................................. 356 FSDev_NOR_PhyRd() ......................................................................... 357 FSDev_NOR_PhyWr() ......................................................................... 359 FSDev_NOR_PhyEraseBlk() ............................................................... 361 FSDev_NOR_PhyEraseChip() ............................................................ 363 SD/MMC Driver Functions ................................................................. 364 FSDev_SD_xxx_QuerySD() ................................................................ 365 FSDev_SD_xxx_RdCID() .................................................................... 367 FSDev_SD_xxx_RdCSD() ................................................................... 369 FAT System Driver Functions ............................................................ 370 FS_FAT_JournalOpen() ...................................................................... 371 FS_FAT_JournalClose() ...................................................................... 372 FS_FAT_JournalStart() ....................................................................... 373 FS_FAT_JournalStop() ....................................................................... 374 FS_FAT_VolChk() ................................................................................ 375 Appendix B B-1 B-2 B-3 B-4 B-5 B-6 B-7 B-8 B-9 C/FS Error Codes ............................................................................. 376 System Error Codes ........................................................................... 376 Buffer Error Codes ............................................................................. 376 Cache Error Codes ............................................................................ 377 Device Error Codes ............................................................................ 377 Device Driver Error Codes ................................................................. 378 Directory Error Codes ........................................................................ 378 ECC Error Codes ................................................................................ 378 Entry Error Codes .............................................................................. 378 File Error Codes ................................................................................. 379 10 B-10 B-11 B-12 B-13 B-14 B-15 Name Error Codes ............................................................................. 379 Partition Error Codes ......................................................................... 380 Pools Error Codes .............................................................................. 380 File System Error Codes .................................................................... 380 Volume Error Codes ........................................................................... 381 OS Layer Error Codes ........................................................................ 382 Appendix C C-1 C-2 C-3 C-4 C-4-1 C-4-2 C-4-3 C-4-4 C-4-5 C-4-6 C-4-7 C-4-8 C-5 C-5-1 C-5-2 C-5-3 C-5-4 C-5-5 C-5-6 C-5-7 C-5-8 C-5-9 C-5-10 C-5-11 C-5-12 C-5-13 C-5-14 C-5-15 C-6 C-6-1 C/FS Porting Manual ........................................................................ 383 Date/Time management .................................................................... 385 CPU Port ............................................................................................. 386 OS Kernel ........................................................................................... 386 Device Driver ...................................................................................... 394 NameGet() .......................................................................................... 396 Init() ..................................................................................................... 397 Open() ................................................................................................. 398 Close() ................................................................................................. 400 Rd() ..................................................................................................... 401 Wr() ..................................................................................................... 402 Query() ................................................................................................ 404 IO_Ctrl() ............................................................................................... 406 IDE/CF Device BSP ............................................................................ 408 FSDev_IDE_BSP_Open() .................................................................... 410 FSDev_IDE_BSP_Close() ................................................................... 411 FSDev_IDE_BSP_Lock() / FSDev_IDE_BSP_Unlock() ...................... 412 FSDev_IDE_BSP_Reset() ................................................................... 413 FSDev_IDE_BSP_RegRd() ................................................................. 414 FSDev_IDE_BSP_RegWr() ................................................................. 415 FSDev_IDE_BSP_CmdWr() ................................................................ 416 FSDev_IDE_BSP_DataRd() ................................................................ 417 FSDev_IDE_BSP_DataWr() ................................................................ 418 FSDev_IDE_BSP_DMA_Start() ........................................................... 419 FSDev_IDE_BSP_DMA_End() ............................................................ 420 FSDev_IDE_BSP_GetDrvNbr() ........................................................... 422 FSDev_IDE_BSP_GetModesSupported() .......................................... 423 FSDev_IDE_BSP_SetMode() .............................................................. 424 FSDev_IDE_BSP_Dly400_ns() ............................................................ 425 NAND Flash Physical-Layer Driver .................................................... 426 Open() ................................................................................................. 429 11 C-6-2 C-6-3 C-6-4 C-6-5 C-6-6 C-6-7 C-6-8 C-6-9 C-7 C-7-1 C-7-2 C-7-3 C-7-4 C-7-5 C-7-6 C-7-7 C-7-8 C-7-9 C-8 C-9 C-9-1 C-9-2 C-9-3 C-9-4 C-9-5 C-9-6 C-10 C-10-1 C-10-2 C-10-3 C-10-4 C-10-5 C-10-6 C-11 C-12 C-12-1 C-12-2 C-12-3 Close() ................................................................................................. 431 RdPage() ............................................................................................. 432 RdSpare() ............................................................................................ 434 WrPage() ............................................................................................. 435 WrSpare() ............................................................................................ 436 CopyBack() ......................................................................................... 437 EraseBlk() ........................................................................................... 438 IO_Ctrl() ............................................................................................... 439 NAND Flash BSP ................................................................................ 440 FSDev_NAND_BSP_Open() ............................................................... 441 FSDev_NAND_BSP_Close() ............................................................... 442 FSDev_NAND_BSP_ChipSelEn() ....................................................... 443 FSDev_NAND_BSP_ChipSelDis() ...................................................... 444 FSDev_NAND_BSP_RdData() ............................................................ 445 FSDev_NAND_BSP_WrAddr() ............................................................ 446 FSDev_NAND_BSP_WrCmd() ............................................................ 447 FSDev_NAND_BSP_WrData() ............................................................ 448 FSDev_NAND_BSP_WaitWhileBusy() ............................................... 449 NAND Flash SPI BSP ......................................................................... 450 NOR Flash Physical-Layer Driver ...................................................... 450 Open() ................................................................................................. 453 Close() ................................................................................................. 454 Rd() ..................................................................................................... 455 Wr() ..................................................................................................... 456 EraseBlk() ........................................................................................... 457 IO_Ctrl() ............................................................................................... 458 NOR Flash BSP .................................................................................. 459 FSDev_NOR_BSP_Open() .................................................................. 460 FSDev_NOR_BSP_Close() ................................................................. 461 FSDev_NOR_BSP_Rd_XX() ................................................................ 462 FSDev_NOR_BSP_RdWord_XX() ....................................................... 463 FSDev_NOR_BSP_WrWord_XX() ....................................................... 464 FSDev_NOR_BSP_WaitWhileBusy() .................................................. 465 NOR Flash SPI BSP ........................................................................... 466 SD/MMC Cardmode BSP .................................................................. 467 FSDev_SD_Card_BSP_Open() ........................................................... 470 FSDev_SD_Card_BSP_Lock() ............................................................ 471 FSDev_SD_Card_BSP_CmdStart() .................................................... 472 12 C-12-4 C-12-5 C-12-6 C-12-7 C-12-8 C-12-9 C-12-10 C-12-11 C-12-12 C-13 C-14 C-14-1 C-14-2 C-14-3 C-14-4 C-14-5 C-14-6 C-14-7 FSDev_SD_Card_BSP_CmdWaitEnd() .............................................. 477 FSDev_SD_Card_BSP_CmdDataRd() ............................................... 481 FSDev_SD_Card_BSP_CmdDataWr() ............................................... 484 FSDev_SD_Card_BSP_GetBlkCntMax() ............................................ 487 FSDev_SD_Card_BSP_GetBusWidthMax() ....................................... 488 FSDev_SD_Card_BSP_SetBusWidth() .............................................. 489 FSDev_SD_Card_BSP_SetClkFreq() ................................................. 491 FSDev_SD_Card_BSP_SetTimeoutData() ......................................... 492 FSDev_SD_Card_BSP_SetTimeoutResp() ........................................ 493 SD/MMC SPI mode BSP .................................................................... 493 SPI BSP .............................................................................................. 494 Open() ................................................................................................. 499 Close() ................................................................................................. 501 Lock() / Unlock() ................................................................................. 502 Rd() ..................................................................................................... 503 Wr() ..................................................................................................... 504 ChipSelEn() /ChipSelDis() .................................................................. 505 SetClkFreq() ........................................................................................ 506 Appendix D D-1 D-2 D-3 D-4 D-5 D-6 D-7 D-8 D-9 D-10 C/FS Types and Structures ............................................................. 507 FS_CFG .............................................................................................. 508 FS_DEV_INFO ..................................................................................... 510 FS_DEV_NAND_CFG .......................................................................... 511 FS_DEV_NOR_CFG ............................................................................ 513 FS_DEV_RAM_CFG ............................................................................ 516 FS_DIR_ENTRY (struct fs_dirent) ...................................................... 517 FS_ENTRY_INFO ................................................................................ 518 FS_FAT_SYS_CFG ............................................................................. 520 FS_PARTITION_ENTRY ..................................................................... 522 FS_VOL_INFO ..................................................................................... 523 Appendix E E-1 E-2 E-3 E-4 E-5 C/FS Configuration .......................................................................... 525 File System Configuration ................................................................. 526 Feature Inclusion Configuration ........................................................ 527 Name Restriction Configuration ........................................................ 530 Debug Configuration .......................................................................... 531 Argument Checking Configuration .................................................... 531 13 E-6 E-7 E-8 E-9 File System Counter Configuration ................................................... 532 Fat Configuration ............................................................................... 532 SD/MMC SPI Configuration ............................................................... 533 Trace Configuration ........................................................................... 534 Appendix F F-1 F-2 F-3 F-3-1 F-3-2 F-3-3 F-3-4 F-3-5 F-3-6 F-3-7 F-3-8 F-3-9 F-3-10 F-3-11 F-3-12 F-3-13 F-3-14 F-3-15 F-3-16 F-3-17 F-4 Shell Commands ................................................................................ 535 Files and Directories .......................................................................... 536 Using the Shell Commands ............................................................... 537 Commands ......................................................................................... 540 fs_cat .................................................................................................. 541 fs_cd ................................................................................................... 542 fs_cp ................................................................................................... 544 fs_date ................................................................................................ 545 fs_df .................................................................................................... 546 fs_ls ..................................................................................................... 547 fs_mkdir .............................................................................................. 548 fs_mkfs ............................................................................................... 549 fs_mount ............................................................................................. 550 fs_mv .................................................................................................. 551 fs_od ................................................................................................... 552 fs_pwd ................................................................................................ 553 fs_rm ................................................................................................... 554 fs_rmdir ............................................................................................... 555 fs_touch .............................................................................................. 556 fs_umount ........................................................................................... 557 fs_wc ................................................................................................... 558 Configuration ...................................................................................... 559 Appendix G Bibliography ....................................................................................... 561 Appendix H H-1 H-1-1 H-1-2 H-1-3 H-1-4 C/FS Licensing Policy ...................................................................... 563 C/FS Licensing ................................................................................. 563 C/FS Source Code ........................................................................... 563 C/FS Maintenance Renewal ............................................................ 564 C/FS Source Code Updates ............................................................ 564 C/FS Support ................................................................................... 564 14 Chapter 1 Introduction Files and directories are common abstractions, which we encounter daily when sending an e-mail attachment, downloading a new application or archiving old information. Those same abstractions may be leveraged in an embedded system for similar tasks or for unique ones. A device may serve web pages, play or record media (images, video or music) or log data. The file system software which performs such actions must meet the general expectations of an embedded environment--a limited code footprint, for instance--which still delivering good performance. 1-1 WHAT IS A FILE SYSTEM? A file system is a collection of files and directories; since directories are containers of files, a hierarchical organization results. A PC operating system such as Windows or Linux presents its file systems through a visual interface (e.g, "Windows Explorer"), with a tree-like structure of entries that can be moved, renamed or deleted with menus or actions like "dragging and dropping". Alternatively, a headless system like DOS (or any other command line) integrates utilities to accomplish the same operations. Above, we stated that a system "presents its file systems"--file systems plural--because each drive is a separate file system, a separate collection of files. Each of these is anchored by some unique drive letter (Windows) or mount point (Linux) within the larger context of a "virtual" file system wherein every entry has a unique identifier. (Within the "everything is a file" mentality of Linux, this is taken further, but that is beyond this discussion.) Being separate, each file system may have a different format--one may be FAT, the next NTFS-- and will be located on different physical devices or on separate partitions of the same device. If files are to be read from a volume, file system software is required, with three basic elements. First, a device driver must be able to read and write to the device. Next, a file system driver must be able to parse the device's on-disk structures to read the names, 15 Introduction properties and data of files and to format those structures to modify existing entries and create new ones. Finally, an application-level interface must provide for the exigencies of file and directory access. 1-2 C/FS C/FS is a compact, reliable, high-performance file system. It offers full-featured file and directory access with flexible device and volume management including support for partitions. Source Code: C/FS is provided in ANSI-C source to licensees. The source code is written to an exacting coding standard that emphasizes cleanness and readability. Moreover, extensive comments pepper the code to elucidate its logic and describe global variables and functions. Where appropriate, the code directly references standards and supporting documents. Device Drivers: Device drivers are available for most common media including SD/MMC cards, NAND flash, NOR flash and IDE/CF. Each of these is written with a clear, layered structure so that it can easily be ported to your hardware. The device driver structure is simple--basically just initialization, read and write functions--so that C/FS can easily be ported to a new medium. Devices and Volumes: Multiple media can be accessed simultaneously, including multiple instances of the same type of medium (since all drivers are re-entrant). DOS partitions are supported, so more than one volume can be located on a device. In addition, the logical device driver allows a single volume to span several (typically identical) devices, such as a bank of flash chips. FAT: All standard FAT variants and features are supported including FAT12/FAT16/FAT32 and long file names, which encompasses Unicode file names. Files can be up to 4-GB and volumes up to 8-TB (the standard maximum). An optional journaling module provides total power fail-safety to the FAT system driver. Application Programming Interface (API): C/FS provides two APIs for file and directory access. A standard POSIX-compatible API is provided, including functions like fs_fwrite(), fs_fread() and fs_fsetpos() that have the same arguments and return values as 16 Introduction the POSIX functions fwrite(), fread() and fsetpos(). Another API with parallel argument placement and meaningful return error codes is provided as an alternate, with functions like FSFile_Wr(), FSFile_Rd() and FSFile_PosSet(). Scalable: The memory footprint of C/FS can be adjusted at compile-time based on the features you need and the desired level of run-time argument checking. For applications with limited RAM, features such as cache and read/write buffering can be disabled; for applications with sufficient RAM, these features can be enabled in order to gain better performance. Portable: C/FS was designed for resource-constrained embedded applications. Although C/FS can work on 8- and 16-bit processors, it will work best with 32- or 64-bit CPUs. RTOS: C/FS does not assume the presence of a RTOS kernel. However, if you are using a RTOS, a simple port layer is required (consisting of a few semaphores), in order to prevent simultaneous access to core structures from different tasks. If you are not using a RTOS, this port layer may consist of empty functions. 1-3 TYPICAL USAGES Applications have sundry reasons for non-volatile storage. A subset require (or benefit from) organizing data into named files within a directory hierarchy on a volume--basically, from having a file system. Perhaps the most obvious expose the structure of information to the user, like products that store images, video or music that are transferred to or from a PC. A web interface poses a similar opportunity, since the URLs of pages and images fetched by the remote browser would resolve neatly to locations on a volume. Another typical use is data logging. A primary purpose of a device may be to collect data from its environment for later retrieval. If the information must persist across device reset events or will exceed the capacity of its RAM, some non-volatile memory is necessary. The benefit of a file system is the ability to organize that information logically, with a fitting directory structure, through a familiar API. A file system can also store programs. In a simple embedded CPU, the program is stored at a fixed location in a non-volatile memory (usually flash). If an application must support firmware updates, a file system may be a more convenient place, since the software handles the details of storing the program. The boot-loader, of course, would need to be able to 17 Introduction load the application, but since that requires only read-only access, no imposing program is required. The ROM boot-loaders in some CPUs can check the root directory of a SD card for a binary in addition to the more usual locations such as external NAND or NOR flash. 1-4 WHY FAT? File Allocation Table (FAT) is a simple file system, widely supported across major OSs. While it has been supplanted as the format of hard drives in Windows PCs, removable media still use FAT because of its wide support. That is suitable for embedded systems, which would often be challenged to muster the resources for the modern file systems developed principally for large fixed disks. C/FS supports FAT because of the interoperability requirements of removable media, allowing that a storage medium be removed from an embedded device and connected to a PC. All variants and extensions are supported to specification. A notorious weakness of FAT (exacerbated by early Windows system drivers) is its non-fail safe architecture. Certain operations leave the file system in an inconsistent state, albeit briefly, which may corrupt the disk or force a disk check upon unexpected power failure. C/FS minimizes the problem by ordering modifications wisely. The problem is completely solved in an optional journaling module which logs information about pending changes so those can be resumed on start-up after a power failure. 1-5 CHAPTER CONTENTS Figure 1-1 shows the layout and flow of the book. This diagram should be useful to understand the relationship between chapters. The first (leftmost) column lists chapters that should be read in order to understand C/FS's structure. The chapters in the second column give greater detail about the application of C/FS. Each of the chapters in the third column examines a storage technology and its device driver. Finally, the fourth column lists the appendices, the topmost being the C/FS reference, configuration and porting manuals. Reference these sections regularly when designing a product using C/FS. 18 Introduction $ &)6$3, 5HIHUHQFH 0DQXDO % &)6 (UURU &RGHV & &)6 3RUWLQJ 0DQXDO 3UHIDFH ,QWURGXFWLRQ &)6 $UFKLWHFWXUH 'LUHFWRULHV DQG )LOHV ,'(&) 'ULYHU /RJLFDO 'HYLFHV 'ULYHU ' &)6 7\SHVDQG 6WUXFWXUHV 0DVV6WRUDJH &ODVV 06& 'ULYHU ( &)6 &RQILJXUDWLRQ 0DQXDO 1$1' )ODVK 'ULYHU ) 125 )ODVK 'ULYHU &)6 6KHOO &RPPDQGV 0LVFHOODQHRXV 'HYLFHV DQG 9ROXPHV 326,; $3, )$7)LOH 6\VWHP 'HYLFH 'ULYHUV )LOHV 'LUHFWRULHV 5$0'LVN 'ULYHU 6'00& 'ULYHU * %LEOLRJUDSK\ + /LFHQVLQJ 3ROLF\ Figure 1-1 C/FS Book Layout Chapter 1, Introduction. This chapter. Chapter 2, C/FS Architecture. This chapter contains a simplified block diagram of the various different C/FS modules and their relationships. The relationships are then explained. Chapter 3, Directories and Files. This chapter explains the directory structure and files needed to build a C/FS-based application. Learn about the files that are needed, where they should be placed, which module does what, and more. Chapter 4, Miscellaneous. In this chapter, you will learn the nomenclature used in C/FS to access files and folders and the ressources needed to use C/FS in your application. Chapter 5, Devices and Volumes. Every file and directory accessed with C/FS is a constituent of a volume (a collection of files and directories) on a device (a physical or logical sector-addressed entity). This chapter explains how devices and volumes are managed. 19 Introduction Chapter 6, POSIX API. The best-known API for accessing and managing files and directories is specified within the POSIX standard (IEEE Std 1003.1), which is based in part in the ISO C standard (ISO/IEC 9899). This chapter explains how to use this API and examines some of its pitfalls and shortcomings. Chapter 7, Files. C/FS complements the POSIX API with its own file access API. This chapter explains this API. Chapter 8, Directories. C/FS complements the POSIX API with its own directory access API. This chapter explains this API. Chapter 9, FAT File System. This chapter details the low-level architecture of the FAT file system. Though the API of C/FS is file system agnostic, the file system type does affect performance, reliability and security, as explained here as well. Chapter 10, Device Drivers. All hardware accesses are eventually performed by a device driver. This chapter describes the drivers available with C/FS and broadly profiles supported media types in terms of cost, performance and complexity. Chapter 11, IDE Devices. The IDE driver supports compact flash (CF) cards and ATA IDE hard drives. Chapter 12, Logical Devices Driver. This feature is not available yet. Chapter 13, Mass Storage Class (MSC) Driver. The now-common USB drive implements the Mass Storage Class (MSC) protocol, and a CPU with a USB host interface can access these devices with appropriate software. The MSC driver, discussed in this chapter, with C/USB-Host is just such appropriate software. Chapter 14, NAND Flash. NAND flash is the first category of flash media. Write speeds are fast (compared to NOR flash), at the expense of slower read speeds and complexities such as bit-errors and page program limitations. This chapter describes the functions of these devices and the architecture of the supporting driver. Chapter 15, NOR Flash. NOR flash is the second category of flash media. They suffer slow write speeds, balanced with blazingly-fast read speeds. Importantly, they are not plagued by the complications of NAND flash, which simplifies interfacing with them. This chapter describes the function of these devices and the architecture of the supporting driver. 20 Introduction Chapter 16, RAM Disk. This chapter demonstrates the use of the simplest storage medium, the RAM disk. Chapter 17, SD/MMC Devices. SD and MMC cards are flash-based removable storage devices commonly used in consumer electronics. For embedded CPUs, a SD/MMC card is an appealing medium because of its simple and widely-supported physical interfaces (one choice is SPI). This chapter describes the interface and function of these devices. Appendix A, C/FS API Reference Manual. The reference manual describes every API function. The arguments and return value of each function are given, supplemented by notes about its use and an example code listing. Appendix B, C/FS Error Codes. This appendix provides a brief explanation of C/FS error codes defined in fs_err.h. Appendix C, C/FS Porting Manual. The portability of C/FS relies upon ports to interface between its modules and the platform or environment. Most of the ports constitute the board support package (BSP), which is interposed between the file system suite (or driver) and hardware. The OS port adapts the software to a particularly OS kernel. The porting manual describes each port function. Appendix D, C/FS Types and Structures. This appendix provides a reference to the C/FS types and structures. Appendix E, C/FS API Configuration Manual. C/FS is configured via defines in a single configuration file, fs_cfg.h. The configuration manual specifies each define and the meaning of possible values. Appendix F, C/FS Shell Commands. A familiar method of accessing a file system, at least to engineers and computer scientists, is the command line. In an embedded system, a UART is a port over which commands can be executed easily, even for debug purposes. A set of shell commands have been developed for C/FS that mirror the syntax of UNIX utilities, as described in this chapter. Appendix G, Bibliographhy. Appendix H, Licensing Policy. 21 Chapter 2 C/FS Architecture C/FS was written from the ground up to be modular and easy to adapt to different CPUs (Central Processing Units), RTOSs (Real-Time Operating Systems), file system media and compilers. Figure 2-1 shows a simplified block diagram of the different C/FS modules and their relationships. Notice that all of the C/FS files start with `fs_'. This convention allows you to quickly identify which files belong to C/FS. Also note that all functions and global variables start with `FS', and all macros and #defines start with `FS_'. 22 C/FS Architecture .c where is the an identifier for the device driver. For example, the driver for SD/MMC cards using SPI mode is called fs_dev_sd_spi.c. Most device drivers require a BSP layer, with code for accessing registers, reading from or writing to a data bus, etc. This file is named according to the pattern fs_dev__bsp.c For example, fs_dev_sd_spi_bsp.c contains the BSP functions for the driver SD/MMC cards using SPI mode. 2-1-7 CPU LAYER C/FS can work with either an 8, 16, 32 or even 64-bit CPU, but needs to have information about the CPU you are using. The CPU layer defines such things as the C data type corresponding to 16-bit and 32-bit variables, whether the CPU is little- or big-endian and, how interrupts are disabled and enabled on the CPU, etc. 26 C/FS Architecture CPU specific files are found in the ...\uC-CPU directory and, in order to adapt C/FS to a different CPU, you would need to either modify the cpu*.* files or, create new ones based on the ones supplied in the uC-CPU directory. In general, it's much easier to modify existing files because you have a better chance of not forgetting anything. 2-1-8 RTOS LAYER C/FS does not require an RTOS. However, if C/FS is used with an RTOS, a set of functions must be implemented to prevent simultaneous access of devices and core C/FS structures by multiple tasks. C/FS is provided with a no-RTOS (which contains just empty functions), a C/OS-II and a C/OS-III interface. If you use a different RTOS, you can use the fs_os.* for C/OS-II as a template to interface to the RTOS of your choice. 27 Chapter 3 Directories and Files C/FS is fairly easy to use once you understand which source files are needed to make up a C/FS-based application. This chapter will discuss the modules available for C/FS and how everything fits together. Figure 1-01 shows the C/FS architecture and its relationship with the hardware. Memory devices may include actual media both removable (SD/MMC, CF cards) and fixed (NAND flash, NOR flash) as well as any controllers for such devices. Of course, your hardware would most likely contain other devices such as UARTs (Universal Asynchronous Receiver Transmitters), ADCs (Analog to Digital Converters) and Ethernet controller(s). Moreover, your application may include other middleware components like an OS kernel, networking (TCP/IP) stack or USB stack that may integrate with C/FS. A WindowsTM-based development platform is assumed. The directories and files make references to typical Windows-type directory structures. However, since C/FS is available in source form then it can certainly be used on Unix, Linux or other development platforms. This, of course, assumes that you are a valid C/FS licensee in order to obtain the source code. The names of the files are shown in upper case to make them `stand out'. The file names, however, are actually lower case. 28 Directories and Files )6B&)*+ $33&+ )6B$33&+ 3ODWIRUP,QGHSHQGHQW )6&+ )6B$3,&+ )6B%8)&+ )6B&$&+(&+ )6B&)*B)6+ )6B&75+ )6B'(9&+ )6B',5&+ )6B(175<&+ /LEUDULHV )6B(55&+ )6B),/(&+ )6B3$57,7,21&+ )6B322/&+ )6B6<6&+ )6B7<3(+ )6B81,&2'(&+ )6B87,/&+ )6B92/&+ /,%B$6&,,&+ /,%B'()+ /,%B0$7+&+ /,%B0(0&+ /,%B675&+ &/.&+ &/.B26&+ )LOHV\VWHP 'ULYHU )6B)$7&+ )6B)$7B',5&+ )6B)$7B(175<&+ )6B)$7B)$7&+ )6B)$7B)$7&+ )6B)$7B)$7&+ )6B)$7B),/(&+ )6B)$7B-2851$/&+ )6B)$7B/)1&+ )6B)$7B6)1&+ )6B)$7B7<3(+ (&&+ ('&B&5&&+ (&&B+$00,1*&+ 'HYLFH'ULYHUV 266SHFLILF )6B'(9B &+ )6B26&+ 3ODWIRUP6SHFLILF &38 6SHFLILF %RDUG6XSSRUW3DFNDJH )6B'(9B B%63& &38+ &38B$$60 &38B&25(&+ %63&+ & + Figure 3-1 C/FS Architecture 29 Directories and Files F3-1(1) The application code consist of project or product files. For convenience, we simply called these app.c and app.h but your application can contain any number of files and they do not have to be called app.*. The application code is typically where you would find main(). F3-1(2) Quite often, semiconductor manufacturers provide library functions in source form for accessing the peripherals on their CPU (Central Processing Unit) or MCU (Micro Controller Unit). These libraries are quite useful and often save valuable time. Since there is no naming convention for these files, *.c and *.h are assumed. F3-1(3) The Board Support Package (BSP) is code that you would typically write to interface to peripherals on your target board. For example you can have code to turn on and off LEDs (light emitting diodes), functions to turn on and off relays, and code to read switches and temperature sensors. F3-1(4) At Micrim, we like to encapsulate CPU functionality. These files define functions to disable and enable interrupts, data types (e.g., CPU_INT08U, CPU_FP32) independent of the CPU and compiler and many more functions. F3-1(5) C/LIB consists of a group of source files to provide common functions for memory copy, string manipulation and character mapping. Some of the functions replace stdlib functions provided by the compiler. These are provided to ensure that they are fully portable from application to application and (most importantly) from compiler to compiler. F3-1(6) C/Clk is an independant clock/calendar management module, with source code for easily managing date and time in a product. C/FS uses the date and time information from C/Clk to update files and directories with the proper creation/modification/access time. F3-1(7) C/CRC is a stand-alone module for calculating checksums and error correction codes. This module is used by some of C/FS device drivers. F3-1(8) This is the C/FS platform-independent code, free of dependencies on CPU and memory device. This code is written in highly-portable ANSI C code. This code is only available to C/FS licensees. 30 Directories and Files F3-1(9) This is the C/FS system driver for FAT file systems. This code is only available to C/FS licensees. F3-1(10) This is the collection of device drivers for C/FS. Each driver supports a certain device type, such as SD/MMC cards, NAND flash or NOR flash. Drivers are only available to C/FS licensees. F3-1(11) This is the C/FS code that is adapted to a specific platform. It consists of small code modules written for specific drivers called ports that must be adapted to the memory device controllers or peripherals integrated into or attached to the CPU. The requirements for these ports are described in Appendix C, Porting Manual. F3-1(12) C/FS does not require an RTOS. However, if C/FS is used with an RTOS, a set of functions must be implemented to prevent simultaneous access of devices and core C/FS structures by multiple tasks. F3-1(13) This C/FS configuration file defines which C/FS features (fs_cfg.h) are included in the application. 3-1 APPLICATION CODE When Micrim provides you with example projects, we typically place those in a directory structure as shown below. Of course, you can use whatever directory structure suits your project/product. \Micrium \Software \EvalBoards \ \ \ \ \*.* 31 Directories and Files \Micrium This is where we place all software components and projects provided by Micrim. This directory generally starts from the root directory of your computer. \Software This sub-directory contains all the software components and projects. \EvalBoards This sub-directory contains all the projects related to the evaluation boards supported by Micrim. \ Is the name of the manufacturer of the evaluation board. The `<' and `>' are not part of the actual name. \ This is the name of the evaluation board. A board from Micrim will typically be called uC-Eval-xxxx where `xxxx' will represent the CPU or MCU used on the evaluation board. The `<' and `>' are not part of the actual name. \ This is the name of the compiler or compiler manufacturer used to build the code for the evaluation board. The `<' and `>' are not part of the actual name. \ This is the name of the project that will be demonstrated. For example a simple C/FS project might have a project name of `FS-Ex1'. The `-Ex1' represents a project containing only C/FS. A project name of FS-Probe-Ex1 would represent a project containing C/FS as well as C/Probe. The `<' and `>' are not part of the actual name. \*.* These are the source files for the project/product. You are certainly welcomed to call the main files APP*.* for your own projects but you don't have to. This directory also contains the configuration file FS_CFG.H and other files as needed by the project. 32 Directories and Files 3-2 CPU As shown below is the directory where we place semiconductor manufacturer peripheral interface source files. Of course, you can use whatever directory structure suits your project/product. \Micrium \Software \CPU \ \ \*.* \Micrium This is where we place all software components and projects provided by Micrim. \Software This sub-directory contains all the software components and projects. \CPU This sub-directory is always called CPU. \ Is the name of the semiconductor manufacturer who provided the peripheral library. The `<' and `>' are not part of the actual name. \ This is the name of the specific library and is generally associated with a CPU name or an architecture. \*.* These are the library source files. The names of the files are determined by the semiconductor manufacturer. 33 Directories and Files 3-3 BOARD SUPPORT PACKAGE (BSP) The BSP is generally found with the evaluation or target board because the BSP is specific to that board. In fact, if well written, the BSP should be used for multiple projects. \Micrium \Software \EvalBoards \ \ \ \BSP \*.* \Micrium This is where we place all software components and projects provided by Micrim. \Software This sub-directory contains all the software components and projects. \EvalBoards This sub-directory contains all the projects related to evaluation boards. \ Is the name of the manufacturer of the evaluation board. The `<' and `>' are not part of the actual name. \ This is the name of the evaluation board. A board from Micrim will typically be called uC Eval xxxx where `xxxx' will be the name of the CPU or MCU used on the evaluation board. The `<' and `>' are not part of the actual name. \ This is the name of the compiler or compiler manufacturer used to build the code for the evaluation board. The `<' and `>' are not part of the actual name. \BSP This directory is always called BSP. 34 Directories and Files \*.* These are the source files of the BSP. Typically all the file names start with BSP_ but they don't have to. It's thus typical to find bsp.c and bsp.h in this directory. Again, the BSP code should contain functions such as LED control functions, initialization of timers, interface to Ethernet controllers and more. 3-4 C/CPU, CPU SPECIFIC SOURCE CODE C/CPU consists of files that encapsulate common CPU-specific functionality as well as CPU- and compiler-specific data types. \Micrium \Software \uC-CPU \cpu_core.c \cpu_core.h \cpu_def.h \Cfg\Template \cpu_cfg.h \ \ \cpu.h \cpu_a.asm \cpu_c.c \Micrium This directory contains all software components and projects provided by Micrim. \Software This sub-directory contains all the software components and projects. \uC-CPU This is the main C/CPU directory. 35 Directories and Files cpu_core.c contains C code that is common to all CPU architectures. Specifically, this file contains functions to measure the interrupt disable time of the CPU_CRITICAL_ENTER() and CPU_CRITICAL_EXIT() macros, a function that emulates a count leading zeros instruction and a few other functions. cpu_core.h contains the function prototypes of the functions provided in cpu_core.c as well as allocation of the variables used by this module to measure interrupt disable time. cpu_def.h contains miscellaneous #define constants used by the C/CPU module. \Cfg\Template This directory contains a configuration template file (cpu_cfg.h) that you will need to copy to your application directory in order to configure the C/CPU module based on your application requirements. cpu_cfg.h determines whether you will enable measurement of the interrupt disable time, whether your CPU implements a count leading zeros instruction in assembly language or whether it will need to be emulated in C and more. \ This is the name of the CPU architecture for which C/CPU was ported to. The `<' and `>' are not part of the actual name. \ This is the name of the compiler or compiler manufacturer used to build the code for the C/CPU port. The `<' and `>' are not part of the actual name. The files in this directory contain the C/CPU port. cpu.h contains type definitions to make C/FS and other modules independent of the CPU and compiler word sizes. Specifically, you will find the declaration of the CPU_INT16U, CPU_INT32U, CPU_FP32 and many other data types. Also, this file specifies whether the CPU is a big- or little-endian machine and contains function prototypes for functions that are specific to the CPU architecture and more. 36 Directories and Files cpu_a.asm contains the assembly language functions to implement the code to disable and enable CPU interrupts, count leading zeros (if the CPU supports that instruction) and other CPU specific functions that can only be written in assembly language. This file could also contain code to enable caches, setup MPUs and MMU and more. The functions provided in this file are accessible from C. cpu_c.c contains C code of functions that are specific to the specific CPU architecture but written in C for portability. As a general rule, if a function can be written in C then it should, unless there are significant performance benefits by writing it in assembly language. 3-5 C/LIB, PORTABLE LIBRARY FUNCTIONS C/LIB consists of library functions that are meant to be highly portable and not tied to any specific compiler. This was done to facilitate third party certification of Micrim products. \Micrium \Software \uC-LIB \lib_ascii.c \lib_ascii.h \lib_def.h \lib_math.c \lib_math.h \lib_mem.c \lib_mem.h \lib_str.c \lib_str.h \Cfg\Template \lib_cfg.h \Ports \ \ \lib_mem_a.asm \Micrium This directory contains all software components and projects provided by Micrim. 37 Directories and Files \Software This sub-directory contains all the software components and projects. \uC-LIB This is the main C/LIB directory. \Cfg\Template This directory contains a configuration template file (lib_cfg.h) that must be copied to the application directory to configure the C/LIB module based on application requirements. lib_cfg.h determines whether to enable assembly-language optimization (assuming there is an assembly-language file for the processor, i.e. lib_mem_a.asm) and a few other #defines. 3-6 C/CLK, TIME/CALENDAR MANAGEMENT C/Clk consists of functions that are meant to centralize time management in one independant module. This way, the same time info can be easily shared across all Micrium products. \Micrium \Software \uC-Clk \Cfg \Template \clk_cfg.h \OS \ \clk_os.c \Source \clk.c \clk.h \Micrium This directory contains all software components and projects provided by Micrim. 38 Directories and Files \Software This sub-directory contains all the software components and projects. \uC-Clk This is the main C/Clk directory. \Cfg\Template This directory contains a configuration template file (clk_cfg.h) that must be copied to the application directory to configure the C/Clk module based on application requirements. clk_cfg.h determines whether clock will be managed by the RTOS or in your application. A few other #defines are used to enable/disable some features of C/Clk and to configure some parameteres, like the clock frequency. \OS This is the main OS directory. \ This is the directory that contains the file to perform RTOS abstraction. Note that the file for the selected RTOS abstraction layer must always be named clk_os.c. C/Clk has been tested with C/OS-II, C/OS-III and the RTOS layer files for these RTOS are found in the following directories: \Micrium\Software\uC-Clk\OS\uCOS-II\clk_os.c \Micrium\Software\uC-Clk\OS\uCOS-III\clk_os.c \Source This directory contains the CPU-independant source code for C/Clk. All file in this directory should be included in the build (assuming the presence of the source code). Features that are not required will be compiled out based on the value of #define constants in clk_cfg.h. 39 Directories and Files 3-7 C/CRC, CHECKSUMS AND ERROR CORRECTION CODES C/CRC consists of functions to compute different error detection and correction codes. The functions are speed-optimized to avoid the important impact on performances that these CPU-intensive calcutions may present. \Micrium \Software \uC-CRC \Cfg \Template \crc_cfg.h \Ports \ \ \ecc_bch_4bit_a.asm \ecc_bch_8bit_a.asm \ecc_hamming_a.asm \edc_crc_a.asm \Source \edc_crc.h \edc_crc.c \ecc_hamming.h \ecc_hamming.c \ecc_bch_8bit.h \ecc_bch_8bit.c \ecc_bch_4bit.h \ecc_bch_4bit.c \ecc_bch.h \ecc_bch.c \ecc.h \Micrium This directory contains all software components and projects provided by Micrim. \Software This sub-directory contains all the software components and projects. 40 Directories and Files \uC-CRC This is the main C/CRC directory. \Cfg\Template This directory contains a configuration template file (crc_cfg.h) that must be copied to the application directory to configure the C/CRC module based on application requirements. crc_cfg.h determines whether to enable assembly-language optimization (assuming there is an assembly-language file for the processor) and a few other #defines. \ The name of the CPU architecture that C/CRC was ported to. The `<' and `>' are not part of the actual name. \ The name of the compiler or compiler manufacturer used to build code for the C/CRC port. The `<' and `>' are not part of the actual name. ecc_bch_4bit_a.asm contains the assembly language functions to optimize the calculation speed of 4-bit correction BCH (Bos, Ray-Chaudhuri, Hocquenghem) code. ecc_bch_8bit_a.asm contains the assembly language functions to optimize the calculation speed of 8-bit correction BCH (Bos, Ray-Chaudhuri, Hocquenghem) code. ecc_hamming_a.asm contains the assembly language functions to optimize the calculation speed of Hamming code. edc_crc_a.asm contains the assembly language functions to optimize the calculation speed of CRC (cyclic redundancy checks). \Source This is the directory that contains all the CPU independent source code files. of C/CRC. 41 Directories and Files 3-8 C/FS PLATFORM-INDEPENDENT SOURCE CODE The files in these directories are available to C/FS licensees (see Appendix H, Licensing Policy). \Micrium \Software \uC-FS \APP\Template \fs_app.c \fs_app.h \Cfg\Template \fs_cfg.h \OS\Template \fs_os.c \fs_os.h \Source \fs_c \fs.h \fs_api.c \fs_api.h \fs_buf.c \fs_buf.h \fs_cache.c \fs_cache.h \fs_cfg_fs.h \fs_ctr.h \fs_def.h \fs_dev.c \fs_dev.h \fs_dir.c \fs_dir.h \fs_entry.c \fs_entry.h \fs_err.c \fs_err.h \fs_file.c \fs_file.h 42 Directories and Files \fs_partition.c \fs_partition.h \fs_pool.c \fs_pool.h \fs_sys.c \fs_sys.h \fs_type.h \fs_unicode.c \fs_unicode.h \fs_util.c \fs_util.h \fs_vol.c \fs_vol.h \Micrium This is where we place all software components and projects provided by Micrim. \Software This sub-directory contains all the software components and projects. \uC-FS This is the main C/FS directory. \APP\Template This directory contains a template of the code for initializing the file system. \Cfg\Template This directory contains a configuration template file (lib_cfg.h) that is required to be copied to the application directory to configure the C/FS module based on application requirements. fs_cfg.h specifies which features of C/FS you want in your application. If C/FS is provided in linkable object code format then this file will be provided to show you what features are available in the object file. See Appendix B, C/FS Configuration Manual. 43 Directories and Files \Source This directory contains the platform-independent source code for C/FS. All the files in this directory should be included in your build (assuming you have the source code). Features that you don't want will be compiled out based on the value of #define constants in fs_cfg.h. fs.c/h contain core functionality for C/FS including FS_Init() (called to initialize C/FS) and FS_WorkingDirSet()/FS_WorkingDirGet() (used to get and set the working directory). fs.h is the ONLY core header file that should be #included by the application. fs_api.c/h contains the code for the POSIX-compatible API. See Chapter x, API for details about the POSIX-compatible API. fs_buf.c/h contains the code for the buffer management (used internally by C/FS). fs_dev.c/h contains code for device management. See Chapter x, Devices for details about devices. fs_dir.c/h contains code for directory access. See Chapter x, Directories for details about directory access. fs_entry.c/h contains code for entry access. See Chapter x, Entries for details about entry access. fs_file.c/h contains code for file access. See Chapter x, Files for details about file access. fs_pool.c/h contains the code for pool management (used internally by C/FS). fs_sys.c/h contains the code for system driver management (used internally by C/FS). fs_unicode.c/h contains the code for handling Unicode strings (used internally by C/FS). 44 Directories and Files 3-9 C/FS FAT FILESYSTEM SOURCE CODE The files in these directories are available to C/FS licensees (see Appendix H, Licensing Policy). \Micrium \Software \uC-FS \FAT \fs_fat.c \fs_fat.h \fs_fat_dir.c \fs_fat_dir.h \fs_fat_entry.c \fs_fat_entry.h \fs_fat_fat12.c \fs_fat_fat12.h \fs_fat_fat16.c \fs_fat_fat16.h \fs_fat_fat32.c \fs_fat_fat32.h \fs_fat_file.c \fs_fat_file.h \fs_fat_journal.c \fs_fat_journal.h \fs_fat_lfn.c \fs_fat_lfn.h \fs_fat_sfn.c \fs_fat_sfn.h \fs_fat_type.h \Micrium This is where we place all software components and projects provided by Micrim. \Software This sub-directory contains all the software components and projects. 45 Directories and Files \uC-FS This is the main C/FS directory. \FAT This directory contains the FAT system driver for C/FS. All the files in this directory should be included in your build (assuming you have the source code). 3-10 C/FS MEMORY DEVICE DRIVERS These files are generic drivers to use with differenty memory devices. \Micrium \Software \uC-FS \Dev \IDE \fs_dev_ide.c \fs_dev_ide.h \BSP\Template \fs_dev_ide_bsp.c \MSC \fs_dev_msc.c \fs_dev_msc.h \NAND \fs_dev_nand.c \fs_dev_nand.h \PHY \fs_dev_nand_0512_x08.c \fs_dev_nand_0512_x08.h \fs_dev_nand_0512_x08.c \fs_dev_nand_0512_x08.h \fs_dev_nand_0512_x08.c \fs_dev_nand_0512_x08.h \fs_dev_nand_0512_x08.c \fs_dev_nand_0512_x08.h \Template \fs_dev_nand_template.c 46 Directories and Files \fs_dev_nand_template.h \BSP\Template \fs_dev_nand_bsp.c \BSP\Template (GPIO) \fs_dev_nand_bsp.c \BSP\Template (SPI GPIO) \fs_dev_nand_bsp.c \BSP\Template (SPI) \fs_dev_nand_bsp.c \NOR \fs_dev_nor.c \fs_dev_nor.h \PHY \fs_dev_nor_amd_1x08.c \fs_dev_nor_amd_1x08.h \fs_dev_nor_amd_1x16.c \fs_dev_nor_amd_1x16.h \fs_dev_nor_intel.c \fs_dev_nor_intel.h \fs_dev_nor_sst25.c \fs_dev_nor_sst25.h \fs_dev_nor_sst39.c \fs_dev_nor_sst39.h \fs_dev_nor_stm25.c \fs_dev_nor_stm25.h \fs_dev_nor_stm29_1x08.c \fs_dev_nor_stm29_1x08.h \fs_dev_nor_stm29_1x16.c \fs_dev_nor_stm29_1x16.h \Template \fs_dev_nor_template.c \fs_dev_nor_template.h \BSP\Template \fs_dev_nor_bsp.c \BSP\Template (SPI GPIO) \fs_dev_nor_bsp.c \BSP\Template (SPI) \fs_dev_nor_bsp.c 47 Directories and Files \RAMDisk \fs_dev_ram.c \fs_dev_ram.h \SD \fs_dev_sd.c \fs_dev_sd.h \Card \fs_dev_sd_card.c \fs_dev_sd_card.h \BSP\Template \fs_dev_sd_card_bsp.c \SPI \fs_dev_sd_spi.c \fs_dev_sd_spi.h \BSP\Template \fs_dev_sd_spi.bsp.c \Template \fs_dev_template.c \fs_dev_template.h \Micrium This directory contains all software components and projects provided by Micrim. \Software This sub-directory contains all the software components and projects. \uC-FS This is the main C/FS directory. \Dev This is where you will find the device driver files for the storage devices you are planning on using. \IDE This directory contains the IDE/CF driver files. 48 Directories and Files fs_dev_ide.* are device driver for IDE devices. These files require a set of BSP functions to be defined in a file named fs_dev_ide_bsp.c to work with a particular hardware setup. For more details on this driver, please refer to Chapter 11, "IDE/CF Driver" on page 124. \MSC This directory contains the MSC (Mass Storage Class - USB drives) driver files. fs_dev_msc.* are device driver for MSC devices. This driver is designed to work with C/USB host stack. For more details on this driver, please refer to Chapter 13, "MSC Driver" on page 134. \NAND This directory contains the NAND driver files. fs_dev_nand.* are the device driver for NAND devices. These files require a set of physical-layer functions (defined in a file name fs_dev_nand_.*) as well as BSP functions (to be defined in a file named fs_dev_nand_bsp.c) to work with a particular hardware setup. For more details on this driver, please refer to Chapter 14, "NAND Flash Driver" on page 137. \NOR This directory contains the NOR driver files. fs_dev_nor.* are the device driver for NOR devices. These files require a set of physical-layer functions (defined in a file name fs_dev_nor_.*) as well as BSP functions (to be defined in a file named fs_dev_nor_bsp.c) to work with a particular hardware setup. For more details on this driver, please refer to Chapter 15, "NOR Flash Driver" on page 151. \RAMDisk This directory contains the RAM disk driver files. 49 Directories and Files fs_dev_ramdisk.* constitue the RAM disk driver. For more details on this driver, please refer to Chapter 16, "RAM Disk Driver" on page 170. \SD This directory contains the SD/MMC driver files. fs_dev_sd.* are device driver for SD devices. Theses files require to be used with either the fs_dev_sd_spi.* (for SPI/one-wire mode) or fs_dev_sd_card.* (for Card/4-wires mode) files. These files require a set of BSP functions to be defined in a file named either fs_dev_sd_spi_bsp.c or fs_dev_sd_card_bsp.c to work with a particular hardware setup. For more details on this driver, please refer to Chapter 17, "SD/MMC Drivers" on page 174. 3-11 C/FS PLATFORM-SPECIFIC SOURCE CODE These files are provided by the C/FS device driver developer. See Chapter 17, Porting C/FS. However, the C/FS source code is delivered with port examples. \Micrium \Software \uC-FS \Examples \BSP \Dev \fs_dev__bsp.c \Micrium This directory contains all software components and projects provided by Micrim. \Software This sub-directory contains all the software components and projects. 50 Directories and Files \uC-FS This is the main C/FS directory. \Examples This is where you will find the device driver BSP example files. \Dev\ This is where you will find the examples BSP for one memory type. The `<' and `>' are not part of the actual name. The memory types supported by C/FS are the following: IDE, NAND, NOR, SD\CARD, SD\SPI. \ The name of the manufacturer of the evaluation board. The `<' and `>' are not part of the actual name. 3-12 C/FS OS ABSTRACTION LAYER This directory contains the RTOS abstraction layer which allows the use of C/FS with nearly any commercial of in-house RTOS, or without any RTOS at all. The abstraction layer for the selected RTOS is placed in a sub-directory under OS as follows: \Micrium \Software \uC-FS \OS \ \fs_os.c \fs_os.h \Micrium This directory contains all software components and projects provided by Micrim. \Software This sub-directory contains all the software components and projects. \uC-FS This is the main C/FS directory. 51 Directories and Files \OS This is the main OS directory. \ This is the directory that contains the files to perform RTOS abstraction. Note that files for the selected RTOS abstraction layer must always be named fs_os.*. C/FS has been tested with C/OS-II, C/OS-III and without an RTOS. The RTOS layer files are found in the following directories: \Micrium\Software\uC-Clk\OS\None\fs_os.* \Micrium\Software\uC-Clk\OS\Template\fs_os.* \Micrium\Software\uC-Clk\OS\uCOS-II\fs_os.* \Micrium\Software\uC-Clk\OS\uCOS-III\fs_os.* 3-13 SUMMARY Below is a summary of all the directories and files involved in a C/FS-based project. The `<-Cfg' on the far right indicates that these files are typically copied into the application (i.e., project) directory and edited based on project requirements. \Micrium \Software \EvalBoards \ \ \ \ \app.c \app.h \other \BSP \bsp.c \bsp.h 52 Directories and Files \other \CPU \ \ \*.* \uC-FS \APP\Template \fs_app.c \fs_app.h \CFG\Template \fs_cfg.h \Dev \IDE \fs_dev_ide.c \fs_dev_ide.h \BSP\Template \fs_dev_ide_bsp.c \MSC \fs_dev_msc.c \fs_dev_msc.h \NAND \fs_dev_nand.c \fs_dev_nand.h \PHY \fs_dev_nand_0512_x08.c \fs_dev_nand_0512_x08.h \fs_dev_nand_0512_x08.c \fs_dev_nand_0512_x08.h \fs_dev_nand_0512_x08.c \fs_dev_nand_0512_x08.h \fs_dev_nand_0512_x08.c \fs_dev_nand_0512_x08.h \Template \fs_dev_nand_template.c \fs_dev_nand_template.h \BSP\