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

Subversion Repositories yifive

[/] [yifive/] [trunk/] [caravel_yifive/] [verilog/] [rtl/] [spi_master/] [src/] [spim_rx.sv] - Blame information for rev 21

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 18 dinesha
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  SPI RX  Module                                              ////
4
////                                                              ////
5
////  This file is part of the YIFive cores project               ////
6
////  http://www.opencores.org/cores/yifive/                      ////
7
////                                                              ////
8
////  Description                                                 ////
9
////     SPI RX module                                            ////
10
////                                                              ////
11
////  To Do:                                                      ////
12
////    nothing                                                   ////
13
////                                                              ////
14
////  Author(s):                                                  ////
15
////      - Dinesh Annayya, dinesha@opencores.org                 ////
16
////                                                              ////
17
////  Revision :                                                  ////
18
////     V.0  -  June 8, 2021                                     ////
19
////                                                              ////
20
//////////////////////////////////////////////////////////////////////
21
////                                                              ////
22
//// Copyright (C) 2000 Authors and OPENCORES.ORG                 ////
23
////                                                              ////
24
//// This source file may be used and distributed without         ////
25
//// restriction provided that this copyright statement is not    ////
26
//// removed from the file and that any derivative work contains  ////
27
//// the original copyright notice and the associated disclaimer. ////
28
////                                                              ////
29
//// This source file is free software; you can redistribute it   ////
30
//// and/or modify it under the terms of the GNU Lesser General   ////
31
//// Public License as published by the Free Software Foundation; ////
32
//// either version 2.1 of the License, or (at your option) any   ////
33
//// later version.                                               ////
34
////                                                              ////
35
//// This source is distributed in the hope that it will be       ////
36
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
37
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
38
//// PURPOSE.  See the GNU Lesser General Public License for more ////
39
//// details.                                                     ////
40
////                                                              ////
41
//// You should have received a copy of the GNU Lesser General    ////
42
//// Public License along with this source; if not, download it   ////
43
//// from http://www.opencores.org/lgpl.shtml                     ////
44
////                                                              ////
45
//////////////////////////////////////////////////////////////////////
46
 
47
 
48
module spim_rx #(
49
                parameter ENDIEAN = 0  // 0 - Little, 1 - Big endian, since RISV is Little indian default set 0
50
        )
51
(
52
    input  logic        clk,
53
    input  logic        rstn,
54
    input  logic        en,
55
    input  logic        rx_edge,
56
    output logic        rx_done,
57
    input  logic        sdi0,
58
    input  logic        sdi1,
59
    input  logic        sdi2,
60
    input  logic        sdi3,
61
    input  logic        en_quad_in,
62
    input  logic [15:0] counter_in,
63
    input  logic        counter_in_upd,
64
    output logic [31:0] data,
65
    input  logic        data_ready,
66
    output logic        data_valid,
67
    output logic        clk_en_o
68
);
69
 
70
  logic [31:0] data_int;
71
  logic [31:0] data_int_next;
72
  logic [15:0] counter;
73
  logic [15:0] counter_trgt;
74
  logic [15:0] counter_next;
75
  logic        reg_done;
76
  enum logic [1:0] { IDLE, RECEIVE, WAIT_FIFO, WAIT_FIFO_DONE } rx_CS, rx_NS;
77
 
78
 
79
  assign reg_done  = (!en_quad_in && (counter[4:0] == 5'b11111)) || (en_quad_in && (counter[2:0] == 3'b111));
80
 
81
  // RISV is little endian, so data is converted to little endian format
82
  assign data = (ENDIEAN) ? data_int_next : {data_int_next[7:0],data_int_next[15:8],data_int_next[23:16],data_int_next[31:24]};
83
 
84
 
85
  always_comb
86
  begin
87
    rx_NS         = rx_CS;
88
    data_int_next = data_int;
89
    data_valid    = 1'b0;
90
    counter_next  = counter;
91
 
92
    case (rx_CS)
93
      IDLE: begin
94
 
95
        // check first if there is available space instead of later
96
        if (en) begin
97
          rx_NS = RECEIVE;
98
        end
99
      end
100
 
101
      RECEIVE: begin
102
 
103
        if (rx_edge) begin
104
          counter_next = counter + 1;
105
          if (en_quad_in)
106
             data_int_next = {data_int[27:0],sdi3,sdi2,sdi1,sdi0};
107
          else
108
             data_int_next = {data_int[30:0],sdi1};
109
 
110
          if (rx_done) begin
111
            counter_next = 0;
112
            data_valid   = 1'b1;
113
 
114
            if (data_ready)
115
              rx_NS = IDLE;
116
            else
117
              rx_NS = WAIT_FIFO_DONE;
118
          end else if (reg_done) begin
119
            data_valid = 1'b1;
120
 
121
            if (~data_ready) begin
122
              // no space in the FIFO, wait for free space
123
              rx_NS    = WAIT_FIFO;
124
            end
125
          end
126
        end
127
      end
128
 
129
      WAIT_FIFO_DONE: begin
130
        data_valid = 1'b1;
131
        if (data_ready)
132
          rx_NS = IDLE;
133
      end
134
 
135
      WAIT_FIFO: begin
136
        data_valid = 1'b1;
137
        if (data_ready)
138
          rx_NS = RECEIVE;
139
      end
140
    endcase
141
  end
142
 
143
 
144
  always_ff @(posedge clk, negedge rstn)
145
  begin
146
    if (rstn == 0)
147
    begin
148
      counter      <= 0;
149
      counter_trgt <= 'h8;
150
      data_int     <= '0;
151 21 dinesha
      rx_done      <= '0;
152
      clk_en_o     <= '0;
153 18 dinesha
      rx_CS        <= IDLE;
154
    end
155
    else
156
    begin
157 21 dinesha
      if (rx_edge) begin
158
         counter      <= counter_next;
159
         data_int     <= data_int_next;
160
         rx_CS        <= rx_NS;
161
         rx_done      <= (counter_next == (counter_trgt-1)) && (rx_NS == RECEIVE);
162
         clk_en_o     <= (rx_NS == RECEIVE);
163
      end
164
       if (en && counter_in_upd) begin
165
          counter_trgt <= (en_quad_in) ? {2'b00,counter_in[15:2]} : counter_in;
166
        end
167 18 dinesha
    end
168
  end
169
 
170
endmodule

powered by: WebSVN 2.1.0

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