Browse Source

Added Audio TWI Slave functionality

Thomas Buck 12 years ago
parent
commit
6221e19681
5 changed files with 98 additions and 88 deletions
  1. 2
    0
      AudioFirmware/main.c
  2. 0
    86
      AudioFirmware/main.hex
  3. 1
    0
      AudioFirmware/makefile
  4. 92
    0
      AudioFirmware/twislave.c
  5. 3
    2
      AudioFirmware/twislave.h

+ 2
- 0
AudioFirmware/main.c View File

@@ -27,6 +27,7 @@
27 27
 
28 28
 #include "adc.h"
29 29
 #include "eq.h"
30
+#include "twislave.h"
30 31
 
31 32
 #ifndef F_CPU
32 33
 #define F_CPU 16000000L
@@ -55,6 +56,7 @@ int main(void) {
55 56
 	DDRC = 0x0C;
56 57
 	DDRD = 0xFF;
57 58
 	
59
+	twiInit(0x42); // All TWI action happens completely in the background.
58 60
 	adcInit();
59 61
 	equalizerInit();
60 62
 

+ 0
- 86
AudioFirmware/main.hex View File

@@ -1,86 +0,0 @@
1
-:1000000012C02CC02BC02AC029C028C027C026C0BF
2
-:1000100025C024C023C022C021C020C01FC01EC0D4
3
-:100020001DC01CC01BC011241FBECFE5D4E0DEBF25
4
-:10003000CDBF10E0A0E6B0E0E8E2F5E002C0059038
5
-:100040000D92A437B107D9F710E0A4E7B0E001C0E2
6
-:100050001D92A837B107E1F764D064C2D1CF81E027
7
-:1000600078D09CD08F3710F0C19A0895C198089528
8
-:100070001F9313D1AC0110E020E030E0FA01E20F51
9
-:10008000F31F8081180F2F5F3F4F27303105B1F7E5
10
-:10009000CA01F0D11C3810F0C29A01C0C2981F9159
11
-:1000A0000895DF93CF93CDB7DEB727970FB6F894B7
12
-:1000B000DEBF0FBECDBFDE011196E0E6F0E087E0C7
13
-:1000C00001900D928150E1F7C29AC198CE0101963C
14
-:1000D00078D084EC99E020E931E0F9013197F1F72B
15
-:1000E0000197D9F7C298C19AFE013196CE010896C0
16
-:1000F0001192E817F907E1F7CE01019662D084EC7E
17
-:1001000099E020E931E0F9013197F1F70197D9F74A
18
-:1001100027960FB6F894DEBF0FBECDBFCF91DF910B
19
-:1001200008951F93CF93DF938FE387BB8CE084BB4D
20
-:100130008FEF81BB26D038D0B4DFB3DF12E0C0EA46
21
-:10014000DFE096DF88B3812788BBCE010197F1F706
22
-:10015000F8CF97B1907F97B997B18F70892B87B9F6
23
-:10016000369A089586B18295869586958370809596
24
-:10017000817008953699FECF369824B135B1C90102
25
-:10018000089584B38C7F84BB17B83E9A86B185608E
26
-:1001900086B9379A80E0DDDFEDDF0895EBDF9695D5
27
-:1001A000879596958795089584B38C6084BBAB9AA8
28
-:1001B00085E08A95F1F7089581E0CBDFEFDF869542
29
-:1001C0000895DF92EF92FF920F931F93DF93CF93E7
30
-:1001D000CDB7DEB727970FB6F894DEBF0FBECDBF01
31
-:1001E0007C01DE011196E7E6F0E087E001900D92D8
32
-:1001F0008150E1F7E1DF21E030E00F8002C0220F03
33
-:10020000331F0A94E2F7322FD22ED094DE011196DA
34
-:1002100040E050E0682F70E001E010E0F701E40FEB
35
-:10022000F51F8081FB01E80FF11DEF37F10584F028
36
-:10023000463059F022B3C8010C9002C0880F991FB4
37
-:100240000A94E2F7282B22BB14C088B3832B10C07A
38
-:10025000463061F022B3C8010C9002C0880F991F8C
39
-:100260000A94E2F78095822382BB03C088B38D2174
40
-:1002700088BB4F5F5F4F11964730510579F627963F
41
-:100280000FB6F894DEBF0FBECDBFCF91DF911F91A7
42
-:100290000F91FF90EF90DF900895BF92CF92DF9281
43
-:1002A000EF92FF920F931F93CF93DF9387E090E03D
44
-:1002B00034D0F92E182FE82E7FDFC82EAB9880E2BD
45
-:1002C00091E00197F1F7812F9F2DEC0190E6D92E57
46
-:1002D00080ECB82E8E01095F1F4FAA9A8D2D8A954A
47
-:1002E000F1F7AA9880E035DF8B2D8A95F1F756DF7C
48
-:1002F0008C0D89938D2D8A95F1F7C017D10769F779
49
-:10030000C7015FDFC701DF91CF911F910F91FF9070
50
-:10031000EF90DF90CF90BF900895CF93DF93BC0113
51
-:100320008230910510F462E070E0A0917600B09107
52
-:100330007700ED01E0E0F0E040E050E021C088818E
53
-:1003400099818617970769F48A819B81309719F004
54
-:100350009383828304C09093770080937600FE019C
55
-:1003600034C06817790738F44115510519F084171E
56
-:10037000950708F4AC01FE018A819B819C01E9018B
57
-:100380002097E9F641155105A9F1CA01861B970B83
58
-:10039000049708F4BA01E0E0F0E02AC08D919C9146
59
-:1003A000119784179507F9F46417750781F412966D
60
-:1003B0008D919C911397309719F09383828304C099
61
-:1003C0009093770080937600FD0132964FC0CA016A
62
-:1003D000861B970BFD01E80FF91F6193719302973C
63
-:1003E0008D939C9343C0FD01828193819C01D9012F
64
-:1003F0001097A1F68091740090917500892B41F4BB
65
-:1004000080917000909171009093750080937400BA
66
-:1004100040917200509173004115510541F44DB760
67
-:100420005EB780916E0090916F00481B590B209130
68
-:1004300074003091750024173507B0F4CA01821B8F
69
-:10044000930B8617970780F0AB014E5F5F4F8417C1
70
-:10045000950750F0420F531F50937500409374005E
71
-:10046000F9016193719302C0E0E0F0E0CF01DF9108
72
-:10047000CF910895CF93DF93009709F450C0EC011A
73
-:1004800022971B821A82A0917600B0917700109774
74
-:1004900009F140E050E0AC17BD0708F1BB83AA8327
75
-:1004A000FE0121913191E20FF31FAE17BF0779F4DE
76
-:1004B0008D919C911197280F391F2E5F3F4F3983E3
77
-:1004C000288312968D919C9113979B838A83411563
78
-:1004D000510571F4D0937700C093760020C0129636
79
-:1004E0008D919C911397AD01009711F0DC01D3CF52
80
-:1004F000FA01D383C28321913191E20FF31FCE170A
81
-:10050000DF0769F488819981280F391F2E5F3F4FDB
82
-:10051000FA01318320838A819B8193838283DF91D7
83
-:08052000CF910895F894FFCF7C
84
-:10052800FFFFFFFFFFFFFF0203040506070020008F
85
-:040538007800000047
86
-:00000001FF

+ 1
- 0
AudioFirmware/makefile View File

@@ -38,6 +38,7 @@ TARGET = main
38 38
 SRC = $(TARGET).c
39 39
 SRC += adc.c # Additional Source-File
40 40
 SRC += eq.c
41
+SRC += twislave.c
41 42
 
42 43
 # List Assembler source files here.
43 44
 # Make them always end in a capital .S.  Files ending in a lowercase .s

+ 92
- 0
AudioFirmware/twislave.c View File

@@ -0,0 +1,92 @@
1
+/*
2
+ * twislave.h
3
+ *
4
+ * Copyright 2011 Thomas Buck <xythobuz@me.com>
5
+ * Copyright 2011 Max Nuding <max.nuding@gmail.com>
6
+ * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
7
+ *
8
+ * This file is part of LED-Cube.
9
+ *
10
+ * LED-Cube is free software: you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation, either version 3 of the License, or
13
+ * (at your option) any later version.
14
+ *
15
+ * LED-Cube is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+ * GNU General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License
21
+ * along with LED-Cube.  If not, see <http://www.gnu.org/licenses/>.
22
+ */
23
+
24
+#include <avr/io.h>
25
+#include <avr/interrupt.h>
26
+#include <stdint.h>
27
+
28
+uint8_t data[7] = { 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42 };
29
+uint8_t dataSent = 0;
30
+
31
+ISR(TWI_vect) {
32
+	static uint8_t dataPos = 0;
33
+	switch (TWSR & ~(7)) {
34
+		case 0xB0: case 0xA8: // We've been called
35
+			dataPos = 1; // Prepare next position
36
+			TWDR = data[0]; // Transmit first byte
37
+			TWCR |= (1 << TWEA); // More follows
38
+			dataSent = 0;
39
+			break;
40
+		case 0xB8: // We're doing fine
41
+			if (dataPos >= 6) {
42
+				// whoops, this shouldn't happen
43
+				// we transmit the last byte again and stop as if nothing happened
44
+				// we do this also if we reached the last byte
45
+				dataPos = 0;
46
+				TWDR = data[6];
47
+				TWCR &= ~(1 << TWEA); // Nothing follows
48
+				dataSent = 1;
49
+			} else {
50
+				TWDR = data[dataPos++]; // Transmit next byte
51
+				TWCR |= (1 << TWEA); // More follows
52
+				dataSent = 0;
53
+			}
54
+			break;
55
+		case 0xC0: case 0xC8: // We're done
56
+			TWCR |= (1 << TWEA);
57
+			dataSent = 1;
58
+			break;
59
+	}
60
+
61
+	TWCR |= (1 << TWINT); // Continue with bus operation
62
+}
63
+
64
+void twiInit(uint8_t address) {
65
+	if (address == 0) { // set slave address
66
+		TWAR = 1;
67
+	} else {
68
+		TWAR = address & ~(1);
69
+	}
70
+	TWCR = (1 << TWEN) | (1 << TWEA) | (1 << TWIE); // Enable twi, interrupt and acknowledge bit
71
+	
72
+	sei(); // Enable interrupts
73
+}
74
+
75
+void twiSetDataToSend(uint8_t *d) { // 7 bytes
76
+	// We don't care if it interrupts while we update the data.
77
+	// It will cause a single faulty frame. This will not be visible while showing many
78
+	// readouts in a short time.
79
+	uint8_t i;
80
+	for (i = 0; i < 7; i++) {
81
+		data[i] = d[i];
82
+	}
83
+}
84
+
85
+uint8_t twiDataWasSent() {
86
+	if (dataSent != 0) {
87
+		dataSent = 0;
88
+		return 1;
89
+	} else {
90
+		return 0;
91
+	}
92
+}

+ 3
- 2
AudioFirmware/twislave.h View File

@@ -21,5 +21,6 @@
21 21
  * along with LED-Cube.  If not, see <http://www.gnu.org/licenses/>.
22 22
  */
23 23
 
24
-void twiInit(uint8_t adress);
25
-void twiSetDataToSend(uint8_t *data);
24
+void twiInit(uint8_t address);
25
+void twiSetDataToSend(uint8_t *d); // 7 bytes
26
+uint8_t twiDataWasSent(void);

Loading…
Cancel
Save