EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
发送时,采取的函数为HAL_SPI_Transmit_IT();' o6 X" v& k( U; L0 C7 o
接收时,由于不知道对方发过来的报文长度,
& L* D, w1 G7 P5 }4 z# ^就不知道用什么函数。厂家提供的软件包里面的receive函数都需要
h. F8 Y% U& n3 r ^( Y( N指定size长度。
" @2 w# x4 w# @4 H! T下面的代码中,我在最开始开启SPI_IT_RXNE中断使能。
+ @. f+ D1 U% Y9 ?. l, Y通过中断函数和数据处理函数来解析报文。并把报文上传网络服务器。' k% V6 h/ V. i
但是ARM SPI接收不到数据。不知道为啥???
7 m* O! n$ |" l+ ~$ k9 n+ X* D本代码为SPI主机的。
0 D3 F* b7 p0 W1 k) b Z/* SPI5 init function */' i( `; [' J# l ~! ^9 ]
void MX_SPI5_Init(void)
. I3 u, [! ^# {, o3 Z+ a{
hspi5.Instance = SPI5;
+ M6 A/ Y0 Z' {2 T' qhspi5.Init.Mode = SPI_MODE_MASTER;
% N9 h8 e- a z, E: shspi5.Init.Direction = SPI_DIRECTION_2LINES;- u. ?$ S P3 k. s. D6 v. l9 m R
hspi5.Init.DataSize = SPI_DATASIZE_8BIT;
$ o7 q& r- k# ehspi5.Init.CLKPolarity = SPI_POLARITY_LOW;6 n. m: n& _) H# ?
hspi5.Init.CLKPhase = SPI_PHASE_1EDGE;
6 j9 ]! a9 x) l! b2 q, Rhspi5.Init.NSS = SPI_NSS_SOFT;3 j7 Q7 H8 L7 K
hspi5.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
" m: T/ L) U1 p* ~( S2 mhspi5.Init.FirstBit = SPI_FIRSTBIT_MSB;+ I a+ [0 a! U' c) p
hspi5.Init.TIMode = SPI_TIMODE_DISABLED;
8 v5 O! B& ]' E0 O( u) ~# Yhspi5.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;* \! ^+ y1 }/ o: L, m6 i
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); ...... }# Y3 M( _9 `- U3 @1 U& k
void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
. a; {2 M6 J, l) e' \{ GPIO_InitTypeDef GPIO_InitStruct;
" b" q7 ]3 P& `5 J& vif(hspi->Instance==SPI4)
3 I6 M5 L8 u! |, t{
2 n5 G$ R' Q+ L! K/* Peripheral clock enable */
3 g# G+ R8 C7 o1 a6 _3 T, m4 Y8 u__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)
' i! n$ U. u! `( Q. `0 h6 ]& Z+ G{2 s! E. F ]; t7 K* Q% h# s
/* Peripheral clock enable /3 r% y+ @9 y; @$ q# Z( a( i/ Z
__SPI5_CLK_ENABLE();& ~& _. c: r! |' z, h
/*SPI5 GPIO Configuration 2 B# l6 R$ C; ?% \, g* s
PF7 ------> SPI5_SCK/ j* l. a" p5 ]+ L( ]
PF8 ------> SPI5_MISO
' |+ {% `. H% j" S4 q3 {! z: ~PF9 ------> SPI5_MOSI
8 u- T3 n# j* @/ l z*/; {- ?7 e2 A) i9 p) W
GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;8 G9 J; ?$ o% _" X7 b2 X! ^# `4 y0 n
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;4 ?- L9 X. |# O3 t/ q
GPIO_InitStruct.Pull = GPIO_NOPULL;! _/ U% X. Z/ P
GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
j! i0 F7 m2 s* qGPIO_InitStruct.Alternate = GPIO_AF5_SPI5;
8 o% I* O! d% i7 U# dHAL_GPIO_Init(GPIOF, &GPIO_InitStruct); } } main()
; Z: _" g( E( T2 |{
1 Z: J* g/ s1 `* | d, k6 pMX_SPI5_Init();/ _: O, e. U( |/ N6 ^: N! U
__HAL_SPI_ENABLE_IT(&hspi5, SPI_IT_RXNE);4 F' u' c& O, D' B* S- W4 ] w
while (1)
9 {1 r; |! i8 \: |: y# L{0 f0 }# X: L, v. q
SPI5_Master_Rx(); //读取从机发过来的数据。
% P }+ i+ T+ L+ I4 w, B* c t; j}
2 B" \& }% G+ X/ y} typedef uint8_t u8 ;
0 K, i5 H% p, A! O4 d. `typedef uint16_t u16 ;8 \6 I; C" x; E( r4 U- G) w8 N3 Y
typedef uint32_t u32 ;
: x I& e# s2 t# {- |* I( [5 z0 yu8 hmosi[50]={0};
: j. h3 a9 \" Y- d# M; Ju8 mosi1[50]={0};
+ P5 w2 W" o1 f0 p0 `3 qu8 phmosi[50]={0};* d# d: K( w2 Q# k$ Z
u32 imosi=0;$ F( k4 @3 U6 c% I
u32 spi5_rx_len;
9 ?6 Y$ }, ?7 L' t; X& Ou32 ic=0;
" z1 G" {4 A+ N. ]7 K6 t8 Q#define comsize 8
) M$ f4 G8 b, M' }4 x
8 L/ Y* R& a: z7 T; gextern u16 tSession;
' A' P5 U5 P# O& n* S) Wu32 udata_len_spi; `, n* y% t7 O; |9 p% l& @9 ^
extern u16 ports[comsize+1];8 a! d+ a" k8 I5 ?" {
extern u32 uTimeLst;
1 E6 s9 d: ^6 s# j3 H2 ^" `4 i u0 \# p
extern SPI_HandleTypeDef hspi5;
) t$ Z$ x# [% ]& d) l+ \+ S& ovoid SPI5_IRQHandler(void)" L; T- E1 d* m( m7 u
{
% e* A9 L3 n6 j; aif(__HAL_SPI_GET_FLAG(&hspi5, SPI_FLAG_RXNE) == SET)
# E* _; x' } ?0 |( X{
. S$ Z m) S, Vhmosi[imosi++]=(uint8_t)(hspi5.Instance->DR); } } void SPI5_Master_Rx(void)
9 R/ ^' a1 D' F3 W& N; l4 n' E{
5 [# N2 ?2 S8 j q; Z2 C8 Q//for(;imosi>=(ic+7);ic++)7 H3 l9 }( Y5 o! T& J+ f
for(;imosi>=(ic+5);ic++)9 C2 Z9 E: |) U8 Y! _$ x% U' [& O
{
: Y% ^. q w7 L2 Z6 e4 Wif((0x2e==hmosi[ic])&&(0x17==hmosi[ic+1])&&(0x0==hmosi[ic+2]))8 w/ S% [' t+ z& W7 U$ {9 Z" G: b
{
. A% {( w R/ z$ z$ t! Xspi5_rx_len=hmosi[ic+6];
# K3 d* C' H! Xudata_len_spi = spi5_rx_len+1;+ b+ A: T8 z+ p1 k* u
memset(phmosi,0x00,udata_len_spi);6 |( O( |: C" J. G2 K ]6 \9 I% s
phmosi[5]=hmosi[ic+5];
, G) n0 L2 G C# |
3 j2 ]- t0 P: ?0 i% s$ Lphmosi[6]=spi5_rx_len-7;
) L& |+ ]" I( \9 h" y( J& T( w. mmEMCpy(phmosi+7,hmosi+ic+7,spi5_rx_len-7);
6 s: C0 h$ L5 C0 w- cmemcpy(mosi1,hmosi+ic,50-ic);
3 y L& f* g# F; x R' Wmemcpy(hmosi,mosi1,50-ic);; i7 H( ~) W* D, K+ s3 h5 M
imosi=imosi-ic;5 y( L1 _& T" H( v1 |# V
ic=0;0 G a, t5 K& n6 ^
//**************************! ]* O: m4 |; L7 n1 D* @& g$ A
tSession++;
2 v2 L: Q: g: u9 P+ R: \# Y2 wphmosi[0]=0x2e;6 h: I6 J9 i9 F
phmosi[1]=0x17;
$ H4 e4 p9 @6 Z+ ]phmosi[2]=0x00;
" B# C. {" M3 D- f, ]) w6 Q& o5 z: `. a* v7 m8 H# M
phmosi[3]=tSession;
3 o2 _' g0 b9 r- pphmosi[4]=(tSession>>8);
( T8 K' a, G* z" M6 W$ \7 e: ^0 n* [4 e$ y8 \' N
//memcpy(udata+7,tx1_buf,rx1cnt);5 E. O2 W7 m! w8 w3 l
phmosi[udata_len_spi-1] = crc(phmosi,udata_len_spi-1,1);7 I7 o) Q& v* z# @8 p
com2net(ports[0],udata_len_spi,phmosi);* E" Q+ {1 Y( y' @1 b& K6 D$ s' s
uTimeLst=HAL_GetTick();$ L' p, i+ ^4 C: n; e& X
}
8 |, y: j/ T1 W- u2 ?+ z1 H}' N. m% U6 ^. e3 `) k& l
}
+ U* h4 D2 M7 ?& d* L" x |