-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio-codec.v
306 lines (236 loc) · 4.68 KB
/
audio-codec.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
* Cast ovladace k tomu audio chipu
*/
module audio_codec (
/* Audio chip connections */
daclrc,
bclk,
dacdat,
xck,
/* sound source connection */
data_left,
data_right,
/* system clock */
sys_clk,
sys_clk_freq,
/* I2C pins */
i2c_sda,
i2c_scl,
/* reset */
reset
);
output xck; reg xck;
input bclk;
input daclrc;
output dacdat; reg dacdat;
input [23:0] data_left; // signed
input [23:0] data_right; // signed
input [31:0] sys_clk_freq;
input sys_clk;
input reset;
output i2c_sda;
inout i2c_scl;
reg [31:0]bit_counter;
/* 'I2C Mode' block */
/* generate BCLK and DACLRC signal */
reg sc; // synchronization
reg [7:0] counter;
gen_clock sync
(
.clock_in(sys_clk),
.in_freq(sys_clk_freq),
.clock_out(sc),
.out_freq(48_000 * 256 * 2)
);
/* generate crystal signal */
always@(negedge sc)
begin
xck <= ~xck;
end
reg old_daclrc;
always@(negedge bclk)
begin
if (old_daclrc != daclrc)
counter <= 0;
else if (counter < 24)
begin
if (daclrc)
dacdat <= data_right[24-counter];
else
dacdat <= data_left[24-counter];
counter <= counter + 1'b1;
end
else
dacdat <= 0;
old_daclrc <= daclrc;
end
/////////////////////////////////////////////
///////////////////// Memory ////////////////
/////////////////////////////////////////////
/*
reg [7:0] addr;
reg [7:0] rom_out;
reg rom_clk;
config_rom rom
(
.address(addr),
.clock(rom_clk),
.q(rom_out)
);
rom2i2c r2i
(
.rom_addr(addr),
.rom_clk(rom_clk),
.rom_out(rom_out),
.i2c_sda(i2c_sda),
.i2c_scl(i2c_scl)
);
*/
/////////////////////
// Setup the Codec //
/////////////////////
reg write_codec;
reg done_codec;
reg [7:0] config_data;
reg [7:0] config_reg;
i2c_write i2c_codec
(
.sda(i2c_sda),
.scl(i2c_scl),
.addr(8'h34), // Codec's address
.register(config_reg),
.data(config_data),
.sys_clk(sys_clk),
.sys_freq(sys_clk_freq),
.i2c_freq(4*10_000),
.write(write_codec),
.done(done_codec)
);
/***********************************/
/* Power on && Reset configuration */
/***********************************/
reg [7:0] config_phase = 0;
reg config_working;
always@(posedge sys_clk)
begin
// device reset
// (wait for an event)
if (config_phase == 0)
begin
config_phase <= 10;
end
// Reset
if (config_phase == 10)
begin
if (config_working == 0 && done_codec == 0)
begin
config_data <= 8'b00000000; // Reset
config_reg <= 8'b00011110; // Reset register
config_working <= 1;
write_codec <= 1;
end
if (config_working == 1 && done_codec == 1)
begin
write_codec <= 0;
config_working <= 0;
config_phase <= 20;
end
end
// Power ON
if(config_phase == 20)
begin
if (config_working == 0 && done_codec == 0)
begin
config_data <=8'b00000000;
config_reg <= 8'b00001100;
config_working <= 1;
write_codec <= 1;
end
if (config_working == 1 && done_codec == 1)
begin
write_codec <= 0;
config_working <= 0;
config_phase <= 30;
end
end
// Turn soft mute off
if (config_phase == 30)
begin
if (config_working == 0 && done_codec == 0)
begin
config_data <= 8'b00000110; // /DACMU
config_reg <= 8'b00001010; //
config_working <= 1;
write_codec <= 1;
end
if (config_working == 1 && done_codec == 1)
begin
write_codec <= 0;
config_working <= 0;
config_phase <= 40;
end
end
// Enable DAC output
if(config_phase == 40)
begin
if (config_working == 0 && done_codec == 0)
begin
config_data <=8'b00111000; // DACSEL + MUTEMIC
config_reg <= 8'b00001000; // analog audio path control
config_working <= 1;
write_codec <= 1;
end
if (config_working == 1 && done_codec == 1)
begin
write_codec <= 0;
config_working <= 0;
config_phase <= 50;
end
end
// Master mode
if (config_phase == 50)
begin
if (config_working == 0 && done_codec == 0)
begin
config_data <= 8'b01001010; // default | master
config_reg <= 8'b00001110; // R7 - Digital audio interface format
config_working <= 1;
write_codec <= 1;
end
if (config_working == 1 && done_codec == 1)
begin
write_codec <= 0;
config_working <= 0;
config_phase <= 60;
end
end
// Set it active
if(config_phase == 60)
begin
if (config_working == 0 && done_codec == 0)
begin
config_data <=8'b00000001;
config_reg <= 8'b00010010;
config_working <= 1;
write_codec <= 1;
end
if (config_working == 1 && done_codec == 1)
begin
write_codec <= 0;
config_working <= 0;
config_phase <= 100;
end
end
/***
RESET feature is OFF
***/
/* reset on 'reset' posedge signal */
if (config_phase == 100)
begin
/*
if (reset == 1)
config_phase <= 0;
*/
end
end
endmodule