这次调试SPI, 在网上查了不少资料, 特别是论坛上的, 现在硬件3线SPI已调通, 跟大家分享下.8 a. j+ O8 c. D6 ?5 v+ `$ e- ~" X3 H 由于外设是3线spi,MCU不得不也跟着使用3线SPI. MCU: STM8L051F36 P& s& Y; }/ D2 d& H 外设: A7139' A2 I0 u& g0 D M5 }; F) @! z A 操作寄存器方式: 官方库函数7 ^$ z. i8 o3 | `$ j SPI: 3线 + u: r! r$ L7 K1 ~, f2 j6 g - b! v$ u# F% A2 ?- i) H+ W% p7 G 主要步骤:7 e7 S' Z4 w) W4 d4 }- j/ p, ] 1 初始化
/**; j8 i) {0 O' M * @brief Initializes the SPI and CS pins. * @param None& \! R* ^: P* J ?- z$ ^: H * @retval None6 U+ G; [7 Y! a8 n. \ */ void SPI_LowLevel_Init(void) { /* Enable SPI clock */ CLK_PeripheralClockConfig(SPI_CLK, ENABLE);4 d% x' a$ p* J$ M /* Set the MOSI,MISO and SCK at high level */( d `$ |+ H5 S3 v( O GPIO_ExternalPullUpConfig(SPI_SCK_GPIO_PORT, SPI_MISO_PIN | SPI_MOSI_PIN | \) r: w2 {8 \! P5 x( Y4 r8 a' Y SPI_SCK_PIN, ENABLE);+ j; Y6 r' y8 v- H/ D/ { /* SPI Config */ SPI_Init(SPI, SPI_FirstBit_MSB, SPI_BaudRatePrescaler_128, SPI_Mode_Master,- u, [1 R4 Q# `9 \7 S( _ SPI_CPOL_Low, SPI_CPHA_1Edge, SPI_Direction_1Line_Tx /*SPI_Direction_2Lines_FullDuplex*/, SPI_NSS_Soft, 0x07); /* SPI enable */ SPI_Cmd(SPI, ENABLE); " c' ~1 |7 V0 t. | /* Set MSD ChipSelect pin in Output push-pull high level */- o1 ]5 e1 ]7 v& Q3 l) e0 N GPIO_Init(CS_GPIO_PORT, CS_PIN, GPIO_Mode_Out_PP_High_Slow);8 S: q0 m( h4 w } 2 读写函数4 v6 p. O* j( z
/** * @brief Write a byte .% s, j; ]! A) Y* T# g * @param Data: byte to send. * @retval None0 e" H }9 b- j$ U- g( Q1 d5 N */ uint8_t SPIx_WriteByte(uint8_t Data)9 B7 Z3 |# o# a8 b; c( u6 T {2 Q' f! g/ S3 M //SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Tx); //设置单线为发送2 @) Q* W( [( r' `. m 4 ^5 A- S: ?. c /*!< Wait until SPI is not busy */ while (SPI_GetFlagStatus(SPI, SPI_FLAG_BSY) != RESET) {}% c9 p9 i+ l4 r+ I. s& O3 Q 7 w' F" [1 S) B /*!< Send the byte */ SPI_SendData(SPI, Data); ; Q- z" C) K# x( d% u+ r- j; N /*!< Wait until SPI is not busy */ while (SPI_GetFlagStatus(SPI, SPI_FLAG_BSY) != RESET), y7 j& x2 ?( ]. X4 | {}# G8 k/ W7 l. K+ m+ |6 f, B% k: r - V( D; q P) c% I- s$ p' V8 [ //SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Rx); //设置单线为接收8 `9 o5 @, O7 U% {' w. _, i return 1;! K7 \( [) d6 l X: e. P% K } /** * @brief Read a byte .8 l7 b. s6 S6 _0 Y' z. ] * @param None * @retval The received byte. */ Y/ T8 t. x* }% R1 U7 T, b uint8_t SPIx_ReadByte(void), ^5 O) {# W0 B( [ { uint8_t Data = 0;- c$ `" ]% f1 n9 ^ //SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Rx); //设置单线为接收2 l" V' D% S; P1 ] //SPI_Cmd(SPI, ENABLE);# n, N9 i! V |2 X0 h " E9 P. B+ j) x& u" V+ H6 u /* Wait untils SPI is working */1 w5 H* ^' H$ t) V while (SPI_GetFlagStatus(SPI, SPI_FLAG_BSY) == SET); /*!< Wait until a data is received */* X: u* `* q! Z while (SPI_GetFlagStatus(SPI, SPI_FLAG_RXNE) == RESET)/ ^' Z* E" f% S9 E: K5 H5 F {}0 @* P. W; M. T' m9 N /*!< Get the received data */ Data = SPI_ReceiveData(SPI);8 e C. _( d/ j5 H/ R" @3 Y" i - j5 i6 `( d# \+ J' K* K //SPI_Cmd(SPI, DISABLE); //SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Tx);* Z) O K# x9 q( c /*!< Return the shifted data */" l- h+ q3 q: C5 ^% k7 r' T* H* ? return Data; } void A7139_SetCID(Uint32 id), W" ?2 c. V5 w; e1 N: G { SPI_CS_LOW(); //SCS=0;+ r2 f1 u; F c$ {) } SPIx_WriteByte(CMD_CID_W);& y8 v4 l- q( X( v* s7 E- ^2 ~ SPIx_WriteByte((Uint8)(id>>24));9 [( X* W+ g" a SPIx_WriteByte((Uint8)(id>>16));4 X- Y. \# W9 } SPIx_WriteByte((Uint8)(id>>8)); SPIx_WriteByte((Uint8)id);4 N. e Z# f* H SPI_CS_HIGH(); //SCS=1; }" s- J4 ~2 S9 _$ _1 Q Uint32 A7139_ReadCID(void). d5 W4 G( u$ U5 [ { Uint32 data = 0; Uint8 i = 0; Uint32 byte[4] = {0};2 f6 M9 ?6 S5 b$ z : i2 s, {$ m* G9 G. c SPI_CS_LOW(); SPIx_WriteByte(CMD_CID_R); SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Rx); //设置单线为接收6 v. B- ^4 j; H* s: ?1 u" h) a // for (i = 0; i < 4; i++) // {( Y. g: K$ o5 X // data = data << 8;6 k' M* l; Z o+ {/ ^ // data |= SPIx_ReadByte(); // } byte[0] = SPIx_ReadByte();8 k+ O- D; r; }+ Q2 x* J7 G; B byte[1] = SPIx_ReadByte(); byte[2] = SPIx_ReadByte();# }" C) q+ @! c6 Y* k% z; O& ? byte[3] = SPIx_ReadByte(); data = (byte[0] << 24) | (byte[1] << 16) | (byte[2] << 8) | byte[3];: J# i, u2 G/ V& G. K0 `" P SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Tx);/ Y U2 D- y9 e6 e/ j& m9 E SPI_CS_HIGH(); ) Y' s- p. H2 b return data;3 f5 Z9 w0 Q) @ } * Q: o/ J* j& X8 M7 O; D+ J6 B9 k* F, f |
https://www.stmcu.org.cn/module/forum/thread-612887-1-1.html