我在STM32中的虚拟eeprom库在闪存中保存查找表,选择了3kByte的页面大小来保存所有数据,并且在页面已满之前它可以正常工作。但是在写入新数据时调用nonstop,总是会抛出错误。在这个例程中,第1页是有效页面,如果复制到第0页,就会出现失败。重新启动后,EE_Init()函数会抛出错误并且无法恢复,直到完成完全闪存。有谁知道这里有什么不对吗?STM32 EEPROM库中是否有错误?我从eeprom.h库和下面的函数EE_PageTransfer()附加了我的设置。我无法添加整个eeprom.c文件,因为它太大了。如果有人知道如何正确上传源文件,请告诉我。编辑:我第一个想到它可能是什么。首先,正确复制页面0,它似乎传输页面。但是在将1kbyte写入第1页后,PageFull错误再次出现,而不是在3kbyte之后。从那时起,始终会抛出页面完整错误。库中某处有错误吗?编辑2:看起来这个库不是为2kByte以外的页面大小而制作的,但我确信这可以以某种方式改变。我会试着弄清楚如何。2kByte页面大小一切正常。- /* Define to prevent recursive inclusion -------------------------------------*/
- #ifndef __EEPROM_H
- #define __EEPROM_H
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f3xx_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
- /* Base address of the Flash sectors */
- #define ADDR_FLASH_PAGE_0 ((uint32_t)0x08000000) /* Base @ of Page 0, 2 Kbytes */
- #define ADDR_FLASH_PAGE_1 ((uint32_t)0x08000800) /* Base @ of Page 1, 2 Kbytes */
- #define ADDR_FLASH_PAGE_2 ((uint32_t)0x08001000) /* Base @ of Page 2, 2 Kbytes */
- #define ADDR_FLASH_PAGE_3 ((uint32_t)0x08001800) /* Base @ of Page 3, 2 Kbytes */
- #define ADDR_FLASH_PAGE_4 ((uint32_t)0x08002000) /* Base @ of Page 4, 2 Kbytes */
- #define ADDR_FLASH_PAGE_5 ((uint32_t)0x08002800) /* Base @ of Page 5, 2 Kbytes */
- #define ADDR_FLASH_PAGE_6 ((uint32_t)0x08003000) /* Base @ of Page 6, 2 Kbytes */
- #define ADDR_FLASH_PAGE_7 ((uint32_t)0x08003800) /* Base @ of Page 7, 2 Kbytes */
- #define ADDR_FLASH_PAGE_8 ((uint32_t)0x08004000) /* Base @ of Page 8, 2 Kbytes */
- #define ADDR_FLASH_PAGE_9 ((uint32_t)0x08004800) /* Base @ of Page 9, 2 Kbytes */
- #define ADDR_FLASH_PAGE_10 ((uint32_t)0x08005000) /* Base @ of Page 10, 2 Kbytes */
- #define ADDR_FLASH_PAGE_11 ((uint32_t)0x08005800) /* Base @ of Page 11, 2 Kbytes */
- #define ADDR_FLASH_PAGE_12 ((uint32_t)0x08006000) /* Base @ of Page 12, 2 Kbytes */
- #define ADDR_FLASH_PAGE_13 ((uint32_t)0x08006800) /* Base @ of Page 13, 2 Kbytes */
- #define ADDR_FLASH_PAGE_14 ((uint32_t)0x08007000) /* Base @ of Page 14, 2 Kbytes */
- #define ADDR_FLASH_PAGE_15 ((uint32_t)0x08007800) /* Base @ of Page 15, 2 Kbytes */
- #define ADDR_FLASH_PAGE_16 ((uint32_t)0x08008000) /* Base @ of Page 16, 2 Kbytes */
- #define ADDR_FLASH_PAGE_17 ((uint32_t)0x08008800) /* Base @ of Page 17, 2 Kbytes */
- #define ADDR_FLASH_PAGE_18 ((uint32_t)0x08009000) /* Base @ of Page 18, 2 Kbytes */
- #define ADDR_FLASH_PAGE_19 ((uint32_t)0x08009800) /* Base @ of Page 19, 2 Kbytes */
- #define ADDR_FLASH_PAGE_20 ((uint32_t)0x0800A000) /* Base @ of Page 20, 2 Kbytes */
- #define ADDR_FLASH_PAGE_21 ((uint32_t)0x0800A800) /* Base @ of Page 21, 2 Kbytes */
- #define ADDR_FLASH_PAGE_22 ((uint32_t)0x0800B000) /* Base @ of Page 22, 2 Kbytes */
- #define ADDR_FLASH_PAGE_23 ((uint32_t)0x0800B800) /* Base @ of Page 23, 2 Kbytes */
- #define ADDR_FLASH_PAGE_24 ((uint32_t)0x0800C000) /* Base @ of Page 24, 2 Kbytes */
- #define ADDR_FLASH_PAGE_25 ((uint32_t)0x0800C800) /* Base @ of Page 25, 2 Kbytes */
- #define ADDR_FLASH_PAGE_26 ((uint32_t)0x0800D000) /* Base @ of Page 26, 2 Kbytes */
- #define ADDR_FLASH_PAGE_27 ((uint32_t)0x0800D800) /* Base @ of Page 27, 2 Kbytes */
- #define ADDR_FLASH_PAGE_28 ((uint32_t)0x0800E000) /* Base @ of Page 28, 2 Kbytes */
- #define ADDR_FLASH_PAGE_29 ((uint32_t)0x0800E800) /* Base @ of Page 29, 2 Kbytes */
- #define ADDR_FLASH_PAGE_30 ((uint32_t)0x0800F000) /* Base @ of Page 30, 2 Kbytes */
- #define ADDR_FLASH_PAGE_31 ((uint32_t)0x0800F800) /* Base @ of Page 31, 2 Kbytes */
- /* Define the size of the sectors to be used */
- #define PAGE_SIZE (uint32_t)FLASH_PAGE_SIZE /* Page size */ //Now 6kByte -> HEX 0x1800
- /* EEPROM start address in Flash */
- #define EEPROM_START_ADDRESS ((uint32_t)ADDR_FLASH_PAGE_26) /* EEPROM emulation start address */
- /* Pages 0 and 1 base and end addresses */
- #define PAGE0_BASE_ADDRESS ((uint32_t)(EEPROM_START_ADDRESS + 0x0000))
- #define PAGE0_END_ADDRESS ((uint32_t)(EEPROM_START_ADDRESS + (PAGE_SIZE - 1)))
- #define PAGE1_BASE_ADDRESS ((uint32_t)(ADDR_FLASH_PAGE_29))
- #define PAGE1_END_ADDRESS ((uint32_t)(ADDR_FLASH_PAGE_29 + PAGE_SIZE - 1))
- /* Used Flash pages for EEPROM emulation */
- #define PAGE0 ((uint16_t)0)
- #define PAGE1 ((uint16_t)1) /* Page nb between PAGE0_BASE_ADDRESS & PAGE1_BASE_ADDRESS*/
- /* No valid page define */
- #define NO_VALID_PAGE ((uint16_t)0x00AB)
- /* Page status definitions */
- #define ERASED ((uint16_t)0xFFFF) /* Page is empty */
- #define RECEIVE_DATA ((uint16_t)0xEEEE) /* Page is marked to receive data */
- #define VALID_PAGE ((uint16_t)0x0000) /* Page containing valid data */
- /* Valid pages in read and write defines */
- #define READ_FROM_VALID_PAGE ((uint8_t)0x00)
- #define WRITE_IN_VALID_PAGE ((uint8_t)0x01)
- /* Page full define */
- #define PAGE_FULL ((uint16_t)0x1800)
- /* Variables' number */
- #define NB_OF_VAR ((uint16_t)1536) //Half a page, since Address will also be safed -> always uint32_t
- /* Exported types ------------------------------------------------------------*/
- /* Exported macro ------------------------------------------------------------*/
- /* Exported functions ------------------------------------------------------- */
- uint16_t EE_Init(void);
- uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data);
- uint16_t EE_WriteVariable(uint16_t VirtAddress, uint16_t Data);
- #endif /* __EEPROM_H */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码- static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data)
- {
- HAL_StatusTypeDef flashstatus = HAL_OK;
- uint32_t newpageaddress = EEPROM_START_ADDRESS;
- uint32_t oldpageid = 0;
- uint16_t validpage = PAGE0, varidx = 0;
- uint16_t eepromstatus = 0, readstatus = 0;
- uint32_t page_error = 0;
- FLASH_EraseInitTypeDef s_eraseinit;
- /* Get active Page for read operation */
- validpage = EE_FindValidPage(READ_FROM_VALID_PAGE);
- if (validpage == PAGE1) /* Page1 valid */
- {
- /* New page address where variable will be moved to */
- newpageaddress = PAGE0_BASE_ADDRESS;
- /* Old page ID where variable will be taken from */
- oldpageid = PAGE1_BASE_ADDRESS;
- }
- else if (validpage == PAGE0) /* Page0 valid */
- {
- /* New page address where variable will be moved to */
- newpageaddress = PAGE1_BASE_ADDRESS;
- /* Old page ID where variable will be taken from */
- oldpageid = PAGE0_BASE_ADDRESS;
- }
- else
- {
- return NO_VALID_PAGE; /* No valid Page */
- }
- /* Set the new Page status to RECEIVE_DATA status */
- flashstatus = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, newpageaddress, RECEIVE_DATA);
- /* If program operation was failed, a Flash error code is returned */
- if (flashstatus != HAL_OK)
- {
- return flashstatus;
- }
- /* Write the variable passed as parameter in the new active page */
- eepromstatus = EE_VerifyPageFullWriteVariable(VirtAddress, Data);
- /* If program operation was failed, a Flash error code is returned */
- if (eepromstatus != HAL_OK)
- {
- return eepromstatus;
- }
- /* Transfer process: transfer variables from old to the new active page */
- for (varidx = 0; varidx < NB_OF_VAR; varidx++)
- {
- if (varidx != VirtAddress) /* Check each variable except the one passed as parameter */
- {
- /* Read the other last variable updates */
- readstatus = EE_ReadVariable(varidx, &DataVar);
- /* In case variable corresponding to the virtual address was found */
- if (readstatus != 0x1)
- {
- /* Transfer the variable to the new active page */
- eepromstatus = EE_VerifyPageFullWriteVariable(varidx, DataVar);
- /* If program operation was failed, a Flash error code is returned */
- if (eepromstatus != HAL_OK)
- {
- return eepromstatus;
- }
- }
- }
- }
- s_eraseinit.TypeErase = FLASH_TYPEERASE_PAGES;
- s_eraseinit.PageAddress = oldpageid;
- s_eraseinit.NbPages = 1;
- /* Erase the old Page: Set old Page status to ERASED status */
- flashstatus = HAL_FLASHEx_Erase(&s_eraseinit, &page_error);
- /* If erase operation was failed, a Flash error code is returned */
- if (flashstatus != HAL_OK)
- {
- return flashstatus;
- }
- /* Set new Page status to VALID_PAGE status */
- flashstatus = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, newpageaddress, VALID_PAGE);
- /* If program operation was failed, a Flash error code is returned */
- if (flashstatus != HAL_OK)
- {
- return flashstatus;
- }
- /* Return last operation flash status */
- return flashstatus;
- }
复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>