Writing PHAT Files

From Nutwiki
Jump to: navigation, search

Test Environments

Hardware
Comments
Nut/OS
4.6.4
Nut/OS
4.7.4
Nut/OS
4.8.0
Ethernut 3.0 E Compiler
Error
OK
Binaries
NO
EIR 1.0 C Compiler
Error
OK
Binaries
OK
Binaries
AT91SAM7X-EK NT NT
AT91SAM9260-EK NT NT
Compiler: ARM-GCC 4.2.2 ; AVR-GCC 4.3.0

Description

This example demonstrates how to write a file with the PHAT file system to a MMC (SD) card.

Prerequisites

File:Eir10c-mmc-config.png
EIR 1.0 MMC configuration

When using the EIR 1.0 board, make sure, that the option of the MMC Software SPI 0 Driver are configured as shown in the screen shot.

Source Code

<source lang="c">

  1. include <dev/board.h>
  2. include <fs/phatfs.h>
  3. include <stdio.h>
  4. include <io.h>
  5. include <fcntl.h>

int main (void) {

   int hvol;
   FILE *fp;

   NutRegisterDevice(&devPhat0, 0, 0);
   NutRegisterDevice(&DEV_MMCARD, 0, 0);

   hvol = _open(DEV_MMCARD_NAME ":1/PHAT0", _O_RDWR | _O_BINARY);
   
   fp = fopen("PHAT0:/TEST.TXT", "w");
   fprintf(fp, "HelloFile");
   fclose(fp);
   _close(hvol);

   for(;;);

} </source>

Details

<source lang="c"> int hvol; FILE *fp; </source>

Variable hvol is used for the volume handle and variable fp will contain the file pointer.

We must register all devices that we will use. Here it's DEV_MMCARD for the MultiMedia Card device and devPhat0 for the file system driver.

<source lang="c"> NutRegisterDevice(&DEV_MMCARD, 0, 0); NutRegisterDevice(&devPhat0, 0, 0); </source>

To mount the volume, we open the MMC device.

<source lang="c"> hvol = _open(DEV_MMCARD_NAME ":1/PHAT0", _O_RDWR | _O_BINARY); </source>

Note, that the name contains the device name, followed by a colon, followed by the number of the partition, followed by a slash, followed by the name of the file system driver.

The name of the MMC driver may differ among target boards. DEV_MMCARD_NAME is defined in board.h and represents the standard name of the specific target board.

Now we can open a file for writing, write some text into the file and close the file, using the standard C stream functions fopen, fprintf and fclose.

<source lang="c"> fp = fopen("PHAT0:/TEST.TXT", "w"); fprintf(fp, "HelloFile"); fclose(fp); </source>

Finally, the partition gets unmounted by

<source lang="c"> _close(hvol); </source>

See also

External Links

C file input/output The C programming language provides many standard library functions for file input and output.

File Allocation Table A computer file system architecture originally developed by Bill Gates and Marc McDonald.Template:Languages