这次调试SPI, 在网上查了不少资料, 特别是论坛上的, 现在硬件3线SPI已调通, 跟大家分享下.( m: b# ]+ S7 @# P& f- `2 _ 由于外设是3线spi,MCU不得不也跟着使用3线SPI. - t. u/ U3 Z7 \ MCU: STM8L051F3 外设: A7139 操作寄存器方式: 官方库函数 SPI: 3线 4 a7 X. Z+ }- H0 q! } 6 W" u a4 U+ {8 ]6 @+ K% { 主要步骤: 1 初始化; U3 n4 h0 i2 r# t2 L
/** * @brief Initializes the SPI and CS pins. * @param None: ]. q2 W2 [: a$ z! d+ D * @retval None7 E) W1 T0 Y! S */ void SPI_LowLevel_Init(void) { /* Enable SPI clock */ CLK_PeripheralClockConfig(SPI_CLK, ENABLE); /* 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); /* SPI Config */ SPI_Init(SPI, SPI_FirstBit_MSB, SPI_BaudRatePrescaler_128, SPI_Mode_Master,1 c* {% P% Z2 s5 r" q8 x9 q SPI_CPOL_Low, SPI_CPHA_1Edge, SPI_Direction_1Line_Tx /*SPI_Direction_2Lines_FullDuplex*/, SPI_NSS_Soft, 0x07);! Y3 @8 m K! P5 G& x W /* SPI enable */6 l( U0 v9 |7 i) X' Z3 H5 ~* t SPI_Cmd(SPI, ENABLE);1 b- t: I. Y/ f7 L7 V' U3 ?3 x /* Set MSD ChipSelect pin in Output push-pull high level */+ v0 I# h5 x( a1 S o. S2 i: i/ V GPIO_Init(CS_GPIO_PORT, CS_PIN, GPIO_Mode_Out_PP_High_Slow); } 6 `1 U2 z0 l' W1 e 2 读写函数
/** * @brief Write a byte . * @param Data: byte to send.- n6 r' v5 w F, y0 E* | * @retval None3 O. b W6 |8 a N */ uint8_t SPIx_WriteByte(uint8_t Data) { ?: Y: I5 l3 ?" y, w //SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Tx); //设置单线为发送 /*!< Wait until SPI is not busy */ while (SPI_GetFlagStatus(SPI, SPI_FLAG_BSY) != RESET)( x8 S& J0 A: m% ?# a7 M I T {}0 z- x) F Q* x# u+ Q " } y! Q- Y+ {# j7 ^" K" i /*!< Send the byte */ SPI_SendData(SPI, Data);' S# A1 j6 H2 }& f. R" |9 _+ {$ G , Z) X: K# [% ] /*!< Wait until SPI is not busy */9 ?( Y d+ w8 ?/ \, t while (SPI_GetFlagStatus(SPI, SPI_FLAG_BSY) != RESET) {}; P+ \+ W5 ?( n$ r4 I/ _) ~+ b! E //SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Rx); //设置单线为接收 return 1; } /** * @brief Read a byte ./ R5 q' @% ~- O" x" V * @param None * @retval The received byte.& @1 S. k( w8 P+ `- n */+ `- H* h8 p8 e3 k# ` uint8_t SPIx_ReadByte(void) { uint8_t Data = 0; //SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Rx); //设置单线为接收 //SPI_Cmd(SPI, ENABLE);4 S- K% T* N* Q: [# ?( [! Y 3 g7 X& i9 m. U( z1 d; G /* Wait untils SPI is working */0 w9 d# F8 E: \' N6 U) U* c2 u6 O while (SPI_GetFlagStatus(SPI, SPI_FLAG_BSY) == SET); /*!< Wait until a data is received */ while (SPI_GetFlagStatus(SPI, SPI_FLAG_RXNE) == RESET)0 i- c# S1 m% b {}# M2 q0 v4 m1 r& T; H8 m3 P& F7 a! \ /*!< Get the received data */ Data = SPI_ReceiveData(SPI);! Q" M: w, b6 t //SPI_Cmd(SPI, DISABLE); //SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Tx);; s/ ]8 |" I" x s0 K /*!< Return the shifted data */7 }# S6 r8 S9 p# y! M return Data; }% `! {* I4 K+ E3 e l$ R void A7139_SetCID(Uint32 id) {, _$ j$ |0 x) @1 I5 o# } SPI_CS_LOW(); //SCS=0;* ]" M, j- W7 [% b" f0 I SPIx_WriteByte(CMD_CID_W); SPIx_WriteByte((Uint8)(id>>24)); SPIx_WriteByte((Uint8)(id>>16)); SPIx_WriteByte((Uint8)(id>>8)); SPIx_WriteByte((Uint8)id);( ?* i/ D4 f' l6 V( ~2 f: g SPI_CS_HIGH(); //SCS=1; }( c( C' |4 Q6 \4 P9 N# ]- c 6 \; u. o) q# D% T& K. l Uint32 A7139_ReadCID(void)" y6 C5 a! f& ]- V$ {5 }3 r) s {8 e4 \$ A o# g# v9 m Uint32 data = 0; Uint8 i = 0; Uint32 byte[4] = {0}; ; o+ L: ]1 {5 \ SPI_CS_LOW(); SPIx_WriteByte(CMD_CID_R);9 P3 ] b/ z, `& `* P0 v SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Rx); //设置单线为接收; G; M6 Z: r0 W9 F$ J // for (i = 0; i < 4; i++) // { // data = data << 8; // data |= SPIx_ReadByte();; o/ i' h% o" N9 j) ^) j4 f; H // } byte[0] = SPIx_ReadByte();" N* g: k+ `1 n1 e8 L* M byte[1] = SPIx_ReadByte(); byte[2] = SPIx_ReadByte();+ f4 |' D; N4 b# A+ E' N2 f3 M/ j; p byte[3] = SPIx_ReadByte();% x( Z5 i. W2 e7 |3 [ data = (byte[0] << 24) | (byte[1] << 16) | (byte[2] << 8) | byte[3]; SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Tx); SPI_CS_HIGH(); & G4 u3 \1 y2 b: \2 J( W / V4 u) {6 g+ |/ @+ N return data;8 ~) e, F* ^. N$ w S8 f! l } 6 z- O9 a8 l: _6 _! Z' R 9 e* a# F6 j/ N5 ? |
https://www.stmcu.org.cn/module/forum/thread-612887-1-1.html