void rtc_set(uint32_t val) { PWR_BackupAccessCmd(ENABLE); RTC_SetCounter(val); RTC_WaitForLastTask(); PWR_BackupAccessCmd(DISABLE); }
/******************************************************************************* * Function Name : SetTime * Description : Sets the RTC Current Counter Value * Input : Hour, Minute and Seconds data * Output : None * Return : None *******************************************************************************/ void SetTime(u8 u8_Hour,u8 u8_Minute,u8 u8_Seconds) { u32 u32_CounterValue; u32_CounterValue=((u8_Hour * 3600)+ (u8_Minute * 60)+u8_Seconds); RTC_WaitForLastTask(); RTC_SetCounter(u32_CounterValue); RTC_WaitForLastTask(); }
/** * @brief Adjusts time. * @param None * @retval None */ void Time_Adjust(void) { /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Change the current time */ RTC_SetCounter(Time_Regulate()); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); }
/******************************************************************************* * Function Name : my_RTC_SetCounter * Description : sets the hardware-counter * Input : new counter-value * Output : None * Return : None *******************************************************************************/ static void my_RTC_SetCounter(uint32_t cnt) { /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Change the current time */ RTC_SetCounter(cnt); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); }
void setRtcTime(time_t current) { PWR_BackupAccessCmd(ENABLE); RTC_WaitForLastTask(); RTC_SetCounter(current); RTC_WaitForLastTask(); PWR_BackupAccessCmd(DISABLE); RTC_WaitForLastTask(); gSecondFlag = 1; }
void rtc_timer_start(u32 alarmValue) { //enable BKP and PWR, Clock RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP|RCC_APB1Periph_PWR , ENABLE); // RTC clock source configuration PWR_BackupAccessCmd(ENABLE); //Allow access to BKP Domain RCC_LSEConfig(RCC_LSE_ON); //Enable LSE OSC while(RCC_GetFlagStatus(RCC_FLAG_LSERDY)==RESET); //Wait till LSE is ready RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); //Select the RTC Clock Source RCC_RTCCLKCmd(ENABLE); //enable RTC // RTC configuration // Wait for RTC APB registers synchronisation RTC_WaitForSynchro(); RTC_SetPrescaler(0); //Set the RTC time base to 30.5us RTC_WaitForLastTask(); //Wait until last write operation on RTC registers has finished //Set the RTC time counter to 0 RTC_SetCounter(0); RTC_WaitForLastTask(); // Set the RTC time alarm(the length of slot) RTC_SetAlarm(alarmValue); RTC_WaitForLastTask(); //interrupt when reach alarm value RTC_ClearFlag(RTC_IT_ALR); RTC_ITConfig(RTC_IT_ALR, ENABLE); //Configures EXTI line 17 to generate an interrupt on rising edge(alarm interrupt to wakeup board) EXTI_ClearITPendingBit(EXTI_Line17); EXTI_InitTypeDef EXTI_InitStructure; 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); //Configure RTC global interrupt: //Configure NVIC: Preemption Priority = 1 and Sub Priority = 1 NVIC_InitTypeDef NVIC_InitStructure; 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); //Configure RTC Alarm interrupt: //Configure NVIC: Preemption Priority = 0 and Sub Priority = 1 NVIC_InitStructure.NVIC_IRQChannel = RTCAlarm_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
uint08 RTC_Set_Stamp(uint32 stamp) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //使能PWR和BKP外设时钟 PWR_BackupAccessCmd(ENABLE); //使能RTC和后备寄存器访问 RTC_SetCounter(stamp); //设置RTC计数器的值 RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成 return 0; }
/* ************************************************************ * 函数名称: RTC_AlarmReSet * * 函数功能: RTC闹钟重设 * * 入口参数: sec:秒值 * * 返回参数: 无 * * 说明: 当RTC闹钟中断产生后,需要重设一下,不然就停止了 ************************************************************ */ void RTC_AlarmReSet(unsigned int sec) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //使能PWR和BKP外设时钟 PWR_BackupAccessCmd(ENABLE); //使能后备寄存器访问 RTC_SetCounter(0); //设置RTC计数器的值 RTC_AlarmSet(sec); }
/** * @brief Sets the RTC Current Counter Value * @param Hour, Minute and Seconds data * @retval : None */ void SetTime(uint8_t Hour,uint8_t Minute,uint8_t Seconds) { uint32_t CounterValue; CounterValue=((Hour * 3600)+ (Minute * 60)+Seconds); RTC_WaitForLastTask(); RTC_SetCounter(CounterValue); RTC_WaitForLastTask(); }
/* ************************************************************ * 函数名称: RTC_SetTime * * 函数功能: RTC时间设置 * * 入口参数: sec:秒值 * * 返回参数: 无 * * 说明: ************************************************************ */ void RTC_SetTime(unsigned int sec) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //使能PWR和BKP外设时钟 PWR_BackupAccessCmd(ENABLE); //使能RTC和后备寄存器访问 RTC_SetCounter(sec); //设置RTC计数器的值 RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成 }
/* * 函数名:Time_Adjust * 描述 :时间调节 * 输入 :无 * 输出 :无 * 调用 :主函数调用 */ void Time_Adjust(void) { /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Change the current time * 将设置的初始时间值装入RTC计数器,RTC开始运行时,计数器里面的值 * 会在初始值的基础上自动一秒加1次 */ RTC_SetCounter(Time_Regulate()); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); }
/******************************************************************************* * Function Name : Time_SetUnixTime() * Description : 将给定的Unix时间戳写入RTC * Input : time_t t * Output : None * Return : None *******************************************************************************/ void Time_SetUnixTime(time_t t) { //GPIOB->ODR ^= (1<<15); RTC_WaitForLastTask(); RTC_SetCounter((u32)t); GPIOB->ODR ^= (1<<13); RTC_WaitForLastTask(); return; }
/* ************************************************************ * 函数名称: RTC_Init * * 函数功能: RTC初始化 * * 入口参数: 无 * * 返回参数: 无 * * 说明: ************************************************************ */ _Bool RTC_Init(void) { #if(USE_EXT_RCC == 1) unsigned char errCount = 0; #endif RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //使能PWR和BKP外设时钟 PWR_BackupAccessCmd(ENABLE); //使能后备寄存器访问 BKP_DeInit(); //复位备份区域 #if(USE_EXT_RCC == 1) RCC_LSEConfig(RCC_LSE_ON); //设置外部低速晶振(LSE),使用外设低速晶振 while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET && errCount < 250) //检查指定的RCC标志位设置与否,等待低速晶振就绪 { errCount++; DelayMs(10); } if(errCount >= 250) return 1; //初始化时钟失败,晶振有问题 #endif #if(USE_EXT_RCC == 1) RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); //设置RTC时钟(RTCCLK),选择LSE作为RTC时钟 #else RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128); //设置RTC时钟(HSE/128),选择HES作为RTC时钟 #endif RCC_RTCCLKCmd(ENABLE); //使能RTC时钟 RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成 RTC_WaitForSynchro(); //等待RTC寄存器同步 RTC_ITConfig(RTC_IT_ALR, ENABLE); //使能RTC闹钟中断 RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成 RTC_EnterConfigMode(); //允许配置 #if(USE_EXT_RCC == 1) RTC_SetPrescaler(32767); //设置RTC预分频的值 #else RTC_SetPrescaler(HSE_VALUE / 128 - 1); //设置RTC预分频的值 #endif RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成 RTC_SetCounter(0); //设置RTC计数器的值 RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成 RTC_ExitConfigMode(); //退出配置模式 RTC_NVIC_Init(); return 0; }
/******************************************************************************* * Function Name : rtc_settime * Description : sets HW-RTC with values from time-struct, takes DST into * account, HW-RTC always running in non-DST time * Input : None * Output : None * Return : not used *******************************************************************************/ bool rtc_settime (const RTC_t *rtc) { uint32_t cnt; RTC_t ts; cnt = struct_to_counter( rtc ); // non-DST counter-value counter_to_struct( cnt, &ts ); // normalize struct (for weekday) if ( isDST( &ts ) ) { cnt -= 60*60; // Subtract one hour } RTC_SetCounter( cnt ); return true; }
void CheckForDaysElapsed(void) { uint8_t DaysElapsed; if((RTC_GetCounter() / SECONDS_IN_DAY) != 0) { for(DaysElapsed = 0; DaysElapsed < (RTC_GetCounter() / SECONDS_IN_DAY)\ ;DaysElapsed++) { DateUpdate(); } RTC_SetCounter(RTC_GetCounter() % SECONDS_IN_DAY); } }
void timer_start() { RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP | RCC_APB1Periph_PWR, ENABLE); PWR_BackupAccessCmd(ENABLE); RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128); RCC_RTCCLKCmd(ENABLE); RTC_WaitForLastTask(); RTC_WaitForSynchro(); RTC_WaitForLastTask(); RTC_SetPrescaler(0); // counting at 8e6 / 128 RTC_WaitForLastTask(); RTC_SetCounter(0); RTC_WaitForLastTask(); }
/******************************************************************************* * 函 数 名: * 功 能: * 参 数: * 返 回: *******************************************************************************/ uint8_t RTC_Init(uint32_t pre_value, uint32_t alarm_value, uint32_t count_value) { uint8_t count; NVIC_InitTypeDef NVIC_InitStructure; //RTC全局中断 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); RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR , ENABLE); PWR_BackupAccessCmd(ENABLE); //设置外部低速晶振(LSE),使用外设低速晶振 RCC_LSEConfig(RCC_LSE_ON); //检查指定的RCC标志位设置与否,等待低速晶振就绪 while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) { count++; Delayms(10); } if(count>=250) return 1; //设置RTC时钟(RTCCLK),选择LSE作为RTC时钟 RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); RCC_RTCCLKCmd(ENABLE); //等待最近一次对RTC寄存器的读操作完成 RTC_WaitForSynchro(); //等待最近一次对RTC寄存器的写操作完成 RTC_WaitForLastTask(); //设置报警时间 RTC_SetAlarm(alarm_value); RTC_WaitForLastTask(); //配置中断 RTC_ITConfig( RTC_IT_SEC|RTC_IT_ALR|RTC_IT_OW, ENABLE); RTC_WaitForLastTask(); //设置RTC预分频的值 RTC_SetPrescaler(pre_value); RTC_WaitForLastTask(); //清除RCC的复位标志位 RCC_ClearFlag(); RTC_WaitForLastTask(); RTC_SetCounter(count_value); RTC_WaitForLastTask(); return 0; }
/* * 函数名:Time_Adjust * 描述 :时间调节 * 输入 :用于读取RTC时间的结构体指针 * 输出 :无 * 调用 :外部调用 */ void Time_Adjust(struct rtc_time *tm) { /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Get time entred by the user on the hyperterminal */ Time_Regulate(tm); /* Get wday */ GregorianDay(tm); /* 修改当前RTC计数寄存器内容 */ RTC_SetCounter(mktimev(tm)); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); }
/******************************************************************************* * Function Name : RTC_IRQHandler * Description : This function handles RTC global interrupt request. * Input : None * Output : None * Return : None *******************************************************************************/ void RTC_IRQHandler(void) { if (RTC_GetITStatus(RTC_IT_SEC) != RESET) { /* Clear the RTC Second interrupt */ RTC_ClearITPendingBit(RTC_IT_SEC); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Reset RTC Counter when Time is 23:59:59 */ if (RTC_GetCounter() == 0x00015180) { RTC_SetCounter(0x0); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); } } }
/** * @brief Displays the current time. * @param TimeVar: RTC counter value. * @retval None */ void Time_Display(uint32_t TimeVar) { /* Reset RTC Counter when Time is 23:59:59 */ if (RTC_GetCounter() == 0x0001517F) { RTC_SetCounter(0x0); IncreaseSingleDay(); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Backup routine*/ int YY, MD; YY = GetYearAndMergeToInt(); MD = GetMonthAndMergeToInt() * 100 + GetDayAndMergeToInt(); printf("\r\n\n Automatic Backup routine excuted"); BKP_WriteBackupRegister(BKP_DR2, YY); BKP_WriteBackupRegister(BKP_DR3, MD); printf("\r\n Calendar data successfully saved to backup register BKP_DR2, 3"); } /* get calendar */ TYR = GetYearAndMergeToInt(); TMO = GetMonthAndMergeToInt(); TDD = GetDayAndMergeToInt(); /* Compute hours */ THH = TimeVar / 3600; /* Compute minutes */ TMM = (TimeVar % 3600) / 60; /* Compute seconds */ TSS = (TimeVar % 3600) % 60; int mTHH, mTMM, mTSS; mTHH = THH; mTMM = TMM; mTSS = TSS; sprintf(DATE, "DATE: %04d.%02d.%02d", TYR, TMO, TDD); sprintf(TIME, "TIME: %02d:%02d:%02d", mTHH, mTMM, mTSS); /* Display through CLCD */ CLCD_Write(0, 0, DATE); CLCD_Write(0, 1, TIME); }
/******************************************************************************* * Function Name : RTC_IRQHandler * Description : This function handles RTC global interrupt request. * Input : None * Output : None * Return : None *******************************************************************************/ void RTC_IRQHandler(void) { /* If counter is equal to 86339: one day was elapsed */ if((RTC_GetCounter()/3600 == 23)&&(((RTC_GetCounter()%3600)/60) == 59)&& (((RTC_GetCounter()%3600)%60) == 59)) /* 23*3600 + 59*60 + 59 = 86339 */ { /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Reset counter value */ RTC_SetCounter(0x0); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Increment the date */ // Date_Update(); } /* Clear the RTC Second Interrupt pending bit */ RTC_ClearITPendingBit(RTC_IT_SEC); }
void setSysTime(VSysClock* vsc) { struct tm stm; time_t t; stm.tm_year = vsc->wYear - 1900; stm.tm_mon = vsc->byMonth - 1; stm.tm_mday = vsc->byDay; stm.tm_wday = vsc->byWeek; stm.tm_hour = vsc->byHour; stm.tm_min = vsc->byMinute; stm.tm_sec = vsc->bySecond; stm.tm_isdst = 0;//Disable Daylight Saving Time t = mktime(&stm); RTC_WaitForLastTask(); RTC_SetCounter(vsc->wMSecond); RTC_WaitForLastTask(); seconds = t; }
/** * @brief Displays the current time. * @param TimeVar: RTC counter value. * @retval None */ void Time_Display(uint32_t TimeVar) { uint32_t THH = 0, TMM = 0, TSS = 0; /* Reset RTC Counter when Time is 23:59:59 */ if (RTC_GetCounter() == 0x0001517F) { RTC_SetCounter(0x0); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); } /* Compute hours */ THH = TimeVar / 3600; /* Compute minutes */ TMM = (TimeVar % 3600) / 60; /* Compute seconds */ TSS = (TimeVar % 3600) % 60; printf("Time: %0.2d:%0.2d:%0.2d\r", THH, TMM, TSS); }
static rt_err_t rt_rtc_control(rt_device_t dev, rt_uint8_t cmd, void *args) { rt_time_t *time; RT_ASSERT(dev != RT_NULL); switch (cmd) { case RT_DEVICE_CTRL_RTC_GET_TIME: time = (rt_time_t *)args; /* read device */ *time = RTC_GetCounter(); break; case RT_DEVICE_CTRL_RTC_SET_TIME: { time = (rt_time_t *)args; /* Enable PWR and BKP clocks */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Change the current time */ RTC_SetCounter(*time); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); BKP_WriteBackupRegister(BKP_DR1, 0xA5A5); } break; } return RT_EOK; }
u8 p_dr_RtcSet(u16 syear,u8 smon,u8 sday,u8 hour,u8 min,u8 sec) { u16 t; u32 seccount=0; if(syear<1970||syear>2099)return 1; for(t=1970; t<syear; t++) //把所有年份的秒钟相加 { if(p_dr_RtcIsLeapYear(t))seccount+=31622400;//闰年的秒钟数 else seccount+=31536000; //平年的秒钟数 } smon-=1; for(t=0; t<smon; t++) //把前面月份的秒钟数相加 { seccount+=(u32)mon_table[t]*86400;//月份秒钟数相加 if(p_dr_RtcIsLeapYear(syear)&&t==1)seccount+=86400;//闰年2月份增加一天的秒钟数 } seccount+=(u32)(sday-1)*86400;//把前面日期的秒钟数相加 seccount+=(u32)hour*3600;//小时秒钟数 seccount+=(u32)min*60; //分钟秒钟数 seccount+=sec;//最后的秒钟加上去 //设置时钟 //RCC->APB1ENR|=1<<28;//使能电源时钟 //RCC->APB1ENR|=1<<27;//使能备份时钟 //PWR->CR|=1<<8; //取消备份区写保护 //上面三步是必须的! //RTC->CRL|=1<<4; //允许配置 //RTC->CNTL=seccount&0xffff; //RTC->CNTH=seccount>>16; //RTC->CRL&=~(1<<4);//配置更新 //while(!(RTC->CRL&(1<<5)));//等待RTC寄存器操作完成 /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成 /* Change the current time */ RTC_SetCounter(seccount); //设置RTC计数器的值 /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成 return 0; }
/** * @brief This function handles RTC global interrupt request. * @param None * @retval : None */ void RTC_IRQHandler(void) { if (RTC_GetITStatus(RTC_IT_SEC) != RESET) { /* Clear the RTC Second interrupt */ RTC_ClearITPendingBit(RTC_IT_SEC); /* Toggle GPIO_LED pin 6 each 1s */ //GPIO_WriteBit(GPIO_LED, GPIO_Pin_6, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIO_LED, GPIO_Pin_6))); /* Enable time update */ /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Reset RTC Counter when Time is 23:59:59 */ if (RTC_GetCounter() == 0x0001517F) { RTC_SetCounter(0x0); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); } } }
uint08 RTC_Set(uint16 syear,uint08 smon,uint08 sday,uint08 hour,uint08 min,uint08 sec) { uint16 t; uint32 seccount=0; if ( syear<1970||syear>2099 )return 1; for ( t=1970;t<syear;t++ ) //把所有年份的秒钟相加 { if ( Is_Leap_Year(t) ) { seccount+=31622400;//闰年的秒钟数 } else { seccount+=31536000; //平年的秒钟数 } } smon-=1; for ( t=0;t<smon;t++ ) //把前面月份的秒钟数相加 { seccount+=(uint32)mon_table[t]*86400;//月份秒钟数相加 if ( Is_Leap_Year(syear)&&t==1 ) { seccount+=86400;//闰年2月份增加一天的秒钟数 } } seccount+=(uint32)(sday-1)*86400;//把前面日期的秒钟数相加 seccount+=(uint32)hour*3600;//小时秒钟数 seccount+=(uint32)min*60; //分钟秒钟数 seccount+=sec;//最后的秒钟加上去 RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //使能PWR和BKP外设时钟 PWR_BackupAccessCmd(ENABLE); //使能RTC和后备寄存器访问 RTC_SetCounter(seccount); //设置RTC计数器的值 RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成 return 0; }
/** * @brief This function handles RTC_IRQHandler . * @param None * @retval : None */ void RTC_IRQHandler(void) { NVIC_ClearPendingIRQ(RTC_IRQn); RTC_ClearITPendingBit(RTC_IT_SEC); CalculateTime(); alarm_check(alarm_buffer_ptr); /* If counter is equal to 86399: one day was elapsed */ /* This takes care of date change and resetting of counter in case of power on - Run mode/ Main supply switched on condition*/ if(RTC_GetCounter() == 86399) { /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Reset counter value */ RTC_SetCounter(0x0); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Increment the date */ DateUpdate(); } }
/** * @brief This function handles RTC global interrupt request. * @param None * @retval None */ void RTC_IRQHandler(void) { if (RTC_GetITStatus(RTC_IT_SEC) != RESET) { /* Clear the RTC Second interrupt */ RTC_ClearITPendingBit(RTC_IT_SEC); /* Toggle LED1 */ STM_EVAL_LEDToggle(LED1); /* Enable time update */ TimeDisplay = 1; /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Reset RTC Counter when Time is 23:59:59 */ if (RTC_GetCounter() == 0x00015180) { RTC_SetCounter(0x0); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); } } }
/** * @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 */ /* System Clocks Configuration */ RCC_Configuration(); /* System Tick Configuration at 1us */ SysTick_Config(SystemCoreClock / 1000000); #if (defined USE_EQDAS01) || (defined USE_EQDAS02) /* TIM2 Configuration (Client & ATFC Server) */ TIM2_Configuration(); #elif defined USE_EQDAS_SERVER /* TIM4 Configuration */ TIM4_Configuration(); #endif /* TIM5 Configuration (GLCD & Ethernet) */ TIM5_Configuration(); /* TIM6 Configuration (RTC load) */ //TIM6_Configuration(); /* CLCD Configuration */ CLCD_Configuration(); /* GLCD Configuration */ GLCD_Configuration(); /* UART1 Configuration */ UART_Configuration(); /* RTC configuration by setting the time by Serial USART1 */ RTC_SetTimeBySerial(); /* Let user set the IP through terminal forcefully */ //ForceIPSetBySerial(); /* WIZ820io SPI1 configuration */ WIZ820io_SPI1_Configuration(); /* W5200 Configuration */ Set_network(); /* Print WIZ820io configuration */ printSysCfg(); /* EXTI Configuration */ EXTI_Configuration(); /* FatFS configuration */ f_mount(0, &fs); // mSD //f_mount(1, &fs); // NAND /* Display Total size of SD card in MB scale */ SD_TotalSize(); /* Scan all files in mSD card */ //scan_files(path); /* MAL configuration */ //Set_System(); /* UMS configuration */ //Set_USBClock(); //USB_Interrupts_Config(); //USB_Init(); /* loop upon completion of USB Enumeration */ //while (bDeviceState != CONFIGURED); /* ATFC Algorithm GPIO */ ATFC_GPIO_Configuration(); /* ATFC Parameter Initialization */ ATFCAlgorithmParameterSetup(); /* GPS-UART3 Configuration - This have to be here otherwise it wouldn't work */ GPS_Configuration(); // For TCP client's connection request delay presentTime = my_time; /* Create directory and sub directory in accordance with current date */ filePath = CreateDirectoryAccordingly(GetYearAndMergeToInt(), GetMonthAndMergeToInt(), GetDayAndMergeToInt(), RTC_GetCounter() / 3600); /* Create file in append mode in accordance with current minute */ CreateFileAppendModeAccordingly(filePath, (RTC_GetCounter() % 3600) / 60); /* Clear GLCD to better represent waveform */ GLCD_Clear(); //BKP_WriteBackupRegister(BKP_DR8, 0); // When everything is set, print message printf("\r\n\n - System is ready - "); while (1) { #if (defined USE_EQDAS01) || (defined USE_EQDAS02) /* Index synchronization routine -----------------------------------------------*/ if(SyncFlag) { // prevent unpleasant impuse from happening // Index synchronization dedicated to GLCD & Ethernet if(arrIdx != index) { // Index synchronization arrIdx = index; } } /* End of index synchronization routine -----------------------------------------*/ if(TimerCount > 999) { // 0 ~ 999 (1000) = 1 sec TimerCount = 0; //my_time++; // uncomment when tcp connection is needed /* Setup TCP Client or Server -------------------------------------------------*/ /* Please open config.h file to choose a proper board you wish to use ---------*/ /* Start TCP Client process */ ProcessTcpClient(SOCK_ZERO); // TCP Client /* Parameter setting Server side with port 5050 in default */ ATFCTcpServer(SOCK_TWO, EQDAS_Conf_PORT); // SOCK_TWO because of flag conformity /*------------------------------------------------------------------------------*/ /* Process Parameter Text Stream -----------------------------------------------*/ if(PCFlag) { // EQDAS Client System and ATFC Algorithm Setting PCFlag = false; ProcessParameterStream(); } /* End of Parameter process ----------------------------------------------------*/ } /* 10ms interval between points */ if(TIM5Count >= 9) { TIM5Count = 0; // Make a copy from raw collected data to temporary array CopyToTmpArray(arrIdx); // Determine KMA scale KMAGrade = DetermineKMA(arrIdx); // Check sign bit and apply to int container CheckSignAndToInt(arrIdx); // this function also cuts surplus 1G /* Switch menu & waveform display through graphic lcd */ GLCD_AxisViewWithWaveform(mode, arrIdx); //int mATFCBit; //mATFCBit_lcd = mAxisBuf.ATFCBit_lcd[arrIdx]; int AxisDataToATFCAlgorithm, mATFCEventDetection; AxisDataToATFCAlgorithm = mAxisBuf.tmp_data_y_lcd[arrIdx]; // Axis Z ATFCAlgorithm(AxisDataToATFCAlgorithm); mATFCEventDetection = EventDetection; /* Display KMA Intensity on Graphic LCD */ GLCD_DisplayKMAIntensity(KMAGrade, mATFCEventDetection); /* Prevent access to volatile variable warning */ /* This have to be here in order to correct data to be used in ATFC */ /* ATFC Server side for each EQ DAS Client */ if(EQATFCFlag) { int mYear, mMonth, mDay, mHour, mMin, mSec, mTMSec; mYear = year; mMonth = month; mDay = day; mHour = hour; mMin = minute; mSec = second; mTMSec = arrIdx; int mX, mY, mZ, mATFCBit; mX = mAxisBuf.tmp_data_x_lcd[arrIdx] >> 2; mY = mAxisBuf.tmp_data_y_lcd[arrIdx] >> 2; mZ = mAxisBuf.tmp_data_z_lcd[arrIdx] >> 2; mATFCBit = mATFCEventDetection; char ATFC_Buf[40]; sprintf(ATFC_Buf, "%04d%02d%02d_%02d%02d%02d%02d_%+05d_%+05d_%+05d_%d\r\n", mYear, mMonth, mDay, mHour, mMin, mSec, mTMSec, mX, mY, mZ, mATFCBit); // Only when socket is established, allow send data if(getSn_SR(SOCK_TWO) == SOCK_ESTABLISHED) { // SOCK_TWO : PC /* send selected data */ send(SOCK_TWO, (uint8_t*)ATFC_Buf, strlen(ATFC_Buf), (bool)false); } } // Copy to data buffer to be written through FATFS //CopyToFatFsDataBuffer(arrIdx); } /* RTC 1Hz interrupt */ if(RTCTimeDisplay) { // 1Hz calibrated by RTC RTCTimeDisplay = false; int TimeVar; TimeVar = RTC_GetCounter(); /* Compute hour */ THH = TimeVar / 3600; /* Compute minute */ TMM = (TimeVar % 3600) / 60; /* Compute second */ TSS = (TimeVar % 3600) % 60; /* Refresh date on every 1s */ year = GetYearAndMergeToInt(); month = GetMonthAndMergeToInt(); day = GetDayAndMergeToInt(); hour = THH; minute = TMM; second = TSS; tmsecond = 0; if(ThirtyMinuteMark == 1799) { ThirtyMinuteMark = 0; ThirtyMinuteFlag = true; } else { ThirtyMinuteMark++; } /* Adjust realtime clock deviation */ if(hour > 23) { int i, currentDay, mDay, mHour, mMin, mSec; mDay = hour / 24; for(i=0; i<mDay; i++) { IncreaseSingleDay(); if(i == mDay - 1) { currentDay = (GetMonthAndMergeToInt() * 100) + GetDayAndMergeToInt(); BKP_WriteBackupRegister(BKP_DR3, currentDay); // Save Month and Date } } mHour = THH % 24; mMin = TMM; mSec = TSS; /* Change the current time */ RTC_SetCounter(mHour*3600 + mMin*60 + mSec); } } #endif if(ParseGPS) { ParseGPS = false; char *srcstr = "$GPRMC"; char *token = ","; char *processedString; char StringYear[3], StringMonth[3], StringDay[3], StringHour[3], StringMinute[3], StringSecond[3]; int GPSYear, GPSMonth, GPSDay, GPSHour, GPSMinute, GPSSecond; if(strncmp((char const*)GPS_Buffer, srcstr, 6) == 0) { //printf("GPS_Buffer = %s\r\n\r\n", (char*)GPS_Buffer); processedString = strtok((char*)GPS_Buffer, token); processedString = strtok(NULL, token); strncpy(StringHour, processedString, 2); StringHour[2] = 0; strncpy(StringMinute, processedString+2, 2); StringMinute[2] = 0; strncpy(StringSecond, processedString+4, 2); StringSecond[2] = 0; GPSHour = atoi(StringHour) + 9; // Current Hour = StringHour + 9 GPSMinute = atoi(StringMinute); GPSSecond = atoi(StringSecond); int i; for(i=4; i!=0 ; i--) processedString = strtok(NULL, token); strncpy(StringYear, processedString+4, 2); StringYear[2] = 0; strncpy(StringMonth, processedString+2, 2); StringMonth[2] = 0; strncpy(StringDay, processedString, 2); StringDay[2] = 0; GPSYear = atoi(StringYear) + 2000; // Currnet Year = StringYear + 2000 GPSMonth = atoi(StringMonth); GPSDay = atoi(StringDay); /* The Year is chosen as criteria to the time */ if( (GPSYear == GetYearAndMergeToInt()) && ThirtyMinuteFlag ) { // only when year matches between RTC and GPS ThirtyMinuteFlag = false; if(GPSMonth != GetMonthAndMergeToInt() || GPSDay != GetDayAndMergeToInt() || GPSHour != THH || GPSMinute != TMM || GPSSecond != TSS) { /* Change the month and day */ TranslateIntoMonth(GPSMonth); TranslateIntoDay(GPSDay); /* Save year data to unresettable backup register addr. no. 3 */ int MMDD; MMDD = (GPSMonth * 100) + GPSDay; BKP_WriteBackupRegister(BKP_DR3, MMDD); // Save Month and Date /* Change the current time */ RTC_SetCounter(GPSHour*3600 + GPSMinute*60 + GPSSecond); printf("GPSHour = %d\r\n", GPSHour); printf("GPSMinute = %d\r\n", GPSMinute); printf("GPSSecond = %d\r\n\r\n", GPSSecond); printf("GPSYear = %d\r\n", GPSYear); printf("GPSMonth = %d\r\n", GPSMonth); printf("GPSDay = %d\r\n\r\n", GPSDay); printf("GPS-to-System synchronization complete!\r\n\r\n"); } } } } if(ParseUSART1) { ParseUSART1 = false; // run some test on SDIO //SDIO_TEST(); #if (defined USE_EQDAS01) || (defined USE_EQDAS02) /* Print WIZ820io configuration */ printSysCfg(); printf("\r\n"); printf("BKP_DR1 = %d\r\n", BKP_ReadBackupRegister(BKP_DR1)); printf("BKP_DR2 = %d\r\n", BKP_ReadBackupRegister(BKP_DR2)); printf("BKP_DR3 = %d\r\n", BKP_ReadBackupRegister(BKP_DR3)); printf("BKP_DR4 = %d\r\n", BKP_ReadBackupRegister(BKP_DR4)); printf("BKP_DR5 = %d\r\n", BKP_ReadBackupRegister(BKP_DR5)); printf("BKP_DR6 = %d\r\n", BKP_ReadBackupRegister(BKP_DR6)); printf("BKP_DR7 = %d\r\n", BKP_ReadBackupRegister(BKP_DR7)); printf("BKP_DR8 = %d\r\n", BKP_ReadBackupRegister(BKP_DR8)); printf("BKP_DR9 = %d\r\n", BKP_ReadBackupRegister(BKP_DR9)); printf("BKP_DR10 = %d\r\n", BKP_ReadBackupRegister(BKP_DR10)); printf("BKP_DR11 = %d\r\n", BKP_ReadBackupRegister(BKP_DR11)); printf("BKP_DR12 = %d\r\n", BKP_ReadBackupRegister(BKP_DR12)); printf("BKP_DR13 = %d\r\n", BKP_ReadBackupRegister(BKP_DR13)); printf("BKP_DR14 = %d\r\n", BKP_ReadBackupRegister(BKP_DR14)); printf("BKP_DR15 = %d\r\n", BKP_ReadBackupRegister(BKP_DR15)); printf("BKP_DR16 = %d\r\n\r\n", BKP_ReadBackupRegister(BKP_DR16)); printf("RX_BUF = %s\r\n", RX_BUF); /* printf("\r\nstrlen(HEADER) : %d %s", strlen(HEADER), HEADER); printf("\r\nf_mkdir1 : "); char *dirPath = "0:/20130517"; res = f_mkdir(dirPath); FPrintFatResult(res); printf("\r\nf_mkdir2 : "); dirPath = "0:/20130517/22H-23H"; res = f_mkdir(dirPath); FPrintFatResult(res); char *filePath = "0:/20130517/2-23H/test.txt"; // Create log file on the drive 0 res = open_append(&fsrc, filePath); FPrintFatResult(res); if(res == FR_OK) { printf("test.txt successfully created\r\n"); // Write buffer to file int bytesWritten; bytesWritten = f_printf(&fsrc, HEADER); printf("\r\n%d of bytesWritten", bytesWritten); // Close file f_close(&fsrc); } else if ( res == FR_EXIST ) { printf("\r\ntest.txt already exist"); } */ #elif (defined) USE_EQDAS_SERVER char buffer[40]; sprintf(buffer, "%s_%s_%s_%s_%s\r\n", DAQBoardOne[arrIdx].Date, DAQBoardOne[arrIdx].Time, DAQBoardOne[arrIdx].AxisX, DAQBoardOne[arrIdx].AxisY, DAQBoardOne[arrIdx].AxisZ); printf("\r\nRX_BUF : %s, strlen(RX_BUF) : %d", (char*)RX_BUF, strlen((char*)RX_BUF)); printf("\r\nstrlen(buffer) = %d\n%s", strlen(buffer), buffer); #endif } // following routine is only necessary when the board works as server #if defined USE_EQDAS_SERVER /* Server also needs to have get CLCD going while running */ /* RTC 1Hz interrupt */ if(RTCTimeDisplay) { // 1Hz calibrated by RTC RTCTimeDisplay = false; /* Adjust realtime clock deviation */ if(hour > 23) { int i, currentDay, mDay, mHour, mMin, mSec; mDay = hour / 24; for(i=0; i<mDay; i++) { IncreaseSingleDay(); if(i == mDay - 1) { currentDay = (GetMonthAndMergeToInt() * 100) + GetDayAndMergeToInt(); BKP_WriteBackupRegister(BKP_DR3, currentDay); // Save Month and Date } } mHour = THH % 24; mMin = TMM; mSec = TSS; /* Change the current time */ RTC_SetCounter(mHour*3600 + mMin*60 + mSec); } /* Display current time */ Time_Display(RTC_GetCounter()); } /* EQ-DAQ-01 Parsing routine ------------------------------------------------- */ /* Set E1Flag indicate that we have valid connection from EQ-DAQ-01(port 5050) */ if(E1Flag) { E1Flag = false; // clear flag since this routine excutes ceaselessly over time ProcessTextStream(EQ_ONE, (char*)RX_BUF, E1Order); /* PC Client Parsing routine ------------------------------------------------- */ /* Set PCFlag indicate that we have valid connection from PC Client(port 7070) */ if(PCFlag && !E2Flag) { // only when PC is connected and EQ-DAQ-02 is not connected // Send directly to PC SingleBoardDataToSendToPC(EQ_ONE, E1Order-10); } if(E1Order < 99) E1Order++; else E1Order = 0; } /* EQ-DAQ-02 Parsing routine ------------------------------------------------- */ /* Set E2Flag indicate that we have valid connection from EQ-DAQ-02(port 6060) */ if(E2Flag) { E2Flag = false; ProcessTextStream(EQ_TWO, (char*)RX_BUF, E2Order); /* PC Client Parsing routine ------------------------------------------------- */ /* Set PCFlag indicate that we have valid connection from PC Client(port 7070) */ if(PCFlag && !E1Flag) { // only when PC is connected and EQ-DAQ-01 is not connected // Send directly to PC //SendToPC(EQ_TWO, E2Order); } if(E2Order < 99) E2Order++; else E2Order = 0; /* PC Client Parsing routine ------------------------------------------------- */ /* Set PCFlag indicate that we have valid connection from PC Client(port 7070) */ if(PCFlag) { // Send directly to PC MultipleBoardDataToSendToPC(EQ_BOTH, E1Order-10, E2Order-10); } } /* Process server socket with each port */ ProcessTcpServer(SOCK_ZERO, 5050); // designated as for EQM-DAQ-01 with port 5050 ProcessTcpServer(SOCK_ONE, 6060); // designated as for EQM-DAQ-02 with port 6060 ProcessTcpServer(SOCK_TWO, 7070); // designated as for PC-CLIENT with port 7070 ProcessTcpServer(SOCK_THREE, 8080); // designated as for PC_DUMP with port 8080 /* ProcessTcpServer(SOCK_FOUR, 9090); // designated as for TOBEUSED with port 9090 ProcessTcpServer(SOCK_FIVE, 10010); // designated as for TOBEUSED with port 10010 ProcessTcpServer(SOCK_SIX, 10020); // designated as for TOBEUSED with port 10020 ProcessTcpServer(SOCK_SEVEN, 10030); // designated as for TOBEUSED with port 10030 */ #endif }