AnsweredAssumed Answered

[bug report] In stm32f1xx_hal_rcc.c from STM32Cube_FW_F1_V1.6.0

Question asked by ali maqsood on Feb 14, 2018
Latest reply on May 3, 2018 by Merle Skelton

Hi, I have generated code for STM32F103C8T6 MCU in CubeMX. My clock setting is 72Mhz. The HAL_RCC_ClockConfig function always returns HAL_ERROR. Upon debuging I found the following code in this function which returns HAL_ERROR

#if defined(FLASH_ACR_LATENCY)
  /* Increasing the number of wait states because of higher CPU frequency */
  if(FLatency > (FLASH->ACR & FLASH_ACR_LATENCY))
  {   
    /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
    __HAL_FLASH_SET_LATENCY(FLatency);
   
    /* Check that the new number of wait states is taken into account to access the Flash
    memory by reading the FLASH_ACR register */

    if((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
    {
      return HAL_ERROR;
    }
  }

 

I thought it might be due to _HAL_FLASH_SET_LATENCY(FLatency) setting latency and immediately after that checking in FLASH->ACR if the latency has been set without waiting. So, I added a wait loop as following

 

#if defined(FLASH_ACR_LATENCY)
  /* Increasing the number of wait states because of higher CPU frequency */
  if(FLatency > (FLASH->ACR & FLASH_ACR_LATENCY))
  {   
    /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
    __HAL_FLASH_SET_LATENCY(FLatency);
   
    for(uint32_t i=0; i<100; i++)
    {
         asm("nop");
    }
    /* Check that the new number of wait states is taken into account to access the Flash
    memory by reading the FLASH_ACR register */

    if((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
    {
      return HAL_ERROR;
    }
  }

 

Now it works every time. Is this the correct solution? If so, kindly fix it.

Thank you

Regards

Outcomes