/*-----------------------------------------------------------------------*/
/* Low level disk I/O module skeleton for FatFs     (C)ChaN, 2013        */
/*-----------------------------------------------------------------------*/
/* 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 module to the FatFs module with a defined API.        */
/*-----------------------------------------------------------------------*/

#include "diskio.h"		/* FatFs lower layer API */
#include <string.h> // memcpy

#include "stm32f4xx.h"
#include "../SD_Card/sdio_sd.h"

#define BLOCK_SIZE            512 /* Block Size in Bytes */

/* Definitions of physical drive number for each media */
#define ATA		0
#define MMC		1
#define USB		2


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

DSTATUS disk_initialize (
	BYTE pdrv				/* Physical drive nmuber (0..) */
)
{
	SD_Error  Status;

	/* Supports only single drive */
	if (pdrv) return STA_NOINIT;
	/*-------------------------- SD Init ----------------------------- */
	Status = SD_Init();
	if (Status!=SD_OK ) return STA_NOINIT;
	else return RES_OK;
}



/*-----------------------------------------------------------------------*/
/* Get Disk Status                                                       */
/*-----------------------------------------------------------------------*/

DSTATUS disk_status (
	BYTE pdrv		/* Physical drive nmuber (0..) */
)
{
	DSTATUS stat = 0;

	if (SD_Detect() != SD_PRESENT)
		stat |= STA_NODISK;

	// STA_NOTINIT - Subsystem not initailized
	// STA_PROTECTED - Write protected, MMC/SD switch if available

	return(stat);
}



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

DRESULT disk_read (
	BYTE pdrv,		/* Physical drive nmuber (0..) */
	BYTE *buff,		/* Data buffer to store read data */
	DWORD sector,	/* Sector address (LBA) */
	UINT count		/* Number of sectors to read (1..128) */
)
{
	SDTransferState state;
	DRESULT res = RES_OK;

	if (SD_Detect() != SD_PRESENT) return RES_NOTRDY;

	if (count == 0) return RES_PARERR;

	//SD_ReadMultiBlocks ( buff, sector, 512, 1 );
	SD_ReadMultiBlocksFIXED (buff, sector, 512, count);

	SD_WaitReadOperation();
    do {
      state = SD_GetStatus();
      if(state == SD_TRANSFER_ERROR) {
        res = RES_ERROR;
        break;
      }
	} while (state != SD_TRANSFER_OK);

	return res;
}

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

#if _USE_WRITE
DRESULT disk_write (
	BYTE pdrv,			/* Physical drive nmuber (0..) */
	const BYTE *buff,	/* Data to be written */
	DWORD sector,		/* Sector address (LBA) */
	UINT count			/* Number of sectors to write (1..128) */
)
{
	//SDTransferState state;
	//  DRESULT res = RES_OK;

	if (SD_Detect( ) != SD_PRESENT) return RES_NOTRDY;

	if (count == 0) return RES_PARERR;

	//SD_WriteMultiBlocks ( (uint8_t *)buff, sector, 512, count );
	SD_WriteMultiBlocksFIXED ((uint8_t *)buff, sector, 512, count);
	SD_WaitWriteOperation();
	while (SD_GetStatus() != SD_TRANSFER_OK);

	return RES_OK;
}
#endif


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

#if _USE_IOCTL
DRESULT disk_ioctl (
	BYTE pdrv,		/* Physical drive nmuber (0..) */
	BYTE cmd,		/* Control code */
	void *buff		/* Buffer to send/receive control data */
)
{
	return RES_OK;
}
#endif