tlv320dac.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. /*!
  2. * Copyright (C) 2008 by egnite GmbH.
  3. * Copyright (C) 2007 by egnite Software GmbH.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. */
  35. /*!
  36. * \file tlv320dac.c
  37. * \brief DAC routines.
  38. *
  39. * \verbatim
  40. * $Id: tlv320dac.c 4473 2012-08-20 15:12:45Z haraldkipp $
  41. * \endverbatim
  42. */
  43. #include <dev/board.h>
  44. #include <dev/irqreg.h>
  45. #include <sys/event.h>
  46. #include <sys/timer.h>
  47. #include <stdlib.h>
  48. #include <string.h>
  49. #include <memdebug.h>
  50. #include <dev/tlv320dac.h>
  51. #ifndef TWI_SLA_DAC
  52. #define TWI_SLA_DAC 0x1A
  53. #endif
  54. /*
  55. * Initial volume.
  56. */
  57. #ifndef TLV320DAC_VOL
  58. #define TLV320DAC_VOL 0x18
  59. #endif
  60. /*
  61. * Number of PCM buffers.
  62. */
  63. #ifndef SAMPLE_BUFFERS
  64. #if defined (AT91SAM9260_EK)
  65. #define SAMPLE_BUFFERS 32
  66. #else
  67. #define SAMPLE_BUFFERS 3
  68. #endif
  69. #endif
  70. /*
  71. * Select TWI pins for bit-banging
  72. */
  73. #if defined(AT91SAM9260_EK)
  74. #ifndef TWI_SCL_BASE
  75. #define TWI_SCL_BASE PIOB_BASE
  76. #endif
  77. #ifndef TWI_SDA_BASE
  78. #define TWI_SDA_BASE PIOB_BASE
  79. #endif
  80. #ifndef TWI_SCL_BIT
  81. #define TWI_SCL_BIT 13
  82. #endif
  83. #ifndef TWI_SDA_BIT
  84. #define TWI_SDA_BIT 12
  85. #endif
  86. #ifndef TWI_DELAY
  87. #define TWI_DELAY 16
  88. #endif
  89. #elif defined(AT91SAM7X_EK)
  90. #ifndef TWI_SCL_BASE
  91. #define TWI_SCL_BASE PIOA_BASE
  92. #endif
  93. #ifndef TWI_SDA_BASE
  94. #define TWI_SDA_BASE PIOA_BASE
  95. #endif
  96. #ifndef TWI_SCL_BIT
  97. #define TWI_SCL_BIT 11
  98. #endif
  99. #ifndef TWI_SDA_BIT
  100. #define TWI_SDA_BIT 10
  101. #endif
  102. #endif
  103. /*
  104. * Select I2S pins.
  105. */
  106. #if defined (MCU_AT91SAM9260) /* _EK */
  107. #define DACI2S_PIO_ID PIOB_ID
  108. #define DACI2S_PINS_A _BV(PB18_TD0_A) | _BV(PB17_TF0_A) | _BV(PB16_TK0_A)
  109. #define DACI2S_PINS_B 0
  110. #else /* _EK */
  111. #define DACI2S_PIO_ID PIOA_ID
  112. #define DACI2S_PINS_A _BV(PA23_TD_A) | _BV(PA21_TF_A) | _BV(PA22_TK_A)
  113. #define DACI2S_PINS_B 0
  114. #endif /* _EK */
  115. /*
  116. * Determine PIO used by I2S.
  117. */
  118. #if DACI2S_PIO_ID == PIOA_ID /* DACI2S_PIO_ID */
  119. #define DACI2S_PDR PIOA_PDR
  120. #define DACI2S_ASR PIOA_ASR
  121. #define DACI2S_BSR PIOA_BSR
  122. #elif DACI2S_PIO_ID == PIOB_ID /* DACI2S_PIO_ID */
  123. #define DACI2S_PDR PIOB_PDR
  124. #define DACI2S_ASR PIOB_ASR
  125. #define DACI2S_BSR PIOB_BSR
  126. #endif /* DACI2S_PIO_ID */
  127. /*!
  128. * \brief I2S event queue.
  129. */
  130. static HANDLE i2s_que;
  131. /*!
  132. * \brief Number of channels.
  133. */
  134. #define PCM_CHANS 2
  135. /*!
  136. * \brief Number of bits per sample.
  137. */
  138. #define PCM_BITS 16
  139. /*!
  140. * \brief PCM buffer type.
  141. */
  142. typedef struct _PCM_BUFFER {
  143. u_short *wbf_dat; /*!< Pointer to PCM data. */
  144. int wbf_siz; /*!< Buffer size. */
  145. int wbf_len; /*!< Number of valid bytes buffered. */
  146. } PCM_BUFFER;
  147. /* PCM buffer queue. */
  148. static PCM_BUFFER pcm_bufq[SAMPLE_BUFFERS];
  149. /* PCM buffer read index. */
  150. static volatile unsigned int brd_idx;
  151. /* PCM buffer read position. */
  152. static volatile int brd_pos;
  153. /* PCM buffer write index. */
  154. static unsigned int bwr_idx;
  155. #if defined(TWI_SCL_BASE) && defined(TWI_SDA_BASE)
  156. #define TWI_ENABLE() { \
  157. outr(TWI_SCL_BASE + PIO_SODR_OFF, _BV(TWI_SCL_BIT)); \
  158. outr(TWI_SCL_BASE + PIO_PER_OFF, _BV(TWI_SCL_BIT)); \
  159. outr(TWI_SCL_BASE + PIO_OER_OFF, _BV(TWI_SCL_BIT)); \
  160. outr(TWI_SDA_BASE + PIO_SODR_OFF, _BV(TWI_SDA_BIT)); \
  161. outr(TWI_SDA_BASE + PIO_PER_OFF, _BV(TWI_SDA_BIT)); \
  162. }
  163. #define SDA_LOW() { \
  164. outr(TWI_SDA_BASE + PIO_CODR_OFF, _BV(TWI_SDA_BIT)); \
  165. outr(TWI_SDA_BASE + PIO_OER_OFF, _BV(TWI_SDA_BIT)); \
  166. }
  167. #define SDA_HIGH() { \
  168. outr(TWI_SDA_BASE + PIO_SODR_OFF, _BV(TWI_SDA_BIT)); \
  169. outr(TWI_SDA_BASE + PIO_ODR_OFF, _BV(TWI_SDA_BIT)); \
  170. }
  171. #define SCL_LOW() outr(TWI_SCL_BASE + PIO_CODR_OFF, _BV(TWI_SCL_BIT))
  172. #define SCL_HIGH() outr(TWI_SCL_BASE + PIO_SODR_OFF, _BV(TWI_SCL_BIT))
  173. #define SDA_STAT() (inr(TWI_SCL_BASE + PIO_PDSR_OFF) & _BV(TWI_SDA_BIT))
  174. #else
  175. #define TWI_ENABLE()
  176. #define SDA_LOW()
  177. #define SDA_HIGH()
  178. #define SCL_LOW()
  179. #define SCL_HIGH()
  180. #define SDA_STAT() (1)
  181. #endif
  182. #ifndef TWI_DELAY
  183. #define TWI_DELAY 8
  184. #endif
  185. /*
  186. * Short delay.
  187. *
  188. * Our bit banging code relies on pull-up resistors. The I/O ports mimic
  189. * open collector outputs by switching to input mode for high level and
  190. * switching to output mode for low level. This is much slower than
  191. * switching an output between low to high. Thus we need some delay.
  192. */
  193. static void TwBitDelay(int nops)
  194. {
  195. while (nops--) {
  196. _NOP();
  197. }
  198. }
  199. /*
  200. * Falling edge on the data line while the clock line is high indicates
  201. * a start condition.
  202. *
  203. * Entry: SCL any, SDA any
  204. * Exit: SCL low, SDA low
  205. */
  206. static void TwBitStart(void)
  207. {
  208. SDA_HIGH();
  209. TwBitDelay(TWI_DELAY);
  210. SCL_HIGH();
  211. TwBitDelay(TWI_DELAY);
  212. SDA_LOW();
  213. TwBitDelay(TWI_DELAY);
  214. SCL_LOW();
  215. TwBitDelay(TWI_DELAY);
  216. }
  217. /*
  218. * Rising edge on the data line while the clock line is high indicates
  219. * a stop condition.
  220. *
  221. * Entry: SCL low, SDA any
  222. * Exit: SCL high, SDA high
  223. */
  224. static void TwBitStop(void)
  225. {
  226. SDA_LOW();
  227. TwBitDelay(TWI_DELAY);
  228. SCL_HIGH();
  229. TwBitDelay(2 * TWI_DELAY);
  230. SDA_HIGH();
  231. TwBitDelay(8 * TWI_DELAY);
  232. }
  233. /*
  234. * Toggles out a single byte in master mode.
  235. *
  236. * Entry: SCL low, SDA any
  237. * Exit: SCL low, SDA high
  238. */
  239. static int TwBitPut(uint8_t octet)
  240. {
  241. int i;
  242. for (i = 0x80; i; i >>= 1) {
  243. /* Set the data bit. */
  244. if (octet & i) {
  245. SDA_HIGH();
  246. } else {
  247. SDA_LOW();
  248. }
  249. /* Wait for data to stabelize. */
  250. TwBitDelay(TWI_DELAY);
  251. /* Toggle the clock. */
  252. SCL_HIGH();
  253. TwBitDelay(2 * TWI_DELAY);
  254. SCL_LOW();
  255. TwBitDelay(TWI_DELAY);
  256. }
  257. /* Set data line high to receive the ACK bit. */
  258. SDA_HIGH();
  259. /* ACK should appear shortly after the clock's rising edge. */
  260. SCL_HIGH();
  261. TwBitDelay(2 * TWI_DELAY);
  262. if (SDA_STAT()) {
  263. i = -1;
  264. } else {
  265. i = 0;
  266. }
  267. SCL_LOW();
  268. return i;
  269. }
  270. /*
  271. * Toggles in a single byte in master mode.
  272. *
  273. * Entry: SCL low, SDA any
  274. * Exit: SCL low, SDA high
  275. */
  276. static uint8_t TwBitGet(void)
  277. {
  278. uint8_t rc = 0;
  279. int i;
  280. /* SDA is input. */
  281. SDA_HIGH();
  282. TwBitDelay(TWI_DELAY);
  283. for (i = 0x80; i; i >>= 1) {
  284. TwBitDelay(TWI_DELAY);
  285. /* Data should appear shortly after the clock's rising edge. */
  286. SCL_HIGH();
  287. TwBitDelay(2 * TWI_DELAY);
  288. /* SDA read. */
  289. if (SDA_STAT()) {
  290. rc |= i;
  291. }
  292. SCL_LOW();
  293. }
  294. return rc;
  295. }
  296. /*
  297. * Toggles out an acknowledge bit in master mode.
  298. *
  299. * Entry: SCL low, SDA any
  300. * Exit: SCL low, SDA high
  301. */
  302. static void TwBitAck(void)
  303. {
  304. SDA_LOW();
  305. TwBitDelay(TWI_DELAY);
  306. SCL_HIGH();
  307. TwBitDelay(2 * TWI_DELAY);
  308. SCL_LOW();
  309. TwBitDelay(TWI_DELAY);
  310. SDA_HIGH();
  311. }
  312. static int TwBitInit(uint8_t sla)
  313. {
  314. TWI_ENABLE();
  315. return 0;
  316. }
  317. static int TwBitMasterTransact(uint8_t sla, const void *txdata, uint16_t txlen, void *rxdata, uint16_t rxsiz, uint32_t tmo)
  318. {
  319. int rc = 0;
  320. uint8_t *cp;
  321. if (txlen) {
  322. TwBitStart();
  323. /* Send SLA+W and check for ACK. */
  324. if ((rc = TwBitPut(sla << 1)) == 0) {
  325. for (cp = (uint8_t *)txdata; txlen--; cp++) {
  326. if ((rc = TwBitPut(*cp)) != 0) {
  327. break;
  328. }
  329. }
  330. }
  331. }
  332. if (rc == 0 && rxsiz) {
  333. TwBitStart();
  334. /* Send SLA+R and check for ACK. */
  335. if ((rc = TwBitPut((sla << 1) | 1)) == 0) {
  336. for (cp = rxdata;; cp++) {
  337. *cp = TwBitGet();
  338. if (++rc >= rxsiz) {
  339. break;
  340. }
  341. TwBitAck();
  342. }
  343. }
  344. }
  345. TwBitStop();
  346. return rc;
  347. }
  348. /*!
  349. * \brief Set PDC pointer and counter registers.
  350. *
  351. * SSC interrupts must have been disabled befor calling this function.
  352. */
  353. static void I2sPdcFill(void)
  354. {
  355. if (brd_idx != bwr_idx) {
  356. if (inr(SSC_TNCR) == 0) {
  357. if (++brd_idx >= SAMPLE_BUFFERS) {
  358. brd_idx = 0;
  359. }
  360. outr(SSC_TNPR, (unsigned int) pcm_bufq[brd_idx].wbf_dat);
  361. outr(SSC_TNCR, pcm_bufq[brd_idx].wbf_len);
  362. }
  363. }
  364. }
  365. /*!
  366. * \brief SSC interrupt handler.
  367. *
  368. * Called when PDC transmission done.
  369. */
  370. static void I2sInterrupt(void *arg)
  371. {
  372. I2sPdcFill();
  373. NutEventPostFromIrq(&i2s_que);
  374. }
  375. /*!
  376. * \brief Read value from specified DAC register.
  377. *
  378. * Not implemented, because the TLV320AIC23B is a write-only device.
  379. *
  380. * \param reg DAC register address.
  381. *
  382. * \return Always 0xFF.
  383. */
  384. u_char Tlv320DacReadReg(unsigned int reg)
  385. {
  386. return 0xFF;
  387. }
  388. /*!
  389. * \brief Write value to specified DAC register.
  390. *
  391. * Communicates with the DAC chip via TWI.
  392. *
  393. * \param reg DAC register address.
  394. * \param val Value to store in specified register.
  395. */
  396. void Tlv320DacWriteReg(unsigned int reg, unsigned int val)
  397. {
  398. u_char txdata[2];
  399. txdata[0] = (u_char)(reg << 1) | (u_char)(val >> 8);
  400. txdata[1] = (u_char)val;
  401. TwBitMasterTransact(TWI_SLA_DAC, txdata, 2, NULL, 0, 0);
  402. }
  403. /*!
  404. * \brief Enable I2S interface with a given rate.
  405. *
  406. * \param rate PCM sample rate in Hertz.
  407. *
  408. * \return Always 0.
  409. */
  410. static int Tlv320I2sEnable(unsigned int rate)
  411. {
  412. /* Enable SSC clock. */
  413. outr(PMC_PCER, _BV(SSC_ID));
  414. /* Select SSC peripheral functions. */
  415. outr(DACI2S_ASR, DACI2S_PINS_A);
  416. outr(DACI2S_BSR, DACI2S_PINS_B);
  417. /* Enable SSC peripheral pins. */
  418. outr(DACI2S_PDR, DACI2S_PINS_A | DACI2S_PINS_B);
  419. /* Configure 16-bit stereo I2S transmit format. */
  420. outr(SSC_CMR, 0);
  421. outr(SSC_TCMR, /* Set transmit clock mode. */
  422. SSC_CKS_PIN | /* Use external clock at TK. */
  423. SSC_START_EDGE_RF | /* Start transmission on any edge. */
  424. (1 << SSC_STTDLY_LSB)); /* Delay start by 1 cycle. */
  425. outr(SSC_TFMR, /* Set transmit frame mode. */
  426. ((PCM_BITS - 1) << SSC_DATLEN_LSB) | /* Transmit 16 bits. */
  427. SSC_MSBF); /* Most significant bit first. */
  428. /* Enable transmitter in PDC mode. */
  429. outr(SSC_PTCR, PDC_TXTEN);
  430. outr(SSC_CR, SSC_TXEN);
  431. return 0;
  432. }
  433. /*!
  434. * \brief Disable I2S interface.
  435. *
  436. * \return Always 0.
  437. */
  438. static int Tlv320I2sDisable(void)
  439. {
  440. /* Disable all interrupts. */
  441. outr(SSC_IDR, 0xFFFFFFFF);
  442. /* Disable SSC interrupt. */
  443. NutIrqDisable(&sig_SSC);
  444. /* Disable SSC clock. */
  445. outr(PMC_PCDR, _BV(SSC_ID));
  446. /* Reset receiver and transmitter. */
  447. outr(SSC_CR, SSC_SWRST | SSC_RXDIS | SSC_TXDIS);
  448. outr(SSC_RCMR, 0);
  449. outr(SSC_RFMR, 0);
  450. outr(SSC_PTCR, PDC_RXTDIS);
  451. outr(SSC_PTCR, PDC_TXTDIS);
  452. outr(SSC_TNCR, 0);
  453. outr(SSC_TCR, 0);
  454. return 0;
  455. }
  456. /*!
  457. * \brief Configure the SSC for I2S mode.
  458. *
  459. * \param rate Sampling rate in Hertz.
  460. *
  461. * \return Always 0.
  462. */
  463. static int Tlv320I2sInit(unsigned int rate)
  464. {
  465. /* Register SSC interrupt handler. */
  466. NutRegisterIrqHandler(&sig_SSC, I2sInterrupt, 0);
  467. Tlv320I2sDisable();
  468. Tlv320I2sEnable(rate);
  469. /* Enable SSC interrupt. */
  470. NutIrqEnable(&sig_SSC);
  471. return 0;
  472. }
  473. int Tlv320DacSetRate(unsigned int rate)
  474. {
  475. switch(rate) {
  476. case 8000:
  477. #ifdef AT91SAM7X_EK
  478. Tlv320DacWriteReg(DAC_SRATE, (3 << DAC_SRATE_SR_LSB));
  479. #else
  480. Tlv320DacWriteReg(DAC_SRATE, (3 << DAC_SRATE_SR_LSB) | DAC_SRATE_USB);
  481. #endif
  482. break;
  483. case 8021:
  484. Tlv320DacWriteReg(DAC_SRATE, (11 << DAC_SRATE_SR_LSB) | DAC_SRATE_BOSR | DAC_SRATE_USB);
  485. break;
  486. case 44100:
  487. Tlv320DacWriteReg(DAC_SRATE, (8 << DAC_SRATE_SR_LSB) | DAC_SRATE_BOSR | DAC_SRATE_USB);
  488. break;
  489. case 48000:
  490. Tlv320DacWriteReg(DAC_SRATE, (0 << DAC_SRATE_SR_LSB) | DAC_SRATE_USB);
  491. break;
  492. case 88200:
  493. Tlv320DacWriteReg(DAC_SRATE, (15 << DAC_SRATE_SR_LSB) | DAC_SRATE_BOSR | DAC_SRATE_USB);
  494. break;
  495. case 96000:
  496. Tlv320DacWriteReg(DAC_SRATE, (7 << DAC_SRATE_SR_LSB) | DAC_SRATE_USB);
  497. break;
  498. default:
  499. return -1;
  500. }
  501. return 0;
  502. }
  503. /*!
  504. * \brief Initialize TLV320AIC23B DAC interface.
  505. *
  506. * \param rate Sample rate.
  507. *
  508. * \return 0 on success, -1 otherwise.
  509. */
  510. int Tlv320DacInit(unsigned int rate)
  511. {
  512. /* Initialize TWI. */
  513. TwBitInit(0);
  514. Tlv320DacWriteReg(DAC_RESET, 0);
  515. /* Power down line in. */
  516. Tlv320DacWriteReg(DAC_PWRDN, DAC_PWRDN_LINE);
  517. Tlv320DacWriteReg(DAC_PWRDN, 0);
  518. /* Set sampling rate. */
  519. if (Tlv320DacSetRate(rate)) {
  520. Tlv320DacWriteReg(DAC_RESET, 0);
  521. return -1;
  522. }
  523. Tlv320DacWriteReg(DAC_ANA_PATH, DAC_ANA_PATH_DAC | DAC_ANA_PATH_INSEL | DAC_ANA_PATH_MICB);
  524. Tlv320DacWriteReg(DAC_DIG_PATH, 0);
  525. /* I2S master. */
  526. Tlv320DacWriteReg(DAC_DAI_FMT, DAC_DAI_FMT_MS | DAC_DAI_FMT_FOR_I2S);
  527. Tlv320DacWriteReg(DAC_DI_ACT, DAC_DI_ACT_ACT);
  528. Tlv320DacWriteReg(DAC_LHP_VOL, DAC_LHP_VOL_LRS | (0x60 << DAC_LHP_VOL_LHV_LSB));
  529. /* Initialize I2S. */
  530. return Tlv320I2sInit(rate);
  531. }
  532. /*!
  533. * \brief Start transmitting audio samples.
  534. *
  535. * \return Always 0.
  536. */
  537. static int Tlv320DacStart(void)
  538. {
  539. NutIrqDisable(&sig_SSC);
  540. outr(SSC_IDR, SSC_TXEMPTY);
  541. /* Enable transmitter in PDC mode. */
  542. outr(SSC_PTCR, PDC_TXTEN);
  543. I2sPdcFill();
  544. outr(SSC_IER, SSC_ENDTX);
  545. /* Enable transmitter. */
  546. outr(SSC_CR, SSC_TXEN);
  547. NutIrqEnable(&sig_SSC);
  548. return 0;
  549. }
  550. /*!
  551. * \brief Wait until all buffered samples have been transmitted.
  552. *
  553. * \return Always 0.
  554. */
  555. int Tlv320DacFlush(void)
  556. {
  557. int rc = 0;
  558. while (bwr_idx != brd_idx) {
  559. if ((rc = NutEventWait(&i2s_que, 100)) != 0) {
  560. Tlv320DacStart();
  561. }
  562. }
  563. return rc;
  564. }
  565. /*!
  566. * \brief Add audio samples to the TLV320AIC23B transmit queue.
  567. *
  568. * \param buf Points to PCM data.
  569. * \param len Number of samples.
  570. *
  571. * \return 0 on success or -1 if out of memory.
  572. */
  573. int Tlv320DacWrite(void *buf, int len)
  574. {
  575. unsigned int idx;
  576. /* Move to the next buffer to write to. */
  577. idx = bwr_idx + 1;
  578. if (idx >= SAMPLE_BUFFERS) {
  579. idx = 0;
  580. }
  581. /* If all buffers are filled, wait for an event posted by the
  582. interrupt routine. */
  583. while (idx == brd_idx) {
  584. if (NutEventWait(&i2s_que, 100)) {
  585. Tlv320DacStart();
  586. }
  587. }
  588. /*
  589. * Check, if the current buffer size is too small or not allocated.
  590. */
  591. if (pcm_bufq[idx].wbf_siz < len) {
  592. if (pcm_bufq[idx].wbf_siz) {
  593. free(pcm_bufq[idx].wbf_dat);
  594. pcm_bufq[idx].wbf_siz = 0;
  595. }
  596. pcm_bufq[idx].wbf_dat = malloc(len * 2);
  597. if (pcm_bufq[idx].wbf_dat == NULL) {
  598. /* Out of memory. */
  599. return -1;
  600. }
  601. pcm_bufq[idx].wbf_siz = len;
  602. }
  603. /*
  604. * At this point we got an available buffer with sufficient size.
  605. * Move the data to it, set the number of valid bytes and update
  606. * the write (producer) index.
  607. */
  608. memcpy(pcm_bufq[idx].wbf_dat, buf, len * 2);
  609. pcm_bufq[idx].wbf_len = len;
  610. bwr_idx = idx;
  611. return 0;
  612. }
  613. /*!
  614. * \brief Set volume.
  615. *
  616. * Sets the master playback gain. Range is +6..-73 dB.
  617. *
  618. * \param left Left channel gain in dB.
  619. * \param right Right channel gain in dB.
  620. *
  621. * \return 0 on success, -1 otherwise.
  622. */
  623. int Tlv320DacSetVolume(int left, int right)
  624. {
  625. /* Cut to limits. */
  626. if (left > DAC_MAX_VOLUME) {
  627. left = DAC_MAX_VOLUME;
  628. }
  629. else if (left < DAC_MIN_VOLUME) {
  630. left = DAC_MIN_VOLUME;
  631. }
  632. if (right > DAC_MAX_VOLUME) {
  633. right = DAC_MAX_VOLUME;
  634. }
  635. else if (right < DAC_MIN_VOLUME) {
  636. right = DAC_MIN_VOLUME;
  637. }
  638. Tlv320DacWriteReg(DAC_LHP_VOL, (unsigned int)(left + 121));
  639. Tlv320DacWriteReg(DAC_RHP_VOL, (unsigned int)(right + 121));
  640. return 0;
  641. }