STM32H7使用FileX库BUG,SD卡挂载失败

发布于:2024-04-27 ⋅ 阅读:(21) ⋅ 点赞:(0)

问题描述:

使用STM32H7+ThreadX+FileX,之前使用swissbit牌的存储卡可正常使用,最近项目用了金士顿的存储卡,发现无法挂载文件系统。

原因分析:

调试过程发现,关闭D-Cache可以挂载使用exfat文件系统。
FileX对SD卡读写接口全部位于fx_stm32_sdio_driver.c中,查看该文件可以发现在读写函数中已经考虑到使用Cache与DMA的情况,如下代码所示。

SCB_InvalidateDCache_by_Addr((uint32_t*)scratch, DEFAULT_SECTOR_SIZE);
SCB_InvalidateDCache_by_Addr((uint32_t*)media_ptr->fx_media_driver_buffer, num_sectors * DEFAULT_SECTOR_SIZE);

SCB_InvalidateDCache_by_Addr()函数用于将该地址D-Cache无效化,无效化的意思是将Cache Line 标记为无效,等同于删除操作。这样下次读写该地址数据时,D-Cache中无此数据,CPU直接对SRAM读写数据,保证CPU读取到的数据是真实的。
源码分析:
以读取函数为例:

        case FX_DRIVER_READ:
        {
            media_ptr->fx_media_driver_status = FX_IO_ERROR;
            //TX_DISABLE /* disable interrupts */
           if ((ULONG)(media_ptr->fx_media_driver_buffer) & 0x03)
           {
              if (sd_read_data(media_ptr, media_ptr->fx_media_driver_logical_sector + media_ptr->fx_media_hidden_sectors,
                                media_ptr->fx_media_driver_sectors, 1) == FX_SUCCESS)
              {
                  media_ptr->fx_media_driver_status = FX_SUCCESS;
              }
           }
           ……

if ((ULONG)(media_ptr->fx_media_driver_buffer) & 0x03)判断读取地址是否4字节对齐,因为使用MDMA读写SD卡需要地址4字节对齐,并对sd_read_data()函数传入相应值。
sd_read_data()函数如下:

static UINT sd_read_data(FX_MEDIA *media_ptr, ULONG start_sector, UINT num_sectors, UINT use_scratch_buffer)
{
    INT i = 0;
    UINT status;
    if (use_scratch_buffer)
    {
        memset(scratch, '\0', DEFAULT_SECTOR_SIZE);
        for (i = 0; i < num_sectors; i++)
        {
            status = BSP_SD_ReadBlocks_DMA((uint32_t*)scratch, start_sector++, 1);

            if (status != BSP_ERROR_NONE)
            {
                status = FX_IO_ERROR;
                break;
            }

            if(tx_semaphore_get(&transfer_semaphore, DEFAULT_TIMEOUT) != TX_SUCCESS)
            {
                status = FX_BUFFER_ERROR;
                break;
            }
            else
            {
#if (ENABLE_CACHE_MAINTENANCE == 1)
                SCB_InvalidateDCache_by_Addr((uint32_t*)scratch, DEFAULT_SECTOR_SIZE);
#endif

                _fx_utility_memory_copy(scratch, media_ptr->fx_media_driver_buffer, DEFAULT_SECTOR_SIZE);
                media_ptr->fx_media_driver_buffer += DEFAULT_SECTOR_SIZE;
            }
        }

        if (i == num_sectors)
        {
            status = FX_SUCCESS;
        }
    }
    else
    {
        status = BSP_SD_ReadBlocks_DMA((uint32_t*)media_ptr->fx_media_driver_buffer, start_sector, num_sectors);

        if (status == BSP_ERROR_NONE)
        {
            if(tx_semaphore_get(&transfer_semaphore, DEFAULT_TIMEOUT) == TX_SUCCESS)
            {
#if (ENABLE_CACHE_MAINTENANCE == 1)
                SCB_InvalidateDCache_by_Addr((uint32_t*)media_ptr->fx_media_driver_buffer, num_sectors * DEFAULT_SECTOR_SIZE);
#endif
                status = FX_SUCCESS;
            }
            else
            {
                status =  FX_BUFFER_ERROR;
            }
        }
    }

    return status;
}

根据传入参数判断,若传输地址不是4字节对齐,则使用内部定义的一块32字节对齐的地址作为中转站,先用MDMA读取到数据,再将数据拷贝到用户提供地址。else分支若地址已经是4字节对齐,则直接使用用户提供的地址进行MDMA传输。
然而else分支的代码存在一个bug,当用户传入的地址为4字节对齐时,进入else分支,在MDMA传输完成后,进行SCB_InvalidateDCache_by_Addr()操作。但是该函数要求传入的地址为32字节对齐,因为Cortex-M7内核的Cache line大小为8个字。所以当传入的地址为4字节对齐但非32字节对齐时,SCB_InvalidateDCache_by_Addr()调用传入的参数不符合要求,D-Cache缓存清除错误,使CPU读取到缓存中错误的值。
在这里插入图片描述

解决方案:

在判断地址是否4字节对齐的地方,改成判断地址是否32字节对齐(即0x03改成0x1f),共修改2行代码,在代码中搜索media_ptr->fx_media_driver_buffer即可找到。

if ((ULONG)(media_ptr->fx_media_driver_buffer) & 0x1f)

网站公告

今日签到

点亮在社区的每一天
去签到