int main(void) { //! [setup_init] struct tc_module tc_instance; struct events_resource example_event; struct events_hook hook; system_init(); system_interrupt_enable_global(); configure_event_channel(&example_event); configure_event_user(&example_event); configure_event_interrupt(&example_event, &hook); configure_tc(&tc_instance); //! [setup_init] //! [main] //! [main_1] while (events_is_busy(&example_event)) { /* Wait for channel */ }; //! [main_1] //! [main_2] tc_start_counter(&tc_instance); //! [main_2] while (true) { /* Nothing to do */ } //! [main] }
int main(void) { //! [events_resource_struct] struct events_resource example_event; //! [events_resource_struct] system_init(); //! [setup_init] configure_event_channel(&example_event); configure_event_user(&example_event); //! [setup_init] //! [main] //! [main_1] while (events_is_busy(&example_event)) { /* Wait for channel */ }; //! [main_1] //! [main_2] events_trigger(&example_event); //! [main_2] while (true) { /* Nothing to do */ } //! [main] }
/** * \internal * \brief Test for event system in resynchronous mode. * * This test waits for event channel and user to be ready and then * starts the RTC to generate overflow event. It waits until the timer * is started. If the timer starts running then it can be assumed that * the event has been propagated properly. * * \param test Current test case. */ static void run_resynchronous_event_test(const struct test_case *test) { uint32_t timeout_cycles = 1000; /* Skip test if initialization failed */ test_assert_true(test, init_success, "Skipping test due to failed initialization"); /* Check whether event user is ready */ do { timeout_cycles--; if (events_is_users_ready(&events)) { break; } } while (timeout_cycles > 0); test_assert_true(test, timeout_cycles > 0, "Timeout error: Event user not ready"); /* Check whether event channel is ready */ timeout_cycles = 1000; do { timeout_cycles--; if (!events_is_busy(&events)) { break; } } while (timeout_cycles > 0); test_assert_true(test, timeout_cycles > 0, "Timeout error: Event channel not ready"); /* Event action test */ rtc_count_enable(&rtc_inst); rtc_count_set_period(&rtc_inst, 100); timeout_cycles = 10000; do { timeout_cycles--; if (tc_get_count_value(&tc_inst)) { break; } } while (timeout_cycles > 0); test_assert_true(test, timeout_cycles > 0, "Error: Timeout in event reception/action"); }
enum status_code events_release(struct events_resource *resource) { enum status_code err = STATUS_OK; Assert(resource); /* Check if channel is busy */ if(events_is_busy(resource)) { return STATUS_BUSY; } if (!(_events_inst.allocated_channels & (1<<resource->channel))) { err = STATUS_ERR_NOT_INITIALIZED; } else { _events_release_channel(resource->channel); } return err; }
/** * \brief Initialize the TC3 & RTC for unit test * * Initializes the RTC module and TC3 module which are used as * event generator and event user respectively. */ static void test_event_gen_user_init(void) { enum status_code status; init_success = true; /* Timer configuration (Event User) */ struct tc_config config_tc; tc_get_config_defaults(&config_tc); config_tc.counter_16_bit.compare_capture_channel[0] = (0xFFFF / 4); /* Initialize the TC3 */ status = tc_init(&tc_inst, TC3, &config_tc); if (status != STATUS_OK) { init_success = false; } struct tc_events events_tc; events_tc.on_event_perform_action = true; events_tc.event_action = TC_EVENT_ACTION_START; tc_enable_events(&tc_inst, &events_tc); /* Enable the TC3 */ tc_enable(&tc_inst); /* RTC configuration (Event Generator) */ struct rtc_count_config config_rtc_count; struct rtc_count_events config_rtc_event = { .generate_event_on_overflow = true }; /* Initialize the RTC module */ rtc_count_get_config_defaults(&config_rtc_count); config_rtc_count.prescaler = RTC_COUNT_PRESCALER_DIV_1; config_rtc_count.mode = RTC_COUNT_MODE_16BIT; #ifdef FEATURE_RTC_CONTINUOUSLY_UPDATED config_rtc_count.continuously_update = true; #endif config_rtc_count.compare_values[0] = 50; status = rtc_count_init(&rtc_inst, RTC, &config_rtc_count); if (status != STATUS_OK) { init_success = false; } /* Enable RTC events */ config_rtc_event.generate_event_on_overflow = true; rtc_count_enable_events(&rtc_inst, &config_rtc_event); } /** * \internal * \brief Setup Function: Synchronous event propagation. * * This function initializes the event system channel 0 and the RTC * module (event generator) to be in the same clock domain for * synchronous event propagation. * * \param test Current test case. */ static void setup_synchronous_event_test(const struct test_case *test) { struct events_config events_conf; /* Get default event channel configuration */ events_get_config_defaults(&events_conf); events_conf.clock_source = GCLK_GENERATOR_2; events_conf.edge_detect = EVENTS_EDGE_DETECT_RISING; events_conf.path = EVENTS_PATH_SYNCHRONOUS; events_conf.generator = TEST_EVENT_GEN; events_allocate(&events, &events_conf); events_attach_user(&events, TEST_EVENT_USER); } /** * \internal * \brief Test for event system in synchronous mode. * * This test waits for event channel and user to be ready and then * starts the RTC to generate overflow event. It waits until the timer * is started. If the timer starts running then it can be assumed that * the event has been propagated properly. * * \param test Current test case. */ static void run_synchronous_event_test(const struct test_case *test) { uint32_t timeout_cycles = 1000; /* Skip test if initialization failed */ test_assert_true(test, init_success, "Skipping test due to failed initialization"); /* Check whether event user is ready */ do { timeout_cycles--; if (events_is_users_ready(&events)) { break; } } while (timeout_cycles > 0); test_assert_true(test, timeout_cycles > 0, "Timeout error: Event user not ready"); /* Check whether event channel is ready */ timeout_cycles = 1000; do { timeout_cycles--; if (!events_is_busy(&events)) { break; } } while (timeout_cycles > 0); test_assert_true(test, timeout_cycles > 0, "Timeout error: Event channel not ready"); /* Event action test */ rtc_count_enable(&rtc_inst); rtc_count_set_period(&rtc_inst, 100); timeout_cycles = 10000; do { timeout_cycles--; if (tc_get_count_value(&tc_inst)) { break; } } while (timeout_cycles > 0); test_assert_true(test, timeout_cycles > 0, "Error: Timeout in event reception/action"); }