打印
[STM32F1]

STM32FF103CBT6内部FLASH模拟U盘求助(HAL库)

[复制链接]
3892|20
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
yklstudent|  楼主 | 2018-4-22 11:32 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
各位,好:

最近想通过内部FLASH模拟U盘实现用户程序升级,使用的是STM32CubeMX创建的工程,目前连接PC时可以显示U盘,提示需要格式化U盘;
但是U盘格式化一直不成功,不知道哪位是否研究过,或者可以帮忙看下是什么原因导致无法格式化。
代码如下所示:
/**
  ******************************************************************************
  * @file           : usbd_storage_if.c
  * @version        : v2.0_Cube
  * @brief          : Memory management layer.
  ******************************************************************************
  * This notice applies to any and all portions of this file
  * that are not between comment pairs USER CODE BEGIN and
  * USER CODE END. Other portions of this file, whether
  * inserted by the user or by software development tools
  * are owned by their respective copyright owners.
  *
  * Copyright (c) 2018 STMicroelectronics International N.V.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted, provided that the following conditions are met:
  *
  * 1. Redistribution of source code must retain the above copyright notice,
  *    this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright notice,
  *    this list of conditions and the following disclaimer in the documentation
  *    and/or other materials provided with the distribution.
  * 3. Neither the name of STMicroelectronics nor the names of other
  *    contributors to this software may be used to endorse or promote products
  *    derived from this software without specific written permission.
  * 4. This software, including modifications and/or derivative works of this
  *    software, must execute solely and exclusively on microcontroller or
  *    microprocessor devices manufactured by or for STMicroelectronics.
  * 5. Redistribution and use of this software other than as permitted under
  *    this license is void and will automatically terminate your rights under
  *    this license.
  *
  * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "usbd_storage_if.h"

/* USER CODE BEGIN INCLUDE */

/* USER CODE END INCLUDE */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/

/* USER CODE END PV */

/** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
  * @brief Usb device.
  * @{
  */

/** @defgroup USBD_STORAGE
  * @brief Usb mass storage device module
  * @{
  */

/** @defgroup USBD_STORAGE_Private_TypesDefinitions
  * @brief Private types.
  * @{
  */

/* USER CODE BEGIN PRIVATE_TYPES */

/* USER CODE END PRIVATE_TYPES */

/**
  * @}
  */

/** @defgroup USBD_STORAGE_Private_Defines
  * @brief Private defines.
  * @{
  */

#define STORAGE_LUN_NBR                          1
#define STORAGE_BLK_NBR                          0x40
#define STORAGE_BLK_SIZ                          0x200

/* USER CODE BEGIN PRIVATE_DEFINES */
#define FLASH_START_ADDR                                        0x08008000
#define FLASH_SIZE                                                        0x8000
/* USER CODE END PRIVATE_DEFINES */

/**
  * @}
  */

/** @defgroup USBD_STORAGE_Private_Macros
  * @brief Private macros.
  * @{
  */

/* USER CODE BEGIN PRIVATE_MACRO */

/* USER CODE END PRIVATE_MACRO */

/**
  * @}
  */

/** @defgroup USBD_STORAGE_Private_Variables
  * @brief Private variables.
  * @{
  */

/* USER CODE BEGIN INQUIRY_DATA_FS */
/** USB Mass storage Standard Inquiry Data. */
const int8_t STORAGE_Inquirydata_FS[] = {/* 36 */

        /* LUN 0 */
        0x00,
        0x80,
        0x02,
        0x02,
        (STANDARD_INQUIRY_DATA_LEN - 5),
        0x00,
        0x00,       
        0x00,
        'S', 'T', 'M', ' ', ' ', ' ', ' ', ' ', /* Manufacturer : 8 bytes */
        'P', 'r', 'o', 'd', 'u', 'c', 't', ' ', /* Product      : 16 Bytes */
        ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
        '0', '.', '0' ,'1'                      /* Version      : 4 Bytes */
};
/* USER CODE END INQUIRY_DATA_FS */

/* USER CODE BEGIN PRIVATE_VARIABLES */

/* USER CODE END PRIVATE_VARIABLES */

/**
  * @}
  */

/** @defgroup USBD_STORAGE_Exported_Variables
  * @brief Public variables.
  * @{
  */

extern USBD_HandleTypeDef hUsbDeviceFS;

/* USER CODE BEGIN EXPORTED_VARIABLES */

/* USER CODE END EXPORTED_VARIABLES */

/**
  * @}
  */

/** @defgroup USBD_STORAGE_Private_FunctionPrototypes
  * @brief Private functions declaration.
  * @{
  */

static int8_t STORAGE_Init_FS(uint8_t lun);
static int8_t STORAGE_GetCapacity_FS(uint8_t lun, uint32_t *block_num, uint16_t *block_size);
static int8_t STORAGE_IsReady_FS(uint8_t lun);
static int8_t STORAGE_IsWriteProtected_FS(uint8_t lun);
static int8_t STORAGE_Read_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
static int8_t STORAGE_Write_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
static int8_t STORAGE_GetMaxLun_FS(void);

void STFlash_ReadBuffer(uint32_t blk_addr, uint8_t *buf, uint16_t blk_len);
void STFlash_WriteBuffer(uint32_t blk_addr, uint8_t *buf, uint16_t blk_len);

/* USER CODE BEGIN PRIVATE_FUNCTIONS_DECLARATION */

/* USER CODE END PRIVATE_FUNCTIONS_DECLARATION */

/**
  * @}
  */

USBD_StorageTypeDef USBD_Storage_Interface_fops_FS =
{
        STORAGE_Init_FS,
        STORAGE_GetCapacity_FS,
        STORAGE_IsReady_FS,
        STORAGE_IsWriteProtected_FS,
        STORAGE_Read_FS,
        STORAGE_Write_FS,
        STORAGE_GetMaxLun_FS,
        (int8_t *)STORAGE_Inquirydata_FS
};

/* Private functions ---------------------------------------------------------*/
/**
  * @brief  Initializes over USB FS IP
  * @param  lun:
  * @retval USBD_OK if all operations are OK else USBD_FAIL
  */
int8_t STORAGE_Init_FS(uint8_t lun)
{
        /* USER CODE BEGIN 2 */
        return (USBD_OK);
        /* USER CODE END 2 */
}

/**
  * @brief  .
  * @param  lun: .
  * @param  block_num: .
  * @param  block_size: .
  * @retval USBD_OK if all operations are OK else USBD_FAIL
  */
int8_t STORAGE_GetCapacity_FS(uint8_t lun, uint32_t *block_num, uint16_t *block_size)
{
        /* USER CODE BEGIN 3 */
        *block_num  = STORAGE_BLK_NBR;
        *block_size = STORAGE_BLK_SIZ;
        return (USBD_OK);
        /* USER CODE END 3 */
}

/**
  * @brief  .
  * @param  lun: .
  * @retval USBD_OK if all operations are OK else USBD_FAIL
  */
int8_t STORAGE_IsReady_FS(uint8_t lun)
{
        /* USER CODE BEGIN 4 */
        return (USBD_OK);
        /* USER CODE END 4 */
}

/**
  * @brief  .
  * @param  lun: .
  * @retval USBD_OK if all operations are OK else USBD_FAIL
  */
int8_t STORAGE_IsWriteProtected_FS(uint8_t lun)
{
        /* USER CODE BEGIN 5 */
        return (USBD_OK);
        /* USER CODE END 5 */
}

/**
  * @brief  .
  * @param  lun: .
  * @retval USBD_OK if all operations are OK else USBD_FAIL
  */
int8_t STORAGE_Read_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{
        /* USER CODE BEGIN 6 */
        STFlash_ReadBuffer(blk_addr, buf, blk_len);
        return (USBD_OK);
        /* USER CODE END 6 */
}

/**
  * @brief  .
  * @param  lun: .
  * @retval USBD_OK if all operations are OK else USBD_FAIL
  */
int8_t STORAGE_Write_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{
        /* USER CODE BEGIN 7 */
        STFlash_WriteBuffer(blk_addr, buf, blk_len);
        return (USBD_OK);
        /* USER CODE END 7 */
}

/**
  * @brief  .
  * @param  None
  * @retval .
  */
int8_t STORAGE_GetMaxLun_FS(void)
{
        /* USER CODE BEGIN 8 */
        return (STORAGE_LUN_NBR - 1);
        /* USER CODE END 8 */
}

/* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */
//uint32_t ulFlashData;
//uint32_t ulFlashAddr;
//uint32_t i,j,n;
void STFlash_ReadBuffer(uint32_t blk_addr, uint8_t *buf, uint16_t blk_len)
{
        uint32_t ulFlashData = 0;
        uint32_t ulFlashAddr = FLASH_START_ADDR + blk_addr*STORAGE_BLK_SIZ;
        uint32_t i,j,n = 0;
        if((blk_addr + blk_len)*STORAGE_BLK_SIZ < FLASH_SIZE)
        {
                for(i=0; i<blk_len; i++)
                {
                        for(j=0; j<STORAGE_BLK_SIZ; j+=4)
                        {
                                ulFlashData = *(volatile unsigned int*)(ulFlashAddr);
                                buf[n++] = (uint8_t)(ulFlashData >> 0);
                                buf[n++] = (uint8_t)(ulFlashData >> 8);
                                buf[n++] = (uint8_t)(ulFlashData >> 16);
                                buf[n++] = (uint8_t)(ulFlashData >> 24);
                                ulFlashAddr += 4;
                        }
                }
        }
}
       
volatile uint8_t iMemory[FLASH_PAGE_SIZE];
volatile uint8_t eMemory[STORAGE_BLK_SIZ];
void STFlash_WriteBuffer(uint32_t blk_addr, uint8_t *buf, uint16_t blk_len)
{
        uint32_t ulFlashData = 0;
        uint32_t ulFlashAddr = 0;
        uint32_t secpos;                // 扇区地址
        uint16_t secoff;                // 扇区内偏移地址
       
        if((blk_addr+blk_len)*STORAGE_BLK_SIZ >= FLASH_SIZE)
        {
                return;
        }
        //uint32_t ulFlashData = 0;
        //uint32_t ulFlashAddr = 0;
        ulFlashData = 0;
        ulFlashAddr = 0;
        uint32_t i, j;
        for(i=0; i<STORAGE_BLK_SIZ; i++)
        {
                eMemory = buf;
                if(eMemory[0] == 0xEB && eMemory[1] == 0x3C)
                {
                        ulFlashData = 0;
                        ulFlashAddr = 0;
                }
        }
        // FLASH_PAGE_SIZE
        for(i=0; i<blk_len; i++)
        {
                secpos = (blk_addr + i) / 2;
                secoff = (blk_addr + i) % 2;
                STFlash_ReadBuffer(secpos, (uint8_t*)&iMemory[0], 4);
                for(j=0; j<STORAGE_BLK_SIZ; j++)
                {
                        if(iMemory[j+secoff*STORAGE_BLK_SIZ] != 0xFF)
                                break;
                }
                HAL_FLASH_Unlock();
                if(j < STORAGE_BLK_SIZ)
                {
                        /* Clear all FLASH flags */
                        __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR);
                        if(secpos >= 32)
                        {
                                ulFlashData = 0;
                                ulFlashAddr = 0;
                        }
                        uint32_t PageError = 0;
                        FLASH_EraseInitTypeDef pEraseInit;
                        pEraseInit.TypeErase = FLASH_TYPEERASE_PAGES;
                        pEraseInit.PageAddress = FLASH_START_ADDR + secpos*FLASH_PAGE_SIZE;
                        pEraseInit.Banks = FLASH_BANK_1;
                        pEraseInit.NbPages = 1;
                        HAL_FLASHEx_Erase(&pEraseInit, &PageError);
                        for(j=0; j<STORAGE_BLK_SIZ; j++)
                        {
                                iMemory[j + secoff*STORAGE_BLK_SIZ] = buf[j + i*STORAGE_BLK_SIZ];
                        }
                        ulFlashAddr = FLASH_START_ADDR + secpos*FLASH_PAGE_SIZE;
                        for(j=0; j<FLASH_PAGE_SIZE; j+=4)
                        {
                                ulFlashData  = (uint32_t)iMemory[j + 3] << 24;
                                ulFlashData += (uint32_t)iMemory[j + 2] << 16;
                                ulFlashData += (uint32_t)iMemory[j + 1] << 8;
                                ulFlashData += (uint32_t)iMemory[j + 0] << 0;
                                HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, ulFlashAddr, ulFlashData);
                                ulFlashAddr += 4;
                        }
                }
                else
                {
                        for(j=0; j<STORAGE_BLK_SIZ; j++)
                        {
                                iMemory[j + secoff*STORAGE_BLK_SIZ] = buf[j + i*STORAGE_BLK_SIZ];
                        }
                        ulFlashAddr = FLASH_START_ADDR + secpos*FLASH_PAGE_SIZE + secoff*STORAGE_BLK_SIZ;
                        for(j=0; j<STORAGE_BLK_SIZ; j+=4)
                        {
                                ulFlashData  = (uint32_t)iMemory[j + secoff*STORAGE_BLK_SIZ + 3] << 24;
                                ulFlashData += (uint32_t)iMemory[j + secoff*STORAGE_BLK_SIZ + 2] << 16;
                                ulFlashData += (uint32_t)iMemory[j + secoff*STORAGE_BLK_SIZ + 1] << 8;
                                ulFlashData += (uint32_t)iMemory[j + secoff*STORAGE_BLK_SIZ + 0] << 0;
                                HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, ulFlashAddr, ulFlashData);
                                ulFlashAddr += 4;
                        }
                }
                HAL_FLASH_Lock();
        }
}
/* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */

/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/


工程文件如下所示:
STM32F103CBT6_DRIVER_MSC.zip (13.87 MB)

沙发
mmuuss586| | 2018-4-22 12:47 | 只看该作者

使用特权

评论回复
板凳
gyh974| | 2018-4-22 18:30 | 只看该作者
本帖最后由 gyh974 于 2018-4-22 18:35 编辑

模拟U盘可以直接把FAT的MBK内容做成一个表,这样就不用每次上电都要格式化才能用,另外不能格式化跟STFlash_ReadBuffer函数有关,请问你这样读FLASH内容有何用意呢?这个函数是你自己写的还是自动生成的?

使用特权

评论回复
地板
yklstudent|  楼主 | 2018-4-22 20:55 | 只看该作者
gyh974 发表于 2018-4-22 18:30
模拟U盘可以直接把FAT的MBK内容做成一个表,这样就不用每次上电都要格式化才能用,另外不能格式化跟STFlash ...

首先谢谢你的恢复。
内部FLASH的读写函数都是需要自己写的;你说的FAT的MDK内容表我知道是可以提前生产的,DEVICE插上PC后可以成功模拟U盘,但是没法成功往U盘内拷贝等文件功能,感觉应该是FLASH的读写函数有问题,但是现在自己确实没法找出自己写的FLASH有什么问题,所以想发到网上寻求网友的帮助,解决下。

使用特权

评论回复
5
huzi2099| | 2018-4-23 10:21 | 只看该作者
你这个BLK_NBR    太小了,不够建文件系统的

使用特权

评论回复
6
yklstudent|  楼主 | 2018-4-23 11:51 | 只看该作者
huzi2099 发表于 2018-4-23 10:21
你这个BLK_NBR    太小了,不够建文件系统的

大小是没有问题的,STM32F407VET6芯片内部64KB/RAM就试过模拟U盘,STM32F103CBT6内部RAM没有那么大,但是内部FLASH大小是够的,最低试过40KB的RAM就可以

使用特权

评论回复
7
yklstudent|  楼主 | 2018-4-23 11:51 | 只看该作者
huzi2099 发表于 2018-4-23 10:21
你这个BLK_NBR    太小了,不够建文件系统的

大小是没有问题的,STM32F407VET6芯片内部64KB/RAM就试过模拟U盘,STM32F103CBT6内部RAM没有那么大,但是内部FLASH大小是够的,最低试过40KB的RAM就可以

使用特权

评论回复
8
huzi2099| | 2018-4-23 16:46 | 只看该作者
可能是字节顺序不对吧,按理说你函数全都返回OK是不会失败的,你在确认一下第一扇区是否正确

使用特权

评论回复
9
huzi2099| | 2018-4-23 16:47 | 只看该作者
yklstudent 发表于 2018-4-23 11:51
大小是没有问题的,STM32F407VET6芯片内部64KB/RAM就试过模拟U盘,STM32F103CBT6内部RAM没有那么大,但是 ...

你这个是32K,那么小做U盘有一半会浪费掉

使用特权

评论回复
10
yklstudent|  楼主 | 2018-4-24 14:48 | 只看该作者
huzi2099 发表于 2018-4-23 16:46
可能是字节顺序不对吧,按理说你函数全都返回OK是不会失败的,你在确认一下第一扇区是否正确 ...

格式化提示失败,查看U盘第一扇区,数据确实有问题
现在主要卡在这里了,不知道什么原因导致的

使用特权

评论回复
11
yklstudent|  楼主 | 2018-4-24 14:48 | 只看该作者
huzi2099 发表于 2018-4-23 16:46
可能是字节顺序不对吧,按理说你函数全都返回OK是不会失败的,你在确认一下第一扇区是否正确 ...

格式化提示失败,查看U盘第一扇区,数据确实有问题
现在主要卡在这里了,不知道什么原因导致的

使用特权

评论回复
12
菜鸟同学| | 2018-4-24 17:17 | 只看该作者
不能格式化说明你的 读写有问题,内部的flash 很简单的,现成的例子,你用的芯片看看每个页面操作是1KB 还是2KB,然后做好 写函数,就OK了。

使用特权

评论回复
13
beyond696| | 2018-4-24 17:36 | 只看该作者
自己建表,不用格式化,接电脑后自动就识别为U盘,用FAT16文件系统,我刚刚做完一个这样的项目,还顺便解决了win10下的一些问题

使用特权

评论回复
14
huzi2099| | 2018-4-24 22:46 | 只看该作者
beyond696 发表于 2018-4-24 17:36
自己建表,不用格式化,接电脑后自动就识别为U盘,用FAT16文件系统,我刚刚做完一个这样的项目,还顺便解决 ...

那你发一下啊,不知道你fat表有多大

使用特权

评论回复
15
yklstudent|  楼主 | 2018-4-25 09:09 | 只看该作者
huzi2099 发表于 2018-4-24 22:46
那你发一下啊,不知道你fat表有多大

0xEB,0x3C,0x90,0x4D,0x53,0x44,0x4F,0x53,0x35,0x2E,0x30,0x00,0x02,0x01,0x06,0x00,
        0x02,0x00,0x02,0x50,0x00,0xF8,0x01,0x00,0x01,0x00,0x01,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x80,0x00,0x29,0x85,0x76,0xED,0x06,0x4E,0x4F,0x20,0x4E,0x41,
        0x4D,0x45,0x20,0x20,0x20,0x20,0x46,0x41,0x54,0x31,0x32,0x20,0x20,0x20,0x33,0xC9,
        0x8E,0xD1,0xBC,0xF0,0x7B,0x8E,0xD9,0xB8,0x00,0x20,0x8E,0xC0,0xFC,0xBD,0x00,0x7C,
        0x38,0x4E,0x24,0x7D,0x24,0x8B,0xC1,0x99,0xE8,0x3C,0x01,0x72,0x1C,0x83,0xEB,0x3A,
        0x66,0xA1,0x1C,0x7C,0x26,0x66,0x3B,0x07,0x26,0x8A,0x57,0xFC,0x75,0x06,0x80,0xCA,
        0x02,0x88,0x56,0x02,0x80,0xC3,0x10,0x73,0xEB,0x33,0xC9,0x8A,0x46,0x10,0x98,0xF7,
        0x66,0x16,0x03,0x46,0x1C,0x13,0x56,0x1E,0x03,0x46,0x0E,0x13,0xD1,0x8B,0x76,0x11,
        0x60,0x89,0x46,0xFC,0x89,0x56,0xFE,0xB8,0x20,0x00,0xF7,0xE6,0x8B,0x5E,0x0B,0x03,
        0xC3,0x48,0xF7,0xF3,0x01,0x46,0xFC,0x11,0x4E,0xFE,0x61,0xBF,0x00,0x00,0xE8,0xE6,
        0x00,0x72,0x39,0x26,0x38,0x2D,0x74,0x17,0x60,0xB1,0x0B,0xBE,0xA1,0x7D,0xF3,0xA6,
        0x61,0x74,0x32,0x4E,0x74,0x09,0x83,0xC7,0x20,0x3B,0xFB,0x72,0xE6,0xEB,0xDC,0xA0,
        0xFB,0x7D,0xB4,0x7D,0x8B,0xF0,0xAC,0x98,0x40,0x74,0x0C,0x48,0x74,0x13,0xB4,0x0E,
        0xBB,0x07,0x00,0xCD,0x10,0xEB,0xEF,0xA0,0xFD,0x7D,0xEB,0xE6,0xA0,0xFC,0x7D,0xEB,
        0xE1,0xCD,0x16,0xCD,0x19,0x26,0x8B,0x55,0x1A,0x52,0xB0,0x01,0xBB,0x00,0x00,0xE8,
        0x3B,0x00,0x72,0xE8,0x5B,0x8A,0x56,0x24,0xBE,0x0B,0x7C,0x8B,0xFC,0xC7,0x46,0xF0,
        0x3D,0x7D,0xC7,0x46,0xF4,0x29,0x7D,0x8C,0xD9,0x89,0x4E,0xF2,0x89,0x4E,0xF6,0xC6,
        0x06,0x96,0x7D,0xCB,0xEA,0x03,0x00,0x00,0x20,0x0F,0xB6,0xC8,0x66,0x8B,0x46,0xF8,
        0x66,0x03,0x46,0x1C,0x66,0x8B,0xD0,0x66,0xC1,0xEA,0x10,0xEB,0x5E,0x0F,0xB6,0xC8,
        0x4A,0x4A,0x8A,0x46,0x0D,0x32,0xE4,0xF7,0xE2,0x03,0x46,0xFC,0x13,0x56,0xFE,0xEB,
        0x4A,0x52,0x50,0x06,0x53,0x6A,0x01,0x6A,0x10,0x91,0x8B,0x46,0x18,0x96,0x92,0x33,
        0xD2,0xF7,0xF6,0x91,0xF7,0xF6,0x42,0x87,0xCA,0xF7,0x76,0x1A,0x8A,0xF2,0x8A,0xE8,
        0xC0,0xCC,0x02,0x0A,0xCC,0xB8,0x01,0x02,0x80,0x7E,0x02,0x0E,0x75,0x04,0xB4,0x42,
        0x8B,0xF4,0x8A,0x56,0x24,0xCD,0x13,0x61,0x61,0x72,0x0B,0x40,0x75,0x01,0x42,0x03,
        0x5E,0x0B,0x49,0x75,0x06,0xF8,0xC3,0x41,0xBB,0x00,0x00,0x60,0x66,0x6A,0x00,0xEB,
        0xB0,0x42,0x4F,0x4F,0x54,0x4D,0x47,0x52,0x20,0x20,0x20,0x20,0x0D,0x0A,0x52,0x65,
        0x6D,0x6F,0x76,0x65,0x20,0x64,0x69,0x73,0x6B,0x73,0x20,0x6F,0x72,0x20,0x6F,0x74,
        0x68,0x65,0x72,0x20,0x6D,0x65,0x64,0x69,0x61,0x2E,0xFF,0x0D,0x0A,0x44,0x69,0x73,
        0x6B,0x20,0x65,0x72,0x72,0x6F,0x72,0xFF,0x0D,0x0A,0x50,0x72,0x65,0x73,0x73,0x20,
        0x61,0x6E,0x79,0x20,0x6B,0x65,0x79,0x20,0x74,0x6F,0x20,0x72,0x65,0x73,0x74,0x61,
        0x72,0x74,0x0D,0x0A,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xAC,0xCB,0xD8,0x55,0xAA,
这个就是自己定义的FAT表,128K的

使用特权

评论回复
16
beyond696| | 2018-4-25 09:44 | 只看该作者
yklstudent 发表于 2018-4-25 09:09
0xEB,0x3C,0x90,0x4D,0x53,0x44,0x4F,0x53,0x35,0x2E,0x30,0x00,0x02,0x01,0x06,0x00,
        0x02,0x00,0x02,0 ...

我的只有2048字节

使用特权

评论回复
17
huzi2099| | 2018-4-25 10:51 | 只看该作者
本帖最后由 huzi2099 于 2018-4-25 11:56 编辑
yklstudent 发表于 2018-4-25 09:09
0xEB,0x3C,0x90,0x4D,0x53,0x44,0x4F,0x53,0x35,0x2E,0x30,0x00,0x02,0x01,0x06,0x00,
        0x02,0x00,0x02,0 ...

保留1个扇区和1个fat表也行吧,总共是20480字节

不过有个麻烦的地方,就是stm32的flash不能单独扇区擦



使用特权

评论回复
18
yklstudent|  楼主 | 2018-4-25 20:48 | 只看该作者
huzi2099 发表于 2018-4-25 10:51
保留1个扇区和1个fat表也行吧,总共是20480字节

不过有个麻烦的地方,就是stm32的flash不能单独扇区擦

是的,所以写数据前,需要确定所写扇区所在的FLASH区块,读出整个区块的数据,然后在更新数据,最后把数据写到FLASH内;
思路过程就是这样的,但是现在测试结果有问题;感觉应该是读写函数存在问题,但是看不出自己写的有啥问题

使用特权

评论回复
19
huzi2099| | 2018-5-25 20:13 | 只看该作者
yklstudent 发表于 2018-4-25 09:09
0xEB,0x3C,0x90,0x4D,0x53,0x44,0x4F,0x53,0x35,0x2E,0x30,0x00,0x02,0x01,0x06,0x00,
        0x02,0x00,0x02,0 ...

        0xEB, 0x3C, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x08, 0x01, 0x00,
        0x01, 0x00, 0x01, 0x80, 0x10, 0xF8, 0x02, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x00, 0x00, 0x00, 0x00, 0x4E, 0x4F, 0x20, 0x4E, 0x41,
        0x4D, 0x45, 0x20, 0x20, 0x20, 0x20, 0x46, 0x41, 0x54, 0x31, 0x32, 0x20, 0x20, 0x20, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xAA,
       
        0xF8, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,        
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00

弄了一个更小的,一个引导扇区,2个扇区的FAT表,1个fat表,每簇8扇区,根目录要占一个簇,一共是11个扇区

使用特权

评论回复
20
yklstudent|  楼主 | 2018-5-26 20:45 | 只看该作者
huzi2099 发表于 2018-5-25 20:13
0xEB, 0x3C, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x08, 0x01, 0x00,
...

你测试过有效,能解决我提的问题?

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:本人熟悉STM32、PIC、AVR等嵌入式软件开发;联系方式:524716771.

39

主题

3256

帖子

22

粉丝