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.
 
 
 

1421 lines
48 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32wbxx_ll_spi.h
  4. * @author MCD Application Team
  5. * @brief Header file of SPI LL module.
  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. /* Define to prevent recursive inclusion -------------------------------------*/
  20. #ifndef STM32WBxx_LL_SPI_H
  21. #define STM32WBxx_LL_SPI_H
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /* Includes ------------------------------------------------------------------*/
  26. #include "stm32wbxx.h"
  27. /** @addtogroup STM32WBxx_LL_Driver
  28. * @{
  29. */
  30. #if defined (SPI1) || defined (SPI2)
  31. /** @defgroup SPI_LL SPI
  32. * @{
  33. */
  34. /* Private types -------------------------------------------------------------*/
  35. /* Private variables ---------------------------------------------------------*/
  36. /* Private macros ------------------------------------------------------------*/
  37. /* Exported types ------------------------------------------------------------*/
  38. #if defined(USE_FULL_LL_DRIVER)
  39. /** @defgroup SPI_LL_ES_INIT SPI Exported Init structure
  40. * @{
  41. */
  42. /**
  43. * @brief SPI Init structures definition
  44. */
  45. typedef struct
  46. {
  47. uint32_t TransferDirection; /*!< Specifies the SPI unidirectional or bidirectional data mode.
  48. This parameter can be a value of @ref SPI_LL_EC_TRANSFER_MODE.
  49. This feature can be modified afterwards using unitary function @ref LL_SPI_SetTransferDirection().*/
  50. uint32_t Mode; /*!< Specifies the SPI mode (Master/Slave).
  51. This parameter can be a value of @ref SPI_LL_EC_MODE.
  52. This feature can be modified afterwards using unitary function @ref LL_SPI_SetMode().*/
  53. uint32_t DataWidth; /*!< Specifies the SPI data width.
  54. This parameter can be a value of @ref SPI_LL_EC_DATAWIDTH.
  55. This feature can be modified afterwards using unitary function @ref LL_SPI_SetDataWidth().*/
  56. uint32_t ClockPolarity; /*!< Specifies the serial clock steady state.
  57. This parameter can be a value of @ref SPI_LL_EC_POLARITY.
  58. This feature can be modified afterwards using unitary function @ref LL_SPI_SetClockPolarity().*/
  59. uint32_t ClockPhase; /*!< Specifies the clock active edge for the bit capture.
  60. This parameter can be a value of @ref SPI_LL_EC_PHASE.
  61. This feature can be modified afterwards using unitary function @ref LL_SPI_SetClockPhase().*/
  62. uint32_t NSS; /*!< Specifies whether the NSS signal is managed by hardware (NSS pin) or by software using the SSI bit.
  63. This parameter can be a value of @ref SPI_LL_EC_NSS_MODE.
  64. This feature can be modified afterwards using unitary function @ref LL_SPI_SetNSSMode().*/
  65. uint32_t BaudRate; /*!< Specifies the BaudRate prescaler value which will be used to configure the transmit and receive SCK clock.
  66. This parameter can be a value of @ref SPI_LL_EC_BAUDRATEPRESCALER.
  67. @note The communication clock is derived from the master clock. The slave clock does not need to be set.
  68. This feature can be modified afterwards using unitary function @ref LL_SPI_SetBaudRatePrescaler().*/
  69. uint32_t BitOrder; /*!< Specifies whether data transfers start from MSB or LSB bit.
  70. This parameter can be a value of @ref SPI_LL_EC_BIT_ORDER.
  71. This feature can be modified afterwards using unitary function @ref LL_SPI_SetTransferBitOrder().*/
  72. uint32_t CRCCalculation; /*!< Specifies if the CRC calculation is enabled or not.
  73. This parameter can be a value of @ref SPI_LL_EC_CRC_CALCULATION.
  74. This feature can be modified afterwards using unitary functions @ref LL_SPI_EnableCRC() and @ref LL_SPI_DisableCRC().*/
  75. uint32_t CRCPoly; /*!< Specifies the polynomial used for the CRC calculation.
  76. This parameter must be a number between Min_Data = 0x00 and Max_Data = 0xFFFF.
  77. This feature can be modified afterwards using unitary function @ref LL_SPI_SetCRCPolynomial().*/
  78. } LL_SPI_InitTypeDef;
  79. /**
  80. * @}
  81. */
  82. #endif /* USE_FULL_LL_DRIVER */
  83. /* Exported constants --------------------------------------------------------*/
  84. /** @defgroup SPI_LL_Exported_Constants SPI Exported Constants
  85. * @{
  86. */
  87. /** @defgroup SPI_LL_EC_GET_FLAG Get Flags Defines
  88. * @brief Flags defines which can be used with LL_SPI_ReadReg function
  89. * @{
  90. */
  91. #define LL_SPI_SR_RXNE SPI_SR_RXNE /*!< Rx buffer not empty flag */
  92. #define LL_SPI_SR_TXE SPI_SR_TXE /*!< Tx buffer empty flag */
  93. #define LL_SPI_SR_BSY SPI_SR_BSY /*!< Busy flag */
  94. #define LL_SPI_SR_CRCERR SPI_SR_CRCERR /*!< CRC error flag */
  95. #define LL_SPI_SR_MODF SPI_SR_MODF /*!< Mode fault flag */
  96. #define LL_SPI_SR_OVR SPI_SR_OVR /*!< Overrun flag */
  97. #define LL_SPI_SR_FRE SPI_SR_FRE /*!< TI mode frame format error flag */
  98. /**
  99. * @}
  100. */
  101. /** @defgroup SPI_LL_EC_IT IT Defines
  102. * @brief IT defines which can be used with LL_SPI_ReadReg and LL_SPI_WriteReg functions
  103. * @{
  104. */
  105. #define LL_SPI_CR2_RXNEIE SPI_CR2_RXNEIE /*!< Rx buffer not empty interrupt enable */
  106. #define LL_SPI_CR2_TXEIE SPI_CR2_TXEIE /*!< Tx buffer empty interrupt enable */
  107. #define LL_SPI_CR2_ERRIE SPI_CR2_ERRIE /*!< Error interrupt enable */
  108. /**
  109. * @}
  110. */
  111. /** @defgroup SPI_LL_EC_MODE Operation Mode
  112. * @{
  113. */
  114. #define LL_SPI_MODE_MASTER (SPI_CR1_MSTR | SPI_CR1_SSI) /*!< Master configuration */
  115. #define LL_SPI_MODE_SLAVE 0x00000000U /*!< Slave configuration */
  116. /**
  117. * @}
  118. */
  119. /** @defgroup SPI_LL_EC_PROTOCOL Serial Protocol
  120. * @{
  121. */
  122. #define LL_SPI_PROTOCOL_MOTOROLA 0x00000000U /*!< Motorola mode. Used as default value */
  123. #define LL_SPI_PROTOCOL_TI (SPI_CR2_FRF) /*!< TI mode */
  124. /**
  125. * @}
  126. */
  127. /** @defgroup SPI_LL_EC_PHASE Clock Phase
  128. * @{
  129. */
  130. #define LL_SPI_PHASE_1EDGE 0x00000000U /*!< First clock transition is the first data capture edge */
  131. #define LL_SPI_PHASE_2EDGE (SPI_CR1_CPHA) /*!< Second clock transition is the first data capture edge */
  132. /**
  133. * @}
  134. */
  135. /** @defgroup SPI_LL_EC_POLARITY Clock Polarity
  136. * @{
  137. */
  138. #define LL_SPI_POLARITY_LOW 0x00000000U /*!< Clock to 0 when idle */
  139. #define LL_SPI_POLARITY_HIGH (SPI_CR1_CPOL) /*!< Clock to 1 when idle */
  140. /**
  141. * @}
  142. */
  143. /** @defgroup SPI_LL_EC_BAUDRATEPRESCALER Baud Rate Prescaler
  144. * @{
  145. */
  146. #define LL_SPI_BAUDRATEPRESCALER_DIV2 0x00000000U /*!< BaudRate control equal to fPCLK/2 */
  147. #define LL_SPI_BAUDRATEPRESCALER_DIV4 (SPI_CR1_BR_0) /*!< BaudRate control equal to fPCLK/4 */
  148. #define LL_SPI_BAUDRATEPRESCALER_DIV8 (SPI_CR1_BR_1) /*!< BaudRate control equal to fPCLK/8 */
  149. #define LL_SPI_BAUDRATEPRESCALER_DIV16 (SPI_CR1_BR_1 | SPI_CR1_BR_0) /*!< BaudRate control equal to fPCLK/16 */
  150. #define LL_SPI_BAUDRATEPRESCALER_DIV32 (SPI_CR1_BR_2) /*!< BaudRate control equal to fPCLK/32 */
  151. #define LL_SPI_BAUDRATEPRESCALER_DIV64 (SPI_CR1_BR_2 | SPI_CR1_BR_0) /*!< BaudRate control equal to fPCLK/64 */
  152. #define LL_SPI_BAUDRATEPRESCALER_DIV128 (SPI_CR1_BR_2 | SPI_CR1_BR_1) /*!< BaudRate control equal to fPCLK/128 */
  153. #define LL_SPI_BAUDRATEPRESCALER_DIV256 (SPI_CR1_BR_2 | SPI_CR1_BR_1 | SPI_CR1_BR_0) /*!< BaudRate control equal to fPCLK/256 */
  154. /**
  155. * @}
  156. */
  157. /** @defgroup SPI_LL_EC_BIT_ORDER Transmission Bit Order
  158. * @{
  159. */
  160. #define LL_SPI_LSB_FIRST (SPI_CR1_LSBFIRST) /*!< Data is transmitted/received with the LSB first */
  161. #define LL_SPI_MSB_FIRST 0x00000000U /*!< Data is transmitted/received with the MSB first */
  162. /**
  163. * @}
  164. */
  165. /** @defgroup SPI_LL_EC_TRANSFER_MODE Transfer Mode
  166. * @{
  167. */
  168. #define LL_SPI_FULL_DUPLEX 0x00000000U /*!< Full-Duplex mode. Rx and Tx transfer on 2 lines */
  169. #define LL_SPI_SIMPLEX_RX (SPI_CR1_RXONLY) /*!< Simplex Rx mode. Rx transfer only on 1 line */
  170. #define LL_SPI_HALF_DUPLEX_RX (SPI_CR1_BIDIMODE) /*!< Half-Duplex Rx mode. Rx transfer on 1 line */
  171. #define LL_SPI_HALF_DUPLEX_TX (SPI_CR1_BIDIMODE | SPI_CR1_BIDIOE) /*!< Half-Duplex Tx mode. Tx transfer on 1 line */
  172. /**
  173. * @}
  174. */
  175. /** @defgroup SPI_LL_EC_NSS_MODE Slave Select Pin Mode
  176. * @{
  177. */
  178. #define LL_SPI_NSS_SOFT (SPI_CR1_SSM) /*!< NSS managed internally. NSS pin not used and free */
  179. #define LL_SPI_NSS_HARD_INPUT 0x00000000U /*!< NSS pin used in Input. Only used in Master mode */
  180. #define LL_SPI_NSS_HARD_OUTPUT (((uint32_t)SPI_CR2_SSOE << 16U)) /*!< NSS pin used in Output. Only used in Slave mode as chip select */
  181. /**
  182. * @}
  183. */
  184. /** @defgroup SPI_LL_EC_DATAWIDTH Datawidth
  185. * @{
  186. */
  187. #define LL_SPI_DATAWIDTH_4BIT (SPI_CR2_DS_0 | SPI_CR2_DS_1) /*!< Data length for SPI transfer: 4 bits */
  188. #define LL_SPI_DATAWIDTH_5BIT (SPI_CR2_DS_2) /*!< Data length for SPI transfer: 5 bits */
  189. #define LL_SPI_DATAWIDTH_6BIT (SPI_CR2_DS_2 | SPI_CR2_DS_0) /*!< Data length for SPI transfer: 6 bits */
  190. #define LL_SPI_DATAWIDTH_7BIT (SPI_CR2_DS_2 | SPI_CR2_DS_1) /*!< Data length for SPI transfer: 7 bits */
  191. #define LL_SPI_DATAWIDTH_8BIT (SPI_CR2_DS_2 | SPI_CR2_DS_1 | SPI_CR2_DS_0) /*!< Data length for SPI transfer: 8 bits */
  192. #define LL_SPI_DATAWIDTH_9BIT (SPI_CR2_DS_3) /*!< Data length for SPI transfer: 9 bits */
  193. #define LL_SPI_DATAWIDTH_10BIT (SPI_CR2_DS_3 | SPI_CR2_DS_0) /*!< Data length for SPI transfer: 10 bits */
  194. #define LL_SPI_DATAWIDTH_11BIT (SPI_CR2_DS_3 | SPI_CR2_DS_1) /*!< Data length for SPI transfer: 11 bits */
  195. #define LL_SPI_DATAWIDTH_12BIT (SPI_CR2_DS_3 | SPI_CR2_DS_1 | SPI_CR2_DS_0) /*!< Data length for SPI transfer: 12 bits */
  196. #define LL_SPI_DATAWIDTH_13BIT (SPI_CR2_DS_3 | SPI_CR2_DS_2) /*!< Data length for SPI transfer: 13 bits */
  197. #define LL_SPI_DATAWIDTH_14BIT (SPI_CR2_DS_3 | SPI_CR2_DS_2 | SPI_CR2_DS_0) /*!< Data length for SPI transfer: 14 bits */
  198. #define LL_SPI_DATAWIDTH_15BIT (SPI_CR2_DS_3 | SPI_CR2_DS_2 | SPI_CR2_DS_1) /*!< Data length for SPI transfer: 15 bits */
  199. #define LL_SPI_DATAWIDTH_16BIT (SPI_CR2_DS_3 | SPI_CR2_DS_2 | SPI_CR2_DS_1 | SPI_CR2_DS_0) /*!< Data length for SPI transfer: 16 bits */
  200. /**
  201. * @}
  202. */
  203. #if defined(USE_FULL_LL_DRIVER)
  204. /** @defgroup SPI_LL_EC_CRC_CALCULATION CRC Calculation
  205. * @{
  206. */
  207. #define LL_SPI_CRCCALCULATION_DISABLE 0x00000000U /*!< CRC calculation disabled */
  208. #define LL_SPI_CRCCALCULATION_ENABLE (SPI_CR1_CRCEN) /*!< CRC calculation enabled */
  209. /**
  210. * @}
  211. */
  212. #endif /* USE_FULL_LL_DRIVER */
  213. /** @defgroup SPI_LL_EC_CRC_LENGTH CRC Length
  214. * @{
  215. */
  216. #define LL_SPI_CRC_8BIT 0x00000000U /*!< 8-bit CRC length */
  217. #define LL_SPI_CRC_16BIT (SPI_CR1_CRCL) /*!< 16-bit CRC length */
  218. /**
  219. * @}
  220. */
  221. /** @defgroup SPI_LL_EC_RX_FIFO_TH RX FIFO Threshold
  222. * @{
  223. */
  224. #define LL_SPI_RX_FIFO_TH_HALF 0x00000000U /*!< RXNE event is generated if FIFO level is greater than or equal to 1/2 (16-bit) */
  225. #define LL_SPI_RX_FIFO_TH_QUARTER (SPI_CR2_FRXTH) /*!< RXNE event is generated if FIFO level is greater than or equal to 1/4 (8-bit) */
  226. /**
  227. * @}
  228. */
  229. /** @defgroup SPI_LL_EC_RX_FIFO RX FIFO Level
  230. * @{
  231. */
  232. #define LL_SPI_RX_FIFO_EMPTY 0x00000000U /*!< FIFO reception empty */
  233. #define LL_SPI_RX_FIFO_QUARTER_FULL (SPI_SR_FRLVL_0) /*!< FIFO reception 1/4 */
  234. #define LL_SPI_RX_FIFO_HALF_FULL (SPI_SR_FRLVL_1) /*!< FIFO reception 1/2 */
  235. #define LL_SPI_RX_FIFO_FULL (SPI_SR_FRLVL_1 | SPI_SR_FRLVL_0) /*!< FIFO reception full */
  236. /**
  237. * @}
  238. */
  239. /** @defgroup SPI_LL_EC_TX_FIFO TX FIFO Level
  240. * @{
  241. */
  242. #define LL_SPI_TX_FIFO_EMPTY 0x00000000U /*!< FIFO transmission empty */
  243. #define LL_SPI_TX_FIFO_QUARTER_FULL (SPI_SR_FTLVL_0) /*!< FIFO transmission 1/4 */
  244. #define LL_SPI_TX_FIFO_HALF_FULL (SPI_SR_FTLVL_1) /*!< FIFO transmission 1/2 */
  245. #define LL_SPI_TX_FIFO_FULL (SPI_SR_FTLVL_1 | SPI_SR_FTLVL_0) /*!< FIFO transmission full */
  246. /**
  247. * @}
  248. */
  249. /** @defgroup SPI_LL_EC_DMA_PARITY DMA Parity
  250. * @{
  251. */
  252. #define LL_SPI_DMA_PARITY_EVEN 0x00000000U /*!< Select DMA parity Even */
  253. #define LL_SPI_DMA_PARITY_ODD 0x00000001U /*!< Select DMA parity Odd */
  254. /**
  255. * @}
  256. */
  257. /**
  258. * @}
  259. */
  260. /* Exported macro ------------------------------------------------------------*/
  261. /** @defgroup SPI_LL_Exported_Macros SPI Exported Macros
  262. * @{
  263. */
  264. /** @defgroup SPI_LL_EM_WRITE_READ Common Write and read registers Macros
  265. * @{
  266. */
  267. /**
  268. * @brief Write a value in SPI register
  269. * @param __INSTANCE__ SPI Instance
  270. * @param __REG__ Register to be written
  271. * @param __VALUE__ Value to be written in the register
  272. * @retval None
  273. */
  274. #define LL_SPI_WriteReg(__INSTANCE__, __REG__, __VALUE__) WRITE_REG(__INSTANCE__->__REG__, (__VALUE__))
  275. /**
  276. * @brief Read a value in SPI register
  277. * @param __INSTANCE__ SPI Instance
  278. * @param __REG__ Register to be read
  279. * @retval Register value
  280. */
  281. #define LL_SPI_ReadReg(__INSTANCE__, __REG__) READ_REG(__INSTANCE__->__REG__)
  282. /**
  283. * @}
  284. */
  285. /**
  286. * @}
  287. */
  288. /* Exported functions --------------------------------------------------------*/
  289. /** @defgroup SPI_LL_Exported_Functions SPI Exported Functions
  290. * @{
  291. */
  292. /** @defgroup SPI_LL_EF_Configuration Configuration
  293. * @{
  294. */
  295. /**
  296. * @brief Enable SPI peripheral
  297. * @rmtoll CR1 SPE LL_SPI_Enable
  298. * @param SPIx SPI Instance
  299. * @retval None
  300. */
  301. __STATIC_INLINE void LL_SPI_Enable(SPI_TypeDef *SPIx)
  302. {
  303. SET_BIT(SPIx->CR1, SPI_CR1_SPE);
  304. }
  305. /**
  306. * @brief Disable SPI peripheral
  307. * @note When disabling the SPI, follow the procedure described in the Reference Manual.
  308. * @rmtoll CR1 SPE LL_SPI_Disable
  309. * @param SPIx SPI Instance
  310. * @retval None
  311. */
  312. __STATIC_INLINE void LL_SPI_Disable(SPI_TypeDef *SPIx)
  313. {
  314. CLEAR_BIT(SPIx->CR1, SPI_CR1_SPE);
  315. }
  316. /**
  317. * @brief Check if SPI peripheral is enabled
  318. * @rmtoll CR1 SPE LL_SPI_IsEnabled
  319. * @param SPIx SPI Instance
  320. * @retval State of bit (1 or 0).
  321. */
  322. __STATIC_INLINE uint32_t LL_SPI_IsEnabled(SPI_TypeDef *SPIx)
  323. {
  324. return ((READ_BIT(SPIx->CR1, SPI_CR1_SPE) == (SPI_CR1_SPE)) ? 1UL : 0UL);
  325. }
  326. /**
  327. * @brief Set SPI operation mode to Master or Slave
  328. * @note This bit should not be changed when communication is ongoing.
  329. * @rmtoll CR1 MSTR LL_SPI_SetMode\n
  330. * CR1 SSI LL_SPI_SetMode
  331. * @param SPIx SPI Instance
  332. * @param Mode This parameter can be one of the following values:
  333. * @arg @ref LL_SPI_MODE_MASTER
  334. * @arg @ref LL_SPI_MODE_SLAVE
  335. * @retval None
  336. */
  337. __STATIC_INLINE void LL_SPI_SetMode(SPI_TypeDef *SPIx, uint32_t Mode)
  338. {
  339. MODIFY_REG(SPIx->CR1, SPI_CR1_MSTR | SPI_CR1_SSI, Mode);
  340. }
  341. /**
  342. * @brief Get SPI operation mode (Master or Slave)
  343. * @rmtoll CR1 MSTR LL_SPI_GetMode\n
  344. * CR1 SSI LL_SPI_GetMode
  345. * @param SPIx SPI Instance
  346. * @retval Returned value can be one of the following values:
  347. * @arg @ref LL_SPI_MODE_MASTER
  348. * @arg @ref LL_SPI_MODE_SLAVE
  349. */
  350. __STATIC_INLINE uint32_t LL_SPI_GetMode(SPI_TypeDef *SPIx)
  351. {
  352. return (uint32_t)(READ_BIT(SPIx->CR1, SPI_CR1_MSTR | SPI_CR1_SSI));
  353. }
  354. /**
  355. * @brief Set serial protocol used
  356. * @note This bit should be written only when SPI is disabled (SPE = 0) for correct operation.
  357. * @rmtoll CR2 FRF LL_SPI_SetStandard
  358. * @param SPIx SPI Instance
  359. * @param Standard This parameter can be one of the following values:
  360. * @arg @ref LL_SPI_PROTOCOL_MOTOROLA
  361. * @arg @ref LL_SPI_PROTOCOL_TI
  362. * @retval None
  363. */
  364. __STATIC_INLINE void LL_SPI_SetStandard(SPI_TypeDef *SPIx, uint32_t Standard)
  365. {
  366. MODIFY_REG(SPIx->CR2, SPI_CR2_FRF, Standard);
  367. }
  368. /**
  369. * @brief Get serial protocol used
  370. * @rmtoll CR2 FRF LL_SPI_GetStandard
  371. * @param SPIx SPI Instance
  372. * @retval Returned value can be one of the following values:
  373. * @arg @ref LL_SPI_PROTOCOL_MOTOROLA
  374. * @arg @ref LL_SPI_PROTOCOL_TI
  375. */
  376. __STATIC_INLINE uint32_t LL_SPI_GetStandard(SPI_TypeDef *SPIx)
  377. {
  378. return (uint32_t)(READ_BIT(SPIx->CR2, SPI_CR2_FRF));
  379. }
  380. /**
  381. * @brief Set clock phase
  382. * @note This bit should not be changed when communication is ongoing.
  383. * This bit is not used in SPI TI mode.
  384. * @rmtoll CR1 CPHA LL_SPI_SetClockPhase
  385. * @param SPIx SPI Instance
  386. * @param ClockPhase This parameter can be one of the following values:
  387. * @arg @ref LL_SPI_PHASE_1EDGE
  388. * @arg @ref LL_SPI_PHASE_2EDGE
  389. * @retval None
  390. */
  391. __STATIC_INLINE void LL_SPI_SetClockPhase(SPI_TypeDef *SPIx, uint32_t ClockPhase)
  392. {
  393. MODIFY_REG(SPIx->CR1, SPI_CR1_CPHA, ClockPhase);
  394. }
  395. /**
  396. * @brief Get clock phase
  397. * @rmtoll CR1 CPHA LL_SPI_GetClockPhase
  398. * @param SPIx SPI Instance
  399. * @retval Returned value can be one of the following values:
  400. * @arg @ref LL_SPI_PHASE_1EDGE
  401. * @arg @ref LL_SPI_PHASE_2EDGE
  402. */
  403. __STATIC_INLINE uint32_t LL_SPI_GetClockPhase(SPI_TypeDef *SPIx)
  404. {
  405. return (uint32_t)(READ_BIT(SPIx->CR1, SPI_CR1_CPHA));
  406. }
  407. /**
  408. * @brief Set clock polarity
  409. * @note This bit should not be changed when communication is ongoing.
  410. * This bit is not used in SPI TI mode.
  411. * @rmtoll CR1 CPOL LL_SPI_SetClockPolarity
  412. * @param SPIx SPI Instance
  413. * @param ClockPolarity This parameter can be one of the following values:
  414. * @arg @ref LL_SPI_POLARITY_LOW
  415. * @arg @ref LL_SPI_POLARITY_HIGH
  416. * @retval None
  417. */
  418. __STATIC_INLINE void LL_SPI_SetClockPolarity(SPI_TypeDef *SPIx, uint32_t ClockPolarity)
  419. {
  420. MODIFY_REG(SPIx->CR1, SPI_CR1_CPOL, ClockPolarity);
  421. }
  422. /**
  423. * @brief Get clock polarity
  424. * @rmtoll CR1 CPOL LL_SPI_GetClockPolarity
  425. * @param SPIx SPI Instance
  426. * @retval Returned value can be one of the following values:
  427. * @arg @ref LL_SPI_POLARITY_LOW
  428. * @arg @ref LL_SPI_POLARITY_HIGH
  429. */
  430. __STATIC_INLINE uint32_t LL_SPI_GetClockPolarity(SPI_TypeDef *SPIx)
  431. {
  432. return (uint32_t)(READ_BIT(SPIx->CR1, SPI_CR1_CPOL));
  433. }
  434. /**
  435. * @brief Set baud rate prescaler
  436. * @note These bits should not be changed when communication is ongoing. SPI BaudRate = fPCLK/Prescaler.
  437. * @rmtoll CR1 BR LL_SPI_SetBaudRatePrescaler
  438. * @param SPIx SPI Instance
  439. * @param BaudRate This parameter can be one of the following values:
  440. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV2
  441. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV4
  442. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV8
  443. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV16
  444. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV32
  445. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV64
  446. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV128
  447. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV256
  448. * @retval None
  449. */
  450. __STATIC_INLINE void LL_SPI_SetBaudRatePrescaler(SPI_TypeDef *SPIx, uint32_t BaudRate)
  451. {
  452. MODIFY_REG(SPIx->CR1, SPI_CR1_BR, BaudRate);
  453. }
  454. /**
  455. * @brief Get baud rate prescaler
  456. * @rmtoll CR1 BR LL_SPI_GetBaudRatePrescaler
  457. * @param SPIx SPI Instance
  458. * @retval Returned value can be one of the following values:
  459. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV2
  460. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV4
  461. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV8
  462. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV16
  463. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV32
  464. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV64
  465. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV128
  466. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV256
  467. */
  468. __STATIC_INLINE uint32_t LL_SPI_GetBaudRatePrescaler(SPI_TypeDef *SPIx)
  469. {
  470. return (uint32_t)(READ_BIT(SPIx->CR1, SPI_CR1_BR));
  471. }
  472. /**
  473. * @brief Set transfer bit order
  474. * @note This bit should not be changed when communication is ongoing. This bit is not used in SPI TI mode.
  475. * @rmtoll CR1 LSBFIRST LL_SPI_SetTransferBitOrder
  476. * @param SPIx SPI Instance
  477. * @param BitOrder This parameter can be one of the following values:
  478. * @arg @ref LL_SPI_LSB_FIRST
  479. * @arg @ref LL_SPI_MSB_FIRST
  480. * @retval None
  481. */
  482. __STATIC_INLINE void LL_SPI_SetTransferBitOrder(SPI_TypeDef *SPIx, uint32_t BitOrder)
  483. {
  484. MODIFY_REG(SPIx->CR1, SPI_CR1_LSBFIRST, BitOrder);
  485. }
  486. /**
  487. * @brief Get transfer bit order
  488. * @rmtoll CR1 LSBFIRST LL_SPI_GetTransferBitOrder
  489. * @param SPIx SPI Instance
  490. * @retval Returned value can be one of the following values:
  491. * @arg @ref LL_SPI_LSB_FIRST
  492. * @arg @ref LL_SPI_MSB_FIRST
  493. */
  494. __STATIC_INLINE uint32_t LL_SPI_GetTransferBitOrder(SPI_TypeDef *SPIx)
  495. {
  496. return (uint32_t)(READ_BIT(SPIx->CR1, SPI_CR1_LSBFIRST));
  497. }
  498. /**
  499. * @brief Set transfer direction mode
  500. * @note For Half-Duplex mode, Rx Direction is set by default.
  501. * In master mode, the MOSI pin is used and in slave mode, the MISO pin is used for Half-Duplex.
  502. * @rmtoll CR1 RXONLY LL_SPI_SetTransferDirection\n
  503. * CR1 BIDIMODE LL_SPI_SetTransferDirection\n
  504. * CR1 BIDIOE LL_SPI_SetTransferDirection
  505. * @param SPIx SPI Instance
  506. * @param TransferDirection This parameter can be one of the following values:
  507. * @arg @ref LL_SPI_FULL_DUPLEX
  508. * @arg @ref LL_SPI_SIMPLEX_RX
  509. * @arg @ref LL_SPI_HALF_DUPLEX_RX
  510. * @arg @ref LL_SPI_HALF_DUPLEX_TX
  511. * @retval None
  512. */
  513. __STATIC_INLINE void LL_SPI_SetTransferDirection(SPI_TypeDef *SPIx, uint32_t TransferDirection)
  514. {
  515. MODIFY_REG(SPIx->CR1, SPI_CR1_RXONLY | SPI_CR1_BIDIMODE | SPI_CR1_BIDIOE, TransferDirection);
  516. }
  517. /**
  518. * @brief Get transfer direction mode
  519. * @rmtoll CR1 RXONLY LL_SPI_GetTransferDirection\n
  520. * CR1 BIDIMODE LL_SPI_GetTransferDirection\n
  521. * CR1 BIDIOE LL_SPI_GetTransferDirection
  522. * @param SPIx SPI Instance
  523. * @retval Returned value can be one of the following values:
  524. * @arg @ref LL_SPI_FULL_DUPLEX
  525. * @arg @ref LL_SPI_SIMPLEX_RX
  526. * @arg @ref LL_SPI_HALF_DUPLEX_RX
  527. * @arg @ref LL_SPI_HALF_DUPLEX_TX
  528. */
  529. __STATIC_INLINE uint32_t LL_SPI_GetTransferDirection(SPI_TypeDef *SPIx)
  530. {
  531. return (uint32_t)(READ_BIT(SPIx->CR1, SPI_CR1_RXONLY | SPI_CR1_BIDIMODE | SPI_CR1_BIDIOE));
  532. }
  533. /**
  534. * @brief Set frame data width
  535. * @rmtoll CR2 DS LL_SPI_SetDataWidth
  536. * @param SPIx SPI Instance
  537. * @param DataWidth This parameter can be one of the following values:
  538. * @arg @ref LL_SPI_DATAWIDTH_4BIT
  539. * @arg @ref LL_SPI_DATAWIDTH_5BIT
  540. * @arg @ref LL_SPI_DATAWIDTH_6BIT
  541. * @arg @ref LL_SPI_DATAWIDTH_7BIT
  542. * @arg @ref LL_SPI_DATAWIDTH_8BIT
  543. * @arg @ref LL_SPI_DATAWIDTH_9BIT
  544. * @arg @ref LL_SPI_DATAWIDTH_10BIT
  545. * @arg @ref LL_SPI_DATAWIDTH_11BIT
  546. * @arg @ref LL_SPI_DATAWIDTH_12BIT
  547. * @arg @ref LL_SPI_DATAWIDTH_13BIT
  548. * @arg @ref LL_SPI_DATAWIDTH_14BIT
  549. * @arg @ref LL_SPI_DATAWIDTH_15BIT
  550. * @arg @ref LL_SPI_DATAWIDTH_16BIT
  551. * @retval None
  552. */
  553. __STATIC_INLINE void LL_SPI_SetDataWidth(SPI_TypeDef *SPIx, uint32_t DataWidth)
  554. {
  555. MODIFY_REG(SPIx->CR2, SPI_CR2_DS, DataWidth);
  556. }
  557. /**
  558. * @brief Get frame data width
  559. * @rmtoll CR2 DS LL_SPI_GetDataWidth
  560. * @param SPIx SPI Instance
  561. * @retval Returned value can be one of the following values:
  562. * @arg @ref LL_SPI_DATAWIDTH_4BIT
  563. * @arg @ref LL_SPI_DATAWIDTH_5BIT
  564. * @arg @ref LL_SPI_DATAWIDTH_6BIT
  565. * @arg @ref LL_SPI_DATAWIDTH_7BIT
  566. * @arg @ref LL_SPI_DATAWIDTH_8BIT
  567. * @arg @ref LL_SPI_DATAWIDTH_9BIT
  568. * @arg @ref LL_SPI_DATAWIDTH_10BIT
  569. * @arg @ref LL_SPI_DATAWIDTH_11BIT
  570. * @arg @ref LL_SPI_DATAWIDTH_12BIT
  571. * @arg @ref LL_SPI_DATAWIDTH_13BIT
  572. * @arg @ref LL_SPI_DATAWIDTH_14BIT
  573. * @arg @ref LL_SPI_DATAWIDTH_15BIT
  574. * @arg @ref LL_SPI_DATAWIDTH_16BIT
  575. */
  576. __STATIC_INLINE uint32_t LL_SPI_GetDataWidth(SPI_TypeDef *SPIx)
  577. {
  578. return (uint32_t)(READ_BIT(SPIx->CR2, SPI_CR2_DS));
  579. }
  580. /**
  581. * @brief Set threshold of RXFIFO that triggers an RXNE event
  582. * @rmtoll CR2 FRXTH LL_SPI_SetRxFIFOThreshold
  583. * @param SPIx SPI Instance
  584. * @param Threshold This parameter can be one of the following values:
  585. * @arg @ref LL_SPI_RX_FIFO_TH_HALF
  586. * @arg @ref LL_SPI_RX_FIFO_TH_QUARTER
  587. * @retval None
  588. */
  589. __STATIC_INLINE void LL_SPI_SetRxFIFOThreshold(SPI_TypeDef *SPIx, uint32_t Threshold)
  590. {
  591. MODIFY_REG(SPIx->CR2, SPI_CR2_FRXTH, Threshold);
  592. }
  593. /**
  594. * @brief Get threshold of RXFIFO that triggers an RXNE event
  595. * @rmtoll CR2 FRXTH LL_SPI_GetRxFIFOThreshold
  596. * @param SPIx SPI Instance
  597. * @retval Returned value can be one of the following values:
  598. * @arg @ref LL_SPI_RX_FIFO_TH_HALF
  599. * @arg @ref LL_SPI_RX_FIFO_TH_QUARTER
  600. */
  601. __STATIC_INLINE uint32_t LL_SPI_GetRxFIFOThreshold(SPI_TypeDef *SPIx)
  602. {
  603. return (uint32_t)(READ_BIT(SPIx->CR2, SPI_CR2_FRXTH));
  604. }
  605. /**
  606. * @}
  607. */
  608. /** @defgroup SPI_LL_EF_CRC_Management CRC Management
  609. * @{
  610. */
  611. /**
  612. * @brief Enable CRC
  613. * @note This bit should be written only when SPI is disabled (SPE = 0) for correct operation.
  614. * @rmtoll CR1 CRCEN LL_SPI_EnableCRC
  615. * @param SPIx SPI Instance
  616. * @retval None
  617. */
  618. __STATIC_INLINE void LL_SPI_EnableCRC(SPI_TypeDef *SPIx)
  619. {
  620. SET_BIT(SPIx->CR1, SPI_CR1_CRCEN);
  621. }
  622. /**
  623. * @brief Disable CRC
  624. * @note This bit should be written only when SPI is disabled (SPE = 0) for correct operation.
  625. * @rmtoll CR1 CRCEN LL_SPI_DisableCRC
  626. * @param SPIx SPI Instance
  627. * @retval None
  628. */
  629. __STATIC_INLINE void LL_SPI_DisableCRC(SPI_TypeDef *SPIx)
  630. {
  631. CLEAR_BIT(SPIx->CR1, SPI_CR1_CRCEN);
  632. }
  633. /**
  634. * @brief Check if CRC is enabled
  635. * @note This bit should be written only when SPI is disabled (SPE = 0) for correct operation.
  636. * @rmtoll CR1 CRCEN LL_SPI_IsEnabledCRC
  637. * @param SPIx SPI Instance
  638. * @retval State of bit (1 or 0).
  639. */
  640. __STATIC_INLINE uint32_t LL_SPI_IsEnabledCRC(SPI_TypeDef *SPIx)
  641. {
  642. return ((READ_BIT(SPIx->CR1, SPI_CR1_CRCEN) == (SPI_CR1_CRCEN)) ? 1UL : 0UL);
  643. }
  644. /**
  645. * @brief Set CRC Length
  646. * @note This bit should be written only when SPI is disabled (SPE = 0) for correct operation.
  647. * @rmtoll CR1 CRCL LL_SPI_SetCRCWidth
  648. * @param SPIx SPI Instance
  649. * @param CRCLength This parameter can be one of the following values:
  650. * @arg @ref LL_SPI_CRC_8BIT
  651. * @arg @ref LL_SPI_CRC_16BIT
  652. * @retval None
  653. */
  654. __STATIC_INLINE void LL_SPI_SetCRCWidth(SPI_TypeDef *SPIx, uint32_t CRCLength)
  655. {
  656. MODIFY_REG(SPIx->CR1, SPI_CR1_CRCL, CRCLength);
  657. }
  658. /**
  659. * @brief Get CRC Length
  660. * @rmtoll CR1 CRCL LL_SPI_GetCRCWidth
  661. * @param SPIx SPI Instance
  662. * @retval Returned value can be one of the following values:
  663. * @arg @ref LL_SPI_CRC_8BIT
  664. * @arg @ref LL_SPI_CRC_16BIT
  665. */
  666. __STATIC_INLINE uint32_t LL_SPI_GetCRCWidth(SPI_TypeDef *SPIx)
  667. {
  668. return (uint32_t)(READ_BIT(SPIx->CR1, SPI_CR1_CRCL));
  669. }
  670. /**
  671. * @brief Set CRCNext to transfer CRC on the line
  672. * @note This bit has to be written as soon as the last data is written in the SPIx_DR register.
  673. * @rmtoll CR1 CRCNEXT LL_SPI_SetCRCNext
  674. * @param SPIx SPI Instance
  675. * @retval None
  676. */
  677. __STATIC_INLINE void LL_SPI_SetCRCNext(SPI_TypeDef *SPIx)
  678. {
  679. SET_BIT(SPIx->CR1, SPI_CR1_CRCNEXT);
  680. }
  681. /**
  682. * @brief Set polynomial for CRC calculation
  683. * @rmtoll CRCPR CRCPOLY LL_SPI_SetCRCPolynomial
  684. * @param SPIx SPI Instance
  685. * @param CRCPoly This parameter must be a number between Min_Data = 0x00 and Max_Data = 0xFFFF
  686. * @retval None
  687. */
  688. __STATIC_INLINE void LL_SPI_SetCRCPolynomial(SPI_TypeDef *SPIx, uint32_t CRCPoly)
  689. {
  690. WRITE_REG(SPIx->CRCPR, (uint16_t)CRCPoly);
  691. }
  692. /**
  693. * @brief Get polynomial for CRC calculation
  694. * @rmtoll CRCPR CRCPOLY LL_SPI_GetCRCPolynomial
  695. * @param SPIx SPI Instance
  696. * @retval Returned value is a number between Min_Data = 0x00 and Max_Data = 0xFFFF
  697. */
  698. __STATIC_INLINE uint32_t LL_SPI_GetCRCPolynomial(SPI_TypeDef *SPIx)
  699. {
  700. return (uint32_t)(READ_REG(SPIx->CRCPR));
  701. }
  702. /**
  703. * @brief Get Rx CRC
  704. * @rmtoll RXCRCR RXCRC LL_SPI_GetRxCRC
  705. * @param SPIx SPI Instance
  706. * @retval Returned value is a number between Min_Data = 0x00 and Max_Data = 0xFFFF
  707. */
  708. __STATIC_INLINE uint32_t LL_SPI_GetRxCRC(SPI_TypeDef *SPIx)
  709. {
  710. return (uint32_t)(READ_REG(SPIx->RXCRCR));
  711. }
  712. /**
  713. * @brief Get Tx CRC
  714. * @rmtoll TXCRCR TXCRC LL_SPI_GetTxCRC
  715. * @param SPIx SPI Instance
  716. * @retval Returned value is a number between Min_Data = 0x00 and Max_Data = 0xFFFF
  717. */
  718. __STATIC_INLINE uint32_t LL_SPI_GetTxCRC(SPI_TypeDef *SPIx)
  719. {
  720. return (uint32_t)(READ_REG(SPIx->TXCRCR));
  721. }
  722. /**
  723. * @}
  724. */
  725. /** @defgroup SPI_LL_EF_NSS_Management Slave Select Pin Management
  726. * @{
  727. */
  728. /**
  729. * @brief Set NSS mode
  730. * @note LL_SPI_NSS_SOFT Mode is not used in SPI TI mode.
  731. * @rmtoll CR1 SSM LL_SPI_SetNSSMode\n
  732. * @rmtoll CR2 SSOE LL_SPI_SetNSSMode
  733. * @param SPIx SPI Instance
  734. * @param NSS This parameter can be one of the following values:
  735. * @arg @ref LL_SPI_NSS_SOFT
  736. * @arg @ref LL_SPI_NSS_HARD_INPUT
  737. * @arg @ref LL_SPI_NSS_HARD_OUTPUT
  738. * @retval None
  739. */
  740. __STATIC_INLINE void LL_SPI_SetNSSMode(SPI_TypeDef *SPIx, uint32_t NSS)
  741. {
  742. MODIFY_REG(SPIx->CR1, SPI_CR1_SSM, NSS);
  743. MODIFY_REG(SPIx->CR2, SPI_CR2_SSOE, ((uint32_t)(NSS >> 16U)));
  744. }
  745. /**
  746. * @brief Get NSS mode
  747. * @rmtoll CR1 SSM LL_SPI_GetNSSMode\n
  748. * @rmtoll CR2 SSOE LL_SPI_GetNSSMode
  749. * @param SPIx SPI Instance
  750. * @retval Returned value can be one of the following values:
  751. * @arg @ref LL_SPI_NSS_SOFT
  752. * @arg @ref LL_SPI_NSS_HARD_INPUT
  753. * @arg @ref LL_SPI_NSS_HARD_OUTPUT
  754. */
  755. __STATIC_INLINE uint32_t LL_SPI_GetNSSMode(SPI_TypeDef *SPIx)
  756. {
  757. uint32_t Ssm = (READ_BIT(SPIx->CR1, SPI_CR1_SSM));
  758. uint32_t Ssoe = (READ_BIT(SPIx->CR2, SPI_CR2_SSOE) << 16U);
  759. return (Ssm | Ssoe);
  760. }
  761. /**
  762. * @brief Enable NSS pulse management
  763. * @note This bit should not be changed when communication is ongoing. This bit is not used in SPI TI mode.
  764. * @rmtoll CR2 NSSP LL_SPI_EnableNSSPulseMgt
  765. * @param SPIx SPI Instance
  766. * @retval None
  767. */
  768. __STATIC_INLINE void LL_SPI_EnableNSSPulseMgt(SPI_TypeDef *SPIx)
  769. {
  770. SET_BIT(SPIx->CR2, SPI_CR2_NSSP);
  771. }
  772. /**
  773. * @brief Disable NSS pulse management
  774. * @note This bit should not be changed when communication is ongoing. This bit is not used in SPI TI mode.
  775. * @rmtoll CR2 NSSP LL_SPI_DisableNSSPulseMgt
  776. * @param SPIx SPI Instance
  777. * @retval None
  778. */
  779. __STATIC_INLINE void LL_SPI_DisableNSSPulseMgt(SPI_TypeDef *SPIx)
  780. {
  781. CLEAR_BIT(SPIx->CR2, SPI_CR2_NSSP);
  782. }
  783. /**
  784. * @brief Check if NSS pulse is enabled
  785. * @note This bit should not be changed when communication is ongoing. This bit is not used in SPI TI mode.
  786. * @rmtoll CR2 NSSP LL_SPI_IsEnabledNSSPulse
  787. * @param SPIx SPI Instance
  788. * @retval State of bit (1 or 0).
  789. */
  790. __STATIC_INLINE uint32_t LL_SPI_IsEnabledNSSPulse(SPI_TypeDef *SPIx)
  791. {
  792. return ((READ_BIT(SPIx->CR2, SPI_CR2_NSSP) == (SPI_CR2_NSSP)) ? 1UL : 0UL);
  793. }
  794. /**
  795. * @}
  796. */
  797. /** @defgroup SPI_LL_EF_FLAG_Management FLAG Management
  798. * @{
  799. */
  800. /**
  801. * @brief Check if Rx buffer is not empty
  802. * @rmtoll SR RXNE LL_SPI_IsActiveFlag_RXNE
  803. * @param SPIx SPI Instance
  804. * @retval State of bit (1 or 0).
  805. */
  806. __STATIC_INLINE uint32_t LL_SPI_IsActiveFlag_RXNE(SPI_TypeDef *SPIx)
  807. {
  808. return ((READ_BIT(SPIx->SR, SPI_SR_RXNE) == (SPI_SR_RXNE)) ? 1UL : 0UL);
  809. }
  810. /**
  811. * @brief Check if Tx buffer is empty
  812. * @rmtoll SR TXE LL_SPI_IsActiveFlag_TXE
  813. * @param SPIx SPI Instance
  814. * @retval State of bit (1 or 0).
  815. */
  816. __STATIC_INLINE uint32_t LL_SPI_IsActiveFlag_TXE(SPI_TypeDef *SPIx)
  817. {
  818. return ((READ_BIT(SPIx->SR, SPI_SR_TXE) == (SPI_SR_TXE)) ? 1UL : 0UL);
  819. }
  820. /**
  821. * @brief Get CRC error flag
  822. * @rmtoll SR CRCERR LL_SPI_IsActiveFlag_CRCERR
  823. * @param SPIx SPI Instance
  824. * @retval State of bit (1 or 0).
  825. */
  826. __STATIC_INLINE uint32_t LL_SPI_IsActiveFlag_CRCERR(SPI_TypeDef *SPIx)
  827. {
  828. return ((READ_BIT(SPIx->SR, SPI_SR_CRCERR) == (SPI_SR_CRCERR)) ? 1UL : 0UL);
  829. }
  830. /**
  831. * @brief Get mode fault error flag
  832. * @rmtoll SR MODF LL_SPI_IsActiveFlag_MODF
  833. * @param SPIx SPI Instance
  834. * @retval State of bit (1 or 0).
  835. */
  836. __STATIC_INLINE uint32_t LL_SPI_IsActiveFlag_MODF(SPI_TypeDef *SPIx)
  837. {
  838. return ((READ_BIT(SPIx->SR, SPI_SR_MODF) == (SPI_SR_MODF)) ? 1UL : 0UL);
  839. }
  840. /**
  841. * @brief Get overrun error flag
  842. * @rmtoll SR OVR LL_SPI_IsActiveFlag_OVR
  843. * @param SPIx SPI Instance
  844. * @retval State of bit (1 or 0).
  845. */
  846. __STATIC_INLINE uint32_t LL_SPI_IsActiveFlag_OVR(SPI_TypeDef *SPIx)
  847. {
  848. return ((READ_BIT(SPIx->SR, SPI_SR_OVR) == (SPI_SR_OVR)) ? 1UL : 0UL);
  849. }
  850. /**
  851. * @brief Get busy flag
  852. * @note The BSY flag is cleared under any one of the following conditions:
  853. * -When the SPI is correctly disabled
  854. * -When a fault is detected in Master mode (MODF bit set to 1)
  855. * -In Master mode, when it finishes a data transmission and no new data is ready to be
  856. * sent
  857. * -In Slave mode, when the BSY flag is set to '0' for at least one SPI clock cycle between
  858. * each data transfer.
  859. * @rmtoll SR BSY LL_SPI_IsActiveFlag_BSY
  860. * @param SPIx SPI Instance
  861. * @retval State of bit (1 or 0).
  862. */
  863. __STATIC_INLINE uint32_t LL_SPI_IsActiveFlag_BSY(SPI_TypeDef *SPIx)
  864. {
  865. return ((READ_BIT(SPIx->SR, SPI_SR_BSY) == (SPI_SR_BSY)) ? 1UL : 0UL);
  866. }
  867. /**
  868. * @brief Get frame format error flag
  869. * @rmtoll SR FRE LL_SPI_IsActiveFlag_FRE
  870. * @param SPIx SPI Instance
  871. * @retval State of bit (1 or 0).
  872. */
  873. __STATIC_INLINE uint32_t LL_SPI_IsActiveFlag_FRE(SPI_TypeDef *SPIx)
  874. {
  875. return ((READ_BIT(SPIx->SR, SPI_SR_FRE) == (SPI_SR_FRE)) ? 1UL : 0UL);
  876. }
  877. /**
  878. * @brief Get FIFO reception Level
  879. * @rmtoll SR FRLVL LL_SPI_GetRxFIFOLevel
  880. * @param SPIx SPI Instance
  881. * @retval Returned value can be one of the following values:
  882. * @arg @ref LL_SPI_RX_FIFO_EMPTY
  883. * @arg @ref LL_SPI_RX_FIFO_QUARTER_FULL
  884. * @arg @ref LL_SPI_RX_FIFO_HALF_FULL
  885. * @arg @ref LL_SPI_RX_FIFO_FULL
  886. */
  887. __STATIC_INLINE uint32_t LL_SPI_GetRxFIFOLevel(SPI_TypeDef *SPIx)
  888. {
  889. return (uint32_t)(READ_BIT(SPIx->SR, SPI_SR_FRLVL));
  890. }
  891. /**
  892. * @brief Get FIFO Transmission Level
  893. * @rmtoll SR FTLVL LL_SPI_GetTxFIFOLevel
  894. * @param SPIx SPI Instance
  895. * @retval Returned value can be one of the following values:
  896. * @arg @ref LL_SPI_TX_FIFO_EMPTY
  897. * @arg @ref LL_SPI_TX_FIFO_QUARTER_FULL
  898. * @arg @ref LL_SPI_TX_FIFO_HALF_FULL
  899. * @arg @ref LL_SPI_TX_FIFO_FULL
  900. */
  901. __STATIC_INLINE uint32_t LL_SPI_GetTxFIFOLevel(SPI_TypeDef *SPIx)
  902. {
  903. return (uint32_t)(READ_BIT(SPIx->SR, SPI_SR_FTLVL));
  904. }
  905. /**
  906. * @brief Clear CRC error flag
  907. * @rmtoll SR CRCERR LL_SPI_ClearFlag_CRCERR
  908. * @param SPIx SPI Instance
  909. * @retval None
  910. */
  911. __STATIC_INLINE void LL_SPI_ClearFlag_CRCERR(SPI_TypeDef *SPIx)
  912. {
  913. CLEAR_BIT(SPIx->SR, SPI_SR_CRCERR);
  914. }
  915. /**
  916. * @brief Clear mode fault error flag
  917. * @note Clearing this flag is done by a read access to the SPIx_SR
  918. * register followed by a write access to the SPIx_CR1 register
  919. * @rmtoll SR MODF LL_SPI_ClearFlag_MODF
  920. * @param SPIx SPI Instance
  921. * @retval None
  922. */
  923. __STATIC_INLINE void LL_SPI_ClearFlag_MODF(SPI_TypeDef *SPIx)
  924. {
  925. __IO uint32_t tmpreg_sr;
  926. tmpreg_sr = SPIx->SR;
  927. (void) tmpreg_sr;
  928. CLEAR_BIT(SPIx->CR1, SPI_CR1_SPE);
  929. }
  930. /**
  931. * @brief Clear overrun error flag
  932. * @note Clearing this flag is done by a read access to the SPIx_DR
  933. * register followed by a read access to the SPIx_SR register
  934. * @rmtoll SR OVR LL_SPI_ClearFlag_OVR
  935. * @param SPIx SPI Instance
  936. * @retval None
  937. */
  938. __STATIC_INLINE void LL_SPI_ClearFlag_OVR(SPI_TypeDef *SPIx)
  939. {
  940. __IO uint32_t tmpreg;
  941. tmpreg = SPIx->DR;
  942. (void) tmpreg;
  943. tmpreg = SPIx->SR;
  944. (void) tmpreg;
  945. }
  946. /**
  947. * @brief Clear frame format error flag
  948. * @note Clearing this flag is done by reading SPIx_SR register
  949. * @rmtoll SR FRE LL_SPI_ClearFlag_FRE
  950. * @param SPIx SPI Instance
  951. * @retval None
  952. */
  953. __STATIC_INLINE void LL_SPI_ClearFlag_FRE(SPI_TypeDef *SPIx)
  954. {
  955. __IO uint32_t tmpreg;
  956. tmpreg = SPIx->SR;
  957. (void) tmpreg;
  958. }
  959. /**
  960. * @}
  961. */
  962. /** @defgroup SPI_LL_EF_IT_Management Interrupt Management
  963. * @{
  964. */
  965. /**
  966. * @brief Enable error interrupt
  967. * @note This bit controls the generation of an interrupt when an error condition occurs (CRCERR, OVR, MODF in SPI mode, FRE at TI mode).
  968. * @rmtoll CR2 ERRIE LL_SPI_EnableIT_ERR
  969. * @param SPIx SPI Instance
  970. * @retval None
  971. */
  972. __STATIC_INLINE void LL_SPI_EnableIT_ERR(SPI_TypeDef *SPIx)
  973. {
  974. SET_BIT(SPIx->CR2, SPI_CR2_ERRIE);
  975. }
  976. /**
  977. * @brief Enable Rx buffer not empty interrupt
  978. * @rmtoll CR2 RXNEIE LL_SPI_EnableIT_RXNE
  979. * @param SPIx SPI Instance
  980. * @retval None
  981. */
  982. __STATIC_INLINE void LL_SPI_EnableIT_RXNE(SPI_TypeDef *SPIx)
  983. {
  984. SET_BIT(SPIx->CR2, SPI_CR2_RXNEIE);
  985. }
  986. /**
  987. * @brief Enable Tx buffer empty interrupt
  988. * @rmtoll CR2 TXEIE LL_SPI_EnableIT_TXE
  989. * @param SPIx SPI Instance
  990. * @retval None
  991. */
  992. __STATIC_INLINE void LL_SPI_EnableIT_TXE(SPI_TypeDef *SPIx)
  993. {
  994. SET_BIT(SPIx->CR2, SPI_CR2_TXEIE);
  995. }
  996. /**
  997. * @brief Disable error interrupt
  998. * @note This bit controls the generation of an interrupt when an error condition occurs (CRCERR, OVR, MODF in SPI mode, FRE at TI mode).
  999. * @rmtoll CR2 ERRIE LL_SPI_DisableIT_ERR
  1000. * @param SPIx SPI Instance
  1001. * @retval None
  1002. */
  1003. __STATIC_INLINE void LL_SPI_DisableIT_ERR(SPI_TypeDef *SPIx)
  1004. {
  1005. CLEAR_BIT(SPIx->CR2, SPI_CR2_ERRIE);
  1006. }
  1007. /**
  1008. * @brief Disable Rx buffer not empty interrupt
  1009. * @rmtoll CR2 RXNEIE LL_SPI_DisableIT_RXNE
  1010. * @param SPIx SPI Instance
  1011. * @retval None
  1012. */
  1013. __STATIC_INLINE void LL_SPI_DisableIT_RXNE(SPI_TypeDef *SPIx)
  1014. {
  1015. CLEAR_BIT(SPIx->CR2, SPI_CR2_RXNEIE);
  1016. }
  1017. /**
  1018. * @brief Disable Tx buffer empty interrupt
  1019. * @rmtoll CR2 TXEIE LL_SPI_DisableIT_TXE
  1020. * @param SPIx SPI Instance
  1021. * @retval None
  1022. */
  1023. __STATIC_INLINE void LL_SPI_DisableIT_TXE(SPI_TypeDef *SPIx)
  1024. {
  1025. CLEAR_BIT(SPIx->CR2, SPI_CR2_TXEIE);
  1026. }
  1027. /**
  1028. * @brief Check if error interrupt is enabled
  1029. * @rmtoll CR2 ERRIE LL_SPI_IsEnabledIT_ERR
  1030. * @param SPIx SPI Instance
  1031. * @retval State of bit (1 or 0).
  1032. */
  1033. __STATIC_INLINE uint32_t LL_SPI_IsEnabledIT_ERR(SPI_TypeDef *SPIx)
  1034. {
  1035. return ((READ_BIT(SPIx->CR2, SPI_CR2_ERRIE) == (SPI_CR2_ERRIE)) ? 1UL : 0UL);
  1036. }
  1037. /**
  1038. * @brief Check if Rx buffer not empty interrupt is enabled
  1039. * @rmtoll CR2 RXNEIE LL_SPI_IsEnabledIT_RXNE
  1040. * @param SPIx SPI Instance
  1041. * @retval State of bit (1 or 0).
  1042. */
  1043. __STATIC_INLINE uint32_t LL_SPI_IsEnabledIT_RXNE(SPI_TypeDef *SPIx)
  1044. {
  1045. return ((READ_BIT(SPIx->CR2, SPI_CR2_RXNEIE) == (SPI_CR2_RXNEIE)) ? 1UL : 0UL);
  1046. }
  1047. /**
  1048. * @brief Check if Tx buffer empty interrupt
  1049. * @rmtoll CR2 TXEIE LL_SPI_IsEnabledIT_TXE
  1050. * @param SPIx SPI Instance
  1051. * @retval State of bit (1 or 0).
  1052. */
  1053. __STATIC_INLINE uint32_t LL_SPI_IsEnabledIT_TXE(SPI_TypeDef *SPIx)
  1054. {
  1055. return ((READ_BIT(SPIx->CR2, SPI_CR2_TXEIE) == (SPI_CR2_TXEIE)) ? 1UL : 0UL);
  1056. }
  1057. /**
  1058. * @}
  1059. */
  1060. /** @defgroup SPI_LL_EF_DMA_Management DMA Management
  1061. * @{
  1062. */
  1063. /**
  1064. * @brief Enable DMA Rx
  1065. * @rmtoll CR2 RXDMAEN LL_SPI_EnableDMAReq_RX
  1066. * @param SPIx SPI Instance
  1067. * @retval None
  1068. */
  1069. __STATIC_INLINE void LL_SPI_EnableDMAReq_RX(SPI_TypeDef *SPIx)
  1070. {
  1071. SET_BIT(SPIx->CR2, SPI_CR2_RXDMAEN);
  1072. }
  1073. /**
  1074. * @brief Disable DMA Rx
  1075. * @rmtoll CR2 RXDMAEN LL_SPI_DisableDMAReq_RX
  1076. * @param SPIx SPI Instance
  1077. * @retval None
  1078. */
  1079. __STATIC_INLINE void LL_SPI_DisableDMAReq_RX(SPI_TypeDef *SPIx)
  1080. {
  1081. CLEAR_BIT(SPIx->CR2, SPI_CR2_RXDMAEN);
  1082. }
  1083. /**
  1084. * @brief Check if DMA Rx is enabled
  1085. * @rmtoll CR2 RXDMAEN LL_SPI_IsEnabledDMAReq_RX
  1086. * @param SPIx SPI Instance
  1087. * @retval State of bit (1 or 0).
  1088. */
  1089. __STATIC_INLINE uint32_t LL_SPI_IsEnabledDMAReq_RX(SPI_TypeDef *SPIx)
  1090. {
  1091. return ((READ_BIT(SPIx->CR2, SPI_CR2_RXDMAEN) == (SPI_CR2_RXDMAEN)) ? 1UL : 0UL);
  1092. }
  1093. /**
  1094. * @brief Enable DMA Tx
  1095. * @rmtoll CR2 TXDMAEN LL_SPI_EnableDMAReq_TX
  1096. * @param SPIx SPI Instance
  1097. * @retval None
  1098. */
  1099. __STATIC_INLINE void LL_SPI_EnableDMAReq_TX(SPI_TypeDef *SPIx)
  1100. {
  1101. SET_BIT(SPIx->CR2, SPI_CR2_TXDMAEN);
  1102. }
  1103. /**
  1104. * @brief Disable DMA Tx
  1105. * @rmtoll CR2 TXDMAEN LL_SPI_DisableDMAReq_TX
  1106. * @param SPIx SPI Instance
  1107. * @retval None
  1108. */
  1109. __STATIC_INLINE void LL_SPI_DisableDMAReq_TX(SPI_TypeDef *SPIx)
  1110. {
  1111. CLEAR_BIT(SPIx->CR2, SPI_CR2_TXDMAEN);
  1112. }
  1113. /**
  1114. * @brief Check if DMA Tx is enabled
  1115. * @rmtoll CR2 TXDMAEN LL_SPI_IsEnabledDMAReq_TX
  1116. * @param SPIx SPI Instance
  1117. * @retval State of bit (1 or 0).
  1118. */
  1119. __STATIC_INLINE uint32_t LL_SPI_IsEnabledDMAReq_TX(SPI_TypeDef *SPIx)
  1120. {
  1121. return ((READ_BIT(SPIx->CR2, SPI_CR2_TXDMAEN) == (SPI_CR2_TXDMAEN)) ? 1UL : 0UL);
  1122. }
  1123. /**
  1124. * @brief Set parity of Last DMA reception
  1125. * @rmtoll CR2 LDMARX LL_SPI_SetDMAParity_RX
  1126. * @param SPIx SPI Instance
  1127. * @param Parity This parameter can be one of the following values:
  1128. * @arg @ref LL_SPI_DMA_PARITY_ODD
  1129. * @arg @ref LL_SPI_DMA_PARITY_EVEN
  1130. * @retval None
  1131. */
  1132. __STATIC_INLINE void LL_SPI_SetDMAParity_RX(SPI_TypeDef *SPIx, uint32_t Parity)
  1133. {
  1134. MODIFY_REG(SPIx->CR2, SPI_CR2_LDMARX, (Parity << SPI_CR2_LDMARX_Pos));
  1135. }
  1136. /**
  1137. * @brief Get parity configuration for Last DMA reception
  1138. * @rmtoll CR2 LDMARX LL_SPI_GetDMAParity_RX
  1139. * @param SPIx SPI Instance
  1140. * @retval Returned value can be one of the following values:
  1141. * @arg @ref LL_SPI_DMA_PARITY_ODD
  1142. * @arg @ref LL_SPI_DMA_PARITY_EVEN
  1143. */
  1144. __STATIC_INLINE uint32_t LL_SPI_GetDMAParity_RX(SPI_TypeDef *SPIx)
  1145. {
  1146. return (uint32_t)(READ_BIT(SPIx->CR2, SPI_CR2_LDMARX) >> SPI_CR2_LDMARX_Pos);
  1147. }
  1148. /**
  1149. * @brief Set parity of Last DMA transmission
  1150. * @rmtoll CR2 LDMATX LL_SPI_SetDMAParity_TX
  1151. * @param SPIx SPI Instance
  1152. * @param Parity This parameter can be one of the following values:
  1153. * @arg @ref LL_SPI_DMA_PARITY_ODD
  1154. * @arg @ref LL_SPI_DMA_PARITY_EVEN
  1155. * @retval None
  1156. */
  1157. __STATIC_INLINE void LL_SPI_SetDMAParity_TX(SPI_TypeDef *SPIx, uint32_t Parity)
  1158. {
  1159. MODIFY_REG(SPIx->CR2, SPI_CR2_LDMATX, (Parity << SPI_CR2_LDMATX_Pos));
  1160. }
  1161. /**
  1162. * @brief Get parity configuration for Last DMA transmission
  1163. * @rmtoll CR2 LDMATX LL_SPI_GetDMAParity_TX
  1164. * @param SPIx SPI Instance
  1165. * @retval Returned value can be one of the following values:
  1166. * @arg @ref LL_SPI_DMA_PARITY_ODD
  1167. * @arg @ref LL_SPI_DMA_PARITY_EVEN
  1168. */
  1169. __STATIC_INLINE uint32_t LL_SPI_GetDMAParity_TX(SPI_TypeDef *SPIx)
  1170. {
  1171. return (uint32_t)(READ_BIT(SPIx->CR2, SPI_CR2_LDMATX) >> SPI_CR2_LDMATX_Pos);
  1172. }
  1173. /**
  1174. * @brief Get the data register address used for DMA transfer
  1175. * @rmtoll DR DR LL_SPI_DMA_GetRegAddr
  1176. * @param SPIx SPI Instance
  1177. * @retval Address of data register
  1178. */
  1179. __STATIC_INLINE uint32_t LL_SPI_DMA_GetRegAddr(SPI_TypeDef *SPIx)
  1180. {
  1181. return (uint32_t) &(SPIx->DR);
  1182. }
  1183. /**
  1184. * @}
  1185. */
  1186. /** @defgroup SPI_LL_EF_DATA_Management DATA Management
  1187. * @{
  1188. */
  1189. /**
  1190. * @brief Read 8-Bits in the data register
  1191. * @rmtoll DR DR LL_SPI_ReceiveData8
  1192. * @param SPIx SPI Instance
  1193. * @retval RxData Value between Min_Data=0x00 and Max_Data=0xFF
  1194. */
  1195. __STATIC_INLINE uint8_t LL_SPI_ReceiveData8(SPI_TypeDef *SPIx)
  1196. {
  1197. return (uint8_t)(READ_REG(SPIx->DR));
  1198. }
  1199. /**
  1200. * @brief Read 16-Bits in the data register
  1201. * @rmtoll DR DR LL_SPI_ReceiveData16
  1202. * @param SPIx SPI Instance
  1203. * @retval RxData Value between Min_Data=0x00 and Max_Data=0xFFFF
  1204. */
  1205. __STATIC_INLINE uint16_t LL_SPI_ReceiveData16(SPI_TypeDef *SPIx)
  1206. {
  1207. return (uint16_t)(READ_REG(SPIx->DR));
  1208. }
  1209. /**
  1210. * @brief Write 8-Bits in the data register
  1211. * @rmtoll DR DR LL_SPI_TransmitData8
  1212. * @param SPIx SPI Instance
  1213. * @param TxData Value between Min_Data=0x00 and Max_Data=0xFF
  1214. * @retval None
  1215. */
  1216. __STATIC_INLINE void LL_SPI_TransmitData8(SPI_TypeDef *SPIx, uint8_t TxData)
  1217. {
  1218. #if defined (__GNUC__)
  1219. __IO uint8_t *spidr = ((__IO uint8_t *)&SPIx->DR);
  1220. *spidr = TxData;
  1221. #else
  1222. *((__IO uint8_t *)&SPIx->DR) = TxData;
  1223. #endif /* __GNUC__ */
  1224. }
  1225. /**
  1226. * @brief Write 16-Bits in the data register
  1227. * @rmtoll DR DR LL_SPI_TransmitData16
  1228. * @param SPIx SPI Instance
  1229. * @param TxData Value between Min_Data=0x00 and Max_Data=0xFFFF
  1230. * @retval None
  1231. */
  1232. __STATIC_INLINE void LL_SPI_TransmitData16(SPI_TypeDef *SPIx, uint16_t TxData)
  1233. {
  1234. #if defined (__GNUC__)
  1235. __IO uint16_t *spidr = ((__IO uint16_t *)&SPIx->DR);
  1236. *spidr = TxData;
  1237. #else
  1238. SPIx->DR = TxData;
  1239. #endif /* __GNUC__ */
  1240. }
  1241. /**
  1242. * @}
  1243. */
  1244. #if defined(USE_FULL_LL_DRIVER)
  1245. /** @defgroup SPI_LL_EF_Init Initialization and de-initialization functions
  1246. * @{
  1247. */
  1248. ErrorStatus LL_SPI_DeInit(SPI_TypeDef *SPIx);
  1249. ErrorStatus LL_SPI_Init(SPI_TypeDef *SPIx, LL_SPI_InitTypeDef *SPI_InitStruct);
  1250. void LL_SPI_StructInit(LL_SPI_InitTypeDef *SPI_InitStruct);
  1251. /**
  1252. * @}
  1253. */
  1254. #endif /* USE_FULL_LL_DRIVER */
  1255. /**
  1256. * @}
  1257. */
  1258. /**
  1259. * @}
  1260. */
  1261. #endif /* defined (SPI1) || defined (SPI2) */
  1262. /**
  1263. * @}
  1264. */
  1265. #ifdef __cplusplus
  1266. }
  1267. #endif
  1268. #endif /* STM32WBxx_LL_SPI_H */
  1269. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/