STM32L0 Stop Mode Not Working Properly

Viewed 6336

Currently, I am working on optimizing current consumption by using STOP Mode of the STM32L053C6T6 processor. I'm facing a problem that I can not get through, and I'm very happy if you can help. First of all, I would like to explain the algorithm in a very simplified way. In the while loop, I print to Pc "STARTED" through the UART-Serial converter. Next, I set the RTC alarm to 10 seconds later. And the finally I sleep the MCU by using STOP MODE. After 10 seconds, MCU wakes up properly. So far everything is normal. After MCU wake up, I call the System Clock Config function. I print "woken-up" on the PC screen through UART. I can work this way forever But I do not like the current that circuit consume.

To reduce the power consumption, I set all the pins to analog before entering sleep mode. this method worked very well on reducing current consumption. But after all the pins are analog and MCU goes sleep and wake up, the MCU can not send to string to PC. I think UART Module does not work properly... But I/O's and systick timer are running. I can understand this by toggling the LED in a certain delay(with HAL_DELAY) Why I can not make it work with reinit all peripherals?

Here is my main code

int main(void){
HAL_Init();
SystemClock_Config();

MX_GPIO_Init();
MX_ADC_Init();
MX_I2C1_Init();
MX_RTC_Init();
MX_TIM22_Init();
MX_USART1_UART_Init()
MX_USART2_UART_Init();;
Init_RTC();
HAL_Delay(500);
while(1)
{
    printf("Started\n\r");                      
    for(uint16_t count1 = 0;count1<25;count1++){
        HAL_GPIO_TogglePin(STATE_LED0_GPIO_Port,STATE_LED0_Pin);
        HAL_Delay(25);
    }
    SetAlarm(10);                           
    EnterStopMode();                        
    printf("Woken-Up\n\r");
    HAL_Delay(1000);    
}
}

void EnterStopMode(void){
GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_GPIOA_CLK_ENABLE();                           
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_All;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);


/* Disable GPIOs clock */
__HAL_RCC_GPIOA_CLK_DISABLE();
__HAL_RCC_GPIOB_CLK_DISABLE();

// STOP Moda girme komutu
HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
HAL_Delay(1000);
SystemClock_Config();

HAL_Delay(500);
MX_GPIO_Init();
MX_USART2_UART_Init();  

Solved

Well, I checked the Hal functions line by line. And I saw that after first peripheral initialization (uart,i2c,adc etc.), Hal functions do not allow re-init again by checking that particular peripherals instant state. For example, In HAL_UART_Init(&huart2) function, In order to Init the low level hardware: (GPIO,CLOCK) the instant uart modules state must be UART_STATE_RESET. In here

if(huart->gState == HAL_UART_STATE_RESET)
{
/* Allocate lock resource and initialize it */
huart->Lock = HAL_UNLOCKED;

/* Init the low level hardware : GPIO, CLOCK */
HAL_UART_MspInit(huart);
}

To overcome this problem, I reset that particular state of peripheral init. Because without not clearing those flags, uart init functions does not enter HAL_UART_MspInit(huart);

I know that is bulky solution, but it works for now.

void EnterStopMode(void)
{
  GPIO_InitTypeDef GPIO_InitStruct;
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();

 /* Configure all GPIO port pins in Analog Input mode (floating input trigger OFF) */
 GPIO_InitStruct.Pin = GPIO_PIN_All;
 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 
 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

 /* Disable GPIOs clock */
 __HAL_RCC_GPIOA_CLK_DISABLE();
 __HAL_RCC_GPIOB_CLK_DISABLE();

 HAL_TIM_Base_Stop_IT(&htim22);
 HAL_ADC_Stop_IT(&hadc);

 /* Enter Stop Mode */

 HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);

 /* init the SYS Clocks */
 SystemClock_Config();

 /*Init IO's */
 MX_GPIO_Init();

 /* Clear Peripheral States for MspInit *7
 huart1.gState = HAL_UART_STATE_RESET;
 huart2.gState = HAL_UART_STATE_RESET;
 hi2c1.State   = HAL_I2C_STATE_RESET;
 hadc.State    = HAL_ADC_STATE_RESET;

 MX_USART2_UART_Init();  
 MX_USART1_UART_Init();    
 MX_I2C1_Init();
 MX_RTC_Init();
 MX_TIM22_Init();
 MX_ADC_Init();
 HAL_ADCEx_Calibration_Start(&hadc,ADC_SINGLE_ENDED);   
 }
0 Answers
Related