See graph

Dependencies:   MCP23017 SDFileSystem WattBob_TextLCD mbed

Fork of Embedded_Software_Assignment_2 by Steven Kay

Committer:
sk398
Date:
Wed Mar 02 09:38:47 2016 +0000
Revision:
9:46408a8dea0c
Parent:
8:7f3594882cec
Child:
10:c0531edf4850
All tasks operating and Cyclic Exec working. Just need to implement task 1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sk398 4:b85bc0d810e1 1 /* #####################################################################
sk398 4:b85bc0d810e1 2 main.cpp
sk398 4:b85bc0d810e1 3 ---------
sk398 4:b85bc0d810e1 4
sk398 4:b85bc0d810e1 5 Embedded Software - Assignment 2
sk398 4:b85bc0d810e1 6 --------------------------------
sk398 4:b85bc0d810e1 7
sk398 4:b85bc0d810e1 8 Written by: Steven Kay
sk398 4:b85bc0d810e1 9
sk398 4:b85bc0d810e1 10 Date: February 2016
sk398 4:b85bc0d810e1 11
sk398 4:b85bc0d810e1 12 Function: This
sk398 4:b85bc0d810e1 13
sk398 4:b85bc0d810e1 14 Version: 1.0
sk398 4:b85bc0d810e1 15
sk398 4:b85bc0d810e1 16 Version History
sk398 4:b85bc0d810e1 17 ---------------
sk398 4:b85bc0d810e1 18
sk398 4:b85bc0d810e1 19 1.1 rgdfgdfgdfggdfgdg
sk398 4:b85bc0d810e1 20
sk398 4:b85bc0d810e1 21 1.0 gdgddfdddgd
sk398 4:b85bc0d810e1 22
sk398 4:b85bc0d810e1 23 ##################################################################### */
sk398 2:22ebabd78084 24
sk398 0:5989ac10c4d3 25 #include "mbed.h"
sk398 1:221d677fe0d3 26 #include "Tasks.h"
sk398 0:5989ac10c4d3 27
sk398 7:2973bf297f3d 28 // ============================================================================
sk398 7:2973bf297f3d 29 // Task Declerations
sk398 7:2973bf297f3d 30 // ============================================================================
sk398 6:ceda53939eb8 31 Task1 task1(p11); // Square wave Measurement
sk398 7:2973bf297f3d 32 Task2 task2_switch1(p12); // Read digital Input
sk398 6:ceda53939eb8 33 Task3 task3(p13); // Watchdog Pulse
sk398 6:ceda53939eb8 34 Task4 task4(p15,p16); // Read analog Inputs
sk398 6:ceda53939eb8 35 Task5 task5(p9,p10,0x40); // Output to LCD Display
sk398 6:ceda53939eb8 36 Task6 task6; // Logical checks
sk398 6:ceda53939eb8 37 Task7 task7(p5,p6,p7,p8,"SD","/SD/A2"); // SD Card Write
sk398 0:5989ac10c4d3 38
sk398 7:2973bf297f3d 39 // ============================================================================
sk398 7:2973bf297f3d 40 // Cyclic Executive Objects and Declerations
sk398 7:2973bf297f3d 41 // ============================================================================
sk398 7:2973bf297f3d 42 DigitalOut ErrorLED(LED1); // Error Indicating LED
sk398 7:2973bf297f3d 43 DigitalOut LenPin(p21); // Pulse Pin
sk398 7:2973bf297f3d 44 DigitalIn SDRemoval(p18); // Switch state to indicate to remove SD
sk398 7:2973bf297f3d 45 Timer BusyWait; // Wasted time Timer
sk398 7:2973bf297f3d 46 Ticker CyclicTicker; // Ticker object to cycle tasks
sk398 7:2973bf297f3d 47
sk398 7:2973bf297f3d 48 // ============================================================================
sk398 7:2973bf297f3d 49 // Global Data Parameters used in Cyclic Executive
sk398 7:2973bf297f3d 50 // ============================================================================
sk398 7:2973bf297f3d 51 // Counter to record the number of ticks went through by the Cyclic Executive
sk398 7:2973bf297f3d 52 int cyclicTicks = 1;
sk398 7:2973bf297f3d 53
sk398 7:2973bf297f3d 54 // Global parameter storing the most up to date value of the return from Task 1
sk398 7:2973bf297f3d 55 volatile int task1Frequency;
sk398 7:2973bf297f3d 56
sk398 7:2973bf297f3d 57 // Global parameter storing the most up to date value of the return from Task 2
sk398 7:2973bf297f3d 58 volatile int task2SwitchState;
sk398 7:2973bf297f3d 59
sk398 7:2973bf297f3d 60 // Global parameter storing the most up to date value of the return from Task 4
sk398 7:2973bf297f3d 61 volatile float task4AnalogChannels[2];
sk398 7:2973bf297f3d 62
sk398 7:2973bf297f3d 63 // Global parameter storing the most up to date value of the return from Task 6
sk398 7:2973bf297f3d 64 volatile int task6ErrorState;
sk398 7:2973bf297f3d 65
sk398 7:2973bf297f3d 66 // Char array to store the concatenated string for output onto the SD Card
sk398 7:2973bf297f3d 67 char logData[50];
sk398 7:2973bf297f3d 68
sk398 7:2973bf297f3d 69 int slotCounter = 0;
sk398 7:2973bf297f3d 70 int taskNum = 2;
sk398 7:2973bf297f3d 71
sk398 7:2973bf297f3d 72 // ============================================================================
sk398 7:2973bf297f3d 73 // Cyclic Executive Function Prototypes
sk398 7:2973bf297f3d 74 // ============================================================================
sk398 7:2973bf297f3d 75 void CyclicExec();
sk398 7:2973bf297f3d 76
sk398 7:2973bf297f3d 77 // ============================================================================
sk398 7:2973bf297f3d 78 // Main Execution Program
sk398 7:2973bf297f3d 79 // ============================================================================
sk398 0:5989ac10c4d3 80 int main() {
sk398 7:2973bf297f3d 81
sk398 7:2973bf297f3d 82 // Attempt to open SD file
sk398 7:2973bf297f3d 83 // If open failed, do not run Cyclic Exec
sk398 7:2973bf297f3d 84 if(!task7.openFile("/SD/A2/test.csv","a"))
sk398 7:2973bf297f3d 85 {
sk398 7:2973bf297f3d 86 // Start Cyclic Executive
sk398 7:2973bf297f3d 87 CyclicTicker.attach(&CyclicExec,0.025); // 25ms pulses
sk398 7:2973bf297f3d 88
sk398 7:2973bf297f3d 89 // Keep program running until RESET
sk398 7:2973bf297f3d 90 while(1)
sk398 7:2973bf297f3d 91 {
sk398 7:2973bf297f3d 92 }
sk398 7:2973bf297f3d 93 }
sk398 4:b85bc0d810e1 94
sk398 7:2973bf297f3d 95 // If FIle is not opened, prompt user and show Error on LED
sk398 6:ceda53939eb8 96 else
sk398 6:ceda53939eb8 97 {
sk398 7:2973bf297f3d 98 // Prompt user about error
sk398 7:2973bf297f3d 99 printf("File not opened\r\nNot executing Cyclic Executive");
sk398 7:2973bf297f3d 100
sk398 7:2973bf297f3d 101 // Execute error code on LED
sk398 7:2973bf297f3d 102 while(1)
sk398 7:2973bf297f3d 103 {
sk398 7:2973bf297f3d 104 ErrorLED = 1;
sk398 7:2973bf297f3d 105 wait(1);
sk398 7:2973bf297f3d 106 ErrorLED = 0;
sk398 7:2973bf297f3d 107 wait(1);
sk398 7:2973bf297f3d 108 }
sk398 7:2973bf297f3d 109 }
sk398 7:2973bf297f3d 110 }
sk398 7:2973bf297f3d 111
sk398 7:2973bf297f3d 112 #define TASK1_TICKS 40
sk398 7:2973bf297f3d 113 #define TASK2_TICKS 12
sk398 7:2973bf297f3d 114 #define TASK3_TICKS 12
sk398 7:2973bf297f3d 115 #define TASK4_TICKS 16
sk398 7:2973bf297f3d 116 #define TASK5_TICKS 80
sk398 7:2973bf297f3d 117 #define TASK6_TICKS 32
sk398 7:2973bf297f3d 118 #define TASK7_TICKS 200
sk398 7:2973bf297f3d 119
sk398 7:2973bf297f3d 120 #define HIGH 1
sk398 7:2973bf297f3d 121
sk398 9:46408a8dea0c 122 Timer stampTime;
sk398 7:2973bf297f3d 123 Timer task1t;
sk398 7:2973bf297f3d 124 Timer task2t;
sk398 7:2973bf297f3d 125 Timer task3t;
sk398 7:2973bf297f3d 126 Timer task4t;
sk398 7:2973bf297f3d 127 Timer task5t;
sk398 7:2973bf297f3d 128 Timer task6t;
sk398 7:2973bf297f3d 129
sk398 7:2973bf297f3d 130 void CyclicExec()
sk398 7:2973bf297f3d 131 {
sk398 7:2973bf297f3d 132 cyclicTicks++;
sk398 7:2973bf297f3d 133 if(cyclicTicks % 200 == 0)
sk398 7:2973bf297f3d 134 {
sk398 7:2973bf297f3d 135 stampTime.stop();
sk398 7:2973bf297f3d 136 printf("T7\r\n");
sk398 7:2973bf297f3d 137 int a = sprintf( logData,"Time=%1.2f,Freq=%d,SW1=%d,A1=%1.3f,A2=%1.3f\n",
sk398 7:2973bf297f3d 138 stampTime.read(),
sk398 7:2973bf297f3d 139 task1Frequency,
sk398 7:2973bf297f3d 140 task2SwitchState,
sk398 7:2973bf297f3d 141 task4AnalogChannels[0],
sk398 7:2973bf297f3d 142 task4AnalogChannels[1] );
sk398 7:2973bf297f3d 143 task7.writeData(logData);
sk398 7:2973bf297f3d 144 stampTime.reset();
sk398 7:2973bf297f3d 145 stampTime.start();
sk398 7:2973bf297f3d 146 }
sk398 7:2973bf297f3d 147
sk398 8:7f3594882cec 148 if(cyclicTicks % 84 == 0)
sk398 7:2973bf297f3d 149 {
sk398 7:2973bf297f3d 150 task5t.stop();
sk398 7:2973bf297f3d 151 printf("T5 %1.2f\r\n",task5t.read());
sk398 7:2973bf297f3d 152 task5.updateDisplay( task1Frequency,
sk398 7:2973bf297f3d 153 task2SwitchState,
sk398 7:2973bf297f3d 154 task6ErrorState,
sk398 7:2973bf297f3d 155 task4AnalogChannels[0],
sk398 7:2973bf297f3d 156 task4AnalogChannels[1] );
sk398 7:2973bf297f3d 157 task5t.reset();
sk398 7:2973bf297f3d 158 task5t.start();
sk398 7:2973bf297f3d 159 }
sk398 7:2973bf297f3d 160
sk398 8:7f3594882cec 161 if(cyclicTicks % 38 == 0)
sk398 7:2973bf297f3d 162 {
sk398 7:2973bf297f3d 163 task1t.stop();
sk398 7:2973bf297f3d 164 printf("T1 %d\r\n",task1t.read_ms());
sk398 7:2973bf297f3d 165 task1Frequency = task1.ReadFrequency();
sk398 7:2973bf297f3d 166 task1t.reset();
sk398 7:2973bf297f3d 167 task1t.start();
sk398 6:ceda53939eb8 168 }
sk398 6:ceda53939eb8 169
sk398 7:2973bf297f3d 170 if(cyclicTicks % 32 == 0)
sk398 7:2973bf297f3d 171 {
sk398 7:2973bf297f3d 172 task6t.stop();
sk398 7:2973bf297f3d 173 printf("T6 %d\r\n",task6t.read_ms());
sk398 7:2973bf297f3d 174 task6ErrorState = task6.updateErrorCode( task2SwitchState,
sk398 7:2973bf297f3d 175 task4AnalogChannels[0],
sk398 7:2973bf297f3d 176 task4AnalogChannels[1] );
sk398 7:2973bf297f3d 177 task6t.reset();
sk398 7:2973bf297f3d 178 task6t.start();
sk398 7:2973bf297f3d 179 }
sk398 7:2973bf297f3d 180
sk398 7:2973bf297f3d 181 if(cyclicTicks % 16 == 0)
sk398 7:2973bf297f3d 182 {
sk398 7:2973bf297f3d 183 task4t.stop();
sk398 7:2973bf297f3d 184 printf("T4 %d\r\n",task4t.read_ms());
sk398 7:2973bf297f3d 185 float *analogReading = task4.returnAnalogReadings();
sk398 7:2973bf297f3d 186 task4AnalogChannels[0] = *(analogReading);
sk398 7:2973bf297f3d 187 task4AnalogChannels[1]= *(analogReading+1);
sk398 7:2973bf297f3d 188 task4t.reset();
sk398 7:2973bf297f3d 189 task4t.start();
sk398 0:5989ac10c4d3 190 }
sk398 7:2973bf297f3d 191
sk398 8:7f3594882cec 192 if(cyclicTicks % 6 == 0)
sk398 7:2973bf297f3d 193 {
sk398 8:7f3594882cec 194 if(taskNum == 2)
sk398 8:7f3594882cec 195 {
sk398 8:7f3594882cec 196 task2t.stop();
sk398 8:7f3594882cec 197 printf("T2 %d\r\n",task2t.read_ms());
sk398 8:7f3594882cec 198 task2SwitchState = task2_switch1.digitalInState();
sk398 8:7f3594882cec 199 taskNum = 3;
sk398 8:7f3594882cec 200 task2t.reset();
sk398 8:7f3594882cec 201 task2t.start();
sk398 8:7f3594882cec 202 }
sk398 8:7f3594882cec 203 else if(taskNum == 3)
sk398 8:7f3594882cec 204 {
sk398 8:7f3594882cec 205 task3t.stop();
sk398 8:7f3594882cec 206 printf("T3 %d\r\n",task3t.read_ms());
sk398 8:7f3594882cec 207 task3.OutputWatchdogPulse();
sk398 8:7f3594882cec 208 taskNum = 2;
sk398 8:7f3594882cec 209 task3t.reset();
sk398 8:7f3594882cec 210 task3t.start();
sk398 8:7f3594882cec 211 }
sk398 7:2973bf297f3d 212 }
sk398 7:2973bf297f3d 213
sk398 7:2973bf297f3d 214 if(SDRemoval == HIGH)
sk398 7:2973bf297f3d 215 {
sk398 7:2973bf297f3d 216 printf("SD Removed");
sk398 7:2973bf297f3d 217 task7.closeFile();
sk398 9:46408a8dea0c 218 CyclicTicker.detatch();
sk398 7:2973bf297f3d 219 }
sk398 7:2973bf297f3d 220 }
sk398 7:2973bf297f3d 221