/*---------------------------------------------------------------------------*/ 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 Set timer period.Called from Qdebug . */ void set_timer_period(uint16_t time) { rtc_count_set_compare(&rtc_instance,time,RTC_COUNT_COMPARE_0); //! [enable] rtc_count_enable(&rtc_instance); }
/** * \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(); }
/*! \brief Configure the RTC timer count after which interrupts comes */ void configure_rtc_count(void) { //! [init_conf] struct rtc_count_config config_rtc_count; typedef enum rtc_count_prescaler rtc_count_prescaler_t; #if DEF_SURF_LOW_POWER_SENSOR_ENABLE == 1 struct rtc_count_events config_rtc_event = { .generate_event_on_periodic[DEF_LOWPOWER_SENSOR_EVENT_PERIODICITY] = true }; #endif rtc_count_get_config_defaults(&config_rtc_count); volatile uint16_t temp; //! [set_config] config_rtc_count.prescaler = (rtc_count_prescaler_t)RTC_MODE0_CTRL_PRESCALER_DIV2; config_rtc_count.mode = RTC_COUNT_MODE_32BIT; #ifdef FEATURE_RTC_CONTINUOUSLY_UPDATED config_rtc_count.continuously_update = true; #endif config_rtc_count.clear_on_match = true; //! [init_rtc] rtc_count_init(&rtc_instance, RTC, &config_rtc_count); #if DEF_SURF_LOW_POWER_SENSOR_ENABLE == 1 /* Enable RTC events */ config_rtc_event.generate_event_on_periodic[DEF_LOWPOWER_SENSOR_EVENT_PERIODICITY] = true; rtc_count_enable_events(&rtc_instance, &config_rtc_event); #endif /*Set timer period */ temp = TIME_PERIOD_1MSEC * rtc_timer_msec; rtc_count_set_compare(&rtc_instance, temp, RTC_COUNT_COMPARE_0); //! [enable] rtc_count_enable(&rtc_instance); //! [enable] } /*! \brief Initialize timer * */ void timer_init(void) { /* Configure and enable RTC */ configure_rtc_count(); /* Configure and enable callback */ configure_rtc_callbacks(); }
static void _rtc_timer_start(uint32_t ms) { uint32_t compare = 0; compare = (uint32_t)(32.768 * ms); // rtc_count_register_callback(&rtc_instance, _rtc_timer_int_cb, RTC_COUNT_CALLBACK_COMPARE_0); rtc_count_enable_callback(&rtc_instance, RTC_COUNT_CALLBACK_COMPARE_0); rtc_count_set_count(&rtc_instance, 0); rtc_count_set_compare(&rtc_instance, compare, RTC_COUNT_COMPARE_0); rtc_count_enable(&rtc_instance); }
void configure_rtc_count(void) { struct rtc_count_config config_rtc_count; 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; config_rtc_count.continuously_update = true; rtc_count_init(&rtc_instance, RTC, &config_rtc_count); rtc_count_enable(&rtc_instance); }
/** * \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"); }
/** Initialize the RTC * * Initialize the RTC with default time * @param[void] void */ void rtc_init(void) { if (g_sys_init == 0) { system_init(); g_sys_init = 1; } struct rtc_count_config config_rtc_count; rtc_count_get_config_defaults(&config_rtc_count); config_rtc_count.prescaler = RTC_COUNT_PRESCALER_DIV_1024; config_rtc_count.mode = RTC_COUNT_MODE_32BIT; #ifdef FEATURE_RTC_CONTINUOUSLY_UPDATED config_rtc_count.continuously_update = true; #endif rtc_count_init(&rtc_instance, RTC, &config_rtc_count); rtc_count_enable(&rtc_instance); rtc_inited = 1; }
//! [setup_rtc] void configure_rtc_count(void) { //! [setup_rtc_event] struct rtc_count_events rtc_event; //! [setup_rtc_event] //! [setup_rtc_config] struct rtc_count_config config_rtc_count; //! [setup_rtc_config] //! [setup_rtc_config_defaults] rtc_count_get_config_defaults(&config_rtc_count); //! [setup_rtc_config_defaults] //! [setup_rtc_modify_conf] 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 //! [setup_rtc_modify_conf] //! [init_rtc_count] rtc_count_init(&rtc_instance, RTC, &config_rtc_count); //! [init_rtc_count] //! [setup_rtc_overflow_event] rtc_event.generate_event_on_overflow = true; //! [setup_rtc_overflow_event] //! [enable_rtc_overflow_event] rtc_count_enable_events(&rtc_instance, &rtc_event); //! [enable_rtc_overflow_event] //! [enable_rtc] rtc_count_enable(&rtc_instance); //! [enable_rtc] }
/** * \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"); }