|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
本帖最后由 ygcgsa 于 2019-4-22 11:16 编辑
6 ^; z& P. f8 W8 ^9 h) P& X; f; O! Y& c
+ i4 P* l& T# p2 q. I* d我是用STM32和EPM240做SPI通信,STM32为主SPI,cpld为从SPI,8位通信,将always@(posedge CS_N or negedge rst_n) 下的 " m7 t, a4 s- t# ^
else6 N5 Q: C0 o3 J1 g2 Y9 x- k4 d! f
begin
( J4 r" \" K9 q0 E" F5 M txd_data <=txd_data+1'b1;
& U, s* D. t: G4 u3 Y end
" H3 h3 S: ~8 W3 ~ F& K' ]换成 txd_data <=5'b11100;(固定赋值)或txd_data <=rxd_data,数据都可以正常传输给STM32,但是当换成txd_data+1'b1后,每次加的值都不对,我实测了几组数据大概如下:+ v( N& K' e* F% f0 [% W
1,5,9,13,17,22,24,27,30,16,35,38,41,46,484 Y. ^2 S/ q, Y8 X
1,5,9,13,17,22,24,27,30,16,35,39,42,47,51有一定的重复规律性0 w8 Y7 a* C/ |
查了CS_N信号的边沿,也没有发现有多个上升沿,百思不得其解,情高手帮忙看看代码有没有问题
; d+ g) m* I3 g; S! r1 C2 S4 u7 S
! E! ?5 V9 }+ Lmodule spi(clk,rst_n,CS_N,SCK,MOSI,MISO,rxd_data);
( o1 [, f5 e% T/ R. e3 D6 Tinput clk; //CPLD时钟,50MHZ
+ F; O: H* r# y1 e) Minput rst_n;3 Z. U' U. ]6 j3 Y' n
input CS_N;
/ E z$ a9 d/ `' s( U# Winput SCK; //STM32发送的SPI时钟,实测周期2uS
, M/ h. `- ?' V$ I' {# Y. Z! G$ G! tinput MOSI;- `: u& o, U R2 Q
reg [7:0] txd_data;
3 U0 S* d: W' `- X, Foutput reg MISO; @' B9 Y, D9 r0 a# Y6 u
reg [7:0] rxd_data;
) [: j: e+ [- `3 noutput [7:0] rxd_data; V- o3 z* @$ j* m. z l
) ^$ p6 e& Z1 f9 F" M. x3 _
2 p* J7 M9 ?$ D9 n m: O, B& g, Q/ y
0 @! `" }2 D& j1 ^4 t" ~) \//-------------------------capture the sck-----------------------------
, E/ A5 n. [2 j$ \6 Vreg sck_r0,sck_r1;( k: o2 w5 e* X4 Z Q# N
wire sck_n,sck_p;
$ V( M5 T: V3 k U9 b1 l4 Talways@(posedge clk or negedge rst_n)
3 X2 i) x8 o( ~+ N1 [# [) jbegin) e% `0 u4 X6 u
if(!rst_n)$ c8 H: j2 u. B
begin' L2 ~8 D. T6 D
sck_r0 <= 1'b1; //sck of the idle state is high
/ q7 z* _% W$ l! N sck_r1 <= 1'b1;
0 N; L% K2 K1 p9 N0 } end
. f3 t1 O. O2 _9 e( }! V else; S* j; a1 ~) k7 F) g! R1 P# C$ O) L
begin
% U; I( h f$ q# P ~2 u6 y5 l sck_r0 <= SCK;+ i, s( y( c' |
sck_r1 <= sck_r0;
4 Y, Y& u6 f5 u0 l9 ]% M& E) u% y3 [ end% U% N; g9 B/ ?- f+ }$ w( S( p) t. c
end
; c7 q1 i0 W/ N" e+ v, i! K3 R: o' A% ^/ N
assign sck_n = (~sck_r0 & sck_r1)? 1'b1:1'b0; //capture the sck down0 s% o; R7 j' w+ j8 r# K% W. r4 A
assign sck_p = (~sck_r1 & sck_r0)? 1'b1:1'b0; //capture the sck up, e$ K# K* ]# O, a- T: n
+ D( ~1 o& T7 Q- ?0 n# Q- O, d" V! W
G. k3 J3 F: ?+ L# g* ?5 @! w) M//-----------------------spi_slaver read data-------------------------------
: m2 Y6 H0 X: C9 q ^+ }reg rxd_flag_r;- M3 Y& D; g, c/ Z# V" {5 W* [
reg [2:0] rxd_state;% A3 q* ]. J/ z) _) ]
always@(posedge clk or negedge rst_n)) F, G2 b& X2 d; W7 p- A9 l8 g
begin# f7 u6 M+ J; V3 x5 _* F
if(!rst_n); i% g g' o5 }0 V$ P! s6 o, n( S: u
begin
& B; e# b9 N7 u$ C4 i rxd_data <= 1'b0;" `% R* [+ ?1 I) {. ~! k
rxd_flag_r <= 1'b0;
3 ]$ t% f; b4 f# d rxd_state <= 1'b0;3 n% _( Z( Y7 c- ~- j$ e- |
end! \* P3 y/ I: `! y; V
else if(sck_n && !CS_N) 5 h" d7 N( d1 v5 S* n7 w$ w) K% Z
begin
$ R( R, t4 E/ ^, L" e' D- v case(rxd_state)9 ^/ f4 Y9 [$ o. G3 Y4 x
3'd0:begin
; O& d$ \- Y2 p' C- y, ~ rxd_data[7] <= MOSI;" T% `8 [3 g, Y9 |
rxd_flag_r <= 1'b0; //reset rxd_flag
; a- K" X2 X1 n4 l$ c! ~9 X rxd_state <= 3'd1;& ]. _7 Q L2 V% i& t! j
end: ^6 |: ]9 l, q1 Y( j" C
3'd1:begin
* Z) ]3 e! l: f, v rxd_data[6] <= MOSI;0 ^ b; u/ X2 X4 B8 g, e& Z7 M
rxd_state <= 3'd2;3 H1 z( z* u' Q T9 `9 `
end' Q# b" z* H; I
3'd2:begin& M3 f% O; h9 Q8 U. _) `) S
rxd_data[5] <= MOSI;4 `" S5 C b' v9 j- V5 n
rxd_state <= 3'd3;
# p( z' D" i4 H2 m2 z/ Y; |- f end5 l3 h' ^8 h8 K* Y' `( P4 D5 F
3'd3:begin
% m7 c4 ?; Z- l3 l rxd_data[4] <= MOSI;
% ^; }: G: `0 m5 [# n rxd_state <= 3'd4;# f9 P; K! u6 I+ U6 v" v T
end
4 h- u( X, U* ^6 y9 Q; w# Y _; O 3'd4:begin
0 N5 U: _9 T2 C. l3 A$ _ rxd_data[3] <= MOSI;
4 m/ W" [! D: I- R; V) n rxd_state <= 3'd5;/ @ d8 P6 v" S: N; F
end
% X2 x1 ^; P# C 3'd5:begin8 n4 i( H4 O& T8 S4 U
rxd_data[2] <= MOSI;
% \; t8 i i8 ^5 I# Q rxd_state <= 3'd6;# S9 `, y& l( m# ^5 b7 g6 Q' @
end
* P# D& `" L1 i; u4 u 3'd6:begin
8 P; H$ o& J+ Y' S A9 { rxd_data[1] <= MOSI;# A# m. ?2 T0 I0 d; ?
rxd_state <= 3'd7;+ ~- P) h) g3 _: D( ^( Z9 J
end
3 o2 C! i& s/ O8 } 3'd7:begin* t9 w& A: _9 l
rxd_data[0] <= MOSI;
7 V% u% V7 D% _" i rxd_flag_r <= 1'b1; //set rxd_flag
+ t) O8 I& _- b+ F5 R N rxd_state <= 3'd0;
) H! b) K' q2 Z$ e' `: Z( l/ g end+ `* t3 s) B: {5 c; i
default: ;# n' ]0 [* u* D5 N6 _6 m0 |
endcase
, x! y/ ?3 |9 g; p 1 \" ^7 b1 L' b0 y
end/ d0 F m/ t, p# U1 N# c
end# e, q7 f9 l# L8 X2 O* I
always@(posedge CS_N or negedge rst_n) //当CPLD捕捉到SPI_CS信号的上升沿时,要给STM32发送的数据加18 ?2 R9 J2 J7 \# O
begin: w ]; m+ B( a' _2 `$ V( n. M
if(!rst_n)8 U/ k5 W0 s P( G5 W
begin
& \" w, ]! {4 V1 F0 J/ c% b* @4 a txd_data <= 1'b0;( B1 S4 N* P- y
end
9 z$ Q8 a1 Z6 E! h9 y else
1 z7 L7 A0 g7 ^ begin$ ^8 C F8 q$ }
txd_data <=txd_data+1'b1; + J; U5 M& S0 E$ M0 V& j- }
end
8 B5 M' e0 O$ Oend
+ b- ?& O$ g/ @* y, k//---------------------spi_slaver send data---------------------------4 ^0 J o+ k1 C
reg [2:0] txd_state;# r& G% R1 t( e( e6 i
always@(posedge clk or negedge rst_n)
" ~: E% s: H9 q- Gbegin
5 @0 G, }8 P* n" M% C if(!rst_n)
0 A' H" Y% ?3 e: [5 ~* M begin
* k- l0 M" w. |' g2 D5 z txd_state <= 1'b0;, Q+ a" c _! m# y a3 s% a
MISO <=1'b0;( W( c" g* N0 V$ l, P
7 n9 ]$ | m y9 V5 w end
1 ^" U7 N9 E2 ?1 V+ k8 e else if(sck_p && !CS_N)
( a/ c1 |$ i4 J+ K. Z begin' P4 W9 m) v6 z% _
case(txd_state)% g+ V. }! _6 V: h
3'd0:begin
% v2 E- n& H" j MISO <= txd_data[7];, x( H& d- q' o
txd_state <= 3'd1;
; j n9 d3 M% t3 R5 A8 k8 } end8 V# V3 Z$ c+ s. f' t
3'd1:begin1 _! l% G$ F% N
MISO <= txd_data[6];, }0 r' h) Q9 Z9 d; q4 E: k
txd_state <= 3'd2;+ o; _ n [+ z" l
end
8 O# M7 |6 j, I" J1 G: J9 T4 C8 N 3'd2:begin
6 q9 E& l, |; O0 n% v2 h$ P6 f+ @6 ` MISO <= txd_data[5];3 I" G4 `( f2 d; ~; h4 _; F" g
txd_state <= 3'd3;
. r' M$ v. \( Z0 |2 N! h end
1 Z- w! ^% J+ \2 ^7 S 3'd3:begin
, a$ P4 ]+ X, ]3 ~ MISO <= txd_data[4];
- s( [3 P+ m. D ^% C" } txd_state <= 3'd4;4 s: @5 k5 [+ m0 s% U# c% @9 o
end
7 V8 F- ^9 I- s( ~ 3'd4:begin
9 n u0 L/ Z% H1 x MISO <= txd_data[3];
8 M6 N* J: Y; R2 O1 N2 G! ~ txd_state <= 3'd5;$ s: v; W; j% u8 M0 P) l. O+ k
end
& a# k6 T& D. Q) B+ |4 Q7 C 3'd5:begin
4 e8 s; i) C. y) `+ O* ]1 N( B1 W MISO <= txd_data[2];% O& ]2 R; D% J! r: k! _0 R2 x
txd_state <= 3'd6;
$ t' E$ Q+ C* t end
/ s/ |- ?* N* Z2 K# @& E. ? 3'd6:begin- w- ^- X8 E3 A
MISO <= txd_data[1];
0 ?% N `3 C7 ?+ r6 W& U txd_state <= 3'd7;
" m( D1 }+ e0 r8 n end
1 U/ ~& w; s: w1 k' p0 g( v. w 3'd7:begin8 G2 J. T# A& J' @
MISO <= txd_data[0];
p; @5 Y" {* _ R# j$ e6 g' h txd_state <= 3'd0;
/ }& \$ e4 o9 R
1 d& w4 w ^: I" Y, T. q7 x end% s$ w6 v8 M1 S# K( |8 t) a
default: ;
3 ?; p0 i2 p( S# w! @+ D) x endcase5 L0 V1 k) h9 f; v2 \( N. D( F
end
$ B6 j/ g) u" Q# _ l# r' W5 L0 E else if(CS_N)
' \; l& G. A6 E1 s# H( d begin1 V1 {2 k) v Q) P
MISO <=1'b0;) T1 n N( U% T* p- n
end: T) J H1 X. x8 t
1 b/ S8 Z l6 M$ d+ f6 G U9 l
7 p& y* c! I1 g+ Cend
- c. G& J& W+ U* I5 b( W' K# U: f B* i4 F: Y
& ^/ t! G0 C- K8 b, h2 _endmodule |
|