chen
2024-07-29 55579b9ada2fc22edcae817e7d6e65710c47edd3
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
/*
 * Copyright (c) 2019-2023 Beijing Hanwei Innovation Technology Ltd. Co. and
 * its subsidiaries and affiliates (collectly called MKSEMI).
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form, except as embedded into an MKSEMI
 *    integrated circuit in a product or a software update for such product,
 *    must reproduce the above copyright notice, this list of conditions and
 *    the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of MKSEMI nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * 4. This software, with or without modification, must only be used with a
 *    MKSEMI integrated circuit.
 *
 * 5. Any software provided in binary form under this license must not be
 *    reverse engineered, decompiled, modified and/or disassembled.
 *
 * THIS SOFTWARE IS PROVIDED BY MKSEMI "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL MKSEMI OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
#include "mk_dma.h"
#include "mk_clock.h"
#include "mk_reset.h"
#include "mk_trace.h"
 
static struct DMA_HANDLE_T dma_handle[DMA_MAX_NUM] = {
    {
        .base = DMA,
        .irq = DMA_IRQn,
    },
};
 
int dma_open(enum DMA_CH_T ch, struct DMA_CH_CFG_T *config)
{
    if ((ch >= DMA_CH_NUM) || (config == NULL))
    {
        return DRV_ERROR;
    }
 
    if ((dma_handle[DMA_ID0].base->CTRL0 & DMA_CTRL0_ENABLE_MSK) == 0)
    {
        // enable DMA clock
        clock_enable(CLOCK_DMA);
        reset_module(RESET_MODULE_DMA);
 
        // enable DMA
        dma_handle[DMA_ID0].base->CTRL0 |= DMA_CTRL0_ENABLE_MSK;
 
        NVIC_SetPriority(dma_handle[DMA_ID0].irq, IRQ_PRIORITY_HIGH);
        NVIC_ClearPendingIRQ(dma_handle[DMA_ID0].irq);
        NVIC_EnableIRQ(dma_handle[DMA_ID0].irq);
    }
 
    dma_handle[DMA_ID0].base->CH[ch].CFG = DMA_CH_CFG_DST_REQ_SEL(config->dst_req_sel) | DMA_CH_CFG_SRC_REQ_SEL(config->src_req_sel);
 
    dma_handle[DMA_ID0].base->CH[ch].CTRL = DMA_CH_CTRL_FIFO_TH(config->fifo_th) | DMA_CH_CTRL_SRC_BURST_SIZE(config->src_burst_size) |
                                            DMA_CH_CTRL_SRC_DATA_WIDTH(config->src_width) | DMA_CH_CTRL_DST_DATA_WIDTH(config->dst_width) |
                                            DMA_CH_CTRL_MODE_MSK | DMA_CH_CTRL_SRC_ADDR_CTRL(config->src_addr_ctrl) |
                                            DMA_CH_CTRL_DST_ADDR_CTRL(config->dst_addr_ctrl);
 
    return DRV_OK;
}
 
int dma_close(enum DMA_CH_T ch)
{
    if (ch >= DMA_CH_NUM)
    {
        return DRV_ERROR;
    }
 
    dma_handle[DMA_ID0].base->CH[ch].CTRL &= ~DMA_CH_CTRL_EN_MSK;
 
    if ((dma_handle[DMA_ID0].base->STATUS1 & DMA_STATUS1_ENABLE_MSK) == 0)
    {
        // disable DMA
        dma_handle[DMA_ID0].base->CTRL0 &= ~DMA_CTRL0_ENABLE_MSK;
        // disable DMA clock
        clock_disable(CLOCK_DMA);
 
        NVIC_DisableIRQ(dma_handle[DMA_ID0].irq);
        NVIC_ClearPendingIRQ(dma_handle[DMA_ID0].irq);
    }
    return DRV_OK;
}
uint32_t get_uart1_dma_cndtr(void)
{
return dma_handle[DMA_ID0].base->CH[6].DATA_SIZE;
}
uint32_t get_uart0_dma_cndtr(void)
{
return dma_handle[DMA_ID0].base->CH[4].DATA_SIZE;
}
 
int dma_transfer(enum DMA_CH_T ch, void *src_addr, void *dst_addr, uint32_t size, drv_callback_t callback)
{
    ASSERT(dst_addr, "Invalid transfer dst addr");
 
    if (dma_handle[DMA_ID0].base->STATUS1 & (1U << (ch + DMA_STATUS1_ENABLE_POS)))
    {
        return DRV_BUSY;
    }
 
    dma_handle[DMA_ID0].callback[ch] = callback;
 
    // enable dma channel int
    dma_handle[DMA_ID0].base->CH[ch].CFG &= ~(DMA_CH_CFG_INT_DONE_MSK | DMA_CH_CFG_INT_ERR_MSK | DMA_CH_CFG_INT_ABORT_MSK);
 
    // source address
    dma_handle[DMA_ID0].base->CH[ch].ADDR_SRC = (uint32_t)src_addr;
 
    // destination address
    dma_handle[DMA_ID0].base->CH[ch].ADDR_DST = (uint32_t)dst_addr;
 
    // transfer size
    dma_handle[DMA_ID0].base->CH[ch].DATA_SIZE = size;
 
    // enable dma channel
    dma_handle[DMA_ID0].base->CH[ch].CTRL |= DMA_CH_CTRL_EN_MSK;
 
    return DRV_OK;
}
 
int dma_abort(enum DMA_CH_T ch, drv_callback_t callback)
{
    int ret = DRV_ERROR;
    uint32_t lock = int_lock();
    // Detect whether dma is in a busy state. If it is not in a busy state,
    // abort will not be executed and callback will not be called
    if (REG_IS_BIT_SET(dma_handle[DMA_ID0].base->STATUS1, (1U << (ch + DMA_STATUS1_ENABLE_POS))))
    {
        // disable dma channel done and err int
        dma_handle[DMA_ID0].base->CH[ch].CFG |= (DMA_CH_CFG_INT_DONE_MSK | DMA_CH_CFG_INT_ERR_MSK);
        // Transaction abort
        dma_handle[DMA_ID0].base->CH[ch].CTRL |= DMA_CH_CTRL_ABORT_MSK;
 
        // Clear Status0 Register
        dma_handle[DMA_ID0].base->INTR_CLR = ((1U << (ch + DMA_INTR_STATUS_DONE_POS)) | (1U << (ch + DMA_INTR_STATUS_ERR_POS)));
        dma_handle[DMA_ID0].abort_callback[ch] = callback;
        ret = DRV_OK;
    }
 
    int_unlock(lock);
    // LOG_INFO(TRACE_MODULE_DRIVER, "dma_abort %d\r\n", ch);
    return ret;
}
 
void dma_force_abort(enum DMA_CH_T ch, drv_callback_t callback)
{
    uint32_t lock = int_lock();
    // disable dma channel done/abort/err int
    dma_handle[DMA_ID0].base->CH[ch].CFG |= (DMA_CH_CFG_INT_ABORT_MSK | DMA_CH_CFG_INT_DONE_MSK | DMA_CH_CFG_INT_ERR_MSK);
 
    if (REG_IS_BIT_SET(dma_handle[DMA_ID0].base->STATUS1, (1U << (ch + DMA_STATUS1_ENABLE_POS))))
    {
        // Transaction abort
        dma_handle[DMA_ID0].base->CH[ch].CTRL |= DMA_CH_CTRL_ABORT_MSK;
    }
 
    // Clear interrupt penging source
    dma_handle[DMA_ID0].base->INTR_CLR =
        (1U << (ch + DMA_INTR_STATUS_DONE_POS)) | (1U << (ch + DMA_INTR_STATUS_ERR_POS)) | (1U << (ch + DMA_INTR_STATUS_ABORT_POS));
    // Clear interrupt pending
    NVIC_ClearPendingIRQ(dma_handle[DMA_ID0].irq);
 
    if (callback)
    {
        callback(&ch, DMA_INT_TYPE_FORCE_ABORT);
    }
 
    int_unlock(lock);
}
 
void DMA_IRQHandler(void)
{
    uint32_t err_code = 0;
    uint32_t int_status = dma_handle[DMA_ID0].base->INTR_STATUS;
 
    for (uint8_t i = 0; i < DMA_CH_NUM; i++)
    {
        // abort/error/done interrupt process
        if (int_status & (1U << (i + DMA_INTR_STATUS_ABORT_POS)))
        {
            dma_handle[DMA_ID0].base->INTR_CLR = (1U << (i + DMA_INTR_STATUS_ABORT_POS));
            err_code = DMA_INT_TYPE_ABORT;
        }
        else if (int_status & (1U << (i + DMA_INTR_STATUS_ERR_POS)))
        {
            dma_handle[DMA_ID0].base->INTR_CLR = (1U << (i + DMA_INTR_STATUS_ERR_POS));
            err_code = DMA_INT_TYPE_ERROR;
        }
        else if (int_status & (1U << (i + DMA_INTR_STATUS_DONE_POS)))
        {
            dma_handle[DMA_ID0].base->INTR_CLR = (1U << (i + DMA_INTR_STATUS_DONE_POS));
            err_code = DMA_INT_TYPE_DONE;
        }
 
        if (err_code == DMA_INT_TYPE_DONE || err_code == DMA_INT_TYPE_ERROR)
        {
            if (dma_handle[DMA_ID0].callback[i])
            {
                dma_handle[DMA_ID0].callback[i](&i, err_code);
            }
            err_code = 0;
        }
        else if (err_code == DMA_INT_TYPE_ABORT)
        {
            if (dma_handle[DMA_ID0].abort_callback[i])
            {
                dma_handle[DMA_ID0].abort_callback[i](&i, err_code);
            }
            err_code = 0;
        }
    }
}