简体   繁体   English

STM32 在 Flash 扇区擦除时崩溃

[英]STM32 Crash on Flash Sector Erase

I'm trying to write 4 uint32's of data into the flash memory of my STM32F767ZI so I've looked at some examples and in the reference manual but still I cannot do it.我正在尝试将 4 个 uint32 的数据写入我的 STM32F767ZI 的 flash memory,所以我查看了一些示例和参考手册,但我仍然无法做到。 My goal is to write 4 uint32's into the flash and read them back and compare with the original data, and light different leds depending on the success of the comparison.我的目标是将 4 个 uint32 写入 flash 并读回并与原始数据进行比较,并根据比较的成功点亮不同的 LED。 My code is as follows:我的代码如下:

void flash_write(uint32_t offset, uint32_t *data, uint32_t size) {
    FLASH_EraseInitTypeDef EraseInitStruct = {0};
    uint32_t SectorError = 0;

    HAL_FLASH_Unlock();

    EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
    EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
    EraseInitStruct.Sector = FLASH_SECTOR_11; 
    EraseInitStruct.NbSectors = 1;
    //EraseInitStruct.Banks = FLASH_BANK_1; // or FLASH_BANK_2 or FLASH_BANK_BOTH

    st = HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError);

    if (st == HAL_OK) {
        for (int i = 0; i < size; i += 4) {
            st = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, FLASH_USER_START_ADDR + offset + i, *(data + i)); //This is what's giving me trouble
            if (st != HAL_OK) {
                // handle the error
                break;
            }
        }
    }else {
        // handle the error
    }
    HAL_FLASH_Lock();
}

void flash_read(uint32_t offset, uint32_t *data, uint32_t size) {
    for (int i = 0; i < size; i += 4) {
        *(data + i) = *(__IO uint32_t*)(FLASH_USER_START_ADDR + offset + i);
    }
}

int main(void) {

    uint32_t data[] = {'a', 'b', 'c', 'd'};
    uint32_t read_data[] = {0, 0, 0, 0};

    HAL_Init();
    SystemClock_Config();
    MX_GPIO_Init();

    flash_write(0, data, sizeof(data));
    flash_read(0, read_data, sizeof(read_data));

    if (compareArrays(data,read_data,4))
    {
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7,SET);
    }
    else
    {
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14,SET);
    }

    return 0;
}

The problem is that before writing data I must erase a sector, and when I do it with the HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) , function, the program always crashes, and sometimes even corrupts my codespace forcing me to update firmware.问题是,在写入数据之前,我必须擦除一个扇区,当我使用HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) function 执行此操作时,程序总是崩溃,有时甚至会破坏我的代码空间,迫使我更新固件。 I've selected the sector farthest from the code space but still it crashes when i try to erase it.我选择了离代码空间最远的扇区,但当我试图擦除它时它仍然崩溃。 I've read in the reference manual that我在参考手册中读到

Any attempt to read the Flash memory while it is being written or erased, causes the bus to stall.在写入或擦除 Flash memory 时,任何尝试读取它都会导致总线停止。 Read operations are processed correctly once the program operation has completed.一旦程序操作完成,读操作就会被正确处理。 This means that code or data fetches cannot be performed while a write/erase operation is ongoing.这意味着在写/擦除操作正在进行时无法执行代码或数据提取。

which I believe means the code should ideally be run from RAM while we operate on the flash, but I've seen other people online not have this issue so I'm wondering if that's the only problem I have.我认为这意味着当我们在 flash 上运行时,代码最好从 RAM 运行,但我看到网上的其他人没有这个问题,所以我想知道这是否是我遇到的唯一问题。 With that in mind I wanted to confirm if this is my only issue, or if I'm doing something wrong?考虑到这一点,我想确认这是我唯一的问题,还是我做错了什么?

In your loop, you are adding multiples of 4 to i , but then you are adding i to data .在您的循环中,您将 4 的倍数添加到i ,然后将i添加到data When you add to a pointer it is automatically multiplied by the size of the pointed type, so you are adding multiples of 16 bytes and reading past the end of your input buffer.当您添加到指针时,它会自动乘以指向类型的大小,因此您添加的是 16 字节的倍数并读取输入缓冲区的末尾。

Also, make sure you initialize all members of EraseInitStruct .另外,请确保初始化EraseInitStruct的所有成员。 Uncomment that line and set the correct value!取消注释该行并设置正确的值!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM