OSStatus platform_rtc_init(void) { #ifdef MICO_ENABLE_MCU_RTC RTC_InitTypeDef RTC_InitStruct; RTC_DeInit( ); RTC_InitStruct.RTC_HourFormat = RTC_HourFormat_24; /* RTC ticks every second */ RTC_InitStruct.RTC_AsynchPrediv = 0x7F; RTC_InitStruct.RTC_SynchPrediv = 0xFF; RTC_Init( &RTC_InitStruct ); #ifdef USE_RTC_BKP /* Enable the PWR clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); PWR_BackupRegulatorCmd(ENABLE); #endif /* Enable the LSE OSC */ RCC_LSEConfig(RCC_LSE_ON); /* Wait till LSE is ready */ while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) { } /* Select the RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); /* Enable the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* RTC configuration -------------------------------------------------------*/ /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); #ifdef USE_RTC_BKP if (RTC_ReadBackupRegister(RTC_BKP_DR0) != USE_RTC_BKP) { /* set it to 12:20:30 08/04/2013 monday */ platform_rtc_set_time(&default_rtc_time); RTC_WriteBackupRegister(RTC_BKP_DR0, USE_RTC_BKP); } #else //#ifdef RTC_ENABLED /* application must have mico_application_default_time structure declared somewhere, otherwise it wont compile */ /* write default application time inside rtc */ platform_rtc_set_time(&mico_default_time); //#endif /* RTC_ENABLED */ #endif return kNoErr; #else return kUnsupportedErr; #endif }
platform_result_t platform_rtc_abort_powersave( void ) { /* Change the clocking state of the RTC, so its tick is back to normal */ stm32f2_rtc_change_clock( ¤t_clock_state, CLOCKING_EVERY_SEC ); //#ifdef RTC_ENABLED /* !!If we dont set the time after the clocks have been changed. */ /*get an error while trying to enter STM RTC initialisation mode */ /* restore time saved before */ platform_rtc_set_time( &saved_rtc_time ); //#endif /* #ifdef RTC_ENABLED */ return PLATFORM_SUCCESS; }
platform_result_t platform_rtc_exit_powersave( uint32_t requested_sleep_time, uint32_t *cpu_sleep_time ) { uint32_t time_units_passed_since_powersave_enter; /* time unit is 1.25ms when we are sleeping */ #ifdef WICED_ENABLE_MCU_RTC uint32_t seconds_contribution; static int leftover=0; #endif /* #ifdef WICED_ENABLE_MCU_RTC */ float temp; /* Get current calendar register values and convert them to a number of 1.25ms that passed since power-save entry */ time_units_passed_since_powersave_enter = convert_rtc_calendar_values_to_units_passed(); #ifdef WICED_ENABLE_MCU_RTC /* Make contribution of time-units to the current time, get the leftover that is less then a second */ leftover+= add_1p25ms_contribution( time_units_passed_since_powersave_enter, &seconds_contribution ); /* Subtract 1 second for every 1.25 * 800, because in reality we are running 799,2195Hz which wil give the tick value = 1.25122 */ if( leftover > NUM_1P25MS_IN_SEC ) { compensate_time_error(1, WICED_FALSE); leftover -= NUM_1P25MS_IN_SEC; } #endif /* #ifdef WICED_ENABLE_MCU_RTC */ //#ifdef RTC_ENABLED /* !!If we dont set the time after the clocks have been changed. */ /*get an error while trying to enter STM RTC initialisation mode */ /* update RTC time */ platform_rtc_set_time(&saved_rtc_time); //#endif /* #ifdef RTC_ENABLED */ /* Change, rtc clock state and update rtc peripheral, even when RTC is not enabled */ /* reducing clock frequency is better for power consumption */ stm32f2_rtc_change_clock( ¤t_clock_state, CLOCKING_EVERY_SEC ); temp = (float)time_units_passed_since_powersave_enter * (float)1.25; /* Round up to milliseconds, not a problem if the system will get less ticks as expected, it is not a time which is populated to a user */ if( requested_sleep_time > 1 ) { *cpu_sleep_time = (uint32_t)temp; } else { /* When a delay was set to 1, we will get a wake up interrupt before RTC tick */ /* we will think that there were no rtc tick, and the cpu_sleep_time will be set to 0 */ /* operating system will miss a tick which in reality did happen */ *cpu_sleep_time = 1; } return PLATFORM_SUCCESS; }
/* Wecan Modify platform_rtc_init 2015.06.10 */ OSStatus platform_rtc_init(void) { #ifdef MICO_ENABLE_MCU_RTC /* CLKOUT = 32K_Osc */ Chip_IOCON_PinMuxSet(LPC_IOCON, platform_gpio_pins[CLKOUT].port , platform_gpio_pins[CLKOUT].pin_number, (IOCON_MODE_PULLUP | IOCON_FUNC1 | IOCON_DIGITAL_EN | IOCON_INPFILT_OFF)); Chip_Clock_SetCLKOUTSource(SYSCON_CLKOUTSRC_RTC, 1); /* Turn on the RTC 32K Oscillator */ Chip_SYSCON_PowerUp(SYSCON_PDRUNCFG_PD_32K_OSC); /* Enable the RTC oscillator, oscillator rate can be determined by calling Chip_Clock_GetRTCOscRate() */ Chip_Clock_EnableRTCOsc(); /* Initialize RTC driver (enables RTC clocking) */ Chip_RTC_Init(LPC_RTC); /* Enable RTC as a peripheral wakeup event */ //Chip_SYSCON_EnsableWakeup(SYSCON_STARTER_RTC); /* RTC reset */ Chip_RTC_Reset(LPC_RTC); /* Start RTC at a count of 0 when RTC is disabled. If the RTC is enabled, you need to disable it before setting the initial RTC count. */ Chip_RTC_Disable(LPC_RTC); platform_rtc_set_time(&default_rtc_time); //Chip_RTC_SetCount(LPC_RTC, 0); /* Set a long alarm time so the interrupt won't trigger */ //Chip_RTC_SetAlarm(LPC_RTC, (Chip_RTC_GetCount(LPC_RTC) + 5)); /* Enable RTC and high resolution timer - this can be done in a single call with Chip_RTC_EnableOptions(LPC_RTC, (RTC_CTRL_RTC1KHZ_EN | RTC_CTRL_RTC_EN)); */ //Chip_RTC_Enable1KHZ(LPC_RTC); Chip_RTC_Enable(LPC_RTC); /* Clear latched RTC interrupt statuses */ Chip_RTC_ClearStatus(LPC_RTC, (RTC_CTRL_OFD | RTC_CTRL_ALARM1HZ | RTC_CTRL_WAKE1KHZ)); return kNoErr; #else return kUnsupportedErr; #endif }
platform_result_t platform_rtc_init( void ) { #ifdef WICED_ENABLE_MCU_RTC RTC_InitTypeDef RTC_InitStruct; RTC_DeInit( ); RTC_InitStruct.RTC_HourFormat = RTC_HourFormat_24; /* RTC ticks every second */ RTC_InitStruct.RTC_AsynchPrediv = 0x7F; RTC_InitStruct.RTC_SynchPrediv = 0xFF; RTC_Init( &RTC_InitStruct ); /* Enable the LSE OSC */ RCC_LSEConfig(RCC_LSE_ON); /* Wait till LSE is ready */ while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) { } /* Select the RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); /* Enable the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* RTC configuration -------------------------------------------------------*/ /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); /* write default application time inside rtc */ platform_rtc_set_time( &default_rtc_time ); return PLATFORM_SUCCESS; #else return PLATFORM_FEATURE_DISABLED; #endif }
OSStatus MicoRtcSetTime(mico_rtc_time_t* time) { return (OSStatus) platform_rtc_set_time( time ); }
OSStatus platform_mcu_powersave_init(void) { #ifndef MICO_DISABLE_MCU_POWERSAVE EXTI_InitTypeDef EXTI_InitStructure; RTC_InitTypeDef RTC_InitStruct; //RTC_DeInit( ); RTC_InitStruct.RTC_HourFormat = RTC_HourFormat_24; /* RTC ticks every second */ RTC_InitStruct.RTC_AsynchPrediv = 0x7F; RTC_InitStruct.RTC_SynchPrediv = 0xFF; RTC_Init( &RTC_InitStruct ); /* Enable the PWR clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* RTC clock source configuration ------------------------------------------*/ /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); PWR_BackupRegulatorCmd(ENABLE); /* Enable the LSE OSC */ RCC_LSEConfig(RCC_LSE_ON); /* Wait till LSE is ready */ while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) { } /* Select the RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); /* Enable the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* RTC configuration -------------------------------------------------------*/ /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); RTC_WakeUpCmd( DISABLE ); EXTI_ClearITPendingBit( RTC_INTERRUPT_EXTI_LINE ); PWR_ClearFlag(PWR_FLAG_WU); RTC_ClearFlag(RTC_FLAG_WUTF); RTC_WakeUpClockConfig(RTC_WakeUpClock_RTCCLK_Div2); EXTI_ClearITPendingBit( RTC_INTERRUPT_EXTI_LINE ); EXTI_InitStructure.EXTI_Line = RTC_INTERRUPT_EXTI_LINE; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); NVIC_EnableIRQ( RTC_WKUP_IRQn ); RTC_ITConfig(RTC_IT_WUT, DISABLE); /* Prepare Stop-Mode but leave disabled */ //PWR_ClearFlag(PWR_FLAG_WU); PWR->CR |= PWR_CR_LPDS; PWR->CR &= (unsigned long)(~(PWR_CR_PDDS)); SCB->SCR |= ((unsigned long)SCB_SCR_SLEEPDEEP_Msk); #ifdef USE_RTC_BKP if (RTC_ReadBackupRegister(RTC_BKP_DR0) != USE_RTC_BKP) { /* set it to 12:20:30 08/04/2013 monday */ platform_rtc_set_time(&default_rtc_time); RTC_WriteBackupRegister(RTC_BKP_DR0, USE_RTC_BKP); } #else //#ifdef RTC_ENABLED /* application must have wiced_application_default_time structure declared somewhere, otherwise it wont compile */ /* write default application time inside rtc */ platform_rtc_set_time( &default_rtc_time ); //#endif /* RTC_ENABLED */ #endif return kNoErr; #else return kUnsupportedErr; #endif }