如何在STM32F302上读写FLASH

问题描述

我正在使用 CubeIDE 为 STM32F302K8UX 编写代码,我需要一个变量在重置后保持不变。

我看到 ppl 这样做的一种方法是制作一个直接保存在闪存中的变量(至少这是我对它的理解)。 我从@Stephan 在这文章的回答中得到了它: How to write/read to FLASH on STM32F4,Cortex M4

所以我尝试通过一些修改来遵循它。在 STM32F302K8UX_FLASH.ld 的 MEMORY 部分:

/* Memories deFinition */
MEMORY
{
  RAM    (xrw)    : ORIGIN = 0x20000000,LENGTH = 16K
  FLASH    (rx)    : ORIGIN = 0x8000000,LENGTH = 63K // subtracted 1kb from original value (64K)
  DATA (rx)       : ORIGIN = 0x0800FC00,LENGTH = 1K // created new section at the end of prevIoUs (0x8000000 + (63 * 1024))
}

然后在 SECTIONS 部分,我在开头添加了这个:

SECTIONS
{
  .user_data (NOLOAD):
  {
    . = ALIGN(4);
    _user_data_start = .; /* create a global symbol at user_data start */
     KEEP(*(.user_data))
    . = ALIGN(4);
    _user_data_end = .;  /* create a global symbol at user_data end */
  } >DATA

...

现在在我的主代码中,我声明了变量:

uint8_t persistent_config __attribute__ ((section(".user_data")));

但是我收到了一个我无法理解的链接错误

14:03:41 **** Incremental Build of configuration Debug for project sonicPlayer ****
make -j12 all 
arm-none-eabi-gcc "../Core/Src/main.c" -mcpu=cortex-m4 -std=gnu11 -g3 -DUSE_HAL_DRIVER -DSTM32F302x8 -DDEBUG -c -I../Core/Inc -I../Drivers/STM32F3xx_HAL_Driver/Inc -I../Drivers/STM32F3xx_HAL_Driver/Inc/Legacy -I../Drivers/CMSIS/Device/ST/STM32F3xx/Include -I../Drivers/CMSIS/Include -O0 -ffunction-sections -fdata-sections -Wall -fstack-usage -MMD -MP -MF"Core/Src/main.d" -MT"Core/Src/main.o" --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -o "Core/Src/main.o"
arm-none-eabi-gcc -o "sonicPlayer.elf" @"objects.list"   -mcpu=cortex-m4 -T"C:\Users\werne\OwnCloud\Documents\Arquivos\Drive\Projetos\SonicScrewDriver\sonicPlayer\STM32F302K8UX_FLASH.ld" --specs=nosys.specs -Wl,-Map="sonicPlayer.map" -Wl,--gc-sections -static --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -Wl,--start-group -lc -lm -Wl,--end-group
c:\st\stm32cubeide_1.4.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.7-2018-q2-update.win32_1.4.0.202007081208\tools\arm-none-eabi\bin\ld.exe:C:\Users\werne\OwnCloud\Documents\Arquivos\Drive\Projetos\SonicScrewDriver\sonicPlayer\STM32F302K8UX_FLASH.ld:40: Syntax error
collect2.exe: error: ld returned 1 exit status
make: *** [makefile:50: sonicPlayer.elf] Error 1
"make -j12 all" terminated with exit code 2. Build might be incomplete.

14:03:42 Build Failed. 2 errors,0 warnings. (took 598ms)
Description Resource    Path    Location    Type
make: *** [makefile:50: sonicPlayer.elf] Error 1    sonicPlayer         C/C++ Problem
make: *** No rule to make target 'clean'.  Stop.    Firmware            C/C++ Problem
Syntax error    sonicPlayer     line 40,external location: c:\st\stm32cubeide_1.4.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.7-2018-q2-update.win32_1.4.0.202007081208\tools\arm-none-eabi\bin\ld.exe:C:\Users\werne\OwnCloud\Documents\Arquivos\Drive\Projetos\SonicScrewDriver\sonicPlayer\STM32F302K8UX_FLASH.ld C/C++ Problem

我该如何解决这个问题?

错误描述对我来说很神秘,我可以在谷歌上找到任何东西。

如果你能解释一下这里发生了什么,那就太好了,我是。在这种过于具体的事情上不是很有经验。

编辑 我可以看到语法错误,但不知道在哪里或什么。

编辑 2:解决方案: 为了修复错误代码,我按照@KamilCuk 的建议将 // 注释替换为 /* */ 但是代码不完整

根据所有其他答案进行了更多研究,我得到了这个链接https://os.mbed.com/users/olympux/code/eeprom_flash/ 所以我废弃了以上所有内容,将 .h 和 .c 添加到我的项目中,将 #include "mbed.h" 替换为 #include "stm32f3xx_hal.h"

然后我改变了eeprom_flash.c的第一个函数

#include "eeprom_flash.h"

FLASH_EraseInitTypeDef eraseInit = {
    FLASH_TYPEERASE_PAGES,EEPROM_START_ADDRESS,// Memory location (beggining of page)
    1                        // Number of pages to erase
};
uint32_t pageError;

/*
 * Must call this first to enable writing
 */
void enableEEPROMWriting() {
    HAL_StatusTypeDef status = HAL_FLASH_Unlock();
    HAL_FLASHEx_Erase(&eraseInit,&pageError);
}

就是这样!写入闪存:

                    enableEEPROMWriting();
                    writeEEPROMHalfWord(0x0,var);
                    disableEEPROMWriting();

并阅读

uint16_t var = readEEPROMHalfWord(0x0);

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)