PDL for FM4  Version1.2
Peripheral Driverl Library for FM4
dma.c
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2013 Spansion LLC. All Rights Reserved.
3 *
4 * This software is owned and published by:
5 * Spansion LLC, 915 DeGuigne Dr. Sunnyvale, CA 94088-3453 ("Spansion").
6 *
7 * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
8 * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
9 *
10 * This software contains source code for use with Spansion
11 * components. This software is licensed by Spansion to be adapted only
12 * for use in systems utilizing Spansion components. Spansion shall not be
13 * responsible for misuse or illegal use of this software for devices not
14 * supported herein. Spansion is providing this software "AS IS" and will
15 * not be responsible for issues arising from incorrect user implementation
16 * of the software.
17 *
18 * SPANSION MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
19 * REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
20 * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
21 * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
22 * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
23 * WARRANTY OF NONINFRINGEMENT.
24 * SPANSION SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
25 * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
26 * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
27 * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
28 * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
30 * SAVINGS OR PROFITS,
31 * EVEN IF SPANSION HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
33 * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
34 * FROM, THE SOFTWARE.
35 *
36 * This software may be replicated in part or whole for the licensed use,
37 * with the restriction that this Disclaimer and Copyright notice must be
38 * included with each copy of this software, whether used in part or whole,
39 * at all times.
40 */
41 /******************************************************************************/
52 /******************************************************************************/
53 /* Include files */
54 /******************************************************************************/
55 #include "dma.h"
56 
57 #if (defined(PDL_PERIPHERAL_DMA_ACTIVE))
58 
64 
65 /******************************************************************************/
66 /* Local pre-processor symbols/macros ('#define') */
67 /******************************************************************************/
68 // DMACAn => FM3_DMAC_BASE + size of register set + offset of DMACA0
69 // DMACAn => 0x40060000 + 0x10 * n + 0x10
70 #define GET_DMA_ADDRESS(x) (FM4_DMAC_BASE + (0x10ul * (x)) + 0x10ul)
71 
72 /******************************************************************************/
73 /* Global variable definitions (declared in header file with 'extern') */
74 /******************************************************************************/
76 
77 /******************************************************************************/
78 /* Local type definitions ('typedef') */
79 /******************************************************************************/
80 typedef union
81 {
82  uint32_t u32DMACA;
83  stc_dmac_dmaca0_field_t stcDMACA;
84 
86 
87 typedef union
88 {
89  stc_dmac_dmacb0_field_t stcDMACB;
90  uint32_t u32DMACB;
91 
93 
94 /******************************************************************************/
95 /* Local function prototypes ('static') */
96 /******************************************************************************/
97 
98 /******************************************************************************/
99 /* Local variable definitions ('static') */
100 /******************************************************************************/
101 
102 /******************************************************************************/
103 /* Function implementation - global ('extern') and local ('static') */
104 /******************************************************************************/
105 static en_result_t Dma_Irq_Init(uint8_t u8DmaChannel);
106 static en_result_t Dma_Irq_DeInit(uint8_t u8DmaChannel);
107 
119 void DmaIrqHandler(uint8_t u8DmaChannel)
120 {
121  uint32_t u32DmaRegisterBaseAddress;
122  uint32_t u32RegisterReadOut;
123  uint8_t u8StopStatus;
124 
125  u32DmaRegisterBaseAddress = GET_DMA_ADDRESS(u8DmaChannel);
126 
127  // Readout DMACB and update
128  u32RegisterReadOut = *(uint32_t*)((uint32_t)(u32DmaRegisterBaseAddress + DMA_DMACB_OFFSET));
129 
130  u8StopStatus = (uint8_t)((u32RegisterReadOut & 0x00070000ul)>> 16u);
131 
132  // Clear interrupt cause
133  u32RegisterReadOut &= 0xFFF8FFFF;
134  *(uint32_t*)(uint32_t)((u32DmaRegisterBaseAddress + DMA_DMACB_OFFSET))
135  = u32RegisterReadOut;
136 
137  if (0x5u == u8StopStatus) // Successful DMA transfer
138  {
139  stcDmaInternData.apfnDmaCallback[u8DmaChannel]();
140  }
141  else // Errornous DMA transfer
142  {
143  stcDmaInternData.apfnDmaErrorCallback[u8DmaChannel](u8StopStatus);
144  }
145 }
146 
158 static en_result_t Dma_Irq_Init(uint8_t u8DmaChannel)
159 {
160  switch(u8DmaChannel)
161  {
162  case 0u:
163  NVIC_ClearPendingIRQ(DMAC0_IRQn);
164  NVIC_EnableIRQ(DMAC0_IRQn);
165  NVIC_SetPriority(DMAC0_IRQn, PDL_DMA0_LEVEL);
166  break;
167  case 1u:
168  NVIC_ClearPendingIRQ(DMAC1_IRQn);
169  NVIC_EnableIRQ(DMAC1_IRQn);
170  NVIC_SetPriority(DMAC1_IRQn, PDL_DMA1_LEVEL);
171  break;
172  case 2u:
173  NVIC_ClearPendingIRQ(DMAC2_IRQn);
174  NVIC_EnableIRQ(DMAC2_IRQn);
175  NVIC_SetPriority(DMAC2_IRQn, PDL_DMA2_LEVEL);
176  break;
177  case 3u:
178  NVIC_ClearPendingIRQ(DMAC3_IRQn);
179  NVIC_EnableIRQ(DMAC3_IRQn);
180  NVIC_SetPriority(DMAC3_IRQn, PDL_DMA3_LEVEL);
181  break;
182  case 4u:
183  NVIC_ClearPendingIRQ(DMAC4_IRQn);
184  NVIC_EnableIRQ(DMAC4_IRQn);
185  NVIC_SetPriority(DMAC4_IRQn, PDL_DMA4_LEVEL);
186  break;
187  case 5u:
188  NVIC_ClearPendingIRQ(DMAC5_IRQn);
189  NVIC_EnableIRQ(DMAC5_IRQn);
190  NVIC_SetPriority(DMAC5_IRQn, PDL_DMA5_LEVEL);
191  break;
192  case 6u:
193  NVIC_ClearPendingIRQ(DMAC6_IRQn);
194  NVIC_EnableIRQ(DMAC6_IRQn);
195  NVIC_SetPriority(DMAC6_IRQn, PDL_DMA6_LEVEL);
196  break;
197  case 7u:
198  NVIC_ClearPendingIRQ(DMAC7_IRQn);
199  NVIC_EnableIRQ(DMAC7_IRQn);
200  NVIC_SetPriority(DMAC7_IRQn, PDL_DMA7_LEVEL);
201  break;
202  default:
203  break;
204  }
205  return Ok;
206 }
207 
219 static en_result_t Dma_Irq_DeInit(uint8_t u8DmaChannel)
220 {
221  switch(u8DmaChannel)
222  {
223  case 0:
224  NVIC_ClearPendingIRQ(DMAC0_IRQn);
225  NVIC_DisableIRQ(DMAC0_IRQn);
226  NVIC_SetPriority(DMAC0_IRQn, PDL_DEFAULT_INTERRUPT_LEVEL);
227  break;
228  case 1:
229  NVIC_ClearPendingIRQ(DMAC1_IRQn);
230  NVIC_DisableIRQ(DMAC1_IRQn);
231  NVIC_SetPriority(DMAC1_IRQn, PDL_DEFAULT_INTERRUPT_LEVEL);
232  break;
233  case 2:
234  NVIC_ClearPendingIRQ(DMAC2_IRQn);
235  NVIC_DisableIRQ(DMAC2_IRQn);
236  NVIC_SetPriority(DMAC2_IRQn, PDL_DEFAULT_INTERRUPT_LEVEL);
237  break;
238  case 3:
239  NVIC_ClearPendingIRQ(DMAC3_IRQn);
240  NVIC_DisableIRQ(DMAC3_IRQn);
241  NVIC_SetPriority(DMAC3_IRQn, PDL_DEFAULT_INTERRUPT_LEVEL);
242  break;
243  case 4:
244  NVIC_ClearPendingIRQ(DMAC4_IRQn);
245  NVIC_DisableIRQ(DMAC4_IRQn);
246  NVIC_SetPriority(DMAC4_IRQn, PDL_DEFAULT_INTERRUPT_LEVEL);
247  break;
248  case 5:
249  NVIC_ClearPendingIRQ(DMAC5_IRQn);
250  NVIC_DisableIRQ(DMAC5_IRQn);
251  NVIC_SetPriority(DMAC5_IRQn, PDL_DEFAULT_INTERRUPT_LEVEL);
252  break;
253  case 6:
254  NVIC_ClearPendingIRQ(DMAC6_IRQn);
255  NVIC_DisableIRQ(DMAC6_IRQn);
256  NVIC_SetPriority(DMAC6_IRQn, PDL_DEFAULT_INTERRUPT_LEVEL);
257  break;
258  case 7:
259  NVIC_ClearPendingIRQ(DMAC7_IRQn);
260  NVIC_DisableIRQ(DMAC7_IRQn);
261  NVIC_SetPriority(DMAC7_IRQn, PDL_DEFAULT_INTERRUPT_LEVEL);
262  break;
263  default:
264  break;
265  }
266  return Ok;
267 }
268 
284 {
285  un_dmac_dmaca_t unDmacDmaca;
286  un_dmac_dmacb_t unDmacDmacb;
287  uint32_t u32DmaRegisterBaseAddress;
288  func_ptr_t pfnCallBackPointer;
289  func_ptr_arg1_t pfnErrorCallBackPointer;
290  boolean_t bAvoidSideEffects;
291 
292  // Check for NULL pointer
293  if (pstcConfig == NULL)
294  {
295  return ErrorInvalidParameter ;
296  }
297 
298  unDmacDmaca.u32DMACA = 0u;
299  unDmacDmacb.u32DMACB = 0u;
300 
301  u32DmaRegisterBaseAddress = GET_DMA_ADDRESS(pstcConfig->u8DmaChannel);
302 
303  // Set Callback function pointers
304  pfnCallBackPointer = pstcConfig->pfnCallback;
305  stcDmaInternData.apfnDmaCallback[pstcConfig->u8DmaChannel] = pfnCallBackPointer;
306 
307  pfnErrorCallBackPointer = pstcConfig->pfnErrorCallback;
308  stcDmaInternData.apfnDmaErrorCallback[pstcConfig->u8DmaChannel] = pfnErrorCallBackPointer;
309 
310  // Check if DMA channel already used
311  if (stcDmaInternData.au8DmaChannelActive[pstcConfig->u8DmaChannel] == DMA_CHANNEL_ACTIVE)
312  {
313  return OperationInProgress;
314  }
315 
316  // Set DMA channel as active
317  stcDmaInternData.au8DmaChannelActive[pstcConfig->u8DmaChannel] = DMA_CHANNEL_ACTIVE;
318 
319  // Setup DMACA ...
320  //
321  // Block Count
322  unDmacDmaca.stcDMACA.BC = 0x0Fu & (uint32_t)(pstcConfig->u8BlockCount);
323 
324  // Transfer Count
325  unDmacDmaca.stcDMACA.TC = 0xFFFFu & (uint32_t)(pstcConfig->u16TransferCount);
326 
327  // Interrupt Request Channel
328  if (pstcConfig->enDmaIdrq == Software)
329  {
330  unDmacDmaca.stcDMACA.IS = 0u;
331  }
332  else
333  {
334  unDmacDmaca.stcDMACA.IS = 0x20u | (0x1Fu & (uint32_t)(pstcConfig->enDmaIdrq));
335  }
336 
337  // ... and update hardware
338  *(uint32_t*)(uint32_t)((u32DmaRegisterBaseAddress + DMA_DMACA_OFFSET)) = unDmacDmaca.u32DMACA ;
339 
340  // Setup DMACB ...
341  //
342  // First Transfer mode
343  switch (pstcConfig->enTransferMode)
344  {
345  case DmaBlockTransfer:
346  unDmacDmacb.stcDMACB.MS = 0u;
347  break;
348  case DmaBurstTransfer:
349  unDmacDmacb.stcDMACB.MS = 1u;
350  break;
351  case DmaDemandTransfer:
352  unDmacDmacb.stcDMACB.MS = 2u;
353  break;
354  default :
355  return ErrorInvalidParameter;
356  }
357 
358  // Transfer width
359  switch (pstcConfig->enTransferWdith)
360  {
361  case Dma8Bit:
362  unDmacDmacb.stcDMACB.TW = 0u;
363  break;
364  case Dma16Bit:
365  unDmacDmacb.stcDMACB.TW = 1u;
366  break;
367  case Dma32Bit:
368  unDmacDmacb.stcDMACB.TW = 2u;
369  break;
370  default :
371  return ErrorInvalidParameter;
372  }
373 
374  // Fixed source
375  unDmacDmacb.stcDMACB.FS = (uint8_t)((pstcConfig->bFixedSource == TRUE) ? 1u : 0u);
376 
377  // Fixed destination
378  unDmacDmacb.stcDMACB.FD = (uint8_t)((pstcConfig->bFixedDestination == TRUE) ? 1u : 0u);
379 
380  // Reload Count (BC/TC reload)
381  unDmacDmacb.stcDMACB.RC = (uint8_t)((pstcConfig->bReloadCount == TRUE) ? 1u : 0u);
382 
383  // Reload Source
384  unDmacDmacb.stcDMACB.RS = (uint8_t)((pstcConfig->bReloadSource == TRUE) ? 1u : 0u);
385 
386  // Reload Destination
387  unDmacDmacb.stcDMACB.RD = (uint8_t)((pstcConfig->bReloadDestination == TRUE) ? 1u : 0u);
388 
389  // Error interrupt enable
390  unDmacDmacb.stcDMACB.EI = (uint8_t)((pstcConfig->bErrorInterruptEnable == TRUE) ? 1u : 0u);
391 
392  // Completion interrupt enable
393  unDmacDmacb.stcDMACB.CI = (uint8_t)((pstcConfig->bCompletionInterruptEnable == TRUE) ? 1u : 0u);
394 
395  // Workaround for MISRA rule 12.4!!! pstcConfig is not modified in this context!!!
396  bAvoidSideEffects = ((pstcConfig->bErrorInterruptEnable == TRUE) ? 1u : 0u);
397  bAvoidSideEffects |= ((pstcConfig->bCompletionInterruptEnable == TRUE) ? 1u : 0u);
398  if (bAvoidSideEffects == TRUE)
399  {
400  Dma_Irq_Init(pstcConfig->u8DmaChannel);
401  }
402 
403  // Enable bit mask
404  unDmacDmacb.stcDMACB.EM = (uint8_t)((pstcConfig->bEnableBitMask == TRUE) ? 1u : 0u);
405 
406  // ... and update hardware
407  *(uint32_t*)((uint32_t)(u32DmaRegisterBaseAddress + DMA_DMACB_OFFSET))
408  = unDmacDmacb.u32DMACB ;
409 
410  // Setup source address
411  *(uint32_t*)((uint32_t)(u32DmaRegisterBaseAddress + DMA_DMASA_OFFSET))
412  = pstcConfig->u32SourceAddress;
413 
414  // Setup destination address
415  *(uint32_t*)((uint32_t)(u32DmaRegisterBaseAddress + DMA_DMADA_OFFSET))
416  = pstcConfig->u32DestinationAddress;
417 
418  // Switch resource interrupt to DMA (except software DMA)
419  if (pstcConfig->enDmaIdrq != Software)
420  {
421  uint32_t u32DrqselBit = 1ul << (uint32_t)(pstcConfig->enDmaIdrq);
422  FM4_INTREQ->DRQSEL |= u32DrqselBit;
423  }
424 
425  return Ok;
426 }
427 
442 {
443  un_dmac_dmaca_t unDmacDmaca;
444  uint32_t u32RegisterReadOut ;
445  uint32_t u32DmaRegisterBaseAddress ;
446 
447  // Check for NULL pointer
448  if (pstcConfig == NULL)
449  {
450  return ErrorInvalidParameter ;
451  }
452 
453  unDmacDmaca.u32DMACA = 0u;
454  unDmacDmaca.stcDMACA.EB = (uint8_t)((pstcConfig->bEnable == TRUE) ? 1u : 0u);
455  unDmacDmaca.stcDMACA.PB = (uint8_t)((pstcConfig->bPause == TRUE) ? 1u : 0u);
456  unDmacDmaca.stcDMACA.ST = (uint8_t)((pstcConfig->bSoftwareTrigger == TRUE) ? 1u : 0u);
457 
458  u32DmaRegisterBaseAddress = GET_DMA_ADDRESS(pstcConfig->u8DmaChannel);
459 
460  // Readout DMACA and update
461  u32RegisterReadOut = *(uint32_t*)((uint32_t)(u32DmaRegisterBaseAddress + DMA_DMACA_OFFSET)) ;
462  unDmacDmaca.u32DMACA |= (u32RegisterReadOut & 0x1FFFFFFFu); // masking EB, PB, ST ...
463  *(uint32_t*)((uint32_t)(u32DmaRegisterBaseAddress + DMA_DMACA_OFFSET)) = unDmacDmaca.u32DMACA ;
464 
465  return Ok;
466 }
467 
476 {
477  FM4_DMAC->DMACR = 0x80000000ul;
478 
479  return Ok;
480 }
481 
490 {
491  FM4_DMAC->DMACR = 0u;
492 
493  return Ok;
494 }
495 
509 {
510  uint32_t u32DmaRegisterBaseAddress;
511 
512  // Check for NULL pointer
513  if (pstcConfig == NULL)
514  {
515  return ErrorInvalidParameter ;
516  }
517 
518  Dma_Irq_DeInit(pstcConfig->u8DmaChannel);
519 
520  u32DmaRegisterBaseAddress = GET_DMA_ADDRESS(pstcConfig->u8DmaChannel);
521 
522  // clear all registers of DMA channel
523  *(uint32_t*)((uint32_t)(u32DmaRegisterBaseAddress + DMA_DMACA_OFFSET)) = 0u;
524  *(uint32_t*)((uint32_t)(u32DmaRegisterBaseAddress + DMA_DMACB_OFFSET)) = 0u;
525  *(uint32_t*)((uint32_t)(u32DmaRegisterBaseAddress + DMA_DMASA_OFFSET)) = 0u;
526  *(uint32_t*)((uint32_t)(u32DmaRegisterBaseAddress + DMA_DMADA_OFFSET)) = 0u;
527 
528  // Mark DMA channel as inactive
529  stcDmaInternData.au8DmaChannelActive[pstcConfig->u8DmaChannel]
531 
532  return Ok;
533 } // Dma_DeInit_Channel
534 
554 en_result_t Dma_Set_ChannelParam( uint8_t u8DmaChannel,
555  boolean_t bEnable,
556  boolean_t bPause,
557  boolean_t bSoftwareTrigger
558  )
559 {
560  un_dmac_dmaca_t unDmacDmaca;
561  uint32_t u32RegisterReadOut ;
562  uint32_t u32DmaRegisterBaseAddress ;
563 
564  unDmacDmaca.u32DMACA = 0u;
565  unDmacDmaca.stcDMACA.EB = (uint8_t)((bEnable == TRUE) ? 1u : 0u);
566  unDmacDmaca.stcDMACA.PB = (uint8_t)((bPause == TRUE) ? 1u : 0u);
567  unDmacDmaca.stcDMACA.ST = (uint8_t)((bSoftwareTrigger == TRUE) ? 1u : 0u);
568 
569  u32DmaRegisterBaseAddress = GET_DMA_ADDRESS(u8DmaChannel);
570 
571  // Readout DMACA and update
572  u32RegisterReadOut = *(uint32_t*)((uint32_t)(u32DmaRegisterBaseAddress + DMA_DMACA_OFFSET)) ;
573  unDmacDmaca.u32DMACA |= (u32RegisterReadOut & 0x1FFFFFFFul); // masking EB, PB, ST ...
574  *(uint32_t*)((uint32_t)(u32DmaRegisterBaseAddress + DMA_DMACA_OFFSET)) = unDmacDmaca.u32DMACA ;
575 
576  return Ok;
577 } // Dma_Set_ChannelParam
578 
590 en_result_t Dma_DeInit_ChannelParam(uint8_t u8DmaChannel)
591 {
592  uint32_t u32DmaRegisterBaseAddress;
593 
594  Dma_Irq_DeInit(u8DmaChannel);
595 
596  u32DmaRegisterBaseAddress = GET_DMA_ADDRESS(u8DmaChannel);
597 
598  // clear all registers of DMA channel
599  *(uint32_t*)((uint32_t)(u32DmaRegisterBaseAddress + DMA_DMACA_OFFSET)) = 0ul;
600  *(uint32_t*)((uint32_t)(u32DmaRegisterBaseAddress + DMA_DMACB_OFFSET)) = 0ul;
601  *(uint32_t*)((uint32_t)(u32DmaRegisterBaseAddress + DMA_DMASA_OFFSET)) = 0ul;
602  *(uint32_t*)((uint32_t)(u32DmaRegisterBaseAddress + DMA_DMADA_OFFSET)) = 0ul;
603 
604  // Mark DMA channel as inactive
605  stcDmaInternData.au8DmaChannelActive[u8DmaChannel]
607 
608  return Ok;
609 } // Dma_DeInit_ChannelParam
610 
612 
613 #endif // #if (defined(PDL_PERIPHERAL_DMA_ACTIVE))
614 
615 /******************************************************************************/
616 /* EOF (not truncated) */
617 /******************************************************************************/
618