cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 RTC wakeup event - counter not starting

jf659
Associate II
Posted on January 28, 2014 at 13:47

the counter receives the write value, but it doesnt start when i enable the wake up event. The WUTF bit should be set when the counter reaches 0, but this never happens.

void init_RTC(void) 
{ 
/* Enable the PWR clock */ 
RCC->APB1ENR |= (1UL << 
28
); 
/* Allow access to RTC */ 
PWR->CR |= (1UL << 
8
); 
/* Select the Clock source LSE or LSI */ 
select_LSEclk(); 
//select_LSIclk(); 
/* Enable the RTC */ 
RCC->BDCR |= (1UL << 
15
); 
/* Set the RTC time */ 
/* wait for RTC register APB synchronisation */ 
while((RTC->ISR & (1UL << 
5
)) == 0) 
{ 
} 
/* Turn on LED to verify RTC is working (DEBUG) */ 
LED_On(1); 
} 
/************************************************************************** 
LSE 
Initialize the RTC using an external clock source LSE 768 kHz 
crystal oscillator. This clock source has the lowest power consumption 
***************************************************************************/ 
void select_LSEclk(void) { 
/* Select the clock source: using LSE 32 kHz as the RTC clock 
Enable the RTC clock assuming it has the correct prescaler provided */ 
RCC->BDCR |= 0x1; 
/* Wait til LSE is ready */ 
while((RCC->BDCR & (1UL << 
1
)) == 0) 
{ 
/* Do nothing while crystal is not ready */ 
} 
/* Select the RTC clock source to LSE */ 
RCC->BDCR |= (1UL << 
8
); 
} 
/************************************************************************** 
LSI 
Initialize the RTC using an internal clock source LSI 32 kHz 
crystal oscillator. 
***************************************************************************/ 
void select_LSIclk(void) { 
/* Select the clock source: using LSI 32 kHz as the RTC clock 
Enable the RTC clock assuming it has the correct prescaler provided */ 
RCC->CSR |= 0x1; 
/* Wait til LSI is ready */ 
while((RCC->CSR & (1UL << 
1
)) == 0) 
{ 
/* Do nothing while crystal is not ready */ 
} 
/* Select the RTC clock source to LSI */ 
RCC->BDCR |= (1UL << 
9
); 
} 
/************************************************************************* 
Initialising the Auto-wakeup unit for a periodic wakeup event, 0x39 
should initialise the counter for 30 mins 
Do we want to enable the wakeup timer interrupt?? 
**************************************************************************/ 
void init_autowakeup(void) 
{ 
/* Allow access to RTC */ 
PWR->CR |= (1UL << 
8
); 
/* Disable the RTC registers Write protection */ 
rtc_write_enable(); 
/* Disable Wakeup Counter */ 
RTC_WakeUpCmd(DISABLE); 
/* Ensure access to Wakeup auto-reload 
counter and bits WUCKSEL[2:0] is allowed. 
Poll WUTWF until it is set in RTC_ISR (takes 2 RTCCLK cycles) */ 
while((RTC->ISR & (1UL << 
2
)) == 0) 
{ 
} 
RTC_WKUP_IRQHandler(); 
/* Enable the RTC registers write protection */ 
rtc_write_disable(); 
/* Enable Wakeup Counter */ 
RTC_WakeUpCmd(ENABLE); 
LED_On(3); 
} 
/************************************************************************* 
Enable the wakeup event interrupt. 
EXTI line 22 is connected to the RTC Wakeup event 
**************************************************************************/ 
void RTC_WKUP_IRQHandler(void) 
{ 
EXTI_InitTypeDef EXTI_InitStructure; 
NVIC_InitTypeDef NVIC_InitStructure; 
RTC_WakeUpClockConfig(RTC_WakeUpClock_CK_SPRE_16bits); 
RTC_SetWakeUpCounter(0x000F); 
//RTC_SetWakeUpCounter(0x1FFF); 
/* Enable WUTIE wake up interrupt enabled */ 
//RTC->CR |= (1UL << 14); 
/* Enable the Wakeup Interrupt */ 
RTC_ITConfig(RTC_IT_WUT, ENABLE); 
/* Connect EXTI_Line22 to the RTC Wakeup event */ 
EXTI_ClearITPendingBit(EXTI_Line22); 
EXTI_InitStructure.EXTI_Line = EXTI_Line22; 
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; 
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; 
EXTI_InitStructure.EXTI_LineCmd = ENABLE; 
EXTI_Init(&EXTI_InitStructure); 
/* Enable the RTC Wakeup Interrupt */ 
NVIC_InitStructure.NVIC_IRQChannel = RTC_WKUP_IRQn; 
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; 
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; 
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
NVIC_Init(&NVIC_InitStructure); 
}

thanks

2 REPLIES 2
Posted on January 28, 2014 at 15:25

Are you using an STM32F4-Discovery board? Have you added an LSE crystal?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
jf659
Associate II
Posted on January 28, 2014 at 15:38

Yes i'm using this board. I have added the LSE crystal, this is working fine as the flag is set to verify the crystal is stable.