stm32l4xx_ll_spi.c 11 KB

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