cancel
Showing results for 
Search instead for 
Did you mean: 

How to edit multiple FLASH pages using the STM32C0

shank
Associate III

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,

1 ACCEPTED SOLUTION

Accepted Solutions
Pavel A.
Evangelist III

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?

 

View solution in original post

7 REPLIES 7
Pavel A.
Evangelist III

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?

 

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,

Pavel A.
Evangelist III

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

 

Also PG can be set for whole block of data

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Hi, Pavel A.

Thanks for further advice.
However, the FLASH write does not work even if I make the FLASH_DELETE(); statement one time.

Even with a simpler program, the operation only works correctly for one page of writes.

The following program works fine.

 

void patch_data(){

  __disable_irq();
  huart1.Instance->CR1 &= ~0x04;

  FLASH_UNLOCK();

  FLASH_DELETE(0x0E);
  if(data[0]==51){
   FLASH_Write(0x08007000,51);
  }else if(data[0]==170){
   FLASH_Write(0x08007000,170);
  }else if(data[0]==204){
   FLASH_Write(0x08007000,204);
  }else if(data[0]==240){
   FLASH_Write(0x08007000,240);

  }

  huart1.Instance->CR1 |= 0x04;
  __enable_irq();

}

 

However, the following program, which adds another page manipulation operation, fails to write to page14, which worked fine above.

 

void patch_data(){

  __disable_irq();
  huart1.Instance->CR1 &= ~0x04;

  FLASH_UNLOCK();

  FLASH_DELETE(0x0F); //add
  FLASH_Write(0x08007800,170); //add

  FLASH_DELETE(0x0E);
  if(data[0]==51){
   FLASH_Write(0x08007000,51);
  }else if(data[0]==170){
   FLASH_Write(0x08007000,170);
  }else if(data[0]==204){
   FLASH_Write(0x08007000,204);
  }else if(data[0]==240){
   FLASH_Write(0x08007000,240);

  }

  huart1.Instance->CR1 |= 0x04;
  __enable_irq();

}

I am using USART and SPI in addition to FLASH write, is it possible that other functions are affecting this?

 

Best regards,

 

 

Pavel A.
Evangelist III

Yes it is very possible. 

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.