Ejemplo n.º 1
0
Archivo: main.c Proyecto: thaigiang/VMS
/**
  * @brief   Main program
  * @param  None
  * @retval None
  */
int main(void)
{	
	RCC_Configuration();
	GPIO_Configuration();
	USART_Configuration();	
	SysTick_Config(SystemCoreClock/10);
	// Enable the LSI OSC 
 	RCC_LSICmd(ENABLE);
	// Wait till LSI is ready 
	while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {};
	
 	IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
	// IWDG counter clock: LSI/256 
	IWDG_SetPrescaler(IWDG_Prescaler_256);
	IWDG_SetReload(0x0FFF);
	// Reload IWDG counter 
	IWDG_ReloadCounter();
	// Enable IWDG (the LSI oscillator will be enabled by hardware) 
	IWDG_Enable(); 
	
	// Write memmory
	FLASH_UnlockBank1();
	FLASH_ErasePage(FLAG_ADDR);
	FLASH_ProgramWord(FLAG_ADDR,(u32)FLAG_UPDATED);
	FLASH_LockBank1();
	
	updateFW_control();
}
Ejemplo n.º 2
0
void TM_RTC_Config(TM_RTC_ClockSource_t source) {
	if (source == TM_RTC_ClockSource_Internal) {
		/* Enable the LSI OSC */
		RCC_LSICmd(ENABLE);

		/* Wait till LSI is ready */
		while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET);

		/* Select the RTC Clock Source */
		RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
	} else if (source == TM_RTC_ClockSource_External) {
		/* 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);
	
	/* Wait for register synchronization */
	RTC_WaitForSynchro();

	/* Write status */
	RTC_WriteBackupRegister(RTC_STATUS_REG, RTC_STATUS_INIT_OK);
}
Ejemplo n.º 3
0
/**
  * @brief  Configures the LCD Glass. LSI is used as LCD clock source
  * @param  None
  * @retval None
  */
void LCD_Glass_Config(void)
{
  /* Enable PWR clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

  /* Allow access to the RTC */
  PWR_RTCAccessCmd(ENABLE);

  /* Reset Backup Domain */
  RCC_RTCResetCmd(ENABLE);
  RCC_RTCResetCmd(DISABLE);

  /* LSI Enable */
  RCC_LSICmd(ENABLE);

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

  /* LCD Clock Source Selection: LSI */
  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
#ifdef USE_STM32L152_EVAL
  /* LCD GLASS Initialization */
  LCD_GLASS_Init();
#endif  
}
Ejemplo n.º 4
0
void rtc_init(void) {
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); // Enable PWR clock

    PWR_RTCAccessCmd(ENABLE); // Enable access to RTC

    // Note: the LSI is used as RTC source clock
    // The RTC Clock may vary due to LSI frequency dispersion.  
   
    RCC_LSICmd(ENABLE); // Enable LSI
  
    while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {} // Wait until ready
    
    RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); // Select LSI as RTC Clock Source
  
    RCC_RTCCLKCmd(ENABLE); // Enable RTC Clock 
      
    RTC_WaitForSynchro(); // Wait for RTC registers synchronization

    uint32_t lsi_freq = 40000; // [TODO] To be measured precisely using a timer input capture

    RTC_InitTypeDef RTC_InitStructure;
    RTC_InitStructure.RTC_AsynchPrediv = 127;
    RTC_InitStructure.RTC_SynchPrediv	 = (lsi_freq / 128) - 1;
    RTC_InitStructure.RTC_HourFormat   = RTC_HourFormat_24;
    RTC_Init(&RTC_InitStructure);
    
    PWR_RTCAccessCmd(DISABLE); // Disable access to RTC
      
    rtc_inited = 1;
}
/****************************************************************************
* 名    称:void RTC_Configuration(void)
* 功    能:RTC配置函数
* 入口参数:无
* 出口参数:无
* 说    明:
* 调用方法:
****************************************************************************/
static void RTC_Configuration(void)
{ 
    /* Enable the PWR clock */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
    /* Allow access to RTC */
    PWR_RTCAccessCmd(ENABLE);
     /*!< Reset RTC Domain */
    RCC_RTCResetCmd(ENABLE);
    RCC_RTCResetCmd(DISABLE);

#if defined (RTC_CLOCK_SOURCE_LSI)  /* LSI used as RTC source clock*/
    /* The RTC Clock may varies due to LSI frequency dispersion. */   
    /* Enable the LSI OSC */ 
    RCC_LSICmd(ENABLE);
    /* Wait till LSI is ready */  
    while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {}
    /* Select the RTC Clock Source */
    RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
  
#elif defined (RTC_CLOCK_SOURCE_LSE) /* LSE used as RTC source clock *///外部32.378K晶振
    /* 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);
#else
    #error Please select the RTC Clock source inside the main.c file
#endif /* RTC_CLOCK_SOURCE_LSI */
  
    /* Enable the RTC Clock */
    RCC_RTCCLKCmd(ENABLE);
    /* Wait for RTC APB registers synchronisation */
    RTC_WaitForSynchro();
}
Ejemplo n.º 6
0
/**
  * @brief  Configures RTC clock source and prescaler.
  * @param  None
  * @retval None
  */
void RTC_Configuration(void)
{
    /* RTC clock source configuration ----------------------------------------*/
	/* Allow access to BKP Domain */
  	PWR_BackupAccessCmd(ENABLE);
	
    /* Reset Backup Domain */
    BKP_DeInit();
  
    /* Enable LSE OSC */
	RCC_LSICmd(ENABLE);
    /* Wait till LSE is ready */
	while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
    {
    }

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

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

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

    /* Set the RTC time base to 1s */
    RTC_SetPrescaler(32767);  
    /* Wait until last write operation on RTC registers has finished */
    RTC_WaitForLastTask();
	RTC_ITConfig(RTC_IT_ALR, ENABLE);
	RTC_WaitForLastTask();
  
}
Ejemplo n.º 7
0
void RTC_SetUp(void)
{
  /* Allow access to RTC */
	PWR_BackupAccessCmd(ENABLE);
	RCC_LSICmd(ENABLE); // LSI is used as RTC clock source
	while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
		; // Wait till LSI is ready

	RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); // Select RTC clock source
// Enable RTC clock
	RCC_RTCCLKCmd(ENABLE);

	RTC_WaitForSynchro(); // Wait until the RTC Time and Date registers (RTC_TR and RTC_DR) are synchronized with RTC APB clock.

	  // Set RTC calendar clock to 1 HZ (1 second)
	RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
	RTC_InitStructure.RTC_AsynchPrediv = 88;
	RTC_InitStructure.RTC_SynchPrediv = 470;
  
	if (RTC_Init(&RTC_InitStructure) == ERROR)
	{
		while (1)
			;
	}
}
Ejemplo n.º 8
0
/*
*********************************************************************************************************
*	函 数 名: RTC_Config
*	功能说明: 配置RTC用于跑表
*	形    参:无
*	返 回 值: 无
*********************************************************************************************************
*/
static void RTC_Config(void)
{
	/* 使能PWR时钟 */
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

	/* 允许访问RTC */
	PWR_BackupAccessCmd(ENABLE);

//	/* 复位备份域 */
//	RCC_BackupResetCmd(ENABLE);
//	RCC_BackupResetCmd(DISABLE);
	 
    /* 选择RTC时钟源 LSI或者LSE */	
	#if defined (RTC_CLOCK_SOURCE_LSI) 
		RCC_LSICmd(ENABLE);
		while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
		{
		}
		RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);

	#elif defined (RTC_CLOCK_SOURCE_LSE) 
		RCC_LSEConfig(RCC_LSE_ON);
		while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
		{
		}
		RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
	#else
		#error Please select the RTC Clock source inside the main.c file
	#endif
}
void PhotonWdgs::begin(bool _enableWwdg,bool _enableIwdg,unsigned long _timeout, TIMid id)
{
    if(!_enableWwdg && !_enableIwdg) {
        // nothing to do ...
        return;
    }
    PhotonWdgs::_aliveCount = 0;
    PhotonWdgs::_timeoutval = _timeout / 10;

    RCC_LSICmd(ENABLE); //LSI is needed for Watchdogs

    PhotonWdgs::_wdgTimer.begin(PhotonWdgs::_tickleWDGs, 20, hmSec, id);
    // OTA updates won't work with watchdog enabled
    System.disableUpdates();
    PhotonWdgs::_wwdgRunning = _enableWwdg;
    if(_enableWwdg) {
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, ENABLE);
        WWDG_SetPrescaler(WWDG_Prescaler_8);
        WWDG_SetWindowValue(0x7F);
        WWDG_Enable(0x7F);
    }
    PhotonWdgs::_iwdgRunning = _enableIwdg;
    if(_enableIwdg) {
        IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
        IWDG_SetPrescaler(IWDG_Prescaler_256);
        IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
        IWDG_SetReload(0xFFF);
        IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
        IWDG_Enable();
    }
}
Ejemplo n.º 10
0
//RTC设置成内部或者外部时钟
void RTC_Config(rtc_clk rtcclk)
{
  RTC_InitTypeDef RTC_InitStructure;
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
  PWR_BackupAccessCmd(ENABLE);

	if(rtcclk == extrnal)
	{
		/* 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);
		/* Wait for RTC APB registers synchronisation */
		RTC_WaitForSynchro();
	}
	if(rtcclk == inner)
	{
		RCC_LSICmd(ENABLE); //开启内部低速
		while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET);

		RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
		RCC_RTCCLKCmd(ENABLE);
		RTC_WaitForSynchro();
	}
	
	RTC_InitStructure.RTC_AsynchPrediv = 0x7F;
	RTC_InitStructure.RTC_SynchPrediv = 0xFF;
	RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
	RTC_Init(&RTC_InitStructure);
}
Ejemplo n.º 11
0
// 配置RTC硬件。
void RTC_Configure(void)
{
  	/* Enable PWR and BKP clocks */
  	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
  
  	/* Allow access to BKP Domain */
  	PWR_BackupAccessCmd(ENABLE);
	
	RCC_RTCCLKCmd(ENABLE);

  	/* Reset Backup Domain */
  	BKP_DeInit();
  
  	/* Wait until last write operation on RTC registers has finished */
  	RTC_WaitForLastTask();	

#ifdef RTCClockSource_LSI

  	/* Enable LSI */ 
  	RCC_LSICmd(ENABLE);

  	/* Wait till LSI is ready */
  	while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET);
	
  	/* Select LSI as RTC Clock Source */
  	RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);  

#elif defined	RTCClockSource_LSE  

  	/* 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);  
	
#endif

	// 上电后需要校准RTC时钟,也即准确计算出RTC的周期时长。
	RTC_Calibrate();

#ifdef RTCClockOutput_Enable  

  	/* Disable the Tamper Pin */
  	BKP_TamperPinCmd(DISABLE); /* To output RTCCLK/64 on Tamper pin, the tamper functionality must be disabled */
                               
  	/* Enable RTC Clock Output on Tamper Pin */
  	BKP_RTCCalibrationClockOutputCmd(ENABLE);
  
#endif 
  
  	/*允许RTC报警中断*/
	RTC_ITConfig(RTC_IT_ALR, ENABLE); 

  	/* Wait until last write operation on RTC registers has finished */
  	RTC_WaitForLastTask();
}
Ejemplo n.º 12
0
void RTC_Configuration(void)
{
	/* Enable PWR and BKP clocks */
  	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);

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

  	/* Reset Backup Domain */
  	BKP_DeInit();

#ifdef RTCClockSource_LSI
  /* Enable LSI */ 
  RCC_LSICmd(ENABLE);
  /* Wait till LSI is ready */
  while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
  {
  }

  /* Select LSI as RTC Clock Source */
  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);  
#elif defined	RTCClockSource_LSE
  	/* 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);
#endif
  	/* Enable RTC Clock */
  	RCC_RTCCLKCmd(ENABLE);

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

  	/* Wait until last write operation on RTC registers has finished */
  	RTC_WaitForLastTask();

  	/* Enable the RTC Second */
  	RTC_ITConfig(RTC_IT_SEC, ENABLE);

  	/* Wait until last write operation on RTC registers has finished */
  	RTC_WaitForLastTask();

  	/* Set RTC prescaler: set RTC period to 1sec */
      /* Set RTC prescaler: set RTC period to 1sec */
#ifdef RTCClockSource_LSI
  		RTC_SetPrescaler(31999); /* RTC period = RTCCLK/RTC_PR = (32.000 KHz)/(31999+1) */
#elif defined	RTCClockSource_LSE
  		RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
#endif

  //	RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */

  	/* Wait until last write operation on RTC registers has finished */
  	RTC_WaitForLastTask();
}
Ejemplo n.º 13
0
void SetRtcTime(void)
{
		/* Enable PWR and BKP clocks */
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);

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

		/* Reset Backup Domain */
		BKP_DeInit();

		#ifdef RTC_LSE
		/* 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);
		#else
		/* Enable the LSI OSC */
		RCC_LSICmd(ENABLE);
		/* Wait till LSI is ready */
		while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
		{}
		/* Select the RTC Clock Source */
		RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
		#endif

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

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

		/* Wait until last write operation on RTC registers has finished */
		RTC_WaitForLastTask();

		/* Enable the RTC Second */
		RTC_ITConfig(RTC_IT_SEC, ENABLE);

		/* Wait until last write operation on RTC registers has finished */
		RTC_WaitForLastTask();


		printf("\r\n RTC configured....");

		/* Adjust time by values entred by the user on the hyperterminal */
//		Time_Adjust();
		Time_SetCalendarTime(time_set);

		BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
		/* Wait until last write operation on RTC registers has finished */
		RTC_WaitForLastTask();
}
Ejemplo n.º 14
0
/**
  * @brief  To select MSI as System clock source 
  * @caller ADC_Icc_Test
  * @param Frequence, DIV by 2 ot not , With or without RTC
  * @retval None
  */
void SetHSICLKToMSI(uint32_t freq,bool div2,bool With_RTC)
{
  
  /* RCC system reset */
  RCC_DeInit();

  /* Flash 1 wait state */
  FLASH_SetLatency(FLASH_Latency_0);
  
  /* Disable Prefetch Buffer */
  FLASH_PrefetchBufferCmd(DISABLE);

  /* Disable 64-bit access */
  FLASH_ReadAccess64Cmd(DISABLE);
         
  /* Disable FLASH during SLeep  */
  FLASH_SLEEPPowerDownCmd(ENABLE);
 
  /* Enable the PWR APB1 Clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

  /* Select the Voltage Range 3 (1.2V) */
  PWR_VoltageScalingConfig(PWR_VoltageScaling_Range3);

  /* Wait Until the Voltage Regulator is ready */
  while (PWR_GetFlagStatus(PWR_FLAG_VOS) != RESET)
  {}

  /* To configure the MSI frequency */
  RCC_MSIRangeConfig(freq);
  
  /* Select MSI as system clock source */
  RCC_SYSCLKConfig(RCC_SYSCLKSource_MSI);

  /* Wait till MSI is used as system clock source */
  while (RCC_GetSYSCLKSource() != 0x00)
  {}
  
  if (div2)
  {
    RCC_HCLKConfig(RCC_SYSCLK_Div2);    
  }

  RCC_HSICmd(DISABLE);

  /* Disable HSE clock */
  RCC_HSEConfig(RCC_HSE_OFF);

  /* Disable LSE clock */
  if (! With_RTC)
    RCC_LSEConfig(RCC_LSE_OFF);

  /* Disable LSI clock */
  RCC_LSICmd(DISABLE);  

}
Ejemplo n.º 15
0
/*==================================================================================
* 函 数 名: rcc_init
* 参    数: None
* 功能描述:  rcc初始化
* 返 回 值: None
* 备    注: 初始化系统时钟,需要注意stm32f10x.h中对系统时钟的定义
* 作    者: gaodb
* 创建时间: 2012.10
==================================================================================*/ 
static void rcc_init(void)
{
    ErrorStatus HSEStartUpStatus;

		RCC_DeInit();
		
		wait_sys_peri_ready();
		
    /* Enable HSE */  
    RCC_HSEConfig(RCC_HSE_ON);
    RCC_HSEConfig(RCC_HSE_Bypass);//外部晶振为24M有源晶振
    
    /* Wait till HSE is ready */
    HSEStartUpStatus = RCC_WaitForHSEStartUp(); 
    
    if (HSEStartUpStatus == SUCCESS)
    {    
				FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
				
				FLASH_SetLatency(FLASH_Latency_2);
				
				RCC_HCLKConfig(RCC_SYSCLK_Div1); 
				
				RCC_PCLK1Config(RCC_HCLK_Div2);//低速时钟最高36M
				
				RCC_PCLK2Config(RCC_HCLK_Div1);
				
				RCC_ADCCLKConfig(RCC_PCLK2_Div6);
		
        /* PLLCLK = 24MHz * 3 = 72 MHz */
        RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_3);
        
        /* Enable PLL */
        RCC_PLLCmd(ENABLE);
        
        /* Wait till PLL is ready */
        while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
        {}
        
        /* Select PLL as system clock source */
        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
        
        /* Wait till PLL is used as system clock source */
        while (RCC_GetSYSCLKSource() != 0x08)
        {}
    }
    RCC_ClockSecuritySystemCmd(ENABLE);  //Enable CSSON(Clock securuty system)  
    
		/* Enable the LSI OSC */
	  RCC_LSICmd(ENABLE);									 //为独立看门狗提供时钟
	  
	  /* Wait till LSI is ready */
	  while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
	  {}
}
Ejemplo n.º 16
0
static void rtcConfig(void)
{
    /* Enable PWR and BKP clocks */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);

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

    /* Reset Backup Domain */
    BKP_DeInit();

    /* Enable the LSI OSC */
    RCC_LSICmd(ENABLE);

    /* Wait till LSI is ready */
    while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET);

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

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

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

    /* Wait until last write operation on RTC registers has finished */
    RTC_WaitForLastTask();

    /* Enable the RTC Second */
    RTC_ITConfig(RTC_IT_SEC, ENABLE);

    /* Wait until last write operation on RTC registers has finished */
    RTC_WaitForLastTask();

#ifndef PRECISION_IN_MS
    /* Set RTC prescaler: set RTC period to 1 sec */
    RTC_SetPrescaler(40000); //LSI default is 40k HZ
#else
    /* Set RTC prescaler: set RTC period to 1ms */
    RTC_SetPrescaler(40); //LSI default is 40k HZ
#endif

    /* Wait until last write operation on RTC registers has finished */
    RTC_WaitForLastTask();

    /* To output second signal on Tamper pin, the tamper functionality
     * must be disabled (by default this functionality is disabled)
     */
    //BKP_TamperPinCmd(DISABLE);

    /* Enable the RTC Second Output on Tamper Pin */
    //BKP_RTCOutputConfig(BKP_RTCOutputSource_Second);
}
Ejemplo n.º 17
0
/**
  * @brief  Restore peripheral config before entering STOP mode.
  * @param  None
  * @retval None
  */
void RestoreConfiguration(void)
{
  /* Restore system clock to 32MHz */
  SetSysClock();

  /* Enable HSI clock for ADC */ 
  RCC_HSICmd(ENABLE);

  /* LSI Enable */
  RCC_LSICmd(ENABLE);
}
Ejemplo n.º 18
0
// Control functions
void rtcInit(void) {
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

  PWR_BackupAccessCmd(ENABLE);

  RCC_BackupResetCmd(ENABLE);
  RCC_BackupResetCmd(DISABLE);

  RCC_LSICmd(ENABLE);
  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
  RCC_RTCCLKCmd(ENABLE);
}
Ejemplo n.º 19
0
/*
*********************************************************************************************************
*	函 数 名: bsp_InitIwdg
*	功能说明: 独立看门狗时间配置函数
*	形    参:IWDGTime: 0 ---- 0x0FFF
*			  独立看门狗时间设置,单位为ms,IWDGTime = 1000 大约就是一秒的
*             时间,这里没有结合TIM5测得实际LSI频率,只是为了操作方便取了
*             一个估计值超过IWDGTime不进行喂狗的话系统将会复位。
*			  LSI = 34000左右
*	返 回 值: 无		        
*********************************************************************************************************
*/
void bsp_iwdg_init(unsigned int IWDGTime)
{
		
	/* 检测系统是否从独立看门狗复位中恢复 */
	if (RCC_GetFlagStatus(RCC_FLAG_IWDGRST) != RESET)
	{		
		/* 清除复位标志 */
		RCC_ClearFlag();
	}
	else
	{
		/* 标志没有设置 */

	}
	
#if 1
	/* 通过TIM5输入捕获得到LSI频率 */
	LsiFreq = GetLSIFrequency();
#else
	/* 使能LSI */
	RCC_LSICmd(ENABLE);
	
	/* 等待直到LSI就绪 */
	while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
	{}
	
    /* */		
	LsiFreq = 32000;
#endif
	
	/* 写入0x5555表示允许访问IWDG_PR 和IWDG_RLR寄存器 */
	IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
	
	/*  LSI/32 分频*/
	IWDG_SetPrescaler(IWDG_Prescaler_256);
	
	/*特别注意,由于这里_ulIWDGTime的最小单位是ms, 所以这里重装计数的
	  计数时 需要除以1000
	 Counter Reload Value = (IWDGTime / 1000) /(1 / IWDG counter clock period)
	                      = (IWDGTime / 1000) / (32/LSI)
	                      = (IWDGTime / 1000) / (32/LsiFreq)
	                      = LsiFreq * IWDGTime / 32000
	 实际测试LsiFreq = 34000,所以这里取1的时候 大概就是1ms 
	*/
	IWDG_SetReload(IWDGTime);
	
	/* 重载IWDG计数 */
	IWDG_ReloadCounter();
	
	/* 使能 IWDG (LSI oscillator 由硬件使能) */
	IWDG_Enable();		
}
/**
  * @brief  Configure the RTC peripheral by selecting the clock source.
  * @param  None
  * @retval None
  */
void RTC_Config(void)
{
    /* Enable the PWR clock */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

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

#if defined (RTC_CLOCK_SOURCE_LSI)  /* LSI used as RTC source clock*/
    /* The RTC Clock may varies due to LSI frequency dispersion. */
    /* Enable the LSI OSC */
    RCC_LSICmd(ENABLE);

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

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

    SynchPrediv = 0x18F;
    AsynchPrediv = 0x63;

#elif defined (RTC_CLOCK_SOURCE_LSE) /* LSE used as RTC source clock */
    /* 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);

    SynchPrediv = 0xFF;
    AsynchPrediv = 0x7F;

#else
#error Please select the RTC Clock source inside the main.c file
#endif /* RTC_CLOCK_SOURCE_LSI */

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

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

    /* Enable The TimeStamp */
    RTC_TimeStampCmd(RTC_TimeStampEdge_Falling, ENABLE);
}
Ejemplo n.º 21
0
void IwdgConfig()
{
    RCC_LSICmd(ENABLE);                              //打开LSI
    while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY)==RESET);
    IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);

    IWDG_SetPrescaler(IWDG_Prescaler_32);

    IWDG_SetReload(4000);      //80ms ,max 0xFFF  0~4095 

    IWDG_ReloadCounter();

    IWDG_Enable();
}
Ejemplo n.º 22
0
void RTC_Configuration()
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

	PWR_BackupAccessCmd(ENABLE);

	RCC_BackupResetCmd(ENABLE);
	RCC_BackupResetCmd(DISABLE);

	RCC_LSICmd(ENABLE);
	RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
	RCC_RTCCLKCmd(ENABLE);

	RCC_LSEConfig(RCC_LSE_ON);
}
Ejemplo n.º 23
0
/**
  * @brief  This function configures the system to enter Standby mode with RTC 
  *         clocked by LSI for current consumption measurement purpose.
  *         STANDBY Mode with RTC clocked by LSI
  *         ========================================
  *           - RTC Clocked by LSI
  *           - IWDG OFF
  *           - Automatic Wakeup using RTC 
  * @param  None
  * @retval None
  */
void StandbyRTCMode_Measure(void)
{  
  /* Allow access to RTC */
  PWR_BackupAccessCmd(ENABLE);
  
  /* The RTC Clock may varies due to LSI frequency dispersion. */   
  /* Enable the LSI OSC */ 
  RCC_LSICmd(ENABLE);
  
  /* Wait till LSI is ready */  
  while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
  {
  }
  
  /* Select the RTC Clock Source */
  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
  
  /* Enable the RTC Clock */
  RCC_RTCCLKCmd(ENABLE);
  
  /* Wait for RTC APB registers synchronisation */
  RTC_WaitForSynchro();
    
  /* RTC Wakeup Interrupt Generation: Clock Source: RTCCLK_Div16, Wakeup Time Base: ~5s
     RTC Clock Source LSI ~32KHz 

     Wakeup Time Base = (16 / (LSI)) * WakeUpCounter
  */
  RTC_WakeUpClockConfig(RTC_WakeUpClock_RTCCLK_Div16);
  RTC_SetWakeUpCounter(0x2800-1);
  
  /* Enable the Wakeup Interrupt */
  RTC_ITConfig(RTC_IT_WUT, ENABLE); 

  /* Enable Wakeup Counter */
  RTC_WakeUpCmd(ENABLE);
  
  /* Clear Wakeup flag */
  PWR_ClearFlag(PWR_FLAG_WU);  
    
  /* Request to enter STANDBY mode (Wake Up flag is cleared in PWR_EnterSTANDBYMode function) */
  PWR_EnterSTANDBYMode();
  
  /* Infinite loop */
  while (1)
  {
  }
}
Ejemplo n.º 24
0
void wdgInit(void)
{
#ifdef LSI_TIM_MEASURE
	  /* Enable the LSI OSC */
	  RCC_LSICmd(ENABLE);

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

	  /* TIM Configuration -------------------------------------------------------*/
	  TIM5_ConfigForLSI();

	  /* Wait until the TIM5 get 2 LSI edges */
	  while(CaptureNumber != 2)
	  {
	  }

	  /* Disable TIM5 CC4 Interrupt Request */
	  TIM_ITConfig(TIM5, TIM_IT_CC4, DISABLE);
#endif

	  DBGMCU->CR |=  DBGMCU_CR_DBG_IWDG_STOP; // Debug Independent Watchdog stopped when Core is halted

	  /* Enable write access to IWDG_PR and IWDG_RLR registers */
	  IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);

	  /* IWDG counter clock: LSI/32 */
	  IWDG_SetPrescaler(IWDG_Prescaler_32);

	  /*
	     Set counter reload value to obtain 250ms IWDG TimeOut.
	     Counter Reload Value = 250ms/IWDG counter clock period
	                          = 250ms / (LSI/32)
	                          = 0.25s / (LsiFreq/32)
	                          = LsiFreq/(32 * 4)
	                          = LsiFreq/128
	   */
	  IWDG_SetReload(LsiFreq/128);

	  /* Reload IWDG counter */
	  IWDG_ReloadCounter();

	  /* Enable IWDG (the LSI oscillator will be enabled by hardware) */
	  IWDG_Enable();
}
Ejemplo n.º 25
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);
}
Ejemplo n.º 26
0
void RTCInit(void) {
	NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);  

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

	// Reset Backup Domain
	BKP_DeInit();

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

	// Disable the LSI OSC
	RCC_LSICmd(ENABLE);

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

	// Enable the RTC Clock
	RCC_RTCCLKCmd(ENABLE);

	// Wait for RTC registers synchronization
	RTC_WaitForSynchro();

	// Wait until last write operation on RTC registers has finished
	RTC_WaitForLastTask();

	// Enable the RTC overflow interrupt
	RTC_ITConfig(RTC_IT_SEC, ENABLE);

	//Set 32768 prescaler - for one second interupt
	RTC_SetPrescaler(0x7FFF);

	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

	NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

}
Ejemplo n.º 27
0
/*
*********************************************************************************************************
*	函 数 名: bsp_InitIwdg
*	功能说明: 初始化独立看门狗。
*	形    参:  
*	返 回 值: 无
*********************************************************************************************************
*/
static void bsp_InitIwdg(void)
{
    /* 打开独立看门狗时钟,LSI时钟32KHz*/
    RCC_LSICmd(ENABLE);
    /* 等待LSI时钟准备好 */
    while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
    {
    }
    /* 使能独立看门狗寄存器写操作 */
    IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
    /* 设置独立看门狗时钟为 2KHz, 最大超时时间:2048ms */
    IWDG_SetPrescaler(IWDG_Prescaler_16);
    /* 设置独立看门狗重装载值为1S */
    IWDG_SetReload(2048-1);
    /* 重装载独立看门狗计数器 */
    IWDG_ReloadCounter();
    /* 使能独立看门狗计数器 */
    IWDG_Enable();
}
Ejemplo n.º 28
0
void RTC_Config()
{
	RTC_InitTypeDef RTC_InitStructure;

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);    /* Enable the PWR clock */
	PWR_BackupAccessCmd(ENABLE);                          /* Allow access to RTC */

	RCC_LSICmd(ENABLE);                                   /* Enable the LSI OSC */
	while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET);   /* Wait till LSI is ready */  
	RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);               /* Select the RTC Clock Source */

	RCC_RTCCLKCmd(ENABLE);                                /* Enable the RTC Clock */
	RTC_WaitForSynchro();                  /* Wait for RTC APB registers synchronisation */

	/* Configure the RTC data register and RTC prescaler */
	RTC_InitStructure.RTC_AsynchPrediv = 127;
	RTC_InitStructure.RTC_SynchPrediv = 249;
	RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
	RTC_Init(&RTC_InitStructure);
}
Ejemplo n.º 29
0
/**
 * We want to use the ADC in this app, which needs the HSI, which runs at 16MHz,
 * by default the 32L starts up in voltage range 2, which only allows 8Mhz 
 * without inserting wait states for flash and so on.
 */
void SystemInit(void)
{
    // we need clocks to reach the power part...
    RCC->APB1ENR |= RCC_APB1ENR_PWREN;

    /* Select the Voltage Range 1 (1.8 V) */
    PWR->CR = PWR_CR_VOS_0;

    /* Wait Until the Voltage Regulator is ready */
    while ((PWR->CSR & PWR_CSR_VOSF) != RESET)
        ;

    /* HSI is 16mhz RC clock directly fed to SYSCLK (rm00038, figure 9) */

    //#define USE_RAW_CMSIS
#ifdef USE_RAW_CMSIS
    // Do it all by hand with RCC->CR and so on...
#else // use stm32 lib methods...
    /* enable the HSI clock (high speed internal) */
    RCC_HSICmd(ENABLE);

    /* wail til HSI ready */
    while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET)
        ;

    /* now switch to this source */
    RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);

    // Turn off unneeded clocks...
    // We're not using LSE yet, but we might want to experiment with it...
    RCC_HSEConfig(RCC_HSE_OFF);
    RCC_LSICmd(DISABLE);
    RCC_MSICmd(DISABLE);
    RCC_PLLCmd(DISABLE);
#endif
    // we don't really need to wait for them to shut down...

    // not sure how to use this yet...
    // SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
    //SCB->VTOR = FLASH_BASE | 0x0; /* Vector Table Relocation in Internal FLASH. */
}
Ejemplo n.º 30
0
int RTC_LSI(void)
{
    uint32_t count=0xFFFFFFFF;
    
    // Enable the LSI OSC
    RCC_LSICmd(ENABLE);

    // Wait till LSI is ready
    while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET && (--count) > 0) {
        __NOP();
        __NOP();
        __NOP();
        __NOP();
    }
    if(count == 0) return -1;

    // Select the RTC Clock Source
    RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
    
    return 0;
}