simple mutex demo on lpc4337
Fork of lpc4337_mbed_os_semaphore_demo by
Revision 1:c8817129932e, committed 2016-10-03
- Comitter:
- uLipe
- Date:
- Mon Oct 03 23:08:57 2016 +0000
- Parent:
- 0:d767141a0a9c
- Commit message:
- first working commit
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Wed Sep 28 16:28:49 2016 +0000 +++ b/main.cpp Mon Oct 03 23:08:57 2016 +0000 @@ -11,6 +11,9 @@ #define FONT_HEIGHT 10 #define FONT_WIDTH 5 +#define RT_TASK 0x8001 +#define HP_TASK 0x8002 + /* allocate statically stacks for the three threads */ unsigned char rt_stk[1024]; @@ -23,40 +26,62 @@ Thread low_prio_thread(osPriorityNormal, 1024, &lp_stk[0]); -/* creates semaphores to explore the capabilites of synchronization */ -Semaphore rt_sema; -Semaphore hp_sema; -const char rt_message[] = {"rt_task() sema:\0"}; -const char hp_message[] = {"hp_task() sema:\0"}; -const char lp_message[] = {"np_task() exec:\0"}; +/* crates a mutex that will help us to controll a resource access */ +Mutex message_lock; +/* console buffer which has shared access */ +char console_buffer[64] = {0}; /* creates a instance of display */ ST7567 disp(D11, D13, D12, D9, D10); +/** + * @brief prints the current task which executes: + */ +static void log_thread(int t, int noof_exec) { + + /* try to take the mutex, or wait until becomes avalialble*/ + + + disp.cls(); + + disp.locate(0, FONT_HEIGHT * 1); + disp.printf("Thread status: "); + disp.locate(0, FONT_HEIGHT * 2); + if(t == RT_TASK) { + message_lock.lock(); + strcpy(&console_buffer[0], "-current thread: rt_task();\0" ); + message_lock.unlock(); + disp.printf("%s", console_buffer); + } else { + message_lock.lock(); + strcpy(&console_buffer[0], "-current thread: hp_task();\0" ); + message_lock.unlock(); + disp.printf("%s", console_buffer); + } + + disp.locate(0, FONT_HEIGHT * 3); + disp.printf("-noof executions: %d ;", noof_exec); + disp.locate(0, FONT_HEIGHT * 4); + disp.printf("-mutex status: taken ."); + + +} /** * @brief real time prio task function */ -static void rt_task(void) { - - disp.locate(0, FONT_HEIGHT * 2); - disp.printf(rt_message); - rt_sema.wait(); +static void rt_task(void) { + int execs = 0; for(;;) { - - /* dsiplay the semaphore status */ - disp.locate((sizeof(lp_message) * FONT_WIDTH), FONT_HEIGHT * 4); - disp.printf("WAIT!"); - - disp.locate((sizeof(rt_message) * FONT_WIDTH), FONT_HEIGHT * 2); - disp.printf("TOOK!"); - rt_sema.wait(); + execs++; + log_thread(RT_TASK, execs); + Thread::wait(2000); } } @@ -65,20 +90,13 @@ * @brief high prio task function */ static void hp_task(void){ - disp.locate(0, FONT_HEIGHT * 3); - disp.printf(hp_message); - hp_sema.wait(); + int execs = 0; for(;;) { - - /* dsiplay the semaphore status */ - disp.locate((sizeof(lp_message) * FONT_WIDTH), FONT_HEIGHT * 4); - disp.printf("WAIT!"); - - disp.locate((sizeof(hp_message) * FONT_WIDTH), FONT_HEIGHT * 3); - disp.printf("TOOK!"); - hp_sema.wait(); + execs++; + log_thread(HP_TASK, execs); + Thread::wait(2000); } } @@ -86,31 +104,10 @@ * @brief normal prio task function */ static void np_task(void) { - - disp.locate(0, FONT_HEIGHT * 4); - disp.printf(lp_message); - - disp.locate((sizeof(lp_message) * FONT_WIDTH), FONT_HEIGHT * 4); - disp.printf("RUNN!"); - disp.locate((sizeof(rt_message) * FONT_WIDTH), FONT_HEIGHT * 2); - disp.printf("WAIT!"); - disp.locate((sizeof(hp_message) * FONT_WIDTH), FONT_HEIGHT * 3); - disp.printf("WAIT!"); - + + Thread::wait(0); for(;;) { - rt_sema.release(); - disp.locate((sizeof(rt_message) * FONT_WIDTH), FONT_HEIGHT * 2); - disp.printf("WAIT!"); - disp.locate((sizeof(lp_message) * FONT_WIDTH), FONT_HEIGHT * 4); - disp.printf("RUNN!"); - - - hp_sema.release(); - disp.locate((sizeof(hp_message) * FONT_WIDTH), FONT_HEIGHT * 3); - disp.printf("WAIT!"); - disp.locate((sizeof(lp_message) * FONT_WIDTH), FONT_HEIGHT * 4); - disp.printf("RUNN!"); } }