sdcard.h
来自「ARM7 lpc2132+vs1003+sdcard 制作的 MP3 此程序在」· C头文件 代码 · 共 417 行 · 第 1/2 页
H
417 行
/****************************************Copyright (c)***********************************************
** qsw
**
**
**--------------File Info----------------------------------------------------------------------------
** File name: sdhal.c
** Last modified Date: 2007.11.20
** Last Version: V1.0
** Descriptions: sd 卡驱动软件包: 硬件抽象层 ---- SPI1操作函数
** Soft Packet of SD Card Driver: hard abstrast layer ---- function of SPI1 operation
**
**------------------------------------------------------------------------------------------------
** Created by: qsw
** Created date: 2007.11.20
** Version: V1.0
** Descriptions: The original version
**
**------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
**************************************************************************************************/
#ifndef __SDCARD_H__
#define __SDCARD_H__
#define SPI1_INIT() PINSEL1 &= ~((0x03 << 2) + (0x03 << 4) + (0x03 << 6)); \
PINSEL1 |= (0x01 << 2) + (0x01 << 4) + (0x01 << 6);
//power p0.09
#define SD_POWER 0
#define SD_POWER_GPIO() 0
#define SD_POWER_OUT() 0
#define SD_POWER_OFF() 0
#define SD_POWER_ON() 0
//cs p0.20
#define SPI1_CS (0x01 << 20)
#define SPI1_CS_GPIO() PINSEL1 &= ~(0x03 << 8)
#define SPI1_CS_OUT() IO0DIR |= SPI1_CS
#define SPI1_CS_SET() IO0SET |= SPI1_CS
#define SPI1_CS_CLR() IO0CLR |= SPI1_CS
//sck p0.17
#define SPI1_SCK (0x01 << 17)
#define SPI1_SCK_GPIO() PINSEL1 &= ~(0x03 << 2)
#define SPI1_SCK_OUT() IO0DIR |= SPI1_SCK
#define SPI1_SCK_SET() IO0SET |= SPI1_SCK
#define SPI1_SCK_CLR() IO0CLR |= SPI1_SCK
//miso p0.18
#define SPI1_MISO (0x01 << 18)
#define SPI1_MISO_GPIO() PINSEL1 &= ~(0x03 << 4)
#define SPI1_MISO_OUT() IO0DIR &=~(SPI1_MISO)
#define SPI1_MISO_SET() IO0SET |= SPI1_MISO
#define SPI1_MISO_CLR() IO0CLR |= SPI1_MISO
//mosi p0.19
#define SPI1_MOSI (0x01 << 19)
#define SPI1_MOSI_GPIO() PINSEL1 &= ~(0x03 << 6)
#define SPI1_MOSI_OUT() IO0DIR |= SPI1_MOSI
#define SPI1_MOSI_SET() IO0SET |= SPI1_MOSI
#define SPI1_MOSI_CLR() IO0CLR |= SPI1_MOSI
//SD_IN p0.16
#define SD_INSERT 0
#define SD_INSERT_GPIO() 0
#define SD_INSERT_IN() 0
#define SD_INSERT_STATUS() 0
//SD_WP
#define SD_WP 0
#define SD_WP_GPIO() 0
#define SD_WP_IN() 0
#define SD_WP_STATUS() 0
/**************************************************************
用户API函数: 读, 写, 擦 SD卡 API函数及错误码
**************************************************************/
/* 错误码 error code */
#define SD_NO_ERR 0x00 // 函数执行成功
#define SD_ERR_NO_CARD 0x01 // SD卡没有完全插入到卡座中
#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 // 操作超出卡容量范围
/* SD命令可能返回的错误码 */
#define SD_ERR_CMD_RESPTYPE 0x10 // 命令类型错误
#define SD_ERR_CMD_TIMEOUT 0x11 // SD命令响应超时
#define SD_ERR_CMD_RESP 0x12 // SD命令响应错误
/* 数据流错误码 */
#define SD_ERR_DATA_CRC16 0x20 // 数据流CRC16校验不通过
#define SD_ERR_DATA_START_TOK 0x21 // 读单块或多块时,数据开始令牌不正确
#define SD_ERR_DATA_RESP 0x22 // 写单块或多块时,SD卡数据响应令牌不正确
/* 等待错误码 */
#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 // 初始化SD卡时,等待SD卡进入空闲状态超时错误
/* 写操作可能返回的错误码 */
#define SD_ERR_WRITE_BLK 0x40 // 写块数据错误
#define SD_ERR_WRITE_BLKNUMS 0x41 // 写多块时,想要写入的块与正确写入的块数不一致
#define SD_ERR_WRITE_PROTECT 0x42 // 卡外壳的写保护开关打在写保护位置
/**************************************************************
下面为软件包用到的与SD卡相关的宏定义
**************************************************************/
#define MSK_OCR_33 0xC0 /* Mask off the bits in the OCR corresponding to voltage range 3.2V to*/
/* 3.4V, OCR bits 20 and 21 */
#define SD_IDLE_WAIT_MAX 1000 /* 定义在初始化阶段,等待SD卡退出空闲状态的重试次数 */
/* Number of tries to wait for the card to go idle during initialization */
#define SD_CMD_TIMEOUT 100 /* SD卡命令超时时间(单位 8clock)*/
/* timeout of command */
#define READ_TIMEOUT_100MS 100 * SPI1_CLOCK / 1000 / 8 /* 100ms 相当的SPI01时钟数(单位 unit: 8 clocks) */
/* 100ms correspond to SPI01 clock(unit: 8 clocks)*/
#define WRITE_TIMEOUT_250MS 250 * SPI1_CLOCK / 1000 / 8 /* 250ms 相当的SPI01时钟数(单位 unit: 8 clocks) */
/* 250ms correspond to SPI01 clock(unit: 8 clocks)*/
/* CSD中一些域的字节位置(高字节在前) */
#define TAAC_POS 1 //TACC
#define NSAC_POS 2 //NSAC
#define READ_BL_LEN_POS 5 //READ_BL_LEN
#define C_SIZE_POS1 6 //C_SIZE upper 2-bit
#define C_SIZE_POS2 7 //C_SIZE middle 8-bit
#define C_SIZE_POS3 8 //C_SIZE lower 2-bit
#define C_SIZE_MULT_POS1 9 //C_SIZE_MULT upper 2-bit
#define C_SIZE_MULT_POS2 10 //C_SIZE_MULT lower 1-bit
#define SECTOR_SIZE_POS1 10 //SECTOR_SIZE upper 5-bit
#define SECTOR_SIZE_POS2 11 //SECTOR_SIZE lower 2-bit
#define R2WFACTOR_POS 12 //R2WFACTOR_POS
//CSD中一些域的掩码
#define TAAC_MSK 0x07 //TACC 域掩码
#define NSAC_MSK 0x78 //NSAC 域掩码
#define READ_BL_LEN_MSK 0x0F //READ_BL_LEN 的掩码
#define C_SIZE_MSK1 0x03 //C_SIZE 高2位掩码
#define C_SIZE_MSK3 0xC0 //C_SIZE 低2位掩码
#define C_SIZE_MULT_MSK1 0x03 //C_SIZE_MULT 的高2位掩码
#define C_SIZE_MULT_MSK2 0x80 //C_SIZE_MULT 的低2位掩码
#define R2WFACTOR_MSK 0x1C //R2WFACTOR 掩码
#define SECTOR_SIZE_MSK1 0x3F //SECTOR_SIZE 的高5位
#define SECTOR_SIZE_MSK2 0x80 //SECTOR_SIZE 的低2位
/********************************************************
SD 卡的相关命令与响应宏定义
********************************************************/
/* 命令响应定义 define command's response */
#define R1 1
#define R1B 2
#define R2 3
#define R3 4
/* 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
/* R2低字节错误码 lower byte of R2 error code */
#define MSK_TOK_ERROR 0x01
#define MSK_TOK_CC_ERROR 0x02
#define MSK_TOK_ECC_FAILED 0x04
#define MSK_TOK_CC_OUTOFRANGE 0x08
#define MSK_TOK_CC_LOCKED 0x10
/* 数据令牌 Data Tokens */
#define SD_TOK_READ_STARTBLOCK 0xFE
#define SD_TOK_WRITE_STARTBLOCK 0xFE
#define SD_TOK_READ_STARTBLOCK_M 0xFE
#define SD_TOK_WRITE_STARTBLOCK_M 0xFC
#define SD_TOK_STOP_MULTI 0xFD
/* 数据响应令牌 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_READREG_TIMEOUT 8
/**********************************************
SD卡SPI1模式下命令集
**********************************************/
/******************************** *************
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?