summaryrefslogtreecommitdiff
path: root/bsp/acm32/acm32f0x0-nucleo/libraries/HAL_Driver/Src/HAL_DMA.c
blob: e48be23216808aa05988101f6758e9bbc9c6cb8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
  ******************************************************************************
  * @file    HAL_DMA.c
  * @version V1.0.0
  * @date    2020
  * @brief   DMA HAL module driver.
  *          This file provides firmware functions to manage the following
  *          functionalities of the Direct Memory Access (DMA) peripheral:
  *           @ Initialization and de-initialization functions
  *           @ IO operation functions
  ******************************************************************************
*/
#include "ACM32Fxx_HAL.h"

/**************** Used in cycle mode ****************/
static DMA_LLI_InitTypeDef Cycle_Channel[DMA_CHANNEL_NUM];

/*********************************************************************************
* Function    : HAL_DMA_IRQHandler
* Description : This function handles DMA interrupt request.
* Input       : hdma : pointer to a DMA_HandleTypeDef structure that contains
*                      the configuration information for DMA module
* Output      : 
* Author      : Chris_Kyle                         Data : 2020
**********************************************************************************/
__weak void HAL_DMA_IRQHandler(DMA_HandleTypeDef *hdma)
{
    uint32_t lu32_Channel_Index;
    
    /* Get DMA Channel number */
    lu32_Channel_Index = ((uint32_t)(hdma->Instance) - (uint32_t)(DMA_Channel0)) / 0x20;

    /* Channel has been interrupted */
    if (DMA->INT_STATUS & (1 << lu32_Channel_Index))
    {
        /* Transfer complete interrupt */
        if (DMA->INT_TC_STATUS & (1 << lu32_Channel_Index))
        {
            DMA->INT_TC_CLR |= (1 << lu32_Channel_Index);

            if (NULL != hdma->DMA_ITC_Callback)
            {
                hdma->DMA_ITC_Callback();
            }
        }
        
        /* Transfer error interrupt */
        if (DMA->INT_ERR_STATUS & (1 << lu32_Channel_Index))
        {
            DMA->INT_ERR_CLR |= (1 << lu32_Channel_Index);

            if (NULL != hdma->DMA_IE_Callback)
            {
                hdma->DMA_IE_Callback();
            }
        }
    }
}

/*********************************************************************************
* Function    : HAL_DMA_Init
* Description : DMA initial with parameters. 
* Input       : hdma : pointer to a DMA_HandleTypeDef structure that contains
*                      the configuration information for DMA module
* Output      : 
* Author      : Chris_Kyle                         Data : 2020
**********************************************************************************/
HAL_StatusTypeDef HAL_DMA_Init(DMA_HandleTypeDef *hdma) 
{
#if (USE_FULL_ASSERT == 1)
    /* Check DMA Parameter */
    if (!IS_DMA_ALL_INSTANCE(hdma->Instance))                 return HAL_ERROR;
    if (!IS_DMA_DATA_FLOW(hdma->Init.Data_Flow))              return HAL_ERROR;
    if (!IS_DMA_REQUEST_ID(hdma->Init.Request_ID))            return HAL_ERROR;
    if (!IS_DMA_SRC_WIDTH(hdma->Init.Source_Width))           return HAL_ERROR;
    if (!IS_DMA_DST_WIDTH(hdma->Init.Desination_Width))       return HAL_ERROR;
#endif

    /* Enable DMA Module */
    System_Module_Enable(EN_DMA);
    
    /* Enable External Interrupt */
    NVIC_ClearPendingIRQ(DMA_IRQn);
    NVIC_EnableIRQ(DMA_IRQn);

    /* Default Little-Endian、Enable DMA */
    DMA->CONFIG = DMA_CONFIG_EN;

    /* Clear Channel Config */
    hdma->Instance->CONFIG = 0x00000000; 
    
    if (hdma->Init.Data_Flow == DMA_DATA_FLOW_M2P) 
    {
        hdma->Init.Request_ID <<= DMA_CHANNEL_CONFIG_DEST_PERIPH_POS;
    }
    else if (hdma->Init.Data_Flow == DMA_DATA_FLOW_P2M) 
    {
        hdma->Init.Request_ID <<= DMA_CHANNEL_CONFIG_SRC_PERIPH_POS;
    }

    hdma->Instance->CONFIG = hdma->Init.Data_Flow | hdma->Init.Request_ID;
    
    /* Config Channel Control */

    hdma->Instance->CTRL = DMA_CHANNEL_CTRL_ITC;

    /* Source or Desination address increase */
    hdma->Instance->CTRL |= (hdma->Init.Desination_Inc | hdma->Init.Source_Inc);
    /* Source or Desination date width */
    hdma->Instance->CTRL |= (hdma->Init.Desination_Width | hdma->Init.Source_Width);

    return HAL_OK;
}

/*********************************************************************************
* Function    : HAL_DMA_DeInit
* Description : DMA De-initial with parameters. 
* Input       : hdma : pointer to a DMA_HandleTypeDef structure that contains
*                      the configuration information for DMA module
* Output      : 
* Author      : Chris_Kyle                         Data : 2020
**********************************************************************************/
HAL_StatusTypeDef HAL_DMA_DeInit(DMA_HandleTypeDef *hdma) 
{
#if (USE_FULL_ASSERT == 1)
    /* Check DMA Parameter */
    if (!IS_DMA_ALL_INSTANCE(hdma->Instance))                 return HAL_ERROR;
    if (!IS_DMA_DATA_FLOW(hdma->Init.Data_Flow))              return HAL_ERROR;
    if (!IS_DMA_REQUEST_ID(hdma->Init.Request_ID))            return HAL_ERROR;
    if (!IS_DMA_SRC_WIDTH(hdma->Init.Source_Width))           return HAL_ERROR;
    if (!IS_DMA_DST_WIDTH(hdma->Init.Desination_Width))       return HAL_ERROR;
#endif

    /* Reset DMA Module */
    System_Module_Reset(RST_DMA);
    
    /* Disable DMA Module */
    System_Module_Disable(EN_DMA);
    
    /* Disable Interrupt */
    NVIC_ClearPendingIRQ(DMA_IRQn);
    NVIC_DisableIRQ(DMA_IRQn);
    
    hdma->DMA_IE_Callback = NULL;
    hdma->DMA_ITC_Callback = NULL;

    memset(&hdma->Init, 0, sizeof(hdma->Init));
    
    return HAL_OK;
}

/*********************************************************************************
* Function    : HAL_DMA_NormalMode_Start
* Description : DMA transfer start. 
* Input       : hdma : pointer to a DMA_HandleTypeDef structure that contains
*                      the configuration information for DMA module
* Input       : fu32_SrcAddr: source address
* Input       : fu32_DstAddr: desination address
* Input       : fu32_Size:    transfer size (This parameter can be a 12-bit Size)
* Output      : 
* Author      : Chris_Kyle                         Data : 2020
**********************************************************************************/
HAL_StatusTypeDef HAL_DMA_NormalMode_Start(DMA_HandleTypeDef *hdma, uint32_t fu32_SrcAddr, uint32_t fu32_DstAddr, uint32_t fu32_Size)
{
#if (USE_FULL_ASSERT == 1)
    /* Check DMA Parameter */
    if (!IS_DMA_ALL_INSTANCE(hdma->Instance))    return HAL_ERROR;
#endif
    
    if (fu32_Size > 0xFFF) 
    {
        return HAL_ERROR;
    }

    /* Set source address and desination address */
    hdma->Instance->SRC_ADDR  = fu32_SrcAddr;
    hdma->Instance->DEST_ADDR = fu32_DstAddr;

    /* Set Transfer Size */
    hdma->Instance->CTRL = (hdma->Instance->CTRL & (~0xFFF)) | fu32_Size;

    /* DMA Channel Enable */
    hdma->Instance->CONFIG |= DMA_CHANNEL_CONFIG_EN;

    return HAL_OK;
}

/*********************************************************************************
* Function    : HAL_DMA_NormalMode_Start_IT
* Description : DMA transfer start with interrupt. 
* Input       : hdma : pointer to a DMA_HandleTypeDef structure that contains
*                      the configuration information for DMA module
* Input       : fu32_SrcAddr: source address
* Input       : fu32_DstAddr: desination address
* Input       : fu32_Size:    transfer size (This parameter can be a 12-bit Size)
* Output      : 
* Author      : Chris_Kyle                         Data : 2020
**********************************************************************************/
HAL_StatusTypeDef HAL_DMA_NormalMode_Start_IT(DMA_HandleTypeDef *hdma, uint32_t fu32_SrcAddr, uint32_t fu32_DstAddr, uint32_t fu32_Size)
{
#if (USE_FULL_ASSERT == 1)
    /* Check DMA Parameter */
    if (!IS_DMA_ALL_INSTANCE(hdma->Instance))    return HAL_ERROR;
#endif

    /* Set source address and desination address */
    hdma->Instance->SRC_ADDR  = fu32_SrcAddr;
    hdma->Instance->DEST_ADDR = fu32_DstAddr;

    /* Set Transfer Size and enable LLI interrupt */
    hdma->Instance->CTRL = (hdma->Instance->CTRL & (~0xFFF)) | fu32_Size;
//        hdma->Instance->CTRL &=~(1<<31);
    /* DMA Channel Enable and enable transfer error interrupt and transfer complete interrupt*/
    hdma->Instance->CONFIG |= DMA_CHANNEL_CONFIG_ITC | DMA_CHANNEL_CONFIG_IE | DMA_CHANNEL_CONFIG_EN;

    return HAL_OK;
}

/*********************************************************************************
* Function    : HAL_DMA_CycleMode_Start
* Description : DMA Cycle transfer start. 
* Input       : hdma : pointer to a DMA_HandleTypeDef structure that contains
*                      the configuration information for DMA module
* Input       : fu32_SrcAddr: source address
* Input       : fu32_DstAddr: desination address
* Input       : fu32_Size:    transfer size (This parameter can be a 12-bit Size)
* Output      : 
* Author      : Chris_Kyle                         Data : 2020
**********************************************************************************/
HAL_StatusTypeDef HAL_DMA_CycleMode_Start(DMA_HandleTypeDef *hdma, uint32_t fu32_SrcAddr, uint32_t fu32_DstAddr, uint32_t fu32_Size)
{
    uint32_t lu32_Channel_Index;

#if (USE_FULL_ASSERT == 1)
    /* Check DMA Parameter */
    if (!IS_DMA_ALL_INSTANCE(hdma->Instance))    return HAL_ERROR;
#endif

    /* Get DMA Channel number */
    lu32_Channel_Index = ((uint32_t)(hdma->Instance) - (uint32_t)(DMA_Channel0)) / 0x20;
    
    /* Set source address and desination address */
    hdma->Instance->SRC_ADDR  = fu32_SrcAddr;
    hdma->Instance->DEST_ADDR = fu32_DstAddr;

    /* Set Next Link */
    hdma->Instance->LLI = (uint32_t)&Cycle_Channel[lu32_Channel_Index];

    /* Set Transfer Size */
    hdma->Instance->CTRL = (hdma->Instance->CTRL & (~0xFFF)) | fu32_Size;

    /* The list point to oneself */
    Cycle_Channel[lu32_Channel_Index].SrcAddr = fu32_SrcAddr;
    Cycle_Channel[lu32_Channel_Index].DstAddr = fu32_DstAddr;
    Cycle_Channel[lu32_Channel_Index].Next    = &Cycle_Channel[lu32_Channel_Index];
    Cycle_Channel[lu32_Channel_Index].Control = (hdma->Instance->CTRL & (~0xFFF)) | fu32_Size;

    /* DMA Channel Enable */
    hdma->Instance->CONFIG |= DMA_CHANNEL_CONFIG_EN;

    return HAL_OK;
}

/*********************************************************************************
* Function    : HAL_DMA_CycleMode_Start_IT
* Description : DMA Cycle transfer start with interrupt. 
* Input       : hdma : pointer to a DMA_HandleTypeDef structure that contains
*                      the configuration information for DMA module
* Input       : fu32_SrcAddr: source address
* Input       : fu32_DstAddr: desination address
* Input       : fu32_Size:    transfer size (This parameter can be a 12-bit Size)
* Output      : 
* Author      : Chris_Kyle                         Data : 2020
**********************************************************************************/
HAL_StatusTypeDef HAL_DMA_CycleMode_Start_IT(DMA_HandleTypeDef *hdma, uint32_t fu32_SrcAddr, uint32_t fu32_DstAddr, uint32_t fu32_Size)
{
    uint32_t lu32_Channel_Index;

#if (USE_FULL_ASSERT == 1)
    /* Check DMA Parameter */
    if (!IS_DMA_ALL_INSTANCE(hdma->Instance))    return HAL_ERROR;
#endif

    /* Get DMA Channel number */
    lu32_Channel_Index = ((uint32_t)(hdma->Instance) - (uint32_t)(DMA_Channel0)) / 0x20;
    
    /* Set source address and desination address */
    hdma->Instance->SRC_ADDR  = fu32_SrcAddr;
    hdma->Instance->DEST_ADDR = fu32_DstAddr;

    /* Set Next Link */
    hdma->Instance->LLI = (uint32_t)&Cycle_Channel[lu32_Channel_Index];

    /* Set Transfer Size */
    hdma->Instance->CTRL = (hdma->Instance->CTRL & (~0xFFF)) | fu32_Size;

    /* The list point to oneself */
    Cycle_Channel[lu32_Channel_Index].SrcAddr = fu32_SrcAddr;
    Cycle_Channel[lu32_Channel_Index].DstAddr = fu32_DstAddr;
    Cycle_Channel[lu32_Channel_Index].Next    = &Cycle_Channel[lu32_Channel_Index];
    Cycle_Channel[lu32_Channel_Index].Control = (hdma->Instance->CTRL & (~0xFFF)) | fu32_Size;

    /* DMA Channel Enable and enable transfer error interrupt and transfer complete interrupt*/
    hdma->Instance->CONFIG |= DMA_CHANNEL_CONFIG_ITC | DMA_CHANNEL_CONFIG_IE | DMA_CHANNEL_CONFIG_EN;

    return HAL_OK;
}

/*********************************************************************************
* Function    : HAL_DMA_Start
* Description : DMA transfer start. 
* Input       : hdma : pointer to a DMA_HandleTypeDef structure that contains
*                      the configuration information for DMA module
* Input       : fu32_SrcAddr: source address
* Input       : fu32_DstAddr: desination address
* Input       : fu32_Size:    transfer size (This parameter can be a 12-bit Size)
* Output      : 
* Author      : Chris_Kyle                         Data : 2020
**********************************************************************************/
HAL_StatusTypeDef HAL_DMA_Start(DMA_HandleTypeDef *hdma, uint32_t fu32_SrcAddr, uint32_t fu32_DstAddr, uint32_t fu32_Size)
{
    /* Check DMA Parameter */
    if (!IS_DMA_MODE(hdma->Init.Mode))    return HAL_ERROR;
        
    if (hdma->Init.Mode == DMA_NORMAL)
    {
        return HAL_DMA_NormalMode_Start(hdma, fu32_SrcAddr, fu32_DstAddr, fu32_Size);
    }
    else
    {
        return HAL_DMA_CycleMode_Start(hdma, fu32_SrcAddr, fu32_DstAddr, fu32_Size);
    }
}

/*********************************************************************************
* Function    : HAL_DMA_Start_IT
* Description : DMA transfer start with interrupt. 
* Input       : hdma : pointer to a DMA_HandleTypeDef structure that contains
*                      the configuration information for DMA module
* Input       : fu32_SrcAddr: source address
* Input       : fu32_DstAddr: desination address
* Input       : fu32_Size:    transfer size (This parameter can be a 12-bit Size)
* Output      : 
* Author      : Chris_Kyle                         Data : 2020
**********************************************************************************/
HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t fu32_SrcAddr, uint32_t fu32_DstAddr, uint32_t fu32_Size)
{
    /* Check DMA Parameter */
    if (!IS_DMA_MODE(hdma->Init.Mode))    return HAL_ERROR;

    if (hdma->Init.Mode == DMA_NORMAL)
    {
        return HAL_DMA_NormalMode_Start_IT(hdma, fu32_SrcAddr, fu32_DstAddr, fu32_Size);
    }
    else
    {
        return HAL_DMA_CycleMode_Start_IT(hdma, fu32_SrcAddr, fu32_DstAddr, fu32_Size);
    }
}

/*********************************************************************************
* Function    : HAL_DMA_Abort
* Description : Abort the DMA Transfer 
* Input       : hdma : pointer to a DMA_HandleTypeDef structure that contains
*                      the configuration information for DMA module
* Output      : 
* Author      : Chris_Kyle                         Data : 2020
**********************************************************************************/
HAL_StatusTypeDef HAL_DMA_Abort(DMA_HandleTypeDef *hdma)
{
    uint32_t lu32_Channel_Index;

#if (USE_FULL_ASSERT == 1)
    /* Check DMA Parameter */
    if (!IS_DMA_ALL_INSTANCE(hdma->Instance))    return HAL_ERROR;
#endif

    /* Get DMA Channel number */
    lu32_Channel_Index = ((uint32_t)(hdma->Instance) - (uint32_t)(DMA_Channel0)) / 0x20;

    /* DMA Channel Disable */
    hdma->Instance->CONFIG &= ~(1 << 0);

    /* Clear TC ERR Falg */
    DMA->INT_TC_CLR  |= (1 << lu32_Channel_Index);
    DMA->INT_ERR_CLR |= (1 << lu32_Channel_Index);

    return HAL_OK;
}

/*********************************************************************************
* Function    : HAL_DMA_GetState
* Description : Returns the DMA state..
* Input       : hdma : pointer to a DMA_HandleTypeDef structure that contains
*                      the configuration information for DMA module
* Output      : 
* Author      :                          Data : 2021
**********************************************************************************/
HAL_StatusTypeDef HAL_DMA_GetState(DMA_HandleTypeDef *hdma)
{
    uint32_t lu32_Channel_Index;
    HAL_StatusTypeDef States = HAL_ERROR;
    
    /* Get DMA Channel number */
    lu32_Channel_Index = ((uint32_t)(hdma->Instance) - (uint32_t)(DMA_Channel0)) / 0x20;

    /* Transfer complete interrupt */
    if (DMA->RAW_INT_TC_STATUS & (1 << lu32_Channel_Index))
    {
        DMA->INT_TC_CLR |= (1 << lu32_Channel_Index);

        States = HAL_OK;
    }
    
    /* Transfer error interrupt */
    if (DMA->RAW_INT_ERR_STATUS & (1 << lu32_Channel_Index))
    {
        DMA->INT_ERR_CLR |= (1 << lu32_Channel_Index);

        States = HAL_ERROR;
    }
    
    return States;
}