/** Handle key repeat timer alarm on timer wakeup */ static void handle_repeat_key_timer_alarm(TimerHandle *timer) { static MidpEvent newMidpEvent; static int midp_keycode; if (timer != NULL) { midp_keycode = (int)(get_timer_data(timer)); newMidpEvent.type = MIDP_KEY_EVENT; newMidpEvent.CHR = midp_keycode; newMidpEvent.ACTION = KEYMAP_STATE_REPEATED; midpStoreEventAndSignalForeground(newMidpEvent); timer = remove_timer(timer); set_timer_wakeup(timer, get_timer_wakeup(timer) + REPEAT_PERIOD); add_timer(timer); } }
/*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 } }