
gdfgdh
Dependencies: ELEC350-Practicals-FZ429
Fork of Task633-mbedos-FZ429 by
main.cpp@9:31031bbb59c7, 2016-03-21 (annotated)
- Committer:
- noutram
- Date:
- Mon Mar 21 13:50:11 2016 +0000
- Revision:
- 9:31031bbb59c7
- Parent:
- 8:c5663f5fa848
- Child:
- 10:3a3d2a571c8f
Task631 - simple message queue between an interrupt and a thread
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:f916cefba2f4 | 1 | #include "mbed.h" |
noutram | 0:f916cefba2f4 | 2 | #include "rtos.h" |
noutram | 2:70084af839d3 | 3 | #include "string.h" |
noutram | 2:70084af839d3 | 4 | #include <stdio.h> |
noutram | 2:70084af839d3 | 5 | #include <ctype.h> |
noutram | 0:f916cefba2f4 | 6 | |
noutram | 8:c5663f5fa848 | 7 | #define SWITCH1_RELEASE 1 |
noutram | 8:c5663f5fa848 | 8 | |
noutram | 8:c5663f5fa848 | 9 | void thread1( const void* ); |
noutram | 8:c5663f5fa848 | 10 | void thread2( const void* ); |
noutram | 8:c5663f5fa848 | 11 | void switchISR(); |
noutram | 0:f916cefba2f4 | 12 | |
noutram | 9:31031bbb59c7 | 13 | //Analogue inputs |
noutram | 9:31031bbb59c7 | 14 | AnalogIn adcIn(A0); |
noutram | 9:31031bbb59c7 | 15 | |
noutram | 0:f916cefba2f4 | 16 | //Digital outputs |
noutram | 0:f916cefba2f4 | 17 | DigitalOut onBoardLED(LED1); |
noutram | 0:f916cefba2f4 | 18 | DigitalOut redLED(D7); |
noutram | 0:f916cefba2f4 | 19 | DigitalOut yellowLED(D6); |
noutram | 0:f916cefba2f4 | 20 | DigitalOut greenLED(D5); |
noutram | 0:f916cefba2f4 | 21 | |
noutram | 0:f916cefba2f4 | 22 | //Digital inputs |
noutram | 0:f916cefba2f4 | 23 | DigitalIn onBoardSwitch(USER_BUTTON); |
noutram | 8:c5663f5fa848 | 24 | DigitalIn sw1(D4); //CONSIDER CHANGING THIS TO AN INTERRUPT |
noutram | 8:c5663f5fa848 | 25 | DigitalIn sw2(D3); |
noutram | 8:c5663f5fa848 | 26 | |
noutram | 8:c5663f5fa848 | 27 | //Threads |
noutram | 8:c5663f5fa848 | 28 | Thread *t1; |
noutram | 0:f916cefba2f4 | 29 | |
noutram | 9:31031bbb59c7 | 30 | //Queues - "A message can be a integer or pointer value to a certain type T that is sent to a thread or interrupt service routine." |
noutram | 9:31031bbb59c7 | 31 | Queue<uint32_t, 5> *queue; |
noutram | 0:f916cefba2f4 | 32 | |
noutram | 8:c5663f5fa848 | 33 | |
noutram | 9:31031bbb59c7 | 34 | // Call this on precise intervals |
noutram | 9:31031bbb59c7 | 35 | void adcISR() { |
noutram | 9:31031bbb59c7 | 36 | |
noutram | 9:31031bbb59c7 | 37 | //Option to starve the queue |
noutram | 9:31031bbb59c7 | 38 | if (sw2 == 1) return; |
noutram | 9:31031bbb59c7 | 39 | |
noutram | 9:31031bbb59c7 | 40 | //Read sample - make a copy |
noutram | 9:31031bbb59c7 | 41 | uint32_t sample = (uint32_t)(4095*adcIn.read()); |
noutram | 9:31031bbb59c7 | 42 | |
noutram | 9:31031bbb59c7 | 43 | //Write to queue |
noutram | 9:31031bbb59c7 | 44 | osStatus stat = queue->put((uint32_t*)sample); |
noutram | 9:31031bbb59c7 | 45 | |
noutram | 9:31031bbb59c7 | 46 | //Check if succesful |
noutram | 9:31031bbb59c7 | 47 | if (stat == osErrorResource) { |
noutram | 9:31031bbb59c7 | 48 | redLED = 1; |
noutram | 9:31031bbb59c7 | 49 | printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat); |
noutram | 9:31031bbb59c7 | 50 | } |
noutram | 9:31031bbb59c7 | 51 | |
noutram | 8:c5663f5fa848 | 52 | } |
noutram | 8:c5663f5fa848 | 53 | |
noutram | 9:31031bbb59c7 | 54 | //Normal priority thread (consumer) |
noutram | 4:dae8898e55fe | 55 | void thread1( const void* arg ) |
noutram | 9:31031bbb59c7 | 56 | { |
noutram | 4:dae8898e55fe | 57 | while (true) { |
noutram | 9:31031bbb59c7 | 58 | //Read queue - block (with timeout) |
noutram | 9:31031bbb59c7 | 59 | osEvent evt = queue->get(5000); //With timeout |
noutram | 9:31031bbb59c7 | 60 | |
noutram | 9:31031bbb59c7 | 61 | //Check status of get() |
noutram | 9:31031bbb59c7 | 62 | switch (evt.status) { |
noutram | 9:31031bbb59c7 | 63 | case osEventMessage: |
noutram | 9:31031bbb59c7 | 64 | //Normal status |
noutram | 9:31031bbb59c7 | 65 | printf("value = %d\n\r", evt.value.v); |
noutram | 9:31031bbb59c7 | 66 | greenLED = !greenLED; |
noutram | 9:31031bbb59c7 | 67 | break; |
noutram | 9:31031bbb59c7 | 68 | case osEventTimeout: |
noutram | 9:31031bbb59c7 | 69 | //Timeout |
noutram | 9:31031bbb59c7 | 70 | printf("queue->get() returned %02x status (timeout)\n\r", evt.status); |
noutram | 9:31031bbb59c7 | 71 | break; |
noutram | 9:31031bbb59c7 | 72 | default: |
noutram | 9:31031bbb59c7 | 73 | //All other errors (see cmsis_os.h for meaning of error code) |
noutram | 9:31031bbb59c7 | 74 | printf("queue->get() returned %02x status\n\r", evt.status); |
noutram | 9:31031bbb59c7 | 75 | break; |
noutram | 9:31031bbb59c7 | 76 | } |
noutram | 9:31031bbb59c7 | 77 | |
noutram | 9:31031bbb59c7 | 78 | //Block up consumer if switch is held down |
noutram | 9:31031bbb59c7 | 79 | //Will fill the queue if held long enough |
noutram | 9:31031bbb59c7 | 80 | while (sw1 == 1); |
noutram | 9:31031bbb59c7 | 81 | |
noutram | 9:31031bbb59c7 | 82 | } //end while |
noutram | 0:f916cefba2f4 | 83 | } |
noutram | 0:f916cefba2f4 | 84 | |
noutram | 4:dae8898e55fe | 85 | |
noutram | 8:c5663f5fa848 | 86 | // Main thread |
noutram | 0:f916cefba2f4 | 87 | int main() { |
noutram | 0:f916cefba2f4 | 88 | redLED = 0; |
noutram | 0:f916cefba2f4 | 89 | yellowLED = 0; |
noutram | 2:70084af839d3 | 90 | greenLED = 0; |
noutram | 8:c5663f5fa848 | 91 | |
noutram | 9:31031bbb59c7 | 92 | //Start message |
noutram | 9:31031bbb59c7 | 93 | printf("Welcome\n"); |
noutram | 9:31031bbb59c7 | 94 | |
noutram | 9:31031bbb59c7 | 95 | //Queue |
noutram | 9:31031bbb59c7 | 96 | queue = new Queue<uint32_t,5>(); |
noutram | 9:31031bbb59c7 | 97 | |
noutram | 9:31031bbb59c7 | 98 | //Hook up timer interrupt |
noutram | 9:31031bbb59c7 | 99 | Ticker timer; |
noutram | 9:31031bbb59c7 | 100 | timer.attach(&adcISR, 1.0); |
noutram | 9:31031bbb59c7 | 101 | |
noutram | 8:c5663f5fa848 | 102 | //Threads |
noutram | 9:31031bbb59c7 | 103 | t1 = new Thread(&thread1); |
noutram | 7:cd015e83995a | 104 | |
noutram | 9:31031bbb59c7 | 105 | printf("Main Thread\n"); |
noutram | 2:70084af839d3 | 106 | while (true) { |
noutram | 9:31031bbb59c7 | 107 | Thread::wait(5000); |
noutram | 9:31031bbb59c7 | 108 | puts("Main Thread Alive"); |
noutram | 0:f916cefba2f4 | 109 | } |
noutram | 0:f916cefba2f4 | 110 | } |
noutram | 2:70084af839d3 | 111 | |
noutram | 2:70084af839d3 | 112 |