2022-11-09 10:41 AM
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
2022-11-09 11:45 PM
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.
2022-11-10 02:10 AM
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
2022-11-10 04:25 PM
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.
2022-11-11 08:33 AM
Hi Kaouthar,
Thanks for your reply.
We have some queries regarding the LSI Clock source.
Regards,
Srinivas.V
2022-11-14 09:55 AM
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
2022-11-14 04:18 PM
The example code was already given to you. You have to understand it and adapt to your needs.
2022-11-14 07:44 PM
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
2022-11-15 05:54 PM
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. 😉
2022-11-15 09:20 PM
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.