DEMONSTRATION OF SIGNAL (EVENT & FLAG) in MBED RTOS. One Thread set particular signal and another thread receive it and proceed according signal state.

Committer:
radhey04ec
Date:
Wed Aug 05 07:37:47 2020 +0000
Revision:
1:9dbbb3022957
Parent:
0:747de80ae2bd
Demonstration of EVENT & FLAG , SIGNAL mechanism in MBED RTOS.; PLATFORM : MBED /RTOS; HARDWARE : STM32; PROFILE: SIGNAL,EVENT,FLAG;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
radhey04ec 0:747de80ae2bd 1 /* SIGNAL USAGES IN RTOS
radhey04ec 0:747de80ae2bd 2 What is Signal ? - Answer : Remember these five points
radhey04ec 0:747de80ae2bd 3
radhey04ec 0:747de80ae2bd 4 1) Signals are different from all the other types of kernel object in that they are not autonomous (user need to control or set - No automatioc control like semaphore)
radhey04ec 0:747de80ae2bd 5 2) signals are associated with tasks/Threads and have no independent existence.
radhey04ec 0:747de80ae2bd 6 3) If signals are configured for an application, each task has a set of eight signal flags.
radhey04ec 0:747de80ae2bd 7
radhey04ec 0:747de80ae2bd 8 4) Any task can set the signals of another task. Only the owner task can read the signals.
radhey04ec 0:747de80ae2bd 9 5) The read is destructive – i.e. the signals are cleared by the process of reading. No other task can read or clear a task’s signals.
radhey04ec 0:747de80ae2bd 10
radhey04ec 0:747de80ae2bd 11
radhey04ec 0:747de80ae2bd 12 In below example there are two threads 1)main thread and 2)Thread that is connected with onBoard_led functions
radhey04ec 0:747de80ae2bd 13 We set the signal from main thread , second thread received the signal and toggle the led state.
radhey04ec 0:747de80ae2bd 14
radhey04ec 0:747de80ae2bd 15 Program name : LED BLINKING USING RTOS SIGNAL
radhey04ec 0:747de80ae2bd 16 PLATFORM : STM NUCLEO 64 L476
radhey04ec 0:747de80ae2bd 17 Created by : jaydeep shah
radhey04ec 0:747de80ae2bd 18 email : radhey04ec@gmail.com
radhey04ec 0:747de80ae2bd 19 */
radhey04ec 0:747de80ae2bd 20
radhey04ec 0:747de80ae2bd 21 /* NOTE : WE ARE GOING TO USE EVENT FLAG CLASS METHOD The EventFlags class is used to control event flags or wait for event flags other threads control.
radhey04ec 0:747de80ae2bd 22 */
radhey04ec 0:747de80ae2bd 23 #include "mbed.h"
radhey04ec 0:747de80ae2bd 24 #include "rtos.h"
radhey04ec 0:747de80ae2bd 25
radhey04ec 0:747de80ae2bd 26 DigitalOut led(LED2); //ON BOARD LED
radhey04ec 0:747de80ae2bd 27
radhey04ec 0:747de80ae2bd 28 EventFlags event_flags; // EVESNT FLAG OBJECT CREATED (DOMAIN : SIGNAL)
radhey04ec 0:747de80ae2bd 29
radhey04ec 0:747de80ae2bd 30 //TASK which is related to signal
radhey04ec 0:747de80ae2bd 31 void onBoard_led() {
radhey04ec 1:9dbbb3022957 32 uint32_t read = 0; //CREATE VARIABLE TO STORE DATA
radhey04ec 0:747de80ae2bd 33 while (true) {
radhey04ec 0:747de80ae2bd 34 // Signal flags that are reported as event are automatically cleared -Autonomous object of Kernal
radhey04ec 0:747de80ae2bd 35
radhey04ec 0:747de80ae2bd 36 read = event_flags.wait_any(0x1 | 0x2);
radhey04ec 0:747de80ae2bd 37 if(read == 0x1)
radhey04ec 0:747de80ae2bd 38 {
radhey04ec 0:747de80ae2bd 39 led = !led; //Toggle state
radhey04ec 0:747de80ae2bd 40 }
radhey04ec 0:747de80ae2bd 41 printf("\n Received this flag event 0x%08lx \n\r",read);
radhey04ec 0:747de80ae2bd 42
radhey04ec 0:747de80ae2bd 43 }
radhey04ec 0:747de80ae2bd 44 }
radhey04ec 0:747de80ae2bd 45
radhey04ec 0:747de80ae2bd 46 int main (void) {
radhey04ec 0:747de80ae2bd 47 Thread thread; //create threaed
radhey04ec 0:747de80ae2bd 48
radhey04ec 0:747de80ae2bd 49 thread.start(callback(onBoard_led)); //Thread start
radhey04ec 0:747de80ae2bd 50
radhey04ec 0:747de80ae2bd 51 while (true) {
radhey04ec 0:747de80ae2bd 52 ThisThread::sleep_for(1000); //sleep for 1 sec
radhey04ec 0:747de80ae2bd 53 event_flags.set(0x1); //set the signal for thread task from main thread --LED BLINK TASK
radhey04ec 0:747de80ae2bd 54 ThisThread::sleep_for(1000);
radhey04ec 0:747de80ae2bd 55 event_flags.set(0x2); //This signal for print the statement -- use serial terminal
radhey04ec 0:747de80ae2bd 56 }
radhey04ec 0:747de80ae2bd 57 }