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