Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
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.
 
 
 
 
 
 

743 lines
32 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_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 STM32L1xx_HAL_SPI_H
  21. #define STM32L1xx_HAL_SPI_H
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /* Includes ------------------------------------------------------------------*/
  26. #include "stm32l1xx_hal_def.h"
  27. /** @addtogroup STM32L1xx_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. * @brief SPI TI Mode not supported for Category 1 and 2
  236. * @{
  237. */
  238. #define SPI_TIMODE_DISABLE (0x00000000U)
  239. #if defined(SPI_CR2_FRF)
  240. #define SPI_TIMODE_ENABLE SPI_CR2_FRF
  241. #endif
  242. /**
  243. * @}
  244. */
  245. /** @defgroup SPI_CRC_Calculation SPI CRC Calculation
  246. * @{
  247. */
  248. #define SPI_CRCCALCULATION_DISABLE (0x00000000U)
  249. #define SPI_CRCCALCULATION_ENABLE SPI_CR1_CRCEN
  250. /**
  251. * @}
  252. */
  253. /** @defgroup SPI_Interrupt_definition SPI Interrupt Definition
  254. * @{
  255. */
  256. #define SPI_IT_TXE SPI_CR2_TXEIE
  257. #define SPI_IT_RXNE SPI_CR2_RXNEIE
  258. #define SPI_IT_ERR SPI_CR2_ERRIE
  259. /**
  260. * @}
  261. */
  262. /** @defgroup SPI_Flags_definition SPI Flags Definition
  263. * @{
  264. */
  265. #define SPI_FLAG_RXNE SPI_SR_RXNE /* SPI status flag: Rx buffer not empty flag */
  266. #define SPI_FLAG_TXE SPI_SR_TXE /* SPI status flag: Tx buffer empty flag */
  267. #define SPI_FLAG_BSY SPI_SR_BSY /* SPI status flag: Busy flag */
  268. #define SPI_FLAG_CRCERR SPI_SR_CRCERR /* SPI Error flag: CRC error flag */
  269. #define SPI_FLAG_MODF SPI_SR_MODF /* SPI Error flag: Mode fault flag */
  270. #define SPI_FLAG_OVR SPI_SR_OVR /* SPI Error flag: Overrun flag */
  271. #if defined(SPI_CR2_FRF)
  272. #define SPI_FLAG_FRE SPI_SR_FRE /* SPI Error flag: TI mode frame format error flag */
  273. #define SPI_FLAG_MASK (SPI_SR_RXNE | SPI_SR_TXE | SPI_SR_BSY | SPI_SR_CRCERR\
  274. | SPI_SR_MODF | SPI_SR_OVR | SPI_SR_FRE)
  275. #else
  276. #define SPI_FLAG_MASK (SPI_SR_RXNE | SPI_SR_TXE | SPI_SR_BSY\
  277. | SPI_SR_CRCERR | SPI_SR_MODF | SPI_SR_OVR)
  278. #endif
  279. /**
  280. * @}
  281. */
  282. /**
  283. * @}
  284. */
  285. /* Exported macros -----------------------------------------------------------*/
  286. /** @defgroup SPI_Exported_Macros SPI Exported Macros
  287. * @{
  288. */
  289. /** @brief Reset SPI handle state.
  290. * @param __HANDLE__ specifies the SPI Handle.
  291. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  292. * @retval None
  293. */
  294. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  295. #define __HAL_SPI_RESET_HANDLE_STATE(__HANDLE__) do{ \
  296. (__HANDLE__)->State = HAL_SPI_STATE_RESET; \
  297. (__HANDLE__)->MspInitCallback = NULL; \
  298. (__HANDLE__)->MspDeInitCallback = NULL; \
  299. } while(0)
  300. #else
  301. #define __HAL_SPI_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_SPI_STATE_RESET)
  302. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  303. /** @brief Enable the specified SPI interrupts.
  304. * @param __HANDLE__ specifies the SPI Handle.
  305. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  306. * @param __INTERRUPT__ specifies the interrupt source to enable.
  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_ENABLE_IT(__HANDLE__, __INTERRUPT__) SET_BIT((__HANDLE__)->Instance->CR2, (__INTERRUPT__))
  314. /** @brief Disable the specified SPI interrupts.
  315. * @param __HANDLE__ specifies the SPI handle.
  316. * This parameter can be SPIx where x: 1, 2, or 3 to select the SPI peripheral.
  317. * @param __INTERRUPT__ specifies the interrupt source to disable.
  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 None
  323. */
  324. #define __HAL_SPI_DISABLE_IT(__HANDLE__, __INTERRUPT__) CLEAR_BIT((__HANDLE__)->Instance->CR2, (__INTERRUPT__))
  325. /** @brief Check whether the specified SPI interrupt source is enabled 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 __INTERRUPT__ specifies the SPI interrupt source to check.
  329. * This parameter can be one of the following values:
  330. * @arg SPI_IT_TXE: Tx buffer empty interrupt enable
  331. * @arg SPI_IT_RXNE: RX buffer not empty interrupt enable
  332. * @arg SPI_IT_ERR: Error interrupt enable
  333. * @retval The new state of __IT__ (TRUE or FALSE).
  334. */
  335. #define __HAL_SPI_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__) ((((__HANDLE__)->Instance->CR2\
  336. & (__INTERRUPT__)) == (__INTERRUPT__)) ? SET : RESET)
  337. /** @brief Check whether the specified SPI flag is set or not.
  338. * @param __HANDLE__ specifies the SPI Handle.
  339. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  340. * @param __FLAG__ specifies the flag to check.
  341. * This parameter can be one of the following values:
  342. * @arg SPI_FLAG_RXNE: Receive buffer not empty flag
  343. * @arg SPI_FLAG_TXE: Transmit buffer empty flag
  344. * @arg SPI_FLAG_CRCERR: CRC error flag
  345. * @arg SPI_FLAG_MODF: Mode fault flag
  346. * @arg SPI_FLAG_OVR: Overrun flag
  347. * @arg SPI_FLAG_BSY: Busy flag
  348. * @arg SPI_FLAG_FRE: Frame format error flag
  349. * @retval The new state of __FLAG__ (TRUE or FALSE).
  350. */
  351. #define __HAL_SPI_GET_FLAG(__HANDLE__, __FLAG__) ((((__HANDLE__)->Instance->SR) & (__FLAG__)) == (__FLAG__))
  352. /** @brief Clear the SPI CRCERR pending flag.
  353. * @param __HANDLE__ specifies the SPI Handle.
  354. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  355. * @retval None
  356. */
  357. #define __HAL_SPI_CLEAR_CRCERRFLAG(__HANDLE__) ((__HANDLE__)->Instance->SR = (uint16_t)(~SPI_FLAG_CRCERR))
  358. /** @brief Clear the SPI MODF 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_MODFFLAG(__HANDLE__) \
  364. do{ \
  365. __IO uint32_t tmpreg_modf = 0x00U; \
  366. tmpreg_modf = (__HANDLE__)->Instance->SR; \
  367. CLEAR_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_SPE); \
  368. UNUSED(tmpreg_modf); \
  369. } while(0U)
  370. /** @brief Clear the SPI OVR pending flag.
  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_CLEAR_OVRFLAG(__HANDLE__) \
  376. do{ \
  377. __IO uint32_t tmpreg_ovr = 0x00U; \
  378. tmpreg_ovr = (__HANDLE__)->Instance->DR; \
  379. tmpreg_ovr = (__HANDLE__)->Instance->SR; \
  380. UNUSED(tmpreg_ovr); \
  381. } while(0U)
  382. /** @brief Clear the SPI FRE pending flag.
  383. * @param __HANDLE__ specifies the SPI Handle.
  384. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  385. * @retval None
  386. */
  387. #define __HAL_SPI_CLEAR_FREFLAG(__HANDLE__) \
  388. do{ \
  389. __IO uint32_t tmpreg_fre = 0x00U; \
  390. tmpreg_fre = (__HANDLE__)->Instance->SR; \
  391. UNUSED(tmpreg_fre); \
  392. }while(0U)
  393. /** @brief Enable the SPI peripheral.
  394. * @param __HANDLE__ specifies the SPI Handle.
  395. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  396. * @retval None
  397. */
  398. #define __HAL_SPI_ENABLE(__HANDLE__) SET_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_SPE)
  399. /** @brief Disable the SPI peripheral.
  400. * @param __HANDLE__ specifies the SPI Handle.
  401. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  402. * @retval None
  403. */
  404. #define __HAL_SPI_DISABLE(__HANDLE__) CLEAR_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_SPE)
  405. /**
  406. * @}
  407. */
  408. /* Private macros ------------------------------------------------------------*/
  409. /** @defgroup SPI_Private_Macros SPI Private Macros
  410. * @{
  411. */
  412. /** @brief Set the SPI transmit-only mode.
  413. * @param __HANDLE__ specifies the SPI Handle.
  414. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  415. * @retval None
  416. */
  417. #define SPI_1LINE_TX(__HANDLE__) SET_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_BIDIOE)
  418. /** @brief Set the SPI receive-only mode.
  419. * @param __HANDLE__ specifies the SPI Handle.
  420. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  421. * @retval None
  422. */
  423. #define SPI_1LINE_RX(__HANDLE__) CLEAR_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_BIDIOE)
  424. /** @brief Reset the CRC calculation of the SPI.
  425. * @param __HANDLE__ specifies the SPI Handle.
  426. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  427. * @retval None
  428. */
  429. #define SPI_RESET_CRC(__HANDLE__) do{CLEAR_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_CRCEN);\
  430. SET_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_CRCEN);}while(0U)
  431. /** @brief Check whether the specified SPI flag is set or not.
  432. * @param __SR__ copy of SPI SR regsiter.
  433. * @param __FLAG__ specifies the flag to check.
  434. * This parameter can be one of the following values:
  435. * @arg SPI_FLAG_RXNE: Receive buffer not empty flag
  436. * @arg SPI_FLAG_TXE: Transmit buffer empty flag
  437. * @arg SPI_FLAG_CRCERR: CRC error flag
  438. * @arg SPI_FLAG_MODF: Mode fault flag
  439. * @arg SPI_FLAG_OVR: Overrun flag
  440. * @arg SPI_FLAG_BSY: Busy flag
  441. * @arg SPI_FLAG_FRE: Frame format error flag
  442. * @retval SET or RESET.
  443. */
  444. #define SPI_CHECK_FLAG(__SR__, __FLAG__) ((((__SR__) & ((__FLAG__) & SPI_FLAG_MASK)) == ((__FLAG__) & SPI_FLAG_MASK)) ? SET : RESET)
  445. /** @brief Check whether the specified SPI Interrupt is set or not.
  446. * @param __CR2__ copy of SPI CR2 regsiter.
  447. * @param __INTERRUPT__ specifies the SPI interrupt source to check.
  448. * This parameter can be one of the following values:
  449. * @arg SPI_IT_TXE: Tx buffer empty interrupt enable
  450. * @arg SPI_IT_RXNE: RX buffer not empty interrupt enable
  451. * @arg SPI_IT_ERR: Error interrupt enable
  452. * @retval SET or RESET.
  453. */
  454. #define SPI_CHECK_IT_SOURCE(__CR2__, __INTERRUPT__) ((((__CR2__) & (__INTERRUPT__)) == (__INTERRUPT__)) ? SET : RESET)
  455. /** @brief Checks if SPI Mode parameter is in allowed range.
  456. * @param __MODE__ specifies the SPI Mode.
  457. * This parameter can be a value of @ref SPI_Mode
  458. * @retval None
  459. */
  460. #define IS_SPI_MODE(__MODE__) (((__MODE__) == SPI_MODE_SLAVE) || \
  461. ((__MODE__) == SPI_MODE_MASTER))
  462. /** @brief Checks if SPI Direction Mode parameter is in allowed range.
  463. * @param __MODE__ specifies the SPI Direction Mode.
  464. * This parameter can be a value of @ref SPI_Direction
  465. * @retval None
  466. */
  467. #define IS_SPI_DIRECTION(__MODE__) (((__MODE__) == SPI_DIRECTION_2LINES) || \
  468. ((__MODE__) == SPI_DIRECTION_2LINES_RXONLY) || \
  469. ((__MODE__) == SPI_DIRECTION_1LINE))
  470. /** @brief Checks if SPI Direction Mode parameter is 2 lines.
  471. * @param __MODE__ specifies the SPI Direction Mode.
  472. * @retval None
  473. */
  474. #define IS_SPI_DIRECTION_2LINES(__MODE__) ((__MODE__) == SPI_DIRECTION_2LINES)
  475. /** @brief Checks if SPI Direction Mode parameter is 1 or 2 lines.
  476. * @param __MODE__ specifies the SPI Direction Mode.
  477. * @retval None
  478. */
  479. #define IS_SPI_DIRECTION_2LINES_OR_1LINE(__MODE__) (((__MODE__) == SPI_DIRECTION_2LINES) || \
  480. ((__MODE__) == SPI_DIRECTION_1LINE))
  481. /** @brief Checks if SPI Data Size parameter is in allowed range.
  482. * @param __DATASIZE__ specifies the SPI Data Size.
  483. * This parameter can be a value of @ref SPI_Data_Size
  484. * @retval None
  485. */
  486. #define IS_SPI_DATASIZE(__DATASIZE__) (((__DATASIZE__) == SPI_DATASIZE_16BIT) || \
  487. ((__DATASIZE__) == SPI_DATASIZE_8BIT))
  488. /** @brief Checks if SPI Serial clock steady state parameter is in allowed range.
  489. * @param __CPOL__ specifies the SPI serial clock steady state.
  490. * This parameter can be a value of @ref SPI_Clock_Polarity
  491. * @retval None
  492. */
  493. #define IS_SPI_CPOL(__CPOL__) (((__CPOL__) == SPI_POLARITY_LOW) || \
  494. ((__CPOL__) == SPI_POLARITY_HIGH))
  495. /** @brief Checks if SPI Clock Phase parameter is in allowed range.
  496. * @param __CPHA__ specifies the SPI Clock Phase.
  497. * This parameter can be a value of @ref SPI_Clock_Phase
  498. * @retval None
  499. */
  500. #define IS_SPI_CPHA(__CPHA__) (((__CPHA__) == SPI_PHASE_1EDGE) || \
  501. ((__CPHA__) == SPI_PHASE_2EDGE))
  502. /** @brief Checks if SPI Slave Select parameter is in allowed range.
  503. * @param __NSS__ specifies the SPI Slave Select management parameter.
  504. * This parameter can be a value of @ref SPI_Slave_Select_management
  505. * @retval None
  506. */
  507. #define IS_SPI_NSS(__NSS__) (((__NSS__) == SPI_NSS_SOFT) || \
  508. ((__NSS__) == SPI_NSS_HARD_INPUT) || \
  509. ((__NSS__) == SPI_NSS_HARD_OUTPUT))
  510. /** @brief Checks if SPI Baudrate prescaler parameter is in allowed range.
  511. * @param __PRESCALER__ specifies the SPI Baudrate prescaler.
  512. * This parameter can be a value of @ref SPI_BaudRate_Prescaler
  513. * @retval None
  514. */
  515. #define IS_SPI_BAUDRATE_PRESCALER(__PRESCALER__) (((__PRESCALER__) == SPI_BAUDRATEPRESCALER_2) || \
  516. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_4) || \
  517. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_8) || \
  518. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_16) || \
  519. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_32) || \
  520. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_64) || \
  521. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_128) || \
  522. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_256))
  523. /** @brief Checks if SPI MSB LSB transmission parameter is in allowed range.
  524. * @param __BIT__ specifies the SPI MSB LSB transmission (whether data transfer starts from MSB or LSB bit).
  525. * This parameter can be a value of @ref SPI_MSB_LSB_transmission
  526. * @retval None
  527. */
  528. #define IS_SPI_FIRST_BIT(__BIT__) (((__BIT__) == SPI_FIRSTBIT_MSB) || \
  529. ((__BIT__) == SPI_FIRSTBIT_LSB))
  530. #if defined(SPI_I2SCFGR_I2SMOD)
  531. /** @brief Checks if SPI TI mode parameter is in allowed range.
  532. * @param __MODE__ specifies the SPI TI mode.
  533. * This parameter can be a value of @ref SPI_TI_mode
  534. * @retval None
  535. */
  536. #define IS_SPI_TIMODE(__MODE__) (((__MODE__) == SPI_TIMODE_DISABLE) || \
  537. ((__MODE__) == SPI_TIMODE_ENABLE))
  538. #else
  539. /** @defgroup SPI_TI_mode SPI TI mode disable
  540. * @brief SPI TI Mode not supported for Category 1 and 2
  541. * @{
  542. */
  543. #define IS_SPI_TIMODE(__MODE__) ((__MODE__) == SPI_TIMODE_DISABLE)
  544. #endif
  545. /** @brief Checks if SPI CRC calculation enabled state is in allowed range.
  546. * @param __CALCULATION__ specifies the SPI CRC calculation enable state.
  547. * This parameter can be a value of @ref SPI_CRC_Calculation
  548. * @retval None
  549. */
  550. #define IS_SPI_CRC_CALCULATION(__CALCULATION__) (((__CALCULATION__) == SPI_CRCCALCULATION_DISABLE) || \
  551. ((__CALCULATION__) == SPI_CRCCALCULATION_ENABLE))
  552. /** @brief Checks if SPI polynomial value to be used for the CRC calculation, is in allowed range.
  553. * @param __POLYNOMIAL__ specifies the SPI polynomial value to be used for the CRC calculation.
  554. * This parameter must be a number between Min_Data = 0 and Max_Data = 65535
  555. * @retval None
  556. */
  557. #define IS_SPI_CRC_POLYNOMIAL(__POLYNOMIAL__) (((__POLYNOMIAL__) >= 0x1U) && ((__POLYNOMIAL__) <= 0xFFFFU) && (((__POLYNOMIAL__)&0x1U) != 0U))
  558. /** @brief Checks if DMA handle is valid.
  559. * @param __HANDLE__ specifies a DMA Handle.
  560. * @retval None
  561. */
  562. #define IS_SPI_DMA_HANDLE(__HANDLE__) ((__HANDLE__) != NULL)
  563. /**
  564. * @}
  565. */
  566. /* Exported functions --------------------------------------------------------*/
  567. /** @addtogroup SPI_Exported_Functions
  568. * @{
  569. */
  570. /** @addtogroup SPI_Exported_Functions_Group1
  571. * @{
  572. */
  573. /* Initialization/de-initialization functions ********************************/
  574. HAL_StatusTypeDef HAL_SPI_Init(SPI_HandleTypeDef *hspi);
  575. HAL_StatusTypeDef HAL_SPI_DeInit(SPI_HandleTypeDef *hspi);
  576. void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi);
  577. void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi);
  578. /* Callbacks Register/UnRegister functions ***********************************/
  579. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  580. HAL_StatusTypeDef HAL_SPI_RegisterCallback(SPI_HandleTypeDef *hspi, HAL_SPI_CallbackIDTypeDef CallbackID, pSPI_CallbackTypeDef pCallback);
  581. HAL_StatusTypeDef HAL_SPI_UnRegisterCallback(SPI_HandleTypeDef *hspi, HAL_SPI_CallbackIDTypeDef CallbackID);
  582. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  583. /**
  584. * @}
  585. */
  586. /** @addtogroup SPI_Exported_Functions_Group2
  587. * @{
  588. */
  589. /* I/O operation functions ***************************************************/
  590. HAL_StatusTypeDef HAL_SPI_Transmit(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  591. HAL_StatusTypeDef HAL_SPI_Receive(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  592. HAL_StatusTypeDef HAL_SPI_TransmitReceive(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size,
  593. uint32_t Timeout);
  594. HAL_StatusTypeDef HAL_SPI_Transmit_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size);
  595. HAL_StatusTypeDef HAL_SPI_Receive_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size);
  596. HAL_StatusTypeDef HAL_SPI_TransmitReceive_IT(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData,
  597. uint16_t Size);
  598. HAL_StatusTypeDef HAL_SPI_Transmit_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size);
  599. HAL_StatusTypeDef HAL_SPI_Receive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size);
  600. HAL_StatusTypeDef HAL_SPI_TransmitReceive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData,
  601. uint16_t Size);
  602. HAL_StatusTypeDef HAL_SPI_DMAPause(SPI_HandleTypeDef *hspi);
  603. HAL_StatusTypeDef HAL_SPI_DMAResume(SPI_HandleTypeDef *hspi);
  604. HAL_StatusTypeDef HAL_SPI_DMAStop(SPI_HandleTypeDef *hspi);
  605. /* Transfer Abort functions */
  606. HAL_StatusTypeDef HAL_SPI_Abort(SPI_HandleTypeDef *hspi);
  607. HAL_StatusTypeDef HAL_SPI_Abort_IT(SPI_HandleTypeDef *hspi);
  608. void HAL_SPI_IRQHandler(SPI_HandleTypeDef *hspi);
  609. void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi);
  610. void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi);
  611. void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi);
  612. void HAL_SPI_TxHalfCpltCallback(SPI_HandleTypeDef *hspi);
  613. void HAL_SPI_RxHalfCpltCallback(SPI_HandleTypeDef *hspi);
  614. void HAL_SPI_TxRxHalfCpltCallback(SPI_HandleTypeDef *hspi);
  615. void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi);
  616. void HAL_SPI_AbortCpltCallback(SPI_HandleTypeDef *hspi);
  617. /**
  618. * @}
  619. */
  620. /** @addtogroup SPI_Exported_Functions_Group3
  621. * @{
  622. */
  623. /* Peripheral State and Error functions ***************************************/
  624. HAL_SPI_StateTypeDef HAL_SPI_GetState(SPI_HandleTypeDef *hspi);
  625. uint32_t HAL_SPI_GetError(SPI_HandleTypeDef *hspi);
  626. /**
  627. * @}
  628. */
  629. /**
  630. * @}
  631. */
  632. /**
  633. * @}
  634. */
  635. /**
  636. * @}
  637. */
  638. #ifdef __cplusplus
  639. }
  640. #endif
  641. #endif /* STM32L1xx_HAL_SPI_H */
  642. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/