Memory pools

Dependencies:   ELEC350-Practicals-FZ429

Fork of Task632-mbedos54 by Nicholas Outram

Committer:
noutram
Date:
Tue Nov 14 15:36:59 2017 +0000
Revision:
12:087a7d135bf4
Parent:
11:6cfaf7dfecb9
Converted to F429ZI

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noutram 0:f916cefba2f4 1 #include "mbed.h"
noutram 2:70084af839d3 2 #include "string.h"
noutram 2:70084af839d3 3 #include <stdio.h>
noutram 2:70084af839d3 4 #include <ctype.h>
noutram 12:087a7d135bf4 5 #include "sample_hardware.hpp"
noutram 0:f916cefba2f4 6
noutram 8:c5663f5fa848 7 #define SWITCH1_RELEASE 1
noutram 8:c5663f5fa848 8
noutram 11:6cfaf7dfecb9 9 void thread1();
noutram 11:6cfaf7dfecb9 10 void thread2();
noutram 8:c5663f5fa848 11 void switchISR();
noutram 0:f916cefba2f4 12
noutram 8:c5663f5fa848 13
noutram 8:c5663f5fa848 14 //Threads
noutram 12:087a7d135bf4 15 Thread t1;
noutram 0:f916cefba2f4 16
noutram 10:3a3d2a571c8f 17 //Class type
noutram 10:3a3d2a571c8f 18 class message_t {
noutram 10:3a3d2a571c8f 19 public:
noutram 10:3a3d2a571c8f 20 float adcValue;
noutram 10:3a3d2a571c8f 21 int sw1State;
noutram 10:3a3d2a571c8f 22 int sw2State;
noutram 10:3a3d2a571c8f 23
noutram 10:3a3d2a571c8f 24 //Constructor
noutram 10:3a3d2a571c8f 25 message_t(float f, int s1, int s2) {
noutram 10:3a3d2a571c8f 26 adcValue = f;
noutram 10:3a3d2a571c8f 27 sw1State = s1;
noutram 10:3a3d2a571c8f 28 sw2State = s2;
noutram 10:3a3d2a571c8f 29 }
noutram 10:3a3d2a571c8f 30 };
noutram 10:3a3d2a571c8f 31
noutram 10:3a3d2a571c8f 32 //Memory Pool - with capacity for 16 message_t types
noutram 10:3a3d2a571c8f 33 MemoryPool<message_t, 16> mpool;
noutram 0:f916cefba2f4 34
noutram 10:3a3d2a571c8f 35 //Message queue - matched to the memory pool
noutram 10:3a3d2a571c8f 36 Queue<message_t, 16> queue;
noutram 8:c5663f5fa848 37
noutram 9:31031bbb59c7 38 // Call this on precise intervals
noutram 9:31031bbb59c7 39 void adcISR() {
noutram 9:31031bbb59c7 40
noutram 10:3a3d2a571c8f 41 //Read sample - make a copy
noutram 10:3a3d2a571c8f 42 float sample = adcIn;
noutram 10:3a3d2a571c8f 43 //Grab switch state
noutram 12:087a7d135bf4 44 uint32_t switch1State = SW1;
noutram 12:087a7d135bf4 45 uint32_t switch2State = SW2;
noutram 9:31031bbb59c7 46
noutram 10:3a3d2a571c8f 47 //Allocate a block from the memory pool
noutram 10:3a3d2a571c8f 48 message_t *message = mpool.alloc();
noutram 10:3a3d2a571c8f 49 if (message == NULL) {
noutram 10:3a3d2a571c8f 50 //Out of memory
noutram 10:3a3d2a571c8f 51 redLED = 1;
noutram 10:3a3d2a571c8f 52 return;
noutram 10:3a3d2a571c8f 53 }
noutram 10:3a3d2a571c8f 54
noutram 10:3a3d2a571c8f 55 //Fill in the data
noutram 10:3a3d2a571c8f 56 message->adcValue = sample;
noutram 10:3a3d2a571c8f 57 message->sw1State = switch1State;
noutram 10:3a3d2a571c8f 58 message->sw2State = switch2State;
noutram 9:31031bbb59c7 59
noutram 9:31031bbb59c7 60 //Write to queue
noutram 10:3a3d2a571c8f 61 osStatus stat = queue.put(message); //Note we are sending the "pointer"
noutram 9:31031bbb59c7 62
noutram 9:31031bbb59c7 63 //Check if succesful
noutram 9:31031bbb59c7 64 if (stat == osErrorResource) {
noutram 9:31031bbb59c7 65 redLED = 1;
noutram 9:31031bbb59c7 66 printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat);
noutram 10:3a3d2a571c8f 67 mpool.free(message);
noutram 10:3a3d2a571c8f 68 return;
noutram 9:31031bbb59c7 69 }
noutram 8:c5663f5fa848 70 }
noutram 8:c5663f5fa848 71
noutram 9:31031bbb59c7 72 //Normal priority thread (consumer)
noutram 11:6cfaf7dfecb9 73 void thread1()
noutram 10:3a3d2a571c8f 74 {
noutram 4:dae8898e55fe 75 while (true) {
noutram 10:3a3d2a571c8f 76 //Block on the queue
noutram 10:3a3d2a571c8f 77 osEvent evt = queue.get();
noutram 9:31031bbb59c7 78
noutram 10:3a3d2a571c8f 79 //Check status
noutram 10:3a3d2a571c8f 80 if (evt.status == osEventMessage) {
noutram 10:3a3d2a571c8f 81 message_t *pMessage = (message_t*)evt.value.p; //This is the pointer (address)
noutram 10:3a3d2a571c8f 82 //Make a copy
noutram 10:3a3d2a571c8f 83 message_t msg(pMessage->adcValue, pMessage->sw1State, pMessage->sw2State);
noutram 10:3a3d2a571c8f 84 //We are done with this, so give back the memory to the pool
noutram 10:3a3d2a571c8f 85 mpool.free(pMessage);
noutram 10:3a3d2a571c8f 86
noutram 10:3a3d2a571c8f 87 //Echo to the terminal
noutram 10:3a3d2a571c8f 88 printf("ADC Value: %.2f\t", msg.adcValue);
noutram 10:3a3d2a571c8f 89 printf("SW1: %u\t", msg.sw1State);
noutram 10:3a3d2a571c8f 90 printf("SW2: %u\n\r", msg.sw2State);
noutram 9:31031bbb59c7 91 }
noutram 10:3a3d2a571c8f 92
noutram 10:3a3d2a571c8f 93
noutram 9:31031bbb59c7 94 } //end while
noutram 0:f916cefba2f4 95 }
noutram 0:f916cefba2f4 96
noutram 4:dae8898e55fe 97
noutram 8:c5663f5fa848 98 // Main thread
noutram 0:f916cefba2f4 99 int main() {
noutram 0:f916cefba2f4 100 redLED = 0;
noutram 0:f916cefba2f4 101 yellowLED = 0;
noutram 2:70084af839d3 102 greenLED = 0;
noutram 8:c5663f5fa848 103
noutram 9:31031bbb59c7 104 //Start message
noutram 10:3a3d2a571c8f 105 printf("Welcome\n");
noutram 10:3a3d2a571c8f 106
noutram 9:31031bbb59c7 107 //Hook up timer interrupt
noutram 9:31031bbb59c7 108 Ticker timer;
noutram 10:3a3d2a571c8f 109 timer.attach(&adcISR, 0.1);
noutram 9:31031bbb59c7 110
noutram 8:c5663f5fa848 111 //Threads
noutram 12:087a7d135bf4 112 t1.start(thread1);
noutram 7:cd015e83995a 113
noutram 9:31031bbb59c7 114 printf("Main Thread\n");
noutram 2:70084af839d3 115 while (true) {
noutram 9:31031bbb59c7 116 Thread::wait(5000);
noutram 9:31031bbb59c7 117 puts("Main Thread Alive");
noutram 0:f916cefba2f4 118 }
noutram 0:f916cefba2f4 119 }