这次调试SPI, 在网上查了不少资料, 特别是论坛上的, 现在硬件3线SPI已调通, 跟大家分享下.1 ?3 M" S3 O0 p" v 由于外设是3线spi,MCU不得不也跟着使用3线SPI. & X9 O# K6 |' t/ D. f1 |. U v9 U. ` MCU: STM8L051F3& a0 }( X! A2 c8 L2 _, q$ ` 外设: A7139 操作寄存器方式: 官方库函数. I+ G& z( _$ A9 k# B4 E SPI: 3线 # I9 J7 u, e2 `" m 主要步骤: 1 初始化' l% u3 b( q1 z/ y3 @8 \
/**( s9 C+ ]; r @' [ * @brief Initializes the SPI and CS pins." N9 B4 s K8 k" P' P * @param None * @retval None7 z* x Y$ Y0 s& t* k */ void SPI_LowLevel_Init(void). w% m8 b/ [- ^ z. b; R { /* Enable SPI clock */4 |# |" ?- k3 X. w% b8 O i& N% j* ` CLK_PeripheralClockConfig(SPI_CLK, ENABLE);1 k9 J! H- c, `" O w- }7 Y $ d) `, l( {9 C6 `( S: R/ B /* Set the MOSI,MISO and SCK at high level */ GPIO_ExternalPullUpConfig(SPI_SCK_GPIO_PORT, SPI_MISO_PIN | SPI_MOSI_PIN | \ SPI_SCK_PIN, ENABLE);8 m+ m- `, w6 l& \8 Y e /* SPI Config */ SPI_Init(SPI, SPI_FirstBit_MSB, SPI_BaudRatePrescaler_128, SPI_Mode_Master, SPI_CPOL_Low, SPI_CPHA_1Edge, SPI_Direction_1Line_Tx /*SPI_Direction_2Lines_FullDuplex*/,6 _+ D( f8 L7 q* _ SPI_NSS_Soft, 0x07); $ _8 s5 t9 z" H+ h; U3 E /* SPI enable */* A- v0 V8 `! [4 ^ SPI_Cmd(SPI, ENABLE); 6 w, t2 ]: m+ R; F% G /* Set MSD ChipSelect pin in Output push-pull high level */3 x T% c4 n0 ^: h GPIO_Init(CS_GPIO_PORT, CS_PIN, GPIO_Mode_Out_PP_High_Slow); } 7 @+ \( \4 V. }: p 2 读写函数5 ^ T1 G- c4 Q: E
/** * @brief Write a byte .; C; z8 j" C1 v$ K/ M * @param Data: byte to send.3 S4 B9 l5 \, R) ?! Q * @retval None */ uint8_t SPIx_WriteByte(uint8_t Data) {4 N2 a9 g" h/ F: @7 H0 U) A //SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Tx); //设置单线为发送 3 O' }" o( g7 q+ h7 P! J* Z /*!< Wait until SPI is not busy */ while (SPI_GetFlagStatus(SPI, SPI_FLAG_BSY) != RESET) {} /*!< Send the byte */ SPI_SendData(SPI, Data); /*!< Wait until SPI is not busy */2 n8 B4 k# p: f% d9 k while (SPI_GetFlagStatus(SPI, SPI_FLAG_BSY) != RESET)2 O) L+ H% {$ d0 Y* Y {} //SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Rx); //设置单线为接收$ V$ c# Y' ^. o- Z1 r2 a return 1;6 F5 o8 g& d$ ~8 o } 7 ^/ \. ~0 D: \5 z5 g# {3 s4 A; R+ S /**9 ?; z. L. q, h6 U4 C9 k) X' o * @brief Read a byte .% ]3 b8 H7 u" C9 g2 B * @param None' C1 V* W0 o, a * @retval The received byte.4 h a' }0 E( S$ ~4 u% j' P5 c2 j */ uint8_t SPIx_ReadByte(void) {4 j) R4 i. p8 L3 f4 N uint8_t Data = 0; //SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Rx); //设置单线为接收. h9 @/ H/ l0 K //SPI_Cmd(SPI, ENABLE); /* Wait untils SPI is working */ while (SPI_GetFlagStatus(SPI, SPI_FLAG_BSY) == SET);0 r! ]! u/ M- h7 A! ?+ z! s /*!< Wait until a data is received */& e" b' \0 B; Q while (SPI_GetFlagStatus(SPI, SPI_FLAG_RXNE) == RESET) {}3 z/ ^: X6 F/ r1 W /*!< Get the received data */ Data = SPI_ReceiveData(SPI);9 |4 W0 H+ b% ^6 C' U //SPI_Cmd(SPI, DISABLE);' O" R4 O2 ?+ D) r7 W4 I1 @ //SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Tx);1 ~+ _4 z3 u6 F2 ^) { /*!< Return the shifted data */* {; e( l- c& E! w& E8 f* d return Data; }6 v+ i. M0 C, n# o P9 g/ `8 u6 _' @ void A7139_SetCID(Uint32 id) { SPI_CS_LOW(); //SCS=0; SPIx_WriteByte(CMD_CID_W); SPIx_WriteByte((Uint8)(id>>24)); SPIx_WriteByte((Uint8)(id>>16)); SPIx_WriteByte((Uint8)(id>>8)); SPIx_WriteByte((Uint8)id); SPI_CS_HIGH(); //SCS=1;1 J3 F! Y# K* K" R. B: S }, n1 {0 Y/ ?0 f& j# X$ G$ m `$ c. r" Q/ ^+ o! U% P7 G Uint32 A7139_ReadCID(void) {$ X5 t4 V, W9 x6 y' | Uint32 data = 0;, Z$ D H( K: X/ | P6 d! t Uint8 i = 0; Uint32 byte[4] = {0};7 n* g% t2 {% O5 |2 t* X SPI_CS_LOW(); SPIx_WriteByte(CMD_CID_R); SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Rx); //设置单线为接收. s& F- x' V, ~* P0 \ // for (i = 0; i < 4; i++) // { // data = data << 8; // data |= SPIx_ReadByte();' O' W9 b5 z5 S G$ ^ // } byte[0] = SPIx_ReadByte(); byte[1] = SPIx_ReadByte();9 H4 E+ R N. [. p1 X byte[2] = SPIx_ReadByte(); byte[3] = SPIx_ReadByte(); data = (byte[0] << 24) | (byte[1] << 16) | (byte[2] << 8) | byte[3];0 W$ X% i- `3 T( F# }6 v' S/ f6 J+ O SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Tx); SPI_CS_HIGH(); return data;& N& r: T, e! _) a$ d }4 D/ d' H" Z5 Z& l9 b+ x I& n + y7 M) j! ?1 T |
https://www.stmcu.org.cn/module/forum/thread-612887-1-1.html