cmsis rtos demo
Revision 0:9a66107976a4, committed 2013-01-06
- Comitter:
- cnhzcy14
- Date:
- Sun Jan 06 14:34:08 2013 +0000
- Child:
- 1:8289e2db9449
- Commit message:
- rtos signal
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Sun Jan 06 14:34:08 2013 +0000
@@ -0,0 +1,121 @@
+#include "mbed.h"
+#include "cmsis_os.h"
+
+#define NUM_THREADS 3
+#define TCOUNT 10
+#define COUNT_LIMIT 12
+
+int count = 0;
+osMutexId count_mutex;
+osMutexDef(count_mutex);
+
+struct thread_data
+{
+ osThreadId id;
+ long my_id;
+};
+
+struct thread_data thread_data_array[NUM_THREADS-1];
+Serial debug(USBTX, USBRX);
+
+void inc_count(void const *threadarg)
+{
+ int i;
+ long my_id;
+ osThreadId id;
+ struct thread_data *my_data;
+ my_data = (struct thread_data *) threadarg;
+ id = my_data->id;
+ my_id = my_data->my_id;
+
+ for (i=0; i < TCOUNT; i++) {
+ osMutexWait(count_mutex, osWaitForever);
+ count++;
+
+ /*
+ Check the value of count and signal waiting thread when condition is
+ reached. Note that this occurs while mutex is locked.
+ */
+ if (count == COUNT_LIMIT) {
+ printf("inc_count(): thread %ld, count = %d Threshold reached. ",
+ my_id, count);
+ osSignalSet(id, 0x1);
+ printf("Just sent signal.\n");
+ }
+ printf("inc_count(): thread %ld, count = %d, unlocking mutex\n",
+ my_id, count);
+ osMutexRelease(count_mutex);
+
+ /* Do some work so threads can alternate on mutex lock */
+ osDelay(1000);
+ }
+ id = osThreadGetId();
+ osThreadTerminate(id);
+}
+
+void watch_count(void const *t)
+{
+ osThreadId id;
+ long my_id = (long)t;
+
+ printf("Starting watch_count(): thread %ld\n", my_id);
+
+ //osMutexWait(count_mutex, osWaitForever);
+ while (count < COUNT_LIMIT) {
+ //osMutexRelease(count_mutex);
+ printf("watch_count(): thread %ld Count= %d. Going into wait...\n", my_id,count);
+ osSignalWait(0x1, osWaitForever);
+ //osMutexWait(count_mutex, osWaitForever);
+ printf("watch_count(): thread %ld Condition signal received. Count= %d\n", my_id,count);
+ printf("watch_count(): thread %ld Updating the value of count...\n", my_id);
+ count += 125;
+ printf("watch_count(): thread %ld count now = %d.\n", my_id, count);
+ //osMutexRelease(count_mutex);
+ }
+ printf("watch_count(): thread %ld Unlocking mutex.\n", my_id);
+ //osMutexRelease(count_mutex);
+ id = osThreadGetId();
+ osThreadTerminate(id);
+}
+
+void th0(void const *argument) {watch_count(argument);}
+osThreadDef(th0, osPriorityNormal, DEFAULT_STACK_SIZE);
+
+void th1(void const *argument) {inc_count(argument);}
+osThreadDef(th1, osPriorityNormal, DEFAULT_STACK_SIZE);
+
+void th2(void const *argument) {inc_count(argument);}
+osThreadDef(th2, osPriorityNormal, DEFAULT_STACK_SIZE);
+
+int main(int argc, char *argv[])
+{
+ debug.baud(57600);
+ long t1=1, t2=2, t3=3;
+ osThreadId id;
+
+
+ /* Initialize mutex and condition variable objects */
+ count_mutex = osMutexCreate(osMutex(count_mutex));
+
+ /* For portability, explicitly create threads in a joinable state */
+
+ id = osThreadCreate(osThread(th0), (void *)t1);
+
+ thread_data_array[0].id = id;
+ thread_data_array[0].my_id = t2;
+ thread_data_array[1].id = id;
+ thread_data_array[1].my_id = t3;
+
+ osThreadCreate(osThread(th1), (void *)&thread_data_array[0]);
+
+
+ osThreadCreate(osThread(th2), (void *)&thread_data_array[1]);
+
+ /* Wait for all threads to complete */
+
+ printf ("Main(): Waited and joined with %d threads. Final value of count = %d. Done.\n",
+ NUM_THREADS, count);
+
+
+
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Sun Jan 06 14:34:08 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#88a1a9c26ae3
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sun Jan 06 14:34:08 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/63cdd78b2dc1 \ No newline at end of file
Ye Cheng