Browse Source

CubeFirmware debug mode improvements.

Now talks to FRAM most of the time…
Thomas Buck 12 years ago
parent
commit
36c3ed25b0

+ 0
- 1
AudioFirmware/eq.c View File

@@ -82,7 +82,6 @@ uint8_t *equalizerGet(void) {
82 82
 		_delay_us(STROBEDELAY);
83 83
 	}
84 84
 
85
-	eqLed(result);
86 85
 	return result;
87 86
 }
88 87
 

+ 1
- 1
AudioFirmware/main.c View File

@@ -73,7 +73,7 @@ int main(void) {
73 73
 			PORTB ^= (1 << PB2); // Toggle for every transaction
74 74
 			twiSetDataToSend(music);
75 75
 		}
76
-		eqLed(music);
76
+		// eqLed(music);
77 77
 		free(music);
78 78
 
79 79
 		// Heartbeat

+ 11
- 1
CubeFirmware/audio.c View File

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

+ 15
- 1
CubeFirmware/cube.c View File

@@ -26,7 +26,9 @@
26 26
 #include <stdlib.h>
27 27
 #include <util/atomic.h>
28 28
 
29
- #include "serial.h"
29
+#ifdef DEBUG
30
+#include "serial.h"
31
+#endif
30 32
 #include "cube.h"
31 33
 
32 34
 #ifndef F_CPU
@@ -87,10 +89,22 @@ void initCube(void) {
87 89
 	// We just assume this works, because after reset,
88 90
 	// enough Memory should be available...
89 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
+	}
90 98
 
91 99
 	for(ctr = 0; ctr < 8; ctr++) {
92 100
 		// Same reasoning here...
93 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
+		}
94 108
 	}
95 109
 }
96 110
 

+ 107
- 19
CubeFirmware/main.c View File

@@ -28,7 +28,11 @@
28 28
 #define OK 0x42
29 29
 #define ERROR 0x23
30 30
 
31
-#define VERSION "8^3 LED-Cube v1\n"
31
+#ifdef DEBUG
32
+#define VERSION "v2 (Debug Build)\nNOT COMPATIBLE WITH CubeControl!\n"
33
+#else
34
+#define VERSION "v2\n"
35
+#endif
32 36
 
33 37
 #include <avr/io.h>
34 38
 #include <util/delay.h>
@@ -41,9 +45,22 @@
41 45
 #include "cube.h"
42 46
 #include "time.h"
43 47
 #include "audio.h"
48
+#include "mem.h"
44 49
 #include "memLayer.h"
45 50
 #include "twi.h"
46 51
 
52
+#define NOERROR 0
53
+// Audio does not answer
54
+#define AUDIOERROR 1
55
+// Memory does not answer
56
+#define MEMORYERROR 2
57
+// Memory not writeable
58
+#define MEMORYWRITEERROR 4
59
+
60
+// x = errorcode, e = error definition, not NOERROR
61
+#define ISERROR(x, e) ((x) & (e))
62
+
63
+uint8_t selfTest(void);
47 64
 void serialHandler(char c);
48 65
 void sendAudioData(void);
49 66
 void recieveAnimations(void);
@@ -53,6 +70,9 @@ inline void setPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t **buf);
53 70
 inline void clearPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t **buf);
54 71
 void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t **buf);
55 72
 void visualizeAudioData(uint8_t *audioData, uint8_t **imageData);
73
+#ifdef DEBUG
74
+void printErrors(uint8_t e);
75
+#endif
56 76
 
57 77
 uint8_t refreshAnimationCount = 1;
58 78
 uint8_t lastButtonState = 0;
@@ -76,6 +96,17 @@ int main(void) {
76 96
 	DDRC = 0xFF; // Latch Enable
77 97
 	DDRA = 0xFF; // Latch Data
78 98
 
99
+	i = selfTest();
100
+	if (i) {
101
+		// Something is not working
102
+#ifdef DEBUG
103
+		serialWriteString("Self-Test Error: 0b");
104
+		serialWriteString(itoa(i, buffer, 2));
105
+		serialWrite('\n');
106
+		printErrors(i);
107
+#endif
108
+	}
109
+
79 110
 	imageData = (uint8_t **)malloc(8 * sizeof(uint8_t *));
80 111
 	for (i = 0; i < 8; i++) {
81 112
 		imageData[i] = (uint8_t *)malloc(8 * sizeof(uint8_t));
@@ -132,14 +163,66 @@ int main(void) {
132 163
 	return 0;
133 164
 }
134 165
 
166
+uint8_t selfTest(void) {
167
+	uint8_t result = NOERROR;
168
+	
169
+	uint8_t *data = getAudioData();
170
+	if (data == NULL) {
171
+		result |= AUDIOERROR;
172
+	} else {
173
+		free(data);
174
+	}
175
+
176
+	data = memGetBytes(0, 1);
177
+	if (data == NULL) {
178
+		result |= MEMORYERROR;
179
+	} else {
180
+		free(data);
181
+	}
182
+
183
+	setGeneralPurposeByte(0, 0x42);
184
+	if (getGeneralPurposeByte(0) != 0x42) {
185
+		result |= MEMORYWRITEERROR;
186
+	}
187
+
188
+	return result;
189
+}
190
+
191
+#ifdef DEBUG
192
+void printErrors(uint8_t e) {
193
+	if (ISERROR(e, AUDIOERROR)) {
194
+		serialWriteString(" => No answer from Audio!\n");
195
+	}
196
+	if (ISERROR(e, MEMORYERROR)) {
197
+		serialWriteString(" => No answer from Memory!\n");
198
+	}
199
+	if (ISERROR(e, MEMORYWRITEERROR)) {
200
+		serialWriteString(" => Can't write to Memory!\n");
201
+	}
202
+}
203
+#endif
204
+
135 205
 void serialHandler(char c) {
136 206
 	// Used letters:
137 207
 	// a, c, d, g, s, t, v, x
208
+#ifdef DEBUG
209
+	serialWrite(c);
210
+	serialWriteString(": ");
211
+#endif
212
+
138 213
 	switch(c) {
139 214
 	case OK:
140 215
 		serialWrite(OK);
141 216
 		break;
142 217
 
218
+	case 'h': case 'H':
219
+		serialWriteString("(d)elete, (g)et anims, (s)et anims, (v)ersion\n");
220
+#ifdef DEBUG
221
+		serialWriteString("(t)ime, (a)udio, (c)ount, (x)Custom count\n");
222
+		serialWriteString("(y)Set fixed animation count\n");
223
+#endif
224
+		break;
225
+
143 226
 	case 'd': case 'D':
144 227
 		clearMem();
145 228
 		serialWrite(OK);
@@ -157,6 +240,7 @@ void serialHandler(char c) {
157 240
 		serialWriteString(VERSION);
158 241
 		break;
159 242
 
243
+#ifdef DEBUG
160 244
 	case 't': case 'T':
161 245
 		serialWriteString("System Time: ");
162 246
 		serialWriteString(ltoa(getSystemTime(), buffer, 10));
@@ -190,42 +274,46 @@ void serialHandler(char c) {
190 274
 
191 275
 	case 'x': case 'X':
192 276
 		// Get byte, store as animation count
193
-		serialWriteString("Send a byte...");
277
+		serialWriteString("Send a byte... ");
194 278
 		while (!serialHasChar());
195
-		setAnimationCount(serialGet());
196
-		serialWriteString(" Byte written!\n");
279
+		c = serialGet();
280
+		setAnimationCount(c);
281
+		serialWriteString(itoa(c, buffer, 10));
282
+		serialWriteString(" written!\n");
197 283
 		break;
198 284
 
199
-	case '\n':
200
-		serialWriteString(VERSION);
201
-		serialWriteString("See xythobuz.org for more Infos!\n");
285
+	case 'y': case 'Y':
286
+		setAnimationCount(0x2201);
287
+		serialWriteString("Animation count now 8705!\n");
202 288
 		break;
289
+#endif
203 290
 
204 291
 	default:
205 292
 		serialWrite(ERROR);
206 293
 		break;
207 294
 	}
295
+	// c was used as temp var and does not contain the char anymore...!
208 296
 }
209 297
 
298
+#ifdef DEBUG
210 299
 void sendAudioData(void) {
211 300
 	uint8_t i;
212 301
 	uint8_t *audioData = getAudioData();
213 302
 	if (audioData == NULL) {
214 303
 		serialWriteString("Could not access device!\n");
215
-		return;
216
-	}
217
-
218
-	serialWriteString("Audio Data:\n");
219
-	for (i = 0; i < 7; i++) {
220
-		serialWrite(i + '0');
221
-		serialWriteString(": ");
222
-		itoa(audioData[i], buffer, 10);
223
-		serialWriteString(buffer);
224
-		serialWrite('\n');
304
+	} else {
305
+		serialWriteString("Audio Data:\n");
306
+		for (i = 0; i < 7; i++) {
307
+			serialWrite(i + '0');
308
+			serialWriteString(": ");
309
+			itoa(audioData[i], buffer, 10);
310
+			serialWriteString(buffer);
311
+			serialWrite('\n');
312
+		}
313
+		free(audioData);
225 314
 	}
226
-
227
-	free(audioData);
228 315
 }
316
+#endif
229 317
 
230 318
 void recieveAnimations() {
231 319
 	uint8_t animCount, a, frameCount, f, i;

+ 1
- 3
CubeFirmware/makefile View File

@@ -19,6 +19,7 @@
19 19
 # To rebuild project do "make clean" then "make all".
20 20
 #
21 21
 
22
+CDEFS = -D DEBUG
22 23
 
23 24
 # MCU name
24 25
 MCU = atmega32
@@ -77,9 +78,6 @@ EXTRAINCDIRS =
77 78
 # gnu99 - c99 plus GCC extensions
78 79
 CSTANDARD = -std=gnu99
79 80
 
80
-# Place -D or -U options here
81
-CDEFS = -D DEBUG
82
-
83 81
 # Place -I options here
84 82
 CINCS =
85 83
 

+ 10
- 0
CubeFirmware/mem.c View File

@@ -20,11 +20,15 @@
20 20
  * You should have received a copy of the GNU General Public License
21 21
  * along with LED-Cube.  If not, see <http://www.gnu.org/licenses/>.
22 22
  */
23
+
23 24
 #include <avr/io.h>
24 25
 #include <stdlib.h>
25 26
 #include <stdint.h>
26 27
 #include "twi.h"
27 28
 #include "mem.h"
29
+#ifdef DEBUG
30
+#include "serial.h"
31
+#endif
28 32
 
29 33
 // address is a number between (inclusive) zero and 131071
30 34
 uint8_t memGetByte(uint32_t address) {
@@ -60,6 +64,12 @@ uint8_t *memGetBytes(uint32_t address, uint8_t length) {
60 64
 	addA = memAddress & 0xFF00;
61 65
 	addB = memAddress & 0xFF;
62 66
 	ret = (uint8_t *)malloc(length); // Allocate memory for values read
67
+	if (ret == NULL) {
68
+#ifdef DEBUG
69
+		serialWriteString("memGetBytes: No memory!\n");
70
+#endif
71
+		return NULL;
72
+	}
63 73
 
64 74
 	if (i2c_start(memAddress | I2C_WRITE) == 0) {
65 75
 		i2c_write(addA);

+ 22
- 5
CubeFirmware/memLayer.c View File

@@ -45,12 +45,29 @@ void clearMem() {
45 45
 }
46 46
 
47 47
 uint16_t getAnimationCount() {
48
-	uint16_t animationCount = memGetByte(0);
49
-	animationCount |= (memGetByte(1) << 8);
50
-	return animationCount;
48
+	uint8_t lsb = memGetByte(0);
49
+	uint8_t msb = memGetByte(1);
50
+	uint16_t animationCount = (uint16_t)lsb;
51
+	return (animationCount | (((uint16_t)(msb)) << 8));
51 52
 }
52 53
 
53 54
 void setAnimationCount(uint16_t c) {
54
-	memWriteByte(0, (uint8_t)(c & 0x00FF));
55
-	memWriteByte(0, (uint8_t)((c & 0xFF00) >> 8));
55
+	uint8_t lsb = (uint8_t)(c & 0x00FF);
56
+	uint8_t msb = (uint8_t)((c & 0xFF00) >> 8);
57
+	memWriteByte(0, lsb);
58
+	memWriteByte(1, msb);
59
+}
60
+
61
+void setGeneralPurposeByte(uint8_t address, uint8_t data) {
62
+	if (address < 30) {
63
+		memWriteByte(address + 2, data);
64
+	}
65
+}
66
+
67
+uint8_t getGeneralPurposeByte(uint8_t address) {
68
+	if (address < 30) {
69
+		return memGetByte(address + 2);
70
+	} else {
71
+		return 0;
72
+	}
56 73
 }

+ 4
- 2
CubeFirmware/memLayer.h View File

@@ -23,12 +23,14 @@
23 23
 
24 24
 // Free after usage!
25 25
 uint8_t *getFrame(uint16_t frameNumber);
26
-
27 26
 // 65 bytes framedata, data and duration...
28 27
 void setFrame(uint16_t frameNumber, uint8_t *frameData);
29 28
 
30 29
 void clearMem(void);
31 30
 
32 31
 uint16_t getAnimationCount(void);
32
+void setAnimationCount(uint16_t c);
33 33
 
34
-void setAnimationCount(uint16_t c);
34
+// address 0 - 29
35
+void setGeneralPurposeByte(uint8_t address, uint8_t data);
36
+uint8_t getGeneralPurposeByte(uint8_t address);

BIN
Hardware/Layout.png View File


Loading…
Cancel
Save