From 5f120e40def0f4c1edfa901d887781d147ad0a5c Mon Sep 17 00:00:00 2001 From: vimalrajr Date: Mon, 8 Jun 2015 18:06:50 +0530 Subject: [PATCH] Adding implementation for us_ticker APIs. Not complete, yet to be tested. --- .../hal/TARGET_Atmel/TARGET_SAM21/us_ticker.c | 82 +++++++++++++++++-- 1 file changed, 74 insertions(+), 8 deletions(-) diff --git a/libraries/mbed/targets/hal/TARGET_Atmel/TARGET_SAM21/us_ticker.c b/libraries/mbed/targets/hal/TARGET_Atmel/TARGET_SAM21/us_ticker.c index 9189381b01..56356d102f 100644 --- a/libraries/mbed/targets/hal/TARGET_Atmel/TARGET_SAM21/us_ticker.c +++ b/libraries/mbed/targets/hal/TARGET_Atmel/TARGET_SAM21/us_ticker.c @@ -19,29 +19,95 @@ #include "mbed_assert.h" #include "ins_gclk.h" #include "compiler.h" +#include "tc.h" +#include "tc_interrupt.h" -void us_ticker_irq_handler_internal(void) +static int us_ticker_inited = 0; + +struct tc_module us_ticker_module; + +void us_ticker_irq_handler_internal(struct tc_module* us_tc_module) { - + us_ticker_irq_handler(); + + /* Disable the callback */ + tc_disable_callback(us_tc_module, TC_CALLBACK_CC_CHANNEL0); } void us_ticker_init(void) { + uint32_t cycles_per_us; + uint32_t prescaler = 0; + struct tc_config config_tc; + enum status_code ret_status; + + if (us_ticker_inited) return; + us_ticker_inited = 1; + + tc_get_config_defaults(&config_tc); + + cycles_per_us = system_gclk_gen_get_hz(config_tc.clock_source) / 1000000; + MBED_ASSERT(cycles_per_us > 0); + while((cycles_per_us & 1) == 0 && prescaler <= 10) { + cycles_per_us = cycles_per_us >> 1; + prescaler++; + } + config_tc.clock_prescaler = TC_CTRLA_PRESCALER(prescaler); + config_tc.counter_size = TC_COUNTER_SIZE_32BIT; + config_tc.counter_32_bit.value = 0; + config_tc.counter_32_bit.compare_capture_channel[0] = 0xFFFFFFFF; + + //config_tc.oneshot = true; + + /* Initialize the timer */ + ret_status = tc_init(&us_ticker_module, TC4, &config_tc); + MBED_ASSERT(ret_status == STATUS_OK); + + /* Register callback function */ + tc_register_callback(&us_ticker_module, (tc_callback_t)us_ticker_irq_handler_internal, TC_CALLBACK_CC_CHANNEL0); + + /* Enable the timer module */ + tc_enable(&us_ticker_module); } uint32_t us_ticker_read() { - + if (!us_ticker_inited) + us_ticker_init(); + + return tc_get_count_value(&us_ticker_module); } void us_ticker_set_interrupt(timestamp_t timestamp) { - + uint32_t cur_time; + int32_t delta; + + cur_time = us_ticker_read(); + delta = (int32_t)((uint32_t)timestamp - cur_time); + if (delta < 0) + { + /* Event already occurred in past */ + us_ticker_irq_handler(); + return; + } + + tc_set_compare_value(&us_ticker_module, TC_CALLBACK_CC_CHANNEL0, timestamp); + + /* Enable the callback */ + tc_enable_callback(&us_ticker_module, TC_CALLBACK_CC_CHANNEL0); + + /* Enable the timer module */ + //tc_enable(&us_ticker_module); } -void us_ticker_clear_interrupt(void) -{ - +void us_ticker_disable_interrupt(void) { + /* Disable the callback */ + tc_disable_callback(&us_ticker_module, TC_CALLBACK_CC_CHANNEL0); } -/***************************************************************/ + +void us_ticker_clear_interrupt(void) { + /* Disable the callback */ + tc_disable_callback(&us_ticker_module, TC_CALLBACK_CC_CHANNEL0); +} \ No newline at end of file