PageRenderTime 161ms CodeModel.GetById 101ms app.highlight 52ms RepoModel.GetById 1ms app.codeStats 0ms

/src/compiler/ucos-vs2008/UCOS_SIM/src/ucosii/os_flag.c

http://ftk.googlecode.com/
C | 1146 lines | 684 code | 78 blank | 384 comment | 168 complexity | a63cb391c7b63d8ecbd98fd475c6e1e4 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1/*
   2*********************************************************************************************************
   3*                                                uC/OS-II
   4*                                          The Real-Time Kernel
   5*                                         EVENT FLAG  MANAGEMENT
   6*
   7*                          (c) Copyright 2001-2005, Jean J. Labrosse, Weston, FL
   8*                                           All Rights Reserved
   9*
  10* File    : OS_FLAG.C
  11* By      : Jean J. Labrosse
  12* Version : V2.80
  13*********************************************************************************************************
  14*/
  15
  16#ifndef  OS_MASTER_FILE
  17#include <ucos_ii.h>
  18#endif
  19
  20#if (OS_VERSION >= 251) && (OS_FLAG_EN > 0) && (OS_MAX_FLAGS > 0)
  21/*
  22*********************************************************************************************************
  23*                                            LOCAL PROTOTYPES
  24*********************************************************************************************************
  25*/
  26
  27static  void     OS_FlagBlock(OS_FLAG_GRP *pgrp, OS_FLAG_NODE *pnode, OS_FLAGS flags, INT8U wait_type, INT16U timeout);
  28static  BOOLEAN  OS_FlagTaskRdy(OS_FLAG_NODE *pnode, OS_FLAGS flags_rdy);
  29
  30/*$PAGE*/
  31/*
  32*********************************************************************************************************
  33*                              CHECK THE STATUS OF FLAGS IN AN EVENT FLAG GROUP
  34*
  35* Description: This function is called to check the status of a combination of bits to be set or cleared
  36*              in an event flag group.  Your application can check for ANY bit to be set/cleared or ALL
  37*              bits to be set/cleared.
  38*
  39*              This call does not block if the desired flags are not present.
  40*
  41* Arguments  : pgrp          is a pointer to the desired event flag group.
  42*
  43*              flags         Is a bit pattern indicating which bit(s) (i.e. flags) you wish to check.
  44*                            The bits you want are specified by setting the corresponding bits in
  45*                            'flags'.  e.g. if your application wants to wait for bits 0 and 1 then
  46*                            'flags' would contain 0x03.
  47*
  48*              wait_type     specifies whether you want ALL bits to be set/cleared or ANY of the bits
  49*                            to be set/cleared.
  50*                            You can specify the following argument:
  51*
  52*                            OS_FLAG_WAIT_CLR_ALL   You will check ALL bits in 'flags' to be clear (0)
  53*                            OS_FLAG_WAIT_CLR_ANY   You will check ANY bit  in 'flags' to be clear (0)
  54*                            OS_FLAG_WAIT_SET_ALL   You will check ALL bits in 'flags' to be set   (1)
  55*                            OS_FLAG_WAIT_SET_ANY   You will check ANY bit  in 'flags' to be set   (1)
  56*
  57*                            NOTE: Add OS_FLAG_CONSUME if you want the event flag to be 'consumed' by
  58*                                  the call.  Example, to wait for any flag in a group AND then clear
  59*                                  the flags that are present, set 'wait_type' to:
  60*
  61*                                  OS_FLAG_WAIT_SET_ANY + OS_FLAG_CONSUME
  62*
  63*              err           is a pointer to an error code and can be:
  64*                            OS_NO_ERR              No error
  65*                            OS_ERR_EVENT_TYPE      You are not pointing to an event flag group
  66*                            OS_FLAG_ERR_WAIT_TYPE  You didn't specify a proper 'wait_type' argument.
  67*                            OS_FLAG_INVALID_PGRP   You passed a NULL pointer instead of the event flag
  68*                                                   group handle.
  69*                            OS_FLAG_ERR_NOT_RDY    The desired flags you are waiting for are not
  70*                                                   available.
  71*
  72* Returns    : The flags in the event flag group that made the task ready or, 0 if a timeout or an error
  73*              occurred.
  74*
  75* Called from: Task or ISR
  76*
  77* Note(s)    : 1) IMPORTANT, the behavior of this function has changed from PREVIOUS versions.  The
  78*                 function NOW returns the flags that were ready INSTEAD of the current state of the
  79*                 event flags.
  80*********************************************************************************************************
  81*/
  82
  83#if OS_FLAG_ACCEPT_EN > 0
  84OS_FLAGS  OSFlagAccept (OS_FLAG_GRP *pgrp, OS_FLAGS flags, INT8U wait_type, INT8U *err)
  85{
  86    OS_FLAGS      flags_rdy;
  87    INT8U         result;
  88    BOOLEAN       consume;
  89#if OS_CRITICAL_METHOD == 3                                /* Allocate storage for CPU status register */
  90    OS_CPU_SR     cpu_sr = 0;
  91#endif
  92
  93
  94
  95#if OS_ARG_CHK_EN > 0
  96    if (err == (INT8U *)0) {                               /* Validate 'err'                           */
  97        return ((OS_FLAGS)0);
  98    }
  99    if (pgrp == (OS_FLAG_GRP *)0) {                        /* Validate 'pgrp'                          */
 100        *err = OS_FLAG_INVALID_PGRP;
 101        return ((OS_FLAGS)0);
 102    }
 103#endif
 104    if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG) {          /* Validate event block type                */
 105        *err = OS_ERR_EVENT_TYPE;
 106        return ((OS_FLAGS)0);
 107    }
 108    result = wait_type & OS_FLAG_CONSUME;
 109    if (result != (INT8U)0) {                              /* See if we need to consume the flags      */
 110        wait_type &= ~OS_FLAG_CONSUME;
 111        consume    = TRUE;
 112    } else {
 113        consume    = FALSE;
 114    }
 115/*$PAGE*/
 116    *err = OS_NO_ERR;                                      /* Assume NO error until proven otherwise.  */
 117    OS_ENTER_CRITICAL();
 118    switch (wait_type) {
 119        case OS_FLAG_WAIT_SET_ALL:                         /* See if all required flags are set        */
 120             flags_rdy = pgrp->OSFlagFlags & flags;        /* Extract only the bits we want            */
 121             if (flags_rdy == flags) {                     /* Must match ALL the bits that we want     */
 122                 if (consume == TRUE) {                    /* See if we need to consume the flags      */
 123                     pgrp->OSFlagFlags &= ~flags_rdy;      /* Clear ONLY the flags that we wanted      */
 124                 }
 125             } else {
 126                 *err  = OS_FLAG_ERR_NOT_RDY;
 127             }
 128             OS_EXIT_CRITICAL();
 129             break;
 130
 131        case OS_FLAG_WAIT_SET_ANY:
 132             flags_rdy = pgrp->OSFlagFlags & flags;        /* Extract only the bits we want            */
 133             if (flags_rdy != (OS_FLAGS)0) {               /* See if any flag set                      */
 134                 if (consume == TRUE) {                    /* See if we need to consume the flags      */
 135                     pgrp->OSFlagFlags &= ~flags_rdy;      /* Clear ONLY the flags that we got         */
 136                 }
 137             } else {
 138                 *err  = OS_FLAG_ERR_NOT_RDY;
 139             }
 140             OS_EXIT_CRITICAL();
 141             break;
 142
 143#if OS_FLAG_WAIT_CLR_EN > 0
 144        case OS_FLAG_WAIT_CLR_ALL:                         /* See if all required flags are cleared    */
 145             flags_rdy = (OS_FLAGS)~pgrp->OSFlagFlags & flags; /* Extract only the bits we want        */
 146             if (flags_rdy == flags) {                     /* Must match ALL the bits that we want     */
 147                 if (consume == TRUE) {                    /* See if we need to consume the flags      */
 148                     pgrp->OSFlagFlags |= flags_rdy;       /* Set ONLY the flags that we wanted        */
 149                 }
 150             } else {
 151                 *err  = OS_FLAG_ERR_NOT_RDY;
 152             }
 153             OS_EXIT_CRITICAL();
 154             break;
 155
 156        case OS_FLAG_WAIT_CLR_ANY:
 157             flags_rdy = (OS_FLAGS)~pgrp->OSFlagFlags & flags; /* Extract only the bits we want        */
 158             if (flags_rdy != (OS_FLAGS)0) {               /* See if any flag cleared                  */
 159                 if (consume == TRUE) {                    /* See if we need to consume the flags      */
 160                     pgrp->OSFlagFlags |= flags_rdy;       /* Set ONLY the flags that we got           */
 161                 }
 162             } else {
 163                 *err  = OS_FLAG_ERR_NOT_RDY;
 164             }
 165             OS_EXIT_CRITICAL();
 166             break;
 167#endif
 168
 169        default:
 170             OS_EXIT_CRITICAL();
 171             flags_rdy = (OS_FLAGS)0;
 172             *err      = OS_FLAG_ERR_WAIT_TYPE;
 173             break;
 174    }
 175    return (flags_rdy);
 176}
 177#endif
 178
 179/*$PAGE*/
 180/*
 181*********************************************************************************************************
 182*                                           CREATE AN EVENT FLAG
 183*
 184* Description: This function is called to create an event flag group.
 185*
 186* Arguments  : flags         Contains the initial value to store in the event flag group.
 187*
 188*              err           is a pointer to an error code which will be returned to your application:
 189*                               OS_NO_ERR                if the call was successful.
 190*                               OS_ERR_CREATE_ISR        if you attempted to create an Event Flag from an
 191*                                                        ISR.
 192*                               OS_FLAG_GRP_DEPLETED     if there are no more event flag groups
 193*
 194* Returns    : A pointer to an event flag group or a NULL pointer if no more groups are available.
 195*
 196* Called from: Task ONLY
 197*********************************************************************************************************
 198*/
 199
 200OS_FLAG_GRP  *OSFlagCreate (OS_FLAGS flags, INT8U *err)
 201{
 202    OS_FLAG_GRP *pgrp;
 203#if OS_CRITICAL_METHOD == 3                         /* Allocate storage for CPU status register        */
 204    OS_CPU_SR    cpu_sr = 0;
 205#endif
 206
 207
 208
 209#if OS_ARG_CHK_EN > 0
 210    if (err == (INT8U *)0) {                        /* Validate 'err'                                  */
 211        return ((OS_FLAG_GRP *)0);
 212    }
 213#endif
 214    if (OSIntNesting > 0) {                         /* See if called from ISR ...                      */
 215        *err = OS_ERR_CREATE_ISR;                   /* ... can't CREATE from an ISR                    */
 216        return ((OS_FLAG_GRP *)0);
 217    }
 218    OS_ENTER_CRITICAL();
 219    pgrp = OSFlagFreeList;                          /* Get next free event flag                        */
 220    if (pgrp != (OS_FLAG_GRP *)0) {                 /* See if we have event flag groups available      */
 221                                                    /* Adjust free list                                */
 222        OSFlagFreeList       = (OS_FLAG_GRP *)OSFlagFreeList->OSFlagWaitList;
 223        pgrp->OSFlagType     = OS_EVENT_TYPE_FLAG;  /* Set to event flag group type                    */
 224        pgrp->OSFlagFlags    = flags;               /* Set to desired initial value                    */
 225        pgrp->OSFlagWaitList = (void *)0;           /* Clear list of tasks waiting on flags            */
 226#if OS_FLAG_NAME_SIZE > 1
 227        pgrp->OSFlagName[0]  = '?';
 228        pgrp->OSFlagName[1]  = OS_ASCII_NUL;
 229#endif
 230        OS_EXIT_CRITICAL();
 231        *err                 = OS_NO_ERR;
 232    } else {
 233        OS_EXIT_CRITICAL();
 234        *err                 = OS_FLAG_GRP_DEPLETED;
 235    }
 236    return (pgrp);                                  /* Return pointer to event flag group              */
 237}
 238
 239/*$PAGE*/
 240/*
 241*********************************************************************************************************
 242*                                     DELETE AN EVENT FLAG GROUP
 243*
 244* Description: This function deletes an event flag group and readies all tasks pending on the event flag
 245*              group.
 246*
 247* Arguments  : pgrp          is a pointer to the desired event flag group.
 248*
 249*              opt           determines delete options as follows:
 250*                            opt == OS_DEL_NO_PEND   Deletes the event flag group ONLY if no task pending
 251*                            opt == OS_DEL_ALWAYS    Deletes the event flag group even if tasks are
 252*                                                    waiting.  In this case, all the tasks pending will be
 253*                                                    readied.
 254*
 255*              err           is a pointer to an error code that can contain one of the following values:
 256*                            OS_NO_ERR               The call was successful and the event flag group was
 257*                                                    deleted
 258*                            OS_ERR_DEL_ISR          If you attempted to delete the event flag group from
 259*                                                    an ISR
 260*                            OS_FLAG_INVALID_PGRP    If 'pgrp' is a NULL pointer.
 261*                            OS_ERR_EVENT_TYPE       If you didn't pass a pointer to an event flag group
 262*                            OS_ERR_INVALID_OPT      An invalid option was specified
 263*                            OS_ERR_TASK_WAITING     One or more tasks were waiting on the event flag
 264*                                                    group.
 265*
 266* Returns    : pgrp          upon error
 267*              (OS_EVENT *)0 if the event flag group was successfully deleted.
 268*
 269* Note(s)    : 1) This function must be used with care.  Tasks that would normally expect the presence of
 270*                 the event flag group MUST check the return code of OSFlagAccept() and OSFlagPend().
 271*              2) This call can potentially disable interrupts for a long time.  The interrupt disable
 272*                 time is directly proportional to the number of tasks waiting on the event flag group.
 273*********************************************************************************************************
 274*/
 275
 276#if OS_FLAG_DEL_EN > 0
 277OS_FLAG_GRP  *OSFlagDel (OS_FLAG_GRP *pgrp, INT8U opt, INT8U *err)
 278{
 279    BOOLEAN       tasks_waiting;
 280    OS_FLAG_NODE *pnode;
 281    OS_FLAG_GRP  *pgrp_return;
 282#if OS_CRITICAL_METHOD == 3                                /* Allocate storage for CPU status register */
 283    OS_CPU_SR     cpu_sr = 0;
 284#endif
 285
 286
 287
 288#if OS_ARG_CHK_EN > 0
 289    if (err == (INT8U *)0) {                               /* Validate 'err'                           */
 290        return (pgrp);
 291    }
 292    if (pgrp == (OS_FLAG_GRP *)0) {                        /* Validate 'pgrp'                          */
 293        *err = OS_FLAG_INVALID_PGRP;
 294        return (pgrp);
 295    }
 296#endif
 297    if (OSIntNesting > 0) {                                /* See if called from ISR ...               */
 298        *err = OS_ERR_DEL_ISR;                             /* ... can't DELETE from an ISR             */
 299        return (pgrp);
 300    }
 301    if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG) {          /* Validate event group type                */
 302        *err = OS_ERR_EVENT_TYPE;
 303        return (pgrp);
 304    }
 305    OS_ENTER_CRITICAL();
 306    if (pgrp->OSFlagWaitList != (void *)0) {               /* See if any tasks waiting on event flags  */
 307        tasks_waiting = TRUE;                              /* Yes                                      */
 308    } else {
 309        tasks_waiting = FALSE;                             /* No                                       */
 310    }
 311    switch (opt) {
 312        case OS_DEL_NO_PEND:                               /* Delete group if no task waiting          */
 313             if (tasks_waiting == FALSE) {
 314#if OS_FLAG_NAME_SIZE > 1
 315                 pgrp->OSFlagName[0]  = '?';               /* Unknown name                             */
 316                 pgrp->OSFlagName[1]  = OS_ASCII_NUL;
 317#endif
 318                 pgrp->OSFlagType     = OS_EVENT_TYPE_UNUSED;
 319                 pgrp->OSFlagWaitList = (void *)OSFlagFreeList; /* Return group to free list           */
 320                 pgrp->OSFlagFlags    = (OS_FLAGS)0;
 321                 OSFlagFreeList       = pgrp;
 322                 OS_EXIT_CRITICAL();
 323                 *err                 = OS_NO_ERR;
 324                 pgrp_return          = (OS_FLAG_GRP *)0;  /* Event Flag Group has been deleted        */
 325             } else {
 326                 OS_EXIT_CRITICAL();
 327                 *err                 = OS_ERR_TASK_WAITING;
 328                 pgrp_return          = pgrp;
 329             }
 330             break;
 331
 332        case OS_DEL_ALWAYS:                                /* Always delete the event flag group       */
 333             pnode = (OS_FLAG_NODE *)pgrp->OSFlagWaitList;
 334             while (pnode != (OS_FLAG_NODE *)0) {          /* Ready ALL tasks waiting for flags        */
 335                 (void)OS_FlagTaskRdy(pnode, (OS_FLAGS)0);
 336                 pnode = (OS_FLAG_NODE *)pnode->OSFlagNodeNext;
 337             }
 338#if OS_FLAG_NAME_SIZE > 1
 339             pgrp->OSFlagName[0]  = '?';                   /* Unknown name                             */
 340             pgrp->OSFlagName[1]  = OS_ASCII_NUL;
 341#endif
 342             pgrp->OSFlagType     = OS_EVENT_TYPE_UNUSED;
 343             pgrp->OSFlagWaitList = (void *)OSFlagFreeList;/* Return group to free list                */
 344             pgrp->OSFlagFlags    = (OS_FLAGS)0;
 345             OSFlagFreeList       = pgrp;
 346             OS_EXIT_CRITICAL();
 347             if (tasks_waiting == TRUE) {                  /* Reschedule only if task(s) were waiting  */
 348                 OS_Sched();                               /* Find highest priority task ready to run  */
 349             }
 350             *err = OS_NO_ERR;
 351             pgrp_return          = (OS_FLAG_GRP *)0;      /* Event Flag Group has been deleted        */
 352             break;
 353
 354        default:
 355             OS_EXIT_CRITICAL();
 356             *err                 = OS_ERR_INVALID_OPT;
 357             pgrp_return          = pgrp;
 358             break;
 359    }
 360    return (pgrp_return);
 361}
 362#endif
 363/*$PAGE*/
 364/*
 365*********************************************************************************************************
 366*                                 GET THE NAME OF AN EVENT FLAG GROUP
 367*
 368* Description: This function is used to obtain the name assigned to an event flag group
 369*
 370* Arguments  : pgrp      is a pointer to the event flag group.
 371*
 372*              pname     is a pointer to an ASCII string that will receive the name of the event flag
 373*                        group.  The string must be able to hold at least OS_FLAG_NAME_SIZE characters.
 374*
 375*              err       is a pointer to an error code that can contain one of the following values:
 376*
 377*                        OS_NO_ERR                  if the requested task is resumed
 378*                        OS_ERR_EVENT_TYPE          if 'pevent' is not pointing to an event flag group
 379*                        OS_ERR_PNAME_NULL          You passed a NULL pointer for 'pname'
 380*                        OS_FLAG_INVALID_PGRP       if you passed a NULL pointer for 'pgrp'
 381*
 382* Returns    : The length of the string or 0 if the 'pgrp' is a NULL pointer.
 383*********************************************************************************************************
 384*/
 385
 386#if OS_FLAG_NAME_SIZE > 1
 387INT8U  OSFlagNameGet (OS_FLAG_GRP *pgrp, INT8U *pname, INT8U *err)
 388{
 389    INT8U      len;
 390#if OS_CRITICAL_METHOD == 3                      /* Allocate storage for CPU status register           */
 391    OS_CPU_SR  cpu_sr = 0;
 392#endif
 393
 394
 395
 396    OS_ENTER_CRITICAL();
 397#if OS_ARG_CHK_EN > 0
 398    if (err == (INT8U *)0) {                     /* Validate 'err'                                     */
 399        OS_EXIT_CRITICAL();
 400        return (0);
 401    }
 402    if (pgrp == (OS_FLAG_GRP *)0) {              /* Is 'pgrp' a NULL pointer?                          */
 403        OS_EXIT_CRITICAL();                      /* Yes                                                */
 404        *err = OS_FLAG_INVALID_PGRP;
 405        return (0);
 406    }
 407    if (pname == (INT8U *)0) {                    /* Is 'pname' a NULL pointer?                         */
 408        OS_EXIT_CRITICAL();                      /* Yes                                                */
 409        *err = OS_ERR_PNAME_NULL;
 410        return (0);
 411    }
 412#endif
 413    if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG) {
 414        OS_EXIT_CRITICAL();
 415        *err = OS_ERR_EVENT_TYPE;
 416        return (0);
 417    }
 418    len  = OS_StrCopy(pname, pgrp->OSFlagName);  /* Copy name from OS_FLAG_GRP                         */
 419    OS_EXIT_CRITICAL();
 420    *err = OS_NO_ERR;
 421    return (len);
 422}
 423#endif
 424
 425/*$PAGE*/
 426/*
 427*********************************************************************************************************
 428*                                 ASSIGN A NAME TO AN EVENT FLAG GROUP
 429*
 430* Description: This function assigns a name to an event flag group.
 431*
 432* Arguments  : pgrp      is a pointer to the event flag group.
 433*
 434*              pname     is a pointer to an ASCII string that will be used as the name of the event flag
 435*                        group.  The string must be able to hold at least OS_FLAG_NAME_SIZE characters.
 436*
 437*              err       is a pointer to an error code that can contain one of the following values:
 438*
 439*                        OS_NO_ERR                  if the requested task is resumed
 440*                        OS_ERR_EVENT_TYPE          if 'pevent' is not pointing to an event flag group
 441*                        OS_ERR_PNAME_NULL          You passed a NULL pointer for 'pname'
 442*                        OS_FLAG_INVALID_PGRP       if you passed a NULL pointer for 'pgrp'
 443*
 444* Returns    : None
 445*********************************************************************************************************
 446*/
 447
 448#if OS_FLAG_NAME_SIZE > 1
 449void  OSFlagNameSet (OS_FLAG_GRP *pgrp, INT8U *pname, INT8U *err)
 450{
 451    INT8U      len;
 452#if OS_CRITICAL_METHOD == 3                      /* Allocate storage for CPU status register           */
 453    OS_CPU_SR  cpu_sr = 0;
 454#endif
 455
 456
 457
 458    OS_ENTER_CRITICAL();
 459#if OS_ARG_CHK_EN > 0
 460    if (err == (INT8U *)0) {                     /* Validate 'err'                                     */
 461        OS_EXIT_CRITICAL();
 462        return;
 463    }
 464    if (pgrp == (OS_FLAG_GRP *)0) {              /* Is 'pgrp' a NULL pointer?                          */
 465        OS_EXIT_CRITICAL();                      /* Yes                                                */
 466        *err = OS_FLAG_INVALID_PGRP;
 467        return;
 468    }
 469    if (pname == (INT8U *)0) {                    /* Is 'pname' a NULL pointer?                         */
 470        OS_EXIT_CRITICAL();                      /* Yes                                                */
 471        *err = OS_ERR_PNAME_NULL;
 472        return;
 473    }
 474#endif
 475    if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG) {
 476        OS_EXIT_CRITICAL();
 477        *err = OS_ERR_EVENT_TYPE;
 478        return;
 479    }
 480    len = OS_StrLen(pname);                      /* Can we fit the string in the storage area?         */
 481    if (len > (OS_FLAG_NAME_SIZE - 1)) {         /* No                                                 */
 482        OS_EXIT_CRITICAL();
 483        *err = OS_ERR_FLAG_NAME_TOO_LONG;
 484        return;
 485    }
 486    (void)OS_StrCopy(pgrp->OSFlagName, pname);   /* Yes, copy name from OS_FLAG_GRP                    */
 487    OS_EXIT_CRITICAL();
 488    *err = OS_NO_ERR;
 489    return;
 490}
 491#endif
 492
 493/*$PAGE*/
 494/*
 495*********************************************************************************************************
 496*                                        WAIT ON AN EVENT FLAG GROUP
 497*
 498* Description: This function is called to wait for a combination of bits to be set in an event flag
 499*              group.  Your application can wait for ANY bit to be set or ALL bits to be set.
 500*
 501* Arguments  : pgrp          is a pointer to the desired event flag group.
 502*
 503*              flags         Is a bit pattern indicating which bit(s) (i.e. flags) you wish to wait for.
 504*                            The bits you want are specified by setting the corresponding bits in
 505*                            'flags'.  e.g. if your application wants to wait for bits 0 and 1 then
 506*                            'flags' would contain 0x03.
 507*
 508*              wait_type     specifies whether you want ALL bits to be set or ANY of the bits to be set.
 509*                            You can specify the following argument:
 510*
 511*                            OS_FLAG_WAIT_CLR_ALL   You will wait for ALL bits in 'mask' to be clear (0)
 512*                            OS_FLAG_WAIT_SET_ALL   You will wait for ALL bits in 'mask' to be set   (1)
 513*                            OS_FLAG_WAIT_CLR_ANY   You will wait for ANY bit  in 'mask' to be clear (0)
 514*                            OS_FLAG_WAIT_SET_ANY   You will wait for ANY bit  in 'mask' to be set   (1)
 515*
 516*                            NOTE: Add OS_FLAG_CONSUME if you want the event flag to be 'consumed' by
 517*                                  the call.  Example, to wait for any flag in a group AND then clear
 518*                                  the flags that are present, set 'wait_type' to:
 519*
 520*                                  OS_FLAG_WAIT_SET_ANY + OS_FLAG_CONSUME
 521*
 522*              timeout       is an optional timeout (in clock ticks) that your task will wait for the
 523*                            desired bit combination.  If you specify 0, however, your task will wait
 524*                            forever at the specified event flag group or, until a message arrives.
 525*
 526*              err           is a pointer to an error code and can be:
 527*                            OS_NO_ERR              The desired bits have been set within the specified
 528*                                                   'timeout'.
 529*                            OS_ERR_PEND_ISR        If you tried to PEND from an ISR
 530*                            OS_FLAG_INVALID_PGRP   If 'pgrp' is a NULL pointer.
 531*                            OS_ERR_EVENT_TYPE      You are not pointing to an event flag group
 532*                            OS_TIMEOUT             The bit(s) have not been set in the specified
 533*                                                   'timeout'.
 534*                            OS_FLAG_ERR_WAIT_TYPE  You didn't specify a proper 'wait_type' argument.
 535*
 536* Returns    : The flags in the event flag group that made the task ready or, 0 if a timeout or an error
 537*              occurred.
 538*
 539* Called from: Task ONLY
 540*
 541* Note(s)    : 1) IMPORTANT, the behavior of this function has changed from PREVIOUS versions.  The
 542*                 function NOW returns the flags that were ready INSTEAD of the current state of the
 543*                 event flags.
 544*********************************************************************************************************
 545*/
 546
 547OS_FLAGS  OSFlagPend (OS_FLAG_GRP *pgrp, OS_FLAGS flags, INT8U wait_type, INT16U timeout, INT8U *err)
 548{
 549    OS_FLAG_NODE  node;
 550    OS_FLAGS      flags_rdy;
 551    INT8U         result;
 552    BOOLEAN       consume;
 553#if OS_CRITICAL_METHOD == 3                                /* Allocate storage for CPU status register */
 554    OS_CPU_SR     cpu_sr = 0;
 555#endif
 556
 557
 558
 559#if OS_ARG_CHK_EN > 0
 560    if (err == (INT8U *)0) {                               /* Validate 'err'                           */
 561        return ((OS_FLAGS)0);
 562    }
 563    if (pgrp == (OS_FLAG_GRP *)0) {                        /* Validate 'pgrp'                          */
 564        *err = OS_FLAG_INVALID_PGRP;
 565        return ((OS_FLAGS)0);
 566    }
 567#endif
 568    if (OSIntNesting > 0) {                                /* See if called from ISR ...               */
 569        *err = OS_ERR_PEND_ISR;                            /* ... can't PEND from an ISR               */
 570        return ((OS_FLAGS)0);
 571    }
 572    if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG) {          /* Validate event block type                */
 573        *err = OS_ERR_EVENT_TYPE;
 574        return ((OS_FLAGS)0);
 575    }
 576    result = wait_type & OS_FLAG_CONSUME;
 577    if (result != (INT8U)0) {                             /* See if we need to consume the flags      */
 578        wait_type &= ~OS_FLAG_CONSUME;
 579        consume    = TRUE;
 580    } else {
 581        consume    = FALSE;
 582    }
 583/*$PAGE*/
 584    OS_ENTER_CRITICAL();
 585    switch (wait_type) {
 586        case OS_FLAG_WAIT_SET_ALL:                         /* See if all required flags are set        */
 587             flags_rdy = pgrp->OSFlagFlags & flags;        /* Extract only the bits we want            */
 588             if (flags_rdy == flags) {                     /* Must match ALL the bits that we want     */
 589                 if (consume == TRUE) {                    /* See if we need to consume the flags      */
 590                     pgrp->OSFlagFlags &= ~flags_rdy;      /* Clear ONLY the flags that we wanted      */
 591                 }
 592                 OSTCBCur->OSTCBFlagsRdy = flags_rdy;      /* Save flags that were ready               */
 593                 OS_EXIT_CRITICAL();                       /* Yes, condition met, return to caller     */
 594                 *err                    = OS_NO_ERR;
 595                 return (flags_rdy);
 596             } else {                                      /* Block task until events occur or timeout */
 597                 OS_FlagBlock(pgrp, &node, flags, wait_type, timeout);
 598                 OS_EXIT_CRITICAL();
 599             }
 600             break;
 601
 602        case OS_FLAG_WAIT_SET_ANY:
 603             flags_rdy = pgrp->OSFlagFlags & flags;        /* Extract only the bits we want            */
 604             if (flags_rdy != (OS_FLAGS)0) {               /* See if any flag set                      */
 605                 if (consume == TRUE) {                    /* See if we need to consume the flags      */
 606                     pgrp->OSFlagFlags &= ~flags_rdy;      /* Clear ONLY the flags that we got         */
 607                 }
 608                 OSTCBCur->OSTCBFlagsRdy = flags_rdy;      /* Save flags that were ready               */
 609                 OS_EXIT_CRITICAL();                       /* Yes, condition met, return to caller     */
 610                 *err                    = OS_NO_ERR;
 611                 return (flags_rdy);
 612             } else {                                      /* Block task until events occur or timeout */
 613                 OS_FlagBlock(pgrp, &node, flags, wait_type, timeout);
 614                 OS_EXIT_CRITICAL();
 615             }
 616             break;
 617
 618#if OS_FLAG_WAIT_CLR_EN > 0
 619        case OS_FLAG_WAIT_CLR_ALL:                         /* See if all required flags are cleared    */
 620             flags_rdy = (OS_FLAGS)~pgrp->OSFlagFlags & flags; /* Extract only the bits we want        */
 621             if (flags_rdy == flags) {                     /* Must match ALL the bits that we want     */
 622                 if (consume == TRUE) {                    /* See if we need to consume the flags      */
 623                     pgrp->OSFlagFlags |= flags_rdy;       /* Set ONLY the flags that we wanted        */
 624                 }
 625                 OSTCBCur->OSTCBFlagsRdy = flags_rdy;      /* Save flags that were ready               */
 626                 OS_EXIT_CRITICAL();                       /* Yes, condition met, return to caller     */
 627                 *err                    = OS_NO_ERR;
 628                 return (flags_rdy);
 629             } else {                                      /* Block task until events occur or timeout */
 630                 OS_FlagBlock(pgrp, &node, flags, wait_type, timeout);
 631                 OS_EXIT_CRITICAL();
 632             }
 633             break;
 634
 635        case OS_FLAG_WAIT_CLR_ANY:
 636             flags_rdy = (OS_FLAGS)~pgrp->OSFlagFlags & flags; /* Extract only the bits we want        */
 637             if (flags_rdy != (OS_FLAGS)0) {               /* See if any flag cleared                  */
 638                 if (consume == TRUE) {                    /* See if we need to consume the flags      */
 639                     pgrp->OSFlagFlags |= flags_rdy;       /* Set ONLY the flags that we got           */
 640                 }
 641                 OSTCBCur->OSTCBFlagsRdy = flags_rdy;      /* Save flags that were ready               */
 642                 OS_EXIT_CRITICAL();                       /* Yes, condition met, return to caller     */
 643                 *err                    = OS_NO_ERR;
 644                 return (flags_rdy);
 645             } else {                                      /* Block task until events occur or timeout */
 646                 OS_FlagBlock(pgrp, &node, flags, wait_type, timeout);
 647                 OS_EXIT_CRITICAL();
 648             }
 649             break;
 650#endif
 651
 652        default:
 653             OS_EXIT_CRITICAL();
 654             flags_rdy = (OS_FLAGS)0;
 655             *err      = OS_FLAG_ERR_WAIT_TYPE;
 656             return (flags_rdy);
 657    }
 658    OS_Sched();                                            /* Find next HPT ready to run               */
 659    OS_ENTER_CRITICAL();
 660    if (OSTCBCur->OSTCBPendTO == TRUE) {                   /* Have we timed-out?                       */
 661        OSTCBCur->OSTCBPendTO = FALSE;
 662        OS_FlagUnlink(&node);
 663        OSTCBCur->OSTCBStat   = OS_STAT_RDY;               /* Yes, make task ready-to-run              */
 664        OS_EXIT_CRITICAL();
 665        flags_rdy             = (OS_FLAGS)0;
 666        *err                  = OS_TIMEOUT;                /* Indicate that we timed-out waiting       */
 667        return (flags_rdy);
 668    }
 669    flags_rdy = OSTCBCur->OSTCBFlagsRdy;
 670    if (consume == TRUE) {                                 /* See if we need to consume the flags      */
 671        switch (wait_type) {
 672            case OS_FLAG_WAIT_SET_ALL:
 673            case OS_FLAG_WAIT_SET_ANY:                     /* Clear ONLY the flags we got              */
 674                 pgrp->OSFlagFlags &= ~flags_rdy;
 675                 break;
 676
 677#if OS_FLAG_WAIT_CLR_EN > 0
 678            case OS_FLAG_WAIT_CLR_ALL:
 679            case OS_FLAG_WAIT_CLR_ANY:                     /* Set   ONLY the flags we got              */
 680                 pgrp->OSFlagFlags |=  flags_rdy;
 681                 break;
 682#endif
 683            default:
 684                 OS_EXIT_CRITICAL();
 685                 *err = OS_FLAG_ERR_WAIT_TYPE;
 686                 return ((OS_FLAGS)0);
 687        }
 688    }
 689    OS_EXIT_CRITICAL();
 690    *err = OS_NO_ERR;                                      /* Event(s) must have occurred              */
 691    return (flags_rdy);
 692}
 693/*$PAGE*/
 694/*
 695*********************************************************************************************************
 696*                               GET FLAGS WHO CAUSED TASK TO BECOME READY
 697*
 698* Description: This function is called to obtain the flags that caused the task to become ready to run.
 699*              In other words, this function allows you to tell "Who done it!".
 700*
 701* Arguments  : None
 702*
 703* Returns    : The flags that caused the task to be ready.
 704*
 705* Called from: Task ONLY
 706*********************************************************************************************************
 707*/
 708
 709OS_FLAGS  OSFlagPendGetFlagsRdy (void)
 710{
 711    OS_FLAGS      flags;
 712#if OS_CRITICAL_METHOD == 3                                /* Allocate storage for CPU status register */
 713    OS_CPU_SR     cpu_sr = 0;
 714#endif
 715
 716
 717
 718    OS_ENTER_CRITICAL();
 719    flags = OSTCBCur->OSTCBFlagsRdy;
 720    OS_EXIT_CRITICAL();
 721    return (flags);
 722}
 723
 724/*$PAGE*/
 725/*
 726*********************************************************************************************************
 727*                                         POST EVENT FLAG BIT(S)
 728*
 729* Description: This function is called to set or clear some bits in an event flag group.  The bits to
 730*              set or clear are specified by a 'bit mask'.
 731*
 732* Arguments  : pgrp          is a pointer to the desired event flag group.
 733*
 734*              flags         If 'opt' (see below) is OS_FLAG_SET, each bit that is set in 'flags' will
 735*                            set the corresponding bit in the event flag group.  e.g. to set bits 0, 4
 736*                            and 5 you would set 'flags' to:
 737*
 738*                                0x31     (note, bit 0 is least significant bit)
 739*
 740*                            If 'opt' (see below) is OS_FLAG_CLR, each bit that is set in 'flags' will
 741*                            CLEAR the corresponding bit in the event flag group.  e.g. to clear bits 0,
 742*                            4 and 5 you would specify 'flags' as:
 743*
 744*                                0x31     (note, bit 0 is least significant bit)
 745*
 746*              opt           indicates whether the flags will be:
 747*                                set     (OS_FLAG_SET) or
 748*                                cleared (OS_FLAG_CLR)
 749*
 750*              err           is a pointer to an error code and can be:
 751*                            OS_NO_ERR              The call was successfull
 752*                            OS_FLAG_INVALID_PGRP   You passed a NULL pointer
 753*                            OS_ERR_EVENT_TYPE      You are not pointing to an event flag group
 754*                            OS_FLAG_INVALID_OPT    You specified an invalid option
 755*
 756* Returns    : the new value of the event flags bits that are still set.
 757*
 758* Called From: Task or ISR
 759*
 760* WARNING(s) : 1) The execution time of this function depends on the number of tasks waiting on the event
 761*                 flag group.
 762*              2) The amount of time interrupts are DISABLED depends on the number of tasks waiting on
 763*                 the event flag group.
 764*********************************************************************************************************
 765*/
 766OS_FLAGS  OSFlagPost (OS_FLAG_GRP *pgrp, OS_FLAGS flags, INT8U opt, INT8U *err)
 767{
 768    OS_FLAG_NODE *pnode;
 769    BOOLEAN       sched;
 770    OS_FLAGS      flags_cur;
 771    OS_FLAGS      flags_rdy;
 772    BOOLEAN       rdy;
 773#if OS_CRITICAL_METHOD == 3                          /* Allocate storage for CPU status register       */
 774    OS_CPU_SR     cpu_sr = 0;
 775#endif
 776
 777
 778
 779#if OS_ARG_CHK_EN > 0
 780    if (err == (INT8U *)0) {                         /* Validate 'err'                                 */
 781        return ((OS_FLAGS)0);
 782    }
 783    if (pgrp == (OS_FLAG_GRP *)0) {                  /* Validate 'pgrp'                                */
 784        *err = OS_FLAG_INVALID_PGRP;
 785        return ((OS_FLAGS)0);
 786    }
 787#endif
 788    if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG) {    /* Make sure we are pointing to an event flag grp */
 789        *err = OS_ERR_EVENT_TYPE;
 790        return ((OS_FLAGS)0);
 791    }
 792/*$PAGE*/
 793    OS_ENTER_CRITICAL();
 794    switch (opt) {
 795        case OS_FLAG_CLR:
 796             pgrp->OSFlagFlags &= ~flags;            /* Clear the flags specified in the group         */
 797             break;
 798
 799        case OS_FLAG_SET:
 800             pgrp->OSFlagFlags |=  flags;            /* Set   the flags specified in the group         */
 801             break;
 802
 803        default:
 804             OS_EXIT_CRITICAL();                     /* INVALID option                                 */
 805             *err = OS_FLAG_INVALID_OPT;
 806             return ((OS_FLAGS)0);
 807    }
 808    sched = FALSE;                                   /* Indicate that we don't need rescheduling       */
 809    pnode = (OS_FLAG_NODE *)pgrp->OSFlagWaitList;
 810    while (pnode != (OS_FLAG_NODE *)0) {             /* Go through all tasks waiting on event flag(s)  */
 811        switch (pnode->OSFlagNodeWaitType) {
 812            case OS_FLAG_WAIT_SET_ALL:               /* See if all req. flags are set for current node */
 813                 flags_rdy = pgrp->OSFlagFlags & pnode->OSFlagNodeFlags;
 814                 if (flags_rdy == pnode->OSFlagNodeFlags) {
 815                     rdy = OS_FlagTaskRdy(pnode, flags_rdy);  /* Make task RTR, event(s) Rx'd          */
 816                     if (rdy == TRUE) {
 817                         sched = TRUE;                        /* When done we will reschedule          */
 818                     }
 819                 }
 820                 break;
 821
 822            case OS_FLAG_WAIT_SET_ANY:               /* See if any flag set                            */
 823                 flags_rdy = pgrp->OSFlagFlags & pnode->OSFlagNodeFlags;
 824                 if (flags_rdy != (OS_FLAGS)0) {
 825                     rdy = OS_FlagTaskRdy(pnode, flags_rdy);  /* Make task RTR, event(s) Rx'd          */
 826                     if (rdy == TRUE) {
 827                         sched = TRUE;                        /* When done we will reschedule          */
 828                     }
 829                 }
 830                 break;
 831
 832#if OS_FLAG_WAIT_CLR_EN > 0
 833            case OS_FLAG_WAIT_CLR_ALL:               /* See if all req. flags are set for current node */
 834                 flags_rdy = (OS_FLAGS)~pgrp->OSFlagFlags & pnode->OSFlagNodeFlags;
 835                 if (flags_rdy == pnode->OSFlagNodeFlags) {
 836                     rdy = OS_FlagTaskRdy(pnode, flags_rdy);  /* Make task RTR, event(s) Rx'd          */
 837                     if (rdy == TRUE) {
 838                         sched = TRUE;                        /* When done we will reschedule          */
 839                     }
 840                 }
 841                 break;
 842
 843            case OS_FLAG_WAIT_CLR_ANY:               /* See if any flag set                            */
 844                 flags_rdy = (OS_FLAGS)~pgrp->OSFlagFlags & pnode->OSFlagNodeFlags;
 845                 if (flags_rdy != (OS_FLAGS)0) {
 846                     rdy = OS_FlagTaskRdy(pnode, flags_rdy);  /* Make task RTR, event(s) Rx'd          */
 847                     if (rdy == TRUE) {
 848                         sched = TRUE;                        /* When done we will reschedule          */
 849                     }
 850                 }
 851                 break;
 852#endif
 853            default:
 854                 OS_EXIT_CRITICAL();
 855                 *err = OS_FLAG_ERR_WAIT_TYPE;
 856                 return ((OS_FLAGS)0);
 857        }
 858        pnode = (OS_FLAG_NODE *)pnode->OSFlagNodeNext; /* Point to next task waiting for event flag(s) */
 859    }
 860    OS_EXIT_CRITICAL();
 861    if (sched == TRUE) {
 862        OS_Sched();
 863    }
 864    OS_ENTER_CRITICAL();
 865    flags_cur = pgrp->OSFlagFlags;
 866    OS_EXIT_CRITICAL();
 867    *err      = OS_NO_ERR;
 868    return (flags_cur);
 869}
 870/*$PAGE*/
 871/*
 872*********************************************************************************************************
 873*                                           QUERY EVENT FLAG
 874*
 875* Description: This function is used to check the value of the event flag group.
 876*
 877* Arguments  : pgrp         is a pointer to the desired event flag group.
 878*
 879*              err           is a pointer to an error code returned to the called:
 880*                            OS_NO_ERR              The call was successfull
 881*                            OS_FLAG_INVALID_PGRP   You passed a NULL pointer
 882*                            OS_ERR_EVENT_TYPE      You are not pointing to an event flag group
 883*
 884* Returns    : The current value of the event flag group.
 885*
 886* Called From: Task or ISR
 887*********************************************************************************************************
 888*/
 889
 890#if OS_FLAG_QUERY_EN > 0
 891OS_FLAGS  OSFlagQuery (OS_FLAG_GRP *pgrp, INT8U *err)
 892{
 893    OS_FLAGS   flags;
 894#if OS_CRITICAL_METHOD == 3                       /* Allocate storage for CPU status register          */
 895    OS_CPU_SR  cpu_sr = 0;
 896#endif
 897
 898
 899
 900#if OS_ARG_CHK_EN > 0
 901    if (err == (INT8U *)0) {                      /* Validate 'err'                                    */
 902        return ((OS_FLAGS)0);
 903    }
 904    if (pgrp == (OS_FLAG_GRP *)0) {               /* Validate 'pgrp'                                   */
 905        *err = OS_FLAG_INVALID_PGRP;
 906        return ((OS_FLAGS)0);
 907    }
 908#endif
 909    if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG) { /* Validate event block type                         */
 910        *err = OS_ERR_EVENT_TYPE;
 911        return ((OS_FLAGS)0);
 912    }
 913    OS_ENTER_CRITICAL();
 914    flags = pgrp->OSFlagFlags;
 915    OS_EXIT_CRITICAL();
 916    *err = OS_NO_ERR;
 917    return (flags);                               /* Return the current value of the event flags       */
 918}
 919#endif
 920
 921/*$PAGE*/
 922/*
 923*********************************************************************************************************
 924*                         SUSPEND TASK UNTIL EVENT FLAG(s) RECEIVED OR TIMEOUT OCCURS
 925*
 926* Description: This function is internal to uC/OS-II and is used to put a task to sleep until the desired
 927*              event flag bit(s) are set.
 928*
 929* Arguments  : pgrp          is a pointer to the desired event flag group.
 930*
 931*              pnode         is a pointer to a structure which contains data about the task waiting for
 932*                            event flag bit(s) to be set.
 933*
 934*              flags         Is a bit pattern indicating which bit(s) (i.e. flags) you wish to check.
 935*                            The bits you want are specified by setting the corresponding bits in
 936*                            'flags'.  e.g. if your application wants to wait for bits 0 and 1 then
 937*                            'flags' would contain 0x03.
 938*
 939*              wait_type     specifies whether you want ALL bits to be set/cleared or ANY of the bits
 940*                            to be set/cleared.
 941*                            You can specify the following argument:
 942*
 943*                            OS_FLAG_WAIT_CLR_ALL   You will check ALL bits in 'mask' to be clear (0)
 944*                            OS_FLAG_WAIT_CLR_ANY   You will check ANY bit  in 'mask' to be clear (0)
 945*                            OS_FLAG_WAIT_SET_ALL   You will check ALL bits in 'mask' to be set   (1)
 946*                            OS_FLAG_WAIT_SET_ANY   You will check ANY bit  in 'mask' to be set   (1)
 947*
 948*              timeout       is the desired amount of time that the task will wait for the event flag
 949*                            bit(s) to be set.
 950*
 951* Returns    : none
 952*
 953* Called by  : OSFlagPend()  OS_FLAG.C
 954*
 955* Note(s)    : This function is INTERNAL to uC/OS-II and your application should not call it.
 956*********************************************************************************************************
 957*/
 958
 959static  void  OS_FlagBlock (OS_FLAG_GRP *pgrp, OS_FLAG_NODE *pnode, OS_FLAGS flags, INT8U wait_type, INT16U timeout)
 960{
 961    OS_FLAG_NODE  *pnode_next;
 962    INT8U          y;
 963
 964
 965    OSTCBCur->OSTCBStat      |= OS_STAT_FLAG;
 966    OSTCBCur->OSTCBPendTO     = FALSE;
 967    OSTCBCur->OSTCBDly        = timeout;              /* Store timeout in task's TCB                   */
 968    OSTCBCur->OSTCBEventPtr   = (OS_EVENT *)0;
 969#if OS_TASK_DEL_EN > 0
 970    OSTCBCur->OSTCBFlagNode   = pnode;                /* TCB to link to node                           */
 971#endif
 972    pnode->OSFlagNodeFlags    = flags;                /* Save the flags that we need to wait for       */
 973    pnode->OSFlagNodeWaitType = wait_type;            /* Save the type of wait we are doing            */
 974    pnode->OSFlagNodeTCB      = (void *)OSTCBCur;     /* Link to task's TCB                            */
 975    pnode->OSFlagNodeNext     = pgrp->OSFlagWaitList; /* Add node at beginning of event flag wait list */
 976    pnode->OSFlagNodePrev     = (void *)0;
 977    pnode->OSFlagNodeFlagGrp  = (void *)pgrp;         /* Link to Event Flag Group                      */
 978    pnode_next                = (OS_FLAG_NODE *)pgrp->OSFlagWaitList;
 979    if (pnode_next != (void *)0) {                    /* Is this the first NODE to insert?             */
 980        pnode_next->OSFlagNodePrev = pnode;           /* No, link in doubly linked list                */
 981    }
 982    pgrp->OSFlagWaitList = (void *)pnode;
 983
 984    y            =  OSTCBCur->OSTCBY;                 /* Suspend current task until flag(s) received   */
 985    OSRdyTbl[y] &= ~OSTCBCur->OSTCBBitX;
 986    if (OSRdyTbl[y] == 0x00) {
 987        OSRdyGrp &= ~OSTCBCur->OSTCBBitY;
 988    }
 989}
 990
 991/*$PAGE*/
 992/*
 993*********************************************************************************************************
 994*                                    INITIALIZE THE EVENT FLAG MODULE
 995*
 996* Description: This function is called by uC/OS-II to initialize the event flag module.  Your application
 997*              MUST NOT call this function.  In other words, this function is internal to uC/OS-II.
 998*
 999* Arguments  : none
1000*
1001* Returns    : none
1002*
1003* WARNING    : You MUST NOT call this function from your code.  This is an INTERNAL function to uC/OS-II.
1004*********************************************************************************************************
1005*/
1006
1007void  OS_FlagInit (void)
1008{
1009#if OS_MAX_FLAGS == 1
1010    OSFlagFreeList                 = (OS_FLAG_GRP *)&OSFlagTbl[0];  /* Only ONE event flag group!      */
1011    OSFlagFreeList->OSFlagType     = OS_EVENT_TYPE_UNUSED;
1012    OSFlagFreeList->OSFlagWaitList = (void *)0;
1013    OSFlagFreeList->OSFlagFlags    = (OS_FLAGS)0;
1014#if OS_FLAG_NAME_SIZE > 1
1015    OSFlagFreeList->OSFlagName[0]  = '?';
1016    OSFlagFreeList->OSFlagName[1]  = OS_ASCII_NUL;
1017#endif
1018#endif
1019
1020#if OS_MAX_FLAGS >= 2
1021    INT16U       i;
1022    OS_FLAG_GRP *pgrp1;
1023    OS_FLAG_GRP *pgrp2;
1024
1025
1026    OS_MemClr((INT8U *)&OSFlagTbl[0], sizeof(OSFlagTbl));           /* Clear the flag group table      */
1027    pgrp1 = &OSFlagTbl[0];
1028    pgrp2 = &OSFlagTbl[1];
1029    for (i = 0; i < (OS_MAX_FLAGS - 1); i++) {                      /* Init. list of free EVENT FLAGS  */
1030        pgrp1->OSFlagType     = OS_EVENT_TYPE_UNUSED;
1031        pgrp1->OSFlagWaitList = (void *)pgrp2;
1032#if OS_FLAG_NAME_SIZE > 1
1033        pgrp1->OSFlagName[0]  = '?';                                /* Unknown name                    */
1034        pgrp1->OSFlagName[1]  = OS_ASCII_NUL;
1035#endif
1036        pgrp1++;
1037        pgrp2++;
1038    }
1039    pgrp1->OSFlagType     = OS_EVENT_TYPE_UNUSED;
1040    pgrp1->OSFlagWaitList = (void *)0;
1041#if OS_FLAG_NAME_SIZE > 1
1042    pgrp1->OSFlagName[0]  = '?';                    

Large files files are truncated, but you can click here to view the full file