Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

815 lines
29KB

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