stm32l0xx_hal_spi.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_hal_spi.h
  4. * @author MCD Application Team
  5. * @version V1.7.0
  6. * @date 31-May-2016
  7. * @brief Header file of SPI HAL module.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  12. *
  13. * Redistribution and use in source and binary forms, with or without modification,
  14. * are permitted provided that the following conditions are met:
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  32. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ******************************************************************************
  36. */
  37. /* Define to prevent recursive inclusion -------------------------------------*/
  38. #ifndef __STM32L0xx_HAL_SPI_H
  39. #define __STM32L0xx_HAL_SPI_H
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. /* Includes ------------------------------------------------------------------*/
  44. #include "stm32l0xx_hal_def.h"
  45. /** @addtogroup STM32L0xx_HAL_Driver
  46. * @{
  47. */
  48. /** @defgroup SPI SPI
  49. * @{
  50. */
  51. /* Exported types ------------------------------------------------------------*/
  52. /** @defgroup SPI_Exported_Types SPI Exported Types
  53. * @{
  54. */
  55. /**
  56. * @brief SPI Configuration Structure definition
  57. */
  58. typedef struct
  59. {
  60. uint32_t Mode; /*!< Specifies the SPI operating mode.
  61. This parameter can be a value of @ref SPI_mode */
  62. uint32_t Direction; /*!< Specifies the SPI Directional mode state.
  63. This parameter can be a value of @ref SPI_Direction_mode */
  64. uint32_t DataSize; /*!< Specifies the SPI data size.
  65. This parameter can be a value of @ref SPI_data_size */
  66. uint32_t CLKPolarity; /*!< Specifies the serial clock steady state.
  67. This parameter can be a value of @ref SPI_Clock_Polarity */
  68. uint32_t CLKPhase; /*!< Specifies the clock active edge for the bit capture.
  69. This parameter can be a value of @ref SPI_Clock_Phase */
  70. uint32_t NSS; /*!< Specifies whether the NSS signal is managed by
  71. hardware (NSS pin) or by software using the SSI bit.
  72. This parameter can be a value of @ref SPI_Slave_Select_management */
  73. uint32_t BaudRatePrescaler; /*!< Specifies the Baud Rate prescaler value which will be
  74. used to configure the transmit and receive SCK clock.
  75. This parameter can be a value of @ref SPI_BaudRate_Prescaler
  76. @note The communication clock is derived from the master
  77. clock. The slave clock does not need to be set */
  78. uint32_t FirstBit; /*!< Specifies whether data transfers start from MSB or LSB bit.
  79. This parameter can be a value of @ref SPI_MSB_LSB_transmission */
  80. uint32_t TIMode; /*!< Specifies if the TI mode is enabled or not.
  81. This parameter can be a value of @ref SPI_TI_mode */
  82. uint32_t CRCCalculation; /*!< Specifies if the CRC calculation is enabled or not.
  83. This parameter can be a value of @ref SPI_CRC_Calculation */
  84. uint32_t CRCPolynomial; /*!< Specifies the polynomial used for the CRC calculation.
  85. This parameter must be a number between Min_Data = 0 and Max_Data = 65535 */
  86. }SPI_InitTypeDef;
  87. /**
  88. * @brief HAL SPI State structure definition
  89. */
  90. typedef enum
  91. {
  92. HAL_SPI_STATE_RESET = 0x00U, /*!< SPI not yet initialized or disabled */
  93. HAL_SPI_STATE_READY = 0x01U, /*!< SPI initialized and ready for use */
  94. HAL_SPI_STATE_BUSY = 0x02U, /*!< SPI process is ongoing */
  95. HAL_SPI_STATE_BUSY_TX = 0x12U, /*!< Data Transmission process is ongoing */
  96. HAL_SPI_STATE_BUSY_RX = 0x22U, /*!< Data Reception process is ongoing */
  97. HAL_SPI_STATE_BUSY_TX_RX = 0x32U, /*!< Data Transmission and Reception process is ongoing */
  98. HAL_SPI_STATE_ERROR = 0x03U /*!< SPI error state */
  99. }HAL_SPI_StateTypeDef;
  100. /**
  101. * @brief SPI handle Structure definition
  102. */
  103. typedef struct __SPI_HandleTypeDef
  104. {
  105. SPI_TypeDef *Instance; /*!< SPI registers base address */
  106. SPI_InitTypeDef Init; /*!< SPI communication parameters */
  107. uint8_t *pTxBuffPtr; /*!< Pointer to SPI Tx transfer Buffer */
  108. uint16_t TxXferSize; /*!< SPI Tx transfer size */
  109. uint16_t TxXferCount; /*!< SPI Tx Transfer Counter */
  110. uint8_t *pRxBuffPtr; /*!< Pointer to SPI Rx transfer Buffer */
  111. uint16_t RxXferSize; /*!< SPI Rx transfer size */
  112. uint16_t RxXferCount; /*!< SPI Rx Transfer Counter */
  113. DMA_HandleTypeDef *hdmatx; /*!< SPI Tx DMA handle parameters */
  114. DMA_HandleTypeDef *hdmarx; /*!< SPI Rx DMA handle parameters */
  115. void (*RxISR)(struct __SPI_HandleTypeDef * hspi); /*!< function pointer on Rx ISR */
  116. void (*TxISR)(struct __SPI_HandleTypeDef * hspi); /*!< function pointer on Tx ISR */
  117. HAL_LockTypeDef Lock; /*!< SPI locking object */
  118. __IO HAL_SPI_StateTypeDef State; /*!< SPI communication state */
  119. __IO uint32_t ErrorCode; /*!< SPI Error code */
  120. }SPI_HandleTypeDef;
  121. /**
  122. * @}
  123. */
  124. /* Exported constants --------------------------------------------------------*/
  125. /** @defgroup SPI_Exported_Constants SPI Exported Constants
  126. * @{
  127. */
  128. /**
  129. * @defgroup SPI_ErrorCode SPI Error Code
  130. * @{
  131. */
  132. #define HAL_SPI_ERROR_NONE ((uint32_t)0x00U) /*!< No error */
  133. #define HAL_SPI_ERROR_MODF ((uint32_t)0x01U) /*!< MODF error */
  134. #define HAL_SPI_ERROR_CRC ((uint32_t)0x02U) /*!< CRC error */
  135. #define HAL_SPI_ERROR_OVR ((uint32_t)0x04U) /*!< OVR error */
  136. #define HAL_SPI_ERROR_FRE ((uint32_t)0x08U) /*!< FRE error */
  137. #define HAL_SPI_ERROR_DMA ((uint32_t)0x10U) /*!< DMA transfer error */
  138. #define HAL_SPI_ERROR_FLAG ((uint32_t)0x20U) /*!< Flag: RXNE,TXE, BSY */
  139. /**
  140. * @}
  141. */
  142. /** @defgroup SPI_mode SPI mode
  143. * @{
  144. */
  145. #define SPI_MODE_SLAVE ((uint32_t)0x00000000U)
  146. #define SPI_MODE_MASTER (SPI_CR1_MSTR | SPI_CR1_SSI)
  147. /**
  148. * @}
  149. */
  150. /** @defgroup SPI_Direction_mode SPI Direction mode
  151. * @{
  152. */
  153. #define SPI_DIRECTION_2LINES ((uint32_t)0x00000000U)
  154. #define SPI_DIRECTION_2LINES_RXONLY SPI_CR1_RXONLY
  155. #define SPI_DIRECTION_1LINE SPI_CR1_BIDIMODE
  156. /**
  157. * @}
  158. */
  159. /** @defgroup SPI_data_size SPI data size
  160. * @{
  161. */
  162. #define SPI_DATASIZE_8BIT ((uint32_t)0x00000000U)
  163. #define SPI_DATASIZE_16BIT SPI_CR1_DFF
  164. /**
  165. * @}
  166. */
  167. /** @defgroup SPI_Clock_Polarity SPI Clock Polarity
  168. * @{
  169. */
  170. #define SPI_POLARITY_LOW ((uint32_t)0x00000000U)
  171. #define SPI_POLARITY_HIGH SPI_CR1_CPOL
  172. /**
  173. * @}
  174. */
  175. /** @defgroup SPI_Clock_Phase SPI Clock Phase
  176. * @{
  177. */
  178. #define SPI_PHASE_1EDGE ((uint32_t)0x00000000U)
  179. #define SPI_PHASE_2EDGE SPI_CR1_CPHA
  180. /**
  181. * @}
  182. */
  183. /** @defgroup SPI_Slave_Select_management SPI Slave Select management
  184. * @{
  185. */
  186. #define SPI_NSS_SOFT SPI_CR1_SSM
  187. #define SPI_NSS_HARD_INPUT ((uint32_t)0x00000000U)
  188. #define SPI_NSS_HARD_OUTPUT ((uint32_t)(SPI_CR2_SSOE << 16U))
  189. /**
  190. * @}
  191. */
  192. /** @defgroup SPI_BaudRate_Prescaler SPI BaudRate Prescaler
  193. * @{
  194. */
  195. #define SPI_BAUDRATEPRESCALER_2 ((uint32_t)0x00000000U)
  196. #define SPI_BAUDRATEPRESCALER_4 ((uint32_t)SPI_CR1_BR_0)
  197. #define SPI_BAUDRATEPRESCALER_8 ((uint32_t)SPI_CR1_BR_1)
  198. #define SPI_BAUDRATEPRESCALER_16 ((uint32_t)SPI_CR1_BR_1 | SPI_CR1_BR_0)
  199. #define SPI_BAUDRATEPRESCALER_32 ((uint32_t)SPI_CR1_BR_2)
  200. #define SPI_BAUDRATEPRESCALER_64 ((uint32_t)SPI_CR1_BR_2 | SPI_CR1_BR_0)
  201. #define SPI_BAUDRATEPRESCALER_128 ((uint32_t)SPI_CR1_BR_2 | SPI_CR1_BR_1)
  202. #define SPI_BAUDRATEPRESCALER_256 ((uint32_t)SPI_CR1_BR_2 | SPI_CR1_BR_1 | SPI_CR1_BR_0)
  203. /**
  204. * @}
  205. */
  206. /** @defgroup SPI_MSB_LSB_transmission SPI MSB LSB transmission
  207. * @{
  208. */
  209. #define SPI_FIRSTBIT_MSB ((uint32_t)0x00000000U)
  210. #define SPI_FIRSTBIT_LSB SPI_CR1_LSBFIRST
  211. /**
  212. * @}
  213. */
  214. /** @defgroup SPI_TI_mode SPI TI mode
  215. * @{
  216. */
  217. #define SPI_TIMODE_DISABLE ((uint32_t)0x00000000U)
  218. #define SPI_TIMODE_ENABLE SPI_CR2_FRF
  219. /**
  220. * @}
  221. */
  222. /** @defgroup SPI_CRC_Calculation SPI CRC Calculation
  223. * @{
  224. */
  225. #define SPI_CRCCALCULATION_DISABLE ((uint32_t)0x00000000U)
  226. #define SPI_CRCCALCULATION_ENABLE SPI_CR1_CRCEN
  227. /**
  228. * @}
  229. */
  230. /** @defgroup SPI_Interrupt_configuration_definition SPI Interrupt configuration definition
  231. * @{
  232. */
  233. #define SPI_IT_TXE SPI_CR2_TXEIE
  234. #define SPI_IT_RXNE SPI_CR2_RXNEIE
  235. #define SPI_IT_ERR SPI_CR2_ERRIE
  236. /**
  237. * @}
  238. */
  239. /** @defgroup SPI_Flag_definition SPI Flag definition
  240. * @{
  241. */
  242. #define SPI_FLAG_RXNE SPI_SR_RXNE
  243. #define SPI_FLAG_TXE SPI_SR_TXE
  244. #define SPI_FLAG_CRCERR SPI_SR_CRCERR
  245. #define SPI_FLAG_MODF SPI_SR_MODF
  246. #define SPI_FLAG_OVR SPI_SR_OVR
  247. #define SPI_FLAG_BSY SPI_SR_BSY
  248. #define SPI_FLAG_FRE SPI_SR_FRE
  249. /**
  250. * @}
  251. */
  252. /**
  253. * @}
  254. */
  255. /* Exported macro ------------------------------------------------------------*/
  256. /** @defgroup SPI_Exported_Macros SPI Exported Macros
  257. * @{
  258. */
  259. /** @brief Reset SPI handle state
  260. * @param __HANDLE__: specifies the SPI handle.
  261. * This parameter can be SPIx where x: 1 or 2 to select the SPI peripheral.
  262. * @retval None
  263. */
  264. #define __HAL_SPI_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_SPI_STATE_RESET)
  265. /** @brief Enable the specified SPI interrupts.
  266. * @param __HANDLE__: specifies the SPI handle.
  267. * This parameter can be SPIx where x: 1 or 2 to select the SPI peripheral.
  268. * @param __INTERRUPT__: specifies the interrupt source to enable.
  269. * This parameter can be one of the following values:
  270. * @arg SPI_IT_TXE: Tx buffer empty interrupt enable
  271. * @arg SPI_IT_RXNE: RX buffer not empty interrupt enable
  272. * @arg SPI_IT_ERR: Error interrupt enable
  273. * @retval None
  274. */
  275. #define __HAL_SPI_ENABLE_IT(__HANDLE__, __INTERRUPT__) SET_BIT((__HANDLE__)->Instance->CR2, (__INTERRUPT__))
  276. /** @brief Disable the specified SPI interrupts.
  277. * @param __HANDLE__: specifies the SPI handle.
  278. * This parameter can be SPIx where x: 1 or 2 to select the SPI peripheral.
  279. * @param __INTERRUPT__: specifies the interrupt source to disable.
  280. * This parameter can be one of the following values:
  281. * @arg SPI_IT_TXE: Tx buffer empty interrupt enable
  282. * @arg SPI_IT_RXNE: RX buffer not empty interrupt enable
  283. * @arg SPI_IT_ERR: Error interrupt enable
  284. * @retval None
  285. */
  286. #define __HAL_SPI_DISABLE_IT(__HANDLE__, __INTERRUPT__) CLEAR_BIT((__HANDLE__)->Instance->CR2, (__INTERRUPT__))
  287. /** @brief Check if the specified SPI interrupt source is enabled or disabled.
  288. * @param __HANDLE__: specifies the SPI handle.
  289. * This parameter can be SPIx where x: 1 or 2 to select the SPI peripheral.
  290. * @param __INTERRUPT__: specifies the SPI interrupt source to check.
  291. * This parameter can be one of the following values:
  292. * @arg SPI_IT_TXE: Tx buffer empty interrupt enable
  293. * @arg SPI_IT_RXNE: RX buffer not empty interrupt enable
  294. * @arg SPI_IT_ERR: Error interrupt enable
  295. * @retval The new state of __IT__ (TRUE or FALSE).
  296. */
  297. #define __HAL_SPI_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__) ((((__HANDLE__)->Instance->CR2 & (__INTERRUPT__)) == (__INTERRUPT__)) ? SET : RESET)
  298. /** @brief Check whether the specified SPI flag is set or not.
  299. * @param __HANDLE__: specifies the SPI handle.
  300. * This parameter can be SPIx where x: 1 or 2 to select the SPI peripheral.
  301. * @param __FLAG__: specifies the flag to check.
  302. * This parameter can be one of the following values:
  303. * @arg SPI_FLAG_RXNE: Receive buffer not empty flag
  304. * @arg SPI_FLAG_TXE: Transmit buffer empty flag
  305. * @arg SPI_FLAG_CRCERR: CRC error flag
  306. * @arg SPI_FLAG_MODF: Mode fault flag
  307. * @arg SPI_FLAG_OVR: Overrun flag
  308. * @arg SPI_FLAG_BSY: Busy flag
  309. * @arg SPI_FLAG_FRE: Frame format error flag
  310. * @retval The new state of __FLAG__ (TRUE or FALSE).
  311. */
  312. #define __HAL_SPI_GET_FLAG(__HANDLE__, __FLAG__) ((((__HANDLE__)->Instance->SR) & (__FLAG__)) == (__FLAG__))
  313. /** @brief Clear the SPI CRCERR pending flag.
  314. * @param __HANDLE__: specifies the SPI handle.
  315. * This parameter can be SPIx where x: 1 or 2 to select the SPI peripheral.
  316. * @retval None
  317. */
  318. #define __HAL_SPI_CLEAR_CRCERRFLAG(__HANDLE__) ((__HANDLE__)->Instance->SR = ~(SPI_FLAG_CRCERR))
  319. /** @brief Clear the SPI MODF pending flag.
  320. * @param __HANDLE__: specifies the SPI handle.
  321. * This parameter can be SPIx where x: 1 or 2 to select the SPI peripheral.
  322. * @retval None
  323. */
  324. #define __HAL_SPI_CLEAR_MODFFLAG(__HANDLE__) \
  325. do{ \
  326. __IO uint32_t tmpreg; \
  327. tmpreg = (__HANDLE__)->Instance->SR; \
  328. (__HANDLE__)->Instance->CR1 &= (~SPI_CR1_SPE); \
  329. UNUSED(tmpreg); \
  330. } while(0)
  331. /** @brief Clear the SPI OVR pending flag.
  332. * @param __HANDLE__: specifies the SPI handle.
  333. * This parameter can be SPIx where x: 1 or 2 to select the SPI peripheral.
  334. * @retval None
  335. */
  336. #define __HAL_SPI_CLEAR_OVRFLAG(__HANDLE__) \
  337. do{ \
  338. __IO uint32_t tmpreg; \
  339. tmpreg = (__HANDLE__)->Instance->DR; \
  340. tmpreg = (__HANDLE__)->Instance->SR; \
  341. UNUSED(tmpreg); \
  342. } while(0)
  343. /** @brief Clear the SPI FRE pending flag.
  344. * @param __HANDLE__: specifies the SPI handle.
  345. * This parameter can be SPIx where x: 1 or 2 to select the SPI peripheral.
  346. * @retval None
  347. */
  348. #define __HAL_SPI_CLEAR_FREFLAG(__HANDLE__) \
  349. do{ \
  350. __IO uint32_t tmpreg; \
  351. tmpreg = (__HANDLE__)->Instance->SR; \
  352. UNUSED(tmpreg); \
  353. } while(0)
  354. /** @brief Enables the SPI.
  355. * @param __HANDLE__: specifies the SPI Handle.
  356. * This parameter can be SPIx where x: 1 or 2 to select the SPI peripheral.
  357. * @retval None
  358. */
  359. #define __HAL_SPI_ENABLE(__HANDLE__) SET_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_SPE)
  360. /** @brief Disables the SPI.
  361. * @param __HANDLE__: specifies the SPI Handle.
  362. * This parameter can be SPIx where x: 1 or 2 to select the SPI peripheral.
  363. * @retval None
  364. */
  365. #define __HAL_SPI_DISABLE(__HANDLE__) CLEAR_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_SPE)
  366. /**
  367. * @}
  368. */
  369. /* Private macros -----------------------------------------------------------*/
  370. /** @defgroup SPI_Private_Macros SPI Private Macros
  371. * @{
  372. */
  373. /** @brief Checks if SPI Mode parameter is in allowed range.
  374. * @param __MODE__: specifies the SPI Mode.
  375. * This parameter can be a value of @ref SPI_mode
  376. * @retval None
  377. */
  378. #define IS_SPI_MODE(__MODE__) (((__MODE__) == SPI_MODE_SLAVE) || ((__MODE__) == SPI_MODE_MASTER))
  379. /** @brief Checks if SPI Direction Mode parameter is in allowed range.
  380. * @param __MODE__: specifies the SPI Direction Mode.
  381. * This parameter can be a value of @ref SPI_Direction_mode
  382. * @retval None
  383. */
  384. #define IS_SPI_DIRECTION_MODE(__MODE__) (((__MODE__) == SPI_DIRECTION_2LINES) || \
  385. ((__MODE__) == SPI_DIRECTION_2LINES_RXONLY) || \
  386. ((__MODE__) == SPI_DIRECTION_1LINE))
  387. /** @brief Checks if SPI Direction Mode parameter is 1 or 2 lines.
  388. * @param __MODE__: specifies the SPI Direction Mode.
  389. * @retval None
  390. */
  391. #define IS_SPI_DIRECTION_2LINES_OR_1LINE(__MODE__) (((__MODE__) == SPI_DIRECTION_2LINES) || \
  392. ((__MODE__) == SPI_DIRECTION_1LINE))
  393. /** @brief Checks if SPI Direction Mode parameter is 2 lines.
  394. * @param __MODE__: specifies the SPI Direction Mode.
  395. * @retval None
  396. */
  397. #define IS_SPI_DIRECTION_2LINES(__MODE__) ((__MODE__) == SPI_DIRECTION_2LINES)
  398. /** @brief Checks if SPI Data Size parameter is in allowed range.
  399. * @param __DATASIZE__: specifies the SPI Data Size.
  400. * This parameter can be a value of @ref SPI_data_size
  401. * @retval None
  402. */
  403. #define IS_SPI_DATASIZE(__DATASIZE__) (((__DATASIZE__) == SPI_DATASIZE_16BIT) || \
  404. ((__DATASIZE__) == SPI_DATASIZE_8BIT))
  405. /** @brief Checks if SPI Serial clock steady state parameter is in allowed range.
  406. * @param __CPOL__: specifies the SPI serial clock steady state.
  407. * This parameter can be a value of @ref SPI_Clock_Polarity
  408. * @retval None
  409. */
  410. #define IS_SPI_CPOL(__CPOL__) (((__CPOL__) == SPI_POLARITY_LOW) || \
  411. ((__CPOL__) == SPI_POLARITY_HIGH))
  412. /** @brief Checks if SPI Clock Phase parameter is in allowed range.
  413. * @param __CPHA__: specifies the SPI Clock Phase.
  414. * This parameter can be a value of @ref SPI_Clock_Phase
  415. * @retval None
  416. */
  417. #define IS_SPI_CPHA(__CPHA__) (((__CPHA__) == SPI_PHASE_1EDGE) || \
  418. ((__CPHA__) == SPI_PHASE_2EDGE))
  419. /** @brief Checks if SPI Slave select parameter is in allowed range.
  420. * @param __NSS__: specifies the SPI Slave Slelect management parameter.
  421. * This parameter can be a value of @ref SPI_Slave_Select_management
  422. * @retval None
  423. */
  424. #define IS_SPI_NSS(__NSS__) (((__NSS__) == SPI_NSS_SOFT) || \
  425. ((__NSS__) == SPI_NSS_HARD_INPUT) || \
  426. ((__NSS__) == SPI_NSS_HARD_OUTPUT))
  427. /** @brief Checks if SPI Baudrate prescaler parameter is in allowed range.
  428. * @param __PRESCALER__: specifies the SPI Baudrate prescaler.
  429. * This parameter can be a value of @ref SPI_BaudRate_Prescaler
  430. * @retval None
  431. */
  432. #define IS_SPI_BAUDRATE_PRESCALER(__PRESCALER__) (((__PRESCALER__) == SPI_BAUDRATEPRESCALER_2) || \
  433. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_4) || \
  434. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_8) || \
  435. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_16) || \
  436. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_32) || \
  437. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_64) || \
  438. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_128) || \
  439. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_256))
  440. /** @brief Checks if SPI MSB LSB transmission parameter is in allowed range.
  441. * @param __BIT__: specifies the SPI MSB LSB transmission (whether data transfer starts from MSB or LSB bit).
  442. * This parameter can be a value of @ref SPI_MSB_LSB_transmission
  443. * @retval None
  444. */
  445. #define IS_SPI_FIRST_BIT(__BIT__) (((__BIT__) == SPI_FIRSTBIT_MSB) || \
  446. ((__BIT__) == SPI_FIRSTBIT_LSB))
  447. /** @brief Checks if SPI TI mode parameter is in allowed range.
  448. * @param __MODE__: specifies the SPI TI mode.
  449. * This parameter can be a value of @ref SPI_TI_mode
  450. * @retval None
  451. */
  452. #define IS_SPI_TIMODE(__MODE__) (((__MODE__) == SPI_TIMODE_DISABLE) || \
  453. ((__MODE__) == SPI_TIMODE_ENABLE))
  454. /** @brief Checks if SPI CRC calculation enabled state is in allowed range.
  455. * @param __CALCULATION__: specifies the SPI CRC calculation enable state.
  456. * This parameter can be a value of @ref SPI_CRC_Calculation
  457. * @retval None
  458. */
  459. #define IS_SPI_CRC_CALCULATION(__CALCULATION__) (((__CALCULATION__) == SPI_CRCCALCULATION_DISABLE) || \
  460. ((__CALCULATION__) == SPI_CRCCALCULATION_ENABLE))
  461. /** @brief Checks if SPI polynomial value to be used for the CRC calculation, is in allowed range.
  462. * @param __POLYNOMIAL__: specifies the SPI polynomial value to be used for the CRC calculation.
  463. * This parameter must be a number between Min_Data = 0 and Max_Data = 65535
  464. * @retval None
  465. */
  466. #define IS_SPI_CRC_POLYNOMIAL(__POLYNOMIAL__) (((__POLYNOMIAL__) >= 0x1U) && ((__POLYNOMIAL__) <= 0xFFFFU))
  467. /** @brief Sets the SPI transmit-only mode.
  468. * @param __HANDLE__: specifies the SPI Handle.
  469. * This parameter can be SPIx where x: 1 or 2 to select the SPI peripheral.
  470. * @retval None
  471. */
  472. #define SPI_1LINE_TX(__HANDLE__) SET_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_BIDIOE)
  473. /** @brief Sets the SPI receive-only mode.
  474. * @param __HANDLE__: specifies the SPI Handle.
  475. * This parameter can be SPIx where x: 1 or 2 to select the SPI peripheral.
  476. * @retval None
  477. */
  478. #define SPI_1LINE_RX(__HANDLE__) CLEAR_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_BIDIOE)
  479. /** @brief Resets the CRC calculation of the SPI.
  480. * @param __HANDLE__: specifies the SPI Handle.
  481. * This parameter can be SPIx where x: 1 or 2 to select the SPI peripheral.
  482. * @retval None
  483. */
  484. #define SPI_RESET_CRC(__HANDLE__) do{CLEAR_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_CRCEN);\
  485. SET_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_CRCEN);}while(0)
  486. /**
  487. * @}
  488. */
  489. /* Exported functions --------------------------------------------------------*/
  490. /** @defgroup SPI_Exported_Functions SPI Exported Functions
  491. * @{
  492. */
  493. /* Initialization/de-initialization functions **********************************/
  494. /** @defgroup SPI_Exported_Functions_Group1 Initialization and de-initialization functions
  495. * @{
  496. */
  497. HAL_StatusTypeDef HAL_SPI_Init(SPI_HandleTypeDef *hspi);
  498. HAL_StatusTypeDef HAL_SPI_DeInit (SPI_HandleTypeDef *hspi);
  499. void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi);
  500. void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi);
  501. /**
  502. * @}
  503. */
  504. /* I/O operation functions *****************************************************/
  505. /** @defgroup SPI_Exported_Functions_Group2 IO operation functions
  506. * @{
  507. */
  508. HAL_StatusTypeDef HAL_SPI_Transmit(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  509. HAL_StatusTypeDef HAL_SPI_Receive(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  510. HAL_StatusTypeDef HAL_SPI_TransmitReceive(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout);
  511. HAL_StatusTypeDef HAL_SPI_Transmit_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size);
  512. HAL_StatusTypeDef HAL_SPI_Receive_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size);
  513. HAL_StatusTypeDef HAL_SPI_TransmitReceive_IT(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size);
  514. HAL_StatusTypeDef HAL_SPI_Transmit_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size);
  515. HAL_StatusTypeDef HAL_SPI_Receive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size);
  516. HAL_StatusTypeDef HAL_SPI_TransmitReceive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size);
  517. HAL_StatusTypeDef HAL_SPI_DMAPause(SPI_HandleTypeDef *hspi);
  518. HAL_StatusTypeDef HAL_SPI_DMAResume(SPI_HandleTypeDef *hspi);
  519. HAL_StatusTypeDef HAL_SPI_DMAStop(SPI_HandleTypeDef *hspi);
  520. void HAL_SPI_IRQHandler(SPI_HandleTypeDef *hspi);
  521. void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi);
  522. void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi);
  523. void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi);
  524. void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi);
  525. void HAL_SPI_TxHalfCpltCallback(SPI_HandleTypeDef *hspi);
  526. void HAL_SPI_RxHalfCpltCallback(SPI_HandleTypeDef *hspi);
  527. void HAL_SPI_TxRxHalfCpltCallback(SPI_HandleTypeDef *hspi);
  528. /**
  529. * @}
  530. */
  531. /* Peripheral State and Control functions **************************************/
  532. /** @defgroup SPI_Exported_Functions_Group3 Peripheral State and Errors functions
  533. * @{
  534. */
  535. HAL_SPI_StateTypeDef HAL_SPI_GetState(SPI_HandleTypeDef *hspi);
  536. uint32_t HAL_SPI_GetError(SPI_HandleTypeDef *hspi);
  537. /**
  538. * @}
  539. */
  540. /**
  541. * @}
  542. */
  543. /* Private group definition ------------------------------------------------------*/
  544. /** @defgroup SPI_Private_Macros SPI Private Macros
  545. * @{
  546. */
  547. /**
  548. * @}
  549. */
  550. /* Define the private group ***********************************/
  551. /**************************************************************/
  552. /** @defgroup SPI_Private SPI Private
  553. * @{
  554. */
  555. /**
  556. * @}
  557. */
  558. /**************************************************************/
  559. /**
  560. * @}
  561. */
  562. /**
  563. * @}
  564. */
  565. #ifdef __cplusplus
  566. }
  567. #endif
  568. #endif /* __STM32L0xx_HAL_SPI_H */
  569. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/