AnsweredAssumed Answered

STM32F4 discovery RTC problem

Question asked by jasonas.matas on Jul 7, 2015
Latest reply on Jul 7, 2015 by Clive One
I've been looking around the forum, but I couldn't find anyone with a similar problem. I have soldered the crystal on the board with two capacitors and removed SB15 and SB16. The thing is my program seems to be working only in debug mode (it crashes when I stop it), but I assume it is because the program, cannot track the progress because of the standby mode. I doesn't not start, when I just plug in the power  (not debug mode). I am using IAR IDE and here is my code:


#include "stm32f4xx.h"
#include "stm32f4_discovery.h"


/* Private typedef -----------------------------------------------------------*/
  RTC_InitTypeDef RTC_InitStructure;
  RTC_TimeTypeDef RTC_TimeStructure;
  RTC_DateTypeDef RTC_DateStructure;
  
/* Private define ------------------------------------------------------------*/
#define FIRST_DATA         0x32F2


/* Private function prototypes -----------------------------------------------*/
void RTC_Config(void);
void NVIC_configuration();
void EXTI_init();
void RTC_WKUP_IRQHandler();
void GPIO_init();
void delay();
void blink_blue();
void blink_red();
/* Private functions ---------------------------------------------------------*/



  // main program
int main(){
  EXTI_init();
  NVIC_configuration();
  GPIO_init();
  RTC_Config();
 
  while(1){
   blink_blue();
   PWR_EnterSTANDBYMode();
   blink_red();
  }


}



void RTC_Config(void){
    
  uint32_t uwAsynchPrediv, uwSynchPrediv;
  /* Enable PWR APB1 Clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
  
  /* Allow access to Backup */
  PWR_BackupAccessCmd(ENABLE);
  
  /* Clear Wakeup flag */
  PWR_ClearFlag(PWR_FLAG_WU);
  
  if (PWR_GetFlagStatus(PWR_FLAG_SB) != RESET) {
    
          /* Clear Standby and wakeup flag */
          PWR_ClearFlag(PWR_FLAG_SB);
          
          RTC_WaitForSynchro();
          RTC_ClearITPendingBit(RTC_IT_WUT); 
          RTC_ClearFlag(RTC_FLAG_WUTF);
  }else{
    
  RCC_BackupResetCmd(ENABLE);
  RCC_BackupResetCmd(DISABLE);
  
  // ENABLE THE LSE OSC
  RCC_LSEConfig(RCC_LSE_ON);
  /* Wait until clock ready */
  while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);
  
  /* Select LSE as RTC Clock Source */
  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
  /* ck_spre(1Hz) = RTCCLK(LSE) /(uwAsynchPrediv + 1)*(uwSynchPrediv + 1)*/
  uwSynchPrediv = 0xFF;
  uwAsynchPrediv = 0x7F;
   
   
  

 
  /* Enable the RTC Clock */
  RCC_RTCCLKCmd(ENABLE); 
  
  /* Wait for RTC APB registers synchronisation */
  RTC_WaitForSynchro();
  
  /* Write to the first RTC Backup Data Register */
  RTC_WriteBackupRegister(RTC_BKP_DR0, FIRST_DATA);
  


  
  /* Set the Time */
  RTC_TimeStructure.RTC_Hours   = 0x08;
  RTC_TimeStructure.RTC_Minutes = 0x00;
  RTC_TimeStructure.RTC_Seconds = 0x00;


  /* Set the Date */
  RTC_DateStructure.RTC_Month = RTC_Month_July;
  RTC_DateStructure.RTC_Date = 0x02;  
  RTC_DateStructure.RTC_Year = 0x15; 
  RTC_DateStructure.RTC_WeekDay = RTC_Weekday_Monday; 
  
  /* Calendar Configuration */
  RTC_InitStructure.RTC_AsynchPrediv = uwAsynchPrediv;
  RTC_InitStructure.RTC_SynchPrediv =  uwSynchPrediv;
  RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
  RTC_Init(&RTC_InitStructure);
  
  
  /* Set Current Time and Date */
  RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure);  
  RTC_SetDate(RTC_Format_BCD, &RTC_DateStructure); 
  
  
  /* INTERRUPT CONFIGURATIONS **************************************************/
  
  PWR_WakeUpPinCmd(DISABLE);
  
  EXTI_ClearITPendingBit(EXTI_Line22);
    
  RTC_WakeUpCmd(DISABLE);
  
  RTC_ITConfig(RTC_IT_WUT, DISABLE);
  
  RTC_WakeUpClockConfig(RTC_WakeUpClock_CK_SPRE_16bits);
    // Enable the wakeup counter
  
  RTC_SetWakeUpCounter(0x001);


  RTC_WakeUpCmd(ENABLE);
  
    // Enable wakeup interrupt
  RTC_ITConfig(RTC_IT_WUT, ENABLE);
  
   
  RTC_ClearITPendingBit(RTC_IT_WUT); 
  RTC_ClearFlag(RTC_FLAG_WUTF);
  
  
  
  
 if (RCC->BDCR & 0x1)
        printf("LSE On\n");
      else
        printf("LSE Off\n");
 
  if (RCC->BDCR & 0x2)
        printf("LSE Ready\n");
    else
        printf("LSE Not Ready\n");
 
  if (RCC->BDCR & 0x4)
        printf("LSE ByPass On\n");
    else
        printf("LSE ByPass Off\n");
 
  if (RCC->BDCR & 0x8000)
        printf("RTC Clock Enabled\n");
    else
        printf("RTC Clock Disabled\n");
         
  switch(RCC->BDCR & 0x300)
    {
        case 0x100 : puts("RTC Clock Source LSE"); break;
        case 0x200 : puts("RTC Clock Source LSI"); break;
        case 0x300 : printf("RTC Clock Source HSE/%d", (RCC->CFGR >> 16) & 0x1F); break;
        default : puts("RTC Clock Unknown");
    }
  }
  
}
  
/* EXTI configuration function */
void EXTI_init(){
  
  EXTI_InitTypeDef  EXTI_InitStructure;
  
  EXTI_ClearITPendingBit(EXTI_Line22);
  EXTI_InitStructure.EXTI_Line = EXTI_Line22;
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  EXTI_Init(&EXTI_InitStructure);
  
}

/* NVIC configurations */
void NVIC_configuration(){
  
  NVIC_InitTypeDef  NVIC_InitStructure;
  
  /* Enable the RTC Alarm Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = RTC_WKUP_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
  
}


void RTC_WKUP_IRQHandler(void){
  
   
    if(RTC_GetITStatus(RTC_IT_WUT) != RESET) {
      
      EXTI_ClearITPendingBit(EXTI_Line22); //OK
      RTC_ClearITPendingBit(RTC_IT_WUT);
      RTC_ClearFlag(RTC_FLAG_WUTF);


      
      
      
    }
}


void GPIO_init(){
  GPIO_InitTypeDef GPIO_InitStruct;
  
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15 | GPIO_Pin_14;
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
  
  GPIO_Init(GPIOD, &GPIO_InitStruct);
}


void delay(int time){
  
  while(time != 0){
    time--;
  }
  
}


void blink_blue(){
  GPIO_SetBits(GPIOD, GPIO_Pin_15);
  delay(1000000);
  GPIO_ResetBits(GPIOD, GPIO_Pin_15);
}


void blink_red(){
  GPIO_SetBits(GPIOD, GPIO_Pin_14);
  delay(1000000);
  GPIO_ResetBits(GPIOD, GPIO_Pin_14);
}


If you know any link that might help, you are very welcome to post them.

The program makes the blue LED flash every second. If the program works correctly the red LED never flashes. Between the flashing it goes to standby mode and is woken up by a wakeup interrupt and this loop goes forever.

Outcomes