Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284
  1. #include "Audio.h"
  2. #include "arm_math.h"
  3. static arm_cfft_radix4_instance_q15 fft_inst;
  4. void AudioAnalyzeFFT256::init(void)
  5. {
  6. // TODO: replace this with static const version
  7. arm_cfft_radix4_init_q15(&fft_inst, 256, 0, 1);
  8. //for (int i=0; i<2048; i++) {
  9. //buffer[i] = i * 3;
  10. //}
  11. //__disable_irq();
  12. //ARM_DEMCR |= ARM_DEMCR_TRCENA;
  13. //ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA;
  14. //uint32_t n = ARM_DWT_CYCCNT;
  15. //arm_cfft_radix2_q15(&fft_inst, buffer);
  16. //n = ARM_DWT_CYCCNT - n;
  17. //__enable_irq();
  18. //cycles = n;
  19. //arm_cmplx_mag_q15(buffer, buffer, 512);
  20. // each audio block is 278525 cycles @ 96 MHz
  21. // 256 point fft2 takes 65408 cycles
  22. // 256 point fft4 takes 49108 cycles
  23. // 128 point cmag takes 10999 cycles
  24. // 1024 point fft2 takes 125948 cycles
  25. // 1024 point fft4 takes 125840 cycles
  26. // 512 point cmag takes 43764 cycles
  27. //state = 0;
  28. //outputflag = false;
  29. }
  30. static void copy_to_fft_buffer(void *destination, const void *source)
  31. {
  32. const int16_t *src = (const int16_t *)source;
  33. int16_t *dst = (int16_t *)destination;
  34. // TODO: optimize this
  35. for (int i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  36. *dst++ = *src++; // real
  37. *dst++ = 0; // imaginary
  38. }
  39. }
  40. // computes limit((val >> rshift), 2**bits)
  41. static inline int32_t signed_saturate_rshift(int32_t val, int bits, int rshift) __attribute__((always_inline));
  42. static inline int32_t signed_saturate_rshift(int32_t val, int bits, int rshift)
  43. {
  44. int32_t out;
  45. asm volatile("ssat %0, %1, %2, asr %3" : "=r" (out) : "I" (bits), "r" (val), "I" (rshift));
  46. return out;
  47. }
  48. static void apply_window_to_fft_buffer(void *buffer, const void *window)
  49. {
  50. int16_t *buf = (int16_t *)buffer;
  51. const int16_t *win = (int16_t *)window;;
  52. for (int i=0; i < 256; i++) {
  53. int32_t val = *buf * *win++;
  54. //*buf = signed_saturate_rshift(val, 16, 15);
  55. *buf = val >> 15;
  56. buf += 2;
  57. }
  58. }
  59. void AudioAnalyzeFFT256::update(void)
  60. {
  61. audio_block_t *block;
  62. block = receiveReadOnly();
  63. if (!block) return;
  64. if (!prevblock) {
  65. prevblock = block;
  66. return;
  67. }
  68. copy_to_fft_buffer(buffer, prevblock->data);
  69. copy_to_fft_buffer(buffer+256, block->data);
  70. //window = AudioWindowBlackmanNuttall256;
  71. //window = NULL;
  72. if (window) apply_window_to_fft_buffer(buffer, window);
  73. arm_cfft_radix4_q15(&fft_inst, buffer);
  74. // TODO: is this averaging correct? G. Heinzel's paper says we're
  75. // supposed to average the magnitude squared, then do the square
  76. // root at the end after dividing by naverage.
  77. arm_cmplx_mag_q15(buffer, buffer, 128);
  78. if (count == 0) {
  79. for (int i=0; i < 128; i++) {
  80. output[i] = buffer[i];
  81. }
  82. } else {
  83. for (int i=0; i < 128; i++) {
  84. output[i] += buffer[i];
  85. }
  86. }
  87. if (++count == naverage) {
  88. count = 0;
  89. for (int i=0; i < 128; i++) {
  90. output[i] /= naverage;
  91. }
  92. outputflag = true;
  93. }
  94. release(prevblock);
  95. prevblock = block;
  96. #if 0
  97. block = receiveReadOnly();
  98. if (state == 0) {
  99. //Serial.print("0");
  100. if (block == NULL) return;
  101. copy_to_fft_buffer(buffer, block->data);
  102. state = 1;
  103. } else if (state == 1) {
  104. //Serial.print("1");
  105. if (block == NULL) return;
  106. copy_to_fft_buffer(buffer+256, block->data);
  107. arm_cfft_radix4_q15(&fft_inst, buffer);
  108. state = 2;
  109. } else {
  110. //Serial.print("2");
  111. arm_cmplx_mag_q15(buffer, output, 128);
  112. outputflag = true;
  113. if (block == NULL) return;
  114. copy_to_fft_buffer(buffer, block->data);
  115. state = 1;
  116. }
  117. release(block);
  118. #endif
  119. }
  120. static const int16_t sine_table[] = {
  121. 0, 804, 1608, 2410, 3212, 4011, 4808, 5602, 6393, 7179,
  122. 7962, 8739, 9512, 10278, 11039, 11793, 12539, 13279, 14010, 14732,
  123. 15446, 16151, 16846, 17530, 18204, 18868, 19519, 20159, 20787, 21403,
  124. 22005, 22594, 23170, 23731, 24279, 24811, 25329, 25832, 26319, 26790,
  125. 27245, 27683, 28105, 28510, 28898, 29268, 29621, 29956, 30273, 30571,
  126. 30852, 31113, 31356, 31580, 31785, 31971, 32137, 32285, 32412, 32521,
  127. 32609, 32678, 32728, 32757, 32767, 32757, 32728, 32678, 32609, 32521,
  128. 32412, 32285, 32137, 31971, 31785, 31580, 31356, 31113, 30852, 30571,
  129. 30273, 29956, 29621, 29268, 28898, 28510, 28105, 27683, 27245, 26790,
  130. 26319, 25832, 25329, 24811, 24279, 23731, 23170, 22594, 22005, 21403,
  131. 20787, 20159, 19519, 18868, 18204, 17530, 16846, 16151, 15446, 14732,
  132. 14010, 13279, 12539, 11793, 11039, 10278, 9512, 8739, 7962, 7179,
  133. 6393, 5602, 4808, 4011, 3212, 2410, 1608, 804, 0, -804,
  134. -1608, -2410, -3212, -4011, -4808, -5602, -6393, -7179, -7962, -8739,
  135. -9512,-10278,-11039,-11793,-12539,-13279,-14010,-14732,-15446,-16151,
  136. -16846,-17530,-18204,-18868,-19519,-20159,-20787,-21403,-22005,-22594,
  137. -23170,-23731,-24279,-24811,-25329,-25832,-26319,-26790,-27245,-27683,
  138. -28105,-28510,-28898,-29268,-29621,-29956,-30273,-30571,-30852,-31113,
  139. -31356,-31580,-31785,-31971,-32137,-32285,-32412,-32521,-32609,-32678,
  140. -32728,-32757,-32767,-32757,-32728,-32678,-32609,-32521,-32412,-32285,
  141. -32137,-31971,-31785,-31580,-31356,-31113,-30852,-30571,-30273,-29956,
  142. -29621,-29268,-28898,-28510,-28105,-27683,-27245,-26790,-26319,-25832,
  143. -25329,-24811,-24279,-23731,-23170,-22594,-22005,-21403,-20787,-20159,
  144. -19519,-18868,-18204,-17530,-16846,-16151,-15446,-14732,-14010,-13279,
  145. -12539,-11793,-11039,-10278, -9512, -8739, -7962, -7179, -6393, -5602,
  146. -4808, -4011, -3212, -2410, -1608, -804, 0
  147. };
  148. void AudioSineWave::frequency(float f)
  149. {
  150. if (f > AUDIO_SAMPLE_RATE_EXACT / 2 || f < 0.0) return;
  151. phase_increment = (f / AUDIO_SAMPLE_RATE_EXACT) * 4294967296.0f;
  152. }
  153. void AudioSineWave::update(void)
  154. {
  155. audio_block_t *block;
  156. uint32_t i, ph, inc, index, scale;
  157. int32_t val1, val2, val3;
  158. //Serial.println("AudioSineWave::update");
  159. if (magnitude > 0 && (block = allocate()) != NULL) {
  160. ph = phase;
  161. inc = phase_increment;
  162. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  163. index = ph >> 24;
  164. val1 = sine_table[index];
  165. val2 = sine_table[index+1];
  166. scale = (ph >> 8) & 0xFFFF;
  167. val2 *= scale;
  168. val1 *= 0xFFFF - scale;
  169. val3 = (val1 + val2) >> 16;
  170. block->data[i] = (val3 * magnitude) >> 15;
  171. //Serial.print(block->data[i]);
  172. //Serial.print(", ");
  173. //if ((i % 12) == 11) Serial.println();
  174. ph += inc;
  175. }
  176. //Serial.println();
  177. phase = ph;
  178. transmit(block);
  179. release(block);
  180. } else {
  181. // is this numerical overflow ok?
  182. phase += phase_increment * AUDIO_BLOCK_SAMPLES;
  183. }
  184. }
  185. void AudioSineWaveMod::frequency(float f)
  186. {
  187. if (f > AUDIO_SAMPLE_RATE_EXACT / 2 || f < 0.0) return;
  188. phase_increment = (f / AUDIO_SAMPLE_RATE_EXACT) * 4294967296.0f;
  189. }
  190. void AudioSineWaveMod::update(void)
  191. {
  192. audio_block_t *block, *modinput;
  193. uint32_t i, ph, inc, index, scale;
  194. int32_t val1, val2;
  195. //Serial.println("AudioSineWave::update");
  196. modinput = receiveReadOnly();
  197. ph = phase;
  198. inc = phase_increment;
  199. block = allocate();
  200. if (!block) {
  201. // unable to allocate memory, so we'll send nothing
  202. if (modinput) {
  203. // but if we got modulation data, update the phase
  204. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  205. ph += inc + modinput->data[i] * modulation_factor;
  206. }
  207. release(modinput);
  208. } else {
  209. ph += phase_increment * AUDIO_BLOCK_SAMPLES;
  210. }
  211. phase = ph;
  212. return;
  213. }
  214. if (modinput) {
  215. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  216. index = ph >> 24;
  217. val1 = sine_table[index];
  218. val2 = sine_table[index+1];
  219. scale = (ph >> 8) & 0xFFFF;
  220. val2 *= scale;
  221. val1 *= 0xFFFF - scale;
  222. block->data[i] = (val1 + val2) >> 16;
  223. //Serial.print(block->data[i]);
  224. //Serial.print(", ");
  225. //if ((i % 12) == 11) Serial.println();
  226. ph += inc + modinput->data[i] * modulation_factor;
  227. }
  228. release(modinput);
  229. } else {
  230. ph = phase;
  231. inc = phase_increment;
  232. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  233. index = ph >> 24;
  234. val1 = sine_table[index];
  235. val2 = sine_table[index+1];
  236. scale = (ph >> 8) & 0xFFFF;
  237. val2 *= scale;
  238. val1 *= 0xFFFF - scale;
  239. block->data[i] = (val1 + val2) >> 16;
  240. //Serial.print(block->data[i]);
  241. //Serial.print(", ");
  242. //if ((i % 12) == 11) Serial.println();
  243. ph += inc;
  244. }
  245. }
  246. phase = ph;
  247. transmit(block);
  248. release(block);
  249. }
  250. /******************************************************************/
  251. void AudioPrint::update(void)
  252. {
  253. audio_block_t *block;
  254. uint32_t i;
  255. Serial.println("AudioPrint::update");
  256. Serial.println(name);
  257. block = receiveReadOnly();
  258. if (block) {
  259. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  260. Serial.print(block->data[i]);
  261. Serial.print(", ");
  262. if ((i % 12) == 11) Serial.println();
  263. }
  264. Serial.println();
  265. release(block);
  266. }
  267. }
  268. /******************************************************************/
  269. audio_block_t * AudioOutputPWM::block_1st = NULL;
  270. audio_block_t * AudioOutputPWM::block_2nd = NULL;
  271. uint32_t AudioOutputPWM::block_offset = 0;
  272. bool AudioOutputPWM::update_responsibility = false;
  273. uint8_t AudioOutputPWM::interrupt_count = 0;
  274. DMAMEM uint32_t pwm_dma_buffer[AUDIO_BLOCK_SAMPLES*2];
  275. void AudioOutputPWM::begin(void)
  276. {
  277. //Serial.println("AudioPwmOutput constructor");
  278. block_1st = NULL;
  279. FTM1_SC = 0;
  280. FTM1_CNT = 0;
  281. FTM1_MOD = 543;
  282. FTM1_C0SC = 0x69; // send DMA request on match
  283. FTM1_C1SC = 0x28;
  284. FTM1_SC = FTM_SC_CLKS(1) | FTM_SC_PS(0);
  285. CORE_PIN3_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  286. CORE_PIN4_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  287. FTM1_C0V = 120; // range 120 to 375
  288. FTM1_C1V = 0; // range 0 to 255
  289. for (int i=0; i<256; i+=2) {
  290. pwm_dma_buffer[i] = 120; // zero must not be used
  291. pwm_dma_buffer[i+1] = 0;
  292. }
  293. SIM_SCGC7 |= SIM_SCGC7_DMA;
  294. SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
  295. DMA_CR = 0;
  296. DMA_TCD3_SADDR = pwm_dma_buffer;
  297. DMA_TCD3_SOFF = 4;
  298. DMA_TCD3_ATTR = DMA_TCD_ATTR_SSIZE(2) | DMA_TCD_ATTR_DSIZE(2) | DMA_TCD_ATTR_DMOD(4);
  299. DMA_TCD3_NBYTES_MLNO = 8;
  300. DMA_TCD3_SLAST = -sizeof(pwm_dma_buffer);
  301. DMA_TCD3_DADDR = &FTM1_C0V;
  302. DMA_TCD3_DOFF = 8;
  303. DMA_TCD3_CITER_ELINKNO = sizeof(pwm_dma_buffer) / 8;
  304. DMA_TCD3_DLASTSGA = 0;
  305. DMA_TCD3_BITER_ELINKNO = sizeof(pwm_dma_buffer) / 8;
  306. DMA_TCD3_CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  307. DMAMUX0_CHCFG3 = DMAMUX_DISABLE;
  308. DMAMUX0_CHCFG3 = DMAMUX_SOURCE_FTM1_CH0 | DMAMUX_ENABLE;
  309. DMA_SERQ = 3;
  310. update_responsibility = update_setup();
  311. NVIC_ENABLE_IRQ(IRQ_DMA_CH3);
  312. }
  313. void AudioOutputPWM::update(void)
  314. {
  315. audio_block_t *block;
  316. block = receiveReadOnly();
  317. if (!block) return;
  318. __disable_irq();
  319. if (block_1st == NULL) {
  320. block_1st = block;
  321. block_offset = 0;
  322. __enable_irq();
  323. } else if (block_2nd == NULL) {
  324. block_2nd = block;
  325. __enable_irq();
  326. } else {
  327. audio_block_t *tmp = block_1st;
  328. block_1st = block_2nd;
  329. block_2nd = block;
  330. block_offset = 0;
  331. __enable_irq();
  332. release(tmp);
  333. }
  334. }
  335. void dma_ch3_isr(void)
  336. {
  337. int16_t *src;
  338. uint32_t *dest;
  339. audio_block_t *block;
  340. uint32_t saddr, offset;
  341. saddr = (uint32_t)DMA_TCD3_SADDR;
  342. DMA_CINT = 3;
  343. if (saddr < (uint32_t)pwm_dma_buffer + sizeof(pwm_dma_buffer) / 2) {
  344. // DMA is transmitting the first half of the buffer
  345. // so we must fill the second half
  346. dest = &pwm_dma_buffer[AUDIO_BLOCK_SAMPLES];
  347. } else {
  348. // DMA is transmitting the second half of the buffer
  349. // so we must fill the first half
  350. dest = pwm_dma_buffer;
  351. }
  352. block = AudioOutputPWM::block_1st;
  353. offset = AudioOutputPWM::block_offset;
  354. if (block) {
  355. src = &block->data[offset];
  356. for (int i=0; i < AUDIO_BLOCK_SAMPLES/4; i++) {
  357. uint16_t sample = *src++ + 0x8000;
  358. uint32_t msb = ((sample >> 8) & 255) + 120;
  359. uint32_t lsb = sample & 255;
  360. *dest++ = msb;
  361. *dest++ = lsb;
  362. *dest++ = msb;
  363. *dest++ = lsb;
  364. }
  365. offset += AUDIO_BLOCK_SAMPLES/4;
  366. if (offset < AUDIO_BLOCK_SAMPLES) {
  367. AudioOutputPWM::block_offset = offset;
  368. } else {
  369. AudioOutputPWM::block_offset = 0;
  370. AudioStream::release(block);
  371. AudioOutputPWM::block_1st = AudioOutputPWM::block_2nd;
  372. AudioOutputPWM::block_2nd = NULL;
  373. }
  374. } else {
  375. // fill with silence when no data available
  376. for (int i=0; i < AUDIO_BLOCK_SAMPLES/4; i++) {
  377. *dest++ = 248;
  378. *dest++ = 0;
  379. *dest++ = 248;
  380. *dest++ = 0;
  381. }
  382. }
  383. if (AudioOutputPWM::update_responsibility) {
  384. if (++AudioOutputPWM::interrupt_count >= 4) {
  385. AudioOutputPWM::interrupt_count = 0;
  386. AudioStream::update_all();
  387. }
  388. }
  389. }
  390. // DMA target is: (registers require 32 bit writes)
  391. // 40039010 Channel 0 Value (FTM1_C0V)
  392. // 40039018 Channel 1 Value (FTM1_C1V)
  393. // TCD:
  394. // source address = buffer address
  395. // source offset = 4 bytes
  396. // attr = no src mod, ssize = 32 bit, dest mod = 16 bytes (4), dsize = 32 bit
  397. // minor loop byte count = 8
  398. // source last adjust = -sizeof(buffer)
  399. // dest address = FTM1_C0V
  400. // dest address offset = 8
  401. // citer = sizeof(buffer) / 8 (no minor loop linking)
  402. // dest last adjust = 0 (dest modulo keeps it ready for more)
  403. // control:
  404. // throttling = 0
  405. // major link to same channel
  406. // done = 0
  407. // active = 0
  408. // majorlink = 1
  409. // scatter/gather = 0
  410. // disable request = 0
  411. // inthalf = 1
  412. // intmajor = 1
  413. // start = 0
  414. // biter = sizeof(buffer) / 8 (no minor loop linking)
  415. /******************************************************************/
  416. // MCLK needs to be 48e6 / 1088 * 256 = 11.29411765 MHz -> 44.117647 kHz sample rate
  417. // Possible to create using fractional divider for all USB-compatible Kinetis:
  418. // MCLK = 16e6 * 12 / 17
  419. // MCLK = 24e6 * 8 / 17
  420. // MCLK = 48e6 * 4 / 17
  421. // MCLK = 72e6 * 8 / 51
  422. // MCLK = 96e6 * 2 / 17
  423. // MCLK = 120e6 * 8 / 85
  424. // TODO: instigate using I2S0_MCR to select the crystal directly instead of the system
  425. // clock, which has audio band jitter from the PLL
  426. audio_block_t * AudioOutputI2S::block_left_1st = NULL;
  427. audio_block_t * AudioOutputI2S::block_right_1st = NULL;
  428. audio_block_t * AudioOutputI2S::block_left_2nd = NULL;
  429. audio_block_t * AudioOutputI2S::block_right_2nd = NULL;
  430. uint16_t AudioOutputI2S::block_left_offset = 0;
  431. uint16_t AudioOutputI2S::block_right_offset = 0;
  432. bool AudioOutputI2S::update_responsibility = false;
  433. DMAMEM static uint32_t i2s_tx_buffer[AUDIO_BLOCK_SAMPLES];
  434. void AudioOutputI2S::begin(void)
  435. {
  436. //pinMode(2, OUTPUT);
  437. block_left_1st = NULL;
  438. block_right_1st = NULL;
  439. config_i2s();
  440. CORE_PIN22_CONFIG = PORT_PCR_MUX(6); // pin 22, PTC1, I2S0_TXD0
  441. DMA_CR = 0;
  442. DMA_TCD0_SADDR = i2s_tx_buffer;
  443. DMA_TCD0_SOFF = 2;
  444. DMA_TCD0_ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  445. DMA_TCD0_NBYTES_MLNO = 2;
  446. DMA_TCD0_SLAST = -sizeof(i2s_tx_buffer);
  447. DMA_TCD0_DADDR = &I2S0_TDR0;
  448. DMA_TCD0_DOFF = 0;
  449. DMA_TCD0_CITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  450. DMA_TCD0_DLASTSGA = 0;
  451. DMA_TCD0_BITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  452. DMA_TCD0_CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  453. DMAMUX0_CHCFG0 = DMAMUX_DISABLE;
  454. DMAMUX0_CHCFG0 = DMAMUX_SOURCE_I2S0_TX | DMAMUX_ENABLE;
  455. update_responsibility = update_setup();
  456. DMA_SERQ = 0;
  457. I2S0_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE | I2S_TCSR_FR;
  458. NVIC_ENABLE_IRQ(IRQ_DMA_CH0);
  459. }
  460. void dma_ch0_isr(void)
  461. {
  462. const int16_t *src, *end;
  463. int16_t *dest;
  464. audio_block_t *block;
  465. uint32_t saddr, offset;
  466. saddr = (uint32_t)DMA_TCD0_SADDR;
  467. DMA_CINT = 0;
  468. if (saddr < (uint32_t)i2s_tx_buffer + sizeof(i2s_tx_buffer) / 2) {
  469. // DMA is transmitting the first half of the buffer
  470. // so we must fill the second half
  471. dest = (int16_t *)&i2s_tx_buffer[AUDIO_BLOCK_SAMPLES/2];
  472. end = (int16_t *)&i2s_tx_buffer[AUDIO_BLOCK_SAMPLES];
  473. if (AudioOutputI2S::update_responsibility) AudioStream::update_all();
  474. } else {
  475. // DMA is transmitting the second half of the buffer
  476. // so we must fill the first half
  477. dest = (int16_t *)i2s_tx_buffer;
  478. end = (int16_t *)&i2s_tx_buffer[AUDIO_BLOCK_SAMPLES/2];
  479. }
  480. // TODO: these copy routines could be merged and optimized, maybe in assembly?
  481. block = AudioOutputI2S::block_left_1st;
  482. if (block) {
  483. offset = AudioOutputI2S::block_left_offset;
  484. src = &block->data[offset];
  485. do {
  486. *dest = *src++;
  487. dest += 2;
  488. } while (dest < end);
  489. offset += AUDIO_BLOCK_SAMPLES/2;
  490. if (offset < AUDIO_BLOCK_SAMPLES) {
  491. AudioOutputI2S::block_left_offset = offset;
  492. } else {
  493. AudioOutputI2S::block_left_offset = 0;
  494. AudioStream::release(block);
  495. AudioOutputI2S::block_left_1st = AudioOutputI2S::block_left_2nd;
  496. AudioOutputI2S::block_left_2nd = NULL;
  497. }
  498. } else {
  499. do {
  500. *dest = 0;
  501. dest += 2;
  502. } while (dest < end);
  503. }
  504. dest -= AUDIO_BLOCK_SAMPLES - 1;
  505. block = AudioOutputI2S::block_right_1st;
  506. if (block) {
  507. offset = AudioOutputI2S::block_right_offset;
  508. src = &block->data[offset];
  509. do {
  510. *dest = *src++;
  511. dest += 2;
  512. } while (dest < end);
  513. offset += AUDIO_BLOCK_SAMPLES/2;
  514. if (offset < AUDIO_BLOCK_SAMPLES) {
  515. AudioOutputI2S::block_right_offset = offset;
  516. } else {
  517. AudioOutputI2S::block_right_offset = 0;
  518. AudioStream::release(block);
  519. AudioOutputI2S::block_right_1st = AudioOutputI2S::block_right_2nd;
  520. AudioOutputI2S::block_right_2nd = NULL;
  521. }
  522. } else {
  523. do {
  524. *dest = 0;
  525. dest += 2;
  526. } while (dest < end);
  527. }
  528. }
  529. void AudioOutputI2S::update(void)
  530. {
  531. // null audio device: discard all incoming data
  532. //if (!active) return;
  533. //audio_block_t *block = receiveReadOnly();
  534. //if (block) release(block);
  535. audio_block_t *block;
  536. block = receiveReadOnly(0); // input 0 = left channel
  537. if (block) {
  538. __disable_irq();
  539. if (block_left_1st == NULL) {
  540. block_left_1st = block;
  541. block_left_offset = 0;
  542. __enable_irq();
  543. } else if (block_left_2nd == NULL) {
  544. block_left_2nd = block;
  545. __enable_irq();
  546. } else {
  547. audio_block_t *tmp = block_left_1st;
  548. block_left_1st = block_left_2nd;
  549. block_left_2nd = block;
  550. block_left_offset = 0;
  551. __enable_irq();
  552. release(tmp);
  553. }
  554. }
  555. block = receiveReadOnly(1); // input 1 = right channel
  556. if (block) {
  557. __disable_irq();
  558. if (block_right_1st == NULL) {
  559. block_right_1st = block;
  560. block_right_offset = 0;
  561. __enable_irq();
  562. } else if (block_right_2nd == NULL) {
  563. block_right_2nd = block;
  564. __enable_irq();
  565. } else {
  566. audio_block_t *tmp = block_right_1st;
  567. block_right_1st = block_right_2nd;
  568. block_right_2nd = block;
  569. block_right_offset = 0;
  570. __enable_irq();
  571. release(tmp);
  572. }
  573. }
  574. }
  575. void AudioOutputI2S::config_i2s(void)
  576. {
  577. SIM_SCGC6 |= SIM_SCGC6_I2S;
  578. SIM_SCGC7 |= SIM_SCGC7_DMA;
  579. SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
  580. // if either transmitter or receiver is enabled, do nothing
  581. if (I2S0_TCSR & I2S_TCSR_TE) return;
  582. if (I2S0_RCSR & I2S_RCSR_RE) return;
  583. // enable MCLK output
  584. I2S0_MCR = I2S_MCR_MICS(3) | I2S_MCR_MOE;
  585. I2S0_MDR = I2S_MDR_FRACT(1) | I2S_MDR_DIVIDE(16);
  586. // configure transmitter
  587. I2S0_TMR = 0;
  588. I2S0_TCR1 = I2S_TCR1_TFW(1); // watermark at half fifo size
  589. I2S0_TCR2 = I2S_TCR2_SYNC(0) | I2S_TCR2_BCP | I2S_TCR2_MSEL(1)
  590. | I2S_TCR2_BCD | I2S_TCR2_DIV(3);
  591. I2S0_TCR3 = I2S_TCR3_TCE;
  592. I2S0_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(15) | I2S_TCR4_MF
  593. | I2S_TCR4_FSE | I2S_TCR4_FSP | I2S_TCR4_FSD;
  594. I2S0_TCR5 = I2S_TCR5_WNW(15) | I2S_TCR5_W0W(15) | I2S_TCR5_FBT(15);
  595. // configure receiver (sync'd to transmitter clocks)
  596. I2S0_RMR = 0;
  597. I2S0_RCR1 = I2S_RCR1_RFW(1);
  598. I2S0_RCR2 = I2S_RCR2_SYNC(1) | I2S_TCR2_BCP | I2S_RCR2_MSEL(1)
  599. | I2S_RCR2_BCD | I2S_RCR2_DIV(3);
  600. I2S0_RCR3 = I2S_RCR3_RCE;
  601. I2S0_RCR4 = I2S_RCR4_FRSZ(1) | I2S_RCR4_SYWD(15) | I2S_RCR4_MF
  602. | I2S_RCR4_FSE | I2S_RCR4_FSP | I2S_RCR4_FSD;
  603. I2S0_RCR5 = I2S_RCR5_WNW(15) | I2S_RCR5_W0W(15) | I2S_RCR5_FBT(15);
  604. // configure pin mux for 3 clock signals
  605. CORE_PIN23_CONFIG = PORT_PCR_MUX(6); // pin 23, PTC2, I2S0_TX_FS (LRCLK)
  606. CORE_PIN9_CONFIG = PORT_PCR_MUX(6); // pin 9, PTC3, I2S0_TX_BCLK
  607. CORE_PIN11_CONFIG = PORT_PCR_MUX(6); // pin 11, PTC6, I2S0_MCLK
  608. }
  609. /******************************************************************/
  610. DMAMEM static uint32_t i2s_rx_buffer[AUDIO_BLOCK_SAMPLES];
  611. audio_block_t * AudioInputI2S::block_left = NULL;
  612. audio_block_t * AudioInputI2S::block_right = NULL;
  613. uint16_t AudioInputI2S::block_offset = 0;
  614. bool AudioInputI2S::update_responsibility = false;
  615. void AudioInputI2S::begin(void)
  616. {
  617. //block_left_1st = NULL;
  618. //block_right_1st = NULL;
  619. //pinMode(3, OUTPUT);
  620. //digitalWriteFast(3, HIGH);
  621. //delayMicroseconds(500);
  622. //digitalWriteFast(3, LOW);
  623. AudioOutputI2S::config_i2s();
  624. CORE_PIN13_CONFIG = PORT_PCR_MUX(4); // pin 13, PTC5, I2S0_RXD0
  625. DMA_CR = 0;
  626. DMA_TCD1_SADDR = &I2S0_RDR0;
  627. DMA_TCD1_SOFF = 0;
  628. DMA_TCD1_ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  629. DMA_TCD1_NBYTES_MLNO = 2;
  630. DMA_TCD1_SLAST = 0;
  631. DMA_TCD1_DADDR = i2s_rx_buffer;
  632. DMA_TCD1_DOFF = 2;
  633. DMA_TCD1_CITER_ELINKNO = sizeof(i2s_rx_buffer) / 2;
  634. DMA_TCD1_DLASTSGA = -sizeof(i2s_rx_buffer);
  635. DMA_TCD1_BITER_ELINKNO = sizeof(i2s_rx_buffer) / 2;
  636. DMA_TCD1_CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  637. DMAMUX0_CHCFG1 = DMAMUX_DISABLE;
  638. DMAMUX0_CHCFG1 = DMAMUX_SOURCE_I2S0_RX | DMAMUX_ENABLE;
  639. update_responsibility = update_setup();
  640. DMA_SERQ = 1;
  641. // TODO: is I2S_RCSR_BCE appropriate if sync'd to transmitter clock?
  642. //I2S0_RCSR |= I2S_RCSR_RE | I2S_RCSR_BCE | I2S_RCSR_FRDE | I2S_RCSR_FR;
  643. I2S0_RCSR |= I2S_RCSR_RE | I2S_RCSR_FRDE | I2S_RCSR_FR;
  644. NVIC_ENABLE_IRQ(IRQ_DMA_CH1);
  645. }
  646. void dma_ch1_isr(void)
  647. {
  648. uint32_t daddr, offset;
  649. const int16_t *src, *end;
  650. int16_t *dest_left, *dest_right;
  651. audio_block_t *left, *right;
  652. //digitalWriteFast(3, HIGH);
  653. daddr = (uint32_t)DMA_TCD1_DADDR;
  654. DMA_CINT = 1;
  655. if (daddr < (uint32_t)i2s_rx_buffer + sizeof(i2s_rx_buffer) / 2) {
  656. // DMA is receiving to the first half of the buffer
  657. // need to remove data from the second half
  658. src = (int16_t *)&i2s_rx_buffer[AUDIO_BLOCK_SAMPLES/2];
  659. end = (int16_t *)&i2s_rx_buffer[AUDIO_BLOCK_SAMPLES];
  660. if (AudioInputI2S::update_responsibility) AudioStream::update_all();
  661. } else {
  662. // DMA is receiving to the second half of the buffer
  663. // need to remove data from the first half
  664. src = (int16_t *)&i2s_rx_buffer[0];
  665. end = (int16_t *)&i2s_rx_buffer[AUDIO_BLOCK_SAMPLES/2];
  666. }
  667. left = AudioInputI2S::block_left;
  668. right = AudioInputI2S::block_right;
  669. if (left != NULL && right != NULL) {
  670. offset = AudioInputI2S::block_offset;
  671. if (offset <= AUDIO_BLOCK_SAMPLES/2) {
  672. dest_left = &(left->data[offset]);
  673. dest_right = &(right->data[offset]);
  674. AudioInputI2S::block_offset = offset + AUDIO_BLOCK_SAMPLES/2;
  675. do {
  676. //n = *src++;
  677. //*dest_left++ = (int16_t)n;
  678. //*dest_right++ = (int16_t)(n >> 16);
  679. *dest_left++ = *src++;
  680. *dest_right++ = *src++;
  681. } while (src < end);
  682. }
  683. }
  684. //digitalWriteFast(3, LOW);
  685. }
  686. void AudioInputI2S::update(void)
  687. {
  688. audio_block_t *new_left=NULL, *new_right=NULL, *out_left=NULL, *out_right=NULL;
  689. // allocate 2 new blocks, but if one fails, allocate neither
  690. new_left = allocate();
  691. if (new_left != NULL) {
  692. new_right = allocate();
  693. if (new_right == NULL) {
  694. release(new_left);
  695. new_left = NULL;
  696. }
  697. }
  698. __disable_irq();
  699. if (block_offset >= AUDIO_BLOCK_SAMPLES) {
  700. // the DMA filled 2 blocks, so grab them and get the
  701. // 2 new blocks to the DMA, as quickly as possible
  702. out_left = block_left;
  703. block_left = new_left;
  704. out_right = block_right;
  705. block_right = new_right;
  706. block_offset = 0;
  707. __enable_irq();
  708. // then transmit the DMA's former blocks
  709. transmit(out_left, 0);
  710. release(out_left);
  711. transmit(out_right, 1);
  712. release(out_right);
  713. //Serial.print(".");
  714. } else if (new_left != NULL) {
  715. // the DMA didn't fill blocks, but we allocated blocks
  716. if (block_left == NULL) {
  717. // the DMA doesn't have any blocks to fill, so
  718. // give it the ones we just allocated
  719. block_left = new_left;
  720. block_right = new_right;
  721. block_offset = 0;
  722. __enable_irq();
  723. } else {
  724. // the DMA already has blocks, doesn't need these
  725. __enable_irq();
  726. release(new_left);
  727. release(new_right);
  728. }
  729. } else {
  730. // The DMA didn't fill blocks, and we could not allocate
  731. // memory... the system is likely starving for memory!
  732. // Sadly, there's nothing we can do.
  733. __enable_irq();
  734. }
  735. }
  736. /******************************************************************/
  737. DMAMEM static uint16_t analog_rx_buffer[AUDIO_BLOCK_SAMPLES];
  738. audio_block_t * AudioInputAnalog::block_left = NULL;
  739. uint16_t AudioInputAnalog::block_offset = 0;
  740. #define PDB_CONFIG (PDB_SC_TRGSEL(15) | PDB_SC_PDBEN | PDB_SC_CONT)
  741. #define PDB_PERIOD 1087 // 48e6 / 44100
  742. void AudioInputAnalog::begin(unsigned int pin)
  743. {
  744. uint32_t i, sum=0;
  745. // pin must be 0 to 13 (for A0 to A13)
  746. // or 14 to 23 for digital pin numbers A0-A9
  747. // or 34 to 37 corresponding to A10-A13
  748. if (pin > 23 && !(pin >= 34 && pin <= 37)) return;
  749. //pinMode(2, OUTPUT);
  750. //pinMode(3, OUTPUT);
  751. //digitalWriteFast(3, HIGH);
  752. //delayMicroseconds(500);
  753. //digitalWriteFast(3, LOW);
  754. // Configure the ADC and run at least one software-triggered
  755. // conversion. This completes the self calibration stuff and
  756. // leaves the ADC in a state that's mostly ready to use
  757. analogReadRes(16);
  758. analogReference(INTERNAL); // range 0 to 1.2 volts
  759. //analogReference(DEFAULT); // range 0 to 3.3 volts
  760. analogReadAveraging(8);
  761. // Actually, do many normal reads, to start with a nice DC level
  762. for (i=0; i < 1024; i++) {
  763. sum += analogRead(pin);
  764. }
  765. dc_average = sum >> 10;
  766. // testing only, enable adc interrupt
  767. //ADC0_SC1A |= ADC_SC1_AIEN;
  768. //while ((ADC0_SC1A & ADC_SC1_COCO) == 0) ; // wait
  769. //NVIC_ENABLE_IRQ(IRQ_ADC0);
  770. // set the programmable delay block to trigger the ADC at 44.1 kHz
  771. SIM_SCGC6 |= SIM_SCGC6_PDB;
  772. PDB0_MOD = PDB_PERIOD;
  773. PDB0_SC = PDB_CONFIG | PDB_SC_LDOK;
  774. PDB0_SC = PDB_CONFIG | PDB_SC_SWTRIG;
  775. PDB0_CH0C1 = 0x0101;
  776. // enable the ADC for hardware trigger and DMA
  777. ADC0_SC2 |= ADC_SC2_ADTRG | ADC_SC2_DMAEN;
  778. // set up a DMA channel to store the ADC data
  779. SIM_SCGC7 |= SIM_SCGC7_DMA;
  780. SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
  781. DMA_CR = 0;
  782. DMA_TCD2_SADDR = &ADC0_RA;
  783. DMA_TCD2_SOFF = 0;
  784. DMA_TCD2_ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  785. DMA_TCD2_NBYTES_MLNO = 2;
  786. DMA_TCD2_SLAST = 0;
  787. DMA_TCD2_DADDR = analog_rx_buffer;
  788. DMA_TCD2_DOFF = 2;
  789. DMA_TCD2_CITER_ELINKNO = sizeof(analog_rx_buffer) / 2;
  790. DMA_TCD2_DLASTSGA = -sizeof(analog_rx_buffer);
  791. DMA_TCD2_BITER_ELINKNO = sizeof(analog_rx_buffer) / 2;
  792. DMA_TCD2_CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  793. DMAMUX0_CHCFG2 = DMAMUX_DISABLE;
  794. DMAMUX0_CHCFG2 = DMAMUX_SOURCE_ADC0 | DMAMUX_ENABLE;
  795. //update_responsibility = update_setup();
  796. DMA_SERQ = 2;
  797. NVIC_ENABLE_IRQ(IRQ_DMA_CH2);
  798. }
  799. void dma_ch2_isr(void)
  800. {
  801. uint32_t daddr, offset;
  802. const uint16_t *src, *end;
  803. uint16_t *dest_left;
  804. audio_block_t *left;
  805. //digitalWriteFast(3, HIGH);
  806. daddr = (uint32_t)DMA_TCD2_DADDR;
  807. DMA_CINT = 2;
  808. if (daddr < (uint32_t)analog_rx_buffer + sizeof(analog_rx_buffer) / 2) {
  809. // DMA is receiving to the first half of the buffer
  810. // need to remove data from the second half
  811. src = (uint16_t *)&analog_rx_buffer[AUDIO_BLOCK_SAMPLES/2];
  812. end = (uint16_t *)&analog_rx_buffer[AUDIO_BLOCK_SAMPLES];
  813. //if (AudioInputI2S::update_responsibility) AudioStream::update_all();
  814. } else {
  815. // DMA is receiving to the second half of the buffer
  816. // need to remove data from the first half
  817. src = (uint16_t *)&analog_rx_buffer[0];
  818. end = (uint16_t *)&analog_rx_buffer[AUDIO_BLOCK_SAMPLES/2];
  819. }
  820. left = AudioInputAnalog::block_left;
  821. if (left != NULL) {
  822. offset = AudioInputAnalog::block_offset;
  823. if (offset > AUDIO_BLOCK_SAMPLES/2) offset = AUDIO_BLOCK_SAMPLES/2;
  824. //if (offset <= AUDIO_BLOCK_SAMPLES/2) {
  825. dest_left = (uint16_t *)&(left->data[offset]);
  826. AudioInputAnalog::block_offset = offset + AUDIO_BLOCK_SAMPLES/2;
  827. do {
  828. *dest_left++ = *src++;
  829. } while (src < end);
  830. //}
  831. }
  832. //digitalWriteFast(3, LOW);
  833. }
  834. #if 0
  835. void adc0_isr(void)
  836. {
  837. uint32_t tmp = ADC0_RA; // read ADC result to clear interrupt
  838. digitalWriteFast(3, HIGH);
  839. delayMicroseconds(1);
  840. digitalWriteFast(3, LOW);
  841. }
  842. #endif
  843. void AudioInputAnalog::update(void)
  844. {
  845. audio_block_t *new_left=NULL, *out_left=NULL;
  846. unsigned int dc, offset;
  847. int16_t s, *p, *end;
  848. // allocate new block (ok if NULL)
  849. new_left = allocate();
  850. __disable_irq();
  851. offset = block_offset;
  852. if (offset < AUDIO_BLOCK_SAMPLES) {
  853. // the DMA didn't fill a block
  854. if (new_left != NULL) {
  855. // but we allocated a block
  856. if (block_left == NULL) {
  857. // the DMA doesn't have any blocks to fill, so
  858. // give it the one we just allocated
  859. block_left = new_left;
  860. block_offset = 0;
  861. __enable_irq();
  862. //Serial.println("fail1");
  863. } else {
  864. // the DMA already has blocks, doesn't need this
  865. __enable_irq();
  866. release(new_left);
  867. //Serial.print("fail2, offset=");
  868. //Serial.println(offset);
  869. }
  870. } else {
  871. // The DMA didn't fill a block, and we could not allocate
  872. // memory... the system is likely starving for memory!
  873. // Sadly, there's nothing we can do.
  874. __enable_irq();
  875. //Serial.println("fail3");
  876. }
  877. return;
  878. }
  879. // the DMA filled a block, so grab it and get the
  880. // new block to the DMA, as quickly as possible
  881. out_left = block_left;
  882. block_left = new_left;
  883. block_offset = 0;
  884. __enable_irq();
  885. // find and subtract DC offset....
  886. // TODO: this may not be correct, needs testing with more types of signals
  887. dc = dc_average;
  888. p = out_left->data;
  889. end = p + AUDIO_BLOCK_SAMPLES;
  890. do {
  891. s = (uint16_t)(*p) - dc; // TODO: should be saturating subtract
  892. *p++ = s;
  893. dc += s >> 13; // approx 5.38 Hz high pass filter
  894. } while (p < end);
  895. dc_average = dc;
  896. // then transmit the AC data
  897. transmit(out_left);
  898. release(out_left);
  899. }
  900. /******************************************************************/
  901. #define STATE_DIRECT_8BIT_MONO 0 // playing mono at native sample rate
  902. #define STATE_DIRECT_8BIT_STEREO 1 // playing stereo at native sample rate
  903. #define STATE_DIRECT_16BIT_MONO 2 // playing mono at native sample rate
  904. #define STATE_DIRECT_16BIT_STEREO 3 // playing stereo at native sample rate
  905. #define STATE_CONVERT_8BIT_MONO 4 // playing mono, converting sample rate
  906. #define STATE_CONVERT_8BIT_STEREO 5 // playing stereo, converting sample rate
  907. #define STATE_CONVERT_16BIT_MONO 6 // playing mono, converting sample rate
  908. #define STATE_CONVERT_16BIT_STEREO 7 // playing stereo, converting sample rate
  909. #define STATE_PARSE1 8 // looking for 20 byte ID header
  910. #define STATE_PARSE2 9 // looking for 16 byte format header
  911. #define STATE_PARSE3 10 // looking for 8 byte data header
  912. #define STATE_PARSE4 11 // ignoring unknown chunk
  913. #define STATE_STOP 12
  914. void AudioPlaySDcardWAV::begin(void)
  915. {
  916. state = STATE_STOP;
  917. state_play = STATE_STOP;
  918. data_length = 0;
  919. if (block_left) {
  920. release(block_left);
  921. block_left = NULL;
  922. }
  923. if (block_right) {
  924. release(block_right);
  925. block_right = NULL;
  926. }
  927. }
  928. bool AudioPlaySDcardWAV::play(const char *filename)
  929. {
  930. stop();
  931. wavfile = SD.open(filename);
  932. if (!wavfile) return false;
  933. buffer_remaining = 0;
  934. state_play = STATE_STOP;
  935. state = STATE_PARSE1;
  936. return true;
  937. }
  938. void AudioPlaySDcardWAV::stop(void)
  939. {
  940. __disable_irq();
  941. if (state != STATE_STOP) {
  942. state = STATE_STOP;
  943. __enable_irq();
  944. wavfile.close();
  945. } else {
  946. __enable_irq();
  947. }
  948. }
  949. bool AudioPlaySDcardWAV::start(void)
  950. {
  951. __disable_irq();
  952. if (state == STATE_STOP) {
  953. if (state_play == STATE_STOP) {
  954. __enable_irq();
  955. return false;
  956. }
  957. state = state_play;
  958. }
  959. __enable_irq();
  960. return true;
  961. }
  962. void AudioPlaySDcardWAV::update(void)
  963. {
  964. // only update if we're playing
  965. if (state == STATE_STOP) return;
  966. // allocate the audio blocks to transmit
  967. block_left = allocate();
  968. if (block_left == NULL) return;
  969. if (state >= 8 || (state & 1) == 1) {
  970. block_right = allocate();
  971. if (block_right == NULL) {
  972. release(block_left);
  973. return;
  974. }
  975. } else {
  976. block_right = NULL;
  977. }
  978. block_offset = 0;
  979. //Serial.println("update");
  980. // is there buffered data?
  981. if (buffer_remaining > 0) {
  982. // we have buffered data
  983. if (consume()) return; // it was enough to transmit audio
  984. }
  985. if (state != STATE_STOP && wavfile.available()) {
  986. // we can read more data from the file...
  987. buffer_remaining = wavfile.read(buffer, 512);
  988. if (consume()) {
  989. // good, it resulted in audio transmit
  990. return;
  991. } else {
  992. // not good, no audio was transmitted
  993. buffer_remaining = 0;
  994. if (block_left) release(block_left);
  995. if (block_right) release(block_right);
  996. // if we're still playing, well, there's going to
  997. // be a gap in output, but we can't keep burning
  998. // time trying to read more data. Hopefully things
  999. // will go better next time?
  1000. if (state != STATE_STOP) return;
  1001. }
  1002. }
  1003. // end of file reached or other reason to stop
  1004. wavfile.close();
  1005. state_play = STATE_STOP;
  1006. state = STATE_STOP;
  1007. }
  1008. // https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
  1009. // Consume already buffered data. Returns true if audio transmitted.
  1010. bool AudioPlaySDcardWAV::consume(void)
  1011. {
  1012. uint32_t len, size;
  1013. uint8_t lsb, msb;
  1014. const uint8_t *p;
  1015. size = buffer_remaining;
  1016. p = buffer + 512 - size;
  1017. start:
  1018. if (size == 0) return false;
  1019. //Serial.print("AudioPlaySDcardWAV write, size = ");
  1020. //Serial.print(size);
  1021. //Serial.print(", data_length = ");
  1022. //Serial.print(data_length);
  1023. //Serial.print(", state = ");
  1024. //Serial.println(state);
  1025. switch (state) {
  1026. // parse wav file header, is this really a .wav file?
  1027. case STATE_PARSE1:
  1028. len = 20 - data_length;
  1029. if (size < len) len = size;
  1030. memcpy((uint8_t *)header + data_length, p, len);
  1031. data_length += len;
  1032. if (data_length < 20) return false;
  1033. // parse the header...
  1034. if (header[0] == 0x46464952 && header[2] == 0x45564157
  1035. && header[3] == 0x20746D66 && header[4] == 16) {
  1036. //Serial.println("header ok");
  1037. state = STATE_PARSE2;
  1038. p += len;
  1039. size -= len;
  1040. data_length = 0;
  1041. goto start;
  1042. }
  1043. //Serial.println("unknown WAV header");
  1044. break;
  1045. // check & extract key audio parameters
  1046. case STATE_PARSE2:
  1047. len = 16 - data_length;
  1048. if (size < len) len = size;
  1049. memcpy((uint8_t *)header + data_length, p, len);
  1050. data_length += len;
  1051. if (data_length < 16) return false;
  1052. if (parse_format()) {
  1053. //Serial.println("audio format ok");
  1054. p += len;
  1055. size -= len;
  1056. data_length = 0;
  1057. state = STATE_PARSE3;
  1058. goto start;
  1059. }
  1060. //Serial.println("unknown audio format");
  1061. break;
  1062. // find the data chunk
  1063. case STATE_PARSE3:
  1064. len = 8 - data_length;
  1065. if (size < len) len = size;
  1066. memcpy((uint8_t *)header + data_length, p, len);
  1067. data_length += len;
  1068. if (data_length < 8) return false;
  1069. //Serial.print("chunk id = ");
  1070. //Serial.print(header[0], HEX);
  1071. //Serial.print(", length = ");
  1072. //Serial.println(header[1]);
  1073. p += len;
  1074. size -= len;
  1075. data_length = header[1];
  1076. if (header[0] == 0x61746164) {
  1077. //Serial.println("found data chunk");
  1078. leftover_bytes = 0;
  1079. state = state_play;
  1080. } else {
  1081. state = STATE_PARSE4;
  1082. }
  1083. goto start;
  1084. // ignore any extra unknown chunks (title & artist info)
  1085. case STATE_PARSE4:
  1086. if (size < data_length) {
  1087. data_length -= size;
  1088. return false;
  1089. }
  1090. p += data_length;
  1091. size -= data_length;
  1092. data_length = 0;
  1093. state = STATE_PARSE3;
  1094. goto start;
  1095. // playing mono at native sample rate
  1096. case STATE_DIRECT_8BIT_MONO:
  1097. return false;
  1098. // playing stereo at native sample rate
  1099. case STATE_DIRECT_8BIT_STEREO:
  1100. return false;
  1101. // playing mono at native sample rate
  1102. case STATE_DIRECT_16BIT_MONO:
  1103. if (size > data_length) {
  1104. size = data_length;
  1105. }
  1106. data_length -= size;
  1107. if (leftover_bytes > 0) {
  1108. lsb = header[0];
  1109. leftover_bytes = 0;
  1110. goto do_msb;
  1111. }
  1112. while (1) {
  1113. lsb = *p++;
  1114. size--;
  1115. if (size == 0) {
  1116. if (data_length == 0) break;
  1117. header[0] = lsb;
  1118. leftover_bytes = 1;
  1119. return false;
  1120. }
  1121. do_msb:
  1122. msb = *p++;
  1123. size--;
  1124. block_left->data[block_offset++] = (msb << 8) | lsb;
  1125. if (block_offset >= AUDIO_BLOCK_SAMPLES) {
  1126. transmit(block_left, 0);
  1127. transmit(block_left, 1);
  1128. //Serial1.print('%');
  1129. //delayMicroseconds(90);
  1130. release(block_left);
  1131. block_left = NULL;
  1132. data_length += size;
  1133. buffer_remaining = size;
  1134. if (block_right) release(block_right);
  1135. return true;
  1136. }
  1137. if (size == 0) {
  1138. if (data_length == 0) break;
  1139. leftover_bytes = 0;
  1140. return false;
  1141. }
  1142. }
  1143. // end of file reached
  1144. if (block_offset > 0) {
  1145. // TODO: fill remainder of last block with zero and transmit
  1146. }
  1147. state = STATE_STOP;
  1148. return false;
  1149. // playing stereo at native sample rate
  1150. case STATE_DIRECT_16BIT_STEREO:
  1151. return false;
  1152. // playing mono, converting sample rate
  1153. case STATE_CONVERT_8BIT_MONO :
  1154. return false;
  1155. // playing stereo, converting sample rate
  1156. case STATE_CONVERT_8BIT_STEREO:
  1157. return false;
  1158. // playing mono, converting sample rate
  1159. case STATE_CONVERT_16BIT_MONO:
  1160. return false;
  1161. // playing stereo, converting sample rate
  1162. case STATE_CONVERT_16BIT_STEREO:
  1163. return false;
  1164. // ignore any extra data after playing
  1165. // or anything following any error
  1166. case STATE_STOP:
  1167. return false;
  1168. // this is not supposed to happen!
  1169. //default:
  1170. //Serial.println("AudioPlaySDcardWAV, unknown state");
  1171. }
  1172. state_play = STATE_STOP;
  1173. state = STATE_STOP;
  1174. return false;
  1175. }
  1176. /*
  1177. 00000000 52494646 66EA6903 57415645 666D7420 RIFFf.i.WAVEfmt
  1178. 00000010 10000000 01000200 44AC0000 10B10200 ........D.......
  1179. 00000020 04001000 4C495354 3A000000 494E464F ....LIST:...INFO
  1180. 00000030 494E414D 14000000 49205761 6E742054 INAM....I Want T
  1181. 00000040 6F20436F 6D65204F 76657200 49415254 o Come Over.IART
  1182. 00000050 12000000 4D656C69 73736120 45746865 ....Melissa Ethe
  1183. 00000060 72696467 65006461 746100EA 69030100 ridge.data..i...
  1184. 00000070 FEFF0300 FCFF0400 FDFF0200 0000FEFF ................
  1185. 00000080 0300FDFF 0200FFFF 00000100 FEFF0300 ................
  1186. 00000090 FDFF0300 FDFF0200 FFFF0100 0000FFFF ................
  1187. */
  1188. // SD library on Teensy3 at 96 MHz
  1189. // 256 byte chunks, speed is 443272 bytes/sec
  1190. // 512 byte chunks, speed is 468023 bytes/sec
  1191. bool AudioPlaySDcardWAV::parse_format(void)
  1192. {
  1193. uint8_t num = 0;
  1194. uint16_t format;
  1195. uint16_t channels;
  1196. uint32_t rate;
  1197. uint16_t bits;
  1198. format = header[0];
  1199. //Serial.print(" format = ");
  1200. //Serial.println(format);
  1201. if (format != 1) return false;
  1202. channels = header[0] >> 16;
  1203. //Serial.print(" channels = ");
  1204. //Serial.println(channels);
  1205. if (channels == 1) {
  1206. } else if (channels == 2) {
  1207. num = 1;
  1208. } else {
  1209. return false;
  1210. }
  1211. bits = header[3] >> 16;
  1212. //Serial.print(" bits = ");
  1213. //Serial.println(bits);
  1214. if (bits == 8) {
  1215. } else if (bits == 16) {
  1216. num |= 2;
  1217. } else {
  1218. return false;
  1219. }
  1220. rate = header[1];
  1221. //Serial.print(" rate = ");
  1222. //Serial.println(rate);
  1223. if (rate == AUDIO_SAMPLE_RATE) {
  1224. } else if (rate >= 8000 && rate <= 48000) {
  1225. num |= 4;
  1226. } else {
  1227. return false;
  1228. }
  1229. // we're not checking the byte rate and block align fields
  1230. // if they're not the expected values, all we could do is
  1231. // return false. Do any real wav files have unexpected
  1232. // values in these other fields?
  1233. state_play = num;
  1234. return true;
  1235. }
  1236. /******************************************************************/
  1237. void AudioPlaySDcardRAW::begin(void)
  1238. {
  1239. playing = false;
  1240. if (block) {
  1241. release(block);
  1242. block = NULL;
  1243. }
  1244. }
  1245. bool AudioPlaySDcardRAW::play(const char *filename)
  1246. {
  1247. stop();
  1248. rawfile = SD.open(filename);
  1249. if (!rawfile) {
  1250. Serial.println("unable to open file");
  1251. return false;
  1252. }
  1253. Serial.println("able to open file");
  1254. playing = true;
  1255. return true;
  1256. }
  1257. void AudioPlaySDcardRAW::stop(void)
  1258. {
  1259. __disable_irq();
  1260. if (playing) {
  1261. playing = false;
  1262. __enable_irq();
  1263. rawfile.close();
  1264. } else {
  1265. __enable_irq();
  1266. }
  1267. }
  1268. void AudioPlaySDcardRAW::update(void)
  1269. {
  1270. unsigned int i, n;
  1271. // only update if we're playing
  1272. if (!playing) return;
  1273. // allocate the audio blocks to transmit
  1274. block = allocate();
  1275. if (block == NULL) return;
  1276. if (rawfile.available()) {
  1277. // we can read more data from the file...
  1278. n = rawfile.read(block->data, AUDIO_BLOCK_SAMPLES*2);
  1279. for (i=n/2; i < AUDIO_BLOCK_SAMPLES; i++) {
  1280. block->data[i] = 0;
  1281. }
  1282. transmit(block);
  1283. release(block);
  1284. } else {
  1285. rawfile.close();
  1286. playing = false;
  1287. }
  1288. }
  1289. /******************************************************************/
  1290. // computes ((a[31:0] * b[15:0]) >> 16)
  1291. static inline int32_t signed_multiply_32x16b(int32_t a, uint32_t b) __attribute__((always_inline));
  1292. static inline int32_t signed_multiply_32x16b(int32_t a, uint32_t b)
  1293. {
  1294. int32_t out;
  1295. asm volatile("smulwb %0, %1, %2" : "=r" (out) : "r" (a), "r" (b));
  1296. return out;
  1297. }
  1298. // computes ((a[31:0] * b[31:16]) >> 16)
  1299. static inline int32_t signed_multiply_32x16t(int32_t a, uint32_t b) __attribute__((always_inline));
  1300. static inline int32_t signed_multiply_32x16t(int32_t a, uint32_t b)
  1301. {
  1302. int32_t out;
  1303. asm volatile("smulwt %0, %1, %2" : "=r" (out) : "r" (a), "r" (b));
  1304. return out;
  1305. }
  1306. // computes ((a[15:0) << 16) | b[15:0])
  1307. static inline uint32_t pack_16x16(int32_t a, int32_t b) __attribute__((always_inline));
  1308. static inline uint32_t pack_16x16(int32_t a, int32_t b)
  1309. {
  1310. int32_t out;
  1311. asm volatile("pkhbt %0, %1, %2, lsl #16" : "=r" (out) : "r" (b), "r" (a));
  1312. return out;
  1313. }
  1314. // computes (((a[31:16] + b[31:16]) << 16) | (a[15:0 + b[15:0]))
  1315. static inline uint32_t signed_add_16_and_16(uint32_t a, uint32_t b) __attribute__((always_inline));
  1316. static inline uint32_t signed_add_16_and_16(uint32_t a, uint32_t b)
  1317. {
  1318. int32_t out;
  1319. asm volatile("qadd16 %0, %1, %2" : "=r" (out) : "r" (a), "r" (b));
  1320. return out;
  1321. }
  1322. void applyGain(int16_t *data, int32_t mult)
  1323. {
  1324. uint32_t *p = (uint32_t *)data;
  1325. const uint32_t *end = (uint32_t *)(data + AUDIO_BLOCK_SAMPLES);
  1326. do {
  1327. uint32_t tmp32 = *p; // read 2 samples from *data
  1328. int32_t val1 = signed_multiply_32x16b(mult, tmp32);
  1329. int32_t val2 = signed_multiply_32x16t(mult, tmp32);
  1330. val1 = signed_saturate_rshift(val1, 16, 0);
  1331. val2 = signed_saturate_rshift(val2, 16, 0);
  1332. *p++ = pack_16x16(val2, val1);
  1333. } while (p < end);
  1334. }
  1335. // page 133
  1336. void applyGainThenAdd(int16_t *data, const int16_t *in, int32_t mult)
  1337. {
  1338. uint32_t *dst = (uint32_t *)data;
  1339. const uint32_t *src = (uint32_t *)in;
  1340. const uint32_t *end = (uint32_t *)(data + AUDIO_BLOCK_SAMPLES);
  1341. if (mult == 65536) {
  1342. do {
  1343. uint32_t tmp32 = *dst;
  1344. *dst++ = signed_add_16_and_16(tmp32, *src++);
  1345. tmp32 = *dst;
  1346. *dst++ = signed_add_16_and_16(tmp32, *src++);
  1347. } while (dst < end);
  1348. } else {
  1349. do {
  1350. uint32_t tmp32 = *src++; // read 2 samples from *data
  1351. int32_t val1 = signed_multiply_32x16b(mult, tmp32);
  1352. int32_t val2 = signed_multiply_32x16t(mult, tmp32);
  1353. val1 = signed_saturate_rshift(val1, 16, 0);
  1354. val2 = signed_saturate_rshift(val2, 16, 0);
  1355. tmp32 = pack_16x16(val2, val1);
  1356. uint32_t tmp32b = *dst;
  1357. *dst++ = signed_add_16_and_16(tmp32, tmp32b);
  1358. } while (dst < end);
  1359. }
  1360. }
  1361. void AudioMixer4::update(void)
  1362. {
  1363. audio_block_t *in, *out=NULL;
  1364. unsigned int channel;
  1365. for (channel=0; channel < 4; channel++) {
  1366. if (!out) {
  1367. out = receiveWritable(channel);
  1368. if (out) {
  1369. int32_t mult = multiplier[channel];
  1370. if (mult != 65536) applyGain(out->data, mult);
  1371. }
  1372. } else {
  1373. in = receiveReadOnly(channel);
  1374. if (in) {
  1375. applyGainThenAdd(out->data, in->data, multiplier[channel]);
  1376. release(in);
  1377. }
  1378. }
  1379. }
  1380. if (out) {
  1381. transmit(out);
  1382. release(out);
  1383. }
  1384. }
  1385. /******************************************************************/
  1386. #include "Wire.h"
  1387. #define WM8731_I2C_ADDR 0x1A
  1388. //#define WM8731_I2C_ADDR 0x1B
  1389. #define WM8731_REG_LLINEIN 0
  1390. #define WM8731_REG_RLINEIN 1
  1391. #define WM8731_REG_LHEADOUT 2
  1392. #define WM8731_REG_RHEADOUT 3
  1393. #define WM8731_REG_ANALOG 4
  1394. #define WM8731_REG_DIGITAL 5
  1395. #define WM8731_REG_POWERDOWN 6
  1396. #define WM8731_REG_INTERFACE 7
  1397. #define WM8731_REG_SAMPLING 8
  1398. #define WM8731_REG_ACTIVE 9
  1399. #define WM8731_REG_RESET 15
  1400. bool AudioControlWM8731::enable(void)
  1401. {
  1402. Wire.begin();
  1403. delay(5);
  1404. //write(WM8731_REG_RESET, 0);
  1405. write(WM8731_REG_INTERFACE, 0x02); // I2S, 16 bit, MCLK slave
  1406. write(WM8731_REG_SAMPLING, 0x20); // 256*Fs, 44.1 kHz, MCLK/1
  1407. // In order to prevent pops, the DAC should first be soft-muted (DACMU),
  1408. // the output should then be de-selected from the line and headphone output
  1409. // (DACSEL), then the DAC powered down (DACPD).
  1410. write(WM8731_REG_DIGITAL, 0x08); // DAC soft mute
  1411. write(WM8731_REG_ANALOG, 0x00); // disable all
  1412. write(WM8731_REG_POWERDOWN, 0x60); // linein, mic, adc, dac, lineout
  1413. write(WM8731_REG_LHEADOUT, 0x80); // volume off
  1414. write(WM8731_REG_RHEADOUT, 0x80);
  1415. delay(100); // how long to power up?
  1416. write(WM8731_REG_ACTIVE, 1);
  1417. delay(5);
  1418. write(WM8731_REG_DIGITAL, 0x00); // DAC unmuted
  1419. write(WM8731_REG_ANALOG, 0x10); // DAC selected
  1420. return true;
  1421. }
  1422. bool AudioControlWM8731::write(unsigned int reg, unsigned int val)
  1423. {
  1424. Wire.beginTransmission(WM8731_I2C_ADDR);
  1425. Wire.write((reg << 1) | ((val >> 8) & 1));
  1426. Wire.write(val & 0xFF);
  1427. Wire.endTransmission();
  1428. return true;
  1429. }
  1430. bool AudioControlWM8731::volumeInteger(unsigned int n)
  1431. {
  1432. if (n > 127) n = 127;
  1433. //Serial.print("volumeInteger, n = ");
  1434. //Serial.println(n);
  1435. write(WM8731_REG_LHEADOUT, n | 0x180);
  1436. write(WM8731_REG_RHEADOUT, n | 0x80);
  1437. return true;
  1438. }
  1439. /******************************************************************/
  1440. #define CHIP_ID 0x0000
  1441. // 15:8 PARTID 0xA0 - 8 bit identifier for SGTL5000
  1442. // 7:0 REVID 0x00 - revision number for SGTL5000.
  1443. #define CHIP_DIG_POWER 0x0002
  1444. // 6 ADC_POWERUP 1=Enable, 0=disable the ADC block, both digital & analog,
  1445. // 5 DAC_POWERUP 1=Enable, 0=disable the DAC block, both analog and digital
  1446. // 4 DAP_POWERUP 1=Enable, 0=disable the DAP block
  1447. // 1 I2S_OUT_POWERUP 1=Enable, 0=disable the I2S data output
  1448. // 0 I2S_IN_POWERUP 1=Enable, 0=disable the I2S data input
  1449. #define CHIP_CLK_CTRL 0x0004
  1450. // 5:4 RATE_MODE Sets the sample rate mode. MCLK_FREQ is still specified
  1451. // relative to the rate in SYS_FS
  1452. // 0x0 = SYS_FS specifies the rate
  1453. // 0x1 = Rate is 1/2 of the SYS_FS rate
  1454. // 0x2 = Rate is 1/4 of the SYS_FS rate
  1455. // 0x3 = Rate is 1/6 of the SYS_FS rate
  1456. // 3:2 SYS_FS Sets the internal system sample rate (default=2)
  1457. // 0x0 = 32 kHz
  1458. // 0x1 = 44.1 kHz
  1459. // 0x2 = 48 kHz
  1460. // 0x3 = 96 kHz
  1461. // 1:0 MCLK_FREQ Identifies incoming SYS_MCLK frequency and if the PLL should be used
  1462. // 0x0 = 256*Fs
  1463. // 0x1 = 384*Fs
  1464. // 0x2 = 512*Fs
  1465. // 0x3 = Use PLL
  1466. // The 0x3 (Use PLL) setting must be used if the SYS_MCLK is not
  1467. // a standard multiple of Fs (256, 384, or 512). This setting can
  1468. // also be used if SYS_MCLK is a standard multiple of Fs.
  1469. // Before this field is set to 0x3 (Use PLL), the PLL must be
  1470. // powered up by setting CHIP_ANA_POWER->PLL_POWERUP and
  1471. // CHIP_ANA_POWER->VCOAMP_POWERUP. Also, the PLL dividers must
  1472. // be calculated based on the external MCLK rate and
  1473. // CHIP_PLL_CTRL register must be set (see CHIP_PLL_CTRL register
  1474. // description details on how to calculate the divisors).
  1475. #define CHIP_I2S_CTRL 0x0006
  1476. // 8 SCLKFREQ Sets frequency of I2S_SCLK when in master mode (MS=1). When in slave
  1477. // mode (MS=0), this field must be set appropriately to match SCLK input
  1478. // rate.
  1479. // 0x0 = 64Fs
  1480. // 0x1 = 32Fs - Not supported for RJ mode (I2S_MODE = 1)
  1481. // 7 MS Configures master or slave of I2S_LRCLK and I2S_SCLK.
  1482. // 0x0 = Slave: I2S_LRCLK an I2S_SCLK are inputs
  1483. // 0x1 = Master: I2S_LRCLK and I2S_SCLK are outputs
  1484. // NOTE: If the PLL is used (CHIP_CLK_CTRL->MCLK_FREQ==0x3),
  1485. // the SGTL5000 must be a master of the I2S port (MS==1)
  1486. // 6 SCLK_INV Sets the edge that data (input and output) is clocked in on for I2S_SCLK
  1487. // 0x0 = data is valid on rising edge of I2S_SCLK
  1488. // 0x1 = data is valid on falling edge of I2S_SCLK
  1489. // 5:4 DLEN I2S data length (default=1)
  1490. // 0x0 = 32 bits (only valid when SCLKFREQ=0),
  1491. // not valid for Right Justified Mode
  1492. // 0x1 = 24 bits (only valid when SCLKFREQ=0)
  1493. // 0x2 = 20 bits
  1494. // 0x3 = 16 bits
  1495. // 3:2 I2S_MODE Sets the mode for the I2S port
  1496. // 0x0 = I2S mode or Left Justified (Use LRALIGN to select)
  1497. // 0x1 = Right Justified Mode
  1498. // 0x2 = PCM Format A/B
  1499. // 0x3 = RESERVED
  1500. // 1 LRALIGN I2S_LRCLK Alignment to data word. Not used for Right Justified mode
  1501. // 0x0 = Data word starts 1 I2S_SCLK delay after I2S_LRCLK
  1502. // transition (I2S format, PCM format A)
  1503. // 0x1 = Data word starts after I2S_LRCLK transition (left
  1504. // justified format, PCM format B)
  1505. // 0 LRPOL I2S_LRCLK Polarity when data is presented.
  1506. // 0x0 = I2S_LRCLK = 0 - Left, 1 - Right
  1507. // 1x0 = I2S_LRCLK = 0 - Right, 1 - Left
  1508. // The left subframe should be presented first regardless of
  1509. // the setting of LRPOL.
  1510. #define CHIP_SSS_CTRL 0x000A
  1511. // 14 DAP_MIX_LRSWAP DAP Mixer Input Swap
  1512. // 0x0 = Normal Operation
  1513. // 0x1 = Left and Right channels for the DAP MIXER Input are swapped.
  1514. // 13 DAP_LRSWAP DAP Mixer Input Swap
  1515. // 0x0 = Normal Operation
  1516. // 0x1 = Left and Right channels for the DAP Input are swapped
  1517. // 12 DAC_LRSWAP DAC Input Swap
  1518. // 0x0 = Normal Operation
  1519. // 0x1 = Left and Right channels for the DAC are swapped
  1520. // 10 I2S_LRSWAP I2S_DOUT Swap
  1521. // 0x0 = Normal Operation
  1522. // 0x1 = Left and Right channels for the I2S_DOUT are swapped
  1523. // 9:8 DAP_MIX_SELECT Select data source for DAP mixer
  1524. // 0x0 = ADC
  1525. // 0x1 = I2S_IN
  1526. // 0x2 = Reserved
  1527. // 0x3 = Reserved
  1528. // 7:6 DAP_SELECT Select data source for DAP
  1529. // 0x0 = ADC
  1530. // 0x1 = I2S_IN
  1531. // 0x2 = Reserved
  1532. // 0x3 = Reserved
  1533. // 5:4 DAC_SELECT Select data source for DAC (default=1)
  1534. // 0x0 = ADC
  1535. // 0x1 = I2S_IN
  1536. // 0x2 = Reserved
  1537. // 0x3 = DAP
  1538. // 1:0 I2S_SELECT Select data source for I2S_DOUT
  1539. // 0x0 = ADC
  1540. // 0x1 = I2S_IN
  1541. // 0x2 = Reserved
  1542. // 0x3 = DAP
  1543. #define CHIP_ADCDAC_CTRL 0x000E
  1544. // 13 VOL_BUSY_DAC_RIGHT Volume Busy DAC Right
  1545. // 0x0 = Ready
  1546. // 0x1 = Busy - This indicates the channel has not reached its
  1547. // programmed volume/mute level
  1548. // 12 VOL_BUSY_DAC_LEFT Volume Busy DAC Left
  1549. // 0x0 = Ready
  1550. // 0x1 = Busy - This indicates the channel has not reached its
  1551. // programmed volume/mute level
  1552. // 9 VOL_RAMP_EN Volume Ramp Enable (default=1)
  1553. // 0x0 = Disables volume ramp. New volume settings take immediate
  1554. // effect without a ramp
  1555. // 0x1 = Enables volume ramp
  1556. // This field affects DAC_VOL. The volume ramp effects both
  1557. // volume settings and mute When set to 1 a soft mute is enabled.
  1558. // 8 VOL_EXPO_RAMP Exponential Volume Ramp Enable
  1559. // 0x0 = Linear ramp over top 4 volume octaves
  1560. // 0x1 = Exponential ramp over full volume range
  1561. // This bit only takes effect if VOL_RAMP_EN is 1.
  1562. // 3 DAC_MUTE_RIGHT DAC Right Mute (default=1)
  1563. // 0x0 = Unmute
  1564. // 0x1 = Muted
  1565. // If VOL_RAMP_EN = 1, this is a soft mute.
  1566. // 2 DAC_MUTE_LEFT DAC Left Mute (default=1)
  1567. // 0x0 = Unmute
  1568. // 0x1 = Muted
  1569. // If VOL_RAMP_EN = 1, this is a soft mute.
  1570. // 1 ADC_HPF_FREEZE ADC High Pass Filter Freeze
  1571. // 0x0 = Normal operation
  1572. // 0x1 = Freeze the ADC high-pass filter offset register. The
  1573. // offset continues to be subtracted from the ADC data stream.
  1574. // 0 ADC_HPF_BYPASS ADC High Pass Filter Bypass
  1575. // 0x0 = Normal operation
  1576. // 0x1 = Bypassed and offset not updated
  1577. #define CHIP_DAC_VOL 0x0010
  1578. // 15:8 DAC_VOL_RIGHT DAC Right Channel Volume. Set the Right channel DAC volume
  1579. // with 0.5017 dB steps from 0 to -90 dB
  1580. // 0x3B and less = Reserved
  1581. // 0x3C = 0 dB
  1582. // 0x3D = -0.5 dB
  1583. // 0xF0 = -90 dB
  1584. // 0xFC and greater = Muted
  1585. // If VOL_RAMP_EN = 1, there is an automatic ramp to the
  1586. // new volume setting.
  1587. // 7:0 DAC_VOL_LEFT DAC Left Channel Volume. Set the Left channel DAC volume
  1588. // with 0.5017 dB steps from 0 to -90 dB
  1589. // 0x3B and less = Reserved
  1590. // 0x3C = 0 dB
  1591. // 0x3D = -0.5 dB
  1592. // 0xF0 = -90 dB
  1593. // 0xFC and greater = Muted
  1594. // If VOL_RAMP_EN = 1, there is an automatic ramp to the
  1595. // new volume setting.
  1596. #define CHIP_PAD_STRENGTH 0x0014
  1597. // 9:8 I2S_LRCLK I2S LRCLK Pad Drive Strength (default=1)
  1598. // Sets drive strength for output pads per the table below.
  1599. // VDDIO 1.8 V 2.5 V 3.3 V
  1600. // 0x0 = Disable
  1601. // 0x1 = 1.66 mA 2.87 mA 4.02 mA
  1602. // 0x2 = 3.33 mA 5.74 mA 8.03 mA
  1603. // 0x3 = 4.99 mA 8.61 mA 12.05 mA
  1604. // 7:6 I2S_SCLK I2S SCLK Pad Drive Strength (default=1)
  1605. // 5:4 I2S_DOUT I2S DOUT Pad Drive Strength (default=1)
  1606. // 3:2 CTRL_DATA I2C DATA Pad Drive Strength (default=3)
  1607. // 1:0 CTRL_CLK I2C CLK Pad Drive Strength (default=3)
  1608. // (all use same table as I2S_LRCLK)
  1609. #define CHIP_ANA_ADC_CTRL 0x0020
  1610. // 8 ADC_VOL_M6DB ADC Volume Range Reduction
  1611. // This bit shifts both right and left analog ADC volume
  1612. // range down by 6.0 dB.
  1613. // 0x0 = No change in ADC range
  1614. // 0x1 = ADC range reduced by 6.0 dB
  1615. // 7:4 ADC_VOL_RIGHT ADC Right Channel Volume
  1616. // Right channel analog ADC volume control in 1.5 dB steps.
  1617. // 0x0 = 0 dB
  1618. // 0x1 = +1.5 dB
  1619. // ...
  1620. // 0xF = +22.5 dB
  1621. // This range is -6.0 dB to +16.5 dB if ADC_VOL_M6DB is set to 1.
  1622. // 3:0 ADC_VOL_LEFT ADC Left Channel Volume
  1623. // (same scale as ADC_VOL_RIGHT)
  1624. #define CHIP_ANA_HP_CTRL 0x0022
  1625. // 14:8 HP_VOL_RIGHT Headphone Right Channel Volume (default 0x18)
  1626. // Right channel headphone volume control with 0.5 dB steps.
  1627. // 0x00 = +12 dB
  1628. // 0x01 = +11.5 dB
  1629. // 0x18 = 0 dB
  1630. // ...
  1631. // 0x7F = -51.5 dB
  1632. // 6:0 HP_VOL_LEFT Headphone Left Channel Volume (default 0x18)
  1633. // (same scale as HP_VOL_RIGHT)
  1634. #define CHIP_ANA_CTRL 0x0024
  1635. // 8 MUTE_LO LINEOUT Mute, 0 = Unmute, 1 = Mute (default 1)
  1636. // 6 SELECT_HP Select the headphone input, 0 = DAC, 1 = LINEIN
  1637. // 5 EN_ZCD_HP Enable the headphone zero cross detector (ZCD)
  1638. // 0x0 = HP ZCD disabled
  1639. // 0x1 = HP ZCD enabled
  1640. // 4 MUTE_HP Mute the headphone outputs, 0 = Unmute, 1 = Mute (default)
  1641. // 2 SELECT_ADC Select the ADC input, 0 = Microphone, 1 = LINEIN
  1642. // 1 EN_ZCD_ADC Enable the ADC analog zero cross detector (ZCD)
  1643. // 0x0 = ADC ZCD disabled
  1644. // 0x1 = ADC ZCD enabled
  1645. // 0 MUTE_ADC Mute the ADC analog volume, 0 = Unmute, 1 = Mute (default)
  1646. #define CHIP_LINREG_CTRL 0x0026
  1647. // 6 VDDC_MAN_ASSN Determines chargepump source when VDDC_ASSN_OVRD is set.
  1648. // 0x0 = VDDA
  1649. // 0x1 = VDDIO
  1650. // 5 VDDC_ASSN_OVRD Charge pump Source Assignment Override
  1651. // 0x0 = Charge pump source is automatically assigned based
  1652. // on higher of VDDA and VDDIO
  1653. // 0x1 = the source of charge pump is manually assigned by
  1654. // VDDC_MAN_ASSN If VDDIO and VDDA are both the same
  1655. // and greater than 3.1 V, VDDC_ASSN_OVRD and
  1656. // VDDC_MAN_ASSN should be used to manually assign
  1657. // VDDIO as the source for charge pump.
  1658. // 3:0 D_PROGRAMMING Sets the VDDD linear regulator output voltage in 50 mV steps.
  1659. // Must clear the LINREG_SIMPLE_POWERUP and STARTUP_POWERUP bits
  1660. // in the 0x0030 (CHIP_ANA_POWER) register after power-up, for
  1661. // this setting to produce the proper VDDD voltage.
  1662. // 0x0 = 1.60
  1663. // 0xF = 0.85
  1664. #define CHIP_REF_CTRL 0x0028 // bandgap reference bias voltage and currents
  1665. // 8:4 VAG_VAL Analog Ground Voltage Control
  1666. // These bits control the analog ground voltage in 25 mV steps.
  1667. // This should usually be set to VDDA/2 or lower for best
  1668. // performance (maximum output swing at minimum THD). This VAG
  1669. // reference is also used for the DAC and ADC voltage reference.
  1670. // So changing this voltage scales the output swing of the DAC
  1671. // and the output signal of the ADC.
  1672. // 0x00 = 0.800 V
  1673. // 0x1F = 1.575 V
  1674. // 3:1 BIAS_CTRL Bias control
  1675. // These bits adjust the bias currents for all of the analog
  1676. // blocks. By lowering the bias current a lower quiescent power
  1677. // is achieved. It should be noted that this mode can affect
  1678. // performance by 3-4 dB.
  1679. // 0x0 = Nominal
  1680. // 0x1-0x3=+12.5%
  1681. // 0x4=-12.5%
  1682. // 0x5=-25%
  1683. // 0x6=-37.5%
  1684. // 0x7=-50%
  1685. // 0 SMALL_POP VAG Ramp Control
  1686. // Setting this bit slows down the VAG ramp from ~200 to ~400 ms
  1687. // to reduce the startup pop, but increases the turn on/off time.
  1688. // 0x0 = Normal VAG ramp
  1689. // 0x1 = Slow down VAG ramp
  1690. #define CHIP_MIC_CTRL 0x002A // microphone gain & internal microphone bias
  1691. // 9:8 BIAS_RESISTOR MIC Bias Output Impedance Adjustment
  1692. // Controls an adjustable output impedance for the microphone bias.
  1693. // If this is set to zero the micbias block is powered off and
  1694. // the output is highZ.
  1695. // 0x0 = Powered off
  1696. // 0x1 = 2.0 kohm
  1697. // 0x2 = 4.0 kohm
  1698. // 0x3 = 8.0 kohm
  1699. // 6:4 BIAS_VOLT MIC Bias Voltage Adjustment
  1700. // Controls an adjustable bias voltage for the microphone bias
  1701. // amp in 250 mV steps. This bias voltage setting should be no
  1702. // more than VDDA-200 mV for adequate power supply rejection.
  1703. // 0x0 = 1.25 V
  1704. // ...
  1705. // 0x7 = 3.00 V
  1706. // 1:0 GAIN MIC Amplifier Gain
  1707. // Sets the microphone amplifier gain. At 0 dB setting the THD
  1708. // can be slightly higher than other paths- typically around
  1709. // ~65 dB. At other gain settings the THD are better.
  1710. // 0x0 = 0 dB
  1711. // 0x1 = +20 dB
  1712. // 0x2 = +30 dB
  1713. // 0x3 = +40 dB
  1714. #define CHIP_LINE_OUT_CTRL 0x002C
  1715. // 11:8 OUT_CURRENT Controls the output bias current for the LINEOUT amplifiers. The
  1716. // nominal recommended setting for a 10 kohm load with 1.0 nF load cap
  1717. // is 0x3. There are only 5 valid settings.
  1718. // 0x0=0.18 mA
  1719. // 0x1=0.27 mA
  1720. // 0x3=0.36 mA
  1721. // 0x7=0.45 mA
  1722. // 0xF=0.54 mA
  1723. // 5:0 LO_VAGCNTRL LINEOUT Amplifier Analog Ground Voltage
  1724. // Controls the analog ground voltage for the LINEOUT amplifiers
  1725. // in 25 mV steps. This should usually be set to VDDIO/2.
  1726. // 0x00 = 0.800 V
  1727. // ...
  1728. // 0x1F = 1.575 V
  1729. // ...
  1730. // 0x23 = 1.675 V
  1731. // 0x24-0x3F are invalid
  1732. #define CHIP_LINE_OUT_VOL 0x002E
  1733. // 12:8 LO_VOL_RIGHT LINEOUT Right Channel Volume (default=4)
  1734. // Controls the right channel LINEOUT volume in 0.5 dB steps.
  1735. // Higher codes have more attenuation.
  1736. // 4:0 LO_VOL_LEFT LINEOUT Left Channel Output Level (default=4)
  1737. // Used to normalize the output level of the left line output
  1738. // to full scale based on the values used to set
  1739. // LINE_OUT_CTRL->LO_VAGCNTRL and CHIP_REF_CTRL->VAG_VAL.
  1740. // In general this field should be set to:
  1741. // 40*log((VAG_VAL)/(LO_VAGCNTRL)) + 15
  1742. // Suggested values based on typical VDDIO and VDDA voltages.
  1743. // VDDA VAG_VAL VDDIO LO_VAGCNTRL LO_VOL_*
  1744. // 1.8 V 0.9 3.3 V 1.55 0x06
  1745. // 1.8 V 0.9 1.8 V 0.9 0x0F
  1746. // 3.3 V 1.55 1.8 V 0.9 0x19
  1747. // 3.3 V 1.55 3.3 V 1.55 0x0F
  1748. // After setting to the nominal voltage, this field can be used
  1749. // to adjust the output level in +/-0.5 dB increments by using
  1750. // values higher or lower than the nominal setting.
  1751. #define CHIP_ANA_POWER 0x0030 // power down controls for the analog blocks.
  1752. // The only other power-down controls are BIAS_RESISTOR in the MIC_CTRL register
  1753. // and the EN_ZCD control bits in ANA_CTRL.
  1754. // 14 DAC_MONO While DAC_POWERUP is set, this allows the DAC to be put into left only
  1755. // mono operation for power savings. 0=mono, 1=stereo (default)
  1756. // 13 LINREG_SIMPLE_POWERUP Power up the simple (low power) digital supply regulator.
  1757. // After reset, this bit can be cleared IF VDDD is driven
  1758. // externally OR the primary digital linreg is enabled with
  1759. // LINREG_D_POWERUP
  1760. // 12 STARTUP_POWERUP Power up the circuitry needed during the power up ramp and reset.
  1761. // After reset this bit can be cleared if VDDD is coming from
  1762. // an external source.
  1763. // 11 VDDC_CHRGPMP_POWERUP Power up the VDDC charge pump block. If neither VDDA or VDDIO
  1764. // is 3.0 V or larger this bit should be cleared before analog
  1765. // blocks are powered up.
  1766. // 10 PLL_POWERUP PLL Power Up, 0 = Power down, 1 = Power up
  1767. // When cleared, the PLL is turned off. This must be set before
  1768. // CHIP_CLK_CTRL->MCLK_FREQ is programmed to 0x3. The
  1769. // CHIP_PLL_CTRL register must be configured correctly before
  1770. // setting this bit.
  1771. // 9 LINREG_D_POWERUP Power up the primary VDDD linear regulator, 0 = Power down, 1 = Power up
  1772. // 8 VCOAMP_POWERUP Power up the PLL VCO amplifier, 0 = Power down, 1 = Power up
  1773. // 7 VAG_POWERUP Power up the VAG reference buffer.
  1774. // Setting this bit starts the power up ramp for the headphone
  1775. // and LINEOUT. The headphone (and/or LINEOUT) powerup should
  1776. // be set BEFORE clearing this bit. When this bit is cleared
  1777. // the power-down ramp is started. The headphone (and/or LINEOUT)
  1778. // powerup should stay set until the VAG is fully ramped down
  1779. // (200 to 400 ms after clearing this bit).
  1780. // 0x0 = Power down, 0x1 = Power up
  1781. // 6 ADC_MONO While ADC_POWERUP is set, this allows the ADC to be put into left only
  1782. // mono operation for power savings. This mode is useful when
  1783. // only using the microphone input.
  1784. // 0x0 = Mono (left only), 0x1 = Stereo
  1785. // 5 REFTOP_POWERUP Power up the reference bias currents
  1786. // 0x0 = Power down, 0x1 = Power up
  1787. // This bit can be cleared when the part is a sleep state
  1788. // to minimize analog power.
  1789. // 4 HEADPHONE_POWERUP Power up the headphone amplifiers
  1790. // 0x0 = Power down, 0x1 = Power up
  1791. // 3 DAC_POWERUP Power up the DACs
  1792. // 0x0 = Power down, 0x1 = Power up
  1793. // 2 CAPLESS_HEADPHONE_POWERUP Power up the capless headphone mode
  1794. // 0x0 = Power down, 0x1 = Power up
  1795. // 1 ADC_POWERUP Power up the ADCs
  1796. // 0x0 = Power down, 0x1 = Power up
  1797. // 0 LINEOUT_POWERUP Power up the LINEOUT amplifiers
  1798. // 0x0 = Power down, 0x1 = Power up
  1799. #define CHIP_PLL_CTRL 0x0032
  1800. // 15:11 INT_DIVISOR
  1801. // 10:0 FRAC_DIVISOR
  1802. #define CHIP_CLK_TOP_CTRL 0x0034
  1803. // 11 ENABLE_INT_OSC Setting this bit enables an internal oscillator to be used for the
  1804. // zero cross detectors, the short detect recovery, and the
  1805. // charge pump. This allows the I2S clock to be shut off while
  1806. // still operating an analog signal path. This bit can be kept
  1807. // on when the I2S clock is enabled, but the I2S clock is more
  1808. // accurate so it is preferred to clear this bit when I2S is present.
  1809. // 3 INPUT_FREQ_DIV2 SYS_MCLK divider before PLL input
  1810. // 0x0 = pass through
  1811. // 0x1 = SYS_MCLK is divided by 2 before entering PLL
  1812. // This must be set when the input clock is above 17 Mhz. This
  1813. // has no effect when the PLL is powered down.
  1814. #define CHIP_ANA_STATUS 0x0036
  1815. // 9 LRSHORT_STS This bit is high whenever a short is detected on the left or right
  1816. // channel headphone drivers.
  1817. // 8 CSHORT_STS This bit is high whenever a short is detected on the capless headphone
  1818. // common/center channel driver.
  1819. // 4 PLL_IS_LOCKED This bit goes high after the PLL is locked.
  1820. #define CHIP_ANA_TEST1 0x0038 // intended only for debug.
  1821. #define CHIP_ANA_TEST2 0x003A // intended only for debug.
  1822. #define CHIP_SHORT_CTRL 0x003C
  1823. // 14:12 LVLADJR Right channel headphone short detector in 25 mA steps.
  1824. // 0x3=25 mA
  1825. // 0x2=50 mA
  1826. // 0x1=75 mA
  1827. // 0x0=100 mA
  1828. // 0x4=125 mA
  1829. // 0x5=150 mA
  1830. // 0x6=175 mA
  1831. // 0x7=200 mA
  1832. // This trip point can vary by ~30% over process so leave plenty
  1833. // of guard band to avoid false trips. This short detect trip
  1834. // point is also effected by the bias current adjustments made
  1835. // by CHIP_REF_CTRL->BIAS_CTRL and by CHIP_ANA_TEST1->HP_IALL_ADJ.
  1836. // 10:8 LVLADJL Left channel headphone short detector in 25 mA steps.
  1837. // (same scale as LVLADJR)
  1838. // 6:4 LVLADJC Capless headphone center channel short detector in 50 mA steps.
  1839. // 0x3=50 mA
  1840. // 0x2=100 mA
  1841. // 0x1=150 mA
  1842. // 0x0=200 mA
  1843. // 0x4=250 mA
  1844. // 0x5=300 mA
  1845. // 0x6=350 mA
  1846. // 0x7=400 mA
  1847. // 3:2 MODE_LR Behavior of left/right short detection
  1848. // 0x0 = Disable short detector, reset short detect latch,
  1849. // software view non-latched short signal
  1850. // 0x1 = Enable short detector and reset the latch at timeout
  1851. // (every ~50 ms)
  1852. // 0x2 = This mode is not used/invalid
  1853. // 0x3 = Enable short detector with only manual reset (have
  1854. // to return to 0x0 to reset the latch)
  1855. // 1:0 MODE_CM Behavior of capless headphone central short detection
  1856. // (same settings as MODE_LR)
  1857. #define DAP_CONTROL 0x0100
  1858. #define DAP_PEQ 0x0102
  1859. #define DAP_BASS_ENHANCE 0x0104
  1860. #define DAP_BASS_ENHANCE_CTRL 0x0106
  1861. #define DAP_AUDIO_EQ 0x0108
  1862. #define DAP_SGTL_SURROUND 0x010A
  1863. #define DAP_FILTER_COEF_ACCES 0x010C
  1864. #define DAP_COEF_WR_B0_MSB 0x010E
  1865. #define DAP_COEF_WR_B0_LSB 0x0110
  1866. #define DAP_AUDIO_EQ_BASS_BAND0 0x0116 // 115 Hz
  1867. #define DAP_AUDIO_EQ_BAND1 0x0118 // 330 Hz
  1868. #define DAP_AUDIO_EQ_BAND2 0x011A // 990 Hz
  1869. #define DAP_AUDIO_EQ_BAND3 0x011C // 3000 Hz
  1870. #define DAP_AUDIO_EQ_TREBLE_BAND4 0x011E // 9900 Hz
  1871. #define DAP_MAIN_CHAN 0x0120
  1872. #define DAP_MIX_CHAN 0x0122
  1873. #define DAP_AVC_CTRL 0x0124
  1874. #define DAP_AVC_THRESHOLD 0x0126
  1875. #define DAP_AVC_ATTACK 0x0128
  1876. #define DAP_AVC_DECAY 0x012A
  1877. #define DAP_COEF_WR_B1_MSB 0x012C
  1878. #define DAP_COEF_WR_B1_LSB 0x012E
  1879. #define DAP_COEF_WR_B2_MSB 0x0130
  1880. #define DAP_COEF_WR_B2_LSB 0x0132
  1881. #define DAP_COEF_WR_A1_MSB 0x0134
  1882. #define DAP_COEF_WR_A1_LSB 0x0136
  1883. #define DAP_COEF_WR_A2_MSB 0x0138
  1884. #define DAP_COEF_WR_A2_LSB 0x013A
  1885. #define SGTL5000_I2C_ADDR 0x0A // CTRL_ADR0_CS pin low (normal configuration)
  1886. //#define SGTL5000_I2C_ADDR 0x2A // CTRL_ADR0_CS pin high
  1887. bool AudioControlSGTL5000::enable(void)
  1888. {
  1889. unsigned int n;
  1890. muted = true;
  1891. Wire.begin();
  1892. delay(5);
  1893. Serial.print("chip ID = ");
  1894. delay(5);
  1895. n = read(CHIP_ID);
  1896. Serial.println(n, HEX);
  1897. write(CHIP_ANA_POWER, 0x4060); // VDDD is externally driven with 1.8V
  1898. write(CHIP_LINREG_CTRL, 0x006C); // VDDA & VDDIO both over 3.1V
  1899. write(CHIP_REF_CTRL, 0x01F1); // VAG=1.575 slow ramp, normal bias current
  1900. write(CHIP_LINE_OUT_CTRL, 0x0322);
  1901. write(CHIP_SHORT_CTRL, 0x4446); // allow up to 125mA
  1902. write(CHIP_ANA_CTRL, 0x0137); // enable zero cross detectors
  1903. write(CHIP_ANA_POWER, 0x40FF); // power up: lineout, hp, adc, dac
  1904. write(CHIP_DIG_POWER, 0x0073); // power up all digital stuff
  1905. delay(400);
  1906. write(CHIP_LINE_OUT_VOL, 0x0505); // TODO: correct value for 3.3V
  1907. write(CHIP_CLK_CTRL, 0x0004); // 44.1 kHz, 256*Fs
  1908. write(CHIP_I2S_CTRL, 0x0130); // SCLK=32*Fs, 16bit, I2S format
  1909. // default signal routing is ok?
  1910. write(CHIP_SSS_CTRL, 0x0010); // ADC->I2S, I2S->DAC
  1911. write(CHIP_ADCDAC_CTRL, 0x0000); // disable dac mute
  1912. write(CHIP_DAC_VOL, 0x3C3C); // digital gain, 0dB
  1913. write(CHIP_ANA_HP_CTRL, 0x7F7F); // set volume (lowest level)
  1914. write(CHIP_ANA_CTRL, 0x0136); // enable zero cross detectors
  1915. //mute = false;
  1916. return true;
  1917. }
  1918. unsigned int AudioControlSGTL5000::read(unsigned int reg)
  1919. {
  1920. unsigned int val;
  1921. Wire.beginTransmission(SGTL5000_I2C_ADDR);
  1922. Wire.write(reg >> 8);
  1923. Wire.write(reg);
  1924. if (Wire.endTransmission(false) != 0) return 0;
  1925. if (Wire.requestFrom(SGTL5000_I2C_ADDR, 2) < 2) return 0;
  1926. val = Wire.read() << 8;
  1927. val |= Wire.read();
  1928. return val;
  1929. }
  1930. bool AudioControlSGTL5000::write(unsigned int reg, unsigned int val)
  1931. {
  1932. if (reg == CHIP_ANA_CTRL) ana_ctrl = val;
  1933. Wire.beginTransmission(SGTL5000_I2C_ADDR);
  1934. Wire.write(reg >> 8);
  1935. Wire.write(reg);
  1936. Wire.write(val >> 8);
  1937. Wire.write(val);
  1938. if (Wire.endTransmission() == 0) return true;
  1939. return false;
  1940. }
  1941. bool AudioControlSGTL5000::volumeInteger(unsigned int n)
  1942. {
  1943. if (n == 0) {
  1944. muted = true;
  1945. write(CHIP_ANA_HP_CTRL, 0x7F7F);
  1946. return muteHeadphone();
  1947. } else if (n > 0x80) {
  1948. n = 0;
  1949. } else {
  1950. n = 0x80 - n;
  1951. }
  1952. if (muted) {
  1953. muted = false;
  1954. unmuteHeadphone();
  1955. }
  1956. n = n | (n << 8);
  1957. return write(CHIP_ANA_HP_CTRL, n); // set volume
  1958. }