Writing PHAT Files

From NutWiki

Jump to: navigation, search

Contents

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

EIR 1.0 MMC configuration
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

#include <dev/board.h>
#include <fs/phatfs.h>
#include <stdio.h>
#include <io.h>
#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(;;);
}

Details

int hvol;
FILE *fp;

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.

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

To mount the volume, we open the MMC device.

hvol = _open(DEV_MMCARD_NAME ":1/PHAT0", _O_RDWR | _O_BINARY);

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.

fp = fopen("PHAT0:/TEST.TXT", "w");
fprintf(fp, "HelloFile");
fclose(fp);

Finally, the partition gets unmounted by

_close(hvol);

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.

Personal tools