Browse Source

Added Watchdog timer.

Thomas Buck 12 years ago
parent
commit
e151419ddc
3 changed files with 63 additions and 8 deletions
  1. 48
    3
      CubeFirmware/main.c
  2. 14
    4
      CubeFirmware/strings.c
  3. 1
    1
      Hardware/Parts.txt

+ 48
- 3
CubeFirmware/main.c View File

@@ -34,6 +34,7 @@
34 34
 #include <avr/pgmspace.h>
35 35
 #include <stdint.h>
36 36
 #include <stdlib.h>
37
+#include <avr/wdt.h>
37 38
 
38 39
 #include "serial.h"
39 40
 #include "cube.h"
@@ -73,8 +74,10 @@ void printTime(void);
73 74
 #include "snake.c"
74 75
 #endif
75 76
 
77
+uint8_t shouldRestart = 0;
76 78
 uint8_t refreshAnimationCount = 1;
77 79
 uint8_t lastButtonState = 0;
80
+uint8_t mcusr_mirror;
78 81
 char buffer[11];
79 82
 
80 83
 uint8_t defaultImage[64] = 	{	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
@@ -97,12 +100,18 @@ int main(void) {
97 100
 	uint64_t lastChecked;
98 101
 	uint32_t temp;
99 102
 
103
+	mcusr_mirror = MCUCSR;
104
+	MCUCSR = 0;
105
+	wdt_disable();
106
+
100 107
 	initCube();
101 108
 	serialInit(25, 8, NONE, 1);
102 109
 	i2c_init();
103 110
 	initSystemTimer();
104 111
 	sei(); // Enable Interrupts
105 112
 
113
+	wdt_enable(WDTO_500MS); // Enable watchdog reset after 500ms
114
+
106 115
 	DDRD = 0xFC; // Mosfets as Output
107 116
 	DDRB = 0xFE;
108 117
 	DDRC = 0xFF; // Latch Enable
@@ -122,14 +131,26 @@ int main(void) {
122 131
 		serialWrite('\n');
123 132
 		printErrors(i);
124 133
 	}
125
-#endif
126 134
 
127
-#ifdef DEBUG
128 135
 	serialWriteString(getString(2));
129 136
 	serialWriteString(getString(0));
130 137
 	serialWriteString("Took ");
131 138
 	serialWriteString(itoa(getSystemTime(), buffer, 10));
132 139
 	serialWriteString(" ms!\n");
140
+
141
+	if (mcusr_mirror & WDRF) {
142
+		serialWriteString(getString(31));
143
+	} else if (mcusr_mirror & BORF) {
144
+		serialWriteString(getString(32));
145
+	} else if (mcusr_mirror & EXTRF) {
146
+		serialWriteString(getString(34));
147
+	} else if (mcusr_mirror & JTRF) {
148
+		serialWriteString(getString(35));
149
+	} else if (mcusr_mirror & PORF) {
150
+		serialWriteString(getString(36));
151
+	} else {
152
+		serialWriteString(getString(33));
153
+	}
133 154
 #endif
134 155
 
135 156
 	lastMode = audioModeSelected();
@@ -138,6 +159,11 @@ int main(void) {
138 159
 	i = 0;
139 160
 	count = getAnimationCount();
140 161
 	while (1) {
162
+		// Reset if requested
163
+		if (!shouldRestart) {
164
+			wdt_reset();
165
+		}
166
+
141 167
 		if(lastMode) {
142 168
 			// Get Audio Data and visualize it
143 169
 			if (isFinished()) {
@@ -197,6 +223,8 @@ int main(void) {
197 223
 			serialHandler((char)(serialGet()));
198 224
 		}
199 225
 
226
+#ifdef DEBUG
227
+		// Print frames per second
200 228
 		if ((getSystemTime() >= 1000) && ((DebugDone & 1) == 0)) {
201 229
 			temp = getTriggerCount();
202 230
 			serialWriteString(ltoa(temp, buffer, 10));
@@ -206,6 +234,13 @@ int main(void) {
206 234
 			DebugDone |= 1;
207 235
 		}
208 236
 
237
+		// Show how stable we are running :)
238
+		if (((getSystemTime() % 60000) == 0) && (getSystemTime() > 0)) {
239
+			serialWriteString(getString(37));
240
+			printTime();
241
+		}
242
+#endif
243
+
209 244
 		if ((getSystemTime() - lastChecked) > 150) {
210 245
 			lastMode = audioModeSelected();
211 246
 			lastChecked = getSystemTime();
@@ -285,7 +320,7 @@ void randomAnimation(void) {
285 320
 
286 321
 void serialHandler(char c) {
287 322
 	// Used letters:
288
-	// a, c, d, e, g, i, n, r, s, t, v, x, y, 0, 1, 2
323
+	// a, c, d, e, g, i, n, q, r, s, t, v, x, y, 0, 1, 2
289 324
 	uint8_t i, y, z;
290 325
 #ifdef DEBUG
291 326
 	serialWrite(c);
@@ -336,6 +371,11 @@ void serialHandler(char c) {
336 371
 		break;
337 372
 
338 373
 #ifdef DEBUG
374
+	case 'q': case 'Q':
375
+		shouldRestart = 1;
376
+		serialWriteString(getString(30));
377
+		break;
378
+
339 379
 	case 'r': case 'R':
340 380
 		randomAnimation();
341 381
 		break;
@@ -438,6 +478,11 @@ void printTime(void) {
438 478
 	serialWriteString(getString(14));
439 479
 	serialWriteString(ltoa(getSystemTime(), buffer, 10));
440 480
 	serialWriteString("ms");
481
+	if (getSystemTime() > 60000) {
482
+		serialWriteString(" (");
483
+		serialWriteString(itoa(getSystemTime() / 60000, buffer, 10));
484
+		serialWriteString(" min)");
485
+	}
441 486
 	if (getSystemTime() > 1000) {
442 487
 		serialWriteString(" (");
443 488
 		serialWriteString(itoa(getSystemTime() / 1000, buffer, 10));

+ 14
- 4
CubeFirmware/strings.c View File

@@ -30,7 +30,7 @@ char stringVersion[] PROGMEM = "v2 (Debug Build)\nNOT COMPATIBLE WITH CubeContro
30 30
 char stringVersion[] PROGMEM = "v2 Release\n"; // 0
31 31
 #endif
32 32
 
33
-char stringInit[] PROGMEM = "\n\nInitialized: "; // 1
33
+char stringInit[] PROGMEM = "\nInitialized: "; // 1
34 34
 char stringSelfTestError[] PROGMEM = "Self-Test Error: 0b"; // 2
35 35
 char stringAudioError[] PROGMEM = " => No answer from Audio!\n"; // 3
36 36
 char stringMemError[] PROGMEM = " => No answer from Memory!\n"; // 4
@@ -55,13 +55,21 @@ char stringAudioData[] PROGMEM = "Audio Data:\n"; // 22
55 55
 char stringSnakeControl[] PROGMEM = "Controls: W A S D Q E, x to quit\n"; // 23
56 56
 char stringNoMoreHeap[] PROGMEM = "Ran out of Heap!\n"; // 24
57 57
 char stringKilledAnimation[] PROGMEM = "Animation aborted!\n"; // 25
58
-char stringHelp9[] PROGMEM = "(i)nterrupt count, (r)andom\n"; // 26
58
+char stringHelp9[] PROGMEM = "(i)nterrupt count, (r)andom, (q)reset\n"; // 26
59 59
 char stringInterrupts[] PROGMEM = " Interrupts after 1000msec\n"; // 27
60 60
 char stringFrames2[] PROGMEM = " Frames per Second\n"; // 28
61 61
 char stringDeleted[] PROGMEM = "Memory deleted!\n"; // 29
62
+char stringReset[] PROGMEM = "Reset in 500ms. Bye!\n"; // 30
63
+char stringWatchdog[] PROGMEM = "Watchdog Reset detected.\n"; // 31
64
+char stringBrownout[] PROGMEM = "Brown-Out Reset detected.\n"; // 32
65
+char stringNothing[] PROGMEM = "No Reset reason detected.\n"; // 33
66
+char stringExtern[] PROGMEM = "External Reset detected.\n"; // 34
67
+char stringJtag[] PROGMEM = "JTAG Reset detected.\n"; // 35
68
+char stringPowerOn[] PROGMEM = "Power-On Reset detected.\n"; // 36
69
+char stringMinute[] PROGMEM = "Yay! Another minute passed :)\n"; // 37
62 70
 
63 71
 // Last index + 1
64
-#define STRINGNUM 30
72
+#define STRINGNUM 38
65 73
 
66 74
 PGM_P stringTable[STRINGNUM] PROGMEM = { stringVersion, stringSelfTestError, stringInit,
67 75
 								stringAudioError, stringMemError, stringMemWriteError,
@@ -70,7 +78,9 @@ PGM_P stringTable[STRINGNUM] PROGMEM = { stringVersion, stringSelfTestError, str
70 78
 								stringByte, stringWritten, stringCount, stringSelfTest,
71 79
 								stringKillCount, stringAccessError, stringAudioData,
72 80
 								stringSnakeControl, stringNoMoreHeap, stringKilledAnimation,
73
-								stringHelp9, stringInterrupts, stringFrames2, stringDeleted };
81
+								stringHelp9, stringInterrupts, stringFrames2, stringDeleted,
82
+								stringReset, stringWatchdog, stringBrownout, stringNothing,
83
+								stringExtern, stringJtag, stringPowerOn, stringMinute };
74 84
 
75 85
 char stringNotFoundError[] PROGMEM = "String not found!\n";
76 86
 

+ 1
- 1
Hardware/Parts.txt View File

@@ -18,7 +18,7 @@ Quant.	Value			Device
18 18
 1		4k7R			0603
19 19
 64		120R			0603
20 20
 
21
-64		PNP				PNP
21
+64		PNP				BC 860CW
22 22
 8		IRF530			IRF530
23 23
 8		74HC573D		74HC573D
24 24
 1		MEGA8-AI		MEGA8-AI

Loading…
Cancel
Save