tkernel_2/kernel/sysdepend/device/tef_em1d/cntwus.c | bare source | permlink (0.02 seconds) |
1: /* 2: *---------------------------------------------------------------------- 3: * T-Kernel 2.0 Software Package 4: * 5: * Copyright 2011 by Ken Sakamura. 6: * This software is distributed under the latest version of T-License 2.x. 7: *---------------------------------------------------------------------- 8: * 9: * Released by T-Engine Forum(http://www.t-engine.org/) at 2011/05/17. 10: * Modified by TRON Forum(http://www.tron.org/) at 2015/06/01. 11: * 12: *---------------------------------------------------------------------- 13: */ 14: 15: /* 16: * Calculate the value of required count for loop-wait for a given period (in micro seconds) 17: */ 18: 19: #include <basic.h> 20: #include <sys/sysinfo.h> 21: #include <tk/syslib.h> 22: #include "tkdev_conf.h" 23: 24: /* 25: * Loop wait 26: * disable the inline expansion caused by compiler optimization to obtain accurate 27: * measurement. 28: */ 29: __attribute__ ((noinline)) 30: LOCAL void WaitLoop( UW count ) 31: { 32: Asm(" _loop: subs %0, %0, #1 \n" 33: " bhi _loop " 34: : "=r"(count) 35: : "0"(count + 1) 36: ); 37: } 38: 39: /* 40: * WaitUsec() calculate the loop count for a given time (in microseconds) 41: * interrupt-disabled state is assumed. 42: */ 43: EXPORT void CountWaitUsec( void ) 44: { 45: UW t0, t1, t2; 46: UW cnt; 47: UW d; 48: 49: /* stop timer */ 50: out_w(TI_OP, 0); 51: 52: /* select clock */ 53: out_w(TI0TIN_SEL, (in_w(TI0TIN_SEL) & ~3) | TITIN_PLL3); 54: 55: /* supply clock */ 56: out_w(GCLKCTRL3ENA, in_w(GCLKCTRL3ENA) | TI0_TIN_GCK); 57: out_w(GCLKCTRL3, in_w(GCLKCTRL3) | TI0_TIN_GCK); 58: 59: /* enable timer */ 60: out_w(TI_OP, TM_EN); 61: while ( (in_w(TI_SCLR) & TM_SCLR) != 0 ); 62: WaitLoop(100); 63: 64: /* set counter */ 65: out_w(TI_SET, 0xffffffff); 66: 67: /* start timer counting */ 68: out_w(TI_OP, TSTART|TM_EN); 69: 70: WaitLoop(100); /* wait for a little (just in case) */ 71: 72: /* measurement */ 73: t0 = in_w(TI_RCR); 74: WaitLoop(1001); 75: t1 = in_w(TI_RCR); 76: WaitLoop(21001); 77: t2 = in_w(TI_RCR); 78: 79: /* stop timer */ 80: out_w(TI_OP, 0); 81: 82: /* stop clock */ 83: out_w(GCLKCTRL3, in_w(GCLKCTRL3) & ~TI0_TIN_GCK); 84: 85: /* the time for 20000 loops is calculated excluding the 86: overhead for the rest of measurement time. */ 87: cnt = (t2 - t1) - (t1 - t0); 88: 89: /* Calculate the loop count that spends 64 microseconds 90: * 20000 loops 91: * loop64us = ---------------------- * 64usec 92: * cnt * (1 / TIN_CLK) 93: * TIN_CLK = input clock to the timer [Hz] 94: */ 95: d = in_w(DIVTIMTIN); 96: SCInfo.loop64us = TIN_CLK(d) / (cnt * (50/2)) * (64/2); 97: }