part 2

Fork of Watchdog_sample_nocoverage by William Marsh

Committer:
dhenis
Date:
Fri Mar 09 23:06:31 2018 +0000
Revision:
6:658d42dab71d
Parent:
3:32a940251192
1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Manel_Marin 0:5ce3cfc57999 1 #include "mbed.h"
WilliamMarshQMUL 3:32a940251192 2 #include "rtos.h"
WilliamMarshQMUL 1:159a09ac60ba 3 #include "wdt.h"
dhenis 6:658d42dab71d 4
WilliamMarshQMUL 2:c31a1758ac38 5 // Sample program using the Watchdog
WilliamMarshQMUL 2:c31a1758ac38 6 // ---------------------------------
WilliamMarshQMUL 3:32a940251192 7 // * Three threads co-operate to flash two LEDs
WilliamMarshQMUL 3:32a940251192 8 // * A simple way to inject a fault, by pressing a button
WilliamMarshQMUL 3:32a940251192 9 // * The watchdog is configured with a 32ms timeout
dhenis 6:658d42dab71d 10
WilliamMarshQMUL 3:32a940251192 11 #define ON 1
WilliamMarshQMUL 3:32a940251192 12 #define OFF 0
WilliamMarshQMUL 3:32a940251192 13 DigitalOut led_red(LED_RED, ON);
WilliamMarshQMUL 3:32a940251192 14 DigitalIn button(PTD0, PullUp);
WilliamMarshQMUL 3:32a940251192 15 DigitalOut led1(PTC12, OFF);
WilliamMarshQMUL 3:32a940251192 16 DigitalOut led2(PTC13, OFF);
dhenis 6:658d42dab71d 17 // Ticker for reading analog
dhenis 6:658d42dab71d 18 AnalogIn ain(A0) ;
dhenis 6:658d42dab71d 19
dhenis 6:658d42dab71d 20 char vstring[] = "X.XX\r\n" ;
dhenis 6:658d42dab71d 21
dhenis 6:658d42dab71d 22 Serial pc(USBTX, USBRX); // tx, rx, for debugging
WilliamMarshQMUL 3:32a940251192 23 // This ticker is used to feed the watch dog
WilliamMarshQMUL 3:32a940251192 24 Ticker tick;
dhenis 6:658d42dab71d 25 int oke = 0;
WilliamMarshQMUL 3:32a940251192 26 // Threads
dhenis 6:658d42dab71d 27 Thread threadT(osPriorityNormal, 1000) ; // timer thread
dhenis 6:658d42dab71d 28 Thread threadLED1(osPriorityNormal, 1000) ; // thread LED1
dhenis 6:658d42dab71d 29 Thread threadLED2(osPriorityNormal, 1000) ; // thread LED2
dhenis 6:658d42dab71d 30
WilliamMarshQMUL 3:32a940251192 31 // ------------Fault Injection Button-------------
WilliamMarshQMUL 3:32a940251192 32 // Wait while the button is down
WilliamMarshQMUL 3:32a940251192 33 // Use this to simulate a STUCK fault
WilliamMarshQMUL 3:32a940251192 34 // -----------------------------------------------
WilliamMarshQMUL 3:32a940251192 35 void waitButton() {
dhenis 6:658d42dab71d 36 oke =1;
dhenis 6:658d42dab71d 37 while (!button) {
dhenis 6:658d42dab71d 38 };
WilliamMarshQMUL 3:32a940251192 39 }
WilliamMarshQMUL 2:c31a1758ac38 40
dhenis 6:658d42dab71d 41
WilliamMarshQMUL 3:32a940251192 42 // ---Thread for controlling LED 1----------------
WilliamMarshQMUL 3:32a940251192 43 // Turn LED1 on/off in response to signals
WilliamMarshQMUL 3:32a940251192 44 // -----------------------------------------------
WilliamMarshQMUL 3:32a940251192 45 void led1_thread() { // method to run in thread
WilliamMarshQMUL 3:32a940251192 46 osEvent evt ;
WilliamMarshQMUL 3:32a940251192 47 while (true) {
WilliamMarshQMUL 3:32a940251192 48 evt = Thread::signal_wait(0x0); // wait for any signal
dhenis 6:658d42dab71d 49
dhenis 6:658d42dab71d 50 wdt_kickA();
dhenis 6:658d42dab71d 51
WilliamMarshQMUL 3:32a940251192 52 if (evt.status == osEventSignal) {
WilliamMarshQMUL 3:32a940251192 53 if (evt.value.signals & 0x01) led1 = ON ;
WilliamMarshQMUL 3:32a940251192 54 if (evt.value.signals & 0x02) led1 = OFF ;
dhenis 6:658d42dab71d 55
WilliamMarshQMUL 3:32a940251192 56 }
dhenis 6:658d42dab71d 57
dhenis 6:658d42dab71d 58 // waitButton() ; // POSSIBLE FAULT HERE
dhenis 6:658d42dab71d 59
dhenis 6:658d42dab71d 60
WilliamMarshQMUL 3:32a940251192 61 }
WilliamMarshQMUL 3:32a940251192 62 }
dhenis 6:658d42dab71d 63
WilliamMarshQMUL 3:32a940251192 64 // ---Thread for controlling LED 2----------------
WilliamMarshQMUL 3:32a940251192 65 // Turn LED2 on/off in response to signals
WilliamMarshQMUL 3:32a940251192 66 // -----------------------------------------------
WilliamMarshQMUL 3:32a940251192 67 void led2_thread() { // method to run in thread
WilliamMarshQMUL 3:32a940251192 68 osEvent evt ;
WilliamMarshQMUL 3:32a940251192 69 while (true) {
WilliamMarshQMUL 3:32a940251192 70 evt = Thread::signal_wait(0x0); // wait for any signal
dhenis 6:658d42dab71d 71 wdt_kickB();
dhenis 6:658d42dab71d 72
WilliamMarshQMUL 3:32a940251192 73 if (evt.status == osEventSignal) {
WilliamMarshQMUL 3:32a940251192 74 if (evt.value.signals & 0x01) led2 = ON ;
WilliamMarshQMUL 3:32a940251192 75 if (evt.value.signals & 0x02) led2 = OFF ;
dhenis 6:658d42dab71d 76 //wdt_kickA();
dhenis 6:658d42dab71d 77
dhenis 6:658d42dab71d 78 }
dhenis 6:658d42dab71d 79
dhenis 6:658d42dab71d 80 waitButton() ; // POSSIBLE FAULT HERE
dhenis 6:658d42dab71d 81
WilliamMarshQMUL 3:32a940251192 82 }
WilliamMarshQMUL 3:32a940251192 83 }
dhenis 6:658d42dab71d 84
WilliamMarshQMUL 3:32a940251192 85 // ---Thread for timing --------------------------
WilliamMarshQMUL 3:32a940251192 86 // Send signals to the other threads
WilliamMarshQMUL 3:32a940251192 87 // -----------------------------------------------
WilliamMarshQMUL 3:32a940251192 88 void timer_thread() { // method to run in thread
WilliamMarshQMUL 3:32a940251192 89 while (true) {
dhenis 6:658d42dab71d 90
dhenis 6:658d42dab71d 91
WilliamMarshQMUL 3:32a940251192 92 Thread::wait(250) ;
WilliamMarshQMUL 3:32a940251192 93 threadLED1.signal_set(0x1) ;
WilliamMarshQMUL 3:32a940251192 94 threadLED2.signal_set(0x1) ;
dhenis 6:658d42dab71d 95
WilliamMarshQMUL 3:32a940251192 96 Thread::wait(250) ;
WilliamMarshQMUL 3:32a940251192 97 threadLED1.signal_set(0x2) ;
WilliamMarshQMUL 3:32a940251192 98 threadLED2.signal_set(0x2) ;
dhenis 6:658d42dab71d 99
dhenis 6:658d42dab71d 100 //tick.attach_us(callback(&wdt_kick_all), 20000); // ticks every 20ms
dhenis 6:658d42dab71d 101
dhenis 6:658d42dab71d 102
dhenis 6:658d42dab71d 103 // waitButton() ; // POSSIBLE FAULT HERE
WilliamMarshQMUL 3:32a940251192 104 }
WilliamMarshQMUL 3:32a940251192 105 }
dhenis 6:658d42dab71d 106
WilliamMarshQMUL 3:32a940251192 107 // -----------MAIN-------------------------------
WilliamMarshQMUL 3:32a940251192 108 // Configure watchdog. Start threads.
WilliamMarshQMUL 3:32a940251192 109 // Show start up with RED for 1sec
WilliamMarshQMUL 3:32a940251192 110 // Remember the watchdog is running
WilliamMarshQMUL 3:32a940251192 111 // - 1024ms to set it once
WilliamMarshQMUL 3:32a940251192 112 // - then must feed it every 32ms
WilliamMarshQMUL 3:32a940251192 113 // ----------------------------------------------
dhenis 6:658d42dab71d 114
Manel_Marin 0:5ce3cfc57999 115 int main(void) {
dhenis 6:658d42dab71d 116
dhenis 6:658d42dab71d 117 wdt_1sec() ; // initialise watchdog - 32ms timeout
dhenis 6:658d42dab71d 118 //wdt_32ms();
dhenis 6:658d42dab71d 119
dhenis 6:658d42dab71d 120
dhenis 6:658d42dab71d 121 //tick.attach_us(callback(&wdt_kick_all), 20000); // ticks every 20ms
WilliamMarshQMUL 1:159a09ac60ba 122
WilliamMarshQMUL 3:32a940251192 123 // start threads
WilliamMarshQMUL 3:32a940251192 124 threadT.start(&timer_thread) ; // start the timer thread
WilliamMarshQMUL 3:32a940251192 125 threadLED1.start(&led1_thread) ; // start the LED1 control thread
WilliamMarshQMUL 3:32a940251192 126 threadLED2.start(&led2_thread) ; // start the LED2 control thread
WilliamMarshQMUL 1:159a09ac60ba 127
WilliamMarshQMUL 1:159a09ac60ba 128 // show start-up
WilliamMarshQMUL 3:32a940251192 129 led_red = OFF;
WilliamMarshQMUL 3:32a940251192 130 Thread::wait(1000) ;
WilliamMarshQMUL 3:32a940251192 131 led_red = ON;
Manel_Marin 0:5ce3cfc57999 132 }