Question
STM32L152 RTC ALARM B periodic interrupts not generated.
Posted on October 29, 2014 at 15:24
Good afternoon, I have deal with STM32L152ZD RTC in alarm mode( alarm B), I need to make it interrupt every rtc_period seconds. Clocking source is LSE with frequency 32768 Hz, RTC working good, but interrupts not generated. Can your help me with it? Maybe some stupid mistake has place?
Appreciate your help, Daniil./*
* Each 15 seconds our RTC would make an event and interrupt.
*
* */
#define rtc_period 15
void
sw_irtc_initialization()
{
/// [000] �?а�?тройка ча�?ов реального времени.
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/// Enable the PWR clock.
PWR_RTCAccessCmd(ENABLE);
/// Allow access to RTC.
RCC_RTCResetCmd(ENABLE);
/// Reset RTC Domain.
RCC_RTCResetCmd(DISABLE);
RCC_LSEConfig(RCC_LSE_ON);
/// Enable the LSE OSC.
while
(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET){}
/// Wait till LSE is ready.
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
/// Select the RTC Clock Source.
RTC_InitTypeDef RTC_InitStructure;
/// Configure the RTC data register and RTC prescaler.
RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
RTC_InitStructure.RTC_AsynchPrediv = 0x7F;
RTC_InitStructure.RTC_SynchPrediv = 0xFF;
RTC_Init(&RTC_InitStructure);
RCC_RTCCLKCmd(ENABLE);
/// Enable the RTC Clock.
RTC_WaitForSynchro();
/// Wait for RTC APB registers synchronisation.
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = RTC_Alarm_IRQn;
/// Enable the RTC Wakeup Interrupt.
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
RTC_TimeTypeDef RTC_TimeStruct;
RTC_AlarmTypeDef RTC_AlarmStruct;
uint32_t current_time = 0;
RTC_GetTime(RTC_Format_BIN, &RTC_TimeStruct);
/// [0] Get current time.
current_time = RTC_TimeStruct.RTC_Hours*3600 +
/// [1] Go all to the seconds.
RTC_TimeStruct.RTC_Minutes*60 +
RTC_TimeStruct.RTC_Seconds;
current_time = current_time + rtc_period;
// [2] Add rtc_period seconds.
if
( current_time>864000 )
/// [3] If current time with addition seconds more then seconds in a one day:
{
RTC_TimeStruct.RTC_H12 = RTC_HourFormat_24;
// New day begins, so count from null.
RTC_TimeStruct.RTC_Hours = 0;
RTC_TimeStruct.RTC_Minutes = 0;
RTC_TimeStruct.RTC_Seconds = current_time - 864000;
}
else
{
RTC_TimeStruct.RTC_H12 = RTC_HourFormat_24;
// Still the same day, so convert time in seconds back to hours,minutes,seconds
RTC_TimeStruct.RTC_Hours = current_time/3600;
RTC_TimeStruct.RTC_Minutes = (current_time - RTC_TimeStruct.RTC_Hours*3600)/60;
RTC_TimeStruct.RTC_Seconds = (current_time - RTC_TimeStruct.RTC_Hours*3600 - RTC_TimeStruct.RTC_Minutes*60);
}
RTC_AlarmStruct.RTC_AlarmTime = RTC_TimeStruct;
// [4] Assign recalculated time to Alarm time structure.
RTC_AlarmStruct.RTC_AlarmMask = RTC_AlarmMask_Seconds;
// [5] Turn on alarm mask, RTC will make an event each time it count to the seconds value in RTC_AlarmTime.
RTC_AlarmCmd(RTC_Alarm_B, DISABLE);
// [6] Disable alarm b peripherial, need to make available SetAlarm function.
RTC_SetAlarm(RTC_Format_BIN, RTC_Alarm_B, &RTC_AlarmStruct);
// [7] SetAlarm with predefined parameters.
RTC_ITConfig(RTC_IT_ALRB, ENABLE);
// [7] Make RTC alarm B interrupt available.
RTC_ClearFlag(RTC_FLAG_ALRBF);
// [8] Clear alarm flag
RTC_AlarmCmd(RTC_Alarm_B, ENABLE);
// [9] Make alarm available
}
void
RTC_Alarm_IRQHandler()
{
if
(RTC_GetITStatus(RTC_IT_ALRB) != RESET)
{
RTC_ClearITPendingBit(RTC_IT_ALRB);
RTC_TimeTypeDef RTC_TimeStruct;
RTC_AlarmTypeDef RTC_AlarmStruct;
uint32_t current_time = 0;
RTC_GetTime(RTC_Format_BIN, &RTC_TimeStruct);
/// [0] Get current time.
current_time = RTC_TimeStruct.RTC_Hours*3600 +
/// [1] Go all to the seconds.
RTC_TimeStruct.RTC_Minutes*60 +
RTC_TimeStruct.RTC_Seconds;
current_time = current_time + rtc_period;
// [2] Add rtc_period seconds.
if
( current_time>864000 )
/// [3] If current time with addition seconds more then seconds in a one day:
{
RTC_TimeStruct.RTC_H12 = RTC_HourFormat_24;
// New day begins, so count from null.
RTC_TimeStruct.RTC_Hours = 0;
RTC_TimeStruct.RTC_Minutes = 0;
RTC_TimeStruct.RTC_Seconds = current_time - 864000;
}
else
{
RTC_TimeStruct.RTC_H12 = RTC_HourFormat_24;
// Still the same day, so convert time in seconds back to hours,minutes,seconds
RTC_TimeStruct.RTC_Hours = current_time/3600;
RTC_TimeStruct.RTC_Minutes = (current_time - RTC_TimeStruct.RTC_Hours*3600)/60;
RTC_TimeStruct.RTC_Seconds = (current_time - RTC_TimeStruct.RTC_Hours*3600 - RTC_TimeStruct.RTC_Minutes*60);
}
RTC_AlarmStruct.RTC_AlarmTime = RTC_TimeStruct;
// [4] Assign recalculated time to Alarm time structure.
RTC_AlarmStruct.RTC_AlarmMask = RTC_AlarmMask_Seconds;
// [5] Turn on alarm mask, RTC will make an event each time it count to the seconds value in RTC_AlarmTime.
RTC_AlarmCmd(RTC_Alarm_B, DISABLE);
// [6] Disable alarm b peripherial, need to make available SetAlarm function.
RTC_SetAlarm(RTC_Format_BIN, RTC_Alarm_B, &RTC_AlarmStruct);
// [7] SetAlarm with predefined parameters.
RTC_ITConfig(RTC_IT_ALRB, ENABLE);
// [7] Make RTC alarm B interrupt available.
RTC_ClearFlag(RTC_FLAG_ALRBF);
// [8] Clear alarm flag
RTC_AlarmCmd(RTC_Alarm_B, ENABLE);
// [9] Make alarm available
}
}
#stm32l152 #rtc #rtc-alarm