Forked.
Fork of mbed-dev by
targets/TARGET_NUVOTON/TARGET_NUC472/dma_api.c@174:ed647f63e28d, 2017-12-19 (annotated)
- Committer:
- Dollyparton
- Date:
- Tue Dec 19 12:50:13 2017 +0000
- Revision:
- 174:ed647f63e28d
- Parent:
- 165:e614a9f1c9e2
Added RAW socket.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 144:ef7eb2e8f9f7 | 1 | /* mbed Microcontroller Library |
<> | 144:ef7eb2e8f9f7 | 2 | * Copyright (c) 2015-2016 Nuvoton |
<> | 144:ef7eb2e8f9f7 | 3 | * |
<> | 144:ef7eb2e8f9f7 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
<> | 144:ef7eb2e8f9f7 | 5 | * you may not use this file except in compliance with the License. |
<> | 144:ef7eb2e8f9f7 | 6 | * You may obtain a copy of the License at |
<> | 144:ef7eb2e8f9f7 | 7 | * |
<> | 144:ef7eb2e8f9f7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
<> | 144:ef7eb2e8f9f7 | 9 | * |
<> | 144:ef7eb2e8f9f7 | 10 | * Unless required by applicable law or agreed to in writing, software |
<> | 144:ef7eb2e8f9f7 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
<> | 144:ef7eb2e8f9f7 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
<> | 144:ef7eb2e8f9f7 | 13 | * See the License for the specific language governing permissions and |
<> | 144:ef7eb2e8f9f7 | 14 | * limitations under the License. |
<> | 144:ef7eb2e8f9f7 | 15 | */ |
<> | 144:ef7eb2e8f9f7 | 16 | |
<> | 144:ef7eb2e8f9f7 | 17 | #include "dma_api.h" |
<> | 144:ef7eb2e8f9f7 | 18 | #include "string.h" |
<> | 144:ef7eb2e8f9f7 | 19 | #include "cmsis.h" |
<> | 144:ef7eb2e8f9f7 | 20 | #include "mbed_assert.h" |
<> | 144:ef7eb2e8f9f7 | 21 | #include "PeripheralNames.h" |
<> | 144:ef7eb2e8f9f7 | 22 | #include "nu_modutil.h" |
<> | 144:ef7eb2e8f9f7 | 23 | #include "nu_bitutil.h" |
<> | 144:ef7eb2e8f9f7 | 24 | #include "dma.h" |
<> | 144:ef7eb2e8f9f7 | 25 | |
AnnaBridge | 165:e614a9f1c9e2 | 26 | #define NU_PDMA_CH_MAX PDMA_CH_MAX /* Specify maximum channels of PDMA */ |
AnnaBridge | 165:e614a9f1c9e2 | 27 | #define NU_PDMA_CH_Pos 0 /* Specify first channel number of PDMA */ |
AnnaBridge | 165:e614a9f1c9e2 | 28 | #define NU_PDMA_CH_Msk (((1 << NU_PDMA_CH_MAX) - 1) << NU_PDMA_CH_Pos) |
AnnaBridge | 165:e614a9f1c9e2 | 29 | |
<> | 144:ef7eb2e8f9f7 | 30 | struct nu_dma_chn_s { |
<> | 144:ef7eb2e8f9f7 | 31 | void (*handler)(uint32_t, uint32_t); |
<> | 144:ef7eb2e8f9f7 | 32 | uint32_t id; |
<> | 144:ef7eb2e8f9f7 | 33 | uint32_t event; |
<> | 144:ef7eb2e8f9f7 | 34 | }; |
<> | 144:ef7eb2e8f9f7 | 35 | |
<> | 144:ef7eb2e8f9f7 | 36 | static int dma_inited = 0; |
<> | 144:ef7eb2e8f9f7 | 37 | static uint32_t dma_chn_mask = 0; |
AnnaBridge | 165:e614a9f1c9e2 | 38 | static struct nu_dma_chn_s dma_chn_arr[NU_PDMA_CH_MAX]; |
<> | 144:ef7eb2e8f9f7 | 39 | |
<> | 144:ef7eb2e8f9f7 | 40 | static void pdma_vec(void); |
<> | 144:ef7eb2e8f9f7 | 41 | static const struct nu_modinit_s dma_modinit = {DMA_0, PDMA_MODULE, 0, 0, PDMA_RST, PDMA_IRQn, (void *) pdma_vec}; |
<> | 144:ef7eb2e8f9f7 | 42 | |
<> | 144:ef7eb2e8f9f7 | 43 | |
<> | 144:ef7eb2e8f9f7 | 44 | void dma_init(void) |
<> | 144:ef7eb2e8f9f7 | 45 | { |
<> | 144:ef7eb2e8f9f7 | 46 | if (dma_inited) { |
<> | 144:ef7eb2e8f9f7 | 47 | return; |
<> | 144:ef7eb2e8f9f7 | 48 | } |
<> | 144:ef7eb2e8f9f7 | 49 | |
<> | 144:ef7eb2e8f9f7 | 50 | dma_inited = 1; |
AnnaBridge | 165:e614a9f1c9e2 | 51 | dma_chn_mask = ~NU_PDMA_CH_Msk; |
<> | 144:ef7eb2e8f9f7 | 52 | memset(dma_chn_arr, 0x00, sizeof (dma_chn_arr)); |
<> | 144:ef7eb2e8f9f7 | 53 | |
<> | 144:ef7eb2e8f9f7 | 54 | // Reset this module |
<> | 144:ef7eb2e8f9f7 | 55 | SYS_ResetModule(dma_modinit.rsetidx); |
<> | 144:ef7eb2e8f9f7 | 56 | |
<> | 144:ef7eb2e8f9f7 | 57 | // Enable IP clock |
<> | 144:ef7eb2e8f9f7 | 58 | CLK_EnableModuleClock(dma_modinit.clkidx); |
<> | 144:ef7eb2e8f9f7 | 59 | |
<> | 144:ef7eb2e8f9f7 | 60 | PDMA_Open(0); |
<> | 144:ef7eb2e8f9f7 | 61 | |
<> | 144:ef7eb2e8f9f7 | 62 | NVIC_SetVector(dma_modinit.irq_n, (uint32_t) dma_modinit.var); |
<> | 144:ef7eb2e8f9f7 | 63 | NVIC_EnableIRQ(dma_modinit.irq_n); |
<> | 144:ef7eb2e8f9f7 | 64 | } |
<> | 144:ef7eb2e8f9f7 | 65 | |
<> | 144:ef7eb2e8f9f7 | 66 | int dma_channel_allocate(uint32_t capabilities) |
<> | 144:ef7eb2e8f9f7 | 67 | { |
<> | 144:ef7eb2e8f9f7 | 68 | if (! dma_inited) { |
<> | 144:ef7eb2e8f9f7 | 69 | dma_init(); |
<> | 144:ef7eb2e8f9f7 | 70 | } |
<> | 144:ef7eb2e8f9f7 | 71 | |
<> | 144:ef7eb2e8f9f7 | 72 | int i = nu_cto(dma_chn_mask); |
<> | 144:ef7eb2e8f9f7 | 73 | if (i != 32) { |
<> | 144:ef7eb2e8f9f7 | 74 | dma_chn_mask |= 1 << i; |
AnnaBridge | 165:e614a9f1c9e2 | 75 | memset(dma_chn_arr + i - NU_PDMA_CH_Pos, 0x00, sizeof (struct nu_dma_chn_s)); |
<> | 144:ef7eb2e8f9f7 | 76 | return i; |
<> | 144:ef7eb2e8f9f7 | 77 | } |
<> | 144:ef7eb2e8f9f7 | 78 | |
<> | 144:ef7eb2e8f9f7 | 79 | // No channel available |
<> | 144:ef7eb2e8f9f7 | 80 | return DMA_ERROR_OUT_OF_CHANNELS; |
<> | 144:ef7eb2e8f9f7 | 81 | } |
<> | 144:ef7eb2e8f9f7 | 82 | |
<> | 144:ef7eb2e8f9f7 | 83 | int dma_channel_free(int channelid) |
<> | 144:ef7eb2e8f9f7 | 84 | { |
<> | 144:ef7eb2e8f9f7 | 85 | if (channelid != DMA_ERROR_OUT_OF_CHANNELS) { |
<> | 144:ef7eb2e8f9f7 | 86 | dma_chn_mask &= ~(1 << channelid); |
<> | 144:ef7eb2e8f9f7 | 87 | } |
<> | 144:ef7eb2e8f9f7 | 88 | |
<> | 144:ef7eb2e8f9f7 | 89 | return 0; |
<> | 144:ef7eb2e8f9f7 | 90 | } |
<> | 144:ef7eb2e8f9f7 | 91 | |
<> | 144:ef7eb2e8f9f7 | 92 | void dma_set_handler(int channelid, uint32_t handler, uint32_t id, uint32_t event) |
<> | 144:ef7eb2e8f9f7 | 93 | { |
<> | 144:ef7eb2e8f9f7 | 94 | MBED_ASSERT(dma_chn_mask & (1 << channelid)); |
<> | 144:ef7eb2e8f9f7 | 95 | |
AnnaBridge | 165:e614a9f1c9e2 | 96 | dma_chn_arr[channelid - NU_PDMA_CH_Pos].handler = (void (*)(uint32_t, uint32_t)) handler; |
AnnaBridge | 165:e614a9f1c9e2 | 97 | dma_chn_arr[channelid - NU_PDMA_CH_Pos].id = id; |
AnnaBridge | 165:e614a9f1c9e2 | 98 | dma_chn_arr[channelid - NU_PDMA_CH_Pos].event = event; |
<> | 144:ef7eb2e8f9f7 | 99 | |
<> | 144:ef7eb2e8f9f7 | 100 | // Set interrupt vector if someone has removed it. |
<> | 144:ef7eb2e8f9f7 | 101 | NVIC_SetVector(dma_modinit.irq_n, (uint32_t) dma_modinit.var); |
<> | 144:ef7eb2e8f9f7 | 102 | NVIC_EnableIRQ(dma_modinit.irq_n); |
<> | 144:ef7eb2e8f9f7 | 103 | } |
<> | 144:ef7eb2e8f9f7 | 104 | |
<> | 161:2cc1468da177 | 105 | PDMA_T *dma_modbase(void) |
<> | 161:2cc1468da177 | 106 | { |
<> | 161:2cc1468da177 | 107 | return (PDMA_T *) NU_MODBASE(dma_modinit.modname); |
<> | 161:2cc1468da177 | 108 | } |
<> | 161:2cc1468da177 | 109 | |
<> | 144:ef7eb2e8f9f7 | 110 | static void pdma_vec(void) |
<> | 144:ef7eb2e8f9f7 | 111 | { |
<> | 144:ef7eb2e8f9f7 | 112 | uint32_t intsts = PDMA_GET_INT_STATUS(); |
<> | 144:ef7eb2e8f9f7 | 113 | |
<> | 144:ef7eb2e8f9f7 | 114 | // Abort |
<> | 144:ef7eb2e8f9f7 | 115 | if (intsts & PDMA_INTSTS_ABTIF_Msk) { |
<> | 144:ef7eb2e8f9f7 | 116 | uint32_t abtsts = PDMA_GET_ABORT_STS(); |
<> | 144:ef7eb2e8f9f7 | 117 | // Clear all Abort flags |
<> | 144:ef7eb2e8f9f7 | 118 | PDMA_CLR_ABORT_FLAG(abtsts); |
<> | 144:ef7eb2e8f9f7 | 119 | |
<> | 144:ef7eb2e8f9f7 | 120 | while (abtsts) { |
AnnaBridge | 165:e614a9f1c9e2 | 121 | int chn_id = nu_ctz(abtsts) - PDMA_ABTSTS_ABTIF_Pos + NU_PDMA_CH_Pos; |
<> | 144:ef7eb2e8f9f7 | 122 | if (dma_chn_mask & (1 << chn_id)) { |
AnnaBridge | 165:e614a9f1c9e2 | 123 | struct nu_dma_chn_s *dma_chn = dma_chn_arr + chn_id - NU_PDMA_CH_Pos; |
<> | 144:ef7eb2e8f9f7 | 124 | if (dma_chn->handler && (dma_chn->event & DMA_EVENT_ABORT)) { |
<> | 144:ef7eb2e8f9f7 | 125 | dma_chn->handler(dma_chn->id, DMA_EVENT_ABORT); |
<> | 144:ef7eb2e8f9f7 | 126 | } |
<> | 144:ef7eb2e8f9f7 | 127 | } |
AnnaBridge | 165:e614a9f1c9e2 | 128 | abtsts &= ~(1 << (chn_id - NU_PDMA_CH_Pos + PDMA_ABTSTS_ABTIF_Pos)); |
<> | 144:ef7eb2e8f9f7 | 129 | } |
<> | 144:ef7eb2e8f9f7 | 130 | } |
<> | 144:ef7eb2e8f9f7 | 131 | |
<> | 144:ef7eb2e8f9f7 | 132 | // Transfer done |
<> | 144:ef7eb2e8f9f7 | 133 | if (intsts & PDMA_INTSTS_TDIF_Msk) { |
<> | 144:ef7eb2e8f9f7 | 134 | uint32_t tdsts = PDMA_GET_TD_STS(); |
<> | 144:ef7eb2e8f9f7 | 135 | // Clear all transfer done flags |
<> | 144:ef7eb2e8f9f7 | 136 | PDMA_CLR_TD_FLAG(tdsts); |
<> | 144:ef7eb2e8f9f7 | 137 | |
<> | 144:ef7eb2e8f9f7 | 138 | while (tdsts) { |
AnnaBridge | 165:e614a9f1c9e2 | 139 | int chn_id = nu_ctz(tdsts) - PDMA_TDSTS_TDIF_Pos + NU_PDMA_CH_Pos; |
<> | 144:ef7eb2e8f9f7 | 140 | if (dma_chn_mask & (1 << chn_id)) { |
AnnaBridge | 165:e614a9f1c9e2 | 141 | struct nu_dma_chn_s *dma_chn = dma_chn_arr + chn_id - NU_PDMA_CH_Pos; |
<> | 144:ef7eb2e8f9f7 | 142 | if (dma_chn->handler && (dma_chn->event & DMA_EVENT_TRANSFER_DONE)) { |
<> | 144:ef7eb2e8f9f7 | 143 | dma_chn->handler(dma_chn->id, DMA_EVENT_TRANSFER_DONE); |
<> | 144:ef7eb2e8f9f7 | 144 | } |
<> | 144:ef7eb2e8f9f7 | 145 | } |
AnnaBridge | 165:e614a9f1c9e2 | 146 | tdsts &= ~(1 << (chn_id - NU_PDMA_CH_Pos + PDMA_TDSTS_TDIF_Pos)); |
<> | 144:ef7eb2e8f9f7 | 147 | } |
<> | 144:ef7eb2e8f9f7 | 148 | } |
<> | 144:ef7eb2e8f9f7 | 149 | |
<> | 144:ef7eb2e8f9f7 | 150 | // Table empty |
<> | 144:ef7eb2e8f9f7 | 151 | if (intsts & PDMA_INTSTS_TEIF_Msk) { |
<> | 144:ef7eb2e8f9f7 | 152 | uint32_t scatsts = PDMA_GET_EMPTY_STS(); |
<> | 144:ef7eb2e8f9f7 | 153 | // Clear all table empty flags |
<> | 144:ef7eb2e8f9f7 | 154 | PDMA_CLR_EMPTY_FLAG(scatsts); |
<> | 144:ef7eb2e8f9f7 | 155 | } |
<> | 144:ef7eb2e8f9f7 | 156 | |
<> | 144:ef7eb2e8f9f7 | 157 | // Timeout |
<> | 144:ef7eb2e8f9f7 | 158 | uint32_t reqto = intsts & PDMA_INTSTS_REQTOFX_Msk; |
<> | 144:ef7eb2e8f9f7 | 159 | if (reqto) { |
<> | 144:ef7eb2e8f9f7 | 160 | // Clear all Timeout flags |
<> | 144:ef7eb2e8f9f7 | 161 | PDMA->INTSTS = reqto; |
<> | 144:ef7eb2e8f9f7 | 162 | |
<> | 144:ef7eb2e8f9f7 | 163 | while (reqto) { |
AnnaBridge | 165:e614a9f1c9e2 | 164 | int chn_id = nu_ctz(reqto) - PDMA_INTSTS_REQTOFX_Pos + NU_PDMA_CH_Pos; |
<> | 144:ef7eb2e8f9f7 | 165 | if (dma_chn_mask & (1 << chn_id)) { |
AnnaBridge | 165:e614a9f1c9e2 | 166 | struct nu_dma_chn_s *dma_chn = dma_chn_arr + chn_id - NU_PDMA_CH_Pos; |
<> | 144:ef7eb2e8f9f7 | 167 | if (dma_chn->handler && (dma_chn->event & DMA_EVENT_TIMEOUT)) { |
<> | 144:ef7eb2e8f9f7 | 168 | dma_chn->handler(dma_chn->id, DMA_EVENT_TIMEOUT); |
<> | 144:ef7eb2e8f9f7 | 169 | } |
<> | 144:ef7eb2e8f9f7 | 170 | } |
AnnaBridge | 165:e614a9f1c9e2 | 171 | reqto &= ~(1 << (chn_id - NU_PDMA_CH_Pos + PDMA_INTSTS_REQTOFX_Pos)); |
<> | 144:ef7eb2e8f9f7 | 172 | } |
<> | 144:ef7eb2e8f9f7 | 173 | } |
<> | 144:ef7eb2e8f9f7 | 174 | } |