gonzui


Format: Advanced Search

mtkernel_3/kernel/sysdepend/cpu/core/armv7m/sys_timer.hbare sourcepermlink (0.00 seconds)

Search this content:

    1: /*
    2:  *----------------------------------------------------------------------
    3:  *    micro T-Kernel 3.00.03
    4:  *
    5:  *    Copyright (C) 2006-2021 by Ken Sakamura.
    6:  *    This software is distributed under the T-License 2.2.
    7:  *----------------------------------------------------------------------
    8:  *
    9:  *    Released by TRON Forum(http://www.tron.org) at 2021/03/31.
   10:  *
   11:  *----------------------------------------------------------------------
   12:  */
   13: 
   14: /*
   15:  *      sys_timer.h (ARMv7-M)
   16:  *      Hardware-Dependent System Timer (SysTick) Processing
   17:  */
   18: 
   19: #ifndef _SYSDEPEND_CPU_CORE_SYSTIMER_
   20: #define _SYSDEPEND_CPU_CORE_SYSTIMER_
   21: 
   22: /*
   23:  * Timer start processing
   24:  *      Initialize the timer and start the periodical timer interrupt.
   25:  */
   26: Inline void knl_start_hw_timer( void )
   27: {
   28:         UINT   n, imask;
   29: 
   30:         DI(imask);
   31: 
   32:         /* Set System timer CLK source to Core, Systick exception enable */
   33:         out_w(SYST_CSR, 0x00000006);
   34: 
   35:         /* Set counter: TMCLK(MHz) */
   36:         n = (UINT)(TIMER_PERIOD * TMCLK_KHz - 1);
   37:         out_w(SYST_RVR, n);
   38: 
   39:         /* Start timer count */
   40:         out_w(SYST_CSR, 0x00000007);
   41: 
   42:         EI(imask);
   43: }
   44: 
   45: /*
   46:  * Clear timer interrupt
   47:  *      Clear the timer interrupt request. Depending on the type of
   48:  *      hardware, there are two timings for clearing: at the beginning
   49:  *      and the end of the interrupt handler.
   50:  *      'clear_hw_timer_interrupt()' is called at the beginning of the
   51:  *      timer interrupt handler.
   52:  *      'end_of_hw_timer_interrupt()' is called at the end of the timer
   53:  *      interrupt handler.
   54:  *      Use either or both according to hardware.
   55:  */
   56: Inline void knl_clear_hw_timer_interrupt( void )
   57: {
   58:         (void)in_w(SYST_CSR);                  /* Clear COUNTFLAG */
   59:         out_w(SCB_ICSR, ICSR_PENDSTCLR);
   60: }
   61: 
   62: Inline void knl_end_of_hw_timer_interrupt( void )
   63: {
   64:         /* No processing */
   65: }
   66: 
   67: /*
   68:  * Timer stop processing
   69:  *      Stop the timer operation.
   70:  *      Called when system stops.
   71:  */
   72: Inline void knl_terminate_hw_timer( void )
   73: {
   74:         /* Timer interrupt disable */
   75:         out_w(SYST_CSR, 0x00000000);
   76: }
   77: 
   78: /*
   79:  * Get processing time from the previous timer interrupt to the
   80:  * current (nanosecond)
   81:  *      Consider the possibility that the timer interrupt occurred
   82:  *      during the interrupt disable and calculate the processing time
   83:  *      within the following
   84:  *      range: 0 <= Processing time < TIMER_PERIOD * 2
   85:  */
   86: Inline UW knl_get_hw_timer_nsec( void )
   87: {
   88:         UW     ofs, max, unf;
   89:         UINT   imsk;
   90: 
   91:         DI(imsk);
   92:         max = in_w(SYST_RVR);                  /* Setting count */
   93:         unf = in_w(SYST_CSR) & 0x10000;                /* COUNTFLAG */
   94:         ofs = in_w(SYST_CVR) & 0x00ffffff;     /* Current Remained count */
   95:         if ( unf == 0 ) {              /* Reload not occurred */
   96:                 unf = in_w(SYST_CSR) & 0x10000;       /* Check COUNTFLAG again */
   97:                 if (unf != 0) {               /* Reload occurred */
   98:                         ofs = in_w(SYST_CVR) & 0x00ffffff;
   99:                 }
  100:         }
  101:         EI(imsk);
  102:         ofs = max - ofs;                       /* Elapsed count */
  103:         if ( unf != 0 ) ofs += max + 1;        /* Reload occured, Adjust */
  104: 
  105:         return  (UW) ( ( (D)ofs * 1000000 ) / TMCLK_KHz );
  106: }
  107: 
  108: #endif /* _SYSDEPEND_CPU_CORE_SYSTIMER_ */