2019-07-24 06:21 AM
When trying to implement sleep mode it appears the code enters and exits sleep mode once and then gets stuck in sleep mode after and will not wake back up. I posted some code below of what I believe affects the sleep function. I want the code to sleep, wake up due to interrupt and perform function, and then go back to sleep until next interrupt. Using STM32L476VG and True Studio
void init_All(void){
MICRO_SPI_Init();
init_wireless();
FLASH_INIT();
power_init();
enter_sleep();
}
int main(void)
{
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_SPI3_Init();
MX_SPI2_Init();
MX_UART4_Init();
MX_USART1_UART_Init();
__HAL_UART_ENABLE_IT(&huart4, UART_IT_RXNE); //Enables Receive Interrupt
init_All();
done=1;
while(1){
}
}
static void MX_GPIO_Init(void)
{
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOE_CLK_ENABLE();
GPIO_InitStruct.Pin = MICRO_CS;
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(MICRO_CS_PORT, &GPIO_InitStruct);
/* EXTI interrupt init ERB Clock Select EXTI12*/
HAL_NVIC_SetPriority(EXTI2_IRQn, 1, 0);
HAL_NVIC_EnableIRQ(EXTI2_IRQn);
}
/* USER CODE BEGIN 4 */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if (GPIO_Pin==MICRO_CS)
{
HAL_ResumeTick();
if (done==1){
sendDataState();
}
enter_sleep();
}
}
void power_init(void)
{
/* Disable Prefetch Buffer */
__HAL_FLASH_PREFETCH_BUFFER_DISABLE();
/* Reset all RCC Sleep and Stop modes register to */
/* improve power consumption */
RCC->AHB1SMENR = 0x0;
RCC->AHB2SMENR = 0x0;
RCC->AHB3SMENR = 0x0;
RCC->APB1SMENR1 = 0x0;
RCC->APB1SMENR2 = 0x0;
RCC->APB2SMENR = 0x0;
/* Configure the system Power */
SystemPower_Config();
}
/**
* @brief System Power Configuration
* @retval None
*/
void SystemPower_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Set all GPIO in analog state to reduce power consumption */
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOE_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOG_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Pin = GPIO_PIN_All;
HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
HAL_GPIO_Init(GPIOE, &GPIO_InitStructure);
HAL_GPIO_Init(GPIOF, &GPIO_InitStructure);
HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
HAL_GPIO_Init(GPIOH, &GPIO_InitStructure);
__HAL_RCC_GPIOA_CLK_DISABLE();
__HAL_RCC_GPIOB_CLK_DISABLE();
__HAL_RCC_GPIOC_CLK_DISABLE();
__HAL_RCC_GPIOD_CLK_DISABLE();
__HAL_RCC_GPIOE_CLK_DISABLE();
__HAL_RCC_GPIOF_CLK_DISABLE();
__HAL_RCC_GPIOG_CLK_DISABLE();
__HAL_RCC_GPIOH_CLK_DISABLE();
/* Enable PWR clock */
__HAL_RCC_PWR_CLK_ENABLE();
//Under Voltage Protection
PWR_PVDTypeDef sConfigPVD = { 0 };
//2.9 V under voltage
sConfigPVD.PVDLevel =PWR_PVDLEVEL_6;
HAL_PWR_ConfigPVD(&sConfigPVD);
HAL_PWR_EnablePVD();
HAL_NVIC_SetPriority(PVD_PVM_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(PVD_PVM_IRQn);
}
/**
* @brief Enter Sleep Mode
* @retval None
*/
void enter_sleep(void){
/*Suspend Tick increment to prevent wakeup by Systick interrupt.
Otherwise the Systick interrupt will wake up the device within 1ms (HAL time base)*/
HAL_SuspendTick();
/* Enter Sleep Mode , wake up is done once User push-button is pressed */
HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
}