AnsweredAssumed Answered

Help with RTC, HSE and Interrupt

Question asked by B3rsui on Sep 10, 2014
Latest reply on Sep 11, 2014 by B3rsui
Hello guys, I am having some problems with the following code: 

void RTC_Configuration(void)
{
    RTC_InitTypeDef RTC_InitStructure;
    RTC_TimeTypeDef RTC_TimeStructure;
    RTC_DateTypeDef RTC_DateStructure;
 
    NVIC_InitTypeDef NVIC_InitStructure;
    EXTI_InitTypeDef EXTI_InitStructure;
 
    /* Enable the PWR clock */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
 
    /* Allow access to RTC */
    PWR_BackupAccessCmd(ENABLE);
 
    // Reset the backup domain, clear RTC
    RCC_BackupResetCmd(ENABLE);
    RCC_BackupResetCmd(DISABLE);
 
    if (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET) /* HSE Already running? */
    {
        /* Enable the LSI OSC */
        RCC_HSEConfig(RCC_HSE_ON);
 
        /* Wait till LSI is ready */
        while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);
    }
 
    /* Select the RTC Clock Source */
    RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div8);
 
    /* Enable the RTC Clock */
    RCC_RTCCLKCmd(ENABLE);
 
    /* Wait for RTC APB registers synchronisation */
    RTC_WaitForSynchro();                                               /* Espero la suncronizacion de los registros APB RTC */
 
    RTC_InitStructure.RTC_AsynchPrediv = 123 - 1;                           /* Condiguracion del calendario para HSE/8 = 1 MHz */
    RTC_InitStructure.RTC_SynchPrediv  = 8146 - 1;
    RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
    RTC_Init(&RTC_InitStructure);
 
    RTC_DateStructure.RTC_Year = 14;                                    /* Seteo la fecha al 1/1/14 */
    RTC_DateStructure.RTC_Month = RTC_Month_January;
    RTC_DateStructure.RTC_Date = 0x01;
    RTC_DateStructure.RTC_WeekDay = RTC_Weekday_Wednesday;
    RTC_SetDate(RTC_Format_BCD, &RTC_DateStructure);
 
    RTC_TimeStructure.RTC_H12     = RTC_H12_AM;
    RTC_TimeStructure.RTC_Hours   = 0;
    RTC_TimeStructure.RTC_Minutes = 0;
    RTC_TimeStructure.RTC_Seconds = 0;
    RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure);
 
    /* Configuro las interrupciones */
 
    EXTI_ClearITPendingBit(EXTI_Line22);
    EXTI_InitStructure.EXTI_Line = EXTI_Line22;
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStructure);
 
    NVIC_InitStructure.NVIC_IRQChannel = RTC_WKUP_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 5;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
 
    RTC_WakeUpCmd(DISABLE);
 //   RTC_WriteProtectionCmd(DISABLE);
 
    /* Configure the RTC WakeUp Clock source: CK_SPRE (1Hz) */
    RTC_WakeUpClockConfig(RTC_WakeUpClock_CK_SPRE_16bits);
    RTC_SetWakeUpCounter(1);
 
    /* Enable the RTC Wakeup Interrupt */
    RTC_ITConfig(RTC_IT_WUT, ENABLE);
    RTC_ITConfig(RTC_IT_TS, DISABLE);
    RTC_ITConfig(RTC_IT_ALRA, DISABLE);
    RTC_ITConfig(RTC_IT_ALRB, DISABLE);
    RTC_ITConfig(RTC_IT_TAMP, DISABLE);
    RTC_ITConfig(RTC_IT_TAMP1, DISABLE);
 
    RTC_ClearFlag(RTC_FLAG_WUTF);
    RTC_ClearITPendingBit(RTC_IT_WUT);
    EXTI_ClearITPendingBit(EXTI_Line22);
 
 //   RTC_AlarmCmd(RTC_Alarm_A, DISABLE);
 //   RTC_AlarmCmd(RTC_Alarm_B, DISABLE);
 //   RTC_TimeStampCmd(RTC_TimeStampEdge_Rising, DISABLE);
 //    RTC_TimeStampCmd(RTC_TimeStampEdge_Falling, DISABLE);
 //   RTC_TamperFilterConfig(RTC_TamperFilter_Disable);
 //   RTC_TamperCmd(RTC_Tamper_1, DISABLE);
 //   RTC_TamperPullUpCmd(DISABLE);
 //   RTC_BypassShadowCmd(DISABLE);
 //   RTC_CoarseCalibCmd(DISABLE);
 //   RTC_WriteProtectionCmd(ENABLE);
    /* Enable Wakeup Counter */
    RTC_WakeUpCmd(ENABLE);
 
}
 
 
void RTC_WKUP_IRQHandler(void){
 
    if(RTC_GetITStatus(RTC_IT_WUT) != RESET){
 
        Aux++;
        RTC_WriteProtectionCmd(DISABLE);
 
        EXTI_ClearITPendingBit(EXTI_Line22);
        RTC_ClearITPendingBit(RTC_IT_WUT);
        RTC_ClearFlag(RTC_FLAG_WUTF);
 
        RTC_ClearFlag(RTC_FLAG_RECALPF);
        RTC_ClearFlag(RTC_FLAG_TAMP1F);
        RTC_ClearFlag(RTC_FLAG_TSOVF);
        RTC_ClearFlag(RTC_FLAG_TSF);
        RTC_ClearFlag(RTC_FLAG_WUTF);
        RTC_ClearFlag(RTC_FLAG_ALRBF);
        RTC_ClearFlag(RTC_FLAG_ALRAF);
        RTC_ClearFlag(RTC_FLAG_WUTF);
        RTC_ClearFlag(RTC_FLAG_INITF);
        RTC_ClearFlag(RTC_FLAG_RSF);
        RTC_ClearFlag(RTC_FLAG_SHPF);
        RTC_ClearFlag(RTC_FLAG_WUTWF);
        RTC_ClearFlag(RTC_FLAG_ALRBWF);
        RTC_ClearFlag(RTC_FLAG_ALRAWF);
 
        RTC_WriteProtectionCmd(ENABLE);
 
    }
}

The idea is to have an interrupt every second but when the first interrupt is fired then constantly enters in the interrupt, some times because of the RTC_IT_WUTF and some others because of others flags.

I have been trying a lot of things but without success.

I don't know what I am doing wrong. Can you help me?

Thanks a lot. 

Best regards,

Bruno

Outcomes