vs1053.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900
  1. /****************************************************************************
  2. * drivers/audio/vs1053.c
  3. *
  4. * Audio device driver for VLSI Solutions VS1053 Audio codec.
  5. *
  6. * Copyright (C) 2013 Ken Pettit. All rights reserved.
  7. * Author: Ken Pettit <pettitkd@gmail.com>
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. * 3. Neither the name NuttX nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  26. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  27. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  30. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  31. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  33. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. ****************************************************************************/
  37. /****************************************************************************
  38. * Included Files
  39. ****************************************************************************/
  40. #include <nuttx/config.h>
  41. #include <sys/types.h>
  42. #include <sys/ioctl.h>
  43. #include <stdint.h>
  44. #include <stdbool.h>
  45. #include <stdio.h>
  46. #include <fcntl.h>
  47. #include <string.h>
  48. #include <debug.h>
  49. #include <errno.h>
  50. #include <queue.h>
  51. #include <nuttx/kmalloc.h>
  52. #include <nuttx/signal.h>
  53. #include <nuttx/mqueue.h>
  54. #include <nuttx/fs/fs.h>
  55. #include <nuttx/fs/ioctl.h>
  56. #include <nuttx/audio/audio.h>
  57. #include <nuttx/audio/vs1053.h>
  58. #include <nuttx/lib/math.h>
  59. #include "vs1053.h"
  60. /****************************************************************************
  61. * Pre-processor Definitions
  62. ****************************************************************************/
  63. #ifndef CONFIG_VS1053_SPIMODE
  64. # define CONFIG_VS1053_SPIMODE SPIDEV_MODE0
  65. #endif
  66. #ifndef CONFIG_VS1053_XTALI
  67. # define CONFIG_VS1053_XTALI 12288000
  68. #endif
  69. #ifndef CONFIG_VS1053_MP3_DECODE_FREQ
  70. # define CONFIG_VS1053_MP3_DECODE_FREQ 43000000
  71. #endif
  72. #ifndef CONFIG_VS1053_MSG_PRIO
  73. # define CONFIG_VS1053_MSG_PRIO 1
  74. #endif
  75. #ifndef CONFIG_VS1053_BUFFER_SIZE
  76. # define CONFIG_VS1053_BUFFER_SIZE 8192
  77. #endif
  78. #ifndef CONFIG_VS1053_NUM_BUFFERS
  79. # define CONFIG_VS1053_NUM_BUFFERS 2
  80. #endif
  81. #ifndef CONFIG_VS1053_WORKER_STACKSIZE
  82. # define CONFIG_VS1053_WORKER_STACKSIZE 768
  83. #endif
  84. #define VS1053_DUMMY 0xFF
  85. #define VS1053_DEFAULT_XTALI 12288000
  86. #define VS1053_DATA_FREQ 20000000
  87. #define VS1053_RST_USECS 2000
  88. /****************************************************************************
  89. * Private Types
  90. ****************************************************************************/
  91. struct vs1053_struct_s
  92. {
  93. /* We are an audio lower half driver */
  94. struct audio_lowerhalf_s lower;
  95. /* Our specific driver data goes here */
  96. const FAR struct vs1053_lower_s *hw_lower; /* Pointer to the hardware lower functions */
  97. FAR struct spi_dev_s *spi; /* Pointer to the SPI bus */
  98. FAR struct ap_buffer_s *apb; /* Pointer to the buffer we are processing */
  99. struct dq_queue_s apbq; /* Our queue for enqueued buffers */
  100. unsigned long spi_freq; /* Frequency to run the SPI bus at. */
  101. unsigned long chip_freq; /* Current chip frequency */
  102. mqd_t mq; /* Message queue for receiving messages */
  103. char mqname[16]; /* Our message queue name */
  104. pthread_t threadid; /* ID of our thread */
  105. sem_t apbq_sem; /* Audio Pipeline Buffer Queue sem access */
  106. #ifndef CONFIG_AUDIO_EXCLUDE_VOLUME
  107. int16_t volume; /* Current volume level */
  108. #ifndef CONFIG_AUDIO_EXCLUDE_BALANCE
  109. int16_t balance; /* Current balance level */
  110. #endif /* CONFIG_AUDIO_EXCLUDE_BALANCE */
  111. #endif /* CONFIG_AUDIO_EXCLUDE_VOLUME */
  112. #ifndef CONFIG_AUDIO_EXCLUDE_TONE
  113. uint8_t bass; /* Bass level */
  114. uint8_t treble; /* Bass level */
  115. #endif
  116. uint16_t endfillbytes;
  117. uint8_t endfillchar; /* Fill char to send when no more data */
  118. bool running;
  119. bool paused;
  120. bool endmode;
  121. #ifndef CONFIG_AUDIO_EXCLUDE_STOP
  122. bool cancelmode;
  123. #endif
  124. bool busy; /* Set true when device reserved */
  125. };
  126. /****************************************************************************
  127. * Private Function Prototypes
  128. ****************************************************************************/
  129. static int vs1053_getcaps(FAR struct audio_lowerhalf_s *lower, int type,
  130. FAR struct audio_caps_s *pCaps);
  131. static int vs1053_shutdown(FAR struct audio_lowerhalf_s *lower);
  132. #ifdef CONFIG_AUDIO_MULTI_SESSION
  133. static int vs1053_configure(FAR struct audio_lowerhalf_s *lower,
  134. FAR void *session, FAR const struct audio_caps_s *pCaps);
  135. static int vs1053_start(FAR struct audio_lowerhalf_s *lower,
  136. FAR void *session);
  137. #ifndef CONFIG_AUDIO_EXCLUDE_STOP
  138. static int vs1053_stop(FAR struct audio_lowerhalf_s *lower,
  139. FAR void *session);
  140. #endif
  141. #ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
  142. static int vs1053_pause(FAR struct audio_lowerhalf_s *lower,
  143. FAR void *session);
  144. static int vs1053_resume(FAR struct audio_lowerhalf_s *lower,
  145. FAR void *session);
  146. #endif /* CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME */
  147. static int vs1053_reserve(FAR struct audio_lowerhalf_s *lower,
  148. FAR void** ppContext);
  149. static int vs1053_release(FAR struct audio_lowerhalf_s *lower,
  150. FAR void *pContext);
  151. #else
  152. static int vs1053_configure(FAR struct audio_lowerhalf_s *lower,
  153. FAR const struct audio_caps_s *pCaps);
  154. static int vs1053_start(FAR struct audio_lowerhalf_s *lower);
  155. #ifndef CONFIG_AUDIO_EXCLUDE_STOP
  156. static int vs1053_stop(FAR struct audio_lowerhalf_s *lower);
  157. #endif
  158. #ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
  159. static int vs1053_pause(FAR struct audio_lowerhalf_s *lower);
  160. static int vs1053_resume(FAR struct audio_lowerhalf_s *lower);
  161. #endif /* CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME */
  162. static int vs1053_reserve(FAR struct audio_lowerhalf_s *lower);
  163. static int vs1053_release(FAR struct audio_lowerhalf_s *lower);
  164. #endif /* CONFIG_AUDIO_MULTI_SESION */
  165. static int vs1053_enqueuebuffer(FAR struct audio_lowerhalf_s *lower,
  166. FAR struct ap_buffer_s *apb);
  167. static int vs1053_cancelbuffer(FAR struct audio_lowerhalf_s *lower,
  168. FAR struct ap_buffer_s *apb);
  169. static int vs1053_ioctl(FAR struct audio_lowerhalf_s *lower, int cmd,
  170. unsigned long arg);
  171. /****************************************************************************
  172. * Private Data
  173. ****************************************************************************/
  174. static const struct audio_ops_s g_audioops =
  175. {
  176. vs1053_getcaps, /* getcaps */
  177. vs1053_configure, /* configure */
  178. vs1053_shutdown, /* shutdown */
  179. vs1053_start, /* start */
  180. #ifndef CONFIG_AUDIO_EXCLUDE_STOP
  181. vs1053_stop, /* stop */
  182. #endif
  183. #ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
  184. vs1053_pause, /* pause */
  185. vs1053_resume, /* resume */
  186. #endif
  187. NULL, /* alloc_buffer */
  188. NULL, /* free_buffer */
  189. vs1053_enqueuebuffer, /* enqueue_buffer */
  190. vs1053_cancelbuffer, /* cancel_buffer */
  191. vs1053_ioctl, /* ioctl */
  192. NULL, /* read */
  193. NULL, /* write */
  194. vs1053_reserve, /* reserve */
  195. vs1053_release /* release */
  196. };
  197. /* Volume control log table. This table is in increments of 2% of
  198. * requested volume level and is the register value that should be
  199. * programmed to the VS1053 to achieve that volume pecentage.
  200. */
  201. #ifndef CONFIG_AUDIO_EXCLUDE_VOLUME
  202. static const uint8_t g_logtable [] =
  203. {
  204. 254, 170, 140, 122, 110, /* 0 - 8 */
  205. 100, 92, 85, 80, 74, /* 10 - 18 */
  206. 70, 66, 62, 59, 55, /* 20 - 28 */
  207. 52, 49, 47, 44, 42, /* 30 - 38 */
  208. 40, 38, 36, 34, 32, /* 40 - 48 */
  209. 30, 28, 27, 25, 24, /* 50 - 58 */
  210. 22, 21, 19, 18, 17, /* 60 - 68 */
  211. 15, 14, 13, 12, 11, /* 70 - 78 */
  212. 10, 9, 8, 7, 6, /* 80 - 88 */
  213. 5, 4, 3, 2, 1, /* 90 - 98 */
  214. 0 /* 100 */
  215. };
  216. #endif /* CONFIG_AUDIO_EXCLUDE_VOLUME */
  217. /****************************************************************************
  218. * Private Functions
  219. ****************************************************************************/
  220. /************************************************************************************
  221. * Name: vs1053_spi_lock
  222. ************************************************************************************/
  223. static void vs1053_spi_lock(FAR struct spi_dev_s *dev, unsigned long freq_mhz)
  224. {
  225. /* On SPI busses where there are multiple devices, it will be necessary to
  226. * lock SPI to have exclusive access to the busses for a sequence of
  227. * transfers. The bus should be locked before the chip is selected.
  228. *
  229. * This is a blocking call and will not return until we have exclusiv access to
  230. * the SPI buss. We will retain that exclusive access until the bus is unlocked.
  231. */
  232. (void)SPI_LOCK(dev, true);
  233. /* After locking the SPI bus, the we also need call the setfrequency, setbits, and
  234. * setmode methods to make sure that the SPI is properly configured for the device.
  235. * If the SPI buss is being shared, then it may have been left in an incompatible
  236. * state.
  237. */
  238. SPI_SETMODE(dev, CONFIG_VS1053_SPIMODE);
  239. SPI_SETBITS(dev, 8);
  240. (void)SPI_HWFEATURES(dev, 0);
  241. (void)SPI_SETFREQUENCY(dev, freq_mhz);
  242. }
  243. /************************************************************************************
  244. * Name: vs1053_spi_unlock
  245. ************************************************************************************/
  246. static inline void vs1053_spi_unlock(FAR struct spi_dev_s *dev)
  247. {
  248. (void)SPI_LOCK(dev, false);
  249. }
  250. /************************************************************************************
  251. * Name: vs1053_readreg - Read the specified 16-bit register from the
  252. * VS1053 device. Caller must hold the SPI lock.
  253. ************************************************************************************/
  254. static uint16_t vs1053_readreg(FAR struct vs1053_struct_s *dev, uint8_t reg)
  255. {
  256. uint16_t ret;
  257. FAR struct spi_dev_s *spi = dev->spi;
  258. /* Select the AUDIO_CTRL device on the SPI bus */
  259. SPI_SELECT(spi, SPIDEV_AUDIO_CTRL(0), true);
  260. /* Send the WRITE command followed by the address */
  261. SPI_SEND(spi, VS1053_OPCODE_READ);
  262. SPI_SEND(spi, reg);
  263. /* Now read the 16-bit value */
  264. ret = SPI_SEND(spi, VS1053_DUMMY) << 8;
  265. ret |= SPI_SEND(spi, VS1053_DUMMY);
  266. /* Deselect the CODEC */
  267. SPI_SELECT(spi, SPIDEV_AUDIO_CTRL(0), false);
  268. return ret;
  269. }
  270. /************************************************************************************
  271. * Name: vs1053_writereg - Write the specified 16-bit register to the
  272. * VS1053 device. Caller must hold the SPI lock.
  273. ************************************************************************************/
  274. static void vs1053_writereg(FAR struct vs1053_struct_s *dev, uint8_t reg, uint16_t val)
  275. {
  276. FAR struct spi_dev_s *spi = dev->spi;
  277. /* Select the AUDIO_CTRL device on the SPI bus */
  278. audinfo("Write Reg %d = 0x%0X\n", reg, val);
  279. SPI_SELECT(spi, SPIDEV_AUDIO_CTRL(0), true);
  280. /* Send the WRITE command followed by the address */
  281. SPI_SEND(spi, VS1053_OPCODE_WRITE);
  282. SPI_SEND(spi, reg);
  283. /* Now read the 16-bit value */
  284. SPI_SEND(spi, val >> 8);
  285. SPI_SEND(spi, val & 0xFF);
  286. /* Deselect the CODEC */
  287. SPI_SELECT(spi, SPIDEV_AUDIO_CTRL(0), false);
  288. /* Short delay after a write for VS1053 processing time */
  289. nxsig_usleep(10);
  290. }
  291. /****************************************************************************
  292. * Name: vs1053_setfrequency
  293. *
  294. * Description: Get the audio device capabilities
  295. *
  296. ****************************************************************************/
  297. static int vs1053_setfrequency(FAR struct vs1053_struct_s *dev, uint32_t freq)
  298. {
  299. double factor;
  300. uint16_t reg;
  301. uint8_t timeout;
  302. audinfo("Entry\n");
  303. /* Calculate the clock divisor based on the input frequency */
  304. factor = (double) freq / (double) CONFIG_VS1053_XTALI * 10.0 + 0.5;
  305. /* Check the input frequency against bounds */
  306. if (factor > 50.0)
  307. {
  308. audinfo("Frequency too high! Limiting to XTALI * 5\n");
  309. factor = 50.0;
  310. return -EINVAL;
  311. }
  312. if (factor < 10.0)
  313. {
  314. factor = 10.0;
  315. }
  316. /* Calculate the clock mulit register based on the factor */
  317. if ((int) factor == 10)
  318. {
  319. reg = 0;
  320. }
  321. else
  322. {
  323. reg = (((int) factor - 15) / 5) << VS1053_SC_MULT_SHIFT;
  324. }
  325. /* Set the MULT_ADD factor to the max to allow the chip to dynamically
  326. * increase the frequency the maximum amount as needed
  327. */
  328. reg |= (VS1053_SC_ADD_XTALIx20 << VS1053_SC_ADD_SHIFT);
  329. /* If we aren't running with a 12.228Mhz input crystal, then we
  330. * must tell the chip what the frequency is
  331. */
  332. if (CONFIG_VS1053_XTALI != VS1053_DEFAULT_XTALI)
  333. {
  334. /* Calculate register value based on equation: (XTALI - 8000000) / 4000
  335. * per the datasheet.
  336. */
  337. reg |= (CONFIG_VS1053_XTALI - 8000000) / 4000;
  338. }
  339. /* Now set the new clock multiplier register */
  340. vs1053_writereg(dev, VS1053_SCI_CLOCKF, reg);
  341. /* Wait for DREQ to go active */
  342. timeout = 200;
  343. while (!dev->hw_lower->read_dreq(dev->hw_lower) && timeout)
  344. {
  345. nxsig_usleep(1000);
  346. timeout--;
  347. }
  348. /* Update our internal variables */
  349. dev->chip_freq = freq;
  350. dev->spi_freq = freq / 7;
  351. return OK;
  352. }
  353. /************************************************************************************
  354. * Name: vs1053_logapprox - Approximate the register value in .5 dB increments
  355. * level based on the percentage using a log table since
  356. * math libraries aren't available.
  357. ************************************************************************************/
  358. #ifndef CONFIG_AUDIO_EXCLUDE_VOLUME
  359. uint8_t vs1053_logapprox(int percent)
  360. {
  361. /* Check percentage for bounds */
  362. if (percent >= 100)
  363. {
  364. return 0;
  365. }
  366. return (g_logtable[percent >> 1] + g_logtable[(percent+1) >> 1]) >> 1;
  367. }
  368. #endif /* CONFIG_AUDIO_EXCLUDE_VOLUME */
  369. /************************************************************************************
  370. * Name: vs1053_setvolume - Set the right and left volume values in the VS1053
  371. * device based on the current volume and balance settings.
  372. ************************************************************************************/
  373. #ifndef CONFIG_AUDIO_EXCLUDE_VOLUME
  374. static void vs1053_setvolume(FAR struct vs1053_struct_s *dev)
  375. {
  376. FAR struct spi_dev_s *spi = dev->spi;
  377. uint32_t leftlevel;
  378. uint32_t rightlevel;
  379. uint8_t leftreg, rightreg;
  380. /* Constrain balance */
  381. #ifndef CONFIG_AUDIO_EXCLUDE_BALANCE
  382. if (dev->balance > 1000)
  383. {
  384. dev->balance = 1000;
  385. }
  386. /* Calculate the left channel volume level */
  387. if (dev->balance <= 500)
  388. {
  389. leftlevel = dev->volume;
  390. }
  391. else if (dev->balance == 1000)
  392. {
  393. leftlevel = 0;
  394. }
  395. else
  396. {
  397. leftlevel = dev->volume * (1000 - dev->balance) / 500;
  398. }
  399. /* Calculate the right channel volume level */
  400. if (dev->balance >= 500)
  401. {
  402. rightlevel = dev->volume;
  403. }
  404. else if (dev->balance == 0)
  405. {
  406. rightlevel = 0;
  407. }
  408. else
  409. {
  410. rightlevel = dev->volume * dev->balance / 500;
  411. }
  412. #else
  413. leftlevel = rightlevel = dev->volume;
  414. #endif
  415. /* Calculate the left and right register values */
  416. /* The register sets the volume in dB which is a logrithmic scale,
  417. * so we must use log() to calculate the register value.
  418. */
  419. leftreg = vs1053_logapprox(leftlevel / 10);
  420. rightreg = vs1053_logapprox(rightlevel / 10);
  421. /* Lock the SPI bus to get exclusive access to the chip. */
  422. vs1053_spi_lock(spi, dev->spi_freq);
  423. vs1053_writereg(dev, VS1053_SCI_VOL, (leftreg << 8) | rightreg);
  424. vs1053_spi_unlock(spi);
  425. }
  426. #endif /* CONFIG_AUDIO_EXCLUDE_VOLUME */
  427. /************************************************************************************
  428. * Name: vs1053_setbass - Set the bass and treble level as specified in the
  429. * context's bass and treble variables..
  430. *
  431. * The level and range are in whole percentage levels (0-100).
  432. *
  433. ************************************************************************************/
  434. #ifndef CONFIG_AUDIO_EXCLUDE_TONE
  435. static void vs1053_setbass(FAR struct vs1053_struct_s *dev)
  436. {
  437. FAR struct spi_dev_s *spi = dev->spi;
  438. int bass_range, bass_boost;
  439. int treble_range, treble_boost;
  440. /* Calculate range and boost based on level */
  441. bass_boost = 15 * dev->bass / 100;
  442. bass_range = 15;
  443. treble_boost = 15 * dev->treble / 100;
  444. treble_range = 15;
  445. /* Lock the SPI bus to get exclsive access to the chip. */
  446. vs1053_spi_lock(spi, dev->spi_freq);
  447. vs1053_writereg(dev, VS1053_SCI_BASS, (treble_boost << 12) | (treble_range << 8) |
  448. (bass_boost << 4) | bass_range);
  449. vs1053_spi_unlock(spi);
  450. }
  451. #endif /* CONFIG_AUDIO_EXCLUDE_TONE */
  452. /****************************************************************************
  453. * Name: vs1053_getcaps
  454. *
  455. * Description: Get the audio device capabilities
  456. *
  457. ****************************************************************************/
  458. static int vs1053_getcaps(FAR struct audio_lowerhalf_s *lower, int type,
  459. FAR struct audio_caps_s *pCaps)
  460. {
  461. audinfo("Entry\n");
  462. /* Validate the structure */
  463. DEBUGASSERT(pCaps->ac_len >= sizeof(struct audio_caps_s));
  464. /* Fill in the caller's structure based on requested info */
  465. pCaps->ac_format.hw = 0;
  466. pCaps->ac_controls.w = 0;
  467. switch (pCaps->ac_type)
  468. {
  469. /* Caller is querying for the types of units we support */
  470. case AUDIO_TYPE_QUERY:
  471. /* Provide our overall capabilities. The interfacing software
  472. * must then call us back for specific info for each capability.
  473. */
  474. pCaps->ac_channels = 2; /* Stereo output */
  475. switch (pCaps->ac_subtype)
  476. {
  477. case AUDIO_TYPE_QUERY:
  478. /* The input formats we can decode / accept */
  479. pCaps->ac_format.hw = 0
  480. #ifdef CONFIG_AUDIO_FORMAT_AC3
  481. | (1 << (AUDIO_FMT_AC3 - 1))
  482. #endif
  483. #ifdef CONFIG_AUDIO_FORMAT_MP3
  484. | (1 << (AUDIO_FMT_MP3 - 1))
  485. #endif
  486. #ifdef CONFIG_AUDIO_FORMAT_WMA
  487. | (1 << (AUDIO_FMT_WMA - 1))
  488. #endif
  489. #ifdef CONFIG_AUDIO_FORMAT_MIDI
  490. | (1 << (AUDIO_FMT_MIDI - 1))
  491. #endif
  492. #ifdef CONFIG_AUDIO_FORMAT_PCM
  493. | (1 << (AUDIO_FMT_PCM - 1))
  494. #endif
  495. #ifdef CONFIG_AUDIO_FORMAT_OGG_VORBIS
  496. | (1 << (AUDIO_FMT_OGG_VORBIS - 1))
  497. #endif
  498. ;
  499. /* The types of audio units we implement */
  500. pCaps->ac_controls.b[0] = AUDIO_TYPE_OUTPUT | AUDIO_TYPE_FEATURE |
  501. AUDIO_TYPE_PROCESSING;
  502. break;
  503. /* Report sub-formats for MIDI if requested */
  504. #ifdef CONFIG_AUDIO_FORMAT_MIDI
  505. case AUDIO_FMT_MIDI:
  506. /* We only support Format 0 */
  507. pCaps->ac_controls.b[0] = AUDIO_SUBFMT_MIDI_0;
  508. pCaps->ac_controls.b[1] = AUDIO_SUBFMT_END;
  509. break;
  510. #endif
  511. default:
  512. pCaps->ac_controls.b[0] = AUDIO_SUBFMT_END;
  513. break;
  514. }
  515. break;
  516. /* Provide capabilities of our OUTPUT unit */
  517. case AUDIO_TYPE_OUTPUT:
  518. pCaps->ac_channels = 2;
  519. switch (pCaps->ac_subtype)
  520. {
  521. case AUDIO_TYPE_QUERY:
  522. /* Report the Sample rates we support */
  523. pCaps->ac_controls.b[0] = AUDIO_SAMP_RATE_8K | AUDIO_SAMP_RATE_11K |
  524. AUDIO_SAMP_RATE_16K | AUDIO_SAMP_RATE_22K |
  525. AUDIO_SAMP_RATE_32K | AUDIO_SAMP_RATE_44K |
  526. AUDIO_SAMP_RATE_48K;
  527. break;
  528. case AUDIO_FMT_MP3:
  529. case AUDIO_FMT_WMA:
  530. case AUDIO_FMT_PCM:
  531. /* Report the Bit rates we support. The bit rate support is actually a
  532. * complex function of the format and selected sample rate, and the datasheet
  533. * has multiple tables to indicate the supported bit rate vs sample rate vs
  534. * format. The selected sample rate should be provided in the ac_format
  535. * field of the query, and only a single sample rate should be given.
  536. */
  537. /* TODO: Create a table or set of tables to report this! */
  538. break;
  539. default:
  540. break;
  541. }
  542. break;
  543. /* Provide capabilities of our FEATURE units */
  544. case AUDIO_TYPE_FEATURE:
  545. /* If the sub-type is UNDEF, then report the Feature Units we support */
  546. if (pCaps->ac_subtype == AUDIO_FU_UNDEF)
  547. {
  548. /* Fill in the ac_controls section with the Feature Units we have */
  549. pCaps->ac_controls.b[0] = AUDIO_FU_VOLUME | AUDIO_FU_BASS | AUDIO_FU_TREBLE;
  550. pCaps->ac_controls.b[1] = AUDIO_FU_BALANCE >> 8;
  551. }
  552. else
  553. {
  554. /* TODO: Do we need to provide specific info for the Feature Units,
  555. * such as volume setting ranges, etc.?
  556. */
  557. }
  558. break;
  559. /* Provide capabilities of our PROCESSING unit */
  560. case AUDIO_TYPE_PROCESSING:
  561. switch (pCaps->ac_subtype)
  562. {
  563. case AUDIO_PU_UNDEF:
  564. /* Provide the type of Processing Units we support */
  565. pCaps->ac_controls.b[0] = AUDIO_PU_STEREO_EXTENDER;
  566. break;
  567. case AUDIO_PU_STEREO_EXTENDER:
  568. /* Proivde capabilities of our Stereo Extender */
  569. pCaps->ac_controls.b[0] = AUDIO_STEXT_ENABLE | AUDIO_STEXT_WIDTH;
  570. break;
  571. default:
  572. /* Other types of processing uint we don't support */
  573. break;
  574. }
  575. break;
  576. /* All others we don't support */
  577. default:
  578. /* Zero out the fields to indicate no support */
  579. pCaps->ac_subtype = 0;
  580. pCaps->ac_channels = 0;
  581. break;
  582. }
  583. /* Return the length of the audio_caps_s struct for validation of
  584. * proper Audio device type.
  585. */
  586. return pCaps->ac_len;
  587. }
  588. /****************************************************************************
  589. * Name: vs1053_configure
  590. *
  591. * Description: Configure the audio device for the specified mode of
  592. * operation.
  593. *
  594. ****************************************************************************/
  595. #ifdef CONFIG_AUDIO_MULTI_SESSION
  596. static int vs1053_configure(FAR struct audio_lowerhalf_s *lower,
  597. FAR void *session, FAR const struct audio_caps_s *pCaps)
  598. #else
  599. static int vs1053_configure(FAR struct audio_lowerhalf_s *lower,
  600. FAR const struct audio_caps_s *pCaps)
  601. #endif
  602. {
  603. int ret = OK;
  604. #if !defined(CONFIG_AUDIO_EXCLUDE_VOLUME) || !defined(CONFIG_AUDIO_EXCLUDE_TONE)
  605. FAR struct vs1053_struct_s *dev = (struct vs1053_struct_s *) lower;
  606. #endif
  607. audinfo("Entry\n");
  608. /* Process the configure operation */
  609. switch (pCaps->ac_type)
  610. {
  611. case AUDIO_TYPE_FEATURE:
  612. /* Process based on Feature Unit */
  613. switch (pCaps->ac_format.hw)
  614. {
  615. #ifndef CONFIG_AUDIO_EXCLUDE_VOLUME
  616. case AUDIO_FU_VOLUME:
  617. /* Set the volume */
  618. dev->volume = pCaps->ac_controls.hw[0];
  619. vs1053_setvolume(dev);
  620. break;
  621. #endif /* CONFIG_AUDIO_EXCLUDE_VOLUME */
  622. #if !defined(CONFIG_AUDIO_EXCLUDE_TONE) && !defined(CONFIG_AUDIO_EXCLUDE_VOLUME)
  623. case AUDIO_FU_BALANCE:
  624. /* Set the volume */
  625. dev->balance = pCaps->ac_controls.hw[0];
  626. vs1053_setvolume(dev);
  627. break;
  628. #endif
  629. #ifndef CONFIG_AUDIO_EXCLUDE_TONE
  630. case AUDIO_FU_BASS:
  631. /* Set the bass. The percentage level (0-100) is in the
  632. * ac_controls[0] parameter.
  633. */
  634. dev->bass = pCaps->ac_controls.b[0];
  635. if (dev->bass > 100)
  636. dev->bass = 100;
  637. vs1053_setbass(dev);
  638. break;
  639. case AUDIO_FU_TREBLE:
  640. /* Set the treble. The percentage level (0-100) is in the
  641. * ac_controls.b[0] parameter.
  642. */
  643. dev->treble = pCaps->ac_controls.b[0];
  644. if (dev->treble > 100)
  645. dev->treble = 100;
  646. vs1053_setbass(dev);
  647. break;
  648. #endif /* CONFIG_AUDIO_EXCLUDE_TONE */
  649. default:
  650. /* Others we don't support */
  651. break;
  652. }
  653. break;
  654. case AUDIO_TYPE_OUTPUT:
  655. break;
  656. case AUDIO_TYPE_PROCESSING:
  657. /* We only support STEREO_EXTENDER */
  658. if (pCaps->ac_format.hw == AUDIO_PU_STEREO_EXTENDER)
  659. {
  660. }
  661. break;
  662. }
  663. return ret;
  664. }
  665. /****************************************************************************
  666. * Name: vs1053_softreset
  667. *
  668. * Description: Performs a soft reset on the VS1053 chip by setting the
  669. * RESET bit of the MODE register.
  670. *
  671. ****************************************************************************/
  672. static int vs1053_softreset(FAR struct vs1053_struct_s *dev)
  673. {
  674. uint16_t reg;
  675. uint16_t timeout;
  676. /* First disable interrupts, lower the frequency and lock the SPI bus */
  677. dev->hw_lower->disable(dev->hw_lower); /* Disable the DREQ interrupt */
  678. vs1053_spi_lock(dev->spi, VS1053_DEFAULT_XTALI / 7);
  679. /* Now issue a reset command */
  680. reg = vs1053_readreg(dev, VS1053_SCI_MODE);
  681. vs1053_writereg(dev, VS1053_SCI_MODE, reg | VS1053_SM_RESET);
  682. /* Now wait for the SM_RESET to go inactive */
  683. timeout = 1000;
  684. while (vs1053_readreg(dev, VS1053_SCI_MODE) & VS1053_SM_RESET && timeout)
  685. {
  686. timeout--;
  687. }
  688. /* Switch to low frequency, Unlock the SPI bus and exit */
  689. vs1053_setfrequency(dev, CONFIG_VS1053_XTALI);
  690. vs1053_spi_unlock(dev->spi);
  691. return OK;
  692. }
  693. /****************************************************************************
  694. * Name: vs1053_hardreset
  695. *
  696. * Description: Performs a hardware reset on the VS1053 chip by toggling
  697. * the RST line, disabling IRQ, and setting the default
  698. * XTALI frequency.
  699. *
  700. ****************************************************************************/
  701. static int vs1053_hardreset(FAR struct vs1053_struct_s *dev)
  702. {
  703. dev->hw_lower->disable(dev->hw_lower); /* Disable the DREQ interrupt */
  704. dev->hw_lower->reset(dev->hw_lower, false);
  705. nxsig_usleep(10);
  706. dev->hw_lower->reset(dev->hw_lower, true);
  707. nxsig_usleep(VS1053_RST_USECS);
  708. vs1053_setfrequency(dev, CONFIG_VS1053_XTALI); /* Slow speed at first */
  709. return OK;
  710. }
  711. /****************************************************************************
  712. * Name: vs1053_shutdown
  713. *
  714. * Description: Shutdown the VS1053 chip and put it in the lowest power
  715. * state possible.
  716. *
  717. ****************************************************************************/
  718. static int vs1053_shutdown(FAR struct audio_lowerhalf_s *lower)
  719. {
  720. FAR struct vs1053_struct_s *dev = (struct vs1053_struct_s *) lower;
  721. FAR struct spi_dev_s *spi = dev->spi;
  722. audinfo("Entry\n");
  723. vs1053_spi_lock(spi, dev->spi_freq); /* Lock the device */
  724. vs1053_setfrequency(dev, CONFIG_VS1053_XTALI); /* Reduce speed to minimum */
  725. vs1053_writereg(dev, VS1053_SCI_VOL, 0xFEFE); /* Power down the DAC outputs */
  726. vs1053_spi_unlock(spi); /* Unlock the device */
  727. return OK;
  728. }
  729. /****************************************************************************
  730. * Name: vs1053_feeddata
  731. *
  732. * Description: Feeds more data to the vs1053 chip from the enqueued
  733. * buffers. It will continue feeding data until the DREQ
  734. * line indicates it can't accept any more data.
  735. *
  736. ****************************************************************************/
  737. static void vs1053_feeddata(FAR struct vs1053_struct_s *dev)
  738. {
  739. int bytecount;
  740. int ret;
  741. uint8_t *pSamp = NULL;
  742. uint16_t reg;
  743. struct ap_buffer_s *apb;
  744. FAR struct spi_dev_s *spi = dev->spi;
  745. /* Check for false interrupt caused by an SCI transaction */
  746. if (!dev->hw_lower->read_dreq(dev->hw_lower) || dev->paused)
  747. {
  748. return;
  749. }
  750. /* Grab the SPI bus. We can run at 20Mhz because we increased the
  751. * chip frequency above 40Mhz for the decode operation.
  752. */
  753. vs1053_spi_lock(spi, VS1053_DATA_FREQ); /* Lock the SPI bus */
  754. SPI_SELECT(spi, SPIDEV_AUDIO_DATA(0), true); /* Select the VS1053 data bus */
  755. /* Local stack copy of our active buffer */
  756. apb = dev->apb;
  757. //audinfo("Entry apb=%p, Bytes left=%d\n", apb, apb->nbytes - apb->curbyte);
  758. /* Setup pointer to the next sample in the buffer */
  759. if (apb)
  760. {
  761. pSamp = &apb->samp[apb->curbyte];
  762. }
  763. else if (!dev->endmode)
  764. {
  765. SPI_SELECT(spi, SPIDEV_AUDIO_DATA(0), false);
  766. vs1053_spi_unlock(spi);
  767. return;
  768. }
  769. /* Loop until the FIFO is full */
  770. while (dev->hw_lower->read_dreq(dev->hw_lower))
  771. {
  772. /* If endmode, then send fill characters */
  773. if (dev->endmode)
  774. {
  775. bytecount = 32;
  776. while (bytecount)
  777. {
  778. SPI_SEND(spi, dev->endfillchar);
  779. bytecount--;
  780. }
  781. /* For the VS1053, after the file has been played, we must
  782. * send 2052 bytes of endfillchar per the datasheet.
  783. */
  784. dev->endfillbytes += 32;
  785. /* Process end mode logic. We send 2080 bytes of endfillchar as
  786. * directed by the datasheet, then set SM_CANCEL. Then we wait
  787. * until the chip clears SM_CANCEL while sending endfillchar
  788. * 32 bytes at a time.
  789. */
  790. if (dev->endfillbytes == 32*65)
  791. {
  792. /* After at least 2052 bytes, we send an SM_CANCEL */
  793. dev->hw_lower->disable(dev->hw_lower); /* Disable the DREQ interrupt */
  794. (void)SPI_SETFREQUENCY(dev->spi, dev->spi_freq);
  795. reg = vs1053_readreg(dev, VS1053_SCI_MODE);
  796. vs1053_writereg(dev, VS1053_SCI_MODE, reg | VS1053_SM_CANCEL);
  797. dev->hw_lower->enable(dev->hw_lower); /* Enable the DREQ interrupt */
  798. }
  799. else if (dev->endfillbytes >= 32*130)
  800. {
  801. /* Do a hard reset and terminate */
  802. vs1053_hardreset(dev);
  803. dev->running = false;
  804. dev->endmode = false;
  805. break;
  806. }
  807. else if (dev->endfillbytes > 32*65)
  808. {
  809. /* After each 32 byte of endfillchar, check the status
  810. * register to see if SM_CANCEL has been cleared. If
  811. * it has been cleared, then we're done.
  812. */
  813. if (!(vs1053_readreg(dev, VS1053_SCI_STATUS) & VS1053_SM_CANCEL))
  814. {
  815. (void)SPI_SETFREQUENCY(dev->spi, dev->spi_freq);
  816. dev->hw_lower->disable(dev->hw_lower); /* Disable the DREQ interrupt */
  817. audinfo("HDAT1: 0x%0X HDAT0: 0x%0X\n",
  818. vs1053_readreg(dev, VS1053_SCI_HDAT1),
  819. vs1053_readreg(dev, VS1053_SCI_HDAT0));
  820. vs1053_writereg(dev, VS1053_SCI_WRAMADDR, VS1053_END_FILL_BYTE);
  821. dev->endfillchar = vs1053_readreg(dev, VS1053_SCI_WRAM) >> 8;
  822. audinfo("EndFillChar: 0x%0X\n", dev->endfillchar);
  823. reg = vs1053_readreg(dev, VS1053_SCI_MODE);
  824. vs1053_writereg(dev, VS1053_SCI_MODE, reg | VS1053_SM_RESET);
  825. dev->running = false;
  826. dev->endmode = false;
  827. break;
  828. }
  829. }
  830. }
  831. else
  832. {
  833. /* Send 32 more bytes. We only send 32 at a time because this is
  834. * the meaning of DREQ active from the chip ... that it can
  835. * accept at least 32 more bytes. After each 32 byte block, we
  836. * will recheck the DREQ line again.
  837. */
  838. bytecount = apb->nbytes - apb->curbyte;
  839. if (bytecount > 32)
  840. {
  841. bytecount = 32;
  842. }
  843. #if 1
  844. SPI_SNDBLOCK(spi, pSamp, bytecount);
  845. pSamp += bytecount;
  846. #else
  847. bytecount = bytecount;
  848. while (bytecount--)
  849. {
  850. /* Send next byte from the buffer */
  851. SPI_SEND(spi, *pSamp);
  852. pSamp++;
  853. }
  854. #endif
  855. apb->curbyte += bytecount;
  856. /* Test if we are in cancel mode. If we are, then we need
  857. * to continue sending file data and check for the SM_CANCEL
  858. * bit going inactive.
  859. */
  860. #ifndef CONFIG_AUDIO_EXCLUDE_STOP
  861. if (dev->cancelmode)
  862. {
  863. /* Read the VS1053 MODE register */
  864. dev->hw_lower->disable(dev->hw_lower); /* Disable the DREQ interrupt */
  865. (void)SPI_SETFREQUENCY(dev->spi, dev->spi_freq);
  866. reg = vs1053_readreg(dev, VS1053_SCI_MODE);
  867. (void)SPI_SETFREQUENCY(dev->spi, VS1053_DATA_FREQ);
  868. dev->hw_lower->enable(dev->hw_lower); /* Enable the DREQ interrupt */
  869. /* Check the SM_CANCEL bit */
  870. if (!(reg & VS1053_SM_CANCEL))
  871. {
  872. /* Cancel has begun. Switch to endmode */
  873. apb->nbytes = 0;
  874. apb->curbyte = 0;
  875. }
  876. }
  877. #endif /* CONFIG_AUDIO_EXCLUDE_STOP */
  878. /* Test if we are at the end of the buffer */
  879. if (apb->curbyte >= apb->nbytes)
  880. {
  881. /* Check if this was the final buffer in stream */
  882. if ((apb->flags & AUDIO_APB_FINAL) != 0)
  883. {
  884. /* This is the final buffer. Get the VS1053 endfillchar */
  885. dev->hw_lower->disable(dev->hw_lower); /* Disable the DREQ interrupt */
  886. (void)SPI_SETFREQUENCY(dev->spi, dev->spi_freq);
  887. vs1053_writereg(dev, VS1053_SCI_WRAMADDR, VS1053_END_FILL_BYTE);
  888. dev->endfillchar = vs1053_readreg(dev, VS1053_SCI_WRAM) >> 8;
  889. (void)SPI_SETFREQUENCY(dev->spi, VS1053_DATA_FREQ);
  890. dev->hw_lower->enable(dev->hw_lower); /* Enable the DREQ interrupt */
  891. /* Mark the device as endmode */
  892. dev->endmode = true;
  893. #ifndef CONFIG_AUDIO_EXCLUDE_STOP
  894. if (dev->cancelmode)
  895. {
  896. /* If we are in cancel mode, then we don't dequeue the buffer
  897. * or need to send another SM_CANCEL, so jump into the middle
  898. * of the stop sequence.
  899. */
  900. dev->endfillbytes = 32*65+1;
  901. continue;
  902. }
  903. else
  904. #endif /* CONFIG_AUDIO_EXCLUDE_STOP */
  905. {
  906. dev->endfillbytes = 0;
  907. }
  908. }
  909. /* We referenced the buffer so we must free it */
  910. apb_free(apb);
  911. #ifdef CONFIG_AUDIO_MULTI_SESSION
  912. dev->lower.upper(dev->lower.priv, AUDIO_CALLBACK_DEQUEUE,
  913. apb, OK, NULL);
  914. #else
  915. dev->lower.upper(dev->lower.priv, AUDIO_CALLBACK_DEQUEUE,
  916. apb, OK);
  917. #endif
  918. /* Lock the buffer queue to pop the next buffer */
  919. if ((ret = nxsem_wait(&dev->apbq_sem)) < 0)
  920. {
  921. #ifdef CONFIG_AUDIO_MULTI_SESSION
  922. dev->lower.upper(dev->lower.priv,
  923. AUDIO_CALLBACK_IOERR, NULL, ret, NULL);
  924. #else
  925. dev->lower.upper(dev->lower.priv,
  926. AUDIO_CALLBACK_IOERR, NULL, ret);
  927. #endif
  928. auderr("ERROR: I/O error!\n");
  929. goto err_out;
  930. }
  931. /* Pop the next entry */
  932. apb = (struct ap_buffer_s *) dq_remfirst(&dev->apbq);
  933. dev->apb = apb;
  934. //audinfo("Next Buffer = %p, bytes = %d\n", apb, apb ? apb->nbytes : 0);
  935. if (apb == NULL)
  936. {
  937. nxsem_post(&dev->apbq_sem);
  938. break;
  939. }
  940. pSamp = &apb->samp[apb->curbyte];
  941. apb_reference(apb); /* Add our buffer reference */
  942. nxsem_post(&dev->apbq_sem);
  943. }
  944. }
  945. }
  946. /* Deselect the SPI bus and unlock it */
  947. err_out:
  948. SPI_SELECT(spi, SPIDEV_AUDIO_DATA(0), false);
  949. vs1053_spi_unlock(spi);
  950. }
  951. /****************************************************************************
  952. * Name: vs1053_dreq_isr
  953. *
  954. * This is the ISR that services the DREQ pin from the VS1053, which
  955. * indicates the chip is ready to receive additional data. We use it to
  956. * send a message to our workertherad message queue so it knows to wake
  957. * up and send more data.
  958. *
  959. ****************************************************************************/
  960. static int vs1053_dreq_isr(int irq, FAR void *context, FAR void *arg)
  961. {
  962. struct vs1053_struct_s *dev = (struct vs1053_struct_s *)arg;
  963. struct audio_msg_s msg;
  964. DEBUGASSERT(dev != NULL);
  965. /* Now create a message and send it to the workerthread */
  966. if (dev->running)
  967. {
  968. msg.msgId = AUDIO_MSG_DATA_REQUEST;
  969. (void)nxmq_send(dev->mq, (FAR const char *)&msg, sizeof(msg),
  970. CONFIG_VS1053_MSG_PRIO);
  971. }
  972. else
  973. {
  974. msg.msgId = AUDIO_MSG_DATA_REQUEST;
  975. }
  976. return 0;
  977. }
  978. /****************************************************************************
  979. * Name: vs1053_workerthread
  980. *
  981. * This is the thread that feeds data to the chip and keeps the audio
  982. * stream going.
  983. *
  984. ****************************************************************************/
  985. static void *vs1053_workerthread(pthread_addr_t pvarg)
  986. {
  987. FAR struct vs1053_struct_s *dev = (struct vs1053_struct_s *) pvarg;
  988. struct audio_msg_s msg;
  989. FAR struct ap_buffer_s *apb;
  990. int size;
  991. unsigned int prio;
  992. #ifndef CONFIG_AUDIO_EXCLUDE_STOP
  993. uint16_t reg;
  994. #endif
  995. uint8_t timeout;
  996. audinfo("Entry\n");
  997. #ifndef CONFIG_AUDIO_EXCLUDE_STOP
  998. dev->cancelmode = false;
  999. #endif
  1000. dev->endmode = false;
  1001. dev->endfillbytes = 0;
  1002. /* Fill the VS1053 FIFO with initial data. */
  1003. vs1053_feeddata(dev); /* Fill the VS1053 FIFO */
  1004. /* Wait for DREQ to go active so we can issue a READ command */
  1005. timeout = 200;
  1006. while (!dev->hw_lower->read_dreq(dev->hw_lower) && timeout)
  1007. {
  1008. nxsig_usleep(100);
  1009. timeout--;
  1010. }
  1011. /* Loop as long as we are supposed to be running */
  1012. dev->running = true;
  1013. dev->hw_lower->enable(dev->hw_lower); /* Enable the DREQ interrupt */
  1014. while (dev->running || dev->endmode)
  1015. {
  1016. if (dev->hw_lower->read_dreq(dev->hw_lower))
  1017. {
  1018. vs1053_feeddata(dev); /* Feed more data to the VS1053 FIFO */
  1019. }
  1020. /* Wait for messages from our message queue */
  1021. size = nxmq_receive(dev->mq, (FAR char *)&msg, sizeof(msg), &prio);
  1022. /* Handle the case when we return with no message */
  1023. if (size == 0)
  1024. {
  1025. /* Should we just stop running? */
  1026. dev->running = false;
  1027. break;
  1028. }
  1029. /* Process the message */
  1030. switch (msg.msgId)
  1031. {
  1032. /* The ISR has requested more data */
  1033. case AUDIO_MSG_DATA_REQUEST:
  1034. nxsig_usleep(500);
  1035. vs1053_feeddata(dev); /* Feed more data to the VS1053 FIFO */
  1036. break;
  1037. /* Stop the playback */
  1038. #ifndef CONFIG_AUDIO_EXCLUDE_STOP
  1039. case AUDIO_MSG_STOP:
  1040. if (!dev->hw_lower->read_dreq(dev->hw_lower))
  1041. {
  1042. nxsig_usleep(300);
  1043. }
  1044. /* Send CANCEL message to VS1053 */
  1045. dev->hw_lower->disable(dev->hw_lower);
  1046. vs1053_spi_lock(dev->spi, dev->spi_freq);
  1047. reg = vs1053_readreg(dev, VS1053_SCI_MODE);
  1048. vs1053_writereg(dev, VS1053_SCI_MODE, reg | VS1053_SM_CANCEL);
  1049. vs1053_spi_unlock(dev->spi);
  1050. dev->hw_lower->enable(dev->hw_lower);
  1051. /* Set cancelmode */
  1052. dev->cancelmode = true;
  1053. break;
  1054. #endif
  1055. /* We will wake up when a new buffer enqueued just in case */
  1056. case AUDIO_MSG_ENQUEUE:
  1057. break;
  1058. default:
  1059. break;
  1060. }
  1061. }
  1062. /* Disable the DREQ interrupt */
  1063. dev->hw_lower->disable(dev->hw_lower);
  1064. /* Cancel any leftover buffer in our queue */
  1065. if (nxsem_wait(&dev->apbq_sem) == OK)
  1066. {
  1067. /* Get the next buffer from the queue */
  1068. while ((apb = (FAR struct ap_buffer_s *) dq_remfirst(&dev->apbq)) != NULL)
  1069. ;
  1070. }
  1071. nxsem_post(&dev->apbq_sem);
  1072. /* Free the active buffer */
  1073. if (dev->apb != NULL)
  1074. {
  1075. apb_free(dev->apb);
  1076. dev->apb = NULL;
  1077. }
  1078. /* Close the message queue */
  1079. mq_close(dev->mq);
  1080. mq_unlink(dev->mqname);
  1081. dev->mq = NULL;
  1082. /* Send an AUDIO_MSG_COMPLETE message to the client */
  1083. #ifdef CONFIG_AUDIO_MULTI_SESSION
  1084. dev->lower.upper(dev->lower.priv, AUDIO_CALLBACK_COMPLETE, NULL, OK, NULL);
  1085. #else
  1086. dev->lower.upper(dev->lower.priv, AUDIO_CALLBACK_COMPLETE, NULL, OK);
  1087. #endif
  1088. audinfo("Exit\n");
  1089. return NULL;
  1090. }
  1091. /****************************************************************************
  1092. * Name: vs1053_start
  1093. *
  1094. * Description: Start the configured operation (audio streaming, volume
  1095. * enabled, etc.).
  1096. *
  1097. ****************************************************************************/
  1098. #ifdef CONFIG_AUDIO_MULTI_SESSION
  1099. static int vs1053_start(FAR struct audio_lowerhalf_s *lower, FAR void *session)
  1100. #else
  1101. static int vs1053_start(FAR struct audio_lowerhalf_s *lower)
  1102. #endif
  1103. {
  1104. FAR struct vs1053_struct_s *dev = (struct vs1053_struct_s *) lower;
  1105. struct mq_attr attr;
  1106. struct sched_param sparam;
  1107. pthread_attr_t tattr;
  1108. int ret;
  1109. void *value;
  1110. audinfo("Entry\n");
  1111. vs1053_spi_lock(dev->spi, dev->spi_freq); /* Lock the device */
  1112. audinfo("Entry HDAT1=0x%0X HDAT0=0x%0X\n",
  1113. vs1053_readreg(dev, VS1053_SCI_HDAT1),
  1114. vs1053_readreg(dev, VS1053_SCI_HDAT0));
  1115. vs1053_spi_unlock(dev->spi);
  1116. /* Do a soft reset, just in case */
  1117. vs1053_softreset(dev);
  1118. /* Increase the frequency of the part during processing */
  1119. vs1053_spi_lock(dev->spi, dev->spi_freq); /* Lock the device */
  1120. vs1053_setfrequency(dev, CONFIG_VS1053_MP3_DECODE_FREQ);
  1121. audinfo("Reset HDAT1=0x%0X HDAT0=0x%0X\n",
  1122. vs1053_readreg(dev, VS1053_SCI_HDAT1),
  1123. vs1053_readreg(dev, VS1053_SCI_HDAT0));
  1124. vs1053_spi_unlock(dev->spi);
  1125. /* Create a message queue for the worker thread */
  1126. snprintf(dev->mqname, sizeof(dev->mqname), "/tmp/%X", dev);
  1127. attr.mq_maxmsg = 16;
  1128. attr.mq_msgsize = sizeof(struct audio_msg_s);
  1129. attr.mq_curmsgs = 0;
  1130. attr.mq_flags = 0;
  1131. dev->mq = mq_open(dev->mqname, O_RDWR | O_CREAT, 0644, &attr);
  1132. if (dev->mq == NULL)
  1133. {
  1134. /* Error creating message queue! */
  1135. auderr("ERROR: Couldn't allocate message queue\n");
  1136. return -ENOMEM;
  1137. }
  1138. /* Pop the first enqueued buffer */
  1139. if ((ret = nxsem_wait(&dev->apbq_sem)) == OK)
  1140. {
  1141. dev->apb = (FAR struct ap_buffer_s *) dq_remfirst(&dev->apbq);
  1142. apb_reference(dev->apb); /* Add our buffer reference */
  1143. nxsem_post(&dev->apbq_sem);
  1144. }
  1145. else
  1146. {
  1147. auderr("ERROR: Error getting APB Queue sem\n");
  1148. return ret;
  1149. }
  1150. /* Join any old worker thread we had created to prevent a memory leak */
  1151. if (dev->threadid != 0)
  1152. {
  1153. audinfo("Joining old thread\n");
  1154. pthread_join(dev->threadid, &value);
  1155. }
  1156. /* Start our thread for sending data to the device */
  1157. pthread_attr_init(&tattr);
  1158. sparam.sched_priority = sched_get_priority_max(SCHED_FIFO) - 3;
  1159. (void)pthread_attr_setschedparam(&tattr, &sparam);
  1160. (void)pthread_attr_setstacksize(&tattr, CONFIG_VS1053_WORKER_STACKSIZE);
  1161. audinfo("Starting workerthread\n");
  1162. ret = pthread_create(&dev->threadid, &tattr, vs1053_workerthread,
  1163. (pthread_addr_t) dev);
  1164. if (ret != OK)
  1165. {
  1166. auderr("ERROR: Can't create worker thread, errno=%d\n", errno);
  1167. }
  1168. else
  1169. {
  1170. pthread_setname_np(dev->threadid, "vs1053");
  1171. audinfo("Created worker thread\n");
  1172. }
  1173. return ret;
  1174. }
  1175. /****************************************************************************
  1176. * Name: vs1053_stop
  1177. *
  1178. * Description: Stop the configured operation (audio streaming, volume
  1179. * disabled, etc.).
  1180. *
  1181. ****************************************************************************/
  1182. #ifndef CONFIG_AUDIO_EXCLUDE_STOP
  1183. #ifdef CONFIG_AUDIO_MULTI_SESSION
  1184. static int vs1053_stop(FAR struct audio_lowerhalf_s *lower, FAR void *session)
  1185. #else
  1186. static int vs1053_stop(FAR struct audio_lowerhalf_s *lower)
  1187. #endif
  1188. {
  1189. FAR struct vs1053_struct_s *dev = (struct vs1053_struct_s *) lower;
  1190. struct audio_msg_s term_msg;
  1191. FAR void *value;
  1192. /* Send a message to stop all audio streaming */
  1193. term_msg.msgId = AUDIO_MSG_STOP;
  1194. term_msg.u.data = 0;
  1195. (void)nxmq_send(dev->mq, (FAR const char *)&term_msg, sizeof(term_msg),
  1196. CONFIG_VS1053_MSG_PRIO);
  1197. /* Join the worker thread */
  1198. pthread_join(dev->threadid, &value);
  1199. dev->threadid = 0;
  1200. /* Reduce the decoder's operating frequency to save power */
  1201. vs1053_spi_lock(dev->spi, dev->spi_freq); /* Lock the device */
  1202. vs1053_setfrequency(dev, CONFIG_VS1053_XTALI);
  1203. vs1053_spi_unlock(dev->spi);
  1204. /* Wait for a bit */
  1205. up_mdelay(40);
  1206. return OK;
  1207. }
  1208. #endif
  1209. /****************************************************************************
  1210. * Name: vs1053_pause
  1211. *
  1212. * Description: Pauses the playback.
  1213. *
  1214. ****************************************************************************/
  1215. #ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
  1216. #ifdef CONFIG_AUDIO_MULTI_SESSION
  1217. static int vs1053_pause(FAR struct audio_lowerhalf_s *lower, FAR void *session)
  1218. #else
  1219. static int vs1053_pause(FAR struct audio_lowerhalf_s *lower)
  1220. #endif
  1221. {
  1222. FAR struct vs1053_struct_s *dev = (struct vs1053_struct_s *) lower;
  1223. if (!dev->running)
  1224. {
  1225. return OK;
  1226. }
  1227. /* Disable interrupts to prevent us from supplying any more data */
  1228. dev->paused = true;
  1229. dev->hw_lower->disable(dev->hw_lower); /* Disable the DREQ interrupt */
  1230. return OK;
  1231. }
  1232. #endif /* CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME */
  1233. /****************************************************************************
  1234. * Name: vs1053_resume
  1235. *
  1236. * Description: Resuems the playback.
  1237. *
  1238. ****************************************************************************/
  1239. #ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
  1240. #ifdef CONFIG_AUDIO_MULTI_SESSION
  1241. static int vs1053_resume(FAR struct audio_lowerhalf_s *lower, FAR void *session)
  1242. #else
  1243. static int vs1053_resume(FAR struct audio_lowerhalf_s *lower)
  1244. #endif
  1245. {
  1246. FAR struct vs1053_struct_s *dev = (struct vs1053_struct_s *) lower;
  1247. if (!dev->running)
  1248. {
  1249. return OK;
  1250. }
  1251. /* Enable interrupts to allow suppling data */
  1252. dev->paused = false;
  1253. vs1053_feeddata(dev);
  1254. dev->hw_lower->enable(dev->hw_lower); /* Enable the DREQ interrupt */
  1255. return OK;
  1256. }
  1257. #endif /* CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME */
  1258. /****************************************************************************
  1259. * Name: vs1053_enqueuebuffer
  1260. *
  1261. * Description: Enqueue an Audio Pipeline Buffer for playback/ processing.
  1262. *
  1263. ****************************************************************************/
  1264. static int vs1053_enqueuebuffer(FAR struct audio_lowerhalf_s *lower,
  1265. FAR struct ap_buffer_s *apb)
  1266. {
  1267. FAR struct vs1053_struct_s *dev = (struct vs1053_struct_s *)lower;
  1268. struct audio_msg_s term_msg;
  1269. int ret;
  1270. audinfo("Entry\n");
  1271. /* Lock access to the apbq */
  1272. if ((ret = nxsem_wait(&dev->apbq_sem)) == OK)
  1273. {
  1274. /* We can now safely add the buffer to the queue */
  1275. apb->curbyte = 0;
  1276. apb->flags |= AUDIO_APB_OUTPUT_ENQUEUED;
  1277. dq_addlast(&apb->dq_entry, &dev->apbq);
  1278. nxsem_post(&dev->apbq_sem);
  1279. /* Send a message indicating a new buffer enqueued */
  1280. if (dev->mq != NULL)
  1281. {
  1282. term_msg.msgId = AUDIO_MSG_ENQUEUE;
  1283. term_msg.u.data = 0;
  1284. (void)nxmq_send(dev->mq, (FAR const char *)&term_msg,
  1285. sizeof(term_msg), CONFIG_VS1053_MSG_PRIO);
  1286. }
  1287. }
  1288. return ret;
  1289. }
  1290. /****************************************************************************
  1291. * Name: vs1053_cancelbuffer
  1292. *
  1293. * Description: Called when an enqueued buffer is being cancelled.
  1294. *
  1295. ****************************************************************************/
  1296. static int vs1053_cancelbuffer(FAR struct audio_lowerhalf_s *lower,
  1297. FAR struct ap_buffer_s *apb)
  1298. {
  1299. return OK;
  1300. }
  1301. /****************************************************************************
  1302. * Name: vs1053_ioctl
  1303. *
  1304. * Description: Perform a device ioctl
  1305. *
  1306. ****************************************************************************/
  1307. static int vs1053_ioctl(FAR struct audio_lowerhalf_s *lower, int cmd,
  1308. unsigned long arg)
  1309. {
  1310. #ifdef CONFIG_AUDIO_DRIVER_SPECIFIC_BUFFERS
  1311. FAR struct ap_buffer_info_s *pBufInfo;
  1312. #endif
  1313. /* Deal with ioctls passed from the upper-half driver */
  1314. switch (cmd)
  1315. {
  1316. /* Check for AUDIOIOC_HWRESET ioctl. This ioctl is passed straight
  1317. * through from the upper-half audio driver.
  1318. */
  1319. case AUDIOIOC_HWRESET:
  1320. vs1053_hardreset((FAR struct vs1053_struct_s *) lower);
  1321. break;
  1322. /* Report our preferred buffer size and quantity */
  1323. #ifdef CONFIG_AUDIO_DRIVER_SPECIFIC_BUFFERS
  1324. case AUDIOIOC_GETBUFFERINFO:
  1325. pBufInfo = (FAR struct ap_buffer_info_s *) arg;
  1326. pBufInfo->buffer_size = CONFIG_VS1053_BUFFER_SIZE;
  1327. pBufInfo->nbuffers = CONFIG_VS1053_NUM_BUFFERS;
  1328. break;
  1329. #endif
  1330. default:
  1331. break;
  1332. }
  1333. return OK;
  1334. }
  1335. /****************************************************************************
  1336. * Name: vs1053_reserve
  1337. *
  1338. * Description: Reserves a session (the only one we have).
  1339. *
  1340. ****************************************************************************/
  1341. #ifdef CONFIG_AUDIO_MULTI_SESSION
  1342. static int vs1053_reserve(FAR struct audio_lowerhalf_s *lower,
  1343. FAR void **psession)
  1344. #else
  1345. static int vs1053_reserve(FAR struct audio_lowerhalf_s *lower)
  1346. #endif
  1347. {
  1348. FAR struct vs1053_struct_s *dev = (struct vs1053_struct_s *) lower;
  1349. int ret;
  1350. /* Borrow the APBQ semaphore for thread sync */
  1351. ret = nxsem_wait(&dev->apbq_sem);
  1352. if (ret < 0)
  1353. {
  1354. return ret;
  1355. }
  1356. if (dev->busy)
  1357. {
  1358. ret = -EBUSY;
  1359. }
  1360. else
  1361. {
  1362. /* Initialize the session context. We don't really use it. */
  1363. #ifdef CONFIG_AUDIO_MULTI_SESSION
  1364. *psession = NULL;
  1365. #endif
  1366. dev->busy = true;
  1367. dev->running = false;
  1368. dev->paused = false;
  1369. }
  1370. nxsem_post(&dev->apbq_sem);
  1371. return ret;
  1372. }
  1373. /****************************************************************************
  1374. * Name: vs1053_release
  1375. *
  1376. * Description: Releases the session (the only one we have).
  1377. *
  1378. ****************************************************************************/
  1379. #ifdef CONFIG_AUDIO_MULTI_SESSION
  1380. static int vs1053_release(FAR struct audio_lowerhalf_s *lower,
  1381. FAR void *psession)
  1382. #else
  1383. static int vs1053_release(FAR struct audio_lowerhalf_s *lower)
  1384. #endif
  1385. {
  1386. FAR struct vs1053_struct_s *dev = (struct vs1053_struct_s *) lower;
  1387. void *value;
  1388. int ret;
  1389. /* Join any old worker thread we had created to prevent a memory leak */
  1390. if (dev->threadid != 0)
  1391. {
  1392. pthread_join(dev->threadid, &value);
  1393. dev->threadid = 0;
  1394. }
  1395. /* Borrow the APBQ semaphore for thread sync */
  1396. ret = nxsem_wait(&dev->apbq_sem);
  1397. if (ret < 0)
  1398. {
  1399. return ret;
  1400. }
  1401. /* Really we should free any queued buffers here */
  1402. dev->busy = false;
  1403. nxsem_post(&dev->apbq_sem);
  1404. return OK;
  1405. }
  1406. /****************************************************************************
  1407. * Public Functions
  1408. ****************************************************************************/
  1409. /****************************************************************************
  1410. * Name: vs1053_initialize
  1411. *
  1412. * Description:
  1413. * Initialize the VS1053 device
  1414. *
  1415. * Input Parameters:
  1416. * spidevice - This is a placeholder argument until the Audio interface
  1417. * has been flushed out a bit.
  1418. *
  1419. ****************************************************************************/
  1420. struct audio_lowerhalf_s *vs1053_initialize(FAR struct spi_dev_s *spi,
  1421. FAR const struct vs1053_lower_s *lower,
  1422. unsigned int devno)
  1423. {
  1424. FAR struct vs1053_struct_s *dev;
  1425. uint16_t status;
  1426. uint8_t id;
  1427. uint8_t retry;
  1428. /* Sanity check */
  1429. DEBUGASSERT(spi != NULL);
  1430. DEBUGASSERT(lower != NULL);
  1431. DEBUGASSERT(lower->reset != NULL);
  1432. /* Allocate a VS1053 device structure */
  1433. dev = (struct vs1053_struct_s *)kmm_malloc(sizeof(struct vs1053_struct_s));
  1434. if (dev)
  1435. {
  1436. /* Initialize the VS1053 device structure */
  1437. dev->lower.ops = &g_audioops;
  1438. dev->lower.upper = NULL;
  1439. dev->lower.priv = NULL;
  1440. dev->hw_lower = lower;
  1441. dev->spi_freq = CONFIG_VS1053_XTALI / 7;
  1442. dev->spi = spi;
  1443. dev->mq = NULL;
  1444. dev->busy = false;
  1445. dev->threadid = 0;
  1446. dev->running = false;
  1447. #ifndef CONFIG_AUDIO_EXCLUDE_VOLUME
  1448. dev->volume = 250; /* 25% volume as default */
  1449. #ifndef CONFIG_AUDIO_EXCLUDE_BALANCE
  1450. dev->balance = 500; /* Center balance */
  1451. #endif
  1452. #endif
  1453. #ifndef CONFIG_AUDIO_EXCLUDE_TONE
  1454. dev->bass = 0;
  1455. dev->treble = 0;
  1456. #endif
  1457. nxsem_init(&dev->apbq_sem, 0, 1);
  1458. dq_init(&dev->apbq);
  1459. /* Reset the VS1053 chip */
  1460. lower->reset(lower, false);
  1461. up_udelay(10);
  1462. lower->reset(lower, true);
  1463. up_udelay(VS1053_RST_USECS);
  1464. #if CONFIG_VS1053_XTALI == VS1053_DEFAULT_XTALI
  1465. /* If we have a standard crystal, then wait extra time
  1466. * for the DREQ to be active indicating the device is ready
  1467. */
  1468. retry = 200;
  1469. while (!lower->read_dreq(lower) && retry)
  1470. {
  1471. up_udelay(10);
  1472. retry--;
  1473. }
  1474. #endif
  1475. /* Do device detection to validate the chip is there.
  1476. * We have to hold the SPI lock during reads / writes.
  1477. */
  1478. vs1053_spi_lock(spi, dev->spi_freq);
  1479. status = vs1053_readreg(dev, VS1053_SCI_STATUS);
  1480. vs1053_spi_unlock(spi);
  1481. /* Validate the device ID read from the chip */
  1482. id = (status & VS1053_SS_VER) >> VS1053_VER_SHIFT;
  1483. if (id != VS1053_VER_VS1053)
  1484. {
  1485. auderr("ERROR: Unexpected VER bits: 0x%0X\n", id);
  1486. kmm_free(dev);
  1487. return NULL;
  1488. }
  1489. else
  1490. {
  1491. audinfo("VS1053 Detected!\n");
  1492. }
  1493. /* Attach our ISR to this device */
  1494. dev->hw_lower->attach(dev->hw_lower, vs1053_dreq_isr, dev);
  1495. /* Do some initialization of the codec */
  1496. vs1053_shutdown(&dev->lower); /* Go to shutdown state */
  1497. }
  1498. return &dev->lower;
  1499. }