Sebastian Barrera / Mbed OS Lab6

main.cpp

Committer:
sebbarpar
Date:
2020-03-26
Revision:
8:041c4841cd96
Parent:
7:e49685bca7c7
Child:
9:5d1d36c2664f

File content as of revision 8:041c4841cd96:

#include "mbed.h"
#include "wdt.h"
// Watchdog with better fault coverage
// ---------------------------------
//    * Three threads co-operate to flash two LEDs
//    * A simple way to inject a fault, by pressing a button 
//    * The watchdog is configured with a 1024ms timeout
//    * If hardware fails, set flag to trigger watchdog so it resets the system 

#define ON 1
#define OFF 0
DigitalOut led_red(LED_RED, ON);
DigitalIn button(PTD0, PullUp);
DigitalOut led1(PTC12, OFF);
DigitalOut led2(PTC13, OFF);
AnalogIn ain(PTB0) ; 
AnalogIn anin(PTE20) ;
Serial pc(USBTX, USBRX); // tx, rx, useful for debugging
Ticker tick;

// Threads
Thread threadT(osPriorityNormal, 2000); ; // timer thread 
Thread threadLED1(osPriorityNormal, 2000); ; // thread LED1
Thread threadLED2(osPriorityNormal, 2000); ; // thread LED2

# define ON1 (1UL << 0)   
# define ON2 (1UL << 1)
# define OFF1 (1UL << 2)  
# define OFF2 (1UL << 3)
#define error (1UL << 4)
EventFlags signals;  // event flags for signalling


// ------------Fault Injection Button-------------
//  Wait while the button is down
//     Use this to simulate a STUCK fault
// -----------------------------------------------
void waitButton() {
    while (!button) ;  
}

// ---Thread for controlling LED 1----------------
//   Turn LED1 on/off in response to signals 
// -----------------------------------------------
void led1_thread() {  // method to run in thread
    int evt ;
    while (true) {
        wdt_kickA();//kick watchdog channel A
        evt = signals.wait_any(ON1 | OFF1 | error); // wait for either signal
        if (evt & ON1) led1 = ON ;
        if (evt & OFF1) led1 = OFF ;
        if (evt & error) {while(1){}}}//If hardware error detected, wait until watchdog resets the system
        //waitButton() ;  // POSSIBLE FAULT HERE
    }
}

// ---Thread for controlling LED 2----------------
//   Turn LED2 on/off in response to signals 
// -----------------------------------------------
void led2_thread() {  // method to run in thread
    int evt ;
    while (true) {
        wdt_kickB();//Kick watchdog channel B
        evt = signals.wait_any(ON2 | OFF2 | error); // wait for any signal
        if (evt & ON2) led2 = ON ;
        if (evt & OFF2) led2 = OFF ;
        if (evt & error) {while(1){}}//If hardware error detected, wait until watchdog resets the system
        // waitButton() ; // POSSIBLE FAULT HERE
    }
}

// ---Thread for timing --------------------------
//   Send signals to the other threads
// -----------------------------------------------
void timer_thread() {  // method to run in thread
    while (true) {
        ThisThread::sleep_for(1000) ;
        signals.set(ON1 | ON2) ;
        ThisThread::sleep_for(1000) ;
        signals.set(OFF1 | OFF2) ;
        // waitButton() ; // POSSIBLE FAULT HERE
    }
}

// -----------MAIN-------------------------------
//    Configure watchdog. Start threads. 
//    Show start up with RED for 1sec
//    Remember the watchdog is running
//       - 1024ms to set it once
//       - then must feed it every 1024ms  
// ----------------------------------------------

int main(void) {
    float volts = 0 ;
    float volts2=0;
    int counter = 0 ;
    wdt_1sec() ; // initialise watchdog - 1s timeout
    wdt_kick_all() ;  

    // start threads
    threadT.start(timer_thread) ; // start the timer thread 
    threadLED1.start(led1_thread) ; // start the LED1 control thread 
    threadLED2.start(led2_thread) ; // start the LED2 control thread 
    
    //printf("\nRESTART\n");// Debugging
    while (true) {
        if (led1==ON || led2==ON){//Only check when LEDs are on
            volts=ain.read();
            volts=volts*3.3;
            volts2=anin.read()*3.3;
            //Voltage with LEDs on is around 1.2V, since the resistances available were not of 680ohms.
            if (volts>1.5 || volts2>1.5){signals.set(error);}//Set a signal to detect fault
        }
        //printf("volts: %f volts 2: %f", volts, volts2);//Debugging
        wait(0.1f);
    }
        
    
}