AnsweredAssumed Answered

STM32CubeMX freeRTOS tickless Idle can not go into STOP Mode

Question asked by Huang Gong on Jan 13, 2018
Latest reply on May 2, 2018 by buff thokoa

Hi, I use STM32CubeMX 4.23.0 to generate a FreeRTOS IAR project for STM32L151VB with tickless idle mode enabled, i want to put the MCU into STOP mode in PreSleepProcessing, i also want to set all the GPIO pins except the  EXTI interrupt pins in to GPIO_MODE_ANALOG mode before entering STOP mode,  but the MCU can not go into STOP mode. 

 

void Power_Init(void)
{
   __COMP_CLK_DISABLE();

   HAL_PWR_DisablePVD();

   HAL_PWREx_EnableUltraLowPower();

   HAL_PWREx_EnableFastWakeUp();

   HAL_DBGMCU_DisableDBGStopMode();
}


/***************************************/
void Power_Config_Enter_Stop()
{
   GPIO_InitTypeDef GPIO_InitStruct;

   HAL_ADC_MspDeInit(&hadc);
   HAL_UART_DeInit(&huart1);
   HAL_UART_DeInit(&huart2);
   HAL_UART_DeInit(&huart3);

   HAL_SPI_DeInit( &hspi1);
   HAL_I2C_DeInit( &hi2c1 );
   HAL_I2C_DeInit( &hi2c2 );
   HAL_TIM_PWM_DeInit(&htim3);

 

   GPIO_InitStruct.Pin = GPIO_PIN_1;
   GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

 

   GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
   GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
   GPIO_InitStruct.Pull = GPIO_NOPULL;
   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

 

   GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;
   GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
   GPIO_InitStruct.Pull = GPIO_NOPULL;
   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

   GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9;
   GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
   GPIO_InitStruct.Pull = GPIO_NOPULL;
   HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);


   GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
   GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
   GPIO_InitStruct.Pull = GPIO_NOPULL;
   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

   GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
   GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
   GPIO_InitStruct.Pull = GPIO_NOPULL;
   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

   GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
   GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
   GPIO_InitStruct.Pull = GPIO_NOPULL;
   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);


/*Configure GPIO pins : PE2 PE6 PE7 PE8
PE9 PE10 PE11 PE12
PE13 PE0 PE1 */
   GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8
         |GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12
         |GPIO_PIN_13|GPIO_PIN_0|GPIO_PIN_1
         |MOTOR_A_Pin|MOTOR_B_Pin|KEYBOARD_Reset_Pin|KEYBOARD_EN_Pin;
   GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
   GPIO_InitStruct.Pull = GPIO_NOPULL;
   HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);

/*Configure GPIO pins : PC13 PCPin PC3 PC5
PC6 PC7 PC8 PC9
PC10 PC11 PC12 */
   GPIO_InitStruct.Pin = GPIO_PIN_13|FP_Reset_Pin|GPIO_PIN_3|GPIO_PIN_5
      |GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9
      |GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12
      |FP_VIN_CRL_Pin|POWER_ADC_CTRL_Pin;
   GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
   GPIO_InitStruct.Pull = GPIO_NOPULL;
   HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

/*Configure GPIO pins : PB0 PB1 PB2 PB12
PB13 PB14 PB15 PB3
PB8 PB9 */
   GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_12
      |GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_3
      |GPIO_PIN_8|GPIO_PIN_9|CARD_Reset_Pin;
   GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
   GPIO_InitStruct.Pull = GPIO_NOPULL;
   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);


/*Configure GPIO pins : PD13 PD14 PD15 PD0
PD1 PD2 PD3 PD4
PD5 PD6 PD7 */
GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_0
      |GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4
      |GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7
      |BLE_WAKEUP_Pin|BLE_RESET_Pin;
   GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
   GPIO_InitStruct.Pull = GPIO_NOPULL;
   HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

/*Configure GPIO pins : PA8 PA15 */
   GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_15|WIFI_State_Pin|SPI1_CS_Pin|WIFI_Reset_Pin;
   GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
   GPIO_InitStruct.Pull = GPIO_NOPULL;
   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);


/*Configure GPIO pin : PH2 */
   GPIO_InitStruct.Pin = GPIO_PIN_2;
   GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
   GPIO_InitStruct.Pull = GPIO_NOPULL;
   HAL_GPIO_Init(GPIOH, &GPIO_InitStruct);

 

   __HAL_GPIO_EXTI_CLEAR_IT(KEYBOARD_INT_Pin|CARD_INT_Pin|BLE_INT_Pin|FP_INT_Pin);
   __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
   __HAL_UART_CLEAR_FLAG(&huart2, UART_FLAG_RXNE);
   __HAL_UART_CLEAR_FLAG(&huart3, UART_FLAG_RXNE);

}

uint8_t temp_buf[ 2 ];
***************************************/
void Power_Config_Exit_Stop()
{
   SystemClock_Config();


   __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);


   MX_GPIO_Init();

   MX_USART1_UART_Init();
   MX_USART2_UART_Init();
   MX_USART3_UART_Init();

   MX_I2C1_Init();
   MX_I2C2_Init();
   MX_SPI1_Init();
   MX_ADC_Init();
   MX_TIM3_Init();

//turn on receive IT
if(HAL_UART_Receive_IT(&huart1,temp_buf,1)!=HAL_OK) {
   Error_Handler();
}
if(HAL_UART_Receive_IT(&huart2,temp_buf,1)!=HAL_OK) {
   Error_Handler();
}
if(HAL_UART_Receive_IT(&huart3,temp_buf,1)!=HAL_OK) {
   Error_Handler();
}
}

void PreSleepProcessing(uint32_t * ulExpectedIdleTime)
{
/* Called by the kernel before it places the MCU into a sleep mode because
configPRE_SLEEP_PROCESSING() is #defined to PreSleepProcessing().

NOTE: Additional actions can be taken here to get the power consumption
even lower. For example, peripherals can be turned off here, and then back
on again in the post sleep processing function. For maximum power saving
ensure all unused pins are in their lowest power state. */

/*
(*ulExpectedIdleTime) is set to 0 to indicate that PreSleepProcessing contains
its own wait for interrupt or wait for event instruction and so the kernel vPortSuppressTicksAndSleep
function does not need to execute the wfi instruction
*/

   if( system_busy_bits == 0 ) {
      *ulExpectedIdleTime = 0;
      in_stop_mode = true;
      Power_Config_Enter_Stop();
      HAL_PWR_EnterSTOPMode(PWR_MAINREGULATOR_ON, PWR_STOPENTRY_WFI);
      Power_Config_Exit_Stop();

   }else{
/*Enter to sleep Mode using the HAL function HAL_PWR_EnterSLEEPMode with WFI instruction*/
      *ulExpectedIdleTime = 1;
   }

}

Outcomes