You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 satır
8.3KB

  1. #ifndef DMAChannel_h_
  2. #define DMAChannel_h_
  3. #include "mk20dx128.h"
  4. // This code is a work-in-progress. It's incomplete and not usable yet...
  5. #ifdef __cplusplus
  6. class DMAChannel {
  7. typedef struct __attribute__((packed)) {
  8. volatile const void * volatile SADDR;
  9. int16_t SOFF;
  10. union { uint16_t ATTR; struct { uint8_t ATTR_DST; uint8_t ATTR_SRC; }; };
  11. uint32_t NBYTES;
  12. int32_t SLAST;
  13. volatile void * volatile DADDR;
  14. int16_t DOFF;
  15. volatile uint16_t CITER;
  16. int32_t DLASTSGA;
  17. volatile uint16_t CSR;
  18. volatile uint16_t BITER;
  19. } TCD_t;
  20. public:
  21. // Constructor - allocates which DMA channel each object actually uses
  22. DMAChannel(uint8_t channelRequest=0);
  23. // Triggers cause the DMA channel to actually move data.
  24. //
  25. // Use a hardware trigger to make the DMA channel run
  26. void attachTrigger(uint8_t source) {
  27. volatile uint8_t *mux;
  28. mux = (volatile uint8_t *)&(DMAMUX0_CHCFG0) + channel;
  29. *mux = 0;
  30. *mux = source | DMAMUX_ENABLE;
  31. }
  32. // Use another DMA channel as the trigger, causing this
  33. // channel to trigger every time it triggers. This
  34. // effectively makes the 2 channels run in parallel.
  35. void attachTrigger(DMAChannel &channel) {
  36. }
  37. // Use another DMA channel as the trigger, causing this
  38. // channel to trigger when it completes.
  39. void attachTriggerOnCompletion(DMAChannel &channel) {
  40. }
  41. // An interrupt routine can be run when the DMA channel completes
  42. // the entire transfer.
  43. void attachInterrupt(void (*isr)(void)) {
  44. _VectorsRam[channel + IRQ_DMA_CH0 + 16] = isr;
  45. NVIC_ENABLE_IRQ(IRQ_DMA_CH0 + channel);
  46. }
  47. // Use a single variable as the data source. Typically a register
  48. // for receiving data from one of the hardware peripherals is used.
  49. void source(const signed char &p) { src(&p, 1); }
  50. void source(const unsigned char &p) { src(&p, 1); }
  51. void source(const signed short &p) { src(&p, 2); }
  52. void source(const unsigned short &p) { src(&p, 2); }
  53. void source(const signed int &p) { src(&p, 4); }
  54. void source(const unsigned int &p) { src(&p, 4); }
  55. void source(const signed long &p) { src(&p, 4); }
  56. void source(const unsigned long &p) { src(&p, 4); }
  57. // Use a buffer (array of data) as the data source. Typically a
  58. // buffer for transmitting data is used.
  59. void sourceBuffer(const signed char p[], unsigned int len) { src(p, 1, len); }
  60. void sourceBuffer(const unsigned char p[], unsigned int len) { src(p, 1, len); }
  61. void sourceBuffer(const signed short p[], unsigned int len) { src(p, 2, len); }
  62. void sourceBuffer(const unsigned short p[], unsigned int len) { src(p, 2, len); }
  63. void sourceBuffer(const signed int p[], unsigned int len) { src(p, 4, len); }
  64. void sourceBuffer(const unsigned int p[], unsigned int len) { src(p, 4, len); }
  65. void sourceBuffer(const signed long p[], unsigned int len) { src(p, 4, len); }
  66. void sourceBuffer(const unsigned long p[], unsigned int len) { src(p, 4, len); }
  67. // Use a circular buffer as the data source
  68. void sourceCircular(const signed char p[], unsigned int len) { srcc(p, 1, len); }
  69. void sourceCircular(const unsigned char p[], unsigned int len) { srcc(p, 1, len); }
  70. void sourceCircular(const signed short p[], unsigned int len) { srcc(p, 2, len); }
  71. void sourceCircular(const unsigned short p[], unsigned int len) { srcc(p, 2, len); }
  72. void sourceCircular(const signed int p[], unsigned int len) { srcc(p, 4, len); }
  73. void sourceCircular(const unsigned int p[], unsigned int len) { srcc(p, 4, len); }
  74. void sourceCircular(const signed long p[], unsigned int len) { srcc(p, 4, len); }
  75. void sourceCircular(const unsigned long p[], unsigned int len) { srcc(p, 4, len); }
  76. // Use a single variable as the data destination. Typically a register
  77. // for transmitting data to one of the hardware peripherals is used.
  78. void destination(signed char &p) { src(&p, 1); }
  79. void destination(unsigned char &p) { src(&p, 1); }
  80. void destination(signed short &p) { src(&p, 2); }
  81. void destination(unsigned short &p) { src(&p, 2); }
  82. void destination(signed int &p) { src(&p, 4); }
  83. void destination(unsigned int &p) { src(&p, 4); }
  84. void destination(signed long &p) { src(&p, 4); }
  85. void destination(unsigned long &p) { src(&p, 4); }
  86. // Use a buffer (array of data) as the data destination. Typically a
  87. // buffer for receiving data is used.
  88. void destinationBuffer(signed char p[], unsigned int len) { src(p, 1, len); }
  89. void destinationBuffer(unsigned char p[], unsigned int len) { src(p, 1, len); }
  90. void destinationBuffer(signed short p[], unsigned int len) { src(p, 2, len); }
  91. void destinationBuffer(unsigned short p[], unsigned int len) { src(p, 2, len); }
  92. void destinationBuffer(signed int p[], unsigned int len) { src(p, 4, len); }
  93. void destinationBuffer(unsigned int p[], unsigned int len) { src(p, 4, len); }
  94. void destinationBuffer(signed long p[], unsigned int len) { src(p, 4, len); }
  95. void destinationBuffer(unsigned long p[], unsigned int len) { src(p, 4, len); }
  96. // Use a circular buffer as the data destination
  97. void destinationCircular(signed char p[], unsigned int len) { srcc(p, 1, len); }
  98. void destinationCircular(unsigned char p[], unsigned int len) { srcc(p, 1, len); }
  99. void destinationCircular(signed short p[], unsigned int len) { srcc(p, 2, len); }
  100. void destinationCircular(unsigned short p[], unsigned int len) { srcc(p, 2, len); }
  101. void destinationCircular(signed int p[], unsigned int len) { srcc(p, 4, len); }
  102. void destinationCircular(unsigned int p[], unsigned int len) { srcc(p, 4, len); }
  103. void destinationCircular(signed long p[], unsigned int len) { srcc(p, 4, len); }
  104. void destinationCircular(unsigned long p[], unsigned int len) { srcc(p, 4, len); }
  105. // TODO: explicit function for configuring transfer length....
  106. // should we try to automatically pick it up from the array lengths?
  107. // TODO: functions to configure major/minor loop
  108. // option #1 - trigger moves 1 byte/word (minor=1, major=count)
  109. // option #2 - trigger moves all data (minor=count, major=1)
  110. // option ?? - more complex config, write TCD manually....
  111. // TODO: functions to set other options, functions to enable
  112. // manual start function, etc
  113. // TODO: "get" functions, to read important stuff, like SADDR & DADDR...
  114. // For complex and unusual configurations not possible with the above
  115. // functions, the Transfer Control Descriptor (TCD) and channel number
  116. // can be used directly. This leads to less portable and less readable
  117. // code, but direct control of all parameters is possible.
  118. TCD_t &TCD;
  119. uint8_t channel;
  120. protected:
  121. void src(const void *p, uint32_t size) {
  122. TCD.SADDR = p;
  123. TCD.SOFF = 0;
  124. if (size == 1) {
  125. TCD.ATTR_SRC = 0; // 8 bits
  126. } else if (size == 2) {
  127. TCD.ATTR_SRC = 1; // 16 bits
  128. } else {
  129. TCD.ATTR_SRC = 2; // 32 bits
  130. }
  131. //TCD.NBYTES = size;
  132. TCD.SLAST = 0;
  133. }
  134. void src(const void *p, uint32_t size, uint32_t len) {
  135. TCD.SADDR = p;
  136. TCD.SOFF = size;
  137. if (size == 1) {
  138. TCD.ATTR_SRC = 0; // 8 bits
  139. } else if (size == 2) {
  140. TCD.ATTR_SRC = 1; // 16 bits
  141. } else {
  142. TCD.ATTR_SRC = 2; // 32 bits
  143. }
  144. //TCD.NBYTES = size;
  145. TCD.SLAST = -len;
  146. }
  147. void srcc(const void *p, uint32_t size, uint32_t len) {
  148. TCD.SADDR = p;
  149. TCD.SOFF = size;
  150. if (size == 1) {
  151. TCD.ATTR_SRC = 0 | ((31 - __builtin_clz(len)) << 3); // 8 bits
  152. } else if (size == 2) {
  153. TCD.ATTR_SRC = 1 | ((31 - __builtin_clz(len)) << 3); // 16 bits
  154. } else {
  155. TCD.ATTR_SRC = 2 | ((31 - __builtin_clz(len)) << 3); // 32 bits
  156. }
  157. //TCD.NBYTES = size;
  158. TCD.SLAST = 0;
  159. }
  160. void dst(void *p, uint32_t size) {
  161. TCD.DADDR = p;
  162. TCD.DOFF = 0;
  163. if (size == 1) {
  164. TCD.ATTR_SRC = 0; // 8 bits
  165. } else if (size == 2) {
  166. TCD.ATTR_SRC = 1; // 16 bits
  167. } else {
  168. TCD.ATTR_SRC = 2; // 32 bits
  169. }
  170. //TCD.NBYTES = size;
  171. TCD.DLASTSGA = 0;
  172. }
  173. void dst(void *p, uint32_t size, uint32_t len) {
  174. TCD.DADDR = p;
  175. TCD.DOFF = size;
  176. if (size == 1) {
  177. TCD.ATTR_DST = 0; // 8 bits
  178. } else if (size == 2) {
  179. TCD.ATTR_DST = 1; // 16 bits
  180. } else {
  181. TCD.ATTR_DST = 2; // 32 bits
  182. }
  183. //TCD.NBYTES = size;
  184. TCD.DLASTSGA = -len;
  185. }
  186. void dstc(void *p, uint32_t size, uint32_t len) {
  187. TCD.DADDR = p;
  188. TCD.DOFF = size;
  189. if (size == 1) {
  190. TCD.ATTR_DST = 0 | ((31 - __builtin_clz(len)) << 3); // 8 bits
  191. } else if (size == 2) {
  192. TCD.ATTR_DST = 1 | ((31 - __builtin_clz(len)) << 3); // 16 bits
  193. } else {
  194. TCD.ATTR_DST = 2 | ((31 - __builtin_clz(len)) << 3); // 32 bits
  195. }
  196. //TCD.NBYTES = size;
  197. TCD.DLASTSGA = 0;
  198. }
  199. };
  200. extern "C" {
  201. #endif
  202. extern uint16_t dma_channel_allocated_mask;
  203. #ifdef __cplusplus
  204. }
  205. #endif
  206. #endif