RX651 cannot self program the same 128 byte page twice without erasing


I am using the Renesas 65n1 Flash FIT module to program the code flash on the R6519AxFP.

I can program a page of 128 bytes after an erasure.

I tryed with "HEJ", keeping all the other bytes as 0xFF. This works.

The i tried to program the same page of 128 bytes as

"HEJ MOR" but i get a FLASH_ERR_FAILURE error.

I can however write "Hello World! I like Cake!" to the page if i erase the entire block (32 KiB) first and then write to it.

I can also write to other 128 byte pages without erasing, but i cannot rewrite the same page.

Here is my test code:


    char test[256];

    test[0] = hal_flash_erase(0xFFFE8000, 1); //Erase
    hal_flash_read(test, 0xFFFE8080, 128);     //Check data = ok

    test[0] = 'H';
    test[1] = 'E';
    test[2] = 'J';
    test[0] = hal_flash_write(test, 0xFFFE8080, 128); //Works as intended
    hal_flash_read(test, 0xFFFE8000, 256);

    test[0] = 'H';
    test[1] = 'E';
    test[2] = 'J';
    test[3] = ' ';
    test[4] = 'M';
    test[5] = 'O';
    test[6] = 'R';
    test[0] = hal_flash_write(test, 0xFFFE8080, 128);  //Gives error FLASH_ERR_FAILURE
    hal_flash_read(test, 0xFFFE8000, 256);

    test[0] = 'H';
    test[1] = 'E';
    test[2] = 'J';
    test[0] = hal_flash_write(test, 0xFFFE8000, 128); //Works
    hal_flash_read(test, 0xFFFE8000, 256);

Any ideas?

  • That's specification. Memory cells must only be programmed once. Before reprogramming it must be erased. Reprogramming without erase would put increased stress on the memory cell and is prohibited.

  • In reply to FrankL:

    So it is impossible to change this?
  • In reply to FrankL:

    can you pinpoint this in the manual? just so i can get the clearest image possible

    Best Regards
  • In reply to Worker101:


    Has your question been answered?

    Mike Clements
    RenesasRulz Moderator
  • In reply to Worker101:

    I'm also curious about where this is in the manual, if you're saying two consecutive writes to the same page of RX65* code flash without an intervening erase will return error. I seem to be running into the same issue. I've written to a page of code flash, keeping one sequence of bytes as 0xFF so I can set them later, but I get the PGMERR bit in flash every time I try to do a 2nd write.

    If this is true, it's contrary to the behavior of most flash memory I've worked with. Is the code flash setting a private bit in the block indicating it's been written to and, if set when another write occurs, fails the write?


  • In reply to AnthonyJenkins:

    It is stated on page 2599 in the datasheet for the RX65*

    (3) Prohibition of Additional Programming
    Programming a given area of the code flash memory or data flash memory twice is not possible. To program the code flash memory or data flash memory where has been programed, erase the target area. Programming can be added to the option-setting memory. For details, refer to Flash Memory User’s Manual: Hardware Interface.


    Best regards

  • In reply to Worker101:

    Thanks Worker101! This morning I finally got my bits of user code moved into the RAM segment that r_flash_rx uses for destructive code flash operations and I can safely rewrite code flash.


  • In reply to Worker101:

    Worker101, and Anthony Jenkins,

    I am glad to hear that your question has been answered and your programming is successfull!

    Mike Clements
    RenesasRulz Moderator
  • In reply to AnthonyJenkins:


    I have a similar problem with the RX65N, using the r_flash_rx FIT module.
    I don't understand what you did to be able to safely rewrite code flash.
    Would you mind to provide some details on what you did ?

    Thanks and Best Regards,
  • In reply to ChristopheL:

    Hi Christophe,

    I've chatted with you about this over email, thought I'd also share the algorithm I'm using.

    The problem is trying to change the contents of a block of MCU code/data flash after it has been written once.  This MCU family forbids writes to a block which was previously written.  To write to a previously written MCU flash block, you must do the following

    • If modifying code flash:
      • perform all steps in the app note describing executing code flash operations from RAM
      • ensure ALL functions called while erasing and writing code flash are allocated to the PFRAM linker section, including the function which calls R_FLASH_Erase() and R_FLASH_Write().  This includes preventing interrupt routines from running between the two function calls.
    • Allocate a block of memory the same size as the flash erase block to be modified.
    • Copy the contents of the flash block to be modified to the memory buffer.
      • If reading the flash block fails, then the block is presumed erased; fill memory buffer with 0xFF.
    • Modify the memory buffer with the data to be written to the flash block.
    • [Begin critical section]
    • Erase flash block
    • Write memory buffer to flash block
    • [End critical section]

    The [critical section] should have context switches disabled within it.  The function containing the [critical section] should also be within the PFRAM linker section.

    If the block to be erased is already erased, the above algorithm can be short-circuited by simply writing the desired data to the flash block, but this was not the original use case (block has been written and needs to be written again).