EDA365电子工程师网
标题:
msp430 模拟spi实现对flash的读写
[打印本页]
作者:
hefuzhang
时间:
2017-5-31 21:37
标题:
msp430 模拟spi实现对flash的读写
#include <msp430.h>
: l; p1 C" k5 K6 G! `: }5 K" ~
char readdata=0;
; F. C. I, d8 T) S# v3 \
char busy_data=0;
2 R& _9 u( N+ q x6 L7 ]* B
void write_byte(char data_byte);
0 w& _; _- @9 w; k+ z
void send_command(char address1,char address2, char address3,char data);// command and address and data
8 w3 e$ \/ C" z0 y
char recive_data(char read_address1, char read_address2,char read_address3);
. A7 {2 C/ N& w, p
void delay();
* d6 x: G u( I3 ]" u
void erase(char command,char erase_address1,char erase_address2,char erase_address3);
+ z4 Y- v2 F8 x- l( Q
void read_busy(void);
: Y; r+ u6 ^, u. M% H# [
int main( void )
2 n. t7 Y, W6 E$ N3 Q) ?0 z
{
( d4 o- r4 ^$ K. G# m
// Stop watchdog timer to prevent time out reset
: I# o [5 k) ?% Q
; h" R) M4 i1 C3 F6 k+ r( C
WDTCTL = WDTPW + WDTHOLD;
6 P- e, B: a: [# o% _
P2DIR|=BIT5+BIT4+BIT0; //flsah sck hold cs out
' r0 F5 _7 C" x1 r3 V
P4DIR|=BIT2+BIT3; //flash wp si output
6 k7 h0 J; a" B, S$ b* e* l
$ m2 o7 b" g: o* C
P4REN|=BIT1; // so pull up
0 u0 C! c& a- |7 o5 J- h2 O
P4OUT|=BIT1; //flash so pullup selected p4.1
2 L( p9 k4 K4 r/ C; p) M
P4OUT|=BIT2; //wp=1
: S) R5 Y& m& ~6 ?
* D2 c! \4 I( F
P6DIR=BIT4;
5 h, n, |3 ]9 u4 A! S8 I5 T
P6OUT=BIT4; //led
' f' p/ Z0 \* i* G9 a0 l9 E
P2OUT|=BIT0 ; //CS=1;
+ K: x9 u" t/ T ]! ? ] S
P2OUT|=BIT5; //sck=1;
5 U9 e+ w5 {8 f7 C2 `9 Q
//P2OUT|=BIT4; // hold =1
: R5 n6 B4 D0 n
_NOP();
4 x6 Y& |' d& Z1 S U$ E+ I
_NOP();
- e9 \- ^' I& o0 [( R) |% T7 k
P6OUT^=BIT4;
l1 K% S. B& _% s0 j9 @
erase(0x20,0x00,0x00,0x00);
3 K* s ?# E3 P) _. J* ?9 F
read_busy();
( c& j9 x0 f" ]8 d% b
send_command(0x00,0x01,0x01,0x00); //xie,mingling
: [8 s# f" f6 a
read_busy();
% e$ }( {6 z3 ]8 i
readdata=recive_data(0x00,0x01,0x01);
, f x* n& o1 ?
while(1)
% W$ M. J1 t# r4 F, k
{
7 n5 n/ \ C- l4 D" e: D
// send_command(0x00,0x00,0x00,0x0f); //xie,mingling
, h0 Y, I2 [# a( _% w
, z" q! M/ [& C4 V: \
// if(readdata==0x00)
) F3 `+ g2 [, J$ z/ G1 O
P6OUT^=BIT4;
' g$ A4 A% p9 Y% ~' I1 l
delay();
0 `& \' c) t, u) ?' z
// readdata=recive_data(0x00,0x01,0x01);
: {, f) j8 l% L7 A
}
! w& G: N3 x; j4 t8 \& i, F v* }
8 U/ n4 Y: x6 F! t4 m
}
) G+ C( r U+ R8 Q @
void write_byte(char data_byte)
9 {% `% V! p- ~3 P. L _. I- j
{
3 x3 ]& j6 J$ i. F$ y
char num=0;
7 R( h8 ]5 @) T4 |, s- |; y, G5 H% R5 ^
for(num=0;num<=7;num++)
# _0 Z7 L0 n$ V$ k+ l
{
+ i1 x. p1 h0 v, n7 F- Y; \' k( m
! H% J. c) \. |
if(data_byte&0x80)
' Q* l8 F v" K+ |9 B2 g
P4OUT|=BIT3 ; //si=1;
) K5 p' F# ?% \6 T
else
% T& A- l" m# `) ^
P4OUT&=~BIT3; //si=0;
' z- k4 v6 I, J) ~' ^: G
P2OUT&=~BIT5; //sck=0;
/ H( }) P: r1 t* ?, n! L8 V" i
_NOP();
0 n" ~2 ~1 B; k! Z, n' D
_NOP();
( Q6 h3 V8 F2 v; a* L
_NOP();
0 k$ z4 o/ x* e6 r5 Q7 }, Z' G
_NOP();
1 c( b2 W0 A- t: I8 P
data_byte=data_byte<<1; //数据从高位传输
& _% c4 ]1 v& C5 V y
P2OUT|=BIT5; //sck=1;
8 }, I' t8 E" Q* v) b: L
' r- Y! ^ l8 t. v& w
0 `3 `1 M# K/ _8 \$ u# L7 ~
}
! Q6 k4 u9 I: ^; X$ C, D; e" ~; F
}
/ l$ e& E! N Z% m4 ?
void send_command(char address1, char address2 ,char address3 ,char data)
% \5 H! S; i* L6 ]. P
{
8 h3 `! h X# |5 W
P2OUT|=BIT0 ; //CS=1;
4 P+ t0 v1 b/ ?* w4 D$ G
P2OUT|=BIT5; //sck=1;
! ]8 A1 k' {7 k" o) [
_NOP();
( W; u6 U, s6 q3 ~
P2OUT&=~BIT0 ; //CS=0;
. K! ^ w" v+ e8 `: B
write_byte(0x06); // write command
) ^7 z! k) T5 x2 X
write_byte(address1);
% {: ~+ e( ]9 k. T( ~0 G' }
write_byte(address2);
9 |3 X# s& T& U' @4 ?8 z
write_byte(address3);
' g; b! f% R7 ]9 C* ~3 m
write_byte(data);
# `$ P- H: L+ S3 O
P2OUT|=BIT0 ; //CS=1;
_# y8 |: S, f" w; T. l& m
_NOP();
% T( j- z- h4 e
_NOP();
_0 }$ k. D" Z4 ?* B3 j
_NOP();
5 x. D3 ]& {+ i7 ~( z6 h
}
& ]( w- L: c! S. y1 ^/ p# R: G
; p0 J' \* E5 a X2 u' T; t9 `* R
char recive_data(char read_address1,char read_address2, char read_address3)
3 @' ]( o" q% s' G5 P6 N4 E! G& ^
{
6 N: N$ \/ u# N; _* q: U
char read_data=0;
4 ^" z% g7 ]# N9 ^6 T
char num=0;
6 o. R4 k5 N% U: v/ X* R7 G+ R/ g& R
: ?9 g5 p! F4 O- y) t( j+ a) ?3 s
P2OUT|=BIT0 ; //CS=1;
+ }+ y# S1 c, O5 p9 l6 {
P2OUT|=BIT5; //sck=1;
3 I6 e( n2 P; y8 P1 g& w$ h9 x
_NOP();
" ~- U6 d) q) x6 O& n L% ?# H
P2OUT&=~BIT0 ; //CS=0;
{& x1 g* F6 ^3 D* T9 w: R4 n$ E
write_byte(0x03);
1 l n7 x8 z6 {3 S. `/ y
write_byte(read_address1);
6 c/ E* z& _0 u% ~
write_byte(read_address2);
" u N/ }8 _% j6 ], K2 _
write_byte(read_address3);
F L# o& {7 D# a$ ~( E+ f! d
' s& b& p% s$ p2 X% ^+ T
for(num=0;num<8;num++)
5 H0 x! M/ P% E" ?
{
: P X+ t0 w: i ~( [
P2OUT|=BIT5; //sck=1;
3 I9 q* k. R: U# T
read_data<<=1;
" {, S; @! x; w7 ^6 \6 b8 b
_NOP();
2 D, g6 Z0 ~$ _! y% ~" L
_NOP();
* c* O* c( O3 `- I+ z+ |
_NOP();
" \ {) k& u) B7 M% V, u# J
_NOP();
) L; f* \# T, b7 }
P2OUT&=~BIT5; //sck=0;
6 C' A) H, E( x7 X" G) b7 e
if(P4IN&0x02)
7 X& g& K0 [: ]% c/ s9 o
read_data|=0x01;
2 [( U+ G0 ^( h# t
else
3 o3 W! z% X: ^" } ~$ L* X% C
read_data&=0xfe;
; U' W' b1 M9 |: i5 k
_NOP();
& Y5 \- w- M. M# F& W, Z) g
_NOP();
0 D- \4 n# @: y) O" e0 s
}
- w4 O# {3 H6 z; O2 d
P2OUT|=BIT0 ; //CS=1;
! a: \, z5 x! b3 n# t! E
_NOP();
8 ]8 b! g1 Y/ p0 A" X2 w
_NOP();
$ V6 |7 z5 u9 U$ c* l2 i
_NOP();
5 r% K6 a( ~: x- }0 z, g/ i
return read_data;
1 c5 R N& J5 `7 ~5 M
, I9 A& e$ C! K* m& g% \
0 _) t# S( u+ c6 ~+ I
}
$ |; t2 Y; e: k! Z0 z5 r& F
void erase(char command,char erase_address1,char erase_address2,char erase_address3)
+ Q' O& e' A, ^9 @, i8 z! {' }0 u) t
{
- l5 @ e: X: M. K# d
P2OUT|=BIT0 ; //CS=1;
" b4 B, Z& _4 |. k7 h6 c
P2OUT|=BIT5; //sck=1;
- |% N! D% J3 H0 m- K
_NOP();
0 ^* ?/ Y+ ?0 H$ A h- Y4 {
P2OUT&=~BIT0 ; //CS=0;
. a" K; F u; h+ D2 I: M, C5 L- h; W
write_byte(0x06);
) k5 ^$ `0 r8 C; C9 s9 x" A- o
write_byte(command);
& N$ W% P' I6 q/ h% Y$ c
write_byte(erase_address1);
$ k- i; M4 {2 |9 p' y* D& `0 L
write_byte(erase_address2);
3 p) s. @% Y! B# y
write_byte(erase_address3);
! m4 L# G1 m W+ O1 H
P2OUT|=BIT0 ; //CS=1;
4 N6 ]( u! j* Y5 J8 a Z
_NOP();
% z6 r e& ^8 Z K
_NOP();
9 h* I' H/ ^( Q& C5 {- |; x
_NOP();
# m( t) L6 I& q4 a
}
* W$ O+ S9 T0 A9 P/ \
void delay()
- ^( a: ]+ }0 d- v; y, g
{
( g, i% v# H9 Y, O9 V% B) O( T
unsigned int i=0;
* H( B0 y$ U9 F
unsigned int j=0;
; z; A8 k0 ] Q8 T' H6 Z) t" z @; Q
for(i=0;i<=500;i++)
9 m; ^) E; o" C9 k1 L; T2 V
for(j=0;j<=250;j++)
7 u: m. Q- p0 R; Y) R) A
;
6 Z/ n+ s$ O/ U5 d; k! S8 P
}
F9 e' S. o2 n2 o9 d! B+ r
void read_busy()
1 \$ w8 P1 `' v) d2 j1 U
{
8 { g2 q4 x2 a, T
char read_data=0;
6 F* d# D& }9 C& S! J' H
char num=0;
5 m7 f. U! P0 \& k+ a
P2OUT|=BIT0 ; //CS=1;
% B0 W4 T/ N( U4 w2 d4 d
P2OUT|=BIT5; //sck=1;
W1 c P. `7 W5 g; q. v
_NOP();
6 K% p1 L% R ~' [1 t. x0 R
P2OUT&=~BIT0 ; //CS=0;
6 W7 ^; K R0 I1 R: N
write_byte(0x05); // read the SR1
9 d, \5 r. q+ `6 ~, e( p
do{
+ G2 F8 K! p) i% \% \
$ J% G( s2 }* B
for(num=0;num<8;num++)
9 d8 ?3 Y$ Z" q. E, _- o' O
{
v5 B) r) H2 J" |2 O+ C) f1 L
P2OUT|=BIT5; //sck=1;
. ?% c/ v5 ]2 J
_NOP();
]* \9 \6 e- n q- a
_NOP();
7 _- d# F4 S4 [; D
_NOP();
5 q6 b# F! q5 ?3 X8 `) I, @4 t* p
_NOP();
1 T/ m# K+ a( q; R- C# \& o
P2OUT&=~BIT5; //sck=0;
* J) P9 M+ L) E1 m9 G! f& v- Q& [
if(P4IN&0x02)
' x+ Q$ {* F k' ?2 X, @1 [
read_data|=0x01;
1 q# e( L. D" @
else
! L- Z# n) l% c( F9 i2 _6 R5 ^
read_data&=0xfe;
4 I- @+ Y6 b3 P# e/ ^3 U
read_data<<=1;
( S, u& e' C) p9 B" v
}
; x3 [+ [# F/ Q0 T8 J0 I* i" Y1 D: j
_NOP();
5 w; e- c: S1 [
_NOP();
( M2 W8 n1 _- r! L: e6 ~* C
_NOP();
) p: q2 {" _' Y! N1 W9 x
busy_data=read_data;
) Q u* B: k, f# D6 ~. n
}
$ c4 ~. t4 Q1 y, N6 Y+ {3 P
while((busy_data&0x03)!=0x02);
8 C9 @5 E; i1 U* t3 H+ h! `. |
P2OUT|=BIT0 ; //CS=1;
g4 d# a+ Q3 Q G* G, W4 p
7 [% w" |$ i; L5 Y6 k3 m* e# n
# w+ d/ |2 `% C% y3 f
}
( R) y% m* s) G% k
: N0 S' a: o+ I! z5 j3 n" u
用的单片机是msp430f5438A,模拟spi实现对flash程序的读写,现在读出的数据始终是FF,即应该没有写进数据,那位大神开源指点一二!
6 W. w; V0 m- s; r* y; `/ C: b
, _% R9 `9 l/ A D) B3 z/ C9 J4 p% }
欢迎光临 EDA365电子工程师网 (https://bbs.elecnest.cn/)
Powered by Discuz! X3.2