cancel
Showing results for 
Search instead for 
Did you mean: 

STM8L Flash Programming

Han JaeSeong
Associate
Posted on August 22, 2017 at 09:15

I use STM8L152R8..

I test flash programming... But, now some problem exists...

Byte programming and word programming is no problem.

But, i use block programming then CPU halted.....

#pragma location = 'FLASH_CODE'

void Mem_ProgramBlock(u16 BlockNum, const u8 *Buffer)

{

u16 Count = 0;

u32 StartAddress = 0;

/* Set Start address wich refers to mem_type */

StartAddress = FLASH_PROGRAM_START_PHYSICAL_ADDRESS;

/* Point to the first block address */

StartAddress = StartAddress + ((u32)BlockNum * FLASH_BLOCK_SIZE);

/* Standard programming mode */

FLASH->CR2 |= (u8)0x01;

/* Copy data bytes from RAM to FLASH memory */

for (Count = 0; Count < FLASH_BLOCK_SIZE; Count++)

{

*((PointerAttr u8*)StartAddress + Count) = ((u8)(Buffer[Count]));     <= Halted

}

}

I use memory.... I unlock cpu before this function.... as below

FLASH->CR1 = 0; // Standard programming Time (EEPM, WAITM, IE Clear)

FLASH->PUKR = FLASH_RASS_KEY1; // Unlock Flash Data

FLASH->PUKR = FLASH_RASS_KEY2;

while ((FLASH->IAPSR & FLASH_IAPSR_PUL) == 0); 

I did not find out some bug......

Always  CPU halted in flash programming functioin...

Please help me..

2 REPLIES 2
Max
ST Employee
Posted on August 22, 2017 at 12:09

The Flash memory cannot be used when programming.

In byte and word programming cases, the cpu will wait until the flash is ready again, when the operation is done, to continue.

In block programming, you still need to feed data while the flash memory is still busy. 

This means the copy loop must be executed from RAM.

I recommend you look at the

http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm8-embedded-software/stsw-stm8016.html

implementation as reference. 

Once unzipped, there is an example in the STM8L15x-16x-05x-AL31-L_StdPeriph_Lib\Project\STM8L15x_StdPeriph_Examples\FLASH\Data_Program\ directory

Max
ST Employee
Posted on August 22, 2017 at 12:17

This is clearly described in the

http://www.st.com/resource/en/programming_manual/cd00226555.pdf

 PM0054 chapter 5.2