1    /* 
     2     * Copyright (c) 2011, 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     *  ======== Event.xdc ========
    34     *
    35     *1                     aren't synced to Semaphore state
    36     *! 04-Mar-2008 agd     Stripped down to support binary only and 
    37     *!                     single task.
    38     *! 23-Jan-2008 agd     01/22/08 review changes
    39     *! 13-Dec-2007 agd     Added Event_Id_NONE
    40     *! 10-Dec-2007 connell updated call context table
    41     *! 23-May-2007 agd     Addressed SDSCM00017698
    42     *! 11-May-2007 agd     Addressed SDSCM00016595
    43     *! 16-Apr-2007 cmcc    Added calling context table
    44     *! 01-Feb-2007 rt      Added Log.Events
    45     *! 16-Jan-2007 agd     Moved to ipc package
    46     *! 12-Dec-2006 agd     integrate 12/5/06 code review comments
    47     *! 27-Sep-2006 agd     reworked pre-defined event masks
    48     *! 21-Apr-2006 agd     reworked to support counting Events
    49     *! 02-Mar-2006 agd     redefined default event mask constants
    50     *! 14-Feb-2006 agd     reworked to conform to
    51     *!                     current design requirements
    52     *! 31-Jan-2006 nitya/agd created
    53     */
    54    
    55    package ti.sysbios.knl;
    56    
    57    import xdc.rov.ViewInfo;
    58    
    59    import xdc.runtime.Assert;
    60    import xdc.runtime.Diags;
    61    import xdc.runtime.Log;
    62    
    63    import ti.sysbios.knl.Queue;
    64    import ti.sysbios.knl.Clock;
    65    import ti.sysbios.knl.Task;
    66    
    67    /*!
    68     *  ======== Event ========
    69     *  Event Manager.
    70     *
    71     *  SYS/BIOS events are a means of communication between Tasks and other 
    72     *  threads such as Hwis, Swis, and other Tasks, or between Tasks and 
    73     *  other SYS/BIOS objects. Other SYS/BIOS objects include semaphores, 
    74     *  mailboxes,
    75     *  message queues, etc. Only tasks can wait for events; whereas tasks,
    76     *  Hwis, Swis, or SYS/BIOS objects can post them.
    77     *
    78     *  In order for a task to be notified of an event from a SYS/BIOS object an 
    79     *  event object must first be registered with that object. Separate APIs
    80     *  are provided (in their respective modules) for each of the SYS/BIOS object
    81     *  types that support this feature.
    82     * 
    83     *  Events are synchronous in nature, meaning that a receiving task will 
    84     *  block or pend while waiting for the events to occur. When the desired 
    85     *  events are received, the pending task continues its execution, as it 
    86     *  would after a call to Semaphore_pend(), for example.
    87     *
    88     *  Tasks can also wait on events that are not linked to other SYS/BIOS objects.
    89     *  These events are explicitly posted from other threads such as tasks,
    90     *  Swis, or Hwis. A task does not register to receive these events; the 
    91     *  sending thread simply posts its event(s) to the event object the task
    92     *  is pending on. This scenario is similar to having an ISR post a 
    93     *  semaphore.
    94     *
    95     *  A task can wait on events from multiple resources and/or threads; thus,
    96     *  it can be waiting for a semaphore to be posted and for a message to 
    97     *  arrive in a message queue or an ISR thread to signal that an event has
    98     *  occurred.
    99     *
   100     *  Events are binary. Events become available (posted) on each Event_post()
   101     *  of the eventId and become non-available (consumed) on each qualifying 
   102     *  Event_pend() mask.
   103     *
   104     *  Unlike Semaphores, only a single task can pend on an Event object.
   105     *
   106     *  {@link #pend} is used to wait for events. The andMask & orMask 
   107     *  determine which
   108     *  event(s) must occur before returning from {@link #pend}. The timeout 
   109     *  parameter
   110     *  allows the task to wait until a timeout, wait indefinitely, or not wait 
   111     *  at all. A return value of zero indicates that a timeout has occurred. A
   112     *  non-zero return value is the set of events that were active at the time
   113     *  the task was unblocked. 
   114     *
   115     *  The andMask defines a set of events that must ALL occur to allow 
   116     *  {@link #pend} to return. 
   117     *
   118     *  The orMask defines a set of events that will cause {@link #pend} to 
   119     *  return if ANY of them occur. 
   120     *
   121     *  All active events present in the orMask are consumed (ie removed from 
   122     *  the event object) upon return from {@link #pend}. Only when all events 
   123     *  present in the andMask are active are they consumed on return from 
   124     *  {@link #pend}. 
   125     *
   126     *  {@link #pend} returns immediately if the andMask OR orMask conditions 
   127     *  are true upon entry.
   128     *
   129     *  {@link #post} is used to signal events. If a task is waiting for the event
   130     *  and all of the event conditions are met, {@link #post} unblocks the task.
   131     *  If no task is waiting, {@link #post} simply registers the event with the
   132     *  event object and returns.
   133     *
   134     *  The maximum number of eventIds supported is target specific and depends 
   135     *  on the number of bits in a UInt data type.
   136     *  For 6x and ARM devices the maximum number of eventIds is therefore 32.
   137     *  For 28x and MSP430 devices, the maximum number of eventIds is 16.
   138     *
   139     *  @p(html)
   140     *  <h3> Calling Context </h3>
   141     *  <table border="1" cellpadding="3">
   142     *    <colgroup span="1"></colgroup> <colgroup span="5" align="center">
   143     *    </colgroup>
   144     *
   145     *    <tr><th> Function                 </th><th>  Hwi   </th><th>  Swi   </th>
   146     *    <th>  Task  </th><th>  Main  </th><th>  Startup  </th></tr>
   147     *    <!--                                                                  -->
   148     *    <tr><td> {@link #create}          </td><td>   N    </td><td>   N    </td>
   149     *    <td>   Y    </td><td>   Y    </td><td>   N    </td></tr>
   150     *    <tr><td> {@link #Params_init}     </td><td>   Y    </td><td>   Y    </td>
   151     *    <td>   Y    </td><td>   Y    </td><td>   Y    </td></tr>
   152     *    <tr><td> {@link #construct}       </td><td>   N    </td><td>   N    </td>
   153     *    <td>   Y    </td><td>   Y    </td><td>   N    </td></tr>
   154     *    <tr><td> {@link #delete}          </td><td>   N    </td><td>   N    </td>
   155     *    <td>   Y    </td><td>   Y    </td><td>   N    </td></tr>
   156     *    <tr><td> {@link #destruct}        </td><td>   N    </td><td>   N    </td>
   157     *    <td>   Y    </td><td>   Y    </td><td>   N    </td></tr>
   158     *    <tr><td> {@link #getPostedEvents} </td><td>   Y    </td><td>   Y    </td>
   159     *    <td>   Y    </td><td>   Y    </td><td>   Y    </td></tr>
   160     *    <tr><td> {@link #pend}            </td><td>   N    </td><td>   N    </td>
   161     *    <td>   Y    </td><td>   N    </td><td>   N    </td></tr>
   162     *    <tr><td> {@link #post}            </td><td>   Y    </td><td>   Y    </td>
   163     *    <td>   Y    </td><td>   Y    </td><td>   N    </td></tr>
   164     *    <tr><td colspan="6"> Definitions: <br />
   165     *       <ul>
   166     *         <li> <b>Hwi</b>: API is callable from a Hwi thread. </li>
   167     *         <li> <b>Swi</b>: API is callable from a Swi thread. </li>
   168     *         <li> <b>Task</b>: API is callable from a Task thread. </li>
   169     *         <li> <b>Main</b>: API is callable during any of these phases: </li>
   170     *           <ul>
   171     *             <li> In your module startup after this module is started 
   172     *    (e.g. Event_Module_startupDone() returns TRUE). </li>
   173     *             <li> During xdc.runtime.Startup.lastFxns. </li>
   174     *             <li> During main().</li>
   175     *             <li> During BIOS.startupFxns.</li>
   176     *           </ul>
   177     *         <li> <b>Startup</b>: API is callable during any of these phases:</li>
   178     *           <ul>
   179     *             <li> During xdc.runtime.Startup.firstFxns.</li>
   180     *             <li> In your module startup before this module is started 
   181     *    (e.g. Event_Module_startupDone() returns FALSE).</li>
   182     *           </ul>
   183     *       </ul>
   184     *    </td></tr>
   185     *
   186     *  </table>
   187     *  @p 
   188     */
   189    
   190    /* 
   191     *  To remove Event.Ids not defined for targets with 16 bit Ints 
   192     */
   193    @CustomHeader   
   194    
   195    module Event 
   196    {
   197        /*! 
   198         *  Pre-defined Event Ids 
   199         *
   200         *  Event_Ids are provided to simplify the specification of
   201         *  andMasks and orMasks arguments to {@link #pend Event_pend()}.
   202         *
   203         *  Since each Event_Id is a bitmask composed of only a single bit, 
   204         *  a group of Event_Ids within an andMask or orMask can be indicated
   205         *  by simply adding them together. For instance, to indicate that a task
   206         *  is to be awakened only when both Event_Id_00 and Event_Id_01 have
   207         *  been posted, the andMask for Event_pend() would be constructed
   208         *  as below:
   209         *
   210         *  @p(code)
   211         *  Event_pend(event, Event_Id_00+EventId_01, Event_Id_NONE,
   212         *             BIOS_WAIT_FOREVER);
   213         *  @p
   214         *
   215         *  As shown above, Event_Id_NONE is to be used as an empty (NULL) 
   216         *  andMask or orMask.
   217         *
   218         *  @a(Note)
   219         *  For targets where a UInt is 32 bits in length, 
   220         *  Event_Id_00 thru Event_Id_31 can be used.
   221         *
   222         *  For targets where a UInt is 16 bits in length, 
   223         *  Event_Id_00 thru Event_Id_15 can be used.
   224         */
   225    
   226        const UInt Id_00 = 0x1;
   227        const UInt Id_01 = 0x2;         /*! @see #Id_00 Pre-defined Event IDs. */
   228        const UInt Id_02 = 0x4;         /*! @see #Id_00 Pre-defined Event IDs. */
   229        const UInt Id_03 = 0x8;         /*! @see #Id_00 Pre-defined Event IDs. */
   230        const UInt Id_04 = 0x10;        /*! @see #Id_00 Pre-defined Event IDs. */
   231        const UInt Id_05 = 0x20;        /*! @see #Id_00 Pre-defined Event IDs. */
   232        const UInt Id_06 = 0x40;        /*! @see #Id_00 Pre-defined Event IDs. */
   233        const UInt Id_07 = 0x80;        /*! @see #Id_00 Pre-defined Event IDs. */
   234        const UInt Id_08 = 0x100;       /*! @see #Id_00 Pre-defined Event IDs. */
   235        const UInt Id_09 = 0x200;       /*! @see #Id_00 Pre-defined Event IDs. */
   236        const UInt Id_10 = 0x400;       /*! @see #Id_00 Pre-defined Event IDs. */
   237        const UInt Id_11 = 0x800;       /*! @see #Id_00 Pre-defined Event IDs. */
   238        const UInt Id_12 = 0x1000;      /*! @see #Id_00 Pre-defined Event IDs. */
   239        const UInt Id_13 = 0x2000;      /*! @see #Id_00 Pre-defined Event IDs. */
   240        const UInt Id_14 = 0x4000;      /*! @see #Id_00 Pre-defined Event IDs. */
   241        const UInt Id_15 = 0x8000;      /*! @see #Id_00 Pre-defined Event IDs. */
   242        const UInt Id_16 = 0x10000;     /*! @see #Id_00 Pre-defined Event IDs. */
   243        const UInt Id_17 = 0x20000;     /*! @see #Id_00 Pre-defined Event IDs. */
   244        const UInt Id_18 = 0x40000;     /*! @see #Id_00 Pre-defined Event IDs. */
   245        const UInt Id_19 = 0x80000;     /*! @see #Id_00 Pre-defined Event IDs. */
   246        const UInt Id_20 = 0x100000;    /*! @see #Id_00 Pre-defined Event IDs. */
   247        const UInt Id_21 = 0x200000;    /*! @see #Id_00 Pre-defined Event IDs. */
   248        const UInt Id_22 = 0x400000;    /*! @see #Id_00 Pre-defined Event IDs. */
   249        const UInt Id_23 = 0x800000;    /*! @see #Id_00 Pre-defined Event IDs. */
   250        const UInt Id_24 = 0x1000000;   /*! @see #Id_00 Pre-defined Event IDs. */
   251        const UInt Id_25 = 0x2000000;   /*! @see #Id_00 Pre-defined Event IDs. */
   252        const UInt Id_26 = 0x4000000;   /*! @see #Id_00 Pre-defined Event IDs. */
   253        const UInt Id_27 = 0x8000000;   /*! @see #Id_00 Pre-defined Event IDs. */
   254        const UInt Id_28 = 0x10000000;  /*! @see #Id_00 Pre-defined Event IDs. */
   255        const UInt Id_29 = 0x20000000;  /*! @see #Id_00 Pre-defined Event IDs. */
   256        const UInt Id_30 = 0x40000000;  /*! @see #Id_00 Pre-defined Event IDs. */
   257        const UInt Id_31 = 0x80000000;  /*! @see #Id_00 Pre-defined Event IDs. */
   258    
   259        const UInt Id_NONE = 0;         /*! @see #Id_00 Pre-defined Event IDs. */     
   260    
   261        metaonly struct BasicView {
   262            String          label;
   263            String          postedEvents;
   264            String          pendedTask;
   265            String          andMask;
   266            String          orMask;
   267            String          timeout;
   268        };
   269        
   270        @Facet
   271        metaonly config ViewInfo.Instance rovViewInfo = 
   272            ViewInfo.create({
   273                viewMap: [
   274                    ['Basic', {type: ViewInfo.INSTANCE, viewInitFxn: 'viewInitBasic', 
   275                    structName: 'BasicView'}]
   276                ]
   277            });
   278    
   279        // -------- Module Parameters --------
   280    
   281        // Logs
   282    
   283        /*! Logged on calls to Event_post() */
   284        config Log.Event LM_post = {
   285            mask: Diags.USER1 | Diags.USER2,
   286            msg: "LM_post: event: 0x%x, currEvents: 0x%x, eventId: 0x%x"
   287        };
   288    
   289        /*! Logged on calls to Event_pend() */
   290        config Log.Event LM_pend = {
   291            mask: Diags.USER1 | Diags.USER2,
   292            msg: "LM_pend: event: 0x%x, currEvents: 0x%x, andMask: 0x%x, orMask: 0x%x, timeout: %d"
   293        };
   294    
   295        // Asserts
   296    
   297        /*!
   298         *  Asserted when {@link #pend} is called with andMask and orMask == 0
   299         */
   300        config Assert.Id A_nullEventMasks = {
   301            msg: "A_nullEventMasks: orMask and andMask are null."
   302        };
   303    
   304        /*!
   305         *  Asserted when {@link #post} is called with eventId == 0
   306         */
   307        config Assert.Id A_nullEventId = {
   308            msg: "A_nullEventId: posted eventId is null."
   309        };
   310    
   311        /*!
   312         *  Asserted when {@link #pend} is called by multiple tasks on the same
   313         *  Event object.
   314         */
   315        config Assert.Id A_eventInUse = {
   316            msg: "A_eventInUse: Event object already in use."
   317        };
   318    
   319        /*!
   320         *  Asserted when {@link #pend} is called with non-zero timeout from other
   321         *  than a Task context.
   322         */
   323        config Assert.Id A_badContext = {
   324            msg: "A_badContext: bad calling context. Must be called from a Task."
   325        };
   326    
   327    instance:
   328    
   329        /*!
   330         *  ======== create ========
   331         *  Create an Event object.
   332         *
   333         *  This function creates a new Event object.
   334         */
   335        @DirectCall
   336        create();
   337    
   338        /*!
   339         *  ======== pend ========
   340         *  Wait for events defined in 'and' OR 'or' masks.
   341         *
   342         *  pend() is used to wait for events. The andMask and orMask determine 
   343         *  which
   344         *  event(s) must occur before returning from pend(). The timeout parameter
   345         *  allows the task to wait until a timeout, wait indefinitely, or not wait 
   346         *  at all. A return value of zero indicates that a timeout has occurred. A
   347         *  non-zero return value is the set of events that were active at the time
   348         *  the task was unblocked. 
   349         *
   350         *  The andMask defines a set of events that must
   351         *  ALL occur to allow pend() to return. 
   352         *
   353         *  The orMask defines a set of events
   354         *  that will cause pend() to return if ANY of them occur. 
   355         *
   356         *  All active events
   357         *  present in the orMask are consumed (i.e. removed from the event object) 
   358         *  upon return from pend(). Only when all events present in the andMask 
   359         *  are active are they consumed on return from pend().
   360         *
   361         *  The pend conditions are satisfied when either ALL of the events
   362         *  in the andMask occur or ANY of the events in the orMask occur.
   363         *
   364         *  A timeout value of 
   365         *  {@link ti.sysbios.BIOS#WAIT_FOREVER, BIOS_WAIT_FOREVER} causes 
   366         *  the task to wait indefinitely for matching events to be posted. 
   367         *
   368         *  A timeout value of {@link ti.sysbios.BIOS#NO_WAIT, BIOS_NO_WAIT} 
   369         *  causes Event_pend to return immediately. 
   370         *
   371         *  @param(andMask)         return from pend() when ALL of these 
   372         *                          events have occurred
   373         *  @param(orMask)          return from pend() when ANY of these
   374         *                          events have occurred
   375         *  @param(timeout)         return from pend() after this many system 
   376         *                          time units
   377         *
   378         *  @b(returns)             All consumed events or zero if timeout.
   379         */
   380        @DirectCall
   381        UInt pend(UInt andMask, UInt orMask, UInt timeout);
   382    
   383        /*!
   384         *  ======== post ========
   385         *  Post event(s) to an event object.
   386         *
   387         *  Event_post() is used to signal events. If a task is waiting for the 
   388         *  event and the event conditions are met, post() unblocks the 
   389         *  task. If no tasks are waiting, post() simply registers the event with 
   390         *  the event object and returns.
   391         * 
   392         *  @param(eventMask)         mask of eventIds to post (must be non-zero)
   393         */
   394        @DirectCall
   395        Void post(UInt eventMask);
   396    
   397        /*!
   398         *  ======== getPostedEvents ========
   399         *  Returns the set of events that have not been consumed by a task
   400         *  (ie those events that have not fully satisfied any pend() conditions).
   401         *  No events are consumed!
   402         *
   403         *  @b(returns)             All un-consumed events posted to the event.
   404         */
   405        @DirectCall
   406        UInt getPostedEvents();
   407    
   408        /*!
   409         *  @_nodoc
   410         *  ======== sync ========
   411         *  synchronize an eventId to the value given
   412         *  must call with interrupts disabled
   413         *
   414         *  @param(eventId)         eventId to sync
   415         *  @param(count)           count
   416         */
   417        @DirectCall
   418        Void sync(UInt eventId, UInt count);
   419    
   420        /*!
   421         *  @_nodoc
   422         *  ======== syncMeta ========
   423         *  synchronize an eventId to the value given (static version)
   424         *
   425         *  @param(eventId)         eventId to sync
   426         *  @param(count)           count
   427         */
   428        metaonly Void syncMeta(UInt eventId, UInt count);
   429    
   430    internal:
   431    
   432        /*!
   433         *  ======== pendTimeout ========
   434         *  This function is the clock event handler for pend
   435         */
   436        Void pendTimeout(UArg arg);
   437    
   438        /* pendQ Element PendStates */
   439        enum PendState {
   440            PendState_TIMEOUT = 0,          /* Clock timeout */
   441            PendState_POSTED = 1,           /* posted by post */
   442            PendState_CLOCK_WAIT = 2,       /* in Clock queue */
   443            PendState_WAIT_FOREVER = 3      /* not in Clock queue */
   444        };
   445        
   446        /* Event pendQ Element */
   447        struct PendElem {
   448            Task.PendElem           tpElem;
   449            PendState               pendState;
   450            UInt                    matchingEvents;
   451            UInt                    andMask;
   452            UInt                    orMask;
   453        };
   454    
   455        struct Instance_State {
   456            volatile UInt           postedEvents;   /* Current unconsumed events */
   457            Queue.Object            pendQ;          /* queue of PendElems  */
   458                                                    /* for compatibility with */
   459                                                    /* Semaphore */
   460        };
   461    }
   462    /*
   463     *  @(#) ti.sysbios.knl; 2, 0, 0, 0,451; 2-2-2011 15:07:13; /db/vtree/library/trees/avala/avala-o27x/src/ xlibrary
   464    
   465     */
   466