Lab that has to do with basic IO on mbed.
Dependencies: DebounceIn mbed PinDetect
GeorgesLab1/GeorgeMain.cpp@47:0efd125c7f2d, 2016-01-23 (annotated)
- Committer:
- gtzintzarov3
- Date:
- Sat Jan 23 06:44:49 2016 +0000
- Revision:
- 47:0efd125c7f2d
- Child:
- 49:f2081d0f5eec
GT: Watchdog timer added, assembly language added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gtzintzarov3 | 47:0efd125c7f2d | 1 | /* |
gtzintzarov3 | 47:0efd125c7f2d | 2 | #include "mbed.h" |
gtzintzarov3 | 47:0efd125c7f2d | 3 | #include "DebounceIn.h" |
gtzintzarov3 | 47:0efd125c7f2d | 4 | #include "PinDetect.h" |
gtzintzarov3 | 47:0efd125c7f2d | 5 | #include <algorithm> |
gtzintzarov3 | 47:0efd125c7f2d | 6 | #include "math.h" |
gtzintzarov3 | 47:0efd125c7f2d | 7 | using namespace std; |
gtzintzarov3 | 47:0efd125c7f2d | 8 | |
gtzintzarov3 | 47:0efd125c7f2d | 9 | |
gtzintzarov3 | 47:0efd125c7f2d | 10 | PinDetect pb(p20); |
gtzintzarov3 | 47:0efd125c7f2d | 11 | DigitalOut myled1(LED1); |
gtzintzarov3 | 47:0efd125c7f2d | 12 | DigitalOut myled2(LED2); |
gtzintzarov3 | 47:0efd125c7f2d | 13 | DigitalOut myled3(LED3); |
gtzintzarov3 | 47:0efd125c7f2d | 14 | DigitalOut myled4(LED4); |
gtzintzarov3 | 47:0efd125c7f2d | 15 | PwmOut discrete_led(p21); |
gtzintzarov3 | 47:0efd125c7f2d | 16 | AnalogIn voltage_reg(p17); |
gtzintzarov3 | 47:0efd125c7f2d | 17 | |
gtzintzarov3 | 47:0efd125c7f2d | 18 | DigitalOut latch(p15); |
gtzintzarov3 | 47:0efd125c7f2d | 19 | SPI spi(p11, p12, p13); |
gtzintzarov3 | 47:0efd125c7f2d | 20 | |
gtzintzarov3 | 47:0efd125c7f2d | 21 | int RGB_val[3] = {0,0,0}; |
gtzintzarov3 | 47:0efd125c7f2d | 22 | |
gtzintzarov3 | 47:0efd125c7f2d | 23 | void RGB_intensity(float voltage) { |
gtzintzarov3 | 47:0efd125c7f2d | 24 | float min_ = 0; |
gtzintzarov3 | 47:0efd125c7f2d | 25 | float max_ = 1000; |
gtzintzarov3 | 47:0efd125c7f2d | 26 | float ratio = 2 * (voltage-min_)/(max_-min_); |
gtzintzarov3 | 47:0efd125c7f2d | 27 | float b = max(float(0), 1000*(1-ratio)); |
gtzintzarov3 | 47:0efd125c7f2d | 28 | float r = max(float(0), 1000*(ratio-1)); |
gtzintzarov3 | 47:0efd125c7f2d | 29 | float g = float(1000) - b - r; |
gtzintzarov3 | 47:0efd125c7f2d | 30 | if (r<0) {r=0;} |
gtzintzarov3 | 47:0efd125c7f2d | 31 | if (g<0) {g=0;} |
gtzintzarov3 | 47:0efd125c7f2d | 32 | if (b<0) {b=0;} |
gtzintzarov3 | 47:0efd125c7f2d | 33 | RGB_val[0] = int(r); |
gtzintzarov3 | 47:0efd125c7f2d | 34 | RGB_val[1] = int(g); |
gtzintzarov3 | 47:0efd125c7f2d | 35 | RGB_val[2] = int(b); |
gtzintzarov3 | 47:0efd125c7f2d | 36 | } |
gtzintzarov3 | 47:0efd125c7f2d | 37 | |
gtzintzarov3 | 47:0efd125c7f2d | 38 | void RGB_LED(int red, int green, int blue) { |
gtzintzarov3 | 47:0efd125c7f2d | 39 | unsigned int overallColor = 0; |
gtzintzarov3 | 47:0efd125c7f2d | 40 | unsigned short int MSB = 0; |
gtzintzarov3 | 47:0efd125c7f2d | 41 | unsigned short int LSB = 0; |
gtzintzarov3 | 47:0efd125c7f2d | 42 | overallColor = (((blue<<10)|red)<<10)|(green); |
gtzintzarov3 | 47:0efd125c7f2d | 43 | MSB = (overallColor>>16); |
gtzintzarov3 | 47:0efd125c7f2d | 44 | LSB = overallColor; |
gtzintzarov3 | 47:0efd125c7f2d | 45 | spi.write(MSB); |
gtzintzarov3 | 47:0efd125c7f2d | 46 | spi.write(LSB); |
gtzintzarov3 | 47:0efd125c7f2d | 47 | latch=1; |
gtzintzarov3 | 47:0efd125c7f2d | 48 | latch=0; |
gtzintzarov3 | 47:0efd125c7f2d | 49 | } |
gtzintzarov3 | 47:0efd125c7f2d | 50 | |
gtzintzarov3 | 47:0efd125c7f2d | 51 | class Watchdog { |
gtzintzarov3 | 47:0efd125c7f2d | 52 | public: |
gtzintzarov3 | 47:0efd125c7f2d | 53 | // Load timeout value in watchdog timer and enable |
gtzintzarov3 | 47:0efd125c7f2d | 54 | void kick(float s) { |
gtzintzarov3 | 47:0efd125c7f2d | 55 | LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK |
gtzintzarov3 | 47:0efd125c7f2d | 56 | uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4 |
gtzintzarov3 | 47:0efd125c7f2d | 57 | LPC_WDT->WDTC = s * (float)clk; |
gtzintzarov3 | 47:0efd125c7f2d | 58 | LPC_WDT->WDMOD = 0x3; // Enabled and Reset |
gtzintzarov3 | 47:0efd125c7f2d | 59 | kick(); |
gtzintzarov3 | 47:0efd125c7f2d | 60 | } |
gtzintzarov3 | 47:0efd125c7f2d | 61 | // "kick" or "feed" the dog - reset the watchdog timer |
gtzintzarov3 | 47:0efd125c7f2d | 62 | // by writing this required bit pattern |
gtzintzarov3 | 47:0efd125c7f2d | 63 | void kick() { |
gtzintzarov3 | 47:0efd125c7f2d | 64 | LPC_WDT->WDFEED = 0xAA; |
gtzintzarov3 | 47:0efd125c7f2d | 65 | LPC_WDT->WDFEED = 0x55; |
gtzintzarov3 | 47:0efd125c7f2d | 66 | } |
gtzintzarov3 | 47:0efd125c7f2d | 67 | }; |
gtzintzarov3 | 47:0efd125c7f2d | 68 | |
gtzintzarov3 | 47:0efd125c7f2d | 69 | |
gtzintzarov3 | 47:0efd125c7f2d | 70 | Watchdog wdt; |
gtzintzarov3 | 47:0efd125c7f2d | 71 | |
gtzintzarov3 | 47:0efd125c7f2d | 72 | int main() { |
gtzintzarov3 | 47:0efd125c7f2d | 73 | |
gtzintzarov3 | 47:0efd125c7f2d | 74 | float brightness = 0; |
gtzintzarov3 | 47:0efd125c7f2d | 75 | pb.mode(PullUp); |
gtzintzarov3 | 47:0efd125c7f2d | 76 | |
gtzintzarov3 | 47:0efd125c7f2d | 77 | if (pb) { |
gtzintzarov3 | 47:0efd125c7f2d | 78 | spi.format(16,0); |
gtzintzarov3 | 47:0efd125c7f2d | 79 | spi.frequency(1000000); |
gtzintzarov3 | 47:0efd125c7f2d | 80 | latch=0; |
gtzintzarov3 | 47:0efd125c7f2d | 81 | wait(2); |
gtzintzarov3 | 47:0efd125c7f2d | 82 | while(1) { |
gtzintzarov3 | 47:0efd125c7f2d | 83 | //code for red led |
gtzintzarov3 | 47:0efd125c7f2d | 84 | brightness = voltage_reg; |
gtzintzarov3 | 47:0efd125c7f2d | 85 | myled1 = !pb; |
gtzintzarov3 | 47:0efd125c7f2d | 86 | if (!pb) { |
gtzintzarov3 | 47:0efd125c7f2d | 87 | discrete_led.write(brightness); |
gtzintzarov3 | 47:0efd125c7f2d | 88 | } |
gtzintzarov3 | 47:0efd125c7f2d | 89 | else{ |
gtzintzarov3 | 47:0efd125c7f2d | 90 | discrete_led.write(0); |
gtzintzarov3 | 47:0efd125c7f2d | 91 | } |
gtzintzarov3 | 47:0efd125c7f2d | 92 | |
gtzintzarov3 | 47:0efd125c7f2d | 93 | //code for multicolor led |
gtzintzarov3 | 47:0efd125c7f2d | 94 | float heatmapColor = voltage_reg.read()*float(1000); |
gtzintzarov3 | 47:0efd125c7f2d | 95 | if (heatmapColor < 0) {heatmapColor=0;} |
gtzintzarov3 | 47:0efd125c7f2d | 96 | RGB_intensity(heatmapColor); |
gtzintzarov3 | 47:0efd125c7f2d | 97 | RGB_LED(RGB_val[0], RGB_val[1], RGB_val[2]); |
gtzintzarov3 | 47:0efd125c7f2d | 98 | //wait(.2); |
gtzintzarov3 | 47:0efd125c7f2d | 99 | } |
gtzintzarov3 | 47:0efd125c7f2d | 100 | |
gtzintzarov3 | 47:0efd125c7f2d | 101 | } |
gtzintzarov3 | 47:0efd125c7f2d | 102 | else { |
gtzintzarov3 | 47:0efd125c7f2d | 103 | int count = 0; |
gtzintzarov3 | 47:0efd125c7f2d | 104 | // On reset, indicate a watchdog reset or a pushbutton reset on LED 4 or 3 |
gtzintzarov3 | 47:0efd125c7f2d | 105 | if ((LPC_WDT->WDMOD >> 2) & 1) |
gtzintzarov3 | 47:0efd125c7f2d | 106 | myled4 = 1; else myled3 = 1; |
gtzintzarov3 | 47:0efd125c7f2d | 107 | |
gtzintzarov3 | 47:0efd125c7f2d | 108 | // setup a 10 second timeout on watchdog timer hardware |
gtzintzarov3 | 47:0efd125c7f2d | 109 | // needs to be longer than worst case main loop exection time |
gtzintzarov3 | 47:0efd125c7f2d | 110 | wdt.kick(10.0); |
gtzintzarov3 | 47:0efd125c7f2d | 111 | |
gtzintzarov3 | 47:0efd125c7f2d | 112 | // Main program loop - resets watchdog once each loop iteration |
gtzintzarov3 | 47:0efd125c7f2d | 113 | // Would typically have a lot of code in loop with many calls |
gtzintzarov3 | 47:0efd125c7f2d | 114 | while (1) { |
gtzintzarov3 | 47:0efd125c7f2d | 115 | myled1 = 1; //Flash LEDs 1 & 2 to indicate normal loop activity |
gtzintzarov3 | 47:0efd125c7f2d | 116 | wait(.05); |
gtzintzarov3 | 47:0efd125c7f2d | 117 | myled1 = 0; |
gtzintzarov3 | 47:0efd125c7f2d | 118 | myled2 = 1; |
gtzintzarov3 | 47:0efd125c7f2d | 119 | wait(.05); |
gtzintzarov3 | 47:0efd125c7f2d | 120 | // Simulate a fault lock up with an infinite while loop, but only after 25 loop iterations |
gtzintzarov3 | 47:0efd125c7f2d | 121 | if (count == 25) while (1) {}; |
gtzintzarov3 | 47:0efd125c7f2d | 122 | // LED 2 will stay on during the fault |
gtzintzarov3 | 47:0efd125c7f2d | 123 | myled2 = 0; |
gtzintzarov3 | 47:0efd125c7f2d | 124 | count ++; |
gtzintzarov3 | 47:0efd125c7f2d | 125 | // End of main loop so "kick" to reset watchdog timer and avoid a reset |
gtzintzarov3 | 47:0efd125c7f2d | 126 | wdt.kick(); |
gtzintzarov3 | 47:0efd125c7f2d | 127 | } |
gtzintzarov3 | 47:0efd125c7f2d | 128 | } |
gtzintzarov3 | 47:0efd125c7f2d | 129 | } |
gtzintzarov3 | 47:0efd125c7f2d | 130 | // */ |