void RTC_DRV_Init(uint32_t instance) { uint32_t rtcBaseAddr = g_rtcBaseAddr[instance]; /* Enable clock gate to RTC module */ CLOCK_SYS_EnableRtcClock( 0U); /* Initialize the general configuration for RTC module.*/ RTC_HAL_Init(rtcBaseAddr); RTC_HAL_Enable(rtcBaseAddr); NVIC_ClearPendingIRQ(g_rtcIrqId[instance]); NVIC_ClearPendingIRQ(g_rtcSecondsIrqId[instance]); INT_SYS_EnableIRQ(g_rtcIrqId[instance]); INT_SYS_EnableIRQ(g_rtcSecondsIrqId[instance]); }
rtc_status_t RTC_DRV_Init(uint32_t instance) { RTC_Type *rtcBase = g_rtcBase[instance]; /* Enable clock gate to RTC module */ CLOCK_SYS_EnableRtcClock(0U); /* Initialize the general configuration for RTC module.*/ RTC_HAL_Init(rtcBase); RTC_HAL_Enable(rtcBase); NVIC_ClearPendingIRQ(g_rtcIrqId[instance]); NVIC_ClearPendingIRQ(g_rtcSecondsIrqId[instance]); INT_SYS_EnableIRQ(g_rtcIrqId[instance]); INT_SYS_EnableIRQ(g_rtcSecondsIrqId[instance]); return kStatusRtcSuccess; }
/*FUNCTION********************************************************************** * * Function Name : CLOCK_SYS_RtcOscInit * Description : This funtion initializes the RTC OSC. * *END**************************************************************************/ clock_manager_error_code_t CLOCK_SYS_RtcOscInit(uint32_t instance, rtc_osc_user_config_t *config) { assert(instance < RTC_INSTANCE_COUNT); RTC_Type * rtcBase = g_rtcBase[instance]; CLOCK_SYS_EnableRtcClock(instance); g_xtalRtcClkFreq = config->freq; // If the oscillator is not enabled and should be enabled. if ((!RTC_HAL_IsOscillatorEnabled(rtcBase)) && (config->enableOsc)) { RTC_HAL_SetOsc2pfLoadCmd(rtcBase, config->enableCapacitor2p); RTC_HAL_SetOsc4pfLoadCmd(rtcBase, config->enableCapacitor4p); RTC_HAL_SetOsc8pfLoadCmd(rtcBase, config->enableCapacitor8p); RTC_HAL_SetOsc16pfLoadCmd(rtcBase, config->enableCapacitor16p); } RTC_HAL_SetOscillatorCmd(rtcBase, config->enableOsc); RTC_HAL_SetClockOutCmd(rtcBase, config->enableClockOutput); return kClockManagerSuccess; }
void RtcInit( void ) { if ( RtcInitalized == false ) { rtc_datetime_t datetime; uint32_t srcClock = 0; uint32_t seconds = 0; uint16_t preScaler = 0; uint64_t tmp = 0; /* Enable clock gate to RTC module */ CLOCK_SYS_EnableRtcClock(0U); /* Initialize the general configuration for RTC module.*/ RTC_HAL_Init (RTC_BASE_PTR); /* Clear pending interrupts before enabling them */ NVIC_ClearPendingIRQ (RTC_IRQn); INT_SYS_EnableIRQ(RTC_IRQn); /* Configure RTC external clock */ CLOCK_SYS_RtcOscInit(0U, &rtcOscConfig); // Set a start date time and start RT. datetime.year = 2014U; datetime.month = 12U; datetime.day = 25U; datetime.hour = 19U; datetime.minute = 0; datetime.second = 0; RTC_HAL_ConvertDatetimeToSecs(&datetime, &seconds); if ( (srcClock = CLOCK_SYS_GetRtcFreq(0U)) != 32768U ) { /* As the seconds register will not increment every second, we need to adjust the value * programmed to the seconds register */ tmp = (uint64_t) seconds * (uint64_t) srcClock; preScaler = (uint32_t)(tmp & 0x7FFFU); seconds = (uint32_t)(tmp >> 15U); }
/*! * @brief main demo function. */ int main(void) { demo_power_modes_t testVal = kDemoRun; uint8_t mode; power_manager_error_code_t ret = kPowerManagerSuccess; uint32_t freq = 0; rtc_datetime_t date = { .year = 2014U, .month = 4U, .day = 30U, .hour = 14U, .minute = 0U, .second = 0U, }; // Example of constant configuration // It may save the space in RAM const power_manager_user_config_t vlprConfig = { .mode = kPowerManagerVlpr, .sleepOnExitValue = false, }; power_manager_user_config_t vlpwConfig = vlprConfig; power_manager_user_config_t vlls0Config = vlprConfig; power_manager_user_config_t vlls1Config = vlprConfig; power_manager_user_config_t vlls2Config = vlprConfig; power_manager_user_config_t vlls3Config = vlprConfig; power_manager_user_config_t llsConfig = vlprConfig; power_manager_user_config_t vlpsConfig = vlprConfig; power_manager_user_config_t waitConfig = vlprConfig; power_manager_user_config_t stopConfig = vlprConfig; power_manager_user_config_t runConfig = vlprConfig; power_manager_user_config_t hsrunConfig = { .mode = kPowerManagerHsrun, }; // Initializes array of pointers to power manager configurations power_manager_user_config_t const *powerConfigs[] = { &runConfig, &waitConfig, &stopConfig, &vlprConfig, &vlpwConfig, &vlpsConfig, &llsConfig, &vlls0Config, &vlls1Config, &vlls2Config, &vlls3Config, &hsrunConfig }; // User callback data user_callback_data_t callbackData0; // Initializes callback configuration structure for power manager power_manager_callback_user_config_t callbackCfg0 = { callback0, kPowerManagerCallbackBeforeAfter, (power_manager_callback_data_t*) &callbackData0 }; // Initializes array of pointers to power manager callbacks power_manager_callback_user_config_t * callbacks[] = { &callbackCfg0 }; // Initializes hardware hardware_init(); // Make the current Clock Manager mode configuration 1 (default configuration) /* Set clock configurations to clock manager. */ CLOCK_SYS_Init(g_defaultClockConfigurations, CLOCK_NUMBER_OF_CONFIGURATIONS, clockCallbackTable, ARRAY_SIZE(clockCallbackTable)); CLOCK_SYS_UpdateConfiguration(CLOCK_RUN, kClockManagerPolicyForcible); // select the 1Hz for RTC_CLKOUT CLOCK_SYS_SetRtcOutSrc(kClockRtcoutSrc1Hz); /* Enable clock gate to RTC module */ CLOCK_SYS_EnableRtcClock( 0U); /* Initialize the general configuration for RTC module.*/ RTC_HAL_Init(RTC_BASE_PTR); /* Need to check this here as the RTC_HAL_Init() may have issued a software reset on the * module clearing all prior RTC OSC related setup */ if (!(RTC_HAL_IsOscillatorEnabled(RTC_BASE_PTR))) { BOARD_InitRtcOsc(); } /* Enable the RTC Clock output */ RTC_HAL_SetClockOutCmd(RTC_BASE_PTR, true); NVIC_ClearPendingIRQ(RTC_IRQn); INT_SYS_EnableIRQ(RTC_IRQn); //RTC_DRV_SetDatetime(0, &date); RTC_HAL_SetDatetime(RTC_BASE_PTR, &date); // Initializes GPIO driver for LEDs and buttons GPIO_DRV_Init(switchPins, ledPins); memset(&callbackData0, 0, sizeof(user_callback_data_t)); // initializes configuration structures vlpwConfig.mode = kPowerManagerVlpw; // VLLS0 mode is supported only by some SOCs. vlls0Config.mode = kPowerManagerVlls0; vlls1Config.mode = kPowerManagerVlls1; vlls2Config.mode = kPowerManagerVlls2; vlls3Config.mode = kPowerManagerVlls3; // LLS3 mode retains all ram content so CPU wake up doesn't go through restart sequence llsConfig.mode = kPowerManagerLls3; vlpsConfig.mode = kPowerManagerVlps; waitConfig.mode = kPowerManagerWait; stopConfig.mode = kPowerManagerStop; runConfig.mode = kPowerManagerRun; hsrunConfig.mode = kPowerManagerHsrun; // initialize power manager driver POWER_SYS_Init(powerConfigs, sizeof(powerConfigs)/sizeof(power_manager_user_config_t *), callbacks, sizeof(callbacks)/sizeof(power_manager_callback_user_config_t *)); // Enables LLWU interrupt INT_SYS_EnableIRQ(LLWU_IRQn); mode = kDemoRun - kDemoMin - 1; ret = POWER_SYS_SetMode(mode, kPowerManagerPolicyAgreement); if (ret != kPowerManagerSuccess) { PRINTF("POWER_SYS_SetMode(%u) returned unexpected status : %u\r\n",mode,ret); } while (1) { mode = 0; CLOCK_SYS_GetFreq(kCoreClock, &freq); PRINTF("\r\n#################### Power Manager Demo ####################\r\n\r\n"); PRINTF(" Core Clock = %dHz \r\n", freq); displayPowerMode(); PRINTF("\r\nSelect the desired operation \r\n\r\n"); PRINTF("Press %c for enter: RUN - Normal RUN mode\r\n",kDemoRun); PRINTF("Press %c for enter: Wait - Wait mode\r\n",kDemoWait); PRINTF("Press %c for enter: Stop - Stop mode\r\n",kDemoStop); PRINTF("Press %c for enter: VLPR - Very Low Power Run mode\r\n",kDemoVlpr); PRINTF("Press %c for enter: VLPW - Very Low Power Wait mode\r\n",kDemoVlpw); PRINTF("Press %c for enter: VLPS - Very Low Power Stop mode\r\n",kDemoVlps); PRINTF("Press %c for enter: LLS3 - Low Leakage Stop mode\r\n",kDemoLls); PRINTF("Press %c for enter: VLLS0 - Very Low Leakage Stop 0 mode\r\n",kDemoVlls0); PRINTF("Press %c for enter: VLLS1 - Very Low Leakage Stop 1 mode\r\n",kDemoVlls1); PRINTF("Press %c for enter: VLLS2 - Very Low Leakage Stop 2 mode\r\n",kDemoVlls2); PRINTF("Press %c for enter: VLLS3 - Very Low Leakage Stop 3 mode\r\n",kDemoVlls3); PRINTF("Press %c for enter: HSRUN - High Speed RUN mode\r\n",kDemoHsRun); PRINTF("\r\nWaiting for key press..\r\n\r\n"); // Wait for user response testVal = (demo_power_modes_t)GETCHAR(); if ((testVal >= 'a') && (testVal <= 'z')) { testVal -= 'a' - 'A'; } if (testVal > kDemoMin && testVal < kDemoMax) { mode = testVal - kDemoMin - 1; switch (testVal) { case kDemoWait: if (POWER_SYS_GetCurrentMode() == kPowerManagerVlpr) { PRINTF("Can not go from VLPR to WAIT directly\r\n"); break; } if (POWER_SYS_GetCurrentMode() == kPowerManagerHsrun) { PRINTF("Can not go from HSRUN to WAIT directly\r\n"); break; } setWakeUpSource(selectWakeUpSource(testVal),"Wait mode"); ret = POWER_SYS_SetMode(mode, kPowerManagerPolicyAgreement); CHECK_RET_VAL(ret, mode); break; case kDemoStop: if (POWER_SYS_GetCurrentMode() == kPowerManagerVlpr) { PRINTF("Can not go from VLPR to STOP directly\r\n"); break; } if (POWER_SYS_GetCurrentMode() == kPowerManagerHsrun) { PRINTF("Can not go from HSRUN to STOP directly\r\n"); break; } setWakeUpSource(selectWakeUpSource(testVal),"Stop mode"); ret = POWER_SYS_SetMode(mode, kPowerManagerPolicyAgreement); CHECK_RET_VAL(ret, mode); // update Clock Mode update_clock_mode(CLOCK_RUN); break; case kDemoVlpr: if (POWER_SYS_GetCurrentMode() == kPowerManagerHsrun) { PRINTF("Can not go from HSRUN to VLPR directly\r\n"); break; } if(kPowerManagerVlpr != POWER_SYS_GetCurrentMode()) { /* If apps default CM config mode is not VLPR, but needs to enter VLPR, and real CM config is not VLPR, then we need to update it to VLPR mode here. Otherwise pass through. */ update_clock_mode(CLOCK_VLPR); PRINTF("Entering Very Low Power Run mode\r\n"); ret = POWER_SYS_SetMode(mode, kPowerManagerPolicyAgreement); CHECK_RET_VAL(ret, mode); } else { PRINTF("Very Low Power Run mode already active\r\n"); } break; case kDemoVlpw: if (POWER_SYS_GetCurrentMode() == kPowerManagerRun) { PRINTF("Can not go from RUN to VLPW directly\r\n"); break; } if (POWER_SYS_GetCurrentMode() == kPowerManagerHsrun) { PRINTF("Can not go from HSRUN to VLPW directly\r\n"); break; } setWakeUpSource(selectWakeUpSource(testVal),"Very Low Wait mode"); ret = POWER_SYS_SetMode(mode, kPowerManagerPolicyAgreement); CHECK_RET_VAL(ret, mode); break; case kDemoVlps: if (POWER_SYS_GetCurrentMode() == kPowerManagerHsrun) { PRINTF("Can not go from HSRUN to VLPS directly\r\n"); break; } setWakeUpSource(selectWakeUpSource(testVal),"Very Low Power Stop mode"); ret = POWER_SYS_SetMode(mode, kPowerManagerPolicyAgreement); if (POWER_SYS_GetCurrentMode() == kPowerManagerRun) { // update Clock Mode to Run update_clock_mode(CLOCK_RUN); } CHECK_RET_VAL(ret, mode); break; case kDemoLls: if (POWER_SYS_GetCurrentMode() == kPowerManagerHsrun) { PRINTF("Can not go from HSRUN to LLSx directly\r\n"); break; } setWakeUpSource(selectWakeUpSource(testVal),"Low Leakage Stop mode 3"); ret = POWER_SYS_SetMode(mode, kPowerManagerPolicyAgreement); // Check the mode LLS was entered if(kPowerManagerVlpr != POWER_SYS_GetCurrentMode()) { update_clock_mode(CLOCK_RUN); } CHECK_RET_VAL(ret, mode); break; case kDemoVlls0: if (POWER_SYS_GetCurrentMode() == kPowerManagerHsrun) { PRINTF("Can not go from HSRUN to VLLS0 directly\r\n"); break; } setWakeUpSource(selectWakeUpSource(testVal),"Very Low Leakage Stop 0 mode"); PRINTF("Wake up goes through Reset sequence.\r\n"); ret = POWER_SYS_SetMode(mode, kPowerManagerPolicyAgreement); CHECK_RET_VAL(ret, mode); break; case kDemoVlls1: if (POWER_SYS_GetCurrentMode() == kPowerManagerHsrun) { PRINTF("Can not go from HSRUN to VLLS1 directly\r\n"); break; } setWakeUpSource(selectWakeUpSource(testVal),"Very Low Leakage Stop 1 mode"); PRINTF("Wake up goes through Reset sequence.\r\n"); ret = POWER_SYS_SetMode(mode, kPowerManagerPolicyAgreement); CHECK_RET_VAL(ret, mode); break; case kDemoVlls2: if (POWER_SYS_GetCurrentMode() == kPowerManagerHsrun) { PRINTF("Can not go from HSRUN to VLLS2 directly\r\n"); break; } setWakeUpSource(selectWakeUpSource(testVal),"Very Low Leakage Stop 2 mode"); PRINTF("Wake up goes through Reset sequence.\r\n"); ret = POWER_SYS_SetMode(mode, kPowerManagerPolicyAgreement); CHECK_RET_VAL(ret, mode); break; case kDemoVlls3: if (POWER_SYS_GetCurrentMode() == kPowerManagerHsrun) { PRINTF("Can not go from HSRUN to VLLS3 directly\r\n"); break; } setWakeUpSource(selectWakeUpSource(testVal),"Very Low Leakage Stop 3 mode"); PRINTF("Wake up goes through Reset sequence.\r\n"); ret = POWER_SYS_SetMode(mode, kPowerManagerPolicyAgreement); CHECK_RET_VAL(ret, mode); break; case kDemoRun: /* Need to decrease clock frequence before back RUN mode from HSRUN */ if (POWER_SYS_GetCurrentMode() == kPowerManagerHsrun) { update_clock_mode(CLOCK_RUN); } ret = POWER_SYS_SetMode(mode, kPowerManagerPolicyAgreement); if (ret != kPowerManagerSuccess) { PRINTF("POWER_SYS_SetMode(%u) returned unexpected status : %u\r\n",mode,ret); } else { update_clock_mode(CLOCK_RUN); } break; case kDemoHsRun: if (POWER_SYS_GetCurrentMode() == kPowerManagerVlpr) { PRINTF("Can not go from HSRUN to VLPR directly\r\n"); break; } ret = POWER_SYS_SetMode(mode, kPowerManagerPolicyAgreement); if (ret != kPowerManagerSuccess) { PRINTF("POWER_SYS_SetMode(%u) returned unexpected status : %u\r\n",mode,ret); } else { update_clock_mode(CLOCK_HSRUN); } break; default: PRINTF("Wrong value"); break; } PRINTF("\r\nNext loop\r\n"); } } }