1    /*
     2     * Copyright (c) 2014-2015, Texas Instruments Incorporated
     3     * All rights reserved.
     4     *
     5     * Redistribution and use in source and binary forms, with or without
     6     * modification, are permitted provided that the following conditions
     7     * are met:
     8     *
     9     * *  Redistributions of source code must retain the above copyright
    10     *    notice, this list of conditions and the following disclaimer.
    11     *
    12     * *  Redistributions in binary form must reproduce the above copyright
    13     *    notice, this list of conditions and the following disclaimer in the
    14     *    documentation and/or other materials provided with the distribution.
    15     *
    16     * *  Neither the name of Texas Instruments Incorporated nor the names of
    17     *    its contributors may be used to endorse or promote products derived
    18     *    from this software without specific prior written permission.
    19     *
    20     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    21     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
    22     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    23     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    24     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    25     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    26     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
    27     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    28     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
    29     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
    30     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    31     */
    32    
    33    /*
    34     *  ======== Timer.xdc ========
    35     */
    36    
    37    package ti.sysbios.family.arm.lm4;
    38    
    39    import xdc.rov.ViewInfo;
    40    
    41    import xdc.runtime.Types;
    42    import xdc.runtime.Error;
    43    
    44    import ti.sysbios.interfaces.ITimer;
    45    import ti.sysbios.family.arm.m3.Hwi;
    46    
    47    /*!
    48     *  ======== Timer ========
    49     *  Stellaris LM4 Timer Peripheral Driver
    50     *
    51     *  Stellaris LM4 Timer Peripheral Driver for standalone use and
    52     *  as the {@link ti.sysbios.hal.Timer} delegate.
    53     *
    54     *  Implements {@link ti.sysbios.interfaces.ITimer}
    55     *
    56     *  @p(html)
    57     *  <h3> Calling Context </h3>
    58     *  <table border="1" cellpadding="3">
    59     *    <colgroup span="1"></colgroup> <colgroup span="5" align="center"></colgroup>
    60     *
    61     *    <tr><th> Function                 </th><th>  Hwi   </th><th>  Swi   </th><th>  Task  </th><th>  Main  </th><th>  Startup  </th></tr>
    62     *    <!--                                                                                                                 -->
    63     *    <tr><td> {@link #getAvailMask}            </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td></tr>
    64     *    <tr><td> {@link #getNumTimers}            </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td></tr>
    65     *    <tr><td> {@link #getStatus}               </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td></tr>
    66     *    <tr><td> {@link #Params_init}             </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td></tr>
    67     *    <tr><td> {@link #construct}               </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td></tr>
    68     *    <tr><td> {@link #create}                  </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td></tr>
    69     *    <tr><td> {@link #delete}                  </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td></tr>
    70     *    <tr><td> {@link #destruct}                </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td></tr>
    71     *    <tr><td> {@link #getCount}                </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td></tr>
    72     *    <tr><td> {@link #getFreq}                 </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td></tr>
    73     *    <tr><td> {@link #getPeriod}               </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td></tr>
    74     *    <tr><td> {@link #reconfig}                </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td></tr>
    75     *    <tr><td> {@link #setAvailMask}            </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td></tr>
    76     *    <tr><td> {@link #setPeriod}               </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td></tr>
    77     *    <tr><td> {@link #setPeriodMicroSecs}      </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td></tr>
    78     *    <tr><td> {@link #start}                   </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td></tr>
    79     *    <tr><td> {@link #stop}                    </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td><td>   -    </td></tr>
    80     *    <tr><td colspan="6"> Definitions: <br />
    81     *       <ul>
    82     *         <li> <b>Hwi</b>: API is callable from a Hwi thread. </li>
    83     *         <li> <b>Swi</b>: API is callable from a Swi thread. </li>
    84     *         <li> <b>Task</b>: API is callable from a Task thread. </li>
    85     *         <li> <b>Main</b>: API is callable during any of these phases: </li>
    86     *           <ul>
    87     *             <li> In your module startup after this module is started (e.g. Cache_Module_startupDone() returns TRUE). </li>
    88     *             <li> During xdc.runtime.Startup.lastFxns. </li>
    89     *             <li> During main().</li>
    90     *             <li> During BIOS.startupFxns.</li>
    91     *           </ul>
    92     *         <li> <b>Startup</b>: API is callable during any of these phases:</li>
    93     *           <ul>
    94     *             <li> During xdc.runtime.Startup.firstFxns.</li>
    95     *             <li> In your module startup before this module is started (e.g. Cache_Module_startupDone() returns FALSE).</li>
    96     *           </ul>
    97     *       </ul>
    98     *    </td></tr>
    99     *
   100     *  </table>
   101     *  @p
   102     */
   103    @ModuleStartup          /* to configure static timers */
   104    @InstanceInitStatic
   105    
   106    module Timer inherits ti.sysbios.interfaces.ITimer
   107    {
   108        /*! override supportsDynamic - this Timer DOES support RunMode_DYNAMIC */
   109        override metaonly config Bool supportsDynamic = true;
   110    
   111        /*! Max value of Timer period for PeriodType_COUNTS*/
   112        const UInt MAX_PERIOD = 0xFFFFFFFF;
   113    
   114        /*! Timer clock divider wrt cpu clock */
   115        const Int TIMER_CLOCK_DIVIDER = 1;
   116    
   117        /*! @_nodoc
   118         *  Min instructions to use in trigger().
   119         */
   120        const Int MIN_SWEEP_PERIOD = 1;
   121    
   122        /*! @_nodoc */
   123        @XmlDtd
   124        metaonly struct BasicView {
   125            Ptr         halTimerHandle;
   126            String      label;
   127            UInt        id;
   128            String      device;
   129            String      startMode;
   130            String      runMode;
   131            UInt        period;
   132            String      periodType;
   133            UInt        intNum;
   134            String      tickFxn[];
   135            UArg        arg;
   136            String      extFreq;
   137            String      hwiHandle;
   138        };
   139    
   140    
   141        /*! @_nodoc */
   142        metaonly struct ModuleView {
   143            String      availMask;      /* available 32-bit timer halves */
   144        }
   145    
   146        /*! @_nodoc */
   147        metaonly struct DeviceView {
   148            UInt        id;
   149            String      device;
   150            String      devAddr;
   151            UInt        intNum;
   152            String      runMode;
   153            UInt        period;
   154            UInt        currCount;
   155            UInt        remainingCount;
   156            UInt        prevThreshold;
   157            Bool        altclk;
   158            String      state;
   159        };
   160    
   161        /*! @_nodoc */
   162        @Facet
   163        metaonly config ViewInfo.Instance rovViewInfo =
   164            ViewInfo.create({
   165                viewMap: [
   166                [
   167                    'Basic',
   168                    {
   169                        type: ViewInfo.INSTANCE,
   170                        viewInitFxn: 'viewInitBasic',
   171                        structName: 'BasicView'
   172                    }
   173                ],
   174                [
   175                    'Device',
   176                    {
   177                        type: ViewInfo.INSTANCE,
   178                        viewInitFxn: 'viewInitDevice',
   179                        structName: 'DeviceView'
   180                    }
   181                ],
   182                [
   183                    'Module',
   184                    {
   185                        type: ViewInfo.MODULE,
   186                        viewInitFxn: 'viewInitModule',
   187                        structName: 'ModuleView'
   188                    }
   189                ],
   190                ]
   191            });
   192    
   193        /*! Timer enable function type definition. */
   194        typedef Void (*TimerEnableFuncPtr)(Int);
   195    
   196        /*! Timer disable function type definition. */
   197        typedef Void (*TimerDisableFuncPtr)(Int);
   198    
   199        /*!
   200         *  Error raised when timer id specified is not supported.
   201         */
   202        config Error.Id E_invalidTimer  = {msg: "E_invalidTimer: Invalid Timer Id %d"};
   203    
   204        /*!
   205         *  Error raised when timer requested is in use
   206         */
   207        config Error.Id E_notAvailable  =
   208            {msg: "E_notAvailable: Timer not available %d"};
   209    
   210        /*!
   211         *  Error raised when period requested is not supported
   212         */
   213        config Error.Id E_cannotSupport  =
   214            {msg: "E_cannotSupport: Timer cannot support requested period %d"};
   215    
   216        /*!
   217         *  Error raised when timer does not support altclk
   218         */
   219        config Error.Id E_noaltclk  =
   220            {msg: "E_noaltclk: Timer does not support altclk"};
   221    
   222        /*!
   223         *  ======== anyMask ========
   224         *  Available mask to be used when select = Timer_ANY
   225         */
   226        config UInt anyMask = 0x3F;
   227    
   228        /*!
   229         *  ======== supportsAltclk ========
   230         */
   231        config Bool supportsAltclk = true;
   232    
   233        /*!
   234         *  ======== enableFunc ========
   235         *  Pointer to Timer enable callback function
   236         *
   237         *  Timer enable callback function enables the timer clock and resets
   238         *  the timer.
   239         */
   240        config TimerEnableFuncPtr enableFunc = null;
   241    
   242        /*!
   243         *  ======== disableFunc ========
   244         *  Pointer to Timer disable callback function
   245         *
   246         *  Timer disable callback function disables the timer clock.
   247         */
   248        config TimerDisableFuncPtr disableFunc = null;
   249    
   250        /*!
   251         *  ======== getHandle ========
   252         *  Get Handle associated with a timer id.
   253         *
   254         *  @param(id)      timer Id.
   255         *  @b(returns)     timer Handle
   256         */
   257        Handle getHandle(UInt id);
   258    
   259        /*!
   260         *  ======== getAvailMask ========
   261         *  Returns the availMask.
   262         *
   263         *  @b(returns)     Mask of available timers
   264         */
   265        UInt getAvailMask();
   266    
   267        /*!
   268         *  ======== setAvailMask ========
   269         *  Set the availMask to given mask.
   270         *
   271         *  This function validates the given mask to ensure it does not mark
   272         *  any currently used timer as available. If validation is successful,
   273         *  the mask overwrites the current availMask and the function returns
   274         *  TRUE. Otherwise, the mask is discarded and the function returns
   275         *  FALSE.
   276         *
   277         *  @param(mask)    Mask used to write to availMask
   278         */
   279        Bool setAvailMask(UInt mask);
   280    
   281        /*!
   282         *  ======== isrStub ========
   283         *  @_nodoc
   284         *  Timer interrupts must be acknowledged
   285         *
   286         *  @param(arg)     Timer object.
   287         */
   288        Void isrStub(UArg arg);
   289    
   290    instance:
   291    
   292        /*! Hwi Params for Hwi Object. Default is null. */
   293        config Hwi.Params *hwiParams = null;
   294    
   295        /*! Previous threshold count value. */
   296        config UInt prevThreshold = 0xFFFFFFFF;
   297    
   298        /*!
   299         *  Should timer run off of altclk? Default is false. Note that altclk
   300         *  is only supported on a subset of LM4 devices, and some timers may
   301         *  not be capable of running off of altclk.
   302         */
   303        config Bool altclk = false;
   304    
   305        /*!
   306         *  ======== reconfig ========
   307         *  Used to modify static timer instances at runtime.
   308         *
   309         *  @param(timerParams)     timer Params
   310         *  @param(tickFxn)         function that runs when timer expires
   311         */
   312        Void reconfig(FuncPtr tickFxn, const Params *timerParams, Error.Block *eb);
   313    
   314        /*!
   315         *  ======== getExpiredCounts64 ========
   316         *  Get current timer counter
   317         *
   318         *  @_nodoc
   319         *  Reads timer counter and adds period if IFR was set 
   320         *  before counter read. Used exclusively by TimestampProvider.
   321         *
   322         *  Must be called with interrupts disabled.
   323         *
   324         *  @b(returns)     expired counts.
   325         */
   326        UInt64 getExpiredCounts64();
   327    
   328    internal:   /* not for client use */
   329    
   330        /*
   331         *  ======== noStartupNeeded ========
   332         *  Flag used to prevent misc code from being brought in
   333         *  un-necessarily
   334         */
   335        config UInt startupNeeded = false;
   336    
   337        /*! Information about timer */
   338        struct TimerDevice {
   339            UInt intNum;
   340            Ptr  baseAddr;
   341        };
   342    
   343        /*!
   344         *  ======== numTimerDevices ========
   345         *  The number of logical timers on a device.
   346         */
   347        config Int numTimerDevices;
   348    
   349        /*!
   350         *  ======== enableCC26xx ========
   351         */
   352        Void enableCC26xx(Int id);
   353    
   354        /*!
   355         *  ======== enableCC3200 ========
   356         */
   357        Void enableCC3200(Int id);
   358    
   359        /*!
   360         *  ======== disableCC26xx ========
   361         */
   362        Void disableCC26xx(Int id);
   363    
   364        /*!
   365         *  ======== disableCC3200 ========
   366         */
   367        Void disableCC3200(Int id);
   368    
   369        /*!
   370         *  ======== enableTiva ========
   371         */
   372        Void enableTiva(Int id);
   373    
   374        /*!
   375         *  ======== disableTiva ========
   376         */
   377        Void disableTiva(Int id);
   378    
   379        /*
   380         *  ======== initDevice ========
   381         *  reset timer to its resting state
   382         */
   383        Void initDevice(Object *timer);
   384    
   385        /*
   386         *  ======== postInit ========
   387         *  finish initializing static and dynamic Timers
   388         */
   389        Int postInit(Object *timer, Error.Block *eb);
   390    
   391        /*
   392         *  ======== enableTimers ========
   393         *  enable timer register access
   394         *  called as a Startup.firstFxnw
   395         */
   396        Void enableTimers();
   397    
   398        /*
   399         *  ======== masterDisable ========
   400         *  disable all interrupts by clearing PRIMASK bit
   401         */
   402        Bool masterDisable();
   403    
   404        /*
   405         *  ======== masterEnable ========
   406         *  set PRIMASK bit to enable interrupts
   407         */
   408        Void masterEnable();
   409    
   410        /*
   411         *  ======== write ========
   412         *  write a value to a timer register
   413         */
   414        Void write(Bool altclk, volatile UInt32 *pReg, UInt32 val);
   415    
   416        struct Instance_State {
   417            Bool            staticInst;
   418            Int             id;
   419            RunMode         runMode;
   420            StartMode       startMode;
   421            UInt            period;
   422            PeriodType      periodType;
   423            UInt            intNum;
   424            UArg            arg;
   425            Hwi.FuncPtr     tickFxn;
   426            Types.FreqHz    extFreq;        /* external frequency in Hz */
   427            Hwi.Handle      hwi;
   428            UInt            prevThreshold;
   429            UInt            rollovers;
   430            UInt            savedCurrCount;
   431            Bool            altclk;
   432        }
   433    
   434        struct Module_State {
   435            UInt            availMask;      /* available peripherals */
   436            TimerDevice     device[];       /* timer device information */
   437            Handle          handles[];      /* array of handles based on id */
   438        }
   439    }