#ifndef _SD_MMC_H_
#define _SD_MMC_H_

#include "atmel_start.h"
#include "sd_mmc_intf.h"
#include "pdebug.h"
typedef enum sd_mmc_mode_t
{
	SD_MMC_MODE_SPI = 0,
	SD_MMC_MODE_MCI = 1
}sd_mmc_mode_t;

typedef enum sd_mmc_err_t
{
  SD_MMC_SUCCESS			= 0,
  SD_MMC_FAILURE			= 1,
  SD_MMC_INIT_ONGOING		= 2,
  SD_MMC_ERR_NO_CARD		= 3,
  SD_MMC_ERR_UNUSABLE		= 4,
  SD_MMC_ERR_SLOT			= 5,
  SD_MMC_ERR_COMM			= 6,
  SD_MMC_ERR_PARAM			= 7,
  SD_MMC_ERR_WP			= 8,
  SD_MMC_INIT_FAILURE		= 9,
  SD_MMC_TIMEOUT			= 10,
} sd_mmc_err_t;

// The card detect function can be unused, used regularly, or via interrupt.
// SD_MMC_CD_MODE_UNUSED: The cd pin isn't used
// SD_MMC_CD_MODE_SYNC: The pin is just checked when needed.
// SD_MMC_CD_MODE_ASYNC: The pin's state is stored and updated via interrupt. CURRENTLY UNIMPLEMENTED
typedef enum sd_mmc_cd_mode_t
{
	SD_MMC_CD_MODE_UNUSED = 0,
	SD_MMC_CD_MODE_SYNC = 1,
	SD_MMC_CD_MODE_ASYNC = 2
}sd_mmc_cd_mode_t;


sd_mmc_err_t sd_mmc_init(void);
sd_mmc_err_t sd_mmc_select(uint8_t slot, uint32_t clock, uint8_t bus_width, bool high_speed);
sd_mmc_err_t sd_mmc_deselect(uint8_t slot);
sd_mmc_err_t sd_mmc_send_command(uint8_t cmd, uint32_t arg, uint8_t crc);

#define PARAM_ERROR(x)			(x & 0b01000000)
#define ADDR_ERROR(x)			(x & 0b00100000)
#define ERASE_SEQ_ERROR(x)		(x & 0b00010000)
#define CRC_ERROR(x)			(x & 0b00001000)
#define ILLEGAL_CMD(x)			(x & 0b00000100)
#define ERASE_RESET(x)			(x & 0b00000010)
#define IN_IDLE(x)				(x & 0b00000001)

#define SD_MMC_CD_MODE SD_MMC_CD_MODE_SYNC

#ifndef SD_MMC_CD_MODE
#define SD_MMC_CD_MODE SD_MMC_MODE_UNUSED
#endif

#define CMD0		0
#define CMD0_ARG	0x00000000
#define CMD0_CRC	0x94

#define CMD8		8
#define CMD8_ARG	0x000001AA
#define CMD8_CRC	0x86

#define CMD58       58
#define CMD58_ARG   0x00000000
#define CMD58_CRC   0x00

#define UNUSED(v) (void)(v)

#define CMD_VER(X)          ((X >> 4) & 0xF0)
#define VOL_ACC(X)          (X & 0x1F)

#define VOLTAGE_ACC_27_33   0b00000001
#define VOLTAGE_ACC_LOW     0b00000010
#define VOLTAGE_ACC_RES1    0b00000100
#define VOLTAGE_ACC_RES2    0b00001000

#define POWER_UP_STATUS(X)  X & 0x40
#define CCS_VAL(X)          X & 0x40
#define VDD_2728(X)         X & 0b10000000
#define VDD_2829(X)         X & 0b00000001
#define VDD_2930(X)         X & 0b00000010
#define VDD_3031(X)         X & 0b00000100
#define VDD_3132(X)         X & 0b00001000
#define VDD_3233(X)         X & 0b00010000
#define VDD_3334(X)         X & 0b00100000
#define VDD_3435(X)         X & 0b01000000
#define VDD_3536(X)         X & 0b10000000

#endif
