- 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 144 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().

    00145 {
    00146    segment->u8_lun = fs_g_nav.u8_lun;
    00147    segment->u32_addr = fs_g_seg.u32_addr;
    00148    segment->u16_size = fs_g_seg.u32_size_or_pos;
    00149 }

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 69 of file file.c.

    00070 {
    00071    if( !fat_check_mount_select() )
    00072       return FALSE;
    00073    return fat_check_is_file();
    00074 }

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 89 of file file.c.

Referenced by firm_upgrade_run(), firm_upgrade_status(), ushell_cmd_append_file(), ushell_cmd_cat(), and ushell_cmd_perform_alloc().

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

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 169 of file file.c.

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

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 246 of file file.c.

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

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 360 of file file.c.

Referenced by firm_upgrade_readbyte(), firm_upgrade_readhexline(), and ushell_cmd_cat().

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

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 629 of file file.c.

    00630 {
    00631    if( !fat_check_mount_select_open() )
    00632       return 0;
    00633
    00634    return fs_g_nav_entry.u32_pos_in_file;
    00635 }

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 650 of file file.c.

Referenced by firm_upgrade_run().

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

Here is the caller graph for this function:

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 708 of file file.c.

    00709 {
    00710    if( !fat_check_mount_select_open() )
    00711       return 0xFF;
    00712
    00713    return (0 == fs_g_nav_entry.u32_pos_in_file );
    00714 }

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 723 of file file.c.

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

    00724 {
    00725    if( !fat_check_mount_select_open() )
    00726       return 0xFF;
    00727    return (fs_g_nav_entry.u32_size <= fs_g_nav_entry.u32_pos_in_file );
    00728 }

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 733 of file file.c.

    00734 {
    00735    U8 save_open_mode;
    00736    save_open_mode = fs_g_nav_entry.u8_open_mode;
    00737    file_close();
    00738    fs_g_nav_entry.u8_open_mode = save_open_mode;
    00739 }

void file_close | ( | void |  | ) |

This function closes the file.

Definition at line 744 of file file.c.

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

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

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()).