/*********************************************************************************************************                                               
*                                     MCU ACH118X driver library
*                             COPYRIGHT(c) 2019 Amped RF Technology (ART)
*                                        All Rights Reserved
*
* File    : ach118x_file.h
* Version : V1.0.0
* Brief   : 
*  - Data structures and prototypes for fopen, fclose
**********************************************************************************************************/

#ifndef __ACH118X_FILE_H
#define __ACH118X_FILE_H

/*************************************************************************/
/*                   File Definitions                                    */
/*************************************************************************/
#define FILENAME_MAXSIZE 54

typedef struct {
    uint8_t     EntryType;          // Type of entry
    uint8_t     FileAttribute;      // Attribute of file
    uint16_t    FileStartSector;    // Sector start of file
    uint32_t    FileSize;           // Size of file in bytes
    char        FileName[FILENAME_MAXSIZE]; // Name of file
    uint16_t    CRC16;
} FileEntry_t;

#define ENTRY_DELETED           0x00
#define ENTRY_VALID             0x0F
#define ENTRY_UNUSED            0xFF

#define FILE_FLASHLOADER        0
#define FILE_APPLICATION        1
#define FILE_GENERIC            2

#define ATTR_MASK_ALL           0x03
#define ATTR_EXE                0x01
#define ATTR_DEFAULT            0x02
#define ATTR_DEFAULT_EXE        0x03
#define ATTR_OFFSET             1
#define ATTR_GENERIC_FILE       0xFF

#define SELECT_EXE              0
#define SELECT_EXE_DEFAULT      1
#define SELECT_ALL              3

#define CREATE_ENTRY_OK         0
#define CREATE_ENTRY_FAIL       -1

#ifndef M25D80_SECTOR_SIZE
#define M25D80_SECTOR_SIZE      4096
#endif

#define SECTOR_SIZE             (M25D80_SECTOR_SIZE)
#define SECTOR0_ADDR            (0)
#define SECTOR1_ADDR            (SECTOR_SIZE)
#define PERS_VARS_SECTOR        (3)             // Persistent Variable sector
#define PERS_VARS_SIZE          (4*SECTOR_SIZE)
#define DIR_SECTOR              (2)             // Directory sector
#define DIR_ADDR                (DIR_SECTOR*SECTOR_SIZE)
#define DIR_SIZE                (SECTOR_SIZE)
#define ENTRYMAX                (DIR_SIZE / sizeof(FileEntry_t))
#define APP_EXECUTE_ADDR        (0x20002000)
#define OTP_ADDR                (void *)(0x20001F80)
#define OTP_SIZE                (128)
#define MAC_AREA_ADDR           (0x20001FC0)
#define pMAC_Area               (u8 *)MAC_AREA_ADDR
#define MAC_AREA_SIZE           (64)
#define MAC_FLASH_ADDR          (0x1FC0)
#define GetSectorAddr(n)        (n*SECTOR_SIZE)

/*************************************************************************/
/*                   File globals                                        */
/*************************************************************************/
extern FileEntry_t Directory[];
extern int NumFiles;

typedef struct {
    uint16_t FileEntry;
    char     FileName[FILENAME_MAXSIZE];
    int16_t  Mode;
    uint32_t StartAddr;
    uint32_t Size;
    uint32_t Offset;
} FILE_HANDLE_t;

#define FILE_HANDLE_OFFSET  3
#define FILE_HANDLE_MAX     16
extern FILE_HANDLE_t *__FILE_HANDLE[];
#define FILE int


/*************************************************************************/
/*                   File prototypes                                     */
/*************************************************************************/
int fgets(unsigned char *sbuf, int size, FILE *stream);
int fwrite(char unsigned *buf, int n, int plen, FILE *stream);
FILE *fopen(char *filename, char *opt);
int fseek(FILE *stream, long offset, int whence);
long ftell(FILE *stream);
void fclose(FILE *stream);
int fread(void *ptr, int size, int nmemb, FILE * stream);
int ferror(FILE *stream);
char *getcwd(char *buf, int size);
int ReadDirectory(void);
int NextFile(int n, char *ext);


#endif /* __ACH118X_FILE_H */
/************************ (C) COPYRIGHT ART *****END OF FILE****/
