/*-----------------------------------------------------------------------*/
/* Low level disk I/O module SKELETON for FatFs     (C)ChaN, 2019        */
/*-----------------------------------------------------------------------*/
/* If a working storage control module is available, it should be        */
/* attached to the FatFs via a glue function rather than modifying it.   */
/* This is an example of glue functions to attach various exsisting      */
/* storage control modules to the FatFs module with a defined API.       */
/*-----------------------------------------------------------------------*/

//#include <hitech.h>
#include <stdio.h>
#include "ff.h"			/* Obtains integer types */
#include "diskio.h"		/* Declarations of disk functions */

#include "ch375.h"
#include "rtc.h"

/* Definitions of physical drive number for each drive */
#define DEV_RAM		2	/* Example: Map Ramdisk to physical drive 2 */
#define DEV_MMC		1	/* Example: Map MMC/SD card to physical drive 1 */
#define DEV_USB		0	/* Example: Map USB MSD to physical drive 0 */


/*-----------------------------------------------------------------------*/
/* Get Drive Status                                                      */
/*-----------------------------------------------------------------------*/

DSTATUS disk_status (
	BYTE pdrv		/* Physical drive nmuber to identify the drive */
)
{
	DSTATUS stat;

	switch (pdrv) {

	case DEV_USB :
//		result = USB_disk_status();
//printf("\ndisk status entered");
		// translate the reslut code here
		stat = 0;
		return stat;
	}
	return STA_NOINIT;
}



/*-----------------------------------------------------------------------*/
/* Inidialize a Drive                                                    */
/*-----------------------------------------------------------------------*/

DSTATUS disk_initialize (
	BYTE pdrv				/* Physical drive nmuber to identify the drive */
)
{
//	DSTATUS stat;
//	int result;

	switch (pdrv) {
	case DEV_USB :
//		result = USB_disk_initialize();

		// translate the result code here
		return( ch375_probe());
	}
	return STA_NOINIT;
}



/*-----------------------------------------------------------------------*/
/* Read Sector(s)                                                        */
/*-----------------------------------------------------------------------*/

DRESULT disk_read (
	BYTE pdrv,		/* Physical drive nmuber to identify the drive */
	BYTE *buff,		/* Data buffer to store read data */
	LBA_t sector,	/* Start sector in LBA */
	UINT count		/* Number of sectors to read */
)
{

	switch (pdrv) {

	case DEV_USB :
		// translate the arguments here


		// translate the reslut code here
		return ch375_read(buff, sector, count);
	}

	return RES_PARERR;
}



/*-----------------------------------------------------------------------*/
/* Write Sector(s)                                                       */
/*-----------------------------------------------------------------------*/

#if FF_FS_READONLY == 0

DRESULT disk_write (
	BYTE pdrv,			/* Physical drive nmuber to identify the drive */
	const BYTE *buff,	/* Data to be written */
	LBA_t sector,		/* Start sector in LBA */
	UINT count			/* Number of sectors to write */
)
{

	switch (pdrv) {
	case DEV_USB :
		// translate the arguments here

		// translate the reslut code here
		return ch375_write(buff, sector, count);

	}

	return RES_PARERR;
}

#endif


/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions                                               */
/*-----------------------------------------------------------------------*/

DRESULT disk_ioctl (
	BYTE pdrv,		/* Physical drive nmuber (0..) */
	BYTE cmd,		/* Control code */
	void *buff		/* Buffer to send/receive control data */
)
{

	switch (pdrv) {
//printf("\ndisk ioctl %i", cmd);
	case DEV_USB:
		switch (cmd)
		{
			case CTRL_SYNC:
				return RES_OK;
		
			case GET_SECTOR_COUNT:
				*((DWORD *)buff) = ch375_disk_capacity();
				return RES_OK;
		
			case GET_SECTOR_SIZE:
				*((WORD *)buff) = 512;
				return RES_OK;
		
			case GET_BLOCK_SIZE:
				*((DWORD *)buff) = 1;
				return RES_OK;
		
			case CTRL_TRIM:
				return RES_OK;
		}
		// Process of the command the USB drive
		break;
	}

	return RES_PARERR;
}

BYTE bcd2bin(BYTE val)
{
	return (((val >> 4) * 10) + (val & 0x0F));
}

DWORD get_fattime (void)
{
	DWORD fattime;
	
	rtcdata timestamp;
	if(rtc_fitted())
	{
		getrtc(&timestamp);

		/* Pack date and time into a DWORD variable */
		fattime = 0;
		fattime |= (((DWORD)bcd2bin(timestamp.year) + 20) << 25);
		fattime |= ((DWORD)bcd2bin(timestamp.month) << 21);
		fattime |= ((DWORD)bcd2bin(timestamp.day) << 16);
		fattime |= ((WORD)bcd2bin(timestamp.time.h) << 11);
		fattime |= ((WORD)bcd2bin(timestamp.time.m) << 5);
		fattime |= ((WORD)bcd2bin(timestamp.time.s) >> 1);
	}
	else
	{
		fattime = 0;
		fattime |= ((DWORD)(2000 - 1980) << 25);
		fattime |= ((DWORD)1 << 21);
		fattime |= ((DWORD)1 << 16);
		fattime |= ((WORD)1 << 11);
		fattime |= ((WORD)2 << 5);
		fattime |= ((WORD)3 >> 1);
	}
	
	return fattime;
}
