/****************************************Copyright (c)**************************************************
** Guangzhou ZLG-MCU Development Co.,LTD.
** graduate school
** http://www.zlgmcu.com
**
**--------------File Info-------------------------------------------------------------------------------
** File name: sddriver.c
** Last modified Date: 2005-3-10
** Last Version: V2.0
** Descriptions: SD/MMC卡读写模块: 物理层 用户API函数
** Soft Packet of SD Card: user API funciton
**
**------------------------------------------------------------------------------------------------------
** Created by: Ming Yuan Zheng
** Created date: 2005-1-6
** Version: V1.0
** Descriptions: The original version
**
**------------------------------------------------------------------------------------------------------
** Modified by: Ming Yuan Zheng
** Modified date: 2005-3-10
** Version: V2.0
** Descriptions: 增加了对MMC卡的支持,增加了对UCOS-II的支持,使该模块不仅能运行于前后台系统,还可运行
** 于UCOS-II上
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
********************************************************************************************************/
#include "config.h"
#include "sdhal.h"
#include "SDCmd.h"
#if SD_UCOSII_EN
OS_EVENT *pSemSD = NULL; /* 卡访问权限信号量 semaphore that access card */
#endif
/* SD卡信息结构体变量 the information structure variable of SD Card */
sd_struct sds;
/* 超时时间单位表(单位:0.000000001ns) timeout unit table */
const INT32U time_unit[8] = {1000000000,100000000,10000000,
1000000,100000,10000,1000,100};
/* 超时时间表 timeout value table */
const INT8U time_value[16] = {0,10,12,13,15,20,25,30,
35,40,45,50,55,60,70,80};
/* 超时时间因数表 timeout factor table */
const INT8U r2w_fator[8] = {1,2,4,8,16,32,64,128};
/*
***************************************************************************************************
用户API函数: 初始化,读,写,擦 SD卡 User API Function: Initialize,read,write,erase SD Card
***************************************************************************************************
*/
/*******************************************************************************************************************
** 函数名称: INT8U SD_Initialize() Name: INT8U SD_Initialize()
** 功能描述: 初始化SD/MMC卡 Function: initialize SD/MMC card
** 输 入: 无 Input: NULL
** 输 出: 0: 成功 >0: 错误码 Output: 0: right >0: error code
********************************************************************************************************************/
INT8U SD_Initialize(void)
{
INT8U recbuf[4],ret;
#if SD_UCOSII_EN
if (pSemSD == NULL)
{
pSemSD = OSSemCreate(1); /* 创建访问SD/MMC卡信号量 create SD/MMC semaphore */
if (pSemSD == NULL)
return SD_ERR_CREATE_SEMSD;
}
#endif
SD_StartSD();
SD_HardWareInit(); /* 初始化读写SD卡的硬件条件 Initialize the hardware that access SD Card */
if (SD_ChkCard() != 1) /* 检查卡是否插入 check weather card is inserted */
{
ret = SD_ERR_NO_CARD;
goto SD_ERR;
}
SPI_CS_Assert(); /* 1. 置CS为低 assert CS */
SD_SPIDelay(25); /* 2. 至少延时 74 clock delay more than 74 clock */
SPI_CS_Deassert(); /* 3. 置CS为高 dessert CS */
SD_SPIDelay(2); /* 4. 延时2(8 clock) delay 2(8 clock) */
ret = SD_ResetSD(); /* 5. 发出CMDO命令复位SD卡 send CMD0 command to reset sd card */
if (ret != SD_NO_ERR)
goto SD_ERR;
ret = SD_ActiveInit(); /* 6. 激活卡进入初始化过程. active card initialize process */
if (ret != SD_NO_ERR)
goto SD_ERR;
ret = SD_ReadOCR(4, recbuf); /* 7. 读OCR寄存器,查询卡支持的电压值 read OCR register,get the supported voltage */
if (ret != SD_NO_ERR)
goto SD_ERR;
if ((recbuf[1] & MSK_OCR_33) != MSK_OCR_33)
{
ret = SD_ERR_VOL_NOTSUSP; /* 不支持3.3V,返回错误码 not support 3.3V,return error code */
goto SD_ERR;
}
SPI_ClkToMax(); /* 8. 设置SPI时钟到最大值 set SPI clock to maximum */
#if SD_CRC_EN
ret = SD_EnableCRC(1); /* 使能CRC校验 enable CRC check */
if (ret != SD_NO_ERR)
goto SD_ERR;
#endif
ret = SD_SetBlockLen(SD_BLOCKSIZE); /* 9. 设置块的长度: 512Bytes Set the block length: 512Bytes */
if (ret != SD_NO_ERR)
goto SD_ERR;
ret = SD_GetCardInfo(); /* 10. 读CSD寄存器,获取SD卡信息 read CSD register, get the information of SD card */
if (ret != SD_NO_ERR)
goto SD_ERR;
SD_EndSD();
return SD_NO_ERR; /* 初始化成功 initialize sucessfully */
SD_ERR:
SD_EndSD();
return ret;
}
/********************************************************************************************************************
** 函数名称: INT8U SD_ReadBlock() Name: INT8U SD_ReadBlock()
** 功能描述: 从SD/MMC卡中读一个块 Function: read a single block from SD/MMC card
** 输 入: INT32U blockaddr: 块地址 Input: INT32U blockaddr: address of block
INT8U *recbuf : 接收缓冲区,长度512Bytes INT8U *recbuf : the buffer of receive,length is 512Bytes
** 输 出: 0: 成功 >0: 错误码 Output: 0: right >0: error code
*********************************************************************************************************************/
INT8U SD_ReadBlock(INT32U blockaddr, INT8U *recbuf)
{
INT8U ret;
SD_StartSD(); /* 向OS申请访问卡信号量 request semaphore acessed SD/MMC to OS */
if (SD_ChkCard() != 1)
{
SD_EndSD();
return SD_ERR_NO_CARD; /* 卡没完全插入卡中 card is not inserted entirely */
}
if (blockaddr > sds.block_num)
{
SD_EndSD();
return SD_ERR_OVER_CARDRANGE; /* 操作超出卡容量范围 operate over the card range */
}
ret = SD_ReadSingleBlock(blockaddr); /* 读单块命令 read single blocks command */
if (ret != SD_NO_ERR)
{
SD_EndSD();
return ret;
}
ret = SD_ReadBlo
- 1
- 2
- 3
前往页