py32f0xx_hal_spi.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /**
  2. ******************************************************************************
  3. * @file py32f0xx_hal_spi.h
  4. * @author MCU Application Team
  5. * @brief Header file of SPI HAL module.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by Puya 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. * @attention
  19. *
  20. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  21. * All rights reserved.</center></h2>
  22. *
  23. * This software component is licensed by ST under BSD 3-Clause license,
  24. * the "License"; You may not use this file except in compliance with the
  25. * License. You may obtain a copy of the License at:
  26. * opensource.org/licenses/BSD-3-Clause
  27. *
  28. ******************************************************************************
  29. */
  30. /* Define to prevent recursive inclusion -------------------------------------*/
  31. #ifndef __PY32F0xx_HAL_SPI_H
  32. #define __PY32F0xx_HAL_SPI_H
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /* Includes ------------------------------------------------------------------*/
  37. #include "py32f0xx_hal_def.h"
  38. /** @addtogroup PY32F0xx_HAL_Driver
  39. * @{
  40. */
  41. /** @addtogroup SPI
  42. * @{
  43. */
  44. /* Exported types ------------------------------------------------------------*/
  45. /** @defgroup SPI_Exported_Types SPI Exported Types
  46. * @{
  47. */
  48. /**
  49. * @brief SPI Configuration Structure definition
  50. */
  51. typedef struct
  52. {
  53. uint32_t Mode; /*!< Specifies the SPI operating mode.
  54. This parameter can be a value of @ref SPI_Mode */
  55. uint32_t Direction; /*!< Specifies the SPI bidirectional mode state.
  56. This parameter can be a value of @ref SPI_Direction */
  57. uint32_t DataSize; /*!< Specifies the SPI data size.
  58. This parameter can be a value of @ref SPI_Data_Size */
  59. uint32_t CLKPolarity; /*!< Specifies the serial clock steady state.
  60. This parameter can be a value of @ref SPI_Clock_Polarity */
  61. uint32_t CLKPhase; /*!< Specifies the clock active edge for the bit capture.
  62. This parameter can be a value of @ref SPI_Clock_Phase */
  63. uint32_t NSS; /*!< Specifies whether the NSS signal is managed by
  64. hardware (NSS pin) or by software using the SSI bit.
  65. This parameter can be a value of @ref SPI_Slave_Select_management */
  66. uint32_t BaudRatePrescaler; /*!< Specifies the Baud Rate prescaler value which will be
  67. used to configure the transmit and receive SCK clock.
  68. This parameter can be a value of @ref SPI_BaudRate_Prescaler
  69. @note The communication clock is derived from the master
  70. clock. The slave clock does not need to be set. */
  71. uint32_t FirstBit; /*!< Specifies whether data transfers start from MSB or LSB bit.
  72. This parameter can be a value of @ref SPI_MSB_LSB_transmission */
  73. uint32_t SlaveFastMode; /*!< Specifies the Slave fast mode is enabled or not.
  74. This parameter can be a value of @ref SPI_Slave_Fast_mode */
  75. } SPI_InitTypeDef;
  76. /**
  77. * @brief HAL SPI State structure definition
  78. */
  79. typedef enum
  80. {
  81. HAL_SPI_STATE_RESET = 0x00U, /*!< Peripheral not Initialized */
  82. HAL_SPI_STATE_READY = 0x01U, /*!< Peripheral Initialized and ready for use */
  83. HAL_SPI_STATE_BUSY = 0x02U, /*!< an internal process is ongoing */
  84. HAL_SPI_STATE_BUSY_TX = 0x03U, /*!< Data Transmission process is ongoing */
  85. HAL_SPI_STATE_BUSY_RX = 0x04U, /*!< Data Reception process is ongoing */
  86. HAL_SPI_STATE_BUSY_TX_RX = 0x05U, /*!< Data Transmission and Reception process is ongoing */
  87. HAL_SPI_STATE_ERROR = 0x06U, /*!< SPI error state */
  88. HAL_SPI_STATE_ABORT = 0x07U /*!< SPI abort is ongoing */
  89. } HAL_SPI_StateTypeDef;
  90. /**
  91. * @brief SPI handle Structure definition
  92. */
  93. typedef struct __SPI_HandleTypeDef
  94. {
  95. SPI_TypeDef *Instance; /*!< SPI registers base address */
  96. SPI_InitTypeDef Init; /*!< SPI communication parameters */
  97. uint8_t *pTxBuffPtr; /*!< Pointer to SPI Tx transfer Buffer */
  98. uint16_t TxXferSize; /*!< SPI Tx Transfer size */
  99. __IO uint16_t TxXferCount; /*!< SPI Tx Transfer Counter */
  100. uint8_t *pRxBuffPtr; /*!< Pointer to SPI Rx transfer Buffer */
  101. uint16_t RxXferSize; /*!< SPI Rx Transfer size */
  102. __IO uint16_t RxXferCount; /*!< SPI Rx Transfer Counter */
  103. void (*RxISR)(struct __SPI_HandleTypeDef *hspi); /*!< function pointer on Rx ISR */
  104. void (*TxISR)(struct __SPI_HandleTypeDef *hspi); /*!< function pointer on Tx ISR */
  105. #if (defined(DMA1) || defined(DMA))
  106. DMA_HandleTypeDef *hdmatx; /*!< SPI Tx DMA Handle parameters */
  107. DMA_HandleTypeDef *hdmarx; /*!< SPI Rx DMA Handle parameters */
  108. #endif /* DMA1 or DMA */
  109. HAL_LockTypeDef Lock; /*!< Locking object */
  110. __IO HAL_SPI_StateTypeDef State; /*!< SPI communication state */
  111. __IO uint32_t ErrorCode; /*!< SPI Error code */
  112. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  113. void (* TxCpltCallback)(struct __SPI_HandleTypeDef *hspi); /*!< SPI Tx Completed callback */
  114. void (* RxCpltCallback)(struct __SPI_HandleTypeDef *hspi); /*!< SPI Rx Completed callback */
  115. void (* TxRxCpltCallback)(struct __SPI_HandleTypeDef *hspi); /*!< SPI TxRx Completed callback */
  116. void (* TxHalfCpltCallback)(struct __SPI_HandleTypeDef *hspi); /*!< SPI Tx Half Completed callback */
  117. void (* RxHalfCpltCallback)(struct __SPI_HandleTypeDef *hspi); /*!< SPI Rx Half Completed callback */
  118. void (* TxRxHalfCpltCallback)(struct __SPI_HandleTypeDef *hspi); /*!< SPI TxRx Half Completed callback */
  119. void (* ErrorCallback)(struct __SPI_HandleTypeDef *hspi); /*!< SPI Error callback */
  120. void (* AbortCpltCallback)(struct __SPI_HandleTypeDef *hspi); /*!< SPI Abort callback */
  121. void (* MspInitCallback)(struct __SPI_HandleTypeDef *hspi); /*!< SPI Msp Init callback */
  122. void (* MspDeInitCallback)(struct __SPI_HandleTypeDef *hspi); /*!< SPI Msp DeInit callback */
  123. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  124. } SPI_HandleTypeDef;
  125. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  126. /**
  127. * @brief HAL SPI Callback ID enumeration definition
  128. */
  129. typedef enum
  130. {
  131. HAL_SPI_TX_COMPLETE_CB_ID = 0x00U, /*!< SPI Tx Completed callback ID */
  132. HAL_SPI_RX_COMPLETE_CB_ID = 0x01U, /*!< SPI Rx Completed callback ID */
  133. HAL_SPI_TX_RX_COMPLETE_CB_ID = 0x02U, /*!< SPI TxRx Completed callback ID */
  134. HAL_SPI_TX_HALF_COMPLETE_CB_ID = 0x03U, /*!< SPI Tx Half Completed callback ID */
  135. HAL_SPI_RX_HALF_COMPLETE_CB_ID = 0x04U, /*!< SPI Rx Half Completed callback ID */
  136. HAL_SPI_TX_RX_HALF_COMPLETE_CB_ID = 0x05U, /*!< SPI TxRx Half Completed callback ID */
  137. HAL_SPI_ERROR_CB_ID = 0x06U, /*!< SPI Error callback ID */
  138. HAL_SPI_ABORT_CB_ID = 0x07U, /*!< SPI Abort callback ID */
  139. HAL_SPI_MSPINIT_CB_ID = 0x08U, /*!< SPI Msp Init callback ID */
  140. HAL_SPI_MSPDEINIT_CB_ID = 0x09U /*!< SPI Msp DeInit callback ID */
  141. } HAL_SPI_CallbackIDTypeDef;
  142. /**
  143. * @brief HAL SPI Callback pointer definition
  144. */
  145. typedef void (*pSPI_CallbackTypeDef)(SPI_HandleTypeDef *hspi); /*!< pointer to an SPI callback function */
  146. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  147. /**
  148. * @}
  149. */
  150. /* Exported constants --------------------------------------------------------*/
  151. /** @defgroup SPI_Exported_Constants SPI Exported Constants
  152. * @{
  153. */
  154. /** @defgroup SPI_Error_Code SPI Error Code
  155. * @{
  156. */
  157. #define HAL_SPI_ERROR_NONE (0x00000000U) /*!< No error */
  158. #define HAL_SPI_ERROR_MODF (0x00000001U) /*!< MODF error */
  159. #define HAL_SPI_ERROR_OVR (0x00000004U) /*!< OVR error */
  160. #define HAL_SPI_ERROR_FRE (0x00000008U) /*!< FRE error */
  161. #if (defined(DMA1) || defined(DMA))
  162. #define HAL_SPI_ERROR_DMA (0x00000010U) /*!< DMA transfer error */
  163. #endif /* DMA1 or DMA */
  164. #define HAL_SPI_ERROR_FLAG (0x00000020U) /*!< Error on RXNE/TXE/BSY/FTLVL/FRLVL Flag */
  165. #define HAL_SPI_ERROR_ABORT (0x00000040U) /*!< Error during SPI Abort procedure */
  166. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  167. #define HAL_SPI_ERROR_INVALID_CALLBACK (0x00000080U) /*!< Invalid Callback error */
  168. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  169. /**
  170. * @}
  171. */
  172. /** @defgroup SPI_Mode SPI Mode
  173. * @{
  174. */
  175. #define SPI_MODE_SLAVE (0x00000000U)
  176. #define SPI_MODE_MASTER (SPI_CR1_MSTR | SPI_CR1_SSI)
  177. /**
  178. * @}
  179. */
  180. /** @defgroup SPI_Direction SPI Direction Mode
  181. * @{
  182. */
  183. #define SPI_DIRECTION_2LINES (0x00000000U) /* Full-duplex communication or Simplex communication Transmit-only mode */
  184. #define SPI_DIRECTION_2LINES_RXONLY SPI_CR1_RXONLY /* Simplex communication Receive-only mode */
  185. #define SPI_DIRECTION_1LINE SPI_CR1_BIDIMODE /* Half-duplex communication mode */
  186. /**
  187. * @}
  188. */
  189. /** @defgroup SPI_Data_Size SPI Data Size
  190. * @{
  191. */
  192. #define SPI_DATASIZE_8BIT (0x00000700U)
  193. #define SPI_DATASIZE_16BIT (0x00000F00U)
  194. /**
  195. * @}
  196. */
  197. /** @defgroup SPI_Clock_Polarity SPI Clock Polarity
  198. * @{
  199. */
  200. #define SPI_POLARITY_LOW (0x00000000U)
  201. #define SPI_POLARITY_HIGH SPI_CR1_CPOL
  202. /**
  203. * @}
  204. */
  205. /** @defgroup SPI_Clock_Phase SPI Clock Phase
  206. * @{
  207. */
  208. #define SPI_PHASE_1EDGE (0x00000000U)
  209. #define SPI_PHASE_2EDGE SPI_CR1_CPHA
  210. /**
  211. * @}
  212. */
  213. /** @defgroup SPI_Slave_Select_management SPI Slave Select Management
  214. * @{
  215. */
  216. #define SPI_NSS_SOFT SPI_CR1_SSM
  217. #define SPI_NSS_HARD_INPUT (0x00000000U)
  218. #define SPI_NSS_HARD_OUTPUT (SPI_CR2_SSOE << 16U)
  219. /**
  220. * @}
  221. */
  222. /** @defgroup SPI_BaudRate_Prescaler SPI BaudRate Prescaler
  223. * @{
  224. */
  225. #define SPI_BAUDRATEPRESCALER_2 (0x00000000U)
  226. #define SPI_BAUDRATEPRESCALER_4 (SPI_CR1_BR_0)
  227. #define SPI_BAUDRATEPRESCALER_8 (SPI_CR1_BR_1)
  228. #define SPI_BAUDRATEPRESCALER_16 (SPI_CR1_BR_1 | SPI_CR1_BR_0)
  229. #define SPI_BAUDRATEPRESCALER_32 (SPI_CR1_BR_2)
  230. #define SPI_BAUDRATEPRESCALER_64 (SPI_CR1_BR_2 | SPI_CR1_BR_0)
  231. #define SPI_BAUDRATEPRESCALER_128 (SPI_CR1_BR_2 | SPI_CR1_BR_1)
  232. #define SPI_BAUDRATEPRESCALER_256 (SPI_CR1_BR_2 | SPI_CR1_BR_1 | SPI_CR1_BR_0)
  233. /**
  234. * @}
  235. */
  236. /** @defgroup SPI_MSB_LSB_transmission SPI MSB LSB Transmission
  237. * @{
  238. */
  239. #define SPI_FIRSTBIT_MSB (0x00000000U)
  240. #define SPI_FIRSTBIT_LSB SPI_CR1_LSBFIRST
  241. /**
  242. * @}
  243. */
  244. /** @defgroup SPI_Slave_Fast_mode SPI Slave Fast Mode
  245. * @{
  246. */
  247. #define SPI_SLAVE_FAST_MODE_DISABLE (0x00000000U)
  248. #define SPI_SLAVE_FAST_MODE_ENABLE SPI_CR2_SLVFM
  249. /**
  250. * @}
  251. */
  252. /** @defgroup SPI_FIFO_reception_threshold SPI FIFO Reception Threshold
  253. * @{
  254. * This parameter can be one of the following values:
  255. * SPI_RXFIFO_THRESHOLD or SPI_RXFIFO_THRESHOLD_QF :
  256. * RXNE event is generated if the FIFO
  257. * level is greater or equal to 1/4(8-bits).
  258. * SPI_RXFIFO_THRESHOLD_HF: RXNE event is generated if the FIFO
  259. * level is greater or equal to 1/2(16 bits). */
  260. #define SPI_RXFIFO_THRESHOLD SPI_CR2_FRXTH
  261. #define SPI_RXFIFO_THRESHOLD_QF SPI_CR2_FRXTH
  262. #define SPI_RXFIFO_THRESHOLD_HF (0x00000000U)
  263. /**
  264. * @}
  265. */
  266. /** @defgroup SPI_Interrupt_definition SPI Interrupt Definition
  267. * @{
  268. */
  269. #define SPI_IT_TXE SPI_CR2_TXEIE
  270. #define SPI_IT_RXNE SPI_CR2_RXNEIE
  271. #define SPI_IT_ERR SPI_CR2_ERRIE
  272. /**
  273. * @}
  274. */
  275. /** @defgroup SPI_Flags_definition SPI Flags Definition
  276. * @{
  277. */
  278. #define SPI_FLAG_RXNE SPI_SR_RXNE /* SPI status flag: Rx buffer not empty flag */
  279. #define SPI_FLAG_TXE SPI_SR_TXE /* SPI status flag: Tx buffer empty flag */
  280. #define SPI_FLAG_BSY SPI_SR_BSY /* SPI status flag: Busy flag */
  281. #define SPI_FLAG_MODF SPI_SR_MODF /* SPI Error flag: Mode fault flag */
  282. #define SPI_FLAG_OVR SPI_SR_OVR /* SPI Error flag: Overrun flag */
  283. #define SPI_FLAG_FTLVL SPI_SR_FTLVL /* SPI fifo transmission level */
  284. #define SPI_FLAG_FRLVL SPI_SR_FRLVL /* SPI fifo reception level */
  285. #define SPI_FLAG_MASK (SPI_SR_RXNE | SPI_SR_TXE \
  286. | SPI_SR_MODF | SPI_SR_OVR | SPI_SR_BSY \
  287. | SPI_SR_FRLVL | SPI_SR_FTLVL)
  288. /**
  289. * @}
  290. */
  291. /** @defgroup SPI_transmission_fifo_status_level SPI Transmission FIFO Status Level
  292. * @{
  293. */
  294. #define SPI_FTLVL_EMPTY (0x00000000U)
  295. #define SPI_FTLVL_QUARTER_FULL (0x00000800U)
  296. #define SPI_FTLVL_HALF_FULL (0x00001000U)
  297. #define SPI_FTLVL_FULL (0x00001800U)
  298. /**
  299. * @}
  300. */
  301. /** @defgroup SPI_reception_fifo_status_level SPI Reception FIFO Status Level
  302. * @{
  303. */
  304. #define SPI_FRLVL_EMPTY (0x00000000U)
  305. #define SPI_FRLVL_QUARTER_FULL (0x00000200U)
  306. #define SPI_FRLVL_HALF_FULL (0x00000400U)
  307. #define SPI_FRLVL_FULL (0x00000600U)
  308. /**
  309. * @}
  310. */
  311. /**
  312. * @}
  313. */
  314. /* Exported macros -----------------------------------------------------------*/
  315. /** @defgroup SPI_Exported_Macros SPI Exported Macros
  316. * @{
  317. */
  318. /** @brief Reset SPI handle state.
  319. * @param __HANDLE__ specifies the SPI Handle.
  320. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  321. * @retval None
  322. */
  323. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  324. #define __HAL_SPI_RESET_HANDLE_STATE(__HANDLE__) do{ \
  325. (__HANDLE__)->State = HAL_SPI_STATE_RESET; \
  326. (__HANDLE__)->MspInitCallback = NULL; \
  327. (__HANDLE__)->MspDeInitCallback = NULL; \
  328. } while(0)
  329. #else
  330. #define __HAL_SPI_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_SPI_STATE_RESET)
  331. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  332. /** @brief Enable the specified SPI interrupts.
  333. * @param __HANDLE__ specifies the SPI Handle.
  334. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  335. * @param __INTERRUPT__ specifies the interrupt source to enable.
  336. * This parameter can be one of the following values:
  337. * @arg SPI_IT_TXE: Tx buffer empty interrupt enable
  338. * @arg SPI_IT_RXNE: RX buffer not empty interrupt enable
  339. * @arg SPI_IT_ERR: Error interrupt enable
  340. * @retval None
  341. */
  342. #define __HAL_SPI_ENABLE_IT(__HANDLE__, __INTERRUPT__) SET_BIT((__HANDLE__)->Instance->CR2, (__INTERRUPT__))
  343. /** @brief Disable the specified SPI interrupts.
  344. * @param __HANDLE__ specifies the SPI handle.
  345. * This parameter can be SPIx where x: 1, 2, or 3 to select the SPI peripheral.
  346. * @param __INTERRUPT__ specifies the interrupt source to disable.
  347. * This parameter can be one of the following values:
  348. * @arg SPI_IT_TXE: Tx buffer empty interrupt enable
  349. * @arg SPI_IT_RXNE: RX buffer not empty interrupt enable
  350. * @arg SPI_IT_ERR: Error interrupt enable
  351. * @retval None
  352. */
  353. #define __HAL_SPI_DISABLE_IT(__HANDLE__, __INTERRUPT__) CLEAR_BIT((__HANDLE__)->Instance->CR2, (__INTERRUPT__))
  354. /** @brief Check whether the specified SPI interrupt source is enabled or not.
  355. * @param __HANDLE__ specifies the SPI Handle.
  356. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  357. * @param __INTERRUPT__ specifies the SPI interrupt source to check.
  358. * This parameter can be one of the following values:
  359. * @arg SPI_IT_TXE: Tx buffer empty interrupt enable
  360. * @arg SPI_IT_RXNE: RX buffer not empty interrupt enable
  361. * @arg SPI_IT_ERR: Error interrupt enable
  362. * @retval The new state of __IT__ (TRUE or FALSE).
  363. */
  364. #define __HAL_SPI_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__) ((((__HANDLE__)->Instance->CR2\
  365. & (__INTERRUPT__)) == (__INTERRUPT__)) ? SET : RESET)
  366. /** @brief Check whether the specified SPI flag is set or not.
  367. * @param __HANDLE__ specifies the SPI Handle.
  368. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  369. * @param __FLAG__ specifies the flag to check.
  370. * This parameter can be one of the following values:
  371. * @arg SPI_FLAG_RXNE: Receive buffer not empty flag
  372. * @arg SPI_FLAG_TXE: Transmit buffer empty flag
  373. * @arg SPI_FLAG_MODF: Mode fault flag
  374. * @arg SPI_FLAG_OVR: Overrun flag
  375. * @arg SPI_FLAG_BSY: Busy flag
  376. * @arg SPI_FLAG_FRE: Frame format error flag
  377. * @arg SPI_FLAG_FTLVL: SPI fifo transmission level
  378. * @arg SPI_FLAG_FRLVL: SPI fifo reception level
  379. * @retval The new state of __FLAG__ (TRUE or FALSE).
  380. */
  381. #define __HAL_SPI_GET_FLAG(__HANDLE__, __FLAG__) ((((__HANDLE__)->Instance->SR) & (__FLAG__)) == (__FLAG__))
  382. /** @brief Clear the SPI MODF 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_MODFFLAG(__HANDLE__) \
  388. do{ \
  389. __IO uint32_t tmpreg_modf = 0x00U; \
  390. tmpreg_modf = (__HANDLE__)->Instance->SR; \
  391. CLEAR_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_SPE); \
  392. UNUSED(tmpreg_modf); \
  393. } while(0U)
  394. /** @brief Clear the SPI OVR pending flag.
  395. * @param __HANDLE__ specifies the SPI Handle.
  396. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  397. * @retval None
  398. */
  399. #define __HAL_SPI_CLEAR_OVRFLAG(__HANDLE__) \
  400. do{ \
  401. __IO uint32_t tmpreg_ovr = 0x00U; \
  402. tmpreg_ovr = (__HANDLE__)->Instance->DR; \
  403. tmpreg_ovr = (__HANDLE__)->Instance->SR; \
  404. UNUSED(tmpreg_ovr); \
  405. } while(0U)
  406. /** @brief Clear the SPI FRE pending flag.
  407. * @param __HANDLE__ specifies the SPI Handle.
  408. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  409. * @retval None
  410. */
  411. #define __HAL_SPI_CLEAR_FREFLAG(__HANDLE__) \
  412. do{ \
  413. __IO uint32_t tmpreg_fre = 0x00U; \
  414. tmpreg_fre = (__HANDLE__)->Instance->SR; \
  415. UNUSED(tmpreg_fre); \
  416. }while(0U)
  417. /** @brief Enable the SPI peripheral.
  418. * @param __HANDLE__ specifies the SPI Handle.
  419. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  420. * @retval None
  421. */
  422. #define __HAL_SPI_ENABLE(__HANDLE__) SET_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_SPE)
  423. /** @brief Disable the SPI peripheral.
  424. * @param __HANDLE__ specifies the SPI Handle.
  425. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  426. * @retval None
  427. */
  428. #define __HAL_SPI_DISABLE(__HANDLE__) CLEAR_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_SPE)
  429. /**
  430. * @}
  431. */
  432. /* Private macros ------------------------------------------------------------*/
  433. /** @defgroup SPI_Private_Macros SPI Private Macros
  434. * @{
  435. */
  436. /** @brief Set the SPI transmit-only mode.
  437. * @param __HANDLE__ specifies the SPI Handle.
  438. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  439. * @retval None
  440. */
  441. #define SPI_1LINE_TX(__HANDLE__) SET_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_BIDIOE)
  442. /** @brief Set the SPI receive-only mode.
  443. * @param __HANDLE__ specifies the SPI Handle.
  444. * This parameter can be SPI where x: 1, 2, or 3 to select the SPI peripheral.
  445. * @retval None
  446. */
  447. #define SPI_1LINE_RX(__HANDLE__) CLEAR_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_BIDIOE)
  448. /** @brief Check whether the specified SPI flag is set or not.
  449. * @param __SR__ copy of SPI SR regsiter.
  450. * @param __FLAG__ specifies the flag to check.
  451. * This parameter can be one of the following values:
  452. * @arg SPI_FLAG_RXNE: Receive buffer not empty flag
  453. * @arg SPI_FLAG_TXE: Transmit buffer empty flag
  454. * @arg SPI_FLAG_MODF: Mode fault flag
  455. * @arg SPI_FLAG_OVR: Overrun flag
  456. * @arg SPI_FLAG_BSY: Busy flag
  457. * @arg SPI_FLAG_FRE: Frame format error flag
  458. * @arg SPI_FLAG_FTLVL: SPI fifo transmission level
  459. * @arg SPI_FLAG_FRLVL: SPI fifo reception level
  460. * @retval SET or RESET.
  461. */
  462. #define SPI_CHECK_FLAG(__SR__, __FLAG__) ((((__SR__) & ((__FLAG__) & SPI_FLAG_MASK)) == ((__FLAG__) & SPI_FLAG_MASK)) ? SET : RESET)
  463. /** @brief Check whether the specified SPI Interrupt is set or not.
  464. * @param __CR2__ copy of SPI CR2 regsiter.
  465. * @param __INTERRUPT__ specifies the SPI interrupt source to check.
  466. * This parameter can be one of the following values:
  467. * @arg SPI_IT_TXE: Tx buffer empty interrupt enable
  468. * @arg SPI_IT_RXNE: RX buffer not empty interrupt enable
  469. * @arg SPI_IT_ERR: Error interrupt enable
  470. * @retval SET or RESET.
  471. */
  472. #define SPI_CHECK_IT_SOURCE(__CR2__, __INTERRUPT__) ((((__CR2__) & (__INTERRUPT__)) == (__INTERRUPT__)) ? SET : RESET)
  473. /** @brief Checks if SPI Mode parameter is in allowed range.
  474. * @param __MODE__ specifies the SPI Mode.
  475. * This parameter can be a value of @ref SPI_Mode
  476. * @retval None
  477. */
  478. #define IS_SPI_MODE(__MODE__) (((__MODE__) == SPI_MODE_SLAVE) || \
  479. ((__MODE__) == SPI_MODE_MASTER))
  480. /** @brief Checks if SPI Direction Mode parameter is in allowed range.
  481. * @param __MODE__ specifies the SPI Direction Mode.
  482. * This parameter can be a value of @ref SPI_Direction
  483. * @retval None
  484. */
  485. #define IS_SPI_DIRECTION(__MODE__) (((__MODE__) == SPI_DIRECTION_2LINES) || \
  486. ((__MODE__) == SPI_DIRECTION_2LINES_RXONLY) || \
  487. ((__MODE__) == SPI_DIRECTION_1LINE))
  488. /** @brief Checks if SPI Direction Mode parameter is 2 lines.
  489. * @param __MODE__ specifies the SPI Direction Mode.
  490. * @retval None
  491. */
  492. #define IS_SPI_DIRECTION_2LINES(__MODE__) ((__MODE__) == SPI_DIRECTION_2LINES)
  493. /** @brief Checks if SPI Direction Mode parameter is 1 or 2 lines.
  494. * @param __MODE__ specifies the SPI Direction Mode.
  495. * @retval None
  496. */
  497. #define IS_SPI_DIRECTION_2LINES_OR_1LINE(__MODE__) (((__MODE__) == SPI_DIRECTION_2LINES) || \
  498. ((__MODE__) == SPI_DIRECTION_1LINE))
  499. /** @brief Checks if SPI Data Size parameter is in allowed range.
  500. * @param __DATASIZE__ specifies the SPI Data Size.
  501. * This parameter can be a value of @ref SPI_Data_Size
  502. * @retval None
  503. */
  504. #define IS_SPI_DATASIZE(__DATASIZE__) (((__DATASIZE__) == SPI_DATASIZE_16BIT) || \
  505. ((__DATASIZE__) == SPI_DATASIZE_8BIT))
  506. /** @brief Checks if SPI Serial clock steady state parameter is in allowed range.
  507. * @param __CPOL__ specifies the SPI serial clock steady state.
  508. * This parameter can be a value of @ref SPI_Clock_Polarity
  509. * @retval None
  510. */
  511. #define IS_SPI_CPOL(__CPOL__) (((__CPOL__) == SPI_POLARITY_LOW) || \
  512. ((__CPOL__) == SPI_POLARITY_HIGH))
  513. /** @brief Checks if SPI Clock Phase parameter is in allowed range.
  514. * @param __CPHA__ specifies the SPI Clock Phase.
  515. * This parameter can be a value of @ref SPI_Clock_Phase
  516. * @retval None
  517. */
  518. #define IS_SPI_CPHA(__CPHA__) (((__CPHA__) == SPI_PHASE_1EDGE) || \
  519. ((__CPHA__) == SPI_PHASE_2EDGE))
  520. /** @brief Checks if SPI Slave Select parameter is in allowed range.
  521. * @param __NSS__ specifies the SPI Slave Select management parameter.
  522. * This parameter can be a value of @ref SPI_Slave_Select_management
  523. * @retval None
  524. */
  525. #define IS_SPI_NSS(__NSS__) (((__NSS__) == SPI_NSS_SOFT) || \
  526. ((__NSS__) == SPI_NSS_HARD_INPUT) || \
  527. ((__NSS__) == SPI_NSS_HARD_OUTPUT))
  528. /** @brief Checks if SPI Baudrate prescaler parameter is in allowed range.
  529. * @param __PRESCALER__ specifies the SPI Baudrate prescaler.
  530. * This parameter can be a value of @ref SPI_BaudRate_Prescaler
  531. * @retval None
  532. */
  533. #define IS_SPI_BAUDRATE_PRESCALER(__PRESCALER__) (((__PRESCALER__) == SPI_BAUDRATEPRESCALER_2) || \
  534. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_4) || \
  535. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_8) || \
  536. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_16) || \
  537. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_32) || \
  538. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_64) || \
  539. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_128) || \
  540. ((__PRESCALER__) == SPI_BAUDRATEPRESCALER_256))
  541. /** @brief Checks if SPI MSB LSB transmission parameter is in allowed range.
  542. * @param __BIT__ specifies the SPI MSB LSB transmission (whether data transfer starts from MSB or LSB bit).
  543. * This parameter can be a value of @ref SPI_MSB_LSB_transmission
  544. * @retval None
  545. */
  546. #define IS_SPI_FIRST_BIT(__BIT__) (((__BIT__) == SPI_FIRSTBIT_MSB) || \
  547. ((__BIT__) == SPI_FIRSTBIT_LSB))
  548. #if (defined(DMA1) || defined(DMA))
  549. /** @brief Checks if DMA handle is valid.
  550. * @param __HANDLE__ specifies a DMA Handle.
  551. * @retval None
  552. */
  553. #define IS_SPI_DMA_HANDLE(__HANDLE__) ((__HANDLE__) != NULL)
  554. #endif /* DMA1 or DMA */
  555. /** @brief Checks if a data address is 16bit aligned.
  556. * @param __DATA__ specifies a data address.
  557. * @retval None
  558. */
  559. #define IS_SPI_16BIT_ALIGNED_ADDRESS(__DATA__) (((uint32_t)(__DATA__) % 2U) == 0U)
  560. /**
  561. * @}
  562. */
  563. /* Exported functions --------------------------------------------------------*/
  564. /** @addtogroup SPI_Exported_Functions
  565. * @{
  566. */
  567. /** @addtogroup SPI_Exported_Functions_Group1
  568. * @{
  569. */
  570. /* Initialization/de-initialization functions ********************************/
  571. HAL_StatusTypeDef HAL_SPI_Init(SPI_HandleTypeDef *hspi);
  572. HAL_StatusTypeDef HAL_SPI_DeInit(SPI_HandleTypeDef *hspi);
  573. void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi);
  574. void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi);
  575. /* Callbacks Register/UnRegister functions ***********************************/
  576. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  577. HAL_StatusTypeDef HAL_SPI_RegisterCallback(SPI_HandleTypeDef *hspi, HAL_SPI_CallbackIDTypeDef CallbackID, pSPI_CallbackTypeDef pCallback);
  578. HAL_StatusTypeDef HAL_SPI_UnRegisterCallback(SPI_HandleTypeDef *hspi, HAL_SPI_CallbackIDTypeDef CallbackID);
  579. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  580. /**
  581. * @}
  582. */
  583. /** @addtogroup SPI_Exported_Functions_Group2
  584. * @{
  585. */
  586. /* I/O operation functions ***************************************************/
  587. HAL_StatusTypeDef HAL_SPI_Transmit(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  588. HAL_StatusTypeDef HAL_SPI_Receive(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  589. HAL_StatusTypeDef HAL_SPI_TransmitReceive(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size,
  590. uint32_t Timeout);
  591. HAL_StatusTypeDef HAL_SPI_Transmit_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size);
  592. HAL_StatusTypeDef HAL_SPI_Receive_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size);
  593. HAL_StatusTypeDef HAL_SPI_TransmitReceive_IT(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData,
  594. uint16_t Size);
  595. #if (defined(DMA1) || defined(DMA))
  596. HAL_StatusTypeDef HAL_SPI_Transmit_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size);
  597. HAL_StatusTypeDef HAL_SPI_Receive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size);
  598. HAL_StatusTypeDef HAL_SPI_TransmitReceive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData,
  599. uint16_t Size);
  600. HAL_StatusTypeDef HAL_SPI_DMAPause(SPI_HandleTypeDef *hspi);
  601. HAL_StatusTypeDef HAL_SPI_DMAResume(SPI_HandleTypeDef *hspi);
  602. HAL_StatusTypeDef HAL_SPI_DMAStop(SPI_HandleTypeDef *hspi);
  603. #endif /* DMA1 or DMA */
  604. /* Transfer Abort functions */
  605. HAL_StatusTypeDef HAL_SPI_Abort(SPI_HandleTypeDef *hspi);
  606. HAL_StatusTypeDef HAL_SPI_Abort_IT(SPI_HandleTypeDef *hspi);
  607. void HAL_SPI_IRQHandler(SPI_HandleTypeDef *hspi);
  608. void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi);
  609. void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi);
  610. void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi);
  611. void HAL_SPI_TxHalfCpltCallback(SPI_HandleTypeDef *hspi);
  612. void HAL_SPI_RxHalfCpltCallback(SPI_HandleTypeDef *hspi);
  613. void HAL_SPI_TxRxHalfCpltCallback(SPI_HandleTypeDef *hspi);
  614. void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi);
  615. void HAL_SPI_AbortCpltCallback(SPI_HandleTypeDef *hspi);
  616. /**
  617. * @}
  618. */
  619. /** @addtogroup SPI_Exported_Functions_Group3
  620. * @{
  621. */
  622. /* Peripheral State and Error functions ***************************************/
  623. HAL_SPI_StateTypeDef HAL_SPI_GetState(SPI_HandleTypeDef *hspi);
  624. uint32_t HAL_SPI_GetError(SPI_HandleTypeDef *hspi);
  625. /**
  626. * @}
  627. */
  628. /**
  629. * @}
  630. */
  631. /**
  632. * @}
  633. */
  634. /**
  635. * @}
  636. */
  637. #ifdef __cplusplus
  638. }
  639. #endif
  640. #endif /* __PY32F0xx_HAL_SPI_H */
  641. /************************ (C) COPYRIGHT Puya *****END OF FILE****/