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 14:01:36 2019 +0000
Revision:
33:ebb57af47121
Parent:
32:88edbd7e31e3
unused mutex deleted

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