spi.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. ******************************************************************************
  3. * @file spi.c
  4. * $Author: 飞鸿踏雪 $
  5. * $Revision: 17 $
  6. * $Date:: 2014-10-25 11:16:48 +0800 #$
  7. * @brief SPI驱动函数实现.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. *<h3><center>&copy; Copyright 2009-2012, EmbedNet</center>
  12. *<center><a href="http:\\www.embed-net.com">http://www.embed-net.com</a></center>
  13. *<center>All Rights Reserved</center></h3>
  14. *
  15. ******************************************************************************
  16. */
  17. /* Includes ------------------------------------------------------------------*/
  18. #include "stm32f10x_conf.h"
  19. /* Private typedef -----------------------------------------------------------*/
  20. /* Private define ------------------------------------------------------------*/
  21. /* Private macro -------------------------------------------------------------*/
  22. /* Private variables ---------------------------------------------------------*/
  23. /* Private function prototypes -----------------------------------------------*/
  24. /* Private functions ---------------------------------------------------------*/
  25. /**
  26. * @brief 使能SPI时钟
  27. * @retval None
  28. */
  29. static void SPI_RCC_Configuration(void)
  30. {
  31. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  32. RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
  33. }
  34. /**
  35. * @brief 配置指定SPI的引脚
  36. * @retval None
  37. */
  38. static void SPI_GPIO_Configuration(void)
  39. {
  40. GPIO_InitTypeDef GPIO_InitStruct;
  41. //PB12->CS,PB13->SCK,PB14->MISO,PB15->MOSI
  42. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14|GPIO_Pin_15;
  43. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  44. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
  45. GPIO_Init(GPIOB, &GPIO_InitStruct);
  46. //初始化片选输出引脚
  47. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12;
  48. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
  49. GPIO_Init(GPIOB, &GPIO_InitStruct);
  50. GPIO_SetBits(GPIOB,GPIO_Pin_12);
  51. }
  52. /**
  53. * @brief 根据外部SPI设备配置SPI相关参数
  54. * @retval None
  55. */
  56. void SPI_Configuration(void)
  57. {
  58. SPI_InitTypeDef SPI_InitStruct;
  59. SPI_RCC_Configuration();
  60. SPI_GPIO_Configuration();
  61. SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
  62. SPI_InitStruct.SPI_Direction= SPI_Direction_2Lines_FullDuplex;
  63. SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
  64. SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
  65. SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
  66. SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
  67. SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;
  68. SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
  69. SPI_InitStruct.SPI_CRCPolynomial = 7;
  70. SPI_Init(SPI2,&SPI_InitStruct);
  71. SPI_SSOutputCmd(SPI2, ENABLE);
  72. SPI_Cmd(SPI2, ENABLE);
  73. }
  74. /**
  75. * @brief 写1字节数据到SPI总线
  76. * @param TxData 写到总线的数据
  77. * @retval None
  78. */
  79. void SPI_WriteByte(uint8_t TxData)
  80. {
  81. while((SPI2->SR&SPI_I2S_FLAG_TXE)==0); //等待发送区空
  82. SPI2->DR=TxData; //发送一个byte
  83. while((SPI2->SR&SPI_I2S_FLAG_RXNE)==0); //等待接收完一个byte
  84. SPI2->DR;
  85. }
  86. /**
  87. * @brief 从SPI总线读取1字节数据
  88. * @retval 读到的数据
  89. */
  90. uint8_t SPI_ReadByte(void)
  91. {
  92. while((SPI2->SR&SPI_I2S_FLAG_TXE)==0); //等待发送区空
  93. SPI2->DR=0xFF; //发送一个空数据产生输入数据的时钟
  94. while((SPI2->SR&SPI_I2S_FLAG_RXNE)==0); //等待接收完一个byte
  95. return SPI2->DR;
  96. }
  97. /**
  98. * @brief 进入临界区
  99. * @retval None
  100. */
  101. void SPI_CrisEnter(void)
  102. {
  103. __set_PRIMASK(1);
  104. }
  105. /**
  106. * @brief 退出临界区
  107. * @retval None
  108. */
  109. void SPI_CrisExit(void)
  110. {
  111. __set_PRIMASK(0);
  112. }
  113. /**
  114. * @brief 片选信号输出低电平
  115. * @retval None
  116. */
  117. void SPI_CS_Select(void)
  118. {
  119. GPIO_ResetBits(GPIOB,GPIO_Pin_12);
  120. }
  121. /**
  122. * @brief 片选信号输出高电平
  123. * @retval None
  124. */
  125. void SPI_CS_Deselect(void)
  126. {
  127. GPIO_SetBits(GPIOB,GPIO_Pin_12);
  128. }
  129. /*********************************END OF FILE**********************************/