A very simple example of an RTOS executing on an LPC1768 Platform

Dependencies:   mbed-rtos mbed

Fork of app-board-RTOS-Threads by jim hamblen

Committer:
TheFella
Date:
Mon Aug 08 14:55:12 2016 +0000
Revision:
8:261f8af23dae
Parent:
7:ea9f0cd97179

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TheFella 8:261f8af23dae 1 // Example using mbedRTOS on the LPC1768
TheFella 8:261f8af23dae 2 // Introduction to RTOS
TheFella 7:ea9f0cd97179 3 // Pot1 ADC limit for LED1
TheFella 7:ea9f0cd97179 4 // Pot2 ADC limit for LED2
dreschpe 1:1c6a9eaf55b5 5
dreschpe 0:f6a57b843f79 6 #include "mbed.h"
dreschpe 0:f6a57b843f79 7 #include "rtos.h"
dreschpe 0:f6a57b843f79 8 #include "stdio.h"
dreschpe 0:f6a57b843f79 9
TheFella 5:d6da74ac1cc3 10 #define LEDON 1
TheFella 5:d6da74ac1cc3 11 #define LEDOFF 0
4180_1 4:79863d2ea5a0 12
TheFella 5:d6da74ac1cc3 13
TheFella 5:d6da74ac1cc3 14 Serial pc(USBTX, USBRX);
dreschpe 1:1c6a9eaf55b5 15 AnalogIn Pot1(p19);
dreschpe 1:1c6a9eaf55b5 16 AnalogIn Pot2(p20);
TheFella 5:d6da74ac1cc3 17 DigitalOut led1(LED1);
TheFella 5:d6da74ac1cc3 18 DigitalOut led2(LED2);
dreschpe 2:a69c8c5f5b03 19
TheFella 5:d6da74ac1cc3 20
TheFella 5:d6da74ac1cc3 21 // mutex to make the serial thread safe
TheFella 5:d6da74ac1cc3 22 Mutex pc_mutex;
dreschpe 0:f6a57b843f79 23
dreschpe 2:a69c8c5f5b03 24 // Thread 1
dreschpe 2:a69c8c5f5b03 25 // print counter into first line and wait for 1 s
dreschpe 0:f6a57b843f79 26 void thread1(void const *args)
dreschpe 0:f6a57b843f79 27 {
dreschpe 0:f6a57b843f79 28 int i;
TheFella 8:261f8af23dae 29 while(true)
TheFella 8:261f8af23dae 30 { // thread loop
TheFella 5:d6da74ac1cc3 31 pc_mutex.lock();
TheFella 5:d6da74ac1cc3 32 pc.printf("Thread1 count: %d\n\r",i);
TheFella 5:d6da74ac1cc3 33 pc_mutex.unlock();
dreschpe 0:f6a57b843f79 34 i++;
dreschpe 0:f6a57b843f79 35 Thread::wait(1000);
dreschpe 0:f6a57b843f79 36 }
dreschpe 0:f6a57b843f79 37 }
dreschpe 0:f6a57b843f79 38
dreschpe 2:a69c8c5f5b03 39 // Thread 2
dreschpe 2:a69c8c5f5b03 40 // print counter into third line and wait for 0,5s
dreschpe 0:f6a57b843f79 41 void thread2(void const *args)
dreschpe 0:f6a57b843f79 42 {
dreschpe 1:1c6a9eaf55b5 43 int k;
TheFella 8:261f8af23dae 44 while(true)
TheFella 8:261f8af23dae 45 { // thread loop
TheFella 5:d6da74ac1cc3 46 pc_mutex.lock();
TheFella 5:d6da74ac1cc3 47 pc.printf("Thread 2 count : %d\n\r",k);
TheFella 5:d6da74ac1cc3 48 pc_mutex.unlock();
dreschpe 1:1c6a9eaf55b5 49 k++;
dreschpe 2:a69c8c5f5b03 50 Thread::wait(500); // wait 0.5s
dreschpe 0:f6a57b843f79 51 }
dreschpe 0:f6a57b843f79 52 }
dreschpe 0:f6a57b843f79 53
dreschpe 2:a69c8c5f5b03 54 // Thread 4
TheFella 8:261f8af23dae 55 // Pot1's value is compared to a limit and LED1 is set or not
dreschpe 2:a69c8c5f5b03 56 void thread4(void const *args)
dreschpe 1:1c6a9eaf55b5 57 {
TheFella 8:261f8af23dae 58 while(true)
TheFella 8:261f8af23dae 59 { // thread loop
TheFella 5:d6da74ac1cc3 60 pc_mutex.lock();
TheFella 5:d6da74ac1cc3 61 if( Pot1.read_u16() > 0xEFFF)
TheFella 5:d6da74ac1cc3 62 {
TheFella 5:d6da74ac1cc3 63 led1 = LEDON;
TheFella 5:d6da74ac1cc3 64 }
TheFella 5:d6da74ac1cc3 65 else
TheFella 5:d6da74ac1cc3 66 {
TheFella 5:d6da74ac1cc3 67 led1 = LEDOFF;
TheFella 5:d6da74ac1cc3 68 }
TheFella 5:d6da74ac1cc3 69
TheFella 5:d6da74ac1cc3 70 pc_mutex.unlock();
4180_1 4:79863d2ea5a0 71 }
TheFella 5:d6da74ac1cc3 72 Thread::wait(500); // value of pot1 / 100
4180_1 4:79863d2ea5a0 73 }
TheFella 5:d6da74ac1cc3 74
4180_1 4:79863d2ea5a0 75 // Thread 5
TheFella 8:261f8af23dae 76 // Pot2's value is compared to a limit and LED2 is set or not
4180_1 4:79863d2ea5a0 77 void thread5(void const *args)
4180_1 4:79863d2ea5a0 78 {
TheFella 8:261f8af23dae 79 while(true)
TheFella 8:261f8af23dae 80 { // thread loop
TheFella 5:d6da74ac1cc3 81 pc_mutex.lock();
TheFella 5:d6da74ac1cc3 82 if( Pot2.read_u16() > 0xEFFF)
TheFella 5:d6da74ac1cc3 83 {
TheFella 5:d6da74ac1cc3 84 led2 = LEDON;
TheFella 5:d6da74ac1cc3 85 }
TheFella 5:d6da74ac1cc3 86 else
TheFella 5:d6da74ac1cc3 87 {
TheFella 5:d6da74ac1cc3 88 led2 = LEDOFF;
TheFella 5:d6da74ac1cc3 89 }
TheFella 5:d6da74ac1cc3 90
TheFella 5:d6da74ac1cc3 91 pc_mutex.unlock();
4180_1 4:79863d2ea5a0 92 }
TheFella 5:d6da74ac1cc3 93 Thread::wait(500); // value of pot1 / 100
dreschpe 1:1c6a9eaf55b5 94 }
4180_1 4:79863d2ea5a0 95
dreschpe 0:f6a57b843f79 96 int main()
dreschpe 0:f6a57b843f79 97 {
dreschpe 2:a69c8c5f5b03 98 Thread t1(thread1); //start thread1
dreschpe 2:a69c8c5f5b03 99 Thread t2(thread2); //start thread2
dreschpe 2:a69c8c5f5b03 100 Thread t4(thread4); //start thread4
4180_1 4:79863d2ea5a0 101 Thread t5(thread5); //start thread5
TheFella 5:d6da74ac1cc3 102
TheFella 8:261f8af23dae 103 while(true)
TheFella 8:261f8af23dae 104 { // main is the next thread
TheFella 5:d6da74ac1cc3 105 pc_mutex.lock();
TheFella 8:261f8af23dae 106 pc.printf("main\n\r");
TheFella 5:d6da74ac1cc3 107 pc_mutex.unlock();
dreschpe 2:a69c8c5f5b03 108 Thread::wait(500); // wait 0.5s
dreschpe 0:f6a57b843f79 109 }
dreschpe 0:f6a57b843f79 110 }