cancel
Showing results for 
Search instead for 
Did you mean: 

Need RTC Code with Interrupt Enable for Every 1 sec.

Hi Team,

Need the RTC Code base with LSE or LSI as clock source and needs to get the interrupt at every 1sec rate. Is there any example code with these settings. Please share the code base or any example project, those are very much useful to us.

Regards,

Srinivas

9 REPLIES 9
KDJEM.1
ST Employee

Hello @User16669512935851271792​,

The RTC_LSI example is available in the STM32CubeWB MCU package.

This example uses the LSI clock source autocalibration to get a precise RTC clock.

Besides, the RTC WakeUp is configured to generate an interrupt each 1s.

When your question is answered, please close this topic by choosing Select as Best. This will help other users find that answer faster.

Kaouthar

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

JPeac.1
Senior

I use the RTC ALARMB interrupt for my 1sec timebase. The trick is to set the alarm for subseconds rolling over to zero. That saves the wakeup timer for other use. Accuracy as good as the RTCCLK source (LSE or LSI). Sorry, no code, I don't use HAL or Cube.

Jack Peacock

This is a nice solution. I'm doing the same for minutes - set the alarm on 0 seconds and it triggers on every change of the minutes.

Hi Kaouthar,

Thanks for your reply.

We have some queries regarding the LSI Clock source.

  1. If we use the LSI Clock, calibration is required continuously or at a certain frequency only it is required.
  2. If we use the LSE Clock also calibration is required or not.
  3. Can we give the source code for the RTC in Wakeup counter mode with LSE Clock source with all register settings, instead of LSI clock source.

Regards,

Srinivas.V

HI JPeac & Piranha,

Thanks for your reply.

Can you people share the RTCALARMB code for 1sec time base. we can reuse the same for our application.

Regards,

Srinivas.V

The example code was already given to you. You have to understand it and adapt to your needs.

I use the old SPL library, adapted for the STM32L4 series. This is how I set up a 1 sec timebase:

// common setup for RTC externals
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;	// set up wakeup timer irq
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;	// rising edge for all RTC irqs
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;	// enable wakeup timer irq
 
// configure alarm A and B interrupts
RTC_ITConfig(RTC_IT_ALRAF | RTC_IT_ALRBF, DISABLE);  // RTC alarm interrupt off
RTC_ClearFlag(RTC_FLAG_ALRAF | RTC_FLAG_ALRBF);   // clear alarm flags
EXTI_ClearITPendingBit(EXTI_Line18);				// clear alarm IRQs
EXTI_InitStructure.EXTI_Line = EXTI_Line18;		// set up IRQ trigger
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = RTC_Alarm_IRQn;	// enable alarm vector
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = ALARM_NVIC_PRIORITY;
NVIC_Init(&NVIC_InitStructure);				// alarm irq ready
// set up Alarm B as one second timebase
RTC_AlarmStruct.RTC_AlarmTime.RTC_Seconds = 0;          // seconds 0-59
RTC_AlarmStruct.RTC_AlarmTime.RTC_Minutes = 0;          // minutes 0-59
RTC_AlarmStruct.RTC_AlarmTime.RTC_Hours = 0;            // hour 0-23
RTC_AlarmStruct.RTC_AlarmDateWeekDay = 1;      // day of week not used
RTC_AlarmCmd(RTC_Alarm_B, DISABLE);              // disable alarm B before change
RTC_AlarmStruct.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_WeekDay;
RTC_AlarmStruct.RTC_AlarmMask =
RTC_AlarmMask_DateWeekDay | RTC_AlarmMask_Hours | RTC_AlarmMask_Minutes | RTC_AlarmMask_Seconds;
RTC_SetAlarm(RTC_Format_BIN, RTC_Alarm_B, &RTC_AlarmStruct);	
                                                                        // use subseconds only
temp = 0x0001;							// subsec bit match
RTC_AlarmSubSecondConfig(RTC_Alarm_B, temp, RTC_AlarmSubSecondMask_SS14_10);              // 1sec alarm
RTC_ITConfig(RTC_IT_ALRBF, ENABLE);            // RTC alarm interrupt on
RTC_AlarmCmd(RTC_Alarm_B, ENABLE);          // enable alarm B to start time base
 
 
 
 
/**
 * @brief	RTC Alarm Interrupt Service Routine
 * @param	None
 * @retval	None
 *
 * Alarm B is used to generate a periodic one second time base event.
 */
void RTCAlarm_IRQHandler(void)
{
// check for one second TOD alarm B
    if(RTC_GetITStatus(RTC_IT_ALRBF) != RESET)
    {                                                       // Alarm B detection event occurred
        RTC_ClearITPendingBit(RTC_IT_ALRBF);     // clear alarm B interrupt
    }
}
 
 

In the ISR I removed all the event flag RTOS code since it is app dependent. After clearing the RTC event in the ISR you can use whatever semaphore or task unblock is appropriate to your RTOS.

This is only an example. You will have to adapt the idea to your specific RTOS and app environment.

Jack Peacock

By the way, doing the same thing with registers and CMSIS functions requires 3 times less C code and approximately 100 bytes of FLASH space, which most likely is some 10 times less than with the "helpful" library. ;)

JPeac.1
Senior

And if it were coded in Assembler it could be even faster and smaller compared to the "helpful" compiler generated code.

The SPL was the original register abstraction for 32-bit ST controllers. Some of my STM32 code has its origin in the old STR750 (ARM7TDMI, pre M3). Any tested, working code that can be reused over decades is worth the extra flash space that would otherwise go unfilled.

Aside from library arguments, the example is intended to illustrate a procedure for programming the RTC. Since the SPL closely follows register usage it serves to describe the principles. Efficiency arguments can be left to optimizing code after it works.