Lab version

Committer:
noutram
Date:
Thu Jul 13 14:49:20 2017 +0000
Revision:
1:ccec17874183
Parent:
0:c6ec99d3da16
updated for mbed-os 5.5

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