cancel
Showing results for 
Search instead for 
Did you mean: 

Flash Programming

sirpak
Associate II
Posted on July 29, 2014 at 21:40

Hi,

I am using STM32F103R8T6.

I normally perform the in-circuit programming using the STMFlashLoader.exe

Sometimes I need to erase and program some pages performing an In-Application programming. I use the library function FLASH_ProgramWord().

The flash gets programmed correctly, but when later on I try to re-program using the STMFlashLoader.exe (I selectively erase only the pages that I program) it gives me error in verifying (''some pages might be write protected'').

In order to solve the problem I am obliged to perform an Erase All.

Can someone help me on this? Do I miss something?

Thanks,

Marco

#!psychic #flash #stm32l
3 REPLIES 3
Posted on July 29, 2014 at 22:40

Can someone help me on this? Do I miss something?

Seem to be aggravating something. Do you modify the Option Bytes?

Can you present a complete/concise example of what you're doing now so I don't have to guess what's going on.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
sirpak
Associate II
Posted on July 30, 2014 at 12:39

In the following the function I use to program.

void UserFlashProgram()

{

u32 StartUserAdd;

u32 EndUserAdd;

u32 EraseCounter, Address;

vu32 WRPR_Value = 0xFFFFFFFF, ProtectedPages = 0x0;

u8 NbrOfPage;

u8 IxOfPage;

volatile FLASH_Status FLASHStatus;

volatile TestStatus MemoryProgramStatus;

ErrorStatus HSEStartUpStatus;

 

// Inizializzazione Variabili

FLASHStatus = FLASH_COMPLETE;

  MemoryProgramStatus = PASSED;  

  EraseCounter = 0x0;

  StartUserAdd = (u32)(0x08000000 + START_FLASH_USER_DATA_ADDRESS);

EndUserAdd = StartUserAdd + 0x0400;

  NbrOfPage = (EndUserAdd - StartUserAdd) / 0x400;

IxOfPage = (u32)START_FLASH_USER_DATA_ADDRESS/0x400;

// Capire a cosa serve!!!!

FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP|FLASH_FLAG_PGERR |FLASH_FLAG_WRPRTERR);

// Il WRPR è un registro in cui ogni bit indica se quattro pagine sono Write protected o no

WRPR_Value = FLASH_GetWriteProtectionOptionByte();

ProtectedPages = WRPR_Value & (0x00000001 << (IxOfPage/4));  

// Pages are write protected

  if (ProtectedPages == 0x00)

{

// Disable the write protection

FLASHStatus = FLASH_EraseOptionBytes();

// Generate System Reset to load the new option byte values

NVIC_GenerateSystemReset(); //////////////////////////////Problema, non restituisco nulla!!

}

  // If Pages are not write protected, perform erase and program operations. Else nothing

else

{

// Clear All pending flags

FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP|FLASH_FLAG_PGERR |FLASH_FLAG_WRPRTERR);

// Erase the FLASH pages

for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)

{

FLASHStatus = FLASH_ErasePage(StartUserAdd + (0x400 * EraseCounter));

}

Address = StartUserAdd;

while((Address < EndUserAdd) && (FLASHStatus == FLASH_COMPLETE))

{

// La funzione già di per se mi garantisce che vengano rispettate le attese

// C'è un Timeout se non ce la faccio

FLASHStatus = FLASH_ProgramWord(Address, AdLp[(Address - StartUserAdd)/4]);

Address = Address + 4;

}

if(FLASHStatus != FLASH_COMPLETE)

{

SetErrFat(ERR_DATA);

return;

}

// Check the corectness of written data

Address = StartUserAdd;

while((Address < EndUserAdd) && (MemoryProgramStatus != FAILED))

{

if((*(vu32*) Address) != AdLp[(Address - StartUserAdd)/4])

{

MemoryProgramStatus = FAILED;

}

Address += 4;

}

if(MemoryProgramStatus == FAILED)

{

SetErrFat(ERR_DATA);

return;

}

}  

Posted on July 30, 2014 at 16:45

The code would seem to have a problem with the Write Protect status if more than one page is being written, you might want to create a mask of all the bits of interest before you do that test.

So, not seeing any code to unlock the flash, change the write protection state, or output any diagnostic information as the flash proceeds.

Can your code reflash itself a second time? Does it reset? Is there an issue with the reset?

There are a number of defines and variables missing from your example making it difficult to analyze. Can you create a small stand-alone applet, that's compilable, and demonstrates the problem?
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..