-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspi_master.vhd
523 lines (398 loc) · 11.1 KB
/
spi_master.vhd
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
--spi_master
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity spi_master is
port (
clk : in std_logic;
rst : in std_logic;
mosi : out std_logic;
miso : in std_logic;
sclk_out : out std_logic;
cs_out : out std_logic;
int1 : in std_logic;
int2 : in std_logic;
go : in std_logic;
pol : in std_logic;
pha : in std_logic;
bytes : in std_logic_vector (3 downto 0);
rxData: out std_logic_vector(7 downto 0);
txData: in std_logic_vector(7 downto 0);
rxDataReady: out std_logic
);
end spi_master;
architecture FSM_1P of spi_master is
------------------------------------
-- State signals
------------------------------------
type STATE_TYPE is (S_IDLE, S_TXRX);
signal state, next_state : STATE_TYPE;
------------------------------------
-- Clock Div (spi clock) Signals
------------------------------------
signal sclk : std_logic;
signal enable : std_logic := '0';
--signal bytes : std_logic_vector (3 downto 0);
signal polarity : std_logic;
signal last_clk_active : std_logic;
signal clk_active : std_logic;
signal byte_flag : std_logic;
------------------------------------
-- SPI Data Signals
------------------------------------
signal rx_buffer : std_logic_vector (7 downto 0) := (others => '1');
signal tx_buffer : std_logic_vector (7 downto 0);
signal cs : std_logic := '1';
constant byte_w : natural := 8;
signal bit : integer range -1 to 8 := 7; --init to msb+1 idx
signal w_r : std_logic := '0'; --write = 0 read = 1
signal phase_Delay : std_logic := '0';
signal last_cs : std_logic := '1';
signal last_sclk : std_logic := '0';
begin
U_CLOCK_DIV : entity work.clock_div
generic map(
clk_in_freq => 50000000,
clk_out_freq => 5000000)
port map(
clk_in => clk,
clk_out => sclk,
rst => rst,
enable => enable,
bytes => bytes,
polarity => polarity,
clk_active => clk_active,
byte_flag => byte_flag);
--enable <= int1 or int2 or go;
process (clk, rst)
begin
if(rst = '1') then
state <= S_IDLE;
last_clk_active <= '0';
enable <= '0';
cs <= '1';
elsif (clk'event and clk = '1') then
--process(state, int1, int2, go, clk_active, byte_flag)--, txData)
--begin
rxDataReady <= '0'; --only high when data is ready
polarity <= pol;
--w_r <= not pha; --first write for phase 1 is pre toggled. 0 is default
-- next_state <= state;
enable <= '0';
last_clk_active <= clk_active;
case state is
when S_IDLE =>
--enable <= '0'; --default
cs <= '1';
if((int1 = '1' or int2 = '1' or go = '1') and rst = '0') then
state <= S_TXRX;
-- polarity <= pol;
-- w_r <= not pha; --first write for phase 1 is pre toggled. 0 is default
enable <= '1';
cs <= '0';
tx_buffer <= txData; --TEST
end if;
when S_TXRX =>
enable <= '1';
if(last_clk_active = '1' and clk_active = '0') then --falling edge of clk_active
enable <= '0';
cs <= '1';
rxDataReady <= '1';
state <= S_IDLE;
else
enable <= '1';
cs <= '0';
if(byte_flag = '1') then
rxDataReady <= '1';
tx_buffer <= txData;
end if;
end if;
when others => null;
end case;
end if;
end process;
cs_out <= cs;
sclk_out <= sclk;
process (clk, sclk, cs, rst)--, state)
begin
if( rst = '1') then
bit <= 7;
mosi <= '1';
--else
elsif(clk'event and clk = '1') then
last_cs <= cs;
last_sclk <= sclk; --clk version
-- if(state'event and state = S_IDLE) then -- entering idle init bit and rxbuffer
-- w_r <= pha;
-- bit <= 7;
-- rx_buffer <= (others => '1');
-- --initialize w/r
-- -- this will update whenever pha updates or state changes to re-init
--
-- elsif ( state'event and state = S_TXRX) then -- state should not trigger any toggling
-- null;
-- else
--pha 0 means write on falling cs
--pha 1 means write on first clk edge
-- w/r 0 means write
--w/r 1 means read
if(cs = '0' and last_cs = '1' ) then
w_r <= not pha; --pha 1 goes to write and pha 0 goes to read
if(pha = '0') then
mosi <= tx_buffer(bit); --write
end if;
elsif(cs = '0' and (last_sclk /= sclk)) then -- this point on are only clk events high or low
if(w_r = '0') then -- write
mosi <= tx_buffer(bit);
elsif (w_r = '1') then -- read
rx_buffer <= rx_buffer(6 downto 0) & miso;
bit <= bit - 1; -- only decrement after reads
if(bit = 0) then -- reset bit to 7 at end of byte instead of decrementing
bit <= 7;
--rxData <= rx_buffer;
end if;
else
null; -- do nothing
end if;
w_r <= not w_r; -- toggle write/read for next clock edge
else
--mosi <= 'Z';
null;
end if;
if(bit = 7) then
rxData <= rx_buffer;
end if;
end if;
end process;
--rxData <= rx_buffer;
end FSM_1P;
architecture FSM_2P of spi_master is
------------------------------------
-- State signals
------------------------------------
type STATE_TYPE is (S_IDLE, S_TXRX);
signal state, next_state : STATE_TYPE;
------------------------------------
-- Clock Div (spi clock) Signals
------------------------------------
signal sclk : std_logic;
signal enable : std_logic := '0';
--signal bytes : std_logic_vector (3 downto 0);
signal polarity : std_logic;
signal clk_active : std_logic;
signal byte_flag : std_logic;
------------------------------------
-- SPI Data Signals
------------------------------------
signal rx_buffer : std_logic_vector (7 downto 0) := (others => '1');
signal tx_buffer : std_logic_vector (7 downto 0);
signal cs : std_logic := '1';
constant byte_w : natural := 8;
signal bit : integer range -1 to 8 := 7; --init to msb+1 idx
signal w_r : std_logic := '0'; --write = 0 read = 1
signal phase_Delay : std_logic := '0';
signal last_cs : std_logic := '1';
signal last_sclk : std_logic := '0';
begin
U_CLOCK_DIV : entity work.clock_div
generic map(
clk_in_freq => 50000000,
clk_out_freq => 5)
port map(
clk_in => clk,
clk_out => sclk,
rst => rst,
enable => enable,
bytes => bytes,
polarity => polarity,
clk_active => clk_active,
byte_flag => byte_flag);
--enable <= int1 or int2 or go;
process (clk, rst)
begin
if(rst = '1') then
state <= S_IDLE;
elsif (clk'event and clk = '1') then
state <= next_state;
end if;
end process;
process(state, int1, int2, go, clk_active, byte_flag)--, txData)
begin
rxDataReady <= '0'; --only high when data is ready
polarity <= pol;
--w_r <= not pha; --first write for phase 1 is pre toggled. 0 is default
next_state <= state;
enable <= '0';
case state is
when S_IDLE =>
--enable <= '0'; --default
cs <= '1';
if((int1 = '1' or int2 = '1' or go = '1') and rst = '0') then
next_state <= S_TXRX;
-- polarity <= pol;
-- w_r <= not pha; --first write for phase 1 is pre toggled. 0 is default
enable <= '1';
cs <= '0';
tx_buffer <= txData; --TEST
end if;
when S_TXRX =>
enable <= '1';
if(clk_active = '0') then
enable <= '0';
cs <= '1';
rxDataReady <= '1';
next_state <= S_IDLE;
else
enable <= '1';
cs <= '0';
if(byte_flag = '1') then
rxDataReady <= '1';
tx_buffer <= txData;
end if;
end if;
when others => null;
end case;
end process;
cs_out <= cs;
sclk_out <= sclk;
process (clk, sclk, cs, rst)--, state)
begin
if( rst = '1') then
bit <= 7;
mosi <= '1';
--else
elsif(clk'event and clk = '1') then
last_cs <= cs;
last_sclk <= sclk; --clk version
-- if(state'event and state = S_IDLE) then -- entering idle init bit and rxbuffer
-- w_r <= pha;
-- bit <= 7;
-- rx_buffer <= (others => '1');
-- --initialize w/r
-- -- this will update whenever pha updates or state changes to re-init
--
-- elsif ( state'event and state = S_TXRX) then -- state should not trigger any toggling
-- null;
-- else
--pha 0 means write on falling cs
--pha 1 means write on first clk edge
-- w/r 0 means write
--w/r 1 means read
if(cs = '0' and last_cs = '1' ) then
w_r <= not pha; --pha 1 goes to write and pha 0 goes to read
if(pha = '0') then
mosi <= tx_buffer(bit); --write
end if;
elsif(cs = '0' and (last_sclk /= sclk)) then -- this point on are only clk events high or low
if(w_r = '0') then -- write
mosi <= tx_buffer(bit);
elsif (w_r = '1') then -- read
rx_buffer <= rx_buffer(6 downto 0) & miso;
bit <= bit - 1; -- only decrement after reads
if(bit = 0) then -- reset bit to 7 at end of byte instead of decrementing
bit <= 7;
end if;
else
null; -- do nothing
end if;
w_r <= not w_r; -- toggle write/read for next clock edge
else
--mosi <= 'Z';
null;
end if;
end if;
end process;
--
-- process (sclk, cs, state, pha)
-- variable clk_cs_event : std_logic;
--
-- begin
--
-- if(state = S_IDLE) then
-- w_r <= pha;
-- bit <= 7;
-- rx_buffer <= (others => '1');
-- --initialize w/r
-- -- this will update whenever pha updates or state changes to re-init
-- end if;
--
-- clk_cs_event := '0';
--
-- if(falling_edge(cs) or (sclk'event and cs = '0')) then
-- w_r <= not w_r;
-- clk_cs_event := '1';
-- end if;
-- --if sclk event, do w/r and then toggle it
-- -- if cs falls and pha = 1, write
--
-- --write if w/r = 0
-- --this works because at init w/r = pha
-- -- so pha = 0 is 1. no case will be true and w/r will toggle
-- --next event will be a clok edge
-- -- read if w/r = 1
-- -- if cs event and pha
--
-- if(clk_cs_event = '1') then
-- if(w_r = '0') then --write
-- mosi <= tx_buffer(bit);
--
-- elsif((falling_edge(cs) and (pha = '0')) or (sclk'event and w_r = '1')) then --read
-- rx_buffer <= rx_buffer(6 downto 0) & miso;
-- bit <= bit - 1;
-- if(bit = 0) then -- reset bit to 7 at end of byte instead of decrementing
-- bit <= 7;
-- end if;
--
-- end if;
--
--
-- end if;
--
--
-- end process;
-- process(sclk, cs)
-- begin
--
-- -- phase
-- -- 0 start : write on cs, read on first clock
-- -- 0 normal: write on first edge, read on second edge
-- -- 1 start : write on first edge, read on second edge
-- -- 1 normal: write on first edge, read on second edge
--
--
-- phase_Delay <= '0'; --set delay to false
--
-- if( bit = 0) then
-- bit <= 7;
-- end if;
--
-- bit <= bit - 1;
--
----
--
-- if ( sclk'event ) then
-- -- w_r <= not w_r;
--
-- elsif (falling_edge(cs) and pha = '0') then
-- --write
-- --next clock is going to be raising or falling
-- --if pol 0, then rising
-- --if pol 1, then falling
-- phase_Delay <= '1';
--
-- end if;
--
--
-- if ( w_r = '0' and phase_Delay = '0') then --write except if pha = 1 and before first clk event
--
-- mosi <= tx_buffer(bit);
--
-- elsif(w_r = '1') then --read
-- rx_buffer <= miso & rx_buffer(7 downto 1);
-- end if;
--
--
-- end process;
--tx_buffer <= txData;
rxData <= rx_buffer;
end FSM_2P;