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.

output_pwm.cpp 12KB

10 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2014, Paul Stoffregen, paul@pjrc.com
  3. *
  4. * Development of this audio library was funded by PJRC.COM, LLC by sales of
  5. * Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
  6. * open source software by purchasing Teensy or other PJRC products.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice, development funding notice, and this permission
  16. * notice shall be included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <Arduino.h>
  27. #include "output_pwm.h"
  28. bool AudioOutputPWM::update_responsibility = false;
  29. #if defined(KINETISK)
  30. audio_block_t * AudioOutputPWM::block_1st = NULL;
  31. audio_block_t * AudioOutputPWM::block_2nd = NULL;
  32. uint32_t AudioOutputPWM::block_offset = 0;
  33. uint8_t AudioOutputPWM::interrupt_count = 0;
  34. DMAMEM uint32_t pwm_dma_buffer[AUDIO_BLOCK_SAMPLES*2];
  35. DMAChannel AudioOutputPWM::dma(false);
  36. // TODO: this code assumes F_BUS is 48 MHz.
  37. // supporting other speeds is not easy, but should be done someday
  38. void AudioOutputPWM::begin(void)
  39. {
  40. dma.begin(true); // Allocate the DMA channel first
  41. //Serial.println("AudioPwmOutput constructor");
  42. block_1st = NULL;
  43. FTM1_SC = 0;
  44. FTM1_CNT = 0;
  45. FTM1_MOD = 543;
  46. FTM1_C0SC = 0x69; // send DMA request on match
  47. FTM1_C1SC = 0x28;
  48. FTM1_SC = FTM_SC_CLKS(1) | FTM_SC_PS(0);
  49. CORE_PIN3_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  50. CORE_PIN4_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  51. FTM1_C0V = 120; // range 120 to 375
  52. FTM1_C1V = 0; // range 0 to 255
  53. for (int i=0; i<(AUDIO_BLOCK_SAMPLES*2); i+=2) {
  54. pwm_dma_buffer[i] = 120; // zero must not be used
  55. pwm_dma_buffer[i+1] = 0;
  56. }
  57. dma.TCD->SADDR = pwm_dma_buffer;
  58. dma.TCD->SOFF = 4;
  59. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(2)
  60. | DMA_TCD_ATTR_DSIZE(2) | DMA_TCD_ATTR_DMOD(4);
  61. dma.TCD->NBYTES_MLNO = 8;
  62. dma.TCD->SLAST = -sizeof(pwm_dma_buffer);
  63. dma.TCD->DADDR = &FTM1_C0V;
  64. dma.TCD->DOFF = 8;
  65. dma.TCD->CITER_ELINKNO = sizeof(pwm_dma_buffer) / 8;
  66. dma.TCD->DLASTSGA = 0;
  67. dma.TCD->BITER_ELINKNO = sizeof(pwm_dma_buffer) / 8;
  68. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  69. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_FTM1_CH0);
  70. dma.enable();
  71. update_responsibility = update_setup();
  72. dma.attachInterrupt(isr);
  73. }
  74. void AudioOutputPWM::update(void)
  75. {
  76. audio_block_t *block;
  77. block = receiveReadOnly();
  78. if (!block) return;
  79. __disable_irq();
  80. if (block_1st == NULL) {
  81. block_1st = block;
  82. block_offset = 0;
  83. __enable_irq();
  84. } else if (block_2nd == NULL) {
  85. block_2nd = block;
  86. __enable_irq();
  87. } else {
  88. audio_block_t *tmp = block_1st;
  89. block_1st = block_2nd;
  90. block_2nd = block;
  91. block_offset = 0;
  92. __enable_irq();
  93. release(tmp);
  94. }
  95. }
  96. void AudioOutputPWM::isr(void)
  97. {
  98. int16_t *src;
  99. uint32_t *dest;
  100. audio_block_t *block;
  101. uint32_t saddr, offset;
  102. saddr = (uint32_t)(dma.TCD->SADDR);
  103. dma.clearInterrupt();
  104. if (saddr < (uint32_t)pwm_dma_buffer + sizeof(pwm_dma_buffer) / 2) {
  105. // DMA is transmitting the first half of the buffer
  106. // so we must fill the second half
  107. dest = &pwm_dma_buffer[AUDIO_BLOCK_SAMPLES];
  108. } else {
  109. // DMA is transmitting the second half of the buffer
  110. // so we must fill the first half
  111. dest = pwm_dma_buffer;
  112. }
  113. block = AudioOutputPWM::block_1st;
  114. offset = AudioOutputPWM::block_offset;
  115. if (block) {
  116. src = &block->data[offset];
  117. for (int i=0; i < AUDIO_BLOCK_SAMPLES/4; i++) {
  118. uint16_t sample = *src++ + 0x8000;
  119. uint32_t msb = ((sample >> 8) & 255) + 120;
  120. uint32_t lsb = sample & 255;
  121. *dest++ = msb;
  122. *dest++ = lsb;
  123. *dest++ = msb;
  124. *dest++ = lsb;
  125. }
  126. offset += AUDIO_BLOCK_SAMPLES/4;
  127. if (offset < AUDIO_BLOCK_SAMPLES) {
  128. AudioOutputPWM::block_offset = offset;
  129. } else {
  130. AudioOutputPWM::block_offset = 0;
  131. AudioStream::release(block);
  132. AudioOutputPWM::block_1st = AudioOutputPWM::block_2nd;
  133. AudioOutputPWM::block_2nd = NULL;
  134. }
  135. } else {
  136. // fill with silence when no data available
  137. for (int i=0; i < AUDIO_BLOCK_SAMPLES/4; i++) {
  138. *dest++ = 248;
  139. *dest++ = 0;
  140. *dest++ = 248;
  141. *dest++ = 0;
  142. }
  143. }
  144. if (AudioOutputPWM::update_responsibility) {
  145. if (++AudioOutputPWM::interrupt_count >= 4) {
  146. AudioOutputPWM::interrupt_count = 0;
  147. AudioStream::update_all();
  148. }
  149. }
  150. }
  151. // DMA target is: (registers require 32 bit writes)
  152. // 40039010 Channel 0 Value (FTM1_C0V)
  153. // 40039018 Channel 1 Value (FTM1_C1V)
  154. // TCD:
  155. // source address = buffer address
  156. // source offset = 4 bytes
  157. // attr = no src mod, ssize = 32 bit, dest mod = 16 bytes (4), dsize = 32 bit
  158. // minor loop byte count = 8
  159. // source last adjust = -sizeof(buffer)
  160. // dest address = FTM1_C0V
  161. // dest address offset = 8
  162. // citer = sizeof(buffer) / 8 (no minor loop linking)
  163. // dest last adjust = 0 (dest modulo keeps it ready for more)
  164. // control:
  165. // throttling = 0
  166. // major link to same channel
  167. // done = 0
  168. // active = 0
  169. // majorlink = 1
  170. // scatter/gather = 0
  171. // disable request = 0
  172. // inthalf = 1
  173. // intmajor = 1
  174. // start = 0
  175. // biter = sizeof(buffer) / 8 (no minor loop linking)
  176. #elif defined(KINETISL)
  177. void AudioOutputPWM::update(void)
  178. {
  179. audio_block_t *block;
  180. block = receiveReadOnly();
  181. if (block) release(block);
  182. }
  183. #elif defined(__IMXRT1062__)
  184. /*
  185. * by Frank B
  186. */
  187. static const uint8_t silence[2] = {0x80, 0x00};
  188. extern uint8_t analog_write_res;
  189. extern const struct _pwm_pin_info_struct pwm_pin_info[];
  190. audio_block_t * AudioOutputPWM::block = NULL;
  191. DMAMEM __attribute__((aligned(32))) static uint16_t pwm_tx_buffer[2][AUDIO_BLOCK_SAMPLES * 2];
  192. DMAChannel AudioOutputPWM::dma[2](false);
  193. _audio_info_flexpwm AudioOutputPWM::apins[2];
  194. FLASHMEM
  195. void AudioOutputPWM::begin(void) { begin(3, 4); }
  196. FLASHMEM
  197. void AudioOutputPWM::begin(uint8_t pin1, uint8_t pin2)
  198. {
  199. analogWriteResolution(8);
  200. const uint8_t pins[2] = {pin1, pin2};
  201. for (unsigned i = 0; i < 2; i++) {
  202. // use the existing code here:
  203. analogWriteFrequency(pins[i], AUDIO_SAMPLE_RATE_EXACT);
  204. analogWrite(pins[i], silence[i]);
  205. //Fill structure
  206. apins[i].pin = pins[i];
  207. apins[i].info = pwm_pin_info[apins[i].pin];
  208. uint8_t dmamux_source;
  209. if (apins[i].info.type == 1) { //only for valid flexPWM pin:
  210. unsigned module = (apins[i].info.module >> 4) & 3;
  211. unsigned submodule = apins[i].info.module & 3;
  212. switch (module) {
  213. case 0: {
  214. apins[i].flexpwm = &IMXRT_FLEXPWM1;
  215. switch (submodule) {
  216. case 0: dmamux_source = DMAMUX_SOURCE_FLEXPWM1_WRITE0; break;
  217. case 1: dmamux_source = DMAMUX_SOURCE_FLEXPWM1_WRITE1; break;
  218. case 2: dmamux_source = DMAMUX_SOURCE_FLEXPWM1_WRITE2; break;
  219. default: dmamux_source = DMAMUX_SOURCE_FLEXPWM1_WRITE3;
  220. }
  221. break;
  222. }
  223. case 1: {
  224. apins[i].flexpwm = &IMXRT_FLEXPWM2;
  225. switch (submodule) {
  226. case 0: dmamux_source = DMAMUX_SOURCE_FLEXPWM2_WRITE0; break;
  227. case 1: dmamux_source = DMAMUX_SOURCE_FLEXPWM2_WRITE1; break;
  228. case 2: dmamux_source = DMAMUX_SOURCE_FLEXPWM2_WRITE2; break;
  229. default: dmamux_source = DMAMUX_SOURCE_FLEXPWM2_WRITE3;
  230. }
  231. break;
  232. }
  233. case 2: {
  234. apins[i].flexpwm = &IMXRT_FLEXPWM3;
  235. switch (submodule) {
  236. case 0: dmamux_source = DMAMUX_SOURCE_FLEXPWM3_WRITE0; break;
  237. case 1: dmamux_source = DMAMUX_SOURCE_FLEXPWM3_WRITE1; break;
  238. case 2: dmamux_source = DMAMUX_SOURCE_FLEXPWM3_WRITE2; break;
  239. default: dmamux_source = DMAMUX_SOURCE_FLEXPWM3_WRITE3;
  240. }
  241. break;
  242. }
  243. default: {
  244. apins[i].flexpwm = &IMXRT_FLEXPWM4;
  245. switch (submodule) {
  246. case 0: dmamux_source = DMAMUX_SOURCE_FLEXPWM4_WRITE0; break;
  247. case 1: dmamux_source = DMAMUX_SOURCE_FLEXPWM4_WRITE1; break;
  248. case 2: dmamux_source = DMAMUX_SOURCE_FLEXPWM4_WRITE2; break;
  249. default: dmamux_source = DMAMUX_SOURCE_FLEXPWM4_WRITE3;
  250. }
  251. }
  252. }
  253. volatile uint16_t *valReg;
  254. switch (apins[i].info.channel) {
  255. case 0: valReg = &apins[i].flexpwm->SM[submodule].VAL0; break;
  256. case 1: valReg = &apins[i].flexpwm->SM[submodule].VAL3; break;
  257. default: valReg = &apins[i].flexpwm->SM[submodule].VAL5; break;
  258. }
  259. dma[i].begin(true);
  260. dma[i].TCD->SADDR = &pwm_tx_buffer[i][0];
  261. dma[i].TCD->SOFF = 2;
  262. dma[i].TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  263. dma[i].TCD->NBYTES_MLNO = 2;
  264. dma[i].TCD->SLAST = -sizeof(pwm_tx_buffer[0]);
  265. dma[i].TCD->DOFF = 0;
  266. dma[i].TCD->CITER_ELINKNO = sizeof(pwm_tx_buffer[0]) / 2;
  267. dma[i].TCD->DLASTSGA = 0;
  268. dma[i].TCD->BITER_ELINKNO = sizeof(pwm_tx_buffer[0]) / 2;
  269. dma[i].TCD->DADDR = valReg;
  270. dma[i].triggerAtHardwareEvent(dmamux_source);
  271. if (i == 1) { //One interrupt only
  272. dma[i].TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  273. dma[i].attachInterrupt(isr);
  274. }
  275. //set PWM-DMA-Enable
  276. apins[i].flexpwm->SM[submodule].DMAEN = FLEXPWM_SMDMAEN_VALDE;
  277. //clear inital dma data:
  278. uint32_t modulo = apins[i].flexpwm->SM[apins[i].info.module & 3].VAL1;
  279. for (unsigned j=0; j<AUDIO_BLOCK_SAMPLES * 2; j++) {
  280. uint32_t cval = (silence[i] * (modulo + 1)) >> analog_write_res;
  281. if (cval > modulo) cval = modulo;
  282. pwm_tx_buffer[i][j] = cval;
  283. }
  284. arm_dcache_flush_delete(&pwm_tx_buffer[i][0], sizeof(pwm_tx_buffer[0]) / 2 );
  285. }
  286. }
  287. dma[0].enable();
  288. dma[1].enable();
  289. update_responsibility = update_setup();
  290. //pinMode(13,OUTPUT);
  291. }
  292. void AudioOutputPWM::isr(void)
  293. {
  294. dma[1].clearInterrupt();
  295. uint16_t *dest, *dest1;
  296. uint32_t saddr = (uint32_t)(dma[0].TCD->SADDR);
  297. if (saddr < (uint32_t)&pwm_tx_buffer[0][AUDIO_BLOCK_SAMPLES]) {
  298. // DMA is transmitting the first half of the buffer
  299. // so we must fill the second half
  300. dest = &pwm_tx_buffer[0][AUDIO_BLOCK_SAMPLES];
  301. dest1 = &pwm_tx_buffer[1][AUDIO_BLOCK_SAMPLES];
  302. } else {
  303. // DMA is transmitting the second half of the buffer
  304. // so we must fill the first half
  305. dest = &pwm_tx_buffer[0][0];
  306. dest1 = &pwm_tx_buffer[1][0];
  307. }
  308. const uint32_t modulo[2] = { apins[0].flexpwm->SM[apins[0].info.module & 3].VAL1, apins[1].flexpwm->SM[apins[1].info.module & 3].VAL1};
  309. if (block) {
  310. for (unsigned i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  311. uint32_t sample = (uint16_t)block->data[i] + 0x8000;
  312. uint32_t msb = ((sample >> 8) & 255)/* + 120 ???*/;
  313. uint32_t cval0 = (msb * (modulo[0] + 1)) >> analog_write_res;
  314. if (cval0 > modulo[0]) cval0 = modulo[0]; // TODO: is this check correct?
  315. *dest++ = cval0;
  316. uint32_t lsb = sample & 255;
  317. uint32_t cval1 = (lsb * (modulo[1] + 1)) >> analog_write_res;
  318. if (cval1 > modulo[1]) cval1 = modulo[1];
  319. *dest1++ = cval1;
  320. }
  321. arm_dcache_flush_delete(dest, sizeof(pwm_tx_buffer[0]) / 2 );
  322. arm_dcache_flush_delete(dest1, sizeof(pwm_tx_buffer[1]) / 2 );
  323. AudioStream::release(block);
  324. block = NULL;
  325. } else {
  326. //Serial.println(".");
  327. // fill with silence when no data available
  328. uint32_t cval0 = (silence[0] * (modulo[0] + 1)) >> analog_write_res;
  329. if (cval0 > modulo[0]) cval0 = modulo[0];
  330. uint32_t cval1 = (silence[1] * (modulo[1] + 1)) >> analog_write_res;
  331. if (cval1 > modulo[1]) cval1 = modulo[1];
  332. for (unsigned i=0; i < AUDIO_BLOCK_SAMPLES / 2; i++) {
  333. *dest++ = cval0;
  334. *dest++ = cval0;
  335. *dest1++ = cval1;
  336. *dest1++ = cval1;
  337. }
  338. arm_dcache_flush_delete(dest, sizeof(pwm_tx_buffer[0]) / 2 );
  339. arm_dcache_flush_delete(dest1, sizeof(pwm_tx_buffer[1]) / 2 );
  340. }
  341. AudioStream::update_all();
  342. //digitalWriteFast(13, !digitalRead(13));
  343. }
  344. void AudioOutputPWM::update(void)
  345. {
  346. audio_block_t *tblock;
  347. tblock = receiveReadOnly();
  348. if (!tblock) return;
  349. __disable_irq();
  350. block = tblock;
  351. __enable_irq();
  352. }
  353. #endif