py32f0xx_ll_dma.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /**
  2. ******************************************************************************
  3. * @file py32f0xx_ll_dma.c
  4. * @author MCU Application Team
  5. * @brief DMA LL module driver.
  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. #if defined(USE_FULL_LL_DRIVER)
  31. /* Includes ------------------------------------------------------------------*/
  32. #include "py32f0xx_ll_dma.h"
  33. #ifdef USE_FULL_ASSERT
  34. #include "py32_assert.h"
  35. #else
  36. #define assert_param(expr) ((void)0U)
  37. #endif
  38. /** @addtogroup PY32F0xx_LL_Driver
  39. * @{
  40. */
  41. #if defined (DMA1)
  42. /** @defgroup DMA_LL DMA
  43. * @{
  44. */
  45. /* Private types -------------------------------------------------------------*/
  46. /* Private variables ---------------------------------------------------------*/
  47. /* Private constants ---------------------------------------------------------*/
  48. /* Private macros ------------------------------------------------------------*/
  49. /** @addtogroup DMA_LL_Private_Macros
  50. * @{
  51. */
  52. #define IS_LL_DMA_DIRECTION(__VALUE__) (((__VALUE__) == LL_DMA_DIRECTION_PERIPH_TO_MEMORY) || \
  53. ((__VALUE__) == LL_DMA_DIRECTION_MEMORY_TO_PERIPH) || \
  54. ((__VALUE__) == LL_DMA_DIRECTION_MEMORY_TO_MEMORY))
  55. #define IS_LL_DMA_MODE(__VALUE__) (((__VALUE__) == LL_DMA_MODE_NORMAL) || \
  56. ((__VALUE__) == LL_DMA_MODE_CIRCULAR))
  57. #define IS_LL_DMA_PERIPHINCMODE(__VALUE__) (((__VALUE__) == LL_DMA_PERIPH_INCREMENT) || \
  58. ((__VALUE__) == LL_DMA_PERIPH_NOINCREMENT))
  59. #define IS_LL_DMA_MEMORYINCMODE(__VALUE__) (((__VALUE__) == LL_DMA_MEMORY_INCREMENT) || \
  60. ((__VALUE__) == LL_DMA_MEMORY_NOINCREMENT))
  61. #define IS_LL_DMA_PERIPHDATASIZE(__VALUE__) (((__VALUE__) == LL_DMA_PDATAALIGN_BYTE) || \
  62. ((__VALUE__) == LL_DMA_PDATAALIGN_HALFWORD) || \
  63. ((__VALUE__) == LL_DMA_PDATAALIGN_WORD))
  64. #define IS_LL_DMA_MEMORYDATASIZE(__VALUE__) (((__VALUE__) == LL_DMA_MDATAALIGN_BYTE) || \
  65. ((__VALUE__) == LL_DMA_MDATAALIGN_HALFWORD) || \
  66. ((__VALUE__) == LL_DMA_MDATAALIGN_WORD))
  67. #define IS_LL_DMA_NBDATA(__VALUE__) ((__VALUE__) <= 0x0000FFFFU)
  68. #define IS_LL_DMA_PRIORITY(__VALUE__) (((__VALUE__) == LL_DMA_PRIORITY_LOW) || \
  69. ((__VALUE__) == LL_DMA_PRIORITY_MEDIUM) || \
  70. ((__VALUE__) == LL_DMA_PRIORITY_HIGH) || \
  71. ((__VALUE__) == LL_DMA_PRIORITY_VERYHIGH))
  72. #define IS_LL_DMA_ALL_CHANNEL_INSTANCE(INSTANCE, CHANNEL) ((((INSTANCE) == DMA1) && \
  73. (((CHANNEL) == LL_DMA_CHANNEL_1) || \
  74. ((CHANNEL) == LL_DMA_CHANNEL_2) || \
  75. ((CHANNEL) == LL_DMA_CHANNEL_3))))
  76. /**
  77. * @}
  78. */
  79. /* Private function prototypes -----------------------------------------------*/
  80. /* Exported functions --------------------------------------------------------*/
  81. /** @addtogroup DMA_LL_Exported_Functions
  82. * @{
  83. */
  84. /** @addtogroup DMA_LL_EF_Init
  85. * @{
  86. */
  87. /**
  88. * @brief De-initialize the DMA registers to their default reset values.
  89. * @param DMAx DMAx Instance
  90. * @param Channel This parameter can be one of the following values:
  91. * @arg @ref LL_DMA_CHANNEL_1
  92. * @arg @ref LL_DMA_CHANNEL_2
  93. * @arg @ref LL_DMA_CHANNEL_3
  94. * @retval An ErrorStatus enumeration value:
  95. * - SUCCESS: DMA registers are de-initialized
  96. * - ERROR: DMA registers are not de-initialized
  97. */
  98. uint32_t LL_DMA_DeInit(DMA_TypeDef *DMAx, uint32_t Channel)
  99. {
  100. DMA_Channel_TypeDef *tmp = (DMA_Channel_TypeDef *)DMA1_Channel1;
  101. ErrorStatus status = SUCCESS;
  102. /* Check the DMA Instance DMAx and Channel parameters*/
  103. assert_param(IS_LL_DMA_ALL_CHANNEL_INSTANCE(DMAx, Channel));
  104. tmp = (DMA_Channel_TypeDef *)(__LL_DMA_GET_CHANNEL_INSTANCE(DMAx, Channel));
  105. /* Disable the selected DMAx_Channely */
  106. CLEAR_BIT(tmp->CCR, DMA_CCR_EN);
  107. /* Reset DMAx_Channely control register */
  108. LL_DMA_WriteReg(tmp, CCR, 0U);
  109. /* Reset DMAx_Channely remaining bytes register */
  110. LL_DMA_WriteReg(tmp, CNDTR, 0U);
  111. /* Reset DMAx_Channely peripheral address register */
  112. LL_DMA_WriteReg(tmp, CPAR, 0U);
  113. /* Reset DMAx_Channely memory address register */
  114. LL_DMA_WriteReg(tmp, CMAR, 0U);
  115. if (Channel == LL_DMA_CHANNEL_1)
  116. {
  117. /* Reset interrupt pending bits for DMAx Channel1 */
  118. LL_DMA_ClearFlag_GI1(DMAx);
  119. }
  120. else if (Channel == LL_DMA_CHANNEL_2)
  121. {
  122. /* Reset interrupt pending bits for DMAx Channel2 */
  123. LL_DMA_ClearFlag_GI2(DMAx);
  124. }
  125. else if (Channel == LL_DMA_CHANNEL_3)
  126. {
  127. /* Reset interrupt pending bits for DMAx Channel3 */
  128. LL_DMA_ClearFlag_GI3(DMAx);
  129. }
  130. else
  131. {
  132. status = ERROR;
  133. }
  134. return status;
  135. }
  136. /**
  137. * @brief Initialize the DMA registers according to the specified parameters in DMA_InitStruct.
  138. * @note To convert DMAx_Channely Instance to DMAx Instance and Channely, use helper macros :
  139. * @arg @ref __LL_DMA_GET_INSTANCE
  140. * @arg @ref __LL_DMA_GET_CHANNEL
  141. * @param DMAx DMAx Instance
  142. * @param Channel This parameter can be one of the following values:
  143. * @arg @ref LL_DMA_CHANNEL_1
  144. * @arg @ref LL_DMA_CHANNEL_2
  145. * @arg @ref LL_DMA_CHANNEL_3
  146. * @param DMA_InitStruct pointer to a @ref LL_DMA_InitTypeDef structure.
  147. * @retval An ErrorStatus enumeration value:
  148. * - SUCCESS: DMA registers are initialized
  149. * - ERROR: Not applicable
  150. */
  151. uint32_t LL_DMA_Init(DMA_TypeDef *DMAx, uint32_t Channel, LL_DMA_InitTypeDef *DMA_InitStruct)
  152. {
  153. /* Check the DMA Instance DMAx and Channel parameters*/
  154. assert_param(IS_LL_DMA_ALL_CHANNEL_INSTANCE(DMAx, Channel));
  155. /* Check the DMA parameters from DMA_InitStruct */
  156. assert_param(IS_LL_DMA_DIRECTION(DMA_InitStruct->Direction));
  157. assert_param(IS_LL_DMA_MODE(DMA_InitStruct->Mode));
  158. assert_param(IS_LL_DMA_PERIPHINCMODE(DMA_InitStruct->PeriphOrM2MSrcIncMode));
  159. assert_param(IS_LL_DMA_MEMORYINCMODE(DMA_InitStruct->MemoryOrM2MDstIncMode));
  160. assert_param(IS_LL_DMA_PERIPHDATASIZE(DMA_InitStruct->PeriphOrM2MSrcDataSize));
  161. assert_param(IS_LL_DMA_MEMORYDATASIZE(DMA_InitStruct->MemoryOrM2MDstDataSize));
  162. assert_param(IS_LL_DMA_NBDATA(DMA_InitStruct->NbData));
  163. assert_param(IS_LL_DMA_PRIORITY(DMA_InitStruct->Priority));
  164. /*---------------------------- DMAx CCR Configuration ------------------------
  165. * Configure DMAx_Channely: data transfer direction, data transfer mode,
  166. * peripheral and memory increment mode,
  167. * data size alignment and priority level with parameters :
  168. * - Direction: DMA_CCR_DIR and DMA_CCR_MEM2MEM bits
  169. * - Mode: DMA_CCR_CIRC bit
  170. * - PeriphOrM2MSrcIncMode: DMA_CCR_PINC bit
  171. * - MemoryOrM2MDstIncMode: DMA_CCR_MINC bit
  172. * - PeriphOrM2MSrcDataSize: DMA_CCR_PSIZE[1:0] bits
  173. * - MemoryOrM2MDstDataSize: DMA_CCR_MSIZE[1:0] bits
  174. * - Priority: DMA_CCR_PL[1:0] bits
  175. */
  176. LL_DMA_ConfigTransfer(DMAx, Channel, DMA_InitStruct->Direction | \
  177. DMA_InitStruct->Mode | \
  178. DMA_InitStruct->PeriphOrM2MSrcIncMode | \
  179. DMA_InitStruct->MemoryOrM2MDstIncMode | \
  180. DMA_InitStruct->PeriphOrM2MSrcDataSize | \
  181. DMA_InitStruct->MemoryOrM2MDstDataSize | \
  182. DMA_InitStruct->Priority);
  183. /*-------------------------- DMAx CMAR Configuration -------------------------
  184. * Configure the memory or destination base address with parameter :
  185. * - MemoryOrM2MDstAddress: DMA_CMAR_MA[31:0] bits
  186. */
  187. LL_DMA_SetMemoryAddress(DMAx, Channel, DMA_InitStruct->MemoryOrM2MDstAddress);
  188. /*-------------------------- DMAx CPAR Configuration -------------------------
  189. * Configure the peripheral or source base address with parameter :
  190. * - PeriphOrM2MSrcAddress: DMA_CPAR_PA[31:0] bits
  191. */
  192. LL_DMA_SetPeriphAddress(DMAx, Channel, DMA_InitStruct->PeriphOrM2MSrcAddress);
  193. /*--------------------------- DMAx CNDTR Configuration -----------------------
  194. * Configure the peripheral base address with parameter :
  195. * - NbData: DMA_CNDTR_NDT[15:0] bits
  196. */
  197. LL_DMA_SetDataLength(DMAx, Channel, DMA_InitStruct->NbData);
  198. return SUCCESS;
  199. }
  200. /**
  201. * @brief Set each @ref LL_DMA_InitTypeDef field to default value.
  202. * @param DMA_InitStruct Pointer to a @ref LL_DMA_InitTypeDef structure.
  203. * @retval None
  204. */
  205. void LL_DMA_StructInit(LL_DMA_InitTypeDef *DMA_InitStruct)
  206. {
  207. /* Set DMA_InitStruct fields to default values */
  208. DMA_InitStruct->PeriphOrM2MSrcAddress = 0x00000000U;
  209. DMA_InitStruct->MemoryOrM2MDstAddress = 0x00000000U;
  210. DMA_InitStruct->Direction = LL_DMA_DIRECTION_PERIPH_TO_MEMORY;
  211. DMA_InitStruct->Mode = LL_DMA_MODE_NORMAL;
  212. DMA_InitStruct->PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT;
  213. DMA_InitStruct->MemoryOrM2MDstIncMode = LL_DMA_MEMORY_NOINCREMENT;
  214. DMA_InitStruct->PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_BYTE;
  215. DMA_InitStruct->MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_BYTE;
  216. DMA_InitStruct->NbData = 0x00000000U;
  217. DMA_InitStruct->Priority = LL_DMA_PRIORITY_LOW;
  218. }
  219. /**
  220. * @}
  221. */
  222. /**
  223. * @}
  224. */
  225. /**
  226. * @}
  227. */
  228. #endif /* DMA1 || DMA2 */
  229. /**
  230. * @}
  231. */
  232. #endif /* USE_FULL_LL_DRIVER */
  233. /************************ (C) COPYRIGHT Puya *****END OF FILE****/