Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

734 linhas
31 KiB

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