bool Uploader_getTime(void){ volatile uint32_t tempTime = 0; volatile uint8_t timeOutCounter = 0; volatile uint8_t commandCounter = 0; if(!Debug_SendByte('T')){ return false; } while(true){ if(Debug_CharReadyToRead()){ command[commandCounter+1] = Debug_GetByte(false); //if(!Debug_SendByte(command[commandCounter+1])){ // return false; //} commandCounter++; if(commandCounter == 4){ tempTime = command[1]; tempTime <<= 8; tempTime += command[2]; tempTime <<= 8; tempTime += command[3]; tempTime <<= 8; tempTime += command[4]; if(tempTime < 1260000000){ // time is invalid for(uint8_t i = 0; i < 4; i++){ if(!Debug_SendByte('0')){ return false; } } return false; } else { for(uint8_t i = 0; i < 4; i++){ if(!Debug_SendByte(command[i+1])){ return false; } } Time_Set(tempTime); return true; } } } _delay_ms(1); timeOutCounter++; if(timeOutCounter > 100){ return false; } } return true; }
/** * @brief Init hardware of RTC * @param none * @retval None */ u8 RTC_Hardware_Init(void) { u16 countDown = 1000; RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); PWR_BackupAccessCmd(ENABLE); //BKP_DeInit(); if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5) //从指定的后备寄存器中读出数据,判断是否为第一次配置 { BKP_DeInit(); //将外设BKP的全部寄存器重设为缺省值 RCC_LSEConfig(RCC_LSE_ON); //使能外部低速时钟 32.768KHz while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) //检查指定的RCC标志位设置与否,等待低速晶振就绪 { vTaskDelay(2 / portTICK_RATE_MS); countDown--; if (countDown == 0) { ShowDialogue("Hardware Error", "LSE crystal", "not ready!", false, true); OLED_Refresh_Gram(); while (1); } } RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); //设置RTC时钟(RTCCLK),选择LSE作为RTC时钟 RCC_RTCCLKCmd(ENABLE); //使能RTC时钟 RTC_WaitForSynchro(); //等待RTC寄存器(RTC_CNT,RTC_ALR和RTC_PRL)与RTC APB时钟同步 RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成 RTC_ITConfig(RTC_IT_SEC, ENABLE); //使能RTC秒中断 RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成 RTC_SetPrescaler(32767); //设置RTC预分频的值 RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成 Time_Set(); //时间设置 BKP_WriteBackupRegister(BKP_DR1, 0xA5A5); //向指定的后备寄存器中写入用户程序数据0X5555做判断标志 } else //不是第一次配置 继续计时 { if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET) //检查指定的RCC标志位设置与否:POR/PDR复位 { ; } else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET) //检查指定的RCC标志位设置与否:管脚复位 { ; } RCC_ClearFlag(); // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //先占优先级1位,从优先级3位 // NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn; //RTC全局中断 // NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //先占优先级1位,从优先级3位 // NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //先占优先级0位,从优先级4位 // NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能该通道中断 // NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器 RTC_WaitForSynchro(); //等待最近一次对RTC寄存器的写操作完成 // RTC_ITConfig(RTC_IT_SEC, ENABLE); //使能RTC秒中断 RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成 } //Time_Get(); //更新时间 RCC_ClearFlag(); //清除RCC的复位标志位 return 0; //ok }