py32f002b_hal_spi.h 29 KB

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