SPI.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "main.h"
  2. static void SPI1_Init(void);
  3. static void SPI2_Init(void);
  4. static uint8_t SPI_WriteRead(SPI_TypeDef* SPIx, uint8_t data, uint32_t timeout);
  5. static void SPI_CS_WritePin(SPI_TypeDef* SPIx, BitAction bitVal);
  6. SPIClassStruct SPIClass = {
  7. .SPI1_Init = SPI1_Init,
  8. .SPI2_Init = SPI2_Init,
  9. .WriteRead = SPI_WriteRead,
  10. .WriteCSPin = SPI_CS_WritePin
  11. };
  12. // SPI1 CS
  13. #define SPI1_CS_GPIO_PORT GPIOB
  14. #define SPI1_CS_GPIO_CLK RCC_APB2Periph_GPIOB
  15. #define SPI1_CS_GPIO_PIN GPIO_Pin_0
  16. // SPI2 CS
  17. #define SPI2_CS_GPIO_PORT GPIOA
  18. #define SPI2_CS_GPIO_CLK RCC_APB2Periph_GPIOA
  19. #define SPI2_CS_GPIO_PIN GPIO_Pin_0
  20. /**
  21. * @brief SPI1初始化
  22. * @param None
  23. * @retval None
  24. */
  25. static void SPI1_Init(void)
  26. {
  27. GPIO_InitTypeDef GPIO_InitStructure;
  28. SPI_InitTypeDef SPI_InitStructure;
  29. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  30. // SPI1 SCK-PA5 MOSI-PA7
  31. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7;
  32. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  33. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  34. GPIO_Init(GPIOA, &GPIO_InitStructure);
  35. // SPI1 MISO-PA6
  36. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  37. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  38. GPIO_Init(GPIOA, &GPIO_InitStructure);
  39. // software CS user write
  40. RCC_APB2PeriphClockCmd(SPI1_CS_GPIO_CLK, ENABLE);
  41. GPIO_InitStructure.GPIO_Pin = SPI1_CS_GPIO_PIN;
  42. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  43. GPIO_Init(SPI1_CS_GPIO_PORT, &GPIO_InitStructure);
  44. GPIO_WriteBit(SPI1_CS_GPIO_PORT, SPI1_CS_GPIO_PIN, Bit_SET); // 拉高片选引脚
  45. RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); // SPI1时钟使能
  46. SPI_Cmd(SPI1, DISABLE);
  47. SPI_I2S_DeInit(SPI1);
  48. SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // SPI设置为双线全双工
  49. SPI_InitStructure.SPI_Mode = SPI_Mode_Master; // 设置SPI为主模式
  50. SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; // SPI发送接收8位帧结构
  51. SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; // SPI时钟空闲时为低电平
  52. SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; // 第一个时钟沿开始采样数据
  53. SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; // CS信号由软件管理
  54. SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; // SPI波特率预分频值 72M / Prescaler
  55. SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; // 数据传输从MSB位开始
  56. SPI_InitStructure.SPI_CRCPolynomial = 7; // CRC值计算的多项式
  57. SPI_Init(SPI1, &SPI_InitStructure); // 根据SPI_InitStruct中指定的参数初始化外设SPI寄存器
  58. SPI_Cmd(SPI1, ENABLE); // 使能SPI
  59. }
  60. /**
  61. * @brief SPI2初始化
  62. * @param None
  63. * @retval None
  64. */
  65. static void SPI2_Init(void)
  66. {
  67. GPIO_InitTypeDef GPIO_InitStructure;
  68. SPI_InitTypeDef SPI_InitStructure;
  69. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  70. // SPI1 SCK-PB13 MOSI-PB15
  71. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15;
  72. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  73. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  74. GPIO_Init(GPIOB, &GPIO_InitStructure);
  75. // SPI1 MISO-PB14
  76. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
  77. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  78. GPIO_Init(GPIOB, &GPIO_InitStructure);
  79. // software CS user write
  80. RCC_APB2PeriphClockCmd(SPI2_CS_GPIO_CLK, ENABLE);
  81. GPIO_InitStructure.GPIO_Pin = SPI2_CS_GPIO_PIN;
  82. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  83. GPIO_Init(SPI2_CS_GPIO_PORT, &GPIO_InitStructure);
  84. GPIO_WriteBit(SPI2_CS_GPIO_PORT, SPI2_CS_GPIO_PIN, Bit_SET); // 拉高片选引脚
  85. RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); // SPI2时钟使能
  86. SPI_Cmd(SPI2, DISABLE);
  87. SPI_I2S_DeInit(SPI2);
  88. SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // SPI设置为双线全双工
  89. SPI_InitStructure.SPI_Mode = SPI_Mode_Master; // 设置SPI为主模式
  90. SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; // SPI发送接收8位帧结构
  91. SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; // SPI时钟空闲时为低电平
  92. SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; // 第一个时钟沿开始采样数据
  93. SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; // CS信号由软件管理
  94. SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; // SPI波特率预分频值 36M / Prescaler
  95. SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; // 数据传输从MSB位开始
  96. SPI_InitStructure.SPI_CRCPolynomial = 7; // CRC值计算的多项式
  97. SPI_Init(SPI2, &SPI_InitStructure); // 根据SPI_InitStruct中指定的参数初始化外设SPI寄存器
  98. SPI_Cmd(SPI2, ENABLE); // 使能SPI
  99. }
  100. /**
  101. * @brief SPI读写函数
  102. * @param None
  103. * @retval 0-超时
  104. */
  105. static uint8_t SPI_WriteRead(SPI_TypeDef* SPIx, uint8_t data, uint32_t timeout)
  106. {
  107. uint32_t retry = 0;
  108. /* Loop while DR register in not emplty */
  109. while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET) // 发送缓存标志位为空
  110. {
  111. retry++;
  112. if(retry > timeout) return 0;
  113. }
  114. SPI_I2S_SendData(SPIx, data); // 通过外设SPI2发送一个数据
  115. retry = 0;
  116. /* Wait to receive a byte */
  117. while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET) // 接收缓存标志位不为空
  118. {
  119. retry++;
  120. if(retry > timeout) return 0;
  121. }
  122. /* Return the byte read from the SPI bus */
  123. return SPI_I2S_ReceiveData(SPIx); // 通过SPI2返回接收数据
  124. }
  125. /**
  126. * @brief SPI读写函数
  127. * @param None
  128. * @retval 0-成功
  129. */
  130. static void SPI_CS_WritePin(SPI_TypeDef* SPIx, BitAction bitVal)
  131. {
  132. if(SPIx == SPI1) {
  133. GPIO_WriteBit(SPI1_CS_GPIO_PORT, SPI1_CS_GPIO_PIN, bitVal);
  134. } else if(SPIx == SPI2) {
  135. GPIO_WriteBit(SPI2_CS_GPIO_PORT, SPI2_CS_GPIO_PIN, bitVal);
  136. }
  137. }