/* ---------------------------------------------------------------------------
 * diskio.c                                                     (C)ChaN, 2007
 * ---------------------------------------------------------------------------
 * 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: 
 * This is a stub disk I/O module that acts as front end of the existing 
 * disk I/O modules and attach it to FatFs module with common interface.
 * 
 * ---------------------------------------------------------------------------
 * Version(s):  0.2, Aug 11, 2008, MMi
 *                  Edited to fit into LAN_2636 Project
 *                  Change disk_write and disk_read functions to work with MMC
 *                  disable multible drive support
 *  
 *              0.1, 2007 ChanN
 *                  Creation
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * System include files
 * ---------------------------------------------------------------------------
 */
#include "LPC23xx.h"
#include "types.h"
/* ---------------------------------------------------------------------------
 * Application include files
 * ---------------------------------------------------------------------------
 */
#include "fat_diskio.h"
#include "rtc.h"
#include "mmc.h"
#include "mmc_transfer.h"

/* ---------------------------------------------------------------------------
 * Local constant and macro definitions
 * ---------------------------------------------------------------------------
 */
#define ATA		0
#define MMC		1
#define USB		2

#define PAGESIZE        512             /* Pagesize of Card in Byte         */
/* ---------------------------------------------------------------------------
 * Global variable definitions
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * Local variable definitions
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * Local function definitions
 * ---------------------------------------------------------------------------
 */

DSTATUS disk_initialize(UINT8 drive)
{
    DSTATUS stat = 0;

    /* Multidevice support disabled, always return "Operation suceeded"     */
    switch (drive)
        {
        case ATA:
            return (stat);

        case MMC:
            return (stat);

        case USB:
            return (stat);
            
        default:
            return (stat);
        }
    return STA_NOINIT;
}


DSTATUS disk_status(UINT8 drive)
{
    DSTATUS stat = 0;

    /* Multidevice support disabled, always return "Operation suceeded"     */
    switch (drive)
        {
        case ATA:
            return (stat);

        case MMC:
            return (stat);

        case USB:
            return (stat);
            
        default:
            return (stat);
        }
    return STA_NOINIT;
}


DRESULT disk_ioctl(UINT8 drive, UINT8 ctrl, void *buffer)
{
    DRESULT res = 0;

    /* Multidevice support disabled, always return "Operation suceeded"     */
    switch (drive)
        {
        case ATA:
            return (res);

        case MMC:
            return (res);

        case USB:
            return (res);
            
        default:
            return (res);
        }
    return RES_PARERR;
}


DRESULT disk_read(UINT8 drive, pUINT8 buffer, UINT32 sector, UINT8 count)
{
    UINT32 readResult = 1;
    MmcState_t cardResult;              /* returned from the card reading   */

    /* Read MMC/SD card on the desired sector for desired length
     * The multiplications are necessary because the File Systems counts in
     * sectors whereas the original MMC/SD driver requires the adress offset
     */
    cardResult = CardRead(buffer, (sector * PAGESIZE), (count * PAGESIZE));

    /* Switch corresponding to the read result                              */
    switch (cardResult)
        {
        case MmcOk:
            /* Enter here, the reading worked without problems              */
            readResult = 0;
            break;
        default:
            /* enter here, there were some problems while reading           */
            readResult = 1;
            break;
        }

    return (readResult);
}


#if _READONLY == 0                      /* defined in diskio.h              */
DRESULT disk_write(UINT8 drive, pUINT8 buffer, UINT32 sector, UINT8 count)
{
    UINT32 writeResult = 1;
    MmcState_t cardResult;

    /* Read MMC/SD card on the desired sector for desired length
     * The multiplications are necessary because the File Systems counts in
     * sectors whereas the original MMC/SD driver requires the adress offset
     */
    cardResult = CardWrite(buffer, (sector * PAGESIZE), (count * PAGESIZE));

    /* Switch corresponding to the read result                              */
    switch (cardResult)
        {
        case MmcOk:
            /* Enter here, the writing worked without problems              */
            writeResult = 0;
            break;
        default:
            /* enter here, there were some problems while writing           */
            writeResult = 1;
            break;
        }

    return (writeResult);
}
#endif                                  /* _READONLY                        */

