cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f103ret6 flash erase problem

Jian Chen
Associate II
Posted on October 31, 2017 at 16:22

Hi,

I need to perform STM32F103RET6 flash memory operation: erase, write.  My erase failed.  Anybody has the EWARM project for internal bootloader(burnt to chip from factory)?  Or any working flash routines for STM32F103RET6?

I am using \AN4657-STM32Cube_IAP_using_UART\Projects\STM3210C_EVAL\IAP_Main project. And change USER_FLASH_END_ADDRESS from 0x08040000 to 0x08080000 to match the flash for STM32F103RET6.  I think this is the only change for the STM32F103RET6 device from STM32F107 device. All other #define macro should be the same.

But when I execute the statement “FLASH_If_Erase(APPLICATION_ADDRESS);â€� the program still went to Hard_Fault function. 

My code is:

int main(void) {

  HAL_Init();

  SystemClock_Config();

  FLASH_If_Init();

  FlashProtection = FLASH_If_GetWriteProtectionStatus();  // return to 0 indicate not write protected.

 FLASH_If_Erase(APPLICATION_ADDRESS);

while(1) {}

 }

void SystemClock_Config(void)

{

  RCC_ClkInitTypeDef clkinitstruct = {0};

  RCC_OscInitTypeDef oscinitstruct = {0};

 

  oscinitstruct.OscillatorType  = RCC_OSCILLATORTYPE_HSE;

  oscinitstruct.HSEState        = RCC_HSE_ON;

  oscinitstruct.HSEPredivValue  = RCC_HSE_PREDIV_DIV1;

  oscinitstruct.PLL.PLLState    = RCC_PLL_ON;

  oscinitstruct.PLL.PLLSource   = RCC_PLLSOURCE_HSE;

  oscinitstruct.PLL.PLLMUL      = RCC_PLL_MUL6;  // 8MHz*6=48MHz

  if (HAL_RCC_OscConfig(&oscinitstruct)!= HAL_OK)

  {

    sys_err_code |= ERR_RCC_OSC_CONFIG;

  }

  clkinitstruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);

  clkinitstruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

  clkinitstruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

  clkinitstruct.APB2CLKDivider = RCC_HCLK_DIV1;

  clkinitstruct.APB1CLKDivider = RCC_HCLK_DIV2; 

  if (HAL_RCC_ClockConfig(&clkinitstruct, FLASH_LATENCY_2)!= HAL_OK)

  {

    sys_err_code |= ERR_RCC_CLK_CONFIG;

  }

  RCC->CFGR &= ~0x0000C000;

  RCC->CFGR |= 0x00008000;      // pclk2/6 as ADC clock(=48/6=8MHz)

}

3 REPLIES 3
Posted on October 31, 2017 at 16:31

You'd want to make sure the flash sector sizes are correct, and that you don't erase code you are currently executing.

Perhaps look at what the register conditions are when it Hard Faults?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on October 31, 2017 at 18:06

I set up a break point. And the program stops at “return HAL_ERROR;â€� statement in HAL_FLASH_Unlock function.  The call tree is FLASH_If_Init()  -->  HAL_FLASH_Unlock()  --> return HAL_ERROR;

This is the beginning of flash operation. Cannot unlock flash.

HAL_StatusTypeDef HAL_FLASH_Unlock(void)

{

  if (HAL_IS_BIT_SET(FLASH->CR, FLASH_CR_LOCK))

  {

    /* Authorize the FLASH Registers access */

    WRITE_REG(FLASH->KEYR, FLASH_KEY1);

    WRITE_REG(FLASH->KEYR, FLASH_KEY2);

  }

  else

  {

    return HAL_ERROR;

  }

  return HAL_OK;

}

Amel NASRI
ST Employee
Posted on November 01, 2017 at 12:34

Hi

Chen.Jian.005

‌,

You have also to use the correct header file relative to your device (stm32f103xe.h in your case).

Why don't you try using the exampleSTM32Cube_FW_F1_V1.6.0\Projects\STM3210E_EVAL\Applications\IAP\IAP_Main?

-Amel

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.