py32f002b_ll_spi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /**
  2. ******************************************************************************
  3. * @file py32f002b_ll_spi.c
  4. * @author MCU Application Team
  5. * @brief SPI LL module driver.
  6. ******************************************************************************
  7. Additional table :
  8. DataSize = SPI_DATASIZE_8BIT:
  9. +----------------------------------------------------------------------------------------------+
  10. | | | 2Lines Fullduplex | 2Lines RxOnly | 1Line |
  11. | Process | Tranfert mode |---------------------|----------------------|----------------------|
  12. | | | Master | Slave | Master | Slave | Master | Slave |
  13. |==============================================================================================|
  14. | TX | Polling | Fpclk/2 | Fpclk/32 | NA | NA | NA | NA |
  15. | / |----------------|----------|----------|-----------|----------|-----------|----------|
  16. | RX | Interrupt | Fpclk/2 | Fpclk/32 | NA | NA | NA | NA |
  17. | |----------------|----------|----------|-----------|----------|-----------|----------|
  18. |=========|================|==========|==========|===========|==========|===========|==========|
  19. | | Polling | NA | NA | Fpclk/4 | Fpclk/8 | Fpclk/4 | Fpclk/8 |
  20. | R |----------------|----------|----------|-----------|----------|-----------|----------|
  21. | X | Interrupt | NA | NA | Fpclk/16 | Fpclk/16 | Fpclk/16 | Fpclk/16 |
  22. | |----------------|----------|----------|-----------|----------|-----------|----------|
  23. |=========|================|==========|==========|===========|==========|===========|==========|
  24. | | Polling | NA | NA | NA | NA | Fpclk/2 | Fpclk/8 |
  25. | T |----------------|----------|----------|-----------|----------|-----------|----------|
  26. | X | Interrupt | NA | NA | NA | NA | Fpclk/2 | Fpclk/16 |
  27. | |----------------|----------|----------|-----------|----------|-----------|----------|
  28. +----------------------------------------------------------------------------------------------+
  29. DataSize = SPI_DATASIZE_16BIT:
  30. +----------------------------------------------------------------------------------------------+
  31. | | | 2Lines Fullduplex | 2Lines RxOnly | 1Line |
  32. | Process | Tranfert mode |---------------------|----------------------|----------------------|
  33. | | | Master | Slave | Master | Slave | Master | Slave |
  34. |==============================================================================================|
  35. | TX | Polling | Fpclk/2 | Fpclk/16 | NA | NA | NA | NA |
  36. | / |----------------|----------|----------|-----------|----------|-----------|----------|
  37. | RX | Interrupt | Fpclk/2 | Fpclk/16 | NA | NA | NA | NA |
  38. | |----------------|----------|----------|-----------|----------|-----------|----------|
  39. |=========|================|==========|==========|===========|==========|===========|==========|
  40. | | Polling | NA | NA | Fpclk/2 | Fpclk/4 | Fpclk/2 | Fpclk/4 |
  41. | |----------------|----------|----------|-----------|----------|-----------|----------|
  42. | R | Interrupt | NA | NA | Fpclk/2 | Fpclk/8 | Fpclk/2 | Fpclk/8 |
  43. | X |----------------|----------|----------|-----------|----------|-----------|----------|
  44. |=========|================|==========|==========|===========|==========|===========|==========|
  45. | | Polling | NA | NA | NA | NA | Fpclk/2 | Fpclk/4 |
  46. | |----------------|----------|----------|-----------|----------|-----------|----------|
  47. | T | Interrupt | NA | NA | NA | NA | Fpclk/2 | Fpclk/8 |
  48. | X |----------------|----------|----------|-----------|----------|-----------|----------|
  49. +----------------------------------------------------------------------------------------------+
  50. @note The max SPI frequency depend on SPI data size (8bits, 16bits),
  51. SPI mode(2 Lines fullduplex, 2 lines RxOnly, 1 line TX/RX) and Process mode (Polling, IT).
  52. ******************************************************************************
  53. * @attention
  54. *
  55. * <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
  56. * All rights reserved.</center></h2>
  57. *
  58. * This software component is licensed by Puya under BSD 3-Clause license,
  59. * the "License"; You may not use this file except in compliance with the
  60. * License. You may obtain a copy of the License at:
  61. * opensource.org/licenses/BSD-3-Clause
  62. *
  63. ******************************************************************************
  64. * @attention
  65. *
  66. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  67. * All rights reserved.</center></h2>
  68. *
  69. * This software component is licensed by ST under BSD 3-Clause license,
  70. * the "License"; You may not use this file except in compliance with the
  71. * License. You may obtain a copy of the License at:
  72. * opensource.org/licenses/BSD-3-Clause
  73. *
  74. ******************************************************************************
  75. */
  76. #if defined(USE_FULL_LL_DRIVER)
  77. /* Includes ------------------------------------------------------------------*/
  78. #include "py32f002b_ll_spi.h"
  79. #include "py32f002b_ll_bus.h"
  80. #include "py32f002b_ll_rcc.h"
  81. #ifdef USE_FULL_ASSERT
  82. #include "py32_assert.h"
  83. #else
  84. #define assert_param(expr) ((void)0U)
  85. #endif /* USE_FULL_ASSERT */
  86. /** @addtogroup PY32F002B_LL_Driver
  87. * @{
  88. */
  89. #if defined (SPI1)
  90. /** @addtogroup SPI_LL
  91. * @{
  92. */
  93. /* Private types -------------------------------------------------------------*/
  94. /* Private variables ---------------------------------------------------------*/
  95. /* Private constants ---------------------------------------------------------*/
  96. /** @defgroup SPI_LL_Private_Constants SPI Private Constants
  97. * @{
  98. */
  99. /* SPI registers Masks */
  100. #define SPI_CR1_CLEAR_MASK (SPI_CR1_CPHA | SPI_CR1_CPOL | SPI_CR1_MSTR | \
  101. SPI_CR1_BR | SPI_CR1_LSBFIRST | SPI_CR1_SSI | \
  102. SPI_CR1_SSM | SPI_CR1_RXONLY | SPI_CR1_DFF | \
  103. SPI_CR1_BIDIOE | SPI_CR1_BIDIMODE)
  104. /**
  105. * @}
  106. */
  107. /* Private macros ------------------------------------------------------------*/
  108. /** @defgroup SPI_LL_Private_Macros SPI Private Macros
  109. * @{
  110. */
  111. #define IS_LL_SPI_TRANSFER_DIRECTION(__VALUE__) (((__VALUE__) == LL_SPI_FULL_DUPLEX) \
  112. || ((__VALUE__) == LL_SPI_SIMPLEX_RX) \
  113. || ((__VALUE__) == LL_SPI_HALF_DUPLEX_RX) \
  114. || ((__VALUE__) == LL_SPI_HALF_DUPLEX_TX))
  115. #define IS_LL_SPI_MODE(__VALUE__) (((__VALUE__) == LL_SPI_MODE_MASTER) \
  116. || ((__VALUE__) == LL_SPI_MODE_SLAVE))
  117. #define IS_LL_SPI_DATAWIDTH(__VALUE__) (((__VALUE__) == LL_SPI_DATAWIDTH_8BIT) \
  118. || ((__VALUE__) == LL_SPI_DATAWIDTH_16BIT))
  119. #define IS_LL_SPI_POLARITY(__VALUE__) (((__VALUE__) == LL_SPI_POLARITY_LOW) \
  120. || ((__VALUE__) == LL_SPI_POLARITY_HIGH))
  121. #define IS_LL_SPI_PHASE(__VALUE__) (((__VALUE__) == LL_SPI_PHASE_1EDGE) \
  122. || ((__VALUE__) == LL_SPI_PHASE_2EDGE))
  123. #define IS_LL_SPI_NSS(__VALUE__) (((__VALUE__) == LL_SPI_NSS_SOFT) \
  124. || ((__VALUE__) == LL_SPI_NSS_HARD_INPUT) \
  125. || ((__VALUE__) == LL_SPI_NSS_HARD_OUTPUT))
  126. #define IS_LL_SPI_BAUDRATE(__VALUE__) (((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV2) \
  127. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV4) \
  128. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV8) \
  129. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV16) \
  130. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV32) \
  131. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV64) \
  132. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV128) \
  133. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV256))
  134. #define IS_LL_SPI_BITORDER(__VALUE__) (((__VALUE__) == LL_SPI_LSB_FIRST) \
  135. || ((__VALUE__) == LL_SPI_MSB_FIRST))
  136. /**
  137. * @}
  138. */
  139. /* Private function prototypes -----------------------------------------------*/
  140. /* Exported functions --------------------------------------------------------*/
  141. /** @addtogroup SPI_LL_Exported_Functions
  142. * @{
  143. */
  144. /** @addtogroup SPI_LL_EF_Init
  145. * @{
  146. */
  147. /**
  148. * @brief De-initialize the SPI registers to their default reset values.
  149. * @param SPIx SPI Instance
  150. * @retval An ErrorStatus enumeration value:
  151. * - SUCCESS: SPI registers are de-initialized
  152. * - ERROR: SPI registers are not de-initialized
  153. */
  154. ErrorStatus LL_SPI_DeInit(SPI_TypeDef *SPIx)
  155. {
  156. ErrorStatus status = ERROR;
  157. /* Check the parameters */
  158. assert_param(IS_SPI_ALL_INSTANCE(SPIx));
  159. if (SPIx == SPI1)
  160. {
  161. /* Force reset of SPI clock */
  162. LL_APB1_GRP2_ForceReset(LL_APB1_GRP2_PERIPH_SPI1);
  163. /* Release reset of SPI clock */
  164. LL_APB1_GRP2_ReleaseReset(LL_APB1_GRP2_PERIPH_SPI1);
  165. status = SUCCESS;
  166. }
  167. return status;
  168. }
  169. /**
  170. * @brief Initialize the SPI registers according to the specified parameters in SPI_InitStruct.
  171. * @note As some bits in SPI configuration registers can only be written when the SPI is disabled (SPI_CR1_SPE bit =0),
  172. * SPI peripheral should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
  173. * @param SPIx SPI Instance
  174. * @param SPI_InitStruct pointer to a @ref LL_SPI_InitTypeDef structure
  175. * @retval An ErrorStatus enumeration value. (Return always SUCCESS)
  176. */
  177. ErrorStatus LL_SPI_Init(SPI_TypeDef *SPIx, LL_SPI_InitTypeDef *SPI_InitStruct)
  178. {
  179. ErrorStatus status = ERROR;
  180. /* Check the SPI Instance SPIx*/
  181. assert_param(IS_SPI_ALL_INSTANCE(SPIx));
  182. /* Check the SPI parameters from SPI_InitStruct*/
  183. assert_param(IS_LL_SPI_TRANSFER_DIRECTION(SPI_InitStruct->TransferDirection));
  184. assert_param(IS_LL_SPI_MODE(SPI_InitStruct->Mode));
  185. assert_param(IS_LL_SPI_DATAWIDTH(SPI_InitStruct->DataWidth));
  186. assert_param(IS_LL_SPI_POLARITY(SPI_InitStruct->ClockPolarity));
  187. assert_param(IS_LL_SPI_PHASE(SPI_InitStruct->ClockPhase));
  188. assert_param(IS_LL_SPI_NSS(SPI_InitStruct->NSS));
  189. assert_param(IS_LL_SPI_BAUDRATE(SPI_InitStruct->BaudRate));
  190. assert_param(IS_LL_SPI_BITORDER(SPI_InitStruct->BitOrder));
  191. if (LL_SPI_IsEnabled(SPIx) == 0x00000000U)
  192. {
  193. /*---------------------------- SPIx CR1 Configuration ------------------------
  194. * Configure SPIx CR1 with parameters:
  195. * - TransferDirection: SPI_CR1_BIDIMODE, SPI_CR1_BIDIOE and SPI_CR1_RXONLY bits
  196. * - Master/Slave Mode: SPI_CR1_MSTR bit
  197. * - ClockPolarity: SPI_CR1_CPOL bit
  198. * - ClockPhase: SPI_CR1_CPHA bit
  199. * - NSS management: SPI_CR1_SSM bit
  200. * - BaudRate prescaler: SPI_CR1_BR[2:0] bits
  201. * - BitOrder: SPI_CR1_LSBFIRST bit
  202. * - DataWidth: SPI_CR1_DFF bit
  203. */
  204. MODIFY_REG(SPIx->CR1,
  205. SPI_CR1_CLEAR_MASK,
  206. SPI_InitStruct->TransferDirection | SPI_InitStruct->Mode |
  207. SPI_InitStruct->ClockPolarity | SPI_InitStruct->ClockPhase |
  208. (SPI_InitStruct->NSS & SPI_CR1_SSM) | SPI_InitStruct->BaudRate |
  209. SPI_InitStruct->BitOrder | SPI_InitStruct->DataWidth);
  210. /*---------------------------- SPIx CR2 Configuration ------------------------
  211. * Configure SPIx CR2 with parameters:
  212. * - NSS management: SSOE bit
  213. */
  214. MODIFY_REG(SPIx->CR2, SPI_CR2_SSOE, ((SPI_InitStruct->NSS >> 16U) & SPI_CR2_SSOE));
  215. status = SUCCESS;
  216. }
  217. return status;
  218. }
  219. /**
  220. * @brief Set each @ref LL_SPI_InitTypeDef field to default value.
  221. * @param SPI_InitStruct pointer to a @ref LL_SPI_InitTypeDef structure
  222. * whose fields will be set to default values.
  223. * @retval None
  224. */
  225. void LL_SPI_StructInit(LL_SPI_InitTypeDef *SPI_InitStruct)
  226. {
  227. /* Set SPI_InitStruct fields to default values */
  228. SPI_InitStruct->TransferDirection = LL_SPI_FULL_DUPLEX;
  229. SPI_InitStruct->Mode = LL_SPI_MODE_SLAVE;
  230. SPI_InitStruct->DataWidth = LL_SPI_DATAWIDTH_8BIT;
  231. SPI_InitStruct->ClockPolarity = LL_SPI_POLARITY_LOW;
  232. SPI_InitStruct->ClockPhase = LL_SPI_PHASE_1EDGE;
  233. SPI_InitStruct->NSS = LL_SPI_NSS_HARD_INPUT;
  234. SPI_InitStruct->BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV2;
  235. SPI_InitStruct->BitOrder = LL_SPI_MSB_FIRST;
  236. }
  237. /**
  238. * @}
  239. */
  240. /**
  241. * @}
  242. */
  243. /**
  244. * @}
  245. */
  246. #endif /* defined (SPI1) */
  247. /**
  248. * @}
  249. */
  250. #endif /* USE_FULL_LL_DRIVER */
  251. /************************ (C) COPYRIGHT Puya *****END OF FILE****/