Sample example of using EventFlags in mbed-os threads

Dependencies:   IHM_V2

/media/uploads/jacquesolivierklein/mbed-os-eventflags.png

Purpose: Sample example of using EventFlags in mbed-os threads

that trigger a thread thrgough its EventFlags. Target: L432KC / Nboard (from IUT-Cachan) Tested: YES (2019-01-09) Author: Jacques-Olivier Klein - IUT de CACHAN Date: 2018-02-10 rev. 2019-01-09 OS_STACK_SIZE=4096 Libraries: mbed-os rev5345:c966348(03jan2019) + IHMV2 (from IUT-Cachan) rev6:ad91067

Committer:
jacquesolivierklein
Date:
Wed Jan 09 12:25:49 2019 +0000
Revision:
31:2afcd3a7b307
Parent:
29:76fa085de35e
Child:
32:88edbd7e31e3
update for mbed-os-5.11 (and IHM-V2)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jacquesolivierklein 31:2afcd3a7b307 1 // Title :mbed-os-eventflags (previously mbed-os-signals)
jacquesolivierklein 27:3546959ecd59 2 // Author: Jacques-Olivier Klein - IUT de CACHAN
jacquesolivierklein 31:2afcd3a7b307 3 // Date: 2018-02-11 rev. 2019-01-06
jacquesolivierklein 27:3546959ecd59 4
Jonathan Austin 0:2757d7abb7d9 5 #include "mbed.h"
jacquesolivierklein 27:3546959ecd59 6 #include "IHM.h"
jacquesolivierklein 27:3546959ecd59 7
jacquesolivierklein 27:3546959ecd59 8 IHM ihm;
jacquesolivierklein 31:2afcd3a7b307 9 Mutex bargraph_mutex;
Jonathan Austin 0:2757d7abb7d9 10
jacquesolivierklein 27:3546959ecd59 11 DigitalOut L0 (PB_3) ; // led L0
jacquesolivierklein 27:3546959ecd59 12 DigitalOut L1 (PA_7) ; // led L1
jacquesolivierklein 27:3546959ecd59 13 DigitalOut L2 (PA_6) ; // led L2
jacquesolivierklein 27:3546959ecd59 14 DigitalOut L6 (PA_0) ; // led L6
jacquesolivierklein 27:3546959ecd59 15
jacquesolivierklein 27:3546959ecd59 16 InterruptIn BP0Interrupt(PA_9);
jacquesolivierklein 27:3546959ecd59 17 InterruptIn BP1Interrupt(PA_10);
jacquesolivierklein 27:3546959ecd59 18 InterruptIn BP2Interrupt(PB_0);
jacquesolivierklein 27:3546959ecd59 19 InterruptIn BP3Interrupt(PB_7);
jacquesolivierklein 27:3546959ecd59 20
jacquesolivierklein 29:76fa085de35e 21 void led_thread_signal();
Jonathan Austin 0:2757d7abb7d9 22
jacquesolivierklein 27:3546959ecd59 23 void isrBP0 ();
jacquesolivierklein 27:3546959ecd59 24 void isrBP1 ();
jacquesolivierklein 27:3546959ecd59 25 void isrBP2 ();
jacquesolivierklein 27:3546959ecd59 26 void isrBP3 ();
jacquesolivierklein 27:3546959ecd59 27
jacquesolivierklein 31:2afcd3a7b307 28 Thread thread_signal;
jacquesolivierklein 31:2afcd3a7b307 29
jacquesolivierklein 31:2afcd3a7b307 30 EventFlags my_event_flags;
jacquesolivierklein 27:3546959ecd59 31
jacquesolivierklein 27:3546959ecd59 32 int main(void)
jacquesolivierklein 27:3546959ecd59 33 { ihm.LCD_clear();
jacquesolivierklein 27:3546959ecd59 34 ihm.LCD_printf("signal%s %s",__DATE__,__TIME__);
jacquesolivierklein 27:3546959ecd59 35 printf("\n\rmbed-os-signal\n\r");
jacquesolivierklein 27:3546959ecd59 36 printf("\n\r%s - %s\n\r",__DATE__,__TIME__);
jacquesolivierklein 31:2afcd3a7b307 37 printf("OS_STACK_SIZE:%d\n\r", OS_STACK_SIZE);
jacquesolivierklein 27:3546959ecd59 38 BP0Interrupt.mode(PullUp);
jacquesolivierklein 27:3546959ecd59 39 BP0Interrupt.fall(&isrBP0);
jacquesolivierklein 27:3546959ecd59 40 BP1Interrupt.mode(PullUp);
jacquesolivierklein 27:3546959ecd59 41 BP1Interrupt.fall(&isrBP1);
jacquesolivierklein 27:3546959ecd59 42 BP2Interrupt.mode(PullUp);
jacquesolivierklein 27:3546959ecd59 43 BP2Interrupt.fall(&isrBP2);
jacquesolivierklein 27:3546959ecd59 44 BP3Interrupt.mode(PullUp);
jacquesolivierklein 27:3546959ecd59 45 BP3Interrupt.fall(&isrBP3);
jacquesolivierklein 27:3546959ecd59 46
jacquesolivierklein 29:76fa085de35e 47 thread_signal.start(led_thread_signal);
jacquesolivierklein 27:3546959ecd59 48
jacquesolivierklein 27:3546959ecd59 49 while(1){
jacquesolivierklein 31:2afcd3a7b307 50 wait(4.000);
jacquesolivierklein 27:3546959ecd59 51 L6=!L6;
jacquesolivierklein 31:2afcd3a7b307 52 printf("M [pid-%d]Main \n\r",osThreadGetId());
Jonathan Austin 0:2757d7abb7d9 53 }
Jonathan Austin 0:2757d7abb7d9 54 }
Jonathan Austin 1:846c97078558 55
jacquesolivierklein 29:76fa085de35e 56 void led_thread_signal(){
jacquesolivierklein 31:2afcd3a7b307 57 uint32_t flags;
jacquesolivierklein 27:3546959ecd59 58 while (true) {
jacquesolivierklein 31:2afcd3a7b307 59 flags = my_event_flags.wait_any(0x3); // wait for 1 or 2
jacquesolivierklein 31:2afcd3a7b307 60 ihm.BAR_set(flags);
jacquesolivierklein 31:2afcd3a7b307 61 printf(" S [pid-%d]led6_thread_signal <%d> 1 or 2 detected \n\r",osThreadGetId(),flags);
jacquesolivierklein 31:2afcd3a7b307 62 flags = my_event_flags.wait_all(0xC); // wait for 4 and 8
jacquesolivierklein 31:2afcd3a7b307 63 ihm.BAR_set(flags);
jacquesolivierklein 31:2afcd3a7b307 64 printf(" S [pid-%d]led6_thread_signal <%d> 4 and 8 detected \n\r",osThreadGetId(),flags);
jacquesolivierklein 27:3546959ecd59 65 }
jacquesolivierklein 27:3546959ecd59 66 }
jacquesolivierklein 27:3546959ecd59 67
jacquesolivierklein 27:3546959ecd59 68 void isrBP0 (void){
jacquesolivierklein 31:2afcd3a7b307 69 my_event_flags.set(0x1);
jacquesolivierklein 27:3546959ecd59 70 }
jacquesolivierklein 27:3546959ecd59 71
jacquesolivierklein 27:3546959ecd59 72 void isrBP1 (void){
jacquesolivierklein 31:2afcd3a7b307 73 my_event_flags.set(0x2);
jacquesolivierklein 27:3546959ecd59 74 }
jacquesolivierklein 27:3546959ecd59 75
jacquesolivierklein 27:3546959ecd59 76 void isrBP2 (void){
jacquesolivierklein 31:2afcd3a7b307 77 my_event_flags.set(0x4);
jacquesolivierklein 27:3546959ecd59 78 }
jacquesolivierklein 27:3546959ecd59 79
jacquesolivierklein 27:3546959ecd59 80 void isrBP3 (void){
jacquesolivierklein 31:2afcd3a7b307 81 my_event_flags.set(0x8);
jacquesolivierklein 27:3546959ecd59 82 }