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_fsm.v] - Blame information for rev 4

Go to most recent revision | 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 3 qaztronic
 
28
 
29
`include "timescale.v"
30
 
31
 
32
module
33
  i2c_to_wb_fsm
34
  (
35
    input         i2c_data,
36
    input         i2c_data_rise,
37
    input         i2c_data_fall,
38
 
39
    input         i2c_clk,
40
    input         i2c_clk_rise,
41
    input         i2c_clk_fall,
42
 
43 4 qaztronic
    input         i2c_r_w_bit,
44
    input         i2c_ack_out,
45 3 qaztronic
    output        i2c_ack_done,
46
 
47
    output        tip_addr_byte,
48
    output        tip_read_byte,
49
    output        tip_write_byte,
50
    output        tip_wr_ack,
51
    output        tip_rd_ack,
52
    output        tip_addr_ack,
53
 
54
    output  [7:0] state_out,
55 4 qaztronic
    output        i2c_error,
56 3 qaztronic
 
57
    input         wb_clk_i,
58
    input         wb_rst_i
59
  );
60
 
61
  // --------------------------------------------------------------------
62
  //  wires
63
  wire xmt_byte_done;
64
 
65
  wire tip_ack;
66
 
67
 
68
  // --------------------------------------------------------------------
69 4 qaztronic
  //  start & stop & ack
70 3 qaztronic
 
71
  wire start_detected = i2c_data_fall & i2c_clk;
72
  wire stop_detected  = i2c_data_rise & i2c_clk;
73
 
74
 
75
  // --------------------------------------------------------------------
76
  //  state machine
77
 
78
  localparam   STATE_IDLE       = 8'b00000001;
79
  localparam   STATE_ADDR_BYTE  = 8'b00000010;
80
  localparam   STATE_ADDR_ACK   = 8'b00000100;
81
  localparam   STATE_WRITE      = 8'b00001000;
82
  localparam   STATE_WR_ACK     = 8'b00010000;
83
  localparam   STATE_READ       = 8'b00100000;
84
  localparam   STATE_RD_ACK     = 8'b01000000;
85
  localparam   STATE_ERROR      = 8'b10000000;
86
 
87
  reg [7:0] state;
88
  reg [7:0] next_state;
89
 
90
  always @(posedge wb_clk_i or posedge wb_rst_i)
91
    if(wb_rst_i)
92
      state <= STATE_IDLE;
93
    else
94
      state <= next_state;
95
 
96
  always @(*)
97
    case( state )
98
      STATE_IDLE:       if( start_detected )
99
                          next_state = STATE_ADDR_BYTE;
100
                        else
101
                          next_state = STATE_IDLE;
102
 
103
      STATE_ADDR_BYTE:  if( xmt_byte_done )
104 4 qaztronic
                          next_state = STATE_ADDR_ACK;
105 3 qaztronic
                        else if( start_detected | stop_detected )
106
                          next_state = STATE_ERROR;
107
                        else
108
                          next_state = STATE_ADDR_BYTE;
109
 
110 4 qaztronic
      STATE_ADDR_ACK:   if(i2c_ack_out)
111
                          next_state = STATE_IDLE;
112
                        else
113
                          if( i2c_ack_done )
114
                            if( i2c_r_w_bit )
115
                              next_state = STATE_READ;
116
                            else
117
                              next_state = STATE_WRITE;
118
                          else if( start_detected | stop_detected )
119
                            next_state = STATE_ERROR;
120 3 qaztronic
                          else
121 4 qaztronic
                            next_state = STATE_ADDR_ACK;
122 3 qaztronic
 
123
      STATE_WRITE:      if( xmt_byte_done )
124
                          next_state = STATE_WR_ACK;
125
                        else if( start_detected )
126
                          next_state = STATE_ADDR_BYTE;
127
                        else if( stop_detected )
128
                          next_state = STATE_IDLE;
129
                        else
130
                          next_state = STATE_WRITE;
131
 
132
      STATE_WR_ACK:     if( i2c_ack_done )
133
                          next_state = STATE_WRITE;
134
                        else if( start_detected | stop_detected )
135
                          next_state = STATE_ERROR;
136
                        else
137
                          next_state = STATE_WR_ACK;
138
 
139
      STATE_READ:       if( xmt_byte_done )
140
                          next_state = STATE_RD_ACK;
141
                        else if( start_detected )
142
                          next_state = STATE_ADDR_BYTE;
143
                        else if( stop_detected )
144
                          next_state = STATE_IDLE;
145
                        else
146
                          next_state = STATE_READ;
147
 
148
      STATE_RD_ACK:     if( i2c_ack_done )
149 4 qaztronic
                          if(i2c_data)
150
                            next_state = STATE_IDLE;
151
                          else
152
                            next_state = STATE_READ;
153 3 qaztronic
                        else if( start_detected | stop_detected )
154
                          next_state = STATE_ERROR;
155
                        else
156
                          next_state = STATE_RD_ACK;
157
 
158
      STATE_ERROR:      next_state = STATE_IDLE;
159
 
160
      default:          next_state = STATE_ERROR;
161
    endcase
162
 
163
 
164
  // --------------------------------------------------------------------
165
  //  bit counter 
166
  reg [3:0] bit_count;
167
 
168
  assign  xmt_byte_done = (bit_count == 4'h7) & i2c_clk_rise;
169
  assign  tip_ack       = (bit_count == 4'h8);
170
  assign  i2c_ack_done  = tip_ack & i2c_clk_rise;
171
 
172
  always @(posedge wb_clk_i)
173
    if( wb_rst_i | i2c_ack_done | start_detected )
174
      bit_count <= 4'hf;
175
    else if( i2c_clk_fall )
176
      bit_count <= bit_count + 1;
177
 
178
 
179
// --------------------------------------------------------------------
180
//  outputs
181
 
182
  assign state_out = state;
183
 
184
  assign  tip_addr_byte   = (state == STATE_ADDR_BYTE);
185
  assign  tip_addr_ack    = (state == STATE_ADDR_ACK);
186
  assign  tip_read_byte   = (state == STATE_READ);
187
  assign  tip_write_byte  = tip_addr_byte               | (state == STATE_WRITE);
188
  assign  tip_wr_ack      = tip_addr_ack                | (state == STATE_WR_ACK);
189
  assign  tip_rd_ack      = (state == STATE_RD_ACK);
190 4 qaztronic
 
191
  assign i2c_error = (state == STATE_ERROR);
192 3 qaztronic
 
193
endmodule
194
 
195
 
196
 

powered by: WebSVN 2.1.0

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