No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

DMAChannel.cpp 4.6KB

hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #include "DMAChannel.h"
  2. #if DMA_NUM_CHANNELS <= 16
  3. #define DMA_MAX_CHANNELS DMA_NUM_CHANNELS
  4. #else
  5. #define DMA_MAX_CHANNELS 16
  6. #endif
  7. // The channel allocation bitmask is accessible from "C" namespace,
  8. // so C-only code can reserve DMA channels
  9. uint16_t dma_channel_allocated_mask = 0;
  10. /****************************************************************/
  11. /** Teensy 3.0 & 3.1 **/
  12. /****************************************************************/
  13. #if defined(KINETISK)
  14. void DMAChannel::begin(bool force_initialization)
  15. {
  16. uint32_t ch = 0;
  17. __disable_irq();
  18. if (!force_initialization && TCD && channel < DMA_MAX_CHANNELS
  19. && (dma_channel_allocated_mask & (1 << channel))
  20. && (uint32_t)TCD == (uint32_t)(0x40009000 + channel * 32)) {
  21. // DMA channel already allocated
  22. __enable_irq();
  23. return;
  24. }
  25. while (1) {
  26. if (!(dma_channel_allocated_mask & (1 << ch))) {
  27. dma_channel_allocated_mask |= (1 << ch);
  28. __enable_irq();
  29. break;
  30. }
  31. if (++ch >= DMA_MAX_CHANNELS) {
  32. __enable_irq();
  33. TCD = (TCD_t *)0;
  34. channel = DMA_MAX_CHANNELS;
  35. return; // no more channels available
  36. // attempts to use this object will hardfault
  37. }
  38. }
  39. channel = ch;
  40. SIM_SCGC7 |= SIM_SCGC7_DMA;
  41. SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
  42. #if DMA_NUM_CHANNELS <= 16
  43. DMA_CR = DMA_CR_EMLM | DMA_CR_EDBG; // minor loop mapping is available
  44. #else
  45. DMA_CR = DMA_CR_GRP1PRI| DMA_CR_EMLM | DMA_CR_EDBG;
  46. #endif
  47. DMA_CERQ = ch;
  48. DMA_CERR = ch;
  49. DMA_CEEI = ch;
  50. DMA_CINT = ch;
  51. TCD = (TCD_t *)(0x40009000 + ch * 32);
  52. uint32_t *p = (uint32_t *)TCD;
  53. *p++ = 0;
  54. *p++ = 0;
  55. *p++ = 0;
  56. *p++ = 0;
  57. *p++ = 0;
  58. *p++ = 0;
  59. *p++ = 0;
  60. *p++ = 0;
  61. }
  62. void DMAChannel::release(void)
  63. {
  64. if (channel >= DMA_MAX_CHANNELS) return;
  65. DMA_CERQ = channel;
  66. __disable_irq();
  67. dma_channel_allocated_mask &= ~(1 << channel);
  68. __enable_irq();
  69. channel = DMA_MAX_CHANNELS;
  70. TCD = (TCD_t *)0;
  71. }
  72. static uint32_t priority(const DMAChannel &c)
  73. {
  74. uint32_t n;
  75. n = *(uint32_t *)((uint32_t)&DMA_DCHPRI3 + (c.channel & 0xFC));
  76. n = __builtin_bswap32(n);
  77. return (n >> ((c.channel & 0x03) << 3)) & 0x0F;
  78. }
  79. static void swap(DMAChannel &c1, DMAChannel &c2)
  80. {
  81. uint8_t c;
  82. DMABaseClass::TCD_t *t;
  83. c = c1.channel;
  84. c1.channel = c2.channel;
  85. c2.channel = c;
  86. t = c1.TCD;
  87. c1.TCD = c2.TCD;
  88. c2.TCD = t;
  89. }
  90. /****************************************************************/
  91. /** Teensy-LC **/
  92. /****************************************************************/
  93. #elif defined(KINETISL)
  94. void DMAChannel::begin(bool force_initialization)
  95. {
  96. uint32_t ch = 0;
  97. __disable_irq();
  98. if (!force_initialization && CFG && channel < DMA_MAX_CHANNELS
  99. && (dma_channel_allocated_mask & (1 << channel))
  100. && (uint32_t)CFG == (uint32_t)(0x40008100 + channel * 16)) {
  101. // DMA channel already allocated
  102. __enable_irq();
  103. return;
  104. }
  105. while (1) {
  106. if (!(dma_channel_allocated_mask & (1 << ch))) {
  107. dma_channel_allocated_mask |= (1 << ch);
  108. __enable_irq();
  109. break;
  110. }
  111. if (++ch >= DMA_MAX_CHANNELS) {
  112. __enable_irq();
  113. CFG = (CFG_t *)0;
  114. channel = DMA_MAX_CHANNELS;
  115. return; // no more channels available
  116. // attempts to use this object will hardfault
  117. }
  118. }
  119. channel = ch;
  120. SIM_SCGC7 |= SIM_SCGC7_DMA;
  121. SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
  122. CFG = (CFG_t *)(0x40008100 + ch * 16);
  123. CFG->DSR_BCR = DMA_DSR_BCR_DONE;
  124. CFG->DCR = DMA_DCR_CS;
  125. CFG->SAR = NULL;
  126. CFG->DAR = NULL;
  127. }
  128. void DMAChannel::release(void)
  129. {
  130. if (channel >= DMA_MAX_CHANNELS) return;
  131. CFG->DSR_BCR = DMA_DSR_BCR_DONE;
  132. __disable_irq();
  133. dma_channel_allocated_mask &= ~(1 << channel);
  134. __enable_irq();
  135. channel = 16;
  136. CFG = (CFG_t *)0;
  137. }
  138. static uint32_t priority(const DMAChannel &c)
  139. {
  140. return 3 - c.channel;
  141. }
  142. static void swap(DMAChannel &c1, DMAChannel &c2)
  143. {
  144. uint8_t c;
  145. DMABaseClass::CFG_t *t;
  146. c = c1.channel;
  147. c1.channel = c2.channel;
  148. c2.channel = c;
  149. t = c1.CFG;
  150. c1.CFG = c2.CFG;
  151. c2.CFG = t;
  152. }
  153. #endif
  154. void DMAPriorityOrder(DMAChannel &ch1, DMAChannel &ch2)
  155. {
  156. if (priority(ch1) < priority(ch2)) swap(ch1, ch2);
  157. }
  158. void DMAPriorityOrder(DMAChannel &ch1, DMAChannel &ch2, DMAChannel &ch3)
  159. {
  160. if (priority(ch2) < priority(ch3)) swap(ch2, ch3);
  161. if (priority(ch1) < priority(ch2)) swap(ch1, ch2);
  162. if (priority(ch2) < priority(ch3)) swap(ch2, ch3);
  163. }
  164. void DMAPriorityOrder(DMAChannel &ch1, DMAChannel &ch2, DMAChannel &ch3, DMAChannel &ch4)
  165. {
  166. if (priority(ch3) < priority(ch4)) swap(ch3, ch4);
  167. if (priority(ch2) < priority(ch3)) swap(ch2, ch3);
  168. if (priority(ch1) < priority(ch2)) swap(ch1, ch2);
  169. if (priority(ch3) < priority(ch4)) swap(ch2, ch3);
  170. if (priority(ch2) < priority(ch3)) swap(ch1, ch2);
  171. if (priority(ch3) < priority(ch4)) swap(ch2, ch3);
  172. }