//----------------------------------------------------------------------------------
// <copyright file="sd_ff.h" company="LiquidRobotics">
//	Copyright (c) Liquid Robotics Corporation.  All rights reserved.
// </copyright>
//
// <summary>
// 	Header File for SD file system wrappers around the generic FAT File implementation
// </summary>
//
// <owner>Jim Kirklin</owner>
//---------------------------------------------------------------------------------

#ifndef _SD_FF_H_
#define _SD_FF_H_

#define MAX_FILES_OPEN	4



//File Function return values
#define	SD_OK				0			
#define	SD_DISK_ERR			1			
#define	SD_INT_ERR			2			
#define	SD_NOT_READY		3			
#define	SD_NO_FILE			4			
#define	SD_NO_PATH			5			
#define	SD_INVALID_NAME		6			
#define	SD_DENIED			7			
#define	SD_EXIST			8			
#define	SD_INVALID_OBJECT	9			
#define	SD_WRITE_PROTECTED	10			
#define	SD_INVALID_DRIVE	11			
#define	SD_NOT_ENABLED		12			
#define	SD_NO_FILESYSTEM	13			
#define	SD_MKFS_ABORTED		14			
#define	SD_TIMEOUT			15			
#define SD_TOO_MANY_FILES	16			
#define SD_INVALID_HANDLE	17			
#define SD_SEEK_PAST_EOF	18			


//File Creation Flags
#define SD_CREATE_AND_OPEN	0x01

//Open File Flags
#define	SD_READ				0x01
#define	SD_OPEN_EXISTING	0x00
#define	SD_WRITE			0x02
#define	SD_CREATE_NEW		0x04
#define	SD_CREATE_ALWAYS	0x08
#define	SD_OPEN_ALWAYS		0x10
#define SD__WRITTEN			0x20
#define SD__DIRTY			0x40
#define SD__ERROR			0x80

typedef struct _FILEINFO_ 
{
	uint32_t	fsize;		/* File size */
	uint16_t	fdate;		/* Last modified date */
	uint16_t	ftime;		/* Last modified time */
	uint8_t	fattrib;	/* Attribute */
	char	fname[13];	/* Short file name (8.3 format) */
} FILEINFO, *PFILEINFO;


uint8_t	SD_Init(void);
uint8_t SD_Mount(void);
uint8_t SD_Unmount (void);
uint8_t SD_CreateFile(char* pFileName, int8_t* pHandle, uint8_t flags);
uint8_t SD_CreateFixedSizeFile(char* pFileName, uint32_t size, int8_t* pHandle, uint8_t flags);
uint8_t SD_fopen(char* pFileName, uint8_t flags, int8_t* pHandle);
uint8_t SD_fclose(int8_t handle);
uint8_t SD_fread(int8_t handle, char* pBuffer, uint16_t bytesToRead, uint16_t* pBytesRead);
uint8_t SD_freadat(int8_t handle, char* pBuffer,uint32_t start, uint16_t bytesToRead, uint16_t* pBytesRead);
uint8_t SD_freadat_atomic(char* pFileName, char* pBuffer,uint32_t start, uint16_t bytesToRead, uint16_t* pBytesRead);
uint8_t SD_fwrite(int8_t handle, char* pBuffer, uint16_t bytesToWrite, uint16_t* bytesWritten);
uint8_t SD_fwriteat(int8_t handle, char* pBuffer, uint32_t start,  uint16_t bytesToWrite, uint16_t* bytesWritten);
uint8_t SD_fwriteat_atomic(char* pFileName, char* pBuffer, uint32_t start, uint16_t bytesToWrite, uint16_t* bytesWritten);
uint8_t SD_delete(char* pFileName);
uint8_t SD_dir(uint8_t getFirst, PFILEINFO pFileInfo);
//uint8_t SD_getFileInfo(int8_t handle, PFILEINFO pFileInfo);
uint8_t SD_getFileInfo(const char* lpszFileName, PFILEINFO pFileInfo);
uint8_t	SD_seek(int8_t handle, uint32_t position);

#endif		//_SD_FF_H_

