Adding implementation for us_ticker APIs. Not complete, yet to be tested.

pull/1214/head
vimalrajr 2015-06-08 18:06:50 +05:30 committed by Karthik Purushothaman
parent 1fbe9894cd
commit 5f120e40de
1 changed files with 74 additions and 8 deletions

View File

@ -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);
}