cmsis rtos demo

Dependencies:   mbed-rtos mbed

Committer:
cnhzcy14
Date:
Sun Jan 06 14:34:08 2013 +0000
Revision:
0:9a66107976a4
Child:
1:8289e2db9449
rtos signal

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cnhzcy14 0:9a66107976a4 1 #include "mbed.h"
cnhzcy14 0:9a66107976a4 2 #include "cmsis_os.h"
cnhzcy14 0:9a66107976a4 3
cnhzcy14 0:9a66107976a4 4 #define NUM_THREADS 3
cnhzcy14 0:9a66107976a4 5 #define TCOUNT 10
cnhzcy14 0:9a66107976a4 6 #define COUNT_LIMIT 12
cnhzcy14 0:9a66107976a4 7
cnhzcy14 0:9a66107976a4 8 int count = 0;
cnhzcy14 0:9a66107976a4 9 osMutexId count_mutex;
cnhzcy14 0:9a66107976a4 10 osMutexDef(count_mutex);
cnhzcy14 0:9a66107976a4 11
cnhzcy14 0:9a66107976a4 12 struct thread_data
cnhzcy14 0:9a66107976a4 13 {
cnhzcy14 0:9a66107976a4 14 osThreadId id;
cnhzcy14 0:9a66107976a4 15 long my_id;
cnhzcy14 0:9a66107976a4 16 };
cnhzcy14 0:9a66107976a4 17
cnhzcy14 0:9a66107976a4 18 struct thread_data thread_data_array[NUM_THREADS-1];
cnhzcy14 0:9a66107976a4 19 Serial debug(USBTX, USBRX);
cnhzcy14 0:9a66107976a4 20
cnhzcy14 0:9a66107976a4 21 void inc_count(void const *threadarg)
cnhzcy14 0:9a66107976a4 22 {
cnhzcy14 0:9a66107976a4 23 int i;
cnhzcy14 0:9a66107976a4 24 long my_id;
cnhzcy14 0:9a66107976a4 25 osThreadId id;
cnhzcy14 0:9a66107976a4 26 struct thread_data *my_data;
cnhzcy14 0:9a66107976a4 27 my_data = (struct thread_data *) threadarg;
cnhzcy14 0:9a66107976a4 28 id = my_data->id;
cnhzcy14 0:9a66107976a4 29 my_id = my_data->my_id;
cnhzcy14 0:9a66107976a4 30
cnhzcy14 0:9a66107976a4 31 for (i=0; i < TCOUNT; i++) {
cnhzcy14 0:9a66107976a4 32 osMutexWait(count_mutex, osWaitForever);
cnhzcy14 0:9a66107976a4 33 count++;
cnhzcy14 0:9a66107976a4 34
cnhzcy14 0:9a66107976a4 35 /*
cnhzcy14 0:9a66107976a4 36 Check the value of count and signal waiting thread when condition is
cnhzcy14 0:9a66107976a4 37 reached. Note that this occurs while mutex is locked.
cnhzcy14 0:9a66107976a4 38 */
cnhzcy14 0:9a66107976a4 39 if (count == COUNT_LIMIT) {
cnhzcy14 0:9a66107976a4 40 printf("inc_count(): thread %ld, count = %d Threshold reached. ",
cnhzcy14 0:9a66107976a4 41 my_id, count);
cnhzcy14 0:9a66107976a4 42 osSignalSet(id, 0x1);
cnhzcy14 0:9a66107976a4 43 printf("Just sent signal.\n");
cnhzcy14 0:9a66107976a4 44 }
cnhzcy14 0:9a66107976a4 45 printf("inc_count(): thread %ld, count = %d, unlocking mutex\n",
cnhzcy14 0:9a66107976a4 46 my_id, count);
cnhzcy14 0:9a66107976a4 47 osMutexRelease(count_mutex);
cnhzcy14 0:9a66107976a4 48
cnhzcy14 0:9a66107976a4 49 /* Do some work so threads can alternate on mutex lock */
cnhzcy14 0:9a66107976a4 50 osDelay(1000);
cnhzcy14 0:9a66107976a4 51 }
cnhzcy14 0:9a66107976a4 52 id = osThreadGetId();
cnhzcy14 0:9a66107976a4 53 osThreadTerminate(id);
cnhzcy14 0:9a66107976a4 54 }
cnhzcy14 0:9a66107976a4 55
cnhzcy14 0:9a66107976a4 56 void watch_count(void const *t)
cnhzcy14 0:9a66107976a4 57 {
cnhzcy14 0:9a66107976a4 58 osThreadId id;
cnhzcy14 0:9a66107976a4 59 long my_id = (long)t;
cnhzcy14 0:9a66107976a4 60
cnhzcy14 0:9a66107976a4 61 printf("Starting watch_count(): thread %ld\n", my_id);
cnhzcy14 0:9a66107976a4 62
cnhzcy14 0:9a66107976a4 63 //osMutexWait(count_mutex, osWaitForever);
cnhzcy14 0:9a66107976a4 64 while (count < COUNT_LIMIT) {
cnhzcy14 0:9a66107976a4 65 //osMutexRelease(count_mutex);
cnhzcy14 0:9a66107976a4 66 printf("watch_count(): thread %ld Count= %d. Going into wait...\n", my_id,count);
cnhzcy14 0:9a66107976a4 67 osSignalWait(0x1, osWaitForever);
cnhzcy14 0:9a66107976a4 68 //osMutexWait(count_mutex, osWaitForever);
cnhzcy14 0:9a66107976a4 69 printf("watch_count(): thread %ld Condition signal received. Count= %d\n", my_id,count);
cnhzcy14 0:9a66107976a4 70 printf("watch_count(): thread %ld Updating the value of count...\n", my_id);
cnhzcy14 0:9a66107976a4 71 count += 125;
cnhzcy14 0:9a66107976a4 72 printf("watch_count(): thread %ld count now = %d.\n", my_id, count);
cnhzcy14 0:9a66107976a4 73 //osMutexRelease(count_mutex);
cnhzcy14 0:9a66107976a4 74 }
cnhzcy14 0:9a66107976a4 75 printf("watch_count(): thread %ld Unlocking mutex.\n", my_id);
cnhzcy14 0:9a66107976a4 76 //osMutexRelease(count_mutex);
cnhzcy14 0:9a66107976a4 77 id = osThreadGetId();
cnhzcy14 0:9a66107976a4 78 osThreadTerminate(id);
cnhzcy14 0:9a66107976a4 79 }
cnhzcy14 0:9a66107976a4 80
cnhzcy14 0:9a66107976a4 81 void th0(void const *argument) {watch_count(argument);}
cnhzcy14 0:9a66107976a4 82 osThreadDef(th0, osPriorityNormal, DEFAULT_STACK_SIZE);
cnhzcy14 0:9a66107976a4 83
cnhzcy14 0:9a66107976a4 84 void th1(void const *argument) {inc_count(argument);}
cnhzcy14 0:9a66107976a4 85 osThreadDef(th1, osPriorityNormal, DEFAULT_STACK_SIZE);
cnhzcy14 0:9a66107976a4 86
cnhzcy14 0:9a66107976a4 87 void th2(void const *argument) {inc_count(argument);}
cnhzcy14 0:9a66107976a4 88 osThreadDef(th2, osPriorityNormal, DEFAULT_STACK_SIZE);
cnhzcy14 0:9a66107976a4 89
cnhzcy14 0:9a66107976a4 90 int main(int argc, char *argv[])
cnhzcy14 0:9a66107976a4 91 {
cnhzcy14 0:9a66107976a4 92 debug.baud(57600);
cnhzcy14 0:9a66107976a4 93 long t1=1, t2=2, t3=3;
cnhzcy14 0:9a66107976a4 94 osThreadId id;
cnhzcy14 0:9a66107976a4 95
cnhzcy14 0:9a66107976a4 96
cnhzcy14 0:9a66107976a4 97 /* Initialize mutex and condition variable objects */
cnhzcy14 0:9a66107976a4 98 count_mutex = osMutexCreate(osMutex(count_mutex));
cnhzcy14 0:9a66107976a4 99
cnhzcy14 0:9a66107976a4 100 /* For portability, explicitly create threads in a joinable state */
cnhzcy14 0:9a66107976a4 101
cnhzcy14 0:9a66107976a4 102 id = osThreadCreate(osThread(th0), (void *)t1);
cnhzcy14 0:9a66107976a4 103
cnhzcy14 0:9a66107976a4 104 thread_data_array[0].id = id;
cnhzcy14 0:9a66107976a4 105 thread_data_array[0].my_id = t2;
cnhzcy14 0:9a66107976a4 106 thread_data_array[1].id = id;
cnhzcy14 0:9a66107976a4 107 thread_data_array[1].my_id = t3;
cnhzcy14 0:9a66107976a4 108
cnhzcy14 0:9a66107976a4 109 osThreadCreate(osThread(th1), (void *)&thread_data_array[0]);
cnhzcy14 0:9a66107976a4 110
cnhzcy14 0:9a66107976a4 111
cnhzcy14 0:9a66107976a4 112 osThreadCreate(osThread(th2), (void *)&thread_data_array[1]);
cnhzcy14 0:9a66107976a4 113
cnhzcy14 0:9a66107976a4 114 /* Wait for all threads to complete */
cnhzcy14 0:9a66107976a4 115
cnhzcy14 0:9a66107976a4 116 printf ("Main(): Waited and joined with %d threads. Final value of count = %d. Done.\n",
cnhzcy14 0:9a66107976a4 117 NUM_THREADS, count);
cnhzcy14 0:9a66107976a4 118
cnhzcy14 0:9a66107976a4 119
cnhzcy14 0:9a66107976a4 120
cnhzcy14 0:9a66107976a4 121 }