STM32使用内部flash模拟EEPROM

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

开发环境:

Ubuntu16.04 + SW4STM32 + STM32CubeMX + STM32F103C8T6

 

首先复制SW4STM32模拟EEPROM例子中的eeprom.h和eeprom.c到自己的project,例子路径为:

~/.ac6/SW4STM32/firmwares/STM32Cube_FW_F1_V1.6.0/Projects/STM32F103RB-Nucleo/Applications/EEPROM/EEPROM_Emulation

并按如下进行修改eeprom.h。

默认的page太大,我们将其改为1024Byte,这样2个page就只占用了2k的空间。

我们所用芯片只有64k的flash,所以把最后的2k拿来模拟eeprom,因此要将起始地址 改为ADDR_FLASH_PAGE_62。

/* Define the size of the sectors to be used */
-#define PAGE_SIZE               (uint32_t)FLASH_PAGE_SIZE  /* Page size */
+#define PAGE_SIZE               1024  /* Page size */

/* EEPROM start address in Flash */
-#define EEPROM_START_ADDRESS  ((uint32_t)ADDR_FLASH_PAGE_32) /* EEPROM emulation start address */
+#define EEPROM_START_ADDRESS  ((uint32_t)ADDR_FLASH_PAGE_62) /* 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 PAGE0_ID               ADDR_FLASH_PAGE_32
+#define PAGE0_ID               EEPROM_START_ADDRESS

-#define PAGE1_BASE_ADDRESS    ((uint32_t)(EEPROM_START_ADDRESS + 0x10000))
-#define PAGE1_END_ADDRESS     ((uint32_t)(EEPROM_START_ADDRESS + 0x10000 + PAGE_SIZE - 1))
-#define PAGE1_ID               ADDR_FLASH_PAGE_96
+#define PAGE1_BASE_ADDRESS    ((uint32_t)(EEPROM_START_ADDRESS + PAGE_SIZE))
+#define PAGE1_END_ADDRESS     ((uint32_t)(EEPROM_START_ADDRESS + PAGE_SIZE + PAGE_SIZE - 1))
+#define PAGE1_ID               (EEPROM_START_ADDRESS + PAGE_SIZE)

然后在main.c中修改:

+/* Virtual address defined by the user: 0xFFFF value is prohibited */
+uint16_t VirtAddVarTab[NB_OF_VAR] = {0x5555, 0x6666, 0x7777};
+uint16_t VarDataTab[NB_OF_VAR] = {0, 0, 0};
+uint16_t VarValue = 0;


int main(void)
{
  ......
  SystemClock_Config();
 
   /* USER CODE BEGIN SysInit */
+  /* Unlock the Flash Program Erase controller */
+  HAL_FLASH_Unlock();
+
+  /* EEPROM Init */
+ EE_Init();
  ......
}

以下为测试代码,main()中直接调用进行测试。

+void EE_Test(void)
+{
+#if 1
+    /* --- Store successively many values of the three variables in the EEPROM ---*/
+      /* Store 0x1000 values of Variable1 in EEPROM */
+      for (VarValue = 1; VarValue <= 9; VarValue++)
+      {
+        EE_WriteVariable(VirtAddVarTab[0], VarValue);
+      }
+
+      /* read the last stored variables data*/
+      EE_ReadVariable(VirtAddVarTab[0], &VarDataTab[0]);
+      printf("The 1st RW:\r\n");
+      for (int i = 0; i < NB_OF_VAR; i++) {
+          printf("VarDataTab[%d]=%d\r\n", i, VarDataTab[i]);
+
+      }
+
+      /* Store 0x2000 values of Variable2 in EEPROM */
+      for (VarValue = 1; VarValue <= 4; VarValue++)
+      {
+        EE_WriteVariable(VirtAddVarTab[1], VarValue);
+      }
+
+      /* read the last stored variables data*/
+      EE_ReadVariable(VirtAddVarTab[0], &VarDataTab[0]);
+      EE_ReadVariable(VirtAddVarTab[1], &VarDataTab[1]);
+      printf("The 2nd RW:\r\n");
+      for (int i = 0; i < NB_OF_VAR; i++) {
+          printf("VarDataTab[%d]=%d\r\n", i, VarDataTab[i]);
+
+      }
+
+
+      /* Store 0x3000 values of Variable3 in EEPROM */
+      for (VarValue = 1; VarValue <= 7; VarValue++)
+      {
+        EE_WriteVariable(VirtAddVarTab[2], VarValue);
+      }
+
+      /* read the last stored variables data*/
+      EE_ReadVariable(VirtAddVarTab[0], &VarDataTab[0]);
+      EE_ReadVariable(VirtAddVarTab[1], &VarDataTab[1]);
+      EE_ReadVariable(VirtAddVarTab[2], &VarDataTab[2]);
+      printf("The 3rd RW:\r\n");
+      for (int i = 0; i < NB_OF_VAR; i++) {
+          printf("VarDataTab[%d]=%d\r\n", i, VarDataTab[i]);
+
+      }
+#endif
+}

 

转载于:https://my.oschina.net/igiantpanda/blog/1611416

你可能感兴趣的:(STM32使用内部flash模拟EEPROM)