/*---------------------------------------------------------------------------*/ void rtimer_arch_init(void) { struct rtc_count_config config_rtc_count; /* Configure RTC in counter mode */ rtc_count_get_config_defaults(&config_rtc_count); config_rtc_count.prescaler = RTC_COUNT_PRESCALER_DIV_1; #ifdef TEST_RTC_COUNTER config_rtc_count.mode = RTC_COUNT_MODE_16BIT; #else config_rtc_count.mode = RTC_COUNT_MODE_32BIT; #endif config_rtc_count.continuously_update = true; rtc_count_init(&rtc_instance, RTC, &config_rtc_count); /* Enable RTC */ rtc_count_enable(&rtc_instance); #ifdef TEST_RTC_COUNTER /* Overflow callback and reload value */ rtc_count_register_callback( &rtc_instance, rtc_overflow_callback, RTC_COUNT_CALLBACK_OVERFLOW); rtc_count_enable_callback(&rtc_instance, RTC_COUNT_CALLBACK_OVERFLOW); rtc_count_set_period(&rtc_instance, 100); #endif /* Compare callback */ rtc_count_register_callback( &rtc_instance, rtc_compare_callback, RTC_COUNT_CALLBACK_COMPARE_0); INFO("RTIMER initialized"); }
/** * \internal * \brief Test for event system in asynchronous 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_asynchronous_event_test(const struct test_case *test) { uint32_t timeout_cycles; /* Skip test if initialization failed */ test_assert_true(test, init_success, "Skipping test due to failed initialization"); /* 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"); }
/*---------------------------------------------------------------------------*/ static void _hal_tcInit(void) { //! [init_conf] struct rtc_count_config config_rtc_count; rtc_count_get_config_defaults(&config_rtc_count); //! [init_conf] //! [set_config] config_rtc_count.prescaler = RTC_COUNT_PRESCALER_DIV_1; config_rtc_count.mode = RTC_COUNT_MODE_16BIT; config_rtc_count.continuously_update = true; //! [set_config] //! [init_rtc] rtc_count_init(&rtc_instance, RTC, &config_rtc_count); //! [init_rtc] //! [period] rtc_count_set_period(&rtc_instance, 32); //! [period] //! [reg_callback] rtc_count_register_callback( &rtc_instance, _isr_tc_interrupt, RTC_COUNT_CALLBACK_OVERFLOW); //! [reg_callback] //! [en_callback] rtc_count_enable_callback(&rtc_instance, RTC_COUNT_CALLBACK_OVERFLOW); //! [en_callback] //! [enable] rtc_count_enable(&rtc_instance); //! [enable] } /* _hal_tcInit() */
/** * \brief This function puts the transceiver and device to sleep */ void sm_sleep(uint32_t interval) { interval = interval * 1000; rtc_count_set_period(&rtc_instance, interval); rtc_count_enable(&rtc_instance); /*put the MCU in standby mode with RTC as wakeup source*/ system_set_sleepmode(SYSTEM_SLEEPMODE_STANDBY); system_sleep(); }
/** * \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"); }
//! [setup_dac] void configure_dac(void) { //! [setup_dac_config] struct dac_config config_dac; //! [setup_dac_config] //! [setup_dac_config_default] dac_get_config_defaults(&config_dac); //! [setup_dac_config_default] //! [setup_dac_start_on_event] #if (SAML21) dac_instance.start_on_event[DAC_CHANNEL_0] = true; #else dac_instance.start_on_event = true; #endif //! [setup_dac_start_on_event] //! [setup_dac_instance] dac_init(&dac_instance, DAC, &config_dac); //! [setup_dac_instance] //! [setup_dac_on_event_start_conversion] struct dac_events events = #if (SAML21) { .on_event_chan0_start_conversion = true }; #else { .on_event_start_conversion = true }; #endif //! [setup_dac_on_event_start_conversion] //! [enable_dac_event] dac_enable_events(&dac_instance, &events); //! [enable_dac_event] } //! [setup_dac] //! [setup_dac_channel] void configure_dac_channel(void) { //! [setup_dac_chan_config] struct dac_chan_config config_dac_chan; //! [setup_dac_chan_config] //! [setup_dac_chan_config_default] dac_chan_get_config_defaults(&config_dac_chan); //! [setup_dac_chan_config_default] //! [set_dac_chan_config] dac_chan_set_config(&dac_instance, DAC_CHANNEL_0, &config_dac_chan); //! [set_dac_chan_config] //! [enable_dac_channel] dac_chan_enable(&dac_instance, DAC_CHANNEL_0); //! [enable_dac_channel] } //! [setup_dac_channel] int main(void) { //! [data_length_var] uint32_t i; //! [data_length_var] system_init(); //! [setup_init] //! [init_rtc] configure_rtc_count(); //! [init_rtc] //! [set_rtc_period] rtc_count_set_period(&rtc_instance, 1); //! [set_rtc_period] //! [init_dac] configure_dac(); //! [init_dac] //! [init_dac_chan] configure_dac_channel(); //! [init_dac_chan] //! [enable_dac] dac_enable(&dac_instance); //! [enable_dac] //! [init_event_resource] configure_event_resource(); //! [init_event_resource] //! [register_dac_callback] dac_register_callback(&dac_instance, DAC_CHANNEL_0, dac_callback,DAC_CALLBACK_TRANSFER_COMPLETE); //! [register_dac_callback] //! [enable_dac_callback] dac_chan_enable_callback(&dac_instance, DAC_CHANNEL_0, DAC_CALLBACK_TRANSFER_COMPLETE); //! [enable_dac_callback] //! [setup_dac_data] for (i = 0;i < DATA_LENGTH;i++) { dac_data[i] = 0xfff * i; } //! [setup_dac_data] //! [setup_init] //! [main_start] //! [main_write] dac_chan_write_buffer_job(&dac_instance, DAC_CHANNEL_0, dac_data, DATA_LENGTH); //! [main_write] //! [main_check_transfer_done] while (!transfer_is_done) { /* Wait for transfer done */ } //! [main_check_transfer_done] //! [main_loop] while (1) { } //! [main_loop] //! [main_start] }
/** * \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"); }
void rtc_set_count(uint32_t rtc_count) { rtc_count_set_period(&rtc_instance, rtc_count); }