Ignore:
Timestamp:
Nov 22, 2009, 11:16:41 PM (15 years ago)
Author:
demin
Message:

add I2C master

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/MultiChannelUSB/control.v

    r65 r69  
    44                input   wire                    rx_empty, tx_full,
    55                input   wire    [7:0]   rx_data,
     6
    67                input   wire    [1:0]   mux_max_byte,
    78                input   wire    [15:0]  mux_min_addr, mux_max_addr,
     
    1314                output  wire    [1:0]   mux_byte,
    1415                output  wire    [15:0]  mux_addr,
     16
    1517                output  wire                    rx_rdreq,
    1618                output  wire                    tx_wrreq,
    1719                output  wire    [7:0]   tx_data,
     20
    1821                output  wire                    ram_we,
    1922                output  wire    [19:0]  ram_addr,
    2023                inout   wire    [17:0]  ram_data,
     24
     25                output  wire                    i2c_wrreq,
     26                output  wire    [15:0]  i2c_data,
     27                input   wire                    i2c_full,
     28
    2129                output  wire                    led
    2230        );
     
    3341        reg                             int_led;
    3442
     43        reg     [15:0]  int_i2c_data;
     44        reg                     int_i2c_wrreq;
     45
    3546
    3647        wire                    crc_error = 1'b0;
     
    3950        reg             [4:0]   idle_counter;
    4051
    41         reg             [3:0]   state;
     52        reg             [4:0]   state;
    4253       
    4354        wire    [15:0]  src, dst;
     
    5667        assign  int_ram_q = ram_data;
    5768//      assign  ram_data = int_ram_we ? int_ram_data : 18'bz;
    58         assign  ram_addr = {ram_counter[18:5],1'd0,ram_counter[4:0]};
     69//      assign  ram_addr = {ram_counter[18:5],1'd0,ram_counter[4:0]};
     70        assign  ram_addr = {1'd0, ram_counter[18:0]};
    5971
    6072        genvar j;
     
    101113                                idle_counter <= 5'd0;
    102114                                byte_counter <= 3'd0;
    103                                 state <= 4'd1;
     115                                state <= 5'd1;
    104116                        end
    105117
     
    115127                                        begin
    116128                                                int_rdreq <= 1'b0;
    117                                                 state <= 4'd2;
     129                                                state <= 5'd2;
    118130                                        end
    119131                                end
     
    125137                                                int_rdreq <= 1'b0;
    126138                                                crc_reset <= 1'b1;
    127                                                 state <= 4'd0;
     139                                                state <= 5'd0;
    128140                                        end
    129141                                end
     
    140152                                                16'h0000:
    141153                                                begin
    142                                                         state <= 4'd0;
     154                                                        state <= 5'd0;
    143155                                                end
    144156
     
    148160                                                        int_chan <= src[1:0];
    149161                                                        int_reset <= 1'b1;
    150                                                         state <= 4'd0;
     162                                                        state <= 5'd0;
    151163                                                end
    152164
     
    155167                                                        int_type <= src[4];
    156168                                                        int_chan <= src[1:0];
    157                                                         state <= 4'd3;
     169                                                        state <= 5'd3;
    158170                                                end
    159171
     
    161173                                                begin
    162174                                                        tst_counter <= 11'd0;   
    163                                                         state <= 4'd6;
     175                                                        state <= 5'd6;
    164176                                                end
    165177                                                16'h0004:
     
    168180                                                        int_ram_data <= 18'd0;
    169181                                                        ram_counter <= 19'd0;   
    170                                                         state <= 4'd9;
     182                                                        state <= 5'd9;
     183                                                end
     184                                                16'h0005:
     185                                                begin
     186                                                        int_i2c_data <= src;
     187                                                        int_i2c_wrreq <= 1'b1;
     188                                                        state <= 5'd15;
    171189                                                end
    172190                                        endcase
     
    182200                                int_max_byte <= mux_max_byte;
    183201                                int_byte <= 2'd0;       
    184                                 state <= 4'd4;
     202                                state <= 5'd4;
    185203                        end
    186204       
     
    188206                        begin
    189207                                int_wrreq <= 1'b0;
    190                                 state <= 4'd5;
     208                                state <= 5'd5;
    191209                        end
    192210
     
    199217                                        if ((int_byte == int_max_byte) && (int_addr == int_max_addr))
    200218                                        begin
    201                                                 state <= 4'd0;
     219                                                state <= 5'd0;
    202220                                        end
    203221                                        else
    204222                                        begin
    205                                                 state <= 4'd4;
     223                                                state <= 5'd4;
    206224                                                if (int_byte == int_max_byte)
    207225                                                begin
     
    224242                                int_wrreq <= 1'b1;
    225243                                tst_counter <= tst_counter + 11'd1;
    226                                 state <= 4'd7;
     244                                state <= 5'd7;
    227245                        end
    228246                        7:
     
    233251                                        if (&tst_counter)
    234252                                        begin
    235                                                 state <= 4'd8;
     253                                                state <= 5'd8;
    236254                                        end
    237255                                        else
     
    246264                                begin
    247265                                        int_wrreq <= 1'b0;
    248                                         state <= 4'd0;
     266                                        state <= 5'd0;
    249267                                end
    250268                        end
     
    253271                        begin
    254272                                crc_reset <= 1'b0;
    255                                 state <= 4'd10;
     273                                state <= 5'd10;
    256274                        end
    257275                        10:
     
    261279                                if (&ram_counter)
    262280                                begin
    263                                         state <= 4'd11;
     281                                        state <= 5'd11;
    264282                                end
    265283                                else
    266284                                begin
    267                                         state <= 4'd9;
     285                                        state <= 5'd9;
    268286                                        ram_counter <= ram_counter + 19'd1;
    269287                                end
     
    274292                                int_ram_data <= 18'd0;
    275293                                ram_counter <= 19'd0;
    276                                 state <= 4'd12;
     294                                state <= 5'd12;
    277295                        end
    278296                        12:
    279297                        begin
    280298                                int_wrreq <= 1'b0;
    281                                 state <= 4'd13;
     299                                state <= 5'd13;
    282300                        end
    283301                        13:
    284302                        begin
    285                                 state <= 4'd14;
     303                                state <= 5'd14;
    286304                        end
    287305                        14:
     
    293311                                        if (&ram_counter)
    294312                                        begin
    295                                                 state <= 4'd0;
     313                                                state <= 5'd0;
    296314                                        end
    297315                                        else
    298316                                        begin
    299                                                 state <= 4'd12;
     317                                                state <= 5'd12;
    300318                                                ram_counter <= ram_counter + 19'd1;
    301319                                        end
     
    303321                        end
    304322                                               
     323                        // i2c write
     324                        15:
     325                        begin
     326                                crc_reset <= 1'b0;
     327                                if (~i2c_full)
     328                                begin
     329                                        int_i2c_wrreq <= 1'b0;
     330                                        state <= 5'd0;
     331                                end
     332                        end
     333
    305334                        default:
    306335                        begin
    307                                 state <= 4'd0;
     336                                state <= 5'd0;
    308337                        end
    309338                endcase
     
    318347        assign  tx_wrreq = int_wrreq & (~tx_full);
    319348        assign  tx_data = int_data;
     349        assign  i2c_wrreq = int_i2c_wrreq;
     350        assign  i2c_data = int_i2c_data;
    320351        assign  led = int_led;
    321352
Note: See TracChangeset for help on using the changeset viewer.