2013-03-31 11:21 PM
Hi, I want to use the RTC's alarm A for waking up from standby. However, I cannot get the alarm interrupt. I am initialising timer 3 for interrupting every 5 seconds and reading the rtc within the interrupt handler. However, if the alarm is set, then the RTC time registers are not counting at all. My code is pasted below. Please help:
void RTC_Initialise()
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/* Allow access to RTC */
PWR_RTCAccessCmd(ENABLE);
/*!< LSE Enable */
RCC_LSICmd(ENABLE);
/*!< Wait till LSE is ready */
while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
{}
/*!< RTC Clock Source Selection */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
/* Enable the RTC Clock */
RCC_RTCCLKCmd(ENABLE);
/* Wait for RTC APB registers synchronisation */
if(RTC_WaitForSynchro() != SUCCESS)
{
// TODO: Handle RTC Error
}
}
Code that sets up the RTC alarm:
void RTC_Set_Alarm()
{
RTC_TimeTypeDef time;
RTC_AlarmTypeDef alarm;
EXTI_InitTypeDef exti_init;
INIT_NVIC_Disable_Peripheral_Interrupt(TIM3_IRQn,0,0);
time.RTC_Hours = 11;
time.RTC_Minutes = 0;
time.RTC_Seconds = 0;
time.RTC_H12 = RTC_H12_AM;
alarm.RTC_AlarmTime = time ;
alarm.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay|RTC_AlarmMask_Seconds ;
alarm.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
alarm.RTC_AlarmDateWeekDay = RTC_current_time.date;
/* I had to insert all this stuff here otherwise the alarm register wouldnt initialise */
RTC_WriteProtectionCmd(DISABLE);
RTC_EnterInitMode();
RTC_SetAlarm(RTC_Format_BIN, RTC_Alarm_A, &alarm);
RTC_WriteProtectionCmd(ENABLE);
RTC_ExitInitMode();
RTC_ITConfig(RTC_IT_ALRA,ENABLE);
RTC_AlarmCmd(RTC_Alarm_A,ENABLE);
EXTI_StructInit(&exti_init);
exti_init.EXTI_Line = EXTI_Line17;
exti_init.EXTI_Trigger = EXTI_Trigger_Falling;
exti_init.EXTI_LineCmd = ENABLE;
EXTI_Init(&exti_init);
INIT_NVIC_Enable_Peripheral_Interrupt(RTC_Alarm_IRQn,0,1);
INIT_NVIC_Enable_Peripheral_Interrupt(TIM3_IRQn,0,0);
}
Timer interrupt to read RTC and the function that reads RTC values:
void TIM3_IRQHandler(void)
{
TIM_ClearFlag(TIM3,TIM_FLAG_Update | TIM_FLAG_Trigger) ;
TIM_ClearITPendingBit( TIM3,TIM_IT_Update | TIM_IT_Trigger ) ;
/* The waitforsynchro function was causing problems so I commented it */
//RTC_WaitForSynchro();
RTC_Read();
}
void RTC_Read()
{
/* Structure variables to hold read date and time */
RTC_DateTypeDef RTC_DateStruct;
RTC_TimeTypeDef RTC_TimeStruct;
/* Reads the Date and Time from the RTC registers */
RTC_GetDate(RTC_Format_BIN, &RTC_DateStruct);
DELAY_ms(5);
RTC_GetTime(RTC_Format_BIN, &RTC_TimeStruct);
DELAY_ms(5);
/* RTC_current_time is a structure defined to hold both date and time together */
RTC_current_time.year = ( RTC_DateStruct.RTC_Year );
RTC_current_time.month = ( RTC_DateStruct.RTC_Month );
RTC_current_time.date = ( RTC_DateStruct.RTC_Date );
RTC_current_time.hour = ( RTC_TimeStruct.RTC_Hours );
RTC_current_time.minute = ( RTC_TimeStruct.RTC_Minutes );
RTC_current_time.second = ( RTC_TimeStruct.RTC_Seconds );
}
2013-04-01 05:03 AM
I don't have time to dig through this, but here are a couple of observations.
I don't think LSI is usable in standby. It's not in the BKP/VBAT power domain. Make sure to clear any pending alarm conditions. Look at how you are servicing the Alarm EXTI. You don't mention a part, assuming and F4 series part. How do you have VBAT and LSE wired up? When I tested the Alarm mode Standby Reset I advanced the alarm by several minutes, and had different RTC initialization depending on if it was a cold start vs a standby restart.2013-04-01 05:09 AM
clive1 thanks for the suggestions. However the controller is right now in the run mode and not in standby. So I believe using the LSI shouldnt really be the issue here. Also sorry I forgot to mention the part number. I am using an STM32L151VBT6 in a custom board. No external crystals and I am using the HSI at 8 MHz to clock my core. My RTC alarm interrupt does nothing as of now. I am only clearing the interrupt inside the ISR.
2013-04-01 05:31 AM
May be review the examples
STM32L1xx_StdPeriph_Lib_V1.1.1\Project\STM32L1xx_StdPeriph_Examples\PWR\STANDBY STM32L1xx_StdPeriph_Lib_V1.1.1\Project\STM32L1xx_StdPeriph_Examples\RTC\RTC_Timer