你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

072RB-Nucleo上手之SPI

[复制链接]
woshilee 发布时间:2014-12-16 19:36
本帖最后由 woshilee 于 2014-12-16 19:55 编辑
: K: p  J5 B- W
3 m+ A8 X$ r, \% eM0的SPI终于调通了,玩儿M3,M4多了现在玩M0就有了固定的思维模式,老路走不通的话就要找它们之间的不同之处了贴上程序吧, i8 Z1 D% [- o
#include "stm32f0xx.h"
$ k! K* a# c/ U  \#include "stdio.h"% n# v/ e2 E/ g! k' ^* k
uint8_t         getbyte=0;
+ ^6 R' n4 i( s' q: W3 a3 H7 Z/**
) ]' ]  X  z0 z% |. ]- E4 G& J  * @brief  Delay
# C: d6 d9 v' t. Z; f8 c  * @param  None
& L. j. A& I& n6 o9 j5 \, r  * @retval None) d- Y- D8 u' r8 ^9 i, k) t
  */
" Q# q7 ?5 F: g8 `& I3 wvoid delay1ms(unsigned int ms): r7 _, p. T, S3 x( G: t
{
9 L4 `5 U0 _* ^1 X: ^0 p+ S    unsigned int i,j;- h% S' U, I6 w& G2 Y6 S
    for(i=0; i<ms; i++)
/ a1 f8 ]: F6 V* i6 j# U' K- L) }* V        for(j=0; j<100; j++);- T: t# Y/ R& \1 f& V- V
}
, X* U% K* f2 j; q, \  l8 v  _, \* p3 {4 n
/**! y6 \1 q* o' u- |; N
  * @brief  GPIO Config" b1 F* a: a% h2 Y
  * @param  None
& S8 c9 W$ R6 e  * @retval None3 G$ ?4 [3 E# W! J" M4 ~) e/ x) C: X
  */
* C; Z. ^& {( R5 U( O  nvoid GPIO_Configuration(void)
# l' Q& A2 s: Y5 L9 v/ }{
/ i4 Q$ [/ @2 C$ A' ^8 J3 ~    GPIO_InitTypeDef        GPIO_InitStructure;
+ u% g  e5 Z8 V
  ~5 R+ g+ M) g5 X    /* Configure  PA5£¬PA7 in output nopull mode */
  t- l8 ^  }7 A% S- r$ x( ]4 F  r    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_7;
* Z& F8 J  d7 c5 H          GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
' Q' B! w2 }/ d4 o; d          GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;/ u3 @2 [, Y( `4 L  v& N0 _
          GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;( A) J/ I; C- }. Z
          GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;4 x# c- J, ^5 I1 f' R4 G" I
    GPIO_Init(GPIOA, &GPIO_InitStructure);
8 R: F) m2 g" p- t' m. [* @# c: g$ x# ?9 v8 a0 w: ?
           : u/ R' n. |+ N4 H, M
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;- M- y' W5 b4 p
//    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
9 Y1 p. i! ~" _7 r% t3 m+ h    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;8 w9 N8 r- ]+ Y2 `
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//ÍÆÍ츴ÓÃÊä³ö) s* {3 C0 L0 J3 f: G
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
: X& E# B/ m9 Y/ Q- w2 e* l    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//Ðü¿Õ»òÉÏÀ­ÊäÈë2 Z' x1 B: G2 ?
    GPIO_Init(GPIOA, &GPIO_InitStructure);
! S: k# E5 I1 @. l
) w) |& t  B; c4 R2 P  L# f- W/ K
    GPIO_PinAFConfig(GPIOA,GPIO_PinSource4, GPIO_AF_0);) i  V. q- g; j: ^& j0 e5 Q
    GPIO_PinAFConfig(GPIOA,GPIO_PinSource5, GPIO_AF_0);
( b5 i5 Y$ L4 C    GPIO_PinAFConfig(GPIOA,GPIO_PinSource6, GPIO_AF_0);
' t5 `8 U* m  ^    GPIO_PinAFConfig(GPIOA,GPIO_PinSource7, GPIO_AF_0);
' d+ K0 t* A6 @8 g}
- L2 Z( W/ `$ u; Uvoid RCC_Configuration(void)3 D/ @- y7 i# H; ~6 n+ V3 o7 n0 ]
{$ f* P, e% [3 u$ t+ e# `: g
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
, u$ G* _/ J5 d8 Y2 W    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);6 U3 i, X5 E7 p5 `2 t. v/ o& P
}5 n9 p2 D/ J$ ?
void SPI_Configuration(void)
+ G5 R7 |7 |2 P, P{% I- D0 O! O# C( u
    SPI_InitTypeDef SPI_InitStructure;
9 z/ g1 b$ E/ b2 y- E$ k                SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;         ' J' m+ n1 w5 B- h% ]9 i
                SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; - S+ ^  L9 x" p( n! c6 N3 h# z
                SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;$ c+ n9 D! O3 ?3 u
                SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;  P6 Y$ s( [/ k9 _0 M+ \
                SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;   1 F& s- }0 V! J: z/ n* C3 G
                SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;2 n) T3 H2 I, I. w5 E" U
                SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; 1 e7 r/ s0 C: F% E! T( }8 Y- |
                SPI_InitStructure.SPI_CRCPolynomial = 7;                                 ' u; u* R+ V; i* i- h' z7 z
                SPI_InitStructure.SPI_Mode = SPI_Mode_Master;" o+ i. Q3 z1 f$ t" ^' t& m6 s5 m
                SPI_Init(SPI1, &SPI_InitStructure);                                          + t$ D- m* e6 }8 o8 q( Y1 ?
                SPI_NSSInternalSoftwareConfig(SPI1, SPI_NSSInternalSoft_Set);* |0 L. z* H! n1 `; ~( Y, Z
        
. [/ ~5 I4 a) h5 o) ~$ t                SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_QF);
) z. U- I# u. o; q" S        ! l4 O! `/ I; y& v# h
                SPI_Cmd(SPI1, ENABLE); /* SPI enable */
8 }6 o* H& M8 i        1 p7 T/ ]. e- }1 |- c5 O
}% c( B2 N( \$ b* Y
; p; P. K, [8 \3 u7 V$ O
9 m% C4 U$ [$ p+ F6 g7 U2 H
uint8_t SPI1_ReadWriteByte(uint8_t TxData)
) ?* m9 `! P; \, y  @+ _{               
* n& H' n% U) O9 N% P        uint8_t RxData = 0;
# I9 I" Z- M  _        while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);         //µÈ´ý·¢ËÍ»º³åÇø¿Õ & H) u# S5 y% @  I
        SPI_SendData8(SPI1, TxData);                                         //·¢Ò»¸ö×Ö½Ú' _& D( Y4 r* g( G: ~. P" M$ K: f
        
! b$ e4 |- M5 A8 X( |" `) j  while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
% U# a9 z) I4 v% Z( U        RxData = SPI_ReceiveData8(SPI1);//SPI2                                     //·µ»Ø½ÓÊÕµ½µÄÊý¾Ý*/- q+ H4 G! M5 D* t
        return (uint8_t)RxData;                                    & s) \  B* f( a2 E
}
% ]" A# _& Z& F! g8 W2 c) p. j7 ~2 n& a8 {
! \% l  o6 Z0 m2 R/ n" ^

$ f# C$ Z# T! k; O& D% _
( B9 f5 k; _; O1 I4 ~int main(void)  o; W) Z- ~, e& n! s
{' l- @" `7 h& F9 e  r+ s( g3 m4 ^

2 R+ P* T" }! J  ]' C        RCC_Configuration();
  V0 Z5 o2 k+ h& s5 I+ l1 V        GPIO_Configuration();
% f, o5 s1 i( P        SPI_Configuration();
" i; L* l9 ?" l        
) a. Q4 O6 M+ i2 x" Y9 a1 J& t3 b//    NVIC_Configuration();. M' l* o/ s$ E$ [& l
        
" c2 T9 Z& M1 t5 e- c* X) Z* h; v
' R' W$ |/ K" I* T) b7 w' u$ ?        while (1)
! d" g5 B' x2 K# Y2 v        {- d4 ~; I' s/ ?0 I
2 n* |1 m9 e; S% d0 l& Z
          getbyte=SPI1_ReadWriteByte(0x55);9 L# D$ U2 y5 A; m9 d0 P! V
                delay1ms(100);0 Z  `2 _# Z" v1 n/ O8 h
    }( ]" D9 ?0 d" V$ {- \
}, x) ?" f! B" q8 V
红色的语句在M3中是没有的,在M0中这个函数的定义如下:& H% q& H! \" a" r

, I" S) L0 b9 v5 p/**% X% H! e  R5 t+ J! D6 r
  * @brief  Configures the FIFO reception threshold for the selected SPI.: }- L% m! f  B" H
  * @param  SPIx: where x can be 1 or 2 to select the SPI peripheral.4 B9 D/ q( A+ }  e% H: r' H6 b
  * @param  SPI_RxFIFOThreshold: specifies the FIFO reception threshold.6 `2 r8 l  A  V: O: F; N4 U$ v. J
  *   This parameter can be one of the following values:
* g4 p4 a; j$ y/ X# X" k. T) M8 }3 P0 \  *     @arg SPI_RxFIFOThreshold_HF: RXNE event is generated if the FIFO ' o7 L; C5 ^  m, F8 n5 G
  *          level is greater or equal to 1/2.
# h4 N  Q( e# g0 x  *     @arg SPI_RxFIFOThreshold_QF: RXNE event is generated if the FIFO
, O! k6 a9 w. L* [- l/ o  *          level is greater or equal to 1/4. * n8 b5 }& ~* `0 [
  * @retval None0 ~; M; K/ T2 G
  */
% A7 @# b( u& E6 ?6 Ivoid SPI_RxFIFOThresholdConfig(SPI_TypeDef* SPIx, uint16_t SPI_RxFIFOThreshold)6 d4 R+ y5 Z5 q- \2 M0 B5 D% C
{
. R/ C" m  s; D% }6 J  /* Check the parameters */
8 z3 E+ j- t2 [9 ^" r6 x$ E  assert_param(IS_SPI_ALL_PERIPH(SPIx));
' K: o# C% k. T, K$ n5 v/ h  assert_param(IS_SPI_RX_FIFO_THRESHOLD(SPI_RxFIFOThreshold));( |& ]2 S5 D9 b) d1 r& ~: H5 S7 q

; X# H( I+ @+ U) ^. U4 u  /* Clear FRXTH bit */( p) Y) f9 d: t/ k+ F& F
  SPIx->CR2 &= (uint16_t)~((uint16_t)SPI_CR2_FRXTH);
3 A1 ^7 \8 a5 L9 O( I2 {. a% f9 Y* @9 f0 H7 Q9 R4 }! B! w
  /* Set new FRXTH bit value */8 O3 ^; N+ Q* `2 Q
  SPIx->CR2 |= SPI_RxFIFOThreshold;3 e% @5 ]5 R8 L
}. }% ~+ C$ v& _" p1 f

' X+ j7 O5 c/ R3 Y! z如果不加这句话的话,肯定会卡在判断RXNE位的地方,因为没有产生这个标志,如果用延时的代替的话是可以发送出去的,但是时序就会有问题,因为发送语句执行后不是立即就完成的。希望大家注意到这一点
1 I2 k% J. N7 W: E* Y/ w1 C* N# v  y8 P. z) F, [# a: X
所有SPI 数据换都通过32 位的嵌入式FIFO,设置FRXTH 位。 RXFIFO 的阈值必须和对SPIx_DR 寄存器的读访问的字长对齐比如发送的是8位的数据,就要设置SPI_RxFIFOThreshold_QF: RXNE event is generated if the FIFO   *level is greater or equal to 1/4. 就是32/4=8--》1个字节& v5 X3 d; r8 `( m7 F5 d! r
如果发送的是16位数据就要设置SPI_RxFIFOThreshold_HF: RXNE event is generated if the FIFO   *level is greater or equal to 1/2.就是 32/2=16--》2个字节0 i$ b; @  l" P
M0-SPI框图.jpg
收藏 1 评论3 发布时间:2014-12-16 19:36

举报

3个回答
沐紫 回答时间:2014-12-17 08:56:09
谢谢楼主!
子曰小玖 回答时间:2014-12-17 09:04:43
好帖子必须顶一个!
woshilee 回答时间:2014-12-17 14:35:28
沐紫 发表于 2014-12-17 08:56- ]$ {& Y# t2 D
谢谢楼主!

' x/ v+ ^3 e. ?- P! s我这是应该做的,跟你比就小巫见大巫啦

所属标签

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版