Skip to main content
Visitor II
November 5, 2019
Question

IAR for STM8, FLASH_EraseBlock stuck in release mode

  • November 5, 2019
  • 2 replies
  • 1130 views

#define RAM_EXECUTION (1) configured with STM8AF52xx

Use FLASH_EraseBlock in debug mode to erase normally.

In release mode (medium or high optimization) use FLASH_EraseBlock to die.

why?

    This topic has been closed for replies.

    2 replies

    Explorer
    November 5, 2019

    Most probably because you use questionable constructs, or omit data/variable qualifiers.

    If you e.g. not using "volatile" for externally referenced variables, the compiler will optimize it out.

    Llai.930Author
    Visitor II
    November 5, 2019

    I use STM8S_StdPeriph_Driver\src\stm8s_flash.c

    IN_RAM(void FLASH_EraseBlock(uint16_t BlockNum, FLASH_MemType_TypeDef FLASH_MemType))
    {
     uint32_t startaddress = 0;
     
    #if defined(STM8S105) || defined(STM8S005) || defined(STM8S103) || defined(STM8S003) || \
     defined(STM8S001) || defined(STM8S903) || defined (STM8AF626x) || defined (STM8AF622x)
     uint32_t PointerAttr *pwFlash;
    #elif defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined (STM8AF62Ax) || defined (STM8AF52Ax) 
     uint8_t PointerAttr *pwFlash;
    #endif
     
     /* Check parameters */
     assert_param(IS_MEMORY_TYPE_OK(FLASH_MemType));
     if(FLASH_MemType == FLASH_MEMTYPE_PROG)
     {
     assert_param(IS_FLASH_PROG_BLOCK_NUMBER_OK(BlockNum));
     startaddress = FLASH_PROG_START_PHYSICAL_ADDRESS;
     }
     else
     {
     assert_param(IS_FLASH_DATA_BLOCK_NUMBER_OK(BlockNum));
     startaddress = FLASH_DATA_START_PHYSICAL_ADDRESS;
     }
     
     /* Point to the first block address */
    #if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined (STM8AF62Ax) || defined (STM8AF52Ax)
     pwFlash = (PointerAttr uint8_t *)(MemoryAddressCast)(startaddress + ((uint32_t)BlockNum * FLASH_BLOCK_SIZE));
    #elif defined(STM8S105) || defined(STM8S005) || defined(STM8S103) || defined(STM8S003) || \
     defined(STM8S001) || defined (STM8S903) || defined (STM8AF626x) || defined (STM8AF622x)
     pwFlash = (PointerAttr uint32_t *)(MemoryAddressCast)(startaddress + ((uint32_t)BlockNum * FLASH_BLOCK_SIZE));
    #endif	/* STM8S208, STM8S207 */
     
     /* Enable erase block mode */
     FLASH->CR2 |= FLASH_CR2_ERASE;
     FLASH->NCR2 &= (uint8_t)(~FLASH_NCR2_NERASE);
     
    #if defined(STM8S105) || defined(STM8S005) || defined(STM8S103) || defined(STM8S003) || \
     defined(STM8S001) || defined(STM8S903) || defined (STM8AF626x) || defined (STM8AF622x)
     *pwFlash = (uint32_t)0;
    #elif defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined (STM8AF62Ax) || \
     defined (STM8AF52Ax)
     *pwFlash = (uint8_t)0;
     *(pwFlash + 1) = (uint8_t)0;
     *(pwFlash + 2) = (uint8_t)0;
     *(pwFlash + 3) = (uint8_t)0; 
    #endif
    }

    Explorer
    November 5, 2019

    I have no experience with Flash erase/program on the STM8. Just did a project some while ago.

    How about changing the optimization settings in the Debug version to medium/high, and trying to debug the issue ?

    Or, instrument the Release version build (serial output or GPIO toggle) to see how far you really come.

    Llai.930Author
    Visitor II
    November 8, 2019

    Has anyone encountered a similar problem?