-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsimple_spi_slave.v
171 lines (156 loc) · 3.73 KB
/
simple_spi_slave.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer: Richard Aplin ( @drtune )
//
// Create Date: 20:11:02 10/27/2017
// Design Name:
// Module Name: simple_spi_slave
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module simple_spi_slave #( parameter WIDTH=8) (
input mosi,
output reg miso,
input sck,
input cs,
input CLK_40,
output reg [WIDTH-1:0] rx_data,
input [WIDTH-1:0] tx_data,
output reg tx_ready, //valid for one clk40
input tx_data_strobe,
output reg rx_data_strobe,
output reg rx_start, //during the same clk40 as rx_data_strobe, but only the first one of each 'transaction' (definition depends on wire protocol)
output reg rx_end_strobe,
input reset
);
parameter STATE_IDLE=0;
parameter STATE_RX=1;
integer i;
reg [WIDTH-1:0] tx_shift;
reg [WIDTH-1:0] tx_buf;
reg [1:0] state;
reg [2:0] bit_count;
initial
begin
state=STATE_IDLE;
rx_data=0;
tx_shift=0;
bit_count=0;
end
//assign miso=0;
// sync SCK to the FPGA clock using a 3-bits shift register
reg [2:0] SCKr; always @(posedge CLK_40) SCKr <= {SCKr[1:0], sck};
wire SCK_risingedge = (SCKr[2:1]==2'b01); // now we can detect SCK rising edges
wire SCK_fallingedge = (SCKr[2:1]==2'b10); // and falling edges
wire SCK_data= SCKr[1];
// and for MOSI
reg [2:0] MOSIr; always @(posedge CLK_40) MOSIr <= {MOSIr[1:0], mosi};
wire MOSI_data = MOSIr[1];
wire MOSI_fallingedge = (MOSIr[2:1]==2'b10);
//and cs
reg [2:0] CSr; always @(posedge CLK_40) CSr <= {CSr[1:0], cs};
wire CS_data = CSr[1];
wire CS_fallingedge = (CSr[2:1]==2'b10);
wire CS_risingedge = (CSr[2:1]==2'b01);
always @(posedge CLK_40 or posedge reset)
begin
if (reset)
begin
state<=STATE_IDLE;
rx_data_strobe<=0;
miso<=0;
tx_ready<=1;
bit_count<=0;
rx_start<=0;
rx_end_strobe<=0;
end
else
begin
if (rx_data_strobe)
begin
rx_data_strobe<=0;
rx_start<=0;
end
if (tx_data_strobe)
begin
tx_buf<=tx_data;
tx_ready<=0;
end
rx_end_strobe<=0;
if (CS_fallingedge)
begin
//start of message
bit_count<=0;
tx_shift<=tx_buf; //should set the first MISO (tx) byte before the master drops CS; usually this byte is a 'don't care' - we could force it to be zero here optionally
rx_start<=1;
end
else
if (CS_risingedge)
begin
rx_end_strobe<=1;
end
else
begin
//ok we basically do MSB first, CPHA=1 (change data on rising SCLK and sample on falling), CS=active low
if (SCK_fallingedge)
begin
//shift in msb first
rx_data<={ rx_data[WIDTH-2:0],MOSI_data } ;
bit_count<=bit_count+1;
if (bit_count==7) //bit count will wrap automatically
begin
rx_data_strobe<=1;
end
end
else
if (SCK_risingedge)
begin
miso<=tx_shift[ 7-bit_count];
if (bit_count==7)
begin
if (tx_ready)
begin
//underflow on tx data, flag it if you want
end
tx_shift<=tx_buf;
tx_ready<=1;
end
end
/*
case (state)
STATE_IDLE: begin
if (rx_shift == SYNC_WORD)
begin
state<=STATE_RX;
bit_count<=0;
end
end
STATE_RX: begin
if (bit_count==WIDTH-1)
begin
rx_data<=rx_shift;
rx_ready<=1;
state<=STATE_IDLE;
end
else
bit_count<=bit_count+1;
end
default: begin
end
endcase
end
*/
end
end //not reset
end
endmodule