找回密码
 注册
关于网站域名变更的通知
查看: 407|回复: 2
打印 上一主题 下一主题

两个ARM间的SPI通信怎么实现?ARM型号均为STM32F429ZIT???

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2019-6-24 17:33 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

EDA365欢迎您登录!

您需要 登录 才可以下载或查看,没有帐号?注册

x

发送时,采取的函数为HAL_SPI_Transmit_IT();
, r& ^. Z* {) x, x接收时,由于不知道对方发过来的报文长度,
, A- Q+ |, Y: I( R" I- Q0 R4 I就不知道用什么函数。厂家提供的软件包里面的receive函数都需要: ~( A" `# R1 ~# n/ }9 Y$ Q  k3 Y
指定size长度。7 u- S+ K5 ^9 l* h; u
下面的代码中,我在最开始开启SPI_IT_RXNE中断使能。9 Z7 D, J0 F* w7 u8 G
通过中断函数和数据处理函数来解析报文。并把报文上传网络服务器。
6 ^$ R8 h7 _7 K但是ARM SPI接收不到数据。不知道为啥???

" c- L. D' `7 T$ [2 c
本代码为SPI主机的。6 ?/ d5 K9 q9 V* J7 ?* p; m, o! c
/* SPI5 init function */
: e: O7 s. G" K& F3 s* Kvoid MX_SPI5_Init(void)- U& h( F5 [% F1 J  i; I2 ]
{

hspi5.Instance = SPI5;- ^) j: S* ?# `
hspi5.Init.Mode = SPI_MODE_MASTER;
+ c7 Y) Q) e1 ]: g; U9 Khspi5.Init.Direction = SPI_DIRECTION_2LINES;
, C& [0 Y' b& A. R$ _" C" ehspi5.Init.DataSize = SPI_DATASIZE_8BIT;
1 r) f; u* t+ ~% c  }& q' dhspi5.Init.CLKPolarity = SPI_POLARITY_LOW;" h* A7 j, s! A, |3 G* A' w- W+ \
hspi5.Init.CLKPhase = SPI_PHASE_1EDGE;( U3 `" X3 G7 V& e
hspi5.Init.NSS = SPI_NSS_SOFT;  m1 k4 h4 G( X4 `2 l
hspi5.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;  \7 F8 E( R, {4 O! r5 F% V
hspi5.Init.FirstBit = SPI_FIRSTBIT_MSB;
6 z) J3 w; z5 rhspi5.Init.TIMode = SPI_TIMODE_DISABLED;: p5 v7 q7 \$ R1 \+ f2 O9 C( R
hspi5.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;0 u: d$ ~6 s6 f0 l2 O4 q/ x5 `
HAL_SPI_Init(&hspi5);

}

/**

  • @brief Initializes the SPI according to the specified parameters
  • in the SPI_InitTypeDef and create the associated handle.
  • @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  • the configuration information for SPI module.
  • @retval HAL status */ HAL_StatusTypeDef HAL_SPI_Init(SPI_HandleTypeDef *hspi) { ..... HAL_SPI_MspInit(hspi); ...... }5 I0 w% B' C9 R# d

void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)/ o& _  A; D' {8 W
{

GPIO_InitTypeDef GPIO_InitStruct;
! ?7 o: ^  V" h. S0 }( A' m6 gif(hspi->Instance==SPI4)
* j' @9 ~2 {. a, z6 w{. k  y2 M8 z0 ?+ C
/* Peripheral clock enable */
' J: T9 W2 |. |  `8 e4 M__SPI4_CLK_ENABLE();

/**SPI4 GPIO Configuration PE2    ------> SPI4_SCKPE5    ------> SPI4_MISOPE6    ------> SPI4_MOSI*/GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_5|GPIO_PIN_6;GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_LOW;GPIO_InitStruct.Alternate = GPIO_AF5_SPI4;HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);

}

if(hspi->Instance==SPI5)
7 N# v" ^- P* i( c6 f{# l' }) J2 b: ^( A- \4 F; D5 c8 P
/* Peripheral clock enable /8 l( b1 K3 c& I
__SPI5_CLK_ENABLE();: ?5 c- s+ L; Y6 R0 X' H- T
/*SPI5 GPIO Configuration
6 a$ v( Q4 ?' rPF7 ------> SPI5_SCK
3 b) P. y1 t+ _1 jPF8 ------> SPI5_MISO
2 w: U/ Z/ K$ cPF9 ------> SPI5_MOSI
* O: |- _" \7 I" o1 E( P*/
1 K) u7 O; ^& O$ J4 ]8 R' g3 xGPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;
+ M; E" p8 S4 ~GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
. J0 l- r5 D$ \% Q" uGPIO_InitStruct.Pull = GPIO_NOPULL;
/ g6 X$ x6 G' P& @6 [. qGPIO_InitStruct.Speed = GPIO_SPEED_LOW;
" c5 Z! Q; j( `7 G. V4 W+ `1 ?$ vGPIO_InitStruct.Alternate = GPIO_AF5_SPI5;
6 q* L7 G" G/ R% t9 r& E9 B  }; tHAL_GPIO_Init(GPIOF, &GPIO_InitStruct);

}

}

main()
3 R: @9 p2 W* f5 Z/ s2 X{# [& ~6 L1 y' H8 N6 U# u
MX_SPI5_Init();3 v/ g) R% I9 Q3 i% {; Q
__HAL_SPI_ENABLE_IT(&hspi5, SPI_IT_RXNE);9 m5 }4 H- h3 n- y  s
while (1)
$ Q2 T8 d2 o$ l9 n' O7 s6 O{$ a- ?# E; c  a
SPI5_Master_Rx(); //读取从机发过来的数据。
& T3 t- z5 @! _. a" N}
: N6 f5 Y. N! a}

typedef uint8_t u8 ;
/ x0 Q3 c; p- T- c. G* btypedef uint16_t u16 ;
9 t3 q* p: b1 X) V. p  y7 htypedef uint32_t u32 ;% S7 s; i; o! w2 Q- f6 r
u8 hmosi[50]={0};9 R5 O. Y2 l9 B* v
u8 mosi1[50]={0};9 j! ]% L1 e% _7 S8 F# o2 b4 A
u8 phmosi[50]={0};
7 }% g& E6 _; W( c- tu32 imosi=0;: @& ~" _, b3 c4 ]. J  r
u32 spi5_rx_len;( `6 h" X; K5 ~5 F
u32 ic=0;( |8 y1 t  J5 P  `
#define comsize 8) b; A# w4 f% ?7 S4 I. _' n  x

8 M2 M8 s' F3 b3 A: w* q# sextern u16 tSession;7 A3 O* r% H( Q4 s9 Y
u32 udata_len_spi;9 q+ g' f0 T9 O. ^" e8 k
extern u16 ports[comsize+1];1 N9 e) c2 V7 I7 S# O# }
extern u32 uTimeLst;3 P9 F; h, q& k& W% M5 Q3 V' t

) \5 t) @( p* E/ s- A. f! rextern SPI_HandleTypeDef hspi5;/ F6 L8 K$ t6 t& u' T  c* J
void SPI5_IRQHandler(void)% @8 \+ e" s& R" H6 C0 I- L
{
. r6 Y( m5 n# k9 s7 y9 N, J" ?if(__HAL_SPI_GET_FLAG(&hspi5, SPI_FLAG_RXNE) == SET); s' c7 n: D# ], p! S
{/ G/ I( d- g5 M9 R! ~7 h+ }9 k
hmosi[imosi++]=(uint8_t)(hspi5.Instance->DR);

}            

}

void SPI5_Master_Rx(void)
# s' ]6 K& Z1 x( V{
5 ^% [0 A) X8 O0 Q5 i; k$ l//for(;imosi>=(ic+7);ic++)! f; h! z8 q$ v: `# b) b# f! c
for(;imosi>=(ic+5);ic++)
7 J- r. t4 n- P% a. t9 R5 G, m{
+ ], z8 r! a5 T: G( Z( o: r( D( dif((0x2e==hmosi[ic])&&(0x17==hmosi[ic+1])&&(0x0==hmosi[ic+2]))
0 l$ _8 N9 p7 @1 R3 {/ d{
1 K2 F( e, f9 G+ n5 `7 L. j. |spi5_rx_len=hmosi[ic+6];
! `) j; R, X7 @9 t/ \- H+ ^udata_len_spi = spi5_rx_len+1;
: h# v. `/ T$ |, B( wmemset(phmosi,0x00,udata_len_spi);: z/ D; h. C9 N4 r, \. L; ~
phmosi[5]=hmosi[ic+5];
; V. E: t4 a; _) b# Z* M8 p
( w8 L+ I4 G. L3 Z6 R5 R7 zphmosi[6]=spi5_rx_len-7;
, \% g4 f$ y' h! R* @1 G% `mEMCpy(phmosi+7,hmosi+ic+7,spi5_rx_len-7);
4 h. W# e$ n$ |* h+ z/ Cmemcpy(mosi1,hmosi+ic,50-ic);8 a1 O2 f1 p. S# A, {
memcpy(hmosi,mosi1,50-ic);
  i3 P8 j8 x, c8 `- H* T; Uimosi=imosi-ic;
, J6 {  Y9 G8 f" }/ ]ic=0;
, e/ W8 [8 H6 a6 w1 R# x//**************************
9 B% z" V5 q3 p( S( UtSession++;  ]  K4 `0 _0 Y8 r4 V
phmosi[0]=0x2e;2 A, K4 U* _- R# {% e1 T
phmosi[1]=0x17;
" H6 C. H8 G2 G1 `" Lphmosi[2]=0x00;
, b  ~; c' W1 s  w) b- x) b9 F9 R4 w" _6 x  q
phmosi[3]=tSession;
7 M; {5 N' t. vphmosi[4]=(tSession>>8);, ]) d8 @0 k* y2 [$ e1 `

: u8 _2 L4 U& W1 L! w+ P//memcpy(udata+7,tx1_buf,rx1cnt);* Z- V+ p* v4 z, m
phmosi[udata_len_spi-1] = crc(phmosi,udata_len_spi-1,1);7 @$ `/ i- {$ l$ C
com2net(ports[0],udata_len_spi,phmosi);2 t2 O, ]7 W  C1 b6 w# P% \
uTimeLst=HAL_GetTick();
" `- Y, L6 K( \0 e( L}
0 i. b& q$ S7 g) e}
4 e/ J& l1 U0 q: [1 a8 `}

9 s* L5 f: D2 @% b- M% R
  • TA的每日心情
    难过
    2019-11-20 15:02
  • 签到天数: 1 天

    [LV.1]初来乍到

    2#
    发表于 2019-6-24 19:53 | 只看该作者
    同命相连,帮顶

    该用户从未签到

    3#
    发表于 2019-7-5 22:50 | 只看该作者
    我也遇到同样的问题,我在同一个板子上的两个口通信,总是收不到
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    关闭

    推荐内容上一条 /1 下一条

    EDA365公众号

    关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )

    GMT+8, 2025-11-24 19:37 , Processed in 0.156250 second(s), 23 queries , Gzip On.

    深圳市墨知创新科技有限公司

    地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

    快速回复 返回顶部 返回列表