forked from delvecja/audio_module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Audio_Chip_Init.v
184 lines (146 loc) · 3.43 KB
/
Audio_Chip_Init.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
module Audio_Chip_Init
(
input rst,
input CLOCK_50,
input audioInitPulse,
inout SDAT,
output SDCLK,
output reg audio_init_done,
input send,
input[6:0] register,
input[8:0] data,
output reg send_done,
output reg [3:0]audio_init_err
);
parameter ADDRESS = 7'b0011010;
reg [6:0]REGISTERS[0:5];
reg [8:0]DATA[0:5];
reg [3:0]currentInit;
reg [2:0]initState;
//i2c interface
reg i2cEnable;
initial i2cEnable = 1'b0;
reg [6:0]i2cRegister;
reg [8:0]i2cData;
wire [3:0]i2cError;
wire i2cDone;
i2c myI2c( i2cEnable,
CLOCK_50,
ADDRESS,//Same address every time: the sound chip.
i2cRegister,
i2cData,
1'b0,//We're always writing. Can't read from this device.
i2cError,//This will output an error if something goes wrong.
SDAT,//This is the i2c data line.
SDCLK,
i2cDone//Will go high when i2c is done.
);
always @ (posedge CLOCK_50 or negedge rst)
begin
if(rst == 1'b0)
begin
initState <= 1'b0;
end
else
begin
case(initState)
3'd0:
begin
DATA[0] <= 9'b0_000_1_0000;//Power *most* on.
DATA[1] <= 9'b0_0_1_0_1_00_11;//Audio format
DATA[2] <= 9'b0000_10_000;//DACSEL ON, BYPASS off.
DATA[3] <= 9'b0000_0_0_00_0;//DACMU[te] off
DATA[4] <= 9'b000000001;//Activate
DATA[5] <= 9'b0_000_0_0000;//Power all the way on.
REGISTERS[0] <= 7'b0000110;//Power Control
REGISTERS[1] <= 7'b0000111;//Digital Audio Interface (Audio format)
REGISTERS[2] <= 7'b0000100;//Analog Audio Path Control (BYPASS, DACSEL)
REGISTERS[3] <= 7'b0000101;//Digital Audio Path Control
REGISTERS[4] <= 7'b0001001;//Activate Control
REGISTERS[5] <= 7'b0000110;//Power Control
//Wait for the go signal.
if(audioInitPulse)
begin
initState <= initState + 1'b1;
end
end
3'd1:
begin
i2cEnable <= 1'b1;
i2cRegister <= REGISTERS[currentInit];
i2cData <= DATA[currentInit];
//Go to next state to wait
initState <= initState + 1'b1;
end
3'd2:
begin
//Wait for completion
if(i2cDone)
begin
initState <= initState + 1'b1;
end
else
begin
//Keep waiting
end
end
3'd3:
begin
//Disable i2c.
i2cEnable <= 1'b0;
//Do we have more to do?
if(currentInit < 4'd5)
begin
//Setup next register.
currentInit <= currentInit + 1'b1;
//Go back to beginning
initState <= 3'd1;
end
else
begin
//Stay here forever.
//"Remove" this module from the circuit too.
initState = 3'd4;
end
end
3'd4:
begin
//Stay here forever.
audio_init_done <= 1'b1;
send_done <= 1'b0;
i2cEnable <= 1'b0;
//Unless we want to send data manually...
if(send == 1'b1)
begin
//Send i2c data manually.
i2cEnable <= 1'b1;
i2cData <= data;
i2cRegister <= register;
//Go to a wait state.
initState <= 3'd5;
end
end
3'd5:
begin
if(i2cDone)
begin
//Go to a finished state
initState <= 3'd6;
end
end
3'd6:
begin
//Done
i2cEnable <= 1'b0;
send_done <= 1'b1;
initState <= 3'd4;
end
default:
begin
end
endcase
end
end
always @(*)
audio_init_err = i2cError;
endmodule