Browse Source

Worked at firmware.

Interrupt execution time does not change, strangely?
Thomas Buck 12 years ago
parent
commit
4b9006ec7c
8 changed files with 121 additions and 86 deletions
  1. 4
    10
      CubeFirmware/audio.c
  2. 1
    1
      CubeFirmware/audio.h
  3. 29
    43
      CubeFirmware/cube.c
  4. 2
    1
      CubeFirmware/cube.h
  5. 66
    26
      CubeFirmware/main.c
  6. 3
    3
      CubeFirmware/mem.c
  7. 8
    0
      CubeFirmware/snake.c
  8. 8
    2
      CubeFirmware/strings.c

+ 4
- 10
CubeFirmware/audio.c View File

@@ -30,18 +30,13 @@
30 30
 #include "serial.h"
31 31
 #endif
32 32
 
33
-// free returned memory!
33
+uint8_t ret[7]; // Buffer for data
34
+
35
+// don't free returned memory! It's static mem
34 36
 uint8_t *getAudioData(void) {
35 37
 	// We read 7 bytes from our Audio µC
36 38
 	uint8_t i;
37
-	uint8_t *ret = (uint8_t *)malloc(7);
38
-	if (ret == NULL) {
39
-		// Ran out of memory...
40
-#ifdef DEBUG
41
-		serialWriteString("getAudioData: No memory!\n");
42
-#endif
43
-		return NULL;
44
-	}
39
+
45 40
 	if (i2c_start(TWIADDRESSAUDIO | I2C_READ) == 0) {
46 41
 		for (i = 0; i < 6; i++) {
47 42
 			ret[i] = i2c_readAck();
@@ -50,7 +45,6 @@ uint8_t *getAudioData(void) {
50 45
 		i2c_stop();
51 46
 		return ret;
52 47
 	} else {
53
-		free(ret);
54 48
 		return NULL;
55 49
 	}
56 50
 }

+ 1
- 1
CubeFirmware/audio.h View File

@@ -23,5 +23,5 @@
23 23
 
24 24
 #define TWIADDRESSAUDIO 0x42
25 25
 
26
-// free returned memory!
26
+// don't free returned memory! It's static mem
27 27
 uint8_t *getAudioData(void);

+ 29
- 43
CubeFirmware/cube.c View File

@@ -36,18 +36,38 @@
36 36
 #endif
37 37
 
38 38
 // Should be 41666
39
-#define FIRSTCOUNT 1000
40
-// Time we wait for latch in ns, should be 63
41
-#define LATCHDELAY 63
39
+#define COUNT 41666
42 40
 
43
-volatile uint8_t **imgBuffer = NULL; // imgBuffer[8][8]
41
+/*
42
+ * We have:
43
+ * Fosc / prescaler / fps / interruptsPerFrame = interruptCount
44
+ * 16000000 / 1 / 24 / 8 = 83333 (round-a-bout)
45
+ * That means, 192 Interrupts per second, 1920 after 10 secs!
46
+ *
47
+ * Small Study: Interrupt count after 10 seconds
48
+ *
49
+ * |-------------------------------|
50
+ * |  COUNT   |  1   | 41666 |     |
51
+ * |----------|------|-------|-----|
52
+ * | intcount | 2451 | 2451  |     |
53
+ * |-------------------------------|
54
+ *
55
+ * Haha, what's up with that?
56
+ */
57
+
58
+volatile uint8_t imgBuffer[8][8];
44 59
 volatile uint8_t changedFlag = 0;
45 60
 volatile uint8_t imgFlag = 0;
46 61
 volatile uint8_t layer = 0;
47 62
 
48
-inline void delay_ns(int16_t ns);
49 63
 inline void isrCall(void);
50 64
 
65
+volatile uint32_t timesTriggered = 0;
66
+
67
+uint32_t getTriggerCount(void) {
68
+	return timesTriggered;
69
+}
70
+
51 71
 void setImage(uint8_t *img) {
52 72
 	uint8_t i, j;
53 73
 	ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
@@ -79,47 +99,19 @@ uint8_t isFinished(void) {
79 99
 }
80 100
 
81 101
 void initCube(void) {
82
-	uint8_t ctr = 0;
83
-
84 102
 	TCCR1A |= (1 << WGM12); // CTC Mode
85 103
 	TCCR1B |= (1 << CS10); // Prescaler: 1
86
-	OCR1A = FIRSTCOUNT;
104
+	OCR1A = COUNT;
87 105
 	TIMSK = (1 << OCIE1A); // Enable Output Compare Interrupt
88
-
89
-	// We just assume this works, because after reset,
90
-	// enough Memory should be available...
91
-	imgBuffer = malloc(8 * sizeof(uint8_t*));
92
-	if (imgBuffer == NULL) {
93
-#ifdef DEBUG
94
-		serialWriteString("initCube: No memory!\nHalting!");
95
-#endif
96
-		while(1);
97
-	}
98
-
99
-	for(ctr = 0; ctr < 8; ctr++) {
100
-		// Same reasoning here...
101
-		imgBuffer[ctr] = malloc(8 * sizeof(uint8_t));
102
-		if (imgBuffer[ctr] == NULL) {
103
-#ifdef DEBUG
104
-			serialWriteString("initCube: No memory!\nHalting!");
105
-#endif
106
-			while(1);
107
-		}
108
-	}
109 106
 }
110 107
 
111 108
 void close(void) {
112
-	uint8_t ctr = 0;
113
-	for (; ctr < 8; ctr++) {
114
-		free((uint8_t *)imgBuffer[ctr]);
115
-	}
116
-	free(imgBuffer);
117 109
 	TIMSK &= ~(1 << OCIE1A); // Disable interrupt
118 110
 }
119 111
 
120
-// Count to FIRSTCOUNT SECONDCOUNT times...
121 112
 ISR(TIMER1_COMPA_vect) {
122 113
 	isrCall();
114
+	timesTriggered++;
123 115
 }
124 116
 
125 117
 // Data is sent to 8 Fet bits...
@@ -139,7 +131,7 @@ inline void selectLatch(uint8_t latchNr) {
139 131
 inline void setLatch(uint8_t latchNr, uint8_t data) {
140 132
 	selectLatch(latchNr); // Activate current latch
141 133
 	PORTA = data; // Put latch data
142
-	delay_ns(LATCHDELAY); // Wait for latch
134
+	asm volatile("nop"::); // Wait for latch
143 135
 }
144 136
 
145 137
 inline void isrCall(void) {
@@ -150,7 +142,7 @@ inline void isrCall(void) {
150 142
 		layer = 0;
151 143
 		changedFlag = 0;
152 144
 	}
153
-	setFet(0);
145
+	// setFet(0);
154 146
 	for (; latchCtr < 8; latchCtr++) {
155 147
 		setLatch(latchCtr, imgBuffer[layer][latchCtr]); // Put out all the data
156 148
 	}
@@ -164,9 +156,3 @@ inline void isrCall(void) {
164 156
 		imgFlag++; // Finished
165 157
 	}
166 158
 }
167
-
168
-inline void delay_ns(int16_t ns) {
169
-	// minimum 63 nanoseconds (= 1 tick)
170
-	for (;ns > 0; ns -= 63)
171
-		asm volatile("nop"::);
172
-}

+ 2
- 1
CubeFirmware/cube.h View File

@@ -28,7 +28,7 @@
28 28
  */
29 29
 extern void initCube(void);
30 30
 
31
-// Copies the data in img into its own buffer, so free img afterwards...
31
+// Copies the data in img into its own buffer
32 32
 extern void setImage(uint8_t *img); // img[64]
33 33
 extern uint8_t isFinished(void);
34 34
 extern void close(void);
@@ -38,3 +38,4 @@ extern void fillBuffer(uint8_t val);
38 38
 // For debugging, not normal operation:
39 39
 extern void setFet(uint8_t data);
40 40
 extern void setLatch(uint8_t latchNr, uint8_t data);
41
+extern uint32_t getTriggerCount(void);

+ 66
- 26
CubeFirmware/main.c View File

@@ -65,6 +65,7 @@ void visualizeAudioData(uint8_t *audioData, uint8_t *imageData);
65 65
 #ifdef DEBUG
66 66
 void printErrors(uint8_t e);
67 67
 uint8_t selfTest(void);
68
+void printTime(void);
68 69
 
69 70
 #include "snake.c"
70 71
 #endif
@@ -88,6 +89,7 @@ int main(void) {
88 89
 	uint8_t i, length = 0, lastMode;
89 90
 	uint16_t count;
90 91
 	uint64_t lastChecked;
92
+	uint8_t DebugDone = 0; // Bit 0: 10s int. count, Bit 1: switch idle display
91 93
 
92 94
 	initCube();
93 95
 	serialInit(25, 8, NONE, 1);
@@ -136,10 +138,15 @@ int main(void) {
136 138
 				audioData = getAudioData();
137 139
 				if (audioData != NULL) {
138 140
 					imageData = (uint8_t *)malloc(64);
141
+					if (imageData == NULL) {
142
+#ifdef DEBUG
143
+						serialWriteString(getString(24));
144
+#endif
145
+						while(1);
146
+					}
139 147
 					visualizeAudioData(audioData, imageData);
140 148
 					setImage(imageData);
141 149
 					free(imageData);
142
-					free(audioData);
143 150
 				}
144 151
 			}
145 152
 		} else {
@@ -164,6 +171,17 @@ int main(void) {
164 171
 					setImage(imageData);
165 172
 					free(imageData);
166 173
 				}
174
+			} else {
175
+				if (isFinished() >= 12) {
176
+					// Should happen every half second
177
+					if (DebugDone & 2) {
178
+						fillBuffer(0);
179
+						DebugDone &= ~(2);
180
+					} else {
181
+						fillBuffer(0xFF);
182
+						DebugDone |= 2;
183
+					}
184
+				}
167 185
 			}
168 186
 		}
169 187
 
@@ -171,6 +189,13 @@ int main(void) {
171 189
 			serialHandler((char)(serialGet()));
172 190
 		}
173 191
 
192
+		if ((getSystemTime() >= 10000) && ((DebugDone & 1) == 0)) {
193
+			printTime();
194
+			serialWriteString(ltoa(getTriggerCount(), buffer, 10));
195
+			serialWrite('\n');
196
+			DebugDone |= 1;
197
+		}
198
+
174 199
 		if ((getSystemTime() - lastChecked) > 150) {
175 200
 			lastMode = audioModeSelected();
176 201
 			lastChecked = getSystemTime();
@@ -222,7 +247,7 @@ void printErrors(uint8_t e) {
222 247
 
223 248
 void serialHandler(char c) {
224 249
 	// Used letters:
225
-	// a, c, d, g, s, t, v, x
250
+	// a, c, d, e, g, n, s, t, v, x, 0, 1, 2
226 251
 	uint8_t i, y, z;
227 252
 #ifdef DEBUG
228 253
 	serialWrite(c);
@@ -244,6 +269,7 @@ void serialHandler(char c) {
244 269
 		serialWriteString(getString(11));
245 270
 		serialWriteString(getString(12));
246 271
 		serialWriteString(getString(13));
272
+		serialWriteString(getString(26));
247 273
 #endif
248 274
 		break;
249 275
 
@@ -266,25 +292,7 @@ void serialHandler(char c) {
266 292
 
267 293
 #ifdef DEBUG
268 294
 	case 't': case 'T':
269
-		serialWriteString(getString(14));
270
-		serialWriteString(ltoa(getSystemTime(), buffer, 10));
271
-		serialWriteString("ms");
272
-		if (getSystemTime() > 1000) {
273
-			serialWriteString(" (");
274
-			serialWriteString(itoa(getSystemTime() / 1000, buffer, 10));
275
-			itoa(getSystemTime() % 1000, buffer, 10);
276
-			if (buffer[0] != '\0')
277
-				serialWrite('.');
278
-			if (buffer[2] == '\0') 
279
-				serialWrite('0');
280
-			if (buffer[1] == '\0')
281
-				serialWrite('0');
282
-			if (buffer[0] != '\0')
283
-				serialWriteString(buffer);
284
-			serialWriteString("s)\n");
285
-		} else {
286
-			serialWrite('\n');
287
-		}
295
+		printTime();
288 296
 		break;
289 297
 
290 298
 	case 'a': case 'A':
@@ -349,16 +357,27 @@ void serialHandler(char c) {
349 357
 					for (z = 0; z < 8; z++) {
350 358
 						defaultImage[y + (i * 8)] |= (1 << z);
351 359
 						setImage(defaultImage);
352
-						while (isFinished() == 0);
360
+						while (isFinished() == 0) {
361
+							if (serialHasChar()) {
362
+								goto killMeForIt; // Yes I know...
363
+								// But I need to break out of 2 while Loops...
364
+							}
365
+						}
353 366
 					}
354 367
 					defaultImage[y + (i * 8)] = 0;
355 368
 				}
356 369
 			}
357
-			if (serialHasChar()) {
358
-				break;
359
-			}
360 370
 		}
361 371
 		break;
372
+killMeForIt:
373
+		serialGet();
374
+		serialWriteString(getString(25));
375
+		break;
376
+
377
+	case 'I': case 'i':
378
+		serialWriteString(ltoa(getTriggerCount(), buffer, 10));
379
+		serialWrite('\n');
380
+		break;
362 381
 #endif
363 382
 
364 383
 	default:
@@ -369,6 +388,28 @@ void serialHandler(char c) {
369 388
 }
370 389
 
371 390
 #ifdef DEBUG
391
+void printTime(void) {
392
+	serialWriteString(getString(14));
393
+	serialWriteString(ltoa(getSystemTime(), buffer, 10));
394
+	serialWriteString("ms");
395
+	if (getSystemTime() > 1000) {
396
+		serialWriteString(" (");
397
+		serialWriteString(itoa(getSystemTime() / 1000, buffer, 10));
398
+		itoa(getSystemTime() % 1000, buffer, 10);
399
+		if (buffer[0] != '\0')
400
+			serialWrite('.');
401
+		if (buffer[2] == '\0') 
402
+			serialWrite('0');
403
+		if (buffer[1] == '\0')
404
+			serialWrite('0');
405
+		if (buffer[0] != '\0')
406
+			serialWriteString(buffer);
407
+		serialWriteString("s)\n");
408
+	} else {
409
+		serialWrite('\n');
410
+	}
411
+}
412
+
372 413
 void sendAudioData(void) {
373 414
 	uint8_t i;
374 415
 	uint8_t *audioData = getAudioData();
@@ -383,7 +424,6 @@ void sendAudioData(void) {
383 424
 			serialWriteString(buffer);
384 425
 			serialWrite('\n');
385 426
 		}
386
-		free(audioData);
387 427
 	}
388 428
 }
389 429
 #endif

+ 3
- 3
CubeFirmware/mem.c View File

@@ -28,6 +28,7 @@
28 28
 #include "mem.h"
29 29
 #ifdef DEBUG
30 30
 #include "serial.h"
31
+#include "strings.h"
31 32
 #endif
32 33
 
33 34
 // address is a number between (inclusive) zero and 131071
@@ -55,8 +56,7 @@ uint8_t memGetByte(uint32_t address) {
55 56
 // Free returned memory!
56 57
 uint8_t *memGetBytes(uint32_t address, uint8_t length) {
57 58
 	// We could use the High-Speed Mode of the FM24V10 here, but we don't, right now...
58
-	uint8_t addA, addB, memAddress = MEMTWIADDRESS, i;
59
-	uint8_t *ret;
59
+	uint8_t addA, addB, memAddress = MEMTWIADDRESS, i, *ret;
60 60
 	if (address >= 65536) {
61 61
 		// Address needs more than 16 bits, we have to set the PAGE bit in i2c address
62 62
 		memAddress |= 2;
@@ -66,7 +66,7 @@ uint8_t *memGetBytes(uint32_t address, uint8_t length) {
66 66
 	ret = (uint8_t *)malloc(length); // Allocate memory for values read
67 67
 	if (ret == NULL) {
68 68
 #ifdef DEBUG
69
-		serialWriteString("memGetBytes: No memory!\n");
69
+		serialWriteString(getString(24));
70 70
 #endif
71 71
 		return NULL;
72 72
 	}

+ 8
- 0
CubeFirmware/snake.c View File

@@ -48,6 +48,10 @@ uint8_t pixelSet(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf) {
48 48
 
49 49
 void displayBuffs(uint8_t *a, uint8_t *b) {
50 50
 	uint8_t *buf = (uint8_t *)malloc(64);
51
+	if (buf == NULL) {
52
+		serialWriteString(getString(24));
53
+		while(1);
54
+	}
51 55
 	uint8_t i;
52 56
 	for (i = 0; i < 64; i++) {
53 57
 		buf[i] = a[i] | b[i];
@@ -150,6 +154,10 @@ void clearTail(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf, uint8_t wrongDir)
150 154
 
151 155
 void snake() {
152 156
 	uint8_t *snake = (uint8_t *)malloc(64); // Snake
157
+	if (snake == NULL) {
158
+		serialWriteString(getString(24));
159
+		while(1);
160
+	}
153 161
 	uint8_t i, xPos = 3, yPos = 3, zPos = 3, dir = UP, length = 1;
154 162
 	uint8_t xCoin = 1, yCoin = 1, zCoin = 1;
155 163
 	char c;

+ 8
- 2
CubeFirmware/strings.c View File

@@ -53,15 +53,21 @@ char stringKillCount[] PROGMEM = "Killed Animation Counter!\n"; // 20
53 53
 char stringAccessError[] PROGMEM = "Could not access device!\n"; // 21
54 54
 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
+char stringNoMoreHeap[] PROGMEM = "Ran out of Heap... Bye\n"; // 24
57
+char stringKilledAnimation[] PROGMEM = "Animation aborted!\n"; // 25
58
+char stringHelp9[] PROGMEM = "(i)nterrupt count\n"; // 26
56 59
 
57
-#define STRINGNUM 24
60
+// Last index + 1
61
+#define STRINGNUM 27
58 62
 
59 63
 PGM_P stringTable[STRINGNUM] PROGMEM = { stringVersion, stringSelfTestError, stringInit,
60 64
 								stringAudioError, stringMemError, stringMemWriteError,
61 65
 								stringHelp1, stringHelp2, stringHelp3, stringHelp4, stringHelp5,
62 66
 								stringHelp6, stringHelp7, stringHelp8, stringTime, stringFrames,
63 67
 								stringByte, stringWritten, stringCount, stringSelfTest,
64
-								stringKillCount, stringAccessError, stringAudioData, stringSnakeControl };
68
+								stringKillCount, stringAccessError, stringAudioData,
69
+								stringSnakeControl, stringNoMoreHeap, stringKilledAnimation,
70
+								stringHelp9 };
65 71
 
66 72
 char stringNotFoundError[] PROGMEM = "String not found!\n";
67 73
 

Loading…
Cancel
Save