我正在使用 CubeIDE 为 STM32F302K8UX 编写代码,我需要一个变量在复位时保持不变。
我看到 ppl 这样做的一种方法是创建一个直接保存在闪存上的变量(至少这是我对它的理解)。我从@Stephan 在这篇文章的回答中得到它: 如何在 STM32F4、Cortex M4 上写入/读取到 FLASH
因此,我尝试对其进行一些修改。在 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);