Queues
Dependencies: ELEC350-Practicals-FZ429
Fork of Task631-mbedos54 by
main.cpp@15:25f524c156ef, 2019-11-15 (annotated)
- Committer:
- noutram
- Date:
- Fri Nov 15 16:06:49 2019 +0000
- Revision:
- 15:25f524c156ef
- Parent:
- 14:1e4ce7eb6d1e
Fixed pin assignment
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:f916cefba2f4 | 1 | #include "mbed.h" |
noutram | 12:43370561e925 | 2 | #include "sample_hardware.hpp" |
noutram | 12:43370561e925 | 3 | |
noutram | 2:70084af839d3 | 4 | #include "string.h" |
noutram | 2:70084af839d3 | 5 | #include <stdio.h> |
noutram | 2:70084af839d3 | 6 | #include <ctype.h> |
noutram | 0:f916cefba2f4 | 7 | |
noutram | 8:c5663f5fa848 | 8 | #define SWITCH1_RELEASE 1 |
noutram | 8:c5663f5fa848 | 9 | |
noutram | 10:624be54e5299 | 10 | void thread1(); |
noutram | 10:624be54e5299 | 11 | void thread2(); |
noutram | 8:c5663f5fa848 | 12 | void switchISR(); |
noutram | 0:f916cefba2f4 | 13 | |
noutram | 8:c5663f5fa848 | 14 | //Threads |
noutram | 12:43370561e925 | 15 | Thread t1; |
noutram | 0:f916cefba2f4 | 16 | |
noutram | 9:31031bbb59c7 | 17 | //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 | 12:43370561e925 | 18 | Queue<uint32_t, 5> queue; |
noutram | 0:f916cefba2f4 | 19 | |
noutram | 9:31031bbb59c7 | 20 | // Call this on precise intervals |
noutram | 9:31031bbb59c7 | 21 | void adcISR() { |
noutram | 9:31031bbb59c7 | 22 | |
noutram | 9:31031bbb59c7 | 23 | //Option to starve the queue |
noutram | 12:43370561e925 | 24 | if (SW2 == 1) return; |
noutram | 9:31031bbb59c7 | 25 | |
noutram | 9:31031bbb59c7 | 26 | //Read sample - make a copy |
noutram | 9:31031bbb59c7 | 27 | uint32_t sample = (uint32_t)(4095*adcIn.read()); |
noutram | 9:31031bbb59c7 | 28 | |
noutram | 9:31031bbb59c7 | 29 | //Write to queue |
noutram | 12:43370561e925 | 30 | osStatus stat = queue.put((uint32_t*)sample); |
noutram | 9:31031bbb59c7 | 31 | |
noutram | 9:31031bbb59c7 | 32 | //Check if succesful |
noutram | 9:31031bbb59c7 | 33 | if (stat == osErrorResource) { |
noutram | 9:31031bbb59c7 | 34 | redLED = 1; |
noutram | 9:31031bbb59c7 | 35 | printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat); |
noutram | 9:31031bbb59c7 | 36 | } |
noutram | 9:31031bbb59c7 | 37 | |
noutram | 8:c5663f5fa848 | 38 | } |
noutram | 8:c5663f5fa848 | 39 | |
noutram | 9:31031bbb59c7 | 40 | //Normal priority thread (consumer) |
noutram | 10:624be54e5299 | 41 | void thread1() |
noutram | 9:31031bbb59c7 | 42 | { |
noutram | 4:dae8898e55fe | 43 | while (true) { |
noutram | 9:31031bbb59c7 | 44 | //Read queue - block (with timeout) |
noutram | 12:43370561e925 | 45 | osEvent evt = queue.get(5000); //With timeout |
noutram | 9:31031bbb59c7 | 46 | |
noutram | 9:31031bbb59c7 | 47 | //Check status of get() |
noutram | 9:31031bbb59c7 | 48 | switch (evt.status) { |
noutram | 9:31031bbb59c7 | 49 | case osEventMessage: |
noutram | 9:31031bbb59c7 | 50 | //Normal status |
noutram | 9:31031bbb59c7 | 51 | printf("value = %d\n\r", evt.value.v); |
noutram | 9:31031bbb59c7 | 52 | greenLED = !greenLED; |
noutram | 9:31031bbb59c7 | 53 | break; |
noutram | 9:31031bbb59c7 | 54 | case osEventTimeout: |
noutram | 9:31031bbb59c7 | 55 | //Timeout |
noutram | 14:1e4ce7eb6d1e | 56 | printf("queue.get() returned %02x status (timeout)\n\r", evt.status); |
noutram | 9:31031bbb59c7 | 57 | break; |
noutram | 9:31031bbb59c7 | 58 | default: |
noutram | 9:31031bbb59c7 | 59 | //All other errors (see cmsis_os.h for meaning of error code) |
noutram | 14:1e4ce7eb6d1e | 60 | printf("queue.get() returned %02x status\n\r", evt.status); |
noutram | 9:31031bbb59c7 | 61 | break; |
noutram | 9:31031bbb59c7 | 62 | } |
noutram | 9:31031bbb59c7 | 63 | |
noutram | 9:31031bbb59c7 | 64 | //Block up consumer if switch is held down |
noutram | 9:31031bbb59c7 | 65 | //Will fill the queue if held long enough |
noutram | 12:43370561e925 | 66 | while (SW1 == 1); |
noutram | 9:31031bbb59c7 | 67 | |
noutram | 9:31031bbb59c7 | 68 | } //end while |
noutram | 0:f916cefba2f4 | 69 | } |
noutram | 0:f916cefba2f4 | 70 | |
noutram | 4:dae8898e55fe | 71 | |
noutram | 8:c5663f5fa848 | 72 | // Main thread |
noutram | 0:f916cefba2f4 | 73 | int main() { |
noutram | 13:ab9f53427355 | 74 | post(); |
noutram | 8:c5663f5fa848 | 75 | |
noutram | 9:31031bbb59c7 | 76 | //Start message |
noutram | 9:31031bbb59c7 | 77 | printf("Welcome\n"); |
noutram | 9:31031bbb59c7 | 78 | |
noutram | 9:31031bbb59c7 | 79 | //Hook up timer interrupt |
noutram | 9:31031bbb59c7 | 80 | Ticker timer; |
noutram | 9:31031bbb59c7 | 81 | timer.attach(&adcISR, 1.0); |
noutram | 9:31031bbb59c7 | 82 | |
noutram | 8:c5663f5fa848 | 83 | //Threads |
noutram | 12:43370561e925 | 84 | t1.start(thread1); |
noutram | 7:cd015e83995a | 85 | |
noutram | 9:31031bbb59c7 | 86 | printf("Main Thread\n"); |
noutram | 2:70084af839d3 | 87 | while (true) { |
noutram | 9:31031bbb59c7 | 88 | Thread::wait(5000); |
noutram | 9:31031bbb59c7 | 89 | puts("Main Thread Alive"); |
noutram | 0:f916cefba2f4 | 90 | } |
noutram | 0:f916cefba2f4 | 91 | } |
noutram | 2:70084af839d3 | 92 | |
noutram | 2:70084af839d3 | 93 |