Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2017 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. #ifndef DMAChannel_h_
  31. #define DMAChannel_h_
  32. #include "imxrt.h"
  33. // Discussion about DMAChannel is here:
  34. // http://forum.pjrc.com/threads/25778-Could-there-be-something-like-an-ISR-template-function/page3
  35. #define DMACHANNEL_HAS_BEGIN
  36. #define DMACHANNEL_HAS_BOOLEAN_CTOR
  37. // The channel allocation bitmask is accessible from "C" namespace,
  38. // so C-only code can reserve DMA channels
  39. #ifdef __cplusplus
  40. extern "C" {
  41. #endif
  42. extern uint16_t dma_channel_allocated_mask;
  43. #ifdef __cplusplus
  44. }
  45. #endif
  46. #ifdef __cplusplus
  47. // known libraries with DMA usage (in need of porting to this new scheme):
  48. //
  49. // https://github.com/PaulStoffregen/Audio
  50. // https://github.com/PaulStoffregen/OctoWS2811
  51. // https://github.com/pedvide/ADC
  52. // https://github.com/duff2013/SerialEvent
  53. // https://github.com/pixelmatix/SmartMatrix
  54. // https://github.com/crteensy/DmaSpi <-- DmaSpi has adopted this scheme
  55. class DMABaseClass {
  56. public:
  57. typedef struct __attribute__((packed, aligned(4))) {
  58. volatile const void * volatile SADDR;
  59. int16_t SOFF;
  60. union { uint16_t ATTR;
  61. struct { uint8_t ATTR_DST; uint8_t ATTR_SRC; }; };
  62. union { uint32_t NBYTES; uint32_t NBYTES_MLNO;
  63. uint32_t NBYTES_MLOFFNO; uint32_t NBYTES_MLOFFYES; };
  64. int32_t SLAST;
  65. volatile void * volatile DADDR;
  66. int16_t DOFF;
  67. union { volatile uint16_t CITER;
  68. volatile uint16_t CITER_ELINKYES; volatile uint16_t CITER_ELINKNO; };
  69. int32_t DLASTSGA;
  70. volatile uint16_t CSR;
  71. union { volatile uint16_t BITER;
  72. volatile uint16_t BITER_ELINKYES; volatile uint16_t BITER_ELINKNO; };
  73. } TCD_t;
  74. TCD_t *TCD;
  75. /***************************************/
  76. /** Data Transfer **/
  77. /***************************************/
  78. // Use a single variable as the data source. Typically a register
  79. // for receiving data from one of the hardware peripherals is used.
  80. void source(volatile const signed char &p) { source(*(volatile const uint8_t *)&p); }
  81. void source(volatile const unsigned char &p) {
  82. TCD->SADDR = &p;
  83. TCD->SOFF = 0;
  84. TCD->ATTR_SRC = 0;
  85. if ((uint32_t)&p < 0x40000000 || TCD->NBYTES == 0) TCD->NBYTES = 1;
  86. TCD->SLAST = 0;
  87. }
  88. void source(volatile const signed short &p) { source(*(volatile const uint16_t *)&p); }
  89. void source(volatile const unsigned short &p) {
  90. TCD->SADDR = &p;
  91. TCD->SOFF = 0;
  92. TCD->ATTR_SRC = 1;
  93. if ((uint32_t)&p < 0x40000000 || TCD->NBYTES == 0) TCD->NBYTES = 2;
  94. TCD->SLAST = 0;
  95. }
  96. void source(volatile const signed int &p) { source(*(volatile const uint32_t *)&p); }
  97. void source(volatile const unsigned int &p) { source(*(volatile const uint32_t *)&p); }
  98. void source(volatile const signed long &p) { source(*(volatile const uint32_t *)&p); }
  99. void source(volatile const unsigned long &p) {
  100. TCD->SADDR = &p;
  101. TCD->SOFF = 0;
  102. TCD->ATTR_SRC = 2;
  103. if ((uint32_t)&p < 0x40000000 || TCD->NBYTES == 0) TCD->NBYTES = 4;
  104. TCD->SLAST = 0;
  105. }
  106. // Use a buffer (array of data) as the data source. Typically a
  107. // buffer for transmitting data is used.
  108. void sourceBuffer(volatile const signed char p[], unsigned int len) {
  109. sourceBuffer((volatile const uint8_t *)p, len); }
  110. void sourceBuffer(volatile const unsigned char p[], unsigned int len) {
  111. TCD->SADDR = p;
  112. TCD->SOFF = 1;
  113. TCD->ATTR_SRC = 0;
  114. TCD->NBYTES = 1;
  115. TCD->SLAST = -len;
  116. TCD->BITER = len;
  117. TCD->CITER = len;
  118. }
  119. void sourceBuffer(volatile const signed short p[], unsigned int len) {
  120. sourceBuffer((volatile const uint16_t *)p, len); }
  121. void sourceBuffer(volatile const unsigned short p[], unsigned int len) {
  122. TCD->SADDR = p;
  123. TCD->SOFF = 2;
  124. TCD->ATTR_SRC = 1;
  125. TCD->NBYTES = 2;
  126. TCD->SLAST = -len;
  127. TCD->BITER = len / 2;
  128. TCD->CITER = len / 2;
  129. }
  130. void sourceBuffer(volatile const signed int p[], unsigned int len) {
  131. sourceBuffer((volatile const uint32_t *)p, len); }
  132. void sourceBuffer(volatile const unsigned int p[], unsigned int len) {
  133. sourceBuffer((volatile const uint32_t *)p, len); }
  134. void sourceBuffer(volatile const signed long p[], unsigned int len) {
  135. sourceBuffer((volatile const uint32_t *)p, len); }
  136. void sourceBuffer(volatile const unsigned long p[], unsigned int len) {
  137. TCD->SADDR = p;
  138. TCD->SOFF = 4;
  139. TCD->ATTR_SRC = 2;
  140. TCD->NBYTES = 4;
  141. TCD->SLAST = -len;
  142. TCD->BITER = len / 4;
  143. TCD->CITER = len / 4;
  144. }
  145. // Use a circular buffer as the data source
  146. void sourceCircular(volatile const signed char p[], unsigned int len) {
  147. sourceCircular((volatile const uint8_t *)p, len); }
  148. void sourceCircular(volatile const unsigned char p[], unsigned int len) {
  149. TCD->SADDR = p;
  150. TCD->SOFF = 1;
  151. TCD->ATTR_SRC = ((31 - __builtin_clz(len)) << 3);
  152. TCD->NBYTES = 1;
  153. TCD->SLAST = 0;
  154. TCD->BITER = len;
  155. TCD->CITER = len;
  156. }
  157. void sourceCircular(volatile const signed short p[], unsigned int len) {
  158. sourceCircular((volatile const uint16_t *)p, len); }
  159. void sourceCircular(volatile const unsigned short p[], unsigned int len) {
  160. TCD->SADDR = p;
  161. TCD->SOFF = 2;
  162. TCD->ATTR_SRC = ((31 - __builtin_clz(len)) << 3) | 1;
  163. TCD->NBYTES = 2;
  164. TCD->SLAST = 0;
  165. TCD->BITER = len / 2;
  166. TCD->CITER = len / 2;
  167. }
  168. void sourceCircular(volatile const signed int p[], unsigned int len) {
  169. sourceCircular((volatile const uint32_t *)p, len); }
  170. void sourceCircular(volatile const unsigned int p[], unsigned int len) {
  171. sourceCircular((volatile const uint32_t *)p, len); }
  172. void sourceCircular(volatile const signed long p[], unsigned int len) {
  173. sourceCircular((volatile const uint32_t *)p, len); }
  174. void sourceCircular(volatile const unsigned long p[], unsigned int len) {
  175. TCD->SADDR = p;
  176. TCD->SOFF = 4;
  177. TCD->ATTR_SRC = ((31 - __builtin_clz(len)) << 3) | 2;
  178. TCD->NBYTES = 4;
  179. TCD->SLAST = 0;
  180. TCD->BITER = len / 4;
  181. TCD->CITER = len / 4;
  182. }
  183. // Use a single variable as the data destination. Typically a register
  184. // for transmitting data to one of the hardware peripherals is used.
  185. void destination(volatile signed char &p) { destination(*(volatile uint8_t *)&p); }
  186. void destination(volatile unsigned char &p) {
  187. TCD->DADDR = &p;
  188. TCD->DOFF = 0;
  189. TCD->ATTR_DST = 0;
  190. if ((uint32_t)&p < 0x40000000 || TCD->NBYTES == 0) TCD->NBYTES = 1;
  191. TCD->DLASTSGA = 0;
  192. }
  193. void destination(volatile signed short &p) { destination(*(volatile uint16_t *)&p); }
  194. void destination(volatile unsigned short &p) {
  195. TCD->DADDR = &p;
  196. TCD->DOFF = 0;
  197. TCD->ATTR_DST = 1;
  198. if ((uint32_t)&p < 0x40000000 || TCD->NBYTES == 0) TCD->NBYTES = 2;
  199. TCD->DLASTSGA = 0;
  200. }
  201. void destination(volatile signed int &p) { destination(*(volatile uint32_t *)&p); }
  202. void destination(volatile unsigned int &p) { destination(*(volatile uint32_t *)&p); }
  203. void destination(volatile signed long &p) { destination(*(volatile uint32_t *)&p); }
  204. void destination(volatile unsigned long &p) {
  205. TCD->DADDR = &p;
  206. TCD->DOFF = 0;
  207. TCD->ATTR_DST = 2;
  208. if ((uint32_t)&p < 0x40000000 || TCD->NBYTES == 0) TCD->NBYTES = 4;
  209. TCD->DLASTSGA = 0;
  210. }
  211. // Use a buffer (array of data) as the data destination. Typically a
  212. // buffer for receiving data is used.
  213. void destinationBuffer(volatile signed char p[], unsigned int len) {
  214. destinationBuffer((volatile uint8_t *)p, len); }
  215. void destinationBuffer(volatile unsigned char p[], unsigned int len) {
  216. TCD->DADDR = p;
  217. TCD->DOFF = 1;
  218. TCD->ATTR_DST = 0;
  219. TCD->NBYTES = 1;
  220. TCD->DLASTSGA = -len;
  221. TCD->BITER = len;
  222. TCD->CITER = len;
  223. }
  224. void destinationBuffer(volatile signed short p[], unsigned int len) {
  225. destinationBuffer((volatile uint16_t *)p, len); }
  226. void destinationBuffer(volatile unsigned short p[], unsigned int len) {
  227. TCD->DADDR = p;
  228. TCD->DOFF = 2;
  229. TCD->ATTR_DST = 1;
  230. TCD->NBYTES = 2;
  231. TCD->DLASTSGA = -len;
  232. TCD->BITER = len / 2;
  233. TCD->CITER = len / 2;
  234. }
  235. void destinationBuffer(volatile signed int p[], unsigned int len) {
  236. destinationBuffer((volatile uint32_t *)p, len); }
  237. void destinationBuffer(volatile unsigned int p[], unsigned int len) {
  238. destinationBuffer((volatile uint32_t *)p, len); }
  239. void destinationBuffer(volatile signed long p[], unsigned int len) {
  240. destinationBuffer((volatile uint32_t *)p, len); }
  241. void destinationBuffer(volatile unsigned long p[], unsigned int len) {
  242. TCD->DADDR = p;
  243. TCD->DOFF = 4;
  244. TCD->ATTR_DST = 2;
  245. TCD->NBYTES = 4;
  246. TCD->DLASTSGA = -len;
  247. TCD->BITER = len / 4;
  248. TCD->CITER = len / 4;
  249. }
  250. // Use a circular buffer as the data destination
  251. void destinationCircular(volatile signed char p[], unsigned int len) {
  252. destinationCircular((volatile uint8_t *)p, len); }
  253. void destinationCircular(volatile unsigned char p[], unsigned int len) {
  254. TCD->DADDR = p;
  255. TCD->DOFF = 1;
  256. TCD->ATTR_DST = ((31 - __builtin_clz(len)) << 3);
  257. TCD->NBYTES = 1;
  258. TCD->DLASTSGA = 0;
  259. TCD->BITER = len;
  260. TCD->CITER = len;
  261. }
  262. void destinationCircular(volatile signed short p[], unsigned int len) {
  263. destinationCircular((volatile uint16_t *)p, len); }
  264. void destinationCircular(volatile unsigned short p[], unsigned int len) {
  265. TCD->DADDR = p;
  266. TCD->DOFF = 2;
  267. TCD->ATTR_DST = ((31 - __builtin_clz(len)) << 3) | 1;
  268. TCD->NBYTES = 2;
  269. TCD->DLASTSGA = 0;
  270. TCD->BITER = len / 2;
  271. TCD->CITER = len / 2;
  272. }
  273. void destinationCircular(volatile signed int p[], unsigned int len) {
  274. destinationCircular((volatile uint32_t *)p, len); }
  275. void destinationCircular(volatile unsigned int p[], unsigned int len) {
  276. destinationCircular((volatile uint32_t *)p, len); }
  277. void destinationCircular(volatile signed long p[], unsigned int len) {
  278. destinationCircular((volatile uint32_t *)p, len); }
  279. void destinationCircular(volatile unsigned long p[], unsigned int len) {
  280. TCD->DADDR = p;
  281. TCD->DOFF = 4;
  282. TCD->ATTR_DST = ((31 - __builtin_clz(len)) << 3) | 2;
  283. TCD->NBYTES = 4;
  284. TCD->DLASTSGA = 0;
  285. TCD->BITER = len / 4;
  286. TCD->CITER = len / 4;
  287. }
  288. /*************************************************/
  289. /** Quantity of Data to Transfer **/
  290. /*************************************************/
  291. // Set the data size used for each triggered transfer
  292. void transferSize(unsigned int len) {
  293. if (len == 16) {
  294. TCD->NBYTES = 16;
  295. if (TCD->SOFF != 0) TCD->SOFF = 16;
  296. if (TCD->DOFF != 0) TCD->DOFF = 16;
  297. TCD->ATTR = (TCD->ATTR & 0xF8F8) | 0x0404;
  298. } else if (len == 4) {
  299. TCD->NBYTES = 4;
  300. if (TCD->SOFF != 0) TCD->SOFF = 4;
  301. if (TCD->DOFF != 0) TCD->DOFF = 4;
  302. TCD->ATTR = (TCD->ATTR & 0xF8F8) | 0x0202;
  303. } else if (len == 2) {
  304. TCD->NBYTES = 2;
  305. if (TCD->SOFF != 0) TCD->SOFF = 2;
  306. if (TCD->DOFF != 0) TCD->DOFF = 2;
  307. TCD->ATTR = (TCD->ATTR & 0xF8F8) | 0x0101;
  308. } else {
  309. TCD->NBYTES = 1;
  310. if (TCD->SOFF != 0) TCD->SOFF = 1;
  311. if (TCD->DOFF != 0) TCD->DOFF = 1;
  312. TCD->ATTR = TCD->ATTR & 0xF8F8;
  313. }
  314. }
  315. // Set the number of transfers (number of triggers until complete)
  316. void transferCount(unsigned int len) {
  317. if (!(TCD->BITER & DMA_TCD_BITER_ELINK)) {
  318. if (len > 32767) return;
  319. TCD->BITER = len;
  320. TCD->CITER = len;
  321. } else {
  322. if (len > 511) return;
  323. TCD->BITER = (TCD->BITER & 0xFE00) | len;
  324. TCD->CITER = (TCD->CITER & 0xFE00) | len;
  325. }
  326. }
  327. /*************************************************/
  328. /** Special Options / Features **/
  329. /*************************************************/
  330. void interruptAtCompletion(void) {
  331. TCD->CSR |= DMA_TCD_CSR_INTMAJOR;
  332. }
  333. void interruptAtHalf(void) {
  334. TCD->CSR |= DMA_TCD_CSR_INTHALF;
  335. }
  336. void disableOnCompletion(void) {
  337. TCD->CSR |= DMA_TCD_CSR_DREQ;
  338. }
  339. void replaceSettingsOnCompletion(const DMABaseClass &settings) {
  340. TCD->DLASTSGA = (int32_t)(settings.TCD);
  341. TCD->CSR &= ~DMA_TCD_CSR_DONE;
  342. TCD->CSR |= DMA_TCD_CSR_ESG;
  343. }
  344. protected:
  345. // users should not be able to create instances of DMABaseClass, which
  346. // require the inheriting class to initialize the TCD pointer.
  347. DMABaseClass() {}
  348. static inline void copy_tcd(TCD_t *dst, const TCD_t *src) {
  349. dst->CSR &= ~DMA_TCD_CSR_DONE;
  350. const uint32_t *p = (const uint32_t *)src;
  351. uint32_t *q = (uint32_t *)dst;
  352. uint32_t t1, t2, t3, t4;
  353. t1 = *p++; t2 = *p++; t3 = *p++; t4 = *p++;
  354. *q++ = t1; *q++ = t2; *q++ = t3; *q++ = t4;
  355. t1 = *p++; t2 = *p++; t3 = *p++; t4 = *p++;
  356. *q++ = t1; *q++ = t2; *q++ = t3; *q++ = t4;
  357. }
  358. };
  359. // DMASetting represents settings stored only in memory, which can be
  360. // applied to any DMA channel.
  361. class DMASetting : public DMABaseClass {
  362. public:
  363. DMASetting() {
  364. TCD = &tcddata;
  365. }
  366. DMASetting(const DMASetting &c) {
  367. TCD = &tcddata;
  368. *this = c;
  369. }
  370. DMASetting(const DMABaseClass &c) {
  371. TCD = &tcddata;
  372. *this = c;
  373. }
  374. DMASetting & operator = (const DMABaseClass &rhs) {
  375. copy_tcd(TCD, rhs.TCD);
  376. return *this;
  377. }
  378. private:
  379. TCD_t tcddata __attribute__((aligned(32)));
  380. };
  381. // DMAChannel reprents an actual DMA channel and its current settings
  382. class DMAChannel : public DMABaseClass {
  383. public:
  384. /*************************************************/
  385. /** Channel Allocation **/
  386. /*************************************************/
  387. DMAChannel() {
  388. begin();
  389. }
  390. DMAChannel(const DMAChannel &c) {
  391. TCD = c.TCD;
  392. channel = c.channel;
  393. }
  394. DMAChannel(const DMASetting &c) {
  395. begin();
  396. copy_tcd(TCD, c.TCD);
  397. }
  398. DMAChannel(bool allocate) {
  399. if (allocate) begin();
  400. }
  401. DMAChannel & operator = (const DMAChannel &rhs) {
  402. if (channel != rhs.channel) {
  403. release();
  404. TCD = rhs.TCD;
  405. channel = rhs.channel;
  406. }
  407. return *this;
  408. }
  409. DMAChannel & operator = (const DMASetting &rhs) {
  410. copy_tcd(TCD, rhs.TCD);
  411. return *this;
  412. }
  413. ~DMAChannel() {
  414. release();
  415. }
  416. void begin(bool force_initialization = false);
  417. private:
  418. void release(void);
  419. public:
  420. /***************************************/
  421. /** Triggering **/
  422. /***************************************/
  423. // Triggers cause the DMA channel to actually move data. Each
  424. // trigger moves a single data unit, which is typically 8, 16 or
  425. // 32 bits. If a channel is configured for 200 transfers
  426. // Use a hardware trigger to make the DMA channel run
  427. void triggerAtHardwareEvent(uint8_t source) {
  428. volatile uint32_t *mux = &DMAMUX_CHCFG0 + channel;
  429. //mux = (volatile uint32_t *)&(DMAMUX_CHCFG0) + channel;
  430. *mux = 0;
  431. *mux = (source & 0x7F) | DMAMUX_CHCFG_ENBL;
  432. }
  433. // Use another DMA channel as the trigger, causing this
  434. // channel to trigger after each transfer is makes, except
  435. // the its last transfer. This effectively makes the 2
  436. // channels run in parallel until the last transfer
  437. void triggerAtTransfersOf(DMABaseClass &ch) {
  438. ch.TCD->BITER = (ch.TCD->BITER & ~DMA_TCD_BITER_ELINKYES_LINKCH_MASK)
  439. | DMA_TCD_BITER_ELINKYES_LINKCH(channel) | DMA_TCD_BITER_ELINKYES_ELINK;
  440. ch.TCD->CITER = ch.TCD->BITER ;
  441. }
  442. // Use another DMA channel as the trigger, causing this
  443. // channel to trigger when the other channel completes.
  444. void triggerAtCompletionOf(DMABaseClass &ch) {
  445. ch.TCD->CSR = (ch.TCD->CSR & ~(DMA_TCD_CSR_MAJORLINKCH_MASK|DMA_TCD_CSR_DONE))
  446. | DMA_TCD_CSR_MAJORLINKCH(channel) | DMA_TCD_CSR_MAJORELINK;
  447. }
  448. // Cause this DMA channel to be continuously triggered, so
  449. // it will move data as rapidly as possible, without waiting.
  450. // Normally this would be used with disableOnCompletion().
  451. void triggerContinuously(void) {
  452. // TODO: update this for IMXRT. On Kinetis, a small handful
  453. // of DMAMUX slots were dedicated to "always on". On IMXRT,
  454. // all of them can work as "always on" by setting their
  455. // DMAMUX_CHCFG_A_ON bit.
  456. #if 0
  457. volatile uint8_t *mux = (volatile uint8_t *)&DMAMUX0_CHCFG0;
  458. mux[channel] = 0;
  459. #if DMAMUX_NUM_SOURCE_ALWAYS >= DMA_NUM_CHANNELS
  460. mux[channel] = DMAMUX_SOURCE_ALWAYS0 + channel;
  461. #else
  462. // search for an unused "always on" source
  463. unsigned int i = DMAMUX_SOURCE_ALWAYS0;
  464. for (i = DMAMUX_SOURCE_ALWAYS0;
  465. i < DMAMUX_SOURCE_ALWAYS0 + DMAMUX_NUM_SOURCE_ALWAYS; i++) {
  466. unsigned int ch;
  467. for (ch=0; ch < DMA_NUM_CHANNELS; ch++) {
  468. if (mux[ch] == i) break;
  469. }
  470. if (ch >= DMA_NUM_CHANNELS) {
  471. mux[channel] = (i | DMAMUX_ENABLE);
  472. return;
  473. }
  474. }
  475. #endif
  476. #endif
  477. }
  478. // Manually trigger the DMA channel.
  479. void triggerManual(void) {
  480. DMA_SSRT = channel;
  481. }
  482. /***************************************/
  483. /** Interrupts **/
  484. /***************************************/
  485. // An interrupt routine can be run when the DMA channel completes
  486. // the entire transfer, and also optionally when half of the
  487. // transfer is completed.
  488. void attachInterrupt(void (*isr)(void)) {
  489. _VectorsRam[channel + IRQ_DMA_CH0 + 16] = isr;
  490. NVIC_ENABLE_IRQ(IRQ_DMA_CH0 + channel);
  491. }
  492. void attachInterrupt(void (*isr)(void), uint8_t prio) {
  493. _VectorsRam[channel + IRQ_DMA_CH0 + 16] = isr;
  494. NVIC_ENABLE_IRQ(IRQ_DMA_CH0 + channel);
  495. NVIC_SET_PRIORITY(IRQ_DMA_CH0 + channel, prio);
  496. }
  497. void detachInterrupt(void) {
  498. NVIC_DISABLE_IRQ(IRQ_DMA_CH0 + channel);
  499. }
  500. void clearInterrupt(void) {
  501. DMA_CINT = channel;
  502. }
  503. /***************************************/
  504. /** Enable / Disable **/
  505. /***************************************/
  506. void enable(void) {
  507. DMA_SERQ = channel;
  508. }
  509. void disable(void) {
  510. DMA_CERQ = channel;
  511. }
  512. /***************************************/
  513. /** Status **/
  514. /***************************************/
  515. bool complete(void) {
  516. if (TCD->CSR & DMA_TCD_CSR_DONE) return true;
  517. return false;
  518. }
  519. void clearComplete(void) {
  520. DMA_CDNE = channel;
  521. }
  522. bool error(void) {
  523. if (DMA_ERR & (1<<channel)) return true;
  524. return false;
  525. }
  526. void clearError(void) {
  527. DMA_CERR = channel;
  528. }
  529. void * sourceAddress(void) {
  530. return (void *)(TCD->SADDR);
  531. }
  532. void * destinationAddress(void) {
  533. return (void *)(TCD->DADDR);
  534. }
  535. /***************************************/
  536. /** Direct Hardware Access **/
  537. /***************************************/
  538. // For complex and unusual configurations not possible with the above
  539. // functions, the Transfer Control Descriptor (TCD) and channel number
  540. // can be used directly. This leads to less portable and less readable
  541. // code, but direct control of all parameters is possible.
  542. uint8_t channel;
  543. // TCD is accessible due to inheritance from DMABaseClass
  544. };
  545. // arrange the relative priority of 2 or more DMA channels
  546. void DMAPriorityOrder(DMAChannel &ch1, DMAChannel &ch2);
  547. void DMAPriorityOrder(DMAChannel &ch1, DMAChannel &ch2, DMAChannel &ch3);
  548. void DMAPriorityOrder(DMAChannel &ch1, DMAChannel &ch2, DMAChannel &ch3, DMAChannel &ch4);
  549. #endif // __cplusplus
  550. #endif // DMAChannel_h_