stm32f1xx_hal_spi.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_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 STM32F1xx_HAL_SPI_H
  20. #define STM32F1xx_HAL_SPI_H
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /* Includes ------------------------------------------------------------------*/
  25. #include "stm32f1xx_hal_def.h"
  26. /** @addtogroup STM32F1xx_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. 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_DMA (0x00000010U) /*!< DMA transfer error */
  152. #define HAL_SPI_ERROR_FLAG (0x00000020U) /*!< Error on RXNE/TXE/BSY Flag */
  153. #define HAL_SPI_ERROR_ABORT (0x00000040U) /*!< Error during SPI Abort procedure */
  154. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  155. #define HAL_SPI_ERROR_INVALID_CALLBACK (0x00000080U) /*!< Invalid Callback error */
  156. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  157. /**
  158. * @}
  159. */
  160. /** @defgroup SPI_Mode SPI Mode
  161. * @{
  162. */
  163. #define SPI_MODE_SLAVE (0x00000000U)
  164. #define SPI_MODE_MASTER (SPI_CR1_MSTR | SPI_CR1_SSI)
  165. /**
  166. * @}
  167. */
  168. /** @defgroup SPI_Direction SPI Direction Mode
  169. * @{
  170. */
  171. #define SPI_DIRECTION_2LINES (0x00000000U)
  172. #define SPI_DIRECTION_2LINES_RXONLY SPI_CR1_RXONLY
  173. #define SPI_DIRECTION_1LINE SPI_CR1_BIDIMODE
  174. /**
  175. * @}
  176. */
  177. /** @defgroup SPI_Data_Size SPI Data Size
  178. * @{
  179. */
  180. #define SPI_DATASIZE_8BIT (0x00000000U)
  181. #define SPI_DATASIZE_16BIT SPI_CR1_DFF
  182. /**
  183. * @}
  184. */
  185. /** @defgroup SPI_Clock_Polarity SPI Clock Polarity
  186. * @{
  187. */
  188. #define SPI_POLARITY_LOW (0x00000000U)
  189. #define SPI_POLARITY_HIGH SPI_CR1_CPOL
  190. /**
  191. * @}
  192. */
  193. /** @defgroup SPI_Clock_Phase SPI Clock Phase
  194. * @{
  195. */
  196. #define SPI_PHASE_1EDGE (0x00000000U)
  197. #define SPI_PHASE_2EDGE SPI_CR1_CPHA
  198. /**
  199. * @}
  200. */
  201. /** @defgroup SPI_Slave_Select_management SPI Slave Select Management
  202. * @{
  203. */
  204. #define SPI_NSS_SOFT SPI_CR1_SSM
  205. #define SPI_NSS_HARD_INPUT (0x00000000U)
  206. #define SPI_NSS_HARD_OUTPUT (SPI_CR2_SSOE << 16U)
  207. /**
  208. * @}
  209. */
  210. /** @defgroup SPI_BaudRate_Prescaler SPI BaudRate Prescaler
  211. * @{
  212. */
  213. #define SPI_BAUDRATEPRESCALER_2 (0x00000000U)
  214. #define SPI_BAUDRATEPRESCALER_4 (SPI_CR1_BR_0)
  215. #define SPI_BAUDRATEPRESCALER_8 (SPI_CR1_BR_1)
  216. #define SPI_BAUDRATEPRESCALER_16 (SPI_CR1_BR_1 | SPI_CR1_BR_0)
  217. #define SPI_BAUDRATEPRESCALER_32 (SPI_CR1_BR_2)
  218. #define SPI_BAUDRATEPRESCALER_64 (SPI_CR1_BR_2 | SPI_CR1_BR_0)
  219. #define SPI_BAUDRATEPRESCALER_128 (SPI_CR1_BR_2 | SPI_CR1_BR_1)
  220. #define SPI_BAUDRATEPRESCALER_256 (SPI_CR1_BR_2 | SPI_CR1_BR_1 | SPI_CR1_BR_0)
  221. /**
  222. * @}
  223. */
  224. /** @defgroup SPI_MSB_LSB_transmission SPI MSB LSB Transmission
  225. * @{
  226. */
  227. #define SPI_FIRSTBIT_MSB (0x00000000U)
  228. #define SPI_FIRSTBIT_LSB SPI_CR1_LSBFIRST
  229. /**
  230. * @}
  231. */
  232. /** @defgroup SPI_TI_mode SPI TI Mode
  233. * @{
  234. */
  235. #define SPI_TIMODE_DISABLE (0x00000000U)
  236. /**
  237. * @}
  238. */
  239. /** @defgroup SPI_CRC_Calculation SPI CRC Calculation
  240. * @{
  241. */
  242. #define SPI_CRCCALCULATION_DISABLE (0x00000000U)
  243. #define SPI_CRCCALCULATION_ENABLE SPI_CR1_CRCEN
  244. /**
  245. * @}
  246. */
  247. /** @defgroup SPI_Interrupt_definition SPI Interrupt Definition
  248. * @{
  249. */
  250. #define SPI_IT_TXE SPI_CR2_TXEIE
  251. #define SPI_IT_RXNE SPI_CR2_RXNEIE
  252. #define SPI_IT_ERR SPI_CR2_ERRIE
  253. /**
  254. * @}
  255. */
  256. /** @defgroup SPI_Flags_definition SPI Flags Definition
  257. * @{
  258. */
  259. #define SPI_FLAG_RXNE SPI_SR_RXNE /* SPI status flag: Rx buffer not empty flag */
  260. #define SPI_FLAG_TXE SPI_SR_TXE /* SPI status flag: Tx buffer empty flag */
  261. #define SPI_FLAG_BSY SPI_SR_BSY /* SPI status flag: Busy flag */
  262. #define SPI_FLAG_CRCERR SPI_SR_CRCERR /* SPI Error flag: CRC error flag */
  263. #define SPI_FLAG_MODF SPI_SR_MODF /* SPI Error flag: Mode fault flag */
  264. #define SPI_FLAG_OVR SPI_SR_OVR /* SPI Error flag: Overrun flag */
  265. #define SPI_FLAG_MASK (SPI_SR_RXNE | SPI_SR_TXE | SPI_SR_BSY\
  266. | SPI_SR_CRCERR | SPI_SR_MODF | SPI_SR_OVR)
  267. /**
  268. * @}
  269. */
  270. /**
  271. * @}
  272. */
  273. /* Exported macros -----------------------------------------------------------*/
  274. /** @defgroup SPI_Exported_Macros SPI Exported Macros
  275. * @{
  276. */
  277. /** @brief Reset SPI handle state.
  278. * @param __HANDLE__ specifies the SPI Handle.
  279. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  280. * @retval None
  281. */
  282. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  283. #define __HAL_SPI_RESET_HANDLE_STATE(__HANDLE__) do{ \
  284. (__HANDLE__)->State = HAL_SPI_STATE_RESET; \
  285. (__HANDLE__)->MspInitCallback = NULL; \
  286. (__HANDLE__)->MspDeInitCallback = NULL; \
  287. } while(0)
  288. #else
  289. #define __HAL_SPI_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_SPI_STATE_RESET)
  290. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  291. /** @brief Enable the specified SPI interrupts.
  292. * @param __HANDLE__ specifies the SPI Handle.
  293. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  294. * @param __INTERRUPT__ specifies the interrupt source to enable.
  295. * This parameter can be one of the following values:
  296. * @arg SPI_IT_TXE: Tx buffer empty interrupt enable
  297. * @arg SPI_IT_RXNE: RX buffer not empty interrupt enable
  298. * @arg SPI_IT_ERR: Error interrupt enable
  299. * @retval None
  300. */
  301. #define __HAL_SPI_ENABLE_IT(__HANDLE__, __INTERRUPT__) SET_BIT((__HANDLE__)->Instance->CR2, (__INTERRUPT__))
  302. /** @brief Disable the specified SPI interrupts.
  303. * @param __HANDLE__ specifies the SPI handle.
  304. * This parameter can be SPIx where x: 1, 2, or 3 to select the SPI peripheral.
  305. * @param __INTERRUPT__ specifies the interrupt source to disable.
  306. * This parameter can be one of the following values:
  307. * @arg SPI_IT_TXE: Tx buffer empty interrupt enable
  308. * @arg SPI_IT_RXNE: RX buffer not empty interrupt enable
  309. * @arg SPI_IT_ERR: Error interrupt enable
  310. * @retval None
  311. */
  312. #define __HAL_SPI_DISABLE_IT(__HANDLE__, __INTERRUPT__) CLEAR_BIT((__HANDLE__)->Instance->CR2, (__INTERRUPT__))
  313. /** @brief Check whether the specified SPI interrupt source is enabled or not.
  314. * @param __HANDLE__ specifies the SPI Handle.
  315. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  316. * @param __INTERRUPT__ specifies the SPI interrupt source to check.
  317. * This parameter can be one of the following values:
  318. * @arg SPI_IT_TXE: Tx buffer empty interrupt enable
  319. * @arg SPI_IT_RXNE: RX buffer not empty interrupt enable
  320. * @arg SPI_IT_ERR: Error interrupt enable
  321. * @retval The new state of __IT__ (TRUE or FALSE).
  322. */
  323. #define __HAL_SPI_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__) ((((__HANDLE__)->Instance->CR2\
  324. & (__INTERRUPT__)) == (__INTERRUPT__)) ? SET : RESET)
  325. /** @brief Check whether the specified SPI flag is set or not.
  326. * @param __HANDLE__ specifies the SPI Handle.
  327. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  328. * @param __FLAG__ specifies the flag to check.
  329. * This parameter can be one of the following values:
  330. * @arg SPI_FLAG_RXNE: Receive buffer not empty flag
  331. * @arg SPI_FLAG_TXE: Transmit buffer empty flag
  332. * @arg SPI_FLAG_CRCERR: CRC error flag
  333. * @arg SPI_FLAG_MODF: Mode fault flag
  334. * @arg SPI_FLAG_OVR: Overrun flag
  335. * @arg SPI_FLAG_BSY: Busy flag
  336. * @retval The new state of __FLAG__ (TRUE or FALSE).
  337. */
  338. #define __HAL_SPI_GET_FLAG(__HANDLE__, __FLAG__) ((((__HANDLE__)->Instance->SR) & (__FLAG__)) == (__FLAG__))
  339. /** @brief Clear the SPI CRCERR pending flag.
  340. * @param __HANDLE__ specifies the SPI Handle.
  341. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  342. * @retval None
  343. */
  344. #define __HAL_SPI_CLEAR_CRCERRFLAG(__HANDLE__) ((__HANDLE__)->Instance->SR = (uint16_t)(~SPI_FLAG_CRCERR))
  345. /** @brief Clear the SPI MODF pending flag.
  346. * @param __HANDLE__ specifies the SPI Handle.
  347. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  348. * @retval None
  349. */
  350. #define __HAL_SPI_CLEAR_MODFFLAG(__HANDLE__) \
  351. do{ \
  352. __IO uint32_t tmpreg_modf = 0x00U; \
  353. tmpreg_modf = (__HANDLE__)->Instance->SR; \
  354. CLEAR_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_SPE); \
  355. UNUSED(tmpreg_modf); \
  356. } while(0U)
  357. /** @brief Clear the SPI OVR pending flag.
  358. * @param __HANDLE__ specifies the SPI Handle.
  359. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  360. * @retval None
  361. */
  362. #define __HAL_SPI_CLEAR_OVRFLAG(__HANDLE__) \
  363. do{ \
  364. __IO uint32_t tmpreg_ovr = 0x00U; \
  365. tmpreg_ovr = (__HANDLE__)->Instance->DR; \
  366. tmpreg_ovr = (__HANDLE__)->Instance->SR; \
  367. UNUSED(tmpreg_ovr); \
  368. } while(0U)
  369. /** @brief Enable the SPI peripheral.
  370. * @param __HANDLE__ specifies the SPI Handle.
  371. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  372. * @retval None
  373. */
  374. #define __HAL_SPI_ENABLE(__HANDLE__) SET_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_SPE)
  375. /** @brief Disable the SPI peripheral.
  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. * @retval None
  379. */
  380. #define __HAL_SPI_DISABLE(__HANDLE__) CLEAR_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_SPE)
  381. /**
  382. * @}
  383. */
  384. /* Private constants ---------------------------------------------------------*/
  385. /** @defgroup SPI_Private_Constants SPI Private Constants
  386. * @{
  387. */
  388. #define SPI_INVALID_CRC_ERROR 0U /* CRC error wrongly detected */
  389. #define SPI_VALID_CRC_ERROR 1U /* CRC error is true */
  390. /**
  391. * @}
  392. */
  393. /* Private macros ------------------------------------------------------------*/
  394. /** @defgroup SPI_Private_Macros SPI Private Macros
  395. * @{
  396. */
  397. /** @brief Set the SPI transmit-only mode.
  398. * @param __HANDLE__ specifies the SPI Handle.
  399. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  400. * @retval None
  401. */
  402. #define SPI_1LINE_TX(__HANDLE__) SET_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_BIDIOE)
  403. /** @brief Set the SPI receive-only mode.
  404. * @param __HANDLE__ specifies the SPI Handle.
  405. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  406. * @retval None
  407. */
  408. #define SPI_1LINE_RX(__HANDLE__) CLEAR_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_BIDIOE)
  409. /** @brief Reset the CRC calculation of the SPI.
  410. * @param __HANDLE__ specifies the SPI Handle.
  411. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  412. * @retval None
  413. */
  414. #define SPI_RESET_CRC(__HANDLE__) do{CLEAR_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_CRCEN);\
  415. SET_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_CRCEN);}while(0U)
  416. /** @brief Check whether the specified SPI flag is set or not.
  417. * @param __SR__ copy of SPI SR register.
  418. * @param __FLAG__ specifies the flag to check.
  419. * This parameter can be one of the following values:
  420. * @arg SPI_FLAG_RXNE: Receive buffer not empty flag
  421. * @arg SPI_FLAG_TXE: Transmit buffer empty flag
  422. * @arg SPI_FLAG_CRCERR: CRC error flag
  423. * @arg SPI_FLAG_MODF: Mode fault flag
  424. * @arg SPI_FLAG_OVR: Overrun flag
  425. * @arg SPI_FLAG_BSY: Busy flag
  426. * @retval SET or RESET.
  427. */
  428. #define SPI_CHECK_FLAG(__SR__, __FLAG__) ((((__SR__) & ((__FLAG__) & SPI_FLAG_MASK)) == \
  429. ((__FLAG__) & SPI_FLAG_MASK)) ? SET : RESET)
  430. /** @brief Check whether the specified SPI Interrupt is set or not.
  431. * @param __CR2__ copy of SPI CR2 register.
  432. * @param __INTERRUPT__ specifies the SPI interrupt source to check.
  433. * This parameter can be one of the following values:
  434. * @arg SPI_IT_TXE: Tx buffer empty interrupt enable
  435. * @arg SPI_IT_RXNE: RX buffer not empty interrupt enable
  436. * @arg SPI_IT_ERR: Error interrupt enable
  437. * @retval SET or RESET.
  438. */
  439. #define SPI_CHECK_IT_SOURCE(__CR2__, __INTERRUPT__) ((((__CR2__) & (__INTERRUPT__)) == \
  440. (__INTERRUPT__)) ? SET : RESET)
  441. /** @brief Checks if SPI Mode parameter is in allowed range.
  442. * @param __MODE__ specifies the SPI Mode.
  443. * This parameter can be a value of @ref SPI_Mode
  444. * @retval None
  445. */
  446. #define IS_SPI_MODE(__MODE__) (((__MODE__) == SPI_MODE_SLAVE) || \
  447. ((__MODE__) == SPI_MODE_MASTER))
  448. /** @brief Checks if SPI Direction Mode parameter is in allowed range.
  449. * @param __MODE__ specifies the SPI Direction Mode.
  450. * This parameter can be a value of @ref SPI_Direction
  451. * @retval None
  452. */
  453. #define IS_SPI_DIRECTION(__MODE__) (((__MODE__) == SPI_DIRECTION_2LINES) || \
  454. ((__MODE__) == SPI_DIRECTION_2LINES_RXONLY) || \
  455. ((__MODE__) == SPI_DIRECTION_1LINE))
  456. /** @brief Checks if SPI Direction Mode parameter is 2 lines.
  457. * @param __MODE__ specifies the SPI Direction Mode.
  458. * @retval None
  459. */
  460. #define IS_SPI_DIRECTION_2LINES(__MODE__) ((__MODE__) == SPI_DIRECTION_2LINES)
  461. /** @brief Checks if SPI Direction Mode parameter is 1 or 2 lines.
  462. * @param __MODE__ specifies the SPI Direction Mode.
  463. * @retval None
  464. */
  465. #define IS_SPI_DIRECTION_2LINES_OR_1LINE(__MODE__) (((__MODE__) == SPI_DIRECTION_2LINES) || \
  466. ((__MODE__) == SPI_DIRECTION_1LINE))
  467. /** @brief Checks if SPI Data Size parameter is in allowed range.
  468. * @param __DATASIZE__ specifies the SPI Data Size.
  469. * This parameter can be a value of @ref SPI_Data_Size
  470. * @retval None
  471. */
  472. #define IS_SPI_DATASIZE(__DATASIZE__) (((__DATASIZE__) == SPI_DATASIZE_16BIT) || \
  473. ((__DATASIZE__) == SPI_DATASIZE_8BIT))
  474. /** @brief Checks if SPI Serial clock steady state parameter is in allowed range.
  475. * @param __CPOL__ specifies the SPI serial clock steady state.
  476. * This parameter can be a value of @ref SPI_Clock_Polarity
  477. * @retval None
  478. */
  479. #define IS_SPI_CPOL(__CPOL__) (((__CPOL__) == SPI_POLARITY_LOW) || \
  480. ((__CPOL__) == SPI_POLARITY_HIGH))
  481. /** @brief Checks if SPI Clock Phase parameter is in allowed range.
  482. * @param __CPHA__ specifies the SPI Clock Phase.
  483. * This parameter can be a value of @ref SPI_Clock_Phase
  484. * @retval None
  485. */
  486. #define IS_SPI_CPHA(__CPHA__) (((__CPHA__) == SPI_PHASE_1EDGE) || \
  487. ((__CPHA__) == SPI_PHASE_2EDGE))
  488. /** @brief Checks if SPI Slave Select parameter is in allowed range.
  489. * @param __NSS__ specifies the SPI Slave Select management parameter.
  490. * This parameter can be a value of @ref SPI_Slave_Select_management
  491. * @retval None
  492. */
  493. #define IS_SPI_NSS(__NSS__) (((__NSS__) == SPI_NSS_SOFT) || \
  494. ((__NSS__) == SPI_NSS_HARD_INPUT) || \
  495. ((__NSS__) == SPI_NSS_HARD_OUTPUT))
  496. /** @brief Checks if SPI Baudrate prescaler parameter is in allowed range.
  497. * @param __PRESCALER__ specifies the SPI Baudrate prescaler.
  498. * This parameter can be a value of @ref SPI_BaudRate_Prescaler
  499. * @retval None
  500. */
  501. #define IS_SPI_BAUDRATE_PRESCALER(__PRESCALER__) (((__PRESCALER__) == SPI_BAUDRATEPRESCALER_2) || \
  502. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_4) || \
  503. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_8) || \
  504. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_16) || \
  505. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_32) || \
  506. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_64) || \
  507. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_128) || \
  508. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_256))
  509. /** @brief Checks if SPI MSB LSB transmission parameter is in allowed range.
  510. * @param __BIT__ specifies the SPI MSB LSB transmission (whether data transfer starts from MSB or LSB bit).
  511. * This parameter can be a value of @ref SPI_MSB_LSB_transmission
  512. * @retval None
  513. */
  514. #define IS_SPI_FIRST_BIT(__BIT__) (((__BIT__) == SPI_FIRSTBIT_MSB) || \
  515. ((__BIT__) == SPI_FIRSTBIT_LSB))
  516. /** @brief Checks if SPI TI mode parameter is disabled.
  517. * @param __MODE__ SPI_TIMODE_DISABLE. Device not support Ti Mode.
  518. * This parameter can be a value of @ref SPI_TI_mode
  519. * @retval None
  520. */
  521. #define IS_SPI_TIMODE(__MODE__) ((__MODE__) == SPI_TIMODE_DISABLE)
  522. /** @brief Checks if SPI CRC calculation enabled state is in allowed range.
  523. * @param __CALCULATION__ specifies the SPI CRC calculation enable state.
  524. * This parameter can be a value of @ref SPI_CRC_Calculation
  525. * @retval None
  526. */
  527. #define IS_SPI_CRC_CALCULATION(__CALCULATION__) (((__CALCULATION__) == SPI_CRCCALCULATION_DISABLE) || \
  528. ((__CALCULATION__) == SPI_CRCCALCULATION_ENABLE))
  529. /** @brief Checks if SPI polynomial value to be used for the CRC calculation, is in allowed range.
  530. * @param __POLYNOMIAL__ specifies the SPI polynomial value to be used for the CRC calculation.
  531. * This parameter must be a number between Min_Data = 0 and Max_Data = 65535
  532. * @retval None
  533. */
  534. #define IS_SPI_CRC_POLYNOMIAL(__POLYNOMIAL__) (((__POLYNOMIAL__) >= 0x1U) && \
  535. ((__POLYNOMIAL__) <= 0xFFFFU) && \
  536. (((__POLYNOMIAL__)&0x1U) != 0U))
  537. /** @brief Checks if DMA handle is valid.
  538. * @param __HANDLE__ specifies a DMA Handle.
  539. * @retval None
  540. */
  541. #define IS_SPI_DMA_HANDLE(__HANDLE__) ((__HANDLE__) != NULL)
  542. /**
  543. * @}
  544. */
  545. /* Private functions ---------------------------------------------------------*/
  546. /** @defgroup SPI_Private_Functions SPI Private Functions
  547. * @{
  548. */
  549. uint8_t SPI_ISCRCErrorValid(SPI_HandleTypeDef *hspi);
  550. /**
  551. * @}
  552. */
  553. /* Exported functions --------------------------------------------------------*/
  554. /** @addtogroup SPI_Exported_Functions
  555. * @{
  556. */
  557. /** @addtogroup SPI_Exported_Functions_Group1
  558. * @{
  559. */
  560. /* Initialization/de-initialization functions ********************************/
  561. HAL_StatusTypeDef HAL_SPI_Init(SPI_HandleTypeDef *hspi);
  562. HAL_StatusTypeDef HAL_SPI_DeInit(SPI_HandleTypeDef *hspi);
  563. void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi);
  564. void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi);
  565. /* Callbacks Register/UnRegister functions ***********************************/
  566. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  567. HAL_StatusTypeDef HAL_SPI_RegisterCallback(SPI_HandleTypeDef *hspi, HAL_SPI_CallbackIDTypeDef CallbackID,
  568. pSPI_CallbackTypeDef pCallback);
  569. HAL_StatusTypeDef HAL_SPI_UnRegisterCallback(SPI_HandleTypeDef *hspi, HAL_SPI_CallbackIDTypeDef CallbackID);
  570. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  571. /**
  572. * @}
  573. */
  574. /** @addtogroup SPI_Exported_Functions_Group2
  575. * @{
  576. */
  577. /* I/O operation functions ***************************************************/
  578. HAL_StatusTypeDef HAL_SPI_Transmit(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  579. HAL_StatusTypeDef HAL_SPI_Receive(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  580. HAL_StatusTypeDef HAL_SPI_TransmitReceive(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size,
  581. uint32_t Timeout);
  582. HAL_StatusTypeDef HAL_SPI_Transmit_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size);
  583. HAL_StatusTypeDef HAL_SPI_Receive_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size);
  584. HAL_StatusTypeDef HAL_SPI_TransmitReceive_IT(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData,
  585. uint16_t Size);
  586. HAL_StatusTypeDef HAL_SPI_Transmit_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size);
  587. HAL_StatusTypeDef HAL_SPI_Receive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size);
  588. HAL_StatusTypeDef HAL_SPI_TransmitReceive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData,
  589. uint16_t Size);
  590. HAL_StatusTypeDef HAL_SPI_DMAPause(SPI_HandleTypeDef *hspi);
  591. HAL_StatusTypeDef HAL_SPI_DMAResume(SPI_HandleTypeDef *hspi);
  592. HAL_StatusTypeDef HAL_SPI_DMAStop(SPI_HandleTypeDef *hspi);
  593. /* Transfer Abort functions */
  594. HAL_StatusTypeDef HAL_SPI_Abort(SPI_HandleTypeDef *hspi);
  595. HAL_StatusTypeDef HAL_SPI_Abort_IT(SPI_HandleTypeDef *hspi);
  596. void HAL_SPI_IRQHandler(SPI_HandleTypeDef *hspi);
  597. void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi);
  598. void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi);
  599. void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi);
  600. void HAL_SPI_TxHalfCpltCallback(SPI_HandleTypeDef *hspi);
  601. void HAL_SPI_RxHalfCpltCallback(SPI_HandleTypeDef *hspi);
  602. void HAL_SPI_TxRxHalfCpltCallback(SPI_HandleTypeDef *hspi);
  603. void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi);
  604. void HAL_SPI_AbortCpltCallback(SPI_HandleTypeDef *hspi);
  605. /**
  606. * @}
  607. */
  608. /** @addtogroup SPI_Exported_Functions_Group3
  609. * @{
  610. */
  611. /* Peripheral State and Error functions ***************************************/
  612. HAL_SPI_StateTypeDef HAL_SPI_GetState(SPI_HandleTypeDef *hspi);
  613. uint32_t HAL_SPI_GetError(SPI_HandleTypeDef *hspi);
  614. /**
  615. * @}
  616. */
  617. /**
  618. * @}
  619. */
  620. /**
  621. * @}
  622. */
  623. /**
  624. * @}
  625. */
  626. #ifdef __cplusplus
  627. }
  628. #endif
  629. #endif /* STM32F1xx_HAL_SPI_H */