/*FUNCTION*------------------------------------------------------------- * * Function Name : Custom_Delay * PARAMS : Delay in msec * * Returned Value : none * Comments : * Starts a timer for specified number of msec, puts EFM in EM2 mode. * EFM will wake up when timer fires. * *END*-----------------------------------------------------------------*/ void Custom_Delay(A_UINT32 delay) { #if MQX_VERSION == (402) /* Install the timer */ if (MQX_OK != _lpt_install (0, delay*1000 , LPT_FLAG_CLOCK_SOURCE_LPO, 2, timer_wakeup_isr, TRUE)) { printf ("\nError during installation of timer interrupt!\n"); _task_block(); } /* Stop the timer */ _lpt_run (0, FALSE); /* start timer */ _lpt_run (CUSTOM_DELAY_TMR, TRUE); #else if (MQX_OK != hwtimer_init(&hwtimer2, &BSP_HWTIMER2_DEV, BSP_HWTIMER2_ID, (BSP_DEFAULT_MQX_HARDWARE_INTERRUPT_LEVEL_MAX + 1))) { printf ("\nError during installation of timer interrupt!\n"); _task_block(); } hwtimer_set_period(&hwtimer2, BSP_HWTIMER2_SOURCE_CLK, delay*1000); hwtimer_callback_reg(&hwtimer2,(HWTIMER_CALLBACK_FPTR)timer_wakeup_isr, NULL); /* Start hwtimer2*/ hwtimer_start(&hwtimer2); #endif /* Setting operation mode to LPM_OPERATION_MODE_SLEEP */ _lpm_set_operation_mode (LPM_OPERATION_MODE_STOP); /* Return to RUN mode */ _lpm_set_operation_mode (LPM_OPERATION_MODE_RUN); if (CM_ERR_OK != _lpm_set_clock_configuration(BSP_CLOCK_CONFIGURATION_DEFAULT)) { printf("Cannot change clock configuration"); _task_block(); } }
void main_task ( uint32_t initial_data ) { BSP_CLOCK_CONFIGURATION clock_configuration; printf("\n***********************************************************\n"); printf(" MQX frequency change demo\n"); printf("***********************************************************\n"); /* Create global event */ if (_lwevent_create(&app_event, LWEVENT_AUTO_CLEAR) != MQX_OK) { printf("\nCreating app_event failed.\n"); _task_block(); } printf("\n*********** DEFAULT CLOCK CONFIGURATION **********\n"); /* Get and print active clock configuration */ clock_configuration = _lpm_get_clock_configuration(); display_clock_values(clock_configuration); printf("\nPress button on the board to loop over frequency modes.\n"); while (1) { /* wait for button press */ wait_for_event(); printf("\n*************** BSP_CLOCK_CONFIGURATION_0 **************\n"); /* Switch clock configuration */ if (CM_ERR_OK != _lpm_set_clock_configuration(BSP_CLOCK_CONFIGURATION_0)) { printf("Cannot change clock configuration"); _task_block(); } /* Get active clock configuration */ clock_configuration = _lpm_get_clock_configuration(); /* Display new clock settings */ display_clock_values(clock_configuration); /* wait for button press */ wait_for_event(); printf("\n*************** BSP_CLOCK_CONFIGURATION_1 **************\n"); /* Switch clock configuration */ if (CM_ERR_OK != _lpm_set_clock_configuration(BSP_CLOCK_CONFIGURATION_1)) { printf("Cannot change clock configuration"); _task_block(); } /* Get active clock configuration */ clock_configuration = _lpm_get_clock_configuration(); /* Display new clock settings */ display_clock_values(clock_configuration); /* wait for button press */ wait_for_event(); printf("\n*************** BSP_CLOCK_CONFIGURATION_2 **************\n"); /* Switch clock configuration */ if (CM_ERR_OK != _lpm_set_clock_configuration(BSP_CLOCK_CONFIGURATION_2)) { printf("Cannot change clock configuration"); _task_block(); } /* Get active clock configuration */ clock_configuration = _lpm_get_clock_configuration(); /* Display new clock settings */ display_clock_values(clock_configuration); } }
/*TASK*----------------------------------------------------- * * Task Name : main_task * Comments : Low power modes switching. * *END*-----------------------------------------------------*/ void main_task ( uint32_t initial_data ) { LPM_OPERATION_MODE operation_mode; IDLE_LOOP_STRUCT idle_loops; uint32_t loop1; /* Initialize switches */ button_led_init(); _int_install_unexpected_isr(); _lpm_register_wakeup_callback(BSP_LLWU_INTERRUPT_VECTOR, BSP_LLWU_INTERRUPT_PRIORITY, NULL); /* Install interrupt for timer wakeup */ install_timer_interrupt(); /* Create global event */ if (_lwevent_create(&app_event, 0) != MQX_OK) { printf("\nCreating app_event failed.\n"); _task_block(); } #if (PSP_MQX_CPU_IS_KINETIS) if (_lpm_get_reset_source() != MQX_RESET_SOURCE_LLWU) printf("\nMQX Low Power Modes Demo\n"); else #endif printf("\nWake up by reset from LLWU\n"); while (1) { #if (MQX_ENABLE_HSRUN) /* Find out current mode setting */ operation_mode = _lpm_get_operation_mode(); printf("\n******************************************************************************\n"); printf("************** Operation mode : %s ***********************\n", get_operation_mode_name (operation_mode)); printf("******************************************************************************\n"); display_operation_mode_setting(operation_mode); printf( "Info: HSRUN operation mode is mapped on HSRUN power mode by default.\n" " The core runs at full clock speed.\n" " It continues the execution after entering the mode.\n" " LED2 blinks quickly, LED1 toggles after the button press.\n"); /* Wait for button press */ printf ("Press button to move to next operation mode.\n"); _lwevent_wait_ticks (&app_event, SW_EVENT_MASK, FALSE, 0); _lwevent_clear (&app_event, ALL_EVENTS_MASK); printf("\nButton pressed. Moving to next operation mode.\n"); /* Change frequency to normal run mode. */ printf("\nChanging frequency to normal run mode.\n"); if (CM_ERR_OK != _lpm_set_clock_configuration(BSP_CLOCK_CONFIGURATION_0)) { printf("Cannot change clock configuration"); _task_block(); } /* Change the operation mode */ printf ("\nSetting operation mode to %s ... ", get_operation_mode_name (operation_mode)); printf ("%s\n", _lpm_set_operation_mode (LPM_OPERATION_MODE_RUN) == 0 ? "OK" : "ERROR"); #endif /* Find out current mode setting */ operation_mode = _lpm_get_operation_mode(); printf("\n******************************************************************************\n"); printf("************** Operation mode : %s ***********************\n", get_operation_mode_name (operation_mode)); printf("******************************************************************************\n"); display_operation_mode_setting(operation_mode); printf( "Info: RUN operation mode is mapped on RUN power mode by default.\n" " The core runs at full clock speed.\n" " It continues the execution after entering the mode.\n" " LED2 blinks quickly, LED1 toggles after the button press.\n"); /* Demonstration of idle task sleep feature */ printf("\nIdle task sleep feature disabled.\n"); _lpm_idle_sleep_setup (FALSE); printf("Task suspended for 1 second to let run the idle task.\n"); _mqx_get_idle_loop_count (&idle_loops); loop1 = idle_loops.IDLE_LOOP1; _time_delay (1000); _mqx_get_idle_loop_count (&idle_loops); printf ("Idle loops per second with idle sleep disabled: %d\n", idle_loops.IDLE_LOOP1 - loop1); printf("Idle task sleep feature enabled.\n"); _lpm_idle_sleep_setup (TRUE); printf("Task suspended for 1 second to let run the idle task.\n"); _mqx_get_idle_loop_count (&idle_loops); loop1 = idle_loops.IDLE_LOOP1; _time_delay (1000); _mqx_get_idle_loop_count (&idle_loops); printf ("Idle loops per second with idle sleep enabled: %d\n", idle_loops.IDLE_LOOP1 - loop1); printf("Idle task sleep feature disabled.\n\n"); _lpm_idle_sleep_setup (FALSE); /* Wait for button press */ printf ("Press button to move to next operation mode.\n"); _lwevent_wait_ticks (&app_event, SW_EVENT_MASK, FALSE, 0); _lwevent_clear (&app_event, ALL_EVENTS_MASK); printf("\nButton pressed. Moving to next operation mode.\n"); operation_mode = LPM_OPERATION_MODE_WAIT; printf("\n******************************************************************************\n"); printf("************** Operation mode : %s **********************\n", get_operation_mode_name (operation_mode)); printf("******************************************************************************\n"); display_operation_mode_setting(operation_mode); printf( "Info: WAIT operation mode is mapped on VLPR power mode by default.\n" " It requires 2 MHz core clock and bypassed pll.\n" " Core continues the execution after entering the mode.\n" " LED2 blinks slowly, LED1 toggles after the button press.\n"); /* The LPM_OPERATION_MODE_WAIT is mapped on LPM_CPU_POWER_MODE_VLPR by default, this mode requires 2 MHz, bypassed PLL clock setting. Change clocks to appropriate mode */ printf("\nChanging frequency to 2 MHz.\n"); if (CM_ERR_OK != _lpm_set_clock_configuration(BSP_CLOCK_CONFIGURATION_2MHZ)) { printf("Cannot change clock configuration"); _task_block(); } /* Change the operation mode */ printf ("\nSetting operation mode to %s ... ", get_operation_mode_name (operation_mode)); printf ("%s\n", _lpm_set_operation_mode (LPM_OPERATION_MODE_WAIT) == 0 ? "OK" : "ERROR"); /* Wait for button press */ printf ("\nPress button to move to next operation mode.\n"); _lwevent_wait_ticks (&app_event, SW_EVENT_MASK, FALSE, 0); _lwevent_clear (&app_event, ALL_EVENTS_MASK); printf("\nButton pressed.\n"); /* Return to RUN mode */ printf ("\nSetting operation mode back to %s ... ", get_operation_mode_name (LPM_OPERATION_MODE_RUN)); printf ("%s\n", _lpm_set_operation_mode (LPM_OPERATION_MODE_RUN) == 0 ? "OK" : "ERROR"); /* Return default clock configuration */ printf("\nChanging frequency back to the default one.\n"); if (CM_ERR_OK != _lpm_set_clock_configuration(BSP_CLOCK_CONFIGURATION_DEFAULT)) { printf("Cannot change clock configuration"); _task_block(); } printf("\nMoving to next operation mode.\n"); operation_mode = LPM_OPERATION_MODE_SLEEP; printf("\n******************************************************************************\n"); printf("************** Operation mode : %s *********************\n", get_operation_mode_name (operation_mode)); printf("******************************************************************************\n"); display_operation_mode_setting(operation_mode); printf( "Info: SLEEP operation mode is mapped on WAIT power mode by default.\n" " The core is inactive in this mode, reacting only to interrupts.\n" " The LPM_CPU_POWER_MODE_FLAG_SLEEP_ON_EXIT is set on Kinetis, therefore\n" " core goes to sleep again after any isr finishes. The core will stay awake\n" " after call to _lpm_wakeup_core() from timer wakeup or serial interrupt.\n" " LED2 doesn't blink, LED1 toggles after the button press.\n"); /* Wake up in 10 seconds */ set_timer_wakeup (); /* Change the operation mode */ printf ("\nSetting operation mode to %s ... ", get_operation_mode_name (operation_mode)); printf ("%s\n", _lpm_set_operation_mode (operation_mode) == 0 ? "OK" : "ERROR"); if (LWEVENT_WAIT_TIMEOUT == _lwevent_wait_ticks (&app_event, TIMER_EVENT_MASK, FALSE, 1)) { printf("\nCore woke up by interrupt. Waiting for timer wakeup ... "); _lwevent_wait_ticks (&app_event, TIMER_EVENT_MASK, FALSE, 0); printf("OK\n"); } else { printf("\nCore woke up by timer wakeup.\n"); } _lwevent_clear (&app_event, ALL_EVENTS_MASK); /* Wait for button press */ printf ("\nPress button to move to next operation mode.\n"); _lwevent_wait_ticks (&app_event, SW_EVENT_MASK, FALSE, 0); _lwevent_clear (&app_event, ALL_EVENTS_MASK); printf("\nButton pressed. Moving to next operation mode.\n"); operation_mode = LPM_OPERATION_MODE_STOP; printf("\n******************************************************************************\n"); printf("************** Operation mode : %s **********************\n", get_operation_mode_name (operation_mode)); printf("******************************************************************************\n"); display_operation_mode_setting(operation_mode); printf( "Info: STOP operation mode is mapped to LLS power mode by default.\n" " Core and most peripherals are inactive in this mode, reacting only to\n" " specified wake up events. The events can be changed in BSP (init_lpm.c).\n" " Serial line is turned off in this mode. The core will wake up from\n" " timer wakeup interrupt.\n" " LED2 doesn't blink, LED1 toggles after the button press.\n"); /* Wake up in 10 seconds */ set_timer_wakeup (); /* Change the operation mode */ printf ("\nSetting operation mode to %s ... \n", get_operation_mode_name (operation_mode)); _lpm_set_operation_mode (operation_mode); /**************************************************************************************************/ /* SCI HW MODULE IS DISABLED AT THIS POINT - SERIAL DRIVER MUST NOT BE USED UNTIL MODE IS CHANGED */ /**************************************************************************************************/ /* Return to RUN mode */ _lpm_set_operation_mode (LPM_OPERATION_MODE_RUN); printf("\nCore is awake. Moved to next operation mode.\n"); #if MQX_ENABLE_HSRUN /* Wait for button press */ printf ("Press button to move to next operation mode.\n"); _lwevent_wait_ticks (&app_event, SW_EVENT_MASK, FALSE, 0); _lwevent_clear (&app_event, ALL_EVENTS_MASK); printf("\nButton pressed. Moving to next operation mode.\n"); /* Change the operation mode */ printf ("\nSetting operation mode to %s ... ", get_operation_mode_name (operation_mode)); printf ("%s\n", _lpm_set_operation_mode (LPM_OPERATION_MODE_HSRUN) == 0 ? "OK" : "ERROR"); /* Change frequency to HSRUN mode . */ printf("\nChanging frequency to HSRUN mode.\n"); if (CM_ERR_OK != _lpm_set_clock_configuration(BSP_CLOCK_CONFIGURATION_3)) { printf("Cannot change clock configuration"); _task_block(); } #endif } }
void main_task ( uint_32 initial_data ) { LPM_OPERATION_MODE power_mode; /* Initialize switches */ button_led_init(); /* Install interrupt for RTC alarm */ install_rtc_interrupt(); /* Create global event */ if (_lwevent_create(&app_event, 0) != MQX_OK) { printf("\nCreating app_event failed.\n"); _task_block(); } printf("\nMQX Low Power Modes Demo\n"); while (1) { /* Find out current mode setting */ power_mode = _lpm_get_operation_mode(); printf("\n******************************************************************************\n"); printf("**************** Current Mode : %s ***********************\n", predefined_power_modes_names[power_mode]); printf("******************************************************************************\n"); display_operation_mode_setting(power_mode); /* Wait for button press */ printf ("Press button to move to next operation mode.\n"); _lwevent_wait_ticks (&app_event, SW_EVENT_MASK, FALSE, 0); _lwevent_clear (&app_event, ALL_EVENTS_MASK); printf("\nButton pressed. Moving to next operation mode.\n"); power_mode = LPM_OPERATION_MODE_WAIT; printf("\n******************************************************************************\n"); printf("**************** Current Mode : %s **********************\n", predefined_power_modes_names[power_mode]); printf("******************************************************************************\n"); display_operation_mode_setting(power_mode); printf( "Info: WAIT mode is mapped on Kinets VLPR mode by default.\n" " It requires 2 MHz clock and bypassed pll.\n" " Core continues the execution after entering the mode.\n"); /* The LPM_OPERATION_MODE_WAIT is mapped on LPM_CPU_POWER_MODE_KINETIS_VLPR by default, this mode requires 2 MHz, bypassed PLL clock setting. Change clocks to appropriate mode */ printf("\nChanging frequency to 2 MHz.\n"); if (CM_ERR_OK != _lpm_set_clock_configuration(BSP_CLOCK_CONFIGURATION_2MHZ)) { printf("Cannot change clock configuration"); _task_block(); } /* Change the operation mode */ printf ("\nSetting operation mode to %s ... ", predefined_power_modes_names[power_mode]); printf ("%s\n", _lpm_set_operation_mode (LPM_OPERATION_MODE_WAIT) == 0 ? "OK" : "ERROR"); /* Wait for button press */ printf ("\nPress button to move to next operation mode.\n"); _lwevent_wait_ticks (&app_event, SW_EVENT_MASK, FALSE, 0); _lwevent_clear (&app_event, ALL_EVENTS_MASK); printf("\nButton pressed.\n"); /* Return to RUN mode */ printf ("\nSetting operation mode back to %s ... ", predefined_power_modes_names[LPM_OPERATION_MODE_RUN]); printf ("%s\n", _lpm_set_operation_mode (LPM_OPERATION_MODE_RUN) == 0 ? "OK" : "ERROR"); /* Return default clock configuration */ printf("\nChanging frequency back to the default one.\n"); if (CM_ERR_OK != _lpm_set_clock_configuration(BSP_CLOCK_CONFIGURATION_DEFAULT)) { printf("Cannot change clock configuration"); _task_block(); } printf("\nMoving to next operation mode.\n"); power_mode = LPM_OPERATION_MODE_SLEEP; printf("\n******************************************************************************\n"); printf("**************** Current Mode : %s *********************\n", predefined_power_modes_names[power_mode]); printf("******************************************************************************\n"); display_operation_mode_setting(power_mode); printf( "Info: SLEEP mode is mapped on Kinetis WAIT mode by default. Core is inactive\n" " in this mode, reacting only to interrupts.\n" " The LPM_CPU_POWER_MODE_FLAG_SLEEP_ON_EXIT is set, therefore core goes\n" " to sleep again after any isr finishes. The core will stay awake after\n" " call to _lpm_wakeup_core() from RTC or serial line interrupt.\n"); /* Wake up in 10 seconds */ set_rtc_alarm(10); /* Change the operation mode */ printf ("\nSetting operation mode to %s ... ", predefined_power_modes_names[power_mode]); printf ("%s\n", _lpm_set_operation_mode (power_mode) == 0 ? "OK" : "ERROR"); if (LWEVENT_WAIT_TIMEOUT == _lwevent_wait_ticks (&app_event, RTC_EVENT_MASK, FALSE, 1)) { printf("\nCore woke up by serial interrupt. Waiting for RTC alarm ... "); _lwevent_wait_ticks (&app_event, RTC_EVENT_MASK, FALSE, 0); printf("OK\n"); } else { printf("\nCore woke up by RTC interrupt.\n"); } _lwevent_clear (&app_event, ALL_EVENTS_MASK); /* Wait for button press */ printf ("\nPress button to move to next operation mode.\n"); _lwevent_wait_ticks (&app_event, SW_EVENT_MASK, FALSE, 0); _lwevent_clear (&app_event, ALL_EVENTS_MASK); printf("\nButton pressed. Moving to next operation mode.\n"); power_mode = LPM_OPERATION_MODE_STOP; printf("\n******************************************************************************\n"); printf("**************** Current Mode : %s **********************\n", predefined_power_modes_names[power_mode]); printf("******************************************************************************\n"); display_operation_mode_setting(power_mode); printf( "Info: STOP mode is mapped to Kinets LLS mode by default.\n" " Core and most peripherals are inactive in this mode, reacting only to\n" " specified wake up events. The events can be changed in BSP (init_lpm.c).\n" " Serial line is turned off in this mode. The core will wake up from\n" " RTC interrupt.\n"); /* Wake up in 10 seconds */ set_rtc_alarm(10); /* Change the operation mode */ printf ("\nSetting operation mode to %s ... \n", predefined_power_modes_names[power_mode]); _lpm_set_operation_mode (power_mode); /**************************************************************************************************/ /* SCI HW MODULE IS DISABLED AT THIS POINT - SERIAL DRIVER MUST NOT BE USED UNTIL MODE IS CHANGED */ /**************************************************************************************************/ /* Return to RUN mode */ _lpm_set_operation_mode (LPM_OPERATION_MODE_RUN); printf("\nCore is awake. Moved to next operation mode.\n"); } }