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
 #include "serial.h"
30
 #include "serial.h"
31
 #endif
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
 uint8_t *getAudioData(void) {
36
 uint8_t *getAudioData(void) {
35
 	// We read 7 bytes from our Audio µC
37
 	// We read 7 bytes from our Audio µC
36
 	uint8_t i;
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
 	if (i2c_start(TWIADDRESSAUDIO | I2C_READ) == 0) {
40
 	if (i2c_start(TWIADDRESSAUDIO | I2C_READ) == 0) {
46
 		for (i = 0; i < 6; i++) {
41
 		for (i = 0; i < 6; i++) {
47
 			ret[i] = i2c_readAck();
42
 			ret[i] = i2c_readAck();
50
 		i2c_stop();
45
 		i2c_stop();
51
 		return ret;
46
 		return ret;
52
 	} else {
47
 	} else {
53
-		free(ret);
54
 		return NULL;
48
 		return NULL;
55
 	}
49
 	}
56
 }
50
 }

+ 1
- 1
CubeFirmware/audio.h View File

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

+ 29
- 43
CubeFirmware/cube.c View File

36
 #endif
36
 #endif
37
 
37
 
38
 // Should be 41666
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
 volatile uint8_t changedFlag = 0;
59
 volatile uint8_t changedFlag = 0;
45
 volatile uint8_t imgFlag = 0;
60
 volatile uint8_t imgFlag = 0;
46
 volatile uint8_t layer = 0;
61
 volatile uint8_t layer = 0;
47
 
62
 
48
-inline void delay_ns(int16_t ns);
49
 inline void isrCall(void);
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
 void setImage(uint8_t *img) {
71
 void setImage(uint8_t *img) {
52
 	uint8_t i, j;
72
 	uint8_t i, j;
53
 	ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
73
 	ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
79
 }
99
 }
80
 
100
 
81
 void initCube(void) {
101
 void initCube(void) {
82
-	uint8_t ctr = 0;
83
-
84
 	TCCR1A |= (1 << WGM12); // CTC Mode
102
 	TCCR1A |= (1 << WGM12); // CTC Mode
85
 	TCCR1B |= (1 << CS10); // Prescaler: 1
103
 	TCCR1B |= (1 << CS10); // Prescaler: 1
86
-	OCR1A = FIRSTCOUNT;
104
+	OCR1A = COUNT;
87
 	TIMSK = (1 << OCIE1A); // Enable Output Compare Interrupt
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
 void close(void) {
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
 	TIMSK &= ~(1 << OCIE1A); // Disable interrupt
109
 	TIMSK &= ~(1 << OCIE1A); // Disable interrupt
118
 }
110
 }
119
 
111
 
120
-// Count to FIRSTCOUNT SECONDCOUNT times...
121
 ISR(TIMER1_COMPA_vect) {
112
 ISR(TIMER1_COMPA_vect) {
122
 	isrCall();
113
 	isrCall();
114
+	timesTriggered++;
123
 }
115
 }
124
 
116
 
125
 // Data is sent to 8 Fet bits...
117
 // Data is sent to 8 Fet bits...
139
 inline void setLatch(uint8_t latchNr, uint8_t data) {
131
 inline void setLatch(uint8_t latchNr, uint8_t data) {
140
 	selectLatch(latchNr); // Activate current latch
132
 	selectLatch(latchNr); // Activate current latch
141
 	PORTA = data; // Put latch data
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
 inline void isrCall(void) {
137
 inline void isrCall(void) {
150
 		layer = 0;
142
 		layer = 0;
151
 		changedFlag = 0;
143
 		changedFlag = 0;
152
 	}
144
 	}
153
-	setFet(0);
145
+	// setFet(0);
154
 	for (; latchCtr < 8; latchCtr++) {
146
 	for (; latchCtr < 8; latchCtr++) {
155
 		setLatch(latchCtr, imgBuffer[layer][latchCtr]); // Put out all the data
147
 		setLatch(latchCtr, imgBuffer[layer][latchCtr]); // Put out all the data
156
 	}
148
 	}
164
 		imgFlag++; // Finished
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
  */
28
  */
29
 extern void initCube(void);
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
 extern void setImage(uint8_t *img); // img[64]
32
 extern void setImage(uint8_t *img); // img[64]
33
 extern uint8_t isFinished(void);
33
 extern uint8_t isFinished(void);
34
 extern void close(void);
34
 extern void close(void);
38
 // For debugging, not normal operation:
38
 // For debugging, not normal operation:
39
 extern void setFet(uint8_t data);
39
 extern void setFet(uint8_t data);
40
 extern void setLatch(uint8_t latchNr, uint8_t data);
40
 extern void setLatch(uint8_t latchNr, uint8_t data);
41
+extern uint32_t getTriggerCount(void);

+ 66
- 26
CubeFirmware/main.c View File

65
 #ifdef DEBUG
65
 #ifdef DEBUG
66
 void printErrors(uint8_t e);
66
 void printErrors(uint8_t e);
67
 uint8_t selfTest(void);
67
 uint8_t selfTest(void);
68
+void printTime(void);
68
 
69
 
69
 #include "snake.c"
70
 #include "snake.c"
70
 #endif
71
 #endif
88
 	uint8_t i, length = 0, lastMode;
89
 	uint8_t i, length = 0, lastMode;
89
 	uint16_t count;
90
 	uint16_t count;
90
 	uint64_t lastChecked;
91
 	uint64_t lastChecked;
92
+	uint8_t DebugDone = 0; // Bit 0: 10s int. count, Bit 1: switch idle display
91
 
93
 
92
 	initCube();
94
 	initCube();
93
 	serialInit(25, 8, NONE, 1);
95
 	serialInit(25, 8, NONE, 1);
136
 				audioData = getAudioData();
138
 				audioData = getAudioData();
137
 				if (audioData != NULL) {
139
 				if (audioData != NULL) {
138
 					imageData = (uint8_t *)malloc(64);
140
 					imageData = (uint8_t *)malloc(64);
141
+					if (imageData == NULL) {
142
+#ifdef DEBUG
143
+						serialWriteString(getString(24));
144
+#endif
145
+						while(1);
146
+					}
139
 					visualizeAudioData(audioData, imageData);
147
 					visualizeAudioData(audioData, imageData);
140
 					setImage(imageData);
148
 					setImage(imageData);
141
 					free(imageData);
149
 					free(imageData);
142
-					free(audioData);
143
 				}
150
 				}
144
 			}
151
 			}
145
 		} else {
152
 		} else {
164
 					setImage(imageData);
171
 					setImage(imageData);
165
 					free(imageData);
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
 			serialHandler((char)(serialGet()));
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
 		if ((getSystemTime() - lastChecked) > 150) {
199
 		if ((getSystemTime() - lastChecked) > 150) {
175
 			lastMode = audioModeSelected();
200
 			lastMode = audioModeSelected();
176
 			lastChecked = getSystemTime();
201
 			lastChecked = getSystemTime();
222
 
247
 
223
 void serialHandler(char c) {
248
 void serialHandler(char c) {
224
 	// Used letters:
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
 	uint8_t i, y, z;
251
 	uint8_t i, y, z;
227
 #ifdef DEBUG
252
 #ifdef DEBUG
228
 	serialWrite(c);
253
 	serialWrite(c);
244
 		serialWriteString(getString(11));
269
 		serialWriteString(getString(11));
245
 		serialWriteString(getString(12));
270
 		serialWriteString(getString(12));
246
 		serialWriteString(getString(13));
271
 		serialWriteString(getString(13));
272
+		serialWriteString(getString(26));
247
 #endif
273
 #endif
248
 		break;
274
 		break;
249
 
275
 
266
 
292
 
267
 #ifdef DEBUG
293
 #ifdef DEBUG
268
 	case 't': case 'T':
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
 		break;
296
 		break;
289
 
297
 
290
 	case 'a': case 'A':
298
 	case 'a': case 'A':
349
 					for (z = 0; z < 8; z++) {
357
 					for (z = 0; z < 8; z++) {
350
 						defaultImage[y + (i * 8)] |= (1 << z);
358
 						defaultImage[y + (i * 8)] |= (1 << z);
351
 						setImage(defaultImage);
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
 					defaultImage[y + (i * 8)] = 0;
367
 					defaultImage[y + (i * 8)] = 0;
355
 				}
368
 				}
356
 			}
369
 			}
357
-			if (serialHasChar()) {
358
-				break;
359
-			}
360
 		}
370
 		}
361
 		break;
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
 #endif
381
 #endif
363
 
382
 
364
 	default:
383
 	default:
369
 }
388
 }
370
 
389
 
371
 #ifdef DEBUG
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
 void sendAudioData(void) {
413
 void sendAudioData(void) {
373
 	uint8_t i;
414
 	uint8_t i;
374
 	uint8_t *audioData = getAudioData();
415
 	uint8_t *audioData = getAudioData();
383
 			serialWriteString(buffer);
424
 			serialWriteString(buffer);
384
 			serialWrite('\n');
425
 			serialWrite('\n');
385
 		}
426
 		}
386
-		free(audioData);
387
 	}
427
 	}
388
 }
428
 }
389
 #endif
429
 #endif

+ 3
- 3
CubeFirmware/mem.c View File

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

+ 8
- 0
CubeFirmware/snake.c View File

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

+ 8
- 2
CubeFirmware/strings.c View File

53
 char stringAccessError[] PROGMEM = "Could not access device!\n"; // 21
53
 char stringAccessError[] PROGMEM = "Could not access device!\n"; // 21
54
 char stringAudioData[] PROGMEM = "Audio Data:\n"; // 22
54
 char stringAudioData[] PROGMEM = "Audio Data:\n"; // 22
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... 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
 PGM_P stringTable[STRINGNUM] PROGMEM = { stringVersion, stringSelfTestError, stringInit,
63
 PGM_P stringTable[STRINGNUM] PROGMEM = { stringVersion, stringSelfTestError, stringInit,
60
 								stringAudioError, stringMemError, stringMemWriteError,
64
 								stringAudioError, stringMemError, stringMemWriteError,
61
 								stringHelp1, stringHelp2, stringHelp3, stringHelp4, stringHelp5,
65
 								stringHelp1, stringHelp2, stringHelp3, stringHelp4, stringHelp5,
62
 								stringHelp6, stringHelp7, stringHelp8, stringTime, stringFrames,
66
 								stringHelp6, stringHelp7, stringHelp8, stringTime, stringFrames,
63
 								stringByte, stringWritten, stringCount, stringSelfTest,
67
 								stringByte, stringWritten, stringCount, stringSelfTest,
64
-								stringKillCount, stringAccessError, stringAudioData, stringSnakeControl };
68
+								stringKillCount, stringAccessError, stringAudioData,
69
+								stringSnakeControl, stringNoMoreHeap, stringKilledAnimation,
70
+								stringHelp9 };
65
 
71
 
66
 char stringNotFoundError[] PROGMEM = "String not found!\n";
72
 char stringNotFoundError[] PROGMEM = "String not found!\n";
67
 
73
 

Loading…
Cancel
Save