dma_8xx.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * @brief LPC82x DMA chip driver
  3. *
  4. * @note
  5. * Copyright(C) NXP Semiconductors, 2013
  6. * All rights reserved.
  7. *
  8. * @par
  9. * Software that is described herein is for illustrative purposes only
  10. * which provides customers with programming information regarding the
  11. * LPC products. This software is supplied "AS IS" without any warranties of
  12. * any kind, and NXP Semiconductors and its licensor disclaim any and
  13. * all warranties, express or implied, including all implied warranties of
  14. * merchantability, fitness for a particular purpose and non-infringement of
  15. * intellectual property rights. NXP Semiconductors assumes no responsibility
  16. * or liability for the use of the software, conveys no license or rights under any
  17. * patent, copyright, mask work right, or any other intellectual property rights in
  18. * or to any products. NXP Semiconductors reserves the right to make changes
  19. * in the software without notification. NXP Semiconductors also makes no
  20. * representation or warranty that such application will be suitable for the
  21. * specified use without further testing or modification.
  22. *
  23. * @par
  24. * Permission to use, copy, modify, and distribute this software and its
  25. * documentation is hereby granted, under NXP Semiconductors' and its
  26. * licensor's relevant copyrights in the software, without fee, provided that it
  27. * is used in conjunction with NXP Semiconductors microcontrollers. This
  28. * copyright, permission, and disclaimer notice must appear in all copies of
  29. * this code.
  30. */
  31. #include "chip.h"
  32. /*****************************************************************************
  33. * Private types/enumerations/variables
  34. ****************************************************************************/
  35. /*****************************************************************************
  36. * Public types/enumerations/variables
  37. ****************************************************************************/
  38. /* DMA SRAM table - this can be optionally used with the Chip_DMA_SetSRAMBase()
  39. function if a DMA SRAM table is needed. This table is correctly aligned for
  40. the DMA controller. */
  41. #ifdef __ICCARM__
  42. #define ASTR(str) #str
  43. #define ALIGN(x) _Pragma(ASTR(data_alignment=##x))
  44. #else
  45. #define ALIGN(x) __attribute__((aligned(x)))
  46. #endif
  47. /* Alignment of 512 bytes */
  48. #define DMA_ALIGN ALIGN(512)
  49. DMA_ALIGN DMA_CHDESC_T Chip_DMA_Table[MAX_DMA_CHANNEL];
  50. /*****************************************************************************
  51. * Private functions
  52. ****************************************************************************/
  53. /*****************************************************************************
  54. * Public functions
  55. ****************************************************************************/
  56. void ChipEz_DMA_Init(uint32_t isEnableIRQ)
  57. {
  58. Chip_DMA_Init(LPC_DMA);
  59. Chip_DMA_Enable(LPC_DMA);
  60. Chip_DMA_SetSRAMBase(LPC_DMA, DMA_ADDR(Chip_DMA_Table));
  61. if (isEnableIRQ)
  62. NVIC_EnableIRQ(DMA_IRQn);
  63. }
  64. /**
  65. * Initialize DMA parameters specific to a channel
  66. *
  67. * @param channel
  68. * @param src_address
  69. * @param dst_address
  70. * @param xfr_width
  71. * @param length_bytes
  72. */
  73. void ChipEz_DMA_InitChannel( DMA_CHID_T channel, uint32_t src_address, uint32_t src_increment,
  74. uint32_t dst_address, uint32_t dst_increment, uint32_t xfr_width, uint32_t length_bytes, uint32_t priority)
  75. {
  76. Chip_DMA_EnableChannel(LPC_DMA, channel);
  77. Chip_DMA_EnableIntChannel(LPC_DMA, channel);
  78. Chip_DMA_SetupChannelConfig(LPC_DMA, channel, DMA_CFG_PERIPHREQEN |
  79. DMA_CFG_CHPRIORITY(priority));
  80. if (src_increment != DMA_XFERCFG_SRCINC_0) {
  81. Chip_DMA_Table[channel].source = DMA_ADDR((src_address + length_bytes)
  82. - (1UL << xfr_width));
  83. } else {
  84. Chip_DMA_Table[channel].source = DMA_ADDR(src_address);
  85. }
  86. if (dst_increment != DMA_XFERCFG_DSTINC_0) {
  87. Chip_DMA_Table[channel].dest = DMA_ADDR((dst_address + length_bytes)
  88. - (1UL << xfr_width));
  89. } else {
  90. Chip_DMA_Table[channel].dest = DMA_ADDR(dst_address);
  91. }
  92. Chip_DMA_Table[channel].next = DMA_ADDR(0);
  93. }
  94. /**
  95. * Start the DMA transfer
  96. *
  97. * @param channel
  98. * @param src_increment
  99. * @param dst_increment
  100. * @param xfr_width
  101. * @param length_bytes
  102. */
  103. void ChipEz_DMA_StartTransfer(DMA_CHID_T channel, uint32_t src_increment, uint32_t dst_increment, uint32_t xfr_width, uint32_t length_bytes)
  104. {
  105. uint32_t xfer_count;
  106. /* Calculate transfer_count ( length in terms of transfers) */
  107. xfer_count = (xfr_width == DMA_XFERCFG_WIDTH_8) ? length_bytes :
  108. (xfr_width == DMA_XFERCFG_WIDTH_16) ? (length_bytes >> 1) :
  109. (length_bytes >> 2);
  110. Chip_DMA_SetupChannelTransfer(LPC_DMA, channel,
  111. (DMA_XFERCFG_CFGVALID | DMA_XFERCFG_SETINTA | DMA_XFERCFG_SWTRIG |
  112. xfr_width | src_increment | dst_increment |
  113. DMA_XFERCFG_XFERCOUNT(xfer_count)));
  114. }
  115. bool ChipEzr_DMA_Transfer( DMA_CHID_T channel, uint32_t src_address, uint32_t src_increment,
  116. uint32_t dst_address, uint32_t dst_increment, uint32_t xfr_width, uint32_t length_bytes, uint32_t priority)
  117. {
  118. if ((Chip_DMA_GetBusyChannels(LPC_DMA) & (1 << channel)) != 0)
  119. return FALSE;
  120. ChipEz_DMA_InitChannel(channel, src_address, src_increment, dst_address, dst_increment, xfr_width, length_bytes, priority);
  121. ChipEz_DMA_StartTransfer(channel, src_increment, dst_increment, xfr_width, length_bytes);
  122. return TRUE;
  123. }
  124. void ChipEz_DMA_AbortChannel(DMA_CHID_T ch) {
  125. Chip_DMA_DisableChannel(LPC_DMA, ch);
  126. while ((Chip_DMA_GetBusyChannels(LPC_DMA) & (1 << ch)) != 0) {}
  127. Chip_DMA_AbortChannel(LPC_DMA, ch);
  128. Chip_DMA_ClearErrorIntChannel(LPC_DMA, ch);
  129. }