/*
 * shimatta_sdio-driver.h
 *
 *  Created on: Apr 26, 2015
 *  Mario Hüttel
 */

#ifndef FATFS_SHIMATTA_SDIO_DRIVER_SHIMATTA_SDIO_DRIVER_H_
#define FATFS_SHIMATTA_SDIO_DRIVER_SHIMATTA_SDIO_DRIVER_H_

#include <fatfs/diskio.h>
#include <fatfs/ff.h>
#include <stdint.h>

DSTATUS sdio_status();
DSTATUS sdio_initialize();

DRESULT sdio_disk_read(BYTE *buff, DWORD sector, UINT count);
DRESULT sdio_disk_write(const BYTE *buff, DWORD sector, UINT count);
DRESULT sdio_disk_ioctl(BYTE cmd, void* buff);
DWORD get_fattime();

int sdio_check_inserted();


//Defines for Card Status in struct _CardStatus
#define CURRENT_STATE_IDLE 0
#define CURRENT_STATE_READY 1
#define CURRENT_STATE_IDENT 2
#define CURRENT_STATE_STBY 3
#define CURRENT_STATE_TRAN 4
#define CURRENT_STATE_DATA 5
#define CURRENT_STATE_RCV 6
#define CURRENT_STATE_PRG 7
#define CURRENT_STATE_DIS 8

struct sd_card_status {
    uint32_t reserved           : 3;
    uint32_t AKE_SEQ_ERROR      : 1;
    uint32_t reserved_2         : 1;
    uint32_t APP_CMD            : 1;
    uint32_t reserved_3         : 2;
    uint32_t READY_FOR_DATA     : 1;
    uint32_t CURRENT_STATE      : 4;
    uint32_t ERASE_RESET        : 1;
    uint32_t CARD_ECC_DIABLED   : 1;
    uint32_t WP_ERASE_SKIP      : 1;
    uint32_t CSD_OVERWRITE      : 1;
    uint32_t reserved17         : 1;
    uint32_t reserved18         : 1;
    uint32_t ERROR              : 1;
    uint32_t CC_ERROR           : 1;
    uint32_t CARD_ECC_FAILED    : 1;
    uint32_t ILLEGAL_COMMAND    : 1;
    uint32_t COM_CRC_ERROR      : 1;
    uint32_t LOCK_UNLOCK_FAILED : 1;
    uint32_t CARD_IS_LOCKED     : 1;
    uint32_t WP_VIOLATION       : 1;
    uint32_t ERASE_PARAM        : 1;
    uint32_t ERASE_SEQ_ERROR    : 1;
    uint32_t BLOCK_LEN_ERROR    : 1;
    uint32_t ADDRESS_ERROR      : 1;
    uint32_t OUT_OF_RANGE       : 1;
};

enum sdio_card_type {CARD_NONE = 0, MMC, SD_V1, SD_V2_SC, SD_V2_HC};
                        // MMC not supported
struct sd_info {
    uint16_t rca;
    enum sdio_card_type type;
    uint32_t sector_count;
};

union sdio_status_conv {
    struct sd_card_status statusstruct;
    uint32_t value;
};

#endif /* FATFS_SHIMATTA_SDIO_DRIVER_SHIMATTA_SDIO_DRIVER_H_ */
