📄 sdcmd.c
字号:
/****************************************Copyright (c)**************************************************** Guangzhou ZHIYUAN electronics Co.,LTD.** ** http://www.zyinside.com****--------------File Info-------------------------------------------------------------------------------** File Name: sdcmd.c** Last modified Date: 2006.01.09** Last Version: V1.0 ** Description: sd/mmc 卡支持的命令(SD模式)** commands that sd/mmc card supported**------------------------------------------------------------------------------------------------------** Created By: MingYuan Zheng 郑明远** Created date: 2006.01.09** Version: V1.0** Descriptions: The original version 初始版本****------------------------------------------------------------------------------------------------------** Modified by:** Modified date:** Version:** Description:**********************************************************************************************************/#include "config.h"/*********************************************************************************************************** Function name: SD_SendCommand** Descriptions: send command to the card,and get a response** 向卡发送命令,并取得响应 ** Input: INT8U cmd : 命令字 INT8U cmd : command byte INT8U *param : 命令参数,长度为4字节 INT8U *param : command parameter,length is 4 bytes INT8U resptype: 响应类型 INT8U resptype: response type INT8U *resp : 响应,长度为0-17字节 INT8U *resp : response,length is 0-17 bytes** Output: 0: 正确 >0: 错误码 0: right >0: error code** Created by: MingYuan Zheng 郑明远 ** Created Date: 2006-01-09 **-------------------------------------------------------------------------------------------------------** Modified by:** Modified Date: **------------------------------------------------------------------------------------------------------********************************************************************************************************/INT8U SD_SendCommand(INT8U cmd, INT32U param, INT8U resptype, INT8U *resp){ INT32U resp_len, stat = 0; stat = SDICCON_START | ((cmd & 0x3F) | 0x40); /* 发送命令头和命令字 send command header and word */ SDICARG = param; /* 发送参数 send parameter */ resp_len = 0; switch (resptype) /* 根据不同的命令,得到不同的响应长度 */ { /* according various command,get the various response length */ case R0: resp_len = 0; break; case R1: case R1B: case R3: case R6: stat |= SDICCON_WRSP; /* SDICCON_WRSP:等待响应. SDICCON_WRSP: must wait for response */ resp_len = 6; break; case R2: stat |= SDICCON_WRSP; stat |= SDICCON_LRSP; /* SDICCON_LRSP:长响应. SDICCON_LRSP: long response */ resp_len = 17; break; default: return SD_ERR_CMD_RESPTYPE; /* 返回命令响应类型错误 return error of command response type */ break; } SDICCON = stat; /* 向SD/MMC卡发送命令 send command to sd/mmc card */ /* no response */ if (resp_len == 0) { do { /* recycle until the command have been sent */ stat = SDICSTA; if (!card_insert) { /* 卡被拔出 card is removed */ SDICSTA = stat; return SD_ERR_NO_CARD; } }while (!(stat & SDICSTA_SENT)); SDICSTA = stat; /* clear bits */ return SD_NO_ERR; /* 返回执行成功 return perform sucessfully */ } /* there is response */ while(1) { stat = SDICSTA; if (stat & SDICSTA_TOUT) { /* time is out */ SDICSTA = stat; /* clear bits */ return SD_ERR_CMD_TIMEOUT; /* 返回命令超时 return response timeout of command */ } if (stat & SDICSTA_RSP) /* 接收到响应 receive the response */ break; if (!card_insert) { /* 卡被拔出 card is removed */ SDICSTA = stat; return SD_ERR_NO_CARD; } } SDICSTA = stat; /* clear bits */ /* read the response */ if (resp_len == 6) { *resp = (INT8U)(SDIRSP1 >> 24); word_to_byte(SDIRSP0, resp + 1); } else { word_to_byte(SDIRSP0, resp + 12); word_to_byte(SDIRSP1, resp + 8); word_to_byte(SDIRSP2, resp + 4); word_to_byte(SDIRSP3, resp); } return SD_NO_ERR; /* 返回执行成功 return perform sucessfully */}/*********************************************************************************************************** Function name: word_to_byte** Descriptions: change 32bit value to bytes form** 将32位的参数转为字节形式 ** Input: INT32U value : 被转换的字 INT32U value : the word changed INT8U *pdst : 目标缓冲区,长度为4字节 INT8U *param : target buffer,length is 4 bytes** Output: NULL ** Created by: MingYuan Zheng 郑明远 ** Created Date: 2006-01-09 **-------------------------------------------------------------------------------------------------------** Modified by:** Modified Date: **------------------------------------------------------------------------------------------------------********************************************************************************************************/void word_to_byte(INT32U value, INT8U *pdst){ pdst[3] = (INT8U)(value >> 24); pdst[2] = (INT8U)(value >> 16); pdst[1] = (INT8U)(value >> 8); pdst[0] = (INT8U)(value);}/*********************************************************************************************************** Function name: SD_JudgeResult** Descriptions: Judge wheather response of R0 is right ** 向卡发送命令,并取得响应 ** Input: INT8U *CardStatus: R0中的Card Status域 INT8U *CardStatus: Card Status field of R0** Output: 0: 正确 >0: 错误码 0: right >0: error code** Created by: MingYuan Zheng 郑明远 ** Created Date: 2006-01-09 **-------------------------------------------------------------------------------------------------------** Modified by:** Modified Date: **------------------------------------------------------------------------------------------------------********************************************************************************************************/INT8U SD_JudgeResult(INT8U *CardStatus){ if ((CardStatus[0] & CARDSTATUS_ERR_MSK0) != 0) return SD_ERR_CMD_RESP; if ((CardStatus[1] & CARDSTATUS_ERR_MSK1) != 0) return SD_ERR_CMD_RESP; if ((CardStatus[2] & CARDSTATUS_ERR_MSK2) != 0) return SD_ERR_CMD_RESP; if ((CardStatus[3] & CARDSTATUS_ERR_MSK3) != 0) return SD_ERR_CMD_RESP; return SD_NO_ERR;}/*********************************************************************************************************** Function name: SD_ResetSD** Descriptions: Reset SD/MMC card** Input: NULL** Output: 0: right >0: error code** Created by: MingYuan Zheng 郑明远 ** Created Date: 2006-01-09 **-------------------------------------------------------------------------------------------------------** Modified by:** Modified Date: **------------------------------------------------------------------------------------------------------********************************************************************************************************/INT8U SD_ResetSD(void){ INT32U param = 0; INT8U resp; return (SD_SendCommand(CMD0, param, CMD0_R, &resp)); /* 复位命令 command that reset card */}/*********************************************************************************************************** Function name: SD_BlockCommand** Descriptions: command of block operation** 块命令** Input: INT8U cmd : 命令字 INT8U cmd : command byte INT8U resptype : 响应类型 INT8U resptype : response type INT32U parameter: 块操作参数 INT32U parameter: parameter of block operation** Output: 0: 正确 >0: 错误码 0: right >0: error code ** Created by: MingYuan Zheng 郑明远 ** Created Date: 2006-01-09 **-------------------------------------------------------------------------------------------------------** Modified by:** Modified Date: **------------------------------------------------------------------------------------------------------********************************************************************************************************/INT8U SD_BlockCommand(INT8U cmd, INT8U resptype, INT32U parameter){ INT8U response[6],ret; parameter <<= SD_BLOCKSIZE_NBITS; /* 调整地址:左移9位 adjust address: move 9 bits left */ ret = SD_SendCommand(cmd, parameter, resptype, response); if (ret != SD_NO_ERR) return ret; /* 结束数据传输失败 stop transmission operation fail */ return (SD_JudgeResult(&response[1])); /* 返回结果 return Result */}/*********************************************************************************************************** Function name: SD_ReadAllCID** Descriptions: read CID register of card** 读卡的CID寄存器** Input: INT8U cidlen : 寄存器长度(固定为16) INT8U cidlen : the length of register (fixed,is 16) INT8U *recbuf : 接收缓冲区 INT8U *recbuf : receive buffer ** Output: 0: 正确 >0: 错误码 Output: 0: right >0: error code** Created by: MingYuan Zheng 郑明远 ** Created Date: 2006-01-09 **-------------------------------------------------------------------------------------------------------** Modified by:** Modified Date: **------------------------------------------------------------------------------------------------------********************************************************************************************************/INT8U SD_ReadAllCID(INT8U cidlen, INT8U *recbuf){ INT32U param = 0; INT8U response[17],tmp; tmp = SD_SendCommand(CMD2, param, CMD2_R, response); /* 读CID寄存器命令 command that read CID register */ if (tmp != SD_NO_ERR) return tmp; for (tmp = 0; tmp < 16; tmp++) /* 从响应中取得CID的内容 get CID from response */ recbuf[tmp] = response[tmp]; return SD_NO_ERR;}/*********************************************************************************************************** Function name: SD_GetRCA** Descriptions: get RCA(relative address) of card** 获得卡的RCA(相对地址)** Input: INT16U *RCA : RCA接收缓冲(长度1) INPUT: INT16U *RCA : rec buffer of RCA(length: 1)** Output: 0: 正确 >0: 错误码 Output: 0: right >0: error code
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -