# STM32F4 disc board sleep mode

**URL:** <https://forums.freertos.org/t/stm32f4-disc-board-sleep-mode/20715>\
**Category:** Kernel\
**Created:** [June 14, 2024, 1:02pm UTC](https://forums.freertos.org/t/stm32f4-disc-board-sleep-mode/20715 "2024-06-14T13:02:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![simon88](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/simon88/32/4698_2.png) [@simon88](https://forums.freertos.org/u/simon88)\
**Post date:** [June 14, 2024, 1:02pm UTC](https://forums.freertos.org/t/stm32f4-disc-board-sleep-mode/20715/1 "2024-06-14T13:02:36Z")

</div>

Hi all,  
I know this is not the first post on the subject, but I have been trying for some time now to put my board into sleep mode and wake it up via an interrupt on a gpio but without success. I read a lot of posts and tried a lot of methods without success.  
My goal is to put my card in sleep mode for an unknown duration while waiting for an interrupt to wake the board up.

According to my research I have to use the tickless mode of freertos. Here is what I’ve tried :  
FreeRTOSconfig.h

> #define configUSE\_TICKLESS\_IDLE 1  
> #define configEXPECTED\_IDLE\_TIME\_BEFORE\_SLEEP 100

My EXTI9\_5\_IRQHandler for PB9 : (works fine)

```auto
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  uint32_t current_time = HAL_GetTick();
  if (__HAL_GPIO_EXTI_GET_IT(STAT_ALIM_GPIO_PIN) != RESET) {
    __HAL_GPIO_EXTI_CLEAR_IT(STAT_ALIM_GPIO_PIN);
    if (current_time - last_interrupt_time > 50) {
      last_interrupt_time = current_time;
      if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_9) == GPIO_PIN_RESET) {
        vTaskNotifyGiveFromISR(T_handleStatAlimInterrupt, &xHigherPriorityTaskWoken);
        portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
      }
    }
  }

```

```auto
void vPortSuppressTicksAndSleep(TickType_t xExpectedIdleTime) {
  __disable_irq();
  __DSB();
  __ISB();

  if (eTaskConfirmSleepModeStatus() == eAbortSleep) {
    __enable_irq();
  } else {
    SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;

    HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
    SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;

    __enable_irq();
  }
}

```

And my task where I want to enter in sleep mode 🙂

```auto
void User_handleStatAlimInterrupt(__unused void const *argument) {
  while (1) {
    ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
    if(HAL_GPIO_ReadPin (STAT_ALIM_GPIO_PORT, STAT_ALIM_GPIO_PIN)) {
      USER_LOG_INFO("stat alim 1");

      vTaskSuspend(task1);
      vTaskSuspend(task2);

      __disable_irq();
      __HAL_GPIO_EXTI_CLEAR_FLAG(GPIO_PIN_9);
      __HAL_RCC_PWR_CLK_ENABLE();
      HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
      __enable_irq();
      USER_LOG_INFO("wake");

      vTaskResume(task1);
      vTaskResume(task2);

    }else{
      USER_LOG_INFO("stat alim 0");
    }
  }
}

```

My problem is, as soon as I go to sleep I wake up directly without having any interruption on my pin, any ideas?

Thanks

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [June 17, 2024, 5:12am UTC](https://forums.freertos.org/t/stm32f4-disc-board-sleep-mode/20715/2 "2024-06-17T05:12:27Z")

</div>

> [@simon88](#):
>
> `void vPortSuppressTicksAndSleep(TickType_t xExpectedIdleTime) {`

Can you try using the default `vPortSuppressTicksAndSleep` and use `configPRE_SLEEP_PROCESSING` to call `HAL_PWR_EnterSLEEPMode`?

> [@simon88](#):
>
> My problem is, as soon as I go to sleep I wake up directly without having any interruption on my pin, any ideas?

Is it possible that an interrupt is pending which is waking the device up? Does the control go to any ISR as soon as you enable interrupts?
