STM32——写和读flash [英] STM32 - writing and reading flash

查看:89
本文介绍了STM32——写和读flash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我我做错了什么或理解了什么愚蠢的事情?作为测试,我试图将一个简单的数字写入闪存并检索它.成功后,我会将其扩展为 6 个有符号值.我的设备是 STM32L476RG

Can someone tell me what stupid thing I am doing wrong or understanding? As a test, I am trying to write a simple number into flash and retrieve it. Once successful, I will expand this to 6 signed values. My device is an STM32L476RG

uint64_t data = 88;
Erase_Flash();
HAL_FLASH_Unlock();

Address = ADDR_FLASH_PAGE_256;
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_FAST, Address, data) != HAL_OK)
    serprintf("Error writing flash.");

HAL_FLASH_Lock();
uint8_t *flash_biases = (uint8_t*) (ADDR_FLASH_PAGE_256);

根据我所读到的内容,我应该能够像我一样访问闪存.但它没有检索到我期望的值.

Based on what I've read, I should be able to access the flash memory like I have. But it's not retrieving the value I expect.

Erase_Flash() 函数如下所示:

The Erase_Flash() function looks like this:

void Erase_Flash() {
    HAL_FLASH_Unlock();
    /* Clear OPTVERR bit set on virgin samples */
    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);

    /* Fill EraseInit structure*/
    EraseInitStruct.TypeErase = FLASH_TYPEERASE_MASSERASE;
    EraseInitStruct.Banks = FLASH_BANK_2;

    if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK) {
        serprintf("Error erasing flash.");
    }
    HAL_FLASH_Lock();
}

推荐答案

FLASH_TYPEPROGRAM_FAST 模式用于一次写入 32 个双字,使用该模式时,第三个参数(data) 成为那个 32 双字数据源的原始起始地址,而不是数据本身.

FLASH_TYPEPROGRAM_FAST mode is used for writing 32 double words at once and when this mode is used, the third argument (data) becomes the raw starting address of that 32 double word data source, not the data itself.

目前,您的代码从从 0x88 开始的地址获取数据并将其(共 256 字节)写入闪存.地址 0x88 上似乎有 245 个.

Currently, your code fetches data from the address starting from 0x88 and writes it (a total of 256 bytes) to the flash. It appears that there is 245 on address 0x88.

您需要使用FLASH_TYPEPROGRAM_DOUBLEWORD 来写入uint64_t 数据.

You need to use FLASH_TYPEPROGRAM_DOUBLEWORD for writing uint64_t data.

这篇关于STM32——写和读flash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆