OpenCores
URL https://opencores.org/ocsvn/i2c_to_wb/i2c_to_wb/trunk

Subversion Repositories i2c_to_wb

[/] [i2c_to_wb/] [trunk/] [src/] [i2c_to_wb_if.v] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 qaztronic
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
4
////                                                              ////
5
//// This source file may be used and distributed without         ////
6
//// restriction provided that this copyright statement is not    ////
7
//// removed from the file and that any derivative work contains  ////
8
//// the original copyright notice and the associated disclaimer. ////
9
////                                                              ////
10
//// This source file is free software; you can redistribute it   ////
11
//// and/or modify it under the terms of the GNU Lesser General   ////
12
//// Public License as published by the Free Software Foundation; ////
13
//// either version 2.1 of the License, or (at your option) any   ////
14
//// later version.                                               ////
15
////                                                              ////
16
//// This source is distributed in the hope that it will be       ////
17
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
18
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
19
//// PURPOSE.  See the GNU Lesser General Public License for more ////
20
//// details.                                                     ////
21
////                                                              ////
22
//// You should have received a copy of the GNU Lesser General    ////
23
//// Public License along with this source; if not, download it   ////
24
//// from http://www.opencores.org/lgpl.shtml                     ////
25
////                                                              ////
26
//////////////////////////////////////////////////////////////////////
27
 
28
 
29
`include "timescale.v"
30
 
31
 
32
module
33
  i2c_to_wb_if
34
  #(
35
    parameter DW = 32,
36
    parameter AW = 8
37
  )
38
  (
39
    input                   i2c_data,
40
    input                   i2c_ack_done,
41
    input       [7:0]       i2c_byte_in,
42
    output reg  [7:0]       i2c_byte_out,
43
    output                  i2c_parallel_load,
44
    input                   tip_wr_ack,
45
    input                   tip_rd_ack,
46
    input                   tip_addr_ack,
47
 
48
    input       [(DW-1):0]  wb_data_i,
49
    output      [(DW-1):0]  wb_data_o,
50
    output      [(AW-1):0]  wb_addr_o,
51
    output  reg [3:0]       wb_sel_o,
52
    output                  wb_we_o,
53
    output                  wb_cyc_o,
54
    output                  wb_stb_o,
55
    input                   wb_ack_i,
56
    input                   wb_err_i,
57
    input                   wb_rty_i,
58
 
59
    input                   wb_clk_i,
60
    input                   wb_rst_i
61
  );
62
 
63
 
64
  // --------------------------------------------------------------------
65
  //  wires
66
  wire i2c_r_w_bit = i2c_byte_in[0];
67
 
68
 
69
  // --------------------------------------------------------------------
70
  //  state machine
71
 
72
  localparam   STATE_IDLE       = 5'b00001;
73
  localparam   STATE_WRITE      = 5'b00010;
74
  localparam   STATE_WRITE_WAIT = 5'b00100;
75
  localparam   STATE_READ       = 5'b01000;
76
  localparam   STATE_READ_WAIT  = 5'b10000;
77
 
78
  reg [4:0] state;
79
  reg [4:0] next_state;
80
 
81
  always @(posedge wb_clk_i or posedge wb_rst_i)
82
    if(wb_rst_i)
83
      state <= STATE_IDLE;
84
    else
85
      state <= next_state;
86
 
87
  always @(*)
88
    case( state )
89
      STATE_IDLE:       if( tip_addr_ack & i2c_ack_done )
90
                          if(i2c_r_w_bit)
91
                            next_state = STATE_READ;
92
                          else
93
                            next_state = STATE_WRITE_WAIT;
94
                        else
95
                          next_state = STATE_IDLE;
96
 
97
      STATE_WRITE:      if( wb_ack_i )
98
                          next_state = STATE_WRITE_WAIT;
99
                        else
100
                          next_state = STATE_WRITE;
101
 
102
      STATE_WRITE_WAIT: if( tip_addr_ack )
103
                          next_state = STATE_IDLE;
104
                        else
105
                          if( tip_wr_ack & i2c_ack_done )
106
                            next_state = STATE_WRITE;
107
                          else
108
                            next_state = STATE_WRITE_WAIT;
109
 
110
      STATE_READ:       if( wb_ack_i )
111
                          next_state = STATE_READ_WAIT;
112
                        else
113
                          next_state = STATE_READ;
114
 
115
      STATE_READ_WAIT:  if( tip_addr_ack )
116
                          next_state = STATE_IDLE;
117
                        else
118
                          if( tip_rd_ack & i2c_ack_done )
119
                            if(i2c_data)
120
                              next_state = STATE_IDLE;
121
                            else
122
                              next_state = STATE_READ;
123
                          else
124
                            next_state = STATE_READ_WAIT;
125
 
126
      default:          next_state = STATE_IDLE;
127
    endcase
128
 
129
 
130
  // --------------------------------------------------------------------
131
  //  wishbone offset address
132
 
133
  reg [7:0] i2c_offset_r;
134
  always @(posedge wb_clk_i)
135
    if( tip_addr_ack )
136
      if(i2c_r_w_bit)
137
        i2c_offset_r <= 8'h00;
138
      else
139
        i2c_offset_r <= 8'hff;
140
    else if( i2c_ack_done )
141
      i2c_offset_r <= i2c_offset_r + 1;
142
 
143
 
144
  // --------------------------------------------------------------------
145
  //  byte lane select
146
 
147
  always @(*)
148
    case( i2c_offset_r[1:0] )
149
      2'b00:  wb_sel_o = 4'b0001;
150
      2'b01:  wb_sel_o = 4'b0010;
151
      2'b10:  wb_sel_o = 4'b0100;
152
      2'b11:  wb_sel_o = 4'b1000;
153
    endcase
154
 
155
  always @(*)
156
    case( wb_sel_o )
157
      4'b0001:  i2c_byte_out = wb_data_i[7:0];
158
      4'b0010:  i2c_byte_out = wb_data_i[15:8];
159
      4'b0100:  i2c_byte_out = wb_data_i[23:16];
160
      4'b1000:  i2c_byte_out = wb_data_i[31:24];
161
      default:  i2c_byte_out = wb_data_i[7:0];
162
    endcase
163
 
164
 
165
  // --------------------------------------------------------------------
166
  //  outputs
167
 
168
  assign i2c_parallel_load = (state == STATE_READ);
169
 
170
  assign wb_addr_o[7:0] = i2c_offset_r;
171
  assign wb_data_o      = {i2c_byte_in, i2c_byte_in, i2c_byte_in, i2c_byte_in};
172
  assign wb_cyc_o       = (state == STATE_WRITE) | (state == STATE_READ);
173
  assign wb_stb_o       = (state == STATE_WRITE) | (state == STATE_READ);
174
  assign wb_we_o        = (state == STATE_WRITE);
175
 
176
 
177
endmodule
178
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.