summaryrefslogtreecommitdiff
path: root/bsp/essemi/es32vf2264/libraries/ALD/ES32VF2264/Source/ald_dma.c
blob: 508428a4fa2e187140e3d6d7266ec699fb545d83 (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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/**
  *********************************************************************************
  *
  * @file    ald_dma.c
  * @brief   DMA module driver.
  *
  * @version V1.0
  * @date    17 Feb. 2023
  * @author  AE Team
  * @note
  *          Change Logs:
  *          Date            Author          Notes
  *          17 Feb. 2023    Lisq            The first version
  *
  * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
  *
  * SPDX-License-Identifier: Apache-2.0
  *
  * Licensed under the Apache License, Version 2.0 (the License); you may
  * not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  * www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  **********************************************************************************
  */

#include <string.h>
#include "ald_dma.h"

/** @addtogroup ES32VF2264_ALD
  * @{
  */

/** @defgroup DMA DMA
  * @brief DMA module driver
  * @{
  */

/** @defgroup DMA_Private_Variables DMA Private Variables
  * @{
  */
ald_dma_call_back_t dma_cbk[7];
/**
  * @}
  */

/** @defgroup DMA_Private_Functions DMA Private Functions
  * @{
  */

/**
  * @brief  Configure DMA channel using dma_config_t structure
  * @param  None
  * @param  config: Pointer to dma_config_t which contains
  *            DMA channel parameter. see @ref dma_config_t
  * @retval None
  */
void ald_dma_config_base(ald_dma_config_t *config)
{
    assert_param(config->src != NULL);
    assert_param(config->dst != NULL);
    assert_param(IS_DMA_DATA_SIZE(config->size));
    assert_param(IS_DMA_DATASIZE_TYPE(config->src_data_width));
    assert_param(IS_DMA_DATASIZE_TYPE(config->dst_data_width));
    assert_param(IS_DMA_DATAINC_TYPE(config->src_inc));
    assert_param(IS_DMA_DATAINC_TYPE(config->dst_inc));
    assert_param(IS_DMA_ARBITERCONFIG_TYPE(config->R_power));
    assert_param(IS_DMA_PRIORITY_TYPE(config->priority));
    assert_param(IS_DMA_MSEL_TYPE(config->msel));
    assert_param(IS_DMA_MSIGSEL_TYPE(config->msigsel));
    assert_param(IS_DMA_CHANNEL(config->channel));

    WRITE_REG(DMA->CHANNEL[config->channel].SAR, (uint32_t)config->src);
    MODIFY_REG(DMA->CHANNEL[config->channel].CON, DMA_CON_SDWSEL_MSK, config->src_data_width << DMA_CON_SDWSEL_POSS);
    MODIFY_REG(DMA->CHANNEL[config->channel].CON, DMA_CON_SINC_MSK, config->src_inc << DMA_CON_SINC_POS);
    WRITE_REG(DMA->CHANNEL[config->channel].DAR, (uint32_t)config->dst);
    MODIFY_REG(DMA->CHANNEL[config->channel].CON, DMA_CON_DDWSEL_MSK, config->dst_data_width << DMA_CON_DDWSEL_POSS);
    MODIFY_REG(DMA->CHANNEL[config->channel].CON, DMA_CON_DINC_MSK, config->dst_inc << DMA_CON_DINC_POS);
    MODIFY_REG(DMA->CHANNEL[config->channel].CON, DMA_CON_CIRC_MSK, config->circle_mode << DMA_CON_CIRC_POS);
    MODIFY_REG(DMA->CHANNEL[config->channel].CON, DMA_CON_M2M_MSK, config->mem_to_mem << DMA_CON_M2M_POS);
    MODIFY_REG(DMA->CHANNEL[config->channel].CON, DMA_CON_CHPRI_MSK, config->priority << DMA_CON_CHPRI_POSS);
    MODIFY_REG(DMA->CHANNEL[config->channel].CON, DMA_CON_MAX_BURST_MSK, config->R_power << DMA_CON_MAX_BURST_POSS);
    MODIFY_REG(DMA->CHANNEL[config->channel].NDT, DMA_NDT_TNDT_MSK, config->size << DMA_NDT_TNDT_POSS);
    MODIFY_REG(DMA_MUX->CH_SELCON[config->channel], DMA_SELCON_MSEL_MSK, config->msel << DMA_SELCON_MSEL_POSS);
    MODIFY_REG(DMA_MUX->CH_SELCON[config->channel], DMA_SELCON_MSIGSEL_MSK, config->msigsel << DMA_SELCON_MSIGSEL_POSS);

    return;
}

/**
  * @brief  Configure DMA channel according to the specified parameter
  *         in the dma_handle_t structure. The DMA mode is basic.
  *         This mode is used to carry data from peripheral to memory
  *         or from memory to peripheral.
  * @param  hperh: Pointer to dma_handle_t structure that contains
  *         configuration information for specified DMA channel.
  * @retval None
  */
void ald_dma_config_basic(ald_dma_handle_t *hperh)
{
    dma_cbk[hperh->config.channel].cplt_tc_cbk = hperh->cplt_tc_cbk;
    dma_cbk[hperh->config.channel].cplt_ht_cbk  = hperh->cplt_ht_cbk;
    dma_cbk[hperh->config.channel].cplt_tc_arg = hperh->cplt_tc_arg;
    dma_cbk[hperh->config.channel].cplt_ht_arg  = hperh->cplt_ht_arg;

    ald_dma_clear_flag_status(hperh->config.channel, ALD_DMA_IT_FLAG_TC);
    ald_dma_clear_flag_status(hperh->config.channel, ALD_DMA_IT_FLAG_HT);
    ald_dma_config_base(&hperh->config);
    ald_dma_channel_config(hperh->config.channel, ENABLE);

    return;
}

/**
  * @brief  Configure DMA channel according to the specified parameter.
  *         The DMA mode is basic. This mode is used to carry data
  *         from peripheral to memory or negative direction. If user want
  *         use the dma easily, they can invoke this function.
  * @param  DMAx: Pointer to DMA peripheral
  * @param  src: Source data begin pointer
  * @param  dst: Destination data begin pointer
  * @param  size: The total number of DMA transfers that DMA cycle contains
  * @param  msel: Input source to DMA channel @ref dma_msel_t
  * @param  msigsel: Input signal to DMA channel @ref dma_msigsel_t
  * @param  channel: Channel index which will be used
  * @param  cbk: DMA complete callback function
  *
  * @retval None
  *
 */
void ald_dma_config_basic_easy(void *src, void *dst, uint16_t size, ald_dma_msel_t msel,
        ald_dma_msigsel_t msigsel, uint8_t channel, void (*cbk)(void *arg))
{
    ald_dma_handle_t hperh;

    ald_dma_config_struct(&hperh.config);

    if (((uint32_t)src) >= 0x40000000)
        hperh.config.src_inc = ALD_DMA_DATA_INC_DISABLE;

    if (((uint32_t)dst) >= 0x40000000)
        hperh.config.dst_inc = ALD_DMA_DATA_INC_DISABLE;

    hperh.config.src     = src;
    hperh.config.dst     = dst;
    hperh.config.size    = size;
    hperh.config.msel    = msel;
    hperh.config.msigsel = msigsel;
    hperh.config.channel = channel;

    hperh.perh        = DMA;
    hperh.cplt_tc_cbk = cbk;
    hperh.cplt_tc_arg = NULL;
    hperh.cplt_ht_cbk = NULL;

    ald_dma_clear_flag_status(channel, ALD_DMA_IT_FLAG_TC);
    ald_dma_clear_flag_status(channel, ALD_DMA_IT_FLAG_HT);
    ald_dma_config_basic(&hperh);

    return;
}

/**
  * @brief  Handle DMA interrupt
  * @retval None
  */
void ald_dma_irq_handler(void)
{
    uint8_t i;

    for (i = 0; i < ALD_DMA_CH_COUNT; ++i)
    {
        if (READ_BIT(DMA->IFM, 1U << (2U * i)))
        {
            if (dma_cbk[i].cplt_tc_cbk != NULL)
                dma_cbk[i].cplt_tc_cbk(dma_cbk[i].cplt_tc_arg);

            DMA->ICR = 1U << (2U * i);
        }

        if (READ_BIT(DMA->IFM, 1U << (2U * i + 1U)))
        {
            if (dma_cbk[i].cplt_ht_cbk != NULL)
                dma_cbk[i].cplt_ht_cbk(dma_cbk[i].cplt_ht_arg);

            DMA->ICR = 1U << (2U * i + 1U);
        }
    }

    return;
}
/**
  * @}
  */

/** @defgroup DMA_Public_Functions DMA Public Functions
  * @{
  */

/** @defgroup DMA_Public_Functions_Group1 Initialization functions
  * @brief Initialization functions
  *
  * @{
  */

/**
  * @brief  Reset the DMA register
  * @param  None
  * @retval None
  */
void ald_dma_reset(void)
{
    uint32_t i;

    WRITE_REG(DMA->IDR, 0x3FFF);
    WRITE_REG(DMA->ICR, 0x3FFF);

    for (i = 0; i < DMA_CHANNELS; ++i)
    {
        CLEAR_BIT(DMA->CHANNEL[i].CON, DMA_CON_CHEN_MSK);
        WRITE_REG(DMA->CHANNEL[i].CON, 0x0);
        WRITE_REG(DMA->CHANNEL[i].SAR, 0x0);
        WRITE_REG(DMA->CHANNEL[i].DAR, 0x0);
        WRITE_REG(DMA->CHANNEL[i].NDT, 0x0);
        WRITE_REG(DMA_MUX->CH_SELCON[i], 0x0);
    }

    return;
}

/**
  * @brief  DMA module initialization, this function
  *         is invoked by ald_cmu_init().
  * @param  None
  * @retval None
  */
void ald_dma_init(void)
{
    memset(dma_cbk, 0x0, sizeof(dma_cbk));

    ald_dma_reset();
    ald_mcu_irq_config(DMA_IRQn, 4, ENABLE);

    return;
}

/**
  * @brief  Configure dma_config_t structure using default parameter.
  *         User can invoked this function, before configure dma_config_t
  * @param  config: Pointer to dma_config_t structure, see @ref dma_config_t
  * @retval None
  */
void ald_dma_config_struct(ald_dma_config_t *config)
{
    config->size           = 0;
    config->src_data_width = ALD_DMA_DATA_SIZE_BYTE;
    config->dst_data_width = ALD_DMA_DATA_SIZE_BYTE;
    config->src_inc        = ALD_DMA_DATA_INC_DISABLE;
    config->dst_inc        = ALD_DMA_DATA_INC_DISABLE;
    config->R_power        = ALD_DMA_R_POWER_1;
    config->priority       = ALD_DMA_LOW_PRIORITY;
    config->mem_to_mem     = DISABLE;
    config->circle_mode    = DISABLE;
    config->msel           = ALD_DMA_MSEL_NONE;
    config->msigsel        = ALD_DMA_MSIGSEL_NONE;
    config->channel        = ALD_DMA_CH_0;

    return;
}

/**
  * @}
  */

/**
  * @}
  */

/** @defgroup DMA_Public_Functions_Group2 DMA Control functions
  * @brief DMA control functions
  *
  * @verbatim
    ===================================================================

                    #### DMA control functions ####

    ===================================================================
    [..]
    This subsection provides some functions allowing to control DMA:
    (+) ald_dma_channel_config(): Control DMA channel ENABLE/DISABLE.
    (+) ald_dma_interrupt_config(): Control DMA channel interrupt ENABLE or
        DISABLE.
    (+) ald_dma_get_it_status(): Check whether the specified channel
        interrupt is SET or RESET.
    (+) ald_dma_get_flag_status(): Check whether the specified channel
        flag is SET or RESET.
    (+) ald_dma_clear_flag_status(): Clear the specified channel
        pending flag

    @endverbatim
  * @{
  */

/**
  * @brief  Configure channel enable or disable. It will unbind descriptor with
  *         channel, when channel has been disable.
  * @param  None
  * @param  channel: channel index
  * @param  state: status of channel:
  *           @arg ENABLE: Enable the channel
  *           @arg DISABLE: Disable the channel
  * @retval None
  */
void ald_dma_channel_config(uint8_t channel, type_func_t state)
{
    assert_param(IS_DMA_CHANNEL(channel));
    assert_param(IS_FUNC_STATE(state));

    if (state)
    {
        SET_BIT(DMA->CHANNEL[channel].CON, DMA_CON_CHEN_MSK);
    }
    else
    {
        CLEAR_BIT(DMA->CHANNEL[channel].CON, DMA_CON_CHEN_MSK);
        WRITE_REG(DMA->CHANNEL[channel].CON, 0x0);
        WRITE_REG(DMA->CHANNEL[channel].SAR, 0x0);
        WRITE_REG(DMA->CHANNEL[channel].DAR, 0x0);
        WRITE_REG(DMA->CHANNEL[channel].NDT, 0x0);
        WRITE_REG(DMA_MUX->CH_SELCON[channel], 0x0);
    }

    return;
}

/**
  * @brief  Configure the interrupt enable or disable
  * @param  channel: Channel index.
  *           @arg 0~6: Channel index
  * @param  it: IT type.
  *           @arg DMA_IT_FLAG_TC
  *           @arg DMA_IT_FLAG_HT
  * @param  state: status of channel:
  *           @arg ENABLE: Enable the channel
  *           @arg DISABLE: Disable the channel
  *
  * @retval None
  */
void ald_dma_interrupt_config(uint8_t channel, ald_dma_it_flag_t it, type_func_t state)
{
    assert_param(IS_DMA_CHANNEL(channel));
    assert_param(IS_DMA_IT_TYPE(it));
    assert_param(IS_FUNC_STATE(state));

    if (state)
        SET_BIT(DMA->IER, 1U << (channel * 2U + it));
    else
        SET_BIT(DMA->IDR, 1U << (channel * 2U + it));

    return;
}

/**
  * @brief  Check whether the specified channel interrupt
  *         is set or reset
  * @param  it: IT type.
  *           @arg DMA_IT_FLAG_TC
  *           @arg DMA_IT_FLAG_HT
  * @param  channel: Channel index
  *           @arg 0~6: Channel index
  * @retval Status:
  *           - SET: Channel interrupt is set
  *           - RESET: Channel interrupt is reset
  */
it_status_t ald_dma_get_it_status(uint8_t channel, ald_dma_it_flag_t it)
{
    assert_param(IS_DMA_CHANNEL(channel));
    assert_param(IS_DMA_IT_TYPE(it));

    if (READ_BIT(DMA->IVS, 1 << (channel * 2U + it)))
        return SET;

    return RESET;
}

/**
  * @brief  Check whether the specified channel flag
  *         is set or reset
  * @param  channel: Channel index
  *           @arg 0~6: Channel index
  * @param  it: IT type.
  *           @arg DMA_IT_FLAG_TC
  *           @arg DMA_IT_FLAG_HT
  * @retval Status:
  *           - SET: Channel flag is set
  *           - RESET: Channel flag is reset
  */
flag_status_t ald_dma_get_flag_status(uint8_t channel, ald_dma_it_flag_t it)
{
    assert_param(IS_DMA_CHANNEL(channel));
    assert_param(IS_DMA_IT_TYPE(it));

    if (READ_BIT(DMA->IFM, 1 << (channel * 2U + it)))
        return SET;

    return RESET;
}

/**
  * @brief  Clear the specified channel pending flag
  * @param  channel: Channel index
  *           @arg 0~6: Channel index
  * @param  it: IT type.
  *           @arg DMA_IT_FLAG_TC
  *           @arg DMA_IT_FLAG_HT
  * @retval None
  */
void ald_dma_clear_flag_status(uint8_t channel, ald_dma_it_flag_t it)
{
    assert_param(IS_DMA_CHANNEL(channel));
    assert_param(IS_DMA_IT_TYPE(it));

    SET_BIT(DMA->ICR, 1U << (channel * 2U + it));
    return;
}

/**
  * @}
  */

/**
  * @}
  */
/**
  * @}
  */

/**
  * @}
  */