Пример #1
0
/*!
 * @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");
        }
    }
}
Пример #2
0
/*******************************************************************************
 * Main function for application.
 ******************************************************************************/
void lab2_power(void)
{
    demo_power_modes_t testVal = kDemoRun;
    volatile uint8_t powerMode;
    uint8_t clockManagerMode = CLOCK_RUN;
    uint32_t freq = 0;

    //*******************************************************
	//* Step 1: Initialize the Clock Manager  configurations.
	//*******************************************************

    // Create list of supported clock configurations.  These are taken from the
    // board.c file and will be passed into CLOCK_SYS_Init().
    clock_manager_user_config_t const *clockConfigs[] =
    {
        NULL,
        &g_defaultClockConfigVlpr,
        &g_defaultClockConfigRun,
        &g_defaultClockConfigHsrun,
    };

    //*****************************************************
	//* Step 2: Configure Clock Manager callback function.
	//*****************************************************

    // Clock Manager callback function.
    clock_manager_callback_user_config_t clockManagerCallbackCfg =
    {
        .callback     = clockManagerCallback,
        .callbackType = kClockManagerCallbackBeforeAfter,
        .callbackData = NULL
    };

    clock_manager_callback_user_config_t *clockCallbacks[] =
    {
        &clockManagerCallbackCfg
    };

    //*****************************************************
	//* Step 3: Initialize the Clock Manager.
	//*****************************************************

    // Pass in configuration and callback data to Clock Manager.
	CLOCK_SYS_Init(clockConfigs,
				   CLOCK_NUMBER_OF_CONFIGURATIONS,
				   clockCallbacks,
				   ARRAY_SIZE(clockCallbacks));

	// Set to RUN mode.
	CLOCK_SYS_UpdateConfiguration(CLOCK_RUN, kClockManagerPolicyForcible);

    //*****************************************************
    //* Step 4: Set up supported power mode structures.
    //*****************************************************

    const power_manager_user_config_t runConfig =
    {
        .mode = kPowerManagerRun,
        .sleepOnExitValue = false,
    };

    power_manager_user_config_t hsrunConfig = runConfig;
    hsrunConfig.mode = kPowerManagerHsrun;

    power_manager_user_config_t waitConfig  = runConfig;
    waitConfig.mode = kPowerManagerWait;

    power_manager_user_config_t stopConfig  = runConfig;
    stopConfig.mode = kPowerManagerStop;

    power_manager_user_config_t vlprConfig   = runConfig;
    vlprConfig.mode = kPowerManagerVlpr;

    power_manager_user_config_t vlpwConfig	= runConfig;
    vlpwConfig.mode = kPowerManagerVlpw;

    power_manager_user_config_t vlpsConfig  = runConfig;
    vlpsConfig.mode = kPowerManagerVlps;

    power_manager_user_config_t lls3Config   = runConfig;
    lls3Config.mode = kPowerManagerLls3;

    power_manager_user_config_t vlls0Config   = runConfig;
    vlls0Config.mode = kPowerManagerVlls0;

    //***********************************************************
    //* Step 5: Configure managed power configurations structure.
    //***********************************************************

    // Create the list of supported modes to pass into the Power Manager.
    power_manager_user_config_t const *powerConfigs[] =
    {
    	&runConfig,
		&hsrunConfig,
		&waitConfig,
		&stopConfig,
		&vlprConfig,
		&vlpwConfig,
		&vlpsConfig,
		&lls3Config,
		&vlls0Config
    };

    //*****************************************************
	//* Step 6: Configure Power Manager callback function.
	//*****************************************************

    // Initializes callback configuration structure for the Power Manager.
    power_manager_callback_user_config_t powerManagerCallbackCfg =
    {
		.callback  	  = powerManagerCallback,
		.callbackType = kPowerManagerCallbackBeforeAfter,
		.callbackData = NULL
    };

    // Initializes array of pointers to power manager callbacks.
    power_manager_callback_user_config_t *powerCallbacks[] =
    {
    	&powerManagerCallbackCfg
    };

    //*****************************************************
    //* Step 7: Initialize the Power Manager.
    //*****************************************************

    // Pass power configurations and callback info to Power Manager.
    POWER_SYS_Init(powerConfigs,
    			   sizeof(powerConfigs) / sizeof(power_manager_user_config_t *),
				   powerCallbacks,
				   ARRAY_SIZE(powerCallbacks));

    // Initialize system to RUN mode.
    powerMode = kDemoRun - kDemoMin - 1;
    POWER_SYS_SetMode(powerMode, kPowerManagerPolicyAgreement);

    //**************************************************************
    //* Step 8: Configure the rest of the chip for this application.
    //**************************************************************

    // Configure pin mux for UART functionality.
    configure_uart_pins(BOARD_DEBUG_UART_INSTANCE);

    // Initializes GPIO driver for LEDs and buttons
    GPIO_DRV_Init(switchPins, ledPins);

    // Initialize the debug UART console.
    DbgConsole_Init(BOARD_DEBUG_UART_INSTANCE, BOARD_LOW_POWER_UART_BAUD,
    				kDebugConsoleUART);

    // Enable PORTC clock for GPIO/LLWU interrupt.
    CLOCK_SYS_EnablePortClock(2);

    // Enables falling edge interrupt for switch SW2.
    PORT_HAL_SetMuxMode(BOARD_SW_LLWU_BASE, BOARD_SW_LLWU_PIN, kPortMuxAsGpio);
    PORT_HAL_SetPinIntMode(BOARD_SW_LLWU_BASE, BOARD_SW_LLWU_PIN, kPortIntFallingEdge);

    // Enable GPIO interrupt.
    INT_SYS_EnableIRQ(PORTC_IRQn);

    // Configure the LLWU.
    LLWU_HAL_ClearExternalPinWakeupFlag(LLWU, BOARD_SW_LLWU_EXT_PIN);
    LLWU_HAL_SetExternalInputPinMode(LLWU, kLlwuExternalPinFallingEdge, BOARD_SW_LLWU_EXT_PIN);

    // Enable LLWU interrupt.
    INT_SYS_EnableIRQ(LLWU_IRQn);

    // Main loop.
    while (1)
    {
    	// Get the system clock frequency and current clock configuration to print out.
        CLOCK_SYS_GetFreq(kCoreClock, &freq);
        clockManagerMode = CLOCK_SYS_GetCurrentConfiguration();

        PRINTF("\n\r####################  Power Manager Demo ####################\n\n\r");
        PRINTF("    Core Clock   = %d MHz \n\r", freq / 1000000);
        PRINTF("    Current Mode = ");

        switch(clockManagerMode)
        {
			case CLOCK_RUN:
				PRINTF("RUN\n\r");
				break;
			case CLOCK_VLPR:
				PRINTF("VLPR\n\r");
				break;
			case CLOCK_HSRUN:
				PRINTF("HSRUN\n\r");
				break;
        }

        PRINTF("\n\rSelect the desired operation \n\n\r");
        PRINTF("Press  %c for enter: RUN   - Normal RUN mode\n\r", kDemoRun);
        PRINTF("Press  %c for enter: HSRUN - High Speed RUN mode\n\r", kDemoHsRun);
        PRINTF("Press  %c for enter: Wait  - Wait mode\n\r", kDemoWait);
        PRINTF("Press  %c for enter: Stop  - Stop mode\n\r", kDemoStop);
        PRINTF("Press  %c for enter: VLPR  - Very Low Power Run mode\n\r", kDemoVlpr);
        PRINTF("Press  %c for enter: VLPW  - Very Low Power Wait mode\n\r", kDemoVlpw);
        PRINTF("Press  %c for enter: VLPS  - Very Low Power Stop mode\n\r", kDemoVlps);
        PRINTF("Press  %c for enter: LLS3  - Low Leakage Stop mode\n\r", kDemoLls3);
        PRINTF("Press  %c for enter: VLLS0 - Very Low Leakage Stop mode 0\n\r", kDemoVlls0);
        //PRINTF("Press  %c for enter: VLLS3 - Very Low Leakage Stop mode 3\n\r", kDemoVlls3);

        PRINTF("\n\rWaiting for key press...\n\r\n\r");

        // Wait for user input.
        testVal = (demo_power_modes_t)GETCHAR();

        if ((testVal >= 'a') && (testVal <= 'z'))
        {
            testVal -= 'a' - 'A';
        }

        if (testVal > kDemoMin && testVal < kDemoMax)
        {
        	// Obtain Power Manager readable version of the value passed in.
        	powerMode = testVal - kDemoMin - 1;

        	// Switch to selected mode.
            switch (testVal)
            {
				case kDemoRun:

					if (POWER_SYS_GetCurrentMode() == kPowerManagerRun)
					{
						PRINTF("Run mode already active.\n\r");
						break;
					}

					// Update the power configuration.
					POWER_SYS_SetMode(powerMode, kPowerManagerPolicyAgreement);

					// Update the clock configuration.
					CLOCK_SYS_UpdateConfiguration(CLOCK_RUN, kClockManagerPolicyAgreement);

					break;

				case kDemoHsRun:

					if (POWER_SYS_GetCurrentMode() == kPowerManagerHsrun)
					{
						PRINTF("High Speed Run mode already active.\n\r");
						break;
					}

					// Update the power configuration.
					POWER_SYS_SetMode(powerMode, kPowerManagerPolicyAgreement);

					// Update the clock configuration.
					CLOCK_SYS_UpdateConfiguration(CLOCK_HSRUN, kClockManagerPolicyAgreement);

					break;

                case kDemoWait:

                    if (POWER_SYS_GetCurrentMode() == kPowerManagerVlpr)
                    {
                        PRINTF("Cannot go from VLPR to WAIT directly...\n\r");
                        break;
                    }
                    if (POWER_SYS_GetCurrentMode() == kPowerManagerHsrun)
                    {
                        PRINTF("Cannot go from HSRUN to WAIT directly...\n\r");
                        break;
                    }

                    PRINTF("Entering WAIT mode. Press SW2 to wake...\n\r");

                    // Update the power configuration.
                    POWER_SYS_SetMode(powerMode, kPowerManagerPolicyAgreement);

                    break;

                case kDemoStop:

                    if (POWER_SYS_GetCurrentMode() == kPowerManagerVlpr)
                    {
                        PRINTF("Cannot go from VLPR to STOP directly...\n\r");
                        break;
                    }
                    if (POWER_SYS_GetCurrentMode() == kPowerManagerHsrun)
                    {
                        PRINTF("Cannot go from HSRUN to STOP directly...\n\r");
                        break;
                    }

                    PRINTF("Entering STOP mode. Press SW2 to wake...\n\r");

                    // Update the power configuration.
                    POWER_SYS_SetMode(powerMode, kPowerManagerPolicyAgreement);

                    // Update the clock configuration.
                    CLOCK_SYS_UpdateConfiguration(clockManagerMode, kClockManagerPolicyAgreement);

                    break;

                case kDemoVlpr:

                    if (POWER_SYS_GetCurrentMode() == kPowerManagerHsrun)
                    {
                        PRINTF("Cannot go from HSRUN to VLPR directly...\n\r");
                        break;
                    }

                    if(POWER_SYS_GetCurrentMode() != kPowerManagerVlpr)
                    {
                    	// Update the clock configuration PRIOR to entering VLPR.
                    	CLOCK_SYS_UpdateConfiguration(CLOCK_VLPR, kClockManagerPolicyAgreement);

                    	// Update the power configuration.
                        POWER_SYS_SetMode(powerMode, kPowerManagerPolicyAgreement);
                    }
                    else
                    {
                        PRINTF("Very Low Power Run mode already active.\n\r");
                    }

                    break;

                case kDemoVlpw:

                    if (POWER_SYS_GetCurrentMode() == kPowerManagerRun)
                    {
                        PRINTF("Cannot go from RUN to VLPW directly...\n\r");
                        break;
                    }

                    if (POWER_SYS_GetCurrentMode() == kPowerManagerHsrun)
                    {
                        PRINTF("Cannot go from HSRUN to VLPW directly...\n\r");
                        break;
                    }

                    PRINTF("Entering VLPW mode. Press SW2 to wake...\n\r");

                    // Update the power configuration.
                    POWER_SYS_SetMode(powerMode, kPowerManagerPolicyAgreement);

                    break;

                case kDemoVlps:

                    if (POWER_SYS_GetCurrentMode() == kPowerManagerHsrun)
                    {
                        PRINTF("Cannot go from HSRUN to VLPS directly...\n\r");
                        break;
                    }

                    PRINTF("Entering VLPS mode. Press SW2 to wake...\n\r");

                    // Update the power configuration.
                    POWER_SYS_SetMode(powerMode, kPowerManagerPolicyAgreement);

                    // If we entered via RUN mode, restore clock settings.
                    if (POWER_SYS_GetCurrentMode() == kPowerManagerRun)
					{
                    	CLOCK_SYS_UpdateConfiguration(clockManagerMode, kClockManagerPolicyAgreement);
					}

                    break;

                case kDemoLls3:

                    if (POWER_SYS_GetCurrentMode() == kPowerManagerHsrun)
                    {
                        PRINTF("Cannot go from HSRUN to LLSx directly...\n\r");
                        break;
                    }

                    PRINTF("Entering LLS3 mode. Press SW2 to wake...\n\r");

                    // Update the power configuration.
                    POWER_SYS_SetMode(powerMode, kPowerManagerPolicyAgreement);

                    // Check the mode active mode prior to LLS3 entry.
                    if(POWER_SYS_GetCurrentMode() != kPowerManagerVlpr)
                    {
                        CLOCK_SYS_UpdateConfiguration(clockManagerMode, kClockManagerPolicyAgreement);
                    }

                    break;

                case kDemoVlls0:

                    if (POWER_SYS_GetCurrentMode() == kPowerManagerHsrun)
                    {
                        PRINTF("Cannot go from HSRUN to VLLSx directly...\n\r");
                        break;
                    }

                    PRINTF("Press SW2 to wake. VLLSx wake goes through RESET sequence.\n\r");

                    // Update the power configuration.
                    POWER_SYS_SetMode(powerMode, kPowerManagerPolicyAgreement);

                    break;

                default:
                    PRINTF("Bad value.");
                    break;
            }

            PRINTF("\n\rNext loop\n\r");
        }
    }
}
Пример #3
0
void task_lpm(task_param_t param)
{
    demo_power_modes_t testVal;
    uint8_t cmConfigMode = CLOCK_RUN;
    uint8_t mode;
    power_manager_error_code_t ret;
    uint32_t freq = 0;

    rtc_datetime_t date;
    memset(&date, 0, sizeof(rtc_datetime_t));
    memset(&cmCallbackData, 0, sizeof(lptmrStructure_t));
    cmCallbackData.instance = PM_RTOS_DEMO_LPTMR_FUNC_INSTANCE;
    lptmr_user_config_t *lptmrUserConfig = &(cmCallbackData.lptmrUserConfig);
    lptmr_state_t *lptmrState = &(cmCallbackData.lptmrState);

    CLOCK_SYS_Init(g_defaultClockConfigurations,
                   CLOCK_NUMBER_OF_CONFIGURATIONS,
                   cm_callback_tbl,
                   cm_callback_tbl_size);

    CLOCK_SYS_UpdateConfiguration(cmConfigMode, kClockManagerPolicyForcible);

    // Set a start date time and start RTC
    date.year = 2014;
    date.month = 4U;
    date.day = 30U;
    date.hour = 14U;
    date.minute = 0U;
    date.second = 0U;
    rtcInit(PM_RTOS_DEMO_RTC_FUNC_INSTANCE, &date);
    lptmrUserConfig->timerMode = kLptmrTimerModeTimeCounter; // Use LPTMR in Time Counter mode
    lptmrUserConfig->freeRunningEnable = false; // When hit compare value, set counter back to zero
    lptmrUserConfig->prescalerEnable = false; // bypass prescaler
    lptmrUserConfig->prescalerClockSource = kClockLptmrSrcLpoClk; // use 1kHz Low Power Clock
    lptmrUserConfig->isInterruptEnabled = false;
    lptmrInit(lptmrUserConfig, lptmrState);

    // initialize power manager driver
    POWER_SYS_Init(powerConfigs,
                   powerConfigsSize,
                   pm_callback_tbl,
                   pm_callback_tbl_size);

    // Enables LLWU interrupt
    INT_SYS_EnableIRQ(LLWU_IRQn);

#if (defined FSL_RTOS_BM)
    PRINTF("\n\r####################  Power Manager BM Demo ####################\n\n\r");
#elif (defined FSL_RTOS_FREE_RTOS)
    PRINTF("\n\r####################  Power Manager FreeRTOS Demo ####################\n\n\r");
#elif (defined FSL_RTOS_MQX)
    PRINTF("\n\r####################  Power Manager MQX Demo ####################\n\n\r");
#elif (defined FSL_RTOS_UCOSII)
    PRINTF("\n\r####################  Power Manager Ucosii Demo ####################\n\n\r");
#elif (defined FSL_RTOS_UCOSIII)
    PRINTF("\n\r####################  Power Manager Ucosiii Demo ####################\n\n\r");
#else
    PRINTF("\n\rUnknown RTOS\n\n\r");
#endif

    while (1)
    {
        mode = 0;
        CLOCK_SYS_GetFreq(kCoreClock, &freq);
        PRINTF("    Core Clock = %luHz \n\r", freq);
        displayPowerMode();
        PRINTF("\n\rSelect the desired operation \n\n\r");
        PRINTF("Press  %c for enter: RUN   - Normal RUN mode\n\r",kDemoRun);
        PRINTF("Press  %c for enter: Wait  - Wait mode\n\r",kDemoWait);
        PRINTF("Press  %c for enter: Stop  - Stop mode\n\r",kDemoStop);
        PRINTF("Press  %c for enter: VLPR  - Very Low Power Run mode\n\r",kDemoVlpr);
        PRINTF("Press  %c for enter: VLPW  - Very Low Power Wait mode\n\r",kDemoVlpw);
        PRINTF("Press  %c for enter: VLPS  - Very Low Power Stop mode\n\r",kDemoVlps);

        PRINTF("Press  %c for enter: LLS   - Low Leakage Stop mode\n\r",kDemoLls);

        PRINTF("Press  %c for enter: VLLS1 - Very Low Leakage Stop 1 mode\n\r",kDemoVlls1);

        PRINTF("Press  %c for enter: VLLS2 - Very Low Leakage Stop 2 mode\n\r",kDemoVlls2);

        PRINTF("Press  %c for enter: VLLS3 - Very Low Leakage Stop 3 mode\n\r",kDemoVlls3);
        PRINTF("Press  %c to get current chip temperature\n\r",KDemoADC);

        PRINTF("------------------------------------------------------------\n\r");
        PRINTF("\n\rWaiting for key press..\n\r\n\r");

        // Wait for user response
        testVal = (demo_power_modes_t)getInput();
        PRINTF("You pressed: '%c'\r\n", testVal);

        // convert lower to upper character.
        if(testVal > kDemoMax)
        {
            testVal = (demo_power_modes_t)(testVal + 'A' - 'a');
        }

        mode = testVal - kDemoMin - 1;

        switch (testVal)
        {
            case kDemoWait:
                if (POWER_SYS_GetCurrentMode() == kPowerManagerVlpr)
                {
                    PRINTF("Can not go from VLPR to WAIT directly\n\r");
                    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\n\r");
                    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(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\n\r");

                    ret = POWER_SYS_SetMode(mode, kPowerManagerPolicyAgreement);

                    CHECK_RET_VAL(ret, mode);
                }
                else
                {
                    PRINTF("Very Low Power Run mode already active\n\r");
                }
                break;

            case kDemoVlpw:
                if (POWER_SYS_GetCurrentMode() == kPowerManagerRun)
                {
                    PRINTF("Can not go from RUN to VLPW directly\n\r");
                    break;
                }

                setWakeUpSource(selectWakeUpSource(testVal),"Very Low Wait 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 kDemoVlps:
                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:
                setWakeUpSource(selectWakeUpSource(testVal),"Low Leakage Stop mode");
                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 kDemoVlls1:
                setWakeUpSource(selectWakeUpSource(testVal),"Very Low Leakage Stop 1 mode");
                PRINTF("Wake up goes through Reset sequence.\n\r");

                ret = POWER_SYS_SetMode(mode, kPowerManagerPolicyAgreement);
                CHECK_RET_VAL(ret, mode);

                break;

            case kDemoVlls2:
                setWakeUpSource(selectWakeUpSource(testVal),"Very Low Leakage Stop 2 mode");
                PRINTF("Wake up goes through Reset sequence.\n\r");

                ret = POWER_SYS_SetMode(mode, kPowerManagerPolicyAgreement);
                CHECK_RET_VAL(ret, mode);

                break;
            case kDemoVlls3:
                setWakeUpSource(selectWakeUpSource(testVal),"Very Low Leakage Stop 3 mode");
                PRINTF("Wake up goes through Reset sequence.\n\r");

                ret = POWER_SYS_SetMode(mode, kPowerManagerPolicyAgreement);
                CHECK_RET_VAL(ret, mode);

                break;

            case kDemoRun:

                ret = POWER_SYS_SetMode(mode, kPowerManagerPolicyAgreement);
                if (ret != kPowerManagerSuccess)
                {
                    PRINTF("POWER_SYS_SetMode(%u) returned unexpected status : %u\n\r",mode,ret);
                }
                else
                {
                    update_clock_mode(CLOCK_RUN);
                }
                break;
            case KDemoADC:
                adc16PrintTemperature();
            break;

            default:
                break;
        }
        PRINTF("\n\rNext loop\n\r\n\r");
    }
}
Пример #4
0
void HEXIWEAR_startup( task_param_t param )
{
    uint8_t
    status = 0;

    /** output GPIO config */

    GPIO_DRV_Init( NULL, OLED_cfg );
    GPIO_DRV_Init( NULL, FLASH_cfg );
    GPIO_DRV_Init( NULL, PWR_cfg );
    GPIO_DRV_Init( NULL, VIBRO_cfg );
    GPIO_DRV_Init( NULL, RGB_cfg );
    GPIO_DRV_Init( NULL, KW40_GPIO_cfg );
    GPIO_DRV_Init( NULL, REL_GPIO_cfg );


    /** input GPIO config */

    GPIO_DRV_Init( BAT_CHG_cfg, NULL );
    GPIO_DRV_Init( TAP_cfg, NULL );

    // lajkatest
    // GPIO_DRV_Init( GPIO_TEST_CFG, NULL );

#if defined( HEXIWEAR_DEBUG )
    GPIO_DRV_Init( NULL, DEBUG_cfg );
#endif

    power_ResetKW40();
    timer_Init( HEXIWEAR_TIMER_SENSOR );

    status |= RTC_Init();
    status |= FLASH_Init( &flashModule, &flashSettings );

    // intern flash initialization
    INTFLASH_Init();

    // enable power save by default
    // power_EnablePowerSave();
    power_DisablePowerSave();

    // set to deep sleep by default
    // power_SetSleepMode( POWER_SLEEP_TYPE_DEEP );

    // RGB off
    FLASH_SetOFF();

    // visual indication for testing HR sensor
//  MAXIM_Test();

    /** create basic tasks */
    status |= Notification_Init();
    status |= HostInterface_Init();
    status |= sensor_Init();
    status |= power_Init();
    status |= GuiDriver_Init();

    haptic_MutexCreate();

    sensor_InitAcc();

    /** set GPIO interrupt for the tap function */
    PORT_HAL_SetPinIntMode( PORTC, 1, kPortIntFallingEdge );

    /** set charging battery interrupt */
    PORT_HAL_SetPinIntMode( PORTC, 12, kPortIntEitherEdge );
    NVIC_SetPriority( PORTC_IRQn, HEXIWEAR_CHG_IRQ_PRIO );
    INT_SYS_EnableIRQ( PORTC_IRQn );

    // status |= Run_USB_Task();

    if ( HEXIWEAR_STATUS_SUCCESS != status )
    {
        catch( CATCH_INIT );
    }

    /** check for settings in flash at startup */
    gui_sensorTag_CheckAtStartup();
    haptic_CheckAtStartup();

    CLOCK_SYS_Init( g_clockManConfigsArr, FSL_CLOCK_MANAGER_CONFIG_CNT, g_clockManCallbacksArr, FSL_CLOCK_MANAGER_CALLBACK_CNT );
    POWER_SYS_Init( powerConfigsArr, 2U, powerStaticCallbacksConfigsArr , 2U );

    // make battery readings regular
    sensor_SetPacketTargets( PACKET_BAT, sensor_GetPacketTargets( PACKET_BAT) | PACKET_PUSH_POWER_MGMT, true );

    volatile uint32_t
    foo = CLOCK_SYS_GetSystemClockFreq();

    while (1)
    {
        OSA_TaskDestroy( NULL );
    }
}
Пример #5
0
void HEXIWEAR_startup( task_param_t param )
{
  uint8_t
    status = 0;

  /** output GPIO configuration */

  GPIO_DRV_Init( NULL, OLED_cfg );
  GPIO_DRV_Init( NULL, FLASH_cfg );
  GPIO_DRV_Init( NULL, PWR_cfg );
  GPIO_DRV_Init( NULL, VIBRO_cfg );
  GPIO_DRV_Init( NULL, RGB_cfg );
  GPIO_DRV_Init( NULL, KW40_GPIO_cfg );


  /** input GPIO configuration */

  GPIO_DRV_Init( BAT_CHG_cfg, NULL );
  GPIO_DRV_Init( TAP_cfg, NULL );

#if defined( HEXIWEAR_DEBUG )
  GPIO_DRV_Init( NULL, DEBUG_cfg );
#endif

  power_ResetKW40();
  timer_Init( HEXIWEAR_TIMER_SENSOR );

  status |= RTC_Init();
  status |= FLASH_Init( &flashModule, &flashSettings );

  // intern flash initialization
  INTFLASH_Init();

  // RGB off
  FLASH_SetOFF();

  /** create basic tasks */
  status |= Notification_Init();
  status |= HostInterface_Init();
  status |= sensor_Init();
  status |= GuiDriver_Init();

  haptic_MutexCreate();

  sensor_InitAcc();

  /** set GPIO interrupt for the tap function */
  PORT_HAL_SetPinIntMode( PORTC, 1, kPortIntFallingEdge );

  /** set charging battery interrupt */
  PORT_HAL_SetPinIntMode( PORTC, 12, kPortIntEitherEdge );
  NVIC_SetPriority( PORTC_IRQn, HEXIWEAR_CHG_IRQ_PRIO );
  INT_SYS_EnableIRQ( PORTC_IRQn );

  if ( HEXIWEAR_STATUS_SUCCESS != status )
  {
    catch( CATCH_INIT );
  }

  /** check for settings in flash at startup */
  gui_sensorTag_CheckAtStartup();
  haptic_CheckAtStartup();

  CLOCK_SYS_Init( g_clockManConfigsArr, FSL_CLOCK_MANAGER_CONFIG_CNT, g_clockManCallbacksArr, FSL_CLOCK_MANAGER_CALLBACK_CNT );
  POWER_SYS_Init( powerConfigsArr, 2U, powerStaticCallbacksConfigsArr , 2U );

  // turn on regular battery readings
  sensor_SetPacketTargets( PACKET_BAT, sensor_GetPacketTargets( PACKET_BAT) | PACKET_PUSH_POWER_MGMT, true );

  while (1)
  {
    power_Init();
    OSA_TaskDestroy( NULL );
  }
}