cancel
Showing results for 
Search instead for 
Did you mean: 

RTC alarm handler does not want to work

hitsumen
Associate II
Posted on February 13, 2014 at 09:34

Good day,

Like topic said, the alarm handler does not to work, but if I do like this:

while (!RTC_GetITStatus(RTC_IT_ALRA));printf(''\nALARM''); //it works

Here is my code: 

int main(void)

{

  NVIC_InitTypeDef  NVIC_InitStructure;

  

  STM_EVAL_PBInit(BUTTON_USER , BUTTON_MODE_EXTI);

  

  STM_EVAL_LEDInit(LED3);

  STM_EVAL_LEDInit(LED4);

  STM_EVAL_LEDInit(LED5);

  STM_EVAL_LEDInit(LED6);

    

  /* Turn LED2 ON */

  STM_EVAL_LEDOn(LED3);

  

DBGU_Init();

   

  RTC_Config();

  

  RTC_TimeShow();

  RTC_AlarmShow();

  

  NVIC_InitStructure.NVIC_IRQChannel = RTC_Alarm_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

  while (!RTC_GetITStatus(RTC_IT_ALRA));printf(''\nALARM''); //works

  

while(1);

}

and handler:

void RTC_Alarm_IRQHandler(void)

{

  if(RTC_GetITStatus(RTC_IT_ALRA) != RESET)

  {

    STM_EVAL_LEDToggle(LED6);

    RTC_ClearITPendingBit(RTC_IT_ALRA);

    EXTI_ClearITPendingBit(USER_BUTTON_EXTI_LINE);

  } 

}
6 REPLIES 6
chen
Associate II
Posted on February 13, 2014 at 10:18

Hi

You have not said which STM32 Discovery board you are using.

You have not shown your code for ''

RTC_Config();

''

Have you done this :

''All RTC interrupts are connected to the EXTI controller.

To enable the RTC Alarm interrupt, the following sequence is required:

1. Configure and enable the EXTI Line 17 in interrupt mode and select the rising edge

sensitivity.

2. Configure and enable the RTC_Alarm IRQ channel in the NVIC.

3. Configure the RTC to generate RTC alarms (Alarm A or Alarm B).

To enable the RTC Wakeup interrupt, the following sequence is required:

1. Configure and enable the EXTI Line 22 in interrupt mode and select the rising edge

sensitivity.

2. Configure and enable the RTC_WKUP IRQ channel in the NVIC.

3. Configure the RTC to generate the RTC wakeup timer event.''

(Take from the reference manual)

hitsumen
Associate II
Posted on February 13, 2014 at 10:30

Why do I need EXTI line for that? I use STM32F4-DISCOVERY.

static void RTC_Config(void)

{

  RTC_DateTypeDef RTC_DateStructure;

  

  /* Enable the PWR clock */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

  /* Allow access to RTC */

  PWR_BackupAccessCmd(ENABLE);

  

  RCC_LSEConfig(RCC_LSE_ON);

  /* Wait till LSE is ready */  

  while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)

  {

  }

  /* Select the RTC Clock Source */

  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);

  /* ck_spre(1Hz) = RTCCLK(LSE) /(uwAsynchPrediv + 1)*(uwSynchPrediv + 1)*/

  uwSynchPrediv = 0xFF;

  uwAsynchPrediv = 0x7F;

  /* Enable the RTC Clock */

  RCC_RTCCLKCmd(ENABLE);

  /* Wait for RTC APB registers synchronisation */

  RTC_WaitForSynchro();

  

  /* Configure the RTC data register and RTC prescaler */

  RTC_InitStructure.RTC_AsynchPrediv = uwAsynchPrediv;

  RTC_InitStructure.RTC_SynchPrediv = uwSynchPrediv;

  RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;

  RTC_Init(&RTC_InitStructure);

  

  /* Set the alarm 05h:20min:30s */

  RTC_AlarmStructure.RTC_AlarmTime.RTC_H12     = RTC_H12_AM;

  RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours   = 0x05;

  RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = 0x20;

  RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = 0x05;

  RTC_AlarmStructure.RTC_AlarmDateWeekDay = 0x31;

  RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;

  RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay;

  

  /* Configure the RTC Alarm A register */

  RTC_SetAlarm(RTC_Format_BCD, RTC_Alarm_A, &RTC_AlarmStructure);

  

  /* Enable RTC Alarm A Interrupt */

  RTC_ITConfig(RTC_IT_ALRA, ENABLE);

  

  /* Enable the alarm */

  RTC_AlarmCmd(RTC_Alarm_A, ENABLE);

  

  RTC_ClearFlag(RTC_FLAG_ALRAF);

  

  /* Set the date: Friday January 11th 2013 */

  RTC_DateStructure.RTC_Year = 0x13;

  RTC_DateStructure.RTC_Month = RTC_Month_January;

  RTC_DateStructure.RTC_Date = 0x11;

  RTC_DateStructure.RTC_WeekDay = RTC_Weekday_Saturday;

  RTC_SetDate(RTC_Format_BCD, &RTC_DateStructure);

  

  /* Set the time to 05h 20mn 00s AM */

  RTC_TimeStructure.RTC_H12     = RTC_H12_AM;

  RTC_TimeStructure.RTC_Hours   = 0x05;

  RTC_TimeStructure.RTC_Minutes = 0x20;

  RTC_TimeStructure.RTC_Seconds = 0x00; 

  

  RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure);   

}

chen
Associate II
Posted on February 13, 2014 at 10:41

''Why do I need EXTI line for that?''

The Reference manual says so.

''

  /* Enable RTC Alarm A Interrupt */

  RTC_ITConfig(RTC_IT_ALRA, ENABLE);''

Check this does what the reference manual says.

chen
Associate II
Posted on February 13, 2014 at 10:46

Hi

Also, just spotted you are missing this :

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);

Since your project is so simple - it does not matter which group you pick.

hitsumen
Associate II
Posted on February 13, 2014 at 11:29

Actually you were right I need EXTI, and handler worked

/* EXTI configuration */

EXTI_ClearITPendingBit(EXTI_Line17);

EXTI_InitStructure.EXTI_Line = EXTI_Line17;

EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;

EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;

EXTI_InitStructure.EXTI_LineCmd = ENABLE;

EXTI_Init(&EXTI_InitStructure);

And I do not need this:

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);

About alarms, can I do it on other day?

I just saw that I could only set up alarm time..

Thank you,

//Nikolaj

chen
Associate II
Posted on February 13, 2014 at 12:08

Hi

I have not used the RTC peripheral myself (yet).

Looking at the RTC registers, it implies you can set any day and time.

By implication :

''About alarms, can I do it on other day?

I just saw that I could only set up alarm time..''

Yes - you should be able to set the alarm for another day.

It CANNOT set an alarm in another month or year.