EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
发送时,采取的函数为HAL_SPI_Transmit_IT();- A! f0 r# | J+ q- O k; H; N& e
接收时,由于不知道对方发过来的报文长度,
, [6 j" f1 i7 X& N就不知道用什么函数。厂家提供的软件包里面的receive函数都需要
1 B1 M3 E$ _3 Y0 \( _' @# m- z指定size长度。% O9 T1 f: D% a% }: |
下面的代码中,我在最开始开启SPI_IT_RXNE中断使能。
' \; q8 L! v2 I! [+ L通过中断函数和数据处理函数来解析报文。并把报文上传网络服务器。
: k9 k1 W7 \; O" q5 l) [但是ARM SPI接收不到数据。不知道为啥???
, g' e+ B" r& H1 m3 n本代码为SPI主机的。
! F6 Q* N* A, n2 d( q. A9 e/* SPI5 init function */3 P+ S0 K& h( o" ]
void MX_SPI5_Init(void)
- M1 E* X' r7 v- {! d, N{
hspi5.Instance = SPI5;3 |3 w! Q: J3 W9 h' W* K4 q) c
hspi5.Init.Mode = SPI_MODE_MASTER;
& m; C' i& v/ I9 l) qhspi5.Init.Direction = SPI_DIRECTION_2LINES;
, R) e5 a' v$ J) Z' H8 _2 |hspi5.Init.DataSize = SPI_DATASIZE_8BIT;
! w' m9 [- ^5 O, Z7 C# Mhspi5.Init.CLKPolarity = SPI_POLARITY_LOW;
( g8 i" d: }, Y. U8 fhspi5.Init.CLKPhase = SPI_PHASE_1EDGE;) a) T7 r4 ?5 [' B' b- \# S! \7 g
hspi5.Init.NSS = SPI_NSS_SOFT;
& S) b3 o- `# s" mhspi5.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
( B( @% r9 T/ Z4 F; B9 B7 xhspi5.Init.FirstBit = SPI_FIRSTBIT_MSB;
! l! K" F7 G( W7 _( p. shspi5.Init.TIMode = SPI_TIMODE_DISABLED;5 r! F4 u( h+ A3 f5 U5 u
hspi5.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;1 l! Y0 M4 W7 B/ H: ^: w
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); ...... }3 h) b3 S& A C( L8 j7 t! k
void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
( p; U3 R* s& o+ E$ |{ GPIO_InitTypeDef GPIO_InitStruct;3 [/ z/ a' ?# D3 c5 h
if(hspi->Instance==SPI4)" h7 \9 S& U/ N" T F
{4 v" F9 q3 v$ Y1 \, C$ F2 e
/* Peripheral clock enable */: f! ^$ Y1 p4 S6 R/ {- I
__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)* L, i. u1 Q7 R
{
7 e3 l. B. f3 T! Z/* Peripheral clock enable / o5 M. k- p$ \! A, M% v
__SPI5_CLK_ENABLE();
8 C4 X1 D" P/ F- ]! M$ g* H8 u/*SPI5 GPIO Configuration * L! f0 }% r+ K: {0 S W* U/ E
PF7 ------> SPI5_SCK
& h9 w. u- H9 g/ lPF8 ------> SPI5_MISO+ E: w: G: P4 i' Z9 c! l
PF9 ------> SPI5_MOSI4 |' g! b2 F, X
*/
0 K/ d0 ?/ E5 t" RGPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;7 A& F% E; \7 U% R
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
! Z0 ?3 O( E0 }4 W( VGPIO_InitStruct.Pull = GPIO_NOPULL;
( d Z2 X" G7 A/ c) R' N; W, mGPIO_InitStruct.Speed = GPIO_SPEED_LOW;
0 A. c0 i5 c8 u: |0 R# h bGPIO_InitStruct.Alternate = GPIO_AF5_SPI5;0 [; \' D; m0 n1 m. H8 Y! J: f" H
HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); } } main()
- r% x2 ^ d L) s* Z: o" V" P{
/ X) k* \8 q( @; D! u4 NMX_SPI5_Init();
# q7 \2 t2 p' N7 n__HAL_SPI_ENABLE_IT(&hspi5, SPI_IT_RXNE);
/ U1 m( Q& I/ ], @4 Mwhile (1)
1 R$ ^7 k, T4 A7 |{
/ X; y0 t$ l8 |SPI5_Master_Rx(); //读取从机发过来的数据。
9 n: R* s! z8 b}! J( q; c+ W# i4 h }4 e
} typedef uint8_t u8 ;5 j6 E0 e8 w9 p: F! Z/ G
typedef uint16_t u16 ;. ^$ Y4 E( \) x# ? K" f
typedef uint32_t u32 ;( r" _# ^; R, x, h+ @ l. a' d
u8 hmosi[50]={0};$ h& F' J) h6 H" D. N
u8 mosi1[50]={0};: z o- |) ^( {! X U1 [
u8 phmosi[50]={0};2 @+ Q- q; L& K+ U! p8 { N9 Z
u32 imosi=0;
' u/ X$ S% J6 F0 X, K9 _& f: Yu32 spi5_rx_len;" l9 X, A( v8 z( ?
u32 ic=0;
6 W# b( F6 p" p$ [# G#define comsize 8
* A3 b; a) w* X/ m1 E, x
5 k9 H S. {" [extern u16 tSession;. D1 r1 @+ i& K4 \" v5 V% _- E+ g
u32 udata_len_spi;, z7 H! w2 X: k" T
extern u16 ports[comsize+1];: s. V- L2 T5 }, L' m. i$ W
extern u32 uTimeLst;7 }& i4 @) t! r$ w
$ k# {' i" @- `5 Nextern SPI_HandleTypeDef hspi5;
/ g" _1 k- L- ]! i& s/ avoid SPI5_IRQHandler(void)% B$ o0 B+ C3 B; M7 |$ P& I i- u/ u
{& T( S4 M w1 m2 V( s. {$ {2 U; ]
if(__HAL_SPI_GET_FLAG(&hspi5, SPI_FLAG_RXNE) == SET)- [8 I; [* e3 i3 z) m
{
2 d+ C4 V' _1 g) Q8 ahmosi[imosi++]=(uint8_t)(hspi5.Instance->DR); } } void SPI5_Master_Rx(void)
. S6 h+ w3 m- q# ?- |{8 t# s1 a4 r- ?7 B& h
//for(;imosi>=(ic+7);ic++). }" ~% \7 t9 Z
for(;imosi>=(ic+5);ic++)
) }" O0 Q2 x' l. [- A{
: L! ]: W O1 Z \. R( R# @if((0x2e==hmosi[ic])&&(0x17==hmosi[ic+1])&&(0x0==hmosi[ic+2]))
$ Q+ p- L( R9 q5 m- G{
. u# e( Y: j# w7 Ospi5_rx_len=hmosi[ic+6];2 p1 r# g( w/ u8 Z* }! D0 t
udata_len_spi = spi5_rx_len+1;
; f5 V* F8 d1 ^; c9 Wmemset(phmosi,0x00,udata_len_spi);
/ Y+ x3 {& C) z: fphmosi[5]=hmosi[ic+5];
( v0 t8 D/ [: M% t, ?% x1 l- T+ [# ]% x/ q& o, w, X
phmosi[6]=spi5_rx_len-7;
* t6 Q' q$ k( l# N7 s, T, BmEMCpy(phmosi+7,hmosi+ic+7,spi5_rx_len-7);
% F ^& ` Z; b6 o0 Ememcpy(mosi1,hmosi+ic,50-ic);
! P2 W s. p% N3 ]1 P, O4 Wmemcpy(hmosi,mosi1,50-ic);/ s* m$ A* ^2 S+ l3 c+ w# `$ q. X9 R
imosi=imosi-ic;+ Q( o t1 ?. N# s7 U9 ]- F
ic=0;6 l3 S1 J* P2 q8 H) N8 P
//**************************- J: G o( W( T: _
tSession++;" |& f" d2 Z& ~/ }1 w1 _
phmosi[0]=0x2e;
1 B, Y/ Q' V9 x2 z5 g4 s# D8 aphmosi[1]=0x17;9 i3 F% W2 d' n- N: i
phmosi[2]=0x00;" v" K% C2 V( m9 R
3 d4 [- D7 P: O' m" k- P
phmosi[3]=tSession;
$ `& M$ Y! h% b4 }) \9 nphmosi[4]=(tSession>>8);6 b3 r1 y$ ~, ~' _( h/ [7 Q6 H* ?
) K( x" `+ h7 q9 v: N9 L4 E j
//memcpy(udata+7,tx1_buf,rx1cnt);4 I8 S) C$ W, k3 V& q# X
phmosi[udata_len_spi-1] = crc(phmosi,udata_len_spi-1,1);, v& _) p' Z- H. W0 b$ G
com2net(ports[0],udata_len_spi,phmosi);% L; H2 \) g- A. q' D h
uTimeLst=HAL_GetTick();
7 ]% r; |0 j# P, J# ^# h}
5 D% ?7 }" v' V. v% b% A2 L}5 E7 z* n7 Q2 b! [; }8 O
}
+ m3 |! r* j0 U) c, m9 Q6 V. H |