Learn how to use embedded RTOS (Beta version RTOS)

Dependencies:   mbed-rtos mbed

Committer:
leonardoaraujosantos
Date:
Thu Sep 03 22:11:03 2015 +0000
Revision:
1:4d067c712d03
Parent:
0:5bd42710f2e7
Child:
2:daaf6b6ad568
Adding queue mutex, and stuff

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leonardoaraujosantos 0:5bd42710f2e7 1 #include "mbed.h"
leonardoaraujosantos 0:5bd42710f2e7 2 #include "rtos.h"
leonardoaraujosantos 0:5bd42710f2e7 3
leonardoaraujosantos 0:5bd42710f2e7 4 //------------------------------------
leonardoaraujosantos 0:5bd42710f2e7 5 // Serial configuration (Put on hyperterminal or Putty)
leonardoaraujosantos 0:5bd42710f2e7 6 // 115200 bauds, 8-bit data, no parity
leonardoaraujosantos 0:5bd42710f2e7 7 //------------------------------------
leonardoaraujosantos 0:5bd42710f2e7 8
leonardoaraujosantos 0:5bd42710f2e7 9 Serial serialIO(SERIAL_TX, SERIAL_RX);
leonardoaraujosantos 0:5bd42710f2e7 10
leonardoaraujosantos 0:5bd42710f2e7 11 DigitalOut led2(LED2);
leonardoaraujosantos 1:4d067c712d03 12 DigitalIn button(USER_BUTTON);
leonardoaraujosantos 1:4d067c712d03 13
leonardoaraujosantos 1:4d067c712d03 14 // Some data queue
leonardoaraujosantos 1:4d067c712d03 15 typedef struct {
leonardoaraujosantos 1:4d067c712d03 16 float voltage; /* AD result of measured voltage */
leonardoaraujosantos 1:4d067c712d03 17 uint32_t counter; /* A counter value */
leonardoaraujosantos 1:4d067c712d03 18 } message_t;
leonardoaraujosantos 1:4d067c712d03 19
leonardoaraujosantos 1:4d067c712d03 20 MemoryPool<message_t, 16> mpool;
leonardoaraujosantos 1:4d067c712d03 21 Queue<message_t, 16> queue;
leonardoaraujosantos 1:4d067c712d03 22
leonardoaraujosantos 1:4d067c712d03 23 // Mutex shared by threads that want's to use the serial port
leonardoaraujosantos 1:4d067c712d03 24 Mutex stdio_mutex;
leonardoaraujosantos 1:4d067c712d03 25
leonardoaraujosantos 1:4d067c712d03 26 void led_thread(void const *args)
leonardoaraujosantos 1:4d067c712d03 27 {
leonardoaraujosantos 1:4d067c712d03 28 while (true) {
leonardoaraujosantos 1:4d067c712d03 29 // Toogle led
leonardoaraujosantos 1:4d067c712d03 30 led2 = !led2;
leonardoaraujosantos 1:4d067c712d03 31
leonardoaraujosantos 1:4d067c712d03 32 // To avoid using the serial port at the same time by two threads use the mutex
leonardoaraujosantos 1:4d067c712d03 33 stdio_mutex.lock();
leonardoaraujosantos 1:4d067c712d03 34 serialIO.printf("Toogle led2 from thread1 !\r\n");
leonardoaraujosantos 1:4d067c712d03 35 stdio_mutex.unlock();
leonardoaraujosantos 1:4d067c712d03 36
leonardoaraujosantos 1:4d067c712d03 37 // Put thread in wait state for 500ms
leonardoaraujosantos 1:4d067c712d03 38 Thread::wait(500);
leonardoaraujosantos 1:4d067c712d03 39 }
leonardoaraujosantos 1:4d067c712d03 40 }
leonardoaraujosantos 1:4d067c712d03 41
leonardoaraujosantos 1:4d067c712d03 42 void check_button(void const *args)
leonardoaraujosantos 1:4d067c712d03 43 {
leonardoaraujosantos 1:4d067c712d03 44 while (true) {
leonardoaraujosantos 1:4d067c712d03 45 // Read button
leonardoaraujosantos 1:4d067c712d03 46 int butVal = button;
leonardoaraujosantos 0:5bd42710f2e7 47
leonardoaraujosantos 1:4d067c712d03 48 // To avoid using the serial port at the same time by two threads use the mutex
leonardoaraujosantos 1:4d067c712d03 49 stdio_mutex.lock();
leonardoaraujosantos 1:4d067c712d03 50 serialIO.printf("Button state is:%d\r\n",butVal);
leonardoaraujosantos 1:4d067c712d03 51 stdio_mutex.unlock();
leonardoaraujosantos 1:4d067c712d03 52
leonardoaraujosantos 1:4d067c712d03 53 // Put thread in wait state for 100ms
leonardoaraujosantos 1:4d067c712d03 54 Thread::wait(100);
leonardoaraujosantos 1:4d067c712d03 55 }
leonardoaraujosantos 1:4d067c712d03 56 }
leonardoaraujosantos 1:4d067c712d03 57
leonardoaraujosantos 1:4d067c712d03 58 void data_producer(void const *args)
leonardoaraujosantos 1:4d067c712d03 59 {
leonardoaraujosantos 1:4d067c712d03 60 uint32_t i = 0;
leonardoaraujosantos 0:5bd42710f2e7 61 while (true) {
leonardoaraujosantos 1:4d067c712d03 62 i++; // fake data update
leonardoaraujosantos 1:4d067c712d03 63 message_t *message = mpool.alloc();
leonardoaraujosantos 1:4d067c712d03 64 message->voltage = (i * 0.1) * 33;
leonardoaraujosantos 1:4d067c712d03 65 message->counter = i;
leonardoaraujosantos 1:4d067c712d03 66 queue.put(message);
leonardoaraujosantos 1:4d067c712d03 67 Thread::wait(100);
leonardoaraujosantos 1:4d067c712d03 68 }
leonardoaraujosantos 1:4d067c712d03 69 }
leonardoaraujosantos 1:4d067c712d03 70
leonardoaraujosantos 1:4d067c712d03 71 void data_consumer(void const *args)
leonardoaraujosantos 1:4d067c712d03 72 {
leonardoaraujosantos 1:4d067c712d03 73 while(true) {
leonardoaraujosantos 1:4d067c712d03 74 // Block until there is an event on the queue
leonardoaraujosantos 1:4d067c712d03 75 osEvent evt = queue.get();
leonardoaraujosantos 1:4d067c712d03 76 if (evt.status == osEventMessage) {
leonardoaraujosantos 1:4d067c712d03 77 message_t *message = (message_t*)evt.value.p;
leonardoaraujosantos 1:4d067c712d03 78
leonardoaraujosantos 1:4d067c712d03 79 // Grab mutex to print data
leonardoaraujosantos 1:4d067c712d03 80 stdio_mutex.lock();
leonardoaraujosantos 1:4d067c712d03 81 serialIO.printf("\nVoltage: %.2f V\n\r" , message->voltage);
leonardoaraujosantos 1:4d067c712d03 82 serialIO.printf("Number of cycles: %u\n\r", message->counter);
leonardoaraujosantos 1:4d067c712d03 83 stdio_mutex.unlock();
leonardoaraujosantos 1:4d067c712d03 84
leonardoaraujosantos 1:4d067c712d03 85 mpool.free(message);
leonardoaraujosantos 1:4d067c712d03 86 }
leonardoaraujosantos 0:5bd42710f2e7 87 }
leonardoaraujosantos 0:5bd42710f2e7 88 }
leonardoaraujosantos 0:5bd42710f2e7 89
leonardoaraujosantos 0:5bd42710f2e7 90 int main()
leonardoaraujosantos 1:4d067c712d03 91 {
leonardoaraujosantos 0:5bd42710f2e7 92 // Configure the serial speed.
leonardoaraujosantos 0:5bd42710f2e7 93 serialIO.baud(115200);
leonardoaraujosantos 1:4d067c712d03 94 serialIO.printf("Learning Microcontrollers with mbed !\r\n");
leonardoaraujosantos 1:4d067c712d03 95
leonardoaraujosantos 0:5bd42710f2e7 96 // Start a thread to togle the led
leonardoaraujosantos 1:4d067c712d03 97 Thread thread_1(led_thread);
leonardoaraujosantos 1:4d067c712d03 98 Thread thread_2(check_button);
leonardoaraujosantos 1:4d067c712d03 99 Thread thread_3(data_producer);
leonardoaraujosantos 1:4d067c712d03 100 Thread thread_4(data_consumer);
leonardoaraujosantos 1:4d067c712d03 101
leonardoaraujosantos 0:5bd42710f2e7 102 // Lock forever here...
leonardoaraujosantos 0:5bd42710f2e7 103 while(1) {
leonardoaraujosantos 1:4d067c712d03 104
leonardoaraujosantos 0:5bd42710f2e7 105 }
leonardoaraujosantos 0:5bd42710f2e7 106 }