]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M7_STM32F7_STM32756G-EVAL_IAR_Keil/ST_Library/stm32f7xx_hal_dma_ex.c
Final V8.2.1 release ready for tagging:
[freertos] / FreeRTOS / Demo / CORTEX_M7_STM32F7_STM32756G-EVAL_IAR_Keil / ST_Library / stm32f7xx_hal_dma_ex.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32f7xx_hal_dma_ex.c\r
4   * @author  MCD Application Team\r
5   * @version V1.0.0RC1\r
6   * @date    24-March-2015\r
7   * @brief   DMA Extension HAL module driver\r
8   *         This file provides firmware functions to manage the following \r
9   *         functionalities of the DMA Extension peripheral:\r
10   *           + Extended features functions\r
11   *\r
12   @verbatim\r
13   ==============================================================================\r
14                         ##### How to use this driver #####\r
15   ==============================================================================\r
16   [..]\r
17   The DMA Extension HAL driver can be used as follows:\r
18    (#) Start a multi buffer transfer using the HAL_DMA_MultiBufferStart() function\r
19        for polling mode or HAL_DMA_MultiBufferStart_IT() for interrupt mode.\r
20                    \r
21      -@-  In Memory-to-Memory transfer mode, Multi (Double) Buffer mode is not allowed.\r
22      -@-  When Multi (Double) Buffer mode is enabled the, transfer is circular by default.\r
23      -@-  In Multi (Double) buffer mode, it is possible to update the base address for \r
24           the AHB memory port on the fly (DMA_SxM0AR or DMA_SxM1AR) when the stream is enabled. \r
25   \r
26   @endverbatim\r
27   ******************************************************************************\r
28   * @attention\r
29   *\r
30   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>\r
31   *\r
32   * Redistribution and use in source and binary forms, with or without modification,\r
33   * are permitted provided that the following conditions are met:\r
34   *   1. Redistributions of source code must retain the above copyright notice,\r
35   *      this list of conditions and the following disclaimer.\r
36   *   2. Redistributions in binary form must reproduce the above copyright notice,\r
37   *      this list of conditions and the following disclaimer in the documentation\r
38   *      and/or other materials provided with the distribution.\r
39   *   3. Neither the name of STMicroelectronics nor the names of its contributors\r
40   *      may be used to endorse or promote products derived from this software\r
41   *      without specific prior written permission.\r
42   *\r
43   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
44   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
45   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
46   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\r
47   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
48   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\r
49   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\r
50   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\r
51   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
52   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
53   *\r
54   ******************************************************************************\r
55   */\r
56 \r
57 /* Includes ------------------------------------------------------------------*/\r
58 #include "stm32f7xx_hal.h"\r
59 \r
60 /** @addtogroup STM32F7xx_HAL_Driver\r
61   * @{\r
62   */\r
63 \r
64 /** @defgroup DMAEx DMAEx\r
65   * @brief DMA Extended HAL module driver\r
66   * @{\r
67   */\r
68 \r
69 #ifdef HAL_DMA_MODULE_ENABLED\r
70 \r
71 /* Private types -------------------------------------------------------------*/\r
72 /* Private variables ---------------------------------------------------------*/\r
73 /* Private Constants ---------------------------------------------------------*/\r
74 /* Private macros ------------------------------------------------------------*/\r
75 /* Private functions ---------------------------------------------------------*/\r
76 /** @addtogroup DMAEx_Private_Functions\r
77   * @{\r
78   */\r
79 \r
80 static void DMA_MultiBufferSetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength);\r
81 \r
82 /**\r
83   * @brief  Set the DMA Transfer parameter.\r
84   * @param  hdma:       pointer to a DMA_HandleTypeDef structure that contains\r
85   *                     the configuration information for the specified DMA Stream.  \r
86   * @param  SrcAddress: The source memory Buffer address\r
87   * @param  DstAddress: The destination memory Buffer address\r
88   * @param  DataLength: The length of data to be transferred from source to destination\r
89   * @retval HAL status\r
90   */\r
91 static void DMA_MultiBufferSetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)\r
92 {  \r
93   /* Configure DMA Stream data length */\r
94   hdma->Instance->NDTR = DataLength;\r
95   \r
96   /* Peripheral to Memory */\r
97   if((hdma->Init.Direction) == DMA_MEMORY_TO_PERIPH)\r
98   {   \r
99     /* Configure DMA Stream destination address */\r
100     hdma->Instance->PAR = DstAddress;\r
101     \r
102     /* Configure DMA Stream source address */\r
103     hdma->Instance->M0AR = SrcAddress;\r
104   }\r
105   /* Memory to Peripheral */\r
106   else\r
107   {\r
108     /* Configure DMA Stream source address */\r
109     hdma->Instance->PAR = SrcAddress;\r
110     \r
111     /* Configure DMA Stream destination address */\r
112     hdma->Instance->M0AR = DstAddress;\r
113   }\r
114 }\r
115 \r
116 /**\r
117   * @}\r
118   */\r
119 \r
120 /* Exported functions ---------------------------------------------------------*/\r
121 \r
122 /** @addtogroup DMAEx_Exported_Functions\r
123   * @{\r
124   */\r
125 \r
126 \r
127 /** @addtogroup DMAEx_Exported_Functions_Group1\r
128   *\r
129 @verbatim   \r
130  ===============================================================================\r
131                 #####  Extended features functions  #####\r
132  ===============================================================================  \r
133     [..]  This section provides functions allowing to:\r
134       (+) Configure the source, destination address and data length and \r
135           Start MultiBuffer DMA transfer\r
136       (+) Configure the source, destination address and data length and \r
137           Start MultiBuffer DMA transfer with interrupt\r
138       (+) Change on the fly the memory0 or memory1 address.\r
139       \r
140 @endverbatim\r
141   * @{\r
142   */\r
143 \r
144 \r
145 /**\r
146   * @brief  Starts the multi_buffer DMA Transfer.\r
147   * @param  hdma      : pointer to a DMA_HandleTypeDef structure that contains\r
148   *                     the configuration information for the specified DMA Stream.  \r
149   * @param  SrcAddress: The source memory Buffer address\r
150   * @param  DstAddress: The destination memory Buffer address\r
151   * @param  SecondMemAddress: The second memory Buffer address in case of multi buffer Transfer  \r
152   * @param  DataLength: The length of data to be transferred from source to destination\r
153   * @retval HAL status\r
154   */\r
155 HAL_StatusTypeDef HAL_DMAEx_MultiBufferStart(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t SecondMemAddress, uint32_t DataLength)\r
156 {\r
157   /* Process Locked */\r
158   __HAL_LOCK(hdma);\r
159 \r
160   /* Current memory buffer used is Memory 0 */\r
161   if((hdma->Instance->CR & DMA_SxCR_CT) == 0)\r
162   {\r
163     hdma->State = HAL_DMA_STATE_BUSY_MEM0;\r
164   }\r
165   /* Current memory buffer used is Memory 1 */\r
166   else if((hdma->Instance->CR & DMA_SxCR_CT) != 0)\r
167   {\r
168     hdma->State = HAL_DMA_STATE_BUSY_MEM1;\r
169   }\r
170 \r
171    /* Check the parameters */\r
172   assert_param(IS_DMA_BUFFER_SIZE(DataLength));\r
173 \r
174   /* Disable the peripheral */\r
175   __HAL_DMA_DISABLE(hdma);  \r
176 \r
177   /* Enable the double buffer mode */\r
178   hdma->Instance->CR |= (uint32_t)DMA_SxCR_DBM;\r
179 \r
180   /* Configure DMA Stream destination address */\r
181   hdma->Instance->M1AR = SecondMemAddress;\r
182 \r
183   /* Configure the source, destination address and the data length */\r
184   DMA_MultiBufferSetConfig(hdma, SrcAddress, DstAddress, DataLength);\r
185 \r
186   /* Enable the peripheral */\r
187   __HAL_DMA_ENABLE(hdma);\r
188 \r
189   return HAL_OK;\r
190 }\r
191 \r
192 /**\r
193   * @brief  Starts the multi_buffer DMA Transfer with interrupt enabled.\r
194   * @param  hdma:       pointer to a DMA_HandleTypeDef structure that contains\r
195   *                     the configuration information for the specified DMA Stream.  \r
196   * @param  SrcAddress: The source memory Buffer address\r
197   * @param  DstAddress: The destination memory Buffer address\r
198   * @param  SecondMemAddress: The second memory Buffer address in case of multi buffer Transfer  \r
199   * @param  DataLength: The length of data to be transferred from source to destination\r
200   * @retval HAL status\r
201   */\r
202 HAL_StatusTypeDef HAL_DMAEx_MultiBufferStart_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t SecondMemAddress, uint32_t DataLength)\r
203 {\r
204   /* Process Locked */\r
205   __HAL_LOCK(hdma);\r
206 \r
207   /* Current memory buffer used is Memory 0 */\r
208   if((hdma->Instance->CR & DMA_SxCR_CT) == 0)\r
209   {\r
210     hdma->State = HAL_DMA_STATE_BUSY_MEM0;\r
211   }\r
212   /* Current memory buffer used is Memory 1 */\r
213   else if((hdma->Instance->CR & DMA_SxCR_CT) != 0)\r
214   {\r
215     hdma->State = HAL_DMA_STATE_BUSY_MEM1;\r
216   }\r
217 \r
218   /* Check the parameters */\r
219   assert_param(IS_DMA_BUFFER_SIZE(DataLength));\r
220 \r
221   /* Disable the peripheral */\r
222   __HAL_DMA_DISABLE(hdma);  \r
223 \r
224   /* Enable the Double buffer mode */\r
225   hdma->Instance->CR |= (uint32_t)DMA_SxCR_DBM;\r
226 \r
227   /* Configure DMA Stream destination address */\r
228   hdma->Instance->M1AR = SecondMemAddress;\r
229 \r
230   /* Configure the source, destination address and the data length */\r
231   DMA_MultiBufferSetConfig(hdma, SrcAddress, DstAddress, DataLength); \r
232 \r
233   /* Enable the transfer complete interrupt */\r
234   __HAL_DMA_ENABLE_IT(hdma, DMA_IT_TC);\r
235 \r
236   /* Enable the Half transfer interrupt */\r
237   __HAL_DMA_ENABLE_IT(hdma, DMA_IT_HT);\r
238 \r
239   /* Enable the transfer Error interrupt */\r
240   __HAL_DMA_ENABLE_IT(hdma, DMA_IT_TE);\r
241 \r
242   /* Enable the fifo Error interrupt */\r
243   __HAL_DMA_ENABLE_IT(hdma, DMA_IT_FE);  \r
244 \r
245   /* Enable the direct mode Error interrupt */\r
246   __HAL_DMA_ENABLE_IT(hdma, DMA_IT_DME); \r
247 \r
248   /* Enable the peripheral */\r
249   __HAL_DMA_ENABLE(hdma); \r
250 \r
251   return HAL_OK; \r
252 }\r
253 \r
254 /**\r
255   * @brief  Change the memory0 or memory1 address on the fly.\r
256   * @param  hdma:       pointer to a DMA_HandleTypeDef structure that contains\r
257   *                     the configuration information for the specified DMA Stream.  \r
258   * @param  Address:    The new address\r
259   * @param  memory:     the memory to be changed, This parameter can be one of \r
260   *                     the following values:\r
261   *                      MEMORY0 /\r
262   *                      MEMORY1\r
263   * @note   The MEMORY0 address can be changed only when the current transfer use\r
264   *         MEMORY1 and the MEMORY1 address can be changed only when the current \r
265   *         transfer use MEMORY0.\r
266   * @retval HAL status\r
267   */\r
268 HAL_StatusTypeDef HAL_DMAEx_ChangeMemory(DMA_HandleTypeDef *hdma, uint32_t Address, HAL_DMA_MemoryTypeDef memory)\r
269 {\r
270   if(memory == MEMORY0)\r
271   {\r
272     /* change the memory0 address */\r
273     hdma->Instance->M0AR = Address;\r
274   }\r
275   else\r
276   {\r
277     /* change the memory1 address */\r
278     hdma->Instance->M1AR = Address;\r
279   }\r
280 \r
281   return HAL_OK;\r
282 }\r
283 \r
284 /**\r
285   * @}\r
286   */\r
287 \r
288 /**\r
289   * @}\r
290   */\r
291 \r
292 #endif /* HAL_DMA_MODULE_ENABLED */\r
293 /**\r
294   * @}\r
295   */\r
296 \r
297 /**\r
298   * @}\r
299   */\r
300 \r
301 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/\r