• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15  * Description: DMA Sample Source. \n
16  *
17  * History: \n
18  * 2023-07-06, Create file. \n
19  */
20 #include "hal_dma.h"
21 #include "soc_osal.h"
22 #include "securec.h"
23 #include "app_init.h"
24 
25 #define DMA_TRANSFER_WORD_NUM       32
26 #define DMA_TRANSFER_PRIORITY       0
27 #define DMA_TRANSFER_WIDTH          2
28 #define DMA_TASK_DURATION_MS        500
29 
30 #define DMA_TASK_PRIO               24
31 #define DMA_TASK_STACK_SIZE         0x1000
32 
33 static uint32_t g_app_dma_src_data[DMA_TRANSFER_WORD_NUM] = { 0 };
34 static uint32_t g_app_dma_desc_data[DMA_TRANSFER_WORD_NUM] = { 0 };
35 static uint8_t g_dma_trans_done = 0;
36 
app_dma_trans_done_callback(uint8_t int_type,uint8_t channel,uintptr_t arg)37 static void app_dma_trans_done_callback(uint8_t int_type, uint8_t channel, uintptr_t arg)
38 {
39     unused(arg);
40     unused(channel);
41     switch (int_type) {
42         case HAL_DMA_INTERRUPT_TFR:
43             g_dma_trans_done = 1;
44             break;
45         case HAL_DMA_INTERRUPT_BLOCK:
46             g_dma_trans_done = 1;
47             break;
48         case HAL_DMA_INTERRUPT_ERR:
49             osal_printk("DMA transfer error.\r\n");
50             break;
51         default:
52             break;
53     }
54 }
55 
dma_task(const char * arg)56 static void *dma_task(const char *arg)
57 {
58     unused(arg);
59     /* DMA init. */
60     uapi_dma_init();
61     uapi_dma_open();
62 
63 #if defined(CONFIG_DMA_MEMORY_LLI_TRANSFER_MODE)
64     dma_channel_t dma_channel = DMA_CHANNEL_NONE;
65     dma_channel = uapi_dma_get_lli_channel(0, HAL_DMA_HANDSHAKING_MAX_NUM);
66 #endif
67 
68     for (uint32_t i = 0; i < DMA_TRANSFER_WORD_NUM; i++) {
69         g_app_dma_src_data[i] = i;
70     }
71     memset_s(g_app_dma_desc_data, DMA_TRANSFER_WORD_NUM, 0, DMA_TRANSFER_WORD_NUM);
72 
73     dma_ch_user_memory_config_t transfer_config = { 0 };
74     transfer_config.src = (uint32_t)(uintptr_t)g_app_dma_src_data;
75     transfer_config.dest = (uint32_t)(uintptr_t)g_app_dma_desc_data;
76     transfer_config.transfer_num = DMA_TRANSFER_WORD_NUM;
77     transfer_config.priority = DMA_TRANSFER_PRIORITY;
78     transfer_config.width = DMA_TRANSFER_WIDTH;
79 
80     while (1) {
81         osal_msleep(DMA_TASK_DURATION_MS);
82         g_dma_trans_done = 0;
83 #if defined(CONFIG_DMA_MEMORY_LLI_TRANSFER_MODE)
84         osal_printk("dma config link list item of memory to memory start!\r\n");
85         if (uapi_dma_transfer_memory_lli(dma_channel, &transfer_config, app_dma_trans_done_callback) == ERRCODE_SUCC) {
86             osal_printk("dma config link list item of memory to memory succ!\r\n");
87         }
88         osal_printk("dma enable lli memory transfer start!\r\n");
89         if (uapi_dma_enable_lli(dma_channel, app_dma_trans_done_callback, (uintptr_t)NULL) == ERRCODE_SUCC) {
90             osal_printk("dma enable lli memory transfer succ!\r\n");
91         }
92         while (!g_dma_trans_done) {}
93         if (uapi_dma_end_transfer(dma_channel) == ERRCODE_SUCC) {
94             osal_printk("dma channel transfer finish!\r\n");
95         }
96 #else
97         osal_printk("dma single memory transfer start!\r\n");
98         if (uapi_dma_transfer_memory_single(&transfer_config, app_dma_trans_done_callback,
99                                             (uintptr_t)NULL) == ERRCODE_SUCC) {
100             osal_printk("dma single memory transfer succ!\r\n");
101         }
102         while (!g_dma_trans_done) {}
103         osal_printk("dma checking transfer from 0x%08x to 0x%08x...\r\n", transfer_config.src, transfer_config.dest);
104         if (memcmp((void *)transfer_config.src, (void *)transfer_config.dest, transfer_config.transfer_num) == 0) {
105             osal_printk("dma memory copy test succ, length = %d block\r\n", transfer_config.transfer_num);
106         }
107 #endif
108     }
109 
110     return NULL;
111 }
112 
dma_entry(void)113 static void dma_entry(void)
114 {
115     osal_task *task_handle = NULL;
116     osal_kthread_lock();
117     task_handle = osal_kthread_create((osal_kthread_handler)dma_task, 0, "DmaTask", DMA_TASK_STACK_SIZE);
118     if (task_handle != NULL) {
119         osal_kthread_set_priority(task_handle, DMA_TASK_PRIO);
120         osal_kfree(task_handle);
121     }
122     osal_kthread_unlock();
123 }
124 
125 /* Run the dma_entry. */
126 app_run(dma_entry);