http://www.ethernut.de/nutwiki/index.php?title=Writing_PHAT_Files&feed=atom&action=historyWriting PHAT Files - Revision history2024-03-29T01:51:14ZRevision history for this page on the wikiMediaWiki 1.26.2http://www.ethernut.de/nutwiki/index.php?title=Writing_PHAT_Files&diff=298&oldid=prevHarald: 1 revision imported2016-10-27T16:03:02Z<p>1 revision imported</p>
<table class='diff diff-contentalign-left'>
<tr style='vertical-align: top;' lang='en'>
<td colspan='1' style="background-color: white; color:black; text-align: center;">← Older revision</td>
<td colspan='1' style="background-color: white; color:black; text-align: center;">Revision as of 16:03, 27 October 2016</td>
</tr><tr><td colspan='2' style='text-align: center;' lang='en'><div class="mw-diff-empty">(No difference)</div>
</td></tr></table>Haraldhttp://www.ethernut.de/nutwiki/index.php?title=Writing_PHAT_Files&diff=297&oldid=prevDaniel: /* Test Environments */2009-03-10T17:42:08Z<p><span dir="auto"><span class="autocomment">Test Environments</span></span></p>
<p><b>New page</b></p><div>== Test Environments ==<br />
<br />
{| class="smalldiv"<br />
| style="background-color:#F6F9ED" |<br />
| style="background-color:#F6F9ED" | Hardware<br>Comments<br />
| style="background-color:#F6F9ED" | '''Nut/OS<br>4.6.4'''<br />
| style="background-color:#F6F9ED" | '''Nut/OS<br>4.7.4'''<br />
| style="background-color:#F6F9ED" | '''Nut/OS<br>4.8.0'''<br />
|-<br />
| <span style="color:green">Ethernut 3.0 E</span><br />
|<br />
| Compiler<br>Error<br />
| OK <br> [http://ethernut.de/arc/WIKI_BINARIES_ZIPPED/write-phat-enut30E-nutos-4.7.4.zip '''Binaries''']<br />
| NO<br />
|-<br />
| <span style="color:green">EIR 1.0 C</span><br />
|<br />
| Compiler<br>Error<br />
| OK <br> [http://ethernut.de/arc/WIKI_BINARIES_ZIPPED/write-phat-EIR1C-nutos-4.7.4.zip '''Binaries''']<br />
| OK <br> [http://ethernut.de/arc/WIKI_BINARIES_ZIPPED/write-phat-EIR1C-nutos-4.8.0.zip '''Binaries''']<br />
|-<br />
| <span style="color:gray">AT91SAM7X-EK</span><br />
|<br />
| NT<br />
| NT<br />
|-<br />
| <span style="color:gray">AT91SAM9260-EK</span><br />
|<br />
| NT<br />
| NT<br />
|-<br />
| id="compilertd" colspan="5" |'''Compiler:''' ARM-GCC 4.2.2 ; AVR-GCC 4.3.0<br />
|}<br />
<br />
== Description ==<br />
<br />
This example demonstrates how to write a file with the PHAT file system to a MMC (SD) card.<br />
<br />
== Prerequisites ==<br />
<br />
[[Image:Eir10c-mmc-config.png|thumb|right|EIR 1.0 MMC configuration]]<br />
<br />
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.<br />
<br />
== Source Code ==<br />
<source lang="c"><br />
#include <dev/board.h><br />
#include <fs/phatfs.h><br />
#include <stdio.h><br />
#include <io.h><br />
#include <fcntl.h><br />
<br />
int main (void)<br />
{<br />
int hvol;<br />
FILE *fp;<br />
<br />
NutRegisterDevice(&devPhat0, 0, 0);<br />
NutRegisterDevice(&DEV_MMCARD, 0, 0);<br />
<br />
hvol = _open(DEV_MMCARD_NAME ":1/PHAT0", _O_RDWR | _O_BINARY);<br />
<br />
fp = fopen("PHAT0:/TEST.TXT", "w");<br />
fprintf(fp, "HelloFile");<br />
fclose(fp);<br />
<br />
_close(hvol);<br />
<br />
for(;;);<br />
}<br />
</source><br />
<br />
== Details ==<br />
<br />
<source lang="c"><br />
int hvol;<br />
FILE *fp;<br />
</source><br />
<br />
Variable <code>hvol</code> is used for the volume handle and variable <code>fp</code> will contain the file pointer.<br />
<br />
We must register all devices that we will use. Here it's <code>DEV_MMCARD</code> for the MultiMedia Card device and <code>devPhat0</code> for the file system driver.<br />
<br />
<source lang="c"><br />
NutRegisterDevice(&DEV_MMCARD, 0, 0);<br />
NutRegisterDevice(&devPhat0, 0, 0);<br />
</source><br />
<br />
To mount the volume, we open the MMC device.<br />
<br />
<source lang="c"><br />
hvol = _open(DEV_MMCARD_NAME ":1/PHAT0", _O_RDWR | _O_BINARY);<br />
</source><br />
<br />
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.<br />
<br />
The name of the MMC driver may differ among target boards. <code>DEV_MMCARD_NAME</code> is defined in <code>board.h</code> and represents the standard name of the specific target board.<br />
<br />
Now we can open a file for writing, write some text into the file and close the file, using the standard C stream functions <code>fopen</code>, <code>fprintf</code> and <code>fclose</code>.<br />
<br />
<source lang="c"><br />
fp = fopen("PHAT0:/TEST.TXT", "w");<br />
fprintf(fp, "HelloFile");<br />
fclose(fp);<br />
</source><br />
<br />
Finally, the partition gets unmounted by<br />
<br />
<source lang="c"><br />
_close(hvol);<br />
</source><br />
<br />
== See also ==<br />
* [[Reading PHAT Files]]<br />
* [[Reading PHAT Directories]]<br />
* More [[Nut/OS Examples]]<br />
<br />
== External Links ==<br />
<br />
[http://en.wikipedia.org/wiki/Fopen C file input/output] The C programming language provides many standard library functions for file input and output.<br />
<br />
[http://en.wikipedia.org/wiki/File_Allocation_Table File Allocation Table] A computer file system architecture originally developed by Bill Gates and Marc McDonald.<br />
<br />
<br />
[[Category:Code Snippets]]<br />
<br />
{{Languages}}</div>Daniel