stm32f1xx_hal_spi.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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. 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_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__) \
  284. do{ \
  285. (__HANDLE__)->State = HAL_SPI_STATE_RESET; \
  286. (__HANDLE__)->MspInitCallback = NULL; \
  287. (__HANDLE__)->MspDeInitCallback = NULL; \
  288. } while(0)
  289. #else
  290. #define __HAL_SPI_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_SPI_STATE_RESET)
  291. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  292. /** @brief Enable the specified SPI interrupts.
  293. * @param __HANDLE__ specifies the SPI Handle.
  294. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  295. * @param __INTERRUPT__ specifies the interrupt source to enable.
  296. * This parameter can be one of the following values:
  297. * @arg SPI_IT_TXE: Tx buffer empty interrupt enable
  298. * @arg SPI_IT_RXNE: RX buffer not empty interrupt enable
  299. * @arg SPI_IT_ERR: Error interrupt enable
  300. * @retval None
  301. */
  302. #define __HAL_SPI_ENABLE_IT(__HANDLE__, __INTERRUPT__) SET_BIT((__HANDLE__)->Instance->CR2, (__INTERRUPT__))
  303. /** @brief Disable the specified SPI interrupts.
  304. * @param __HANDLE__ specifies the SPI handle.
  305. * This parameter can be SPIx where x: 1, 2, or 3 to select the SPI peripheral.
  306. * @param __INTERRUPT__ specifies the interrupt source to disable.
  307. * This parameter can be one of the following values:
  308. * @arg SPI_IT_TXE: Tx buffer empty interrupt enable
  309. * @arg SPI_IT_RXNE: RX buffer not empty interrupt enable
  310. * @arg SPI_IT_ERR: Error interrupt enable
  311. * @retval None
  312. */
  313. #define __HAL_SPI_DISABLE_IT(__HANDLE__, __INTERRUPT__) CLEAR_BIT((__HANDLE__)->Instance->CR2, (__INTERRUPT__))
  314. /** @brief Check whether the specified SPI interrupt source is enabled or not.
  315. * @param __HANDLE__ specifies the SPI Handle.
  316. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  317. * @param __INTERRUPT__ specifies the SPI interrupt source to check.
  318. * This parameter can be one of the following values:
  319. * @arg SPI_IT_TXE: Tx buffer empty interrupt enable
  320. * @arg SPI_IT_RXNE: RX buffer not empty interrupt enable
  321. * @arg SPI_IT_ERR: Error interrupt enable
  322. * @retval The new state of __IT__ (TRUE or FALSE).
  323. */
  324. #define __HAL_SPI_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__) ((((__HANDLE__)->Instance->CR2\
  325. & (__INTERRUPT__)) == (__INTERRUPT__)) ? SET : RESET)
  326. /** @brief Check whether the specified SPI flag is set or not.
  327. * @param __HANDLE__ specifies the SPI Handle.
  328. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  329. * @param __FLAG__ specifies the flag to check.
  330. * This parameter can be one of the following values:
  331. * @arg SPI_FLAG_RXNE: Receive buffer not empty flag
  332. * @arg SPI_FLAG_TXE: Transmit buffer empty flag
  333. * @arg SPI_FLAG_CRCERR: CRC error flag
  334. * @arg SPI_FLAG_MODF: Mode fault flag
  335. * @arg SPI_FLAG_OVR: Overrun flag
  336. * @arg SPI_FLAG_BSY: Busy flag
  337. * @retval The new state of __FLAG__ (TRUE or FALSE).
  338. */
  339. #define __HAL_SPI_GET_FLAG(__HANDLE__, __FLAG__) ((((__HANDLE__)->Instance->SR) & (__FLAG__)) == (__FLAG__))
  340. /** @brief Clear the SPI CRCERR pending flag.
  341. * @param __HANDLE__ specifies the SPI Handle.
  342. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  343. * @retval None
  344. */
  345. #define __HAL_SPI_CLEAR_CRCERRFLAG(__HANDLE__) ((__HANDLE__)->Instance->SR = (uint16_t)(~SPI_FLAG_CRCERR))
  346. /** @brief Clear the SPI MODF pending flag.
  347. * @param __HANDLE__ specifies the SPI Handle.
  348. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  349. * @retval None
  350. */
  351. #define __HAL_SPI_CLEAR_MODFFLAG(__HANDLE__) \
  352. do{ \
  353. __IO uint32_t tmpreg_modf = 0x00U; \
  354. tmpreg_modf = (__HANDLE__)->Instance->SR; \
  355. CLEAR_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_SPE); \
  356. UNUSED(tmpreg_modf); \
  357. } while(0U)
  358. /** @brief Clear the SPI OVR pending flag.
  359. * @param __HANDLE__ specifies the SPI Handle.
  360. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  361. * @retval None
  362. */
  363. #define __HAL_SPI_CLEAR_OVRFLAG(__HANDLE__) \
  364. do{ \
  365. __IO uint32_t tmpreg_ovr = 0x00U; \
  366. tmpreg_ovr = (__HANDLE__)->Instance->DR; \
  367. tmpreg_ovr = (__HANDLE__)->Instance->SR; \
  368. UNUSED(tmpreg_ovr); \
  369. } while(0U)
  370. /** @brief Enable the SPI peripheral.
  371. * @param __HANDLE__ specifies the SPI Handle.
  372. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  373. * @retval None
  374. */
  375. #define __HAL_SPI_ENABLE(__HANDLE__) SET_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_SPE)
  376. /** @brief Disable the SPI peripheral.
  377. * @param __HANDLE__ specifies the SPI Handle.
  378. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  379. * @retval None
  380. */
  381. #define __HAL_SPI_DISABLE(__HANDLE__) CLEAR_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_SPE)
  382. /**
  383. * @}
  384. */
  385. /* Private constants ---------------------------------------------------------*/
  386. /** @defgroup SPI_Private_Constants SPI Private Constants
  387. * @{
  388. */
  389. #define SPI_INVALID_CRC_ERROR 0U /* CRC error wrongly detected */
  390. #define SPI_VALID_CRC_ERROR 1U /* CRC error is true */
  391. /**
  392. * @}
  393. */
  394. /* Private macros ------------------------------------------------------------*/
  395. /** @defgroup SPI_Private_Macros SPI Private Macros
  396. * @{
  397. */
  398. /** @brief Set the SPI transmit-only mode.
  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 SPI_1LINE_TX(__HANDLE__) SET_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_BIDIOE)
  404. /** @brief Set the SPI receive-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_RX(__HANDLE__) CLEAR_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_BIDIOE)
  410. /** @brief Reset the CRC calculation of the SPI.
  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_RESET_CRC(__HANDLE__) \
  416. do{ \
  417. CLEAR_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_CRCEN); \
  418. SET_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_CRCEN); \
  419. } while(0U)
  420. /** @brief Check whether the specified SPI flag is set or not.
  421. * @param __SR__ copy of SPI SR register.
  422. * @param __FLAG__ specifies the flag to check.
  423. * This parameter can be one of the following values:
  424. * @arg SPI_FLAG_RXNE: Receive buffer not empty flag
  425. * @arg SPI_FLAG_TXE: Transmit buffer empty flag
  426. * @arg SPI_FLAG_CRCERR: CRC error flag
  427. * @arg SPI_FLAG_MODF: Mode fault flag
  428. * @arg SPI_FLAG_OVR: Overrun flag
  429. * @arg SPI_FLAG_BSY: Busy flag
  430. * @retval SET or RESET.
  431. */
  432. #define SPI_CHECK_FLAG(__SR__, __FLAG__) ((((__SR__) & ((__FLAG__) & SPI_FLAG_MASK)) == \
  433. ((__FLAG__) & SPI_FLAG_MASK)) ? SET : RESET)
  434. /** @brief Check whether the specified SPI Interrupt is set or not.
  435. * @param __CR2__ copy of SPI CR2 register.
  436. * @param __INTERRUPT__ specifies the SPI interrupt source to check.
  437. * This parameter can be one of the following values:
  438. * @arg SPI_IT_TXE: Tx buffer empty interrupt enable
  439. * @arg SPI_IT_RXNE: RX buffer not empty interrupt enable
  440. * @arg SPI_IT_ERR: Error interrupt enable
  441. * @retval SET or RESET.
  442. */
  443. #define SPI_CHECK_IT_SOURCE(__CR2__, __INTERRUPT__) ((((__CR2__) & (__INTERRUPT__)) == \
  444. (__INTERRUPT__)) ? SET : RESET)
  445. /** @brief Checks if SPI Mode parameter is in allowed range.
  446. * @param __MODE__ specifies the SPI Mode.
  447. * This parameter can be a value of @ref SPI_Mode
  448. * @retval None
  449. */
  450. #define IS_SPI_MODE(__MODE__) (((__MODE__) == SPI_MODE_SLAVE) || \
  451. ((__MODE__) == SPI_MODE_MASTER))
  452. /** @brief Checks if SPI Direction Mode parameter is in allowed range.
  453. * @param __MODE__ specifies the SPI Direction Mode.
  454. * This parameter can be a value of @ref SPI_Direction
  455. * @retval None
  456. */
  457. #define IS_SPI_DIRECTION(__MODE__) (((__MODE__) == SPI_DIRECTION_2LINES) || \
  458. ((__MODE__) == SPI_DIRECTION_2LINES_RXONLY) || \
  459. ((__MODE__) == SPI_DIRECTION_1LINE))
  460. /** @brief Checks if SPI Direction Mode parameter is 2 lines.
  461. * @param __MODE__ specifies the SPI Direction Mode.
  462. * @retval None
  463. */
  464. #define IS_SPI_DIRECTION_2LINES(__MODE__) ((__MODE__) == SPI_DIRECTION_2LINES)
  465. /** @brief Checks if SPI Direction Mode parameter is 1 or 2 lines.
  466. * @param __MODE__ specifies the SPI Direction Mode.
  467. * @retval None
  468. */
  469. #define IS_SPI_DIRECTION_2LINES_OR_1LINE(__MODE__) (((__MODE__) == SPI_DIRECTION_2LINES) || \
  470. ((__MODE__) == SPI_DIRECTION_1LINE))
  471. /** @brief Checks if SPI Data Size parameter is in allowed range.
  472. * @param __DATASIZE__ specifies the SPI Data Size.
  473. * This parameter can be a value of @ref SPI_Data_Size
  474. * @retval None
  475. */
  476. #define IS_SPI_DATASIZE(__DATASIZE__) (((__DATASIZE__) == SPI_DATASIZE_16BIT) || \
  477. ((__DATASIZE__) == SPI_DATASIZE_8BIT))
  478. /** @brief Checks if SPI Serial clock steady state parameter is in allowed range.
  479. * @param __CPOL__ specifies the SPI serial clock steady state.
  480. * This parameter can be a value of @ref SPI_Clock_Polarity
  481. * @retval None
  482. */
  483. #define IS_SPI_CPOL(__CPOL__) (((__CPOL__) == SPI_POLARITY_LOW) || \
  484. ((__CPOL__) == SPI_POLARITY_HIGH))
  485. /** @brief Checks if SPI Clock Phase parameter is in allowed range.
  486. * @param __CPHA__ specifies the SPI Clock Phase.
  487. * This parameter can be a value of @ref SPI_Clock_Phase
  488. * @retval None
  489. */
  490. #define IS_SPI_CPHA(__CPHA__) (((__CPHA__) == SPI_PHASE_1EDGE) || \
  491. ((__CPHA__) == SPI_PHASE_2EDGE))
  492. /** @brief Checks if SPI Slave Select parameter is in allowed range.
  493. * @param __NSS__ specifies the SPI Slave Select management parameter.
  494. * This parameter can be a value of @ref SPI_Slave_Select_management
  495. * @retval None
  496. */
  497. #define IS_SPI_NSS(__NSS__) (((__NSS__) == SPI_NSS_SOFT) || \
  498. ((__NSS__) == SPI_NSS_HARD_INPUT) || \
  499. ((__NSS__) == SPI_NSS_HARD_OUTPUT))
  500. /** @brief Checks if SPI Baudrate prescaler parameter is in allowed range.
  501. * @param __PRESCALER__ specifies the SPI Baudrate prescaler.
  502. * This parameter can be a value of @ref SPI_BaudRate_Prescaler
  503. * @retval None
  504. */
  505. #define IS_SPI_BAUDRATE_PRESCALER(__PRESCALER__) (((__PRESCALER__) == SPI_BAUDRATEPRESCALER_2) || \
  506. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_4) || \
  507. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_8) || \
  508. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_16) || \
  509. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_32) || \
  510. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_64) || \
  511. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_128) || \
  512. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_256))
  513. /** @brief Checks if SPI MSB LSB transmission parameter is in allowed range.
  514. * @param __BIT__ specifies the SPI MSB LSB transmission (whether data transfer starts from MSB or LSB bit).
  515. * This parameter can be a value of @ref SPI_MSB_LSB_transmission
  516. * @retval None
  517. */
  518. #define IS_SPI_FIRST_BIT(__BIT__) (((__BIT__) == SPI_FIRSTBIT_MSB) || \
  519. ((__BIT__) == SPI_FIRSTBIT_LSB))
  520. /** @brief Checks if SPI TI mode parameter is disabled.
  521. * @param __MODE__ SPI_TIMODE_DISABLE. Device not support Ti Mode.
  522. * This parameter can be a value of @ref SPI_TI_mode
  523. * @retval None
  524. */
  525. #define IS_SPI_TIMODE(__MODE__) ((__MODE__) == SPI_TIMODE_DISABLE)
  526. /** @brief Checks if SPI CRC calculation enabled state is in allowed range.
  527. * @param __CALCULATION__ specifies the SPI CRC calculation enable state.
  528. * This parameter can be a value of @ref SPI_CRC_Calculation
  529. * @retval None
  530. */
  531. #define IS_SPI_CRC_CALCULATION(__CALCULATION__) (((__CALCULATION__) == SPI_CRCCALCULATION_DISABLE) || \
  532. ((__CALCULATION__) == SPI_CRCCALCULATION_ENABLE))
  533. /** @brief Checks if SPI polynomial value to be used for the CRC calculation, is in allowed range.
  534. * @param __POLYNOMIAL__ specifies the SPI polynomial value to be used for the CRC calculation.
  535. * This parameter must be a number between Min_Data = 0 and Max_Data = 65535
  536. * @retval None
  537. */
  538. #define IS_SPI_CRC_POLYNOMIAL(__POLYNOMIAL__) (((__POLYNOMIAL__) >= 0x1U) && \
  539. ((__POLYNOMIAL__) <= 0xFFFFU) && \
  540. (((__POLYNOMIAL__)&0x1U) != 0U))
  541. /** @brief Checks if DMA handle is valid.
  542. * @param __HANDLE__ specifies a DMA Handle.
  543. * @retval None
  544. */
  545. #define IS_SPI_DMA_HANDLE(__HANDLE__) ((__HANDLE__) != NULL)
  546. /**
  547. * @}
  548. */
  549. /* Private functions ---------------------------------------------------------*/
  550. /** @defgroup SPI_Private_Functions SPI Private Functions
  551. * @{
  552. */
  553. uint8_t SPI_ISCRCErrorValid(SPI_HandleTypeDef *hspi);
  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 /* STM32F1xx_HAL_SPI_H */