STM32-SPI Documentation Compilation

STM32-SPI Documentation Compilation

Click the blue text

Follow us

1. Introduction to SPI

1. SPI Physical Layer

SPI generally uses 4 lines for communication:

  • NSS: Chip select line

  • MOSI: Master data output, slave data input

  • MISO: Master data input, slave data output

  • SCK: Clock line, provided by the master

STM32-SPI Documentation Compilation

2. Protocol Layer

(1) Basic SPI Communication Timing

STM32-SPI Documentation Compilation

(2) Start and Stop Signals

  • Start signal: NSS goes from high to low

  • Stop signal: NSS goes from low to high

(3) Data Validity

  • SPI uses the SCK line for data synchronization, with MOSI and MISO transmitting one bit of data during each clock cycle of SCK, and the input and output of data occurring synchronously

  • During data transmission, it is generally adopted that the MSB is sent first (high byte first, then low byte)

  • Each data transmission in SPI can be in units of 8 bits or 16 bits, with no limit on the number of units transmitted each time

(4) Four Communication Modes of SPI (CPOL – Clock Polarity and CPHA – Clock Phase)

  • Clock Polarity CPOL: Indicates the level of SCK when idle. (e.g., CPOL = 0 —- SCK idle state is low; CPOL = 1 —- SCK idle state is high)

  • Clock Phase CPHA: Indicates when data is sampled. (e.g., CPHA = 0 —- data is sampled on the odd edge of SCK; CPHA = 1 —- data is sampled on the even edge of SCK)

Four modes of SPI:

STM32-SPI Documentation Compilation

When CPHA = 0 (SPI timing diagram with sampling edge on odd edge)

STM32-SPI Documentation Compilation

When CPHA = 1 (SPI timing diagram with sampling edge on even edge)

STM32-SPI Documentation Compilation

2. Software SPI Example

1. Software SPI Structure and SPI Mode Enumeration

STM32-SPI Documentation Compilation

2. SPI Initialization Function

  1. void vSoftSPIInit(exSPIModeEnum eMode)

  2. {

  3. GPIO_InitTypeDef GPIO_InitStruct = {0};

  4. /* Pin initialization */

  5. SOFT_SPI_CLK_ENABLE();

  6. /**SPI1 GPIO Configuration

  7. PB14 ——> SPI1_NSS

  8. PB3 ——> SPI1_SCK

  9. PB4 ——> SPI1_MISO

  10. PB5 ——> SPI1_MOSI

  11. */

  12. GPIO_InitStruct.Pin = SOFT_SPI_NSS_PIN;

  13. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

  14. GPIO_InitStruct.Pull = GPIO_NOPULL;

  15. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

  16. HAL_GPIO_Init(SOFT_SPI_NSS_PORT, &GPIO_InitStruct);

  17. GPIO_InitStruct.Pin = SOFT_SPI_SCK_PIN;

  18. HAL_GPIO_Init(SOFT_SPI_SCK_PORT, &GPIO_InitStruct);

  19. GPIO_InitStruct.Pin = SOFT_SPI_MOSI_PIN;

  20. HAL_GPIO_Init(SOFT_SPI_MOSI_PORT, &GPIO_InitStruct);

  21. GPIO_InitStruct.Pin = SOFT_SPI_MISO_PIN;

  22. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

  23. HAL_GPIO_Init(SOFT_SPI_MISO_PORT, &GPIO_InitStruct);

  24. /* Initialize Soft SPI handle */

  25. ex_SoftSPIHandle.NSS_Port = ex_SoftSPIHandle.SCK_Port = ex_SoftSPIHandle.MISO_Port = ex_SoftSPIHandle.MOSI_Port = GPIOB;

  26. ex_SoftSPIHandle.NSS_Pin = SOFT_SPI_NSS_PIN;

  27. ex_SoftSPIHandle.SCK_Pin = SOFT_SPI_SCK_PIN;

  28. ex_SoftSPIHandle.MOSI_Pin = SOFT_SPI_MOSI_PIN;

  29. ex_SoftSPIHandle.MISO_Pin = SOFT_SPI_MISO_PIN;

  30. /* Register Soft SPI functions */

  31. ex_SoftSPIHandle.SetHigh = vSetHigh;

  32. ex_SoftSPIHandle.SetLow = vSetLow;

  33. ex_SoftSPIHandle.SetMode = vSetMode;

  34. ex_SoftSPIHandle.ReadPin = HAL_GPIO_ReadPin;

  35. ex_SoftSPIHandle.SoftSPIWriteData = vSoftSPIWriteData;

  36. ex_SoftSPIHandle.SoftSPIReadData = vSoftSPIReadData;

  37. /* Set corresponding SPI mode */

  38. ex_SoftSPIHandle.mode = eMode;

  39. if(eMode & MODE_0)

  40. {

  41. /* CPOL = 0 ; CPHA = 0 */

  42. /* SPI default mode 0 is idle */

  43. SOFT_SPI_NSS_High();

  44. SOFT_SPI_SCK_Low();

  45. }

  46. else if(eMode & MODE_1)

  47. {

  48. /* CPOL = 0 ; CPHA = 1 */

  49. /* SPI default mode 1 is idle */

  50. SOFT_SPI_NSS_High();

  51. SOFT_SPI_SCK_Low();

  52. }

  53. else if(eMode & MODE_2)

  54. {

  55. /* CPOL = 1 ; CPHA = 0 */

  56. /* SPI default mode 2 is idle */

  57. SOFT_SPI_NSS_High();

  58. SOFT_SPI_SCK_High();

  59. }

  60. else

  61. {

  62. /* CPOL = 1 ; CPHA = 1 */

  63. /* SPI default mode 3 is idle */

  64. SOFT_SPI_NSS_High();

  65. SOFT_SPI_SCK_High();

  66. }

  67. }

3. SPI Read/Write Functions

STM32-SPI Documentation CompilationSTM32-SPI Documentation CompilationSTM32-SPI Documentation CompilationSTM32-SPI Documentation Compilation

3. STM32-SPI Peripheral Usage Example (HAL Library)

STM32-SPI Documentation CompilationSTM32-SPI Documentation CompilationSTM32-SPI Documentation CompilationSTM32-SPI Documentation Compilation

STM32-SPI Documentation Compilation

*Disclaimer: This article is original or forwarded by the author. If it inadvertently infringes on someone’s intellectual property, please inform us for deletion.The above images and text are sourced from the internet. If there is any infringement, please contact us in a timely manner, and we will delete it within 24 hours.The content of the article represents the author’s personal views, and the Automotive Ethernet Technology Research Laboratory reprints it only to convey a different perspective, which does not represent the Automotive Ethernet Technology Research Laboratory’s endorsement or support of this view. If there are any objections, please feel free to contact the Automotive Ethernet Technology Research Laboratory.

Original link:

https://blog.csdn.net/laifengyuan1/article/details/110823739

Leave a Comment