Pārlūkot izejas kodu

Some AudioFirmware fixes.

Rewrote MSGEQ7 Algorithm. Removed stdlib.h dependency, thereby reducing
the flash needed by 50%. Interrupts no longer enabled in twislave, but
in main.
Thomas Buck 12 gadus atpakaļ
vecāks
revīzija
1401f65fb7
6 mainītis faili ar 74 papildinājumiem un 129 dzēšanām
  1. 0
    1
      AudioFirmware/adc.c
  2. 29
    25
      AudioFirmware/eq.c
  3. 2
    0
      AudioFirmware/eq.h
  4. 7
    22
      AudioFirmware/main.c
  5. 35
    78
      AudioFirmware/main.hex
  6. 1
    3
      AudioFirmware/twislave.c

+ 0
- 1
AudioFirmware/adc.c Parādīt failu

@@ -26,7 +26,6 @@
26 26
 #include "adc.h"
27 27
 
28 28
 void adcInit(void) {
29
-	DDRC &= ~(3);
30 29
 	ADMUX = 0;
31 30
 	ADMUX |= (1 << REFS0); // Ref. Voltage: Vcc
32 31
 	ADCSRA |= (1 << ADPS2) | (1 << ADPS0); // Prescaler 64

+ 29
- 25
AudioFirmware/eq.c Parādīt failu

@@ -22,49 +22,53 @@
22 22
  */
23 23
 #include <avr/io.h>
24 24
 #include <stdint.h>
25
-#include <stdlib.h>
26 25
 #include <util/delay.h>
27 26
 
28 27
 #include "eq.h"
29 28
 #include "adc.h"
30 29
 
31
-#define RESETDELAY 1 /* in µs */
32
-#define RESETSTROBEDELAY 72 /* in µs */
33
-#define STROBEDELAY 18 /* in µs */
34
-#define READDELAY 36 /* in µs */
30
+#define Trs 72
31
+#define Ts 19
32
+#define To 36
35 33
 
36
-#if ((STROBEDELAY * 2) + READDELAY) < 72
37
-#error Strobe to Strobe Delay too short
38
-#endif
34
+uint8_t result[7] = {128, 128, 128, 128, 128, 128, 128};
39 35
 
40
-void equalizerInit(void) {
41
-	DDRC |= 12; // Strobe: PC2
42
-				// Reset: PC3
43
-				// Out: ADC0
44
-	PORTC |= (1 << PC3); // Reset enabled
45
-	_delay_us(RESETDELAY);
36
+#define RESETPORT PORTC
37
+#define STROBEPORT PORTC
38
+#define RESET PC3
39
+#define STROBE PC2
40
+
41
+#define RESETON RESETPORT |= (1 << RESET)
42
+#define RESETOFF RESETPORT &= ~(1 << RESET)
43
+#define STROBEON STROBEPORT |= (1 << STROBE)
44
+#define STROBEOFF STROBEPORT &= ~(1 << STROBE)
46 45
 
46
+void equalizerInit(void) {
47
+	RESETON;
48
+	STROBEOFF;
47 49
 }
48 50
 
49 51
 uint8_t *equalizerGet(void) {
50
-	uint8_t *result = (uint8_t *)malloc(7); // Has to work... If not, were screwed anyway :)
51 52
 	uint8_t i, offset = getOffset();
52 53
 
53
-	PORTC &= ~(1 << PC3); // Disable reset
54
-	_delay_us(RESETSTROBEDELAY); // Wait trs
54
+	RESETOFF;
55
+	_delay_us(Trs);
55 56
 
56 57
 	for (i = 0; i < 7; i++) {
57
-		PORTC |= (1 << PC2); // Strobe '1'
58
-		_delay_us(STROBEDELAY); // create minimal pulse width
59
-		PORTC &= ~(1 << PC2);
60
-		_delay_us(READDELAY); // Wait for result to appear
61
-		adcStartConversion(0x00);
58
+		STROBEON;
59
+		_delay_us(Ts);
60
+		STROBEOFF;
61
+		_delay_us(To); // Wait for result
62
+
63
+		// Get result, takes ca. 29ms
64
+		adcStartConversion(0);
62 65
 		result[i] = offset + adcGetByte();
63
-		_delay_us(STROBEDELAY);
64 66
 	}
65 67
 
66
-	PORTC |= (1 << PC3); // Enable reset
67
-
68
+	RESETON;
69
+	asm volatile ("nop");
70
+	asm volatile ("nop");   // Ensure minimal reset pulse width
71
+							// 2 NOPs at 16MHz are enough...
68 72
 	return result;
69 73
 }
70 74
 

+ 2
- 0
AudioFirmware/eq.h Parādīt failu

@@ -21,6 +21,8 @@
21 21
  * along with LED-Cube.  If not, see <http://www.gnu.org/licenses/>.
22 22
  */
23 23
 
24
+extern uint8_t result[7];
25
+
24 26
 void equalizerInit(void);
25 27
 
26 28
 // 7 elements in byte array

+ 7
- 22
AudioFirmware/main.c Parādīt failu

@@ -20,10 +20,10 @@
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
-#include <stdlib.h>
24
-#include <stdint.h>
25 23
 #include <avr/io.h>
24
+#include <stdint.h>
26 25
 #include <util/delay.h>
26
+#include <avr/interrupt.h>
27 27
 #include <avr/wdt.h>
28 28
 
29 29
 #include "adc.h"
@@ -34,50 +34,35 @@
34 34
 #define F_CPU 16000000L
35 35
 #endif
36 36
 
37
-void blinkStatus(void) {
38
-	PORTB |= (1 << PB2);
39
-	PORTB &= ~(1 << PB1);
40
-	_delay_ms(250);
41
-	PORTB &= ~(1 << PB2);
42
-	PORTB |= (1 << PB1);
43
-	_delay_ms(250);
44
-}
45
-
46 37
 int main(void) {
47
-	uint8_t *music;
38
+	uint8_t *music = result;
48 39
 
49 40
 	MCUCSR = 0;
50 41
 	wdt_disable();
51 42
 
52
-	DDRB = 6;
53
-	DDRC = 12;
54
-	DDRD = 0;
43
+	DDRB = 6; // LEDs as Output
44
+	DDRC = 12; // MSGEQ7 Pins as Output
45
+	DDRD = 0; // Nothing on this Port!
55 46
 	
56 47
 	twiInit(0x42); // All TWI action happens completely in the background.
57 48
 	adcInit();
58 49
 	equalizerInit();
59 50
 
60
-	blinkStatus();
61
-	blinkStatus();
51
+	sei(); // Enable interrupts
62 52
 
63 53
 	wdt_enable(WDTO_500MS); // Enable watchdog reset after 500ms
64 54
 
65
-	music = equalizerGet();
66 55
 	twiSetDataToSend(music);
67
-	free(music);
68 56
 
69 57
 	while (1) {
70 58
 		if (twiDataWasSent()) {
71 59
 			PORTB ^= (1 << PB2); // Toggle for every transaction
72 60
 			music = equalizerGet();
73 61
 			twiSetDataToSend(music);
74
-			free(music);
75 62
 		}
76 63
 
77 64
 		// Heartbeat
78 65
 		PORTB ^= (1 << PB1);
79
-		_delay_ms(1); // Everything locks if this is removed :(
80
-		// still don't know why...
81 66
 
82 67
 		wdt_reset();
83 68
 	}

+ 35
- 78
AudioFirmware/main.hex Parādīt failu

@@ -1,82 +1,39 @@
1 1
 :1000000012C02CC02BC02AC029C028C027C026C0BF
2 2
 :1000100025C024C023C022C021C020C01FC01EC0D4
3
-:100020001DC0EBC01BC011241FBECFE5D4E0DEBF56
4
-:10003000CDBF10E0A0E6B0E0E6EFF4E002C005902E
3
+:100020001DC0A1C01BC011241FBECFE5D4E0DEBFA0
4
+:10003000CDBF10E0A0E6B0E0EEE4F2E002C0059033
5 5
 :100040000D92AE36B107D9F710E0AEE6B0E001C0D0
6
-:100050001D92A437B107E1F719D04BC2D1CFC29A94
7
-:10006000C19884EC99E020E931E0F9013197F1F78A
8
-:100070000197D9F7C298C19A84EC99E020E931E060
9
-:10008000F9013197F1F70197D9F70895EF92FF92AF
10
-:100090000F931F93CF93DF9314BE88E10FB6F894AC
11
-:1000A00081BD11BC0FBE86E087BB8CE084BB11BA5A
12
-:1000B00082E4F7D042D054D0D2DFD1DF2DE088E106
13
-:1000C00090E00FB6F894A89581BD0FBE21BD55D024
14
-:1000D0008C01F1D0C801B5D194E0E92E82E0F82E70
15
-:1000E000C0EADFE0F9D0882341F088B38E2588BBD1
16
-:1000F00044D08C01E0D0C801A4D188B38F2588BB3F
17
-:10010000CE010197F1F7A895EDCF97B1907F97B900
18
-:1001100097B18F70892B87B9369A089586B18295E9
19
-:100120008695869583708095817008953699FECF67
20
-:10013000369824B135B1C901089584B38C7F84BB4E
21
-:1001400017B83E9A86B1856086B9379A80E0DDDFC0
22
-:10015000EDDF0895EBDF9695879596958795089541
23
-:1001600084B38C6084BBAB9A85E08A95F1F70895DF
24
-:1001700081E0CBDFEFDF86950895BF92CF92DF92CB
25
-:10018000EF92FF920F931F93CF93DF9387E090E05E
26
-:10019000ABD0F92E182FC82EEBDFD82EAB9880E20B
27
-:1001A00091E00197F1F7812F9F2DEC0190E6E92E68
28
-:1001B00080ECB82E8E01095F1F4FAA9A8E2D8A956A
29
-:1001C000F1F7AA988B2D8A95F1F780E09EDFC2DFC8
30
-:1001D0008D0D89938E2D8A95F1F7C017D10769F798
31
-:1001E000AB9A8C2D9F2DDF91CF911F910F91FF9096
32
-:1001F000EF90DF90CF90BF9008951F920F920FB6AF
33
-:100200000F9211248F939F93EF93FF9381B190E00E
34
-:10021000887F883B9105C1F0893B91053CF4883A81
35
-:10022000910559F0803B910581F507C0803C91050F
36
-:1002300031F1883C910549F522C081E080936F003F
37
-:100240008091600083B915C090916F00963040F0A6
38
-:1002500010926F008091660083B986B78F7B11C0C2
39
-:10026000E92FF0E0E05AFF4F808183B99F5F9093C0
40
-:100270006F0086B7806486BF10926E0006C086B796
41
-:10028000806486BF81E080936E0086B7806886BFF9
42
-:10029000FF91EF919F918F910F900FBE0F901F9044
43
-:1002A0001895882311F481E001C08E7F82B985E41E
44
-:1002B00086BF78940895AC0120E030E0F901E05A5F
45
-:1002C000FF4FDA01A20FB31F8C9180832F5F3F4F46
46
-:1002D0002730310599F7089580916E00882319F031
47
-:1002E00010926E0081E00895CF93DF93BC018230BD
48
-:1002F000910510F462E070E0A0917200B09173007B
49
-:10030000ED01E0E0F0E040E050E021C0888199811B
50
-:100310008617970769F48A819B81309719F0938338
51
-:10032000828304C09093730080937200FE0134C0F6
52
-:100330006817790738F44115510519F084179507A6
53
-:1003400008F4AC01FE018A819B819C01E9012097A0
54
-:10035000E9F641155105A9F1CA01861B970B0497CF
55
-:1003600008F4BA01E0E0F0E02AC08D919C91119769
56
-:1003700084179507F9F46417750781F412968D9127
57
-:100380009C911397309719F09383828304C09093C4
58
-:10039000730080937200FD0132964FC0CA01861B24
59
-:1003A000970BFD01E80FF91F6193719302978D93ED
60
-:1003B0009C9343C0FD01828193819C01D9011097D8
61
-:1003C000A1F68091700090917100892B41F4809189
62
-:1003D000690090916A009093710080937000409141
63
-:1003E0006B0050916C004115510541F44DB75EB75B
64
-:1003F0008091670090916800481B590B2091700014
65
-:100400003091710024173507B0F4CA01821B930B99
66
-:100410008617970780F0AB014E5F5F4F84179507F3
67
-:1004200050F0420F531F5093710040937000F90138
68
-:100430006193719302C0E0E0F0E0CF01DF91CF91D2
69
-:100440000895CF93DF93009709F450C0EC012297F1
70
-:100450001B821A82A0917200B0917300109709F16B
71
-:1004600040E050E0AC17BD0708F1BB83AA83FE0152
72
-:1004700021913191E20FF31FAE17BF0779F48D91EF
73
-:100480009C911197280F391F2E5F3F4F3983288386
74
-:1004900012968D919C9113979B838A8341155105E8
75
-:1004A00071F4D0937300C093720020C012968D91A6
76
-:1004B0009C911397AD01009711F0DC01D3CFFA01A5
77
-:1004C000D383C28321913191E20FF31FCE17DF074F
78
-:1004D00069F488819981280F391F2E5F3F4FFA01F7
79
-:1004E000318320838A819B8193838283DF91CF91A3
80
-:0604F0000895F894FFCF0F
81
-:0E04F600424242424242422000740000000096
6
+:100050001D92A037B107E1F702D0F7C0D1CFCF93FF
7
+:10006000DF9314BE88E10FB6F89481BD11BC0FBEBA
8
+:1000700086E087BB8CE084BB11BA82E4C8D032D062
9
+:1000800041D078942DE088E190E00FB6F894A895DF
10
+:1000900081BD0FBE21BD80E690E0C2D0D4E0C2E0B9
11
+:1000A000CDD0882329F088B38D2788BB33D0B8D032
12
+:1000B00088B38C2788BBA895F3CF97B1907F97B969
13
+:1000C00097B18F70892B87B9369A089581E0369958
14
+:1000D00080E00895FBDF8823E9F3369824B135B139
15
+:1000E000C901089517B83E9A86B1856086B9379AD6
16
+:1000F00080E0E3DFEFDF0895EDDF96958795969535
17
+:1001000087950895AB9AAA98089581E0D6DFF4DF29
18
+:10011000869508951F93CF93DF93F7DF182FAB9841
19
+:100120008FE191E00197F1F700C00000C0E6D0E058
20
+:10013000AA9A95E69A95F1F70000AA9880EC8A951C
21
+:10014000F1F780E0BADFD8DF810F899390E0C736FE
22
+:10015000D90771F7AB9A00000000CE010797DF9135
23
+:10016000CF911F9108951F920F920FB60F921124F5
24
+:100170008F939F93EF93FF9381B190E0887F883BAB
25
+:100180009105C1F0893B91053CF4883A910559F0FD
26
+:10019000803B910581F507C0803C910531F1883C99
27
+:1001A000910549F522C081E080936F00809167003E
28
+:1001B00083B915C080916F00863040F010926F00B7
29
+:1001C00080916D0083B986B78F7B11C0E82FF0E076
30
+:1001D000E959FF4F908193B98F5F80936F0086B785
31
+:1001E000806486BF10926E0006C086B7806486BFAA
32
+:1001F00081E080936E0086B7806886BFFF91EF91A3
33
+:100200009F918F910F900FBE0F901F90189588238C
34
+:1002100011F481E001C08E7F82B985E486BF089524
35
+:10022000282F392FD901E7E6F0E08D91819380E006
36
+:10023000EE36F807D1F710926E00089581E09091A4
37
+:0E0240006E00992309F480E00895F894FFCF32
38
+:0E024E00808080808080804242424242424254
82 39
 :00000001FF

+ 1
- 3
AudioFirmware/twislave.c Parādīt failu

@@ -68,8 +68,6 @@ void twiInit(uint8_t address) {
68 68
 		TWAR = address & ~(1);
69 69
 	}
70 70
 	TWCR = (1 << TWEN) | (1 << TWEA) | (1 << TWIE); // Enable twi, interrupt and acknowledge bit
71
-	
72
-	sei(); // Enable interrupts
73 71
 }
74 72
 
75 73
 void twiSetDataToSend(uint8_t *d) { // 7 bytes
@@ -80,11 +78,11 @@ void twiSetDataToSend(uint8_t *d) { // 7 bytes
80 78
 	for (i = 0; i < 7; i++) {
81 79
 		data[i] = d[i];
82 80
 	}
81
+	dataSent = 0;
83 82
 }
84 83
 
85 84
 uint8_t twiDataWasSent(void) {
86 85
 	if (dataSent != 0) {
87
-		dataSent = 0;
88 86
 		return 1;
89 87
 	} else {
90 88
 		return 0;

Notiek ielāde…
Atcelt
Saglabāt