]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F4/stm32f4xx_hal_dma_ex.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32F4 / stm32f4xx_hal_dma_ex.c
1 /**
2   ******************************************************************************
3   * @file    stm32f4xx_hal_dma_ex.c
4   * @author  MCD Application Team
5   * @version V1.1.0
6   * @date    19-June-2014
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) 2014 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 "stm32f4xx_hal.h"
59
60 /** @addtogroup STM32F4xx_HAL_Driver
61   * @{
62   */
63
64 /** @defgroup DMAEx 
65   * @brief DMA Extended HAL module driver
66   * @{
67   */
68
69 #ifdef HAL_DMA_MODULE_ENABLED
70
71 /* Private typedef -----------------------------------------------------------*/
72 /* Private define ------------------------------------------------------------*/
73 /* Private macro -------------------------------------------------------------*/
74 /* Private variables ---------------------------------------------------------*/
75 /* Private function prototypes -----------------------------------------------*/
76 static void DMA_MultiBufferSetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength);
77
78 /* Private functions ---------------------------------------------------------*/
79
80 /** @defgroup DMAEx_Private_Functions
81   * @{
82   */
83
84
85 /** @defgroup DMAEx_Group1 Extended features functions 
86  *  @brief   Extended features functions   
87  *
88 @verbatim   
89  ===============================================================================
90                 #####  Extended features functions  #####
91  ===============================================================================  
92     [..]  This section provides functions allowing to:
93       (+) Configure the source, destination address and data length and 
94           Start MultiBuffer DMA transfer
95       (+) Configure the source, destination address and data length and 
96           Start MultiBuffer DMA transfer with interrupt
97       (+) Change on the fly the memory0 or memory1 address.
98       
99 @endverbatim
100   * @{
101   */
102
103
104 /**
105   * @brief  Starts the multi_buffer DMA Transfer.
106   * @param  hdma      : pointer to a DMA_HandleTypeDef structure that contains
107   *                     the configuration information for the specified DMA Stream.  
108   * @param  SrcAddress: The source memory Buffer address
109   * @param  DstAddress: The destination memory Buffer address
110   * @param  SecondMemAddress: The second memory Buffer address in case of multi buffer Transfer  
111   * @param  DataLength: The length of data to be transferred from source to destination
112   * @retval HAL status
113   */
114 HAL_StatusTypeDef HAL_DMAEx_MultiBufferStart(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t SecondMemAddress, uint32_t DataLength)
115 {
116   /* Process Locked */
117   __HAL_LOCK(hdma);
118
119   /* Current memory buffer used is Memory 0 */
120   if((hdma->Instance->CR & DMA_SxCR_CT) == 0)
121   {
122     hdma->State = HAL_DMA_STATE_BUSY_MEM0;
123   }
124   /* Current memory buffer used is Memory 1 */
125   else if((hdma->Instance->CR & DMA_SxCR_CT) != 0)
126   {
127     hdma->State = HAL_DMA_STATE_BUSY_MEM1;
128   }
129
130    /* Check the parameters */
131   assert_param(IS_DMA_BUFFER_SIZE(DataLength));
132
133   /* Disable the peripheral */
134   __HAL_DMA_DISABLE(hdma);  
135
136   /* Enable the double buffer mode */
137   hdma->Instance->CR |= (uint32_t)DMA_SxCR_DBM;
138
139   /* Configure DMA Stream destination address */
140   hdma->Instance->M1AR = SecondMemAddress;
141
142   /* Configure the source, destination address and the data length */
143   DMA_MultiBufferSetConfig(hdma, SrcAddress, DstAddress, DataLength);
144
145   /* Enable the peripheral */
146   __HAL_DMA_ENABLE(hdma);
147
148   return HAL_OK;
149 }
150
151 /**
152   * @brief  Starts the multi_buffer DMA Transfer with interrupt enabled.
153   * @param  hdma:       pointer to a DMA_HandleTypeDef structure that contains
154   *                     the configuration information for the specified DMA Stream.  
155   * @param  SrcAddress: The source memory Buffer address
156   * @param  DstAddress: The destination memory Buffer address
157   * @param  SecondMemAddress: The second memory Buffer address in case of multi buffer Transfer  
158   * @param  DataLength: The length of data to be transferred from source to destination
159   * @retval HAL status
160   */
161 HAL_StatusTypeDef HAL_DMAEx_MultiBufferStart_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t SecondMemAddress, uint32_t DataLength)
162 {
163   /* Process Locked */
164   __HAL_LOCK(hdma);
165
166   /* Current memory buffer used is Memory 0 */
167   if((hdma->Instance->CR & DMA_SxCR_CT) == 0)
168   {
169     hdma->State = HAL_DMA_STATE_BUSY_MEM0;
170   }
171   /* Current memory buffer used is Memory 1 */
172   else if((hdma->Instance->CR & DMA_SxCR_CT) != 0)
173   {
174     hdma->State = HAL_DMA_STATE_BUSY_MEM1;
175   }
176
177   /* Check the parameters */
178   assert_param(IS_DMA_BUFFER_SIZE(DataLength));
179
180   /* Disable the peripheral */
181   __HAL_DMA_DISABLE(hdma);  
182
183   /* Enable the Double buffer mode */
184   hdma->Instance->CR |= (uint32_t)DMA_SxCR_DBM;
185
186   /* Configure DMA Stream destination address */
187   hdma->Instance->M1AR = SecondMemAddress;
188
189   /* Configure the source, destination address and the data length */
190   DMA_MultiBufferSetConfig(hdma, SrcAddress, DstAddress, DataLength); 
191
192   /* Enable the transfer complete interrupt */
193   __HAL_DMA_ENABLE_IT(hdma, DMA_IT_TC);
194
195   /* Enable the Half transfer interrupt */
196   __HAL_DMA_ENABLE_IT(hdma, DMA_IT_HT);
197
198   /* Enable the transfer Error interrupt */
199   __HAL_DMA_ENABLE_IT(hdma, DMA_IT_TE);
200
201   /* Enable the fifo Error interrupt */
202   __HAL_DMA_ENABLE_IT(hdma, DMA_IT_FE);  
203
204   /* Enable the direct mode Error interrupt */
205   __HAL_DMA_ENABLE_IT(hdma, DMA_IT_DME); 
206
207   /* Enable the peripheral */
208   __HAL_DMA_ENABLE(hdma); 
209
210   return HAL_OK; 
211 }
212
213 /**
214   * @brief  Change the memory0 or memory1 address on the fly.
215   * @param  hdma:       pointer to a DMA_HandleTypeDef structure that contains
216   *                     the configuration information for the specified DMA Stream.  
217   * @param  Address:    The new address
218   * @param  memory:     the memory to be changed, This parameter can be one of 
219   *                     the following values:
220   *                      MEMORY0 /
221   *                      MEMORY1
222   * @note   The MEMORY0 address can be changed only when the current transfer use
223   *         MEMORY1 and the MEMORY1 address can be changed only when the current 
224   *         transfer use MEMORY0.
225   * @retval HAL status
226   */
227 HAL_StatusTypeDef HAL_DMAEx_ChangeMemory(DMA_HandleTypeDef *hdma, uint32_t Address, HAL_DMA_MemoryTypeDef memory)
228 {
229   if(memory == MEMORY0)
230   {
231     /* change the memory0 address */
232     hdma->Instance->M0AR = Address;
233   }
234   else
235   {
236     /* change the memory1 address */
237     hdma->Instance->M1AR = Address;
238   }
239
240   return HAL_OK;
241 }
242
243 /**
244   * @}
245   */
246
247 /**
248   * @brief  Set the DMA Transfer parameter.
249   * @param  hdma:       pointer to a DMA_HandleTypeDef structure that contains
250   *                     the configuration information for the specified DMA Stream.  
251   * @param  SrcAddress: The source memory Buffer address
252   * @param  DstAddress: The destination memory Buffer address
253   * @param  DataLength: The length of data to be transferred from source to destination
254   * @retval HAL status
255   */
256 static void DMA_MultiBufferSetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
257 {  
258   /* Configure DMA Stream data length */
259   hdma->Instance->NDTR = DataLength;
260   
261   /* Peripheral to Memory */
262   if((hdma->Init.Direction) == DMA_MEMORY_TO_PERIPH)
263   {   
264     /* Configure DMA Stream destination address */
265     hdma->Instance->PAR = DstAddress;
266     
267     /* Configure DMA Stream source address */
268     hdma->Instance->M0AR = SrcAddress;
269   }
270   /* Memory to Peripheral */
271   else
272   {
273     /* Configure DMA Stream source address */
274     hdma->Instance->PAR = SrcAddress;
275     
276     /* Configure DMA Stream destination address */
277     hdma->Instance->M0AR = DstAddress;
278   }
279 }
280
281 /**
282   * @}
283   */
284
285 #endif /* HAL_DMA_MODULE_ENABLED */
286 /**
287   * @}
288   */
289
290 /**
291   * @}
292   */
293
294 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/