Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Task631-mbedos54 by
main.cpp@10:624be54e5299, 2017-04-03 (annotated)
- Committer:
- noutram
- Date:
- Mon Apr 03 14:11:54 2017 +0000
- Revision:
- 10:624be54e5299
- Parent:
- 9:31031bbb59c7
updated for mbed os 5.4
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 | 10:624be54e5299 | 9 | void thread1(); |
| noutram | 10:624be54e5299 | 10 | void thread2(); |
| 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 | 10:624be54e5299 | 55 | void thread1() |
| 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 | 10:624be54e5299 | 103 | t1 = new Thread(); |
| noutram | 10:624be54e5299 | 104 | t1->start(thread1); |
| noutram | 7:cd015e83995a | 105 | |
| noutram | 9:31031bbb59c7 | 106 | printf("Main Thread\n"); |
| noutram | 2:70084af839d3 | 107 | while (true) { |
| noutram | 9:31031bbb59c7 | 108 | Thread::wait(5000); |
| noutram | 9:31031bbb59c7 | 109 | puts("Main Thread Alive"); |
| noutram | 0:f916cefba2f4 | 110 | } |
| noutram | 0:f916cefba2f4 | 111 | } |
| noutram | 2:70084af839d3 | 112 | |
| noutram | 2:70084af839d3 | 113 |