stm32l4xx_ll_spi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_ll_spi.c
  4. * @author MCD Application Team
  5. * @brief SPI LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification,
  12. * are permitted provided that the following conditions are met:
  13. * 1. Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  28. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. ******************************************************************************
  34. */
  35. #if defined(USE_FULL_LL_DRIVER)
  36. /* Includes ------------------------------------------------------------------*/
  37. #include "stm32l4xx_ll_spi.h"
  38. #include "stm32l4xx_ll_bus.h"
  39. #ifdef USE_FULL_ASSERT
  40. #include "stm32_assert.h"
  41. #else
  42. #define assert_param(expr) ((void)0U)
  43. #endif
  44. /** @addtogroup STM32L4xx_LL_Driver
  45. * @{
  46. */
  47. #if defined (SPI1) || defined (SPI2) || defined (SPI3)
  48. /** @addtogroup SPI_LL
  49. * @{
  50. */
  51. /* Private types -------------------------------------------------------------*/
  52. /* Private variables ---------------------------------------------------------*/
  53. /* Private constants ---------------------------------------------------------*/
  54. /** @defgroup SPI_LL_Private_Constants SPI Private Constants
  55. * @{
  56. */
  57. /* SPI registers Masks */
  58. #define SPI_CR1_CLEAR_MASK (SPI_CR1_CPHA | SPI_CR1_CPOL | SPI_CR1_MSTR | \
  59. SPI_CR1_BR | SPI_CR1_LSBFIRST | SPI_CR1_SSI | \
  60. SPI_CR1_SSM | SPI_CR1_RXONLY | SPI_CR1_CRCL | \
  61. SPI_CR1_CRCNEXT | SPI_CR1_CRCEN | SPI_CR1_BIDIOE | \
  62. SPI_CR1_BIDIMODE)
  63. /**
  64. * @}
  65. */
  66. /* Private macros ------------------------------------------------------------*/
  67. /** @defgroup SPI_LL_Private_Macros SPI Private Macros
  68. * @{
  69. */
  70. #define IS_LL_SPI_TRANSFER_DIRECTION(__VALUE__) (((__VALUE__) == LL_SPI_FULL_DUPLEX) \
  71. || ((__VALUE__) == LL_SPI_SIMPLEX_RX) \
  72. || ((__VALUE__) == LL_SPI_HALF_DUPLEX_RX) \
  73. || ((__VALUE__) == LL_SPI_HALF_DUPLEX_TX))
  74. #define IS_LL_SPI_MODE(__VALUE__) (((__VALUE__) == LL_SPI_MODE_MASTER) \
  75. || ((__VALUE__) == LL_SPI_MODE_SLAVE))
  76. #define IS_LL_SPI_DATAWIDTH(__VALUE__) (((__VALUE__) == LL_SPI_DATAWIDTH_4BIT) \
  77. || ((__VALUE__) == LL_SPI_DATAWIDTH_5BIT) \
  78. || ((__VALUE__) == LL_SPI_DATAWIDTH_6BIT) \
  79. || ((__VALUE__) == LL_SPI_DATAWIDTH_7BIT) \
  80. || ((__VALUE__) == LL_SPI_DATAWIDTH_8BIT) \
  81. || ((__VALUE__) == LL_SPI_DATAWIDTH_9BIT) \
  82. || ((__VALUE__) == LL_SPI_DATAWIDTH_10BIT) \
  83. || ((__VALUE__) == LL_SPI_DATAWIDTH_11BIT) \
  84. || ((__VALUE__) == LL_SPI_DATAWIDTH_12BIT) \
  85. || ((__VALUE__) == LL_SPI_DATAWIDTH_13BIT) \
  86. || ((__VALUE__) == LL_SPI_DATAWIDTH_14BIT) \
  87. || ((__VALUE__) == LL_SPI_DATAWIDTH_15BIT) \
  88. || ((__VALUE__) == LL_SPI_DATAWIDTH_16BIT))
  89. #define IS_LL_SPI_POLARITY(__VALUE__) (((__VALUE__) == LL_SPI_POLARITY_LOW) \
  90. || ((__VALUE__) == LL_SPI_POLARITY_HIGH))
  91. #define IS_LL_SPI_PHASE(__VALUE__) (((__VALUE__) == LL_SPI_PHASE_1EDGE) \
  92. || ((__VALUE__) == LL_SPI_PHASE_2EDGE))
  93. #define IS_LL_SPI_NSS(__VALUE__) (((__VALUE__) == LL_SPI_NSS_SOFT) \
  94. || ((__VALUE__) == LL_SPI_NSS_HARD_INPUT) \
  95. || ((__VALUE__) == LL_SPI_NSS_HARD_OUTPUT))
  96. #define IS_LL_SPI_BAUDRATE(__VALUE__) (((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV2) \
  97. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV4) \
  98. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV8) \
  99. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV16) \
  100. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV32) \
  101. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV64) \
  102. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV128) \
  103. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV256))
  104. #define IS_LL_SPI_BITORDER(__VALUE__) (((__VALUE__) == LL_SPI_LSB_FIRST) \
  105. || ((__VALUE__) == LL_SPI_MSB_FIRST))
  106. #define IS_LL_SPI_CRCCALCULATION(__VALUE__) (((__VALUE__) == LL_SPI_CRCCALCULATION_ENABLE) \
  107. || ((__VALUE__) == LL_SPI_CRCCALCULATION_DISABLE))
  108. #define IS_LL_SPI_CRC_POLYNOMIAL(__VALUE__) ((__VALUE__) >= 0x1U)
  109. /**
  110. * @}
  111. */
  112. /* Private function prototypes -----------------------------------------------*/
  113. /* Exported functions --------------------------------------------------------*/
  114. /** @addtogroup SPI_LL_Exported_Functions
  115. * @{
  116. */
  117. /** @addtogroup SPI_LL_EF_Init
  118. * @{
  119. */
  120. /**
  121. * @brief De-initialize the SPI registers to their default reset values.
  122. * @param SPIx SPI Instance
  123. * @retval An ErrorStatus enumeration value:
  124. * - SUCCESS: SPI registers are de-initialized
  125. * - ERROR: SPI registers are not de-initialized
  126. */
  127. ErrorStatus LL_SPI_DeInit(SPI_TypeDef *SPIx)
  128. {
  129. ErrorStatus status = ERROR;
  130. /* Check the parameters */
  131. assert_param(IS_SPI_ALL_INSTANCE(SPIx));
  132. #if defined(SPI1)
  133. if (SPIx == SPI1)
  134. {
  135. /* Force reset of SPI clock */
  136. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_SPI1);
  137. /* Release reset of SPI clock */
  138. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_SPI1);
  139. status = SUCCESS;
  140. }
  141. #endif /* SPI1 */
  142. #if defined(SPI2)
  143. if (SPIx == SPI2)
  144. {
  145. /* Force reset of SPI clock */
  146. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_SPI2);
  147. /* Release reset of SPI clock */
  148. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_SPI2);
  149. status = SUCCESS;
  150. }
  151. #endif /* SPI2 */
  152. #if defined(SPI3)
  153. if (SPIx == SPI3)
  154. {
  155. /* Force reset of SPI clock */
  156. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_SPI3);
  157. /* Release reset of SPI clock */
  158. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_SPI3);
  159. status = SUCCESS;
  160. }
  161. #endif /* SPI3 */
  162. return status;
  163. }
  164. /**
  165. * @brief Initialize the SPI registers according to the specified parameters in SPI_InitStruct.
  166. * @note As some bits in SPI configuration registers can only be written when the SPI is disabled (SPI_CR1_SPE bit =0),
  167. * SPI IP should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
  168. * @param SPIx SPI Instance
  169. * @param SPI_InitStruct pointer to a @ref LL_SPI_InitTypeDef structure
  170. * @retval An ErrorStatus enumeration value. (Return always SUCCESS)
  171. */
  172. ErrorStatus LL_SPI_Init(SPI_TypeDef *SPIx, LL_SPI_InitTypeDef *SPI_InitStruct)
  173. {
  174. ErrorStatus status = ERROR;
  175. /* Check the SPI Instance SPIx*/
  176. assert_param(IS_SPI_ALL_INSTANCE(SPIx));
  177. /* Check the SPI parameters from SPI_InitStruct*/
  178. assert_param(IS_LL_SPI_TRANSFER_DIRECTION(SPI_InitStruct->TransferDirection));
  179. assert_param(IS_LL_SPI_MODE(SPI_InitStruct->Mode));
  180. assert_param(IS_LL_SPI_DATAWIDTH(SPI_InitStruct->DataWidth));
  181. assert_param(IS_LL_SPI_POLARITY(SPI_InitStruct->ClockPolarity));
  182. assert_param(IS_LL_SPI_PHASE(SPI_InitStruct->ClockPhase));
  183. assert_param(IS_LL_SPI_NSS(SPI_InitStruct->NSS));
  184. assert_param(IS_LL_SPI_BAUDRATE(SPI_InitStruct->BaudRate));
  185. assert_param(IS_LL_SPI_BITORDER(SPI_InitStruct->BitOrder));
  186. assert_param(IS_LL_SPI_CRCCALCULATION(SPI_InitStruct->CRCCalculation));
  187. if (LL_SPI_IsEnabled(SPIx) == 0x00000000U)
  188. {
  189. /*---------------------------- SPIx CR1 Configuration ------------------------
  190. * Configure SPIx CR1 with parameters:
  191. * - TransferDirection: SPI_CR1_BIDIMODE, SPI_CR1_BIDIOE and SPI_CR1_RXONLY bits
  192. * - Master/Slave Mode: SPI_CR1_MSTR bit
  193. * - ClockPolarity: SPI_CR1_CPOL bit
  194. * - ClockPhase: SPI_CR1_CPHA bit
  195. * - NSS management: SPI_CR1_SSM bit
  196. * - BaudRate prescaler: SPI_CR1_BR[2:0] bits
  197. * - BitOrder: SPI_CR1_LSBFIRST bit
  198. * - CRCCalculation: SPI_CR1_CRCEN bit
  199. */
  200. MODIFY_REG(SPIx->CR1,
  201. SPI_CR1_CLEAR_MASK,
  202. SPI_InitStruct->TransferDirection | SPI_InitStruct->Mode |
  203. SPI_InitStruct->ClockPolarity | SPI_InitStruct->ClockPhase |
  204. SPI_InitStruct->NSS | SPI_InitStruct->BaudRate |
  205. SPI_InitStruct->BitOrder | SPI_InitStruct->CRCCalculation);
  206. /*---------------------------- SPIx CR2 Configuration ------------------------
  207. * Configure SPIx CR2 with parameters:
  208. * - DataWidth: DS[3:0] bits
  209. * - NSS management: SSOE bit
  210. */
  211. MODIFY_REG(SPIx->CR2,
  212. SPI_CR2_DS | SPI_CR2_SSOE,
  213. SPI_InitStruct->DataWidth | (SPI_InitStruct->NSS >> 16U));
  214. /*---------------------------- SPIx CRCPR Configuration ----------------------
  215. * Configure SPIx CRCPR with parameters:
  216. * - CRCPoly: CRCPOLY[15:0] bits
  217. */
  218. if (SPI_InitStruct->CRCCalculation == LL_SPI_CRCCALCULATION_ENABLE)
  219. {
  220. assert_param(IS_LL_SPI_CRC_POLYNOMIAL(SPI_InitStruct->CRCPoly));
  221. LL_SPI_SetCRCPolynomial(SPIx, SPI_InitStruct->CRCPoly);
  222. }
  223. status = SUCCESS;
  224. }
  225. return status;
  226. }
  227. /**
  228. * @brief Set each @ref LL_SPI_InitTypeDef field to default value.
  229. * @param SPI_InitStruct pointer to a @ref LL_SPI_InitTypeDef structure
  230. * whose fields will be set to default values.
  231. * @retval None
  232. */
  233. void LL_SPI_StructInit(LL_SPI_InitTypeDef *SPI_InitStruct)
  234. {
  235. /* Set SPI_InitStruct fields to default values */
  236. SPI_InitStruct->TransferDirection = LL_SPI_FULL_DUPLEX;
  237. SPI_InitStruct->Mode = LL_SPI_MODE_SLAVE;
  238. SPI_InitStruct->DataWidth = LL_SPI_DATAWIDTH_8BIT;
  239. SPI_InitStruct->ClockPolarity = LL_SPI_POLARITY_LOW;
  240. SPI_InitStruct->ClockPhase = LL_SPI_PHASE_1EDGE;
  241. SPI_InitStruct->NSS = LL_SPI_NSS_HARD_INPUT;
  242. SPI_InitStruct->BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV2;
  243. SPI_InitStruct->BitOrder = LL_SPI_MSB_FIRST;
  244. SPI_InitStruct->CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE;
  245. SPI_InitStruct->CRCPoly = 7U;
  246. }
  247. /**
  248. * @}
  249. */
  250. /**
  251. * @}
  252. */
  253. /**
  254. * @}
  255. */
  256. #endif /* defined (SPI1) || defined (SPI2) || defined (SPI3) */
  257. /**
  258. * @}
  259. */
  260. #endif /* USE_FULL_LL_DRIVER */
  261. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/