/** * \brief Run PICOUART driver unit tests. */ int main(void) { struct ast_config ast_conf; const usart_serial_options_t usart_serial_options = { .baudrate = CONF_TEST_BAUDRATE, .charlength = CONF_TEST_CHARLENGTH, .paritytype = CONF_TEST_PARITY, .stopbits = CONF_TEST_STOPBITS }; sysclk_init(); board_init(); stdio_serial_init(CONF_TEST_USART, &usart_serial_options); /* 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); /* Configurate the USART to board monitor */ bm_init(); /* Define all the test cases. */ DEFINE_TEST_CASE(picouart_test, NULL, run_picouart_test, NULL, "SAM PICOUART wakeup test."); DEFINE_TEST_CASE(getversion_test, NULL, run_getversion_test, NULL, "SAM get version test."); /* Put test case addresses in an array. */ DEFINE_TEST_ARRAY(picouart_tests) = { &getversion_test, &picouart_test, }; /* Define the test suite. */ DEFINE_TEST_SUITE(picouart_suite, picouart_tests, "SAM PICOUART driver test suite"); /* Run all tests in the test suite. */ test_suite_run(&picouart_suite); while (1) { /* Busy-wait forever. */ } }
/** * \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 Test events driver with AST trigger. * * \param test Current test case. */ static void run_events_ast_test(const struct test_case *test) { uint32_t retry_times = 3; bool trigger_flag = false; struct events_conf events_config; struct events_ch_conf ch_config; init_ast(); init_dacc(); /* Initialize event module */ events_get_config_defaults(&events_config); events_init(&events_config); events_enable(); /* * Configure an event channel * - AST periodic event 0 --- Generator * - DAC --- User */ events_ch_get_config_defaults(&ch_config); ch_config.channel_id = CONF_TEST_USER_ID; ch_config.generator_id = CONF_TEST_GEN_ID; ch_config.shaper_enable = true; ch_config.igf_edge = EVENT_IGF_EDGE_NONE; events_ch_configure(&ch_config); /* Enable the channel */ events_ch_enable(CONF_TEST_USER_ID); /* Set new DACC value */ dacc_write_conversion_data(DACC, DACC_MAX_DATA / 2); /* Wait for AST event trigger */ events_ch_clear_trigger_status(CONF_TEST_USER_ID); do { if (events_ch_is_triggered(CONF_TEST_USER_ID)) { trigger_flag = true; events_ch_clear_trigger_status(CONF_TEST_USER_ID); break; } delay_ms(1000); } while (retry_times--); /* Disable the AST */ ast_disable(AST); test_assert_true(test, trigger_flag, "AST event not triggered!"); }
/** * \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); }
/** * \brief Run BPM driver unit tests. */ int main(void) { struct ast_config ast_conf; const usart_serial_options_t usart_serial_options = { .baudrate = CONF_TEST_BAUDRATE, .charlength = CONF_TEST_CHARLENGTH, .paritytype = CONF_TEST_PARITY, .stopbits = CONF_TEST_STOPBITS }; sysclk_init(); board_init(); stdio_serial_init(CONF_TEST_USART, &usart_serial_options); /* Initialize AST for all tests */ /* 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); 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 - 2); ast_set_callback(AST, AST_INTERRUPT_PER, ast_per_callback, AST_PER_IRQn, 1); ast_enable_wakeup(AST, AST_WAKEUP_PER); /* AST can wakeup the device */ bpm_enable_wakeup_source(BPM, (1 << BPM_BKUPWEN_AST)); /** * Retain I/O lines after wakeup from backup. * Disable to undo the previous retention state then enable. */ bpm_disable_io_retention(BPM); bpm_enable_io_retention(BPM); /* Enable fast wakeup */ bpm_enable_fast_wakeup(BPM); /* Define all the test cases. */ DEFINE_TEST_CASE(backup_test, NULL, run_backup_test, NULL, "Backup Power Manager, Backup mode & wakeup."); DEFINE_TEST_CASE(ps_test, NULL, run_ps_test, NULL, "Backup Power Manager, Power Scaling."); DEFINE_TEST_CASE(ret_test, NULL, run_ret_test, NULL, "Backup Power Manager, Retention mode & wakeup."); DEFINE_TEST_CASE(wait_test, NULL, run_wait_test, NULL, "Backup Power Manager, Wait mode & wakeup."); DEFINE_TEST_CASE(sleep_3_test, NULL, run_sleep_3_test, NULL, "Backup Power Manager, Sleep mode 3 & wakeup."); DEFINE_TEST_CASE(sleep_2_test, NULL, run_sleep_2_test, NULL, "Backup Power Manager, Sleep mode 2 & wakeup."); DEFINE_TEST_CASE(sleep_1_test, NULL, run_sleep_1_test, NULL, "Backup Power Manager, Sleep mode 1 & wakeup."); DEFINE_TEST_CASE(sleep_0_test, NULL, run_sleep_0_test, NULL, "Backup Power Manager, Sleep mode 0 & wakeup."); /* Put test case addresses in an array. */ DEFINE_TEST_ARRAY(bpm_tests) = { &backup_test, &ps_test, &ret_test, &wait_test, &sleep_3_test, &sleep_2_test, &sleep_1_test, &sleep_0_test, }; /* Define the test suite. */ DEFINE_TEST_SUITE(bpm_suite, bpm_tests, "SAM BPM driver test suite"); /* Run all tests in the test suite. */ test_suite_run(&bpm_suite); /* Disable the AST */ ast_disable(AST); while (1) { /* Busy-wait forever. */ } }
/*! * \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; } } }