一.前言
本文分享IO模拟SPI操作SD卡系列之一:初始化过程,IO模拟SPI我们前面有分享且分享了一系列应用,参见
https://mp.weixin.qq.com/s/dA2QgUEezVY3EkVUK7MD7A
SD卡协议主要参考文件《SD Specifications Part 1 Physical Layer Specification Version 3.01 February 18, 2010 》的第七章”7. SPI Mode”。
二.初始化过程
2.1 SPI接口基本约定
使用MODE3即CLK空闲为高,第二个边沿(上升沿)开始采样。
MSB即高位在前,多字节时高字节在前。
2.2初始化过程
初始化过程参考规格书7.2.1如下示意,因为现在基本都是V2版本的卡,我们暂时只考虑V2版本即右边这部分,今后有需要再完善左边部分。
2.2.1 模式选择
上电默认SD为SD模式,如果CS拉低发送CMD0则进入SPI模式。
这里顺便提一下SD和SPI模式引脚的对应关系
DAT3 CS
CMD DI
CLK CLK
DAT0 DO
2.2.2上电
在CS拉低发送CMD之前0, 需要拉高CS发送一系列CLK来完成上电过程,发送的CLK数实测和具体的卡有关,有些卡发送8个CLK即可,一般是发送大于74个CLK,我们按照8个为单位发送80个。
如下所示
2.2.3 CMD0复位进入空闲
命令由6字节组成,起始位为0然后是一位1表示主机发送到设备,然后是6位命令索引,比如CMD0即索引为0,然后是32位参数,高字节在前高位在前。然后是7位CRC7,最后一位停止位1.
更详细的时序如下,主机拉低CS,在NCS后发送命令,在NCR之后卡回响应,CMD0的响应是R1,然后NEC后主机可以拉高CS
其中NCS等参数如下NCS和NEC可以为0.,NCR可以为1~8个字节,所以这里实现需要注意下,在等待响应时最多要设置等待8个字节,而不应该固定为一个字节。
设备响应R1,R的内容如下,所以如果CMD0响应OK,设备应该回0x01
具体的波形如下
6字节命令如下
40 00 00 00 00 95
设备在以1个字节(最多可能8字节)之后回了0x01
2.2.4 CMD8发送接口条件
CMD8,发送主机支持的电压信息,设备回R7含设备支持的电压信息。
发送命令参数
[11:8]为VHS,其参数含义如下,所以我们需要设置为0001b
[7:0]为check pattern,发多少设备收到后就回多少,我们一般设置为0xAA.
所以命令参数内容为0x000001AA。
卡回R7的内容如下,显示回r1,然后是32位的值,command version为0,
保留位为0,voltage accepted对应前面设置的VHS,正常设置为0001b接受就回0001b
Check pattern即前面的0xAA。
波形如下
发送命令
48 00 00 01 AA 87
一个字节后回
01 00 00 01 AA
2.2.5 CMD55-ACMD41激活卡
ACMD41用于发送容量支持信息,激活卡的初始化序列。激活进行中r1回0x01,否则回0x00。
参数的bit20,HCS设置为1表示主机支持大容量。这里需要注意要设置,否则大容量设备始终回0x01。
由于是A开头的命令,在该命令前需要发送CMD55
波形如下,激活过程中
CMD55的波形
发送命令
77 00 00 00 00 65
回r1
01
软件不断重复,直到卡回00
2.2.5 CMD58读OCR
主机发送CMD58读OCR寄存器值,命令参数为0
卡回R3,即先是r1,此时r1继续保持0x00,OCR的内容如下
波形如下
主机发
7A 00 00 00 00 FD
卡在一个字节后回
R1=0x00
然后是4字节OCR
C0 FF 80 00
即bit31=1表示power up是完成的
Bit30=1 表示CCS=1,即SDXC卡
Bit15~bit23都为1,对应的电压阈都支持。
2.2.9 CMD9读CSD获取容量信息
读CSD寄存器获取容量信息,命令参数为0,设备回r1+CSD数据块,此时r1应该继续保持0x00.
CSD内容如下, 版本1和版本2内容不一样,计算容量方式也不一样
波形如下
主机发送命令
49 00 00 00 00 AF
卡在1个字节后回r1=0x00
然后继续回16字节的CSD内容
注意SPI内容是按照数据块返回的,所以实际是开始包+CSD+CRC16即
0xFE + CSD + CRC16
FE 40 0E 00 32 53 59 00 00 1D E1 7F 80 0A 40 00 13 76 F5
三. 代码实现
我们还是按照面向对象思想,考虑可移植性设计。其中crc7.c/h用于实现命令的crc7计算,完全可移植无需修改。
Io_spi.c/h是io模拟spi实现完全无需修改,
Sd.c/h是sd卡操作实现完全无需修改,
Sd_itf.c/h是接口实现,仅需要实现io操作接口即可。
io_spi.c
#include "io_spi.h"
void io_spi_enable(io_spi_dev_st* dev)
{
if((dev != 0) && (dev->cs_write != 0) && (dev->sck_write != 0))
{
/* 准备空闲时的SCK状态,在CS拉低之前准备好 */
dev->sck_write((dev->mode & 0x02) >> 1);
if(dev->delay_pf != 0)
{
dev->delay_pf(dev->delayns);
}
/* 拉低CS */
dev->cs_write(0);
/* (5) SCK电平保持 */
//if(dev->delay_pf != 0)
//{
// dev->delay_pf(dev->delayns);
//}
}
}
void io_spi_disable(io_spi_dev_st* dev)
{
if((dev != 0) && (dev->cs_write != 0))
{
dev->cs_write(1);
}
}
/**
* _____ _____
* CS |_____________________________________________________________|
* _____________ _________
* SCK(CPOL=0) xx__________| |___ xxx __________| |__________
* __________ ____xxx __________ __________
* SCK(CPOL=1) xx |_____________| |_________|
* (0)
* (1)
* (2)
* (3)(4)
* (5)
* (6)(7)
* MISO ^ ^
* MOSI ^
* (1) (2) (4) (6)
* (3) (5)
* 其中()表示行为,^表示MOSI/MISO的输出或者采样位置.
* (0) io_spi_enable 准备SCK空闲状态,拉低CS.
* (1) 准备SCK初始状态,和(0)时SCK初始状态一样,代码中执行这个操作的目的仅仅是初始化局部变量cpol而已.
* (2) 输出MOSI数据.
* (3) 反转SCK产生第1个边沿.
* (4) 如果CPHA=0 则第1个边沿采样,MISO在此采样.
* (5) SCK高/低电平保持时间.
* (6) 反转SCK产生第2个边沿.
* (7) 如果CPHA=1 则第2个边沿采样,MISO在此采样.
*/
int io_spi_trans(io_spi_dev_st* dev, uint8_t* tx, uint8_t* rx, uint32_t size)
{
uint32_t i = 0; /* 字节数循环 */
uint8_t j = 0; /* 位数循环 */
uint8_t msb = 0; /* MSB标志 */
uint8_t cpha = 0; /* 相位标志bit0 */
uint8_t cpol = 0; /* 极性标志bit1 */
uint8_t rx_val = 0; /* 发送字节缓存 */
uint8_t tx_val = 0; /* 接收字节缓存 */
if(dev == 0)
{
return -1;
}
if((dev->miso_read == 0) || (dev->mosi_write == 0) || (dev->sck_write == 0))
{
/* dev->delay_pf 可以不实现 */
return -1;
}
cpha = dev->mode & 0x01;
msb = dev->msb;
/* (1) 准备空闲时的SCK状态 */
cpol = (dev->mode & 0x02) >> 1;
/* 这一句其实可以不用,和io_spi_enable效果一样,这里仅需要初始化cpol局部变量即可
* 加上这一句可以在此确保SCK引脚状态初始化,可靠性角度来说加上提高冗余.
*/
dev->sck_write(cpol);
for(i=0; i<size; i++)
{
/* 取待发送的值, 用户没有提供则发送0xFF */
if(tx != 0)
{
tx_val = *tx++;
}
else
{
tx_val = 0xFF;
}
/* 接收到的值初始化 */
rx_val = 0;
for(j=0 ;j<8; j++)
{
/* (2)对于发送,不管对方哪个边沿采样,都是都在第一个边沿之前准备好MOSI就行
* 如果对于对方第一个边沿采样,这里修改MOSI之后最好有个数据建立时间
*/
if(msb)
{
dev->mosi_write(tx_val & 0x80); /* 高位在前,先发送高位,未发送数据再往高位移动 */
tx_val <<= 0x1; /* 注意写的时候是先写后移位 */
}
else
{
dev->mosi_write(tx_val & 0x01); /* 低位在前,先发送低位,未发送数据再往高位移动 */
tx_val >>= 0x1;
}
/* (3)反转产生第1个CLK边沿 */
cpol ^= 0x01;
dev->sck_write(cpol);
if(rx != 0)
{
if(cpha == 0)
{
/* (4)第一个边沿采样 */
if(msb)
{
rx_val <<= 0x1; /* 注意读的时候是先移位后读 */
rx_val |= dev->miso_read(); /* 高位在前,先读到低位,已接收数据再往高位移动 */
}
else
{
rx_val >>= 0x1;
rx_val |= dev->miso_read() <<7; /* 低位在前,先读到高位,已接收数据再往低位移动 */
}
}
}
/* (5) SCK电平保持 */
if(dev->delay_pf != 0)
{
dev->delay_pf(dev->delayns);
}
/* (6)反转产生第2个CLK边沿 */
cpol ^= 0x01;
dev->sck_write(cpol);
if(rx != 0)
{
if(cpha == 1)
{
/* (7) 第2个边沿采样 */
if(msb)
{
rx_val <<= 0x1;
rx_val |= dev->miso_read(); /* 高位在前,先读到低位再往高位移动 */
}
else
{
rx_val >>= 0x1;
rx_val |= dev->miso_read()<<7; /* 低位在前,先读到高位再往低位移动 */
}
}
}
/* (5) SCK电平保持 */
if(dev->delay_pf != 0)
{
dev->delay_pf(dev->delayns);
}
}
/* 存储读到的值 */
if(rx != 0)
{
*rx++ = rx_val;
}
}
return 0;
}
void io_spi_init(io_spi_dev_st* dev)
{
if((dev != 0) && (dev->init != 0))
{
dev->init();
}
}
void io_spi_deinit(io_spi_dev_st* dev)
{
if((dev != 0) && (dev->deinit != 0))
{
dev->deinit();
}
}
io_spi.h
extern "C"{
typedef void (*io_spi_cs_write_pf)(uint8_t val); /**< CS写接口 */
typedef void (*io_spi_sck_write_pf)(uint8_t val); /**< SCK写接口 */
typedef void (*io_spi_mosi_write_pf)(uint8_t val); /**< MOSI写接口 */
typedef uint8_t (*io_spi_miso_read_pf)(void); /**< MISO读接口 */
typedef void (*io_spi_delay_ns_pf)(uint32_t delay); /**< 延时接口 */
typedef void (*io_spi_init_pf)(void); /**< 初始化接口 */
typedef void (*io_spi_deinit_pf)(void); /**< 解除初始化接口 */
/**
* \struct io_spi_dev_st
* 接口结构体
*/
typedef struct
{
io_spi_cs_write_pf cs_write; /**< cs写接口 */
io_spi_sck_write_pf sck_write; /**< sck写接口 */
io_spi_mosi_write_pf mosi_write; /**< mosi写接口 */
io_spi_miso_read_pf miso_read; /**< miso读接口 */
io_spi_delay_ns_pf delay_pf; /**< 延时接口 */
io_spi_init_pf init; /**< 初始化接口 */
io_spi_deinit_pf deinit; /**< 解除初始化接口 */
uint32_t delayns; /**< 延迟时间 */
uint8_t mode; /**< 模式0~3 bit0 CPHA bit1 CPOL */
uint8_t msb; /**< 1高位在前 否则低位在前 */
} io_spi_dev_st;
/**
* \fn io_spi_enable
* 发送CS使能信号,拉低CS
* \param[in] dev \ref io_spi_dev_st
*/
void io_spi_enable(io_spi_dev_st* dev);
/**
* \fn io_spi_disable
* 拉高CS,取消片选
* \param[in] dev \ref io_spi_dev_st
*/
void io_spi_disable(io_spi_dev_st* dev);
/**
* \fn io_spi_trans
* 传输,发送的同时读
* \param[in] dev \ref io_spi_dev_st
* \param[in] tx 待发送的数据 如果tx为空则默认发送FF
* \param[out] rx 存储接收的数据 如果rx为空则不读
* \param[in] size 传输的字节数
* \retval 0 读成功
* \retval -1 参数错误
*/
int io_spi_trans(io_spi_dev_st* dev, uint8_t* tx, uint8_t* rx, uint32_t size);
/**
* \fn io_spi_init
* 初始化
* \param[in] dev \ref io_spi_dev_st
*/
void io_spi_init(io_spi_dev_st* dev);
/**
* \fn io_spi_deinit
* 解除初始化
* \param[in] dev \ref io_spi_dev_st
*/
void io_spi_deinit(io_spi_dev_st* dev);
}
crc7.c
static uint8_t s_crc_table[256];
void crc7_gen_tbl(uint8_t* tbl)
{
int i, j;
uint8_t crcpoly = 0x89; /* the value of our CRC-7 polynomial */
/* generate a table value for all 256 possible byte values */
for (i = 0; i < 256; ++i)
{
tbl[i] = (i & 0x80) ? i ^ crcpoly : i;
for (j = 1; j < 8; ++j)
{
tbl[i] <<= 1;
if (tbl[i] & 0x80)
{
tbl[i] ^= crcpoly;
}
}
if(i%16==0)
{
printf("\r\n");
}
printf("0x%02x,",tbl[i]);
}
printf("\r\n");
}
static const uint8_t s_crc_table[256]=
{
0x00,0x09,0x12,0x1b,0x24,0x2d,0x36,0x3f,0x48,0x41,0x5a,0x53,0x6c,0x65,0x7e,0x77,
0x19,0x10,0x0b,0x02,0x3d,0x34,0x2f,0x26,0x51,0x58,0x43,0x4a,0x75,0x7c,0x67,0x6e,
0x32,0x3b,0x20,0x29,0x16,0x1f,0x04,0x0d,0x7a,0x73,0x68,0x61,0x5e,0x57,0x4c,0x45,
0x2b,0x22,0x39,0x30,0x0f,0x06,0x1d,0x14,0x63,0x6a,0x71,0x78,0x47,0x4e,0x55,0x5c,
0x64,0x6d,0x76,0x7f,0x40,0x49,0x52,0x5b,0x2c,0x25,0x3e,0x37,0x08,0x01,0x1a,0x13,
0x7d,0x74,0x6f,0x66,0x59,0x50,0x4b,0x42,0x35,0x3c,0x27,0x2e,0x11,0x18,0x03,0x0a,
0x56,0x5f,0x44,0x4d,0x72,0x7b,0x60,0x69,0x1e,0x17,0x0c,0x05,0x3a,0x33,0x28,0x21,
0x4f,0x46,0x5d,0x54,0x6b,0x62,0x79,0x70,0x07,0x0e,0x15,0x1c,0x23,0x2a,0x31,0x38,
0x41,0x48,0x53,0x5a,0x65,0x6c,0x77,0x7e,0x09,0x00,0x1b,0x12,0x2d,0x24,0x3f,0x36,
0x58,0x51,0x4a,0x43,0x7c,0x75,0x6e,0x67,0x10,0x19,0x02,0x0b,0x34,0x3d,0x26,0x2f,
0x73,0x7a,0x61,0x68,0x57,0x5e,0x45,0x4c,0x3b,0x32,0x29,0x20,0x1f,0x16,0x0d,0x04,
0x6a,0x63,0x78,0x71,0x4e,0x47,0x5c,0x55,0x22,0x2b,0x30,0x39,0x06,0x0f,0x14,0x1d,
0x25,0x2c,0x37,0x3e,0x01,0x08,0x13,0x1a,0x6d,0x64,0x7f,0x76,0x49,0x40,0x5b,0x52,
0x3c,0x35,0x2e,0x27,0x18,0x11,0x0a,0x03,0x74,0x7d,0x66,0x6f,0x50,0x59,0x42,0x4b,
0x17,0x1e,0x05,0x0c,0x33,0x3a,0x21,0x28,0x5f,0x56,0x4d,0x44,0x7b,0x72,0x69,0x60,
0x0e,0x07,0x1c,0x15,0x2a,0x23,0x38,0x31,0x46,0x4f,0x54,0x5d,0x62,0x6b,0x70,0x79,
};
uint8_t crc7(uint8_t* buffer, uint32_t length)
{
int i;
uint8_t crc = 0;
for (i = 0; i < length; ++i)
{
crc = s_crc_table[(crc << 1) ^ buffer[i]];
}
return crc;
}
/**
* CMD0 (Argument=0) --> 01 000000 00000000000000000000000000000000 "1001010" 1
* CMD17 (Argument=0) --> 01 010001 00000000000000000000000000000000 "0101010" 1
* Response of CMD17 --> 00 010001 00000000000000000000100100000000 "0110011" 1
*/
static uint8_t test_input[3][5] =
{
{0x40,0x00,0x00,0x00,0x00},
{0x51,0x00,0x00,0x00,0x00},
{0x11,0x00,0x00,0x09,0x00},
};
static uint8_t test_output[3] = {0x4A,0x2A,0x33};
int main(void)
{
crc7_gen_tbl(s_crc_table);
for(int i=0; i<3; i++)
{
uint8_t crc = crc7(test_input[i],5);
if(crc != test_output[i])
{
printf("crc err: cal:%x src:%x\r\n",crc,test_output[i]);
}
}
}
crc7.h
extern "C"{
uint8_t crc7(uint8_t* buffer, uint32_t length);
}
sd.c
#include "crc7.h"
#include "sd.h"
/**
* @fn sd_set_command
* 填充48位(6字节)的命令
* 位域 47 46 [45:40] [39:8] [7:1] 0
* 位宽 1 1 6 32 7 1
* 值 0 1 x x x 1
* 含义 起始位 传输方向位 命令索引 命令参数 CRC7 停止位
* @param[in] buffer 命令缓存
* @param[in] command_index 6位命令索引
* @param[in] argument 32位参数
*/
static void sd_set_command(uint8_t* buffer, uint8_t command_index, uint32_t argument)
{
if(buffer == (uint8_t*)0)
{
return;
}
buffer[0] = 0x40 | (command_index & 0x3F);
buffer[1] = (argument>>24)&0xFF;
buffer[2] = (argument>>16)&0xFF;
buffer[3] = (argument>>8)&0xFF;
buffer[4] = argument&0xFF;
buffer[5] = (crc7(buffer, 5)<<1) | 0x01;
}
static uint8_t sd_read_r1(sd_dev_st* dev)
{
uint8_t i=0;
uint8_t tmp = 0xFF;
uint8_t r1;
do
{
dev->transfer(&tmp,&r1,1);
i++;
if(i > 8)
{
break;
}
} while(r1 == 0xFF);
return r1;
}
static int sd_read_r3r7(sd_dev_st* dev, uint8_t* r3r7)
{
r3r7[0] = sd_read_r1(dev);
if(r3r7[0] > 1)
{
return -1;
}
dev->transfer(0,&(r3r7[1]),4);
return 0;
}
static int sd_read_ocr(sd_dev_st* dev, uint8_t* ocr)
{
uint8_t tmp = 0xFF;
int res = 0;
uint8_t cmd_buffer[6];
if(dev->en_extclk)
{
dev->transfer(&tmp,0,1);
}
dev->setcs(0);
if(dev->en_extclk)
{
dev->transfer(&tmp,0,1);
}
sd_set_command(cmd_buffer, 58, 0);
dev->transfer(cmd_buffer,0,6);
if(0 != sd_read_r3r7(dev, ocr))
{
res = -1;
}
if(dev->en_extclk)
{
dev->transfer(&tmp,0,1);
}
dev->setcs(1);
if(dev->en_extclk)
{
dev->transfer(&tmp,0,1);
}
return res;
}
static uint8_t sd_go_idle_state(sd_dev_st* dev)
{
uint8_t tmp = 0xFF;
uint8_t r1;
uint8_t cmd_buffer[6];
if(dev->en_extclk)
{
dev->transfer(&tmp,0,1);
}
dev->setcs(0);
if(dev->en_extclk)
{
dev->transfer(&tmp,0,1);
}
sd_set_command(cmd_buffer, 0, 0);
dev->transfer(cmd_buffer,0,6);
r1 = sd_read_r1(dev);
if(dev->en_extclk)
{
dev->transfer(&tmp,0,1);
}
dev->setcs(1);
if(dev->en_extclk)
{
dev->transfer(&tmp,0,1);
}
return r1;
}
static uint8_t sd_send_app(sd_dev_st* dev)
{
uint8_t tmp = 0xFF;
uint8_t r1;
uint8_t cmd_buffer[6];
if(dev->en_extclk)
{
dev->transfer(&tmp,0,1);
}
dev->setcs(0);
if(dev->en_extclk)
{
dev->transfer(&tmp,0,1);
}
sd_set_command(cmd_buffer, 55, 0);
dev->transfer(cmd_buffer,0,6);
r1 = sd_read_r1(dev);
if(dev->en_extclk)
{
dev->transfer(&tmp,0,1);
}
dev->setcs(1);
if(dev->en_extclk)
{
dev->transfer(&tmp,0,1);
}
return r1;
}
static uint8_t sd_send_op_cond(sd_dev_st* dev)
{
uint8_t tmp = 0xFF;
uint8_t r1;
uint8_t cmd_buffer[6];
if(dev->en_extclk)
{
dev->transfer(&tmp,0,1);
}
dev->setcs(0);
if(dev->en_extclk)
{
dev->transfer(&tmp,0,1);
}
sd_set_command(cmd_buffer, 41, 1u<<30);
dev->transfer(cmd_buffer,0,6);
r1 = sd_read_r1(dev);
if(dev->en_extclk)
{
dev->transfer(&tmp,0,1);
}
dev->setcs(1);
if(dev->en_extclk)
{
dev->transfer(&tmp,0,1);
}
return r1;
}
static int sd_send_if_cond(sd_dev_st* dev)
{
uint8_t tmp = 0xFF;
uint8_t r7[5];
uint8_t cmd_buffer[6];
int res = 0;
if(dev->en_extclk)
{
dev->transfer(&tmp,0,1);
}
dev->setcs(0);
if(dev->en_extclk)
{
dev->transfer(&tmp,0,1);
}
sd_set_command(cmd_buffer, 8, 0x1AA);
dev->transfer(cmd_buffer,0,6);
if(0 == sd_read_r3r7(dev, r7))
{
if((r7[0] != 0x01) || (r7[4] != 0xAA))
{
res = -2;
}
}
else
{
res = -1;
}
if(dev->en_extclk)
{
dev->transfer(&tmp,0,1);
}
dev->setcs(1);
if(dev->en_extclk)
{
dev->transfer(&tmp,0,1);
}
return res;
}
static int sd_read_csd(sd_dev_st* dev, uint8_t* buffer)
{
uint8_t tmp = 0xff;
uint8_t r1;
uint8_t token;
uint8_t crc[2];
uint8_t cmd_buffer[6];
int res = -1;
int timeout;
if(dev->en_extclk)
{
dev->transfer(&tmp,0,1);
}
dev->setcs(0);
if(dev->en_extclk)
{
dev->transfer(&tmp,0,1);
}
sd_set_command(cmd_buffer, 9, 0);
dev->transfer(cmd_buffer,0,6); /* 6字节命令 */
r1 = sd_read_r1(dev);
if(r1 != 0xFF)
{
timeout = 100;
token = 0xFF;
do
{
dev->transfer(0,&token,1);
if(token == 0xFF)
{
dev->delayms(1);
}
} while(((timeout--) > 0) && (token == 0xFF));
if(token == 0xFE)
{
dev->transfer(0,buffer,16);
dev->transfer(0,crc,2);
res = 0;
}
}
if(dev->en_extclk)
{
dev->transfer(&tmp,0,1);
}
dev->setcs(1);
if(dev->en_extclk)
{
dev->transfer(&tmp,0,1);
}
return res;
}
int sd_init(sd_dev_st* dev)
{
uint8_t r1;
uint8_t tmp = 0xFF;
uint8_t ocr[5];
if(dev == (sd_dev_st*)0)
{
return -1;
}
if(dev->init != 0)
{
dev->init();
}
dev->setcs(1);
dev->delayms(1);
for(int i=0; i<10; i++)
{
dev->transfer(&tmp,0,1); /* 不同卡需要的CLK数不一样,所以发10*8个 */
}
int res;
int retry = 10;
do
{
r1 = sd_go_idle_state(dev);
} while(((retry --) >0) && (r1 != 0x01));
if(r1 != 0x01)
{
return -2;
}
if(0 != sd_send_if_cond(dev))
{
return -3;
}
retry = 10;
do
{
r1 = sd_send_app(dev);
if(r1 < 2)
{
r1 = sd_send_op_cond(dev);
}
if(r1 != 0)
{
dev->delayms(100);
}
}
while((r1 != 0) && ((retry--)>0));
if(r1 != 0)
{
return -4;
}
res = sd_read_ocr(dev,ocr);
if(res != 0)
{
return -5;
}
if((ocr[1] & 0x80) == 0)
{
return -6;
}
dev->ccs = ((ocr[1] & 0x40) != 0) ? 1 : 0;
if(0 != sd_read_csd(dev,dev->csd))
{
return -7;
}
uint64_t c_size;
if(dev->ccs == 0)
{
uint32_t c_size_mult;
uint32_t read_bl_len;
c_size = (uint32_t)((dev->csd[8]>>6)*0x03) | ((uint32_t)(dev->csd[7])<<2) | ((uint32_t)(dev->csd[6] & 0x03)<<10);
c_size_mult = ((dev->csd[10]>>7)&0x01) | ((dev->csd[9] & 0x03)<<1);
read_bl_len = (dev->csd[5])&0x0F;
c_size = ((c_size+1)<<(read_bl_len))<<(c_size_mult+2);
}
else
{
c_size = (uint32_t)(dev->csd[9]) | ((uint32_t)(dev->csd[8])<<8) | ((uint32_t)(dev->csd[7] & 0x3F)<<16);
c_size = (c_size+1) * (uint64_t)512*1024;
}
dev->cap = c_size;
return 0;
}
int sd_deinit(sd_dev_st* dev)
{
if(dev == (sd_dev_st*)0)
{
return -1;
}
if(dev->deinit != 0)
{
return dev->deinit();
}
return 0;
}
sd.h
extern "C"{
typedef int (*sd_port_init_pf)(void); /**< 初始化接口 */
typedef int (*sd_port_deinit_pf)(void); /**< 解除初始化接口 */
typedef int (*sd_port_transfer_pf)(uint8_t* out, uint8_t* in, uint32_t len); /**< 传输一笔数据 */
typedef int (*sd_port_setcs_pf)(uint8_t cs); /**< CS控制 */
typedef int (*sd_port_delayms_pf)(uint32_t ms); /**< ms延时 */
/**
* \struct sd_dev_st
* 设备接口结构体
*/
typedef struct
{
sd_port_init_pf init; /**< 需要用户实现的初始化接口 */
sd_port_deinit_pf deinit; /**< 需要用户实现的解除初始化接口 */
sd_port_transfer_pf transfer; /**< 需要用户实现的发送-接收一笔数据 */
sd_port_setcs_pf setcs; /**< 需要用户实现的CS控制 */
sd_port_delayms_pf delayms; /**< 需要用户实现的ms延时 */
uint8_t ccs; /**< ACMD41响应(R3 OCR的bit30) CCS=0表示SDSC卡,CCS=1表示SDHC或SDXC卡, 初始化时更新 */
uint8_t en_extclk; /**< 是否在CS拉低前后,拉高前后额外产生8个CLK */
uint8_t csd[16]; /**< 初始化时获取到的CSD寄存器值 */
uint64_t cap; /**< 初始化时获取到的容量值,单位字节 */
} sd_dev_st;
/**
* @fn sd_init
* 初始化SD
* @param[in] dev @ref sd_dev_st 指向设备
* @return 0:成功 其他值:失败
*/
int sd_init(sd_dev_st* dev);
/**
* @fn sd_deinit
* 解除初始化SD
* @param[in] dev @ref sd_dev_st 指向设备
* @return 0:成功 其他值:失败
*/
int sd_deinit(sd_dev_st* dev);
sd_itf.h
extern "C"{
int sd_itf_init(void);
}
sd_itf.c
/**
* 引脚定义
*/
/**
* IO模拟SPI接口
*/
static void port_io_spi_cs_write(uint8_t val)
{
gpio_write(TF_DAT3_PIN,val);
}
static void port_io_spi_sck_write(uint8_t val)
{
gpio_write(TF_CLK_PIN,val);
}
static void port_io_spi_mosi_write(uint8_t val)
{
gpio_write(TF_CMD_PIN,val);
}
static uint8_t port_io_spi_miso_read(void)
{
return gpio_read(TF_DAT0_PIN);
}
static void port_io_spi_init(void)
{
gpio_open(TF_PWR_PIN, GPIO_DIRECTION_OUTPUT);
gpio_set_pull_mode(TF_PWR_PIN, GPIO_PULL_UP);
gpio_write(TF_PWR_PIN,0);
gpio_open(TF_DAT3_PIN, GPIO_DIRECTION_OUTPUT);
gpio_set_pull_mode(TF_DAT3_PIN, GPIO_PULL_UP);
gpio_write(TF_DAT3_PIN,1);
gpio_open(TF_CLK_PIN, GPIO_DIRECTION_OUTPUT);
gpio_set_pull_mode(TF_CLK_PIN, GPIO_PULL_UP);
gpio_write(TF_CLK_PIN,1);
gpio_open(TF_CMD_PIN, GPIO_DIRECTION_OUTPUT);
gpio_set_pull_mode(TF_CMD_PIN, GPIO_PULL_UP);
gpio_write(TF_CMD_PIN,1);
gpio_open(TF_DAT0_PIN, GPIO_DIRECTION_INPUT);
gpio_set_pull_mode(TF_DAT0_PIN, GPIO_PULL_UP);
}
static void port_io_spi_deinit(void)
{
gpio_close(TF_PWR_PIN);
gpio_close(TF_DAT3_PIN);
gpio_close(TF_CLK_PIN);
gpio_close(TF_CMD_PIN);
gpio_close(TF_DAT0_PIN);
}
/* IO模拟SPI设备实例 */
static io_spi_dev_st s_io_spi_dev =
{
.cs_write = port_io_spi_cs_write,
.sck_write = port_io_spi_sck_write,
.mosi_write = port_io_spi_mosi_write,
.miso_read = port_io_spi_miso_read,
.delay_pf = 0,
.init = port_io_spi_init,
.deinit = port_io_spi_deinit,
.delayns = 1,
.mode = 3,
.msb = 1,
};
static int port_sd_init(void)
{
io_spi_init(&s_io_spi_dev);
return 0;
}
static int port_sd_deinit(void)
{
io_spi_deinit(&s_io_spi_dev);
return 0;
}
static int port_sd_transfer(uint8_t* out, uint8_t* in, uint32_t len)
{
//io_spi_enable(&s_io_spi_dev);
io_spi_trans(&s_io_spi_dev,out,in,len);
//io_spi_disable(&s_io_spi_dev);
return 0;
}
static int port_sd_setcs(uint8_t cs)
{
if(cs)
{
io_spi_disable(&s_io_spi_dev);
}
else
{
io_spi_enable(&s_io_spi_dev);
}
return 0;
}
static int port_sd_delayms(uint32_t ms)
{
os_delay(ms);
return 0;
}
static sd_dev_st s_sd_dev =
{
.init = port_sd_init,
.deinit = port_sd_deinit,
.transfer = port_sd_transfer,
.setcs = port_sd_setcs,
.delayms = port_sd_delayms,
.en_extclk = 0,
};
int sd_itf_init(void)
{
static uint8_t s_read_buffer[2][512];
/* CD插入检测 */
gpio_open(TF_CD_PIN, GPIO_DIRECTION_INPUT);
gpio_set_pull_mode(TF_CD_PIN,GPIO_PULL_UP);
os_delay(10);
if(0 == sd_init(&s_sd_dev))
{
}
return 0;
}
四. 测试
调用sd_init(&s_sd_dev)查看是否正确初始化完成,使用逻辑分析仪抓取波形看是否正确。
这里分享一份波形,使用DsView软件打开。
通过百度网盘分享的文件:IO模拟读写TF卡.dsl
链接:https://pan.baidu.com/s/14CjyG3xgOBSZ2p4cmTRSxA?pwd=nm7n
提取码:nm7n
五. 总结
注意上电CS拉高之前需要发送CLK
回r1可能是1~8个字节之后,不要固定为1个字节
ACMD41之前要发CMD55表示后面命令的类型,注意参数bit30要设置为1.