|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
library IEEE;
; S& g* x0 a6 puse IEEE.STD_LOGIC_1164.ALL;0 C" {1 Q; W4 d9 S5 Z- \
use IEEE.STD_LOGIC_ARITH.ALL;
' }) a4 {. C4 ruse IEEE.STD_LOGIC_UNSIGNED.ALL;8 ]* y6 [& x4 ?7 ]" |' ^9 A; Z. x
entity spi is
. y! G* H; v" r: q _- Eport 8 [: g$ L; f7 U6 k
(
/ k( ]! g+ [3 U' C reset : in std_logic; --global reset signal
& j5 a1 g7 E, J; n8 \ sysclk : in std_logic; -- systerm clock! w4 e+ z9 N0 N$ E2 H! B
data_in : in std_logic_vector(13 downto 0);
9 o) L% r& o/ g! V" D4 _( h spi_o : out std_logic;) i4 I! F3 ^: g u" D) s" R, O
sck_out : out std_logic;
2 K- W6 H7 D6 U; m( q4 r, ~ ss_n : out std_logic_vector(1 downto 0)' \7 a0 \, c- `/ {
);
1 l. B# y1 W, g) wend spi;6 ?* C: b: b' x! u: ?6 {4 L2 S0 Z8 E
architecture b of spi is
3 G1 J) C0 m5 k$ t. G5 F type state_type is (idle,shift,stop); -- data type define7 ]0 d# e o5 U$ r' H) i+ Y1 L+ J
signal state : state_type;
* x! ~+ x M. D+ D3 f signal out_reg : std_logic_vector(13 downto 0):=(others=>'0');
8 a8 r1 ]4 N( e6 `0 j1 `4 a5 a signal clkdiv_cnt : std_logic_vector(3 downto 0) :=(others=>'0');
# y/ c& Q2 ?' r. H0 R2 F5 j signal bit_cnt : std_logic_vector(3 downto 0) :=(others=>'0'); s! ^' P% x* M% b4 v. w `/ n
signal sck_o : std_logic;8 c. |, ?" ^8 \ v! j
signal full : std_logic;
" k0 S- n$ ?& T3 M" D; B : @+ t( G- _0 a2 [( K5 h
begin2 q; Q: ^6 x. q9 s
sck_out <= sck_o;
( S3 L1 T) t1 B' |+ U; u$ t process(sysclk)" h: [! `+ i- n0 z6 V: E8 b, E" c( o
begin
! k$ D: z) B6 U9 Y# }: s) }8 a if (sysclk'event and sysclk = '1') then --reset. l/ x* O% X K: l
if (reset = '1') then; n8 b0 U! G, v, j
ss_n <= (others=>'1'); --AD5553 idle CS =1
$ r+ _. v' l* C. ^ out_reg <= (others=>'0');. {8 c% e7 g) y, D' A
clkdiv_cnt <= (others=>'0');
7 k7 A- L! S, V bit_cnt <= (others=>'0');
- C9 m+ R& }5 G3 Z( } spi_o <= '1';
" g( W W' M% e, C sck_o <= '0'; -- AD5553 SCK idle is 0
/ G% @; u2 H- b7 O state <= idle;& B0 x4 O( |8 H
full <= '0';
# x7 } k/ {# N else @, E8 B/ L# J' d* w& @9 p+ t
if(full = '0') then
# [& d; h, @+ f/ O0 ^ out_reg <= data_in ;4 T- D; F# o6 i& L+ R6 ?
full <= '1';/ u5 v( m, u5 ]5 |- o5 s
end if;
7 [6 j( H2 B+ I" w; J: M * }3 D! ~6 g y0 h- {3 p7 t/ v" x# G
case state is / K, C' G& B& G* N% F2 o
when idle =>
7 l h' m) ~% t ! G' S; G) j0 A. w8 w, x( g
state <= shift;
" J3 X0 z1 k/ C* F9 `, G- T) K# w spi_o <= out_reg(13);
0 h& I6 t. L% n; Z out_reg <= out_reg(12 downto 0) & '0';
# ^) h% q2 d$ b sck_o <= '0';
5 a: `( S) d2 W/ F9 P- Y when shift =>( M |- t6 ^* p7 Z
clkdiv_cnt <= clkdiv_cnt + '1';
6 h# h# ^ h* i if (clkdiv_cnt(2 downto 0)="111") then
: M. b4 Z7 M6 ~+ d$ |& q4 \+ y sck_o <= not sck_o;
( \: i. L {/ p4 N% Q$ O end if;
. ?6 ~! K4 ~/ o& B' T# }, a " u' H# h" u7 [ [, y
if (clkdiv_cnt = "1111") then( n' |$ T4 M* {$ f' y9 T
spi_o <= out_reg(13);
8 [' P5 L# \4 I# n6 } out_reg <= out_reg(12 downto 0) & '0';
: R3 I: V8 x$ E9 O2 q; C bit_cnt <= bit_cnt + '1';$ K4 p# M0 ]! ]( S
end if;
8 e4 u, D! f% T' _5 ?7 }
8 F. n8 j- H( y) I0 t if (bit_cnt="1110" and clkdiv_cnt = "1111") then
, t$ v( t/ c1 b state <= stop;( _, h" f" b9 I: X& x
sck_o <= '0';
2 J! d) \" Z# g2 G: p u spi_o <= '1';
* E: v2 l0 P& ?! H end if;. q- w) i0 k, z* f) J$ f
; u* A' G& f2 Q, j# I, c# t" s; R( Q$ G when stop =>0 s- ^8 s" T3 M& u
state <= idle;: N& d' Z$ l" B6 a W+ f
sck_o <= '0';5 g2 |# d9 J3 ?: S3 F" Y5 g* V
spi_o <= '1';% u" l: i% ^8 W
clkdiv_cnt <= (others=>'0');
3 a5 @6 y* S4 X0 O& ]6 p bit_cnt <= (others=>'0');* m0 f/ q& j1 y: j" d
full <= '0';
; P u5 k2 H( v3 v0 U" W4 Y2 | when others =>
* O7 o1 p/ C9 ~! ]6 ` state <= idle;
. X0 C3 _. {, @6 U. c' B( I end case;9 E! `9 ^& i1 S5 Q. J
end if;! Z! S0 L0 a- g* Q7 w
end if;
* R9 x8 i) R, q; F end process;& G0 Y8 _9 ?9 a% W. u
end b;' x* o) _5 h! \/ s
5 I( ]' \# {% B4 _8 V
7 K' \* c4 \/ O6 w4 ]/ u8 l其中out_reg 一直是0,在idle状态赋不上值,大家看是怎么回事
; R8 r4 x6 [0 Z, r) t9 V8 N) H' g- { |
|