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