AnsweredAssumed Answered

STM32L151C8T6 RTC Alarm A interrupts

Question asked by mihaylov.kaloyan on May 26, 2017
Latest reply on Feb 1, 2018 by mihaylov.kaloyan

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;     
}

Outcomes