PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

424 lines
13KB

  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. #if 1
  185. // Frank says this should be disabled for non-beta release
  186. // https://forum.pjrc.com/threads/60532-Teensy-4-1-Beta-Test?p=239244&viewfull=1#post239244
  187. void AudioOutputPWM::begin(void)
  188. {
  189. }
  190. void AudioOutputPWM::update(void)
  191. {
  192. audio_block_t *block;
  193. block = receiveReadOnly();
  194. if (block) release(block);
  195. }
  196. #else
  197. /*
  198. * by Frank B
  199. */
  200. static const uint8_t silence[2] = {0x80, 0x00};
  201. extern uint8_t analog_write_res;
  202. extern const struct _pwm_pin_info_struct pwm_pin_info[];
  203. audio_block_t * AudioOutputPWM::block = NULL;
  204. DMAMEM __attribute__((aligned(32))) static uint16_t pwm_tx_buffer[2][AUDIO_BLOCK_SAMPLES * 2];
  205. DMAChannel AudioOutputPWM::dma[2];
  206. _audio_info_flexpwm AudioOutputPWM::apins[2];
  207. FLASHMEM
  208. void AudioOutputPWM::begin(void) { begin(3, 4); }
  209. FLASHMEM
  210. void AudioOutputPWM::begin(uint8_t pin1, uint8_t pin2)
  211. {
  212. analogWriteResolution(8);
  213. const uint8_t pins[2] = {pin1, pin2};
  214. for (unsigned i = 0; i < 2; i++) {
  215. // use the existing code here:
  216. analogWriteFrequency(pins[i], AUDIO_SAMPLE_RATE_EXACT);
  217. analogWrite(pins[i], silence[i]);
  218. //Fill structure
  219. apins[i].pin = pins[i];
  220. apins[i].info = pwm_pin_info[apins[i].pin];
  221. uint8_t dmamux_source;
  222. if (apins[i].info.type == 1) { //only for valid flexPWM pin:
  223. unsigned module = (apins[i].info.module >> 4) & 3;
  224. unsigned submodule = apins[i].info.module & 3;
  225. switch (module) {
  226. case 0: {
  227. apins[i].flexpwm = &IMXRT_FLEXPWM1;
  228. switch (submodule) {
  229. case 0: dmamux_source = DMAMUX_SOURCE_FLEXPWM1_WRITE0; break;
  230. case 1: dmamux_source = DMAMUX_SOURCE_FLEXPWM1_WRITE1; break;
  231. case 2: dmamux_source = DMAMUX_SOURCE_FLEXPWM1_WRITE2; break;
  232. default: dmamux_source = DMAMUX_SOURCE_FLEXPWM1_WRITE3;
  233. }
  234. break;
  235. }
  236. case 1: {
  237. apins[i].flexpwm = &IMXRT_FLEXPWM2;
  238. switch (submodule) {
  239. case 0: dmamux_source = DMAMUX_SOURCE_FLEXPWM2_WRITE0; break;
  240. case 1: dmamux_source = DMAMUX_SOURCE_FLEXPWM2_WRITE1; break;
  241. case 2: dmamux_source = DMAMUX_SOURCE_FLEXPWM2_WRITE2; break;
  242. default: dmamux_source = DMAMUX_SOURCE_FLEXPWM2_WRITE3;
  243. }
  244. break;
  245. }
  246. case 2: {
  247. apins[i].flexpwm = &IMXRT_FLEXPWM3;
  248. switch (submodule) {
  249. case 0: dmamux_source = DMAMUX_SOURCE_FLEXPWM3_WRITE0; break;
  250. case 1: dmamux_source = DMAMUX_SOURCE_FLEXPWM3_WRITE1; break;
  251. case 2: dmamux_source = DMAMUX_SOURCE_FLEXPWM3_WRITE2; break;
  252. default: dmamux_source = DMAMUX_SOURCE_FLEXPWM3_WRITE3;
  253. }
  254. break;
  255. }
  256. default: {
  257. apins[i].flexpwm = &IMXRT_FLEXPWM4;
  258. switch (submodule) {
  259. case 0: dmamux_source = DMAMUX_SOURCE_FLEXPWM4_WRITE0; break;
  260. case 1: dmamux_source = DMAMUX_SOURCE_FLEXPWM4_WRITE1; break;
  261. case 2: dmamux_source = DMAMUX_SOURCE_FLEXPWM4_WRITE2; break;
  262. default: dmamux_source = DMAMUX_SOURCE_FLEXPWM4_WRITE3;
  263. }
  264. }
  265. }
  266. volatile uint16_t *valReg;
  267. switch (apins[i].info.channel) {
  268. case 0: valReg = &apins[i].flexpwm->SM[submodule].VAL0; break;
  269. case 1: valReg = &apins[i].flexpwm->SM[submodule].VAL3; break;
  270. default: valReg = &apins[i].flexpwm->SM[submodule].VAL5; break;
  271. }
  272. dma[i].begin(true);
  273. dma[i].TCD->SADDR = &pwm_tx_buffer[i][0];
  274. dma[i].TCD->SOFF = 2;
  275. dma[i].TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  276. dma[i].TCD->NBYTES_MLNO = 2;
  277. dma[i].TCD->SLAST = -sizeof(pwm_tx_buffer[0]);
  278. dma[i].TCD->DOFF = 0;
  279. dma[i].TCD->CITER_ELINKNO = sizeof(pwm_tx_buffer[0]) / 2;
  280. dma[i].TCD->DLASTSGA = 0;
  281. dma[i].TCD->BITER_ELINKNO = sizeof(pwm_tx_buffer[0]) / 2;
  282. dma[i].TCD->DADDR = valReg;
  283. dma[i].triggerAtHardwareEvent(dmamux_source);
  284. if (i == 1) { //One interrupt only
  285. dma[i].TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  286. dma[i].attachInterrupt(isr);
  287. }
  288. //set PWM-DMA-Enable
  289. apins[i].flexpwm->SM[submodule].DMAEN = FLEXPWM_SMDMAEN_VALDE;
  290. //clear inital dma data:
  291. uint32_t modulo = apins[i].flexpwm->SM[apins[i].info.module & 3].VAL1;
  292. for (unsigned j=0; j<AUDIO_BLOCK_SAMPLES * 2; j++) {
  293. uint32_t cval = (silence[i] * (modulo + 1)) >> analog_write_res;
  294. if (cval > modulo) cval = modulo;
  295. pwm_tx_buffer[i][j] = cval;
  296. }
  297. arm_dcache_flush_delete(&pwm_tx_buffer[i][0], sizeof(pwm_tx_buffer[0]) / 2 );
  298. }
  299. }
  300. dma[0].enable();
  301. dma[1].enable();
  302. update_responsibility = update_setup();
  303. //pinMode(13,OUTPUT);
  304. }
  305. void AudioOutputPWM::isr(void)
  306. {
  307. dma[1].clearInterrupt();
  308. uint16_t *dest, *dest1;
  309. uint32_t saddr = (uint32_t)(dma[0].TCD->SADDR);
  310. if (saddr < (uint32_t)&pwm_tx_buffer[0][AUDIO_BLOCK_SAMPLES]) {
  311. // DMA is transmitting the first half of the buffer
  312. // so we must fill the second half
  313. dest = &pwm_tx_buffer[0][AUDIO_BLOCK_SAMPLES];
  314. dest1 = &pwm_tx_buffer[1][AUDIO_BLOCK_SAMPLES];
  315. } else {
  316. // DMA is transmitting the second half of the buffer
  317. // so we must fill the first half
  318. dest = &pwm_tx_buffer[0][0];
  319. dest1 = &pwm_tx_buffer[1][0];
  320. }
  321. 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};
  322. if (block) {
  323. for (unsigned i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  324. uint32_t sample = (uint16_t)block->data[i] + 0x8000;
  325. uint32_t msb = ((sample >> 8) & 255)/* + 120 ???*/;
  326. uint32_t cval0 = (msb * (modulo[0] + 1)) >> analog_write_res;
  327. if (cval0 > modulo[0]) cval0 = modulo[0]; // TODO: is this check correct?
  328. *dest++ = cval0;
  329. uint32_t lsb = sample & 255;
  330. uint32_t cval1 = (lsb * (modulo[1] + 1)) >> analog_write_res;
  331. if (cval1 > modulo[1]) cval1 = modulo[1];
  332. *dest1++ = cval1;
  333. }
  334. arm_dcache_flush_delete(dest, sizeof(pwm_tx_buffer[0]) / 2 );
  335. arm_dcache_flush_delete(dest1, sizeof(pwm_tx_buffer[1]) / 2 );
  336. AudioStream::release(block);
  337. block = NULL;
  338. } else {
  339. //Serial.println(".");
  340. // fill with silence when no data available
  341. uint32_t cval0 = (silence[0] * (modulo[0] + 1)) >> analog_write_res;
  342. if (cval0 > modulo[0]) cval0 = modulo[0];
  343. uint32_t cval1 = (silence[1] * (modulo[1] + 1)) >> analog_write_res;
  344. if (cval1 > modulo[1]) cval1 = modulo[1];
  345. for (unsigned i=0; i < AUDIO_BLOCK_SAMPLES / 2; i++) {
  346. *dest++ = cval0;
  347. *dest++ = cval0;
  348. *dest1++ = cval1;
  349. *dest1++ = cval1;
  350. }
  351. arm_dcache_flush_delete(dest, sizeof(pwm_tx_buffer[0]) / 2 );
  352. arm_dcache_flush_delete(dest1, sizeof(pwm_tx_buffer[1]) / 2 );
  353. }
  354. AudioStream::update_all();
  355. //digitalWriteFast(13, !digitalRead(13));
  356. }
  357. void AudioOutputPWM::update(void)
  358. {
  359. audio_block_t *tblock;
  360. tblock = receiveReadOnly();
  361. if (!tblock) return;
  362. __disable_irq();
  363. block = tblock;
  364. __enable_irq();
  365. }
  366. #endif
  367. #endif // __IMXRT1062__