/** * Initialize the AST as event generator. */ static void init_ast(void) { struct ast_config ast_conf; /* Enable osc32 oscillator */ if (!osc_is_ready(OSC_ID_OSC32)) { osc_enable(OSC_ID_OSC32); osc_wait_ready(OSC_ID_OSC32); } /* Enable the AST */ ast_enable(AST); /* Configure the AST with counter mode and set counter to 0 */ ast_conf.mode = AST_COUNTER_MODE; ast_conf.osc_type = AST_OSC_32KHZ; ast_conf.psel = AST_PSEL_32KHZ_1HZ; ast_conf.counter = 0; ast_set_config(AST, &ast_conf); /* Enable period enent of AST */ ast_write_periodic0_value(AST, AST_PSEL_32KHZ_1HZ); ast_enable_event(AST, AST_EVENT_PER); }
int main(void) { enum sleepmgr_mode current_sleep_mode = SLEEPMGR_ACTIVE; uint32_t ast_counter = 0; /* * Initialize the synchronous clock system to the default configuration * set in conf_clock.h. * \note All non-essential peripheral clocks are initially disabled. */ sysclk_init(); /* * Initialize the resources used by this example to the default * configuration set in conf_board.h */ board_init(); /* * Turn the activity status LED on to inform the user that the device * is active. */ ioport_set_pin_level(LED_ACTIVITY_STATUS_PIN, LED_STATUS_ON); osc_priv_enable_osc32(); /* Enable the AST clock. */ ast_enable(AST); /* Initialize the AST in Counter mode. */ struct ast_config ast_conf; ast_conf.mode = AST_COUNTER_MODE; ast_conf.osc_type = AST_OSC_1KHZ; ast_conf.psel = AST_PSEL_32KHZ_1HZ - 6; ast_conf.counter = ast_counter; ast_set_config(AST, &ast_conf); /* * Configure the AST to wake up the CPU when the counter reaches the * selected periodic0 value. */ ast_write_periodic0_value(AST, AST_PSEL_32KHZ_1HZ - 3); ast_enable_wakeup(AST, AST_WAKEUP_PER); ast_enable_event(AST, AST_EVENT_PER); ast_clear_interrupt_flag(AST, AST_INTERRUPT_PER); ast_set_callback(AST, AST_INTERRUPT_PER, ast_per_interrupt_callback, AST_PER_IRQn, 0); /* AST can wakeup the device */ bpm_enable_wakeup_source(BPM, (1 << BPM_BKUPWEN_AST)); /* Initialize the sleep manager, lock initial mode.*/ sleepmgr_init(); sleepmgr_lock_mode(current_sleep_mode); while (1) { /* * Turn the activity status LED off to inform the user that the * device is in a sleep mode. */ ioport_set_pin_level(LED_ACTIVITY_STATUS_PIN, LED_STATUS_OFF); /* * Go to sleep in the deepest allowed sleep mode (i.e. no * deeper than the currently locked sleep mode). */ sleepmgr_enter_sleep(); /* * Turn the activity status LED on to inform the user that the * device is active. */ ioport_set_pin_level(LED_ACTIVITY_STATUS_PIN, LED_STATUS_ON); /* Unlock the current sleep mode. */ sleepmgr_unlock_mode(current_sleep_mode); /* Add a 3s delay. */ delay_s(3); /* Lock the next sleep mode. */ ++current_sleep_mode; if ((current_sleep_mode >= SLEEPMGR_NR_OF_MODES)) { current_sleep_mode = SLEEPMGR_ACTIVE; } sleepmgr_lock_mode(current_sleep_mode); } }