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; }
/** * This function will set MCU RTC time to a new value. Time value must be given in the format of * the structure wiced_rtc_time_t * * @return WICED_SUCCESS : on success. * @return WICED_ERROR : if an error occurred with any step */ OSStatus platform_rtc_set_time( const mico_rtc_time_t* time ) { #ifdef MICO_ENABLE_MCU_RTC bool valid = false; MICO_VERIFY_TIME(time, valid); if( valid == false ) { return kParamErr; } Chip_RTC_SetCount(LPC_RTC, convert_rtc_calendar_values_to_units_passed( time )); return kNoErr; #else /* #ifdef MICO_ENABLE_MCU_RTC */ UNUSED_PARAMETER(time); return kUnsupportedErr; #endif /* #ifdef MICO_ENABLE_MCU_RTC */ }