/* ---------------------------------------------------------------------------
 * FAT_intern.h                                                 (C)ChaN, 2008
 * ---------------------------------------------------------------------------
 * Micro-key bv
 * Industrieweg 28, 9804 TG Noordhorn
 * Postbus 92, 9800 AB Zuidhorn
 * The Netherlands
 * Tel: +31 594 503020
 * Fax: +31 594 505825
 * Email: support@microkey.nl  
 * Web: www.microkey.nl
 * ---------------------------------------------------------------------------
 * Description: FatFs - FAT file system module include file  R0.06
 * 
 * FatFs module is an experimenal project to implement FAT file system to
 * cheap microcontrollers. This is a free software and is opened for education,
 * research and development under license policy of following trems.
 * 
 * Copyright (C) 2008, ChaN, all right reserved.
 * 
 * The FatFs module is a free software and there is no warranty.
 * You can use, modify and/or redistribute it for personal, non-profit or
 * commercial use without any restriction under your responsibility.
 * Redistributions of source code must retain the above copyright notice.
 * ---------------------------------------------------------------------------
 */
/* ---------------------------------------------------------------------------
 * Version(s):  0.2, Aug 11, 2008, MMi
 *                  Edited to fit into LAN_2636 Project
 *                   
 *              0.1, 2008 ChanN
 *                  Creation
 * ---------------------------------------------------------------------------
 */
#ifndef FAT_PUBLIC_H_
#define FAT_PUBLIC_H_ 
/* ---------------------------------------------------------------------------
 * System include files.
 * ---------------------------------------------------------------------------
 */
#include "LPC23xx.h"
#include "types.h"

/* ---------------------------------------------------------------------------
 * Application include files.
 * ---------------------------------------------------------------------------
 */
#include "fat_intern.h"
/* ---------------------------------------------------------------------------
 * Constant and macro definitions.
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * Type definitions.
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * Variable declarations.
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * Function declarations.
 * ---------------------------------------------------------------------------
 */

void FAT_StatusOut (FRESULT result);

/* ---------------------------------------------------------------------------
 * Function: f_mount
 * 
 * Function mounts or unmounts a logical drive
 * 
 * Parameters:  UINT8 drive - Device number that should be (un)mounted
 *              FATFS *fs   - Pointer to a clean file system structure
 * 
 * Return : FRESULT - Result if mounting was successful or not
 * ---------------------------------------------------------------------------
 */
FRESULT f_mount (UINT8 drive, FATFS *fs);


/* ---------------------------------------------------------------------------
 * Function: f_open
 * 
 * Function opens or creates a file, depending on the rights given by the
 * Attributes in mode. The Filename must contain the whole physical path, so
 * include every subfolder. If using multible drives, the drivenumber is also
 * necessary. Skipping the drivenumber forces the System to use 0 as drive
 * number. So in single drive mode, drive number should be 0.
 * 
 * Parameters:  FIL *fp          - Pointer to a clean file structure
 *              const char *path - Name of the File ("drive:subdir/file.ext")
 * 
 * Return : FRESULT - Result if opening was successful or not
 * ---------------------------------------------------------------------------
 */
FRESULT f_open (FIL *fp, const char *path, UINT8 mode);


/* ---------------------------------------------------------------------------
 * Function: f_read
 * 
 * Function to read from a file. File needs to be opened before with f_open.
 * The file structure of this is then given tp f_read. Data is read to *buff.
 * 
 * Parameters:  FIL *fp    - Pointer to opened file structure
 *              void *buff - Array to read to
 *              UINT32 btr - indicates how much bytes should be read
 *              UINT32 *br - returns, how much uch bytes are already read
 * 
 * Return : FRESULT - Result if reading was successful or not
 * ---------------------------------------------------------------------------
 */
FRESULT f_read (FIL *fp, void *buff, UINT32 btr, UINT32 *br);


/* ---------------------------------------------------------------------------
 * Function: f_write
 * 
 * Function to write to a file. File needs to be opened before with f_open.
 * The file structure of this is then given tp f_write. Data is written from
 *  *buff.
 * 
 * Parameters:  FIL *fp    - Pointer to opened file structure
 *              void *buff - Array to write from
 *              UINT32 btr - indicates how much bytes should be written
 *              UINT32 *br - returns, how much uch bytes are already written
 * 
 * Return : FRESULT - Result if writing was successful or not
 * ---------------------------------------------------------------------------
 */
FRESULT f_write (FIL *fp, const void *buff, UINT32 btw, UINT32 *bw);


/* ---------------------------------------------------------------------------
 * Function: f_sync
 * 
 * Function to flush cached data of a written file
 * 
 * Parameters:  FIL *fp - Pointer to opened file structure
 * 
 * Return : FRESULT - Result if syncing was successful or not
 * ---------------------------------------------------------------------------
 */
FRESULT f_sync(FIL *fp);


/* ---------------------------------------------------------------------------
 * Function: f_close
 * 
 * Function to close a opened file. The file is then not accessible anymore.
 * 
 * Parameters:  FIL *fp - Pointer to opened file structure
 * 
 * Return : FRESULT - Result if closing was successful or not
 * ---------------------------------------------------------------------------
 */
FRESULT f_close(FIL *fp); 


/* ---------------------------------------------------------------------------
 * Function: f_lseek
 * 
 * Function to move the file pointer. When creating a file, the pointer is at
 * offset 0x00. After writing or reading, the pointer is moved by the number
 * of bytes that have been read. 
 * 
 * Parameters:  FIL *fp - Pointer to opened file structure
 * 
 * Return : FRESULT - Result if seeking was successful or not
 * ---------------------------------------------------------------------------
 */
FRESULT f_lseek (FIL *fp, UINT32 ofs);


/* ---------------------------------------------------------------------------
 * Function: f_opendir
 * 
 * Function to open an already existing directory. Fails, if directory is not
 * available or accessible.
 * 
 * Parameters:  DIR *dj          - Pointer to clean directory structure
 *              const char *path - Directory name ("dir/subdir/subsubdir")
 * 
 * Return : FRESULT - Result if opening was successful or not
 * ---------------------------------------------------------------------------
 */
FRESULT f_opendir (DIR *dj, const char *path);


/* ---------------------------------------------------------------------------
 * Function: f_readdir
 * 
 * Function to read a directory Item
 * 
 * Parameters:  DIR *dj        - Pointer to directory structure
 *              FILINFO *finfo - Pointer to a clean file info structure
 * 
 * Return : FRESULT - Result if reading was successful or not
 * ---------------------------------------------------------------------------
 */
FRESULT f_readdir (DIR *dj, FILINFO *finfo);


/* ---------------------------------------------------------------------------
 * Function: f_stat
 * 
 * Function to get a file status
 * 
 * Parameters:  const char* path - Name of the path 
 *              FILINFO *finfo   - Pointer to a clean file info structure
 * 
 * Return : FRESULT - Result if reading was successful or not
 * ---------------------------------------------------------------------------
 */
FRESULT f_stat(const char* path, FILINFO* finfo);

/* ---------------------------------------------------------------------------
 * Function: f_truncate
 * 
 * Function to truncate/abort
 * 
 * Parameters:  FIL *fp - Pointer to a opened file object structure
 *              
 * 
 * Return : FRESULT - Result if aborting was successful or not
 * ---------------------------------------------------------------------------
 */
FRESULT f_truncate(FIL *fp);


/* ---------------------------------------------------------------------------
 * Function: f_getfree
 * 
 * Function to get free clusters on the drive
 * 
 * Parameters:  const char* drive - Number of drive
 *              UINT32 *nclust    - Pointer to store number of free clusters
 *              FATFS **fats      - Pointer to Pointer to file system
 *              
 * 
 * Return : FRESULT - Result if getting was successful or not
 * ---------------------------------------------------------------------------
 */
FRESULT f_getfree (const char *drv, UINT32 *nclust, FATFS **fatfs);


/* ---------------------------------------------------------------------------
 * Function: f_unlink
 * 
 * Function to delete an existing file or directory.
 * NOTE: Directories must be empty before deleting!
 * 
 * Parameters:  const char* path - Name of file/directory 
 *              
 * Return : FRESULT - Result if deleting was successful or not
 * ---------------------------------------------------------------------------
 */
FRESULT f_unlink(const char* path);


/* ---------------------------------------------------------------------------
 * Function: f_mkdir
 * 
 * Function to create directories.
 * Syntax: "dir/subdir/subsubdir"
 * 
 * Parameters:  const char* path - Name/Path of directory 
 *              
 * Return : FRESULT - Result if creating was successful or not
 * ---------------------------------------------------------------------------
 */
FRESULT f_mkdir(const char* path);

/* ---------------------------------------------------------------------------
 * Function: f_chmod
 * 
 * Function to change attributes of files and directories.
 * Usage example: 
 * To set read-only flag, clear archive flag and retain others:
 * f_chmod("file.txt", AR_RDO, (AR_RDO | AR_ARC));
 * 
 * Parameters:  const char* path - Name of file/directory
 *              UINT8 value      - The Attribute Bits to SET
 *              UINT8 mask       - All Attribute Bits to CHANGE
 *              
 * Return : FRESULT - Result if changing was successful or not
 * ---------------------------------------------------------------------------
 */
FRESULT f_chmod (const char *path, UINT8 value, UINT8 mask);


/* ---------------------------------------------------------------------------
 * Function: f_utime
 * 
 * Function to change timestamp of file or directory
 * 
 * Parameters:  const char* path     - Name of file/directory
 *              const FILINFO *finfo - file info structure with new timestamp
 *              
 * Return : FRESULT - Result if changing was successful or not
 * ---------------------------------------------------------------------------
 */
FRESULT f_utime (const char *path, const FILINFO *finfo);


/* ---------------------------------------------------------------------------
 * Function: f_rename
 * 
 * Function to rename and move a file or directory. 
 * Usage: To move a file, simply give the complete physical path in path_new
 * 
 * NOTE: The original author mentioned that movies directories to other
 * directories seems to collapse the FAT, but all tests with this were 
 * positive without any problems. If FAT collapses after moving directories,
 * refer to here!
 * 
 * Parameters:  const char* path_old - Old Name of file/directory
 *              const char* path_new - New Name of file/directory
 *              
 * Return : FRESULT - Result if moving/renaming was successful or not
 * ---------------------------------------------------------------------------
 */
FRESULT f_rename (const char *path_old, const char *path_new);


/* ---------------------------------------------------------------------------
 * Function: f_mkfs
 * 
 * Function to make/create a file system
 * 
 * NOTE: Is usually unused!
 * 
 * Parameters:  UINT8 drv        - Drive number
 *              UINT8 partition  - Partition number
 *              UINT16 allocsize - Allocating unit size in bytes
 *              
 * Return : FRESULT - Result if moving/renaming was successful or not
 * ---------------------------------------------------------------------------
 */
FRESULT f_mkfs (UINT8 drv, UINT8 partition, UINT16 allocsize);
#endif /*FAT_PUBLIC_H_*/
