Lab6 part 1

main.cpp

Committer:
WilliamMarshQMUL
Date:
2017-02-28
Revision:
1:159a09ac60ba
Parent:
0:5ce3cfc57999
Child:
2:c31a1758ac38

File content as of revision 1:159a09ac60ba:

#include "mbed.h"
#include "wdt.h"

//WATCHDOG ENABLED (overloading library function SystemInit.c)
// 
// The KL25Z watchdog is enabled after reset with timeout=1024ms (2^10*LPO=1KHz)
// To make the dvice easy to use on mbed, th watchdog is disabled at startup 
// (in file SystemInit.c) **BUT** SIM_COPC register can only be written once 
// after reset and so it can not be enabled later. To resolve this, 
// SystemInit() is overloaded using:   void $Sub$$SystemInit (void) {
//
// The original SystemInit() can be dowloaded from:
//   https://developer.mbed.org/users/mbed_official/code/mbed-src/
//   navigating to ... targets/cmsis/TARGET_Freescale/TARGET_KLXX/
//                       TARGET_KL25Z/system_MKL25Z4.c
//
// MORE on how this overides the original initialisation: 
//   https://developer.mbed.org/users/chris/notebook/Patching-functions-and-libraries/
//   and searching "ARM Compiler toolchain Using the Linker" 
//      "Using $Super$$ and $Sub$$ to patch symbol definitions"


#define LIGHT 0
#define DARK 1
DigitalOut led_red(LED_RED, LIGHT);
DigitalOut led_blue(LED_BLUE, DARK);

// Demonstate the watchdog
//   The red LED is on after reset
//   Th blue LED flashes and the watch dog is fed
//   Aft 5sec, stop feeding the watchdog
enum wState {Working, Stopped} ;

int main(void) {
    // initialise watchdog
    wdt_1sec() ;
    
    // state of the watch dog
    wState watchDog = Working ;
    int counter = 10 ;
    
    // show start-up
    led_blue = DARK;
    led_red = LIGHT;
    wait(0.5) ;
    led_red = DARK;
    
    // Note: the red LED now stays OFF until processor reset
    while(1)
    {
        led_blue = LIGHT;
        wait(0.25);
        if (watchDog == Working) wdt_kick_all() ;
        led_blue = DARK;
        wait(0.25);
        
        // When the counter rachs zero, chang state to stop watchdog 
        if (counter > 0) counter-- ; else watchDog = Stopped ;
    }
}