|
EDA365欢迎您!
您需要 登录 才可以下载或查看,没有帐号?注册
x
library IEEE;
; N- ?& w/ s& g' cuse IEEE.STD_LOGIC_1164.ALL;* e0 m$ |, ?* E9 [% s) K8 v
use IEEE.STD_LOGIC_ARITH.ALL;/ P7 w7 d+ Z, i+ K+ j9 L9 E
use IEEE.STD_LOGIC_UNSIGNED.ALL;, e* \5 G- [: Q6 t
entity spi is
) z; P4 M/ y! P: qport ; t' k! C& u2 e9 {7 ~' o
(
V4 c# ]0 Q0 g+ Y& h reset : in std_logic; --global reset signal1 @: O( N+ t( Q, x$ f
sysclk : in std_logic; -- systerm clock* X2 b7 V; _- T8 R' Y
data_in : in std_logic_vector(13 downto 0);
8 x+ P ]( B7 |2 q' w spi_o : out std_logic;
5 G: N! Z# y$ w sck_out : out std_logic;
+ Y$ A, G+ M7 ^1 C ss_n : out std_logic_vector(1 downto 0)
. |5 \* O% D/ r' B( H C, }; U2 } );
: c& j1 v+ v% g: _8 Y2 m7 _end spi;& T0 @( {: M$ l5 I, U6 H: j) @
architecture b of spi is
, ]/ U6 q$ C( | Z) \& E type state_type is (idle,shift,stop); -- data type define
n( x; O2 Y4 x5 @7 F signal state : state_type;* X; w6 E9 x- Z, n2 y5 x2 e
signal out_reg : std_logic_vector(13 downto 0):=(others=>'0');( t+ |5 f; z& }. v" l* S
signal clkdiv_cnt : std_logic_vector(3 downto 0) :=(others=>'0');' d/ s& }7 |/ h8 X# G1 p7 t
signal bit_cnt : std_logic_vector(3 downto 0) :=(others=>'0');/ [8 t" M8 p3 U( i. o/ _. J2 h- A2 E2 a
signal sck_o : std_logic;
( J1 D0 {3 }$ f9 m" }+ X signal full : std_logic;/ m) n6 y3 T' E1 S2 |$ J p/ x- g
. } ?, z3 y' Cbegin
& Z) f4 s2 y1 c& V) u/ N( J7 u4 T9 b sck_out <= sck_o;% h( P* l, j( ~+ k3 H' H
process(sysclk)% x3 g) G6 c) O+ j2 a D
begin
. @% [' ~$ f E0 [ J: C; ? if (sysclk'event and sysclk = '1') then --reset
; Q1 }4 |3 R/ n+ j if (reset = '1') then: ^; s6 Z. b& x7 U p/ O" c! R0 z
ss_n <= (others=>'1'); --AD5553 idle CS =1
) C- N, u" `6 L7 v( O; I out_reg <= (others=>'0');
; Q6 j6 g! D3 o$ D4 C clkdiv_cnt <= (others=>'0');% l7 L; P n) M0 h$ Y" c4 @4 F
bit_cnt <= (others=>'0');
" Z& V! n/ o" k6 ?0 h" P1 c spi_o <= '1';
" s o- [- Z- R# M( T: } sck_o <= '0'; -- AD5553 SCK idle is 0) W3 L; c$ q P
state <= idle;
+ H% @$ ]& t, h* s full <= '0';
" X4 n' u- f3 B2 V- r; {7 J- Z p else
K" ^2 ~0 A6 p: P1 G* D# C- T3 [. n if(full = '0') then
- E1 k) P$ |4 a: i& h out_reg <= data_in ;
8 v( X. M# E' B1 @ full <= '1';
t4 G/ C- S# v0 Q0 ^8 l1 N$ N! E o end if;& r$ e) p) u. V8 Y6 {( f
6 t+ K9 X; l T case state is
* @* J8 i. V3 ?3 e; X when idle =>0 c" A$ x5 F0 ^" y# I
2 z( \% q5 e: I4 u state <= shift;
; m! m7 v8 w _6 v spi_o <= out_reg(13);
' z6 E4 v' q4 a out_reg <= out_reg(12 downto 0) & '0';
" |3 a# b, J& l' S% @' p% G/ W3 a7 B sck_o <= '0'; - g. Y* M, f0 ^' t. |5 Z. [4 ]
when shift =>
; M6 V. ?" q7 \7 O& r clkdiv_cnt <= clkdiv_cnt + '1';
6 R* w o+ L9 C. t if (clkdiv_cnt(2 downto 0)="111") then% d- b c4 W r* n: U- d
sck_o <= not sck_o;0 l9 I/ j% z8 U* W: I
end if;2 ]6 s( d, M" R* s' H5 l" z
6 Y; s3 J/ ? {! q ?, l1 O) u if (clkdiv_cnt = "1111") then
3 V: K9 x1 {% ]3 J7 z" X; m) `" }- A spi_o <= out_reg(13);& k. X4 L. j: N. r3 H: v' Q7 H
out_reg <= out_reg(12 downto 0) & '0';
7 {% B0 E# I1 _% a bit_cnt <= bit_cnt + '1';1 K3 F9 N; Q+ O) m! g
end if;' l. y$ i9 m0 y/ c* I
" f" ~2 ~; g& C4 {4 _
if (bit_cnt="1110" and clkdiv_cnt = "1111") then
9 e( P$ V0 n) O7 R state <= stop; F: [# |+ E( p; V: _
sck_o <= '0';
. r2 o. Y( y4 m4 @0 ?1 `$ P spi_o <= '1';" n% q( o& j+ {! d
end if;# `& C _% b# _$ _* n0 Z B
% G; I# J; f5 |: k* ?$ h' N when stop =>
0 o. t, `/ L( |, q" ^ state <= idle;
+ W4 m6 I+ i2 a$ S4 X% C sck_o <= '0';! m, t4 a' p8 C, _" [1 Y/ O7 N
spi_o <= '1';
: k# T/ \4 s# ~! ?% j* x clkdiv_cnt <= (others=>'0');7 o) T @3 `) T
bit_cnt <= (others=>'0');6 E5 ]6 S. Q, k' M
full <= '0';0 { }8 c o/ q/ G6 ]+ @
when others =>6 X+ N+ M: [ B( q0 Q1 o( I4 X
state <= idle;2 t) m, t4 _+ r) j$ R' T
end case;1 d/ Z! V, ?6 E8 ^" x( E" q
end if;
4 l& h$ s' N- m end if;
8 Z& z$ ^" o2 U0 G0 B: z% {9 W end process;' D5 e6 b5 l, }! s# V9 s
end b;) o ~1 ^7 `( Y$ D; l2 l
* {- I3 r3 v' a/ O
G& B0 s1 [' V0 F$ N$ F其中out_reg 一直是0,在idle状态赋不上值,大家看是怎么回事
6 I* @+ }* g* s" m |
|