Teensy 4.1 core updated for C++20
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.

преди 5 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. const uint32_t *p = (const uint32_t *)src;
  350. uint32_t *q = (uint32_t *)dst;
  351. uint32_t t1, t2, t3, t4;
  352. t1 = *p++; t2 = *p++; t3 = *p++; t4 = *p++;
  353. *q++ = t1; *q++ = t2; *q++ = t3; *q++ = t4;
  354. t1 = *p++; t2 = *p++; t3 = *p++; t4 = *p++;
  355. *q++ = t1; *q++ = t2; *q++ = t3; *q++ = t4;
  356. }
  357. };
  358. // DMASetting represents settings stored only in memory, which can be
  359. // applied to any DMA channel.
  360. class DMASetting : public DMABaseClass {
  361. public:
  362. DMASetting() {
  363. TCD = &tcddata;
  364. }
  365. DMASetting(const DMASetting &c) {
  366. TCD = &tcddata;
  367. *this = c;
  368. }
  369. DMASetting(const DMABaseClass &c) {
  370. TCD = &tcddata;
  371. *this = c;
  372. }
  373. DMASetting & operator = (const DMABaseClass &rhs) {
  374. copy_tcd(TCD, rhs.TCD);
  375. return *this;
  376. }
  377. private:
  378. TCD_t tcddata __attribute__((aligned(32)));
  379. };
  380. // DMAChannel reprents an actual DMA channel and its current settings
  381. class DMAChannel : public DMABaseClass {
  382. public:
  383. /*************************************************/
  384. /** Channel Allocation **/
  385. /*************************************************/
  386. DMAChannel() {
  387. begin();
  388. }
  389. DMAChannel(const DMAChannel &c) {
  390. TCD = c.TCD;
  391. channel = c.channel;
  392. }
  393. DMAChannel(const DMASetting &c) {
  394. begin();
  395. copy_tcd(TCD, c.TCD);
  396. }
  397. DMAChannel(bool allocate) {
  398. if (allocate) begin();
  399. }
  400. DMAChannel & operator = (const DMAChannel &rhs) {
  401. if (channel != rhs.channel) {
  402. release();
  403. TCD = rhs.TCD;
  404. channel = rhs.channel;
  405. }
  406. return *this;
  407. }
  408. DMAChannel & operator = (const DMASetting &rhs) {
  409. copy_tcd(TCD, rhs.TCD);
  410. return *this;
  411. }
  412. ~DMAChannel() {
  413. release();
  414. }
  415. void begin(bool force_initialization = false);
  416. private:
  417. void release(void);
  418. public:
  419. /***************************************/
  420. /** Triggering **/
  421. /***************************************/
  422. // Triggers cause the DMA channel to actually move data. Each
  423. // trigger moves a single data unit, which is typically 8, 16 or
  424. // 32 bits. If a channel is configured for 200 transfers
  425. // Use a hardware trigger to make the DMA channel run
  426. void triggerAtHardwareEvent(uint8_t source) {
  427. volatile uint32_t *mux = &DMAMUX_CHCFG0 + channel;
  428. //mux = (volatile uint32_t *)&(DMAMUX_CHCFG0) + channel;
  429. *mux = 0;
  430. *mux = (source & 0x7F) | DMAMUX_CHCFG_ENBL;
  431. }
  432. // Use another DMA channel as the trigger, causing this
  433. // channel to trigger after each transfer is makes, except
  434. // the its last transfer. This effectively makes the 2
  435. // channels run in parallel until the last transfer
  436. void triggerAtTransfersOf(DMABaseClass &ch) {
  437. ch.TCD->BITER = (ch.TCD->BITER & ~DMA_TCD_BITER_ELINKYES_LINKCH_MASK)
  438. | DMA_TCD_BITER_ELINKYES_LINKCH(channel) | DMA_TCD_BITER_ELINKYES_ELINK;
  439. ch.TCD->CITER = ch.TCD->BITER ;
  440. }
  441. // Use another DMA channel as the trigger, causing this
  442. // channel to trigger when the other channel completes.
  443. void triggerAtCompletionOf(DMABaseClass &ch) {
  444. ch.TCD->CSR = (ch.TCD->CSR & ~(DMA_TCD_CSR_MAJORLINKCH_MASK|DMA_TCD_CSR_DONE))
  445. | DMA_TCD_CSR_MAJORLINKCH(channel) | DMA_TCD_CSR_MAJORELINK;
  446. }
  447. // Cause this DMA channel to be continuously triggered, so
  448. // it will move data as rapidly as possible, without waiting.
  449. // Normally this would be used with disableOnCompletion().
  450. void triggerContinuously(void) {
  451. // TODO: update this for IMXRT. On Kinetis, a small handful
  452. // of DMAMUX slots were dedicated to "always on". On IMXRT,
  453. // all of them can work as "always on" by setting their
  454. // DMAMUX_CHCFG_A_ON bit.
  455. #if 0
  456. volatile uint8_t *mux = (volatile uint8_t *)&DMAMUX0_CHCFG0;
  457. mux[channel] = 0;
  458. #if DMAMUX_NUM_SOURCE_ALWAYS >= DMA_NUM_CHANNELS
  459. mux[channel] = DMAMUX_SOURCE_ALWAYS0 + channel;
  460. #else
  461. // search for an unused "always on" source
  462. unsigned int i = DMAMUX_SOURCE_ALWAYS0;
  463. for (i = DMAMUX_SOURCE_ALWAYS0;
  464. i < DMAMUX_SOURCE_ALWAYS0 + DMAMUX_NUM_SOURCE_ALWAYS; i++) {
  465. unsigned int ch;
  466. for (ch=0; ch < DMA_NUM_CHANNELS; ch++) {
  467. if (mux[ch] == i) break;
  468. }
  469. if (ch >= DMA_NUM_CHANNELS) {
  470. mux[channel] = (i | DMAMUX_ENABLE);
  471. return;
  472. }
  473. }
  474. #endif
  475. #endif
  476. }
  477. // Manually trigger the DMA channel.
  478. void triggerManual(void) {
  479. DMA_SSRT = channel;
  480. }
  481. /***************************************/
  482. /** Interrupts **/
  483. /***************************************/
  484. // An interrupt routine can be run when the DMA channel completes
  485. // the entire transfer, and also optionally when half of the
  486. // transfer is completed.
  487. void attachInterrupt(void (*isr)(void)) {
  488. _VectorsRam[channel + IRQ_DMA_CH0 + 16] = isr;
  489. NVIC_ENABLE_IRQ(IRQ_DMA_CH0 + channel);
  490. }
  491. void detachInterrupt(void) {
  492. NVIC_DISABLE_IRQ(IRQ_DMA_CH0 + channel);
  493. }
  494. void clearInterrupt(void) {
  495. DMA_CINT = channel;
  496. }
  497. /***************************************/
  498. /** Enable / Disable **/
  499. /***************************************/
  500. void enable(void) {
  501. DMA_SERQ = channel;
  502. }
  503. void disable(void) {
  504. DMA_CERQ = channel;
  505. }
  506. /***************************************/
  507. /** Status **/
  508. /***************************************/
  509. bool complete(void) {
  510. if (TCD->CSR & DMA_TCD_CSR_DONE) return true;
  511. return false;
  512. }
  513. void clearComplete(void) {
  514. DMA_CDNE = channel;
  515. }
  516. bool error(void) {
  517. if (DMA_ERR & (1<<channel)) return true;
  518. return false;
  519. }
  520. void clearError(void) {
  521. DMA_CERR = channel;
  522. }
  523. void * sourceAddress(void) {
  524. return (void *)(TCD->SADDR);
  525. }
  526. void * destinationAddress(void) {
  527. return (void *)(TCD->DADDR);
  528. }
  529. /***************************************/
  530. /** Direct Hardware Access **/
  531. /***************************************/
  532. // For complex and unusual configurations not possible with the above
  533. // functions, the Transfer Control Descriptor (TCD) and channel number
  534. // can be used directly. This leads to less portable and less readable
  535. // code, but direct control of all parameters is possible.
  536. uint8_t channel;
  537. // TCD is accessible due to inheritance from DMABaseClass
  538. };
  539. // arrange the relative priority of 2 or more DMA channels
  540. void DMAPriorityOrder(DMAChannel &ch1, DMAChannel &ch2);
  541. void DMAPriorityOrder(DMAChannel &ch1, DMAChannel &ch2, DMAChannel &ch3);
  542. void DMAPriorityOrder(DMAChannel &ch1, DMAChannel &ch2, DMAChannel &ch3, DMAChannel &ch4);
  543. #endif // __cplusplus
  544. #endif // DMAChannel_h_