You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

280 lines
11 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32wbxx_ll_spi.c
  4. * @author MCD Application Team
  5. * @brief SPI LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. #if defined(USE_FULL_LL_DRIVER)
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "stm32wbxx_ll_spi.h"
  22. #include "stm32wbxx_ll_bus.h"
  23. #ifdef USE_FULL_ASSERT
  24. #include "stm32_assert.h"
  25. #else
  26. #define assert_param(expr) ((void)0U)
  27. #endif
  28. /** @addtogroup STM32WBxx_LL_Driver
  29. * @{
  30. */
  31. #if defined (SPI1) || defined (SPI2)
  32. /** @addtogroup SPI_LL
  33. * @{
  34. */
  35. /* Private types -------------------------------------------------------------*/
  36. /* Private variables ---------------------------------------------------------*/
  37. /* Private constants ---------------------------------------------------------*/
  38. /** @defgroup SPI_LL_Private_Constants SPI Private Constants
  39. * @{
  40. */
  41. /* SPI registers Masks */
  42. #define SPI_CR1_CLEAR_MASK (SPI_CR1_CPHA | SPI_CR1_CPOL | SPI_CR1_MSTR | \
  43. SPI_CR1_BR | SPI_CR1_LSBFIRST | SPI_CR1_SSI | \
  44. SPI_CR1_SSM | SPI_CR1_RXONLY | SPI_CR1_CRCL | \
  45. SPI_CR1_CRCNEXT | SPI_CR1_CRCEN | SPI_CR1_BIDIOE | \
  46. SPI_CR1_BIDIMODE)
  47. /**
  48. * @}
  49. */
  50. /* Private macros ------------------------------------------------------------*/
  51. /** @defgroup SPI_LL_Private_Macros SPI Private Macros
  52. * @{
  53. */
  54. #define IS_LL_SPI_TRANSFER_DIRECTION(__VALUE__) (((__VALUE__) == LL_SPI_FULL_DUPLEX) \
  55. || ((__VALUE__) == LL_SPI_SIMPLEX_RX) \
  56. || ((__VALUE__) == LL_SPI_HALF_DUPLEX_RX) \
  57. || ((__VALUE__) == LL_SPI_HALF_DUPLEX_TX))
  58. #define IS_LL_SPI_MODE(__VALUE__) (((__VALUE__) == LL_SPI_MODE_MASTER) \
  59. || ((__VALUE__) == LL_SPI_MODE_SLAVE))
  60. #define IS_LL_SPI_DATAWIDTH(__VALUE__) (((__VALUE__) == LL_SPI_DATAWIDTH_4BIT) \
  61. || ((__VALUE__) == LL_SPI_DATAWIDTH_5BIT) \
  62. || ((__VALUE__) == LL_SPI_DATAWIDTH_6BIT) \
  63. || ((__VALUE__) == LL_SPI_DATAWIDTH_7BIT) \
  64. || ((__VALUE__) == LL_SPI_DATAWIDTH_8BIT) \
  65. || ((__VALUE__) == LL_SPI_DATAWIDTH_9BIT) \
  66. || ((__VALUE__) == LL_SPI_DATAWIDTH_10BIT) \
  67. || ((__VALUE__) == LL_SPI_DATAWIDTH_11BIT) \
  68. || ((__VALUE__) == LL_SPI_DATAWIDTH_12BIT) \
  69. || ((__VALUE__) == LL_SPI_DATAWIDTH_13BIT) \
  70. || ((__VALUE__) == LL_SPI_DATAWIDTH_14BIT) \
  71. || ((__VALUE__) == LL_SPI_DATAWIDTH_15BIT) \
  72. || ((__VALUE__) == LL_SPI_DATAWIDTH_16BIT))
  73. #define IS_LL_SPI_POLARITY(__VALUE__) (((__VALUE__) == LL_SPI_POLARITY_LOW) \
  74. || ((__VALUE__) == LL_SPI_POLARITY_HIGH))
  75. #define IS_LL_SPI_PHASE(__VALUE__) (((__VALUE__) == LL_SPI_PHASE_1EDGE) \
  76. || ((__VALUE__) == LL_SPI_PHASE_2EDGE))
  77. #define IS_LL_SPI_NSS(__VALUE__) (((__VALUE__) == LL_SPI_NSS_SOFT) \
  78. || ((__VALUE__) == LL_SPI_NSS_HARD_INPUT) \
  79. || ((__VALUE__) == LL_SPI_NSS_HARD_OUTPUT))
  80. #define IS_LL_SPI_BAUDRATE(__VALUE__) (((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV2) \
  81. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV4) \
  82. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV8) \
  83. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV16) \
  84. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV32) \
  85. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV64) \
  86. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV128) \
  87. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV256))
  88. #define IS_LL_SPI_BITORDER(__VALUE__) (((__VALUE__) == LL_SPI_LSB_FIRST) \
  89. || ((__VALUE__) == LL_SPI_MSB_FIRST))
  90. #define IS_LL_SPI_CRCCALCULATION(__VALUE__) (((__VALUE__) == LL_SPI_CRCCALCULATION_ENABLE) \
  91. || ((__VALUE__) == LL_SPI_CRCCALCULATION_DISABLE))
  92. #define IS_LL_SPI_CRC_POLYNOMIAL(__VALUE__) ((__VALUE__) >= 0x1U)
  93. /**
  94. * @}
  95. */
  96. /* Private function prototypes -----------------------------------------------*/
  97. /* Exported functions --------------------------------------------------------*/
  98. /** @addtogroup SPI_LL_Exported_Functions
  99. * @{
  100. */
  101. /** @addtogroup SPI_LL_EF_Init
  102. * @{
  103. */
  104. /**
  105. * @brief De-initialize the SPI registers to their default reset values.
  106. * @param SPIx SPI Instance
  107. * @retval An ErrorStatus enumeration value:
  108. * - SUCCESS: SPI registers are de-initialized
  109. * - ERROR: SPI registers are not de-initialized
  110. */
  111. ErrorStatus LL_SPI_DeInit(SPI_TypeDef *SPIx)
  112. {
  113. ErrorStatus status = ERROR;
  114. /* Check the parameters */
  115. assert_param(IS_SPI_ALL_INSTANCE(SPIx));
  116. #if defined(SPI1)
  117. if (SPIx == SPI1)
  118. {
  119. /* Force reset of SPI clock */
  120. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_SPI1);
  121. /* Release reset of SPI clock */
  122. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_SPI1);
  123. status = SUCCESS;
  124. }
  125. #endif /* SPI1 */
  126. #if defined(SPI2)
  127. if (SPIx == SPI2)
  128. {
  129. /* Force reset of SPI clock */
  130. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_SPI2);
  131. /* Release reset of SPI clock */
  132. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_SPI2);
  133. status = SUCCESS;
  134. }
  135. #endif /* SPI2 */
  136. return status;
  137. }
  138. /**
  139. * @brief Initialize the SPI registers according to the specified parameters in SPI_InitStruct.
  140. * @note As some bits in SPI configuration registers can only be written when the SPI is disabled (SPI_CR1_SPE bit =0),
  141. * SPI peripheral should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
  142. * @param SPIx SPI Instance
  143. * @param SPI_InitStruct pointer to a @ref LL_SPI_InitTypeDef structure
  144. * @retval An ErrorStatus enumeration value. (Return always SUCCESS)
  145. */
  146. ErrorStatus LL_SPI_Init(SPI_TypeDef *SPIx, LL_SPI_InitTypeDef *SPI_InitStruct)
  147. {
  148. ErrorStatus status = ERROR;
  149. /* Check the SPI Instance SPIx*/
  150. assert_param(IS_SPI_ALL_INSTANCE(SPIx));
  151. /* Check the SPI parameters from SPI_InitStruct*/
  152. assert_param(IS_LL_SPI_TRANSFER_DIRECTION(SPI_InitStruct->TransferDirection));
  153. assert_param(IS_LL_SPI_MODE(SPI_InitStruct->Mode));
  154. assert_param(IS_LL_SPI_DATAWIDTH(SPI_InitStruct->DataWidth));
  155. assert_param(IS_LL_SPI_POLARITY(SPI_InitStruct->ClockPolarity));
  156. assert_param(IS_LL_SPI_PHASE(SPI_InitStruct->ClockPhase));
  157. assert_param(IS_LL_SPI_NSS(SPI_InitStruct->NSS));
  158. assert_param(IS_LL_SPI_BAUDRATE(SPI_InitStruct->BaudRate));
  159. assert_param(IS_LL_SPI_BITORDER(SPI_InitStruct->BitOrder));
  160. assert_param(IS_LL_SPI_CRCCALCULATION(SPI_InitStruct->CRCCalculation));
  161. if (LL_SPI_IsEnabled(SPIx) == 0x00000000U)
  162. {
  163. /*---------------------------- SPIx CR1 Configuration ------------------------
  164. * Configure SPIx CR1 with parameters:
  165. * - TransferDirection: SPI_CR1_BIDIMODE, SPI_CR1_BIDIOE and SPI_CR1_RXONLY bits
  166. * - Master/Slave Mode: SPI_CR1_MSTR bit
  167. * - ClockPolarity: SPI_CR1_CPOL bit
  168. * - ClockPhase: SPI_CR1_CPHA bit
  169. * - NSS management: SPI_CR1_SSM bit
  170. * - BaudRate prescaler: SPI_CR1_BR[2:0] bits
  171. * - BitOrder: SPI_CR1_LSBFIRST bit
  172. * - CRCCalculation: SPI_CR1_CRCEN bit
  173. */
  174. MODIFY_REG(SPIx->CR1,
  175. SPI_CR1_CLEAR_MASK,
  176. SPI_InitStruct->TransferDirection | SPI_InitStruct->Mode |
  177. SPI_InitStruct->ClockPolarity | SPI_InitStruct->ClockPhase |
  178. SPI_InitStruct->NSS | SPI_InitStruct->BaudRate |
  179. SPI_InitStruct->BitOrder | SPI_InitStruct->CRCCalculation);
  180. /*---------------------------- SPIx CR2 Configuration ------------------------
  181. * Configure SPIx CR2 with parameters:
  182. * - DataWidth: DS[3:0] bits
  183. * - NSS management: SSOE bit
  184. */
  185. MODIFY_REG(SPIx->CR2,
  186. SPI_CR2_DS | SPI_CR2_SSOE,
  187. SPI_InitStruct->DataWidth | (SPI_InitStruct->NSS >> 16U));
  188. /*---------------------------- SPIx CRCPR Configuration ----------------------
  189. * Configure SPIx CRCPR with parameters:
  190. * - CRCPoly: CRCPOLY[15:0] bits
  191. */
  192. if (SPI_InitStruct->CRCCalculation == LL_SPI_CRCCALCULATION_ENABLE)
  193. {
  194. assert_param(IS_LL_SPI_CRC_POLYNOMIAL(SPI_InitStruct->CRCPoly));
  195. LL_SPI_SetCRCPolynomial(SPIx, SPI_InitStruct->CRCPoly);
  196. }
  197. status = SUCCESS;
  198. }
  199. return status;
  200. }
  201. /**
  202. * @brief Set each @ref LL_SPI_InitTypeDef field to default value.
  203. * @param SPI_InitStruct pointer to a @ref LL_SPI_InitTypeDef structure
  204. * whose fields will be set to default values.
  205. * @retval None
  206. */
  207. void LL_SPI_StructInit(LL_SPI_InitTypeDef *SPI_InitStruct)
  208. {
  209. /* Set SPI_InitStruct fields to default values */
  210. SPI_InitStruct->TransferDirection = LL_SPI_FULL_DUPLEX;
  211. SPI_InitStruct->Mode = LL_SPI_MODE_SLAVE;
  212. SPI_InitStruct->DataWidth = LL_SPI_DATAWIDTH_8BIT;
  213. SPI_InitStruct->ClockPolarity = LL_SPI_POLARITY_LOW;
  214. SPI_InitStruct->ClockPhase = LL_SPI_PHASE_1EDGE;
  215. SPI_InitStruct->NSS = LL_SPI_NSS_HARD_INPUT;
  216. SPI_InitStruct->BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV2;
  217. SPI_InitStruct->BitOrder = LL_SPI_MSB_FIRST;
  218. SPI_InitStruct->CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE;
  219. SPI_InitStruct->CRCPoly = 7U;
  220. }
  221. /**
  222. * @}
  223. */
  224. /**
  225. * @}
  226. */
  227. /**
  228. * @}
  229. */
  230. #endif /* defined (SPI1) || defined (SPI2) */
  231. /**
  232. * @}
  233. */
  234. #endif /* USE_FULL_LL_DRIVER */
  235. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/