2021-09-29 01:11 AM
Hello,
I try to write into program flash in block write. Therefore I have a function
which is executed from RAM:
void ram_flash_write_block(uint32_t startaddress, const uint8_t *buf) {
uint16_t i = 0;
// enable flash block write
FLASH->CR2 = FLASH_CR2_PRG;
FLASH->NCR2 = (uint8_t)(~FLASH_NCR2_NPRG);
// write data from buffer to flash
for (i = 0; i < FLASH_BLOCK_SIZE; i++)
*((volatile uint8_t*) (uint32_t)(startaddress + i)) = ((uint8_t)(buf[i]));
//Wait for write operation to complete according to PM0051, Page 18, Step 6
while((FLASH->IAPSR & FLASH_IAPSR_EOP)!=FLASH_IAPSR_EOP);
}
FLASH_BLOCK_SIZE is 128 (I am on STM8AF5288). startaddress in my case is 0xC000.
The program stops, if I write the flash control registers CR2 and NCR2
consecutively. If I write only one if these registers, the program does not stop
(but also does not write to flash...).
This is a hint, that the block programming mode is activated after writing the
two registers, because on page 17 of the PM0051, we read:
If the program goes back to main program memory, it is stalled until the block
program operation is complete.
On Page 18, we read at step 3:
The programming cycle starts automatically when all data in the block have been
written.
How does the STM8 know, when all data in the block have been written? 128
consecutive write operations?
If I read the flash memory content via ST-Link, I see, that no bytes are written
into flash, so the program stops execution before the programming cycle.
I use SDCC 4.1.0 with memory model large. But the behaviour is the same without the memory model large option, and also on SDCC 3.8.0