|
| 1 | +//Legal Notice: (C)2006 Altera Corporation. All rights reserved. Your |
| 2 | +//use of Altera Corporation's design tools, logic functions and other |
| 3 | +//software and tools, and its AMPP partner logic functions, and any |
| 4 | +//output files any of the foregoing (including device programming or |
| 5 | +//simulation files), and any associated documentation or information are |
| 6 | +//expressly subject to the terms and conditions of the Altera Program |
| 7 | +//License Subscription Agreement or other applicable license agreement, |
| 8 | +//including, without limitation, that your use is for the sole purpose |
| 9 | +//of programming logic devices manufactured by Altera and sold by Altera |
| 10 | +//or its authorized distributors. Please refer to the applicable |
| 11 | +//agreement for further details. |
| 12 | + |
| 13 | +module I2C_AV_Config ( //Host Side |
| 14 | + iCLK, |
| 15 | + iRST_N, |
| 16 | + // I2C Side |
| 17 | + oI2C_SCLK, |
| 18 | + oI2C_SDAT |
| 19 | + ); |
| 20 | + |
| 21 | +// Host Side |
| 22 | +input iCLK; |
| 23 | +input iRST_N; |
| 24 | +// I2C Side |
| 25 | +output oI2C_SCLK; |
| 26 | +inout oI2C_SDAT; |
| 27 | +// Internal Registers/Wires |
| 28 | +reg [15:0] mI2C_CLK_DIV; |
| 29 | +reg [23:0] mI2C_DATA; |
| 30 | +reg mI2C_CTRL_CLK; |
| 31 | +reg mI2C_GO; |
| 32 | +wire mI2C_END; |
| 33 | +wire mI2C_ACK; |
| 34 | +reg [15:0] LUT_DATA; |
| 35 | +reg [3:0] LUT_INDEX; |
| 36 | +reg [1:0] mSetup_ST; |
| 37 | + |
| 38 | +// Clock Setting |
| 39 | +parameter CLK_Freq = 24000000; // 24 MHz |
| 40 | +parameter I2C_Freq = 20000; // 20 KHz |
| 41 | +// LUT Data Number |
| 42 | +parameter LUT_SIZE = 11; |
| 43 | +// Audio Data Index |
| 44 | +parameter Dummy_DATA = 0; |
| 45 | +parameter SET_LIN_L = 1; |
| 46 | +parameter SET_LIN_R = 2; |
| 47 | +parameter SET_HEAD_L = 3; |
| 48 | +parameter SET_HEAD_R = 4; |
| 49 | +parameter A_PATH_CTRL = 5; |
| 50 | +parameter D_PATH_CTRL = 6; |
| 51 | +parameter POWER_ON = 7; |
| 52 | +parameter SET_FORMAT = 8; |
| 53 | +parameter SAMPLE_CTRL = 9; |
| 54 | +parameter SET_ACTIVE = 10; |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +///////////////////// I2C Control Clock //////////////////////// |
| 59 | +always@(posedge iCLK or negedge iRST_N) begin |
| 60 | + if(!iRST_N) begin |
| 61 | + mI2C_CTRL_CLK <= 1'd0; |
| 62 | + mI2C_CLK_DIV <= 16'd0; |
| 63 | + end else begin |
| 64 | + if (mI2C_CLK_DIV < (CLK_Freq/I2C_Freq)) |
| 65 | + mI2C_CLK_DIV <= mI2C_CLK_DIV + 16'd1; |
| 66 | + else begin |
| 67 | + mI2C_CLK_DIV <= 16'd0; |
| 68 | + mI2C_CTRL_CLK <= ~mI2C_CTRL_CLK; |
| 69 | + end |
| 70 | + end |
| 71 | +end |
| 72 | + |
| 73 | +//////////////////////////////////////////////////////////////////// |
| 74 | +I2C_Controller u0 ( |
| 75 | + .CLOCK(mI2C_CTRL_CLK), // Controller Work Clock |
| 76 | + .I2C_SCLK(oI2C_SCLK), // I2C CLOCK |
| 77 | + .I2C_SDAT(oI2C_SDAT), // I2C DATA |
| 78 | + .I2C_DATA(mI2C_DATA), // DATA:[SLAVE_ADDR,SUB_ADDR,DATA] |
| 79 | + .GO(mI2C_GO), // GO transfor |
| 80 | + .END(mI2C_END), // END transfor |
| 81 | + .ACK(mI2C_ACK), // ACK |
| 82 | + .RESET(iRST_N) |
| 83 | +); |
| 84 | +//////////////////////////////////////////////////////////////////// |
| 85 | + |
| 86 | + |
| 87 | +////////////////////// Config Control //////////////////////////// |
| 88 | +always@(posedge mI2C_CTRL_CLK or negedge iRST_N) begin |
| 89 | + if(!iRST_N) begin |
| 90 | + LUT_INDEX <= 4'd0; |
| 91 | + mSetup_ST <= 2'd0; |
| 92 | + mI2C_GO <= 1'd0; |
| 93 | + end else begin |
| 94 | + if(LUT_INDEX < LUT_SIZE) begin |
| 95 | + case(mSetup_ST) |
| 96 | + 0: begin |
| 97 | + mI2C_DATA <= {8'h34,LUT_DATA}; |
| 98 | + mI2C_GO <= 1'd1; |
| 99 | + mSetup_ST <= 2'd1; |
| 100 | + end |
| 101 | + 1: begin |
| 102 | + if(mI2C_END) begin |
| 103 | + if(!mI2C_ACK) |
| 104 | + mSetup_ST <= 2'd2; |
| 105 | + else |
| 106 | + mSetup_ST <= 2'd0; |
| 107 | + mI2C_GO <= 1'd0; |
| 108 | + end |
| 109 | + end |
| 110 | + 2: begin |
| 111 | + LUT_INDEX <= LUT_INDEX + 4'd1; |
| 112 | + mSetup_ST <= 2'd0; |
| 113 | + end |
| 114 | + endcase |
| 115 | + end |
| 116 | + end |
| 117 | +end |
| 118 | +//////////////////////////////////////////////////////////////////// |
| 119 | + |
| 120 | + |
| 121 | +///////////////////// Config Data LUT ////////////////////////// |
| 122 | +always @ (*) |
| 123 | +begin |
| 124 | + case(LUT_INDEX) |
| 125 | + // Audio Config Data |
| 126 | + Dummy_DATA : LUT_DATA <= 16'h0000; |
| 127 | + SET_LIN_L : LUT_DATA <= 16'h009A;//16'h001A; //R0 LINVOL = 1Ah (+4.5bB) |
| 128 | + SET_LIN_R : LUT_DATA <= 16'h029A;//16'h021A; //R1 RINVOL = 1Ah (+4.5bB) |
| 129 | + SET_HEAD_L : LUT_DATA <= 16'h0479; //R2 LHPVOL = 7Bh (+2dB) |
| 130 | + SET_HEAD_R : LUT_DATA <= 16'h0679; //R3 RHPVOL = 7Bh (+2dB) |
| 131 | + A_PATH_CTRL : LUT_DATA <= 16'h08D2;//16'h08F8; //R4 DACSEL = 1 |
| 132 | + D_PATH_CTRL : LUT_DATA <= 16'h0A06; //R5 DEEMP = 11 (48 KHz) |
| 133 | + //POWER_ON : LUT_DATA <= 16'h0C00; //R6 all powered ON |
| 134 | + POWER_ON : LUT_DATA <= 16'h0C20; //R6 internal oscilator MCLK powered down |
| 135 | + //SET_FORMAT : LUT_DATA <= 16'h0E01; //R7 FORMAT=01,16 bit format left justified |
| 136 | + SET_FORMAT : LUT_DATA <= 16'h0E02; //R7 FORMAT=10,16 bit format I2S |
| 137 | + //SAMPLE_CTRL : LUT_DATA <= 16'h1009; //R8 48KHz,USB-mode |
| 138 | + SAMPLE_CTRL : LUT_DATA <= 16'h1008; //R8 48KHz,Normal mode, clkdiv2=0 |
| 139 | + SET_ACTIVE : LUT_DATA <= 16'h1201; //R9 ACTIVE |
| 140 | + default : LUT_DATA <= 16'h0000; |
| 141 | + endcase |
| 142 | +end |
| 143 | +//////////////////////////////////////////////////////////////////// |
| 144 | + |
| 145 | + |
| 146 | +endmodule |
| 147 | + |
0 commit comments