Teensy 4.1 core updated for C++20
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

793 lines
27KB

  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. //
  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(const signed char &p) { source(*(const uint8_t *)&p); }
  42. void source(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(const signed short &p) { source(*(const uint16_t *)&p); }
  50. void source(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(const signed int &p) { source(*(const uint32_t *)&p); }
  58. void source(const unsigned int &p) { source(*(const uint32_t *)&p); }
  59. void source(const signed long &p) { source(*(const uint32_t *)&p); }
  60. void source(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(const signed char p[], unsigned int len) {
  70. sourceBuffer((uint8_t *)p, len); }
  71. void sourceBuffer(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(const signed short p[], unsigned int len) {
  81. sourceBuffer((uint16_t *)p, len); }
  82. void sourceBuffer(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(const signed int p[], unsigned int len) {
  92. sourceBuffer((uint32_t *)p, len); }
  93. void sourceBuffer(const unsigned int p[], unsigned int len) {
  94. sourceBuffer((uint32_t *)p, len); }
  95. void sourceBuffer(const signed long p[], unsigned int len) {
  96. sourceBuffer((uint32_t *)p, len); }
  97. void sourceBuffer(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(const signed char p[], unsigned int len) {
  108. sourceCircular((uint8_t *)p, len); }
  109. void sourceCircular(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(const signed short p[], unsigned int len) {
  119. sourceCircular((uint16_t *)p, len); }
  120. void sourceCircular(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(const signed int p[], unsigned int len) {
  130. sourceCircular((uint32_t *)p, len); }
  131. void sourceCircular(const unsigned int p[], unsigned int len) {
  132. sourceCircular((uint32_t *)p, len); }
  133. void sourceCircular(const signed long p[], unsigned int len) {
  134. sourceCircular((uint32_t *)p, len); }
  135. void sourceCircular(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(signed char &p) { destination(*(uint8_t *)&p); }
  147. void destination(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(signed short &p) { destination(*(uint16_t *)&p); }
  155. void destination(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(signed int &p) { destination(*(uint32_t *)&p); }
  163. void destination(unsigned int &p) { destination(*(uint32_t *)&p); }
  164. void destination(signed long &p) { destination(*(uint32_t *)&p); }
  165. void destination(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(signed char p[], unsigned int len) {
  175. destinationBuffer((uint8_t *)p, len); }
  176. void destinationBuffer(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(signed short p[], unsigned int len) {
  186. destinationBuffer((uint16_t *)p, len); }
  187. void destinationBuffer(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(signed int p[], unsigned int len) {
  197. destinationBuffer((uint32_t *)p, len); }
  198. void destinationBuffer(unsigned int p[], unsigned int len) {
  199. destinationBuffer((uint32_t *)p, len); }
  200. void destinationBuffer(signed long p[], unsigned int len) {
  201. destinationBuffer((uint32_t *)p, len); }
  202. void destinationBuffer(unsigned long p[], unsigned int len) {
  203. TCD->DADDR = p;
  204. TCD->DOFF = 4;
  205. TCD->ATTR_DST = 1;
  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(signed char p[], unsigned int len) {
  213. destinationCircular((uint8_t *)p, len); }
  214. void destinationCircular(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(signed short p[], unsigned int len) {
  224. destinationCircular((uint16_t *)p, len); }
  225. void destinationCircular(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(signed int p[], unsigned int len) {
  235. destinationCircular((uint32_t *)p, len); }
  236. void destinationCircular(unsigned int p[], unsigned int len) {
  237. destinationCircular((uint32_t *)p, len); }
  238. void destinationCircular(signed long p[], unsigned int len) {
  239. destinationCircular((uint32_t *)p, len); }
  240. void destinationCircular(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 size(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 count(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_ESG;
  297. }
  298. protected:
  299. // users should not be able to create instances of DMABaseClass, which
  300. // require the inheriting class to initialize the TCD pointer.
  301. DMABaseClass() {}
  302. static inline void copy_tcd(TCD_t *dst, const TCD_t *src) {
  303. const uint32_t *p = (const uint32_t *)src;
  304. uint32_t *q = (uint32_t *)dst;
  305. uint32_t t1, t2, t3, t4;
  306. t1 = *p++; t2 = *p++; t3 = *p++; t4 = *p++;
  307. *q++ = t1; *q++ = t2; *q++ = t3; *q++ = t4;
  308. t1 = *p++; t2 = *p++; t3 = *p++; t4 = *p++;
  309. *q++ = t1; *q++ = t2; *q++ = t3; *q++ = t4;
  310. }
  311. };
  312. // DMASetting represents settings stored only in memory, which can be
  313. // applied to any DMA channel.
  314. class DMASetting : public DMABaseClass {
  315. public:
  316. DMASetting() {
  317. TCD = &tcddata;
  318. }
  319. DMASetting(const DMASetting &c) {
  320. TCD = &tcddata;
  321. *this = c;
  322. }
  323. DMASetting(const DMABaseClass &c) {
  324. TCD = &tcddata;
  325. *this = c;
  326. }
  327. DMASetting & operator = (const DMABaseClass &rhs) {
  328. copy_tcd(TCD, rhs.TCD);
  329. return *this;
  330. }
  331. private:
  332. TCD_t tcddata __attribute__((aligned(32)));
  333. };
  334. // DMAChannel reprents an actual DMA channel and its current settings
  335. class DMAChannel : public DMABaseClass {
  336. public:
  337. /*************************************************/
  338. /** Channel Allocation **/
  339. /*************************************************/
  340. DMAChannel() {
  341. init();
  342. }
  343. DMAChannel(const DMAChannel &c) {
  344. TCD = c.TCD;
  345. channel = c.channel;
  346. }
  347. DMAChannel(const DMASetting &c) {
  348. init();
  349. copy_tcd(TCD, c.TCD);
  350. }
  351. DMAChannel & operator = (const DMAChannel &rhs) {
  352. if (channel != rhs.channel) {
  353. release();
  354. TCD = rhs.TCD;
  355. channel = rhs.channel;
  356. }
  357. return *this;
  358. }
  359. DMAChannel & operator = (const DMASetting &rhs) {
  360. copy_tcd(TCD, rhs.TCD);
  361. return *this;
  362. }
  363. ~DMAChannel() {
  364. release();
  365. }
  366. private:
  367. void init(void);
  368. void release(void);
  369. public:
  370. /***************************************/
  371. /** Triggering **/
  372. /***************************************/
  373. // Triggers cause the DMA channel to actually move data. Each
  374. // trigger moves a single data unit, which is typically 8, 16 or 32 bits.
  375. // Use a hardware trigger to make the DMA channel run
  376. void attachTrigger(uint8_t source) {
  377. volatile uint8_t *mux;
  378. mux = (volatile uint8_t *)&(DMAMUX0_CHCFG0) + channel;
  379. *mux = 0;
  380. *mux = (source & 63) | DMAMUX_ENABLE;
  381. }
  382. // Use another DMA channel as the trigger, causing this
  383. // channel to trigger after each transfer is makes, except
  384. // the its last transfer. This effectively makes the 2
  385. // channels run in parallel.
  386. void attachTriggerBeforeCompletion(DMABaseClass &ch) {
  387. ch.TCD->BITER = (ch.TCD->BITER & ~DMA_TCD_BITER_ELINKYES_LINKCH_MASK)
  388. | DMA_TCD_BITER_ELINKYES_LINKCH(channel) | DMA_TCD_BITER_ELINKYES_ELINK;
  389. ch.TCD->CITER = ch.TCD->BITER ;
  390. }
  391. // Use another DMA channel as the trigger, causing this
  392. // channel to trigger when the other channel completes.
  393. void attachTriggerAtCompletion(DMABaseClass &ch) {
  394. ch.TCD->CSR = (ch.TCD->CSR & ~DMA_TCD_CSR_MAJORLINKCH_MASK)
  395. | DMA_TCD_CSR_MAJORLINKCH(channel) | DMA_TCD_CSR_MAJORELINK;
  396. }
  397. // Cause this DMA channel to be continuously triggered, so
  398. // it will move data as rapidly as possible, without waiting.
  399. // Normally this would be used with disableOnCompletion().
  400. void attachTriggerContinuous(void) {
  401. volatile uint8_t *mux = (volatile uint8_t *)&DMAMUX0_CHCFG0;
  402. mux[channel] = 0;
  403. #if DMAMUX_NUM_SOURCE_ALWAYS >= DMA_NUM_CHANNELS
  404. mux[channel] = DMAMUX_SOURCE_ALWAYS0 + channel;
  405. #else
  406. // search for an unused "always on" source
  407. unsigned int i = DMAMUX_SOURCE_ALWAYS0;
  408. for (i = DMAMUX_SOURCE_ALWAYS0;
  409. i < DMAMUX_SOURCE_ALWAYS0 + DMAMUX_NUM_SOURCE_ALWAYS; i++) {
  410. unsigned int ch;
  411. for (ch=0; ch < DMA_NUM_CHANNELS; ch++) {
  412. if (mux[ch] == i) break;
  413. }
  414. if (ch >= DMA_NUM_CHANNELS) {
  415. mux[channel] = i;
  416. return;
  417. }
  418. }
  419. #endif
  420. }
  421. // Manually trigger the DMA channel.
  422. void trigger(void) {
  423. DMA_SSRT = channel;
  424. }
  425. /***************************************/
  426. /** Interrupts **/
  427. /***************************************/
  428. // An interrupt routine can be run when the DMA channel completes
  429. // the entire transfer, and also optionally when half of the
  430. // transfer is completed.
  431. void attachInterrupt(void (*isr)(void)) {
  432. _VectorsRam[channel + IRQ_DMA_CH0 + 16] = isr;
  433. NVIC_ENABLE_IRQ(IRQ_DMA_CH0 + channel);
  434. }
  435. void detachInterrupt(void) {
  436. NVIC_DISABLE_IRQ(IRQ_DMA_CH0 + channel);
  437. }
  438. void clearInterrupt(void) {
  439. DMA_CINT = channel;
  440. }
  441. /***************************************/
  442. /** Enable / Disable **/
  443. /***************************************/
  444. void enable(void) {
  445. DMA_SERQ = channel;
  446. }
  447. void disable(void) {
  448. DMA_CERQ = channel;
  449. }
  450. /***************************************/
  451. /** Status **/
  452. /***************************************/
  453. // TODO: "get" functions, to read important stuff, like SADDR & DADDR...
  454. // error status, etc
  455. /***************************************/
  456. /** Direct Hardware Access **/
  457. /***************************************/
  458. // For complex and unusual configurations not possible with the above
  459. // functions, the Transfer Control Descriptor (TCD) and channel number
  460. // can be used directly. This leads to less portable and less readable
  461. // code, but direct control of all parameters is possible.
  462. uint8_t channel;
  463. /* usage cases:
  464. ************************
  465. OctoWS2811:
  466. ************************
  467. // enable clocks to the DMA controller and DMAMUX
  468. SIM_SCGC7 |= SIM_SCGC7_DMA;
  469. SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
  470. DMA_CR = 0;
  471. DMA_CERQ = 1;
  472. DMA_CERQ = 2;
  473. DMA_CERQ = 3;
  474. // DMA channel #1 sets WS2811 high at the beginning of each cycle
  475. DMA_TCD1_SADDR = &ones;
  476. DMA_TCD1_SOFF = 0;
  477. DMA_TCD1_ATTR = DMA_TCD_ATTR_SSIZE(0) | DMA_TCD_ATTR_DSIZE(0);
  478. DMA_TCD1_NBYTES_MLNO = 1;
  479. DMA_TCD1_SLAST = 0;
  480. DMA_TCD1_DADDR = &GPIOD_PSOR;
  481. DMA_TCD1_DOFF = 0;
  482. DMA_TCD1_CITER_ELINKNO = bufsize;
  483. DMA_TCD1_DLASTSGA = 0;
  484. DMA_TCD1_CSR = DMA_TCD_CSR_DREQ;
  485. DMA_TCD1_BITER_ELINKNO = bufsize;
  486. dma1.source(ones);
  487. dma1.destination(GPIOD_PSOR);
  488. dma1.size(1);
  489. dma1.count(bufsize);
  490. dma1.disableOnCompletion();
  491. // DMA channel #2 writes the pixel data at 20% of the cycle
  492. DMA_TCD2_SADDR = frameBuffer;
  493. DMA_TCD2_SOFF = 1;
  494. DMA_TCD2_ATTR = DMA_TCD_ATTR_SSIZE(0) | DMA_TCD_ATTR_DSIZE(0);
  495. DMA_TCD2_NBYTES_MLNO = 1;
  496. DMA_TCD2_SLAST = -bufsize;
  497. DMA_TCD2_DADDR = &GPIOD_PDOR;
  498. DMA_TCD2_DOFF = 0;
  499. DMA_TCD2_CITER_ELINKNO = bufsize;
  500. DMA_TCD2_DLASTSGA = 0;
  501. DMA_TCD2_CSR = DMA_TCD_CSR_DREQ;
  502. DMA_TCD2_BITER_ELINKNO = bufsize;
  503. dma2.source(frameBuffer, sizeof(frameBuffer));
  504. dma2.destination(GPIOD_PDOR);
  505. dma2.size(1);
  506. dma2.count(bufsize);
  507. dma2.disableOnCompletion();
  508. // DMA channel #3 clear all the pins low at 48% of the cycle
  509. DMA_TCD3_SADDR = &ones;
  510. DMA_TCD3_SOFF = 0;
  511. DMA_TCD3_ATTR = DMA_TCD_ATTR_SSIZE(0) | DMA_TCD_ATTR_DSIZE(0);
  512. DMA_TCD3_NBYTES_MLNO = 1;
  513. DMA_TCD3_SLAST = 0;
  514. DMA_TCD3_DADDR = &GPIOD_PCOR;
  515. DMA_TCD3_DOFF = 0;
  516. DMA_TCD3_CITER_ELINKNO = bufsize;
  517. DMA_TCD3_DLASTSGA = 0;
  518. DMA_TCD3_CSR = DMA_TCD_CSR_DREQ | DMA_TCD_CSR_INTMAJOR;
  519. DMA_TCD3_BITER_ELINKNO = bufsize;
  520. dma3.source(ones);
  521. dma3.destination(GPIOD_PCOR);
  522. dma3.size(1);
  523. dma3.count(bufsize);
  524. dma3.disableOnCompletion();
  525. ************************
  526. Audio, DAC
  527. ************************
  528. DMA_CR = 0;
  529. DMA_TCD4_SADDR = dac_buffer;
  530. DMA_TCD4_SOFF = 2;
  531. DMA_TCD4_ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  532. DMA_TCD4_NBYTES_MLNO = 2;
  533. DMA_TCD4_SLAST = -sizeof(dac_buffer);
  534. DMA_TCD4_DADDR = &DAC0_DAT0L;
  535. DMA_TCD4_DOFF = 0;
  536. DMA_TCD4_CITER_ELINKNO = sizeof(dac_buffer) / 2;
  537. DMA_TCD4_DLASTSGA = 0;
  538. DMA_TCD4_BITER_ELINKNO = sizeof(dac_buffer) / 2;
  539. DMA_TCD4_CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  540. DMAMUX0_CHCFG4 = DMAMUX_DISABLE;
  541. DMAMUX0_CHCFG4 = DMAMUX_SOURCE_PDB | DMAMUX_ENABLE;
  542. ************************
  543. Audio, I2S
  544. ************************
  545. DMA_CR = 0;
  546. DMA_TCD0_SADDR = i2s_tx_buffer;
  547. DMA_TCD0_SOFF = 2;
  548. DMA_TCD0_ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  549. DMA_TCD0_NBYTES_MLNO = 2;
  550. DMA_TCD0_SLAST = -sizeof(i2s_tx_buffer);
  551. DMA_TCD0_DADDR = &I2S0_TDR0;
  552. DMA_TCD0_DOFF = 0;
  553. DMA_TCD0_CITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  554. DMA_TCD0_DLASTSGA = 0;
  555. DMA_TCD0_BITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  556. DMA_TCD0_CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  557. DMAMUX0_CHCFG0 = DMAMUX_DISABLE;
  558. DMAMUX0_CHCFG0 = DMAMUX_SOURCE_I2S0_TX | DMAMUX_ENABLE;
  559. ************************
  560. ADC lib, Pedro Villanueva
  561. ************************
  562. DMA_CR = 0; // normal mode of operation
  563. *DMAMUX0_CHCFG = DMAMUX_DISABLE; // disable before changing
  564. *DMA_TCD_ATTR = DMA_TCD_ATTR_SSIZE(DMA_TCD_ATTR_SIZE_16BIT) |
  565. DMA_TCD_ATTR_DSIZE(DMA_TCD_ATTR_SIZE_16BIT) |
  566. DMA_TCD_ATTR_DMOD(4); // src and dst data is 16 bit (2 bytes), buffer size 2^^4 bytes = 8 values
  567. *DMA_TCD_NBYTES_MLNO = 2; // Minor Byte Transfer Count 2 bytes = 16 bits (we transfer 2 bytes each minor loop)
  568. *DMA_TCD_SADDR = ADC_RA; // source address
  569. *DMA_TCD_SOFF = 0; // don't change the address when minor loop finishes
  570. *DMA_TCD_SLAST = 0; // don't change src address after major loop completes
  571. *DMA_TCD_DADDR = elems; // destination address
  572. *DMA_TCD_DOFF = 2; // increment 2 bytes each minor loop
  573. *DMA_TCD_DLASTSGA = 0; // modulus feature takes care of going back to first element
  574. *DMA_TCD_CITER_ELINKNO = 1; // Current Major Iteration Count with channel linking disabled
  575. *DMA_TCD_BITER_ELINKNO = 1; // Starting Major Iteration Count with channel linking disabled
  576. *DMA_TCD_CSR = DMA_TCD_CSR_INTMAJOR; // Control and status: interrupt when major counter is complete
  577. DMA_CERQ = DMA_CERQ_CERQ(DMA_channel); // clear all past request
  578. DMA_CINT = DMA_channel; // clear interrupts
  579. uint8_t DMAMUX_SOURCE_ADC = DMAMUX_SOURCE_ADC0;
  580. if(ADC_number==1){
  581. DMAMUX_SOURCE_ADC = DMAMUX_SOURCE_ADC1;
  582. }
  583. *DMAMUX0_CHCFG = DMAMUX_SOURCE_ADC | DMAMUX_ENABLE; // enable mux and set channel DMA_channel to ADC0
  584. DMA_SERQ = DMA_SERQ_SERQ(DMA_channel); // enable DMA request
  585. NVIC_ENABLE_IRQ(IRQ_DMA_CH); // enable interrupts
  586. ************************
  587. SmartMatrix
  588. ************************
  589. // enable minor loop mapping so addresses can get reset after minor loops
  590. DMA_CR = 1 << 7;
  591. // DMA channel #0 - on latch rising edge, read address from fixed address temporary buffer, and output address on GPIO
  592. // using combo of writes to set+clear registers, to only modify the address pins and not other GPIO pins
  593. // address temporary buffer is refreshed before each DMA trigger (by DMA channel #2)
  594. // only use single major loop, never disable channel
  595. #define ADDRESS_ARRAY_REGISTERS_TO_UPDATE 2
  596. DMA_TCD0_SADDR = &gpiosync.gpio_pcor;
  597. DMA_TCD0_SOFF = (int)&gpiosync.gpio_psor - (int)&gpiosync.gpio_pcor;
  598. DMA_TCD0_SLAST = (ADDRESS_ARRAY_REGISTERS_TO_UPDATE * ((int)&ADDX_GPIO_CLEAR_REGISTER - (int)&ADDX_GPIO_SET_REGISTER));
  599. DMA_TCD0_ATTR = DMA_TCD_ATTR_SSIZE(2) | DMA_TCD_ATTR_DSIZE(2);
  600. // 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
  601. // Source Minor Loop Offset Enabled - source buffer is same size and offset as destination so values reset after each minor loop
  602. DMA_TCD0_NBYTES_MLOFFYES = DMA_TCD_NBYTES_SMLOE | DMA_TCD_NBYTES_DMLOE |
  603. ((ADDRESS_ARRAY_REGISTERS_TO_UPDATE * ((int)&ADDX_GPIO_CLEAR_REGISTER - (int)&ADDX_GPIO_SET_REGISTER)) << 10) |
  604. (ADDRESS_ARRAY_REGISTERS_TO_UPDATE * sizeof(gpiosync.gpio_psor));
  605. // 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)
  606. DMA_TCD0_DADDR = &ADDX_GPIO_CLEAR_REGISTER;
  607. // update destination address so the second update per minor loop is ADDX_GPIO_SET_REGISTER
  608. DMA_TCD0_DOFF = (int)&ADDX_GPIO_SET_REGISTER - (int)&ADDX_GPIO_CLEAR_REGISTER;
  609. DMA_TCD0_DLASTSGA = (ADDRESS_ARRAY_REGISTERS_TO_UPDATE * ((int)&ADDX_GPIO_CLEAR_REGISTER - (int)&ADDX_GPIO_SET_REGISTER));
  610. // single major loop
  611. DMA_TCD0_CITER_ELINKNO = 1;
  612. DMA_TCD0_BITER_ELINKNO = 1;
  613. // link channel 1, enable major channel-to-channel linking, don't clear enable on major loop complete
  614. DMA_TCD0_CSR = (1 << 8) | (1 << 5);
  615. DMAMUX0_CHCFG0 = DMAMUX_SOURCE_LATCH_RISING_EDGE | DMAMUX_ENABLE;
  616. // DMA channel #1 - copy address values from current position in array to buffer to temporarily hold row values for the next timer cycle
  617. // only use single major loop, never disable channel
  618. DMA_TCD1_SADDR = &matrixUpdateBlocks[0][0].addressValues;
  619. DMA_TCD1_SOFF = sizeof(uint16_t);
  620. DMA_TCD1_SLAST = sizeof(matrixUpdateBlock) - (ADDRESS_ARRAY_REGISTERS_TO_UPDATE * sizeof(uint16_t));
  621. DMA_TCD1_ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  622. // 16-bit = 2 bytes transferred
  623. // transfer two 16-bit values, reset destination address back after each minor loop
  624. DMA_TCD1_NBYTES_MLOFFNO = (ADDRESS_ARRAY_REGISTERS_TO_UPDATE * sizeof(uint16_t));
  625. // 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)
  626. DMA_TCD1_DADDR = &gpiosync.gpio_pcor;
  627. DMA_TCD1_DOFF = (int)&gpiosync.gpio_psor - (int)&gpiosync.gpio_pcor;
  628. DMA_TCD1_DLASTSGA = (ADDRESS_ARRAY_REGISTERS_TO_UPDATE * ((int)&gpiosync.gpio_pcor - (int)&gpiosync.gpio_psor));
  629. // no minor loop linking, single major loop, single minor loop, don't clear enable after major loop complete
  630. DMA_TCD1_CITER_ELINKNO = 1;
  631. DMA_TCD1_BITER_ELINKNO = 1;
  632. DMA_TCD1_CSR = 0;
  633. // DMA channel #2 - on latch falling edge, load FTM1_CV1 and FTM1_MOD with with next values from current block
  634. // only use single major loop, never disable channel
  635. // link to channel 3 when complete
  636. #define TIMER_REGISTERS_TO_UPDATE 2
  637. DMA_TCD2_SADDR = &matrixUpdateBlocks[0][0].timerValues.timer_oe;
  638. DMA_TCD2_SOFF = sizeof(uint16_t);
  639. DMA_TCD2_SLAST = sizeof(matrixUpdateBlock) - (TIMER_REGISTERS_TO_UPDATE * sizeof(uint16_t));
  640. DMA_TCD2_ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  641. // 16-bit = 2 bytes transferred
  642. DMA_TCD2_NBYTES_MLOFFNO = TIMER_REGISTERS_TO_UPDATE * sizeof(uint16_t);
  643. DMA_TCD2_DADDR = &FTM1_C1V;
  644. DMA_TCD2_DOFF = (int)&FTM1_MOD - (int)&FTM1_C1V;
  645. DMA_TCD2_DLASTSGA = TIMER_REGISTERS_TO_UPDATE * ((int)&FTM1_C1V - (int)&FTM1_MOD);
  646. // no minor loop linking, single major loop
  647. DMA_TCD2_CITER_ELINKNO = 1;
  648. DMA_TCD2_BITER_ELINKNO = 1;
  649. // link channel 3, enable major channel-to-channel linking, don't clear enable after major loop complete
  650. DMA_TCD2_CSR = (3 << 8) | (1 << 5);
  651. DMAMUX0_CHCFG2 = DMAMUX_SOURCE_LATCH_FALLING_EDGE | DMAMUX_ENABLE;
  652. #define DMA_TCD_MLOFF_MASK (0x3FFFFC00)
  653. // DMA channel #3 - repeatedly load gpio_array into GPIOD_PDOR, stop and int on major loop complete
  654. DMA_TCD3_SADDR = matrixUpdateData[0][0];
  655. DMA_TCD3_SOFF = sizeof(matrixUpdateData[0][0]) / 2;
  656. // SADDR will get updated by ISR, no need to set SLAST
  657. DMA_TCD3_SLAST = 0;
  658. DMA_TCD3_ATTR = DMA_TCD_ATTR_SSIZE(0) | DMA_TCD_ATTR_DSIZE(0);
  659. // after each minor loop, set source to point back to the beginning of this set of data,
  660. // but advance by 1 byte to get the next significant bits data
  661. DMA_TCD3_NBYTES_MLOFFYES = DMA_TCD_NBYTES_SMLOE |
  662. (((1 - sizeof(matrixUpdateData[0])) << 10) & DMA_TCD_MLOFF_MASK) |
  663. (MATRIX_WIDTH * DMA_UPDATES_PER_CLOCK);
  664. DMA_TCD3_DADDR = &GPIOD_PDOR;
  665. DMA_TCD3_DOFF = 0;
  666. DMA_TCD3_DLASTSGA = 0;
  667. DMA_TCD3_CITER_ELINKNO = LATCHES_PER_ROW;
  668. DMA_TCD3_BITER_ELINKNO = LATCHES_PER_ROW;
  669. // int after major loop is complete
  670. DMA_TCD3_CSR = DMA_TCD_CSR_INTMAJOR;
  671. // for debugging - enable bandwidth control (space out GPIO updates so they can be seen easier on a low-bandwidth logic analyzer)
  672. //DMA_TCD3_CSR |= (0x02 << 14);
  673. // enable a done interrupt when all DMA operations are complete
  674. NVIC_ENABLE_IRQ(IRQ_DMA_CH3);
  675. // enable additional dma interrupt used as software interrupt
  676. NVIC_SET_PRIORITY(IRQ_DMA_CH1, 0xFF); // 0xFF = lowest priority
  677. NVIC_ENABLE_IRQ(IRQ_DMA_CH1);
  678. // enable channels 0, 1, 2, 3
  679. DMA_ERQ = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3);
  680. // at the end after everything is set up: enable timer from system clock, with appropriate prescale
  681. FTM1_SC = FTM_SC_CLKS(1) | FTM_SC_PS(LATCH_TIMER_PRESCALE);
  682. */
  683. };
  684. // arrange the relative priority of 2 or more DMA channels
  685. void DMAPriorityOrder(DMAChannel &ch1, DMAChannel &ch2);
  686. void DMAPriorityOrder(DMAChannel &ch1, DMAChannel &ch2, DMAChannel &ch3);
  687. void DMAPriorityOrder(DMAChannel &ch1, DMAChannel &ch2, DMAChannel &ch3, DMAChannel &ch4);
  688. extern "C" {
  689. #endif
  690. extern uint16_t dma_channel_allocated_mask;
  691. #ifdef __cplusplus
  692. }
  693. #endif
  694. #endif