AnsweredAssumed Answered

STM32L151 Interrupt Problem with HSI

Question asked by evil.alex on Apr 29, 2015
Latest reply on May 19, 2015 by evil.alex
Hi, I have a problem with RTC alarm interrupt when clocking by HSI. Program just stopped and don't go further. With HSE all work fine, but when I remove external quartz, it stop. Here my code, pls help.
P.S. for clock I use LSE

#include "stm32l1xx.h"
  
#define K1  GPIO_SetBits(GPIOC,GPIO_Pin_8)
#define K2  GPIO_SetBits(GPIOC,GPIO_Pin_7)
#define K3  GPIO_SetBits(GPIOC,GPIO_Pin_6)
#define K4  GPIO_SetBits(GPIOB,GPIO_Pin_15)
#define K5  GPIO_SetBits(GPIOB,GPIO_Pin_14)
#define K6  GPIO_SetBits(GPIOB,GPIO_Pin_13)
#define K7  GPIO_SetBits(GPIOB,GPIO_Pin_12)
#define K8  GPIO_SetBits(GPIOB,GPIO_Pin_11)
  
#define STATUS_LED  GPIO_ToggleBits(GPIOC,GPIO_Pin_9);
  
  
void RTC_Config(void);
void RTC_TimeRegulate(void);
void init(void);
void sleep(void);
  
  
  
GPIO_InitTypeDef  GPIO_InitStructure;
RTC_TimeTypeDef     RTC_TimeStructure;
RTC_InitTypeDef     RTC_InitStructure;
RTC_AlarmTypeDef  RTC_AlarmStructure;
  
NVIC_InitTypeDef  NVIC_InitStructure;
EXTI_InitTypeDef  EXTI_InitStructure;
  
__IO uint32_t AsynchPrediv = 0, SynchPrediv = 0;
  
  
uint32_t hour=8,minute=0,second=00;
uint32_t alarm_hour=8,alarm_minute=0,alarm_second=01;     
  
uint8_t k_led=0;
  
  
void RTC_Config(void)
{
  
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
  PWR_RTCAccessCmd(ENABLE);
  RCC_LSEConfig(RCC_LSE_ON);
  while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
  {
  }
  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
  RCC_RTCCLKCmd(ENABLE);
  RTC_WaitForSynchro();
}
  
void sleep(void)
{
  
  RTC_AlarmCmd(RTC_Alarm_A, ENABLE);      
  PWR_UltraLowPowerCmd(ENABLE);
  PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
}
  
  
  
  
void RTC_TimeRegulate(void)
      
  
    RTC_TimeStructure.RTC_Hours = hour;
  RTC_TimeStructure.RTC_Minutes = minute;
  RTC_TimeStructure.RTC_Seconds = second;     
    RTC_SetTime(RTC_Format_BIN, &RTC_TimeStructure);
  
  RTC_WriteBackupRegister(RTC_BKP_DR0, 0x32F2);
  RTC_AlarmCmd(RTC_Alarm_A, DISABLE);
  RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours = alarm_hour;
    RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = alarm_minute;
    RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = alarm_second;
  
  RTC_AlarmStructure.RTC_AlarmDateWeekDay = 0x31;
  RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
  RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay;
  
  RTC_SetAlarm(RTC_Format_BIN, RTC_Alarm_A, &RTC_AlarmStructure);
      
}
  
  
  
  
  
void RTC_Alarm_IRQHandler (void)
{
      
    RCC_ClearITPendingBit(RCC_IT_CSS);
  
    EXTI_ClearITPendingBit(EXTI_Line17);  
  
  
    alarm_second++;   
    if (alarm_second==60)
    {
    alarm_second=0;
    alarm_minute++;
    if (alarm_minute==60)
        
        alarm_minute=0;
        alarm_hour++;
        if (alarm_hour==24) alarm_hour=0;         
        RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours = alarm_hour;      
        
        RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = alarm_minute;      
    
    RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = alarm_second;
  
    k_led++;if(k_led==9)k_led=0;  
      
  RTC_AlarmCmd(RTC_Alarm_A, DISABLE); 
    RTC_SetAlarm(RTC_Format_BIN, RTC_Alarm_A, &RTC_AlarmStructure);
      
    RTC_ClearFlag(RTC_FLAG_ALRAF);
      
    RCC_DeInit();
    RCC_ClockSecuritySystemCmd(DISABLE);
//  RCC_ClearITPendingBit(RCC_IT_CSS);
//  RCC_PLLConfig(RCC_PLLSource_HSI,RCC_PLLMul_3,RCC_PLLDiv_2);
    RCC_HSICmd(ENABLE);
    while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET)
    {}
    RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);
    RCC_HCLKConfig(RCC_SYSCLK_Div2);
//  RCC_MSICmd(DISABLE);
//  RCC_HSEConfig(RCC_HSE_OFF);
//  RTC_Config();
      
  
//  RCC_HSEConfig(RCC_HSE_ON);
  
//  while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET)
//  {}
      
    switch (k_led){
        case (0):
            {GPIO_ResetBits(GPIOB,GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
            GPIO_ResetBits(GPIOC,GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8);break;}
        case (1): {K1;break;}
        case (2): {K2;break;}
        case (3): {K3;break;}
        case (4): {K4;break;} 
        case (5): {K5;break;}
        case (6): {K6;break;}
        case (7): {K7;break;}
        case (8): {K8;break;}     
    }         
      
STATUS_LED;
          
  
      
  
}
  
  
  
  
void init(void)
{
      
    RCC_ClearITPendingBit(RCC_IT_CSS);
    RCC_DeInit();
    RCC_ClockSecuritySystemCmd(DISABLE);
  
//  RCC_PLLConfig(RCC_PLLSource_HSI,RCC_PLLMul_3,RCC_PLLDiv_2);
    RCC_HSICmd(ENABLE);
    while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET)
    {}
    RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);
    RCC_HCLKConfig(RCC_SYSCLK_Div2);      
//  RCC_MSICmd(DISABLE);
//  RCC_HSEConfig(RCC_HSE_OFF);
      
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB|RCC_AHBPeriph_GPIOC, ENABLE);
  
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
      
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
  
    GPIO_SetBits(GPIOC,GPIO_Pin_9);
  
    EXTI_DeInit();
  
    PWR_RTCAccessCmd(ENABLE);
    RTC_WaitForSynchro();
    RTC_ClearFlag(RTC_FLAG_ALRAF);
  
  
  EXTI_ClearITPendingBit(EXTI_Line17);
  EXTI_InitStructure.EXTI_Line = EXTI_Line17;
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  EXTI_Init(&EXTI_InitStructure);
    
  NVIC_InitStructure.NVIC_IRQChannel = RTC_Alarm_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure); 
  
  RTC_ITConfig(RTC_IT_ALRA, ENABLE);
  
if (RTC_ReadBackupRegister(RTC_BKP_DR0) != 0x32F2)
  {
    RTC_Config();
  
    RTC_InitStructure.RTC_AsynchPrediv = AsynchPrediv;
    RTC_InitStructure.RTC_SynchPrediv = SynchPrediv;
    RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; 
  }
  
        RTC_TimeRegulate();
      
}
  
      
  
  
  
int main(void)
{
//  uint32_t i=0;
  
    init();
      
    while(1)
    {
              
            sleep();
              
//          STATUS_LED;
//          for(i=0;i<0xFFFF;i++);
  
    }
}

Outcomes