cancel
Showing results for 
Search instead for 
Did you mean: 

LPTIM_ISR_ARROK in LPTIM1_ISR never gets set on STM32L4

SSmit.13
Senior

Hi

  I am wanting to use the LPTIM to break out of STOP2. I have a function to set the timer, and go into stop mode. After setting LPTIM1->ARR,  LPTIM_ISR_ARROK in the LPTIM1->ISR  is never set. After X amount of seconds, the STOP2 exits as expected, but the  PWR_SR1_WUF in the PWR->SR1 is never set.  The timer interupt is called when the STOP2 exits. Here is my code, not sure what I am doing wrong, anyone help?:

 

#define PWR_CR2_LPTIM1WUF_Pos   (5U)
#define PWR_CR2_LPTIM1WUF       (1UL << PWR_CR2_LPTIM1WUF_Pos)

//always gets called when STOP2 exits
void LPTIM1_IRQHandler(void)
{ 
	// Just clear the interrupt to prevent re-entry
	if (LPTIM1->ISR & LPTIM_ISR_ARRM) {
	LPTIM1->ICR = LPTIM_ICR_ARRMCF;
	}
}

void LPTIM1_StartSTOP2Timeout_ms(uint32_t ms)
{
	
	// Enable LPTIM1 and its clock
	RCC->APB1ENR1 |= RCC_APB1ENR1_LPTIM1EN;
	
	// Enable LSI clock
	RCC->CSR |= RCC_CSR_LSION;
	while (!(RCC->CSR & RCC_CSR_LSIRDY));
	
	// Select LSI as LPTIM1 clock
	RCC->CCIPR &= ~RCC_CCIPR_LPTIM1SEL;
	RCC->CCIPR |= (0b01 << RCC_CCIPR_LPTIM1SEL_Pos);
	
	// Enable LPTIM1
	LPTIM1->CR = LPTIM_CR_ENABLE;
	
	// Set prescaler /32 (1ms tick)
	LPTIM1->CFGR &= ~LPTIM_CFGR_PRESC;
	LPTIM1->CFGR |= (0b101 << LPTIM_CFGR_PRESC_Pos); // /32
	
	// Clear status flags
	LPTIM1->ICR = 0xFFFFFFFF;
	
	// Set ARR and wait for it to latch
	LPTIM1->ARR = ms;
        long Tick=0xfffff;  <<even increased 
	while (!(LPTIM1->ISR & LPTIM_ISR_ARROK))
            if(!--Tick)
              break;  //  << always breaks , and LPTIM1->ISR always 0

	// Allow wakeup from LPTIM1
	PWR->CR2 |= PWR_CR2_LPTIM1WUF;
	NVIC_EnableIRQ(LPTIM1_IRQn);
	LPTIM1->IER |= LPTIM_IER_ARRMIE;

	if (PWR->SR1 & PWR_SR1_WUF) {
	  PWR->SCR |= PWR_SCR_CWUF;  // Clear the wake-up flag before entering STOP2
	}
	
	// Set STOP2 mode
	PWR->CR1 &= ~PWR_CR1_LPMS;
	PWR->CR1 |= PWR_CR1_LPMS_STOP2;
	
	// Confirm STOP2 mode is active
	if ((PWR->CR1 & PWR_CR1_LPMS) == PWR_CR1_LPMS_STOP2) {
		printf("Entered STOP2 mode\n");  <<< gets called
	} else {
		printf("STOP2 mode not entered\n");
	}
	
	// Enable deep sleep
	SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
	
	// Start timer in one-shot mode
	LPTIM1->CR |= LPTIM_CR_SNGSTRT;

	HAL_SuspendTick();
	
	// Enable deep sleep mode (SLEEPDEEP bit in SCR)
	SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
	// Enter STOP2 mode
	printf("Entering STOP2 mode...\n");
	HAL_PWREx_EnterSTOP2Mode(PWR_STOPENTRY_WFI);  // Enter STOP2 mode with the WFI entry (Wait For Interrupt)
	
	// --- 6. After Wake-up: Handle timeout and clear wake-up flag ---
	if (PWR->SR1 & PWR_SR1_WUF) {
	  // Clear the wake-up flag
	  PWR->SCR |= PWR_SCR_CWUF;  << never gets called
	}
	HAL_ResumeTick();
	SystemClock_Config();
}

 

Many thanks

 

Scott

 

1 REPLY 1
Sarra.S
ST Employee

Hello @SSmit.13

The code seems ok! I think the issue is somewhere else, maybe the clock frequency is lower than the signal to be counted? 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.