硬汉嵌入式论坛

 找回密码
 立即注册
查看: 345|回复: 3
收起左侧

[ThreadX全家桶] FileX+USBX Msc双边同步问题

[复制链接]

2

主题

2

回帖

8

积分

新手上路

积分
8
发表于 2025-8-20 09:50:20 | 显示全部楼层 |阅读模式
目前使用的存储介质是SD卡,进行fx_media_format格式化之后,可以被PC正常识别,写入读取都是正常的。
但是存在本地和PC端不同步的问题,比如在本地创建或写入文件,创建或写入后立即调用了fx_media_flush,本地已经生效,PC端也不会立即刷新出文件,需要重新枚举才能正常显示。
同样通过PC端写入文件,需要重新fx_media_open本地才能看到PC端上次写入的文件。

造成这样的现象,貌似是两边在维护不同的FAT表。请问有什么好的解决方法吗?

下面是我的格式化代码,以及MSC相关操作代码。

[C] 纯文本查看 复制代码
    HAL_SD_CardInfoTypeDef cardInfo;
    HAL_SD_GetCardInfo(&hsd1, &cardInfo);

    fxStatus = fx_media_format(&sdioDisk,                   //Disk Handle
                                fx_stm32_sd_driver,         //Disk Driver
                                (VOID *)FX_NULL,            //Unused
                                (UCHAR *)fxSDMediaMemory,   //Disk Buffer
                                sizeof(fxSDMediaMemory),    //Disk Buffer Size
                                FX_SD_VOLUME_NAME,          //Volume Name
                                1,                          //Num of FATs
                                32,                         //Directory Entries
                                0,                          //Hidden Sectors
                                ((cardInfo.BlockNbr * cardInfo.BlockSize) / FX_STM32_SD_DEFAULT_SECTOR_SIZE),//Total Sectors
                                FX_STM32_SD_DEFAULT_SECTOR_SIZE,//Bytes per Sector
                                128,                        //Sectors per Cluster
                                1,                          //Heads
                                1);                         //Sectors per Track


[C] 纯文本查看 复制代码
/**
  * @brief  USBD_STORAGE_Read
  *         This function is invoked to read from media.
  * @param  storage_instance : Pointer to the storage class instance.
  * @param  lun: Logical unit number is the command is directed to.
  * @param  data_pointer: Address of the buffer to be used for reading or writing.
  * @param  number_blocks: number of sectors to read/write.
  * @param  lba: Logical block address is the sector address to read.
  * @param  media_status: should be filled out exactly like the media status
  *                       callback return value.
  * @retval status
  */
UINT USBD_STORAGE_Read(VOID *storage_instance, ULONG lun, UCHAR *data_pointer,
                       ULONG number_blocks, ULONG lba, ULONG *media_status)
{
  UINT status = UX_SUCCESS;

  /* USER CODE BEGIN USBD_STORAGE_Read */
    UINT fxStatus = FX_SUCCESS;
    while (number_blocks--) {
        fxStatus = fx_media_read(sdioDisk, lba, data_pointer);
        if (fxStatus != FX_SUCCESS) {
            status = UX_ERROR;
            break;
        }
        data_pointer += sdioDisk->fx_media_bytes_per_sector;
        lba++;
    }
  /* USER CODE END USBD_STORAGE_Read */

  return status;
}

/**
  * @brief  USBD_STORAGE_Write
  *         This function is invoked to write in media.
  * @param  storage_instance : Pointer to the storage class instance.
  * @param  lun: Logical unit number is the command is directed to.
  * @param  data_pointer: Address of the buffer to be used for reading or writing.
  * @param  number_blocks: number of sectors to read/write.
  * @param  lba: Logical block address is the sector address to read.
  * @param  media_status: should be filled out exactly like the media status
  *                       callback return value.
  * @retval status
  */
UINT USBD_STORAGE_Write(VOID *storage_instance, ULONG lun, UCHAR *data_pointer,
                        ULONG number_blocks, ULONG lba, ULONG *media_status)
{
  UINT status = UX_SUCCESS;

  /* USER CODE BEGIN USBD_STORAGE_Write */
    UINT fxStatus = FX_SUCCESS;
    while (number_blocks--) {
        fxStatus = fx_media_write(sdioDisk, lba, data_pointer);
        if (fxStatus != FX_SUCCESS && lba != 0) {
            status = UX_ERROR;
            break;
        }
        data_pointer += sdioDisk->fx_media_bytes_per_sector;
        lba++;
    }
  /* USER CODE END USBD_STORAGE_Write */

  return status;
}

回复

使用道具 举报

2

主题

2

回帖

8

积分

新手上路

积分
8
 楼主| 发表于 2025-8-21 21:55:00 | 显示全部楼层
弃用MSC了,改为PIMA MTP,由设备端统一管理文件系统,可以完美兼容
回复

使用道具 举报

2

主题

2

回帖

8

积分

新手上路

积分
8
 楼主| 发表于 2025-8-21 21:56:01 | 显示全部楼层
弃用MSC了,使用PIMA MTP类,由下位机管理文件系统,可以完美兼容
回复

使用道具 举报

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
118335
QQ
发表于 2025-8-22 12:09:03 来自手机 | 显示全部楼层
是的mtp是非常好的方案
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2025-9-26 18:31 , Processed in 0.041518 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表