Threading of mbed os running in LPC4337 xplorer kit

Dependencies:   ST7567

Committer:
uLipe
Date:
Mon Sep 26 21:14:47 2016 +0000
Revision:
0:da8a31f3e0f7
First commit: initial implementation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
uLipe 0:da8a31f3e0f7 1 /**
uLipe 0:da8a31f3e0f7 2 * @brief Simple threading using the mbed os demo
uLipe 0:da8a31f3e0f7 3 */
uLipe 0:da8a31f3e0f7 4 #include "mbed.h"
uLipe 0:da8a31f3e0f7 5 #include "rtos.h"
uLipe 0:da8a31f3e0f7 6 #include "ST7567.h"
uLipe 0:da8a31f3e0f7 7
uLipe 0:da8a31f3e0f7 8 /* LCD and font parameters */
uLipe 0:da8a31f3e0f7 9 #define LCD_HEIGHT 64
uLipe 0:da8a31f3e0f7 10 #define LCD_WIDTH 128
uLipe 0:da8a31f3e0f7 11 #define FONT_HEIGHT 10
uLipe 0:da8a31f3e0f7 12 #define FONT_WIDTH 5
uLipe 0:da8a31f3e0f7 13
uLipe 0:da8a31f3e0f7 14
uLipe 0:da8a31f3e0f7 15 /* allocate statically stacks for the three threads */
uLipe 0:da8a31f3e0f7 16 unsigned char rt_stk[1024];
uLipe 0:da8a31f3e0f7 17 unsigned char hp_stk[1024];
uLipe 0:da8a31f3e0f7 18 unsigned char lp_stk[1024];
uLipe 0:da8a31f3e0f7 19
uLipe 0:da8a31f3e0f7 20 /* creates three tread objects with different priorities */
uLipe 0:da8a31f3e0f7 21 Thread real_time_thread(osPriorityRealtime, 1024, &rt_stk[0]);
uLipe 0:da8a31f3e0f7 22 Thread high_prio_thread(osPriorityHigh, 1024, &hp_stk[0]);
uLipe 0:da8a31f3e0f7 23 Thread low_prio_thread(osPriorityNormal, 1024, &lp_stk[0]);
uLipe 0:da8a31f3e0f7 24
uLipe 0:da8a31f3e0f7 25
uLipe 0:da8a31f3e0f7 26 /* create a semaphore to synchronize the threads */
uLipe 0:da8a31f3e0f7 27 Semaphore sync_sema;
uLipe 0:da8a31f3e0f7 28
uLipe 0:da8a31f3e0f7 29 /* creates a instance of display */
uLipe 0:da8a31f3e0f7 30 ST7567 disp(D11, D13, D12, D9, D10);
uLipe 0:da8a31f3e0f7 31
uLipe 0:da8a31f3e0f7 32
uLipe 0:da8a31f3e0f7 33
uLipe 0:da8a31f3e0f7 34
uLipe 0:da8a31f3e0f7 35 /**
uLipe 0:da8a31f3e0f7 36 * @brief real time prio task function
uLipe 0:da8a31f3e0f7 37 */
uLipe 0:da8a31f3e0f7 38 static void rt_task(void) {
uLipe 0:da8a31f3e0f7 39 const char rt_message[] = {"Real time prio Task: \0"};
uLipe 0:da8a31f3e0f7 40
uLipe 0:da8a31f3e0f7 41 disp.locate(0, FONT_HEIGHT * 2);
uLipe 0:da8a31f3e0f7 42
uLipe 0:da8a31f3e0f7 43 disp.printf(rt_message);
uLipe 0:da8a31f3e0f7 44
uLipe 0:da8a31f3e0f7 45 for(;;) {
uLipe 0:da8a31f3e0f7 46
uLipe 0:da8a31f3e0f7 47 /* take the semaphore allowing less priority
uLipe 0:da8a31f3e0f7 48 * tasks to run
uLipe 0:da8a31f3e0f7 49 */
uLipe 0:da8a31f3e0f7 50 disp.locate(LCD_WIDTH - (sizeof(rt_message) * FONT_WIDTH), FONT_HEIGHT * 2);
uLipe 0:da8a31f3e0f7 51 disp.printf("WAIT!");
uLipe 0:da8a31f3e0f7 52 sync_sema.wait(0);
uLipe 0:da8a31f3e0f7 53
uLipe 0:da8a31f3e0f7 54 /* show the message by 1 second */
uLipe 0:da8a31f3e0f7 55 disp.locate(LCD_WIDTH - (sizeof(rt_message) * FONT_WIDTH), FONT_HEIGHT * 2);
uLipe 0:da8a31f3e0f7 56 disp.printf("EXEC!");
uLipe 0:da8a31f3e0f7 57
uLipe 0:da8a31f3e0f7 58 disp.locate(LCD_WIDTH - (sizeof(rt_message) * FONT_WIDTH), FONT_HEIGHT * 2);
uLipe 0:da8a31f3e0f7 59 disp.printf("DELAY");
uLipe 0:da8a31f3e0f7 60 Thread::wait(50);
uLipe 0:da8a31f3e0f7 61
uLipe 0:da8a31f3e0f7 62 }
uLipe 0:da8a31f3e0f7 63
uLipe 0:da8a31f3e0f7 64 }
uLipe 0:da8a31f3e0f7 65
uLipe 0:da8a31f3e0f7 66 /**
uLipe 0:da8a31f3e0f7 67 * @brief high prio task function
uLipe 0:da8a31f3e0f7 68 */
uLipe 0:da8a31f3e0f7 69 static void hp_task(void){
uLipe 0:da8a31f3e0f7 70 const char hp_message[] = {"High priority Task: \0"};
uLipe 0:da8a31f3e0f7 71
uLipe 0:da8a31f3e0f7 72 disp.locate(0, FONT_HEIGHT * 3);
uLipe 0:da8a31f3e0f7 73 disp.printf(hp_message);
uLipe 0:da8a31f3e0f7 74
uLipe 0:da8a31f3e0f7 75 for(;;) {
uLipe 0:da8a31f3e0f7 76 /* take the semaphore allowing less priority
uLipe 0:da8a31f3e0f7 77 * tasks to run
uLipe 0:da8a31f3e0f7 78 */
uLipe 0:da8a31f3e0f7 79 disp.locate(LCD_WIDTH - (sizeof(hp_message) * FONT_WIDTH), FONT_HEIGHT * 3);
uLipe 0:da8a31f3e0f7 80 disp.printf("WAIT!");
uLipe 0:da8a31f3e0f7 81 sync_sema.wait(0);
uLipe 0:da8a31f3e0f7 82
uLipe 0:da8a31f3e0f7 83 /* show the message by 1 second */
uLipe 0:da8a31f3e0f7 84 disp.locate(LCD_WIDTH - (sizeof(hp_message) * FONT_WIDTH), FONT_HEIGHT * 3);
uLipe 0:da8a31f3e0f7 85 disp.printf("EXEC!");
uLipe 0:da8a31f3e0f7 86
uLipe 0:da8a31f3e0f7 87 disp.locate(LCD_WIDTH - (sizeof(hp_message) * FONT_WIDTH), FONT_HEIGHT * 3);
uLipe 0:da8a31f3e0f7 88 disp.printf("DELAY");
uLipe 0:da8a31f3e0f7 89 Thread::wait(50);
uLipe 0:da8a31f3e0f7 90
uLipe 0:da8a31f3e0f7 91 }
uLipe 0:da8a31f3e0f7 92 }
uLipe 0:da8a31f3e0f7 93
uLipe 0:da8a31f3e0f7 94 /**
uLipe 0:da8a31f3e0f7 95 * @brief normal prio task function
uLipe 0:da8a31f3e0f7 96 */
uLipe 0:da8a31f3e0f7 97 static void np_task(void) {
uLipe 0:da8a31f3e0f7 98
uLipe 0:da8a31f3e0f7 99 const char lp_message[] = {"Low priority Task: \0"};
uLipe 0:da8a31f3e0f7 100
uLipe 0:da8a31f3e0f7 101 disp.locate(0, FONT_HEIGHT * 4);
uLipe 0:da8a31f3e0f7 102 disp.printf(lp_message);
uLipe 0:da8a31f3e0f7 103
uLipe 0:da8a31f3e0f7 104
uLipe 0:da8a31f3e0f7 105 for(;;) {
uLipe 0:da8a31f3e0f7 106 /* show the message by 1 second */
uLipe 0:da8a31f3e0f7 107 disp.locate(LCD_WIDTH - (sizeof(lp_message) * FONT_WIDTH), FONT_HEIGHT * 4);
uLipe 0:da8a31f3e0f7 108 disp.printf("EXEC!");
uLipe 0:da8a31f3e0f7 109
uLipe 0:da8a31f3e0f7 110 disp.locate(LCD_WIDTH - (sizeof(lp_message) * FONT_WIDTH), FONT_HEIGHT * 4);
uLipe 0:da8a31f3e0f7 111 disp.printf("DELAY");
uLipe 0:da8a31f3e0f7 112 Thread::wait(50);
uLipe 0:da8a31f3e0f7 113
uLipe 0:da8a31f3e0f7 114 /* release the semaphore and syncrhonize the tasks */
uLipe 0:da8a31f3e0f7 115 disp.locate(LCD_WIDTH - (sizeof(lp_message) * FONT_WIDTH), FONT_HEIGHT * 4);
uLipe 0:da8a31f3e0f7 116 disp.printf("WAIT!");
uLipe 0:da8a31f3e0f7 117 sync_sema.release();
uLipe 0:da8a31f3e0f7 118
uLipe 0:da8a31f3e0f7 119 }
uLipe 0:da8a31f3e0f7 120 }
uLipe 0:da8a31f3e0f7 121
uLipe 0:da8a31f3e0f7 122 /**
uLipe 0:da8a31f3e0f7 123 * @brief main loop
uLipe 0:da8a31f3e0f7 124 */
uLipe 0:da8a31f3e0f7 125 int main(void) {
uLipe 0:da8a31f3e0f7 126 const char banner[] = {"Embarcados MBED-OS Thread demo\0"};
uLipe 0:da8a31f3e0f7 127
uLipe 0:da8a31f3e0f7 128 /* configures the display */
uLipe 0:da8a31f3e0f7 129 disp.cls();
uLipe 0:da8a31f3e0f7 130 disp.set_contrast(0x35);
uLipe 0:da8a31f3e0f7 131 disp.locate((LCD_WIDTH - (sizeof(banner) * FONT_WIDTH)) / 2, 0);
uLipe 0:da8a31f3e0f7 132 disp.printf(banner);
uLipe 0:da8a31f3e0f7 133
uLipe 0:da8a31f3e0f7 134
uLipe 0:da8a31f3e0f7 135 /* starts the three tasks */
uLipe 0:da8a31f3e0f7 136 real_time_thread.start(rt_task);
uLipe 0:da8a31f3e0f7 137 high_prio_thread.start(hp_task);
uLipe 0:da8a31f3e0f7 138 low_prio_thread.start(np_task);
uLipe 0:da8a31f3e0f7 139
uLipe 0:da8a31f3e0f7 140
uLipe 0:da8a31f3e0f7 141 return 0;
uLipe 0:da8a31f3e0f7 142 }
uLipe 0:da8a31f3e0f7 143