Lab version

Fork of Task328 by Nicholas Outram

Committer:
noutram
Date:
Thu Sep 12 15:12:04 2019 +0000
Revision:
3:25b67e69f6fc
Parent:
2:52faa20a51b4
2019

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noutram 0:c6ec99d3da16 1 #include "mbed.h"
noutram 0:c6ec99d3da16 2 #define N 100000
noutram 2:52faa20a51b4 3 #define RELEASED 0
noutram 2:52faa20a51b4 4 #define PRESSED 1
noutram 3:25b67e69f6fc 5
noutram 3:25b67e69f6fc 6 #ifdef TARGET_NUCLEO_F429ZI
noutram 3:25b67e69f6fc 7 //#define ONBOARD
noutram 3:25b67e69f6fc 8 #endif
noutram 3:25b67e69f6fc 9
noutram 3:25b67e69f6fc 10 #ifdef ONBOARD
noutram 3:25b67e69f6fc 11 DigitalOut red_led(LED3); //CountUp is in its critical section
noutram 3:25b67e69f6fc 12 DigitalOut yellow_led(LED2); //CountDown is in its critical section
noutram 3:25b67e69f6fc 13 DigitalOut green_led(LED1); //counter != 0
noutram 3:25b67e69f6fc 14 #else
noutram 0:c6ec99d3da16 15 DigitalOut red_led(D7); //CountUp is in its critical section
noutram 0:c6ec99d3da16 16 DigitalOut yellow_led(D6); //CountDown is in its critical section
noutram 0:c6ec99d3da16 17 DigitalOut green_led(D5); //counter != 0
noutram 3:25b67e69f6fc 18 #endif
noutram 0:c6ec99d3da16 19 DigitalIn button(USER_BUTTON);
noutram 0:c6ec99d3da16 20
noutram 0:c6ec99d3da16 21 //Shared mutable state
noutram 0:c6ec99d3da16 22 volatile long long counter = 0; //Volatile means it must be stored in memory
noutram 0:c6ec99d3da16 23
noutram 0:c6ec99d3da16 24 //Increment the shared variable
noutram 0:c6ec99d3da16 25 void countUp()
noutram 0:c6ec99d3da16 26 {
noutram 0:c6ec99d3da16 27 //RED MEANS THE COUNT UP FUNCTION IS IN ITS CRITICAL SECTION
noutram 0:c6ec99d3da16 28 red_led = 1;
noutram 0:c6ec99d3da16 29 for (int n=0; n<N; n++) {
noutram 0:c6ec99d3da16 30 counter++;
noutram 0:c6ec99d3da16 31 counter++;
noutram 0:c6ec99d3da16 32 counter++;
noutram 0:c6ec99d3da16 33 counter++;
noutram 0:c6ec99d3da16 34 counter++;
noutram 0:c6ec99d3da16 35 counter++;
noutram 0:c6ec99d3da16 36 counter++;
noutram 0:c6ec99d3da16 37 counter++;
noutram 0:c6ec99d3da16 38 counter++;
noutram 0:c6ec99d3da16 39 counter++;
noutram 0:c6ec99d3da16 40 }
noutram 0:c6ec99d3da16 41 red_led = 0;
noutram 0:c6ec99d3da16 42
noutram 0:c6ec99d3da16 43 //Last to finish turnes out the green light
noutram 0:c6ec99d3da16 44 if (counter == 0) {
noutram 0:c6ec99d3da16 45 green_led = 0;
noutram 0:c6ec99d3da16 46 }
noutram 0:c6ec99d3da16 47 }
noutram 0:c6ec99d3da16 48
noutram 0:c6ec99d3da16 49 //Decrement the shared variable
noutram 0:c6ec99d3da16 50 void countDown()
noutram 0:c6ec99d3da16 51 {
noutram 0:c6ec99d3da16 52 //YELLOW MEANS THE COUNT DOWN FUNCTION IS IN ITS CRITICAL SECTION
noutram 0:c6ec99d3da16 53 yellow_led = 1;
noutram 0:c6ec99d3da16 54 for (int n=0; n<N; n++) {
noutram 0:c6ec99d3da16 55 counter--;
noutram 0:c6ec99d3da16 56 counter--;
noutram 0:c6ec99d3da16 57 counter--;
noutram 0:c6ec99d3da16 58 counter--;
noutram 0:c6ec99d3da16 59 counter--;
noutram 0:c6ec99d3da16 60 counter--;
noutram 0:c6ec99d3da16 61 counter--;
noutram 0:c6ec99d3da16 62 counter--;
noutram 0:c6ec99d3da16 63 counter--;
noutram 0:c6ec99d3da16 64 counter--;
noutram 0:c6ec99d3da16 65 }
noutram 0:c6ec99d3da16 66 yellow_led = 0;
noutram 0:c6ec99d3da16 67
noutram 0:c6ec99d3da16 68 //Last to finish turns out the green light
noutram 0:c6ec99d3da16 69 if (counter == 0) {
noutram 0:c6ec99d3da16 70 green_led = 0;
noutram 0:c6ec99d3da16 71 }
noutram 0:c6ec99d3da16 72 }
noutram 0:c6ec99d3da16 73 int main() {
noutram 0:c6ec99d3da16 74
noutram 0:c6ec99d3da16 75 green_led = 1;
noutram 3:25b67e69f6fc 76 wait(2);
noutram 0:c6ec99d3da16 77 Timeout t1;
noutram 0:c6ec99d3da16 78
noutram 0:c6ec99d3da16 79 // TRY EACH OF THESE LINES IN TURN.
noutram 0:c6ec99d3da16 80 // ALL IT DOES IS CHANGE THE TIMING OF THE ISR, NOT THE FUNCTION
noutram 0:c6ec99d3da16 81
noutram 0:c6ec99d3da16 82 if (button == PRESSED) {
noutram 0:c6ec99d3da16 83 //VERSION 2: short delay allowing main to be preempted - you might want to tweak this value
noutram 0:c6ec99d3da16 84 t1.attach_us(&countDown, 15);
noutram 0:c6ec99d3da16 85 } else {
noutram 0:c6ec99d3da16 86 //VERSION 1: 2s - ENOUGH TIME FOR COUNTUP TO FINISH
noutram 0:c6ec99d3da16 87 t1.attach(&countDown, 2);
noutram 0:c6ec99d3da16 88 }
noutram 0:c6ec99d3da16 89
noutram 0:c6ec99d3da16 90 //Run count up on the main thread
noutram 0:c6ec99d3da16 91 countUp();
noutram 0:c6ec99d3da16 92
noutram 0:c6ec99d3da16 93 //Now spin-lock for ever
noutram 0:c6ec99d3da16 94 while(1) {
noutram 0:c6ec99d3da16 95 asm("nop");
noutram 0:c6ec99d3da16 96 };
noutram 0:c6ec99d3da16 97 }
noutram 0:c6ec99d3da16 98