4180Lab1Part1 with Watchdog functionality added

Dependencies:   mbed

Committer:
CRaslawski
Date:
Mon Feb 27 22:34:26 2017 +0000
Revision:
0:39f5902e5782
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
CRaslawski 0:39f5902e5782 1 #include "mbed.h"
CRaslawski 0:39f5902e5782 2
CRaslawski 0:39f5902e5782 3 class Watchdog {
CRaslawski 0:39f5902e5782 4 public:
CRaslawski 0:39f5902e5782 5 //load timeout value and enable
CRaslawski 0:39f5902e5782 6 void kick(float s) {
CRaslawski 0:39f5902e5782 7 LPC_WDT->WDCLKSEL = 0x1; //Set CLK src to PCLK
CRaslawski 0:39f5902e5782 8 uint32_t clk = SystemCoreClock / 16; //WD has /4 prescaler and PCLK default is /4
CRaslawski 0:39f5902e5782 9 LPC_WDT->WDTC = s * (float)clk;
CRaslawski 0:39f5902e5782 10 LPC_WDT->WDMOD = 0x3; //Enabled and Reset
CRaslawski 0:39f5902e5782 11 kick();
CRaslawski 0:39f5902e5782 12 }
CRaslawski 0:39f5902e5782 13 //kick the dog by feeding required bit pattern
CRaslawski 0:39f5902e5782 14 void kick() {
CRaslawski 0:39f5902e5782 15 LPC_WDT->WDFEED = 0xAA;
CRaslawski 0:39f5902e5782 16 LPC_WDT->WDFEED = 0x55;
CRaslawski 0:39f5902e5782 17 }
CRaslawski 0:39f5902e5782 18 };
CRaslawski 0:39f5902e5782 19
CRaslawski 0:39f5902e5782 20 DigitalOut myled2(p25);
CRaslawski 0:39f5902e5782 21 PwmOut myled3(p21);
CRaslawski 0:39f5902e5782 22 DigitalIn pb(p24);
CRaslawski 0:39f5902e5782 23 DigitalIn pwmup(p22);
CRaslawski 0:39f5902e5782 24 DigitalIn pwmdown(p23);
CRaslawski 0:39f5902e5782 25 Watchdog wdt;
CRaslawski 0:39f5902e5782 26 DigitalOut led1(LED1);
CRaslawski 0:39f5902e5782 27 DigitalOut led2(LED2);
CRaslawski 0:39f5902e5782 28 DigitalOut led3(LED3);
CRaslawski 0:39f5902e5782 29 DigitalOut led4(LED4);
CRaslawski 0:39f5902e5782 30
CRaslawski 0:39f5902e5782 31 int main() {
CRaslawski 0:39f5902e5782 32 // Part 1
CRaslawski 0:39f5902e5782 33 pb.mode(PullUp); // The variable left_pb will be zero when the pushbutton for moving the player left is pressed
CRaslawski 0:39f5902e5782 34 pwmup.mode(PullUp);
CRaslawski 0:39f5902e5782 35 pwmdown.mode(PullUp);
CRaslawski 0:39f5902e5782 36
CRaslawski 0:39f5902e5782 37 // Part 2
CRaslawski 0:39f5902e5782 38 myled3.period(0.01f);
CRaslawski 0:39f5902e5782 39 volatile float duty = 0.50f;
CRaslawski 0:39f5902e5782 40 myled3.write(duty);
CRaslawski 0:39f5902e5782 41
CRaslawski 0:39f5902e5782 42 // Watchdog
CRaslawski 0:39f5902e5782 43 int count = 0;
CRaslawski 0:39f5902e5782 44 // on reset indicate a watchdog reset on LED 4 or 3
CRaslawski 0:39f5902e5782 45 if((LPC_WDT->WDMOD >> 2) & 1)
CRaslawski 0:39f5902e5782 46 led4 = 1; else led3 = 1;
CRaslawski 0:39f5902e5782 47 // set a 10 second timeout on WD timer hardware, longer than worst case main loop execution time
CRaslawski 0:39f5902e5782 48 wdt.kick(5.0);
CRaslawski 0:39f5902e5782 49
CRaslawski 0:39f5902e5782 50 // resets watchdog timer once every loop
CRaslawski 0:39f5902e5782 51 while(1) {
CRaslawski 0:39f5902e5782 52 //watchdog stuff, ldes 1 and 2 indicate normal operation
CRaslawski 0:39f5902e5782 53 led1 = 1;
CRaslawski 0:39f5902e5782 54 wait(0.05);
CRaslawski 0:39f5902e5782 55 led1 = 0;
CRaslawski 0:39f5902e5782 56 led2 = 1;
CRaslawski 0:39f5902e5782 57 wait(0.05);
CRaslawski 0:39f5902e5782 58 // Part 1
CRaslawski 0:39f5902e5782 59 while(pb == 0) {
CRaslawski 0:39f5902e5782 60 myled2 = 0;
CRaslawski 0:39f5902e5782 61 }
CRaslawski 0:39f5902e5782 62 myled2 = 1;
CRaslawski 0:39f5902e5782 63
CRaslawski 0:39f5902e5782 64 // Part 2
CRaslawski 0:39f5902e5782 65 if(pwmup == 0 && duty < 1.00f) {
CRaslawski 0:39f5902e5782 66 //duty = duty + 0.001f;
CRaslawski 0:39f5902e5782 67 duty += 0.05f;
CRaslawski 0:39f5902e5782 68 //duty = 1.0f;
CRaslawski 0:39f5902e5782 69 myled3.write(duty);
CRaslawski 0:39f5902e5782 70 while(pwmup == 0) {}
CRaslawski 0:39f5902e5782 71 }
CRaslawski 0:39f5902e5782 72 if(pwmdown == 0 && duty > 0.00f) {
CRaslawski 0:39f5902e5782 73 //duty = duty - 0.001f;
CRaslawski 0:39f5902e5782 74 duty -= 0.05f;
CRaslawski 0:39f5902e5782 75 //duty = 0.0f;
CRaslawski 0:39f5902e5782 76 myled3.write(duty);
CRaslawski 0:39f5902e5782 77 while(pwmdown == 0) {}
CRaslawski 0:39f5902e5782 78 }
CRaslawski 0:39f5902e5782 79 //watchdog fault simulation
CRaslawski 0:39f5902e5782 80 if (count == 100) while (1) {};
CRaslawski 0:39f5902e5782 81 // led 2 stays on during the fault
CRaslawski 0:39f5902e5782 82 led2 = 0;
CRaslawski 0:39f5902e5782 83 count++;
CRaslawski 0:39f5902e5782 84 //end of main loop, kick the WD
CRaslawski 0:39f5902e5782 85 wdt.kick();
CRaslawski 0:39f5902e5782 86 }
CRaslawski 0:39f5902e5782 87 }