Exemple #1
0
/**
  * @brief  This function handles Tamper interrupt request.
  * @param  None
  * @retval None
  */
void TAMPER_IRQHandler(void)
{
    if(BKP_GetITStatus() != RESET)
    {   /* Tamper detection event occured */

        /* Check if Backup registers are cleared */
        if(IsBackupRegReset() == 0)
        {   /* OK, Backup registers are reset as expected */

            /* Turn on LED3 */
            STM_EVAL_LEDOn(LED3);
        }
        else
        {   /* Backup registers are not reset */
            /* Turn on LED4 */
            STM_EVAL_LEDOn(LED4);
        }

        /* Clear Tamper pin interrupt pending bit */
        BKP_ClearITPendingBit();

        /* Clear Tamper pin Event(TE) pending flag */
        BKP_ClearFlag();

        /* Disable Tamper pin */
        BKP_TamperPinCmd(DISABLE);

        /* Enable Tamper pin */
        BKP_TamperPinCmd(ENABLE);

    }
}
Exemple #2
0
/* 配置是否使能64分频引脚 */
void cfg_rtc_pin(int enable)
{
	if (1 == enable) {
		BKP_TamperPinCmd(ENABLE);
		BKP_RTCOutputConfig(BKP_RTCOutputSource_CalibClock);
	} else {
		BKP_TamperPinCmd(DISABLE);
		BKP_RTCOutputConfig(BKP_RTCOutputSource_None);
	}

}
/**
  * @brief  Apllication Initialisation Routine
  * @param  None
  * @retval : None
  */
void ApplicationInit(void)
{
  /* System Clocks Configuration */
  RCC_Configuration();
  /*Enables the clock to Backup and power interface peripherals    */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP | RCC_APB1Periph_PWR,ENABLE);
  
  /* SysTick Configuration*/
  SysTickConfig();
  
  /*Initialisation of TFT LCD */
  STM3210B_LCD_Init();
  
  /* Unlock the Flash Program Erase controller */
  FLASH_Unlock();
  /*RTC_NVIC Configuration */
  RTC_NVIC_Configuration();
  
  /* RTC Configuration*/
  RTC_Configuration();
  BKP_RTCOutputConfig(BKP_RTCOutputSource_None);
  
  /* General Purpose I/O Configuration */
  GPIO_Configuration();
  
  /* Battery Removal Emulation   */
  GPIO_SetBits(GPIOC, GPIO_Pin_8);
  
  while(!(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_13)))
  {
    TamperEvent = 1;
  }
  
  /* Joystick NVIC Configuration  */
  NVIC_JoyStickConfig();

  /* Tamper pin NVIC Configuration  */
  Tamper_NVIC_Configuration();

  /* Configure PVD Supervisor to disable the Tamper Interrupt when voltage drops 
  below 2.5 volts*/
  PWR_PVDCmd(ENABLE);
  PWR_PVDLevelConfig(PWR_PVDLevel_2V5);
  PWR_BackupAccessCmd(ENABLE);

  /* Only JoyStick Sel Interrupt is enabled on startup */
  SelIntExtOnOffConfig(ENABLE);

  /* Tamper FeatureRTC  -   Enable Tamper Interrupt and configure for Low level */
  BKP_ITConfig(ENABLE);

  /* Enable Tamper Pin for Active low level: Tamper level detected for low level*/
  BKP_TamperPinLevelConfig(BKP_TamperPinLevel_Low);

  /* Enable tamper Pin Interrupt */
  BKP_TamperPinCmd(ENABLE);

  /*  Menu Initialisation  */
  MenuInit();
}
/**
  * @brief  Calibration of External crystal oscillator manually
  * @param  None
  * @retval : None
  */
void ManualClockCalibration(void)
{
  UpDownIntOnOffConfig(ENABLE);
  RightLeftIntExtOnOffConfig(ENABLE);
  SelIntExtOnOffConfig(DISABLE);
  LCD_Clear(Blue2);
  BKP_TamperPinCmd(DISABLE);
  BKP_RTCOutputConfig(BKP_RTCOutputSource_CalibClock);
  LCD_DisplayString(Line1,Column5,"Calibration");
  LCD_DisplayString(Line3,Column0,"LSE/64 is available");
  LCD_DisplayString(Line4,Column0,"on PC13.Measure the");
  LCD_DisplayString(Line5,Column0,"the frequency and");
  LCD_DisplayString(Line6,Column0,"press Sel to proceed");
  
  while(ReadKey()!=SEL)
  {
  }
  
  LCD_Clear(Blue2);
  BKP_RTCOutputConfig(BKP_RTCOutputSource_None);
  LCD_DisplayString(Line1,Column4,"Please enter");
  LCD_DisplayString(Line2,Column2,"Calibration Value");
  LCD_DisplayCount(Line4,Column6,1,ArrayTime[0]+0x30);
  LCD_DisplayCount(Line4,Column7,0,ArrayTime[1]+0x30);
  LCD_DisplayCount(Line4,Column8,0,ArrayTime[2]+0x30);
  LCD_DisplayString(Line6,Column6,"(0-121)");
  LCD_SetBackColor(Green);
  SelIntExtOnOffConfig(ENABLE);
}
Exemple #5
0
 /**
  * @file   RTC_Init
  * @brief  RTC Initialization
  * @param  无
  * @retval 无
  */
void RTC_Init(void)
{
  if (BKP_ReadBackupRegister(BKP_DR1)!= 0xA5A5)
  {
    /* Backup data register value is not correct or not yet programmed (when
    the first time the program is executed) */
    printf("RTC not yet configured....\r\n");
    /* RTC Configuration */
     RTC_Configuration();
	   Time_Regulate();
	/* Adjust time by values entred by the user on the hyperterminal */
    printf("RTC configured....\r\n");
    BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
  }
  else
  {
    /* Check if the Power On Reset flag is set */
    if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
    {
      printf("Power On Reset occurred....\r\n");
    }
    /* Check if the Pin Reset flag is set */
    else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
    {
      printf("External Reset occurred....\r\n");
    }

    printf("No need to configure RTC....\r\n");
    /* Wait for RTC registers synchronization */
    RTC_WaitForSynchro();

    /* Enable the RTC Second */
    RTC_ITConfig(RTC_IT_SEC, ENABLE);
    /* Wait until last write operation on RTC registers has finished */
    RTC_WaitForLastTask();
  }

   /* NVIC configuration */
   NVIC_Configuration();

#ifdef RTCClockOutput_Enable
  /* Enable PWR and BKP clocks */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);

  /* Allow access to BKP Domain */
  PWR_BackupAccessCmd(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_RTCOutputConfig(BKP_RTCOutputSource_CalibClock);
#endif

   /* Clear reset flags */
  RCC_ClearFlag();
  return;
}
Exemple #6
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();
}
Exemple #7
0
/*******************************************************************************
* Function Name  : TAMPER_IRQHandler
* Description    : This function handles Tamper interrupt request.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void TAMPER_IRQHandler(void)
{
  BKP_TamperPinCmd(DISABLE);
  u8_TamperEvent=1;
  BKP_ClearITPendingBit();
  BKP_ClearFlag();

  //NVIC_ClearIRQChannelPendingBit(TAMPER_IRQn);
}
/**
  * @brief   Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /* System Clocks Configuration */
  RCC_Configuration();
  
  /* NVIC configuration */
  NVIC_Configuration();
    
  /* Initialize Leds mounted on STM3210X-EVAL board */
  STM_EVAL_LEDInit(LED1);
  STM_EVAL_LEDInit(LED2);
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED4);

  /* Enable PWR and BKP clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);

  /* Enable write access to Backup domain */
  PWR_BackupAccessCmd(ENABLE);

  /* Clear Tamper pin Event(TE) pending flag */
  BKP_ClearFlag();

  /* Tamper pin active on low level */
  BKP_TamperPinLevelConfig(BKP_TamperPinLevel_Low);

  /* Enable Tamper interrupt */
  BKP_ITConfig(ENABLE);

  /* Enable Tamper pin */
  BKP_TamperPinCmd(ENABLE);
   
  /* Write data to Backup DRx registers */
  WriteToBackupReg(0xA53C);
  /* Check if the written data are correct */
  if(CheckBackupReg(0xA53C) == 0x00)
  {
    /* Turn on LED1 */
    STM_EVAL_LEDOn(LED1);
  }
  else
  {
    /* Turn on LED2 */
    STM_EVAL_LEDOn(LED2);
  }
        
  while (1)
  {    
  }
}
Exemple #9
0
/**
  * @brief  Configures the RTC.
  * @param  None
  * @retval : None
  */
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();

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

  /* Set RTC prescaler: set RTC period to 1sec */
  RTC_SetPrescaler(40000);

  /* 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);
}
Exemple #10
0
/*******************************************************************************
* Function Name  : RTC_Application
* Description    : RTC Application runs in while loop
* Input          : None
* Output         : Displays Time, Date and Alarm Status
* Return         : None
*******************************************************************************/
void RTC_Application(void)
{
  u8 u8_ReturnValue;

  CalculateTime();
  
  if(u8_TimeDateDisplay==0)
  {
    //DisplayTime();
  }

  if(u8_DisplayDateFlag==1 && u8_TimeDateDisplay==0)
  {
    //DisplayDate();
    u8_DisplayDateFlag=0;
  }

  if(u8_AlarmStatus==1)
  {
    //DisplayAlarm();
    GPIO_SetBits(GPIOC, GPIO_Pin_6);
    u32_Dummy=RTC_GetCounter();
    u8_AlarmStatus=2;
  }
  if(((RTC_GetCounter()-u32_Dummy) == 4) && (u8_AlarmStatus==2))
  {
    GPIO_ResetBits(GPIOC, GPIO_Pin_6);
    //SelIntExtOnOffConfig(ENABLE);
    //RightLeftIntExtOnOffConfig(ENABLE);
    //UpDownIntOnOffConfig(ENABLE);
    //MenuInit();
    u32_Dummy=0;
    u8_AlarmStatus = 0;
  }

  if(u8_TamperEvent==1) /* Tamper event is detected */
  {
    u8_TamperNumber = u8_TamperNumber + 1;
    BKP_WriteBackupRegister(BKP_DR5,u8_TamperNumber);
    BKP_WriteBackupRegister(BKP_DR1, CONFIGURATION_DONE);
    BKP_WriteBackupRegister(BKP_DR2,s_DateStructVar.u8_Month);
    BKP_WriteBackupRegister(BKP_DR3,s_DateStructVar.u8_Day);
    BKP_WriteBackupRegister(BKP_DR4,s_DateStructVar.u16_Year);
    BKP_WriteBackupRegister(BKP_DR9,s_AlarmDateStructVar.u8_Day);
    BKP_WriteBackupRegister(BKP_DR8,s_AlarmDateStructVar.u8_Month);
    BKP_WriteBackupRegister(BKP_DR10,s_AlarmDateStructVar.u16_Year);
    BKP_WriteBackupRegister(BKP_DR6,u8_ClockSource);
    BKP_WriteBackupRegister(BKP_DR7,u16_SummerTimeCorrect);
    /*
    u8_ReturnValue=EE_WriteVariable(u8_TamperNumber);
    
    if(u8_ReturnValue==PAGE_FULL)
    {
      u8_ReturnValue=EE_WriteVariable(u8_TamperNumber);
    }
    
    u8_ReturnValue=EE_WriteVariable(s_DateStructVar.u8_Day);
    u8_ReturnValue=EE_WriteVariable(s_DateStructVar.u8_Month);
    u8_ReturnValue=EE_WriteVariable((s_DateStructVar.u16_Year)/100);
    u8_ReturnValue=EE_WriteVariable((s_DateStructVar.u16_Year)%100);
    u8_ReturnValue=EE_WriteVariable((s_TimeStructVar.u8_HourHigh*10)+\
                    s_TimeStructVar.u8_HourLow);
    u8_ReturnValue=EE_WriteVariable((s_TimeStructVar.u8_MinHigh*10)+\
                    s_TimeStructVar.u8_MinLow);
    u8_ReturnValue=EE_WriteVariable((s_TimeStructVar.u8_SecHigh*10)+\
                    s_TimeStructVar.u8_SecLow);*/
    while(!(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_13)))
    {
    }
    
    u8_TamperEvent=0;
    BKP_TamperPinCmd(ENABLE);
  }
  
  if(u8_BatteryRemoved != 0) /* Battery tamper is detected */
  {
    u8_TamperNumber = u8_TamperNumber + 1;
    BKP_WriteBackupRegister(BKP_DR5,u8_TamperNumber);
    /*
    u8_ReturnValue=EE_WriteVariable(u8_TamperNumber);
    
    if(u8_ReturnValue==PAGE_FULL)
    {
      u8_ReturnValue=EE_WriteVariable(u8_TamperNumber);
    }
    
    u8_ReturnValue=EE_WriteVariable(s_DateStructVar.u8_Day);
    u8_ReturnValue=EE_WriteVariable(s_DateStructVar.u8_Month);
    u8_ReturnValue=EE_WriteVariable((s_DateStructVar.u16_Year)/100);
    u8_ReturnValue=EE_WriteVariable((s_DateStructVar.u16_Year)%100);
    u8_ReturnValue=EE_WriteVariable((s_TimeStructVar.u8_HourHigh*10)\
                    +s_TimeStructVar.u8_HourLow);
    u8_ReturnValue=EE_WriteVariable((s_TimeStructVar.u8_MinHigh*10)\
                    +s_TimeStructVar.u8_MinLow);
    if(u8_BatteryRemoved==1)
    {
      u8_ReturnValue=EE_WriteVariable(BATTERY_REMOVED);
      u8_BatteryRemoved=0;
    }
    else if(u8_BatteryRemoved==2)
    {
      u8_ReturnValue=EE_WriteVariable(BATTERY_RESTORED);
      u8_BatteryRemoved=0;
    }
*/
    BKP_TamperPinCmd(ENABLE);
  }
}
Exemple #11
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_stm32f10x_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f10x.c file
     */     

  /* Initialize LED1 mounted on STM3210X-EVAL board */       
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA , ENABLE);
	
	 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 ; // USART 2
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
	
		 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 ; // USART 2
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	
  /* 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;

	
	USART_Init(USART1,&USART_InitStructure);
	USART_Cmd(USART1,  ENABLE);




  /* NVIC configuration */
  NVIC_Configuration();

  if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
  {
    /* Backup data register value is not correct or not yet programmed (when
       the first time the program is executed) */

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

    /* RTC Configuration */
    RTC_Configuration();

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

    /* Adjust time by values entered by the user on the hyperterminal */
    Time_Adjust();

    BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
  }
  else
  {
    /* Check if the Power On Reset flag is set */
    if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
    {
      printf("\r\n\n Power On Reset occurred....");
    }
    /* Check if the Pin Reset flag is set */
    else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
    {
      printf("\r\n\n External Reset occurred....");
    }

    printf("\r\n No need to configure RTC....");
    /* Wait for RTC registers synchronization */
    RTC_WaitForSynchro();

    /* Enable the RTC Second */
    RTC_ITConfig(RTC_IT_SEC, ENABLE);
    /* Wait until last write operation on RTC registers has finished */
    RTC_WaitForLastTask();
  }

#ifdef RTCClockOutput_Enable
  /* Enable PWR and BKP clocks */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);

  /* Allow access to BKP Domain */
  PWR_BackupAccessCmd(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_RTCOutputConfig(BKP_RTCOutputSource_CalibClock);
#endif

  /* Clear reset flags */
  RCC_ClearFlag();

  /* Display time in infinite loop */
  Time_Show();
}
/**
  * @brief  Calibration of External crystal oscillator auto(through Timer
  *   
  * @param  None
  * @retval : None
  */
void AutoClockCalibration(void)
{
  RCC_ClocksTypeDef ClockValue;
  uint16_t TimerPrescalerValue=0x0003;
  uint16_t CountWait;
  uint16_t DeviationInteger;
  uint32_t CalibrationTimer;
  float f32_Deviation;
  TIM_ICInitTypeDef  TIM_ICInitStructure;
  
  TIM_DeInit(TIM2);
  BKP_TamperPinCmd(DISABLE);
  BKP_RTCOutputConfig(BKP_RTCOutputSource_CalibClock);
  
  /* TIM2 configuration: PWM Input mode ------------------------
     The external signal is connected to TIM2 CH2 pin (PA.01),
     The Rising edge is used as active edge,
     The TIM2 CCR2 is used to compute the frequency value
     The TIM2 CCR1 is used to compute the duty cycle value
  ------------------------------------------------------------ */
  TIM_PrescalerConfig(TIM2,TimerPrescalerValue,TIM_PSCReloadMode_Immediate);
  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
  TIM_ICInitStructure.TIM_ICFilter = 0x00;
  TIM_PWMIConfig(TIM2, &TIM_ICInitStructure);
  TIM_ICInit(TIM2, &TIM_ICInitStructure);
  /* Select the TIM2 Input Trigger: TI2FP2 */
  TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);
  /* Select the slave Mode: Reset Mode */
  TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);
  /* Enable the Master/Slave Mode */
  TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);
  /* TIM enable Counter */
  TIM_Cmd(TIM2, ENABLE);
  LCD_Clear(Blue2);
  LCD_DisplayString(Line4,Column1,"Please Wait.....");
  /* Wait for 2 seconds */
  CalibrationTimer = RTC_GetCounter();
 
  while((RTC_GetCounter() - CalibrationTimer) < 2)
  {
  }
  
  RCC_GetClocksFreq(&ClockValue);
  TimerFrequency=(ClockValue.PCLK1_Frequency * 2)/(TimerPrescalerValue+1);
   /* Enable the CC2 Interrupt Request */
  TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);
   /* Wait for 2 seconds */
  CalibrationTimer = RTC_GetCounter();
  
  while((RTC_GetCounter() - CalibrationTimer) < 2)
  {
  }

  if(!(TIM_GetFlagStatus(TIM2, TIM_FLAG_CC1)))
   /* There is no signal at the timer TIM2 peripheral input */
  {
    LCD_Clear(Blue2);
    LCD_DisplayString(Line3,Column0,"Please connect wire");
    LCD_DisplayString(Line4,Column0,"link between PC13");
    LCD_DisplayString(Line5,Column0,"and PA1");
    LCD_DisplayString(Line7,Column0,"No calibration done");
  }
  else
  {
    /* Calulate Deviation in ppm  using the formula :
    Deviation in ppm = (Deviation from 511.968/511.968)*1 million*/
    if(f32_Frequency > 511.968)
    {
      f32_Deviation=((f32_Frequency-511.968)/511.968)*1000000;
    }
    else
    {
      f32_Deviation=((511.968-f32_Frequency)/511.968)*1000000;
    }
     DeviationInteger = (uint16_t)f32_Deviation;
    
    if(f32_Deviation >= (DeviationInteger + 0.5))
    {
      DeviationInteger = ((uint16_t)f32_Deviation)+1;
    }
    
   CountWait=0;

   /* Frequency deviation in ppm should be les than equal to 121 ppm*/
   if(DeviationInteger <= 121)
   {
     while(CountWait<128)
     {
       if(CalibrationPpm[CountWait] == DeviationInteger)
       break;
       CountWait++;
     }

     BKP_SetRTCCalibrationValue(CountWait);
     LCD_Clear(Blue2);
     LCD_DisplayString(Line4,Column1,"Calibration Value");
     LCD_DisplayChar(Line5,Column10,(CountWait%10)+0x30);
     CountWait=CountWait/10;
     LCD_DisplayChar(Line5,Column9,(CountWait%10)+0x30);
     CountWait=CountWait/10;
   
     if(CountWait>0)
     {
       LCD_DisplayChar(Line5,Column8,(CountWait%10)+0x30);
     }
   }
   else /* Frequency deviation in ppm is more than 121 ppm, hence calibration
           can not be done */
   {
     LCD_Clear(Blue2);
     LCD_DisplayString(Line3,Column1,"Out Of Calibration");
     LCD_DisplayString(Line4,Column4,"Range");
   }
  }
  
  BKP_RTCOutputConfig(BKP_RTCOutputSource_None);
  TIM_ITConfig(TIM2, TIM_IT_CC2, DISABLE);
  TIM_Cmd(TIM2, DISABLE);
  TIM_DeInit(TIM2);
  CalibrationTimer=RTC_GetCounter();
  
  /*  Wait for 2 seconds  */
  while((RTC_GetCounter() - CalibrationTimer) < 5)
  {
  }
  
  MenuInit();
}
/**
  * @brief  RTC Application runs in while loop
  * @param  None
  * @retval : None
  */
void RTC_Application(void)
{
  uint8_t ReturnValue;

  CalculateTime();
  
  if(TimeDateDisplay==0)
  {
    DisplayTime();
  }

  if(DisplayDateFlag==1 && TimeDateDisplay==0)
  {
    DisplayDate();
    DisplayDateFlag=0;
  }

  if(AlarmStatus==1)
  {
    DisplayAlarm();
    GPIO_SetBits(GPIOC, GPIO_Pin_6);
    Dummy=RTC_GetCounter();
    AlarmStatus=2;
  }
  if(((RTC_GetCounter()-Dummy) == 4) && (AlarmStatus==2))
  {
    GPIO_ResetBits(GPIOC, GPIO_Pin_6);
    SelIntExtOnOffConfig(ENABLE);
    RightLeftIntExtOnOffConfig(ENABLE);
    UpDownIntOnOffConfig(ENABLE);
    MenuInit();
    Dummy=0;
    AlarmStatus = 0;
  }

  if(TamperEvent == 1) /* Tamper event is detected */
  {
    TamperNumber = TamperNumber + 1;
    BKP_WriteBackupRegister(BKP_DR5,TamperNumber);
    BKP_WriteBackupRegister(BKP_DR1, CONFIGURATION_DONE);
    BKP_WriteBackupRegister(BKP_DR2,s_DateStructVar.Month);
    BKP_WriteBackupRegister(BKP_DR3,s_DateStructVar.Day);
    BKP_WriteBackupRegister(BKP_DR4,s_DateStructVar.Year);
    BKP_WriteBackupRegister(BKP_DR9,s_AlarmDateStructVar.Day);
    BKP_WriteBackupRegister(BKP_DR8,s_AlarmDateStructVar.Month);
    BKP_WriteBackupRegister(BKP_DR10,s_AlarmDateStructVar.Year);
    BKP_WriteBackupRegister(BKP_DR6,ClockSource);
    BKP_WriteBackupRegister(BKP_DR7,SummerTimeCorrect);
    ReturnValue=EE_WriteVariable(TamperNumber);
    
    if(ReturnValue==PAGE_FULL)
    {
      ReturnValue=EE_WriteVariable(TamperNumber);
    }
    
    ReturnValue=EE_WriteVariable(s_DateStructVar.Day);
    ReturnValue=EE_WriteVariable(s_DateStructVar.Month);
    ReturnValue=EE_WriteVariable((s_DateStructVar.Year)/100);
    ReturnValue=EE_WriteVariable((s_DateStructVar.Year)%100);
    ReturnValue=EE_WriteVariable((s_TimeStructVar.HourHigh*10)+\
                    s_TimeStructVar.HourLow);
    ReturnValue=EE_WriteVariable((s_TimeStructVar.MinHigh*10)+\
                    s_TimeStructVar.MinLow);
    ReturnValue=EE_WriteVariable((s_TimeStructVar.SecHigh*10)+\
                    s_TimeStructVar.SecLow);
    while(!(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_13)))
    {
    }
    
    TamperEvent=0;
    BKP_TamperPinCmd(ENABLE);
  }
  
  if(BatteryRemoved != 0) /* Battery tamper is detected */
  {
    TamperNumber = TamperNumber + 1;
    BKP_WriteBackupRegister(BKP_DR5,TamperNumber);
    ReturnValue=EE_WriteVariable(TamperNumber);
    
    if(ReturnValue==PAGE_FULL)
    {
      ReturnValue=EE_WriteVariable(TamperNumber);
    }
    
    ReturnValue=EE_WriteVariable(s_DateStructVar.Day);
    ReturnValue=EE_WriteVariable(s_DateStructVar.Month);
    ReturnValue=EE_WriteVariable((s_DateStructVar.Year)/100);
    ReturnValue=EE_WriteVariable((s_DateStructVar.Year)%100);
    ReturnValue=EE_WriteVariable((s_TimeStructVar.HourHigh*10)\
                    +s_TimeStructVar.HourLow);
    ReturnValue=EE_WriteVariable((s_TimeStructVar.MinHigh*10)\
                    +s_TimeStructVar.MinLow);
    if(BatteryRemoved==1)
    {
      ReturnValue=EE_WriteVariable(BATTERY_REMOVED);
      BatteryRemoved=0;
    }
    else if(BatteryRemoved==2)
    {
      ReturnValue=EE_WriteVariable(BATTERY_RESTORED);
      BatteryRemoved=0;
    }

    BKP_TamperPinCmd(ENABLE);
  }
}
Exemple #14
0
/**
* @brief  Initialize the IO
* @return   none
*/
static void platform_misc_port_init(void)
{
	GPIO_InitTypeDef	GPIO_InitStructure;
	EXTI_InitTypeDef	EXTI_InitStructure;
	NVIC_InitTypeDef	NVIC_InitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
	PWR_BackupAccessCmd(ENABLE);//允许修改RTC 和后备寄存器
	RCC_LSICmd(DISABLE);//关闭外部低速外部时钟信号功能 后,PC14 PC15 才可以当普通IO用。
	BKP_TamperPinCmd(DISABLE);//关闭入侵检测功能,也就是 PC13,也可以当普通IO 使用

	//USB_CHK -- PC.13
	GPIO_InitStructure.GPIO_Pin				= GPIO_Pin_13;
	GPIO_InitStructure.GPIO_Mode			= GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Speed			= GPIO_Speed_10MHz;
	GPIO_Init(GPIOC, &GPIO_InitStructure);

	/* Connect EXTI Line13 to PC.13 */
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource13);

	EXTI_ClearITPendingBit(EXTI_Line13);
	EXTI_InitStructure.EXTI_Line = EXTI_Line13;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_InitStructure); 
	EXTI_GenerateSWInterrupt(EXTI_Line13);


	//ChargeState detect -- PC.14
	GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_14;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOC, &GPIO_InitStructure);

	/* Connect EXTI Line14 to PC.14 */
	//GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource14);

	//EXTI_ClearITPendingBit(EXTI_Line14);
	//EXTI_InitStructure.EXTI_Line = EXTI_Line14;
	//EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
	//EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
	//EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	//EXTI_Init(&EXTI_InitStructure); 
	//EXTI_GenerateSWInterrupt(EXTI_Line14);

	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);      
	NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQChannel;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 6;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	charge_detect_io_cnt = 0;
	last_charge_detect_io_cnt = 1;

	//LED-Red -- PA.4 	LED-Green -- PA.5	LED-Yellow -- PA.6
	GPIO_InitStructure.GPIO_Pin	= GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	GPIO_SetBits(GPIOA, GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6);

	//Beep -- PB.5  Trig  -- PB.12
	GPIO_InitStructure.GPIO_Pin	= GPIO_Pin_5 | GPIO_Pin_12;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	GPIO_SetBits(GPIOB, GPIO_Pin_12);
	GPIO_ResetBits(GPIOB, GPIO_Pin_5);

	//RFU-IO2 -- PB.8		
	//GPIO_InitStructure.GPIO_Pin	= GPIO_Pin_8;
	//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	//GPIO_Init(GPIOB, &GPIO_InitStructure);
}
Exemple #15
0
void RtcInit(void)
{

	RTC_Configuration();
	
	if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
  {
		/* Backup data register value is not correct or not yet programmed (when
			 the first time the program is executed) */
		printf("\r\n> RTC not yet configured....");
		SetRtcTime();
  }
  else
  {
    /* Check if the Power On Reset flag is set */
    if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
    {
      printf("\r\n> Power On Reset occurred....");
    }
    /* Check if the Pin Reset flag is set */
    else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
    {
      printf("\r\n> External Reset occurred....");
    }

    printf("\r\n> No need to configure RTC....");
    /* Wait for RTC registers synchronization */
    RTC_WaitForSynchro();

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

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

#ifdef RTCClockOutput_Enable
  /* Enable PWR and BKP clocks */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);

  /* Allow access to BKP Domain */
  PWR_BackupAccessCmd(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_RTCOutputConfig(BKP_RTCOutputSource_CalibClock);
#endif

  /* Clear reset flags */
  RCC_ClearFlag();
	
	// Rx8025 对时
	time_set.tm_year	= 2000+ExRtcTime.year;
	time_set.tm_mon		= ExRtcTime.mon;
	time_set.tm_mday	= ExRtcTime.day;
	time_set.tm_hour	= ExRtcTime.hour;
	time_set.tm_min 	= ExRtcTime.min;
	time_set.tm_sec 	= ExRtcTime.sec;

	Time_SetCalendarTime(time_set);	
	
	
	
	
	
}
/*
 * 函数名:RTC_CheckAndConfig
 * 描述  :检查并配置RTC
 * 输入  :用于读取RTC时间的结构体指针
 * 输出  :无
 * 调用  :外部调用
 */
void RTC_CheckAndConfig(struct rtc_time *tm)
{
   	  /*在启动时检查备份寄存器BKP_DR1,如果内容不是0xA5A5,
	  则需重新配置时间并询问用户调整时间*/
	if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
	{
		printf("\r\n\r\n RTC not yet configured....");

		/* RTC Configuration */
		RTC_Configuration();
		
		printf("\r\n\r\n RTC configured....");

		/* Adjust time by users typed on the hyperterminal */
		Time_Adjust(tm);

		BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
	}
	else
	{
	  /*启动无需设置新时钟*/
		/*检查是否掉电重启*/
		if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
		{
		    printf("\r\n\r\n Power On Reset occurred....");
		}
		/*检查是否Reset复位*/
		else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
		{
			printf("\r\n\r\n External Reset occurred....");
		}
	
		printf("\r\n No need to configure RTC....");
		
		/*等待寄存器同步*/
		RTC_WaitForSynchro();
		
		/*允许RTC秒中断*/
		RTC_ITConfig(RTC_IT_SEC, ENABLE);
		
		/*等待上次RTC寄存器写操作完成*/
		RTC_WaitForLastTask();
	}
	   /*定义了时钟输出宏,则配置校正时钟输出到PC13*/
	#ifdef RTCClockOutput_Enable
	  /* Enable PWR and BKP clocks */
	  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
	
	  /* Allow access to BKP Domain */
	  PWR_BackupAccessCmd(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_RTCOutputConfig(BKP_RTCOutputSource_CalibClock);
	#endif
	
	  /* Clear reset flags */
	  RCC_ClearFlag();

}
Exemple #17
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_stm32f10x_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f10x.c file
     */     
       
  /* NVIC configuration */
  NVIC_Configuration();
    
  /* Initialize Leds mounted on STM3210X-EVAL board */
  STM_EVAL_LEDInit(LED1);
  STM_EVAL_LEDInit(LED2);
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED4);

  /* Enable PWR and BKP clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);

  /* Enable write access to Backup domain */
  PWR_BackupAccessCmd(ENABLE);

  /* Disable Tamper pin */
  BKP_TamperPinCmd(DISABLE);
  
  /* Disable Tamper interrupt */
  BKP_ITConfig(DISABLE);

  /* Tamper pin active on low level */
  BKP_TamperPinLevelConfig(BKP_TamperPinLevel_Low);

  /* Clear Tamper pin Event(TE) pending flag */
  BKP_ClearFlag();

  /* Enable Tamper interrupt */
  BKP_ITConfig(ENABLE);

  /* Enable Tamper pin */
  BKP_TamperPinCmd(ENABLE);
   
  /* Write data to Backup DRx registers */
  WriteToBackupReg(0xA53C);
  
  /* Check if the written data are correct */
  if(CheckBackupReg(0xA53C) == 0x00)
  {
    /* Turn on LED1 */
    STM_EVAL_LEDOn(LED1);
  }
  else
  {
    /* Turn on LED2 */
    STM_EVAL_LEDOn(LED2);
  }
        
  while (1)
  {    
  }
}
Exemple #18
0
/*******************************************************************************
* Function Name  : RTC_Config
* Description    : 上电时调用本函数,自动检查是否需要RTC初始化, 
* 					若需要重新初始化RTC,则调用RTC_Configuration()完成相应操作
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void RTC_Config(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;

   //启用PWR和BKP的时钟(from APB1)
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);

	//后备域解锁
	PWR_BackupAccessCmd(ENABLE);   //此要加,否则重启后无法更改时间

	//我们在BKP的后备寄存器1中,存了一个特殊字符0xA5A5
	//第一次上电或后备电源掉电后,该寄存器数据丢失,
	//表明RTC数据丢失,需要重新配置
	if (BKP_ReadBackupRegister(BKP_DR1) != 0xEDED)
	{
		//重新配置RTC
		RTC_Configuration();
		//配置完成后,向后备寄存器中写特殊字符0xA5A5
		BKP_WriteBackupRegister(BKP_DR1, 0xEDED);
		// Lcd_WriteString(0,1,0,Red,"ver 1.0");
	}
	else
	{
		//若后备寄存器没有掉电,则无需重新配置RTC
		//这里我们可以利用RCC_GetFlagStatus()函数查看本次复位类型
		if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
		{
			//Lcd_WriteString(0,3,0,Red,"BATTER OFF");//这是上电复位
		}
		else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
		{
		//	Lcd_WriteString(0,3,0,Red,"RESET");//这是外部RST管脚复位
		}
		//清除RCC中复位标志
		RCC_ClearFlag();

		//虽然RTC模块不需要重新配置,且掉电后依靠后备电池依然运行
		//但是每次上电后,还是要使能RTCCLK???????
		//RCC_RTCCLKCmd(ENABLE);
		//等待RTC时钟与APB1时钟同步
		//RTC_WaitForSynchro();

		//使能秒中断
		RTC_ITConfig(RTC_IT_SEC, ENABLE);
	//	RTC_ITConfig(RTC_IT_OW, ENABLE);
		//等待操作完成
		RTC_WaitForLastTask();
	}

#ifdef RTCClockOutput_Enable
	


	/* Allow access to BKP Domain */
	PWR_BackupAccessCmd(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_RTCOutputConfig(BKP_RTCOutputSource_CalibClock);
#endif

  #ifdef  VECT_TAB_RAM
	NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
  #else  /* VECT_TAB_FLASH  */
	NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
  #endif

  
  /* Configure one bit for preemption priority */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  
  /* Enable the RTC Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQChannel;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

	return;
}
/* Initialisation */
void nrfInit(void)
{
  SPI_InitTypeDef  SPI_InitStructure;
  EXTI_InitTypeDef EXTI_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;

  if (isInit==TRUE)
    return;

  /* Enable the EXTI interrupt router */
  extiInit();

  /* Enable SPI and GPIO clocks */
  RCC_APB2PeriphClockCmd(RADIO_SPI_CLK | RADIO_GPIO_SPI_CLK | RADIO_GPIO_CS_PERIF | 
                         RADIO_GPIO_CE_PERIF | RADIO_GPIO_IRQ_PERIF, ENABLE);
   
  PWR_BackupAccessCmd(ENABLE);
        
  RCC_LSEConfig(RCC_LSE_OFF);
  
  BKP_TamperPinCmd(DISABLE);   
  
  PWR_BackupAccessCmd(DISABLE);
		
  
  /* Configure SPI pins: SCK, MISO and MOSI */
  GPIO_InitStructure.GPIO_Pin = RADIO_GPIO_SPI_SCK |  RADIO_GPIO_SPI_MOSI | RADIO_GPIO_SPI_MISO;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(RADIO_GPIO_SPI_PORT, &GPIO_InitStructure);

  /* Configure I/O for the Chip select */
  GPIO_InitStructure.GPIO_Pin = RADIO_GPIO_CS;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(RADIO_GPIO_CS_PORT, &GPIO_InitStructure);

  /* Configure the interruption (EXTI Source) */
  GPIO_InitStructure.GPIO_Pin = RADIO_GPIO_IRQ;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(RADIO_GPIO_IRQ_PORT, &GPIO_InitStructure);

  GPIO_EXTILineConfig(RADIO_GPIO_IRQ_SRC_PORT, RADIO_GPIO_IRQ_SRC);
  EXTI_InitStructure.EXTI_Line = RADIO_GPIO_IRQ_LINE;
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  EXTI_Init(&EXTI_InitStructure);


  /* disable the chip select */
  RADIO_DIS_CS();

  /* Configure I/O for the Chip Enable */
  GPIO_InitStructure.GPIO_Pin = RADIO_GPIO_CE;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(RADIO_GPIO_CE_PORT, &GPIO_InitStructure);

  /* disable the chip enable */
  RADIO_DIS_CE();

  /* SPI configuration */
  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  SPI_InitStructure.SPI_CRCPolynomial = 7;
  SPI_Init(RADIO_SPI, &SPI_InitStructure);

  /* Enable the SPI  */
  SPI_Cmd(RADIO_SPI, ENABLE);
  
  isInit = TRUE;
}
Exemple #20
0
/*******************************************************************************
* Function Name  : PVD_IRQHandler
* Description    : This function handles PVD interrupt request.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void PVD_IRQHandler(void)
{
  BKP_TamperPinCmd(DISABLE);
  //EXTI_ClearITPendingBit(EXTI_Line16);
  //NVIC_ClearIRQChannelPendingBit(PVD_IRQn);
}
Exemple #21
0
/**
  * @brief  Setting up the time by Serial USART1.
  * @param  None
  * @retval None
  */
void RTC_SetTimeBySerial(void) {
  if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5) {
    /* Backup data register value is not correct or not yet programmed (when
       the first time the program is executed) */

    printf("\r\n * Begin RTC initialization");
    
    /* RTC Configuration */
    RTC_Configuration();
    
    printf("\r\n * Please set calendar");
    
    /* setup year, month, date in 4, 2, 2 digits each */
    Date_Regulate();
    
    printf("\r\n * Please set time");

    /* Adjust time by values entered by the user on the hyperterminal */
    Time_Adjust();
    
    BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
  } else {
    /* Check if the Power On Reset flag is set */
    if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET) {
      printf("\r\n * Power On Reset occurred....");
    }
    /* Check if the Pin Reset flag is set */  
    else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET) {
      printf("\r\n\n * External Reset occurred....");
    }

    printf("\r\n * No need to configure RTC....");
    
    /* Check whether we've written year or month or day value before */
    if(BKP_ReadBackupRegister(BKP_DR2) == 0x0000 || BKP_ReadBackupRegister(BKP_DR3) == 0x0000) {
      /* setup year, month, date in 4, 2, 2 digits each */
      Date_Regulate();
    } else {
      
      uint16_t YY, MD;
      YY = BKP_ReadBackupRegister(BKP_DR2);
      MD = BKP_ReadBackupRegister(BKP_DR3);
      
      int month, day;
      if( (MD / 1000) % 10 == 0) {
        month = (MD / 100) % 10;
      } else {
        month = (MD / 1000) % 10 + (MD / 100) % 10 ;
      }
      
      if( (MD / 10) % 10 == 0 ) {
        day = MD % 10;
      } else {
        day = MD - (MD / 100) * 100;
      }
      
      printf("\r\n\n Previous written calendar data found !");
      printf("\r\n Written values are as follows :");
      printf("\r\n Year : %d, Month : %d, Day : %d", YY, month, day);
      printf("\r\n Above calendar datas will be used to set current calendar automatically\r\n");
      
      TranslateIntoYear(YY);
      TranslateIntoMonth(month);
      TranslateIntoDay(day);
    }
        
    /* NVIC MUST BE CONFIGURED before branch into power on reset */
    NVIC_InitTypeDef NVIC_InitStructure;
    EXTI_InitTypeDef EXTI_InitStructure;
  
    /* Configure one bit for preemption priority */
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  
    /* Enable the RTC Interrupt */
    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);
    
    NVIC_InitStructure.NVIC_IRQChannel = RTCAlarm_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0xFF;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    
    /* Configure EXTI Line17 (RTC Alarm)to generate an interrupt on rising edge */
    EXTI_ClearITPendingBit(EXTI_Line17);
    EXTI_InitStructure.EXTI_Line = EXTI_Line17;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
    EXTI_Init(&EXTI_InitStructure);
  
    /* Wait for RTC registers synchronization */
    RTC_WaitForSynchro();
    
    /* Wait until last write operation on RTC registers has finished */
    RTC_WaitForLastTask();
    
    /* Alarm in 3 second */
    //RTC_SetAlarm(3);
    
    /* Wait until last write operation on RTC registers has finished */
    RTC_WaitForLastTask();
    
    /* Enable the RTC Second, RTC Alarm interrupt */
    RTC_ITConfig(RTC_IT_SEC || RTC_IT_ALR, ENABLE);
    
    /* Wait until last write operation on RTC registers has finished */
    RTC_WaitForLastTask();
  }

#ifdef RTCClockOutput_Enable
  /* Enable PWR and BKP clocks */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);

  /* Allow access to BKP Domain */
  PWR_BackupAccessCmd(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_RTCOutputConfig(BKP_RTCOutputSource_Second);
#endif
  
  /* Clear reset flags */
  RCC_ClearFlag();
}
Exemple #22
0
/*******************************************************************************
* Function Name  : AutoClockCalibration
* Description    : Calibration of External crystal oscillator auto(through Timer
                   peripheral)
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void AutoClockCalibration(void)
{
  RCC_ClocksTypeDef ClockValue;
  u16 u16_TimerPrescalerValue=0x0003;
  u16 u16_CountWait;
  u16 u16_DeviationInteger;
  u32 u32_CalibrationTimer;
  float f32_Deviation;
  TIM_ICInitTypeDef  TIM_ICInitStructure;
  
  TIM_DeInit(TIM2);
  BKP_TamperPinCmd(DISABLE);
  BKP_RTCOutputConfig(BKP_RTCOutputSource_CalibClock);
  
  /* TIM2 configuration: PWM Input mode ------------------------
     The external signal is connected to TIM2 CH2 pin (PA.01),
     The Rising edge is used as active edge,
     The TIM2 CCR2 is used to compute the frequency value
     The TIM2 CCR1 is used to compute the duty cycle value
  ------------------------------------------------------------ */
  TIM_PrescalerConfig(TIM2,u16_TimerPrescalerValue,TIM_PSCReloadMode_Immediate);
  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
  TIM_ICInitStructure.TIM_ICFilter = 0x00;
  TIM_PWMIConfig(TIM2, &TIM_ICInitStructure);
  TIM_ICInit(TIM2, &TIM_ICInitStructure);
  /* Select the TIM2 Input Trigger: TI2FP2 */
  TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);
  /* Select the slave Mode: Reset Mode */
  TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);
  /* Enable the Master/Slave Mode */
  TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);
  /* TIM enable u16_Counter */
  TIM_Cmd(TIM2, ENABLE);

  /* Wait for 2 seconds */
  u32_CalibrationTimer = RTC_GetCounter();
 
  while((RTC_GetCounter() - u32_CalibrationTimer) < 2)
  {
  }
  
  RCC_GetClocksFreq(&ClockValue);
  u32_TimerFrequency=(ClockValue.PCLK1_Frequency * 2)/(u16_TimerPrescalerValue+1);
   /* Enable the CC2 Interrupt Request */
  TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);
   /* Wait for 2 seconds */
  u32_CalibrationTimer = RTC_GetCounter();
  
  while((RTC_GetCounter() - u32_CalibrationTimer) < 2)
  {
  }

  if(!(TIM_GetFlagStatus(TIM2, TIM_FLAG_CC1)))
   /* There is no signal at the timer TIM2 peripheral input */
  {

  }
  else
  {
    /* Calulate Deviation in ppm  using the formula :
    Deviation in ppm = (Deviation from 511.968/511.968)*1 million*/
    if(f32_Frequency > 511.968)
    {
      f32_Deviation=((f32_Frequency-511.968)/511.968)*1000000;
    }
    else
    {
      f32_Deviation=((511.968-f32_Frequency)/511.968)*1000000;
    }
     u16_DeviationInteger = (u16)f32_Deviation;
    
    if(f32_Deviation >= (u16_DeviationInteger + 0.5))
    {
      u16_DeviationInteger = ((u16)f32_Deviation)+1;
    }
    
   u16_CountWait=0;

   /* Frequency deviation in ppm should be les than equal to 121 ppm*/
   if(u16_DeviationInteger <= 121)
   {
     while(u16_CountWait<128)
     {
       if(u8_CalibrationPpm[u16_CountWait] == u16_DeviationInteger)
       break;
       u16_CountWait++;
     }

     BKP_SetRTCCalibrationValue(u16_CountWait);

     u16_CountWait=u16_CountWait/10;

     u16_CountWait=u16_CountWait/10;
   
     if(u16_CountWait>0)
     {

     }
   }
   else /* Frequency deviation in ppm is more than 121 ppm, hence calibration
           can not be done */
   {

   }
  }
  
  BKP_RTCOutputConfig(BKP_RTCOutputSource_None);
  TIM_ITConfig(TIM2, TIM_IT_CC2, DISABLE);
  TIM_Cmd(TIM2, DISABLE);
  TIM_DeInit(TIM2);
  u32_CalibrationTimer=RTC_GetCounter();
  
  /*  Wait for 2 seconds  */
  while((RTC_GetCounter() - u32_CalibrationTimer) < 5)
  {
  }
  
  //MenuInit();
}