spi.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. FlagStatus SPI_I2S_GetFlagStatus(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG)
  2. {
  3. FlagStatus bitstatus = RESET;
  4. /* Check the parameters */
  5. assert_param(IS_SPI_ALL_PERIPH(SPIx));
  6. assert_param(IS_SPI_I2S_GET_FLAG(SPI_I2S_FLAG));
  7. /* Check the status of the specified SPI/I2S flag */
  8. if ((SPIx->SR & SPI_I2S_FLAG) != (uint16_t)RESET)
  9. {
  10. /* SPI_I2S_FLAG is set */
  11. bitstatus = SET;
  12. }
  13. else
  14. {
  15. /* SPI_I2S_FLAG is reset */
  16. bitstatus = RESET;
  17. }
  18. /* Return the SPI_I2S_FLAG status */
  19. return bitstatus;
  20. }
  21. void SPI_I2S_SendData(SPI_TypeDef* SPIx, uint16_t Data)
  22. {
  23. /* Check the parameters */
  24. assert_param(IS_SPI_ALL_PERIPH(SPIx));
  25. /* Write in the DR register the data to be sent */
  26. SPIx->DR = Data;
  27. }
  28. uint16_t SPI_I2S_ReceiveData(SPI_TypeDef* SPIx)
  29. {
  30. /* Check the parameters */
  31. assert_param(IS_SPI_ALL_PERIPH(SPIx));
  32. /* Return the data in the DR register */
  33. return SPIx->DR;
  34. }
  35. void SPI_Init(SPI_TypeDef* SPIx, SPI_InitTypeDef* SPI_InitStruct)
  36. {
  37. uint16_t tmpreg = 0;
  38. /* check the parameters */
  39. assert_param(IS_SPI_ALL_PERIPH(SPIx));
  40. /* Check the SPI parameters */
  41. assert_param(IS_SPI_DIRECTION_MODE(SPI_InitStruct->SPI_Direction));
  42. assert_param(IS_SPI_MODE(SPI_InitStruct->SPI_Mode));
  43. assert_param(IS_SPI_DATASIZE(SPI_InitStruct->SPI_DataSize));
  44. assert_param(IS_SPI_CPOL(SPI_InitStruct->SPI_CPOL));
  45. assert_param(IS_SPI_CPHA(SPI_InitStruct->SPI_CPHA));
  46. assert_param(IS_SPI_NSS(SPI_InitStruct->SPI_NSS));
  47. assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_InitStruct->SPI_BaudRatePrescaler));
  48. assert_param(IS_SPI_FIRST_BIT(SPI_InitStruct->SPI_FirstBit));
  49. assert_param(IS_SPI_CRC_POLYNOMIAL(SPI_InitStruct->SPI_CRCPolynomial));
  50. /*---------------------------- SPIx CR1 Configuration ------------------------*/
  51. /* Get the SPIx CR1 value */
  52. tmpreg = SPIx->CR1;
  53. /* Clear BIDIMode, BIDIOE, RxONLY, SSM, SSI, LSBFirst, BR, MSTR, CPOL and CPHA bits */
  54. tmpreg &= CR1_CLEAR_Mask;
  55. /* Configure SPIx: direction, NSS management, first transmitted bit, BaudRate prescaler
  56. master/salve mode, CPOL and CPHA */
  57. /* Set BIDImode, BIDIOE and RxONLY bits according to SPI_Direction value */
  58. /* Set SSM, SSI and MSTR bits according to SPI_Mode and SPI_NSS values */
  59. /* Set LSBFirst bit according to SPI_FirstBit value */
  60. /* Set BR bits according to SPI_BaudRatePrescaler value */
  61. /* Set CPOL bit according to SPI_CPOL value */
  62. /* Set CPHA bit according to SPI_CPHA value */
  63. tmpreg |= (uint16_t)((uint32_t)SPI_InitStruct->SPI_Direction | SPI_InitStruct->SPI_Mode |
  64. SPI_InitStruct->SPI_DataSize | SPI_InitStruct->SPI_CPOL |
  65. SPI_InitStruct->SPI_CPHA | SPI_InitStruct->SPI_NSS |
  66. SPI_InitStruct->SPI_BaudRatePrescaler | SPI_InitStruct->SPI_FirstBit);
  67. /* Write to SPIx CR1 */
  68. SPIx->CR1 = tmpreg;
  69. /* Activate the SPI mode (Reset I2SMOD bit in I2SCFGR register) */
  70. SPIx->I2SCFGR &= SPI_Mode_Select;
  71. /*---------------------------- SPIx CRCPOLY Configuration --------------------*/
  72. /* Write to SPIx CRCPOLY */
  73. SPIx->CRCPR = SPI_InitStruct->SPI_CRCPolynomial;
  74. }
  75. void SPI_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState)
  76. {
  77. /* Check the parameters */
  78. assert_param(IS_SPI_ALL_PERIPH(SPIx));
  79. assert_param(IS_FUNCTIONAL_STATE(NewState));
  80. if (NewState != DISABLE)
  81. {
  82. /* Enable the selected SPI peripheral */
  83. SPIx->CR1 |= CR1_SPE_Set;
  84. }
  85. else
  86. {
  87. /* Disable the selected SPI peripheral */
  88. SPIx->CR1 &= CR1_SPE_Reset;
  89. }
  90. }