STM32l496 Sleep Mode problem with FreeRTOS

I am trying to enter sleepmode in freertos, but for some reason the system does not enter it.

GLOBAL void SleepMode_Activate(void) {


	unsigned int sleep;
		  flag  = 1;
	/*Disable display task if not already disabled*/
	if (mt_TaskStatus(_TN_ANZ) != _SLEEPING) {
		mt_Terminate(_TN_ANZ);	//Disabled Display
	}

	/*Turn off lcd and other stuff*/
	vw_SlowHwAus();
	/*disables COMs if necessary*/
	/*Enable there alternate wakeup pins of COM*/
	vw_ComBeenden();
	slowspeed_timers = 1;
	/*Enable interrupt on buttons, before they were working in timer*/
	tas_Starten();
	mt_TimerDis();
	osThreadTerminate(Tasks_ID[_TN_KZR]);
	SleepMode_DisableUart();


    hw_SetSpeedSlow();
	HAL_SuspendTick();
	  SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;



		 HAL_PWR_EnterSLEEPMode(PWR_LOWPOWERREGULATOR_ON, PWR_SLEEPENTRY_WFI);

	//************** Slow Section Start
	do {
		hw_Wdr();

	//	bib_Nop025us();
		// because of Instruction Que
		if (bSekNeu)		    // meanwhile a change of seconds takes place ?
		{
			bSekNeu = 0;
			vw_TestNetzSlow();	// Check supply
		}
	} while (bBedienReq == 0 && bUmwReq == 0// bMinNew == 0 is included in bUmwReq
	&& SioReq == 0 && MeldReq == 0);// as long as no key, no min, no SIO, no message
	//************** Slow End section



   //__HAL_FLASH_SLEEP_POWERDOWN_DISABLE();
   /* System is Low Power Run mode when exiting Low Power Sleep mode,
       disable low power run mode and reset the clock to initialization configuration */
    HAL_PWREx_DisableLowPowerRunMode();

   hw_SetSpeedFast();

   /* Resume Tick interrupt if disabled prior to Low Power Sleep mode entry */
   HAL_ResumeTick();
   SysTick->CTRL  |= SysTick_CTRL_TICKINT_Msk;
	slowspeed_timers = 0;
	mt_TimerEn();
	uhr_LiesRtc();			// Complete time
	vw_ComReact();			// V2.0

	//osKernelRestoreLock(lock)


}

I also disabled systick and my base timer. I made sure no interrupts are being generated, but still I cant go into sleepmode. I think this is some kind of a bug. I am not using tickless idle right now for some reasons.

When do you call this function?
Does it work if you call it before starting the scheduler?

This function is called when some conditions are meet.

I did not try before calling the schedualer. Thanks for this idea. I will try it tomorrow and update here.

So, I tested it.
before initializing the kernel the system is stopping at sleepmode.

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();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_CRC_Init();
  MX_TIM5_Init();
  MX_TIM1_Init();
  MX_TIM17_Init();
  MX_USART1_UART_Init();
  MX_SPI1_Init();
  MX_ADC1_Init();
  MX_USART3_UART_Init();
  MX_TIM6_Init();
  MX_USART2_UART_Init();
  MX_FMC_Init();
  MX_UART4_Init();
  MX_IWDG_Init();
  MX_UART5_Init();
  MX_TIM2_Init();
  MX_TIM7_Init();
  MX_TIM4_Init();
  MX_RTC_Init();
  /* USER CODE BEGIN 2 */
 // crc_CCITT_Init();

  hw_SetSpeedSlow();
	HAL_SuspendTick();
	  SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
	// sleep = osKernelSuspend();


		 HAL_PWR_EnterSLEEPMode(PWR_LOWPOWERREGULATOR_ON, PWR_SLEEPENTRY_WFI);

  /* USER CODE END 2 */

  /* Init scheduler */
  osKernelInitialize();

  /* USER CODE BEGIN RTOS_MUTEX */
	/* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
	/* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
	/* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
//DO NOT disable this default task
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of defaultTask */
 // defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);

  /* USER CODE BEGIN RTOS_THREADS */


 // HAL_UART_DeInit(&huart1);
 // HAL_UART_DeInit(&huart2);
 // HAL_UART_DeInit(&huart3);




	vw_Main();







	/* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
	/* add events, ... */
  /* USER CODE END RTOS_EVENTS */

  /* Start scheduler */
  osKernelStart();

  /* We should never get here as control is now taken by the scheduler */
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
	while (1) {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */

	}
  /* USER CODE END 3 */
}

Even if I put sleep api after osKernelInitialize(); it stays in sleep.

I think it is that some interrupts are being generated ? but I disabled them all before calling it. or kernel itself is waking the system ?

That should not happen.

Is it possible to figure out which interrupt is firing, may be by toggling some LED from the ISR?

I have about 10 interrupts that execute in my application.
I will try to debug if anyone of them are triggering for some reason.

will update here