当前位置:   article > 正文

FatFs R0.15文件系统移植到MCU平台详细笔记经验教程_fatfs文件系统移植

fatfs文件系统移植

0、准备工作

        在移植FatFs文件系统前,需要准备好一块开发板,和一张SD卡,且需要已经实现开发板正常的读写SD卡或其它硬件设备。

        本文笔记教程中使用的硬件设备是STM32F407VET6开发板(板载SD插槽),配备8G和32G SD卡用于移植测试;文件系统版本为FatFs R0.15。

1、FatFs加入工程

(1)、下载FatFs文件系统

        进入FatFs官网,根据需要下载FatFs,在写本笔记教程时,最新版本为FatFs R0.15,因此下载使用了最新版本的R0.15进行移植适配。 

FatFs - Generic FAT Filesystem Module (elm-chan.org)icon-default.png?t=N7T8http://elm-chan.org/fsw/ff/00index_e.html

(2)、FatFs文件结构

        在解压下载了FatFs R0.15库文件后,会有如下三个文件夹和文件documents、source、LICENSE.txt;其中documents可以当作是一个官方的参考手册、说明书来看,里面有对文件系统的各个API的使用进行介绍说明,参考Demo等;source文件夹则是整个文件系统的核心所在,包含了文件系统的全部源文件和头文件。

        进入source文件夹后,可以看到如下几个文件,在这些文件中diskio.h、ff.c、ff.h、ffsystem.c和ffunicode.c是不需要更改的;diskio.c这个文件与磁盘底层IO操作接近,其包含了底层存储介质的操作函数,这些函数需要用户自己实现,主要是添加底层驱动函数;ffconf.h这个头文件包含了对FatFs功能配置的宏定义,通过修改这些宏定义就可以裁剪FatFs的功能。ffconf.h和diskio.c是移植FatFs的重点,是后面根据实际情况需要进行修改的文件。

(3)、FatFs库加入MDK工程

①、拷贝源码

        在MDK工程目录下新建一个FatFs文件夹,将下载的FatFs R0.15库文件中的source目录下的相关代码文件全部拷贝到该目录下,具体操作结果如下图所示。

②、工程包含FatFs

        在MDK KEIL v5中打开工程,新建一个FatFs工程组,并将FatFs库路径下的全部源文件加到工程组中,具体操作步骤如下图所示。

在MDK的头文件路径设置中,将FatFs路径加入到MDK工程中,具体操作如下图所示。

        完成上述操作,对整个工程代码进行编译后,出现了如下13个报错信息,通过定位报错信息可以得知,主要是由于文件系统diskio.c中对MMC、RAM、USB设备的初始化、读、写、状态获取API未定义和ff.c中对文件时间戳未定义

        因此,接下来对文件系统的移植操作主要是针对报错信息进行处理,将diskio.c文件中的硬件设备初始化、读、写、状态获取API及ff.c文件时间戳进行适配、定义实现。

3、移植FatFs文件系统

(1)、文件系统时间戳实现

        从上面的编译报错信息中,可以看到在与ff.c文件中,get_fattime( )函数没有被定义。get_fattime函数用于获取当前时间戳,在ff.c文件中被调用。

        打开ff.h文件可以看到,get_fattime( )接口是需要用户自己定义适配的。FatFs在文件创建、被修改时会记录时间, 调用这个函数,因此这个接口的重要性毋庸置疑,需要去实现。

打开ff.c文件,在该文件的最后,加入类似下面的代码,对FatFs的文件系统时间戳进行实现。

具体文件系统时间戳返回值格式为:

bit31:25 ——从1980至今是多少年,范围是 (0..127) ;

bit24:21 ——月份,范围为 (1..12) ;

bit20:16 ——该月份中的第几日,范围为(1..31) ;

bit15:11——时,范围为 (0..23);

bit10:5 ——分,范围为 (0..59);

bit4:0 ——秒/ 2,范围为 (0..29)。

        如果还没有掌握RTC时钟的配置,可以暂时拷贝下面的代码直接粘贴到程序中,但这样每次对文件系统操作,都是一个固定的时间值。

  1. __weak DWORD get_fattime(void)
  2. {
  3. /* 返回当前时间戳 */
  4. return ((DWORD)(2024 - 1980) << 25) /* Year 2024 */
  5. | ((DWORD)5 << 21) /* Month 5 */
  6. | ((DWORD)30 << 16) /* Mday 30 */
  7. | ((DWORD)17 << 11) /* Hour 0 */
  8. | ((DWORD)20 << 5) /* Min 0 */
  9. | ((DWORD)0 >> 1); /* Sec 0 */
  10. }

        如果已经掌握了RTC时钟的配置,可以直接将上面所示接口的固定值,进行替换为RTC时钟实际值,这样每次调用这个接口,都会将设备的实时时间传递到文件系统中,请根据实际情况进行修改,前提是已经开启了RTC时钟。

  1. __weak DWORD get_fattime(void)
  2. {
  3. RTC_Update(); //更新RTC时钟
  4. return ((DWORD)(xRTC.Year - 1980) << 25) /* 年 */
  5. | ((DWORD)xRTC.Month << 21) /* 月 */
  6. | ((DWORD)xRTC.Day << 16) /* 日 */
  7. | ((DWORD)xRTC.Hour << 11) /* 时 */
  8. | ((DWORD)xRTC.Minute << 5) /* 分 */
  9. | ((DWORD)xRTC.Second >> 1); /* 秒 */
  10. }

(2)、diskio.c修改适配

打开diskio.c文件,在包含头文件代码下面,可以看到有三个物理设备的类型编号的宏定义,根据实际的使用情况,这边手动修改了这三个宏定义,如下所示:

  1. #define DEV_SD_CARD 0 //SD Card
  2. #define DEV_SPI_FLASH 1 //SPI FLASH
  3. #define DEV_USB 2 //USB
①、disk_status

        这个函数的功能是获取设备的状态,只有一个参数pdrv,表示物理编号。因暂时只使用了SD卡,因此除了SD卡返回正常状态,其它类型设备全部返回的是非正常状态。

  1. DSTATUS disk_status (
  2. BYTE pdrv /* Physical drive nmuber to identify the drive */
  3. )
  4. {
  5. DSTATUS stat;
  6. //int result;
  7. switch (pdrv)
  8. {
  9. case DEV_SD_CARD :
  10. //result = MMC_disk_status();
  11. stat = RES_OK;
  12. // translate the reslut code here
  13. return stat;
  14. case DEV_SPI_FLASH :
  15. //result = RAM_disk_status();
  16. // translate the reslut code here
  17. stat = STA_NOINIT;
  18. return stat;
  19. case DEV_USB :
  20. //result = USB_disk_status();
  21. // translate the reslut code here
  22. stat = STA_NOINIT;
  23. return stat;
  24. }
  25. return STA_NOINIT;
  26. }
②、disk_initialize

        disk_initialize函数是设备初始化接口,也是有一个参数pdrv,用来指定设备物理编号。因只有一个SD设备,除了SD状态中调用了SD_Init()对SD卡进行了初始化设置,其它类型全部返回了未初始化。提醒:请根据实际情况进行修改SD卡初始化的接口,SD_Init这个接口主要是对芯片引脚GPIO初始化配置以及SDIO的通信参数配置等操作。

  1. DSTATUS disk_initialize (
  2. BYTE pdrv /* Physical drive nmuber to identify the drive */
  3. )
  4. {
  5. DSTATUS stat;
  6. //int result;
  7. switch (pdrv)
  8. {
  9. case DEV_SD_CARD :
  10. //result = MMC_disk_initialize();
  11. if(SD_Init() == SD_OK) //SD卡初始化
  12. {
  13. stat = RES_OK;
  14. }else{
  15. stat = STA_NOINIT;
  16. }
  17. // translate the reslut code here
  18. return stat;
  19. case DEV_SPI_FLASH :
  20. //result = RAM_disk_initialize();
  21. // translate the reslut code here
  22. stat = STA_NOINIT;
  23. return stat;
  24. case DEV_USB :
  25. //result = USB_disk_initialize();
  26. // translate the reslut code here
  27. stat = STA_NOINIT;
  28. return stat;
  29. }
  30. return STA_NOINIT;
  31. }
③、disk_read

        disk_read函数有四个形参。pdrv为设备物理编号。buff是一个BYTE类型指针变量,buff指向用来存放读取到数据的存储区首地址。 sector是一个DWORD类型变量,指定要读取数据的扇区首地址。count是一个UINT类型变量,指定扇区数量。

        BYTE类型实际是unsigned char类型,DWORD类型实际是unsigned long类型, UINT类型实际是 unsigned int类型,类型定义在ff.h文件中。

        SD_ReadDisk( )为SD设备驱动程序中提供的,不同的驱动代码,需要参考实际设备驱动中的代码程序进行相应的修改。

  1. DRESULT disk_read (
  2. BYTE pdrv, /* Physical drive nmuber to identify the drive */
  3. BYTE *buff, /* Data buffer to store read data */
  4. LBA_t sector, /* Start sector in LBA */
  5. UINT count /* Number of sectors to read */
  6. )
  7. {
  8. DRESULT res;
  9. int result;
  10. switch (pdrv) {
  11. case DEV_SD_CARD :
  12. // translate the arguments here
  13. //result = MMC_disk_read(buff, sector, count);
  14. // translate the reslut code here
  15. result = SD_ReadDisk(buff, sector, count);
  16. if(result != 0)
  17. {
  18. res = RES_PARERR;
  19. }else{
  20. res = RES_OK;
  21. }
  22. return res;
  23. case DEV_SPI_FLASH :
  24. // translate the arguments here
  25. //result = RAM_disk_read(buff, sector, count);
  26. // translate the reslut code here
  27. res = RES_PARERR;
  28. return res;
  29. case DEV_USB :
  30. // translate the arguments here
  31. //result = USB_disk_read(buff, sector, count);
  32. // translate the reslut code here
  33. res = RES_PARERR;
  34. return res;
  35. }
  36. return RES_PARERR;
  37. }
④、disk_write

        disk_write函数有四个形参,pdrv为设备物理编号。buff指向待写入扇区数据的首地址。sector,指定要读取数据的扇区首地址。 count指定扇区数量。

  1. DRESULT disk_write (
  2. BYTE pdrv, /* Physical drive nmuber to identify the drive */
  3. const BYTE *buff, /* Data to be written */
  4. LBA_t sector, /* Start sector in LBA */
  5. UINT count /* Number of sectors to write */
  6. )
  7. {
  8. DRESULT res;
  9. int result;
  10. switch (pdrv) {
  11. case DEV_SD_CARD :
  12. // translate the arguments here
  13. //result = MMC_disk_write(buff, sector, count);
  14. // translate the reslut code here
  15. result = SD_WriteDisk((u8 *)buff, sector, count);
  16. if(result != 0)
  17. {
  18. res = RES_PARERR;
  19. }else{
  20. res = RES_OK;
  21. }
  22. return res;
  23. case DEV_SPI_FLASH :
  24. // translate the arguments here
  25. //result = RAM_disk_write(buff, sector, count);
  26. // translate the reslut code here
  27. res = RES_PARERR;
  28. return res;
  29. case DEV_USB :
  30. // translate the arguments here
  31. //result = USB_disk_write(buff, sector, count);
  32. // translate the reslut code here
  33. res = RES_PARERR;
  34. return res;
  35. }
  36. return RES_PARERR;
  37. }
⑤、disk_ioctl

        disk_ioctl函数有三个形参,pdrv为设备物理编号,cmd为控制指令,包括发出同步信号、获取扇区数目、获取扇区大小、 获取擦除块数量等等指令,buff为指令对应的数据指针。

        对于SD卡,为支持格式化功能,需要用到获取扇区数量(GET_SECTOR_COUNT)指令和获取块尺寸(GET_BLOCK_SIZE)。另外,SD卡扇区大小为512字节, 串行Flash芯片一般设置扇区大小为4096字节,所以需要用到获取扇区大小(GET_SECTOR_SIZE)指令。

  1. DRESULT disk_ioctl (
  2. BYTE pdrv, /* Physical drive nmuber (0..) */
  3. BYTE cmd, /* Control code */
  4. void *buff /* Buffer to send/receive control data */
  5. )
  6. {
  7. DRESULT res;
  8. //int result;
  9. switch (pdrv)
  10. {
  11. case DEV_SD_CARD :
  12. // Process of the command for the MMC/SD card
  13. switch (cmd)
  14. {
  15. case GET_SECTOR_COUNT: /* 扇区数量 */
  16. *(DWORD * )buff = SDCardInfo.CardCapacity/SDCardInfo.CardBlockSize;
  17. break;
  18. case GET_SECTOR_SIZE : /* 扇区大小 */
  19. *(WORD * )buff = SDCardInfo.CardBlockSize;
  20. break;
  21. case GET_BLOCK_SIZE : /* 同时擦除扇区个数(单位为扇区) */
  22. *(DWORD * )buff = 1;
  23. break;
  24. }
  25. res = RES_OK;
  26. return res;
  27. case DEV_SPI_FLASH :
  28. // Process of the command for the RAM drive
  29. res = RES_PARERR;
  30. return res;
  31. case DEV_USB :
  32. // Process of the command the USB drive
  33. res = RES_PARERR;
  34. return res;
  35. }
  36. return RES_PARERR;
  37. }

(3)、FatFs系统配置

        ffconf.h文件是FatFs功能配置文件,通过修改该文件的宏定义开关,可以实现对FatFs的阉割、裁剪等配置,以符合开发者的实际需求。本文的移植测试中,修改了如下几个宏定义。

  1. #define FF_USE_MKFS 1
  2. #define FF_CODE_PAGE 936
  3. #define FF_USE_LFN 2
  4. #define FF_VOLUMES 3
  5. #define FF_MIN_SS 512
  6. #define FF_MAX_SS 4096

1) FF_USE_MKFS: 格式化功能选择,为使用FatFs格式化功能,需要把它设置为1。

2) FF_CODE_PAGE: 语言功能选择,并要求把相关语言文件添加到工程宏。为支持简体中文文件名需要使用“936”

3) FF_USE_LFN: 长文件名支持,默认不支持长文件名,这里配置为2,支持长文件名,并指定使用栈空间为缓冲区。

4) FF_VOLUMES: 指定物理设备数量,这里设置为3,包括预留SD卡、SPI Flash芯片和USB设备。

5) FF_MIN_SS FF_MAX_SS: 指定扇区大小的最小值和最大值。SD卡扇区大小一般都为512字节,SPI Flash芯片扇区大小一般设置为4096字节,所以需要把_MAX_SS改为4096。

4、文件系统功能验证

(1)、验证程序

  1. static FATFS fs; /* FatFs文件系统对象 */
  2. static FIL fnew; /* 文件对象 */
  3. static FRESULT res_sd; /* 文件操作结果 */
  4. static UINT fnum; /* 文件成功读写数量 */
  5. static BYTE ReadBuffer[256]= {0}; /* 读缓冲区 */
  6. static BYTE WriteBuffer[] = /* 写缓冲区*/
  7. "Welcome to STM32 FatFs, This is a test file!";
  8. void sd_card_fatfs_test(void)
  9. {
  10. printf("%s\t%d\r\n", __FUNCTION__ , __LINE__);
  11. res_sd = f_mount(&fs,"0:", 1);
  12. printf("%s\t%d\r\n", __FUNCTION__ , __LINE__);
  13. /*----------------------- 格式化测试 -----------------------*/
  14. /* 如果没有文件系统就格式化创建创建文件系统 */
  15. if (res_sd == FR_NO_FILESYSTEM)
  16. {
  17. printf(">> SD Card don't have FatFs Type File System!\r\nCreate SD Card FAT32 File System...\r\nPlease Wait some time!\r\n");
  18. /* 格式化 */
  19. //res_sd=f_mkfs("0:", 0, NULL, 0);
  20. MKFS_PARM opt = {0};
  21. opt.fmt = FM_FAT32; // 选择FAT32格式
  22. opt.n_fat = 2; // 设定FAT副本数量,通常为2
  23. opt.align = 1; // 数据区域对齐,默认为1,表示按扇区对齐
  24. // opt.n_root 不需要设置,因为我们使用的是FAT32,它没有固定大小的根目录
  25. opt.au_size = 512; // 每簇大小,以字节为单位,这里是假设每簇为1个扇区,即512字节
  26. //res_sd = f_mkfs("0:", &opt, NULL, 0);
  27. #define FORMAT_WORK_BUF_SZ 4096 // 根据需要调整
  28. BYTE formatWorkBuf[FORMAT_WORK_BUF_SZ];
  29. res_sd = f_mkfs("0:", &opt, formatWorkBuf, FORMAT_WORK_BUF_SZ);
  30. //res_sd = f_mkfs("0:", &opt, NULL, 4096); //根据实际需求调整
  31. if (res_sd == FR_OK)
  32. {
  33. printf(">> SD Card Create File System success! \r\n");
  34. /* 格式化后,先取消挂载 */
  35. res_sd = f_mount(NULL,"0:",0);
  36. /* 重新挂载 */
  37. res_sd = f_mount(&fs,"0:",1);
  38. } else {
  39. LED_RED_ON;
  40. printf(">> Create file system failed! %d \r\n", res_sd);
  41. while (1);
  42. }
  43. } else if (res_sd!=FR_OK) {
  44. printf(">> SD Card Mount File System failed! %d \r\n",res_sd);
  45. while (1);
  46. } else {
  47. printf(">> Mount file system success! \r\n");
  48. }
  49. /*----------------------- 文件系统测试:写测试 -----------------------*/
  50. printf("\r\n---------------FatFs Write Test---------------\r\n");
  51. /* 打开文件,如果文件不存在则创建它 */
  52. res_sd = f_open(&fnew, "0:FatFs_test.txt",FA_CREATE_ALWAYS | FA_WRITE );
  53. if ( res_sd == FR_OK )
  54. {
  55. printf(">> Open Or Create file success, write data...\r\n");
  56. /* 将指定存储区内容写入到文件内 */
  57. res_sd=f_write(&fnew,WriteBuffer,sizeof(WriteBuffer),&fnum);
  58. if(res_sd==FR_OK)
  59. {
  60. printf(">> Write data Success:%d\r\n",fnum);
  61. printf(">> Write data:\r\n%s\r\n",WriteBuffer);
  62. }else{
  63. printf("Write data failed! (%d)\r\n",res_sd);
  64. }
  65. /* 不再读写,关闭文件 */
  66. f_close(&fnew);
  67. }else{
  68. LED_RED_ON;
  69. printf("Open Or Create file failed!\r\n");
  70. }
  71. /*----------------------- 文件系统测试:读测试 -----------------------*/
  72. printf("--------------- Read file test ---------------\r\n");
  73. res_sd = f_open(&fnew, "0:FatFs_test.txt", FA_OPEN_EXISTING | FA_READ);
  74. if(res_sd == FR_OK)
  75. {
  76. LED_BLUE_ON;
  77. printf(">> Open success...\r\n");
  78. res_sd = f_read(&fnew, ReadBuffer, sizeof(ReadBuffer), &fnum);
  79. if(res_sd==FR_OK)
  80. {
  81. printf(">>Read file data success%d\r\n",fnum);
  82. printf(">>Read data:\r\n%s \r\n", ReadBuffer);
  83. }else{
  84. printf("Read file failed! (%d)\r\n",res_sd);
  85. }
  86. }else{
  87. LED_RED_ON;
  88. printf("Open file failed!\r\n");
  89. }
  90. /* 不再读写,关闭文件 */
  91. f_close(&fnew);
  92. /* 不再使用文件系统,取消挂载文件系统 */
  93. f_mount(NULL,"0:",1);
  94. while(1)
  95. {
  96. }
  97. }

(2)、验证结果

        32G的SD卡格式化过程中,可能需要较多的时间。

        将SD卡插入到PC机中,可以正常识别SD卡及打开查看修改里面的文件,说明文件系统移植成功。

(3)踩坑小提示

        FatFs R0.15版本的API与前几代版本的变动较大,使用起来甚至完全不一样,如果参考了以前几个版本的代码,移植运行会出错的话,建议查看程序源码,挨个调试验证。

        如f_mkfs函数接口,按照以往的版本,如下使用是可以正常格式化SD卡的文件系统的:

f_mkfs("0:", 0, NULL, 0);//参考老版本的常见的使用例程,错误的使用方式

        但R0.15版本中会直接返回FR_NOT_ENOUGH_CORE空间不足的提示,这并非全部是设备的堆空间或栈空间不足,使用上面这一行代码,在启动文件中将堆空间和栈空间变大后,还是出现该问题,经过查看源码发现,只是调用f_mkfs接口时,传参错误,修正后,就可以正常格式化文件系统。

        如下为查阅源码后编写的R0.15版本的正确使用方式:

  1. //正确的使用方式
  2. MKFS_PARM opt = {0};
  3. opt.fmt = FM_FAT32; // 选择FAT32格式
  4. opt.n_fat = 2; // 设定FAT副本数量,通常为2
  5. opt.align = 1; // 数据区域对齐,默认为1,表示按扇区对齐
  6. // opt.n_root 不需要设置,因为我们使用的是FAT32,它没有固定大小的根目录
  7. opt.au_size = 512; // 每簇大小,以字节为单位,这里是假设每簇为1个扇区,即512字节
  8. #define FORMAT_WORK_BUF_SZ 4096 // 根据需要调整
  9. BYTE formatWorkBuf[FORMAT_WORK_BUF_SZ];
  10. res_sd = f_mkfs("0:", &opt, formatWorkBuf, FORMAT_WORK_BUF_SZ);

5、SDIO驱动程序参考

(1)、sdio_sdcard.h

  1. #ifndef __SDIO_SDCARD_H
  2. #define __SDIO_SDCARD_H
  3. #include "stm32f4xx.h"
  4. //
  5. //SDIO 驱动代码
  6. //STM32F4工程模板-库函数版本
  7. //********************************************************************************
  8. //修改说明
  9. //V1.1 20140522
  10. //1,加入超时判断,解决轮询接收死机的问题.
  11. //V1.2 20140715
  12. //1,新增SD_GetState和SD_SendStatus函数.
  13. //
  14. //SDIO相关标志位,拷贝自:stm32f4xx_sdio.h
  15. #define SDIO_FLAG_CCRCFAIL ((uint32_t)0x00000001)
  16. #define SDIO_FLAG_DCRCFAIL ((uint32_t)0x00000002)
  17. #define SDIO_FLAG_CTIMEOUT ((uint32_t)0x00000004)
  18. #define SDIO_FLAG_DTIMEOUT ((uint32_t)0x00000008)
  19. #define SDIO_FLAG_TXUNDERR ((uint32_t)0x00000010)
  20. #define SDIO_FLAG_RXOVERR ((uint32_t)0x00000020)
  21. #define SDIO_FLAG_CMDREND ((uint32_t)0x00000040)
  22. #define SDIO_FLAG_CMDSENT ((uint32_t)0x00000080)
  23. #define SDIO_FLAG_DATAEND ((uint32_t)0x00000100)
  24. #define SDIO_FLAG_STBITERR ((uint32_t)0x00000200)
  25. #define SDIO_FLAG_DBCKEND ((uint32_t)0x00000400)
  26. #define SDIO_FLAG_CMDACT ((uint32_t)0x00000800)
  27. #define SDIO_FLAG_TXACT ((uint32_t)0x00001000)
  28. #define SDIO_FLAG_RXACT ((uint32_t)0x00002000)
  29. #define SDIO_FLAG_TXFIFOHE ((uint32_t)0x00004000)
  30. #define SDIO_FLAG_RXFIFOHF ((uint32_t)0x00008000)
  31. #define SDIO_FLAG_TXFIFOF ((uint32_t)0x00010000)
  32. #define SDIO_FLAG_RXFIFOF ((uint32_t)0x00020000)
  33. #define SDIO_FLAG_TXFIFOE ((uint32_t)0x00040000)
  34. #define SDIO_FLAG_RXFIFOE ((uint32_t)0x00080000)
  35. #define SDIO_FLAG_TXDAVL ((uint32_t)0x00100000)
  36. #define SDIO_FLAG_RXDAVL ((uint32_t)0x00200000)
  37. #define SDIO_FLAG_SDIOIT ((uint32_t)0x00400000)
  38. #define SDIO_FLAG_CEATAEND ((uint32_t)0x00800000)
  39. //用户配置区
  40. //SDIO时钟计算公式:SDIO_CK时钟=SDIOCLK/[clkdiv+2];其中,SDIOCLK固定为48Mhz
  41. //使用DMA模式的时候,传输速率可以到48Mhz(bypass on时),不过如果你的卡不是高速
  42. //卡,可能也会出错,出错就请降低时钟
  43. #define SDIO_INIT_CLK_DIV 0x76 //SDIO初始化频率,最大400Kh
  44. #define SDIO_TRANSFER_CLK_DIV 0x00 //SDIO传输频率,该值太小可能会导致读写文件出错
  45. //SDIO工作模式定义,通过SD_SetDeviceMode函数设置.
  46. #define SD_POLLING_MODE 0 //查询模式,该模式下,如果读写有问题,建议增大SDIO_TRANSFER_CLK_DIV的设置.
  47. #define SD_DMA_MODE 1 //DMA模式,该模式下,如果读写有问题,建议增大SDIO_TRANSFER_CLK_DIV的设置.
  48. //SDIO 各种错误枚举定义
  49. typedef enum
  50. {
  51. //特殊错误定义
  52. SD_CMD_CRC_FAIL = (1), /*!< Command response received (but CRC check failed) */
  53. SD_DATA_CRC_FAIL = (2), /*!< Data bock sent/received (CRC check Failed) */
  54. SD_CMD_RSP_TIMEOUT = (3), /*!< Command response timeout */
  55. SD_DATA_TIMEOUT = (4), /*!< Data time out */
  56. SD_TX_UNDERRUN = (5), /*!< Transmit FIFO under-run */
  57. SD_RX_OVERRUN = (6), /*!< Receive FIFO over-run */
  58. SD_START_BIT_ERR = (7), /*!< Start bit not detected on all data signals in widE bus mode */
  59. SD_CMD_OUT_OF_RANGE = (8), /*!< CMD's argument was out of range.*/
  60. SD_ADDR_MISALIGNED = (9), /*!< Misaligned address */
  61. SD_BLOCK_LEN_ERR = (10), /*!< Transferred block length is not allowed for the card or the number of transferred bytes does not match the block length */
  62. SD_ERASE_SEQ_ERR = (11), /*!< An error in the sequence of erase command occurs.*/
  63. SD_BAD_ERASE_PARAM = (12), /*!< An Invalid selection for erase groups */
  64. SD_WRITE_PROT_VIOLATION = (13), /*!< Attempt to program a write protect block */
  65. SD_LOCK_UNLOCK_FAILED = (14), /*!< Sequence or password error has been detected in unlock command or if there was an attempt to access a locked card */
  66. SD_COM_CRC_FAILED = (15), /*!< CRC check of the previous command failed */
  67. SD_ILLEGAL_CMD = (16), /*!< Command is not legal for the card state */
  68. SD_CARD_ECC_FAILED = (17), /*!< Card internal ECC was applied but failed to correct the data */
  69. SD_CC_ERROR = (18), /*!< Internal card controller error */
  70. SD_GENERAL_UNKNOWN_ERROR = (19), /*!< General or Unknown error */
  71. SD_STREAM_READ_UNDERRUN = (20), /*!< The card could not sustain data transfer in stream read operation. */
  72. SD_STREAM_WRITE_OVERRUN = (21), /*!< The card could not sustain data programming in stream mode */
  73. SD_CID_CSD_OVERWRITE = (22), /*!< CID/CSD overwrite error */
  74. SD_WP_ERASE_SKIP = (23), /*!< only partial address space was erased */
  75. SD_CARD_ECC_DISABLED = (24), /*!< Command has been executed without using internal ECC */
  76. SD_ERASE_RESET = (25), /*!< Erase sequence was cleared before executing because an out of erase sequence command was received */
  77. SD_AKE_SEQ_ERROR = (26), /*!< Error in sequence of authentication. */
  78. SD_INVALID_VOLTRANGE = (27),
  79. SD_ADDR_OUT_OF_RANGE = (28),
  80. SD_SWITCH_ERROR = (29),
  81. SD_SDIO_DISABLED = (30),
  82. SD_SDIO_FUNCTION_BUSY = (31),
  83. SD_SDIO_FUNCTION_FAILED = (32),
  84. SD_SDIO_UNKNOWN_FUNCTION = (33),
  85. //标准错误定义
  86. SD_INTERNAL_ERROR,
  87. SD_NOT_CONFIGURED,
  88. SD_REQUEST_PENDING,
  89. SD_REQUEST_NOT_APPLICABLE,
  90. SD_INVALID_PARAMETER,
  91. SD_UNSUPPORTED_FEATURE,
  92. SD_UNSUPPORTED_HW,
  93. SD_ERROR,
  94. SD_OK = 0
  95. } SD_Error;
  96. //SD卡CSD寄存器数据
  97. typedef struct
  98. {
  99. u8 CSDStruct; /*!< CSD structure */
  100. u8 SysSpecVersion; /*!< System specification version */
  101. u8 Reserved1; /*!< Reserved */
  102. u8 TAAC; /*!< Data read access-time 1 */
  103. u8 NSAC; /*!< Data read access-time 2 in CLK cycles */
  104. u8 MaxBusClkFrec; /*!< Max. bus clock frequency */
  105. u16 CardComdClasses; /*!< Card command classes */
  106. u8 RdBlockLen; /*!< Max. read data block length */
  107. u8 PartBlockRead; /*!< Partial blocks for read allowed */
  108. u8 WrBlockMisalign; /*!< Write block misalignment */
  109. u8 RdBlockMisalign; /*!< Read block misalignment */
  110. u8 DSRImpl; /*!< DSR implemented */
  111. u8 Reserved2; /*!< Reserved */
  112. u32 DeviceSize; /*!< Device Size */
  113. u8 MaxRdCurrentVDDMin; /*!< Max. read current @ VDD min */
  114. u8 MaxRdCurrentVDDMax; /*!< Max. read current @ VDD max */
  115. u8 MaxWrCurrentVDDMin; /*!< Max. write current @ VDD min */
  116. u8 MaxWrCurrentVDDMax; /*!< Max. write current @ VDD max */
  117. u8 DeviceSizeMul; /*!< Device size multiplier */
  118. u8 EraseGrSize; /*!< Erase group size */
  119. u8 EraseGrMul; /*!< Erase group size multiplier */
  120. u8 WrProtectGrSize; /*!< Write protect group size */
  121. u8 WrProtectGrEnable; /*!< Write protect group enable */
  122. u8 ManDeflECC; /*!< Manufacturer default ECC */
  123. u8 WrSpeedFact; /*!< Write speed factor */
  124. u8 MaxWrBlockLen; /*!< Max. write data block length */
  125. u8 WriteBlockPaPartial; /*!< Partial blocks for write allowed */
  126. u8 Reserved3; /*!< Reserded */
  127. u8 ContentProtectAppli; /*!< Content protection application */
  128. u8 FileFormatGrouop; /*!< File format group */
  129. u8 CopyFlag; /*!< Copy flag (OTP) */
  130. u8 PermWrProtect; /*!< Permanent write protection */
  131. u8 TempWrProtect; /*!< Temporary write protection */
  132. u8 FileFormat; /*!< File Format */
  133. u8 ECC; /*!< ECC code */
  134. u8 CSD_CRC; /*!< CSD CRC */
  135. u8 Reserved4; /*!< always 1*/
  136. } SD_CSD;
  137. //SD卡CID寄存器数据
  138. typedef struct
  139. {
  140. u8 ManufacturerID; /*!< ManufacturerID */
  141. u16 OEM_AppliID; /*!< OEM/Application ID */
  142. u32 ProdName1; /*!< Product Name part1 */
  143. u8 ProdName2; /*!< Product Name part2*/
  144. u8 ProdRev; /*!< Product Revision */
  145. u32 ProdSN; /*!< Product Serial Number */
  146. u8 Reserved1; /*!< Reserved1 */
  147. u16 ManufactDate; /*!< Manufacturing Date */
  148. u8 CID_CRC; /*!< CID CRC */
  149. u8 Reserved2; /*!< always 1 */
  150. } SD_CID;
  151. //SD卡状态
  152. typedef enum
  153. {
  154. SD_CARD_READY = ((uint32_t)0x00000001),
  155. SD_CARD_IDENTIFICATION = ((uint32_t)0x00000002),
  156. SD_CARD_STANDBY = ((uint32_t)0x00000003),
  157. SD_CARD_TRANSFER = ((uint32_t)0x00000004),
  158. SD_CARD_SENDING = ((uint32_t)0x00000005),
  159. SD_CARD_RECEIVING = ((uint32_t)0x00000006),
  160. SD_CARD_PROGRAMMING = ((uint32_t)0x00000007),
  161. SD_CARD_DISCONNECTED = ((uint32_t)0x00000008),
  162. SD_CARD_ERROR = ((uint32_t)0x000000FF)
  163. }SDCardState;
  164. //SD卡信息,包括CSD,CID等数据
  165. typedef struct
  166. {
  167. SD_CSD SD_csd;
  168. SD_CID SD_cid;
  169. long long CardCapacity; //SD卡容量,单位:字节,最大支持2^64字节大小的卡.
  170. u32 CardBlockSize; //SD卡块大小
  171. u16 RCA; //卡相对地址
  172. u8 CardType; //卡类型
  173. } SD_CardInfo;
  174. extern SD_CardInfo SDCardInfo;//SD卡信息
  175. //SDIO 指令集
  176. #define SD_CMD_GO_IDLE_STATE ((u8)0)
  177. #define SD_CMD_SEND_OP_COND ((u8)1)
  178. #define SD_CMD_ALL_SEND_CID ((u8)2)
  179. #define SD_CMD_SET_REL_ADDR ((u8)3) /*!< SDIO_SEND_REL_ADDR for SD Card */
  180. #define SD_CMD_SET_DSR ((u8)4)
  181. #define SD_CMD_SDIO_SEN_OP_COND ((u8)5)
  182. #define SD_CMD_HS_SWITCH ((u8)6)
  183. #define SD_CMD_SEL_DESEL_CARD ((u8)7)
  184. #define SD_CMD_HS_SEND_EXT_CSD ((u8)8)
  185. #define SD_CMD_SEND_CSD ((u8)9)
  186. #define SD_CMD_SEND_CID ((u8)10)
  187. #define SD_CMD_READ_DAT_UNTIL_STOP ((u8)11) /*!< SD Card doesn't support it */
  188. #define SD_CMD_STOP_TRANSMISSION ((u8)12)
  189. #define SD_CMD_SEND_STATUS ((u8)13)
  190. #define SD_CMD_HS_BUSTEST_READ ((u8)14)
  191. #define SD_CMD_GO_INACTIVE_STATE ((u8)15)
  192. #define SD_CMD_SET_BLOCKLEN ((u8)16)
  193. #define SD_CMD_READ_SINGLE_BLOCK ((u8)17)
  194. #define SD_CMD_READ_MULT_BLOCK ((u8)18)
  195. #define SD_CMD_HS_BUSTEST_WRITE ((u8)19)
  196. #define SD_CMD_WRITE_DAT_UNTIL_STOP ((u8)20)
  197. #define SD_CMD_SET_BLOCK_COUNT ((u8)23)
  198. #define SD_CMD_WRITE_SINGLE_BLOCK ((u8)24)
  199. #define SD_CMD_WRITE_MULT_BLOCK ((u8)25)
  200. #define SD_CMD_PROG_CID ((u8)26)
  201. #define SD_CMD_PROG_CSD ((u8)27)
  202. #define SD_CMD_SET_WRITE_PROT ((u8)28)
  203. #define SD_CMD_CLR_WRITE_PROT ((u8)29)
  204. #define SD_CMD_SEND_WRITE_PROT ((u8)30)
  205. #define SD_CMD_SD_ERASE_GRP_START ((u8)32) /*!< To set the address of the first write
  206. block to be erased. (For SD card only) */
  207. #define SD_CMD_SD_ERASE_GRP_END ((u8)33) /*!< To set the address of the last write block of the
  208. continuous range to be erased. (For SD card only) */
  209. #define SD_CMD_ERASE_GRP_START ((u8)35) /*!< To set the address of the first write block to be erased.
  210. (For MMC card only spec 3.31) */
  211. #define SD_CMD_ERASE_GRP_END ((u8)36) /*!< To set the address of the last write block of the
  212. continuous range to be erased. (For MMC card only spec 3.31) */
  213. #define SD_CMD_ERASE ((u8)38)
  214. #define SD_CMD_FAST_IO ((u8)39) /*!< SD Card doesn't support it */
  215. #define SD_CMD_GO_IRQ_STATE ((u8)40) /*!< SD Card doesn't support it */
  216. #define SD_CMD_LOCK_UNLOCK ((u8)42)
  217. #define SD_CMD_APP_CMD ((u8)55)
  218. #define SD_CMD_GEN_CMD ((u8)56)
  219. #define SD_CMD_NO_CMD ((u8)64)
  220. /**
  221. * @brief Following commands are SD Card Specific commands.
  222. * SDIO_APP_CMD :CMD55 should be sent before sending these commands.
  223. */
  224. #define SD_CMD_APP_SD_SET_BUSWIDTH ((u8)6) /*!< For SD Card only */
  225. #define SD_CMD_SD_APP_STAUS ((u8)13) /*!< For SD Card only */
  226. #define SD_CMD_SD_APP_SEND_NUM_WRITE_BLOCKS ((u8)22) /*!< For SD Card only */
  227. #define SD_CMD_SD_APP_OP_COND ((u8)41) /*!< For SD Card only */
  228. #define SD_CMD_SD_APP_SET_CLR_CARD_DETECT ((u8)42) /*!< For SD Card only */
  229. #define SD_CMD_SD_APP_SEND_SCR ((u8)51) /*!< For SD Card only */
  230. #define SD_CMD_SDIO_RW_DIRECT ((u8)52) /*!< For SD I/O Card only */
  231. #define SD_CMD_SDIO_RW_EXTENDED ((u8)53) /*!< For SD I/O Card only */
  232. /**
  233. * @brief Following commands are SD Card Specific security commands.
  234. * SDIO_APP_CMD should be sent before sending these commands.
  235. */
  236. #define SD_CMD_SD_APP_GET_MKB ((u8)43) /*!< For SD Card only */
  237. #define SD_CMD_SD_APP_GET_MID ((u8)44) /*!< For SD Card only */
  238. #define SD_CMD_SD_APP_SET_CER_RN1 ((u8)45) /*!< For SD Card only */
  239. #define SD_CMD_SD_APP_GET_CER_RN2 ((u8)46) /*!< For SD Card only */
  240. #define SD_CMD_SD_APP_SET_CER_RES2 ((u8)47) /*!< For SD Card only */
  241. #define SD_CMD_SD_APP_GET_CER_RES1 ((u8)48) /*!< For SD Card only */
  242. #define SD_CMD_SD_APP_SECURE_READ_MULTIPLE_BLOCK ((u8)18) /*!< For SD Card only */
  243. #define SD_CMD_SD_APP_SECURE_WRITE_MULTIPLE_BLOCK ((u8)25) /*!< For SD Card only */
  244. #define SD_CMD_SD_APP_SECURE_ERASE ((u8)38) /*!< For SD Card only */
  245. #define SD_CMD_SD_APP_CHANGE_SECURE_AREA ((u8)49) /*!< For SD Card only */
  246. #define SD_CMD_SD_APP_SECURE_WRITE_MKB ((u8)48) /*!< For SD Card only */
  247. //支持的SD卡定义
  248. #define SDIO_STD_CAPACITY_SD_CARD_V1_1 ((u32)0x00000000)
  249. #define SDIO_STD_CAPACITY_SD_CARD_V2_0 ((u32)0x00000001)
  250. #define SDIO_HIGH_CAPACITY_SD_CARD ((u32)0x00000002)
  251. #define SDIO_MULTIMEDIA_CARD ((u32)0x00000003)
  252. #define SDIO_SECURE_DIGITAL_IO_CARD ((u32)0x00000004)
  253. #define SDIO_HIGH_SPEED_MULTIMEDIA_CARD ((u32)0x00000005)
  254. #define SDIO_SECURE_DIGITAL_IO_COMBO_CARD ((u32)0x00000006)
  255. #define SDIO_HIGH_CAPACITY_MMC_CARD ((u32)0x00000007)
  256. //SDIO相关参数定义
  257. #define NULL 0
  258. #define SDIO_STATIC_FLAGS ((u32)0x000005FF)
  259. #define SDIO_CMD0TIMEOUT ((u32)0x00010000)
  260. #define SDIO_DATATIMEOUT ((u32)0xFFFFFFFF)
  261. #define SDIO_FIFO_Address ((u32)0x40018080)
  262. //Mask for errors Card Status R1 (OCR Register)
  263. #define SD_OCR_ADDR_OUT_OF_RANGE ((u32)0x80000000)
  264. #define SD_OCR_ADDR_MISALIGNED ((u32)0x40000000)
  265. #define SD_OCR_BLOCK_LEN_ERR ((u32)0x20000000)
  266. #define SD_OCR_ERASE_SEQ_ERR ((u32)0x10000000)
  267. #define SD_OCR_BAD_ERASE_PARAM ((u32)0x08000000)
  268. #define SD_OCR_WRITE_PROT_VIOLATION ((u32)0x04000000)
  269. #define SD_OCR_LOCK_UNLOCK_FAILED ((u32)0x01000000)
  270. #define SD_OCR_COM_CRC_FAILED ((u32)0x00800000)
  271. #define SD_OCR_ILLEGAL_CMD ((u32)0x00400000)
  272. #define SD_OCR_CARD_ECC_FAILED ((u32)0x00200000)
  273. #define SD_OCR_CC_ERROR ((u32)0x00100000)
  274. #define SD_OCR_GENERAL_UNKNOWN_ERROR ((u32)0x00080000)
  275. #define SD_OCR_STREAM_READ_UNDERRUN ((u32)0x00040000)
  276. #define SD_OCR_STREAM_WRITE_OVERRUN ((u32)0x00020000)
  277. #define SD_OCR_CID_CSD_OVERWRIETE ((u32)0x00010000)
  278. #define SD_OCR_WP_ERASE_SKIP ((u32)0x00008000)
  279. #define SD_OCR_CARD_ECC_DISABLED ((u32)0x00004000)
  280. #define SD_OCR_ERASE_RESET ((u32)0x00002000)
  281. #define SD_OCR_AKE_SEQ_ERROR ((u32)0x00000008)
  282. #define SD_OCR_ERRORBITS ((u32)0xFDFFE008)
  283. //Masks for R6 Response
  284. #define SD_R6_GENERAL_UNKNOWN_ERROR ((u32)0x00002000)
  285. #define SD_R6_ILLEGAL_CMD ((u32)0x00004000)
  286. #define SD_R6_COM_CRC_FAILED ((u32)0x00008000)
  287. #define SD_VOLTAGE_WINDOW_SD ((u32)0x80100000)
  288. #define SD_HIGH_CAPACITY ((u32)0x40000000)
  289. #define SD_STD_CAPACITY ((u32)0x00000000)
  290. #define SD_CHECK_PATTERN ((u32)0x000001AA)
  291. #define SD_VOLTAGE_WINDOW_MMC ((u32)0x80FF8000)
  292. #define SD_MAX_VOLT_TRIAL ((u32)0x0000FFFF)
  293. #define SD_ALLZERO ((u32)0x00000000)
  294. #define SD_WIDE_BUS_SUPPORT ((u32)0x00040000)
  295. #define SD_SINGLE_BUS_SUPPORT ((u32)0x00010000)
  296. #define SD_CARD_LOCKED ((u32)0x02000000)
  297. #define SD_CARD_PROGRAMMING ((u32)0x00000007)
  298. #define SD_CARD_RECEIVING ((u32)0x00000006)
  299. #define SD_DATATIMEOUT ((u32)0xFFFFFFFF)
  300. #define SD_0TO7BITS ((u32)0x000000FF)
  301. #define SD_8TO15BITS ((u32)0x0000FF00)
  302. #define SD_16TO23BITS ((u32)0x00FF0000)
  303. #define SD_24TO31BITS ((u32)0xFF000000)
  304. #define SD_MAX_DATA_LENGTH ((u32)0x01FFFFFF)
  305. #define SD_HALFFIFO ((u32)0x00000008)
  306. #define SD_HALFFIFOBYTES ((u32)0x00000020)
  307. //Command Class Supported
  308. #define SD_CCCC_LOCK_UNLOCK ((u32)0x00000080)
  309. #define SD_CCCC_WRITE_PROT ((u32)0x00000040)
  310. #define SD_CCCC_ERASE ((u32)0x00000020)
  311. //CMD8指令
  312. #define SDIO_SEND_IF_COND ((u32)0x00000008)
  313. //相关函数定义
  314. SD_Error SD_Init(void);
  315. void SDIO_Clock_Set(u8 clkdiv);
  316. SD_Error SD_PowerON(void);
  317. SD_Error SD_PowerOFF(void);
  318. SD_Error SD_InitializeCards(void);
  319. SD_Error SD_GetCardInfo(SD_CardInfo *cardinfo);
  320. SD_Error SD_EnableWideBusOperation(u32 wmode);
  321. SD_Error SD_SetDeviceMode(u32 mode);
  322. SD_Error SD_SelectDeselect(u32 addr);
  323. SD_Error SD_SendStatus(uint32_t *pcardstatus);
  324. SDCardState SD_GetState(void);
  325. SD_Error SD_ReadBlock(u8 *buf,long long addr,u16 blksize);
  326. SD_Error SD_ReadMultiBlocks(u8 *buf,long long addr,u16 blksize,u32 nblks);
  327. SD_Error SD_WriteBlock(u8 *buf,long long addr, u16 blksize);
  328. SD_Error SD_WriteMultiBlocks(u8 *buf,long long addr,u16 blksize,u32 nblks);
  329. SD_Error SD_ProcessIRQSrc(void);
  330. void SD_DMA_Config(u32*mbuf,u32 bufsize,u32 dir);
  331. //void SD_DMA_Config(u32*mbuf,u32 bufsize,u8 dir);
  332. u8 SD_ReadDisk(u8*buf,u32 sector,u8 cnt); //读SD卡,fatfs/usb调用
  333. u8 SD_WriteDisk(u8*buf,u32 sector,u8 cnt); //写SD卡,fatfs/usb调用
  334. #endif

(2)、sdio_sdcard.h

  1. #include "sdio_sdcard.h"
  2. #include "string.h"
  3. #include "sys.h"
  4. #include "usart.h"
  5. //
  6. //SDIO 驱动代码
  7. //STM32F4工程模板-库函数版本
  8. //********************************************************************************
  9. //修改说明
  10. //V1.1 20140522
  11. //1,加入超时判断,解决轮询接收死机的问题.
  12. //V1.2 20140715
  13. //1,新增SD_GetState和SD_SendStatus函数.
  14. //
  15. /*用于sdio初始化的结构体*/
  16. SDIO_InitTypeDef SDIO_InitStructure;
  17. SDIO_CmdInitTypeDef SDIO_CmdInitStructure;
  18. SDIO_DataInitTypeDef SDIO_DataInitStructure;
  19. SD_Error CmdError(void);
  20. SD_Error CmdResp7Error(void);
  21. SD_Error CmdResp1Error(u8 cmd);
  22. SD_Error CmdResp3Error(void);
  23. SD_Error CmdResp2Error(void);
  24. SD_Error CmdResp6Error(u8 cmd,u16*prca);
  25. SD_Error SDEnWideBus(u8 enx);
  26. SD_Error IsCardProgramming(u8 *pstatus);
  27. SD_Error FindSCR(u16 rca,u32 *pscr);
  28. u8 convert_from_bytes_to_power_of_two(u16 NumberOfBytes);
  29. static u8 CardType=SDIO_STD_CAPACITY_SD_CARD_V1_1; //SD卡类型(默认为1.x卡)
  30. static u32 CSD_Tab[4],CID_Tab[4],RCA=0; //SD卡CSD,CID以及相对地址(RCA)数据
  31. static u8 DeviceMode=SD_DMA_MODE; //工作模式,注意,工作模式必须通过SD_SetDeviceMode,后才算数.这里只是定义一个默认的模式(SD_DMA_MODE)
  32. static u8 StopCondition=0; //是否发送停止传输标志位,DMA多块读写的时候用到
  33. volatile SD_Error TransferError=SD_OK; //数据传输错误标志,DMA读写时使用
  34. volatile u8 TransferEnd=0; //传输结束标志,DMA读写时使用
  35. SD_CardInfo SDCardInfo; //SD卡信息
  36. //SD_ReadDisk/SD_WriteDisk函数专用buf,当这两个函数的数据缓存区地址不是4字节对齐的时候,
  37. //需要用到该数组,确保数据缓存区地址是4字节对齐的.
  38. __align(4) u8 SDIO_DATA_BUFFER[512];
  39. void SDIO_Register_Deinit(void)
  40. {
  41. SDIO->POWER=0x00000000;
  42. SDIO->CLKCR=0x00000000;
  43. SDIO->ARG=0x00000000;
  44. SDIO->CMD=0x00000000;
  45. SDIO->DTIMER=0x00000000;
  46. SDIO->DLEN=0x00000000;
  47. SDIO->DCTRL=0x00000000;
  48. SDIO->ICR=0x00C007FF;
  49. SDIO->MASK=0x00000000;
  50. }
  51. //初始化SD卡
  52. //返回值:错误代码;(0,无错误)
  53. SD_Error SD_Init(void)
  54. {
  55. GPIO_InitTypeDef GPIO_InitStructure;
  56. NVIC_InitTypeDef NVIC_InitStructure;
  57. SD_Error errorstatus = SD_OK;
  58. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOD|RCC_AHB1Periph_DMA2, ENABLE);//使能GPIOC,GPIOD DMA2时钟
  59. RCC_APB2PeriphClockCmd(RCC_APB2Periph_SDIO, ENABLE);//SDIO时钟使能
  60. RCC_APB2PeriphResetCmd(RCC_APB2Periph_SDIO, ENABLE);//SDIO复位
  61. GPIO_InitStructure.GPIO_Pin =GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12; //PC8,9,10,11,12复用功能输出
  62. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
  63. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//100M
  64. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  65. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
  66. GPIO_Init(GPIOC, &GPIO_InitStructure);// PC8,9,10,11,12复用功能输出
  67. GPIO_InitStructure.GPIO_Pin =GPIO_Pin_2;
  68. GPIO_Init(GPIOD, &GPIO_InitStructure);//PD2复用功能输出
  69. //引脚复用映射设置
  70. GPIO_PinAFConfig(GPIOC,GPIO_PinSource8,GPIO_AF_SDIO); //PC8,AF12
  71. GPIO_PinAFConfig(GPIOC,GPIO_PinSource9,GPIO_AF_SDIO);
  72. GPIO_PinAFConfig(GPIOC,GPIO_PinSource10,GPIO_AF_SDIO);
  73. GPIO_PinAFConfig(GPIOC,GPIO_PinSource11,GPIO_AF_SDIO);
  74. GPIO_PinAFConfig(GPIOC,GPIO_PinSource12,GPIO_AF_SDIO);
  75. GPIO_PinAFConfig(GPIOD,GPIO_PinSource2,GPIO_AF_SDIO);
  76. RCC_APB2PeriphResetCmd(RCC_APB2Periph_SDIO, DISABLE);//SDIO结束复位
  77. //SDIO外设寄存器设置为默认值
  78. SDIO_Register_Deinit();
  79. NVIC_InitStructure.NVIC_IRQChannel = SDIO_IRQn;
  80. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;//抢占优先级3
  81. NVIC_InitStructure.NVIC_IRQChannelSubPriority =0; //子优先级3
  82. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
  83. NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、
  84. errorstatus = SD_PowerON(); //SD卡上电
  85. if(errorstatus==SD_OK)
  86. {
  87. errorstatus=SD_InitializeCards(); //初始化SD卡
  88. }
  89. if(errorstatus==SD_OK)
  90. {
  91. errorstatus=SD_GetCardInfo(&SDCardInfo); //获取卡信息
  92. }
  93. if(errorstatus==SD_OK)
  94. {
  95. errorstatus=SD_SelectDeselect((u32)(SDCardInfo.RCA<<16));//选中SD卡
  96. }
  97. if(errorstatus==SD_OK)
  98. {
  99. errorstatus=SD_EnableWideBusOperation(SDIO_BusWide_4b); //4位宽度,如果是MMC卡,则不能用4位模式
  100. }
  101. if((errorstatus==SD_OK)||(SDIO_MULTIMEDIA_CARD==CardType))
  102. {
  103. SDIO_Clock_Set(SDIO_TRANSFER_CLK_DIV); //设置时钟频率,SDIO时钟计算公式:SDIO_CK时钟=SDIOCLK/[clkdiv+2];其中,SDIOCLK固定为48Mhz
  104. errorstatus=SD_SetDeviceMode(SD_DMA_MODE); //设置为DMA模式
  105. //errorstatus=SD_SetDeviceMode(SD_POLLING_MODE);//设置为查询模式
  106. }
  107. return errorstatus;
  108. }
  109. //SDIO时钟初始化设置
  110. //clkdiv:时钟分频系数
  111. //CK时钟=SDIOCLK/[clkdiv+2];(SDIOCLK时钟固定为48Mhz)
  112. void SDIO_Clock_Set(u8 clkdiv)
  113. {
  114. u32 tmpreg = SDIO->CLKCR;
  115. tmpreg &= 0XFFFFFF00;
  116. tmpreg |= clkdiv;
  117. SDIO->CLKCR = tmpreg;
  118. }
  119. //卡上电
  120. //查询所有SDIO接口上的卡设备,并查询其电压和配置时钟
  121. //返回值:错误代码;(0,无错误)
  122. SD_Error SD_PowerON(void)
  123. {
  124. u8 i=0;
  125. SD_Error errorstatus=SD_OK;
  126. u32 response=0,count=0,validvoltage=0;
  127. u32 SDType=SD_STD_CAPACITY;
  128. /*初始化时的时钟不能大于400KHz*/
  129. SDIO_InitStructure.SDIO_ClockDiv = SDIO_INIT_CLK_DIV; /* HCLK = 72MHz, SDIOCLK = 72MHz, SDIO_CK = HCLK/(178 + 2) = 400 KHz */
  130. SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_Rising;
  131. SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable; //不使用bypass模式,直接用HCLK进行分频得到SDIO_CK
  132. SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable; // 空闲时不关闭时钟电源
  133. SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_1b; //1位数据线
  134. SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable;//硬件流
  135. SDIO_Init(&SDIO_InitStructure);
  136. SDIO_SetPowerState(SDIO_PowerState_ON); //上电状态,开启卡时钟
  137. SDIO->CLKCR|=1<<8; //SDIOCK使能
  138. for(i=0;i<74;i++)
  139. {
  140. SDIO_CmdInitStructure.SDIO_Argument = 0x0;//发送CMD0进入IDLE STAGE模式命令.
  141. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_GO_IDLE_STATE; //cmd0
  142. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_No; //无响应
  143. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  144. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; //则CPSM在开始发送命令之前等待数据传输结束。
  145. SDIO_SendCommand(&SDIO_CmdInitStructure); //写命令进命令寄存器
  146. errorstatus=CmdError();
  147. if(errorstatus==SD_OK)
  148. {
  149. break;
  150. }
  151. }
  152. if(errorstatus)return errorstatus;//返回错误状态
  153. SDIO_CmdInitStructure.SDIO_Argument = SD_CHECK_PATTERN; //发送CMD8,短响应,检查SD卡接口特性
  154. SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_SEND_IF_COND; //cmd8
  155. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; //r7
  156. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; //关闭等待中断
  157. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  158. SDIO_SendCommand(&SDIO_CmdInitStructure);
  159. errorstatus=CmdResp7Error(); //等待R7响应
  160. if(errorstatus==SD_OK) //R7响应正常
  161. {
  162. CardType=SDIO_STD_CAPACITY_SD_CARD_V2_0; //SD 2.0卡
  163. SDType=SD_HIGH_CAPACITY; //高容量卡
  164. }
  165. SDIO_CmdInitStructure.SDIO_Argument = 0x00;//发送CMD55,短响应
  166. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
  167. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  168. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  169. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  170. SDIO_SendCommand(&SDIO_CmdInitStructure); //发送CMD55,短响应
  171. errorstatus=CmdResp1Error(SD_CMD_APP_CMD); //等待R1响应
  172. if(errorstatus==SD_OK)//SD2.0/SD 1.1,否则为MMC卡
  173. {
  174. //SD卡,发送ACMD41 SD_APP_OP_COND,参数为:0x80100000
  175. while((!validvoltage)&&(count<SD_MAX_VOLT_TRIAL))
  176. {
  177. SDIO_CmdInitStructure.SDIO_Argument = 0x00;//发送CMD55,短响应
  178. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD; //CMD55
  179. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  180. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  181. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  182. SDIO_SendCommand(&SDIO_CmdInitStructure); //发送CMD55,短响应
  183. errorstatus=CmdResp1Error(SD_CMD_APP_CMD); //等待R1响应
  184. if(errorstatus!=SD_OK)
  185. {
  186. return errorstatus; //响应错误
  187. }
  188. //acmd41,命令参数由支持的电压范围及HCS位组成,HCS位置一来区分卡是SDSc还是sdhc
  189. SDIO_CmdInitStructure.SDIO_Argument = SD_VOLTAGE_WINDOW_SD | SDType; //发送ACMD41,短响应
  190. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_APP_OP_COND;
  191. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; //r3
  192. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  193. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  194. SDIO_SendCommand(&SDIO_CmdInitStructure);
  195. errorstatus = CmdResp3Error(); //等待R3响应
  196. if(errorstatus!=SD_OK)
  197. {
  198. return errorstatus; //响应错误
  199. }
  200. response=SDIO->RESP1;; //得到响应
  201. validvoltage=(((response>>31)==1)?1:0); //判断SD卡上电是否完成
  202. count++;
  203. }
  204. if(count>=SD_MAX_VOLT_TRIAL)
  205. {
  206. errorstatus=SD_INVALID_VOLTRANGE;
  207. return errorstatus;
  208. }
  209. if(response&=SD_HIGH_CAPACITY)
  210. {
  211. CardType=SDIO_HIGH_CAPACITY_SD_CARD;
  212. }
  213. }else//MMC卡
  214. {
  215. //MMC卡,发送CMD1 SDIO_SEND_OP_COND,参数为:0x80FF8000
  216. while((!validvoltage)&&(count<SD_MAX_VOLT_TRIAL))
  217. {
  218. SDIO_CmdInitStructure.SDIO_Argument = SD_VOLTAGE_WINDOW_MMC;//发送CMD1,短响应
  219. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_OP_COND;
  220. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; //r3
  221. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  222. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  223. SDIO_SendCommand(&SDIO_CmdInitStructure);
  224. errorstatus=CmdResp3Error(); //等待R3响应
  225. if(errorstatus!=SD_OK)
  226. {
  227. return errorstatus; //响应错误
  228. }
  229. response=SDIO->RESP1;; //得到响应
  230. validvoltage=(((response>>31)==1)?1:0);
  231. count++;
  232. }
  233. if(count>=SD_MAX_VOLT_TRIAL)
  234. {
  235. errorstatus=SD_INVALID_VOLTRANGE;
  236. return errorstatus;
  237. }
  238. CardType=SDIO_MULTIMEDIA_CARD;
  239. }
  240. return(errorstatus);
  241. }
  242. //SD卡 Power OFF
  243. //返回值:错误代码;(0,无错误)
  244. SD_Error SD_PowerOFF(void)
  245. {
  246. SDIO_SetPowerState(SDIO_PowerState_OFF);//SDIO电源关闭,时钟停止
  247. return SD_OK;
  248. }
  249. //初始化所有的卡,并让卡进入就绪状态
  250. //返回值:错误代码
  251. SD_Error SD_InitializeCards(void)
  252. {
  253. SD_Error errorstatus=SD_OK;
  254. u16 rca = 0x01;
  255. if (SDIO_GetPowerState() == SDIO_PowerState_OFF) //检查电源状态,确保为上电状态
  256. {
  257. errorstatus = SD_REQUEST_NOT_APPLICABLE;
  258. return(errorstatus);
  259. }
  260. if(SDIO_SECURE_DIGITAL_IO_CARD!=CardType) //非SECURE_DIGITAL_IO_CARD
  261. {
  262. SDIO_CmdInitStructure.SDIO_Argument = 0x0;//发送CMD2,取得CID,长响应
  263. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_ALL_SEND_CID;
  264. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Long;
  265. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  266. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  267. SDIO_SendCommand(&SDIO_CmdInitStructure);//发送CMD2,取得CID,长响应
  268. errorstatus=CmdResp2Error(); //等待R2响应
  269. if(errorstatus!=SD_OK)
  270. {
  271. return errorstatus; //响应错误
  272. }
  273. CID_Tab[0]=SDIO->RESP1;
  274. CID_Tab[1]=SDIO->RESP2;
  275. CID_Tab[2]=SDIO->RESP3;
  276. CID_Tab[3]=SDIO->RESP4;
  277. }
  278. if((SDIO_STD_CAPACITY_SD_CARD_V1_1==CardType)||(SDIO_STD_CAPACITY_SD_CARD_V2_0==CardType)||(SDIO_SECURE_DIGITAL_IO_COMBO_CARD==CardType)||(SDIO_HIGH_CAPACITY_SD_CARD==CardType))//判断卡类型
  279. {
  280. SDIO_CmdInitStructure.SDIO_Argument = 0x00;//发送CMD3,短响应
  281. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_REL_ADDR; //cmd3
  282. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; //r6
  283. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  284. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  285. SDIO_SendCommand(&SDIO_CmdInitStructure); //发送CMD3,短响应
  286. errorstatus=CmdResp6Error(SD_CMD_SET_REL_ADDR,&rca);//等待R6响应
  287. if(errorstatus!=SD_OK)
  288. {
  289. return errorstatus; //响应错误
  290. }
  291. }
  292. if (SDIO_MULTIMEDIA_CARD==CardType)
  293. {
  294. SDIO_CmdInitStructure.SDIO_Argument = (u32)(rca<<16);//发送CMD3,短响应
  295. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_REL_ADDR; //cmd3
  296. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; //r6
  297. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  298. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  299. SDIO_SendCommand(&SDIO_CmdInitStructure); //发送CMD3,短响应
  300. errorstatus=CmdResp2Error(); //等待R2响应
  301. if(errorstatus!=SD_OK)
  302. {
  303. return errorstatus; //响应错误
  304. }
  305. }
  306. if (SDIO_SECURE_DIGITAL_IO_CARD!=CardType) //非SECURE_DIGITAL_IO_CARD
  307. {
  308. RCA = rca;
  309. SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)(rca << 16);//发送CMD9+卡RCA,取得CSD,长响应
  310. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_CSD;
  311. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Long;
  312. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  313. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  314. SDIO_SendCommand(&SDIO_CmdInitStructure);
  315. errorstatus=CmdResp2Error(); //等待R2响应
  316. if(errorstatus!=SD_OK)
  317. {
  318. return errorstatus; //响应错误
  319. }
  320. CSD_Tab[0]=SDIO->RESP1;
  321. CSD_Tab[1]=SDIO->RESP2;
  322. CSD_Tab[2]=SDIO->RESP3;
  323. CSD_Tab[3]=SDIO->RESP4;
  324. }
  325. return SD_OK;//卡初始化成功
  326. }
  327. //得到卡信息
  328. //cardinfo:卡信息存储区
  329. //返回值:错误状态
  330. SD_Error SD_GetCardInfo(SD_CardInfo *cardinfo)
  331. {
  332. SD_Error errorstatus=SD_OK;
  333. u8 tmp=0;
  334. cardinfo->CardType=(u8)CardType; //卡类型
  335. cardinfo->RCA=(u16)RCA; //卡RCA值
  336. tmp=(u8)((CSD_Tab[0]&0xFF000000)>>24);
  337. cardinfo->SD_csd.CSDStruct=(tmp&0xC0)>>6; //CSD结构
  338. cardinfo->SD_csd.SysSpecVersion=(tmp&0x3C)>>2; //2.0协议还没定义这部分(为保留),应该是后续协议定义的
  339. cardinfo->SD_csd.Reserved1=tmp&0x03; //2个保留位
  340. tmp=(u8)((CSD_Tab[0]&0x00FF0000)>>16); //第1个字节
  341. cardinfo->SD_csd.TAAC=tmp; //数据读时间1
  342. tmp=(u8)((CSD_Tab[0]&0x0000FF00)>>8); //第2个字节
  343. cardinfo->SD_csd.NSAC=tmp; //数据读时间2
  344. tmp=(u8)(CSD_Tab[0]&0x000000FF); //第3个字节
  345. cardinfo->SD_csd.MaxBusClkFrec=tmp; //传输速度
  346. tmp=(u8)((CSD_Tab[1]&0xFF000000)>>24); //第4个字节
  347. cardinfo->SD_csd.CardComdClasses=tmp<<4; //卡指令类高四位
  348. tmp=(u8)((CSD_Tab[1]&0x00FF0000)>>16); //第5个字节
  349. cardinfo->SD_csd.CardComdClasses|=(tmp&0xF0)>>4;//卡指令类低四位
  350. cardinfo->SD_csd.RdBlockLen=tmp&0x0F; //最大读取数据长度
  351. tmp=(u8)((CSD_Tab[1]&0x0000FF00)>>8); //第6个字节
  352. cardinfo->SD_csd.PartBlockRead=(tmp&0x80)>>7; //允许分块读
  353. cardinfo->SD_csd.WrBlockMisalign=(tmp&0x40)>>6; //写块错位
  354. cardinfo->SD_csd.RdBlockMisalign=(tmp&0x20)>>5; //读块错位
  355. cardinfo->SD_csd.DSRImpl=(tmp&0x10)>>4;
  356. cardinfo->SD_csd.Reserved2=0; //保留
  357. if((CardType==SDIO_STD_CAPACITY_SD_CARD_V1_1)||(CardType==SDIO_STD_CAPACITY_SD_CARD_V2_0)||(SDIO_MULTIMEDIA_CARD==CardType))//标准1.1/2.0卡/MMC卡
  358. {
  359. cardinfo->SD_csd.DeviceSize=(tmp&0x03)<<10; //C_SIZE(12位)
  360. tmp=(u8)(CSD_Tab[1]&0x000000FF); //第7个字节
  361. cardinfo->SD_csd.DeviceSize|=(tmp)<<2;
  362. tmp=(u8)((CSD_Tab[2]&0xFF000000)>>24); //第8个字节
  363. cardinfo->SD_csd.DeviceSize|=(tmp&0xC0)>>6;
  364. cardinfo->SD_csd.MaxRdCurrentVDDMin=(tmp&0x38)>>3;
  365. cardinfo->SD_csd.MaxRdCurrentVDDMax=(tmp&0x07);
  366. tmp=(u8)((CSD_Tab[2]&0x00FF0000)>>16); //第9个字节
  367. cardinfo->SD_csd.MaxWrCurrentVDDMin=(tmp&0xE0)>>5;
  368. cardinfo->SD_csd.MaxWrCurrentVDDMax=(tmp&0x1C)>>2;
  369. cardinfo->SD_csd.DeviceSizeMul=(tmp&0x03)<<1;//C_SIZE_MULT
  370. tmp=(u8)((CSD_Tab[2]&0x0000FF00)>>8); //第10个字节
  371. cardinfo->SD_csd.DeviceSizeMul|=(tmp&0x80)>>7;
  372. cardinfo->CardCapacity=(cardinfo->SD_csd.DeviceSize+1);//计算卡容量
  373. cardinfo->CardCapacity*=(1<<(cardinfo->SD_csd.DeviceSizeMul+2));
  374. cardinfo->CardBlockSize=1<<(cardinfo->SD_csd.RdBlockLen);//块大小
  375. cardinfo->CardCapacity*=cardinfo->CardBlockSize;
  376. }else if(CardType==SDIO_HIGH_CAPACITY_SD_CARD) //高容量卡
  377. {
  378. tmp=(u8)(CSD_Tab[1]&0x000000FF); //第7个字节
  379. cardinfo->SD_csd.DeviceSize=(tmp&0x3F)<<16;//C_SIZE
  380. tmp=(u8)((CSD_Tab[2]&0xFF000000)>>24); //第8个字节
  381. cardinfo->SD_csd.DeviceSize|=(tmp<<8);
  382. tmp=(u8)((CSD_Tab[2]&0x00FF0000)>>16); //第9个字节
  383. cardinfo->SD_csd.DeviceSize|=(tmp);
  384. tmp=(u8)((CSD_Tab[2]&0x0000FF00)>>8); //第10个字节
  385. cardinfo->CardCapacity=(long long)(cardinfo->SD_csd.DeviceSize+1)*512*1024;//计算卡容量
  386. cardinfo->CardBlockSize=512; //块大小固定为512字节
  387. }
  388. cardinfo->SD_csd.EraseGrSize=(tmp&0x40)>>6;
  389. cardinfo->SD_csd.EraseGrMul=(tmp&0x3F)<<1;
  390. tmp=(u8)(CSD_Tab[2]&0x000000FF); //第11个字节
  391. cardinfo->SD_csd.EraseGrMul|=(tmp&0x80)>>7;
  392. cardinfo->SD_csd.WrProtectGrSize=(tmp&0x7F);
  393. tmp=(u8)((CSD_Tab[3]&0xFF000000)>>24); //第12个字节
  394. cardinfo->SD_csd.WrProtectGrEnable=(tmp&0x80)>>7;
  395. cardinfo->SD_csd.ManDeflECC=(tmp&0x60)>>5;
  396. cardinfo->SD_csd.WrSpeedFact=(tmp&0x1C)>>2;
  397. cardinfo->SD_csd.MaxWrBlockLen=(tmp&0x03)<<2;
  398. tmp=(u8)((CSD_Tab[3]&0x00FF0000)>>16); //第13个字节
  399. cardinfo->SD_csd.MaxWrBlockLen|=(tmp&0xC0)>>6;
  400. cardinfo->SD_csd.WriteBlockPaPartial=(tmp&0x20)>>5;
  401. cardinfo->SD_csd.Reserved3=0;
  402. cardinfo->SD_csd.ContentProtectAppli=(tmp&0x01);
  403. tmp=(u8)((CSD_Tab[3]&0x0000FF00)>>8); //第14个字节
  404. cardinfo->SD_csd.FileFormatGrouop=(tmp&0x80)>>7;
  405. cardinfo->SD_csd.CopyFlag=(tmp&0x40)>>6;
  406. cardinfo->SD_csd.PermWrProtect=(tmp&0x20)>>5;
  407. cardinfo->SD_csd.TempWrProtect=(tmp&0x10)>>4;
  408. cardinfo->SD_csd.FileFormat=(tmp&0x0C)>>2;
  409. cardinfo->SD_csd.ECC=(tmp&0x03);
  410. tmp=(u8)(CSD_Tab[3]&0x000000FF); //第15个字节
  411. cardinfo->SD_csd.CSD_CRC=(tmp&0xFE)>>1;
  412. cardinfo->SD_csd.Reserved4=1;
  413. tmp=(u8)((CID_Tab[0]&0xFF000000)>>24); //第0个字节
  414. cardinfo->SD_cid.ManufacturerID=tmp;
  415. tmp=(u8)((CID_Tab[0]&0x00FF0000)>>16); //第1个字节
  416. cardinfo->SD_cid.OEM_AppliID=tmp<<8;
  417. tmp=(u8)((CID_Tab[0]&0x000000FF00)>>8); //第2个字节
  418. cardinfo->SD_cid.OEM_AppliID|=tmp;
  419. tmp=(u8)(CID_Tab[0]&0x000000FF); //第3个字节
  420. cardinfo->SD_cid.ProdName1=tmp<<24;
  421. tmp=(u8)((CID_Tab[1]&0xFF000000)>>24); //第4个字节
  422. cardinfo->SD_cid.ProdName1|=tmp<<16;
  423. tmp=(u8)((CID_Tab[1]&0x00FF0000)>>16); //第5个字节
  424. cardinfo->SD_cid.ProdName1|=tmp<<8;
  425. tmp=(u8)((CID_Tab[1]&0x0000FF00)>>8); //第6个字节
  426. cardinfo->SD_cid.ProdName1|=tmp;
  427. tmp=(u8)(CID_Tab[1]&0x000000FF); //第7个字节
  428. cardinfo->SD_cid.ProdName2=tmp;
  429. tmp=(u8)((CID_Tab[2]&0xFF000000)>>24); //第8个字节
  430. cardinfo->SD_cid.ProdRev=tmp;
  431. tmp=(u8)((CID_Tab[2]&0x00FF0000)>>16); //第9个字节
  432. cardinfo->SD_cid.ProdSN=tmp<<24;
  433. tmp=(u8)((CID_Tab[2]&0x0000FF00)>>8); //第10个字节
  434. cardinfo->SD_cid.ProdSN|=tmp<<16;
  435. tmp=(u8)(CID_Tab[2]&0x000000FF); //第11个字节
  436. cardinfo->SD_cid.ProdSN|=tmp<<8;
  437. tmp=(u8)((CID_Tab[3]&0xFF000000)>>24); //第12个字节
  438. cardinfo->SD_cid.ProdSN|=tmp;
  439. tmp=(u8)((CID_Tab[3]&0x00FF0000)>>16); //第13个字节
  440. cardinfo->SD_cid.Reserved1|=(tmp&0xF0)>>4;
  441. cardinfo->SD_cid.ManufactDate=(tmp&0x0F)<<8;
  442. tmp=(u8)((CID_Tab[3]&0x0000FF00)>>8); //第14个字节
  443. cardinfo->SD_cid.ManufactDate|=tmp;
  444. tmp=(u8)(CID_Tab[3]&0x000000FF); //第15个字节
  445. cardinfo->SD_cid.CID_CRC=(tmp&0xFE)>>1;
  446. cardinfo->SD_cid.Reserved2=1;
  447. return errorstatus;
  448. }
  449. //设置SDIO总线宽度(MMC卡不支持4bit模式)
  450. //wmode:位宽模式.0,1位数据宽度;1,4位数据宽度;2,8位数据宽度
  451. //返回值:SD卡错误状态
  452. //设置SDIO总线宽度(MMC卡不支持4bit模式)
  453. // @arg SDIO_BusWide_8b: 8-bit data transfer (Only for MMC)
  454. // @arg SDIO_BusWide_4b: 4-bit data transfer
  455. // @arg SDIO_BusWide_1b: 1-bit data transfer (默认)
  456. //返回值:SD卡错误状态
  457. SD_Error SD_EnableWideBusOperation(u32 WideMode)
  458. {
  459. SD_Error errorstatus=SD_OK;
  460. if (SDIO_MULTIMEDIA_CARD == CardType)
  461. {
  462. errorstatus = SD_UNSUPPORTED_FEATURE;
  463. return(errorstatus);
  464. }
  465. else if((SDIO_STD_CAPACITY_SD_CARD_V1_1==CardType)||(SDIO_STD_CAPACITY_SD_CARD_V2_0==CardType)||(SDIO_HIGH_CAPACITY_SD_CARD==CardType))
  466. {
  467. if (SDIO_BusWide_8b == WideMode) //2.0 sd不支持8bits
  468. {
  469. errorstatus = SD_UNSUPPORTED_FEATURE;
  470. return(errorstatus);
  471. }
  472. else
  473. {
  474. errorstatus=SDEnWideBus(WideMode);
  475. if(SD_OK==errorstatus)
  476. {
  477. SDIO->CLKCR&=~(3<<11); //清除之前的位宽设置
  478. SDIO->CLKCR|=WideMode;//1位/4位总线宽度
  479. SDIO->CLKCR|=0<<14; //不开启硬件流控制
  480. }
  481. }
  482. }
  483. return errorstatus;
  484. }
  485. //设置SD卡工作模式
  486. //Mode:
  487. //返回值:错误状态
  488. SD_Error SD_SetDeviceMode(u32 Mode)
  489. {
  490. SD_Error errorstatus = SD_OK;
  491. if((Mode==SD_DMA_MODE)||(Mode==SD_POLLING_MODE))
  492. {
  493. DeviceMode=Mode;
  494. }else {
  495. errorstatus=SD_INVALID_PARAMETER;
  496. }
  497. return errorstatus;
  498. }
  499. //选卡
  500. //发送CMD7,选择相对地址(rca)为addr的卡,取消其他卡.如果为0,则都不选择.
  501. //addr:卡的RCA地址
  502. SD_Error SD_SelectDeselect(u32 addr)
  503. {
  504. SDIO_CmdInitStructure.SDIO_Argument = addr;//发送CMD7,选择卡,短响应
  505. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEL_DESEL_CARD;
  506. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  507. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  508. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  509. SDIO_SendCommand(&SDIO_CmdInitStructure);//发送CMD7,选择卡,短响应
  510. return CmdResp1Error(SD_CMD_SEL_DESEL_CARD);
  511. }
  512. //SD卡读取一个块
  513. //buf:读数据缓存区(必须4字节对齐!!)
  514. //addr:读取地址
  515. //blksize:块大小
  516. SD_Error SD_ReadBlock(u8 *buf,long long addr,u16 blksize)
  517. {
  518. SD_Error errorstatus=SD_OK;
  519. u8 power;
  520. u32 count=0,*tempbuff=(u32*)buf;//转换为u32指针
  521. u32 timeout=SDIO_DATATIMEOUT;
  522. if(NULL==buf)
  523. return SD_INVALID_PARAMETER;
  524. SDIO->DCTRL=0x0; //数据控制寄存器清零(关DMA)
  525. if(CardType==SDIO_HIGH_CAPACITY_SD_CARD)//大容量卡
  526. {
  527. blksize=512;
  528. addr>>=9;
  529. }
  530. SDIO_DataInitStructure.SDIO_DataBlockSize= SDIO_DataBlockSize_1b ;//清除DPSM状态机配置
  531. SDIO_DataInitStructure.SDIO_DataLength= 0 ;
  532. SDIO_DataInitStructure.SDIO_DataTimeOut=SD_DATATIMEOUT ;
  533. SDIO_DataInitStructure.SDIO_DPSM=SDIO_DPSM_Enable;
  534. SDIO_DataInitStructure.SDIO_TransferDir=SDIO_TransferDir_ToCard;
  535. SDIO_DataInitStructure.SDIO_TransferMode=SDIO_TransferMode_Block;
  536. SDIO_DataConfig(&SDIO_DataInitStructure);
  537. if(SDIO->RESP1&SD_CARD_LOCKED)
  538. {
  539. return SD_LOCK_UNLOCK_FAILED;//卡锁了
  540. }
  541. if((blksize>0)&&(blksize<=2048)&&((blksize&(blksize-1))==0))
  542. {
  543. power=convert_from_bytes_to_power_of_two(blksize);
  544. SDIO_CmdInitStructure.SDIO_Argument = blksize;
  545. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
  546. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  547. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  548. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  549. SDIO_SendCommand(&SDIO_CmdInitStructure);//发送CMD16+设置数据长度为blksize,短响应
  550. errorstatus=CmdResp1Error(SD_CMD_SET_BLOCKLEN); //等待R1响应
  551. if(errorstatus!=SD_OK)
  552. {
  553. return errorstatus; //响应错误
  554. }
  555. }else {
  556. return SD_INVALID_PARAMETER;
  557. }
  558. SDIO_DataInitStructure.SDIO_DataBlockSize= power<<4 ;//清除DPSM状态机配置
  559. SDIO_DataInitStructure.SDIO_DataLength= blksize ;
  560. SDIO_DataInitStructure.SDIO_DataTimeOut=SD_DATATIMEOUT ;
  561. SDIO_DataInitStructure.SDIO_DPSM=SDIO_DPSM_Enable;
  562. SDIO_DataInitStructure.SDIO_TransferDir=SDIO_TransferDir_ToSDIO;
  563. SDIO_DataInitStructure.SDIO_TransferMode=SDIO_TransferMode_Block;
  564. SDIO_DataConfig(&SDIO_DataInitStructure);
  565. SDIO_CmdInitStructure.SDIO_Argument = addr;
  566. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_READ_SINGLE_BLOCK;
  567. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  568. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  569. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  570. SDIO_SendCommand(&SDIO_CmdInitStructure);//发送CMD17+从addr地址出读取数据,短响应
  571. errorstatus=CmdResp1Error(SD_CMD_READ_SINGLE_BLOCK);//等待R1响应
  572. if(errorstatus!=SD_OK)
  573. {
  574. return errorstatus; //响应错误
  575. }
  576. if(DeviceMode==SD_POLLING_MODE) //查询模式,轮询数据
  577. {
  578. INTX_DISABLE();//关闭总中断(POLLING模式,严禁中断打断SDIO读写操作!!!)
  579. while(!(SDIO->STA&((1<<5)|(1<<1)|(1<<3)|(1<<10)|(1<<9))))//无上溢/CRC/超时/完成(标志)/起始位错误
  580. {
  581. if(SDIO_GetFlagStatus(SDIO_FLAG_RXFIFOHF) != RESET) //接收区半满,表示至少存了8个字
  582. {
  583. for(count=0;count<8;count++) //循环读取数据
  584. {
  585. *(tempbuff+count)=SDIO->FIFO;
  586. }
  587. tempbuff+=8;
  588. timeout=0X7FFFFF; //读数据溢出时间
  589. }else //处理超时
  590. {
  591. if(timeout==0)return SD_DATA_TIMEOUT;
  592. timeout--;
  593. }
  594. }
  595. if(SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET) //数据超时错误
  596. {
  597. SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT); //清错误标志
  598. return SD_DATA_TIMEOUT;
  599. }else if(SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET) //数据块CRC错误
  600. {
  601. SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL); //清错误标志
  602. return SD_DATA_CRC_FAIL;
  603. }else if(SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET) //接收fifo上溢错误
  604. {
  605. SDIO_ClearFlag(SDIO_FLAG_RXOVERR); //清错误标志
  606. return SD_RX_OVERRUN;
  607. }else if(SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET) //接收起始位错误
  608. {
  609. SDIO_ClearFlag(SDIO_FLAG_STBITERR);//清错误标志
  610. return SD_START_BIT_ERR;
  611. }
  612. while(SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET) //FIFO里面,还存在可用数据
  613. {
  614. *tempbuff=SDIO->FIFO; //循环读取数据
  615. tempbuff++;
  616. }
  617. INTX_ENABLE();//开启总中断
  618. SDIO_ClearFlag(SDIO_STATIC_FLAGS);//清除所有标记
  619. }else if(DeviceMode==SD_DMA_MODE)
  620. {
  621. TransferError=SD_OK;
  622. StopCondition=0; //单块读,不需要发送停止传输指令
  623. TransferEnd=0; //传输结束标置位,在中断服务置1
  624. SDIO->MASK|=(1<<1)|(1<<3)|(1<<8)|(1<<5)|(1<<9); //配置需要的中断
  625. SDIO->DCTRL|=1<<3; //SDIO DMA使能
  626. SD_DMA_Config((u32*)buf,blksize,DMA_DIR_PeripheralToMemory);
  627. while(((DMA2->LISR&(1<<27))==RESET)&&(TransferEnd==0)&&(TransferError==SD_OK)&&timeout)
  628. timeout--;//等待传输完成
  629. if(timeout==0)
  630. {
  631. return SD_DATA_TIMEOUT;//超时
  632. }
  633. if(TransferError!=SD_OK)
  634. {
  635. errorstatus=TransferError;
  636. }
  637. }
  638. return errorstatus;
  639. }
  640. //SD卡读取多个块
  641. //buf:读数据缓存区
  642. //addr:读取地址
  643. //blksize:块大小
  644. //nblks:要读取的块数
  645. //返回值:错误状态
  646. __align(4) u32 *tempbuff;
  647. SD_Error SD_ReadMultiBlocks(u8 *buf,long long addr,u16 blksize,u32 nblks)
  648. {
  649. SD_Error errorstatus=SD_OK;
  650. u8 power;
  651. u32 count=0;
  652. u32 timeout=SDIO_DATATIMEOUT;
  653. tempbuff=(u32*)buf;//转换为u32指针
  654. SDIO->DCTRL=0x0; //数据控制寄存器清零(关DMA)
  655. if(CardType==SDIO_HIGH_CAPACITY_SD_CARD)//大容量卡
  656. {
  657. blksize=512;
  658. addr>>=9;
  659. }
  660. SDIO_DataInitStructure.SDIO_DataBlockSize= 0; ;//清除DPSM状态机配置
  661. SDIO_DataInitStructure.SDIO_DataLength= 0 ;
  662. SDIO_DataInitStructure.SDIO_DataTimeOut=SD_DATATIMEOUT ;
  663. SDIO_DataInitStructure.SDIO_DPSM=SDIO_DPSM_Enable;
  664. SDIO_DataInitStructure.SDIO_TransferDir=SDIO_TransferDir_ToCard;
  665. SDIO_DataInitStructure.SDIO_TransferMode=SDIO_TransferMode_Block;
  666. SDIO_DataConfig(&SDIO_DataInitStructure);
  667. if(SDIO->RESP1&SD_CARD_LOCKED)return SD_LOCK_UNLOCK_FAILED;//卡锁了
  668. if((blksize>0)&&(blksize<=2048)&&((blksize&(blksize-1))==0))
  669. {
  670. power=convert_from_bytes_to_power_of_two(blksize);
  671. SDIO_CmdInitStructure.SDIO_Argument = blksize;//发送CMD16+设置数据长度为blksize,短响应
  672. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
  673. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  674. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  675. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  676. SDIO_SendCommand(&SDIO_CmdInitStructure);
  677. errorstatus=CmdResp1Error(SD_CMD_SET_BLOCKLEN); //等待R1响应
  678. if(errorstatus!=SD_OK)return errorstatus; //响应错误
  679. }else return SD_INVALID_PARAMETER;
  680. if(nblks>1) //多块读
  681. {
  682. if(nblks*blksize>SD_MAX_DATA_LENGTH)return SD_INVALID_PARAMETER;//判断是否超过最大接收长度
  683. SDIO_DataInitStructure.SDIO_DataBlockSize= power<<4; ;//nblks*blksize,512块大小,卡到控制器
  684. SDIO_DataInitStructure.SDIO_DataLength= nblks*blksize ;
  685. SDIO_DataInitStructure.SDIO_DataTimeOut=SD_DATATIMEOUT ;
  686. SDIO_DataInitStructure.SDIO_DPSM=SDIO_DPSM_Enable;
  687. SDIO_DataInitStructure.SDIO_TransferDir=SDIO_TransferDir_ToSDIO;
  688. SDIO_DataInitStructure.SDIO_TransferMode=SDIO_TransferMode_Block;
  689. SDIO_DataConfig(&SDIO_DataInitStructure);
  690. SDIO_CmdInitStructure.SDIO_Argument = addr;//发送CMD18+从addr地址出读取数据,短响应
  691. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_READ_MULT_BLOCK;
  692. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  693. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  694. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  695. SDIO_SendCommand(&SDIO_CmdInitStructure);
  696. errorstatus=CmdResp1Error(SD_CMD_READ_MULT_BLOCK);//等待R1响应
  697. if(errorstatus!=SD_OK)return errorstatus; //响应错误
  698. if(DeviceMode==SD_POLLING_MODE)
  699. {
  700. INTX_DISABLE();//关闭总中断(POLLING模式,严禁中断打断SDIO读写操作!!!)
  701. while(!(SDIO->STA&((1<<5)|(1<<1)|(1<<3)|(1<<8)|(1<<9))))//无上溢/CRC/超时/完成(标志)/起始位错误
  702. {
  703. if(SDIO_GetFlagStatus(SDIO_FLAG_RXFIFOHF) != RESET) //接收区半满,表示至少存了8个字
  704. {
  705. for(count=0;count<8;count++) //循环读取数据
  706. {
  707. *(tempbuff+count)=SDIO->FIFO;
  708. }
  709. tempbuff+=8;
  710. timeout=0X7FFFFF; //读数据溢出时间
  711. }else //处理超时
  712. {
  713. if(timeout==0)return SD_DATA_TIMEOUT;
  714. timeout--;
  715. }
  716. }
  717. if(SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET) //数据超时错误
  718. {
  719. SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT); //清错误标志
  720. return SD_DATA_TIMEOUT;
  721. }else if(SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET) //数据块CRC错误
  722. {
  723. SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL); //清错误标志
  724. return SD_DATA_CRC_FAIL;
  725. }else if(SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET) //接收fifo上溢错误
  726. {
  727. SDIO_ClearFlag(SDIO_FLAG_RXOVERR); //清错误标志
  728. return SD_RX_OVERRUN;
  729. }else if(SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET) //接收起始位错误
  730. {
  731. SDIO_ClearFlag(SDIO_FLAG_STBITERR);//清错误标志
  732. return SD_START_BIT_ERR;
  733. }
  734. while(SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET) //FIFO里面,还存在可用数据
  735. {
  736. *tempbuff=SDIO->FIFO; //循环读取数据
  737. tempbuff++;
  738. }
  739. if(SDIO_GetFlagStatus(SDIO_FLAG_DATAEND) != RESET) //接收结束
  740. {
  741. if((SDIO_STD_CAPACITY_SD_CARD_V1_1==CardType)||(SDIO_STD_CAPACITY_SD_CARD_V2_0==CardType)||(SDIO_HIGH_CAPACITY_SD_CARD==CardType))
  742. {
  743. SDIO_CmdInitStructure.SDIO_Argument = 0;//发送CMD12+结束传输
  744. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_STOP_TRANSMISSION;
  745. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  746. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  747. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  748. SDIO_SendCommand(&SDIO_CmdInitStructure);
  749. errorstatus=CmdResp1Error(SD_CMD_STOP_TRANSMISSION);//等待R1响应
  750. if(errorstatus!=SD_OK)return errorstatus;
  751. }
  752. }
  753. INTX_ENABLE();//开启总中断
  754. SDIO_ClearFlag(SDIO_STATIC_FLAGS);//清除所有标记
  755. }else if(DeviceMode==SD_DMA_MODE)
  756. {
  757. TransferError=SD_OK;
  758. StopCondition=1; //多块读,需要发送停止传输指令
  759. TransferEnd=0; //传输结束标置位,在中断服务置1
  760. SDIO->MASK|=(1<<1)|(1<<3)|(1<<8)|(1<<5)|(1<<9); //配置需要的中断
  761. SDIO->DCTRL|=1<<3; //SDIO DMA使能
  762. SD_DMA_Config((u32*)buf,nblks*blksize,DMA_DIR_PeripheralToMemory);
  763. while(((DMA2->LISR&(1<<27))==RESET)&&timeout)timeout--;//等待传输完成
  764. if(timeout==0)return SD_DATA_TIMEOUT;//超时
  765. while((TransferEnd==0)&&(TransferError==SD_OK));
  766. if(TransferError!=SD_OK)errorstatus=TransferError;
  767. }
  768. }
  769. return errorstatus;
  770. }
  771. //SD卡写1个块
  772. //buf:数据缓存区
  773. //addr:写地址
  774. //blksize:块大小
  775. //返回值:错误状态
  776. SD_Error SD_WriteBlock(u8 *buf,long long addr, u16 blksize)
  777. {
  778. SD_Error errorstatus = SD_OK;
  779. u8 power=0,cardstate=0;
  780. u32 timeout=0,bytestransferred=0;
  781. u32 cardstatus=0,count=0,restwords=0;
  782. u32 tlen=blksize; //总长度(字节)
  783. u32*tempbuff=(u32*)buf;
  784. if(buf==NULL)return SD_INVALID_PARAMETER;//参数错误
  785. SDIO->DCTRL=0x0; //数据控制寄存器清零(关DMA)
  786. SDIO_DataInitStructure.SDIO_DataBlockSize= 0; ;//清除DPSM状态机配置
  787. SDIO_DataInitStructure.SDIO_DataLength= 0 ;
  788. SDIO_DataInitStructure.SDIO_DataTimeOut=SD_DATATIMEOUT ;
  789. SDIO_DataInitStructure.SDIO_DPSM=SDIO_DPSM_Enable;
  790. SDIO_DataInitStructure.SDIO_TransferDir=SDIO_TransferDir_ToCard;
  791. SDIO_DataInitStructure.SDIO_TransferMode=SDIO_TransferMode_Block;
  792. SDIO_DataConfig(&SDIO_DataInitStructure);
  793. if(SDIO->RESP1&SD_CARD_LOCKED)return SD_LOCK_UNLOCK_FAILED;//卡锁了
  794. if(CardType==SDIO_HIGH_CAPACITY_SD_CARD) //大容量卡
  795. {
  796. blksize=512;
  797. addr>>=9;
  798. }
  799. if((blksize>0)&&(blksize<=2048)&&((blksize&(blksize-1))==0))
  800. {
  801. power=convert_from_bytes_to_power_of_two(blksize);
  802. SDIO_CmdInitStructure.SDIO_Argument = blksize;//发送CMD16+设置数据长度为blksize,短响应
  803. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
  804. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  805. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  806. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  807. SDIO_SendCommand(&SDIO_CmdInitStructure);
  808. errorstatus=CmdResp1Error(SD_CMD_SET_BLOCKLEN); //等待R1响应
  809. if(errorstatus!=SD_OK)return errorstatus; //响应错误
  810. }else return SD_INVALID_PARAMETER;
  811. SDIO_CmdInitStructure.SDIO_Argument = (u32)RCA<<16;//发送CMD13,查询卡的状态,短响应
  812. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_STATUS;
  813. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  814. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  815. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  816. SDIO_SendCommand(&SDIO_CmdInitStructure);
  817. errorstatus=CmdResp1Error(SD_CMD_SEND_STATUS); //等待R1响应
  818. if(errorstatus!=SD_OK)return errorstatus;
  819. cardstatus=SDIO->RESP1;
  820. timeout=SD_DATATIMEOUT;
  821. while(((cardstatus&0x00000100)==0)&&(timeout>0)) //检查READY_FOR_DATA位是否置位
  822. {
  823. timeout--;
  824. SDIO_CmdInitStructure.SDIO_Argument = (u32)RCA<<16;//发送CMD13,查询卡的状态,短响应
  825. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_STATUS;
  826. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  827. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  828. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  829. SDIO_SendCommand(&SDIO_CmdInitStructure);
  830. errorstatus=CmdResp1Error(SD_CMD_SEND_STATUS); //等待R1响应
  831. if(errorstatus!=SD_OK)return errorstatus;
  832. cardstatus=SDIO->RESP1;
  833. }
  834. if(timeout==0)return SD_ERROR;
  835. SDIO_CmdInitStructure.SDIO_Argument = addr;//发送CMD24,写单块指令,短响应
  836. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_WRITE_SINGLE_BLOCK;
  837. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  838. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  839. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  840. SDIO_SendCommand(&SDIO_CmdInitStructure);
  841. errorstatus=CmdResp1Error(SD_CMD_WRITE_SINGLE_BLOCK);//等待R1响应
  842. if(errorstatus!=SD_OK)return errorstatus;
  843. StopCondition=0; //单块写,不需要发送停止传输指令
  844. SDIO_DataInitStructure.SDIO_DataBlockSize= power<<4; ; //blksize, 控制器到卡
  845. SDIO_DataInitStructure.SDIO_DataLength= blksize ;
  846. SDIO_DataInitStructure.SDIO_DataTimeOut=SD_DATATIMEOUT ;
  847. SDIO_DataInitStructure.SDIO_DPSM=SDIO_DPSM_Enable;
  848. SDIO_DataInitStructure.SDIO_TransferDir=SDIO_TransferDir_ToCard;
  849. SDIO_DataInitStructure.SDIO_TransferMode=SDIO_TransferMode_Block;
  850. SDIO_DataConfig(&SDIO_DataInitStructure);
  851. timeout=SDIO_DATATIMEOUT;
  852. if (DeviceMode == SD_POLLING_MODE)
  853. {
  854. INTX_DISABLE();//关闭总中断(POLLING模式,严禁中断打断SDIO读写操作!!!)
  855. while(!(SDIO->STA&((1<<10)|(1<<4)|(1<<1)|(1<<3)|(1<<9))))//数据块发送成功/下溢/CRC/超时/起始位错误
  856. {
  857. if(SDIO_GetFlagStatus(SDIO_FLAG_TXFIFOHE) != RESET) //发送区半空,表示至少存了8个字
  858. {
  859. if((tlen-bytestransferred)<SD_HALFFIFOBYTES)//不够32字节了
  860. {
  861. restwords=((tlen-bytestransferred)%4==0)?((tlen-bytestransferred)/4):((tlen-bytestransferred)/4+1);
  862. for(count=0;count<restwords;count++,tempbuff++,bytestransferred+=4)
  863. {
  864. SDIO->FIFO=*tempbuff;
  865. }
  866. }else
  867. {
  868. for(count=0;count<8;count++)
  869. {
  870. SDIO->FIFO=*(tempbuff+count);
  871. }
  872. tempbuff+=8;
  873. bytestransferred+=32;
  874. }
  875. timeout=0X3FFFFFFF; //写数据溢出时间
  876. }else
  877. {
  878. if(timeout==0)return SD_DATA_TIMEOUT;
  879. timeout--;
  880. }
  881. }
  882. if(SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET) //数据超时错误
  883. {
  884. SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT); //清错误标志
  885. return SD_DATA_TIMEOUT;
  886. }else if(SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET) //数据块CRC错误
  887. {
  888. SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL); //清错误标志
  889. return SD_DATA_CRC_FAIL;
  890. }else if(SDIO_GetFlagStatus(SDIO_FLAG_TXUNDERR) != RESET) //接收fifo下溢错误
  891. {
  892. SDIO_ClearFlag(SDIO_FLAG_TXUNDERR); //清错误标志
  893. return SD_TX_UNDERRUN;
  894. }else if(SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET) //接收起始位错误
  895. {
  896. SDIO_ClearFlag(SDIO_FLAG_STBITERR);//清错误标志
  897. return SD_START_BIT_ERR;
  898. }
  899. INTX_ENABLE();//开启总中断
  900. SDIO_ClearFlag(SDIO_STATIC_FLAGS);//清除所有标记
  901. }else if(DeviceMode==SD_DMA_MODE)
  902. {
  903. TransferError=SD_OK;
  904. StopCondition=0; //单块写,不需要发送停止传输指令
  905. TransferEnd=0; //传输结束标置位,在中断服务置1
  906. SDIO->MASK|=(1<<1)|(1<<3)|(1<<8)|(1<<4)|(1<<9); //配置产生数据接收完成中断
  907. SD_DMA_Config((u32*)buf,blksize,DMA_DIR_MemoryToPeripheral); //SDIO DMA配置
  908. SDIO->DCTRL|=1<<3; //SDIO DMA使能.
  909. while(((DMA2->LISR&(1<<27))==RESET)&&timeout)timeout--;//等待传输完成
  910. if(timeout==0)
  911. {
  912. SD_Init(); //重新初始化SD卡,可以解决写入死机的问题
  913. return SD_DATA_TIMEOUT; //超时
  914. }
  915. timeout=SDIO_DATATIMEOUT;
  916. while((TransferEnd==0)&&(TransferError==SD_OK)&&timeout)timeout--;
  917. if(timeout==0)return SD_DATA_TIMEOUT; //超时
  918. if(TransferError!=SD_OK)return TransferError;
  919. }
  920. SDIO_ClearFlag(SDIO_STATIC_FLAGS);//清除所有标记
  921. errorstatus=IsCardProgramming(&cardstate);
  922. while((errorstatus==SD_OK)&&((cardstate==SD_CARD_PROGRAMMING)||(cardstate==SD_CARD_RECEIVING)))
  923. {
  924. errorstatus=IsCardProgramming(&cardstate);
  925. }
  926. return errorstatus;
  927. }
  928. //SD卡写多个块
  929. //buf:数据缓存区
  930. //addr:写地址
  931. //blksize:块大小
  932. //nblks:要写入的块数
  933. //返回值:错误状态
  934. SD_Error SD_WriteMultiBlocks(u8 *buf,long long addr,u16 blksize,u32 nblks)
  935. {
  936. SD_Error errorstatus = SD_OK;
  937. u8 power = 0, cardstate = 0;
  938. u32 timeout=0,bytestransferred=0;
  939. u32 count = 0, restwords = 0;
  940. u32 tlen=nblks*blksize; //总长度(字节)
  941. u32 *tempbuff = (u32*)buf;
  942. if(buf==NULL)return SD_INVALID_PARAMETER; //参数错误
  943. SDIO->DCTRL=0x0; //数据控制寄存器清零(关DMA)
  944. SDIO_DataInitStructure.SDIO_DataBlockSize= 0; ; //清除DPSM状态机配置
  945. SDIO_DataInitStructure.SDIO_DataLength= 0 ;
  946. SDIO_DataInitStructure.SDIO_DataTimeOut=SD_DATATIMEOUT ;
  947. SDIO_DataInitStructure.SDIO_DPSM=SDIO_DPSM_Enable;
  948. SDIO_DataInitStructure.SDIO_TransferDir=SDIO_TransferDir_ToCard;
  949. SDIO_DataInitStructure.SDIO_TransferMode=SDIO_TransferMode_Block;
  950. SDIO_DataConfig(&SDIO_DataInitStructure);
  951. if(SDIO->RESP1&SD_CARD_LOCKED)return SD_LOCK_UNLOCK_FAILED;//卡锁了
  952. if(CardType==SDIO_HIGH_CAPACITY_SD_CARD)//大容量卡
  953. {
  954. blksize=512;
  955. addr>>=9;
  956. }
  957. if((blksize>0)&&(blksize<=2048)&&((blksize&(blksize-1))==0))
  958. {
  959. power=convert_from_bytes_to_power_of_two(blksize);
  960. SDIO_CmdInitStructure.SDIO_Argument = blksize; //发送CMD16+设置数据长度为blksize,短响应
  961. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
  962. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  963. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  964. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  965. SDIO_SendCommand(&SDIO_CmdInitStructure);
  966. errorstatus=CmdResp1Error(SD_CMD_SET_BLOCKLEN); //等待R1响应
  967. if(errorstatus!=SD_OK)return errorstatus; //响应错误
  968. }else return SD_INVALID_PARAMETER;
  969. if(nblks>1)
  970. {
  971. if(nblks*blksize>SD_MAX_DATA_LENGTH)return SD_INVALID_PARAMETER;
  972. if((SDIO_STD_CAPACITY_SD_CARD_V1_1==CardType)||(SDIO_STD_CAPACITY_SD_CARD_V2_0==CardType)||(SDIO_HIGH_CAPACITY_SD_CARD==CardType))
  973. {
  974. //提高性能
  975. SDIO_CmdInitStructure.SDIO_Argument = (u32)RCA<<16; //发送ACMD55,短响应
  976. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
  977. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  978. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  979. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  980. SDIO_SendCommand(&SDIO_CmdInitStructure);
  981. errorstatus=CmdResp1Error(SD_CMD_APP_CMD); //等待R1响应
  982. if(errorstatus!=SD_OK)return errorstatus;
  983. SDIO_CmdInitStructure.SDIO_Argument =nblks; //发送CMD23,设置块数量,短响应
  984. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCK_COUNT;
  985. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  986. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  987. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  988. SDIO_SendCommand(&SDIO_CmdInitStructure);
  989. errorstatus=CmdResp1Error(SD_CMD_SET_BLOCK_COUNT);//等待R1响应
  990. if(errorstatus!=SD_OK)return errorstatus;
  991. }
  992. SDIO_CmdInitStructure.SDIO_Argument =addr; //发送CMD25,多块写指令,短响应
  993. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_WRITE_MULT_BLOCK;
  994. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  995. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  996. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  997. SDIO_SendCommand(&SDIO_CmdInitStructure);
  998. errorstatus=CmdResp1Error(SD_CMD_WRITE_MULT_BLOCK); //等待R1响应
  999. if(errorstatus!=SD_OK)return errorstatus;
  1000. SDIO_DataInitStructure.SDIO_DataBlockSize= power<<4; ; //blksize, 控制器到卡
  1001. SDIO_DataInitStructure.SDIO_DataLength= nblks*blksize ;
  1002. SDIO_DataInitStructure.SDIO_DataTimeOut=SD_DATATIMEOUT ;
  1003. SDIO_DataInitStructure.SDIO_DPSM=SDIO_DPSM_Enable;
  1004. SDIO_DataInitStructure.SDIO_TransferDir=SDIO_TransferDir_ToCard;
  1005. SDIO_DataInitStructure.SDIO_TransferMode=SDIO_TransferMode_Block;
  1006. SDIO_DataConfig(&SDIO_DataInitStructure);
  1007. if(DeviceMode==SD_POLLING_MODE)
  1008. {
  1009. timeout=SDIO_DATATIMEOUT;
  1010. INTX_DISABLE();//关闭总中断(POLLING模式,严禁中断打断SDIO读写操作!!!)
  1011. while(!(SDIO->STA&((1<<4)|(1<<1)|(1<<8)|(1<<3)|(1<<9))))//下溢/CRC/数据结束/超时/起始位错误
  1012. {
  1013. if(SDIO_GetFlagStatus(SDIO_FLAG_TXFIFOHE) != RESET) //发送区半空,表示至少存了8字(32字节)
  1014. {
  1015. if((tlen-bytestransferred)<SD_HALFFIFOBYTES)//不够32字节了
  1016. {
  1017. restwords=((tlen-bytestransferred)%4==0)?((tlen-bytestransferred)/4):((tlen-bytestransferred)/4+1);
  1018. for(count=0;count<restwords;count++,tempbuff++,bytestransferred+=4)
  1019. {
  1020. SDIO->FIFO=*tempbuff;
  1021. }
  1022. }else //发送区半空,可以发送至少8字(32字节)数据
  1023. {
  1024. for(count=0;count<SD_HALFFIFO;count++)
  1025. {
  1026. SDIO->FIFO=*(tempbuff+count);
  1027. }
  1028. tempbuff+=SD_HALFFIFO;
  1029. bytestransferred+=SD_HALFFIFOBYTES;
  1030. }
  1031. timeout=0X3FFFFFFF; //写数据溢出时间
  1032. }else
  1033. {
  1034. if(timeout==0)return SD_DATA_TIMEOUT;
  1035. timeout--;
  1036. }
  1037. }
  1038. if(SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET) //数据超时错误
  1039. {
  1040. SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT); //清错误标志
  1041. return SD_DATA_TIMEOUT;
  1042. }else if(SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET) //数据块CRC错误
  1043. {
  1044. SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL); //清错误标志
  1045. return SD_DATA_CRC_FAIL;
  1046. }else if(SDIO_GetFlagStatus(SDIO_FLAG_TXUNDERR) != RESET) //接收fifo下溢错误
  1047. {
  1048. SDIO_ClearFlag(SDIO_FLAG_TXUNDERR); //清错误标志
  1049. return SD_TX_UNDERRUN;
  1050. }else if(SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET) //接收起始位错误
  1051. {
  1052. SDIO_ClearFlag(SDIO_FLAG_STBITERR);//清错误标志
  1053. return SD_START_BIT_ERR;
  1054. }
  1055. if(SDIO_GetFlagStatus(SDIO_FLAG_DATAEND) != RESET) //发送结束
  1056. {
  1057. if((SDIO_STD_CAPACITY_SD_CARD_V1_1==CardType)||(SDIO_STD_CAPACITY_SD_CARD_V2_0==CardType)||(SDIO_HIGH_CAPACITY_SD_CARD==CardType))
  1058. {
  1059. SDIO_CmdInitStructure.SDIO_Argument =0;//发送CMD12+结束传输
  1060. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_STOP_TRANSMISSION;
  1061. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  1062. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  1063. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  1064. SDIO_SendCommand(&SDIO_CmdInitStructure);
  1065. errorstatus=CmdResp1Error(SD_CMD_STOP_TRANSMISSION);//等待R1响应
  1066. if(errorstatus!=SD_OK)return errorstatus;
  1067. }
  1068. }
  1069. INTX_ENABLE();//开启总中断
  1070. SDIO_ClearFlag(SDIO_STATIC_FLAGS);//清除所有标记
  1071. }else if(DeviceMode==SD_DMA_MODE)
  1072. {
  1073. TransferError=SD_OK;
  1074. StopCondition=1; //多块写,需要发送停止传输指令
  1075. TransferEnd=0; //传输结束标置位,在中断服务置1
  1076. SDIO->MASK|=(1<<1)|(1<<3)|(1<<8)|(1<<4)|(1<<9); //配置产生数据接收完成中断
  1077. SD_DMA_Config((u32*)buf,nblks*blksize,DMA_DIR_MemoryToPeripheral); //SDIO DMA配置
  1078. SDIO->DCTRL|=1<<3; //SDIO DMA使能.
  1079. timeout=SDIO_DATATIMEOUT;
  1080. while(((DMA2->LISR&(1<<27))==RESET)&&timeout)timeout--;//等待传输完成
  1081. if(timeout==0) //超时
  1082. {
  1083. SD_Init(); //重新初始化SD卡,可以解决写入死机的问题
  1084. return SD_DATA_TIMEOUT; //超时
  1085. }
  1086. timeout=SDIO_DATATIMEOUT;
  1087. while((TransferEnd==0)&&(TransferError==SD_OK)&&timeout)timeout--;
  1088. if(timeout==0)return SD_DATA_TIMEOUT; //超时
  1089. if(TransferError!=SD_OK)return TransferError;
  1090. }
  1091. }
  1092. SDIO_ClearFlag(SDIO_STATIC_FLAGS);//清除所有标记
  1093. errorstatus=IsCardProgramming(&cardstate);
  1094. while((errorstatus==SD_OK)&&((cardstate==SD_CARD_PROGRAMMING)||(cardstate==SD_CARD_RECEIVING)))
  1095. {
  1096. errorstatus=IsCardProgramming(&cardstate);
  1097. }
  1098. return errorstatus;
  1099. }
  1100. //SDIO中断服务函数
  1101. void SDIO_IRQHandler(void)
  1102. {
  1103. SD_ProcessIRQSrc();//处理所有SDIO相关中断
  1104. }
  1105. //SDIO中断处理函数
  1106. //处理SDIO传输过程中的各种中断事务
  1107. //返回值:错误代码
  1108. SD_Error SD_ProcessIRQSrc(void)
  1109. {
  1110. if(SDIO_GetFlagStatus(SDIO_FLAG_DATAEND) != RESET)//接收完成中断
  1111. {
  1112. if (StopCondition==1)
  1113. {
  1114. SDIO_CmdInitStructure.SDIO_Argument =0;//发送CMD12+结束传输
  1115. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_STOP_TRANSMISSION;
  1116. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  1117. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  1118. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  1119. SDIO_SendCommand(&SDIO_CmdInitStructure);
  1120. TransferError=CmdResp1Error(SD_CMD_STOP_TRANSMISSION);
  1121. }else TransferError = SD_OK;
  1122. SDIO->ICR|=1<<8;//清除完成中断标记
  1123. SDIO->MASK&=~((1<<1)|(1<<3)|(1<<8)|(1<<14)|(1<<15)|(1<<4)|(1<<5)|(1<<9));//关闭相关中断
  1124. TransferEnd = 1;
  1125. return(TransferError);
  1126. }
  1127. if(SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET)//数据CRC错误
  1128. {
  1129. SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL); //清错误标志
  1130. SDIO->MASK&=~((1<<1)|(1<<3)|(1<<8)|(1<<14)|(1<<15)|(1<<4)|(1<<5)|(1<<9));//关闭相关中断
  1131. TransferError = SD_DATA_CRC_FAIL;
  1132. return(SD_DATA_CRC_FAIL);
  1133. }
  1134. if(SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET)//数据超时错误
  1135. {
  1136. SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT); //清中断标志
  1137. SDIO->MASK&=~((1<<1)|(1<<3)|(1<<8)|(1<<14)|(1<<15)|(1<<4)|(1<<5)|(1<<9));//关闭相关中断
  1138. TransferError = SD_DATA_TIMEOUT;
  1139. return(SD_DATA_TIMEOUT);
  1140. }
  1141. if(SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET)//FIFO上溢错误
  1142. {
  1143. SDIO_ClearFlag(SDIO_FLAG_RXOVERR); //清中断标志
  1144. SDIO->MASK&=~((1<<1)|(1<<3)|(1<<8)|(1<<14)|(1<<15)|(1<<4)|(1<<5)|(1<<9));//关闭相关中断
  1145. TransferError = SD_RX_OVERRUN;
  1146. return(SD_RX_OVERRUN);
  1147. }
  1148. if(SDIO_GetFlagStatus(SDIO_FLAG_TXUNDERR) != RESET)//FIFO下溢错误
  1149. {
  1150. SDIO_ClearFlag(SDIO_FLAG_TXUNDERR); //清中断标志
  1151. SDIO->MASK&=~((1<<1)|(1<<3)|(1<<8)|(1<<14)|(1<<15)|(1<<4)|(1<<5)|(1<<9));//关闭相关中断
  1152. TransferError = SD_TX_UNDERRUN;
  1153. return(SD_TX_UNDERRUN);
  1154. }
  1155. if(SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET)//起始位错误
  1156. {
  1157. SDIO_ClearFlag(SDIO_FLAG_STBITERR); //清中断标志
  1158. SDIO->MASK&=~((1<<1)|(1<<3)|(1<<8)|(1<<14)|(1<<15)|(1<<4)|(1<<5)|(1<<9));//关闭相关中断
  1159. TransferError = SD_START_BIT_ERR;
  1160. return(SD_START_BIT_ERR);
  1161. }
  1162. return(SD_OK);
  1163. }
  1164. //检查CMD0的执行状态
  1165. //返回值:sd卡错误码
  1166. SD_Error CmdError(void)
  1167. {
  1168. SD_Error errorstatus = SD_OK;
  1169. u32 timeout=SDIO_CMD0TIMEOUT;
  1170. while(timeout--)
  1171. {
  1172. if(SDIO_GetFlagStatus(SDIO_FLAG_CMDSENT) != RESET)break; //命令已发送(无需响应)
  1173. }
  1174. if(timeout==0)return SD_CMD_RSP_TIMEOUT;
  1175. SDIO_ClearFlag(SDIO_STATIC_FLAGS);//清除所有标记
  1176. return errorstatus;
  1177. }
  1178. //检查R7响应的错误状态
  1179. //返回值:sd卡错误码
  1180. SD_Error CmdResp7Error(void)
  1181. {
  1182. SD_Error errorstatus=SD_OK;
  1183. u32 status;
  1184. u32 timeout=SDIO_CMD0TIMEOUT;
  1185. while(timeout--)
  1186. {
  1187. status=SDIO->STA;
  1188. if(status&((1<<0)|(1<<2)|(1<<6)))break;//CRC错误/命令响应超时/已经收到响应(CRC校验成功)
  1189. }
  1190. if((timeout==0)||(status&(1<<2))) //响应超时
  1191. {
  1192. errorstatus=SD_CMD_RSP_TIMEOUT; //当前卡不是2.0兼容卡,或者不支持设定的电压范围
  1193. SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT); //清除命令响应超时标志
  1194. return errorstatus;
  1195. }
  1196. if(status&1<<6) //成功接收到响应
  1197. {
  1198. errorstatus=SD_OK;
  1199. SDIO_ClearFlag(SDIO_FLAG_CMDREND); //清除响应标志
  1200. }
  1201. return errorstatus;
  1202. }
  1203. //检查R1响应的错误状态
  1204. //cmd:当前命令
  1205. //返回值:sd卡错误码
  1206. SD_Error CmdResp1Error(u8 cmd)
  1207. {
  1208. u32 status;
  1209. while(1)
  1210. {
  1211. status=SDIO->STA;
  1212. if(status&((1<<0)|(1<<2)|(1<<6)))break;//CRC错误/命令响应超时/已经收到响应(CRC校验成功)
  1213. }
  1214. if(SDIO_GetFlagStatus(SDIO_FLAG_CTIMEOUT) != RESET) //响应超时
  1215. {
  1216. SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT); //清除命令响应超时标志
  1217. return SD_CMD_RSP_TIMEOUT;
  1218. }
  1219. if(SDIO_GetFlagStatus(SDIO_FLAG_CCRCFAIL) != RESET) //CRC错误
  1220. {
  1221. SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL); //清除标志
  1222. return SD_CMD_CRC_FAIL;
  1223. }
  1224. if(SDIO->RESPCMD!=cmd)return SD_ILLEGAL_CMD;//命令不匹配
  1225. SDIO_ClearFlag(SDIO_STATIC_FLAGS);//清除所有标记
  1226. return (SD_Error)(SDIO->RESP1&SD_OCR_ERRORBITS);//返回卡响应
  1227. }
  1228. //检查R3响应的错误状态
  1229. //返回值:错误状态
  1230. SD_Error CmdResp3Error(void)
  1231. {
  1232. u32 status;
  1233. while(1)
  1234. {
  1235. status=SDIO->STA;
  1236. if(status&((1<<0)|(1<<2)|(1<<6)))break;//CRC错误/命令响应超时/已经收到响应(CRC校验成功)
  1237. }
  1238. if(SDIO_GetFlagStatus(SDIO_FLAG_CTIMEOUT) != RESET) //响应超时
  1239. {
  1240. SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT); //清除命令响应超时标志
  1241. return SD_CMD_RSP_TIMEOUT;
  1242. }
  1243. SDIO_ClearFlag(SDIO_STATIC_FLAGS);//清除所有标记
  1244. return SD_OK;
  1245. }
  1246. //检查R2响应的错误状态
  1247. //返回值:错误状态
  1248. SD_Error CmdResp2Error(void)
  1249. {
  1250. SD_Error errorstatus=SD_OK;
  1251. u32 status;
  1252. u32 timeout=SDIO_CMD0TIMEOUT;
  1253. while(timeout--)
  1254. {
  1255. status=SDIO->STA;
  1256. if(status&((1<<0)|(1<<2)|(1<<6)))break;//CRC错误/命令响应超时/已经收到响应(CRC校验成功)
  1257. }
  1258. if((timeout==0)||(status&(1<<2))) //响应超时
  1259. {
  1260. errorstatus=SD_CMD_RSP_TIMEOUT;
  1261. SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT); //清除命令响应超时标志
  1262. return errorstatus;
  1263. }
  1264. if(SDIO_GetFlagStatus(SDIO_FLAG_CCRCFAIL) != RESET) //CRC错误
  1265. {
  1266. errorstatus=SD_CMD_CRC_FAIL;
  1267. SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL); //清除响应标志
  1268. }
  1269. SDIO_ClearFlag(SDIO_STATIC_FLAGS);//清除所有标记
  1270. return errorstatus;
  1271. }
  1272. //检查R6响应的错误状态
  1273. //cmd:之前发送的命令
  1274. //prca:卡返回的RCA地址
  1275. //返回值:错误状态
  1276. SD_Error CmdResp6Error(u8 cmd,u16*prca)
  1277. {
  1278. SD_Error errorstatus=SD_OK;
  1279. u32 status;
  1280. u32 rspr1;
  1281. while(1)
  1282. {
  1283. status=SDIO->STA;
  1284. if(status&((1<<0)|(1<<2)|(1<<6)))break;//CRC错误/命令响应超时/已经收到响应(CRC校验成功)
  1285. }
  1286. if(SDIO_GetFlagStatus(SDIO_FLAG_CTIMEOUT) != RESET) //响应超时
  1287. {
  1288. SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT); //清除命令响应超时标志
  1289. return SD_CMD_RSP_TIMEOUT;
  1290. }
  1291. if(SDIO_GetFlagStatus(SDIO_FLAG_CCRCFAIL) != RESET) //CRC错误
  1292. {
  1293. SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL); //清除响应标志
  1294. return SD_CMD_CRC_FAIL;
  1295. }
  1296. if(SDIO->RESPCMD!=cmd) //判断是否响应cmd命令
  1297. {
  1298. return SD_ILLEGAL_CMD;
  1299. }
  1300. SDIO_ClearFlag(SDIO_STATIC_FLAGS);//清除所有标记
  1301. rspr1=SDIO->RESP1; //得到响应
  1302. if(SD_ALLZERO==(rspr1&(SD_R6_GENERAL_UNKNOWN_ERROR|SD_R6_ILLEGAL_CMD|SD_R6_COM_CRC_FAILED)))
  1303. {
  1304. *prca=(u16)(rspr1>>16); //右移16位得到,rca
  1305. return errorstatus;
  1306. }
  1307. if(rspr1&SD_R6_GENERAL_UNKNOWN_ERROR)return SD_GENERAL_UNKNOWN_ERROR;
  1308. if(rspr1&SD_R6_ILLEGAL_CMD)return SD_ILLEGAL_CMD;
  1309. if(rspr1&SD_R6_COM_CRC_FAILED)return SD_COM_CRC_FAILED;
  1310. return errorstatus;
  1311. }
  1312. //SDIO使能宽总线模式
  1313. //enx:0,不使能;1,使能;
  1314. //返回值:错误状态
  1315. SD_Error SDEnWideBus(u8 enx)
  1316. {
  1317. SD_Error errorstatus = SD_OK;
  1318. u32 scr[2]={0,0};
  1319. u8 arg=0X00;
  1320. if(enx)arg=0X02;
  1321. else arg=0X00;
  1322. if(SDIO->RESP1&SD_CARD_LOCKED)return SD_LOCK_UNLOCK_FAILED;//SD卡处于LOCKED状态
  1323. errorstatus=FindSCR(RCA,scr); //得到SCR寄存器数据
  1324. if(errorstatus!=SD_OK)return errorstatus;
  1325. if((scr[1]&SD_WIDE_BUS_SUPPORT)!=SD_ALLZERO) //支持宽总线
  1326. {
  1327. SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16;//发送CMD55+RCA,短响应
  1328. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
  1329. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  1330. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  1331. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  1332. SDIO_SendCommand(&SDIO_CmdInitStructure);
  1333. errorstatus=CmdResp1Error(SD_CMD_APP_CMD);
  1334. if(errorstatus!=SD_OK)return errorstatus;
  1335. SDIO_CmdInitStructure.SDIO_Argument = arg;//发送ACMD6,短响应,参数:10,4位;00,1位.
  1336. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH;
  1337. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  1338. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  1339. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  1340. SDIO_SendCommand(&SDIO_CmdInitStructure);
  1341. errorstatus=CmdResp1Error(SD_CMD_APP_SD_SET_BUSWIDTH);
  1342. return errorstatus;
  1343. }else return SD_REQUEST_NOT_APPLICABLE; //不支持宽总线设置
  1344. }
  1345. //检查卡是否正在执行写操作
  1346. //pstatus:当前状态.
  1347. //返回值:错误代码
  1348. SD_Error IsCardProgramming(u8 *pstatus)
  1349. {
  1350. vu32 respR1 = 0, status = 0;
  1351. SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16; //卡相对地址参数
  1352. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_STATUS;//发送CMD13
  1353. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  1354. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  1355. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  1356. SDIO_SendCommand(&SDIO_CmdInitStructure);
  1357. status=SDIO->STA;
  1358. while(!(status&((1<<0)|(1<<6)|(1<<2))))status=SDIO->STA;//等待操作完成
  1359. if(SDIO_GetFlagStatus(SDIO_FLAG_CCRCFAIL) != RESET) //CRC检测失败
  1360. {
  1361. SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL); //清除错误标记
  1362. return SD_CMD_CRC_FAIL;
  1363. }
  1364. if(SDIO_GetFlagStatus(SDIO_FLAG_CTIMEOUT) != RESET) //命令超时
  1365. {
  1366. SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT); //清除错误标记
  1367. return SD_CMD_RSP_TIMEOUT;
  1368. }
  1369. if(SDIO->RESPCMD!=SD_CMD_SEND_STATUS)return SD_ILLEGAL_CMD;
  1370. SDIO_ClearFlag(SDIO_STATIC_FLAGS);//清除所有标记
  1371. respR1=SDIO->RESP1;
  1372. *pstatus=(u8)((respR1>>9)&0x0000000F);
  1373. return SD_OK;
  1374. }
  1375. //读取当前卡状态
  1376. //pcardstatus:卡状态
  1377. //返回值:错误代码
  1378. SD_Error SD_SendStatus(uint32_t *pcardstatus)
  1379. {
  1380. SD_Error errorstatus = SD_OK;
  1381. if(pcardstatus==NULL)
  1382. {
  1383. errorstatus=SD_INVALID_PARAMETER;
  1384. return errorstatus;
  1385. }
  1386. SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16;//发送CMD13,短响应
  1387. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_STATUS;
  1388. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  1389. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  1390. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  1391. SDIO_SendCommand(&SDIO_CmdInitStructure);
  1392. errorstatus=CmdResp1Error(SD_CMD_SEND_STATUS); //查询响应状态
  1393. if(errorstatus!=SD_OK)
  1394. {
  1395. return errorstatus;
  1396. }
  1397. *pcardstatus=SDIO->RESP1;//读取响应值
  1398. return errorstatus;
  1399. }
  1400. //返回SD卡的状态
  1401. //返回值:SD卡状态
  1402. SDCardState SD_GetState(void)
  1403. {
  1404. u32 resp1=0;
  1405. if(SD_SendStatus(&resp1)!=SD_OK)
  1406. {
  1407. return SD_CARD_ERROR;
  1408. }else{
  1409. return (SDCardState)((resp1>>9) & 0x0F);
  1410. }
  1411. }
  1412. //查找SD卡的SCR寄存器值
  1413. //rca:卡相对地址
  1414. //pscr:数据缓存区(存储SCR内容)
  1415. //返回值:错误状态
  1416. SD_Error FindSCR(u16 rca,u32 *pscr)
  1417. {
  1418. u32 index = 0;
  1419. SD_Error errorstatus = SD_OK;
  1420. u32 tempscr[2]={0,0};
  1421. SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)8; //发送CMD16,短响应,设置Block Size为8字节
  1422. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN; // cmd16
  1423. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; //r1
  1424. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  1425. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  1426. SDIO_SendCommand(&SDIO_CmdInitStructure);
  1427. errorstatus=CmdResp1Error(SD_CMD_SET_BLOCKLEN);
  1428. if(errorstatus!=SD_OK)return errorstatus;
  1429. SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16;
  1430. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;//发送CMD55,短响应
  1431. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  1432. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  1433. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  1434. SDIO_SendCommand(&SDIO_CmdInitStructure);
  1435. errorstatus=CmdResp1Error(SD_CMD_APP_CMD);
  1436. if(errorstatus!=SD_OK)return errorstatus;
  1437. SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
  1438. SDIO_DataInitStructure.SDIO_DataLength = 8; //8个字节长度,block为8字节,SD卡到SDIO.
  1439. SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_8b ; //块大小8byte
  1440. SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO;
  1441. SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
  1442. SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
  1443. SDIO_DataConfig(&SDIO_DataInitStructure);
  1444. SDIO_CmdInitStructure.SDIO_Argument = 0x0;
  1445. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_APP_SEND_SCR; //发送ACMD51,短响应,参数为0
  1446. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; //r1
  1447. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  1448. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  1449. SDIO_SendCommand(&SDIO_CmdInitStructure);
  1450. errorstatus=CmdResp1Error(SD_CMD_SD_APP_SEND_SCR);
  1451. if(errorstatus!=SD_OK)return errorstatus;
  1452. while(!(SDIO->STA&(SDIO_FLAG_RXOVERR|SDIO_FLAG_DCRCFAIL|SDIO_FLAG_DTIMEOUT|SDIO_FLAG_DBCKEND|SDIO_FLAG_STBITERR)))
  1453. {
  1454. if(SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET)//接收FIFO数据可用
  1455. {
  1456. *(tempscr+index)=SDIO->FIFO; //读取FIFO内容
  1457. index++;
  1458. if(index>=2)break;
  1459. }
  1460. }
  1461. if(SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET) //数据超时错误
  1462. {
  1463. SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT); //清错误标志
  1464. return SD_DATA_TIMEOUT;
  1465. }else if(SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET) //数据块CRC错误
  1466. {
  1467. SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL); //清错误标志
  1468. return SD_DATA_CRC_FAIL;
  1469. }else if(SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET) //接收fifo上溢错误
  1470. {
  1471. SDIO_ClearFlag(SDIO_FLAG_RXOVERR); //清错误标志
  1472. return SD_RX_OVERRUN;
  1473. }else if(SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET) //接收起始位错误
  1474. {
  1475. SDIO_ClearFlag(SDIO_FLAG_STBITERR);//清错误标志
  1476. return SD_START_BIT_ERR;
  1477. }
  1478. SDIO_ClearFlag(SDIO_STATIC_FLAGS);//清除所有标记
  1479. //把数据顺序按8位为单位倒过来.
  1480. *(pscr+1)=((tempscr[0]&SD_0TO7BITS)<<24)|((tempscr[0]&SD_8TO15BITS)<<8)|((tempscr[0]&SD_16TO23BITS)>>8)|((tempscr[0]&SD_24TO31BITS)>>24);
  1481. *(pscr)=((tempscr[1]&SD_0TO7BITS)<<24)|((tempscr[1]&SD_8TO15BITS)<<8)|((tempscr[1]&SD_16TO23BITS)>>8)|((tempscr[1]&SD_24TO31BITS)>>24);
  1482. return errorstatus;
  1483. }
  1484. //得到NumberOfBytes以2为底的指数.
  1485. //NumberOfBytes:字节数.
  1486. //返回值:以2为底的指数值
  1487. u8 convert_from_bytes_to_power_of_two(u16 NumberOfBytes)
  1488. {
  1489. u8 count=0;
  1490. while(NumberOfBytes!=1)
  1491. {
  1492. NumberOfBytes>>=1;
  1493. count++;
  1494. }
  1495. return count;
  1496. }
  1497. //配置SDIO DMA
  1498. //mbuf:存储器地址
  1499. //bufsize:传输数据量
  1500. //dir:方向;DMA_DIR_MemoryToPeripheral 存储器-->SDIO(写数据);DMA_DIR_PeripheralToMemory SDIO-->存储器(读数据);
  1501. void SD_DMA_Config(u32*mbuf,u32 bufsize,u32 dir)
  1502. {
  1503. DMA_InitTypeDef DMA_InitStructure;
  1504. while (DMA_GetCmdStatus(DMA2_Stream3) != DISABLE){}//等待DMA可配置
  1505. DMA_DeInit(DMA2_Stream3);//清空之前该stream3上的所有中断标志
  1506. DMA_InitStructure.DMA_Channel = DMA_Channel_4; //通道选择
  1507. DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&SDIO->FIFO;//DMA外设地址
  1508. DMA_InitStructure.DMA_Memory0BaseAddr = (u32)mbuf;//DMA 存储器0地址
  1509. DMA_InitStructure.DMA_DIR = dir;//存储器到外设模式
  1510. DMA_InitStructure.DMA_BufferSize = 0;//数据传输量
  1511. DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设非增量模式
  1512. DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//存储器增量模式
  1513. DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;//外设数据长度:32位
  1514. DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;//存储器数据长度:32位
  1515. DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;// 使用普通模式
  1516. DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;//最高优先级
  1517. DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable; //FIFO使能
  1518. DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;//全FIFO
  1519. DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_INC4;//外设突发4次传输
  1520. DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_INC4;//存储器突发4次传输
  1521. DMA_Init(DMA2_Stream3, &DMA_InitStructure);//初始化DMA Stream
  1522. DMA_FlowControllerConfig(DMA2_Stream3,DMA_FlowCtrl_Peripheral);//外设流控制
  1523. DMA_Cmd(DMA2_Stream3 ,ENABLE);//开启DMA传输
  1524. }
  1525. //读SD卡
  1526. //buf:读数据缓存区
  1527. //sector:扇区地址
  1528. //cnt:扇区个数
  1529. //返回值:错误状态;0,正常;其他,错误代码;
  1530. u8 SD_ReadDisk(u8*buf,u32 sector,u8 cnt)
  1531. {
  1532. u8 sta=SD_OK;
  1533. long long lsector=sector;
  1534. u8 n;
  1535. if(CardType!=SDIO_STD_CAPACITY_SD_CARD_V1_1)lsector<<=9;
  1536. if((u32)buf%4!=0)
  1537. {
  1538. for(n=0;n<cnt;n++)
  1539. {
  1540. sta=SD_ReadBlock(SDIO_DATA_BUFFER,lsector+512*n,512);//单个sector的读操作
  1541. memcpy(buf,SDIO_DATA_BUFFER,512);
  1542. buf+=512;
  1543. }
  1544. }else
  1545. {
  1546. if(cnt==1)sta=SD_ReadBlock(buf,lsector,512); //单个sector的读操作
  1547. else sta=SD_ReadMultiBlocks(buf,lsector,512,cnt);//多个sector
  1548. }
  1549. return sta;
  1550. }
  1551. //写SD卡
  1552. //buf:写数据缓存区
  1553. //sector:扇区地址
  1554. //cnt:扇区个数
  1555. //返回值:错误状态;0,正常;其他,错误代码;
  1556. u8 SD_WriteDisk(u8*buf,u32 sector,u8 cnt)
  1557. {
  1558. u8 sta=SD_OK;
  1559. u8 n;
  1560. long long lsector=sector;
  1561. if(CardType!=SDIO_STD_CAPACITY_SD_CARD_V1_1)lsector<<=9;
  1562. if((u32)buf%4!=0)
  1563. {
  1564. for(n=0;n<cnt;n++)
  1565. {
  1566. memcpy(SDIO_DATA_BUFFER,buf,512);
  1567. sta=SD_WriteBlock(SDIO_DATA_BUFFER,lsector+512*n,512);//单个sector的写操作
  1568. buf+=512;
  1569. }
  1570. }else
  1571. {
  1572. if(cnt==1)sta=SD_WriteBlock(buf,lsector,512); //单个sector的写操作
  1573. else sta=SD_WriteMultiBlocks(buf,lsector,512,cnt); //多个sector
  1574. }
  1575. return sta;
  1576. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/982535
推荐阅读
相关标签
  

闽ICP备14008679号