You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

731 lines
31 KiB

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