Purpose: Simple application example with 2 threads communicating 5-byte buffers through a Mail event-object.

Dependencies:   IHM_V2

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

/media/uploads/jacquesolivierklein/mbed-os-mail-5-bytes-tx-rx.png

Purpose: Simple application example with 2 threads communicating 5-byte buffers through a Mail event-object. 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:
Sat Feb 10 22:10:15 2018 +0000
Revision:
27:da71f99ce831
Parent:
22:af9dcf379926
Child:
28:9a6aff2af525
initial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jacquesolivierklein 27:da71f99ce831 1 // Title: mbed-os-mail-5-Bytes-tx-rx
jacquesolivierklein 27:da71f99ce831 2 // Author: Jacques-Olivier Klein - IUT de CACHAN
jacquesolivierklein 27:da71f99ce831 3 // Date: 2018-02-10
jacquesolivierklein 27:da71f99ce831 4
Jonathan Austin 0:2757d7abb7d9 5 #include "mbed.h"
jacquesolivierklein 27:da71f99ce831 6 #include "IHM.h"
jacquesolivierklein 27:da71f99ce831 7
jacquesolivierklein 27:da71f99ce831 8 #define MY_MAIL_BUFFER_SIZE 5
jacquesolivierklein 27:da71f99ce831 9 #define MY_MAIL_NBER_OF_BUFFER 4
Jonathan Austin 0:2757d7abb7d9 10
jacquesolivierklein 27:da71f99ce831 11 typedef struct{
jacquesolivierklein 27:da71f99ce831 12 char message [MY_MAIL_BUFFER_SIZE] ;
jacquesolivierklein 27:da71f99ce831 13 } Mail_Buffer_T;
jacquesolivierklein 27:da71f99ce831 14
jacquesolivierklein 27:da71f99ce831 15 IHM ihm;
jacquesolivierklein 27:da71f99ce831 16
jacquesolivierklein 27:da71f99ce831 17 DigitalOut L0 (PB_3) ; // led L0
jacquesolivierklein 27:da71f99ce831 18 DigitalOut L1 (PA_7) ; // led L1
jacquesolivierklein 27:da71f99ce831 19 DigitalOut L2 (PA_6) ; // led L2
jacquesolivierklein 27:da71f99ce831 20
jacquesolivierklein 27:da71f99ce831 21 void TxMail ();
jacquesolivierklein 27:da71f99ce831 22 void RxMail ();
jacquesolivierklein 27:da71f99ce831 23
jacquesolivierklein 27:da71f99ce831 24 Mail<Mail_Buffer_T, MY_MAIL_NBER_OF_BUFFER> my_mail_box;
Jonathan Austin 0:2757d7abb7d9 25
jacquesolivierklein 27:da71f99ce831 26 Ticker my_timer;
jacquesolivierklein 27:da71f99ce831 27 Thread thread2;
jacquesolivierklein 27:da71f99ce831 28 Thread thread3;
jacquesolivierklein 27:da71f99ce831 29 Thread thread_Semaphore;
jacquesolivierklein 27:da71f99ce831 30 Thread thread_rx_q(osPriorityNormal,DEFAULT_STACK_SIZE );
jacquesolivierklein 27:da71f99ce831 31 Thread Thread_TxMail;
jacquesolivierklein 27:da71f99ce831 32 Thread Thread_RxMail(osPriorityNormal,500);
jacquesolivierklein 27:da71f99ce831 33 EventQueue the_event_queue;
jacquesolivierklein 27:da71f99ce831 34
jacquesolivierklein 27:da71f99ce831 35 Semaphore mysemaphore(0);
jacquesolivierklein 27:da71f99ce831 36
jacquesolivierklein 27:da71f99ce831 37 int main(void)
jacquesolivierklein 27:da71f99ce831 38 { ihm.LCD_clear();
jacquesolivierklein 27:da71f99ce831 39 ihm.LCD_printf("Mail-5B-tx-rx-%s %s",__DATE__,__TIME__);
jacquesolivierklein 27:da71f99ce831 40 printf("mbed-os-mail-5-Bytes-tx-rx-%s %s\n\r",__DATE__,__TIME__);
jacquesolivierklein 27:da71f99ce831 41 printf("DEFAULT_STACK_SIZE:%d\n\r", DEFAULT_STACK_SIZE);
jacquesolivierklein 27:da71f99ce831 42
jacquesolivierklein 27:da71f99ce831 43 Thread_TxMail.start(TxMail);
jacquesolivierklein 27:da71f99ce831 44 Thread_RxMail.start(RxMail);
jacquesolivierklein 27:da71f99ce831 45
jacquesolivierklein 27:da71f99ce831 46 while(1){
jacquesolivierklein 27:da71f99ce831 47 Thread::wait(3000);
jacquesolivierklein 27:da71f99ce831 48 L0=!L0;
jacquesolivierklein 27:da71f99ce831 49 printf("M [pid-%d]Main \n\r",Thread::gettid());
Jonathan Austin 0:2757d7abb7d9 50 }
Jonathan Austin 0:2757d7abb7d9 51 }
Jonathan Austin 1:846c97078558 52
jacquesolivierklein 27:da71f99ce831 53 void TxMail (){
jacquesolivierklein 27:da71f99ce831 54 Mail_Buffer_T * p_buffer;
jacquesolivierklein 27:da71f99ce831 55 char c = '!';
jacquesolivierklein 27:da71f99ce831 56 int i;
jacquesolivierklein 27:da71f99ce831 57 while(1){
jacquesolivierklein 27:da71f99ce831 58 p_buffer = (Mail_Buffer_T *) my_mail_box.alloc();
jacquesolivierklein 27:da71f99ce831 59 for(i=0;i<MY_MAIL_BUFFER_SIZE-1;i++){
jacquesolivierklein 27:da71f99ce831 60 p_buffer->message[i]=c++;
jacquesolivierklein 27:da71f99ce831 61 if (c=='}')c='!';
jacquesolivierklein 27:da71f99ce831 62 }
jacquesolivierklein 27:da71f99ce831 63 p_buffer->message[MY_MAIL_BUFFER_SIZE-1]=0;
jacquesolivierklein 27:da71f99ce831 64 printf(" T [pid-%d]TxMail:<%s>\n\r",Thread::gettid(),p_buffer->message);
jacquesolivierklein 27:da71f99ce831 65 my_mail_box.put(p_buffer);
jacquesolivierklein 27:da71f99ce831 66 L1 = ! L1;
jacquesolivierklein 27:da71f99ce831 67 Thread::wait(1000);
jacquesolivierklein 27:da71f99ce831 68 }
jacquesolivierklein 27:da71f99ce831 69 }
jacquesolivierklein 27:da71f99ce831 70
jacquesolivierklein 27:da71f99ce831 71 void RxMail (){
jacquesolivierklein 27:da71f99ce831 72 Mail_Buffer_T * p_mail_buffer;
jacquesolivierklein 27:da71f99ce831 73 Mail_Buffer_T local_buffer;
jacquesolivierklein 27:da71f99ce831 74 while(1){
jacquesolivierklein 27:da71f99ce831 75 osEvent evt = my_mail_box.get();
jacquesolivierklein 27:da71f99ce831 76 L2 = ! L2;
jacquesolivierklein 27:da71f99ce831 77 p_mail_buffer = (Mail_Buffer_T *)evt.value.p;
jacquesolivierklein 27:da71f99ce831 78 memcpy ( &local_buffer,p_mail_buffer, MY_MAIL_BUFFER_SIZE);
jacquesolivierklein 27:da71f99ce831 79 my_mail_box.free(p_mail_buffer);
jacquesolivierklein 27:da71f99ce831 80 printf(" R[pid-%d]RxMail:<%s>\n\r",Thread::gettid(),local_buffer.message);
jacquesolivierklein 27:da71f99ce831 81 }
jacquesolivierklein 27:da71f99ce831 82 }