首页 > 代码库 > CMSIS Example - Signal and Yield

CMSIS Example - Signal and Yield

  1 /*----------------------------------------------------------------------------  2  *      RL-ARM - RTX  3  *----------------------------------------------------------------------------  4  *      Name:    RTX_EX2.C  5  *      Purpose: RTX example program  6  *----------------------------------------------------------------------------  7  *  8  * Copyright (c) 1999-2009 KEIL, 2009-2012 ARM Germany GmbH  9  * All rights reserved. 10  * Redistribution and use in source and binary forms, with or without 11  * modification, are permitted provided that the following conditions are met: 12  *  - Redistributions of source code must retain the above copyright 13  *    notice, this list of conditions and the following disclaimer. 14  *  - Redistributions in binary form must reproduce the above copyright 15  *    notice, this list of conditions and the following disclaimer in the 16  *    documentation and/or other materials provided with the distribution. 17  *  - Neither the name of ARM  nor the names of its contributors may be used  18  *    to endorse or promote products derived from this software without  19  *    specific prior written permission. 20  * 21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24  * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE 25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF  27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS  28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN  29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31  * POSSIBILITY OF SUCH DAMAGE. 32  *---------------------------------------------------------------------------*/ 33  34 #include "cmsis_os.h" 35  36 volatile uint16_t counter;            /* counter for main thread             */ 37 volatile uint16_t counter1;           /* counter for thread 1                */ 38 volatile uint16_t counter2;           /* counter for thread 2                */ 39 volatile uint16_t counter3;           /* counter for thread 3                */ 40  41 /* Thread IDs */ 42 osThreadId thread1_id; 43 osThreadId thread2_id; 44 osThreadId thread3_id; 45  46 /* Forward reference */ 47 void job1 (void const *argument); 48 void job2 (void const *argument); 49 void job3 (void const *argument); 50  51 /* Thread definitions */ 52 osThreadDef(job1, osPriorityAboveNormal, 1, 0); 53 osThreadDef(job2, osPriorityNormal, 1, 0); 54 osThreadDef(job3, osPriorityNormal, 1, 0); 55  56 /*---------------------------------------------------------------------------- 57  *   Thread 1: ‘job1‘ 58  *---------------------------------------------------------------------------*/ 59 void job1 (void const *argument) {    /* higher priority to preempt job2     */ 60   while (1) {                         /* endless loop                        */ 61     counter1++;                       /* increment counter 1                 */ 62     osDelay(10);                      /* wait for timeout: 10ms              */ 63   } 64 } 65  66 /*---------------------------------------------------------------------------- 67  *   Thread 2: ‘job2‘ 68  *---------------------------------------------------------------------------*/ 69 void job2 (void const *argument) { 70   while (1)  {                        /* endless loop                        */ 71     counter2++;                       /* increment counter 2                 */ 72     if (counter2 == 0) {              /* signal overflow of counter 2        */ 73       osSignalSet(thread3_id, 0x0001);/* to thread 3                         */ 74       osThreadYield(); 75     } 76   } 77 } 78  79 /*---------------------------------------------------------------------------- 80  *   Thread 3: ‘job3‘ 81  *---------------------------------------------------------------------------*/ 82 void job3 (void const *argument) { 83   while (1) {                         /* endless loop                        */ 84     osSignalWait(0x0001, osWaitForever);  /* wait for signal event           */ 85     counter3++;                       /* process overflow from counter 2     */ 86   } 87 } 88  89 /*---------------------------------------------------------------------------- 90  *   Main Thread 91  *---------------------------------------------------------------------------*/ 92 int main (void) {                     /* program execution starts here       */ 93  94   /* Set higher priority of main thread to preempt job2                      */ 95   osThreadSetPriority(osThreadGetId(), osPriorityAboveNormal); 96  97   thread1_id = osThreadCreate(osThread(job1),NULL);  /* create thread1       */ 98   thread2_id = osThreadCreate(osThread(job2),NULL);  /* create thread2       */ 99   thread3_id = osThreadCreate(osThread(job3),NULL);  /* create thread3       */100 101   while (1) {                         /* endless loop                        */102     counter++;                        /* increment counter                   */103     osDelay(5);                       /* wait for timeout: 5ms               */104   }105 }106 107 /*----------------------------------------------------------------------------108  * end of file109  *---------------------------------------------------------------------------*/