⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sd_bsd.c

📁 可以在ARM单片机上移植的SD卡的驱动程序的原码
💻 C
字号:
/******************************************************************************
 ** File Name:      sd_bsd.c                                                                *
 ** Author:         Benjamin.Wang                                             *
 ** DATE:           07/11/2005                                                *
 ** Copyright:      2005 Spreadtrum, Incoporated. All Rights Reserved.         *
 ** Description:    SDCard Driver virtual interface layer.      *
 ******************************************************************************

 ******************************************************************************
 **                        Edit History                                       *
 ** ------------------------------------------------------------------------- *
 ** DATE           NAME             DESCRIPTION                               *
 ** 07/11/2005     Benjamin.Wang    Create.                                   *
 ******************************************************************************/

/**---------------------------------------------------------------------------*
 **                         Dependencies                                      *
 **---------------------------------------------------------------------------*/ 
#include "os_api.h"
#include "bsd.h"
#include "sd.h"
#include "sd_bsd.h"
//xingyun.he test
#include "XSR_Partition.h"
//#include "STL.h"

/**---------------------------------------------------------------------------*
 **                         Debugging Flag                                    *
 **---------------------------------------------------------------------------*/
#define DEBUG_SD

#ifdef DEBUG_SD
#define SD_PRINT( _format_string )   SCI_TRACE_LOW _format_string 
#else
#define SD_PRINT( _format_string )  
#endif
 
/**---------------------------------------------------------------------------*
 **                         Compiler Flag                                     *
 **---------------------------------------------------------------------------*/
#ifdef   __cplusplus
    extern   "C" 
    {
#endif
/**---------------------------------------------------------------------------*
 **                         Macro Definition                                   *
 **---------------------------------------------------------------------------*/
#define READ_BLOCK_SIZE 512

#define SD_BLOCK_SIZE     512
/**---------------------------------------------------------------------------*
 **                         Global Variables                                  *
 **---------------------------------------------------------------------------*/

//SD_CARD_T* SDCardPartitionVector[MAX_PARTITION] = {NULL, NULL, NULL};
SD_ERROR_CALLBACK g_sdcard_error_callback = NULL;

/**---------------------------------------------------------------------------*
 **                         Constant Variables                                *
 **---------------------------------------------------------------------------*/

/**---------------------------------------------------------------------------*
 **                     Local Function Prototypes                             *
 **---------------------------------------------------------------------------*/
LOCAL BSD_STATUS SD_ReadSector(     // return result. BSD_SUCCESS - success
                                                                    //                 BSD_FAILURE - failure
    //uint32 uiPartitionID,   // the partition id occupied by SDCard
    BSD_DEVICE *hDev,
    uint32 uiOffset,        // the block number
    uint32 uiNumber,    // the read counts
    void* pucBuf       // read buffer
    );

LOCAL BSD_STATUS SD_WriteSector(   // return result. BSD_SUCCESS - success
                                                    //                 BSD_FAILURE - failure
    //uint32 uiPartitionID,       // the partition id occupied by SDCard
    BSD_DEVICE *hDev,
    uint32 uiOffset,            // the block number
    uint32 uiNumber,        // the writecounts
    const void* pucBuf           // write data pointer
    );

LOCAL BSD_STATUS SD_DeleteSector(                  // return result. BSD_SUCCESS - success
                                                                    //                 BSD_FAILURE - failure
    //uint32 uiPartitionID,       // the partition id occupied by SDCard
    BSD_DEVICE *hDev,
    uint32 uiOffset,            //  the block number
    uint32 uiNumber         // erased counts
    );

/**---------------------------------------------------------------------------*
 **                         Function Definitions                              *
 **---------------------------------------------------------------------------*/
/*****************************************************************************/
//  Description:    This function is used to register BSD operation.
//  Author:         Benjamin.Wang
//  Note:           
/*****************************************************************************/
PUBLIC BOOLEAN SD_RegBSD(       // return result. SCI_TRUE - success, SCI_FALSE - failure
    SD_CARD_T* ptSDCard,         // SD card vector
    SD_ERROR_CALLBACK pfSDErrorCallback         //error callback function
    )
{
    BSD_DEVICE dev;

    if (ptSDCard == NULL)
    {
        SCI_ASSERT("SDCARD null!");
    }
    if (SD_Initialize(ptSDCard) != SD_SUCCESS)
    {
        return SCI_FALSE;
    }
    
    SD_SetBlockLength(ptSDCard, SD_BLOCK_SIZE);
    
    SCI_MEMSET(&dev, 0x00, sizeof(dev));;
    strcpy(dev.Name, STL_SD_DEVICE_NAME);
    dev.Type = BSD_SD;
    dev.Attribute = BSD_DEVICE_NORMAL;
    dev.SectorSize = SD_BLOCK_SIZE; //sdcard block size
    dev.EraseUnitSize = 1;
    dev.TotScts = SD_GetDeviceSize(ptSDCard) 
        * (SD_GetBlockLength(ptSDCard) / SD_BLOCK_SIZE);//sdcard total size of block
    dev.Offset = 0;//partition start address
    dev.Read = SD_ReadSector;
    dev.Write = SD_WriteSector;
    dev.Erase = SD_DeleteSector;
    dev.DeviceExtension = ptSDCard;
    
    //Registry the SD operation in BSD
    if(BSD_SUCCESS == BSD_AddDevice(&dev))
    {
        g_sdcard_error_callback = pfSDErrorCallback;
        return SCI_TRUE;
    }
    return SCI_FALSE;
}

/*****************************************************************************/
//  Description:    This function is used to unregister BSD operation.
//  Author:         Xingyun.he
//  Note:           
/*****************************************************************************/
PUBLIC BOOLEAN SD_UNRegBSD(       // return result. SCI_TRUE - success, SCI_FALSE - failure
    SD_CARD_T* ptSDCard)
{ 
	if (ptSDCard == NULL)
    {
        SCI_ASSERT("SDCARD null!");
    }		
	if (BSD_SUCCESS == BSD_RemoveDevice(STL_SD_DEVICE_NAME, TRUE))
	{
		return SCI_TRUE;
	}
	return SCI_FALSE;
}        

/*****************************************************************************/
//  Description:    This function is used to read sectors from sdcard.
//  Author:         Benjamin.Wang
//  Note:           
/*****************************************************************************/
BSD_STATUS SD_ReadSector(     // return result. BSD_SUCCESS - success
                                                                    //                 BSD_FAILURE - failure
    BSD_DEVICE *hDev,   // device occupied by SDCard
    uint32 uiOffset,        // the block number
    uint32 uiNumber,    // the read counts
    void* pucBuf       // read buffer
    )
{
    uint32 uiErr = SD_SUCCESS;
    int32 i;
    uint8* data_buf_ptr = (uint8*)pucBuf;
    uint32 read_block = uiOffset;

    if(NULL == hDev->DeviceExtension)
    {
        SD_PRINT(("SDCard interface hasn't been registered in BSD!"));
        return BSD_ERROR;
    }

    if (NULL == g_sdcard_error_callback)
    {
        return BSD_ERROR;
    }
    
    for(i = 0; (i < uiNumber) && (uiErr == SD_SUCCESS); i++)
    {
        uiErr = SD_ReadBlock(hDev->DeviceExtension, read_block, data_buf_ptr, READ_BLOCK_SIZE);
        data_buf_ptr += READ_BLOCK_SIZE;
        read_block += 1;
    }
    /*
    if(uiNumber == 1)
    {
        uiErr = SD_ReadBlock(hDev->DeviceExtension, uiOffset, pucBuf, READ_BLOCK_SIZE);
    }
    else
    {
        uiErr = SD_ReadMultiBlock(hDev->DeviceExtension, uiOffset, uiNumber, pucBuf, READ_BLOCK_SIZE);
    }
    */
    
    if(uiErr != SD_SUCCESS)
    {
        SD_PRINT(("SD Read Failure: %d, number:%d, offset:%d, i:%d\r\n", uiErr, uiNumber, uiOffset, i));
        (*g_sdcard_error_callback)(hDev->DeviceExtension, uiErr);
        return BSD_ERROR;
    }

    return BSD_SUCCESS;    
}

/*****************************************************************************/
//  Description:    This function is used to write sectors to SDCard.
//  Author:         Benjamin.Wang
//  Note:           
/*****************************************************************************/
BSD_STATUS SD_WriteSector(   // return result. BSD_SUCCESS - success
                                                    //                 BSD_FAILURE - failure
    BSD_DEVICE *hDev,       // the partition id occupied by SDCard
    uint32 uiOffset,            // the block number
    uint32 uiNumber,        // the writecounts
    const void* pucBuf           // write data pointer
    )
{
    uint32 uiErr = SD_SUCCESS;
    int32 i;
    uint8* data_buf_ptr = (uint8*)pucBuf;
    uint32 write_block = uiOffset;

    if(NULL == hDev->DeviceExtension)
    {
        SD_PRINT(("SDCard interface hasn't been registered in BSD!"));
        return BSD_ERROR;
    }

    if (NULL == g_sdcard_error_callback)
    {
        return BSD_ERROR;
    }
    
    for(i = 0; (i < uiNumber) && (uiErr == SD_SUCCESS); i++)
    {
        uiErr = SD_WriteBlock(hDev->DeviceExtension, write_block, data_buf_ptr);
        data_buf_ptr += SD_BLOCK_SIZE;
        write_block += 1;
    }
    
    /*
    if(uiNumber == 1)
    {
        uiErr = SD_WriteBlock(hDev->DeviceExtension, uiOffset, pucBuf);
    }
    else
    {
        uiErr = SD_WriteMultiBlock(hDev->DeviceExtension, uiOffset, uiNumber, pucBuf);
    }
    */

    if(uiErr != SD_SUCCESS)
    {
        SD_PRINT(("SD Write Failure: %d, number:%d, offset:%d\r\n", uiErr, uiNumber, uiOffset));
        (*g_sdcard_error_callback)(hDev->DeviceExtension, uiErr);
        return BSD_ERROR;
    }

    return BSD_SUCCESS;
}

/*****************************************************************************/
//  Description:    This function is used to erase sectors.
//  Author:         Benjamin.Wang
//  Note:           
/*****************************************************************************/
BSD_STATUS SD_DeleteSector(                  // return result. BSD_SUCCESS - success
                                                                    //                 BSD_FAILURE - failure
    BSD_DEVICE *hDev,       // the partition id occupied by SDCard
    uint32 uiOffset,            //  the block number
    uint32 uiNumber         // erased counts
    )
{
    uint32 uiErr;

    if(NULL == hDev->DeviceExtension)
    {
        SD_PRINT(("SDCard interface hasn't been registered in BSD!"));
        return BSD_ERROR;
    }

    if (NULL == g_sdcard_error_callback)
    {
        return BSD_ERROR;
    }
    
    uiErr = SD_Erase(hDev->DeviceExtension, uiOffset, uiOffset + uiNumber - 1);

    if(uiErr != SD_SUCCESS)
    {
        SD_PRINT(("SDCard Erase Failure!"));
        (*g_sdcard_error_callback)(hDev->DeviceExtension, uiErr);
        return BSD_ERROR;
    }

    return BSD_SUCCESS;
}


/*****************************************************************************/
//  Description:    This function is used to get partition information.
//  Author:         Benjamin.Wang
//  Note:           
/*****************************************************************************/
BSD_STATUS SD_GetPartitionInf(       // return result. BSD_SUCCESS - success
                                                            //                 BSD_FAILURE - failure
    BSD_DEVICE *hDev,               // the partition id occupied by SDCard
    BSD_PARTITION * ptPartitionInf     // output infor
    )
{
    strcpy(ptPartitionInf->Name, STL_SD_DEVICE_NAME);
    ptPartitionInf->Index= 0; 
    ptPartitionInf->Offset = 0;
    ptPartitionInf->AttrMask = 0;

    ptPartitionInf->TotScts = SD_GetDeviceSize(hDev->DeviceExtension) 
        * (SD_GetBlockLength(hDev->DeviceExtension) / SD_BLOCK_SIZE);
    return BSD_SUCCESS;
}
//for test
void  sd_err_handle(SD_CARD_T* ptSDCard, uint32 uiError)
{
}

/**---------------------------------------------------------------------------*
 **                         Local Function Definitions                              *
 **---------------------------------------------------------------------------*/

/**---------------------------------------------------------------------------*
 **                         Compiler Flag                                     *
 **---------------------------------------------------------------------------*/
#ifdef   __cplusplus
    }
    
#endif  // End of sd_bsd.c

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -