这次调试SPI, 在网上查了不少资料, 特别是论坛上的, 现在硬件3线SPI已调通, 跟大家分享下. 由于外设是3线spi,MCU不得不也跟着使用3线SPI.0 P% j* \3 W7 y( d; o: T0 w8 O 7 Y# I6 e# s$ u f0 ~ MCU: STM8L051F3+ q: P! q6 l- z( d& O3 Z 外设: A7139 操作寄存器方式: 官方库函数: t4 ^8 F9 w' |6 S5 s SPI: 3线 , l* M! a2 F' s& p+ z( w% x& V1 O) p 2 x. s) c5 f! P$ h" {$ C) C 主要步骤:, s# D) @! S# k' B& t1 c3 M; ^ 1 初始化, u6 h* d3 C9 l+ a
/**& ?7 [ [2 h, C" v$ B * @brief Initializes the SPI and CS pins.% C- r8 Z" n( s2 t: w * @param None/ n9 u' \1 o# }, C# o * @retval None# {" C5 _! R6 B5 O0 d6 F. u */. F2 m7 d p6 l1 p1 b. Y4 ]% F void SPI_LowLevel_Init(void)# n$ O4 w5 ^4 c { /* Enable SPI clock */. u# }+ j0 n7 u7 }- u CLK_PeripheralClockConfig(SPI_CLK, ENABLE); /* Set the MOSI,MISO and SCK at high level */. z+ s$ ]; ~& [$ h9 f4 | GPIO_ExternalPullUpConfig(SPI_SCK_GPIO_PORT, SPI_MISO_PIN | SPI_MOSI_PIN | \( z$ ?& c; V/ r SPI_SCK_PIN, ENABLE); /* SPI Config */- z9 l% s; K* _( o SPI_Init(SPI, SPI_FirstBit_MSB, SPI_BaudRatePrescaler_128, SPI_Mode_Master,9 Z2 g7 X3 ?& g8 _ SPI_CPOL_Low, SPI_CPHA_1Edge, SPI_Direction_1Line_Tx /*SPI_Direction_2Lines_FullDuplex*/,+ {4 N9 s- c1 B! @ SPI_NSS_Soft, 0x07); ( u/ a) |, h5 a% r9 \+ o /* SPI enable */7 F& s7 u$ y* x1 H, r9 O x SPI_Cmd(SPI, ENABLE); , Z" J) l) j% g+ _; R. ?- b5 M /* Set MSD ChipSelect pin in Output push-pull high level */9 @ u: h: O6 ]. n# a4 k" F+ ~1 q GPIO_Init(CS_GPIO_PORT, CS_PIN, GPIO_Mode_Out_PP_High_Slow);' [! X; j) a5 D3 e( z& \ }$ [% I* P/ w9 ^2 S+ g7 X 7 J0 T/ o* M9 F3 `* W( X 2 读写函数8 u* L* \$ f( a* k3 U* S) o
/**( f7 N4 n. B) M. |- X3 F * @brief Write a byte . * @param Data: byte to send.- U, ?; O! [$ D7 o7 b * @retval None */" ?4 @, S* z M0 |6 R# x( ` uint8_t SPIx_WriteByte(uint8_t Data) {+ W7 k1 N5 s$ F4 L% B- [& o" A //SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Tx); //设置单线为发送 /*!< Wait until SPI is not busy */ 7 n& d9 F1 b6 u while (SPI_GetFlagStatus(SPI, SPI_FLAG_BSY) != RESET) {} , Q" ]9 |7 g2 l0 t8 t2 ] Z% I( P /*!< Send the byte */ SPI_SendData(SPI, Data); O ?: v5 z7 {+ g$ v! } \ /*!< Wait until SPI is not busy */, Z' }- @& Q3 k: }& y- q while (SPI_GetFlagStatus(SPI, SPI_FLAG_BSY) != RESET)! q) e& ?( o8 h3 Q {}0 d0 }: ~, v) @ . j! n3 H+ G' i' B //SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Rx); //设置单线为接收3 o" f K) V+ @* u( X% D return 1;( J9 _. R3 d1 _- f3 G }% d" V& R" T s/ G# l 8 z4 a; k& \. f- W+ D /**3 w. x! ^1 N8 N( Z1 Z5 ^5 ~ * @brief Read a byte . * @param None * @retval The received byte.' ~' j: A( h, z/ N# O4 V */ uint8_t SPIx_ReadByte(void)4 u! O8 ?2 F8 G2 c+ Y4 E2 x1 u {# ]8 J! K: a) D uint8_t Data = 0; //SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Rx); //设置单线为接收/ }9 v# Y) {; ` }4 f- j( \ ] //SPI_Cmd(SPI, ENABLE);# Y6 K0 I3 z; n' l/ S" [1 T /* Wait untils SPI is working */ while (SPI_GetFlagStatus(SPI, SPI_FLAG_BSY) == SET);$ P- F) o! {8 |/ I, O1 }0 e4 P /*!< Wait until a data is received */ while (SPI_GetFlagStatus(SPI, SPI_FLAG_RXNE) == RESET)9 P3 H/ B, X, r {} /*!< Get the received data *// b* V @7 G4 c Data = SPI_ReceiveData(SPI);3 i$ B4 x- v4 v* i& ? //SPI_Cmd(SPI, DISABLE); //SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Tx);1 J+ _' x) u5 b* B( g /*!< Return the shifted data */! G) a8 L# W5 N2 ? return Data;! t& O7 G! _/ E0 H6 ]8 O' b }: N4 Y/ H" D- W 8 b1 G r* P! r, X8 |" G* { void A7139_SetCID(Uint32 id) { SPI_CS_LOW(); //SCS=0; SPIx_WriteByte(CMD_CID_W);+ G" P1 ^* n+ t% r SPIx_WriteByte((Uint8)(id>>24));1 r. z8 w% ?7 V# ^5 L% D5 J. m7 C SPIx_WriteByte((Uint8)(id>>16));! g( \ J( r. M& X SPIx_WriteByte((Uint8)(id>>8)); SPIx_WriteByte((Uint8)id); SPI_CS_HIGH(); //SCS=1;) U+ D" I) A4 h0 p( T' D' F" K% M2 Q } Uint32 A7139_ReadCID(void)6 h o, q0 o* ~ Q% C- a7 x! S& `8 E { Uint32 data = 0; Uint8 i = 0;! g$ R+ A- N, F$ |& ` Uint32 byte[4] = {0};7 Y- \, x/ a/ D0 l1 l8 V l, j7 Y SPI_CS_LOW();% l; ?8 b" e: p& ]0 j, Q$ L6 o SPIx_WriteByte(CMD_CID_R); SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Rx); //设置单线为接收5 V2 e5 P4 K9 G' s1 v3 ?7 @ // for (i = 0; i < 4; i++)5 k* L9 [5 H2 G1 [% G* m5 [) k$ I // {) n" ~% i+ f$ f" N1 ` // data = data << 8;* T/ d, f0 S6 ]; a/ c // data |= SPIx_ReadByte();* c$ |) h" E0 R+ j // } byte[0] = SPIx_ReadByte();+ L* z. V' X- o5 t7 S& g byte[1] = SPIx_ReadByte(); byte[2] = SPIx_ReadByte(); byte[3] = SPIx_ReadByte(); . ]* {. z' N# \3 r data = (byte[0] << 24) | (byte[1] << 16) | (byte[2] << 8) | byte[3];8 i' _ u) w( \9 Z# c( q' w( [ SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Tx);+ w0 u `# _1 M: _9 } SPI_CS_HIGH(); ) N5 O5 \5 @: F9 q1 E+ W3 t return data;/ j( R6 ^- l+ R r+ Q* h } * ]0 q& t$ W9 u9 w |
https://www.stmcu.org.cn/module/forum/thread-612887-1-1.html