Example #1
1
/** 
 * @brief Initialize the watchdog timer for a specified timeout
 *
 * It is important to note that this function returns the achieved timeout 
 * for this hardware.  For hardware independence this should be checked when
 * scheduling updates.  Other hardware dependent details may need to be 
 * considered such as a window time which sets a minimum update time, 
 * and this function should return a recommended delay for clearing.  
 *
 * For the STM32 nominal clock rate is 32 khz, but for the maximum clock rate of
 * 60 khz and a prescaler of 4 yields a clock rate of 15 khz.  The delay that is
 * set in the watchdog assumes the nominal clock rate, but the delay for FreeRTOS
 * to use is 75% of the minimal delay.
 *
 * @returns Maximum recommended delay between updates based on PIOS_WATCHDOG_TIMEOUT constant
 */
uint16_t PIOS_WDG_Init()
{
	uint16_t delay = ((uint32_t) PIOS_WATCHDOG_TIMEOUT * 60) / 16;
	if (delay > 0x0fff)
		delay = 0x0fff;
#if defined(PIOS_INCLUDE_WDG)
	DBGMCU_Config(DBGMCU_IWDG_STOP, ENABLE);	// make the watchdog stop counting in debug mode
	IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
	IWDG_SetPrescaler(IWDG_Prescaler_16);
	IWDG_SetReload(delay);
	IWDG_ReloadCounter();
	IWDG_Enable();

	// watchdog flags now stored in backup registers
	PWR_BackupAccessCmd(ENABLE);

	wdg_configuration.bootup_flags = RTC_ReadBackupRegister(PIOS_WDG_REGISTER);

	/*
	 * Start from an empty set of registered flags so previous boots
	 * can't influence the current one
	 */
	RTC_WriteBackupRegister(PIOS_WDG_REGISTER, 0);
#endif
	return delay;
}
Example #2
0
File: ad.c Project: hunxiyi/stm32
/**
 *  存储flash数据
 *  @param  value
 *  @return None
 */
void ad_store_flash(uint16_t *convert_value)
{
	uint8_t i;
	uint8_t buf[12];
	uint32_t flash_wr_addr;  /**< flash写地址 */
	
	/** 读取实时钟(6 byte) */
	rtc_rd_calendar(&buf[0]);
	/** 读取传感器值 */
	for(i = 0; i < 3; i++)
	{
		buf[2 * i + 6] = (*(convert_value + i)) / 256;  /**< 传感器数据高八位在前 */
		buf[2 * i + 7] = (*(convert_value + i)) % 256;
	}	
	
	/** 更新flash写指针 */
	flash_wr_addr = RTC_ReadBackupRegister(RTC_BKP_flash_wr_addr);  /**< 读出这次要写入的地址 */

    /** 当从第一扇区开始写,或者如果写一帧数据(12 bytes)超过最后一个扇区,
         则将第一扇区擦除并从第一扇区开始写 */
    if((flash_wr_addr + 12) > 0x1FFFFF)
    {
        spi_falsh_specify_sector_erase(0);
	    flash_wr_addr = 0;
    }
    /** 如果写一帧数据(12 bytes)达到下一扇区,则将下一扇区进行擦除 */
    else if(((flash_wr_addr + 12) & 0xFF0000) > (flash_wr_addr & 0xFF0000))
    {
        spi_falsh_specify_sector_erase(((flash_wr_addr & 0xFF0000) >> 16) + 1);        
    }
Example #3
0
void test(void)
{
	//Reads data from the specified RTC Backup data Register.
	//入侵检测
if (RTC_ReadBackupRegister(RTC_BKP_DR0) != 0x32F2)
  {
    /* RTC configuration  */
    RTC_Config();
    /* Configure the time&date register */
    RTC_TimeRegulate(); 
  }
else
  {
    /* Enable the PWR clock */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
    /* Allow access to RTC */
    PWR_BackupAccessCmd(ENABLE);
    /* Wait for RTC APB registers synchronisation */
    RTC_WaitForSynchro();
    /* Clear the RTC Alarm Flag */
    RTC_ClearFlag(RTC_FLAG_ALRAF);
    /* Clear the EXTI Line 17 Pending bit (Connected internally to RTC Alarm) */
    EXTI_ClearITPendingBit(EXTI_Line17);
  }
  while (1);
}
Example #4
0
File: hw_init.c Project: ADTL/AFGUI
unsigned int RTC_cfg(void)
{
	unsigned int ret;
	RTC_DateTypeDef   RTC_DateStruct;
	RTC_TimeTypeDef   RTC_TimeStruct;
	
	ret = 0;

	if (RTC_ReadBackupRegister(RTC_BKP_DR1) != 0xA5A5)
	{
	  	ret = 1;
		
		/* Allow access to BKP Domain */
		PWR_BackupAccessCmd(ENABLE);
	 
		/* Reset Backup Domain */
		RTC_DeInit();
	 
		/* Enable LSE */
		RCC_LSEConfig(RCC_LSE_ON);
		
		/* Wait till LSE is ready */
		while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
		{}
	 
		/* Select LSE as RTC Clock Source */
		RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
	 
		/* Enable RTC Clock */
		RCC_RTCCLKCmd(ENABLE);
	 
		/* Wait for RTC registers synchronization */
		RTC_WaitForSynchro();
	 
		/* Adjust time */
		RTC_DateStruct.RTC_Year = 13;
		RTC_DateStruct.RTC_Month = 05;
		RTC_DateStruct.RTC_Date = 29;
		RTC_DateStruct.RTC_WeekDay = 3;
		RTC_TimeStruct.RTC_Hours = 11;
		RTC_TimeStruct.RTC_Minutes = 0;	
		RTC_TimeStruct.RTC_Seconds = 0;
		RTC_SetTime(RTC_Format_BIN, &RTC_TimeStruct);	
		RTC_SetDate(RTC_Format_BIN, &RTC_DateStruct);
			
		RTC_WriteBackupRegister(RTC_BKP_DR1, 0xA5A5);
	}
	else
	{
	
		/* Allow access to BKP Domain */
		PWR_BackupAccessCmd(ENABLE);

		/* Wait for RTC registers synchronization */
		RTC_WaitForSynchro();
		while (RTC_GetFlagStatus(RTC_FLAG_RSF) == RESET);
		
	}
	return ret;
}
Example #5
0
/*!
 * \brief     Determines if an In-Application-Programming request has been made.
 * \param   *comm - Which communication stream to use for the IAP (USB, Telemetry, I2C, SPI, etc)
 * \return    TRUE - if correct sequence found, along with 'comm' updated.
 * 			FALSE - Note that 'comm' will have an invalid comm identifier.
 * \retval
 *
 */
uint32_t	PIOS_IAP_CheckRequest( void )
{
	uint32_t	retval = false;
	uint16_t	reg1;
	uint16_t	reg2;

	reg1 = RTC_ReadBackupRegister( MAGIC_REG_1 );
	reg2 = RTC_ReadBackupRegister( MAGIC_REG_2 );

	if( reg1 == IAP_MAGIC_WORD_1 && reg2 == IAP_MAGIC_WORD_2 ) {
		// We have a match.
		retval = true;
	} else {
		retval = false;
	}
	return retval;
}
Example #6
0
/*
*********************************************************************************************************
*	函 数 名: ReadRTC_BKP_DR
*	功能说明: 读取备份域数据
*	形    参:RTCBackupIndex 寄存器索引值
*	返 回 值: 
*********************************************************************************************************
*/
uint32_t ReadRTC_BKP_DR(uint8_t RTCBackupIndex)
{
	if (RTCBackupIndex < RTC_BKP_DR_NUMBER)
	{
		return RTC_ReadBackupRegister(aRTC_BKP_DR[RTCBackupIndex]);
	}
	
	return 0xFFFFFFFF;
}
Example #7
0
OSStatus platform_rtc_init(void)
{
#ifdef MICO_ENABLE_MCU_RTC
  RTC_InitTypeDef RTC_InitStruct;
  
  RTC_DeInit( );
  
  RTC_InitStruct.RTC_HourFormat = RTC_HourFormat_24;
  
  /* RTC ticks every second */
  RTC_InitStruct.RTC_AsynchPrediv = 0x7F;
  RTC_InitStruct.RTC_SynchPrediv = 0xFF;
  
  RTC_Init( &RTC_InitStruct );

#ifdef USE_RTC_BKP
  /* Enable the PWR clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
  /* Allow access to BKP Domain */
  PWR_BackupAccessCmd(ENABLE);
  PWR_BackupRegulatorCmd(ENABLE);
#endif

  /* Enable the LSE OSC */
  RCC_LSEConfig(RCC_LSE_ON);
  /* Wait till LSE is ready */
  while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
  {
  }
  /* Select the RTC Clock Source */
  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
  
  /* Enable the RTC Clock */
  RCC_RTCCLKCmd(ENABLE);
  
  /* RTC configuration -------------------------------------------------------*/
  /* Wait for RTC APB registers synchronisation */
  RTC_WaitForSynchro();
  
#ifdef USE_RTC_BKP
  if (RTC_ReadBackupRegister(RTC_BKP_DR0) != USE_RTC_BKP) {
    /* set it to 12:20:30 08/04/2013 monday */
    platform_rtc_set_time(&default_rtc_time);
    RTC_WriteBackupRegister(RTC_BKP_DR0, USE_RTC_BKP);
  }
#else
  //#ifdef RTC_ENABLED
  /* application must have mico_application_default_time structure declared somewhere, otherwise it wont compile */
  /* write default application time inside rtc */
  platform_rtc_set_time(&mico_default_time);
  //#endif /* RTC_ENABLED */
#endif
  return kNoErr;
#else
  return kUnsupportedErr;
#endif
}
Example #8
0
/**
  * @brief  Retreive parameters from the backup memory
  * @param  mem_base: parameters memory address in the backup sram
  * @param  cfg: configuration parameters structure 
  * @retval None
  */
void MOD_GetParam(uint16_t mem_base , uint32_t *cfg)
{
  if ( RTC_Error == 0)
  {
    *cfg = RTC_ReadBackupRegister(mem_base);
  }
  else
  {
    *cfg = 0;
  }
}
Example #9
0
/**
  * @brief  Enters MCU in STANDBY mode. The wake-up from STANDBY mode is performed 
  *         by an RTC Alarm event.
  * @param  None
  * @retval None
  */
void LowPower_EnterSTANDBYMode_RTCAlarm(void)
{
 // LCD_Clear(LCD_COLOR_WHITE);
  
  /* Set the LCD Back Color */
//  LCD_SetBackColor(LCD_COLOR_BLUE);
  
  /* Set the LCD Text Color */
 // LCD_SetTextColor(LCD_COLOR_WHITE);
  
  /* External Interrupt Disable */
  //Demo_IntExtOnOffCmd(DISABLE);
  
  /* Enable WakeUp pin */
  PWR_WakeUpPinCmd(PWR_WakeUpPin_1, ENABLE);
    
  /* Check if the StandBy flag is set */
  if (PWR_GetFlagStatus(PWR_FLAG_SB) != RESET)
  {       
    /* Clear StandBy flag */
    PWR_ClearFlag(PWR_FLAG_SB);
   
    RTC_WaitForSynchro();
  }
  
  if (RTC_ReadBackupRegister(RTC_BKP_DR0) != 0x5AA5)
  {
  //  LCD_DisplayStringLine(LCD_LINE_1, "Time and Date are   ");
   // LCD_DisplayStringLine(LCD_LINE_2, "not configured,     ");
  //  LCD_DisplayStringLine(LCD_LINE_3, "please go to the    ");
  //  LCD_DisplayStringLine(LCD_LINE_4, "calendar menu and   ");
  //  LCD_DisplayStringLine(LCD_LINE_5, "set the time and    ");
  //  LCD_DisplayStringLine(LCD_LINE_6, "date parameters.    ");
  //  LCD_DisplayStringLine(LCD_LINE_7, "Press JoyStick to   ");
  //  LCD_DisplayStringLine(LCD_LINE_8, "continue...         ");
    
    
    /* External Interrupt Enable */
    //Demo_IntExtOnOffCmd(ENABLE);
    return;
  }
  
  Calendar_AlarmPreAdjust_A();

 // LCD_DisplayStringLine(LCD_LINE_7, " MCU in STANDBY Mode");
 // LCD_DisplayStringLine(LCD_LINE_8, " Wait For RTC Alarm ");
  
  /* Request to enter STANDBY mode (Wake Up flag is cleared in PWR_EnterSTANDBYMode function) */
  PWR_EnterSTANDBYMode();
}
Example #10
0
/**
  * @brief  Checks if the RTC Backup DRx registers values are correct or not.
  * @param  FirstRTCBackupData: data to be compared with RTC Backup data registers.
  * @retval - 0: All RTC Backup DRx registers values are correct
  *         - Value different from 0: Number of the first Backup register
  *           which value is not correct
  */
static uint32_t CheckRTC_BKP_DR(uint32_t FirstRTCBackupData)
{
  uint32_t index = 0;

  for (index = 0; index < RTC_BKP_DR_NUMBER; index++)
  {
    /* Read from data register */
    if (RTC_ReadBackupRegister(aRTC_BKP_DR[index]) != (FirstRTCBackupData + (index * 0x5A)))
    {
      return (index + 1);
    }
  }
    return 0;
}
Example #11
0
/**
  * @brief  Checks if the Backup data registers values are correct or not.
  * @param  FirstBackupData: data to read from first backup data register
  * @retval - 0: All Backup DRx registers data are correct
  *         - Value different from 0: Number of the first Backup register which
  *           value is not correct
  */
uint32_t CheckBackupReg(uint16_t FirstBackupData)
{
  uint32_t index = 0;

  for (index = 0; index < RTC_BKP_DR_NUMBER; index++)
  {
    if (RTC_ReadBackupRegister(BKPDataReg[index]) != (FirstBackupData + (index * 0x5A)))
    {
      return (index + 1);
    }
  }

  return 0;
}
Example #12
0
/**
  * @brief  Checks if the RTC Backup DRx registers are reset or not.
  * @param  None
  * @retval - 0: All RTC Backup DRx registers are reset
  *         - Value different from 0: Number of the first Backup register
  *           not reset
  */
uint32_t IsBackupRegReset(void)
{
  uint32_t index = 0;

  for (index = 0; index < RTC_BKP_DR_NUMBER; index++)
  {
     /* Read from bkp Data Register */
    if (RTC_ReadBackupRegister(aRTC_BKP_DR[index]) != 0)
    {
      return (index + 1);
    }
  }
  return 0;
}
Example #13
0
void rtc_init(void) {
  rtc_ok = RTC_ReadBackupRegister(RTC_BKP_DR0) == 0xCA7E;
  if(rtc_ok) {
    
    // Enable the PWR clock
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

    // Allow access to RTC
    PWR_BackupAccessCmd(ENABLE);

    // Wait for RTC APB registers synchronisation
    RTC_WaitForSynchro();
  }
}
Example #14
0
/**
 * @brief Function called by modules to indicate they are still running
 *
 * This function will set this flag in the active flags register (which is
 * a backup regsiter) and if all the registered flags are set will clear
 * the watchdog and set only this flag in the backup register
 *
 * @param[in] flag the flag to set
 * @return true if the watchdog cleared, false if flags are pending
 */
bool PIOS_WDG_UpdateFlag(uint16_t flag)
{
    // we can probably avoid using a semaphore here which will be good for
    // efficiency and not blocking critical tasks.  race condition could
    // overwrite their flag update, but unlikely to block _all_ of them
    // for the timeout window
    uint16_t cur_flags = RTC_ReadBackupRegister(PIOS_WDG_REGISTER);

    if ((cur_flags | flag) == wdg_configuration.used_flags) {
        PIOS_WDG_Clear();
        RTC_WriteBackupRegister(PIOS_WDG_REGISTER, flag);
        return true;
    } else {
        RTC_WriteBackupRegister(PIOS_WDG_REGISTER, cur_flags | flag);
        return false;
    }
}
void platform_rtc_init(void)
{
  RTC_InitTypeDef RTC_InitStruct;
  
  RTC_InitStruct.RTC_HourFormat = RTC_HourFormat_24;
  
  /* RTC ticks every second */
  RTC_InitStruct.RTC_AsynchPrediv = 0x7F;
  RTC_InitStruct.RTC_SynchPrediv = 0xFF;
  
  /* Enable the PWR clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
  
  /* RTC clock source configuration ------------------------------------------*/
  /* Allow access to BKP Domain */
  PWR_BackupAccessCmd(ENABLE);
  
  RTC_Init( &RTC_InitStruct );
  /* Enable the LSE OSC */
  RCC_LSEConfig(RCC_LSE_ON);
  /* Wait till LSE is ready */
  while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
  {
  }
  /* Select the RTC Clock Source */
  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
  
  /* Enable the RTC Clock */
  RCC_RTCCLKCmd(ENABLE);
  
  /* RTC configuration -------------------------------------------------------*/
  /* Wait for RTC APB registers synchronisation */
  RTC_WaitForSynchro();
  
#ifdef USE_RTC_BKP
  if (RTC_ReadBackupRegister(RTC_BKP_DR0) != USE_RTC_BKP) {
    /* set it to 12:20:30 08/04/2013 monday */
    MicoRtcSetTime(&mico_default_time);
    RTC_WriteBackupRegister(RTC_BKP_DR0, USE_RTC_BKP);
  }
#else
  /* write default application time inside rtc */
  MicoRtcSetTime(&mico_default_time);
#endif
  
}
/**
 * Configure the RTC peripheral by selecting the clock source.
 * ck_spre(1Hz) = RTCCLK(LSE) /(asynchPrediv + 1)*(synchPrediv + 1)
 *
 * @param none
 * @retval none
 */
void RTC_Config(uint32_t synch_prediv, uint32_t asynch_prediv) {
#ifdef DEBUG
	printf("Configuring real time clock.\n\r");
#endif
	/* Allocate the init structure */
	RTC_InitTypeDef RTC_InitStructure;

	/* Enable the PWR clock */
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

	/* Allow access to RTC */
	PWR_BackupAccessCmd(ENABLE);

	/* Enable the LSE OSC */
	RCC_LSEConfig(RCC_LSE_ON );

	/* Wait till LSE is ready */
	while (RCC_GetFlagStatus(RCC_FLAG_LSERDY ) == RESET) {
		/* Do nothing */
	}

	/* Select the RTC Clock Source */
	RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE );

	/* Configure the RTC data register and RTC prescaler */
	RTC_InitStructure.RTC_AsynchPrediv = synch_prediv;
	RTC_InitStructure.RTC_SynchPrediv = asynch_prediv;
	RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;

	/* Check on RTC init */
	if (RTC_Init(&RTC_InitStructure) == ERROR) {
#ifdef DEBUG
		printf("[Tekdaqc RTC] RTC Prescaler Config failed.\n\r");
#endif
	}

	/* Enable the RTC Clock */
	RCC_RTCCLKCmd(ENABLE);

	/* Wait for RTC APB registers synchronization */
	RTC_WaitForSynchro();

	uint32_t reg = RTC_ReadBackupRegister(RTC_CONFIGURED_REG );
	reg |= RTC_CONFIGURED;
	RTC_WriteBackupRegister(RTC_CONFIGURED_REG, reg);
}
Example #17
0
void RTC_Config(void)
{
  RTC_InitTypeDef RTC_InitStructure;
  RTC_TimeTypeDef RTC_TimeStructure;
  RTC_DateTypeDef RTC_DateStructure;
  
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE);
  PWR_BackupAccessCmd(ENABLE);//使能备份寄存器操作
  
  RCC_LSICmd(ENABLE);
    while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET);
    RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
    RCC_RTCCLKCmd(ENABLE);
    RTC_WaitForSynchro();
  
  if(RTC_ReadBackupRegister(RTC_BKP_DR0) != 0x9741)   //一个变量,看看RTC初始化没
  {
    
    RTC_WriteProtectionCmd(DISABLE);
  
    RTC_EnterInitMode();
    RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
    RTC_InitStructure.RTC_AsynchPrediv = 0x7D-1;
    RTC_InitStructure.RTC_SynchPrediv = 0xFF-1;
    RTC_Init(&RTC_InitStructure);
  
    RTC_TimeStructure.RTC_Seconds = 0x00;
    RTC_TimeStructure.RTC_Minutes = 0x01;
    RTC_TimeStructure.RTC_Hours = 0x01;
    RTC_TimeStructure.RTC_H12 = RTC_H12_AM;
    RTC_SetTime(RTC_Format_BCD,&RTC_TimeStructure);
  
    RTC_DateStructure.RTC_Date = 30;
    RTC_DateStructure.RTC_Month = 5;
    RTC_DateStructure.RTC_WeekDay= RTC_Weekday_Thursday;
    RTC_DateStructure.RTC_Year = 12;
    RTC_SetDate(RTC_Format_BCD,&RTC_DateStructure);
  
    RTC_ExitInitMode();
    RTC_WriteBackupRegister(RTC_BKP_DR0,0X9741);
    RTC_WriteProtectionCmd(ENABLE);
    RTC_WriteBackupRegister(RTC_BKP_DR0,0x9741);  //初始化完成,设置标志
  }
  PWR_BackupAccessCmd(DISABLE);
}
Example #18
0
void RTC_Time_Init(void)
{
  if (RTC_ReadBackupRegister(RTC_BKP_DR0) != 0x32F2)
  {
    /* RTC configuration  */
    RTC_Config();

    /* Configure the RTC data register and RTC prescaler */
    RTC_InitStructure.RTC_AsynchPrediv = AsynchPrediv;
    RTC_InitStructure.RTC_SynchPrediv = SynchPrediv;
    RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
   
    /* Check on RTC init */
    if (RTC_Init(&RTC_InitStructure) == ERROR)
    {
      MessageBox_Show("Prescaler Config failed", "RTC Error", true, false);
    }
    /* Configure the time register */
    RTC_TimeRegulate(0,0,0); 
  }
  else
  {
    /* Check if the Power On Reset flag is set */
    if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
    {
    }
    /* Check if the Pin Reset flag is set */
    else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
    {
    }
    
    /* Enable the PWR clock */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

    /* Allow access to RTC */
    PWR_BackupAccessCmd(ENABLE);

    /* Wait for RTC APB registers synchronisation */
    RTC_WaitForSynchro();

    /* Clear the RTC Alarm Flag */
    RTC_ClearFlag(RTC_FLAG_ALRAF);
  }	
}
Example #19
0
/**
  * @brief  Enters MCU in STOP mode. The wake-up from STOP mode is performed by 
  *         an RTC Alarm.
  * @param  None
  * @retval None
  */
void LowPower_EnterSTOPMode_RTCAlarm(void)
{
  /* Clear the LCD */
  
  
  /* Set the LCD Back Color */
 // LCD_SetBackColor(LCD_COLOR_BLUE);
  
  /* Set the LCD Text Color */
  
  /* External Interrupt Disable */
  //Demo_IntExtOnOffCmd(DISABLE);
  
  if (RTC_ReadBackupRegister(RTC_BKP_DR0) != 0x5AA5)
  {
    
    
    
    /* External Interrupt Enable */
    //Demo_IntExtOnOffCmd(ENABLE);
    return;
  }

  Calendar_AlarmPreAdjust_A();
  
 
  /* Save the GPIO pins current configuration then put all GPIO pins in Analog Input mode */
  LowPower_SaveGPIOConfig();

  /* Request to enter STOP mode with regulator in low power */
  PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
  /* Restore the GPIO Configurations*/
  LowPower_RestoreGPIOConfig();

  /* Configures system clock after wake-up from STOP: enable HSE, PLL and select PLL
     as system clock source (HSE and PLL are disabled in STOP mode) */
  LowPower_SYSCLKConfig_STOP();
  
  /* External Interrupt Enable */
  //Demo_IntExtOnOffCmd(ENABLE);
}
Example #20
0
/**
 * @brief Initialize the watchdog timer for a specified timeout
 *
 * It is important to note that this function returns the achieved timeout
 * for this hardware.  For hardware independence this should be checked when
 * scheduling updates.  Other hardware dependent details may need to be
 * considered such as a window time which sets a minimum update time,
 * and this function should return a recommended delay for clearing.
 *
 * For the STM32 nominal clock rate is 32 khz, but for the maximum clock rate of
 * 60 khz and a prescaler of 4 yields a clock rate of 15 khz.  The delay that is
 * set in the watchdog assumes the nominal clock rate, but the delay for FreeRTOS
 * to use is 75% of the minimal delay.
 *
 * @returns Maximum recommended delay between updates based on PIOS_WATCHDOG_TIMEOUT constant
 */
uint16_t PIOS_WDG_Init()
{
    uint16_t delay = ((uint32_t)PIOS_WATCHDOG_TIMEOUT * 60) / 16;

    if (delay > 0x0fff) {
        delay = 0x0fff;
    }
#if defined(PIOS_INCLUDE_WDG)
    DBGMCU_APB1PeriphConfig(DBGMCU_IWDG_STOP, ENABLE); // OP-1272 : write in APB1 register
    IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
    IWDG_SetPrescaler(IWDG_Prescaler_16);
    IWDG_SetReload(delay);
    IWDG_ReloadCounter();
    IWDG_Enable();

    // watchdog flags now stored in backup registers
    PWR_BackupAccessCmd(ENABLE);

    wdg_configuration.bootup_flags = RTC_ReadBackupRegister(PIOS_WDG_REGISTER);
#endif
    return delay;
}
Example #21
0
//获取时间
char* get_time(void)
{  
	char time[100]={'\0'};
	RTC_TimeTypeDef   	RTC_GetTimeStructure;
	RTC_DateTypeDef 	RTC_GetDateStructure;
	if (RTC_ReadBackupRegister(RTC_BKP_DR0) != 0x32F1)//假如修改了,则再次进行配置
		{
			RTC_Config(extrnal);
			set_time(0x15,0x12,0x28,0x14,0x50,0x01,0x01,RTC_H12_PM);
			//return "fail"
		}
	else
		{//没有修改,则直接继续
			RTC_GetTime(RTC_Format_BIN, &RTC_GetTimeStructure);
			RTC_GetDate(RTC_Format_BIN, &RTC_GetDateStructure);
			//2015-11-10-10:18:33
			sprintf(time,"20%d-%d-%d-%d:%d:%d",\
			RTC_GetDateStructure.RTC_Year,RTC_GetDateStructure.RTC_Month,\
			RTC_GetDateStructure.RTC_Date,RTC_GetTimeStructure.RTC_Hours,\
			RTC_GetTimeStructure.RTC_Minutes,RTC_GetTimeStructure.RTC_Seconds);
		}
	return time;
}
Example #22
0
uint16_t PIOS_IAP_ReadBootCount(void)
{
	return RTC_ReadBackupRegister ( IAP_BOOTCOUNT );
}
Example #23
0
void read_bkpsram(void)
{
	rt_kprintf("test_bkpsram = %d\n",test_bkpsram);
	rt_kprintf("RTC_BKP_DR0 = %d\n",RTC_ReadBackupRegister( RTC_BKP_DR0));
}
Example #24
0
//--------------------------------------------------------------
// Init und Start der RTC
// Return Wert :
//  -> RTC_UNDEFINED = RTC war noch nicht initialisiert
//  -> RTC_INIT_OK   = RTC war schon initialisiert
//                     (aber noch nicht eingestellt)
//  -> RTC_TIME_OK   = RTC war schon eingestellt
//--------------------------------------------------------------
RTC_STATUS_t UB_RTC_Init(void)
{
  RTC_STATUS_t ret_wert=RTC_UNDEFINED;
  uint32_t status;

  // Clock enable fuer PWR
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

  // Zugriff auf RTC freigeben
  PWR_BackupAccessCmd(ENABLE);

  // Test vom Status-Register
  status=RTC_ReadBackupRegister(RTC_STATUS_REG);
  if(status==RTC_STATUS_TIME_OK) {
    // wenn Uhrzeit schon eingestellt ist
    ret_wert=RTC_TIME_OK;
    UB_RTC.status=RTC_TIME_OK;

    // warte bis synconisiert
    RTC_WaitForSynchro();
    #if RTC_USE_WAKEUP_ISR==1
      RTC_ClearITPendingBit(RTC_IT_WUT);
      EXTI_ClearITPendingBit(EXTI_Line22);
    #endif

    // RTC auslesen
    UB_RTC_GetClock(RTC_DEC);
  }
  else if(status==RTC_STATUS_INIT_OK) {
    // wenn RTC schon initialisiert ist
    ret_wert=RTC_INIT_OK;
    UB_RTC.status=RTC_INIT_OK;

    // warte bis synconisiert
    RTC_WaitForSynchro();
    #if RTC_USE_WAKEUP_ISR==1
      RTC_ClearITPendingBit(RTC_IT_WUT);
      EXTI_ClearITPendingBit(EXTI_Line22);
    #endif

    // RTC auslesen
    UB_RTC_GetClock(RTC_DEC);
  }
  else {
    // wenn RTC noch nicht initialisiert ist
    ret_wert=RTC_UNDEFINED;
    UB_RTC.status=RTC_UNDEFINED;

    // RTC Konfig und start
    P_RTC_Config();

    // RTC zuruecksetzen auf (0:00:00 / 1.1.00)
    UB_RTC.std=0;
    UB_RTC.min=0;
    UB_RTC.sek=0;
    UB_RTC.tag=1;
    UB_RTC.monat=1;
    UB_RTC.jahr=0;
    UB_RTC.wotag=1;
    UB_RTC_SetClock(RTC_DEC);

    UB_RTC.status=RTC_INIT_OK;
  }
  
  return(ret_wert);
}
Example #25
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f2xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f2xx.c file
     */
  NVIC_InitTypeDef NVIC_InitStructure;
  EXTI_InitTypeDef  EXTI_InitStructure;

  /* Configure the external interrupt "WAKEUP", "KEY" and "TAMPER" buttons */
  STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_GPIO);
  STM_EVAL_PBInit(BUTTON_TAMPER , BUTTON_MODE_GPIO);
  STM_EVAL_PBInit(BUTTON_WAKEUP , BUTTON_MODE_GPIO);

  /* Initialize the LCD */
  STM322xG_LCD_Init();

  /* Configure the LCD Log Module */
  LCD_LOG_Init();
  LCD_LOG_SetHeader("RTC Backup Domain Example");
  LCD_LOG_SetFooter ("   Copyright (c) STMicroelectronics" );

  /* Display the default RCC BDCR and RTC TAFCR Registers */
  LCD_UsrLog ("Entry Point \n");
  LCD_UsrLog ("RCC BDCR = 0x%x\n", RCC->BDCR);
  LCD_UsrLog ("RTC TAFCR = 0x%x\n", RTC->TAFCR);

  /* Enable the PWR APB1 Clock Interface */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

  /* Allow access to BKP Domain */
  PWR_BackupAccessCmd(ENABLE);

  /* Configure one bit for preemption priority */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

  /* Enable the RTC Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = RTC_WKUP_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  /* EXTI configuration *******************************************************/
  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);

  if(RTC_ReadBackupRegister(RTC_BKP_DR0) != FIRST_DATA)
  {
    LCD_UsrLog ("RTC Config PLZ Wait. \n");

    /* RTC Configuration */
    RTC_Config();

    /* Adjust Current Time */
    Time_Adjust();

    /* Adjust Current Date */
    Date_Adjust();
  }
  else
  {
    /* Wait for RTC APB registers synchronisation */
    RTC_WaitForSynchro();
    RTC_ClearITPendingBit(RTC_IT_WUT);
    EXTI_ClearITPendingBit(EXTI_Line22);

/*  Backup SRAM ***************************************************************/
    /* Enable BKPSRAM Clock */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_BKPSRAM, ENABLE);

    /* Check the written Data */
    for (i = 0x0; i < 0x1000; i += 4)
    {
      if ((*(__IO uint32_t *) (BKPSRAM_BASE + i)) != i)
      {
        errorindex++;
      }
    }
    if(errorindex)
    {
      LCD_ErrLog ("BKP SRAM Number of errors = %d\n", errorindex);
    }
    else
    {
      LCD_UsrLog ("BKP SRAM Content OK  \n");
    }
/* RTC Backup Data Registers **************************************************/
    /* Check if RTC Backup DRx registers data are correct */
    if (CheckBackupReg(FIRST_DATA) == 0x00)
    {
      /* OK, RTC Backup DRx registers data are correct */
      LCD_UsrLog ("OK, RTC Backup DRx registers data are correct. \n");
    }
    else
    {
      /* Error, RTC Backup DRx registers data are not correct */
      LCD_ErrLog ("RTC Backup DRx registers data are not correct\n");
    }
  }

  /* Infinite loop */
  Calendar_Show();

  while (1)
  {
  }
}
void RTC_Wakeup_init(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
  EXTI_InitTypeDef EXTI_InitStructure;
  RTC_InitTypeDef RTC_InitStruct;
  
  RTC_InitStruct.RTC_HourFormat = RTC_HourFormat_24;
  
  /* RTC ticks every second */
  RTC_InitStruct.RTC_AsynchPrediv = 0x7F;
  RTC_InitStruct.RTC_SynchPrediv = 0xFF;
  
  RTC_Init( &RTC_InitStruct );
  
  /* Enable the PWR clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
  
  /* RTC clock source configuration ------------------------------------------*/
  /* Allow access to BKP Domain */
  PWR_BackupAccessCmd(ENABLE);
#ifdef USE_RTC_BKP
  PWR_BackupRegulatorCmd(ENABLE);
#endif
  
  /* Enable the LSE OSC */
  RCC_LSEConfig(RCC_LSE_ON);
  /* Wait till LSE is ready */
  while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
  {
  }
  
  /* Select the RTC Clock Source */
  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
  
  /* Enable the RTC Clock */
  RCC_RTCCLKCmd(ENABLE);
  
  /* RTC configuration -------------------------------------------------------*/
  /* Wait for RTC APB registers synchronisation */
  RTC_WaitForSynchro();
  
  RTC_WakeUpCmd( DISABLE );
  EXTI_ClearITPendingBit( RTC_INTERRUPT_EXTI_LINE );
  PWR_ClearFlag(PWR_FLAG_WU);
  RTC_ClearFlag(RTC_FLAG_WUTF);
  
  RTC_WakeUpClockConfig(RTC_WakeUpClock_RTCCLK_Div2);
  
  EXTI_ClearITPendingBit( RTC_INTERRUPT_EXTI_LINE );
  EXTI_InitStructure.EXTI_Line = RTC_INTERRUPT_EXTI_LINE;
  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_WKUP_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
  RTC_ITConfig(RTC_IT_WUT, DISABLE);
  
  /* Prepare Stop-Mode but leave disabled */
  PWR_ClearFlag(PWR_FLAG_WU);
  PWR->CR |= PWR_CR_LPDS;
  PWR->CR &= (unsigned long)(~(PWR_CR_PDDS));
  SCB->SCR |= ((unsigned long)SCB_SCR_SLEEPDEEP_Msk);
  
#ifdef USE_RTC_BKP
  if (RTC_ReadBackupRegister(RTC_BKP_DR0) != USE_RTC_BKP) {
    /* set it to 12:20:30 08/04/2013 monday */
    MicoRtcSetTime(&mico_default_time);
    RTC_WriteBackupRegister(RTC_BKP_DR0, USE_RTC_BKP);
  }
#else
  //#ifdef RTC_ENABLED
  /* application must have wiced_application_default_time structure declared somewhere, otherwise it wont compile */
  /* write default application time inside rtc */
  MicoRtcSetTime(&mico_default_time);
  //#endif /* RTC_ENABLED */
#endif
  
}
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
    /*!< At this stage the microcontroller clock setting is already configured,
         this is done through SystemInit() function which is called from startup
         file (startup_stm32f0xx.s) before to branch to application main.
         To reconfigure the default setting of SystemInit() function, refer to
         system_stm32f0xx.c file
       */

    /* USARTx configured as follow:
            - BaudRate = 115200 baud
            - Word Length = 8 Bits
            - One Stop Bit
            - No parity
            - Hardware flow control disabled (RTS and CTS signals)
            - Receive and transmit enabled
      */
    USART_InitStructure.USART_BaudRate = 115200;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    STM_EVAL_COMInit(COM1, &USART_InitStructure);

    /* GPIOC Periph clock enable */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);

    /* Output a message on Hyperterminal using printf function */
    printf("\n\r  *********************** RTC Time Stamp Example ***********************\n\r");

    if (RTC_ReadBackupRegister(RTC_BKP_DR0) != 0x32F2)
    {
        /* RTC configuration  */
        RTC_Config();

        /* Configure the RTC data register and RTC prescaler */
        RTC_InitStructure.RTC_AsynchPrediv = AsynchPrediv;
        RTC_InitStructure.RTC_SynchPrediv = SynchPrediv;
        RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;

        /* Check on RTC init */
        if (RTC_Init(&RTC_InitStructure) == ERROR)
        {
            printf("\n\r        /!\\***** RTC Prescaler Config failed ********/!\\ \n\r");
        }

        /* Configure the time register */
        RTC_TimeRegulate();
    }
    else
    {
        /* Check if the Power On Reset flag is set */
        if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
        {
            printf("\r\n Power On Reset occurred....\n\r");
        }
        /* Check if the Pin Reset flag is set */
        else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
        {
            printf("\r\n External Reset occurred....\n\r");
        }

        printf("\r\n No need to configure RTC....\n\r");

        /* Enable the PWR clock */
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

        /* Allow access to RTC */
        PWR_BackupAccessCmd(ENABLE);

        /* Wait for RTC APB registers synchronisation */
        RTC_WaitForSynchro();

        /* Clear the RTC Alarm Flag */
        RTC_ClearFlag(RTC_FLAG_ALRAF);

        /* Clear the EXTI Line 17 Pending bit (Connected internally to RTC Alarm) */
        EXTI_ClearITPendingBit(EXTI_Line17);

        /* Display the RTC Time/Date and TimeStamp Time/Date */
        RTC_TimeShow();
        RTC_DateShow();
        RTC_TimeStampShow();
    }

    /* Configure the external interrupt  "TAMPER" and "Joystick SEL" buttons */
    STM_EVAL_PBInit(BUTTON_TAMPER, BUTTON_MODE_EXTI);
    STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_EXTI);
    STM_EVAL_PBInit(BUTTON_SEL, BUTTON_MODE_EXTI);

    /* Configure LED1 */
    STM_EVAL_LEDInit(LED1);
    STM_EVAL_LEDOn(LED1);

    /* Infinite loop */
    while (1)
    {
    }
}
Example #28
0
u8 RT_Total_Config(void) 
{
   u8  Resualt=0;
   //表明RTC数据丢失,需要重新配置
    if (RTC_ReadBackupRegister(RTC_BKP_DR0) != RTC_First)
   {
	       rt_kprintf("rtc is not configured\n");
              //重新配置RTC
              RTC_Config();
	       // Check  resualt		  
		if ( RTC_Config() != 0)
			{
			   RTC_Config();
					rt_kprintf("rtc configure fail...Reconfig once\r\n");  
					return  0 ;
			}
		else
		{
		     /* Configure the RTC data register and RTC prescaler */
			RTC_InitStructure.RTC_AsynchPrediv = AsynchPrediv;
			RTC_InitStructure.RTC_SynchPrediv = SynchPrediv;
			RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;

			/* Check on RTC init */
			if (RTC_Init(&RTC_InitStructure) == ERROR)
			{
				rt_kprintf("\n\r  /!\\***** RTC Prescaler Config failed ********/!\\ \n\r");
			}			
		 }
		//配置完成后,向后备寄存器中写特殊字符0xA5A5 
	        RTC_WriteBackupRegister(RTC_BKP_DR0, RTC_First);	 		

		 Resualt=1;
   }
    else
 { 
       
    /* Check if the Power On Reset flag is set */
    if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != 0)
    {
       ;// rt_kprintf("\r\n Power On Reset occurred....\n\r");
    }
    /* Check if the Pin Reset flag is set */
    else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != 0)
    {
       ;//rt_kprintf("\r\n External Reset occurred....\n\r");
    }

   // rt_kprintf("\r\n No need to configure RTC....\n\r");
    
    /* Enable the PWR clock */
     RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

    /* Allow access to RTC */
     PWR_BackupAccessCmd(ENABLE);  

    /* Wait for RTC APB registers synchronisation */
     RTC_WaitForSynchro();

     /* Clear the RTC Alarm Flag */
     RTC_ClearFlag(RTC_FLAG_ALRAF);
      
	/* Wait for RTC registers synchronization */
       RTC_WaitForSynchro();
	Resualt=0;				
 } 
   return Resualt; 
}
Example #29
0
void rtc_init(void) {
    // Enable the PWR clock
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

    // Allow access to RTC
    PWR_BackupAccessCmd(ENABLE);

    if (RTC_ReadBackupRegister(RTC_BKP_DR0) == 0x32F2) {
        // RTC still alive, so don't re-init it
        // wait for RTC APB register synchronisation
        RTC_WaitForSynchro();
        rtc_info = RTC_INFO_USE_EXISTING;
        return;
    }

    uint32_t timeout = 10000000;

    // Enable the PWR clock
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

    // Allow access to RTC
    PWR_BackupAccessCmd(ENABLE);

    // Enable the LSE OSC
    RCC_LSEConfig(RCC_LSE_ON);

    // Wait till LSE is ready
    machine_uint_t sys_tick = sys_tick_counter;
    while((RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) && (--timeout > 0)) {
    }

    // record how long it took for the RTC to start up
    rtc_info = (sys_tick_counter - sys_tick) << 2;

    // If LSE timed out, use LSI instead
    if (timeout == 0) {
        // Disable the LSE OSC
        RCC_LSEConfig(RCC_LSE_OFF);

        // Enable the LSI OSC
        RCC_LSICmd(ENABLE);

        // Wait till LSI is ready
        while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {
        }

        // Use LSI as the RTC Clock Source
        RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);

        // record that we are using the LSI
        rtc_info |= RTC_INFO_USE_LSI;
    } else {
        // Use LSE as the RTC Clock Source
        RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);

        // record that we are using the LSE
        rtc_info |= RTC_INFO_USE_LSE;
    }

    // Note: LSI is around (32KHz), these dividers should work either way
    // ck_spre(1Hz) = RTCCLK(LSE) /(uwAsynchPrediv + 1)*(uwSynchPrediv + 1)
    uint32_t uwSynchPrediv = 0xFF;
    uint32_t uwAsynchPrediv = 0x7F;

    // Enable the RTC Clock
    RCC_RTCCLKCmd(ENABLE);

    // Wait for RTC APB registers synchronisation
    RTC_WaitForSynchro();

    // Configure the RTC data register and RTC prescaler
    RTC_InitTypeDef RTC_InitStructure;
    RTC_InitStructure.RTC_AsynchPrediv = uwAsynchPrediv;
    RTC_InitStructure.RTC_SynchPrediv = uwSynchPrediv;
    RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
    RTC_Init(&RTC_InitStructure);

    // Set the date (BCD)
    RTC_DateTypeDef RTC_DateStructure;
    RTC_DateStructure.RTC_Year = 0x13;
    RTC_DateStructure.RTC_Month = RTC_Month_October;
    RTC_DateStructure.RTC_Date = 0x26;
    RTC_DateStructure.RTC_WeekDay = RTC_Weekday_Saturday;
    RTC_SetDate(RTC_Format_BCD, &RTC_DateStructure);

    // Set the time (BCD)
    RTC_TimeTypeDef RTC_TimeStructure;
    RTC_TimeStructure.RTC_H12     = RTC_H12_AM;
    RTC_TimeStructure.RTC_Hours   = 0x01;
    RTC_TimeStructure.RTC_Minutes = 0x53;
    RTC_TimeStructure.RTC_Seconds = 0x00;
    RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure);

    // Indicator for the RTC configuration
    RTC_WriteBackupRegister(RTC_BKP_DR0, 0x32F2);
}
Example #30
0
void RTC_Configuration(void) {
  EXTI_InitTypeDef EXTI_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
  
  /* If this is first time turn on the board */
  if (RTC_ReadBackupRegister(RTC_BKP_DR0) != 0x32F2) {  
    /* RTC configuration  */
    RTC_Config();
    /* Display the RTC Time and Alarm */
    RTC_TimeShow();
    RTC_AlarmShow();
  } else {  // If this is not the first time turn on the board
    /* Check if the Power On Reset flag is set */
    if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET) {
      /* Power On Reset occurred */
      printf("\r\n * Power On Reset occurred");
      
    /* Check if the Pin Reset flag is set */
    } else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET) {
      /* External Reset occurred */
      printf("\r\n * External Reset occurred");
    }
    
    /* Enable the PWR clock */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

    /* Allow access to RTC */
    PWR_BackupAccessCmd(ENABLE);

    /* Wait for RTC APB registers synchronisation */
    RTC_WaitForSynchro();

    /* Clear the RTC Alarm Flag */
    RTC_ClearFlag(RTC_FLAG_ALRAF);

    /* Clear the EXTI Line 17 Pending bit (Connected internally to RTC Alarm) */
    EXTI_ClearITPendingBit(EXTI_Line17);

    /* Display the RTC Time and Alarm */
    RTC_TimeShow();
    RTC_AlarmShow();
  }
   
  /* RTC Alarm A Interrupt Configuration */
  /* EXTI configuration *********************************************************/
  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);
  
  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);
  
  /* Enable the RTC Wakeup Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = RTC_WKUP_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure); 
  
  /* Enable the RTC Alarm Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = RTC_Alarm_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure); 
}