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

+ 14
- 4
CubeFirmware/strings.c View File

30
 char stringVersion[] PROGMEM = "v2 Release\n"; // 0
30
 char stringVersion[] PROGMEM = "v2 Release\n"; // 0
31
 #endif
31
 #endif
32
 
32
 
33
-char stringInit[] PROGMEM = "\n\nInitialized: "; // 1
33
+char stringInit[] PROGMEM = "\nInitialized: "; // 1
34
 char stringSelfTestError[] PROGMEM = "Self-Test Error: 0b"; // 2
34
 char stringSelfTestError[] PROGMEM = "Self-Test Error: 0b"; // 2
35
 char stringAudioError[] PROGMEM = " => No answer from Audio!\n"; // 3
35
 char stringAudioError[] PROGMEM = " => No answer from Audio!\n"; // 3
36
 char stringMemError[] PROGMEM = " => No answer from Memory!\n"; // 4
36
 char stringMemError[] PROGMEM = " => No answer from Memory!\n"; // 4
55
 char stringSnakeControl[] PROGMEM = "Controls: W A S D Q E, x to quit\n"; // 23
55
 char stringSnakeControl[] PROGMEM = "Controls: W A S D Q E, x to quit\n"; // 23
56
 char stringNoMoreHeap[] PROGMEM = "Ran out of Heap!\n"; // 24
56
 char stringNoMoreHeap[] PROGMEM = "Ran out of Heap!\n"; // 24
57
 char stringKilledAnimation[] PROGMEM = "Animation aborted!\n"; // 25
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
 char stringInterrupts[] PROGMEM = " Interrupts after 1000msec\n"; // 27
59
 char stringInterrupts[] PROGMEM = " Interrupts after 1000msec\n"; // 27
60
 char stringFrames2[] PROGMEM = " Frames per Second\n"; // 28
60
 char stringFrames2[] PROGMEM = " Frames per Second\n"; // 28
61
 char stringDeleted[] PROGMEM = "Memory deleted!\n"; // 29
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
 // Last index + 1
71
 // Last index + 1
64
-#define STRINGNUM 30
72
+#define STRINGNUM 38
65
 
73
 
66
 PGM_P stringTable[STRINGNUM] PROGMEM = { stringVersion, stringSelfTestError, stringInit,
74
 PGM_P stringTable[STRINGNUM] PROGMEM = { stringVersion, stringSelfTestError, stringInit,
67
 								stringAudioError, stringMemError, stringMemWriteError,
75
 								stringAudioError, stringMemError, stringMemWriteError,
70
 								stringByte, stringWritten, stringCount, stringSelfTest,
78
 								stringByte, stringWritten, stringCount, stringSelfTest,
71
 								stringKillCount, stringAccessError, stringAudioData,
79
 								stringKillCount, stringAccessError, stringAudioData,
72
 								stringSnakeControl, stringNoMoreHeap, stringKilledAnimation,
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
 char stringNotFoundError[] PROGMEM = "String not found!\n";
85
 char stringNotFoundError[] PROGMEM = "String not found!\n";
76
 
86
 

+ 1
- 1
Hardware/Parts.txt View File

18
 1		4k7R			0603
18
 1		4k7R			0603
19
 64		120R			0603
19
 64		120R			0603
20
 
20
 
21
-64		PNP				PNP
21
+64		PNP				BC 860CW
22
 8		IRF530			IRF530
22
 8		IRF530			IRF530
23
 8		74HC573D		74HC573D
23
 8		74HC573D		74HC573D
24
 1		MEGA8-AI		MEGA8-AI
24
 1		MEGA8-AI		MEGA8-AI

Loading…
Cancel
Save