工业中经常使用EEPROM(电可擦除可编程存储器)来存储更新数据,为降低成本,可以使用FLASH代替。

EEPROM 仿真可以通过多种方式实现,但要注意 Flash 限制和产品要求。下面详述的方法要求为非易失性数据分配至少两个相同大小的 Flash 扇区:一个在开始时擦除,支持逐字编程;另一个在需要对前一扇区执行垃圾回收时接管工作。占用每扇区前半个字(16 位)的头字段指示扇区的状态。在本文档的其余部分,将每一个扇区视为一页,这些页分别称为Page0 和 Page1。


| 0x08000001(0xFFFF|0xFFFF) | |
| 虚拟地址1 | 1 |
| 虚拟地址2 | 2 |
| 虚拟地址3 | 3 |
| 虚拟地址4 | 4 |
写入数据
| 0x00 | 0x04 | 0x08 | 0x0C | ········ | 0x3FF0 | 0x3FF4 | 0x3FF8 | 0x3FFC | |||||||||
| 16位地址 | 16位数据 | 16位地址 | 16位数据 | 16位地址 | 16位数据 | 16位地址 | 16位数据 | ||||||||||
flash默认擦除状态,0xFFFF,从头向后检查,检查到数据地址位0xFFFF,即填入地址并写入数据。
写满后,将该Page的有效数据,都刷新到新的Page,并写入该新的数据。
读数据
从后向前对比,当查询地址与flash中地址一致,取出该值,跳出循环。
/**
******************************************************************************
* @file EEPROM/EEPROM_Emulation/inc/eeprom.h
* @author MCD Application Team
* @version V1.3.2
* @date 13-November-2015
* @brief This file contains all the functions prototypes for the EEPROM
* emulation firmware library.
******************************************************************************
* @attention
*
* <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions 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 its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER 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.
*
******************************************************************************
*/
//
/***********************************************************************************
* Copyright (C),2008-2020,Terry Tech.Co.,Ltd
* File name: feeprom.h
* Author: Terry Version: 1.0
* Description: stm32F103 一页1K, 待存储的数据 < 256 个 uint6_t 个数据,
* 不增大Page的情况下(2 page情况下),参数数量原则越少越好
* 建议参数20个以内,这样使用寿命约为 10 X 10000 即10万次
* Others:
* Function List:
*
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __EEPROM_H
#define __EEPROM_H
/* Includes ------------------------------------------------------------------*/
#include "stm32f1xx_hal.h"
/* Exported constants --------------------------------------------------------*/
/* EEPROM emulation firmware error codes */
#define EE_OK (uint32_t)HAL_OK
#define EE_ERROR (uint32_t)HAL_ERROR
#define EE_BUSY (uint32_t)HAL_BUSY
#define EE_TIMEOUT (uint32_t)HAL_TIMEOUT
/* Define the size of the sectors to be used */
#define PAGE_SIZE (uint32_t)0x400 /* Page size = 1KByte */
/* Device voltage range supposed to be [2.7V to 3.6V], the operation will
be done by word */
//#define VOLTAGE_RANGE (uint8_t)VOLTAGE_RANGE_3
/* EEPROM start address in Flash */
#define EEPROM_START_ADDRESS ((uint32_t)0x08018000) /* EEPROM emulation start address:
from sector2 : after 1KByte of used
Flash memory */
/* Pages 0 and 1 base and end addresses */
#define PAGE0_BASE_ADDRESS ((uint32_t)(EEPROM_START_ADDRESS + 0x000))
#define PAGE0_END_ADDRESS ((uint32_t)(EEPROM_START_ADDRESS + (PAGE_SIZE - 1)))
//#define PAGE0_ID FLASH_SECTOR_2
#define PAGE1_BASE_ADDRESS ((uint32_t)(EEPROM_START_ADDRESS + PAGE_SIZE))
#define PAGE1_END_ADDRESS ((uint32_t)(EEPROM_START_ADDRESS + (2 * PAGE_SIZE - 1)))
//#define PAGE1_ID FLASH_SECTOR_3
// 更多页的负载均衡使用方法,此处略过 Terry 2020.03.01
//
//
/* Used Flash pages for EEPROM emulation */

本文介绍STM32微控制器上实现EEPROM仿真的方法,通过使用两个Flash扇区进行数据存储和垃圾回收,确保数据完整性和延长Flash寿命。文章详细解释了扇区状态管理、数据读写流程及损耗均衡策略。

3288

被折叠的 条评论
为什么被折叠?



