TEN MBED OS Course mutex lab

Committer:
uLipe
Date:
Sun Jan 29 20:41:53 2017 +0000
Revision:
0:8b346455add1
first;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
uLipe 0:8b346455add1 1 /**
uLipe 0:8b346455add1 2 * @brief first thread control program with MBED OS
uLipe 0:8b346455add1 3 */
uLipe 0:8b346455add1 4 #include "mbed.h"
uLipe 0:8b346455add1 5 #include "rtos.h"
uLipe 0:8b346455add1 6
uLipe 0:8b346455add1 7 /* declares threads for this demo: */
uLipe 0:8b346455add1 8 const size_t a_stk_size = 1024;
uLipe 0:8b346455add1 9 uint8_t a_stk[a_stk_size];
uLipe 0:8b346455add1 10 Thread a_thread(osPriorityNormal, a_stk_size, &a_stk[0]);
uLipe 0:8b346455add1 11
uLipe 0:8b346455add1 12 const size_t b_stk_size = 1024;
uLipe 0:8b346455add1 13 uint8_t b_stk[b_stk_size];
uLipe 0:8b346455add1 14 Thread b_thread(osPriorityNormal, b_stk_size, &b_stk[0]);
uLipe 0:8b346455add1 15
uLipe 0:8b346455add1 16 const size_t printer_stk_size = 2048;
uLipe 0:8b346455add1 17 uint8_t printer_stk[printer_stk_size];
uLipe 0:8b346455add1 18 Thread printer_thread(osPriorityHigh, printer_stk_size, &printer_stk[0]);
uLipe 0:8b346455add1 19
uLipe 0:8b346455add1 20 /* semaphore to simulate a io driver*/
uLipe 0:8b346455add1 21 Semaphore sema;
uLipe 0:8b346455add1 22
uLipe 0:8b346455add1 23 /* Mutex declared to share resource */
uLipe 0:8b346455add1 24 Mutex mtx;
uLipe 0:8b346455add1 25
uLipe 0:8b346455add1 26 /* reserve the debbuger uart to shell interface */
uLipe 0:8b346455add1 27 Serial pc_serial(USBTX,USBRX);
uLipe 0:8b346455add1 28
uLipe 0:8b346455add1 29 /* ram buffer to be acessed for both a and b threads */
uLipe 0:8b346455add1 30 char ram_console[1024];
uLipe 0:8b346455add1 31
uLipe 0:8b346455add1 32 /**
uLipe 0:8b346455add1 33 * @brief thread a function
uLipe 0:8b346455add1 34 */
uLipe 0:8b346455add1 35 static void thread_a(void)
uLipe 0:8b346455add1 36 {
uLipe 0:8b346455add1 37 const char message[] = {"thread_a writes: Hocus Pocus! Simsalabim!"};
uLipe 0:8b346455add1 38
uLipe 0:8b346455add1 39 for(;;) {
uLipe 0:8b346455add1 40 a_thread.wait(10);
uLipe 0:8b346455add1 41 /* protect the resource */
uLipe 0:8b346455add1 42 mtx.lock();
uLipe 0:8b346455add1 43 memcpy(&ram_console, &message, sizeof(message));
uLipe 0:8b346455add1 44 sema.release();
uLipe 0:8b346455add1 45 /* release the protected resource */
uLipe 0:8b346455add1 46 mtx.unlock();
uLipe 0:8b346455add1 47 }
uLipe 0:8b346455add1 48 }
uLipe 0:8b346455add1 49
uLipe 0:8b346455add1 50
uLipe 0:8b346455add1 51 /**
uLipe 0:8b346455add1 52 * @brief thread a function
uLipe 0:8b346455add1 53 */
uLipe 0:8b346455add1 54 static void thread_b(void)
uLipe 0:8b346455add1 55 {
uLipe 0:8b346455add1 56 const char message[] = {"thread_b writes: Abracadabra!"};
uLipe 0:8b346455add1 57
uLipe 0:8b346455add1 58 for(;;) {
uLipe 0:8b346455add1 59 b_thread.wait(10);
uLipe 0:8b346455add1 60 /* protect the resource */
uLipe 0:8b346455add1 61 mtx.lock();
uLipe 0:8b346455add1 62 memcpy(&ram_console, &message, sizeof(message));
uLipe 0:8b346455add1 63 sema.release();
uLipe 0:8b346455add1 64 /* release the protected resource */
uLipe 0:8b346455add1 65 mtx.unlock();
uLipe 0:8b346455add1 66 }
uLipe 0:8b346455add1 67 }
uLipe 0:8b346455add1 68
uLipe 0:8b346455add1 69 /**
uLipe 0:8b346455add1 70 * @brief thread printer function
uLipe 0:8b346455add1 71 */
uLipe 0:8b346455add1 72 static void thread_printer(void)
uLipe 0:8b346455add1 73 {
uLipe 0:8b346455add1 74
uLipe 0:8b346455add1 75 for(;;) {
uLipe 0:8b346455add1 76 sema.wait(osWaitForever);
uLipe 0:8b346455add1 77 /* prints the contents of buffer */
uLipe 0:8b346455add1 78 pc_serial.printf("##[PRINTER_OUT]: %s ## \n\r", ram_console);
uLipe 0:8b346455add1 79
uLipe 0:8b346455add1 80 for(int i = 0; i < 0xFFFFFF; i++);
uLipe 0:8b346455add1 81
uLipe 0:8b346455add1 82 /* flushes the I/O console */
uLipe 0:8b346455add1 83 memset(&ram_console, 0, sizeof(ram_console));
uLipe 0:8b346455add1 84 }
uLipe 0:8b346455add1 85 }
uLipe 0:8b346455add1 86
uLipe 0:8b346455add1 87
uLipe 0:8b346455add1 88 /**
uLipe 0:8b346455add1 89 * @brief main application loop
uLipe 0:8b346455add1 90 */
uLipe 0:8b346455add1 91 int main(void)
uLipe 0:8b346455add1 92 {
uLipe 0:8b346455add1 93 pc_serial.baud(115200);
uLipe 0:8b346455add1 94 a_thread.start(thread_a);
uLipe 0:8b346455add1 95 b_thread.start(thread_b);
uLipe 0:8b346455add1 96 printer_thread.start(thread_printer);
uLipe 0:8b346455add1 97 return 0;
uLipe 0:8b346455add1 98 }