/** * \brief Test wakeup functions. * * \param test Current test case. */ static void run_picouart_test(const struct test_case *test) { uint8_t value; Pm *p_pm = PM; struct picouart_dev_inst dev_inst; struct picouart_config config; /* Init the PICOUART */ picouart_get_config_defaults(&config); picouart_init(&dev_inst, PICOUART, &config); /* Enable the PICOUART */ picouart_enable(&dev_inst); delay_ms(30); /* PICOUART can wakeup the device. */ bpm_enable_wakeup_source(BPM, (1 << BPM_BKUPWEN_PICOUART)); p_pm->PM_AWEN = 1 << PM_AWEN_PICOUART; p_pm->PM_IER = PM_IER_WAKE; NVIC_SetPriority(PM_IRQn,1); NVIC_EnableIRQ(PM_IRQn); config.action = PICOUART_ACTION_WAKEUP_ON_STARTBIT; picouart_set_config(&dev_inst, &config); bm_send_picouart_frame('A', 1); bpm_sleep(BPM, BPM_SM_SLEEP_1); config.action = PICOUART_ACTION_WAKEUP_ON_FULLFRAME; picouart_set_config(&dev_inst, &config); bm_send_picouart_frame('T', 5); bpm_sleep(BPM, BPM_SM_SLEEP_3); config.action = PICOUART_ACTION_WAKEUP_ON_FULLFRAME; picouart_set_config(&dev_inst, &config); bm_send_picouart_frame('M', 5); bpm_sleep(BPM, BPM_SM_WAIT); config.action = PICOUART_ACTION_WAKEUP_ON_MATCH; config.match = 'L'; picouart_set_config(&dev_inst, &config); bm_send_picouart_frame('L', 10); bpm_sleep(BPM, BPM_SM_RET); while(!picouart_is_data_ready(&dev_inst)) { } picouart_read(&dev_inst, &value); test_assert_true(test, value == 'L', "Picouart wakeup not work!"); }
/** * \brief Test alarm interrupt and wakeup functions in calendar/counter mode. * * \param test Current test case. */ static void run_alarm_test(const struct test_case *test) { uint32_t ast_alarm, ast_counter; struct ast_calendar calendar; struct ast_config ast_conf; /* Enable the AST. */ ast_enable(AST); /* Set alarm 0 to interrupt after 1 second in calendar mode. */ calendar.FIELD.sec = 0; calendar.FIELD.min = 15; calendar.FIELD.hour = 12; calendar.FIELD.day = 20; calendar.FIELD.month = 9; calendar.FIELD.year = 12; ast_conf.mode = AST_CALENDAR_MODE; ast_conf.osc_type = AST_OSC_32KHZ; ast_conf.psel = AST_PSEL_32KHZ_1HZ; ast_conf.calendar = calendar; ast_set_config(AST, &ast_conf); /* Set callback for alarm0. */ ast_clear_interrupt_flag(AST, AST_INTERRUPT_ALARM); ast_write_alarm0_value(AST, calendar.field + 1); ast_set_callback(AST, AST_INTERRUPT_ALARM, ast_alarm_callback, AST_ALARM_IRQn, 1); flag = 0; delay_ms(1500); test_assert_true(test, flag == 2, "Alarm interrupt not work!"); /* Set alarm 0 to wakeup after 1 second in counter mode. */ ast_conf.mode = AST_COUNTER_MODE; ast_conf.osc_type = AST_OSC_32KHZ; ast_conf.psel = AST_PSEL_32KHZ_1HZ - 2; ast_conf.counter = 0; ast_set_config(AST, &ast_conf); /* ast_init_counter Set Alarm to current time+4 quarter second. */ ast_counter = ast_read_counter_value(AST); ast_alarm = ast_counter + 4; ast_write_alarm0_value(AST, ast_alarm); ast_enable_interrupt(AST, AST_INTERRUPT_ALARM); ast_enable_wakeup(AST, AST_WAKEUP_ALARM); /* AST can wakeup the device. */ bpm_enable_wakeup_source(BPM, (1 << BPM_BKUPWEN_AST)); flag = 0; /* Go into WAIT mode. */ bpm_sleep(BPM, BPM_SM_WAIT); delay_ms(1000); test_assert_true(test, flag == 2, "Alarm wakeup not work!"); /* Disable the AST. */ ast_disable(AST); }
/** * \brief main function : do init and loop. */ int main(void) { uint32_t ast_alarm, ast_counter; uint8_t key; /* Initialize the SAM system. */ sysclk_init(); board_init(); /* Initialize the console uart. */ configure_console(); /* Output example information. */ printf("-- AST Example 2 in counter mode --\r\n"); printf("-- %s\n\r", BOARD_NAME); printf("-- Compiled: %s %s --\n\r", __DATE__, __TIME__); printf("Config AST with 32 KHz oscillator.\r\n"); printf("Use alarm0 to wakeup from low power mode.\r\n"); config_ast(); /* AST and EIC can wakeup the device. */ config_wakeup(); while (1) { /* let the user select the low power mode. */ key = 0; while ((key < 0x31) || (key > 0x37)) { /* Display menu */ display_menu(); scanf("%c", (char *)&key); } key = key - '0'; /* ast_init_counter Set Alarm to current time+6 seconds. */ ast_counter = ast_read_counter_value(AST); ast_alarm = ast_counter + 6; ast_write_alarm0_value(AST, ast_alarm); ast_enable_interrupt(AST, AST_INTERRUPT_ALARM); /* Go into selected low power mode. */ bpm_sleep(BPM, key); while (flag == false); flag = true; /* After wake up, clear the Alarm0. */ ast_clear_interrupt_flag(AST, AST_INTERRUPT_ALARM); /* Output the counter value. */ ast_counter = ast_read_counter_value(AST); printf("\n\r Counter value: %02u \n\r", ast_counter); } }
/** * \brief Test entering and exiting sleep 0 mode. * * \param test Current test case. */ static void run_sleep_0_test(const struct test_case *test) { /* Wait for the printf operation to finish before setting the device in a power save mode. */ delay_ms(30); reset_log(); bpm_sleep(BPM, BPM_SM_SLEEP_0); log_event(EVENT_RUN); test_assert_true(test, IS_NB_EVENTS_OK(), "Unexpected number of events, should be 2"); test_assert_true(test, IS_EVENTS_OK(), "Unexpected events sequence"); wait_test_assert_idle(); }
/** * \brief Test entering and exiting backup mode. * * \param test Current test case. */ static void run_backup_test(const struct test_case *test) { volatile uint32_t wakeup_cause = bpm_get_backup_wakeup_cause(BPM); /* Wakeup from backup mode */ if (wakeup_cause) { test_assert_true(test, wakeup_cause & (1 << BPM_BKUPWEN_AST), "Unexpected backup wakeup cause, should be AST!"); return; } /* Wait for the printf operation to finish before setting the device in a power save mode. */ delay_ms(30); /* Enter backup mode */ bpm_sleep(BPM, BPM_SM_BACKUP); }
/** * \brief Test periodic interrupt and wakeup functions in counter mode. * * \param test Current test case. */ static void run_periodic_test(const struct test_case *test) { struct ast_config ast_conf; /* Enable the AST. */ ast_enable(AST); 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); /* Set periodic 0 to interrupt after 1/16 second in counter mode. */ ast_clear_interrupt_flag(AST, AST_INTERRUPT_PER); ast_write_periodic0_value(AST, AST_PSEL_32KHZ_1HZ - 4); /* Set callback for periodic0. */ flag = 0; ast_set_callback(AST, AST_INTERRUPT_PER, ast_per_callback, AST_PER_IRQn, 1); delay_ms(200); test_assert_true(test, flag == 1, "Periodic interrupt not work!"); /* Set periodic 0 to wakeup after 1/16 second in counter mode. */ while (!(ast_read_interrupt_mask(AST) & AST_IMR_PER0_1)) { ast_enable_interrupt(AST, AST_INTERRUPT_PER); } ast_enable_wakeup(AST, AST_WAKEUP_PER); /* AST can wakeup the device. */ bpm_enable_wakeup_source(BPM, (1 << BPM_BKUPWEN_AST)); flag = 0; /* Go into WAIT mode. */ bpm_sleep(BPM, BPM_SM_WAIT); delay_ms(200); test_assert_true(test, flag == 1, "Periodic wakeup not work!"); ast_disable_interrupt(AST, AST_INTERRUPT_PER); /* Disable the AST. */ ast_disable(AST); }
/* Override the default definition of vPortSuppressTicksAndSleep() that is weakly defined in the FreeRTOS Cortex-M3 port layer with a version that manages the asynchronous timer (AST), as the tick is generated from the low power AST and not the SysTick as would normally be the case on a Cortex-M. */ void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime ) { uint32_t ulAlarmValue, ulCompleteTickPeriods, ulInterruptStatus; eSleepModeStatus eSleepAction; TickType_t xModifiableIdleTime; enum sleepmgr_mode xSleepMode; /* THIS FUNCTION IS CALLED WITH THE SCHEDULER SUSPENDED. */ /* Make sure the AST reload value does not overflow the counter. */ if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks ) { xExpectedIdleTime = xMaximumPossibleSuppressedTicks; } /* Calculate the reload value required to wait xExpectedIdleTime tick periods. */ ulAlarmValue = ulAlarmValueForOneTick * xExpectedIdleTime; if( ulAlarmValue > ulStoppedTimerCompensation ) { /* Compensate for the fact that the AST is going to be stopped momentarily. */ ulAlarmValue -= ulStoppedTimerCompensation; } /* Stop the AST momentarily. The time the AST is stopped for is accounted for as best it can be, but using the tickless mode will inevitably result in some tiny drift of the time maintained by the kernel with respect to calendar time. */ prvDisableAST(); /* Enter a critical section but don't use the taskENTER_CRITICAL() method as that will mask interrupts that should exit sleep mode. */ ulInterruptStatus = cpu_irq_save(); /* The tick flag is set to false before sleeping. If it is true when sleep mode is exited then sleep mode was probably exited because the tick was suppressed for the entire xExpectedIdleTime period. */ ulTickFlag = pdFALSE; /* If a context switch is pending then abandon the low power entry as the context switch might have been pended by an external interrupt that requires processing. */ eSleepAction = eTaskConfirmSleepModeStatus(); if( eSleepAction == eAbortSleep ) { /* Restart tick. */ prvEnableAST(); /* Re-enable interrupts - see comments above the cpsid instruction() above. */ cpu_irq_restore( ulInterruptStatus ); } else { /* Adjust the alarm value to take into account that the current time slice is already partially complete. */ ulAlarmValue -= ast_read_counter_value( AST ); ast_write_alarm0_value( AST, ulAlarmValue ); /* Restart the AST. */ prvEnableAST(); /* Allow the application to define some pre-sleep processing. */ xModifiableIdleTime = xExpectedIdleTime; configPRE_SLEEP_PROCESSING( xModifiableIdleTime ); /* xExpectedIdleTime being set to 0 by configPRE_SLEEP_PROCESSING() means the application defined code has already executed the WAIT instruction. */ if( xModifiableIdleTime > 0 ) { /* Find the deepest allowable sleep mode. */ xSleepMode = sleepmgr_get_sleep_mode(); if( xSleepMode != SLEEPMGR_ACTIVE ) { /* Sleep until something happens. */ bpm_sleep( BPM, xSleepMode ); } } /* Allow the application to define some post sleep processing. */ configPOST_SLEEP_PROCESSING( xModifiableIdleTime ); /* Stop AST. Again, the time the SysTick is stopped for is accounted for as best it can be, but using the tickless mode will inevitably result in some tiny drift of the time maintained by the kernel with respect to calendar time. */ prvDisableAST(); /* Re-enable interrupts - see comments above the cpsid instruction() above. */ cpu_irq_restore( ulInterruptStatus ); if( ulTickFlag != pdFALSE ) { /* The tick interrupt has already executed, although because this function is called with the scheduler suspended the actual tick processing will not occur until after this function has exited. Reset the alarm value with whatever remains of this tick period. */ ulAlarmValue = ulAlarmValueForOneTick - ast_read_counter_value( AST ); ast_write_alarm0_value( AST, ulAlarmValue ); /* The tick interrupt handler will already have pended the tick processing in the kernel. As the pending tick will be processed as soon as this function exits, the tick value maintained by the tick is stepped forward by one less than the time spent sleeping. The actual stepping of the tick appears later in this function. */ ulCompleteTickPeriods = xExpectedIdleTime - 1UL; } else { /* Something other than the tick interrupt ended the sleep. How many complete tick periods passed while the processor was sleeping? */ ulCompleteTickPeriods = ast_read_counter_value( AST ) / ulAlarmValueForOneTick; /* The alarm value is set to whatever fraction of a single tick period remains. */ ulAlarmValue = ast_read_counter_value( AST ) - ( ulCompleteTickPeriods * ulAlarmValueForOneTick ); if( ulAlarmValue == 0 ) { /* There is no fraction remaining. */ ulAlarmValue = ulAlarmValueForOneTick; ulCompleteTickPeriods++; } ast_write_counter_value( AST, 0 ); ast_write_alarm0_value( AST, ulAlarmValue ); } /* Restart the AST so it runs up to the alarm value. The alarm value will get set to the value required to generate exactly one tick period the next time the AST interrupt executes. */ prvEnableAST(); /* Wind the tick forward by the number of tick periods that the CPU remained in a low power state. */ vTaskStepTick( ulCompleteTickPeriods ); } }
/*! * \brief main function : do init and loop (poll if configured so) */ int main(void) { uint8_t key; struct picouart_dev_inst dev_inst; struct picouart_config config; struct ast_config ast_conf; /* Initialize the SAM system */ sysclk_init(); board_init(); /* Initialize the console uart */ configure_console(); /* Output example information */ printf("\r\n"); printf("-- PICOUART Example 1 --\r\n"); printf("-- %s\r\n", BOARD_NAME); printf("-- Compiled: %s %s --\r\n", __DATE__, __TIME__); printf("-- IMPORTANT: This example requires a board " "monitor firmware version V1.3 or greater.\r\n"); /* Enable osc32 oscillator*/ if (!osc_is_ready(OSC_ID_OSC32)) { osc_enable(OSC_ID_OSC32); osc_wait_ready(OSC_ID_OSC32); } /* Disable all AST wake enable bits for safety since the AST is reset only by a POR. */ ast_enable(AST); 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); ast_disable_wakeup(AST, AST_WAKEUP_ALARM); ast_disable_wakeup(AST, AST_WAKEUP_PER); ast_disable_wakeup(AST, AST_WAKEUP_OVF); ast_disable(AST); /* Config the push button */ config_buttons(); /* Configurate the USART to board monitor */ bm_init(); sysclk_enable_hsb_module(SYSCLK_PBA_BRIDGE); sysclk_enable_peripheral_clock(BM_USART_USART); /* Init the PICOUART */ picouart_get_config_defaults(&config); picouart_init(&dev_inst, PICOUART, &config); /* Enable the PICOUART */ picouart_enable(&dev_inst); /* PICOUART and EIC can wakeup the device */ config_wakeup(); /* Display menu */ display_menu(); while (1) { scanf("%c", (char *)&key); switch (key) { case 'h': display_menu(); break; case 's': if (bm_flag) { printf("Switch off the board monitor to wake up..\r\n"); bm_flag = false; } else { printf("Switch on the board monitor to wake up..\r\n"); bm_flag = true; } break; case '0': printf("Enter Sleep mode with start bit wakeup.\r\n"); config.action = PICOUART_ACTION_WAKEUP_ON_STARTBIT; picouart_set_config(&dev_inst, &config); if (bm_flag) { printf("Board monitor will send frame after 3 seconds.\r\n"); bm_send_picouart_frame('A', 3000); } /* Wait for the printf operation to finish before setting the device in a power save mode. */ delay_ms(30); bpm_sleep(BPM, BPM_SM_SLEEP_2); printf("--Exit Sleep mode.\r\n\r\n"); break; case '1': printf("Enter Retention mode with full frame wakeup.\r\n"); config.action = PICOUART_ACTION_WAKEUP_ON_FULLFRAME; picouart_set_config(&dev_inst, &config); if (bm_flag) { printf("Board monitor will send frame after 3 seconds.\r\n"); bm_send_picouart_frame('T', 3000); } /* Wait for the printf operation to finish before setting the device in a power save mode. */ delay_ms(30); bpm_sleep(BPM, BPM_SM_RET); printf("--Exit Retention mode.\r\n\r\n"); break; case '2': printf("Enter backup mode with character match wakeup.\r\n"); config.action = PICOUART_ACTION_WAKEUP_ON_MATCH; config.match = 'L'; picouart_set_config(&dev_inst, &config); if (bm_flag) { printf("Board monitor will send frame after 3 seconds.\r\n"); bm_send_picouart_frame('L', 3000); } /* Wait for the printf operation to finish before setting the device in a power save mode. */ delay_ms(30); bpm_sleep(BPM, BPM_SM_BACKUP); break; default: break; } } }