1 /***************************************************************************//**
2  * @file
3  * @brief Direct memory access (LDMA) module peripheral API
4  *******************************************************************************
5  * # License
6  * <b>Copyright 2018 Silicon Laboratories Inc. www.silabs.com</b>
7  *******************************************************************************
8  *
9  * SPDX-License-Identifier: Zlib
10  *
11  * The licensor of this software is Silicon Laboratories Inc.
12  *
13  * This software is provided 'as-is', without any express or implied
14  * warranty. In no event will the authors be held liable for any damages
15  * arising from the use of this software.
16  *
17  * Permission is granted to anyone to use this software for any purpose,
18  * including commercial applications, and to alter it and redistribute it
19  * freely, subject to the following restrictions:
20  *
21  * 1. The origin of this software must not be misrepresented; you must not
22  *    claim that you wrote the original software. If you use this software
23  *    in a product, an acknowledgment in the product documentation would be
24  *    appreciated but is not required.
25  * 2. Altered source versions must be plainly marked as such, and must not be
26  *    misrepresented as being the original software.
27  * 3. This notice may not be removed or altered from any source distribution.
28  *
29  ******************************************************************************/
30 
31 #include "em_ldma.h"
32 
33 #if defined(LDMA_PRESENT) && (LDMA_COUNT == 1)
34 
35 #include <stddef.h>
36 #include "sl_assert.h"
37 #include "em_bus.h"
38 #include "em_cmu.h"
39 #include "em_core.h"
40 
41 /***************************************************************************//**
42  * @addtogroup ldma
43  * @{
44  ******************************************************************************/
45 
46 #if defined(LDMA_IRQ_HANDLER_TEMPLATE)
47 /***************************************************************************//**
48  * @brief
49  *   A template for an LDMA IRQ handler.
50  ******************************************************************************/
LDMA_IRQHandler(void)51 void LDMA_IRQHandler(void)
52 {
53   uint32_t ch;
54   /* Get all pending and enabled interrupts. */
55   uint32_t pending = LDMA_IntGetEnabled();
56 
57   /* Loop on an LDMA error to enable debugging. */
58   while (pending & LDMA_IF_ERROR) {
59   }
60 
61   /* Iterate over all LDMA channels. */
62   for (ch = 0; ch < DMA_CHAN_COUNT; ch++) {
63     uint32_t mask = 0x1 << ch;
64     if (pending & mask) {
65       /* Clear the interrupt flag. */
66       LDMA->IFC = mask;
67 
68       /* Perform more actions here, execute callbacks, and so on. */
69     }
70   }
71 }
72 #endif
73 
74 /***************************************************************************//**
75  * @brief
76  *   De-initialize the LDMA controller.
77  *
78  *   LDMA interrupts are disabled and the LDMA clock is stopped.
79  ******************************************************************************/
LDMA_DeInit(void)80 void LDMA_DeInit(void)
81 {
82   NVIC_DisableIRQ(LDMA_IRQn);
83   LDMA->IEN  = 0;
84 #if defined(_LDMA_CHDIS_MASK)
85   LDMA->CHDIS = _LDMA_CHEN_MASK;
86 #else
87   LDMA->CHEN = 0;
88 #endif
89 #if defined(LDMA_EN_EN)
90   LDMA->EN = 0;
91 #if defined(LDMA_EN_DISABLING)
92   while (LDMA->EN & _LDMA_EN_DISABLING_MASK) {
93   }
94 #endif
95 #endif
96 
97   CMU_ClockEnable(cmuClock_LDMA, false);
98 #if defined(_SILICON_LABS_32B_SERIES_2_CONFIG) && (_SILICON_LABS_32B_SERIES_2_CONFIG > 1)
99   CMU_ClockEnable(cmuClock_LDMAXBAR, false);
100 #endif
101 }
102 
103 /***************************************************************************//**
104  * @brief
105  *   Enable or disable an LDMA channel request.
106  *
107  * @details
108  *   Use this function to enable or disable an LDMA channel request. This will
109  *   prevent the LDMA from proceeding after its current transaction if disabled.
110  *
111  * @param[in] ch
112  *   LDMA channel to enable or disable requests.
113  *
114  * @param[in] enable
115  *   If 'true', the request will be enabled. If 'false', the request will be disabled.
116  ******************************************************************************/
LDMA_EnableChannelRequest(int ch,bool enable)117 void LDMA_EnableChannelRequest(int ch, bool enable)
118 {
119   EFM_ASSERT(ch < (int)DMA_CHAN_COUNT);
120 
121   BUS_RegBitWrite(&LDMA->REQDIS, ch, !enable);
122 }
123 
124 /***************************************************************************//**
125  * @brief
126  *   Initialize the LDMA controller.
127  *
128  * @details
129  *   This function will disable all the LDMA channels and enable the LDMA bus
130  *   clock in the CMU. This function will also enable the LDMA IRQ in the NVIC
131  *   and set the LDMA IRQ priority to a user-configurable priority. The LDMA
132  *   interrupt priority is configured using the @ref LDMA_Init_t structure.
133  *
134  * @note
135  *   Since this function enables the LDMA IRQ, always add a custom
136  *   LDMA_IRQHandler to the application to handle any interrupts
137  *   from LDMA.
138  *
139  * @param[in] init
140  *   A pointer to the initialization structure used to configure the LDMA.
141  ******************************************************************************/
LDMA_Init(const LDMA_Init_t * init)142 void LDMA_Init(const LDMA_Init_t *init)
143 {
144   uint32_t ldmaCtrlVal;
145   EFM_ASSERT(init != NULL);
146   EFM_ASSERT(!(((uint32_t)init->ldmaInitCtrlNumFixed << _LDMA_CTRL_NUMFIXED_SHIFT)
147                & ~_LDMA_CTRL_NUMFIXED_MASK));
148 
149   EFM_ASSERT(init->ldmaInitIrqPriority < (1 << __NVIC_PRIO_BITS));
150 
151   CMU_ClockEnable(cmuClock_LDMA, true);
152 #if defined(_SILICON_LABS_32B_SERIES_2_CONFIG) && (_SILICON_LABS_32B_SERIES_2_CONFIG > 1)
153   CMU_ClockEnable(cmuClock_LDMAXBAR, true);
154 #endif
155 
156 #if defined(LDMA_EN_EN)
157   LDMA->EN = LDMA_EN_EN;
158 #endif
159 
160   ldmaCtrlVal = (uint32_t)init->ldmaInitCtrlNumFixed << _LDMA_CTRL_NUMFIXED_SHIFT;
161 
162 #if defined(_LDMA_CTRL_SYNCPRSCLREN_SHIFT) && defined (_LDMA_CTRL_SYNCPRSSETEN_SHIFT)
163   ldmaCtrlVal |=  (init->ldmaInitCtrlSyncPrsClrEn << _LDMA_CTRL_SYNCPRSCLREN_SHIFT)
164                  | (init->ldmaInitCtrlSyncPrsSetEn << _LDMA_CTRL_SYNCPRSSETEN_SHIFT);
165 #endif
166 
167   LDMA->CTRL = ldmaCtrlVal;
168 
169 #if defined(_LDMA_SYNCHWEN_SYNCCLREN_SHIFT) && defined (_LDMA_SYNCHWEN_SYNCSETEN_SHIFT)
170   LDMA->SYNCHWEN = ((uint32_t)init->ldmaInitCtrlSyncPrsClrEn << _LDMA_SYNCHWEN_SYNCCLREN_SHIFT)
171                    | ((uint32_t)init->ldmaInitCtrlSyncPrsSetEn << _LDMA_SYNCHWEN_SYNCSETEN_SHIFT);
172 #endif
173 
174 #if defined(_LDMA_CHDIS_MASK)
175   LDMA->CHDIS = _LDMA_CHEN_MASK;
176 #else
177   LDMA->CHEN    = 0;
178 #endif
179   LDMA->DBGHALT = 0;
180   LDMA->REQDIS  = 0;
181 
182   /* Enable the LDMA error interrupt. */
183   LDMA->IEN = LDMA_IEN_ERROR;
184 #if defined (LDMA_HAS_SET_CLEAR)
185   LDMA->IF_CLR = 0xFFFFFFFFU;
186 #else
187   LDMA->IFC = 0xFFFFFFFFU;
188 #endif
189   NVIC_ClearPendingIRQ(LDMA_IRQn);
190 
191   /* Range is 0-7, where 0 is the highest priority. */
192   NVIC_SetPriority(LDMA_IRQn, init->ldmaInitIrqPriority);
193 
194   NVIC_EnableIRQ(LDMA_IRQn);
195 }
196 
197 /***************************************************************************//**
198  * @brief
199  *   Start a DMA transfer.
200  *
201  * @param[in] ch
202  *   A DMA channel.
203  *
204  * @param[in] transfer
205  *   The initialization structure used to configure the transfer.
206  *
207  * @param[in] descriptor
208  *   The transfer descriptor, which can be an array of descriptors linked together.
209  *   Each descriptor's fields stored in RAM will be loaded into the certain
210  *   hardware registers at the proper time to perform the DMA transfer.
211  ******************************************************************************/
LDMA_StartTransfer(int ch,const LDMA_TransferCfg_t * transfer,const LDMA_Descriptor_t * descriptor)212 void LDMA_StartTransfer(int ch,
213                         const LDMA_TransferCfg_t *transfer,
214                         const LDMA_Descriptor_t  *descriptor)
215 {
216 #if !(defined (_LDMA_SYNCHWEN_SYNCCLREN_SHIFT) && defined (_LDMA_SYNCHWEN_SYNCSETEN_SHIFT))
217   uint32_t tmp;
218 #endif
219   CORE_DECLARE_IRQ_STATE;
220   uint32_t chMask = 1UL << (uint8_t)ch;
221 
222   EFM_ASSERT(ch < (int)DMA_CHAN_COUNT);
223   EFM_ASSERT(transfer != NULL);
224 
225 #if defined (_LDMAXBAR_CH_REQSEL_MASK)
226   EFM_ASSERT(!(transfer->ldmaReqSel & ~_LDMAXBAR_CH_REQSEL_MASK));
227 #elif defined (_LDMA_CH_REQSEL_MASK)
228   EFM_ASSERT(!(transfer->ldmaReqSel & ~_LDMA_CH_REQSEL_MASK));
229 #endif
230 
231   EFM_ASSERT(!(((uint32_t)transfer->ldmaCfgArbSlots << _LDMA_CH_CFG_ARBSLOTS_SHIFT)
232                & ~_LDMA_CH_CFG_ARBSLOTS_MASK));
233   EFM_ASSERT(!(((uint32_t)transfer->ldmaCfgSrcIncSign << _LDMA_CH_CFG_SRCINCSIGN_SHIFT)
234                & ~_LDMA_CH_CFG_SRCINCSIGN_MASK));
235   EFM_ASSERT(!(((uint32_t)transfer->ldmaCfgDstIncSign << _LDMA_CH_CFG_DSTINCSIGN_SHIFT)
236                & ~_LDMA_CH_CFG_DSTINCSIGN_MASK));
237 
238   /* Clear the pending channel interrupt. */
239 #if defined (LDMA_HAS_SET_CLEAR)
240   LDMA->IF_CLR = chMask;
241 #else
242   LDMA->IFC = chMask;
243 #endif
244 
245 #if defined(LDMAXBAR)
246   LDMAXBAR->CH[ch].REQSEL = transfer->ldmaReqSel;
247 #else
248   LDMA->CH[ch].REQSEL = transfer->ldmaReqSel;
249 #endif
250   LDMA->CH[ch].LOOP = transfer->ldmaLoopCnt << _LDMA_CH_LOOP_LOOPCNT_SHIFT;
251   LDMA->CH[ch].CFG = (transfer->ldmaCfgArbSlots << _LDMA_CH_CFG_ARBSLOTS_SHIFT)
252                      | (transfer->ldmaCfgSrcIncSign << _LDMA_CH_CFG_SRCINCSIGN_SHIFT)
253                      | (transfer->ldmaCfgDstIncSign << _LDMA_CH_CFG_DSTINCSIGN_SHIFT)
254 #if defined(_LDMA_CH_CFG_SRCBUSPORT_MASK)
255                      | (transfer->ldmaCfgStructBusPort << _LDMA_CH_CFG_STRUCTBUSPORT_SHIFT)
256                      | (transfer->ldmaCfgSrcBusPort << _LDMA_CH_CFG_SRCBUSPORT_SHIFT)
257                      | (transfer->ldmaCfgDstBusPort << _LDMA_CH_CFG_DSTBUSPORT_SHIFT)
258 #endif
259   ;
260 
261   /* Set the descriptor address. */
262   LDMA->CH[ch].LINK = (uint32_t)descriptor & _LDMA_CH_LINK_LINKADDR_MASK;
263 
264   /* A critical region. */
265   CORE_ENTER_ATOMIC();
266 
267   /* Enable the channel interrupt. */
268   BUS_RegMaskedSet(&LDMA->IEN, chMask);
269 
270   if (transfer->ldmaReqDis) {
271     LDMA->REQDIS |= chMask;
272   }
273 
274   if (transfer->ldmaDbgHalt) {
275     LDMA->DBGHALT |= chMask;
276   }
277 
278 #if defined (_LDMA_SYNCHWEN_SYNCCLREN_SHIFT) && defined (_LDMA_SYNCHWEN_SYNCSETEN_SHIFT)
279 
280   LDMA->SYNCHWEN_CLR =
281     (((uint32_t)transfer->ldmaCtrlSyncPrsClrOff << _LDMA_SYNCHWEN_SYNCCLREN_SHIFT)
282      | ((uint32_t)transfer->ldmaCtrlSyncPrsSetOff << _LDMA_SYNCHWEN_SYNCSETEN_SHIFT))
283     & _LDMA_SYNCHWEN_MASK;
284 
285   LDMA->SYNCHWEN_SET =
286     (((uint32_t)transfer->ldmaCtrlSyncPrsClrOn << _LDMA_SYNCHWEN_SYNCCLREN_SHIFT)
287      | ((uint32_t)transfer->ldmaCtrlSyncPrsSetOn << _LDMA_SYNCHWEN_SYNCSETEN_SHIFT))
288     & _LDMA_SYNCHWEN_MASK;
289 
290 #elif defined (_LDMA_CTRL_SYNCPRSCLREN_SHIFT) && defined (_LDMA_CTRL_SYNCPRSSETEN_SHIFT)
291 
292   tmp = LDMA->CTRL;
293 
294   if (transfer->ldmaCtrlSyncPrsClrOff) {
295     tmp &= ~_LDMA_CTRL_SYNCPRSCLREN_MASK
296            | (~transfer->ldmaCtrlSyncPrsClrOff << _LDMA_CTRL_SYNCPRSCLREN_SHIFT);
297   }
298 
299   if (transfer->ldmaCtrlSyncPrsClrOn) {
300     tmp |= transfer->ldmaCtrlSyncPrsClrOn << _LDMA_CTRL_SYNCPRSCLREN_SHIFT;
301   }
302 
303   if (transfer->ldmaCtrlSyncPrsSetOff) {
304     tmp &= ~_LDMA_CTRL_SYNCPRSSETEN_MASK
305            | (~transfer->ldmaCtrlSyncPrsSetOff << _LDMA_CTRL_SYNCPRSSETEN_SHIFT);
306   }
307 
308   if (transfer->ldmaCtrlSyncPrsSetOn) {
309     tmp |= transfer->ldmaCtrlSyncPrsSetOn << _LDMA_CTRL_SYNCPRSSETEN_SHIFT;
310   }
311 
312   LDMA->CTRL = tmp;
313 
314 #else
315 
316   #error  "SYNC Set and SYNC Clear not defined"
317 
318 #endif
319 
320   BUS_RegMaskedClear(&LDMA->CHDONE, chMask);  /* Clear the done flag.     */
321   LDMA->LINKLOAD = chMask;      /* Start a transfer by loading the descriptor.  */
322 
323   /* A critical region end. */
324   CORE_EXIT_ATOMIC();
325 }
326 
327 #if defined(_LDMA_CH_CTRL_EXTEND_MASK)
328 /***************************************************************************//**
329  * @brief
330  *   Start an extended DMA transfer.
331  *
332  * @param[in] ch
333  *   A DMA channel.
334  *
335  * @param[in] transfer
336  *   The initialization structure used to configure the transfer.
337  *
338  * @param[in] descriptor_ext
339  *   The extended transfer descriptor, which can be an array of descriptors
340  *   linked together. Each descriptor's fields stored in RAM will be loaded
341  *   into the certain hardware registers at the proper time to perform the DMA
342  *   transfer.
343  ******************************************************************************/
LDMA_StartTransferExtend(int ch,const LDMA_TransferCfg_t * transfer,const LDMA_DescriptorExtend_t * descriptor_ext)344 void LDMA_StartTransferExtend(int ch,
345                               const LDMA_TransferCfg_t *transfer,
346                               const LDMA_DescriptorExtend_t *descriptor_ext)
347 {
348   // Ensure destination interleaving supported for given channel.
349   EFM_ASSERT(((1 << ch) & LDMA_ILCHNL));
350 
351   LDMA_StartTransfer(ch,
352                      transfer,
353                      (const LDMA_Descriptor_t *)descriptor_ext);
354 }
355 #endif
356 
357 /***************************************************************************//**
358  * @brief
359  *   Stop a DMA transfer.
360  *
361  * @note
362  *   The DMA will complete the current AHB burst transfer before stopping.
363  *
364  * @param[in] ch
365  *   A DMA channel to stop.
366  ******************************************************************************/
LDMA_StopTransfer(int ch)367 void LDMA_StopTransfer(int ch)
368 {
369   uint32_t chMask = 1UL << (uint8_t)ch;
370 
371   EFM_ASSERT(ch < (int)DMA_CHAN_COUNT);
372 
373 #if defined(_LDMA_CHDIS_MASK)
374   CORE_ATOMIC_SECTION(
375     LDMA->IEN &= ~chMask;
376     LDMA->CHDIS = chMask;
377     )
378 #else
379   CORE_ATOMIC_SECTION(
380     LDMA->IEN &= ~chMask;
381     BUS_RegMaskedClear(&LDMA->CHEN, chMask);
382     )
383 #endif
384 }
385 
386 /***************************************************************************//**
387  * @brief
388  *   Check if a DMA transfer has completed.
389  *
390  * @param[in] ch
391  *   A DMA channel to check.
392  *
393  * @return
394  *   True if transfer has completed, false if not.
395  ******************************************************************************/
LDMA_TransferDone(int ch)396 bool LDMA_TransferDone(int ch)
397 {
398   bool     retVal = false;
399   uint32_t chMask = 1UL << (uint8_t)ch;
400 
401   EFM_ASSERT(ch < (int)DMA_CHAN_COUNT);
402 
403 #if defined(_LDMA_CHSTATUS_MASK)
404   CORE_ATOMIC_SECTION(
405     if (((LDMA->CHSTATUS & chMask) == 0) && ((LDMA->CHDONE & chMask) == chMask)) {
406     retVal = true;
407   }
408     )
409 #else
410   CORE_ATOMIC_SECTION(
411     if (((LDMA->CHEN & chMask) == 0) && ((LDMA->CHDONE & chMask) == chMask)) {
412     retVal = true;
413   }
414     )
415 #endif
416 
417   return retVal;
418 }
419 
420 /***************************************************************************//**
421  * @brief
422  *  Get the number of items remaining in a transfer.
423  *
424  * @note
425  *  This function does not take into account that a DMA transfer with
426  *  a chain of linked transfers might be ongoing. It will only check the
427  *  count for the current transfer.
428  *
429  * @param[in] ch
430  *  The channel number of the transfer to check.
431  *
432  * @return
433  *  A number of items remaining in the transfer.
434  ******************************************************************************/
LDMA_TransferRemainingCount(int ch)435 uint32_t LDMA_TransferRemainingCount(int ch)
436 {
437   uint32_t remaining, done;
438   uint32_t chMask = 1UL << (uint8_t)ch;
439 
440   EFM_ASSERT(ch < (int)DMA_CHAN_COUNT);
441 
442   CORE_ATOMIC_SECTION(
443     done   = LDMA->CHDONE;
444     remaining = LDMA->CH[ch].CTRL;
445     )
446 
447   done     &= chMask;
448 
449   if (done) {
450     return 0;
451   }
452 
453   remaining = (remaining & _LDMA_CH_CTRL_XFERCNT_MASK)
454               >> _LDMA_CH_CTRL_XFERCNT_SHIFT;
455 
456   /* +1 because XFERCNT is 0-based. */
457   return remaining + 1;
458 }
459 
460 /** @} (end addtogroup ldma) */
461 #endif /* defined( LDMA_PRESENT ) && ( LDMA_COUNT == 1 ) */
462