cancel
Showing results for 
Search instead for 
Did you mean: 

Sleep mode FreeRTOS

jampino
Associate II

Hello all, I am doing some tests with a NUCLEO-U575ZI-Q Board and FreeRTOS. I have created a simple program based on a STM32 examples, the unit will go into EnterSTOP2Mode and wakes up when the button is pressed.

This is the code I have on the main.c file:

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

/* Configure the System Power */
SystemPower_Config();

/* USER CODE BEGIN SysInit */

/* USER CODE END SysInit */

/* Initialize all configured peripherals */
MX_GPIO_Init();
/* USER CODE BEGIN 2 */

/* USER CODE END 2 */

/* Init scheduler */
osKernelInitialize(); /* Call init function for freertos objects (in freertos.c) */
MX_FREERTOS_Init();

/* 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 */
}

I have a logic analyzer connected to one of the pins so I can see if the Tick is called and incremented:

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
  /* USER CODE BEGIN Callback 0 */

  /* USER CODE END Callback 0 */
  if (htim->Instance == TIM6) {
    HAL_IncTick();
    HAL_GPIO_TogglePin(TICK_OUTPUT_GPIO_Port, TICK_OUTPUT_Pin);
  }
  /* USER CODE BEGIN Callback 1 */

  /* USER CODE END Callback 1 */
}

 

The code on the app_freertos.c file:

void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */
  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */
  /* creation of WakeSem */
  WakeSemHandle = osSemaphoreNew(1, 1, &WakeSem_attributes);

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  osSemaphoreAcquire(WakeSemHandle, osWaitForever);
  /* 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, ... */
  /* USER CODE END RTOS_QUEUES */
  /* creation of defaultTask */
  defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);

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

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

void StartDefaultTask(void *argument)
{
  /* USER CODE BEGIN defaultTask */
  /* Infinite loop */
  for(;;)
  {
	osSemaphoreAcquire(WakeSemHandle, osWaitForever);
  }
  /* USER CODE END defaultTask */
}

The callback for the button

void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)
{
      osSemaphoreRelease(WakeSemHandle);
}

and the pre/postSleepProcessing

__weak void PreSleepProcessing(uint32_t ulExpectedIdleTime)
{
	/* place for user code */
        HAL_GPIO_WritePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin, 1);
        HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, 0);
        HAL_SuspendTick();
	HAL_PWREx_EnterSTOP2Mode(PWR_STOPENTRY_WFI);
}

__weak void PostSleepProcessing(uint32_t ulExpectedIdleTime)
{
    /* place for user code */
    SystemClock_Config();
    HAL_ResumeTick();

    HAL_GPIO_WritePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin, 0);
    HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, 1);

    //	osDelay(500);
    //  HAL_Delay(500);
    //  vTaskDelay(500);
    HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, 0);
}

The problem I am having is that when trying to use any of the commented out delays on  the PostSleepProcessing function, none of them seem to work. The osDelay don't seem to stop at all between switching on and off the led and I will see on the analyzer that the pin will toogle only once as I press the button and go to sleep again. The HAL_Delay and the vTaskDelay will stop the system, as I pressed the button to come out from sleep, the blue led will turn off and the Red led will turn on as expected but it will never be turned off again. I am not completely sure why the system just hangs on the delay, the analyzer is not showing anything so the HAL_TIM_PeriodElapsedCallback is not called at all.

If I don't send the unit to sleep, I can see on the analyzer that the pin is toggling as expected all the time (HAL_TIM_PeriodElapsedCallback)

Can someone help me to understand why this is happening or if I am doing something wrong?

Thank you

0 REPLIES 0