Beispiel #1
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();
}
Beispiel #2
0
void RTC_Config(void)	
{
	/*后备寄存器1中,存了一个特殊字符0xA5A5
	第一次上电或后备电源掉电后,该寄存器数据丢失,
	表明RTC数据丢失,需要重新配置 */
    if(BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5) //检查是否第一次上电或后备电池已经掉电,
    {       
        Write_Log("Backup VBAT PowerDown or First time PowerUp,Initialize RTC\r\n");
        RTC_Configuration();
        BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
        
        time_now.tm_year = 2011;
        time_now.tm_mon = 10; //月份表示为0~11
        time_now.tm_mday = 13;
        time_now.tm_hour = 13;
        time_now.tm_min = 16;
        time_now.tm_sec = 38;
        Time_SetCalendarTime(time_now);//设置初始时间
    } 
    else //若后备寄存器没有掉电,则无需重新配置RTC
    {
        Write_Log("Backup VBAT Keep, Don't RTC Configuralation\r\n");
                    //等待RTC与APB同步
  		RTC_WaitForSynchro();
		RTC_WaitForLastTask();
	
  		//使能秒中断 
  		RTC_ITConfig(RTC_IT_SEC, ENABLE);  
  		RTC_WaitForLastTask();
    }
      //这里我们可以利用RCC_GetFlagStatus()函数查看本次复位类型
    if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
    {
		por_rst_flag = 1;	
        Write_Log("PowerUp Reset\r\n");
    }
    else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
    {
		pin_rst_flag = 1;
        Write_Log("pin Reset\r\n");
    }
    else if(PWR_GetFlagStatus(PWR_FLAG_WU)!= RESET)  //wakeup唤醒
    {
        Write_Log("WakeUp...\r\n");     
    }
    if(PWR_GetFlagStatus(PWR_FLAG_SB) != RESET) //检查是否由待机模式下唤醒,如是则不需要配置RTC
        /* System resumed from STANDBY mode */      
         /* Clear StandBy flag */
    PWR_ClearFlag(PWR_FLAG_SB);

        //清除RCC中复位标志
    RCC_ClearFlag();
	return;
}
Beispiel #3
0
 /**
  * @file   Time_Regulate
  * @brief  RTC校准
  * @param  无
  * @retval 无
  */
void Time_Regulate(void)
{
  struct tm time;
  memset(&time, 0 , sizeof(time) );	/* 清空结构体 */
  printf("=======================Time Settings==========================\r\n");
  printf("Please Set Years between 1970 to 2037\r\n");
	
  while ( time.tm_year>2037 || time.tm_year<1970)
  {   
    time.tm_year = USART_Scanf(1970,2037,4);
  }
  printf("Set Years:  %d\r\n", time.tm_year);

  printf("Please Set Months between 01 to 12\r\n");
  while (time.tm_mon >12 || time.tm_mon < 1 )
  {
    time.tm_mon= USART_Scanf(1,12,2)-1;
  }
  printf("Set Months:  %d\r\n", time.tm_mon);

  printf("Please Set Days between 01 to 31\r\n");
  while (time.tm_mday >31 ||time.tm_mday <1 )
  {
    time.tm_mday = USART_Scanf(1,31,2);
  }
  printf("Set Days:  %d\r\n", time.tm_mday);

  printf("Please Set Hours between 01 to 23\r\n");
  while (time.tm_hour >23 ||time.tm_hour <1 )
  {
    time.tm_hour = USART_Scanf(1,23,2);
  }
  printf("Set Hours:  %d\r\n", time.tm_hour);

  printf("Please Set Minutes between 01 to 59\r\n");
  while (time.tm_min >59 || time.tm_min <1 )
  {
    time.tm_min = USART_Scanf(1,59,2);
  }
  printf("Set Minutes:  %d\r\n", time.tm_min);

  printf("Please Set Seconds between 01 to 59\r\n");
  while (time.tm_sec >59 || time.tm_sec <1 )
  {
    time.tm_sec = USART_Scanf(1,59,2);
  }
  printf("Set Seconds:  %d\r\n", time.tm_sec);
  /* Return the value to store in RTC counter register */
  Time_SetCalendarTime(time);  
}
Beispiel #4
0
 /**
  * @file   Time_Init
  * @brief  Printf Time
  * @param  无
  * @retval 无
  */
void Time_Init(void)
{
    struct tm time;
    memset(&time, 0 , sizeof(time) );	/* 清空结构体 */
    PWR_BackupAccessCmd(ENABLE);	
    RTC_WaitForLastTask(); 
    time.tm_year=2070;
    time.tm_mon=00;
    time.tm_mday=00;
    time.tm_hour=0;
    time.tm_min=0;
    time.tm_sec=0;
    Time_SetCalendarTime(time); 
    RTC_WaitForLastTask();
    PWR_BackupAccessCmd(DISABLE);	
    printf("=======================Time Settings==========================\r\n");
	 
}
Beispiel #5
0
uint32_t ShellEtrDate(char *entry,char *b,uint8_t len)
{
  struct tm t;
  char *p;
  p=entry;
  
  memset(&t, 0, sizeof(t));//tyh:20130308 增加初始化为'0', 避免时间计算出错
  
  t.tm_year =atoi(p);
  p=strpbrk(p,"-")+1;
  t.tm_mon =atoi(p)/*-1*/;//tyh:20130308 去除"-1", 交由 Time_SetCalendarTime 统一处理
  p=strpbrk(p,"-")+1;
  t.tm_mday =atoi(p);
  p=strpbrk(p," ")+1;
  t.tm_hour=atoi(p);
  p=strpbrk(p,"-")+1;
  t.tm_min =atoi(p);
  p=strpbrk(p,"-")+1;
  t.tm_sec =atoi(p);
  if((t.tm_mon>11)||(t.tm_mday>31)||(t.tm_hour>23)||(t.tm_min>59)||(t.tm_sec>59))
  { 
#if LINGUA==EN
    sprintf(b, "Enter Time Fault\r\n");
    p=strlen("Enter Time Fault\r\n")+b;
#endif
#if LINGUA==CH
    sprintf(b, "时间范围错误\r\n");
    p=strlen("时间范围错误\r\n")+b;
#endif
    return (p-b);   
  }
  
  Time_SetCalendarTime(t);
  Time_Calib();
  sprintf(b, "\r\n");
  
  return 2;
}
Beispiel #6
0
/*******************************************************************************
* Function Name  : RTC_Configuration
* Description    : 来重新配置RTC和BKP,仅在检测到后备寄存器数据丢失时使用
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void RTC_Configuration(void)
{
	//启用PWR和BKP的时钟(from APB1)
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);

	//后备域解锁
	PWR_BackupAccessCmd(ENABLE);

	//备份寄存器模块复位
	BKP_DeInit();


	//外部32.768K其哟偶那个
	RCC_LSEConfig(RCC_LSE_ON);
		
	//等待稳定
	while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);
//	Lcd_WriteString(10,0,0,Red,"00");

	//RTC时钟源配置成LSE(外部32.768K)
	RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);

	//RTC开启
	RCC_RTCCLKCmd(ENABLE);

	//开启后需要等待APB1时钟与RTC时钟同步,才能读写寄存器
	RTC_WaitForSynchro();

	//读写寄存器前,要确定上一个操作已经结束
	RTC_WaitForLastTask();

		//使能秒中断
	RTC_ITConfig(RTC_IT_SEC, ENABLE); 

	//等待写入完成
	RTC_WaitForLastTask();

	//设置RTC分频器,使RTC时钟为1Hz
	//RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1)
	RTC_SetPrescaler(32767);


	//等待寄存器写入完成
	RTC_WaitForLastTask(); 




		    time_now.tm_year = 2009;
		    time_now.tm_mon = 7;
			time_now.tm_mday = 31;
			time_now.tm_hour = 13;
			time_now.tm_min = 11;
			time_now.tm_sec = 00;

			Time_SetCalendarTime(time_now);	 



	return;




}
Beispiel #7
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);	
	
	
	
	
	
}
err_t tcp_client_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
{
	struct pbuf *q;
	struct tcp_client_app_arg *appcmd = (struct tcp_client_app_arg *)arg;
	uint16_t i = 0;
	uint8_t ip[4];
	char* str;
	struct tm mytime;

	const char* cmd1 = "hello";
	const char* cmd2 = "echo name";
	const char* cmd3 = "echo log";
	const char* cmd4 = "echo IP";
	const char* cmd5 = "bye";
	const char* cmd6 = "time calibration"; //命令格式:time calibration:YY MM DD HH MM SS

	//printf("tcp client recv\n");
	print_client_pcb_state(pcb);
		
	/* We perform here any necessary processing on the pbuf */
	if (p != NULL) 
	{        
		/* We call this function to tell the LwIp that we have processed the data */
		/* This lets the stack advertise a larger window, so more data can be received*/
		tcp_recved(pcb, p->tot_len);
		
		/* Check the name if NULL, no data passed, return withh illegal argument error */
		if(appcmd == NULL) 
		{
		  pbuf_free(p);
		  return ERR_ARG;
		}
		/*
		for(q=p; q != NULL; q = q->next) 
		{
			
			c = q->payload;
			for(i=0; i<q->len && !done; i++) 
			{
				done = ((c[i] == '\r') || (c[i] == '\n'));
				if(name->length < MAX_NAME_SIZE) 
				{
				  name->bytes[name->length++] = c[i];
				}
			}
			
		}
		*/
		appcmd->dataptr = (char*)mem_calloc(sizeof(uint8_t), p->tot_len);
		if(appcmd->dataptr == NULL){
			printf("Memory error\n");
			return ERR_MEM;
		}
		appcmd->textlen = p->tot_len;
		for(q=p; q!=NULL; q=q->next){
			memcpy((uint8_t*)&appcmd->dataptr[i], q->payload, q->len);  //同一个数据包会存在一个pbuf链表中
			i = i + q->len;
		}
		//应用层代码
		switch(appcmd->app_state){
			case CLIENT_CONNECTED:
			case CLIENT_WAITING_FOR_CMD: {
				if(memcmp(appcmd->dataptr, cmd1, strlen(cmd1)) == 0){
					str = "hello\n";
					//tcp_write(pcb, (uint8_t*)str, strlen(str), 1);
				}
				else if(memcmp(appcmd->dataptr, cmd2, strlen(cmd2)) == 0){
					str = "lwip\n";
				}
				else if(memcmp(appcmd->dataptr, cmd3, strlen(cmd3)) == 0){
					str = "log is ...\n";
				}
				else if(memcmp(appcmd->dataptr, cmd4, strlen(cmd4)) == 0){
					ip[0] = pcb->remote_ip.addr>>24;
					ip[1] = pcb->remote_ip.addr>>16;
					ip[2] = pcb->remote_ip.addr>>8;
					ip[3] = pcb->remote_ip.addr;
					str = mem_calloc(sizeof(uint8_t), 30);
					sprintf((char*)str, "ipaddr:%d,%d,%d,%d\n", ip[3], ip[2], ip[1], ip[0]);
					tcp_write(pcb, (uint8_t *)str, strlen(str), 1);
					mem_free(str);
					break;
				}
				else if(memcmp(appcmd->dataptr, cmd5, strlen(cmd5)) == 0){
					appcmd->app_state = CLIENT_CLOSE;
					//goto get_close;
					break;
				}
				else if(memcmp(appcmd->dataptr, cmd6, strlen(cmd6)) == 0){
					sscanf(appcmd->dataptr, "%*[^:]:%d%d%d%d%d%d", &mytime.tm_year, &mytime.tm_mon, &mytime.tm_mday, &mytime.tm_hour, &mytime.tm_min, &mytime.tm_sec);
					if(mytime.tm_year<100&&mytime.tm_mon-1<12&&mytime.tm_mday<32&&mytime.tm_hour<24&&mytime.tm_min<60&&mytime.tm_sec<60&&mytime.tm_mon>0){
						str = "time calibration success\n";
						DS1302_SetSec(mytime.tm_sec, DISABLE);
						DS1302_SetMin(mytime.tm_min);
						DS1302_SetHour(mytime.tm_hour, DISABLE);
						DS1302_SetDate(mytime.tm_mday);
						DS1302_SetMon(mytime.tm_mon-1);
						DS1302_SetYear(mytime.tm_year);
						
						mytime.tm_year += 2000; 
						mytime.tm_mon --;
						RTC_WaitForLastTask();
						RTC_ITConfig(RTC_IT_SEC, DISABLE);  
						Time_SetCalendarTime(mytime);  
						RTC_ITConfig(RTC_IT_SEC, ENABLE);

						time_update_status = DS1302_SYN_SUL;
						  
						appcmd->app_state = CLIENT_CLOSE;
					}
					else{
						str = "time calibratioin failed\n";
						time_update_status = DS1302_SYN_FAIL;
					}
					tcp_write(pcb, (uint8_t *)str, strlen(str), 1);
					break;
				}
				else{
					str = "unknown cmd\n";
					//tcp_write(pcb, (uint8_t*)str, strlen(str), 1);
				}
				tcp_write(pcb, (uint8_t *)str, strlen(str), 1);
				break;
			}