cancel
Showing results for 
Search instead for 
Did you mean: 

Hitting Hardfault while switching clock from 131KHz to 32MHz. I am using STM32L031 controller.

noobmaster69
Associate III

Hi. I am using STM23L031 for a battery operated product. The expected life cycle is almost 1 year so I need to save battery as much as possible. So I wanted to use Low power sleep mode when not doing anything(I chose this because I need DMA for UART which is always ON since I receive data over UART). Also I have 3 input interrupts over GPIO pins.

Basically, when I am in run mode, I want to run the controller at 32MHz (even after exiting low power mode, I want to switch the frequency to 32MHz).

So, when entering Low power sleep mode I did this as per the datasheet:

  1. Switch OFF flash memory.
  2. Configure clock for 131kHz.
  3. Put voltage reguator in low power mode.
  4. Clear SLEEPDEEP bit(since entering sleep mode).
  5. Enter low power mode using WFI.

But when I exit from Low power mode, I get Hard Fault.

Any reason why this is happening? And how to overcome this problem?

Here is my main() function:

int main(void)
{
  /* USER CODE BEGIN 1 */
 
  /* USER CODE END 1 */
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config_32MHz();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_LPUART1_UART_Init();
  /* USER CODE BEGIN 2 */
 
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	  enter_low_power_sleep_mode();
	  SystemClock_Config_32MHz();
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

And here is my enter_low_power_sleep_mode() function:

void enter_low_power_sleep_mode(void)
{
	__HAL_FLASH_SLEEP_POWERDOWN_ENABLE();//switch off flash memory
	SystemClock_Config_131KHz();//configure clock for 131kHz
	SET_BIT(PWR->CR, PWR_CR_LPSDSR);
	CLEAR_BIT(SCB->SCR, SCB_SCR_SLEEPDEEP_Msk);//low power mode = sleep mode(clear deep sleep bit)
	__WFI();
}

NOTE: SystemClock_Config funcitons are autogenerated using STM32CubeIDE.

Also, any examples which I can refer for doing the same?

0 REPLIES 0