cmsis rtos demo

Dependencies:   mbed-rtos mbed

Dependents:   L152RE_rtos_test

Committer:
cnhzcy14
Date:
Sun Jan 06 15:03:57 2013 +0000
Revision:
1:cbda4d713dc8
Parent:
0:618ffeb1dfc5
comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cnhzcy14 1:cbda4d713dc8 1 /******************************************************************************
cnhzcy14 1:cbda4d713dc8 2 * DESCRIPTION:
cnhzcy14 1:cbda4d713dc8 3 * A "hello world" CMSIS RTOS program which demonstrates another safe way
cnhzcy14 1:cbda4d713dc8 4 * to pass arguments to threads during thread creation. In this case,
cnhzcy14 1:cbda4d713dc8 5 * a structure is used to pass multiple arguments.
cnhzcy14 1:cbda4d713dc8 6 * AUTHOR: Blaise Barney
cnhzcy14 1:cbda4d713dc8 7 * MODIFICATOR: (Simon) CHENG Ye
cnhzcy14 1:cbda4d713dc8 8 * LAST REVISED: 06/JAN/2013
cnhzcy14 1:cbda4d713dc8 9 ******************************************************************************/
cnhzcy14 1:cbda4d713dc8 10
cnhzcy14 0:618ffeb1dfc5 11 #include "mbed.h"
cnhzcy14 0:618ffeb1dfc5 12 #include "cmsis_os.h"
cnhzcy14 0:618ffeb1dfc5 13
cnhzcy14 0:618ffeb1dfc5 14 #define NUM_THREADS 8
cnhzcy14 1:cbda4d713dc8 15
cnhzcy14 1:cbda4d713dc8 16 Serial debug(USBTX, USBRX); // For mbed debug by serial port.
cnhzcy14 0:618ffeb1dfc5 17 char *messages[NUM_THREADS];
cnhzcy14 0:618ffeb1dfc5 18
cnhzcy14 0:618ffeb1dfc5 19 struct thread_data
cnhzcy14 0:618ffeb1dfc5 20 {
cnhzcy14 0:618ffeb1dfc5 21 int thread_id;
cnhzcy14 0:618ffeb1dfc5 22 int sum;
cnhzcy14 0:618ffeb1dfc5 23 char *message;
cnhzcy14 0:618ffeb1dfc5 24 };
cnhzcy14 0:618ffeb1dfc5 25 struct thread_data thread_data_array[NUM_THREADS];
cnhzcy14 0:618ffeb1dfc5 26
cnhzcy14 0:618ffeb1dfc5 27 void PrintHello(void const *threadarg)
cnhzcy14 0:618ffeb1dfc5 28 {
cnhzcy14 0:618ffeb1dfc5 29 int taskid, sum;
cnhzcy14 0:618ffeb1dfc5 30 char *hello_msg;
cnhzcy14 0:618ffeb1dfc5 31 struct thread_data *my_data;
cnhzcy14 0:618ffeb1dfc5 32 osThreadId id;
cnhzcy14 0:618ffeb1dfc5 33
cnhzcy14 0:618ffeb1dfc5 34 osDelay(1000);
cnhzcy14 0:618ffeb1dfc5 35 my_data = (struct thread_data *) threadarg;
cnhzcy14 0:618ffeb1dfc5 36 taskid = my_data->thread_id;
cnhzcy14 0:618ffeb1dfc5 37 sum = my_data->sum;
cnhzcy14 0:618ffeb1dfc5 38 hello_msg = my_data->message;
cnhzcy14 0:618ffeb1dfc5 39 printf("Thread %d: %s Sum=%d\n", taskid, hello_msg, sum);
cnhzcy14 0:618ffeb1dfc5 40 id = osThreadGetId();
cnhzcy14 0:618ffeb1dfc5 41 osThreadTerminate(id);
cnhzcy14 0:618ffeb1dfc5 42 }
cnhzcy14 0:618ffeb1dfc5 43
cnhzcy14 0:618ffeb1dfc5 44 void t0(void const *argument) {PrintHello(argument);}
cnhzcy14 0:618ffeb1dfc5 45 osThreadDef(t0, osPriorityNormal, DEFAULT_STACK_SIZE);
cnhzcy14 0:618ffeb1dfc5 46
cnhzcy14 0:618ffeb1dfc5 47 void t1(void const *argument) {PrintHello(argument);}
cnhzcy14 0:618ffeb1dfc5 48 osThreadDef(t1, osPriorityNormal, DEFAULT_STACK_SIZE);
cnhzcy14 0:618ffeb1dfc5 49
cnhzcy14 0:618ffeb1dfc5 50 void t2(void const *argument) {PrintHello(argument);}
cnhzcy14 0:618ffeb1dfc5 51 osThreadDef(t2, osPriorityNormal, DEFAULT_STACK_SIZE);
cnhzcy14 0:618ffeb1dfc5 52
cnhzcy14 0:618ffeb1dfc5 53 void t3(void const *argument) {PrintHello(argument);}
cnhzcy14 0:618ffeb1dfc5 54 osThreadDef(t3, osPriorityNormal, DEFAULT_STACK_SIZE);
cnhzcy14 0:618ffeb1dfc5 55
cnhzcy14 0:618ffeb1dfc5 56 void t4(void const *argument) {PrintHello(argument);}
cnhzcy14 0:618ffeb1dfc5 57 osThreadDef(t4, osPriorityNormal, DEFAULT_STACK_SIZE);
cnhzcy14 0:618ffeb1dfc5 58
cnhzcy14 0:618ffeb1dfc5 59 void t5(void const *argument) {PrintHello(argument);}
cnhzcy14 0:618ffeb1dfc5 60 osThreadDef(t5, osPriorityNormal, DEFAULT_STACK_SIZE);
cnhzcy14 0:618ffeb1dfc5 61
cnhzcy14 0:618ffeb1dfc5 62 void t6(void const *argument) {PrintHello(argument);}
cnhzcy14 0:618ffeb1dfc5 63 osThreadDef(t6, osPriorityNormal, DEFAULT_STACK_SIZE);
cnhzcy14 0:618ffeb1dfc5 64
cnhzcy14 0:618ffeb1dfc5 65 void t7(void const *argument) {PrintHello(argument);}
cnhzcy14 0:618ffeb1dfc5 66 osThreadDef(t7, osPriorityNormal, DEFAULT_STACK_SIZE);
cnhzcy14 0:618ffeb1dfc5 67
cnhzcy14 1:cbda4d713dc8 68 int main()
cnhzcy14 1:cbda4d713dc8 69 {
cnhzcy14 0:618ffeb1dfc5 70 debug.baud(57600);
cnhzcy14 0:618ffeb1dfc5 71 int t, sum;
cnhzcy14 0:618ffeb1dfc5 72
cnhzcy14 0:618ffeb1dfc5 73 sum=0;
cnhzcy14 0:618ffeb1dfc5 74 messages[0] = "English: Hello World!";
cnhzcy14 0:618ffeb1dfc5 75 messages[1] = "French: Bonjour, le monde!";
cnhzcy14 0:618ffeb1dfc5 76 messages[2] = "Spanish: Hola al mundo";
cnhzcy14 0:618ffeb1dfc5 77 messages[3] = "Klingon: Nuq neH!";
cnhzcy14 0:618ffeb1dfc5 78 messages[4] = "German: Guten Tag, Welt!";
cnhzcy14 0:618ffeb1dfc5 79 messages[5] = "Russian: Zdravstvytye, mir!";
cnhzcy14 0:618ffeb1dfc5 80 messages[6] = "Japan: Sekai e konnichiwa!";
cnhzcy14 0:618ffeb1dfc5 81 messages[7] = "Latin: Orbis, te saluto!";
cnhzcy14 1:cbda4d713dc8 82
cnhzcy14 1:cbda4d713dc8 83 for(t=0;t<NUM_THREADS;t++)
cnhzcy14 1:cbda4d713dc8 84 {
cnhzcy14 0:618ffeb1dfc5 85 sum = sum + t;
cnhzcy14 0:618ffeb1dfc5 86 thread_data_array[t].thread_id = t;
cnhzcy14 0:618ffeb1dfc5 87 thread_data_array[t].sum = sum;
cnhzcy14 0:618ffeb1dfc5 88 thread_data_array[t].message = messages[t];
cnhzcy14 0:618ffeb1dfc5 89 printf("Creating thread %d\n", t);
cnhzcy14 0:618ffeb1dfc5 90 if(t==0) osThreadCreate(osThread(t0), (void *)&thread_data_array[t]);
cnhzcy14 0:618ffeb1dfc5 91 if(t==1) osThreadCreate(osThread(t1), (void *)&thread_data_array[t]);
cnhzcy14 0:618ffeb1dfc5 92 if(t==2) osThreadCreate(osThread(t2), (void *)&thread_data_array[t]);
cnhzcy14 0:618ffeb1dfc5 93 if(t==3) osThreadCreate(osThread(t3), (void *)&thread_data_array[t]);
cnhzcy14 0:618ffeb1dfc5 94 if(t==4) osThreadCreate(osThread(t4), (void *)&thread_data_array[t]);
cnhzcy14 0:618ffeb1dfc5 95 if(t==5) osThreadCreate(osThread(t5), (void *)&thread_data_array[t]);
cnhzcy14 0:618ffeb1dfc5 96 if(t==6) osThreadCreate(osThread(t6), (void *)&thread_data_array[t]);
cnhzcy14 0:618ffeb1dfc5 97 if(t==7) osThreadCreate(osThread(t7), (void *)&thread_data_array[t]);
cnhzcy14 0:618ffeb1dfc5 98 }
cnhzcy14 0:618ffeb1dfc5 99 }