cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L151C8T6 RTC Alarm A interrupts

kaloyan
Associate III
Posted on May 26, 2017 at 01:19

Hello. I'm trying to use the RTC Alarm interrupt to execute code at a specific time interval.

I did everything, but the interrupt code never gets executed. However, all registers seems to be properly set.

Finally I tested the setup forcing Alarm A flag check in a while loop and it works. So obviously the RTC sets the flag but the interrupt isn't executed.

Probably I'm missing something. Any suggestions are appreciated!

The code is posted below.

void sysinit() {
//RCC_SYSCLKConfig( RCC_SYSCLKSource_MSI ); // MSI, HSI, HSE or PLL
RCC_HCLKConfig( RCC_SYSCLK_Div1 ); // AHB clock = SYSCLK 
/* For APB1 and APB2 (PCLK2 related to APB2) see Table 18
from the STM32F100 datasheet (Peripheral current consumption) */
RCC_PCLK1Config( RCC_HCLK_Div1 );
RCC_PCLK2Config( RCC_HCLK_Div1 ); //RCC_HCLK_Div1: APB2 clock = HCLK (from AHB clock)
//RCC_PLLConfig( RCC_PLLSource_HSI_Div2, RCC_PLLMul_6 );
//RCC_PLLCmd( ENABLE );
//RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK ); //use PLL as the SYSCLK clock source
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK); //MISC in the help file...
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = SysTick_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
SysTick_Config(SystemCoreClock / 1000); //ticks - number of ticks btw. 2 interrupts
}
void RTC_Config(void) {
RTC_InitTypeDef RTC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/*!< Allow access to RTC */
PWR_RTCAccessCmd(ENABLE);
/*!< Reset RTC Domain */
RCC_RTCResetCmd(ENABLE);
RCC_RTCResetCmd(DISABLE);
/*
RCC_LSICmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {}
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
*/
RCC_LSEConfig(RCC_LSE_ON);
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) {}
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
RCC_RTCCLKCmd(ENABLE);
/*!< Wait for RTC APB registers synchronisation */
RTC_WaitForSynchro();
/* Calendar Configuration */
//#ifdef NONE
RTC_InitStructure.RTC_AsynchPrediv = 0x7F;
RTC_InitStructure.RTC_SynchPrediv= 0x120; /* (37KHz / 128) - 1 = 0x120*/
RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
RTC_Init(&RTC_InitStructure);
//#endif
}
void rtcAlarmConfig() {
RTC_AlarmTypeDef RTC_AlarmStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RTC_ClearITPendingBit(RTC_IT_ALRA);
/* RTC Alarm A Interrupt Configuration */
/* EXTI configuration *********************************************************/
EXTI_ClearITPendingBit(EXTI_Line17);
EXTI_InitStructure.EXTI_Line = EXTI_Line17;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Event;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //Rising, Falling, Rising_Falling
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* Enable the RTC Alarm Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = RTC_Alarm_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Disable the Alarm A */
RTC_AlarmCmd(RTC_Alarm_A, DISABLE);
//RTC_AlarmStructure.RTC_AlarmTime.RTC_H12 = RTC_H12_AM;
RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours = 0x00; //0-23 for 24h day
RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = 0x00; //0-59
RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = 1; //0-59
/* Set the Alarm A */
RTC_AlarmStructure.RTC_AlarmDateWeekDay = 0x00;
RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_All; //0x80808000; //RTC_AlarmMask_All; //generate interrupt each second
/* Configure the RTC Alarm A register */
RTC_SetAlarm(RTC_Format_BIN, RTC_Alarm_A, &RTC_AlarmStructure);
/* Enable the RTC Alarm A Interrupt */
RTC_ITConfig(RTC_IT_ALRA, ENABLE);
/* Enable the alarm A */
if( RTC_AlarmCmd(RTC_Alarm_A, ENABLE) != SUCCESS ) {
while(1);
}
return;
}
void main() {
sysinit();
RTC_Config();
rtcAlarmConfig();
while(1) {
if( RTC_GetITStatus( RTC_IT_ALRA ) != RESET ) {
//RTC_ClearITPendingBit( RTC_IT_ALRA );
RTC_ClearFlag( RTC_FLAG_ALRAF );
redLed(1);
}
}
}
void RTC_Alarm_IRQHandler(void) {
if(RTC_GetITStatus(RTC_IT_ALRA) != RESET) {
EXTI_ClearITPendingBit(EXTI_Line17);
RTC_ClearITPendingBit(RTC_IT_ALRA);
//code to be executed.
}
}

void redLed( uint8_t flashNbr ) {
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA , ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = REDLEDPIN; //PB2 is output to 3108!!!
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init( REDLEDPORT , &GPIO_InitStructure );
uint8_t i = 0;
GPIO_ToggleBits( REDLEDPORT , REDLEDPIN );
return;
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

1 ACCEPTED SOLUTION

Accepted Solutions
kaloyan
Associate III
Posted on February 01, 2018 at 09:32

Argh, I solved the problem some time after I posted my question and because half an year has passed I forgot the details, but the key moment is as follows:

If you're looking my code in the first post, in the

RTC_Alarm_IRQHandler()
�?

There is

//code to be executed.
�?

This code was something like

uint16_t timer = SysTick + 10; //for example only
while( SysTick < timer ) { redLed(); } //time during the redLed is switched on�?�?

So in the RTC_Alarm interrupt the code relies on another interrupt (SysTick). What happens is that the Alarm interrupt has greater priority over the SysTick Interrupt and the later doesn't get executed.

I changed the code so it didn't get executed in the Alarm interrupt function and it ran fine.

Good luck!

View solution in original post

2 REPLIES 2
Posted on November 08, 2017 at 08:59

I have the same trouble with my STM32L431CC. I set the alarm A, so that the interrupt flag ALRAF on ISR register is logic high. But MCU does not enter to RTC_AlarmEventCallback.

It only works (logically) if I poll ALRAF on main function.

I have a device registering button pushes, and every button push, the MCU wake up from STANDBY. Then, in a predefined hour, I have to send the data to a server. I need MCU to wake up with ALARM A source.

Anybody can help us?

kaloyan
Associate III
Posted on February 01, 2018 at 09:32

Argh, I solved the problem some time after I posted my question and because half an year has passed I forgot the details, but the key moment is as follows:

If you're looking my code in the first post, in the

RTC_Alarm_IRQHandler()
�?

There is

//code to be executed.
�?

This code was something like

uint16_t timer = SysTick + 10; //for example only
while( SysTick < timer ) { redLed(); } //time during the redLed is switched on�?�?

So in the RTC_Alarm interrupt the code relies on another interrupt (SysTick). What happens is that the Alarm interrupt has greater priority over the SysTick Interrupt and the later doesn't get executed.

I changed the code so it didn't get executed in the Alarm interrupt function and it ran fine.

Good luck!