gonzui


Format: Advanced Search

mtkernel_3/lib/libtk/sysdepend/cpu/rza2m/ptimer_rza2m.cbare sourcepermlink (0.02 seconds)

Search this content:

    1: /*
    2:  *----------------------------------------------------------------------
    3:  *    micro T-Kernel 3.00.05
    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/11.
   10:  *
   11:  *----------------------------------------------------------------------
   12:  */
   13: 
   14: #include <sys/machine.h>
   15: #ifdef CPU_RZA2M
   16: /*
   17:  *      ptimer_rza2m.c
   18:  *
   19:  *      Physical timer (for RZ/A2M)
   20:  */
   21: #include <tk/tkernel.h>
   22: #include <tk/syslib.h>
   23: 
   24: #if USE_PTMR
   25: 
   26: typedef struct {
   27:         UW     baddr;              // Register Base Address
   28:         UINT   mode;             // Timer mode
   29:         UW     limit;              // Counter Limit
   30:         FP     ptmrhdr;    // Timer Hnadler
   31:         PRI    intpri;            // Interrupt priority
   32: 
   33:         void   *exinf;           // Extended information
   34: } T_PTMRCB;
   35: 
   36: T_PTMRCB ptmrcb[TK_MAX_PTIMER] = {
   37:         { OSTM1_BASE, -1, 0, (FP)NULL, INTPRI_OSTM1, 0 },      // ptimer No.1
   38:         { OSTM2_BASE, -1, 0, (FP)NULL, INTPRI_OSTM2, 0 },      // ptimer No.2
   39: };
   40: 
   41: #define OSTMn_CMP(n)    (ptmrcb[n].baddr + OSTMnCMP)
   42: #define OSTMn_CNT(n)    (ptmrcb[n].baddr + OSTMnCNT)
   43: #define OSTMn_TE(n)     (ptmrcb[n].baddr + OSTMnTE)
   44: #define OSTMn_TS(n)     (ptmrcb[n].baddr + OSTMnTS)
   45: #define OSTMn_TT(n)     (ptmrcb[n].baddr + OSTMnTT)
   46: #define OSTMn_CTL(n)    (ptmrcb[n].baddr + OSTMnCTL)
   47: 
   48: /*
   49:  * Physical timer interrupt handler
   50:  */
   51: LOCAL void ptmr_int_main( UINT intno, T_PTMRCB *p_cb)
   52: {
   53:         ClearInt( intno);
   54: 
   55:         if( p_cb->ptmrhdr != NULL) {
   56:                 (*p_cb->ptmrhdr)( p_cb->exinf);               // Execute user handler.
   57:         }
   58:         
   59:         if( p_cb->mode == TA_ALM_PTMR)  {
   60:                 DisableInt( intno);
   61:                 out_b( OSTMn_TT((intno==INTNO_OSTM1)?0:1), 1);        // Stop Pysical timer
   62:         }
   63: 
   64:         EndOfInt(intno);
   65: }
   66: 
   67: LOCAL void ptmr1_inthdr( UINT intno ) { ptmr_int_main( intno, &ptmrcb[0]); }
   68: LOCAL void ptmr2_inthdr( UINT intno ) { ptmr_int_main( intno, &ptmrcb[1]); }
   69: 
   70: LOCAL void (* const inthdr_tbl[])() = {
   71:         ptmr1_inthdr, ptmr2_inthdr
   72: };
   73: 
   74: LOCAL const UINT intno_tbl[] = {
   75:         INTNO_OSTM1, INTNO_OSTM2
   76: };
   77: 
   78: /*
   79:  * Start of physical timer operation
   80:  */
   81: EXPORT ER StartPhysicalTimer( UINT ptmrno, UW limit, UINT mode)
   82: {
   83:         T_DINT         dint;
   84:         UINT           intno;
   85:         ER             err;
   86: 
   87:         /* parameter check */
   88:         if(( ptmrno == 0 || ptmrno > TK_MAX_PTIMER )
   89:                 || ( limit == 0 ) || ( mode > TA_CYC_PTMR ))  return E_PAR;
   90: 
   91:         ptmrno--;
   92:         ptmrcb[ptmrno].limit   = limit;
   93:         ptmrcb[ptmrno].mode    = mode;
   94: 
   95:         /* Timer initialization */
   96:         out_b( OSTMn_TT(ptmrno), 1);           // Stop timer.
   97:         out_w( OSTMn_CMP(ptmrno), limit);      // Set the counter upper limit.
   98: 
   99:         /* Register interrupt handler */
  100:         intno          = intno_tbl[ptmrno];
  101:         dint.intatr    = TA_HLNG;
  102:         dint.inthdr    = inthdr_tbl[ptmrno];
  103:         err = tk_def_int( intno, &dint);
  104:         if(err != E_OK) {
  105:                 return err;
  106:         }
  107:         EnableInt( intno, ptmrcb[ptmrno].intpri);
  108: 
  109:         /* Set interval timer mode */
  110:         out_b(OSTMn_CTL(ptmrno), 0);
  111:         
  112:         /* Start Physical Timer */
  113:         out_b(OSTMn_TS(ptmrno), 1);
  114: 
  115:         return E_OK;
  116: }
  117: 
  118: EXPORT ER StopPhysicalTimer( UINT ptmrno )
  119: {
  120:         /* parameter check */
  121:         if( ptmrno == 0 || ptmrno > TK_MAX_PTIMER ) return E_PAR;
  122: 
  123:         ptmrno--;
  124: 
  125:         /* Stop Physical Timer */
  126:         DisableInt( intno_tbl[ptmrno]);
  127:         out_b( OSTMn_TT(ptmrno), 1);           // Stop timer.
  128: 
  129:         return E_OK;
  130: }
  131: 
  132: 
  133: IMPORT ER GetPhysicalTimerCount( UINT ptmrno, UW *p_count )
  134: {
  135:         /* parameter check */
  136:         if( ptmrno == 0 || ptmrno > TK_MAX_PTIMER ) return E_PAR;
  137: 
  138:         ptmrno--;
  139: 
  140:         /* Get Physical timer counter */
  141:         *p_count = ptmrcb[ptmrno].limit - in_w( OSTMn_CNT(ptmrno)) ;   // down counter.
  142: 
  143:         return E_OK;
  144: }
  145: 
  146: 
  147: EXPORT ER DefinePhysicalTimerHandler( UINT ptmrno, CONST T_DPTMR *pk_dptmr )
  148: {
  149:         /* parameter check */
  150:         if( ptmrno == 0 || ptmrno > TK_MAX_PTIMER ) return E_PAR;
  151: 
  152:         ptmrno--;
  153: 
  154:         /* Set user Handler */
  155:         if(pk_dptmr != NULL) {
  156:                 ptmrcb[ptmrno].ptmrhdr        = pk_dptmr->ptmrhdr;
  157:                 ptmrcb[ptmrno].exinf  = pk_dptmr->exinf;
  158:         } else {
  159:                 ptmrcb[ptmrno].ptmrhdr        = NULL;
  160:         }
  161: 
  162:         return E_OK;
  163: }
  164: 
  165: 
  166: EXPORT ER GetPhysicalTimerConfig(UINT ptmrno, T_RPTMR *pk_rptmr)
  167: {
  168:         /* parameter check */
  169:         if( ptmrno == 0 || ptmrno > TK_MAX_PTIMER ) return E_PAR;
  170: 
  171:         pk_rptmr->ptmrclk      = PTMCLK;
  172:         pk_rptmr->maxcount     = PTMR_MAX_CNT;
  173:         pk_rptmr->defhdr       = TRUE;
  174: 
  175:         return E_OK;
  176: }
  177: 
  178: #endif  /* USE_PTMR */
  179: #endif  /* CPU_RZA2M */