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.

811 lines
28KB

  1. #ifndef DMAChannel_h_
  2. #define DMAChannel_h_
  3. #include "kinetis.h"
  4. // This code is a work-in-progress. It's incomplete and not usable yet...
  5. //
  6. // http://forum.pjrc.com/threads/25778-Could-there-be-something-like-an-ISR-template-function/page3
  7. // known libraries with DMA usage (in need of porting to this new scheme):
  8. //
  9. // https://github.com/PaulStoffregen/Audio
  10. // https://github.com/PaulStoffregen/OctoWS2811
  11. // https://github.com/pedvide/ADC
  12. // https://github.com/duff2013/SerialEvent
  13. // https://github.com/pixelmatix/SmartMatrix
  14. // https://github.com/crteensy/DmaSpi
  15. #ifdef __cplusplus
  16. class DMABaseClass {
  17. public:
  18. typedef struct __attribute__((packed)) {
  19. volatile const void * volatile SADDR;
  20. int16_t SOFF;
  21. union { uint16_t ATTR;
  22. struct { uint8_t ATTR_DST; uint8_t ATTR_SRC; }; };
  23. union { uint32_t NBYTES; uint32_t NBYTES_MLNO;
  24. uint32_t NBYTES_MLOFFNO; uint32_t NBYTES_MLOFFYES; };
  25. int32_t SLAST;
  26. volatile void * volatile DADDR;
  27. int16_t DOFF;
  28. union { volatile uint16_t CITER;
  29. volatile uint16_t CITER_ELINKYES; volatile uint16_t CITER_ELINKNO; };
  30. int32_t DLASTSGA;
  31. volatile uint16_t CSR;
  32. union { volatile uint16_t BITER;
  33. volatile uint16_t BITER_ELINKYES; volatile uint16_t BITER_ELINKNO; };
  34. } TCD_t;
  35. TCD_t *TCD;
  36. /***************************************/
  37. /** Data Transfer **/
  38. /***************************************/
  39. // Use a single variable as the data source. Typically a register
  40. // for receiving data from one of the hardware peripherals is used.
  41. void source(volatile const signed char &p) { source(*(volatile const uint8_t *)&p); }
  42. void source(volatile const unsigned char &p) {
  43. TCD->SADDR = &p;
  44. TCD->SOFF = 0;
  45. TCD->ATTR_SRC = 0;
  46. if ((uint32_t)p < 0x40000000 || TCD->NBYTES == 0) TCD->NBYTES = 1;
  47. TCD->SLAST = 0;
  48. }
  49. void source(volatile const signed short &p) { source(*(volatile const uint16_t *)&p); }
  50. void source(volatile const unsigned short &p) {
  51. TCD->SADDR = &p;
  52. TCD->SOFF = 0;
  53. TCD->ATTR_SRC = 1;
  54. if ((uint32_t)p < 0x40000000 || TCD->NBYTES == 0) TCD->NBYTES = 2;
  55. TCD->SLAST = 0;
  56. }
  57. void source(volatile const signed int &p) { source(*(volatile const uint32_t *)&p); }
  58. void source(volatile const unsigned int &p) { source(*(volatile const uint32_t *)&p); }
  59. void source(volatile const signed long &p) { source(*(volatile const uint32_t *)&p); }
  60. void source(volatile const unsigned long &p) {
  61. TCD->SADDR = &p;
  62. TCD->SOFF = 0;
  63. TCD->ATTR_SRC = 2;
  64. if ((uint32_t)p < 0x40000000 || TCD->NBYTES == 0) TCD->NBYTES = 4;
  65. TCD->SLAST = 0;
  66. }
  67. // Use a buffer (array of data) as the data source. Typically a
  68. // buffer for transmitting data is used.
  69. void sourceBuffer(volatile const signed char p[], unsigned int len) {
  70. sourceBuffer((volatile const uint8_t *)p, len); }
  71. void sourceBuffer(volatile const unsigned char p[], unsigned int len) {
  72. TCD->SADDR = p;
  73. TCD->SOFF = 1;
  74. TCD->ATTR_SRC = 0;
  75. TCD->NBYTES = 1;
  76. TCD->SLAST = -len;
  77. TCD->BITER = len;
  78. TCD->CITER = len;
  79. }
  80. void sourceBuffer(volatile const signed short p[], unsigned int len) {
  81. sourceBuffer((volatile const uint16_t *)p, len); }
  82. void sourceBuffer(volatile const unsigned short p[], unsigned int len) {
  83. TCD->SADDR = p;
  84. TCD->SOFF = 2;
  85. TCD->ATTR_SRC = 1;
  86. TCD->NBYTES = 2;
  87. TCD->SLAST = -len;
  88. TCD->BITER = len / 2;
  89. TCD->CITER = len / 2;
  90. }
  91. void sourceBuffer(volatile const signed int p[], unsigned int len) {
  92. sourceBuffer((volatile const uint32_t *)p, len); }
  93. void sourceBuffer(volatile const unsigned int p[], unsigned int len) {
  94. sourceBuffer((volatile const uint32_t *)p, len); }
  95. void sourceBuffer(volatile const signed long p[], unsigned int len) {
  96. sourceBuffer((volatile const uint32_t *)p, len); }
  97. void sourceBuffer(volatile const unsigned long p[], unsigned int len) {
  98. TCD->SADDR = p;
  99. TCD->SOFF = 4;
  100. TCD->ATTR_SRC = 2;
  101. TCD->NBYTES = 4;
  102. TCD->SLAST = -len;
  103. TCD->BITER = len / 4;
  104. TCD->CITER = len / 4;
  105. }
  106. // Use a circular buffer as the data source
  107. void sourceCircular(volatile const signed char p[], unsigned int len) {
  108. sourceCircular((volatile const uint8_t *)p, len); }
  109. void sourceCircular(volatile const unsigned char p[], unsigned int len) {
  110. TCD->SADDR = p;
  111. TCD->SOFF = 1;
  112. TCD->ATTR_SRC = ((31 - __builtin_clz(len)) << 3);
  113. TCD->NBYTES = 1;
  114. TCD->SLAST = 0;
  115. TCD->BITER = len;
  116. TCD->CITER = len;
  117. }
  118. void sourceCircular(volatile const signed short p[], unsigned int len) {
  119. sourceCircular((volatile const uint16_t *)p, len); }
  120. void sourceCircular(volatile const unsigned short p[], unsigned int len) {
  121. TCD->SADDR = p;
  122. TCD->SOFF = 2;
  123. TCD->ATTR_SRC = ((31 - __builtin_clz(len)) << 3) | 1;
  124. TCD->NBYTES = 2;
  125. TCD->SLAST = 0;
  126. TCD->BITER = len / 2;
  127. TCD->CITER = len / 2;
  128. }
  129. void sourceCircular(volatile const signed int p[], unsigned int len) {
  130. sourceCircular((volatile const uint32_t *)p, len); }
  131. void sourceCircular(volatile const unsigned int p[], unsigned int len) {
  132. sourceCircular((volatile const uint32_t *)p, len); }
  133. void sourceCircular(volatile const signed long p[], unsigned int len) {
  134. sourceCircular((volatile const uint32_t *)p, len); }
  135. void sourceCircular(volatile const unsigned long p[], unsigned int len) {
  136. TCD->SADDR = p;
  137. TCD->SOFF = 4;
  138. TCD->ATTR_SRC = ((31 - __builtin_clz(len)) << 3) | 2;
  139. TCD->NBYTES = 4;
  140. TCD->SLAST = 0;
  141. TCD->BITER = len / 4;
  142. TCD->CITER = len / 4;
  143. }
  144. // Use a single variable as the data destination. Typically a register
  145. // for transmitting data to one of the hardware peripherals is used.
  146. void destination(volatile signed char &p) { destination(*(volatile uint8_t *)&p); }
  147. void destination(volatile unsigned char &p) {
  148. TCD->DADDR = &p;
  149. TCD->DOFF = 0;
  150. TCD->ATTR_DST = 0;
  151. if ((uint32_t)p < 0x40000000 || TCD->NBYTES == 0) TCD->NBYTES = 1;
  152. TCD->DLASTSGA = 0;
  153. }
  154. void destination(volatile signed short &p) { destination(*(volatile uint16_t *)&p); }
  155. void destination(volatile unsigned short &p) {
  156. TCD->DADDR = &p;
  157. TCD->DOFF = 0;
  158. TCD->ATTR_DST = 1;
  159. if ((uint32_t)p < 0x40000000 || TCD->NBYTES == 0) TCD->NBYTES = 2;
  160. TCD->DLASTSGA = 0;
  161. }
  162. void destination(volatile signed int &p) { destination(*(volatile uint32_t *)&p); }
  163. void destination(volatile unsigned int &p) { destination(*(volatile uint32_t *)&p); }
  164. void destination(volatile signed long &p) { destination(*(volatile uint32_t *)&p); }
  165. void destination(volatile unsigned long &p) {
  166. TCD->DADDR = &p;
  167. TCD->DOFF = 0;
  168. TCD->ATTR_DST = 2;
  169. if ((uint32_t)p < 0x40000000 || TCD->NBYTES == 0) TCD->NBYTES = 4;
  170. TCD->DLASTSGA = 0;
  171. }
  172. // Use a buffer (array of data) as the data destination. Typically a
  173. // buffer for receiving data is used.
  174. void destinationBuffer(volatile signed char p[], unsigned int len) {
  175. destinationBuffer((volatile uint8_t *)p, len); }
  176. void destinationBuffer(volatile unsigned char p[], unsigned int len) {
  177. TCD->DADDR = p;
  178. TCD->DOFF = 1;
  179. TCD->ATTR_DST = 0;
  180. TCD->NBYTES = 1;
  181. TCD->DLASTSGA = -len;
  182. TCD->BITER = len;
  183. TCD->CITER = len;
  184. }
  185. void destinationBuffer(volatile signed short p[], unsigned int len) {
  186. destinationBuffer((volatile uint16_t *)p, len); }
  187. void destinationBuffer(volatile unsigned short p[], unsigned int len) {
  188. TCD->DADDR = p;
  189. TCD->DOFF = 2;
  190. TCD->ATTR_DST = 1;
  191. TCD->NBYTES = 2;
  192. TCD->DLASTSGA = -len;
  193. TCD->BITER = len / 2;
  194. TCD->CITER = len / 2;
  195. }
  196. void destinationBuffer(volatile signed int p[], unsigned int len) {
  197. destinationBuffer((volatile uint32_t *)p, len); }
  198. void destinationBuffer(volatile unsigned int p[], unsigned int len) {
  199. destinationBuffer((volatile uint32_t *)p, len); }
  200. void destinationBuffer(volatile signed long p[], unsigned int len) {
  201. destinationBuffer((volatile uint32_t *)p, len); }
  202. void destinationBuffer(volatile unsigned long p[], unsigned int len) {
  203. TCD->DADDR = p;
  204. TCD->DOFF = 4;
  205. TCD->ATTR_DST = 2;
  206. TCD->NBYTES = 4;
  207. TCD->DLASTSGA = -len;
  208. TCD->BITER = len / 4;
  209. TCD->CITER = len / 4;
  210. }
  211. // Use a circular buffer as the data destination
  212. void destinationCircular(volatile signed char p[], unsigned int len) {
  213. destinationCircular((volatile uint8_t *)p, len); }
  214. void destinationCircular(volatile unsigned char p[], unsigned int len) {
  215. TCD->DADDR = p;
  216. TCD->DOFF = 1;
  217. TCD->ATTR_DST = ((31 - __builtin_clz(len)) << 3);
  218. TCD->NBYTES = 1;
  219. TCD->DLASTSGA = 0;
  220. TCD->BITER = len;
  221. TCD->CITER = len;
  222. }
  223. void destinationCircular(volatile signed short p[], unsigned int len) {
  224. destinationCircular((volatile uint16_t *)p, len); }
  225. void destinationCircular(volatile unsigned short p[], unsigned int len) {
  226. TCD->DADDR = p;
  227. TCD->DOFF = 2;
  228. TCD->ATTR_DST = ((31 - __builtin_clz(len)) << 3) | 1;
  229. TCD->NBYTES = 2;
  230. TCD->DLASTSGA = 0;
  231. TCD->BITER = len / 2;
  232. TCD->CITER = len / 2;
  233. }
  234. void destinationCircular(volatile signed int p[], unsigned int len) {
  235. destinationCircular((volatile uint32_t *)p, len); }
  236. void destinationCircular(volatile unsigned int p[], unsigned int len) {
  237. destinationCircular((volatile uint32_t *)p, len); }
  238. void destinationCircular(volatile signed long p[], unsigned int len) {
  239. destinationCircular((volatile uint32_t *)p, len); }
  240. void destinationCircular(volatile unsigned long p[], unsigned int len) {
  241. TCD->DADDR = p;
  242. TCD->DOFF = 4;
  243. TCD->ATTR_DST = ((31 - __builtin_clz(len)) << 3) | 2;
  244. TCD->NBYTES = 4;
  245. TCD->DLASTSGA = 0;
  246. TCD->BITER = len / 4;
  247. TCD->CITER = len / 4;
  248. }
  249. /*************************************************/
  250. /** Quantity of Data to Transfer **/
  251. /*************************************************/
  252. // Set the data size used for each triggered transfer
  253. void transferSize(unsigned int len) {
  254. if (len == 4) {
  255. TCD->NBYTES = 4;
  256. if (TCD->SOFF != 0) TCD->SOFF = 4;
  257. if (TCD->DOFF != 0) TCD->DOFF = 4;
  258. TCD->ATTR = (TCD->ATTR & 0xF8F8) | 0x0202;
  259. } else if (len == 2) {
  260. TCD->NBYTES = 2;
  261. if (TCD->SOFF != 0) TCD->SOFF = 2;
  262. if (TCD->DOFF != 0) TCD->DOFF = 2;
  263. TCD->ATTR = (TCD->ATTR & 0xF8F8) | 0x0101;
  264. } else {
  265. TCD->NBYTES = 1;
  266. if (TCD->SOFF != 0) TCD->SOFF = 1;
  267. if (TCD->DOFF != 0) TCD->DOFF = 1;
  268. TCD->ATTR = TCD->ATTR & 0xF8F8;
  269. }
  270. }
  271. // Set the number of transfers (number of triggers until complete)
  272. void transferCount(unsigned int len) {
  273. if (len > 32767) return;
  274. if (len >= 512) {
  275. TCD->BITER = len;
  276. TCD->CITER = len;
  277. } else {
  278. TCD->BITER = (TCD->BITER & 0xFE00) | len;
  279. TCD->CITER = (TCD->CITER & 0xFE00) | len;
  280. }
  281. }
  282. /*************************************************/
  283. /** Special Options / Features **/
  284. /*************************************************/
  285. void interruptAtCompletion(void) {
  286. TCD->CSR |= DMA_TCD_CSR_INTMAJOR;
  287. }
  288. void interruptAtHalf(void) {
  289. TCD->CSR |= DMA_TCD_CSR_INTHALF;
  290. }
  291. void disableOnCompletion(void) {
  292. TCD->CSR |= DMA_TCD_CSR_DREQ;
  293. }
  294. void replaceSettingsOnCompletion(const DMABaseClass &settings) {
  295. TCD->DLASTSGA = (int32_t)(settings.TCD);
  296. TCD->CSR &= ~DMA_TCD_CSR_DONE;
  297. TCD->CSR |= DMA_TCD_CSR_ESG;
  298. }
  299. protected:
  300. // users should not be able to create instances of DMABaseClass, which
  301. // require the inheriting class to initialize the TCD pointer.
  302. DMABaseClass() {}
  303. static inline void copy_tcd(TCD_t *dst, const TCD_t *src) {
  304. const uint32_t *p = (const uint32_t *)src;
  305. uint32_t *q = (uint32_t *)dst;
  306. uint32_t t1, t2, t3, t4;
  307. t1 = *p++; t2 = *p++; t3 = *p++; t4 = *p++;
  308. *q++ = t1; *q++ = t2; *q++ = t3; *q++ = t4;
  309. t1 = *p++; t2 = *p++; t3 = *p++; t4 = *p++;
  310. *q++ = t1; *q++ = t2; *q++ = t3; *q++ = t4;
  311. }
  312. };
  313. // DMASetting represents settings stored only in memory, which can be
  314. // applied to any DMA channel.
  315. class DMASetting : public DMABaseClass {
  316. public:
  317. DMASetting() {
  318. TCD = &tcddata;
  319. }
  320. DMASetting(const DMASetting &c) {
  321. TCD = &tcddata;
  322. *this = c;
  323. }
  324. DMASetting(const DMABaseClass &c) {
  325. TCD = &tcddata;
  326. *this = c;
  327. }
  328. DMASetting & operator = (const DMABaseClass &rhs) {
  329. copy_tcd(TCD, rhs.TCD);
  330. return *this;
  331. }
  332. private:
  333. TCD_t tcddata __attribute__((aligned(32)));
  334. };
  335. // DMAChannel reprents an actual DMA channel and its current settings
  336. class DMAChannel : public DMABaseClass {
  337. public:
  338. /*************************************************/
  339. /** Channel Allocation **/
  340. /*************************************************/
  341. DMAChannel() {
  342. init();
  343. }
  344. DMAChannel(const DMAChannel &c) {
  345. TCD = c.TCD;
  346. channel = c.channel;
  347. }
  348. DMAChannel(const DMASetting &c) {
  349. init();
  350. copy_tcd(TCD, c.TCD);
  351. }
  352. DMAChannel & operator = (const DMAChannel &rhs) {
  353. if (channel != rhs.channel) {
  354. release();
  355. TCD = rhs.TCD;
  356. channel = rhs.channel;
  357. }
  358. return *this;
  359. }
  360. DMAChannel & operator = (const DMASetting &rhs) {
  361. copy_tcd(TCD, rhs.TCD);
  362. return *this;
  363. }
  364. ~DMAChannel() {
  365. release();
  366. }
  367. private:
  368. void init(void);
  369. void release(void);
  370. public:
  371. /***************************************/
  372. /** Triggering **/
  373. /***************************************/
  374. // Triggers cause the DMA channel to actually move data. Each
  375. // trigger moves a single data unit, which is typically 8, 16 or
  376. // 32 bits. If a channel is configured for 200 transfers
  377. // Use a hardware trigger to make the DMA channel run
  378. void triggerAtHardwareEvent(uint8_t source) {
  379. volatile uint8_t *mux;
  380. mux = (volatile uint8_t *)&(DMAMUX0_CHCFG0) + channel;
  381. *mux = 0;
  382. *mux = (source & 63) | DMAMUX_ENABLE;
  383. }
  384. // Use another DMA channel as the trigger, causing this
  385. // channel to trigger after each transfer is makes, except
  386. // the its last transfer. This effectively makes the 2
  387. // channels run in parallel until the last transfer
  388. void triggerAtTransfersOf(DMABaseClass &ch) {
  389. ch.TCD->BITER = (ch.TCD->BITER & ~DMA_TCD_BITER_ELINKYES_LINKCH_MASK)
  390. | DMA_TCD_BITER_ELINKYES_LINKCH(channel) | DMA_TCD_BITER_ELINKYES_ELINK;
  391. ch.TCD->CITER = ch.TCD->BITER ;
  392. }
  393. // Use another DMA channel as the trigger, causing this
  394. // channel to trigger when the other channel completes.
  395. void triggerAtCompletionOf(DMABaseClass &ch) {
  396. ch.TCD->CSR = (ch.TCD->CSR & ~(DMA_TCD_CSR_MAJORLINKCH_MASK|DMA_TCD_CSR_DONE))
  397. | DMA_TCD_CSR_MAJORLINKCH(channel) | DMA_TCD_CSR_MAJORELINK;
  398. }
  399. // Cause this DMA channel to be continuously triggered, so
  400. // it will move data as rapidly as possible, without waiting.
  401. // Normally this would be used with disableOnCompletion().
  402. void triggerContinuously(void) {
  403. volatile uint8_t *mux = (volatile uint8_t *)&DMAMUX0_CHCFG0;
  404. mux[channel] = 0;
  405. #if DMAMUX_NUM_SOURCE_ALWAYS >= DMA_NUM_CHANNELS
  406. mux[channel] = DMAMUX_SOURCE_ALWAYS0 + channel;
  407. #else
  408. // search for an unused "always on" source
  409. unsigned int i = DMAMUX_SOURCE_ALWAYS0;
  410. for (i = DMAMUX_SOURCE_ALWAYS0;
  411. i < DMAMUX_SOURCE_ALWAYS0 + DMAMUX_NUM_SOURCE_ALWAYS; i++) {
  412. unsigned int ch;
  413. for (ch=0; ch < DMA_NUM_CHANNELS; ch++) {
  414. if (mux[ch] == i) break;
  415. }
  416. if (ch >= DMA_NUM_CHANNELS) {
  417. mux[channel] = (i | DMAMUX_ENABLE);
  418. return;
  419. }
  420. }
  421. #endif
  422. }
  423. // Manually trigger the DMA channel.
  424. void triggerManual(void) {
  425. DMA_SSRT = channel;
  426. }
  427. /***************************************/
  428. /** Interrupts **/
  429. /***************************************/
  430. // An interrupt routine can be run when the DMA channel completes
  431. // the entire transfer, and also optionally when half of the
  432. // transfer is completed.
  433. void attachInterrupt(void (*isr)(void)) {
  434. _VectorsRam[channel + IRQ_DMA_CH0 + 16] = isr;
  435. NVIC_ENABLE_IRQ(IRQ_DMA_CH0 + channel);
  436. }
  437. void detachInterrupt(void) {
  438. NVIC_DISABLE_IRQ(IRQ_DMA_CH0 + channel);
  439. }
  440. void clearInterrupt(void) {
  441. DMA_CINT = channel;
  442. }
  443. /***************************************/
  444. /** Enable / Disable **/
  445. /***************************************/
  446. void enable(void) {
  447. DMA_SERQ = channel;
  448. }
  449. void disable(void) {
  450. DMA_CERQ = channel;
  451. }
  452. /***************************************/
  453. /** Status **/
  454. /***************************************/
  455. bool complete(void) {
  456. if (TCD->CSR & DMA_TCD_CSR_DONE) return true;
  457. return false;
  458. }
  459. void clearComplete(void) {
  460. DMA_CDNE = channel;
  461. }
  462. bool error(void) {
  463. if (DMA_ERR & (1<<channel)) return true;
  464. return false;
  465. }
  466. void clearError(void) {
  467. DMA_CERR = channel;
  468. }
  469. void * sourceAddress(void) {
  470. return (void *)(TCD->SADDR);
  471. }
  472. void * destinationAddress(void) {
  473. return (void *)(TCD->DADDR);
  474. }
  475. /***************************************/
  476. /** Direct Hardware Access **/
  477. /***************************************/
  478. // For complex and unusual configurations not possible with the above
  479. // functions, the Transfer Control Descriptor (TCD) and channel number
  480. // can be used directly. This leads to less portable and less readable
  481. // code, but direct control of all parameters is possible.
  482. uint8_t channel;
  483. // TCD is accessible due to inheritance from DMABaseClass
  484. /* usage cases:
  485. ************************
  486. OctoWS2811:
  487. ************************
  488. // enable clocks to the DMA controller and DMAMUX
  489. SIM_SCGC7 |= SIM_SCGC7_DMA;
  490. SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
  491. DMA_CR = 0;
  492. DMA_CERQ = 1;
  493. DMA_CERQ = 2;
  494. DMA_CERQ = 3;
  495. // DMA channel #1 sets WS2811 high at the beginning of each cycle
  496. DMA_TCD1_SADDR = &ones;
  497. DMA_TCD1_SOFF = 0;
  498. DMA_TCD1_ATTR = DMA_TCD_ATTR_SSIZE(0) | DMA_TCD_ATTR_DSIZE(0);
  499. DMA_TCD1_NBYTES_MLNO = 1;
  500. DMA_TCD1_SLAST = 0;
  501. DMA_TCD1_DADDR = &GPIOD_PSOR;
  502. DMA_TCD1_DOFF = 0;
  503. DMA_TCD1_CITER_ELINKNO = bufsize;
  504. DMA_TCD1_DLASTSGA = 0;
  505. DMA_TCD1_CSR = DMA_TCD_CSR_DREQ;
  506. DMA_TCD1_BITER_ELINKNO = bufsize;
  507. dma1.source(ones);
  508. dma1.destination(GPIOD_PSOR);
  509. dma1.size(1);
  510. dma1.count(bufsize);
  511. dma1.disableOnCompletion();
  512. // DMA channel #2 writes the pixel data at 20% of the cycle
  513. DMA_TCD2_SADDR = frameBuffer;
  514. DMA_TCD2_SOFF = 1;
  515. DMA_TCD2_ATTR = DMA_TCD_ATTR_SSIZE(0) | DMA_TCD_ATTR_DSIZE(0);
  516. DMA_TCD2_NBYTES_MLNO = 1;
  517. DMA_TCD2_SLAST = -bufsize;
  518. DMA_TCD2_DADDR = &GPIOD_PDOR;
  519. DMA_TCD2_DOFF = 0;
  520. DMA_TCD2_CITER_ELINKNO = bufsize;
  521. DMA_TCD2_DLASTSGA = 0;
  522. DMA_TCD2_CSR = DMA_TCD_CSR_DREQ;
  523. DMA_TCD2_BITER_ELINKNO = bufsize;
  524. dma2.source(frameBuffer, sizeof(frameBuffer));
  525. dma2.destination(GPIOD_PDOR);
  526. dma2.size(1);
  527. dma2.count(bufsize);
  528. dma2.disableOnCompletion();
  529. // DMA channel #3 clear all the pins low at 48% of the cycle
  530. DMA_TCD3_SADDR = &ones;
  531. DMA_TCD3_SOFF = 0;
  532. DMA_TCD3_ATTR = DMA_TCD_ATTR_SSIZE(0) | DMA_TCD_ATTR_DSIZE(0);
  533. DMA_TCD3_NBYTES_MLNO = 1;
  534. DMA_TCD3_SLAST = 0;
  535. DMA_TCD3_DADDR = &GPIOD_PCOR;
  536. DMA_TCD3_DOFF = 0;
  537. DMA_TCD3_CITER_ELINKNO = bufsize;
  538. DMA_TCD3_DLASTSGA = 0;
  539. DMA_TCD3_CSR = DMA_TCD_CSR_DREQ | DMA_TCD_CSR_INTMAJOR;
  540. DMA_TCD3_BITER_ELINKNO = bufsize;
  541. dma3.source(ones);
  542. dma3.destination(GPIOD_PCOR);
  543. dma3.size(1);
  544. dma3.count(bufsize);
  545. dma3.disableOnCompletion();
  546. ************************
  547. Audio, DAC
  548. ************************
  549. DMA_CR = 0;
  550. DMA_TCD4_SADDR = dac_buffer;
  551. DMA_TCD4_SOFF = 2;
  552. DMA_TCD4_ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  553. DMA_TCD4_NBYTES_MLNO = 2;
  554. DMA_TCD4_SLAST = -sizeof(dac_buffer);
  555. DMA_TCD4_DADDR = &DAC0_DAT0L;
  556. DMA_TCD4_DOFF = 0;
  557. DMA_TCD4_CITER_ELINKNO = sizeof(dac_buffer) / 2;
  558. DMA_TCD4_DLASTSGA = 0;
  559. DMA_TCD4_BITER_ELINKNO = sizeof(dac_buffer) / 2;
  560. DMA_TCD4_CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  561. DMAMUX0_CHCFG4 = DMAMUX_DISABLE;
  562. DMAMUX0_CHCFG4 = DMAMUX_SOURCE_PDB | DMAMUX_ENABLE;
  563. ************************
  564. Audio, I2S
  565. ************************
  566. DMA_CR = 0;
  567. DMA_TCD0_SADDR = i2s_tx_buffer;
  568. DMA_TCD0_SOFF = 2;
  569. DMA_TCD0_ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  570. DMA_TCD0_NBYTES_MLNO = 2;
  571. DMA_TCD0_SLAST = -sizeof(i2s_tx_buffer);
  572. DMA_TCD0_DADDR = &I2S0_TDR0;
  573. DMA_TCD0_DOFF = 0;
  574. DMA_TCD0_CITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  575. DMA_TCD0_DLASTSGA = 0;
  576. DMA_TCD0_BITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  577. DMA_TCD0_CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  578. DMAMUX0_CHCFG0 = DMAMUX_DISABLE;
  579. DMAMUX0_CHCFG0 = DMAMUX_SOURCE_I2S0_TX | DMAMUX_ENABLE;
  580. ************************
  581. ADC lib, Pedro Villanueva
  582. ************************
  583. DMA_CR = 0; // normal mode of operation
  584. *DMAMUX0_CHCFG = DMAMUX_DISABLE; // disable before changing
  585. *DMA_TCD_ATTR = DMA_TCD_ATTR_SSIZE(DMA_TCD_ATTR_SIZE_16BIT) |
  586. DMA_TCD_ATTR_DSIZE(DMA_TCD_ATTR_SIZE_16BIT) |
  587. DMA_TCD_ATTR_DMOD(4); // src and dst data is 16 bit (2 bytes), buffer size 2^^4 bytes = 8 values
  588. *DMA_TCD_NBYTES_MLNO = 2; // Minor Byte Transfer Count 2 bytes = 16 bits (we transfer 2 bytes each minor loop)
  589. *DMA_TCD_SADDR = ADC_RA; // source address
  590. *DMA_TCD_SOFF = 0; // don't change the address when minor loop finishes
  591. *DMA_TCD_SLAST = 0; // don't change src address after major loop completes
  592. *DMA_TCD_DADDR = elems; // destination address
  593. *DMA_TCD_DOFF = 2; // increment 2 bytes each minor loop
  594. *DMA_TCD_DLASTSGA = 0; // modulus feature takes care of going back to first element
  595. *DMA_TCD_CITER_ELINKNO = 1; // Current Major Iteration Count with channel linking disabled
  596. *DMA_TCD_BITER_ELINKNO = 1; // Starting Major Iteration Count with channel linking disabled
  597. *DMA_TCD_CSR = DMA_TCD_CSR_INTMAJOR; // Control and status: interrupt when major counter is complete
  598. DMA_CERQ = DMA_CERQ_CERQ(DMA_channel); // clear all past request
  599. DMA_CINT = DMA_channel; // clear interrupts
  600. uint8_t DMAMUX_SOURCE_ADC = DMAMUX_SOURCE_ADC0;
  601. if(ADC_number==1){
  602. DMAMUX_SOURCE_ADC = DMAMUX_SOURCE_ADC1;
  603. }
  604. *DMAMUX0_CHCFG = DMAMUX_SOURCE_ADC | DMAMUX_ENABLE; // enable mux and set channel DMA_channel to ADC0
  605. DMA_SERQ = DMA_SERQ_SERQ(DMA_channel); // enable DMA request
  606. NVIC_ENABLE_IRQ(IRQ_DMA_CH); // enable interrupts
  607. ************************
  608. SmartMatrix
  609. ************************
  610. // enable minor loop mapping so addresses can get reset after minor loops
  611. DMA_CR = 1 << 7;
  612. // DMA channel #0 - on latch rising edge, read address from fixed address temporary buffer, and output address on GPIO
  613. // using combo of writes to set+clear registers, to only modify the address pins and not other GPIO pins
  614. // address temporary buffer is refreshed before each DMA trigger (by DMA channel #2)
  615. // only use single major loop, never disable channel
  616. #define ADDRESS_ARRAY_REGISTERS_TO_UPDATE 2
  617. DMA_TCD0_SADDR = &gpiosync.gpio_pcor;
  618. DMA_TCD0_SOFF = (int)&gpiosync.gpio_psor - (int)&gpiosync.gpio_pcor;
  619. DMA_TCD0_SLAST = (ADDRESS_ARRAY_REGISTERS_TO_UPDATE * ((int)&ADDX_GPIO_CLEAR_REGISTER - (int)&ADDX_GPIO_SET_REGISTER));
  620. DMA_TCD0_ATTR = DMA_TCD_ATTR_SSIZE(2) | DMA_TCD_ATTR_DSIZE(2);
  621. // Destination Minor Loop Offset Enabled - transfer appropriate number of bytes per minor loop, and put DADDR back to original value when minor loop is complete
  622. // Source Minor Loop Offset Enabled - source buffer is same size and offset as destination so values reset after each minor loop
  623. DMA_TCD0_NBYTES_MLOFFYES = DMA_TCD_NBYTES_SMLOE | DMA_TCD_NBYTES_DMLOE |
  624. ((ADDRESS_ARRAY_REGISTERS_TO_UPDATE * ((int)&ADDX_GPIO_CLEAR_REGISTER - (int)&ADDX_GPIO_SET_REGISTER)) << 10) |
  625. (ADDRESS_ARRAY_REGISTERS_TO_UPDATE * sizeof(gpiosync.gpio_psor));
  626. // start on higher value of two registers, and make offset decrement to avoid negative number in NBYTES_MLOFFYES (TODO: can switch order by masking negative offset)
  627. DMA_TCD0_DADDR = &ADDX_GPIO_CLEAR_REGISTER;
  628. // update destination address so the second update per minor loop is ADDX_GPIO_SET_REGISTER
  629. DMA_TCD0_DOFF = (int)&ADDX_GPIO_SET_REGISTER - (int)&ADDX_GPIO_CLEAR_REGISTER;
  630. DMA_TCD0_DLASTSGA = (ADDRESS_ARRAY_REGISTERS_TO_UPDATE * ((int)&ADDX_GPIO_CLEAR_REGISTER - (int)&ADDX_GPIO_SET_REGISTER));
  631. // single major loop
  632. DMA_TCD0_CITER_ELINKNO = 1;
  633. DMA_TCD0_BITER_ELINKNO = 1;
  634. // link channel 1, enable major channel-to-channel linking, don't clear enable on major loop complete
  635. DMA_TCD0_CSR = (1 << 8) | (1 << 5);
  636. DMAMUX0_CHCFG0 = DMAMUX_SOURCE_LATCH_RISING_EDGE | DMAMUX_ENABLE;
  637. // DMA channel #1 - copy address values from current position in array to buffer to temporarily hold row values for the next timer cycle
  638. // only use single major loop, never disable channel
  639. DMA_TCD1_SADDR = &matrixUpdateBlocks[0][0].addressValues;
  640. DMA_TCD1_SOFF = sizeof(uint16_t);
  641. DMA_TCD1_SLAST = sizeof(matrixUpdateBlock) - (ADDRESS_ARRAY_REGISTERS_TO_UPDATE * sizeof(uint16_t));
  642. DMA_TCD1_ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  643. // 16-bit = 2 bytes transferred
  644. // transfer two 16-bit values, reset destination address back after each minor loop
  645. DMA_TCD1_NBYTES_MLOFFNO = (ADDRESS_ARRAY_REGISTERS_TO_UPDATE * sizeof(uint16_t));
  646. // start with the register that's the highest location in memory and make offset decrement to avoid negative number in NBYTES_MLOFFYES register (TODO: can switch order by masking negative offset)
  647. DMA_TCD1_DADDR = &gpiosync.gpio_pcor;
  648. DMA_TCD1_DOFF = (int)&gpiosync.gpio_psor - (int)&gpiosync.gpio_pcor;
  649. DMA_TCD1_DLASTSGA = (ADDRESS_ARRAY_REGISTERS_TO_UPDATE * ((int)&gpiosync.gpio_pcor - (int)&gpiosync.gpio_psor));
  650. // no minor loop linking, single major loop, single minor loop, don't clear enable after major loop complete
  651. DMA_TCD1_CITER_ELINKNO = 1;
  652. DMA_TCD1_BITER_ELINKNO = 1;
  653. DMA_TCD1_CSR = 0;
  654. // DMA channel #2 - on latch falling edge, load FTM1_CV1 and FTM1_MOD with with next values from current block
  655. // only use single major loop, never disable channel
  656. // link to channel 3 when complete
  657. #define TIMER_REGISTERS_TO_UPDATE 2
  658. DMA_TCD2_SADDR = &matrixUpdateBlocks[0][0].timerValues.timer_oe;
  659. DMA_TCD2_SOFF = sizeof(uint16_t);
  660. DMA_TCD2_SLAST = sizeof(matrixUpdateBlock) - (TIMER_REGISTERS_TO_UPDATE * sizeof(uint16_t));
  661. DMA_TCD2_ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  662. // 16-bit = 2 bytes transferred
  663. DMA_TCD2_NBYTES_MLOFFNO = TIMER_REGISTERS_TO_UPDATE * sizeof(uint16_t);
  664. DMA_TCD2_DADDR = &FTM1_C1V;
  665. DMA_TCD2_DOFF = (int)&FTM1_MOD - (int)&FTM1_C1V;
  666. DMA_TCD2_DLASTSGA = TIMER_REGISTERS_TO_UPDATE * ((int)&FTM1_C1V - (int)&FTM1_MOD);
  667. // no minor loop linking, single major loop
  668. DMA_TCD2_CITER_ELINKNO = 1;
  669. DMA_TCD2_BITER_ELINKNO = 1;
  670. // link channel 3, enable major channel-to-channel linking, don't clear enable after major loop complete
  671. DMA_TCD2_CSR = (3 << 8) | (1 << 5);
  672. DMAMUX0_CHCFG2 = DMAMUX_SOURCE_LATCH_FALLING_EDGE | DMAMUX_ENABLE;
  673. #define DMA_TCD_MLOFF_MASK (0x3FFFFC00)
  674. // DMA channel #3 - repeatedly load gpio_array into GPIOD_PDOR, stop and int on major loop complete
  675. DMA_TCD3_SADDR = matrixUpdateData[0][0];
  676. DMA_TCD3_SOFF = sizeof(matrixUpdateData[0][0]) / 2;
  677. // SADDR will get updated by ISR, no need to set SLAST
  678. DMA_TCD3_SLAST = 0;
  679. DMA_TCD3_ATTR = DMA_TCD_ATTR_SSIZE(0) | DMA_TCD_ATTR_DSIZE(0);
  680. // after each minor loop, set source to point back to the beginning of this set of data,
  681. // but advance by 1 byte to get the next significant bits data
  682. DMA_TCD3_NBYTES_MLOFFYES = DMA_TCD_NBYTES_SMLOE |
  683. (((1 - sizeof(matrixUpdateData[0])) << 10) & DMA_TCD_MLOFF_MASK) |
  684. (MATRIX_WIDTH * DMA_UPDATES_PER_CLOCK);
  685. DMA_TCD3_DADDR = &GPIOD_PDOR;
  686. DMA_TCD3_DOFF = 0;
  687. DMA_TCD3_DLASTSGA = 0;
  688. DMA_TCD3_CITER_ELINKNO = LATCHES_PER_ROW;
  689. DMA_TCD3_BITER_ELINKNO = LATCHES_PER_ROW;
  690. // int after major loop is complete
  691. DMA_TCD3_CSR = DMA_TCD_CSR_INTMAJOR;
  692. // for debugging - enable bandwidth control (space out GPIO updates so they can be seen easier on a low-bandwidth logic analyzer)
  693. //DMA_TCD3_CSR |= (0x02 << 14);
  694. // enable a done interrupt when all DMA operations are complete
  695. NVIC_ENABLE_IRQ(IRQ_DMA_CH3);
  696. // enable additional dma interrupt used as software interrupt
  697. NVIC_SET_PRIORITY(IRQ_DMA_CH1, 0xFF); // 0xFF = lowest priority
  698. NVIC_ENABLE_IRQ(IRQ_DMA_CH1);
  699. // enable channels 0, 1, 2, 3
  700. DMA_ERQ = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3);
  701. // at the end after everything is set up: enable timer from system clock, with appropriate prescale
  702. FTM1_SC = FTM_SC_CLKS(1) | FTM_SC_PS(LATCH_TIMER_PRESCALE);
  703. */
  704. };
  705. // arrange the relative priority of 2 or more DMA channels
  706. void DMAPriorityOrder(DMAChannel &ch1, DMAChannel &ch2);
  707. void DMAPriorityOrder(DMAChannel &ch1, DMAChannel &ch2, DMAChannel &ch3);
  708. void DMAPriorityOrder(DMAChannel &ch1, DMAChannel &ch2, DMAChannel &ch3, DMAChannel &ch4);
  709. extern "C" {
  710. #endif
  711. extern uint16_t dma_channel_allocated_mask;
  712. #ifdef __cplusplus
  713. }
  714. #endif
  715. #endif