/****************************************Copyright (c)**************************************************
**
**
**
**
**
**
**--------------文件信息--------------------------------------------------------------------------------
**文 件 名: SD.c
**创 建 人: 李功周
**最后修改日期: 2008年10月23日
**描 述: SD卡驱动 API 函数
**
**--------------历史版本信息----------------------------------------------------------------------------
** 创建人: 李功周
** 版 本: v1.0
** 日 期: 2008年10月23日
** 描 述: 原始版本
**
**------------------------------------------------------------------------------------------------------
*/
#include <LPC2103.H>
//#include "../Uart1/Uart1.h"
/* SCK引脚 */
#define SPI_SCK ((unsigned int)0x01 << 4)
#define SPI_SCK_GPIO() PINSEL0 &= ~((unsigned int)0x03 << 8) /* 设置 SCK 口为GPIO口 */
#define SPI_SCK_OUT() IODIR |= SPI_SCK /* 设置 SCK 口为输出口 */
#define SPI_SCK_CLR() IOCLR = SPI_SCK /* 置 SCK 为低电平 */
/* MISO 引脚 */
#define SPI_MISO ((unsigned int)0x01 << 5)
#define SPI_MISO_GPIO() PINSEL0 &= ~((unsigned int)0x03 << 10) /* 设置 MISO 口为GPIO口 */
#define SPI_MISO_OUT() IODIR |= SPI_MISO /* 设置 MISO 口为输出口 */
#define SPI_MISO_CLR() IOCLR = SPI_MISO /* 置 MISO 为低电平 */
/* MOSI 引脚 */
#define SPI_MOSI ((unsigned int)0x01 << 6)
#define SPI_MOSI_GPIO() PINSEL0 &= ~((unsigned int)0x03 << 12) /* 设置 MOSI 口为GPIO口 */
#define SPI_MOSI_OUT() IODIR |= SPI_MOSI /* 设置 MOSI 口为输出口 */
#define SPI_MOSI_CLR() IOCLR = SPI_MOSI /* 置 MISO 为低电平 */
/* CS 引脚 */
#define SPI_CS ((unsigned int)0x01 << 30)
#define SPI_CS_GPIO() PINSEL1 &= ~((unsigned int)0x03 << 28) /* 设置 CS 口为GPIO口 */
#define SPI_CS_OUT() IODIR |= SPI_CS; /* 设置 CS 口为输出口 */
#define SPI_CS_SET() IOSET |= SPI_CS; /* 置 CS 为高电平 */
#define SPI_CS_CLR() IOCLR |= SPI_CS; /* 置 CS 为低电平 */
/* 卡完全插入卡座检测引脚 */
#define SD_INSERT ((unsigned int)0x01 << 31)
#define SD_INSERT_GPIO() PINSEL1 &= ~((unsigned int)0x03 << 30) /* 设置 INSERT 口为GPIO口 */
#define SD_INSERT_IN() IODIR &= ~SD_INSERT /* 设置 INSERT 口为输入口 */
#define SD_INSERT_STATUS() (IOPIN & SD_INSERT) /* 读取 INSERT 口的状态 */
/* 卡写保护检测引脚 */
#define SD_WP ((unsigned int)0x01 << 10)
#define SD_WP_GPIO() PINSEL0 &= ~((unsigned int)0x03 << 20) /* 设置 WP 口为GPIO口 */
#define SD_WP_IN() IODIR &= ~SD_WP /* 设置 WP 口为输入口 */
#define SD_WP_STATUS() (IOPIN & SD_WP) /* 读取 WP 口的状态 */
/* SD卡命令超时时间(单位 8clock)*/
/* timeout of command */
#define SD_CMD_TIMEOUT 100
/* 定义在初始化阶段,等待SD卡退出空闲状态的重试次数 */
#define SPI_CLOCK 5529600 /* 正常通信时,SPI时钟频率 frequency (Hz) */
#define SD_READREG_TIMEOUT 8 // 读寄存器的超时时间
#define SD_IDLE_WAIT_MAX 1000
#define READ_TIMEOUT_100MS 100 * SPI_CLOCK / 1000 / 8
/* 250ms 相当的SPI时钟数(单位 unit: 8 clocks) */
/* 250ms correspond to SPI clock(unit: 8 clocks)*/
#define WRITE_TIMEOUT_250MS 250 * SPI_CLOCK / 1000 / 8
/* 错误码 error code */
#define SD_NO_ERR 0x00 // 函数执行成功
#define SD_ERR_NO_CARD 0x01 // 卡没有完全插入到卡座中
#define SD_ERR_USER_PARAM 0x02 // 用户使用API函数时,入口参数有错误
#define SD_ERR_CARD_PARAM 0x03 // 卡中参数有错误(与本模块不兼容)
#define SD_ERR_VOL_NOTSUSP 0x04 // 卡不支持3.3V供电
#define SD_ERR_OVER_CARDRANGE 0x05 // 操作超出卡容量范围
#define SD_ERR_UNKNOWN_CARD 0x06 // 无法识别卡型
/* 等待错误码 */
#define SD_ERR_TIMEOUT_WAIT 0x30 // 写或擦操作时,发生超时错误
#define SD_ERR_TIMEOUT_READ 0x31 // 读操作超时错误
#define SD_ERR_TIMEOUT_WRITE 0x32 // 写操作超时错误
#define SD_ERR_TIMEOUT_ERASE 0x33 // 擦除操作超时错误
#define SD_ERR_TIMEOUT_WAITIDLE 0x34 // 初始化卡时,等待卡退出空闲状态超时错误
/* 数据流错误码 */
#define SD_ERR_DATA_CRC16 0x20 // 数据流CRC16校验不通过
#define SD_ERR_DATA_START_TOK 0x21 // 读单块或多块时,数据开始令牌不正确
#define SD_ERR_DATA_RESP 0x22 // 写单块或多块时,卡数据响应令牌不正确
/* Number of tries to wait for the card to go idle during initialization */
#define SD_IDLE_WAIT_MAX 1000
/* Number of tries to wait for the card to go idle during initialization */
#define SD_IDLE_WAIT_MAX 1000
/* R1和R2高字节错误码 R1 and upper byte of R2 error code */
#define MSK_IDLE 0x01
#define MSK_ERASE_RST 0x02
#define MSK_ILL_CMD 0x04
#define MSK_CRC_ERR 0x08
#define MSK_ERASE_SEQ_ERR 0x10
#define MSK_ADDR_ERR 0x20
#define MSK_PARAM_ERR 0x40
/* SD命令可能返回的错误码 */
#define SD_ERR_CMD_RESPTYPE 0x10 // 命令类型错误
#define SD_ERR_CMD_TIMEOUT 0x11 // 卡命令响应超时
#define SD_ERR_CMD_RESP 0x12 // 卡命令响应错误
/* 写操作可能返回的错误码 */
#define SD_ERR_WRITE_BLK 0x40 // 写块数据错误
#define SD_ERR_WRITE_BLKNUMS 0x41 // 写多块时,想要写入的块与正确写入的块数不一致
#define SD_ERR_WRITE_PROTECT 0x42 // 卡外壳的写保护开关打在写保护位置
/* 数据响应令牌 Data Response Tokens */
#define SD_RESP_DATA_MSK 0x0F //数据响应掩码
#define SD_RESP_DATA_ACCETPTED 0x05 //数据被接受
#define SD_RESP_DATA_REJECT_CRC 0x0B //由于CRC错误而被拒绝
#define SD_RESP_DATA_REJECT_WRITE 0x0D //由于写错误而被拒绝
/* 等待类型 Wait Type */
#define SD_WAIT_READ 0x00 //读等待
#define SD_WAIT_WRITE 0x01 //写等待
#define SD_WAIT_ERASE 0x02 //擦除等待
/* 应用操作错误 */
#define SD_WRITE_ERR 0xf0 //写SD错误
#define SD_READ_ERR 0xf1 //读SD卡错误
#define SD_Efficacy_ERR 0xf2 //效验错误
#define SD_BLOCKADDR_ERR 0xf3 //块地址不合法
#define SD_CRC_EN 0 /* 设置数据传输时是否使用CRC */
/*
*********************************************
SD卡SPI模式下命令集
*********************************************
*/
/* 命令响应定义 define command's response */
#define R1 1
#define R1B 2
#define R2 3
#define R3 4
/******************************** 基本命令集 Basic command set **************************/
/* 复位SD 卡 Reset cards to idle state */
#define CMD0 0
#define CMD0_R R1
/* 读OCR寄存器 Read the OCR (MMC mode, do not use for SD cards) */
#define CMD1 1
#define CMD1_R R1
/* 读CSD寄存器 Card sends the CSD */
#define CMD9 9
#define CMD9_R R1
/* 读CID寄存器 Card sends CID */
#define CMD10 10
#define CMD10_R R1
/* 停止读多块时的数据传输 Stop a multiple block (stream) read/write operation */
#define CMD12 12
#define CMD12_R R1B
/* 读 Card_Status 寄存器 Get the addressed card's status register */
#define CMD13 13
#define CMD13_R R2
/***************************** 块读命令集 Block read commands **************************/
/* 设置块的长度 Set the block length */
#define CMD16 16
#define CMD16_R R1
/* 读单块 Read a single block */
#define CMD17 17
#define CMD17_R R1
/* 读多块,直至主机发送CMD12为止 Read multiple blocks until a CMD12 */
#define CMD18 18
#define CMD18_R R1
/***************************** 块写命令集 Block write commands *************************/
/* 写单块 Write a block of the size selected with CMD16 */
#define CMD24 24
#define CMD24_R R1
/* 写多块 Multiple block write until a CMD12 */
#define CMD25 25
#define CMD25_R R1
/* 写CSD寄存器 Program the programmable bits of the CSD */
#define CMD27 27
#define CMD27_R R1
/***************************** 写保护 Write protection *****************************/
/* Set the write protection bit of
SD.rar_sd卡驱动
版权申诉
5星 · 超过95%的资源 135 浏览量
2022-09-21
05:03:46
上传
评论
收藏 14KB RAR 举报
小波思基
- 粉丝: 72
- 资源: 1万+
最新资源
- WebCrawler.scr
- 【计算机专业毕业设计】大学生就业信息管理系统设计源码.zip
- YOLO 数据集:8种路面缺陷病害检测【包含划分好的数据集、类别class文件、数据可视化脚本】
- JAVA实现Modbus RTU或Modbus TCPIP案例.zip
- 基于YOLOv8的FPS TPS AI自动锁定源码+使用步骤说明.zip
- JAVA实现Modbus RTU或Modbus TCPIP案例.zip
- 基于yolov8+streamlit的火灾检测部署源码+模型.zip
- 测试aaaaaaabbbbb
- VID20240521070643.mp4
- Android系统原理与开发学习要点详解-培训课件.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈