////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 
// 
// This file is part of the Microsoft .NET Micro Framework Porting Kit Code Samples and is unsupported. 
// Copyright (C) Microsoft Corporation. All rights reserved. Use of this sample source code is subject to 
// the terms of the Microsoft license agreement under which you licensed this sample source code. 
// 
// THIS SAMPLE CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, 
// INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// 
// 
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include <tinyhal.h>

//--//

#define SD_MANUFACTURER_CODE                 0xEC
#define SD_DEVICE_CODE                       0xDA
#define SD_BLOCK_COUNT                       2048*32
#define SD_SECTOR_PER_BLOCK                  16
#define SD_SECTOR_SIZE                       512
#define SD_BLOCK_SIZE                        SD_SECTOR_PER_BLOCK*SD_SECTOR_SIZE
#define SD_BLOCK_ERASE_TYPICAL_TIME_USEC     1500000
#define SD_SECTOR_WRITE_TYPICAL_TIME_USEC    200
#define SD_BLOCK_ERASE_MAX_TIME_USEC         2000000
#define SD_SECTOR_WRITE_MAX_TIME_USEC        700


#define SD_SIZE_IN_BYTES                   SD_BLOCK_SIZE*SD_BLOCK_COUNT
#define SD_WP_GPIO_PIN                     GPIO_PIN_NONE
#define SD_WP_ACTIVE                       FALSE


// BlockDeviceInformation

#define SD_REMOVEABLE                      TRUE
#define SD_SUPPORTXIP                      FALSE
#define SD_WRITEPROTECTED                  FALSE
#define SD_NUMREGIONS                      1


#define SD_LOW_VOLTAGE_FLAG      FALSE


#define MEMORY_BLOCKTYPE_SPECIAL  BlockRange::BLOCKTYPE_FILESYSTEM


struct SDD_BLOCK_CONFIG
{
    GPIO_FLAG WriteProtectionPin;
    BlockDeviceInfo *BlockDeviceInformation;
};
struct SDD_BL_CONFIGURATION
{
    SPI_CONFIGURATION   SPI;
    GPIO_PIN            InsertIsrPin;
    GPIO_PIN            EjectIsrPin;
    BOOL                State_After_Erase;
    BOOL                Low_Voltage_Flag;
    BlockStorageDevice* Device;
};

//--//

const BlockRange g_SD_BlockRange[] =
{
    { MEMORY_BLOCKTYPE_SPECIAL    ,  0,  SD_BLOCK_COUNT -1 }
};

const BlockRegionInfo  g_SD_BlkRegion[SD_NUMREGIONS] = 
{
	0,						// SectorAddress		Start;		// Starting Sector address
	SD_BLOCK_COUNT,				// UINT32			NumBlocks;		// total number of blocks in this region
	SD_BLOCK_SIZE,				// UINT16			BytesPerBlock;	// Number of Bytes in each block of this region	
	ARRAYSIZE_CONST_EXPR(g_SD_BlockRange),
	g_SD_BlockRange,
};

//--//

BlockDeviceInfo  g_SD_DeviceInfo=
{
    {  
        SD_REMOVEABLE,
        SD_SUPPORTXIP,
        SD_WRITEPROTECTED,
    },    
    SD_SECTOR_WRITE_MAX_TIME_USEC,		// UINT32 Duration_Max_WordWrite_uSec;
    SD_BLOCK_ERASE_MAX_TIME_USEC,		// UINT32 Duration_Max_SectorErase_uSec;
	SD_SECTOR_SIZE,						// UINT32 BytesPerSector;
	1024*1024*1024,                      // UINT32 Size;
	SD_NUMREGIONS,						// UINT32 NumRegions;
    g_SD_BlkRegion,						// const BlockRegionInfo* pRegions;
};

struct SDD_BLOCK_CONFIG g_SD_BL_Config =
{
	{
		SD_WP_GPIO_PIN,            // GPIO_PIN             Pin;
		SD_WP_ACTIVE,              // BOOL                 ActiveState;
	},
	&g_SD_DeviceInfo,             // BlockDeviceinfo
};

struct BlockStorageDevice g_SD_BL;

struct SDD_BL_CONFIGURATION g_SD_BL_Configuration =
{
	{// SPI_CONFIGURATION
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
    },
	0,
	0,
	TRUE,                   // State_After_Erase will be filled at initialization
	SD_LOW_VOLTAGE_FLAG,
	&g_SD_BL,
};