mbed library sources. Supersedes mbed-src. Fixed broken STM32F1xx RTC on rtc_api.c
Dependents: Nucleo_F103RB_RTC_battery_bkup_pwr_off_okay
Fork of mbed-dev by
targets/TARGET_NUVOTON/TARGET_NUC472/dma_api.c@161:2cc1468da177, 2017-03-30 (annotated)
- Committer:
- <>
- Date:
- Thu Mar 30 13:45:57 2017 +0100
- Revision:
- 161:2cc1468da177
- Parent:
- 149:156823d33999
- Child:
- 165:e614a9f1c9e2
This updates the lib to the mbed lib v139
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 | |
<> | 144:ef7eb2e8f9f7 | 26 | struct nu_dma_chn_s { |
<> | 144:ef7eb2e8f9f7 | 27 | void (*handler)(uint32_t, uint32_t); |
<> | 144:ef7eb2e8f9f7 | 28 | uint32_t id; |
<> | 144:ef7eb2e8f9f7 | 29 | uint32_t event; |
<> | 144:ef7eb2e8f9f7 | 30 | }; |
<> | 144:ef7eb2e8f9f7 | 31 | |
<> | 144:ef7eb2e8f9f7 | 32 | static int dma_inited = 0; |
<> | 144:ef7eb2e8f9f7 | 33 | static uint32_t dma_chn_mask = 0; |
<> | 144:ef7eb2e8f9f7 | 34 | static struct nu_dma_chn_s dma_chn_arr[PDMA_CH_MAX]; |
<> | 144:ef7eb2e8f9f7 | 35 | |
<> | 144:ef7eb2e8f9f7 | 36 | static void pdma_vec(void); |
<> | 144:ef7eb2e8f9f7 | 37 | static const struct nu_modinit_s dma_modinit = {DMA_0, PDMA_MODULE, 0, 0, PDMA_RST, PDMA_IRQn, (void *) pdma_vec}; |
<> | 144:ef7eb2e8f9f7 | 38 | |
<> | 144:ef7eb2e8f9f7 | 39 | |
<> | 144:ef7eb2e8f9f7 | 40 | void dma_init(void) |
<> | 144:ef7eb2e8f9f7 | 41 | { |
<> | 144:ef7eb2e8f9f7 | 42 | if (dma_inited) { |
<> | 144:ef7eb2e8f9f7 | 43 | return; |
<> | 144:ef7eb2e8f9f7 | 44 | } |
<> | 144:ef7eb2e8f9f7 | 45 | |
<> | 144:ef7eb2e8f9f7 | 46 | dma_inited = 1; |
<> | 144:ef7eb2e8f9f7 | 47 | dma_chn_mask = 0; |
<> | 144:ef7eb2e8f9f7 | 48 | memset(dma_chn_arr, 0x00, sizeof (dma_chn_arr)); |
<> | 144:ef7eb2e8f9f7 | 49 | |
<> | 144:ef7eb2e8f9f7 | 50 | // Reset this module |
<> | 144:ef7eb2e8f9f7 | 51 | SYS_ResetModule(dma_modinit.rsetidx); |
<> | 144:ef7eb2e8f9f7 | 52 | |
<> | 144:ef7eb2e8f9f7 | 53 | // Enable IP clock |
<> | 144:ef7eb2e8f9f7 | 54 | CLK_EnableModuleClock(dma_modinit.clkidx); |
<> | 144:ef7eb2e8f9f7 | 55 | |
<> | 144:ef7eb2e8f9f7 | 56 | PDMA_Open(0); |
<> | 144:ef7eb2e8f9f7 | 57 | |
<> | 144:ef7eb2e8f9f7 | 58 | NVIC_SetVector(dma_modinit.irq_n, (uint32_t) dma_modinit.var); |
<> | 144:ef7eb2e8f9f7 | 59 | NVIC_EnableIRQ(dma_modinit.irq_n); |
<> | 144:ef7eb2e8f9f7 | 60 | } |
<> | 144:ef7eb2e8f9f7 | 61 | |
<> | 144:ef7eb2e8f9f7 | 62 | int dma_channel_allocate(uint32_t capabilities) |
<> | 144:ef7eb2e8f9f7 | 63 | { |
<> | 144:ef7eb2e8f9f7 | 64 | if (! dma_inited) { |
<> | 144:ef7eb2e8f9f7 | 65 | dma_init(); |
<> | 144:ef7eb2e8f9f7 | 66 | } |
<> | 144:ef7eb2e8f9f7 | 67 | |
<> | 144:ef7eb2e8f9f7 | 68 | #if 1 |
<> | 144:ef7eb2e8f9f7 | 69 | int i = nu_cto(dma_chn_mask); |
<> | 144:ef7eb2e8f9f7 | 70 | if (i != 32) { |
<> | 144:ef7eb2e8f9f7 | 71 | dma_chn_mask |= 1 << i; |
<> | 144:ef7eb2e8f9f7 | 72 | memset(dma_chn_arr + i, 0x00, sizeof (struct nu_dma_chn_s)); |
<> | 144:ef7eb2e8f9f7 | 73 | return i; |
<> | 144:ef7eb2e8f9f7 | 74 | } |
<> | 144:ef7eb2e8f9f7 | 75 | #else |
<> | 144:ef7eb2e8f9f7 | 76 | int i; |
<> | 144:ef7eb2e8f9f7 | 77 | |
<> | 144:ef7eb2e8f9f7 | 78 | for (i = 0; i < PDMA_CH_MAX; i ++) { |
<> | 144:ef7eb2e8f9f7 | 79 | if ((dma_chn_mask & (1 << i)) == 0) { |
<> | 144:ef7eb2e8f9f7 | 80 | // Channel available |
<> | 144:ef7eb2e8f9f7 | 81 | dma_chn_mask |= 1 << i; |
<> | 144:ef7eb2e8f9f7 | 82 | memset(dma_chn_arr + i, 0x00, sizeof (struct nu_dma_chn_s)); |
<> | 144:ef7eb2e8f9f7 | 83 | return i; |
<> | 144:ef7eb2e8f9f7 | 84 | } |
<> | 144:ef7eb2e8f9f7 | 85 | } |
<> | 144:ef7eb2e8f9f7 | 86 | #endif |
<> | 144:ef7eb2e8f9f7 | 87 | |
<> | 144:ef7eb2e8f9f7 | 88 | // No channel available |
<> | 144:ef7eb2e8f9f7 | 89 | return DMA_ERROR_OUT_OF_CHANNELS; |
<> | 144:ef7eb2e8f9f7 | 90 | } |
<> | 144:ef7eb2e8f9f7 | 91 | |
<> | 144:ef7eb2e8f9f7 | 92 | int dma_channel_free(int channelid) |
<> | 144:ef7eb2e8f9f7 | 93 | { |
<> | 144:ef7eb2e8f9f7 | 94 | if (channelid != DMA_ERROR_OUT_OF_CHANNELS) { |
<> | 144:ef7eb2e8f9f7 | 95 | dma_chn_mask &= ~(1 << channelid); |
<> | 144:ef7eb2e8f9f7 | 96 | } |
<> | 144:ef7eb2e8f9f7 | 97 | |
<> | 144:ef7eb2e8f9f7 | 98 | return 0; |
<> | 144:ef7eb2e8f9f7 | 99 | } |
<> | 144:ef7eb2e8f9f7 | 100 | |
<> | 144:ef7eb2e8f9f7 | 101 | void dma_set_handler(int channelid, uint32_t handler, uint32_t id, uint32_t event) |
<> | 144:ef7eb2e8f9f7 | 102 | { |
<> | 144:ef7eb2e8f9f7 | 103 | MBED_ASSERT(dma_chn_mask & (1 << channelid)); |
<> | 144:ef7eb2e8f9f7 | 104 | |
<> | 144:ef7eb2e8f9f7 | 105 | dma_chn_arr[channelid].handler = (void (*)(uint32_t, uint32_t)) handler; |
<> | 144:ef7eb2e8f9f7 | 106 | dma_chn_arr[channelid].id = id; |
<> | 144:ef7eb2e8f9f7 | 107 | dma_chn_arr[channelid].event = event; |
<> | 144:ef7eb2e8f9f7 | 108 | |
<> | 144:ef7eb2e8f9f7 | 109 | // Set interrupt vector if someone has removed it. |
<> | 144:ef7eb2e8f9f7 | 110 | NVIC_SetVector(dma_modinit.irq_n, (uint32_t) dma_modinit.var); |
<> | 144:ef7eb2e8f9f7 | 111 | NVIC_EnableIRQ(dma_modinit.irq_n); |
<> | 144:ef7eb2e8f9f7 | 112 | } |
<> | 144:ef7eb2e8f9f7 | 113 | |
<> | 161:2cc1468da177 | 114 | PDMA_T *dma_modbase(void) |
<> | 161:2cc1468da177 | 115 | { |
<> | 161:2cc1468da177 | 116 | return (PDMA_T *) NU_MODBASE(dma_modinit.modname); |
<> | 161:2cc1468da177 | 117 | } |
<> | 161:2cc1468da177 | 118 | |
<> | 144:ef7eb2e8f9f7 | 119 | static void pdma_vec(void) |
<> | 144:ef7eb2e8f9f7 | 120 | { |
<> | 144:ef7eb2e8f9f7 | 121 | uint32_t intsts = PDMA_GET_INT_STATUS(); |
<> | 144:ef7eb2e8f9f7 | 122 | |
<> | 144:ef7eb2e8f9f7 | 123 | // Abort |
<> | 144:ef7eb2e8f9f7 | 124 | if (intsts & PDMA_INTSTS_ABTIF_Msk) { |
<> | 144:ef7eb2e8f9f7 | 125 | uint32_t abtsts = PDMA_GET_ABORT_STS(); |
<> | 144:ef7eb2e8f9f7 | 126 | // Clear all Abort flags |
<> | 144:ef7eb2e8f9f7 | 127 | PDMA_CLR_ABORT_FLAG(abtsts); |
<> | 144:ef7eb2e8f9f7 | 128 | |
<> | 144:ef7eb2e8f9f7 | 129 | while (abtsts) { |
<> | 144:ef7eb2e8f9f7 | 130 | int chn_id = nu_ctz(abtsts); |
<> | 144:ef7eb2e8f9f7 | 131 | if (dma_chn_mask & (1 << chn_id)) { |
<> | 144:ef7eb2e8f9f7 | 132 | struct nu_dma_chn_s *dma_chn = dma_chn_arr + chn_id; |
<> | 144:ef7eb2e8f9f7 | 133 | if (dma_chn->handler && (dma_chn->event & DMA_EVENT_ABORT)) { |
<> | 144:ef7eb2e8f9f7 | 134 | dma_chn->handler(dma_chn->id, DMA_EVENT_ABORT); |
<> | 144:ef7eb2e8f9f7 | 135 | } |
<> | 144:ef7eb2e8f9f7 | 136 | } |
<> | 144:ef7eb2e8f9f7 | 137 | abtsts &= ~(1 << chn_id); |
<> | 144:ef7eb2e8f9f7 | 138 | } |
<> | 144:ef7eb2e8f9f7 | 139 | } |
<> | 144:ef7eb2e8f9f7 | 140 | |
<> | 144:ef7eb2e8f9f7 | 141 | // Transfer done |
<> | 144:ef7eb2e8f9f7 | 142 | if (intsts & PDMA_INTSTS_TDIF_Msk) { |
<> | 144:ef7eb2e8f9f7 | 143 | uint32_t tdsts = PDMA_GET_TD_STS(); |
<> | 144:ef7eb2e8f9f7 | 144 | // Clear all transfer done flags |
<> | 144:ef7eb2e8f9f7 | 145 | PDMA_CLR_TD_FLAG(tdsts); |
<> | 144:ef7eb2e8f9f7 | 146 | |
<> | 144:ef7eb2e8f9f7 | 147 | while (tdsts) { |
<> | 144:ef7eb2e8f9f7 | 148 | int chn_id = nu_ctz(tdsts); |
<> | 144:ef7eb2e8f9f7 | 149 | if (dma_chn_mask & (1 << chn_id)) { |
<> | 144:ef7eb2e8f9f7 | 150 | struct nu_dma_chn_s *dma_chn = dma_chn_arr + chn_id; |
<> | 144:ef7eb2e8f9f7 | 151 | if (dma_chn->handler && (dma_chn->event & DMA_EVENT_TRANSFER_DONE)) { |
<> | 144:ef7eb2e8f9f7 | 152 | dma_chn->handler(dma_chn->id, DMA_EVENT_TRANSFER_DONE); |
<> | 144:ef7eb2e8f9f7 | 153 | } |
<> | 144:ef7eb2e8f9f7 | 154 | } |
<> | 144:ef7eb2e8f9f7 | 155 | tdsts &= ~(1 << chn_id); |
<> | 144:ef7eb2e8f9f7 | 156 | } |
<> | 144:ef7eb2e8f9f7 | 157 | } |
<> | 144:ef7eb2e8f9f7 | 158 | |
<> | 144:ef7eb2e8f9f7 | 159 | // Table empty |
<> | 144:ef7eb2e8f9f7 | 160 | if (intsts & PDMA_INTSTS_TEIF_Msk) { |
<> | 144:ef7eb2e8f9f7 | 161 | uint32_t scatsts = PDMA_GET_EMPTY_STS(); |
<> | 144:ef7eb2e8f9f7 | 162 | // Clear all table empty flags |
<> | 144:ef7eb2e8f9f7 | 163 | PDMA_CLR_EMPTY_FLAG(scatsts); |
<> | 144:ef7eb2e8f9f7 | 164 | } |
<> | 144:ef7eb2e8f9f7 | 165 | |
<> | 144:ef7eb2e8f9f7 | 166 | // Timeout |
<> | 144:ef7eb2e8f9f7 | 167 | uint32_t reqto = intsts & PDMA_INTSTS_REQTOFX_Msk; |
<> | 144:ef7eb2e8f9f7 | 168 | if (reqto) { |
<> | 144:ef7eb2e8f9f7 | 169 | // Clear all Timeout flags |
<> | 144:ef7eb2e8f9f7 | 170 | PDMA->INTSTS = reqto; |
<> | 144:ef7eb2e8f9f7 | 171 | |
<> | 144:ef7eb2e8f9f7 | 172 | while (reqto) { |
<> | 161:2cc1468da177 | 173 | int chn_id = nu_ctz(reqto) - PDMA_INTSTS_REQTOFX_Pos; |
<> | 144:ef7eb2e8f9f7 | 174 | if (dma_chn_mask & (1 << chn_id)) { |
<> | 144:ef7eb2e8f9f7 | 175 | struct nu_dma_chn_s *dma_chn = dma_chn_arr + chn_id; |
<> | 144:ef7eb2e8f9f7 | 176 | if (dma_chn->handler && (dma_chn->event & DMA_EVENT_TIMEOUT)) { |
<> | 144:ef7eb2e8f9f7 | 177 | dma_chn->handler(dma_chn->id, DMA_EVENT_TIMEOUT); |
<> | 144:ef7eb2e8f9f7 | 178 | } |
<> | 144:ef7eb2e8f9f7 | 179 | } |
<> | 144:ef7eb2e8f9f7 | 180 | reqto &= ~(1 << (chn_id + PDMA_INTSTS_REQTOFX_Pos)); |
<> | 144:ef7eb2e8f9f7 | 181 | } |
<> | 144:ef7eb2e8f9f7 | 182 | } |
<> | 144:ef7eb2e8f9f7 | 183 | } |