RTOS example - inter-task communication by using a message queue. One task provides numbers and sends them to a second task which displays them on a 4-digit 7-segment LED tube driven by two 74HC595 shift registers. Hardware: NUCLEO_F446RE board with Arduino Multifunction Shield. The ShiftOut Library by Ollie Milton …

Dependencies:   mbed mbed-rtos ShiftOut

Committer:
cspista
Date:
Thu Feb 24 07:59:55 2022 +0000
Revision:
0:fb9706dc4816
final version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cspista 0:fb9706dc4816 1 #include "mbed.h"
cspista 0:fb9706dc4816 2 #include "rtos.h"
cspista 0:fb9706dc4816 3 #include "ShiftOut.h" // Ollie Milton, https://os.mbed.com/users/ollie8/code/ShiftOut/
cspista 0:fb9706dc4816 4
cspista 0:fb9706dc4816 5 DigitalOut buzzer(D3);
cspista 0:fb9706dc4816 6 Serial pc(USBTX,USBRX); //UART via ST-Link
cspista 0:fb9706dc4816 7
cspista 0:fb9706dc4816 8 typedef uint32_t message_t;
cspista 0:fb9706dc4816 9 Queue <message_t, 4> queue;
cspista 0:fb9706dc4816 10
cspista 0:fb9706dc4816 11 void display_thread(void const *argument)
cspista 0:fb9706dc4816 12 {
cspista 0:fb9706dc4816 13 ShiftOut display(D7, D8, D4); // clk=D7, data=D8, latch=D4, 16)
cspista 0:fb9706dc4816 14 uint8_t segment_data[4] = {0xFF, 0xFF, 0xFF, 0xFF}; // A frissítendő szegmenskép
cspista 0:fb9706dc4816 15 /* Számjegyek (0 – 9) szegmensképe, negatív logikával */
cspista 0:fb9706dc4816 16 const uint8_t SEGMENT_MAP[]= {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
cspista 0:fb9706dc4816 17 /* Számjegy (1 - 4) kiválasztó jelek */
cspista 0:fb9706dc4816 18 const uint8_t SEGMENT_SELECT[] = {0x01, 0x02, 0x04, 0x08};
cspista 0:fb9706dc4816 19 uint8_t i, idx = 0;
cspista 0:fb9706dc4816 20 while (true) {
cspista 0:fb9706dc4816 21 osEvent evt = queue.get(0); // Check for a message without waiting
cspista 0:fb9706dc4816 22 switch(evt.status) {
cspista 0:fb9706dc4816 23 case osEventMessage:
cspista 0:fb9706dc4816 24 printf("osEventMessage = %#05x\n",evt.value.v); //message arrived
cspista 0:fb9706dc4816 25 int n = evt.value.v;
cspista 0:fb9706dc4816 26 segment_data[0] = SEGMENT_MAP[(n/1000) %10];
cspista 0:fb9706dc4816 27 segment_data[1] = SEGMENT_MAP[(n/100) % 10];
cspista 0:fb9706dc4816 28 segment_data[2] = SEGMENT_MAP[(n/10) % 10];
cspista 0:fb9706dc4816 29 segment_data[3] = SEGMENT_MAP[n % 10];
cspista 0:fb9706dc4816 30 break;
cspista 0:fb9706dc4816 31 case osOK:
cspista 0:fb9706dc4816 32 //--- pc.printf("osOK\n"); //no error, no message arrived
cspista 0:fb9706dc4816 33 i = idx++ & 0x03;
cspista 0:fb9706dc4816 34 display.write(segment_data[i]);
cspista 0:fb9706dc4816 35 display.write(SEGMENT_SELECT[i]);
cspista 0:fb9706dc4816 36 break;
cspista 0:fb9706dc4816 37 case osEventTimeout:
cspista 0:fb9706dc4816 38 pc.printf("osEventTimeout\n"); //timeout occurred
cspista 0:fb9706dc4816 39 break;
cspista 0:fb9706dc4816 40 case osErrorParameter:
cspista 0:fb9706dc4816 41 pc.printf("osErrorParameter\n");
cspista 0:fb9706dc4816 42 break; //invalid parameter or is out of range.
cspista 0:fb9706dc4816 43 default:
cspista 0:fb9706dc4816 44 pc.printf("Unknown error flag: %#08x\n",(uint32_t)evt.status);
cspista 0:fb9706dc4816 45 break;
cspista 0:fb9706dc4816 46 };
cspista 0:fb9706dc4816 47 Thread::wait(2);
cspista 0:fb9706dc4816 48 }
cspista 0:fb9706dc4816 49 }
cspista 0:fb9706dc4816 50
cspista 0:fb9706dc4816 51 int main (void)
cspista 0:fb9706dc4816 52 {
cspista 0:fb9706dc4816 53 uint16_t raw;
cspista 0:fb9706dc4816 54 Thread thread2(display_thread);
cspista 0:fb9706dc4816 55 pc.baud(115200);
cspista 0:fb9706dc4816 56 buzzer = true;
cspista 0:fb9706dc4816 57 while (true) {
cspista 0:fb9706dc4816 58 Thread::wait(1000);
cspista 0:fb9706dc4816 59 raw++;
cspista 0:fb9706dc4816 60 queue.put((message_t*)raw);
cspista 0:fb9706dc4816 61 }
cspista 0:fb9706dc4816 62 }