stm32l4xx_hal_spi.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_spi.h
  4. * @author MCD Application Team
  5. * @brief Header file of SPI HAL module.
  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. /* Define to prevent recursive inclusion -------------------------------------*/
  36. #ifndef __STM32L4xx_HAL_SPI_H
  37. #define __STM32L4xx_HAL_SPI_H
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. /* Includes ------------------------------------------------------------------*/
  42. #include "stm32l4xx_hal_def.h"
  43. /** @addtogroup STM32L4xx_HAL_Driver
  44. * @{
  45. */
  46. /** @addtogroup SPI
  47. * @{
  48. */
  49. /* Exported types ------------------------------------------------------------*/
  50. /** @defgroup SPI_Exported_Types SPI Exported Types
  51. * @{
  52. */
  53. /**
  54. * @brief SPI Configuration Structure definition
  55. */
  56. typedef struct
  57. {
  58. uint32_t Mode; /*!< Specifies the SPI operating mode.
  59. This parameter can be a value of @ref SPI_Mode */
  60. uint32_t Direction; /*!< Specifies the SPI bidirectional mode state.
  61. This parameter can be a value of @ref SPI_Direction */
  62. uint32_t DataSize; /*!< Specifies the SPI data size.
  63. This parameter can be a value of @ref SPI_Data_Size */
  64. uint32_t CLKPolarity; /*!< Specifies the serial clock steady state.
  65. This parameter can be a value of @ref SPI_Clock_Polarity */
  66. uint32_t CLKPhase; /*!< Specifies the clock active edge for the bit capture.
  67. This parameter can be a value of @ref SPI_Clock_Phase */
  68. uint32_t NSS; /*!< Specifies whether the NSS signal is managed by
  69. hardware (NSS pin) or by software using the SSI bit.
  70. This parameter can be a value of @ref SPI_Slave_Select_management */
  71. uint32_t BaudRatePrescaler; /*!< Specifies the Baud Rate prescaler value which will be
  72. used to configure the transmit and receive SCK clock.
  73. This parameter can be a value of @ref SPI_BaudRate_Prescaler
  74. @note The communication clock is derived from the master
  75. clock. The slave clock does not need to be set. */
  76. uint32_t FirstBit; /*!< Specifies whether data transfers start from MSB or LSB bit.
  77. This parameter can be a value of @ref SPI_MSB_LSB_transmission */
  78. uint32_t TIMode; /*!< Specifies if the TI mode is enabled or not.
  79. This parameter can be a value of @ref SPI_TI_mode */
  80. uint32_t CRCCalculation; /*!< Specifies if the CRC calculation is enabled or not.
  81. This parameter can be a value of @ref SPI_CRC_Calculation */
  82. uint32_t CRCPolynomial; /*!< Specifies the polynomial used for the CRC calculation.
  83. This parameter must be an odd number between Min_Data = 1 and Max_Data = 65535 */
  84. uint32_t CRCLength; /*!< Specifies the CRC Length used for the CRC calculation.
  85. CRC Length is only used with Data8 and Data16, not other data size
  86. This parameter can be a value of @ref SPI_CRC_length */
  87. uint32_t NSSPMode; /*!< Specifies whether the NSSP signal is enabled or not .
  88. This parameter can be a value of @ref SPI_NSSP_Mode
  89. This mode is activated by the NSSP bit in the SPIx_CR2 register and
  90. it takes effect only if the SPI interface is configured as Motorola SPI
  91. master (FRF=0) with capture on the first edge (SPIx_CR1 CPHA = 0,
  92. CPOL setting is ignored).. */
  93. } SPI_InitTypeDef;
  94. /**
  95. * @brief HAL SPI State structure definition
  96. */
  97. typedef enum
  98. {
  99. HAL_SPI_STATE_RESET = 0x00U, /*!< Peripheral not Initialized */
  100. HAL_SPI_STATE_READY = 0x01U, /*!< Peripheral Initialized and ready for use */
  101. HAL_SPI_STATE_BUSY = 0x02U, /*!< an internal process is ongoing */
  102. HAL_SPI_STATE_BUSY_TX = 0x03U, /*!< Data Transmission process is ongoing */
  103. HAL_SPI_STATE_BUSY_RX = 0x04U, /*!< Data Reception process is ongoing */
  104. HAL_SPI_STATE_BUSY_TX_RX = 0x05U, /*!< Data Transmission and Reception process is ongoing */
  105. HAL_SPI_STATE_ERROR = 0x06U, /*!< SPI error state */
  106. HAL_SPI_STATE_ABORT = 0x07U /*!< SPI abort is ongoing */
  107. } HAL_SPI_StateTypeDef;
  108. /**
  109. * @brief SPI handle Structure definition
  110. */
  111. typedef struct __SPI_HandleTypeDef
  112. {
  113. SPI_TypeDef *Instance; /*!< SPI registers base address */
  114. SPI_InitTypeDef Init; /*!< SPI communication parameters */
  115. uint8_t *pTxBuffPtr; /*!< Pointer to SPI Tx transfer Buffer */
  116. uint16_t TxXferSize; /*!< SPI Tx Transfer size */
  117. __IO uint16_t TxXferCount; /*!< SPI Tx Transfer Counter */
  118. uint8_t *pRxBuffPtr; /*!< Pointer to SPI Rx transfer Buffer */
  119. uint16_t RxXferSize; /*!< SPI Rx Transfer size */
  120. __IO uint16_t RxXferCount; /*!< SPI Rx Transfer Counter */
  121. uint32_t CRCSize; /*!< SPI CRC size used for the transfer */
  122. void (*RxISR)(struct __SPI_HandleTypeDef *hspi); /*!< function pointer on Rx ISR */
  123. void (*TxISR)(struct __SPI_HandleTypeDef *hspi); /*!< function pointer on Tx ISR */
  124. DMA_HandleTypeDef *hdmatx; /*!< SPI Tx DMA Handle parameters */
  125. DMA_HandleTypeDef *hdmarx; /*!< SPI Rx DMA Handle parameters */
  126. HAL_LockTypeDef Lock; /*!< Locking object */
  127. __IO HAL_SPI_StateTypeDef State; /*!< SPI communication state */
  128. __IO uint32_t ErrorCode; /*!< SPI Error code */
  129. } SPI_HandleTypeDef;
  130. /**
  131. * @}
  132. */
  133. /* Exported constants --------------------------------------------------------*/
  134. /** @defgroup SPI_Exported_Constants SPI Exported Constants
  135. * @{
  136. */
  137. /** @defgroup SPI_Error_Code SPI Error Code
  138. * @{
  139. */
  140. #define HAL_SPI_ERROR_NONE (0x00000000U) /*!< No error */
  141. #define HAL_SPI_ERROR_MODF (0x00000001U) /*!< MODF error */
  142. #define HAL_SPI_ERROR_CRC (0x00000002U) /*!< CRC error */
  143. #define HAL_SPI_ERROR_OVR (0x00000004U) /*!< OVR error */
  144. #define HAL_SPI_ERROR_FRE (0x00000008U) /*!< FRE error */
  145. #define HAL_SPI_ERROR_DMA (0x00000010U) /*!< DMA transfer error */
  146. #define HAL_SPI_ERROR_FLAG (0x00000020U) /*!< Error on RXNE/TXE/BSY/FTLVL/FRLVL Flag */
  147. #define HAL_SPI_ERROR_ABORT (0x00000040U) /*!< Error during SPI Abort procedure */
  148. /**
  149. * @}
  150. */
  151. /** @defgroup SPI_Mode SPI Mode
  152. * @{
  153. */
  154. #define SPI_MODE_SLAVE (0x00000000U)
  155. #define SPI_MODE_MASTER (SPI_CR1_MSTR | SPI_CR1_SSI)
  156. /**
  157. * @}
  158. */
  159. /** @defgroup SPI_Direction SPI Direction Mode
  160. * @{
  161. */
  162. #define SPI_DIRECTION_2LINES (0x00000000U)
  163. #define SPI_DIRECTION_2LINES_RXONLY SPI_CR1_RXONLY
  164. #define SPI_DIRECTION_1LINE SPI_CR1_BIDIMODE
  165. /**
  166. * @}
  167. */
  168. /** @defgroup SPI_Data_Size SPI Data Size
  169. * @{
  170. */
  171. #define SPI_DATASIZE_4BIT (0x00000300U)
  172. #define SPI_DATASIZE_5BIT (0x00000400U)
  173. #define SPI_DATASIZE_6BIT (0x00000500U)
  174. #define SPI_DATASIZE_7BIT (0x00000600U)
  175. #define SPI_DATASIZE_8BIT (0x00000700U)
  176. #define SPI_DATASIZE_9BIT (0x00000800U)
  177. #define SPI_DATASIZE_10BIT (0x00000900U)
  178. #define SPI_DATASIZE_11BIT (0x00000A00U)
  179. #define SPI_DATASIZE_12BIT (0x00000B00U)
  180. #define SPI_DATASIZE_13BIT (0x00000C00U)
  181. #define SPI_DATASIZE_14BIT (0x00000D00U)
  182. #define SPI_DATASIZE_15BIT (0x00000E00U)
  183. #define SPI_DATASIZE_16BIT (0x00000F00U)
  184. /**
  185. * @}
  186. */
  187. /** @defgroup SPI_Clock_Polarity SPI Clock Polarity
  188. * @{
  189. */
  190. #define SPI_POLARITY_LOW (0x00000000U)
  191. #define SPI_POLARITY_HIGH SPI_CR1_CPOL
  192. /**
  193. * @}
  194. */
  195. /** @defgroup SPI_Clock_Phase SPI Clock Phase
  196. * @{
  197. */
  198. #define SPI_PHASE_1EDGE (0x00000000U)
  199. #define SPI_PHASE_2EDGE SPI_CR1_CPHA
  200. /**
  201. * @}
  202. */
  203. /** @defgroup SPI_Slave_Select_management SPI Slave Select Management
  204. * @{
  205. */
  206. #define SPI_NSS_SOFT SPI_CR1_SSM
  207. #define SPI_NSS_HARD_INPUT (0x00000000U)
  208. #define SPI_NSS_HARD_OUTPUT (SPI_CR2_SSOE << 16U)
  209. /**
  210. * @}
  211. */
  212. /** @defgroup SPI_NSSP_Mode SPI NSS Pulse Mode
  213. * @{
  214. */
  215. #define SPI_NSS_PULSE_ENABLE SPI_CR2_NSSP
  216. #define SPI_NSS_PULSE_DISABLE (0x00000000U)
  217. /**
  218. * @}
  219. */
  220. /** @defgroup SPI_BaudRate_Prescaler SPI BaudRate Prescaler
  221. * @{
  222. */
  223. #define SPI_BAUDRATEPRESCALER_2 (0x00000000U)
  224. #define SPI_BAUDRATEPRESCALER_4 (SPI_CR1_BR_0)
  225. #define SPI_BAUDRATEPRESCALER_8 (SPI_CR1_BR_1)
  226. #define SPI_BAUDRATEPRESCALER_16 (SPI_CR1_BR_1 | SPI_CR1_BR_0)
  227. #define SPI_BAUDRATEPRESCALER_32 (SPI_CR1_BR_2)
  228. #define SPI_BAUDRATEPRESCALER_64 (SPI_CR1_BR_2 | SPI_CR1_BR_0)
  229. #define SPI_BAUDRATEPRESCALER_128 (SPI_CR1_BR_2 | SPI_CR1_BR_1)
  230. #define SPI_BAUDRATEPRESCALER_256 (SPI_CR1_BR_2 | SPI_CR1_BR_1 | SPI_CR1_BR_0)
  231. /**
  232. * @}
  233. */
  234. /** @defgroup SPI_MSB_LSB_transmission SPI MSB LSB Transmission
  235. * @{
  236. */
  237. #define SPI_FIRSTBIT_MSB (0x00000000U)
  238. #define SPI_FIRSTBIT_LSB SPI_CR1_LSBFIRST
  239. /**
  240. * @}
  241. */
  242. /** @defgroup SPI_TI_mode SPI TI Mode
  243. * @{
  244. */
  245. #define SPI_TIMODE_DISABLE (0x00000000U)
  246. #define SPI_TIMODE_ENABLE SPI_CR2_FRF
  247. /**
  248. * @}
  249. */
  250. /** @defgroup SPI_CRC_Calculation SPI CRC Calculation
  251. * @{
  252. */
  253. #define SPI_CRCCALCULATION_DISABLE (0x00000000U)
  254. #define SPI_CRCCALCULATION_ENABLE SPI_CR1_CRCEN
  255. /**
  256. * @}
  257. */
  258. /** @defgroup SPI_CRC_length SPI CRC Length
  259. * @{
  260. * This parameter can be one of the following values:
  261. * SPI_CRC_LENGTH_DATASIZE: aligned with the data size
  262. * SPI_CRC_LENGTH_8BIT : CRC 8bit
  263. * SPI_CRC_LENGTH_16BIT : CRC 16bit
  264. */
  265. #define SPI_CRC_LENGTH_DATASIZE (0x00000000U)
  266. #define SPI_CRC_LENGTH_8BIT (0x00000001U)
  267. #define SPI_CRC_LENGTH_16BIT (0x00000002U)
  268. /**
  269. * @}
  270. */
  271. /** @defgroup SPI_FIFO_reception_threshold SPI FIFO Reception Threshold
  272. * @{
  273. * This parameter can be one of the following values:
  274. * SPI_RXFIFO_THRESHOLD or SPI_RXFIFO_THRESHOLD_QF :
  275. * RXNE event is generated if the FIFO
  276. * level is greater or equal to 1/2(16-bits).
  277. * SPI_RXFIFO_THRESHOLD_HF: RXNE event is generated if the FIFO
  278. * level is greater or equal to 1/4(8 bits). */
  279. #define SPI_RXFIFO_THRESHOLD SPI_CR2_FRXTH
  280. #define SPI_RXFIFO_THRESHOLD_QF SPI_CR2_FRXTH
  281. #define SPI_RXFIFO_THRESHOLD_HF (0x00000000U)
  282. /**
  283. * @}
  284. */
  285. /** @defgroup SPI_Interrupt_definition SPI Interrupt Definition
  286. * @{
  287. */
  288. #define SPI_IT_TXE SPI_CR2_TXEIE
  289. #define SPI_IT_RXNE SPI_CR2_RXNEIE
  290. #define SPI_IT_ERR SPI_CR2_ERRIE
  291. /**
  292. * @}
  293. */
  294. /** @defgroup SPI_Flags_definition SPI Flags Definition
  295. * @{
  296. */
  297. #define SPI_FLAG_RXNE SPI_SR_RXNE /* SPI status flag: Rx buffer not empty flag */
  298. #define SPI_FLAG_TXE SPI_SR_TXE /* SPI status flag: Tx buffer empty flag */
  299. #define SPI_FLAG_BSY SPI_SR_BSY /* SPI status flag: Busy flag */
  300. #define SPI_FLAG_CRCERR SPI_SR_CRCERR /* SPI Error flag: CRC error flag */
  301. #define SPI_FLAG_MODF SPI_SR_MODF /* SPI Error flag: Mode fault flag */
  302. #define SPI_FLAG_OVR SPI_SR_OVR /* SPI Error flag: Overrun flag */
  303. #define SPI_FLAG_FRE SPI_SR_FRE /* SPI Error flag: TI mode frame format error flag */
  304. #define SPI_FLAG_FTLVL SPI_SR_FTLVL /* SPI fifo transmission level */
  305. #define SPI_FLAG_FRLVL SPI_SR_FRLVL /* SPI fifo reception level */
  306. /**
  307. * @}
  308. */
  309. /** @defgroup SPI_transmission_fifo_status_level SPI Transmission FIFO Status Level
  310. * @{
  311. */
  312. #define SPI_FTLVL_EMPTY (0x00000000U)
  313. #define SPI_FTLVL_QUARTER_FULL (0x00000800U)
  314. #define SPI_FTLVL_HALF_FULL (0x00001000U)
  315. #define SPI_FTLVL_FULL (0x00001800U)
  316. /**
  317. * @}
  318. */
  319. /** @defgroup SPI_reception_fifo_status_level SPI Reception FIFO Status Level
  320. * @{
  321. */
  322. #define SPI_FRLVL_EMPTY (0x00000000U)
  323. #define SPI_FRLVL_QUARTER_FULL (0x00000200U)
  324. #define SPI_FRLVL_HALF_FULL (0x00000400U)
  325. #define SPI_FRLVL_FULL (0x00000600U)
  326. /**
  327. * @}
  328. */
  329. /**
  330. * @}
  331. */
  332. /* Exported macros -----------------------------------------------------------*/
  333. /** @defgroup SPI_Exported_Macros SPI Exported Macros
  334. * @{
  335. */
  336. /** @brief Reset SPI handle state.
  337. * @param __HANDLE__ specifies the SPI Handle.
  338. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  339. * @retval None
  340. */
  341. #define __HAL_SPI_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_SPI_STATE_RESET)
  342. /** @brief Enable the specified SPI interrupts.
  343. * @param __HANDLE__ specifies the SPI Handle.
  344. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  345. * @param __INTERRUPT__ specifies the interrupt source to enable.
  346. * This parameter can be one of the following values:
  347. * @arg SPI_IT_TXE: Tx buffer empty interrupt enable
  348. * @arg SPI_IT_RXNE: RX buffer not empty interrupt enable
  349. * @arg SPI_IT_ERR: Error interrupt enable
  350. * @retval None
  351. */
  352. #define __HAL_SPI_ENABLE_IT(__HANDLE__, __INTERRUPT__) SET_BIT((__HANDLE__)->Instance->CR2, (__INTERRUPT__))
  353. /** @brief Disable the specified SPI interrupts.
  354. * @param __HANDLE__ specifies the SPI handle.
  355. * This parameter can be SPIx where x: 1, 2, or 3 to select the SPI peripheral.
  356. * @param __INTERRUPT__ specifies the interrupt source to disable.
  357. * This parameter can be one of the following values:
  358. * @arg SPI_IT_TXE: Tx buffer empty interrupt enable
  359. * @arg SPI_IT_RXNE: RX buffer not empty interrupt enable
  360. * @arg SPI_IT_ERR: Error interrupt enable
  361. * @retval None
  362. */
  363. #define __HAL_SPI_DISABLE_IT(__HANDLE__, __INTERRUPT__) CLEAR_BIT((__HANDLE__)->Instance->CR2, (__INTERRUPT__))
  364. /** @brief Check whether the specified SPI interrupt source is enabled or not.
  365. * @param __HANDLE__ specifies the SPI Handle.
  366. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  367. * @param __INTERRUPT__ specifies the SPI interrupt source to check.
  368. * This parameter can be one of the following values:
  369. * @arg SPI_IT_TXE: Tx buffer empty interrupt enable
  370. * @arg SPI_IT_RXNE: RX buffer not empty interrupt enable
  371. * @arg SPI_IT_ERR: Error interrupt enable
  372. * @retval The new state of __IT__ (TRUE or FALSE).
  373. */
  374. #define __HAL_SPI_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__) ((((__HANDLE__)->Instance->CR2 & (__INTERRUPT__)) == (__INTERRUPT__)) ? SET : RESET)
  375. /** @brief Check whether the specified SPI flag is set or not.
  376. * @param __HANDLE__ specifies the SPI Handle.
  377. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  378. * @param __FLAG__ specifies the flag to check.
  379. * This parameter can be one of the following values:
  380. * @arg SPI_FLAG_RXNE: Receive buffer not empty flag
  381. * @arg SPI_FLAG_TXE: Transmit buffer empty flag
  382. * @arg SPI_FLAG_CRCERR: CRC error flag
  383. * @arg SPI_FLAG_MODF: Mode fault flag
  384. * @arg SPI_FLAG_OVR: Overrun flag
  385. * @arg SPI_FLAG_BSY: Busy flag
  386. * @arg SPI_FLAG_FRE: Frame format error flag
  387. * @arg SPI_FLAG_FTLVL: SPI fifo transmission level
  388. * @arg SPI_FLAG_FRLVL: SPI fifo reception level
  389. * @retval The new state of __FLAG__ (TRUE or FALSE).
  390. */
  391. #define __HAL_SPI_GET_FLAG(__HANDLE__, __FLAG__) ((((__HANDLE__)->Instance->SR) & (__FLAG__)) == (__FLAG__))
  392. /** @brief Clear the SPI CRCERR pending flag.
  393. * @param __HANDLE__ specifies the SPI Handle.
  394. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  395. * @retval None
  396. */
  397. #define __HAL_SPI_CLEAR_CRCERRFLAG(__HANDLE__) ((__HANDLE__)->Instance->SR = (uint16_t)(~SPI_FLAG_CRCERR))
  398. /** @brief Clear the SPI MODF pending flag.
  399. * @param __HANDLE__ specifies the SPI Handle.
  400. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  401. * @retval None
  402. */
  403. #define __HAL_SPI_CLEAR_MODFFLAG(__HANDLE__) \
  404. do{ \
  405. __IO uint32_t tmpreg_modf = 0x00U; \
  406. tmpreg_modf = (__HANDLE__)->Instance->SR; \
  407. CLEAR_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_SPE); \
  408. UNUSED(tmpreg_modf); \
  409. } while(0U)
  410. /** @brief Clear the SPI OVR pending flag.
  411. * @param __HANDLE__ specifies the SPI Handle.
  412. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  413. * @retval None
  414. */
  415. #define __HAL_SPI_CLEAR_OVRFLAG(__HANDLE__) \
  416. do{ \
  417. __IO uint32_t tmpreg_ovr = 0x00U; \
  418. tmpreg_ovr = (__HANDLE__)->Instance->DR; \
  419. tmpreg_ovr = (__HANDLE__)->Instance->SR; \
  420. UNUSED(tmpreg_ovr); \
  421. } while(0U)
  422. /** @brief Clear the SPI FRE pending flag.
  423. * @param __HANDLE__ specifies the SPI Handle.
  424. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  425. * @retval None
  426. */
  427. #define __HAL_SPI_CLEAR_FREFLAG(__HANDLE__) \
  428. do{ \
  429. __IO uint32_t tmpreg_fre = 0x00U; \
  430. tmpreg_fre = (__HANDLE__)->Instance->SR; \
  431. UNUSED(tmpreg_fre); \
  432. }while(0U)
  433. /** @brief Enable the SPI peripheral.
  434. * @param __HANDLE__ specifies the SPI Handle.
  435. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  436. * @retval None
  437. */
  438. #define __HAL_SPI_ENABLE(__HANDLE__) SET_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_SPE)
  439. /** @brief Disable the SPI peripheral.
  440. * @param __HANDLE__ specifies the SPI Handle.
  441. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  442. * @retval None
  443. */
  444. #define __HAL_SPI_DISABLE(__HANDLE__) CLEAR_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_SPE)
  445. /**
  446. * @}
  447. */
  448. /* Private macros ------------------------------------------------------------*/
  449. /** @defgroup SPI_Private_Macros SPI Private Macros
  450. * @{
  451. */
  452. /** @brief Set the SPI transmit-only mode.
  453. * @param __HANDLE__ specifies the SPI Handle.
  454. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  455. * @retval None
  456. */
  457. #define SPI_1LINE_TX(__HANDLE__) SET_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_BIDIOE)
  458. /** @brief Set the SPI receive-only mode.
  459. * @param __HANDLE__ specifies the SPI Handle.
  460. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  461. * @retval None
  462. */
  463. #define SPI_1LINE_RX(__HANDLE__) CLEAR_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_BIDIOE)
  464. /** @brief Reset the CRC calculation of the SPI.
  465. * @param __HANDLE__ specifies the SPI Handle.
  466. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  467. * @retval None
  468. */
  469. #define SPI_RESET_CRC(__HANDLE__) do{CLEAR_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_CRCEN);\
  470. SET_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_CRCEN);}while(0U)
  471. /** @brief Checks if SPI Mode parameter is in allowed range.
  472. * @param __MODE__ specifies the SPI Mode.
  473. * This parameter can be a value of @ref SPI_Mode
  474. * @retval None
  475. */
  476. #define IS_SPI_MODE(__MODE__) (((__MODE__) == SPI_MODE_SLAVE) || \
  477. ((__MODE__) == SPI_MODE_MASTER))
  478. /** @brief Checks if SPI Direction Mode parameter is in allowed range.
  479. * @param __MODE__ specifies the SPI Direction Mode.
  480. * This parameter can be a value of @ref SPI_Direction
  481. * @retval None
  482. */
  483. #define IS_SPI_DIRECTION(__MODE__) (((__MODE__) == SPI_DIRECTION_2LINES) || \
  484. ((__MODE__) == SPI_DIRECTION_2LINES_RXONLY) || \
  485. ((__MODE__) == SPI_DIRECTION_1LINE))
  486. /** @brief Checks if SPI Direction Mode parameter is 2 lines.
  487. * @param __MODE__ specifies the SPI Direction Mode.
  488. * @retval None
  489. */
  490. #define IS_SPI_DIRECTION_2LINES(__MODE__) ((__MODE__) == SPI_DIRECTION_2LINES)
  491. /** @brief Checks if SPI Direction Mode parameter is 1 or 2 lines.
  492. * @param __MODE__ specifies the SPI Direction Mode.
  493. * @retval None
  494. */
  495. #define IS_SPI_DIRECTION_2LINES_OR_1LINE(__MODE__) (((__MODE__) == SPI_DIRECTION_2LINES) || \
  496. ((__MODE__) == SPI_DIRECTION_1LINE))
  497. /** @brief Checks if SPI Data Size parameter is in allowed range.
  498. * @param __DATASIZE__ specifies the SPI Data Size.
  499. * This parameter can be a value of @ref SPI_Data_Size
  500. * @retval None
  501. */
  502. #define IS_SPI_DATASIZE(__DATASIZE__) (((__DATASIZE__) == SPI_DATASIZE_16BIT) || \
  503. ((__DATASIZE__) == SPI_DATASIZE_15BIT) || \
  504. ((__DATASIZE__) == SPI_DATASIZE_14BIT) || \
  505. ((__DATASIZE__) == SPI_DATASIZE_13BIT) || \
  506. ((__DATASIZE__) == SPI_DATASIZE_12BIT) || \
  507. ((__DATASIZE__) == SPI_DATASIZE_11BIT) || \
  508. ((__DATASIZE__) == SPI_DATASIZE_10BIT) || \
  509. ((__DATASIZE__) == SPI_DATASIZE_9BIT) || \
  510. ((__DATASIZE__) == SPI_DATASIZE_8BIT) || \
  511. ((__DATASIZE__) == SPI_DATASIZE_7BIT) || \
  512. ((__DATASIZE__) == SPI_DATASIZE_6BIT) || \
  513. ((__DATASIZE__) == SPI_DATASIZE_5BIT) || \
  514. ((__DATASIZE__) == SPI_DATASIZE_4BIT))
  515. /** @brief Checks if SPI Serial clock steady state parameter is in allowed range.
  516. * @param __CPOL__ specifies the SPI serial clock steady state.
  517. * This parameter can be a value of @ref SPI_Clock_Polarity
  518. * @retval None
  519. */
  520. #define IS_SPI_CPOL(__CPOL__) (((__CPOL__) == SPI_POLARITY_LOW) || \
  521. ((__CPOL__) == SPI_POLARITY_HIGH))
  522. /** @brief Checks if SPI Clock Phase parameter is in allowed range.
  523. * @param __CPHA__ specifies the SPI Clock Phase.
  524. * This parameter can be a value of @ref SPI_Clock_Phase
  525. * @retval None
  526. */
  527. #define IS_SPI_CPHA(__CPHA__) (((__CPHA__) == SPI_PHASE_1EDGE) || \
  528. ((__CPHA__) == SPI_PHASE_2EDGE))
  529. /** @brief Checks if SPI Slave Select parameter is in allowed range.
  530. * @param __NSS__ specifies the SPI Slave Slelect management parameter.
  531. * This parameter can be a value of @ref SPI_Slave_Select_management
  532. * @retval None
  533. */
  534. #define IS_SPI_NSS(__NSS__) (((__NSS__) == SPI_NSS_SOFT) || \
  535. ((__NSS__) == SPI_NSS_HARD_INPUT) || \
  536. ((__NSS__) == SPI_NSS_HARD_OUTPUT))
  537. /** @brief Checks if SPI NSS Pulse parameter is in allowed range.
  538. * @param __NSSP__ specifies the SPI NSS Pulse Mode parameter.
  539. * This parameter can be a value of @ref SPI_NSSP_Mode
  540. * @retval None
  541. */
  542. #define IS_SPI_NSSP(__NSSP__) (((__NSSP__) == SPI_NSS_PULSE_ENABLE) || \
  543. ((__NSSP__) == SPI_NSS_PULSE_DISABLE))
  544. /** @brief Checks if SPI Baudrate prescaler parameter is in allowed range.
  545. * @param __PRESCALER__ specifies the SPI Baudrate prescaler.
  546. * This parameter can be a value of @ref SPI_BaudRate_Prescaler
  547. * @retval None
  548. */
  549. #define IS_SPI_BAUDRATE_PRESCALER(__PRESCALER__) (((__PRESCALER__) == SPI_BAUDRATEPRESCALER_2) || \
  550. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_4) || \
  551. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_8) || \
  552. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_16) || \
  553. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_32) || \
  554. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_64) || \
  555. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_128) || \
  556. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_256))
  557. /** @brief Checks if SPI MSB LSB transmission parameter is in allowed range.
  558. * @param __BIT__ specifies the SPI MSB LSB transmission (whether data transfer starts from MSB or LSB bit).
  559. * This parameter can be a value of @ref SPI_MSB_LSB_transmission
  560. * @retval None
  561. */
  562. #define IS_SPI_FIRST_BIT(__BIT__) (((__BIT__) == SPI_FIRSTBIT_MSB) || \
  563. ((__BIT__) == SPI_FIRSTBIT_LSB))
  564. /** @brief Checks if SPI TI mode parameter is in allowed range.
  565. * @param __MODE__ specifies the SPI TI mode.
  566. * This parameter can be a value of @ref SPI_TI_mode
  567. * @retval None
  568. */
  569. #define IS_SPI_TIMODE(__MODE__) (((__MODE__) == SPI_TIMODE_DISABLE) || \
  570. ((__MODE__) == SPI_TIMODE_ENABLE))
  571. /** @brief Checks if SPI CRC calculation enabled state is in allowed range.
  572. * @param __CALCULATION__ specifies the SPI CRC calculation enable state.
  573. * This parameter can be a value of @ref SPI_CRC_Calculation
  574. * @retval None
  575. */
  576. #define IS_SPI_CRC_CALCULATION(__CALCULATION__) (((__CALCULATION__) == SPI_CRCCALCULATION_DISABLE) || \
  577. ((__CALCULATION__) == SPI_CRCCALCULATION_ENABLE))
  578. /** @brief Checks if SPI CRC length is in allowed range.
  579. * @param __LENGTH__ specifies the SPI CRC length.
  580. * This parameter can be a value of @ref SPI_CRC_length
  581. * @retval None
  582. */
  583. #define IS_SPI_CRC_LENGTH(__LENGTH__) (((__LENGTH__) == SPI_CRC_LENGTH_DATASIZE) ||\
  584. ((__LENGTH__) == SPI_CRC_LENGTH_8BIT) || \
  585. ((__LENGTH__) == SPI_CRC_LENGTH_16BIT))
  586. /** @brief Checks if SPI polynomial value to be used for the CRC calculation, is in allowed range.
  587. * @param __POLYNOMIAL__ specifies the SPI polynomial value to be used for the CRC calculation.
  588. * This parameter must be a number between Min_Data = 0 and Max_Data = 65535
  589. * @retval None
  590. */
  591. #define IS_SPI_CRC_POLYNOMIAL(__POLYNOMIAL__) (((__POLYNOMIAL__) >= 0x1U) && ((__POLYNOMIAL__) <= 0xFFFFU) && (((__POLYNOMIAL__)&0x1U) != 0U))
  592. /** @brief Checks if DMA handle is valid.
  593. * @param __HANDLE__ specifies a DMA Handle.
  594. * @retval None
  595. */
  596. #define IS_SPI_DMA_HANDLE(__HANDLE__) ((__HANDLE__) != NULL)
  597. /**
  598. * @}
  599. */
  600. /* Include SPI HAL Extended module */
  601. #include "stm32l4xx_hal_spi_ex.h"
  602. /* Exported functions --------------------------------------------------------*/
  603. /** @addtogroup SPI_Exported_Functions
  604. * @{
  605. */
  606. /** @addtogroup SPI_Exported_Functions_Group1
  607. * @{
  608. */
  609. /* Initialization/de-initialization functions ********************************/
  610. HAL_StatusTypeDef HAL_SPI_Init(SPI_HandleTypeDef *hspi);
  611. HAL_StatusTypeDef HAL_SPI_DeInit(SPI_HandleTypeDef *hspi);
  612. void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi);
  613. void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi);
  614. /**
  615. * @}
  616. */
  617. /** @addtogroup SPI_Exported_Functions_Group2
  618. * @{
  619. */
  620. /* I/O operation functions ***************************************************/
  621. HAL_StatusTypeDef HAL_SPI_Transmit(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  622. HAL_StatusTypeDef HAL_SPI_Receive(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  623. HAL_StatusTypeDef HAL_SPI_TransmitReceive(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size,
  624. uint32_t Timeout);
  625. HAL_StatusTypeDef HAL_SPI_Transmit_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size);
  626. HAL_StatusTypeDef HAL_SPI_Receive_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size);
  627. HAL_StatusTypeDef HAL_SPI_TransmitReceive_IT(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData,
  628. uint16_t Size);
  629. HAL_StatusTypeDef HAL_SPI_Transmit_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size);
  630. HAL_StatusTypeDef HAL_SPI_Receive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size);
  631. HAL_StatusTypeDef HAL_SPI_TransmitReceive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData,
  632. uint16_t Size);
  633. HAL_StatusTypeDef HAL_SPI_DMAPause(SPI_HandleTypeDef *hspi);
  634. HAL_StatusTypeDef HAL_SPI_DMAResume(SPI_HandleTypeDef *hspi);
  635. HAL_StatusTypeDef HAL_SPI_DMAStop(SPI_HandleTypeDef *hspi);
  636. /* Transfer Abort functions */
  637. HAL_StatusTypeDef HAL_SPI_Abort(SPI_HandleTypeDef *hspi);
  638. HAL_StatusTypeDef HAL_SPI_Abort_IT(SPI_HandleTypeDef *hspi);
  639. void HAL_SPI_IRQHandler(SPI_HandleTypeDef *hspi);
  640. void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi);
  641. void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi);
  642. void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi);
  643. void HAL_SPI_TxHalfCpltCallback(SPI_HandleTypeDef *hspi);
  644. void HAL_SPI_RxHalfCpltCallback(SPI_HandleTypeDef *hspi);
  645. void HAL_SPI_TxRxHalfCpltCallback(SPI_HandleTypeDef *hspi);
  646. void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi);
  647. void HAL_SPI_AbortCpltCallback(SPI_HandleTypeDef *hspi);
  648. /**
  649. * @}
  650. */
  651. /** @addtogroup SPI_Exported_Functions_Group3
  652. * @{
  653. */
  654. /* Peripheral State and Error functions ***************************************/
  655. HAL_SPI_StateTypeDef HAL_SPI_GetState(SPI_HandleTypeDef *hspi);
  656. uint32_t HAL_SPI_GetError(SPI_HandleTypeDef *hspi);
  657. /**
  658. * @}
  659. */
  660. /**
  661. * @}
  662. */
  663. /**
  664. * @}
  665. */
  666. /**
  667. * @}
  668. */
  669. #ifdef __cplusplus
  670. }
  671. #endif
  672. #endif /* __STM32L4xx_HAL_SPI_H */
  673. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/