py32f002b_ll_spi.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. /**
  2. ******************************************************************************
  3. * @file py32f002b_ll_spi.h
  4. * @author MCU Application Team
  5. * @brief Header file of SPI LL 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_LL_SPI_H
  32. #define PY32F002B_LL_SPI_H
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /* Includes ------------------------------------------------------------------*/
  37. #include "py32f0xx.h"
  38. /** @addtogroup PY32F002B_LL_Driver
  39. * @{
  40. */
  41. #if defined (SPI1)
  42. /** @defgroup SPI_LL SPI
  43. * @{
  44. */
  45. /* Private types -------------------------------------------------------------*/
  46. /* Private variables ---------------------------------------------------------*/
  47. /* Private macros ------------------------------------------------------------*/
  48. /* Exported types ------------------------------------------------------------*/
  49. #if defined(USE_FULL_LL_DRIVER)
  50. /** @defgroup SPI_LL_ES_INIT SPI Exported Init structure
  51. * @{
  52. */
  53. /**
  54. * @brief SPI Init structures definition
  55. */
  56. typedef struct
  57. {
  58. uint32_t TransferDirection; /*!< Specifies the SPI unidirectional or bidirectional data mode.
  59. This parameter can be a value of @ref SPI_LL_EC_TRANSFER_MODE.
  60. This feature can be modified afterwards using unitary function @ref LL_SPI_SetTransferDirection().*/
  61. uint32_t Mode; /*!< Specifies the SPI mode (Master/Slave).
  62. This parameter can be a value of @ref SPI_LL_EC_MODE.
  63. This feature can be modified afterwards using unitary function @ref LL_SPI_SetMode().*/
  64. uint32_t DataWidth; /*!< Specifies the SPI data width.
  65. This parameter can be a value of @ref SPI_LL_EC_DATAWIDTH.
  66. This feature can be modified afterwards using unitary function @ref LL_SPI_SetDataWidth().*/
  67. uint32_t ClockPolarity; /*!< Specifies the serial clock steady state.
  68. This parameter can be a value of @ref SPI_LL_EC_POLARITY.
  69. This feature can be modified afterwards using unitary function @ref LL_SPI_SetClockPolarity().*/
  70. uint32_t ClockPhase; /*!< Specifies the clock active edge for the bit capture.
  71. This parameter can be a value of @ref SPI_LL_EC_PHASE.
  72. This feature can be modified afterwards using unitary function @ref LL_SPI_SetClockPhase().*/
  73. uint32_t NSS; /*!< Specifies whether the NSS signal is managed by hardware (NSS pin) or by software using the SSI bit.
  74. This parameter can be a value of @ref SPI_LL_EC_NSS_MODE.
  75. This feature can be modified afterwards using unitary function @ref LL_SPI_SetNSSMode().*/
  76. uint32_t BaudRate; /*!< Specifies the BaudRate prescaler value which will be used to configure the transmit and receive SCK clock.
  77. This parameter can be a value of @ref SPI_LL_EC_BAUDRATEPRESCALER.
  78. @note The communication clock is derived from the master clock. The slave clock does not need to be set.
  79. This feature can be modified afterwards using unitary function @ref LL_SPI_SetBaudRatePrescaler().*/
  80. uint32_t BitOrder; /*!< Specifies whether data transfers start from MSB or LSB bit.
  81. This parameter can be a value of @ref SPI_LL_EC_BIT_ORDER.
  82. This feature can be modified afterwards using unitary function @ref LL_SPI_SetTransferBitOrder().*/
  83. } LL_SPI_InitTypeDef;
  84. /**
  85. * @}
  86. */
  87. #endif /* USE_FULL_LL_DRIVER */
  88. /* Exported constants --------------------------------------------------------*/
  89. /** @defgroup SPI_LL_Exported_Constants SPI Exported Constants
  90. * @{
  91. */
  92. /** @defgroup SPI_LL_EC_GET_FLAG Get Flags Defines
  93. * @brief Flags defines which can be used with LL_SPI_ReadReg function
  94. * @{
  95. */
  96. #define LL_SPI_SR_RXNE SPI_SR_RXNE /*!< Rx buffer not empty flag */
  97. #define LL_SPI_SR_TXE SPI_SR_TXE /*!< Tx buffer empty flag */
  98. #define LL_SPI_SR_BSY SPI_SR_BSY /*!< Busy flag */
  99. #define LL_SPI_SR_MODF SPI_SR_MODF /*!< Mode fault flag */
  100. #define LL_SPI_SR_OVR SPI_SR_OVR /*!< Overrun flag */
  101. /**
  102. * @}
  103. */
  104. /** @defgroup SPI_LL_EC_IT IT Defines
  105. * @brief IT defines which can be used with LL_SPI_ReadReg and LL_SPI_WriteReg functions
  106. * @{
  107. */
  108. #define LL_SPI_CR2_RXNEIE SPI_CR2_RXNEIE /*!< Rx buffer not empty interrupt enable */
  109. #define LL_SPI_CR2_TXEIE SPI_CR2_TXEIE /*!< Tx buffer empty interrupt enable */
  110. #define LL_SPI_CR2_ERRIE SPI_CR2_ERRIE /*!< Error interrupt enable */
  111. /**
  112. * @}
  113. */
  114. /** @defgroup SPI_LL_EC_MODE Operation Mode
  115. * @{
  116. */
  117. #define LL_SPI_MODE_MASTER (SPI_CR1_MSTR | SPI_CR1_SSI) /*!< Master configuration */
  118. #define LL_SPI_MODE_SLAVE 0x00000000U /*!< Slave configuration */
  119. /**
  120. * @}
  121. */
  122. /** @defgroup SPI_LL_EC_PHASE Clock Phase
  123. * @{
  124. */
  125. #define LL_SPI_PHASE_1EDGE 0x00000000U /*!< First clock transition is the first data capture edge */
  126. #define LL_SPI_PHASE_2EDGE (SPI_CR1_CPHA) /*!< Second clock transition is the first data capture edge */
  127. /**
  128. * @}
  129. */
  130. /** @defgroup SPI_LL_EC_POLARITY Clock Polarity
  131. * @{
  132. */
  133. #define LL_SPI_POLARITY_LOW 0x00000000U /*!< Clock to 0 when idle */
  134. #define LL_SPI_POLARITY_HIGH (SPI_CR1_CPOL) /*!< Clock to 1 when idle */
  135. /**
  136. * @}
  137. */
  138. /** @defgroup SPI_LL_EC_BAUDRATEPRESCALER Baud Rate Prescaler
  139. * @{
  140. */
  141. #define LL_SPI_BAUDRATEPRESCALER_DIV2 0x00000000U /*!< BaudRate control equal to fPCLK/2 */
  142. #define LL_SPI_BAUDRATEPRESCALER_DIV4 (SPI_CR1_BR_0) /*!< BaudRate control equal to fPCLK/4 */
  143. #define LL_SPI_BAUDRATEPRESCALER_DIV8 (SPI_CR1_BR_1) /*!< BaudRate control equal to fPCLK/8 */
  144. #define LL_SPI_BAUDRATEPRESCALER_DIV16 (SPI_CR1_BR_1 | SPI_CR1_BR_0) /*!< BaudRate control equal to fPCLK/16 */
  145. #define LL_SPI_BAUDRATEPRESCALER_DIV32 (SPI_CR1_BR_2) /*!< BaudRate control equal to fPCLK/32 */
  146. #define LL_SPI_BAUDRATEPRESCALER_DIV64 (SPI_CR1_BR_2 | SPI_CR1_BR_0) /*!< BaudRate control equal to fPCLK/64 */
  147. #define LL_SPI_BAUDRATEPRESCALER_DIV128 (SPI_CR1_BR_2 | SPI_CR1_BR_1) /*!< BaudRate control equal to fPCLK/128 */
  148. #define LL_SPI_BAUDRATEPRESCALER_DIV256 (SPI_CR1_BR_2 | SPI_CR1_BR_1 | SPI_CR1_BR_0) /*!< BaudRate control equal to fPCLK/256 */
  149. /**
  150. * @}
  151. */
  152. /** @defgroup SPI_LL_EC_BIT_ORDER Transmission Bit Order
  153. * @{
  154. */
  155. #define LL_SPI_LSB_FIRST (SPI_CR1_LSBFIRST) /*!< Data is transmitted/received with the LSB first */
  156. #define LL_SPI_MSB_FIRST 0x00000000U /*!< Data is transmitted/received with the MSB first */
  157. /**
  158. * @}
  159. */
  160. /** @defgroup SPI_LL_EC_TRANSFER_MODE Transfer Mode
  161. * @{
  162. */
  163. #define LL_SPI_FULL_DUPLEX 0x00000000U /*!< Full-Duplex mode. Rx and Tx transfer on 2 lines */
  164. #define LL_SPI_SIMPLEX_RX (SPI_CR1_RXONLY) /*!< Simplex Rx mode. Rx transfer only on 1 line */
  165. #define LL_SPI_HALF_DUPLEX_RX (SPI_CR1_BIDIMODE) /*!< Half-Duplex Rx mode. Rx transfer on 1 line */
  166. #define LL_SPI_HALF_DUPLEX_TX (SPI_CR1_BIDIMODE | SPI_CR1_BIDIOE) /*!< Half-Duplex Tx mode. Tx transfer on 1 line */
  167. /**
  168. * @}
  169. */
  170. /** @defgroup SPI_LL_EC_NSS_MODE Slave Select Pin Mode
  171. * @{
  172. */
  173. #define LL_SPI_NSS_SOFT (SPI_CR1_SSM) /*!< NSS managed internally. NSS pin not used and free */
  174. #define LL_SPI_NSS_HARD_INPUT 0x00000000U /*!< NSS pin used in Input. Only used in Master mode */
  175. #define LL_SPI_NSS_HARD_OUTPUT (((uint32_t)SPI_CR2_SSOE << 16U)) /*!< NSS pin used in Output. Only used in Slave mode as chip select */
  176. /**
  177. * @}
  178. */
  179. /** @defgroup SPI_LL_EC_DATAWIDTH Datawidth
  180. * @{
  181. */
  182. #define LL_SPI_DATAWIDTH_8BIT 0x0 /*!< Data length for SPI transfer: 8 bits */
  183. #define LL_SPI_DATAWIDTH_16BIT (SPI_CR1_DFF) /*!< Data length for SPI transfer: 16 bits */
  184. /**
  185. * @}
  186. */
  187. #if defined(USE_FULL_LL_DRIVER)
  188. #endif /* USE_FULL_LL_DRIVER */
  189. /** @defgroup SPI_LL_EC_RX_FIFO RX FIFO Level
  190. * @{
  191. */
  192. #define LL_SPI_RX_FIFO_EMPTY 0x00000000U /*!< FIFO reception empty */
  193. #define LL_SPI_RX_FIFO_FULL (SPI_SR_FRLVL) /*!< FIFO reception full */
  194. /**
  195. * @}
  196. */
  197. /** @defgroup SPI_LL_EC_TX_FIFO TX FIFO Level
  198. * @{
  199. */
  200. #define LL_SPI_TX_FIFO_EMPTY 0x00000000U /*!< FIFO transmission empty */
  201. #define LL_SPI_TX_FIFO_FULL (SPI_SR_FTLVL) /*!< FIFO transmission full */
  202. /**
  203. * @}
  204. */
  205. /**
  206. * @}
  207. */
  208. /* Exported macro ------------------------------------------------------------*/
  209. /** @defgroup SPI_LL_Exported_Macros SPI Exported Macros
  210. * @{
  211. */
  212. /** @defgroup SPI_LL_EM_WRITE_READ Common Write and read registers Macros
  213. * @{
  214. */
  215. /**
  216. * @brief Write a value in SPI register
  217. * @param __INSTANCE__ SPI Instance
  218. * @param __REG__ Register to be written
  219. * @param __VALUE__ Value to be written in the register
  220. * @retval None
  221. */
  222. #define LL_SPI_WriteReg(__INSTANCE__, __REG__, __VALUE__) WRITE_REG(__INSTANCE__->__REG__, (__VALUE__))
  223. /**
  224. * @brief Read a value in SPI register
  225. * @param __INSTANCE__ SPI Instance
  226. * @param __REG__ Register to be read
  227. * @retval Register value
  228. */
  229. #define LL_SPI_ReadReg(__INSTANCE__, __REG__) READ_REG(__INSTANCE__->__REG__)
  230. /**
  231. * @}
  232. */
  233. /**
  234. * @}
  235. */
  236. /* Exported functions --------------------------------------------------------*/
  237. /** @defgroup SPI_LL_Exported_Functions SPI Exported Functions
  238. * @{
  239. */
  240. /** @defgroup SPI_LL_EF_Configuration Configuration
  241. * @{
  242. */
  243. /**
  244. * @brief Enable SPI peripheral
  245. * @rmtoll CR1 SPE LL_SPI_Enable
  246. * @param SPIx SPI Instance
  247. * @retval None
  248. */
  249. __STATIC_INLINE void LL_SPI_Enable(SPI_TypeDef *SPIx)
  250. {
  251. SET_BIT(SPIx->CR1, SPI_CR1_SPE);
  252. }
  253. /**
  254. * @brief Disable SPI peripheral
  255. * @note When disabling the SPI, follow the procedure described in the Reference Manual.
  256. * @rmtoll CR1 SPE LL_SPI_Disable
  257. * @param SPIx SPI Instance
  258. * @retval None
  259. */
  260. __STATIC_INLINE void LL_SPI_Disable(SPI_TypeDef *SPIx)
  261. {
  262. CLEAR_BIT(SPIx->CR1, SPI_CR1_SPE);
  263. }
  264. /**
  265. * @brief Check if SPI peripheral is enabled
  266. * @rmtoll CR1 SPE LL_SPI_IsEnabled
  267. * @param SPIx SPI Instance
  268. * @retval State of bit (1 or 0).
  269. */
  270. __STATIC_INLINE uint32_t LL_SPI_IsEnabled(SPI_TypeDef *SPIx)
  271. {
  272. return ((READ_BIT(SPIx->CR1, SPI_CR1_SPE) == (SPI_CR1_SPE)) ? 1UL : 0UL);
  273. }
  274. /**
  275. * @brief Set SPI operation mode to Master or Slave
  276. * @note This bit should not be changed when communication is ongoing.
  277. * @rmtoll CR1 MSTR LL_SPI_SetMode\n
  278. * CR1 SSI LL_SPI_SetMode
  279. * @param SPIx SPI Instance
  280. * @param Mode This parameter can be one of the following values:
  281. * @arg @ref LL_SPI_MODE_MASTER
  282. * @arg @ref LL_SPI_MODE_SLAVE
  283. * @retval None
  284. */
  285. __STATIC_INLINE void LL_SPI_SetMode(SPI_TypeDef *SPIx, uint32_t Mode)
  286. {
  287. MODIFY_REG(SPIx->CR1, SPI_CR1_MSTR | SPI_CR1_SSI, Mode);
  288. }
  289. /**
  290. * @brief Get SPI operation mode (Master or Slave)
  291. * @rmtoll CR1 MSTR LL_SPI_GetMode\n
  292. * CR1 SSI LL_SPI_GetMode
  293. * @param SPIx SPI Instance
  294. * @retval Returned value can be one of the following values:
  295. * @arg @ref LL_SPI_MODE_MASTER
  296. * @arg @ref LL_SPI_MODE_SLAVE
  297. */
  298. __STATIC_INLINE uint32_t LL_SPI_GetMode(SPI_TypeDef *SPIx)
  299. {
  300. return (uint32_t)(READ_BIT(SPIx->CR1, SPI_CR1_MSTR | SPI_CR1_SSI));
  301. }
  302. /**
  303. * @brief Set clock phase
  304. * @note This bit should not be changed when communication is ongoing.
  305. * This bit is not used in SPI TI mode.
  306. * @rmtoll CR1 CPHA LL_SPI_SetClockPhase
  307. * @param SPIx SPI Instance
  308. * @param ClockPhase This parameter can be one of the following values:
  309. * @arg @ref LL_SPI_PHASE_1EDGE
  310. * @arg @ref LL_SPI_PHASE_2EDGE
  311. * @retval None
  312. */
  313. __STATIC_INLINE void LL_SPI_SetClockPhase(SPI_TypeDef *SPIx, uint32_t ClockPhase)
  314. {
  315. MODIFY_REG(SPIx->CR1, SPI_CR1_CPHA, ClockPhase);
  316. }
  317. /**
  318. * @brief Get clock phase
  319. * @rmtoll CR1 CPHA LL_SPI_GetClockPhase
  320. * @param SPIx SPI Instance
  321. * @retval Returned value can be one of the following values:
  322. * @arg @ref LL_SPI_PHASE_1EDGE
  323. * @arg @ref LL_SPI_PHASE_2EDGE
  324. */
  325. __STATIC_INLINE uint32_t LL_SPI_GetClockPhase(SPI_TypeDef *SPIx)
  326. {
  327. return (uint32_t)(READ_BIT(SPIx->CR1, SPI_CR1_CPHA));
  328. }
  329. /**
  330. * @brief Set clock polarity
  331. * @note This bit should not be changed when communication is ongoing.
  332. * This bit is not used in SPI TI mode.
  333. * @rmtoll CR1 CPOL LL_SPI_SetClockPolarity
  334. * @param SPIx SPI Instance
  335. * @param ClockPolarity This parameter can be one of the following values:
  336. * @arg @ref LL_SPI_POLARITY_LOW
  337. * @arg @ref LL_SPI_POLARITY_HIGH
  338. * @retval None
  339. */
  340. __STATIC_INLINE void LL_SPI_SetClockPolarity(SPI_TypeDef *SPIx, uint32_t ClockPolarity)
  341. {
  342. MODIFY_REG(SPIx->CR1, SPI_CR1_CPOL, ClockPolarity);
  343. }
  344. /**
  345. * @brief Get clock polarity
  346. * @rmtoll CR1 CPOL LL_SPI_GetClockPolarity
  347. * @param SPIx SPI Instance
  348. * @retval Returned value can be one of the following values:
  349. * @arg @ref LL_SPI_POLARITY_LOW
  350. * @arg @ref LL_SPI_POLARITY_HIGH
  351. */
  352. __STATIC_INLINE uint32_t LL_SPI_GetClockPolarity(SPI_TypeDef *SPIx)
  353. {
  354. return (uint32_t)(READ_BIT(SPIx->CR1, SPI_CR1_CPOL));
  355. }
  356. /**
  357. * @brief Set baud rate prescaler
  358. * @note These bits should not be changed when communication is ongoing. SPI BaudRate = fPCLK/Prescaler.
  359. * @rmtoll CR1 BR LL_SPI_SetBaudRatePrescaler
  360. * @param SPIx SPI Instance
  361. * @param BaudRate This parameter can be one of the following values:
  362. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV2
  363. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV4
  364. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV8
  365. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV16
  366. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV32
  367. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV64
  368. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV128
  369. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV256
  370. * @retval None
  371. */
  372. __STATIC_INLINE void LL_SPI_SetBaudRatePrescaler(SPI_TypeDef *SPIx, uint32_t BaudRate)
  373. {
  374. MODIFY_REG(SPIx->CR1, SPI_CR1_BR, BaudRate);
  375. }
  376. /**
  377. * @brief Get baud rate prescaler
  378. * @rmtoll CR1 BR LL_SPI_GetBaudRatePrescaler
  379. * @param SPIx SPI Instance
  380. * @retval Returned value can be one of the following values:
  381. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV2
  382. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV4
  383. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV8
  384. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV16
  385. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV32
  386. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV64
  387. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV128
  388. * @arg @ref LL_SPI_BAUDRATEPRESCALER_DIV256
  389. */
  390. __STATIC_INLINE uint32_t LL_SPI_GetBaudRatePrescaler(SPI_TypeDef *SPIx)
  391. {
  392. return (uint32_t)(READ_BIT(SPIx->CR1, SPI_CR1_BR));
  393. }
  394. /**
  395. * @brief Set transfer bit order
  396. * @note This bit should not be changed when communication is ongoing. This bit is not used in SPI TI mode.
  397. * @rmtoll CR1 LSBFIRST LL_SPI_SetTransferBitOrder
  398. * @param SPIx SPI Instance
  399. * @param BitOrder This parameter can be one of the following values:
  400. * @arg @ref LL_SPI_LSB_FIRST
  401. * @arg @ref LL_SPI_MSB_FIRST
  402. * @retval None
  403. */
  404. __STATIC_INLINE void LL_SPI_SetTransferBitOrder(SPI_TypeDef *SPIx, uint32_t BitOrder)
  405. {
  406. MODIFY_REG(SPIx->CR1, SPI_CR1_LSBFIRST, BitOrder);
  407. }
  408. /**
  409. * @brief Get transfer bit order
  410. * @rmtoll CR1 LSBFIRST LL_SPI_GetTransferBitOrder
  411. * @param SPIx SPI Instance
  412. * @retval Returned value can be one of the following values:
  413. * @arg @ref LL_SPI_LSB_FIRST
  414. * @arg @ref LL_SPI_MSB_FIRST
  415. */
  416. __STATIC_INLINE uint32_t LL_SPI_GetTransferBitOrder(SPI_TypeDef *SPIx)
  417. {
  418. return (uint32_t)(READ_BIT(SPIx->CR1, SPI_CR1_LSBFIRST));
  419. }
  420. /**
  421. * @brief Set transfer direction mode
  422. * @note For Half-Duplex mode, Rx Direction is set by default.
  423. * In master mode, the MOSI pin is used and in slave mode, the MISO pin is used for Half-Duplex.
  424. * @rmtoll CR1 RXONLY LL_SPI_SetTransferDirection\n
  425. * CR1 BIDIMODE LL_SPI_SetTransferDirection\n
  426. * CR1 BIDIOE LL_SPI_SetTransferDirection
  427. * @param SPIx SPI Instance
  428. * @param TransferDirection This parameter can be one of the following values:
  429. * @arg @ref LL_SPI_FULL_DUPLEX
  430. * @arg @ref LL_SPI_SIMPLEX_RX
  431. * @arg @ref LL_SPI_HALF_DUPLEX_RX
  432. * @arg @ref LL_SPI_HALF_DUPLEX_TX
  433. * @retval None
  434. */
  435. __STATIC_INLINE void LL_SPI_SetTransferDirection(SPI_TypeDef *SPIx, uint32_t TransferDirection)
  436. {
  437. MODIFY_REG(SPIx->CR1, SPI_CR1_RXONLY | SPI_CR1_BIDIMODE | SPI_CR1_BIDIOE, TransferDirection);
  438. }
  439. /**
  440. * @brief Get transfer direction mode
  441. * @rmtoll CR1 RXONLY LL_SPI_GetTransferDirection\n
  442. * CR1 BIDIMODE LL_SPI_GetTransferDirection\n
  443. * CR1 BIDIOE LL_SPI_GetTransferDirection
  444. * @param SPIx SPI Instance
  445. * @retval Returned value can be one of the following values:
  446. * @arg @ref LL_SPI_FULL_DUPLEX
  447. * @arg @ref LL_SPI_SIMPLEX_RX
  448. * @arg @ref LL_SPI_HALF_DUPLEX_RX
  449. * @arg @ref LL_SPI_HALF_DUPLEX_TX
  450. */
  451. __STATIC_INLINE uint32_t LL_SPI_GetTransferDirection(SPI_TypeDef *SPIx)
  452. {
  453. return (uint32_t)(READ_BIT(SPIx->CR1, SPI_CR1_RXONLY | SPI_CR1_BIDIMODE | SPI_CR1_BIDIOE));
  454. }
  455. /**
  456. * @brief Set frame data width
  457. * @rmtoll CR2 DS LL_SPI_SetDataWidth
  458. * @param SPIx SPI Instance
  459. * @param DataWidth This parameter can be one of the following values:
  460. * @arg @ref LL_SPI_DATAWIDTH_8BIT
  461. * @arg @ref LL_SPI_DATAWIDTH_16BIT
  462. * @retval None
  463. */
  464. __STATIC_INLINE void LL_SPI_SetDataWidth(SPI_TypeDef *SPIx, uint32_t DataWidth)
  465. {
  466. MODIFY_REG(SPIx->CR1, SPI_CR1_DFF, DataWidth);
  467. }
  468. /**
  469. * @brief Get frame data width
  470. * @rmtoll CR2 DS LL_SPI_GetDataWidth
  471. * @param SPIx SPI Instance
  472. * @retval Returned value can be one of the following values:
  473. * @arg @ref LL_SPI_DATAWIDTH_8BIT
  474. * @arg @ref LL_SPI_DATAWIDTH_16BIT
  475. */
  476. __STATIC_INLINE uint32_t LL_SPI_GetDataWidth(SPI_TypeDef *SPIx)
  477. {
  478. return (uint32_t)(READ_BIT(SPIx->CR1, SPI_CR1_DFF));
  479. }
  480. /**
  481. * @brief Clear TXFIFO
  482. * @param SPIx SPI Instance
  483. * @retval None
  484. */
  485. __STATIC_INLINE void LL_SPI_ClearTxFifo(SPI_TypeDef *SPIx)
  486. {
  487. SET_BIT(SPIx->CR2, SPI_CR2_CLRTXFIFO);
  488. }
  489. /**
  490. * @}
  491. */
  492. /** @defgroup SPI_LL_EF_NSS_Management Slave Select Pin Management
  493. * @{
  494. */
  495. /**
  496. * @brief Set NSS mode
  497. * @rmtoll CR1 SSM LL_SPI_SetNSSMode\n
  498. * @rmtoll CR2 SSOE LL_SPI_SetNSSMode
  499. * @param SPIx SPI Instance
  500. * @param NSS This parameter can be one of the following values:
  501. * @arg @ref LL_SPI_NSS_SOFT
  502. * @arg @ref LL_SPI_NSS_HARD_INPUT
  503. * @arg @ref LL_SPI_NSS_HARD_OUTPUT
  504. * @retval None
  505. */
  506. __STATIC_INLINE void LL_SPI_SetNSSMode(SPI_TypeDef *SPIx, uint32_t NSS)
  507. {
  508. MODIFY_REG(SPIx->CR1, SPI_CR1_SSM, NSS);
  509. MODIFY_REG(SPIx->CR2, SPI_CR2_SSOE, ((uint32_t)(NSS >> 16U)));
  510. }
  511. /**
  512. * @brief Get NSS mode
  513. * @rmtoll CR1 SSM LL_SPI_GetNSSMode\n
  514. * @rmtoll CR2 SSOE LL_SPI_GetNSSMode
  515. * @param SPIx SPI Instance
  516. * @retval Returned value can be one of the following values:
  517. * @arg @ref LL_SPI_NSS_SOFT
  518. * @arg @ref LL_SPI_NSS_HARD_INPUT
  519. * @arg @ref LL_SPI_NSS_HARD_OUTPUT
  520. */
  521. __STATIC_INLINE uint32_t LL_SPI_GetNSSMode(SPI_TypeDef *SPIx)
  522. {
  523. uint32_t Ssm = (READ_BIT(SPIx->CR1, SPI_CR1_SSM));
  524. uint32_t Ssoe = (READ_BIT(SPIx->CR2, SPI_CR2_SSOE) << 16U);
  525. return (Ssm | Ssoe);
  526. }
  527. /**
  528. * @}
  529. */
  530. /** @defgroup SPI_LL_EF_FLAG_Management FLAG Management
  531. * @{
  532. */
  533. /**
  534. * @brief Check if Rx buffer is not empty
  535. * @rmtoll SR RXNE LL_SPI_IsActiveFlag_RXNE
  536. * @param SPIx SPI Instance
  537. * @retval State of bit (1 or 0).
  538. */
  539. __STATIC_INLINE uint32_t LL_SPI_IsActiveFlag_RXNE(SPI_TypeDef *SPIx)
  540. {
  541. return ((READ_BIT(SPIx->SR, SPI_SR_RXNE) == (SPI_SR_RXNE)) ? 1UL : 0UL);
  542. }
  543. /**
  544. * @brief Check if Tx buffer is empty
  545. * @rmtoll SR TXE LL_SPI_IsActiveFlag_TXE
  546. * @param SPIx SPI Instance
  547. * @retval State of bit (1 or 0).
  548. */
  549. __STATIC_INLINE uint32_t LL_SPI_IsActiveFlag_TXE(SPI_TypeDef *SPIx)
  550. {
  551. return ((READ_BIT(SPIx->SR, SPI_SR_TXE) == (SPI_SR_TXE)) ? 1UL : 0UL);
  552. }
  553. /**
  554. * @brief Get mode fault error flag
  555. * @rmtoll SR MODF LL_SPI_IsActiveFlag_MODF
  556. * @param SPIx SPI Instance
  557. * @retval State of bit (1 or 0).
  558. */
  559. __STATIC_INLINE uint32_t LL_SPI_IsActiveFlag_MODF(SPI_TypeDef *SPIx)
  560. {
  561. return ((READ_BIT(SPIx->SR, SPI_SR_MODF) == (SPI_SR_MODF)) ? 1UL : 0UL);
  562. }
  563. /**
  564. * @brief Get overrun error flag
  565. * @rmtoll SR OVR LL_SPI_IsActiveFlag_OVR
  566. * @param SPIx SPI Instance
  567. * @retval State of bit (1 or 0).
  568. */
  569. __STATIC_INLINE uint32_t LL_SPI_IsActiveFlag_OVR(SPI_TypeDef *SPIx)
  570. {
  571. return ((READ_BIT(SPIx->SR, SPI_SR_OVR) == (SPI_SR_OVR)) ? 1UL : 0UL);
  572. }
  573. /**
  574. * @brief Get busy flag
  575. * @note The BSY flag is cleared under any one of the following conditions:
  576. * -When the SPI is correctly disabled
  577. * -When a fault is detected in Master mode (MODF bit set to 1)
  578. * -In Master mode, when it finishes a data transmission and no new data is ready to be
  579. * sent
  580. * -In Slave mode, when the BSY flag is set to '0' for at least one SPI clock cycle between
  581. * each data transfer.
  582. * @rmtoll SR BSY LL_SPI_IsActiveFlag_BSY
  583. * @param SPIx SPI Instance
  584. * @retval State of bit (1 or 0).
  585. */
  586. __STATIC_INLINE uint32_t LL_SPI_IsActiveFlag_BSY(SPI_TypeDef *SPIx)
  587. {
  588. return ((READ_BIT(SPIx->SR, SPI_SR_BSY) == (SPI_SR_BSY)) ? 1UL : 0UL);
  589. }
  590. /**
  591. * @brief Get FIFO reception Level
  592. * @rmtoll SR FRLVL LL_SPI_GetRxFIFOLevel
  593. * @param SPIx SPI Instance
  594. * @retval Returned value can be one of the following values:
  595. * @arg @ref LL_SPI_RX_FIFO_EMPTY
  596. * @arg @ref LL_SPI_RX_FIFO_FULL
  597. */
  598. __STATIC_INLINE uint32_t LL_SPI_GetRxFIFOLevel(SPI_TypeDef *SPIx)
  599. {
  600. return (uint32_t)(READ_BIT(SPIx->SR, SPI_SR_FRLVL));
  601. }
  602. /**
  603. * @brief Get FIFO Transmission Level
  604. * @rmtoll SR FTLVL LL_SPI_GetTxFIFOLevel
  605. * @param SPIx SPI Instance
  606. * @retval Returned value can be one of the following values:
  607. * @arg @ref LL_SPI_TX_FIFO_EMPTY
  608. * @arg @ref LL_SPI_TX_FIFO_FULL
  609. */
  610. __STATIC_INLINE uint32_t LL_SPI_GetTxFIFOLevel(SPI_TypeDef *SPIx)
  611. {
  612. return (uint32_t)(READ_BIT(SPIx->SR, SPI_SR_FTLVL));
  613. }
  614. /**
  615. * @brief Clear mode fault error flag
  616. * @note Clearing this flag is done by a read access to the SPIx_SR
  617. * register followed by a write access to the SPIx_CR1 register
  618. * @rmtoll SR MODF LL_SPI_ClearFlag_MODF
  619. * @param SPIx SPI Instance
  620. * @retval None
  621. */
  622. __STATIC_INLINE void LL_SPI_ClearFlag_MODF(SPI_TypeDef *SPIx)
  623. {
  624. __IO uint32_t tmpreg_sr;
  625. tmpreg_sr = SPIx->SR;
  626. (void) tmpreg_sr;
  627. CLEAR_BIT(SPIx->CR1, SPI_CR1_SPE);
  628. }
  629. /**
  630. * @brief Clear overrun error flag
  631. * @note Clearing this flag is done by a read access to the SPIx_DR
  632. * register followed by a read access to the SPIx_SR register
  633. * @rmtoll SR OVR LL_SPI_ClearFlag_OVR
  634. * @param SPIx SPI Instance
  635. * @retval None
  636. */
  637. __STATIC_INLINE void LL_SPI_ClearFlag_OVR(SPI_TypeDef *SPIx)
  638. {
  639. __IO uint32_t tmpreg;
  640. tmpreg = SPIx->DR;
  641. (void) tmpreg;
  642. tmpreg = SPIx->SR;
  643. (void) tmpreg;
  644. }
  645. /**
  646. * @}
  647. */
  648. /** @defgroup SPI_LL_EF_IT_Management Interrupt Management
  649. * @{
  650. */
  651. /**
  652. * @brief Enable error interrupt
  653. * @note This bit controls the generation of an interrupt when an error condition occurs (CRCERR, OVR, MODF in SPI mode, FRE at TI mode).
  654. * @rmtoll CR2 ERRIE LL_SPI_EnableIT_ERR
  655. * @param SPIx SPI Instance
  656. * @retval None
  657. */
  658. __STATIC_INLINE void LL_SPI_EnableIT_ERR(SPI_TypeDef *SPIx)
  659. {
  660. SET_BIT(SPIx->CR2, SPI_CR2_ERRIE);
  661. }
  662. /**
  663. * @brief Enable Rx buffer not empty interrupt
  664. * @rmtoll CR2 RXNEIE LL_SPI_EnableIT_RXNE
  665. * @param SPIx SPI Instance
  666. * @retval None
  667. */
  668. __STATIC_INLINE void LL_SPI_EnableIT_RXNE(SPI_TypeDef *SPIx)
  669. {
  670. SET_BIT(SPIx->CR2, SPI_CR2_RXNEIE);
  671. }
  672. /**
  673. * @brief Enable Tx buffer empty interrupt
  674. * @rmtoll CR2 TXEIE LL_SPI_EnableIT_TXE
  675. * @param SPIx SPI Instance
  676. * @retval None
  677. */
  678. __STATIC_INLINE void LL_SPI_EnableIT_TXE(SPI_TypeDef *SPIx)
  679. {
  680. SET_BIT(SPIx->CR2, SPI_CR2_TXEIE);
  681. }
  682. /**
  683. * @brief Disable error interrupt
  684. * @note This bit controls the generation of an interrupt when an error condition occurs (CRCERR, OVR, MODF in SPI mode, FRE at TI mode).
  685. * @rmtoll CR2 ERRIE LL_SPI_DisableIT_ERR
  686. * @param SPIx SPI Instance
  687. * @retval None
  688. */
  689. __STATIC_INLINE void LL_SPI_DisableIT_ERR(SPI_TypeDef *SPIx)
  690. {
  691. CLEAR_BIT(SPIx->CR2, SPI_CR2_ERRIE);
  692. }
  693. /**
  694. * @brief Disable Rx buffer not empty interrupt
  695. * @rmtoll CR2 RXNEIE LL_SPI_DisableIT_RXNE
  696. * @param SPIx SPI Instance
  697. * @retval None
  698. */
  699. __STATIC_INLINE void LL_SPI_DisableIT_RXNE(SPI_TypeDef *SPIx)
  700. {
  701. CLEAR_BIT(SPIx->CR2, SPI_CR2_RXNEIE);
  702. }
  703. /**
  704. * @brief Disable Tx buffer empty interrupt
  705. * @rmtoll CR2 TXEIE LL_SPI_DisableIT_TXE
  706. * @param SPIx SPI Instance
  707. * @retval None
  708. */
  709. __STATIC_INLINE void LL_SPI_DisableIT_TXE(SPI_TypeDef *SPIx)
  710. {
  711. CLEAR_BIT(SPIx->CR2, SPI_CR2_TXEIE);
  712. }
  713. /**
  714. * @brief Check if error interrupt is enabled
  715. * @rmtoll CR2 ERRIE LL_SPI_IsEnabledIT_ERR
  716. * @param SPIx SPI Instance
  717. * @retval State of bit (1 or 0).
  718. */
  719. __STATIC_INLINE uint32_t LL_SPI_IsEnabledIT_ERR(SPI_TypeDef *SPIx)
  720. {
  721. return ((READ_BIT(SPIx->CR2, SPI_CR2_ERRIE) == (SPI_CR2_ERRIE)) ? 1UL : 0UL);
  722. }
  723. /**
  724. * @brief Check if Rx buffer not empty interrupt is enabled
  725. * @rmtoll CR2 RXNEIE LL_SPI_IsEnabledIT_RXNE
  726. * @param SPIx SPI Instance
  727. * @retval State of bit (1 or 0).
  728. */
  729. __STATIC_INLINE uint32_t LL_SPI_IsEnabledIT_RXNE(SPI_TypeDef *SPIx)
  730. {
  731. return ((READ_BIT(SPIx->CR2, SPI_CR2_RXNEIE) == (SPI_CR2_RXNEIE)) ? 1UL : 0UL);
  732. }
  733. /**
  734. * @brief Check if Tx buffer empty interrupt
  735. * @rmtoll CR2 TXEIE LL_SPI_IsEnabledIT_TXE
  736. * @param SPIx SPI Instance
  737. * @retval State of bit (1 or 0).
  738. */
  739. __STATIC_INLINE uint32_t LL_SPI_IsEnabledIT_TXE(SPI_TypeDef *SPIx)
  740. {
  741. return ((READ_BIT(SPIx->CR2, SPI_CR2_TXEIE) == (SPI_CR2_TXEIE)) ? 1UL : 0UL);
  742. }
  743. /**
  744. * @}
  745. */
  746. /** @defgroup SPI_LL_EF_DATA_Management DATA Management
  747. * @{
  748. */
  749. /**
  750. * @brief Read 8-Bits in the data register
  751. * @rmtoll DR DR LL_SPI_ReceiveData8
  752. * @param SPIx SPI Instance
  753. * @retval RxData Value between Min_Data=0x00 and Max_Data=0xFF
  754. */
  755. __STATIC_INLINE uint8_t LL_SPI_ReceiveData8(SPI_TypeDef *SPIx)
  756. {
  757. return (*((__IO uint8_t *)&SPIx->DR));
  758. }
  759. /**
  760. * @brief Read 16-Bits in the data register
  761. * @rmtoll DR DR LL_SPI_ReceiveData16
  762. * @param SPIx SPI Instance
  763. * @retval RxData Value between Min_Data=0x00 and Max_Data=0xFFFF
  764. */
  765. __STATIC_INLINE uint16_t LL_SPI_ReceiveData16(SPI_TypeDef *SPIx)
  766. {
  767. return (uint16_t)(READ_REG(SPIx->DR));
  768. }
  769. /**
  770. * @brief Write 8-Bits in the data register
  771. * @rmtoll DR DR LL_SPI_TransmitData8
  772. * @param SPIx SPI Instance
  773. * @param TxData Value between Min_Data=0x00 and Max_Data=0xFF
  774. * @retval None
  775. */
  776. __STATIC_INLINE void LL_SPI_TransmitData8(SPI_TypeDef *SPIx, uint8_t TxData)
  777. {
  778. #if defined (__GNUC__)
  779. __IO uint8_t *spidr = ((__IO uint8_t *)&SPIx->DR);
  780. *spidr = TxData;
  781. #else
  782. *((__IO uint8_t *)&SPIx->DR) = TxData;
  783. #endif /* __GNUC__ */
  784. }
  785. /**
  786. * @brief Write 16-Bits in the data register
  787. * @rmtoll DR DR LL_SPI_TransmitData16
  788. * @param SPIx SPI Instance
  789. * @param TxData Value between Min_Data=0x00 and Max_Data=0xFFFF
  790. * @retval None
  791. */
  792. __STATIC_INLINE void LL_SPI_TransmitData16(SPI_TypeDef *SPIx, uint16_t TxData)
  793. {
  794. #if defined (__GNUC__)
  795. __IO uint16_t *spidr = ((__IO uint16_t *)&SPIx->DR);
  796. *spidr = TxData;
  797. #else
  798. SPIx->DR = TxData;
  799. #endif /* __GNUC__ */
  800. }
  801. /**
  802. * @}
  803. */
  804. #if defined(USE_FULL_LL_DRIVER)
  805. /** @defgroup SPI_LL_EF_Init Initialization and de-initialization functions
  806. * @{
  807. */
  808. ErrorStatus LL_SPI_DeInit(SPI_TypeDef *SPIx);
  809. ErrorStatus LL_SPI_Init(SPI_TypeDef *SPIx, LL_SPI_InitTypeDef *SPI_InitStruct);
  810. void LL_SPI_StructInit(LL_SPI_InitTypeDef *SPI_InitStruct);
  811. /**
  812. * @}
  813. */
  814. #endif /* USE_FULL_LL_DRIVER */
  815. /**
  816. * @}
  817. */
  818. /**
  819. * @}
  820. */
  821. #endif /* defined (SPI1) */
  822. /**
  823. * @}
  824. */
  825. #ifdef __cplusplus
  826. }
  827. #endif
  828. #endif /* PY32F002B_LL_SPI_H */
  829. /************************ (C) COPYRIGHT Puya *****END OF FILE****/