Skip to main content
Graduate
May 31, 2024
Solved

How to edit multiple FLASH pages using the STM32C0

  • May 31, 2024
  • 3 replies
  • 1769 views

Hello.

I am using STM32C0 to do FLASH writing, I want to use 4 pages from page12~15 to do FLASH writing because the data I want to write in FLASH is not enough for 1 page.

Currently
(1) FLASH unlocking
(2)page12 flash erase
(3) page12 FLASH write
------------------------------------
(4) page13 flash erase

When I design a program in this order, if I only write (1) to (3), it can write to page12 normally, but if I write (1) to (4), writing to page12 does not work either.

Is it possible to write to multiple pages in one program? Also, when editing multiple pages, in what order should I perform flash unlock, erase, and write?

 

Best regards,

    This topic has been closed for replies.
    Best answer by Pavel A.

    Yes it is possible to write to multiple pages, but only once to any location after erasing the page. So in your example, if you erased page 12, write to 12, then erase other page, then write to 12 again, you have to write to address that has NOT been written before.

    in what order should I perform flash unlock, erase, and write?

    This is quite obvious, no?

     

    3 replies

    Pavel A.Answer
    Super User
    May 31, 2024

    Yes it is possible to write to multiple pages, but only once to any location after erasing the page. So in your example, if you erased page 12, write to 12, then erase other page, then write to 12 again, you have to write to address that has NOT been written before.

    in what order should I perform flash unlock, erase, and write?

    This is quite obvious, no?

     

    shankAuthor
    Graduate
    June 1, 2024

    Thank you for your answer.

    I understand. If so, it looks like I can write in order to pages12~15, which is what I currently want to do. However, only one page is being written successfully at the moment.

    Below is the FLASH writing program I wrote. I would like to know if you find any modifications.


    void FLASH_UNLOCK(){
    while ((FLASH->SR & FLASH_SR_BSY1) != 0)
    {
    }
    FLASH->KEYR = 0x45670123;
    FLASH->KEYR = 0xCDEF89AB;
    }

     

    void FLASH_WRITE(uint32_t address, uint64_t data){
    uint32_t data2 = (uint32_t)(data>>32);


    FLASH->CR |= FLASH_CR_PG;

    *(__IO uint32_t*)address =(uint32_t)data;
    *(__IO uint32_t*)(address+4) =data2;

    while ((FLASH->SR & FLASH_SR_BSY1) != 0)
    {
    }
    if ((FLASH->SR & FLASH_SR_EOP) != 0)
    {
    FLASH->SR &= ~FLASH_SR_EOP;
    }
    else
    {
    }
    FLASH->CR &= ~FLASH_CR_PG;
    }

     

    void FLASH_DELETE(uint16_t address){

    FLASH->CR |= FLASH_CR_PER;
    FLASH->CR |= (FLASH_CR_PNB & (address<<3));
    FLASH->CR |= FLASH_CR_STRT;
    while ((FLASH->SR & FLASH_SR_BSY1) != 0)
    {
    }
    if ((FLASH->SR & FLASH_SR_EOP) != 0)
    {
    FLASH->SR |= FLASH_SR_EOP;
    }
    else
    {
    }

    FLASH->CR &= ~FLASH_CR_PER;
    }

     

    void patch_data(){

    uint16_t i;

    FLASH_UNLOCK();
    FLASH_DELETE(0x0C);
    FLASH_WRITE(0x08006000,170);

    FLASH_UNLOCK();
    FLASH_DELETE(0x0D);
    for(i=0;i<256;i++){
    FLASH_WRITE(0x08006800+i*0x00000008,send_data[i+1]);
    }

    FLASH_UNLOCK();
    FLASH_DELETE(0x0E);
    for(i=0;i<256;i++){
    FLASH_WRITE(0x08007000+i*0x00000008,send_data[i+257]);
    }

    FLASH_UNLOCK();
    FLASH_DELETE(0x0F);
    for(i=0;i<64;i++){
    FLASH_WRITE(0x08007800+i*0x00000008,send_data[i+513]);
    }
    }

     

    Best regards,

    Super User
    June 1, 2024

    One unlock is enough. When you repeat unlock, it does not have desired effect.

     

    Graduate II
    June 1, 2024

    Also PG can be set for whole block of data

    Super User
    June 6, 2024

    Yes it is very possible. 

    shankAuthor
    Graduate
    June 12, 2024

    Hi,Pavel A.

    I have successfully written 4 pages by carefully checking the values written to FLASH one page at a time. It seems that I made a mistake in specifying the writing range of each page.
    Thanks for your advice.