fatfs-SDIO的写文件时间耗费在哪里了(之一)?

版权声明:本文为博主unsv29原创文章,未经博主允许不得转载。 https://blog.csdn.net/unsv29/article/details/83410401

分析的是这个驱动,也是网上流传比较多的,如下:

  * @file    fatfs_drv.c
  * @author  MCD Application Team
  * @version V1.1.0
  * @date    31-July-2013
  * @brief   diskio interface

-------------

网上安富莱的例子用的是V1.1.2的驱动,也不知道比V1.1.0的驱动先进在哪里。

今天就分析V1.1.0的。

具体的调用过程是

f_open();

while(1) {

      f_write(); 

}

写函数当然就是

这个函数disk_write,

就分析这个disk_write的里面的扇区对齐且写文件是扇区的整数倍的时候的情形。

假设扇区512,通过f_write函数写入1024*5个字节。

#define    _MAX_SS        512        /* 512, 1024, 2048 or 4096 */
/* Maximum sector size to be handled.
/  Always set 512 for memory card and hard disk but a larger value may be
/  required for on-board flash memory, floppy disk and optical disk.
/  When _MAX_SS is larger than 512, it configures FatFs to variable sector size
/  and GET_SECTOR_SIZE command must be implememted to the disk_ioctl function. */

disk_write里面

有如下的内容:

(1)SD_WriteMultiBlocks((BYTE *)buff, (uint32_t )(sector * BLOCK_SIZE), BLOCK_SIZE, count);//耗费时间tx_1

(2)sdstatus = SD_WaitWriteOperation();                                                                                         //耗费时间ty_1

(3)while(SD_GetStatus() != SD_TRANSFER_OK)                                                                         //耗费时间tz_1

看下时间,抓个图:如下:

可见tx_1时间很固定,大概1个ms,tz_1时间多的有15ms,大部分10ms左右。ty_1的时间偶尔比较长,值是490ms。

10ms左右的延时在我的系统里面还是可以勉强忍受的,但是490ms是无法容忍的。那么就看看这490ms都耗费在哪里?

函数SD_WaitWriteOperation();里面主要有两个延时:如下:

(4)while ((DMAEndOfTransfer == 0x00) && (TransferEnd == 0) && (TransferError == SD_OK) && (timeout > 0))//耗费时间tM_1

(5)while(((SDIO->STA & SDIO_FLAG_TXACT)) && (timeout > 0))//耗费时间tN_1

可以看出主要时间耗费在tM_1了。

那么我们再看一下while ((DMAEndOfTransfer == 0x00) && (TransferEnd == 0) && (TransferError == SD_OK) && (timeout > 0))的几个变量的走势:

局部放大一下:

看下函数SD_WaitWriteOperation的注释:

/**
  * @brief  This function waits until the SDIO DMA data transfer is finished. 
  *         This function should be called after SDIO_WriteBlock() and
  *         SDIO_WriteMultiBlocks() function to insure that all data sent by the 
  *         card are already transferred by the DMA controller.        
  * @param  None.
  * @retval SD_Error: SD Card Error code.
  */

======================

测试到这里就完活了,那么重点来了,怎么解决那490ms呢?

目前还没有好的办法,慢慢想吧,,,,

另外我的程序没有同步数据,可以同步一下数据看看耗费时间。。。

具体的调用过程是

f_open();

while(1) {

      f_write(); 

      f_sync(); 

}

刚刚实验了,同步之后这个390ms仍然存在。

猜你喜欢

转载自blog.csdn.net/unsv29/article/details/83410401