ATMEL: file.c File Reference

- Main Page
- Modules
- Data Structures
- Files
- Related Pages

# file.c File Reference

FAT 12/16/32 Services. More...

`#include "conf_explorer.h"`
`#include "file.h"`
`#include "navigation.h"`
`#include <LIB_MEM>`
`#include <LIB_CTRLACCESS>`

Include dependency graph for file.c:

Go to the source code of this file.

## Functions

static void | file_load_segment_value (Fs_file_segment _MEM_TYPE_SLOW_ *segment)
| This function stores the global segment variable in other variable.
Bool | file_ispresent (void)
| This function checks if a file is selected.
Bool | file_open (U8 fopen_mode)
| This function opens the selected file.
Bool | file_read (Fs_file_segment _MEM_TYPE_SLOW_ *segment)
| This function returns a segment (position & size) in a physical memory corresponding at the file.
U16 | file_read_buf (U8 _MEM_TYPE_SLOW_ *buffer, U16 u16_buf_size)
| This function copys in a buffer the file data corresponding at the current position.
U16 | file_getc (void)
| This function returns the next byte of file.
U32 | file_getpos (void)
| This function returns the position in the file.
Bool | file_seek (U32 u32_pos, U8 u8_whence)
| This function changes the position in the file.
U8 | file_bof (void)
| This function checks the beginning of file.
U8 | file_eof (void)
| This function checks the end of file.
void | file_flush (void)
| This function flushs the internal cache (file datas and file information).
void | file_close (void)
| This function closes the file.

## Variables

_MEM_TYPE_SLOW_ U8 | fs_g_sector [512]
| Use "FAT sector cache" to store a sector from a file (see file_putc(), file_getc(), file_read_buf(), file_write_buf()).

## Detailed Description

FAT 12/16/32 Services.

This file defines a useful set of functions for the file accesses on AVR32 devices.

Author:
  Atmel Corporation: http://www.atmel.com
  Support and FAQ: http://support.atmel.no/

Definition in file file.c.

## Function Documentation

static void file_load_segment_value | ( | Fs_file_segment _MEM_TYPE_SLOW_ * | segment | ) | `[static]`

This function stores the global segment variable in other variable.

Parameters:

  | segment | Pointer on the variable to fill

Definition at line 147 of file file.c.

References fs_g_nav, fs_g_seg, Fs_segment::u32_addr, Fs_segment::u32_size_or_pos, and Fs_management::u8_lun.

Referenced by file_read().

    00148 {
    00149    segment->u8_lun = fs_g_nav.u8_lun;
    00150    segment->u32_addr = fs_g_seg.u32_addr;
    00151    segment->u16_size = fs_g_seg.u32_size_or_pos;
    00152 }

Here is the caller graph for this function:

Bool file_ispresent | ( | void |  | ) |

This function checks if a file is selected.

Returns:
  TRUE, a file is selected

  FALSE, otherwise

Definition at line 72 of file file.c.

    00073 {
    00074    if( !fat_check_mount_select() )
    00075       return FALSE;
    00076    return fat_check_is_file();
    00077 }

Bool file_open | ( | U8 | fopen_mode | ) |

This function opens the selected file.

Parameters:

  fopen_mode

  option to open the file :
  FOPEN_MODE_R R access, flux pointer = 0, size not modify
  FOPEN_MODE_R_PLUS R/W access, flux pointer = 0, size not modify
  FOPEN_MODE_W W access, flux pointer = 0, size = 0
  FOPEN_MODE_W_PLUS R/W access, flux pointer = 0, size = 0
  FOPEN_MODE_APPEND W access, flux pointer = at the end, size not modify

Returns:
  FALSE in case of error, see global value "fs_g_status" for more detail

  TRUE otherwise

Definition at line 92 of file file.c.

Referenced by dfu_load_hex(), ushell_cmd_append_file(), ushell_cmd_cat(), and ushell_cmd_perform_alloc().

    00093 {
    00094    if( !fat_check_mount_select_noopen())
    00095       return FALSE;
    00096
    00097    if( !fat_check_is_file())
    00098       return FALSE;
    00099
    00100    if(FOPEN_WRITE_ACCESS & fopen_mode)
    00101    {
    00102       if( !fat_check_nav_access_file( TRUE ) )
    00103          return FALSE;
    00104 #if (FSFEATURE_WRITE == (FS_LEVEL_FEATURES & FSFEATURE_WRITE))
    00105       if (FS_ATTR_READ_ONLY & fs_g_nav_entry.u8_attr)
    00106       {
    00107          fs_g_status = FS_ERR_READ_ONLY;  // File is read only
    00108          return FALSE;
    00109       }
    00110       if( mem_wr_protect( fs_g_nav.u8_lun  ))
    00111       {
    00112          fs_g_status = FS_LUN_WP;  // Disk read only
    00113          return FALSE;
    00114       }
    00115 #else
    00116       fs_g_status = FS_ERR_MODE_NOAVIALABLE;
    00117       return FALSE;
    00118 #endif  // FS_LEVEL_FEATURES
    00119    }
    00120    else
    00121    {
    00122       if( !fat_check_nav_access_file( FALSE ) )
    00123          return FALSE;
    00124    }
    00125
    00126    if(FOPEN_CLEAR_SIZE & fopen_mode)
    00127    {
    00128       fs_g_nav_entry.u32_size    = 0;     // The size is null
    00129    }
    00130    if(FOPEN_CLEAR_PTR & fopen_mode)
    00131    {
    00132       fs_g_nav_entry.u32_pos_in_file = 0;
    00133    }
    00134    else
    00135    {  // Go to at the end of file
    00136       fs_g_nav_entry.u32_pos_in_file = fs_g_nav_entry.u32_size;
    00137    }
    00138    fs_g_nav_entry.u8_open_mode = fopen_mode;
    00139    return TRUE;
    00140 }

Here is the caller graph for this function:

Bool file_read | ( | Fs_file_segment _MEM_TYPE_SLOW_ * | segment | ) |

This function returns a segment (position & size) in a physical memory corresponding at the file.

Parameters:

  segment

  Pointer on the segment structure:
  ->u32_size_or_pos IN, shall contains maximum number of sector to read in file (0 = unlimited)
  ->u32_size_or_pos OUT, containt the segment size (unit sector)
  ->other IN, ignored
  ->other OUT, contains the segment position

Returns:
  FALSE in case of error, see global value "fs_g_status" for more detail

  TRUE otherwise

    //! This routine is interesting to read a file via a DMA and avoid the file system decode
    //! because this routine returns a physical memory segment without File System information.
    //! Note: the file can be fragmented and you must call file_read() for each fragments.
    //!

Definition at line 172 of file file.c.

    00173 {
    00174    U8 u8_nb_sector_truncated;
    00175
    00176    if( !fat_check_mount_select_open())
    00177       return FALSE;
    00178
    00179    if(!(FOPEN_READ_ACCESS & fs_g_nav_entry.u8_open_mode))
    00180    {
    00181       fs_g_status = FS_ERR_WRITE_ONLY;
    00182       return FALSE;
    00183    }
    00184
    00185    if ( file_eof() )
    00186    {
    00187       // End of the file
    00188       fs_g_status = FS_ERR_EOF;
    00189       return FALSE;
    00190    }
    00191
    00192    if( !fat_read_file(FS_CLUST_ACT_SEG))
    00193    {
    00194       if( FS_ERR_OUT_LIST == fs_g_status )
    00195          fs_g_status = FS_ERR_EOF;  // translate the error
    00196       return FALSE;
    00197    }
    00198    // If the segment is too large then truncate it
    00199    if( (segment->u16_size != 0)                          // if no limit then no truncate
    00200    &&  (segment->u16_size < fs_g_seg.u32_size_or_pos) )
    00201    {
    00202       u8_nb_sector_truncated   = fs_g_seg.u32_size_or_pos - segment->u16_size;
    00203       fs_g_seg.u32_size_or_pos = segment->u16_size ;
    00204    }else{
    00205       u8_nb_sector_truncated = 0;
    00206    }
    00207
    00208    // Update file position
    00209    fs_g_nav_entry.u32_pos_in_file += (U32)fs_g_seg.u32_size_or_pos * FS_512B;
    00210    if( fs_g_nav_entry.u32_size < fs_g_nav_entry.u32_pos_in_file )
    00211    {
    00212       // The segment is more larger then file
    00213       // case possible: if the file don't use all cluster space
    00214       // then compute sectors not used in last cluster of file cluster list
    00215       U8 u8_nb_sector_not_used;
    00216
    00217       // Compute the number of sector used in last cluster
    00218       // remark: also the two first bytes of size is used, because the cluster size can't be more larger then 64KB
    00219       u8_nb_sector_not_used = LSB1( fs_g_nav_entry.u32_size ) >> (FS_512B_SHIFT_BIT-8);
    00220       if( 0 != (fs_g_nav_entry.u32_size & FS_512B_MASK) )
    00221       {  // last sector of file isn't full, but it must been read
    00222          u8_nb_sector_not_used++;
    00223       }
    00224
    00225       // Compute the number of sector not used in last cluster
    00226       u8_nb_sector_not_used = fs_g_nav.u8_BPB_SecPerClus - (u8_nb_sector_not_used % fs_g_nav.u8_BPB_SecPerClus);
    00227       // if all space of cluster isn't used, then it is wrong
    00228       if( u8_nb_sector_not_used == fs_g_nav.u8_BPB_SecPerClus )
    00229          u8_nb_sector_not_used = 0; // The file uses all last cluster space
    00230
    00231       // Subtract this value a the file position and segment size
    00232       u8_nb_sector_not_used -= u8_nb_sector_truncated;
    00233       fs_g_seg.u32_size_or_pos -= u8_nb_sector_not_used;                                     // unit sector
    00234       fs_g_nav_entry.u32_pos_in_file -= ((U16)u8_nb_sector_not_used) << FS_512B_SHIFT_BIT;   // unit byte
    00235    }
    00236    file_load_segment_value( segment );
    00237    return TRUE;
    00238 }

U16 file_read_buf | ( | U8 _MEM_TYPE_SLOW_ * | buffer,
|  | U16 | u16_buf_size |
| ) |  |  |

This function copys in a buffer the file data corresponding at the current position.

Parameters:

  | buffer | buffer to fill
  | u16_buf_size | buffer size

Returns:
  number of byte read

  0, in case of error

Definition at line 249 of file file.c.

    00250 {
    00251    _MEM_TYPE_FAST_ U16 u16_nb_read_tmp;
    00252    _MEM_TYPE_FAST_ U16 u16_nb_read;
    00253    _MEM_TYPE_FAST_ U16 u16_pos_in_sector;
    00254    _MEM_TYPE_FAST_ U32 u32_byte_remaining;
    00255
    00256    if( !fat_check_mount_select_open())
    00257       return FALSE;
    00258
    00259    if(!(FOPEN_READ_ACCESS & fs_g_nav_entry.u8_open_mode))
    00260    {
    00261       fs_g_status = FS_ERR_WRITE_ONLY;
    00262       return FALSE;
    00263    }
    00264
    00265    u16_nb_read = 0;
    00266
    00267    while( 0 != u16_buf_size )
    00268    {
    00269       if ( file_eof() )
    00270       {
    00271          fs_g_status = FS_ERR_EOF;
    00272          return u16_nb_read;     // End of the file
    00273       }
    00274       u32_byte_remaining = fs_g_nav_entry.u32_size-fs_g_nav_entry.u32_pos_in_file;
    00275       u16_pos_in_sector = fs_g_nav_entry.u32_pos_in_file % FS_512B;
    00276
    00277       if( (0== u16_pos_in_sector)
    00278       &&  (FS_512B <= u32_byte_remaining)
    00279       &&  (FS_512B <= u16_buf_size)
    00280 #if __GNUC__ && __AVR32__ || __ICCAVR32__
    00281       &&  (Test_align((U32)buffer, sizeof(U32)))
    00282 #endif
    00283       )
    00284       {
    00285          // The file data sector can been directly transfer from memory to buffer (don't use internal cache)
    00286          if( u16_buf_size <= u32_byte_remaining)
    00287          {
    00288             u16_nb_read_tmp = u16_buf_size;
    00289          }else{
    00290             u16_nb_read_tmp = u32_byte_remaining;
    00291          }
    00292          u16_nb_read_tmp = u16_nb_read_tmp / FS_512B;  // read a modulo sector size
    00293
    00294          // Get following sector segment of file
    00295          if( !fat_read_file(FS_CLUST_ACT_SEG))
    00296          {
    00297             if( FS_ERR_OUT_LIST == fs_g_status )
    00298                fs_g_status = FS_ERR_EOF;  // translate the error
    00299             return u16_nb_read;
    00300          }
    00301          // Truncate the segment size found if more larger than asked size
    00302          if( u16_nb_read_tmp > fs_g_seg.u32_size_or_pos )
    00303          {
    00304             u16_nb_read_tmp = fs_g_seg.u32_size_or_pos;
    00305          }else{
    00306             fs_g_seg.u32_size_or_pos = u16_nb_read_tmp;
    00307          }
    00308
    00309          // Directly data tranfert from memory to buffer
    00310          while( 0 != fs_g_seg.u32_size_or_pos )
    00311          {
    00312             if( CTRL_GOOD != memory_2_ram( fs_g_nav.u8_lun  , fs_g_seg.u32_addr, buffer))
    00313             {
    00314                fs_g_status = FS_ERR_HW;
    00315                return u16_nb_read;
    00316             }
    00317             fs_g_seg.u32_size_or_pos--;
    00318             fs_g_seg.u32_addr++;
    00319             buffer += FS_512B;
    00320          }
    00321          // Translate from sector unit to byte unit
    00322          u16_nb_read_tmp *= FS_512B;
    00323       }
    00324       else
    00325       {
    00326          // The file data can't been directly transfer from memory to buffer, the internal cache must be used
    00327
    00328          // Tranfer data from memory to internal cache
    00329          if( !fat_read_file( FS_CLUST_ACT_ONE ))
    00330          {
    00331             if( FS_ERR_OUT_LIST == fs_g_status )
    00332             {  // Translate the error
    00333                fs_g_status = FS_ERR_EOF;   // End of file
    00334             }
    00335             return u16_nb_read;
    00336          }
    00337
    00338          // Compute the number of data to transfer
    00339          u16_nb_read_tmp = FS_512B - u16_pos_in_sector;  // The number is limited at sector size
    00340          if( u16_nb_read_tmp > u32_byte_remaining )
    00341             u16_nb_read_tmp = u32_byte_remaining;
    00342          if( u16_nb_read_tmp > u16_buf_size )
    00343             u16_nb_read_tmp = u16_buf_size;
    00344
    00345          // Tranfer data from internal cache to buffer
    00346          memcpy_ram2ram( buffer , &fs_g_sector[ u16_pos_in_sector ], u16_nb_read_tmp );
    00347          buffer += u16_nb_read_tmp;
    00348       }
    00349       // Update positions
    00350       fs_g_nav_entry.u32_pos_in_file   += u16_nb_read_tmp;
    00351       u16_nb_read                      += u16_nb_read_tmp;
    00352       u16_buf_size                     -= u16_nb_read_tmp;
    00353    }
    00354    return u16_nb_read;  // Buffer is full
    00355 }

U16 file_getc | ( | void |  | ) |

This function returns the next byte of file.

Returns:
  The byte readed

  EOF, in case of error or end of file

Definition at line 363 of file file.c.

Referenced by dfu_load_hex(), and ushell_cmd_cat().

    00364 {
    00365    U16   u16_byte;
    00366
    00367    while(1)
    00368    {
    00369       if(!(FOPEN_READ_ACCESS & fs_g_nav_entry.u8_open_mode))
    00370       {
    00371          fs_g_status = FS_ERR_WRITE_ONLY;
    00372          break;
    00373       }
    00374       if( fs_g_nav_entry.u32_size <= fs_g_nav_entry.u32_pos_in_file )
    00375       {
    00376          fs_g_status = FS_ERR_EOF;
    00377          break;
    00378       }
    00379
    00380       if( !fat_read_file( FS_CLUST_ACT_ONE ))
    00381       {
    00382          if( FS_ERR_OUT_LIST == fs_g_status )
    00383          {  // Translate the error
    00384             fs_g_status = FS_ERR_EOF;   // End of file
    00385          }
    00386          break;
    00387       }
    00388
    00389       u16_byte = fs_g_sector[ fs_g_nav_entry.u32_pos_in_file & FS_512B_MASK ];
    00390       fs_g_nav_entry.u32_pos_in_file++;
    00391       return u16_byte;
    00392    }
    00393    return FS_EOF;   // No data readed
    00394 }

Here is the caller graph for this function:

U32 file_getpos | ( | void |  | ) |

This function returns the position in the file.

Returns:
  Position in file

Definition at line 632 of file file.c.

    00633 {
    00634    if( !fat_check_mount_select_open() )
    00635       return 0;
    00636
    00637    return fs_g_nav_entry.u32_pos_in_file;
    00638 }

Bool file_seek | ( | U32 | u32_pos,
|  | U8 | u8_whence |
| ) |  |  |

This function changes the position in the file.

Parameters:

  | u32_pos | number of byte to seek

  u8_whence

  direction of seek
  FS_SEEK_SET , start at the beginning and foward
  FS_SEEK_END , start at the end of file and rewind
  FS_SEEK_CUR_RE, start at the current position and rewind
  FS_SEEK_CUR_FW, start at the current position and foward

Returns:
  FALSE in case of error, see global value "fs_g_status" for more detail

  TRUE otherwise

Definition at line 653 of file file.c.

    00654 {
    00655    if( !fat_check_mount_select_open())
    00656       return FALSE;
    00657
    00658    switch(u8_whence)
    00659    {
    00660       case FS_SEEK_CUR_RE:
    00661       if( fs_g_nav_entry.u32_pos_in_file < u32_pos )
    00662       {  // Out of the limit
    00663          fs_g_status = FS_ERR_BAD_POS;
    00664          return FALSE;
    00665       }
    00666       // update the position
    00667       fs_g_nav_entry.u32_pos_in_file -= u32_pos;
    00668       break;
    00669
    00670       case FS_SEEK_SET:
    00671       if( fs_g_nav_entry.u32_size < u32_pos )
    00672       {  // Out of the limit
    00673          fs_g_status = FS_ERR_BAD_POS;
    00674          return FALSE;
    00675       }
    00676       // update the position
    00677       fs_g_nav_entry.u32_pos_in_file = u32_pos;
    00678       break;
    00679
    00680       case FS_SEEK_END:
    00681       if( fs_g_nav_entry.u32_size < u32_pos )
    00682       {  // Out of the limit
    00683          fs_g_status = FS_ERR_BAD_POS;
    00684          return FALSE;
    00685       }
    00686       // update the position
    00687       fs_g_nav_entry.u32_pos_in_file = fs_g_nav_entry.u32_size - u32_pos;
    00688       break;
    00689
    00690       case FS_SEEK_CUR_FW:
    00691       u32_pos += fs_g_nav_entry.u32_pos_in_file;
    00692       if( fs_g_nav_entry.u32_size < u32_pos )
    00693       {  // Out of the limit
    00694          fs_g_status = FS_ERR_BAD_POS;
    00695          return FALSE;
    00696       }
    00697       // update the position
    00698       fs_g_nav_entry.u32_pos_in_file = u32_pos;
    00699       break;
    00700    }
    00701    return TRUE;
    00702 }

U8 file_bof | ( | void |  | ) |

This function checks the beginning of file.

Returns:
  1 the position is at the beginning of file

  0 the position isn't at the beginning of file

  FFh error

Definition at line 711 of file file.c.

    00712 {
    00713    if( !fat_check_mount_select_open() )
    00714       return 0xFF;
    00715
    00716    return (0 == fs_g_nav_entry.u32_pos_in_file );
    00717 }

U8 file_eof | ( | void |  | ) |

This function checks the end of file.

Returns:
  1 the position is at the end of file

  0 the position isn't at the end of file

  FFh error

Definition at line 726 of file file.c.

Referenced by dfu_load_hex(), file_read(), file_read_buf(), and ushell_cmd_cat().

    00727 {
    00728    if( !fat_check_mount_select_open() )
    00729       return 0xFF;
    00730    return (fs_g_nav_entry.u32_size <= fs_g_nav_entry.u32_pos_in_file );
    00731 }

Here is the caller graph for this function:

void file_flush | ( | void |  | ) |

This function flushs the internal cache (file datas and file information).

Definition at line 736 of file file.c.

    00737 {
    00738    U8 save_open_mode;
    00739    save_open_mode = fs_g_nav_entry.u8_open_mode;
    00740    file_close();
    00741    fs_g_nav_entry.u8_open_mode = save_open_mode;
    00742 }

void file_close | ( | void |  | ) |

This function closes the file.

Definition at line 747 of file file.c.

Referenced by dfu_load_hex(), file_flush(), nav_exit(), ushell_cmd_append_file(), ushell_cmd_cat(), ushell_cmd_perform(), and ushell_cmd_perform_alloc().

    00748 {
    00749    // If a file is opened, then close this one
    00750    if( fat_check_mount_select_open() )
    00751    {
    00752
    00753 #if (FSFEATURE_WRITE == (FS_LEVEL_FEATURES & FSFEATURE_WRITE))
    00754       if( FOPEN_WRITE_ACCESS & fs_g_nav_entry.u8_open_mode )
    00755       {
    00756          // Write file information
    00757          if( !fat_read_dir() )
    00758             return;           // error
    00759          fat_write_entry_file();
    00760          fat_cache_flush();   // In case of error during writing data, flush the data before exit function
    00761       }
    00762 #endif  // FS_LEVEL_FEATURES
    00763       Fat_file_close();
    00764    }
    00765 }

Here is the caller graph for this function:

## Variable Documentation

_MEM_TYPE_SLOW_ U8 fs_g_sector[512]

Use "FAT sector cache" to store a sector from a file (see file_putc(), file_getc(), file_read_buf(), file_write_buf()).