codec.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. #include <rthw.h>
  2. #include <rtthread.h>
  3. #include "stm32f10x.h"
  4. #include "board.h"
  5. #include "codec.h"
  6. #if CODEC_USE_SPI3
  7. #define CODEC_I2S_PORT SPI3
  8. #define CODEC_I2S_IRQ SPI3_IRQn
  9. #define CODEC_I2S_DMA DMA2_Channel2
  10. #define CODEC_I2S_DMA_IRQ DMA2_Channel2_IRQn
  11. #define CODEC_I2S_RCC_APB1 RCC_APB1Periph_SPI3
  12. #define CODEC_I2S_RCC_AHB RCC_AHBPeriph_DMA2
  13. // I2S3_WS -> PA15
  14. #define CODEC_I2S_WS_PIN GPIO_Pin_15
  15. #define CODEC_I2S_WS_PORT GPIOA
  16. // I2S3_CK -> PB3
  17. #define CODEC_I2S_CK_PIN GPIO_Pin_3
  18. #define CODEC_I2S_CK_PORT GPIOB
  19. // I2S3_SD -> PB5
  20. #define CODEC_I2S_SD_PIN GPIO_Pin_5
  21. #define CODEC_I2S_SD_PORT GPIOB
  22. #else
  23. #define CODEC_I2S_PORT SPI2
  24. #define CODEC_I2S_IRQ SPI2_IRQn
  25. #define CODEC_I2S_DMA DMA1_Channel5
  26. #define CODEC_I2S_DMA_IRQ DMA1_Channel5_IRQn
  27. #define CODEC_I2S_RCC_APB1 RCC_APB1Periph_SPI2
  28. #define CODEC_I2S_RCC_AHB RCC_AHBPeriph_DMA1
  29. // I2S2_WS -> PB12
  30. #define CODEC_I2S_WS_PIN GPIO_Pin_12
  31. #define CODEC_I2S_WS_PORT GPIOB
  32. // I2S2_CK -> PB13
  33. #define CODEC_I2S_CK_PIN GPIO_Pin_13
  34. #define CODEC_I2S_CK_PORT GPIOB
  35. // I2S2_SD -> PB15
  36. #define CODEC_I2S_SD_PIN GPIO_Pin_15
  37. #define CODEC_I2S_SD_PORT GPIOB
  38. #endif // #if CODEC_USE_SPI3
  39. /*
  40. SCLK PA5 SPI1_SCK
  41. SDIN PA7 SPI1_MOSI
  42. CSB PC5
  43. */
  44. #define CODEC_CSB_PORT GPIOC
  45. #define CODEC_CSB_PIN GPIO_Pin_5
  46. #define codec_set_csb() do { CODEC_CSB_PORT->BSRR = CODEC_CSB_PIN; } while (0)
  47. #define codec_reset_csb() do { CODEC_CSB_PORT->BRR = CODEC_CSB_PIN; } while (0)
  48. void vol(uint16_t v);
  49. static void codec_send(rt_uint16_t s_data);
  50. #define DATA_NODE_MAX 5
  51. /* data node for Tx Mode */
  52. struct codec_data_node
  53. {
  54. rt_uint16_t *data_ptr;
  55. rt_size_t data_size;
  56. };
  57. struct codec_device
  58. {
  59. /* inherit from rt_device */
  60. struct rt_device parent;
  61. /* pcm data list */
  62. struct codec_data_node data_list[DATA_NODE_MAX];
  63. rt_uint16_t read_index, put_index;
  64. /* transmitted offset of current data node */
  65. rt_size_t offset;
  66. };
  67. struct codec_device codec;
  68. static uint16_t r06 = REG_CLOCK_GEN | CLKSEL_PLL | MCLK_DIV2 | BCLK_DIV8;
  69. #if !CODEC_MASTER_MODE
  70. static int codec_sr_new = 0;
  71. #endif
  72. static void NVIC_Configuration(void)
  73. {
  74. NVIC_InitTypeDef NVIC_InitStructure;
  75. /* DMA IRQ Channel configuration */
  76. NVIC_InitStructure.NVIC_IRQChannel = CODEC_I2S_DMA_IRQ;
  77. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  78. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  79. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  80. NVIC_Init(&NVIC_InitStructure);
  81. }
  82. static void GPIO_Configuration(void)
  83. {
  84. GPIO_InitTypeDef GPIO_InitStructure;
  85. /* Disable the JTAG interface and enable the SWJ interface */
  86. GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
  87. /* PC5 CODEC CS */
  88. GPIO_InitStructure.GPIO_Pin = CODEC_CSB_PIN;
  89. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  90. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  91. GPIO_Init(CODEC_CSB_PORT, &GPIO_InitStructure);
  92. // WS
  93. GPIO_InitStructure.GPIO_Pin = CODEC_I2S_WS_PIN;
  94. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  95. #if CODEC_MASTER_MODE
  96. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
  97. #else
  98. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  99. #endif
  100. GPIO_Init(CODEC_I2S_WS_PORT, &GPIO_InitStructure);
  101. // CK
  102. GPIO_InitStructure.GPIO_Pin = CODEC_I2S_CK_PIN;
  103. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  104. #if CODEC_MASTER_MODE
  105. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  106. #else
  107. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  108. #endif
  109. GPIO_Init(CODEC_I2S_CK_PORT, &GPIO_InitStructure);
  110. // SD
  111. GPIO_InitStructure.GPIO_Pin = CODEC_I2S_SD_PIN;
  112. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  113. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  114. GPIO_Init(CODEC_I2S_SD_PORT, &GPIO_InitStructure);
  115. #ifdef CODEC_USE_MCO
  116. /* MCO configure */
  117. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  118. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  119. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  120. GPIO_Init(GPIOA,&GPIO_InitStructure);
  121. RCC_MCOConfig(RCC_MCO_HSE);
  122. #endif
  123. }
  124. static void DMA_Configuration(rt_uint32_t addr, rt_size_t size)
  125. {
  126. DMA_InitTypeDef DMA_InitStructure;
  127. /* DMA Channel configuration ----------------------------------------------*/
  128. DMA_Cmd(CODEC_I2S_DMA, DISABLE);
  129. DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&(CODEC_I2S_PORT->DR));
  130. DMA_InitStructure.DMA_MemoryBaseAddr = (u32) addr;
  131. DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
  132. DMA_InitStructure.DMA_BufferSize = size;
  133. DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  134. DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  135. DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  136. DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  137. DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
  138. DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  139. DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  140. DMA_Init(CODEC_I2S_DMA, &DMA_InitStructure);
  141. /* Enable SPI DMA Tx request */
  142. SPI_I2S_DMACmd(CODEC_I2S_PORT, SPI_I2S_DMAReq_Tx, ENABLE);
  143. DMA_ITConfig(CODEC_I2S_DMA, DMA_IT_TC, ENABLE);
  144. DMA_Cmd(CODEC_I2S_DMA, ENABLE);
  145. }
  146. static void I2S_Configuration(uint32_t I2S_AudioFreq)
  147. {
  148. I2S_InitTypeDef I2S_InitStructure;
  149. /* I2S peripheral configuration */
  150. I2S_InitStructure.I2S_Standard = I2S_Standard_Phillips;
  151. I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_16b;
  152. I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Disable;
  153. I2S_InitStructure.I2S_AudioFreq = I2S_AudioFreq;
  154. I2S_InitStructure.I2S_CPOL = I2S_CPOL_Low;
  155. /* I2S2 configuration */
  156. #if CODEC_MASTER_MODE
  157. I2S_InitStructure.I2S_Mode = I2S_Mode_SlaveTx;
  158. #else
  159. I2S_InitStructure.I2S_Mode = I2S_Mode_MasterTx;
  160. #endif
  161. I2S_Init(CODEC_I2S_PORT, &I2S_InitStructure);
  162. }
  163. uint8_t SPI_WriteByte(unsigned char data)
  164. {
  165. //Wait until the transmit buffer is empty
  166. while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
  167. // Send the byte
  168. SPI_I2S_SendData(SPI1, data);
  169. //Wait until a data is received
  170. while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
  171. // Get the received data
  172. data = SPI_I2S_ReceiveData(SPI1);
  173. // Return the shifted data
  174. return data;
  175. }
  176. static void codec_send(rt_uint16_t s_data)
  177. {
  178. rt_sem_take(&spi1_lock, RT_WAITING_FOREVER);
  179. codec_reset_csb();
  180. SPI_WriteByte((s_data >> 8) & 0xFF);
  181. SPI_WriteByte(s_data & 0xFF);
  182. codec_set_csb();
  183. rt_sem_release(&spi1_lock);
  184. }
  185. static rt_err_t codec_init(rt_device_t dev)
  186. {
  187. codec_send(REG_SOFTWARE_RESET);
  188. // 1.5x boost power up sequence.
  189. // Mute all outputs.
  190. codec_send(REG_LOUT1_VOL | LOUT1MUTE);
  191. codec_send(REG_ROUT1_VOL | ROUT1MUTE);
  192. codec_send(REG_LOUT2_VOL | LOUT2MUTE);
  193. codec_send(REG_ROUT2_VOL | ROUT2MUTE);
  194. // Enable unused output chosen from L/ROUT2, OUT3 or OUT4.
  195. codec_send(REG_POWER_MANAGEMENT3 | OUT4EN);
  196. // Set BUFDCOPEN=1 and BUFIOEN=1 in register R1
  197. codec_send(REG_POWER_MANAGEMENT1 | BUFDCOPEN | BUFIOEN);
  198. // Set SPKBOOST=1 in register R49.
  199. codec_send(REG_OUTPUT | SPKBOOST);
  200. // Set VMIDSEL[1:0] to required value in register R1.
  201. codec_send(REG_POWER_MANAGEMENT1 | BUFDCOPEN | BUFIOEN | VMIDSEL_75K);
  202. // Set L/RMIXEN=1 and DACENL/R=1 in register R3.
  203. codec_send(REG_POWER_MANAGEMENT3 | LMIXEN | RMIXEN | DACENL | DACENR);
  204. // Set BIASEN=1 in register R1.
  205. codec_send(REG_POWER_MANAGEMENT1 | BUFDCOPEN | BUFIOEN | VMIDSEL_75K | BIASEN);
  206. // Set L/ROUT2EN=1 in register R3.
  207. codec_send(REG_POWER_MANAGEMENT3 | LMIXEN | RMIXEN | DACENL | DACENR | LOUT2EN | ROUT2EN);
  208. // Enable other mixers as required.
  209. // Enable other outputs as required.
  210. codec_send(REG_POWER_MANAGEMENT2 | LOUT1EN | ROUT1EN | BOOSTENL | BOOSTENR | INPPGAENL | INPPGAENR);
  211. // Digital inferface setup.
  212. codec_send(REG_AUDIO_INTERFACE | BCP_NORMAL | LRP_NORMAL | WL_16BITS | FMT_I2S);
  213. // PLL setup.
  214. // fs = 44.1KHz * 256fs = 11.2896MHz
  215. // F_PLL = 11.2896MHz * 4 * 2 = 90.3168MHz
  216. // R = 90.3168MHz / 12.288MHz = 7.35
  217. // PLL_N = 7
  218. // PLL_K = 0x59999A (0x5A5A5A for STM32's 44.117KHz fs generated from 72MHz clock)
  219. codec_send(REG_PLL_N | 7);
  220. #if CODEC_MASTER_MODE
  221. codec_send(REG_PLL_K1 | 0x16);
  222. codec_send(REG_PLL_K2 | 0xCC);
  223. codec_send(REG_PLL_K3 | 0x19A);
  224. #else
  225. codec_send(REG_PLL_K1 | 0x16);
  226. codec_send(REG_PLL_K2 | 0x12D);
  227. codec_send(REG_PLL_K3 | 0x5A);
  228. #endif
  229. codec_send(REG_POWER_MANAGEMENT1 | BUFDCOPEN | BUFIOEN | VMIDSEL_75K | BIASEN | PLLEN);
  230. codec_send(r06);
  231. // Enable DAC 128x oversampling.
  232. codec_send(REG_DAC | DACOSR128);
  233. // Set LOUT2/ROUT2 in BTL operation.
  234. codec_send(REG_BEEP | INVROUT2);
  235. // Set output volume.
  236. vol(25);
  237. return RT_EOK;
  238. }
  239. // Exported functions
  240. #include <finsh.h>
  241. void vol(uint16_t v)
  242. {
  243. v = (v & VOL_MASK) << VOL_POS;
  244. codec_send(REG_LOUT1_VOL | v);
  245. codec_send(REG_ROUT1_VOL | HPVU | v);
  246. codec_send(REG_LOUT2_VOL | v);
  247. codec_send(REG_ROUT2_VOL | SPKVU | v);
  248. }
  249. void eq(codec_eq_args_t args)
  250. {
  251. switch (args->channel)
  252. {
  253. case 1:
  254. codec_send(REG_EQ1 | ((args->frequency & EQC_MASK) << EQC_POS) | ((args->gain & EQG_MASK) << EQG_POS) | (args->mode_bandwidth ? EQ3DMODE_DAC : EQ3DMODE_ADC));
  255. break;
  256. case 2:
  257. codec_send(REG_EQ2 | ((args->frequency & EQC_MASK) << EQC_POS) | ((args->gain & EQG_MASK) << EQG_POS) | (args->mode_bandwidth ? EQ2BW_WIDE : EQ2BW_NARROW));
  258. break;
  259. case 3:
  260. codec_send(REG_EQ3 | ((args->frequency & EQC_MASK) << EQC_POS) | ((args->gain & EQG_MASK) << EQG_POS) | (args->mode_bandwidth ? EQ3BW_WIDE : EQ3BW_NARROW));
  261. break;
  262. case 4:
  263. codec_send(REG_EQ4 | ((args->frequency & EQC_MASK) << EQC_POS) | ((args->gain & EQG_MASK) << EQG_POS) | (args->mode_bandwidth ? EQ4BW_WIDE : EQ4BW_NARROW));
  264. break;
  265. case 5:
  266. codec_send(REG_EQ5 | ((args->frequency & EQC_MASK) << EQC_POS) | ((args->gain & EQG_MASK) << EQG_POS));
  267. break;
  268. }
  269. }
  270. // TODO eq1() ~ eq5() are just for testing. To be removed.
  271. void eq1(uint8_t freq, uint8_t gain, uint8_t mode)
  272. {
  273. codec_send(REG_EQ1 | ((freq & EQC_MASK) << EQC_POS) | ((gain & EQG_MASK) << EQG_POS) | (mode ? EQ3DMODE_DAC : EQ3DMODE_ADC));
  274. }
  275. void eq2(uint8_t freq, uint8_t gain, uint8_t bw)
  276. {
  277. codec_send(REG_EQ2 | ((freq & EQC_MASK) << EQC_POS) | ((gain & EQG_MASK) << EQG_POS) | (bw ? EQ2BW_WIDE : EQ2BW_NARROW));
  278. }
  279. void eq3(uint8_t freq, uint8_t gain, uint8_t bw)
  280. {
  281. codec_send(REG_EQ3 | ((freq & EQC_MASK) << EQC_POS) | ((gain & EQG_MASK) << EQG_POS) | (bw ? EQ3BW_WIDE : EQ3BW_NARROW));
  282. }
  283. void eq4(uint8_t freq, uint8_t gain, uint8_t bw)
  284. {
  285. codec_send(REG_EQ4 | ((freq & EQC_MASK) << EQC_POS) | ((gain & EQG_MASK) << EQG_POS) | (bw ? EQ4BW_WIDE : EQ4BW_NARROW));
  286. }
  287. void eq5(uint8_t freq, uint8_t gain)
  288. {
  289. codec_send(REG_EQ2 | ((freq & EQC_MASK) << EQC_POS) | ((gain & EQG_MASK) << EQG_POS));
  290. }
  291. void eq3d(uint8_t depth)
  292. {
  293. codec_send(REG_3D | ((depth & DEPTH3D_MASK) << DEPTH3D_POS));
  294. }
  295. rt_err_t sample_rate(int sr)
  296. {
  297. uint16_t r07 = REG_ADDITIONAL;
  298. switch (sr)
  299. {
  300. case 8000:
  301. r06 = REG_CLOCK_GEN | CLKSEL_MCLK | MCLK_DIV6 | BCLK_DIV8 | (r06 & MS);
  302. r07 |= SR_8KHZ;
  303. break;
  304. case 11025:
  305. r06 = REG_CLOCK_GEN | CLKSEL_PLL | MCLK_DIV8 | BCLK_DIV8 | (r06 & MS);
  306. r07 |= SR_12KHZ;
  307. break;
  308. #if CODEC_MASTER_MODE
  309. case 12000:
  310. r06 = REG_CLOCK_GEN | CLKSEL_MCLK | MCLK_DIV4 | BCLK_DIV8 | (r06 & MS);
  311. r07 |= SR_12KHZ;
  312. break;
  313. #endif
  314. case 16000:
  315. r06 = REG_CLOCK_GEN | CLKSEL_MCLK | MCLK_DIV3 | BCLK_DIV8 | (r06 & MS);
  316. r07 |= SR_16KHZ;
  317. break;
  318. case 22050:
  319. r06 = REG_CLOCK_GEN | CLKSEL_PLL | MCLK_DIV4 | BCLK_DIV8 | (r06 & MS);
  320. r07 |= SR_24KHZ;
  321. break;
  322. #if CODEC_MASTER_MODE
  323. case 24000:
  324. r06 = REG_CLOCK_GEN | CLKSEL_MCLK | MCLK_DIV2 | BCLK_DIV8 | (r06 & MS);
  325. r07 |= SR_24KHZ;
  326. break;
  327. #endif
  328. case 32000:
  329. r06 = REG_CLOCK_GEN | CLKSEL_MCLK | MCLK_DIV1_5 | BCLK_DIV8 | (r06 & MS);
  330. r07 |= SR_32KHZ;
  331. break;
  332. case 44100:
  333. r06 = REG_CLOCK_GEN | CLKSEL_PLL | MCLK_DIV2 | BCLK_DIV8 | (r06 & MS);
  334. r07 |= SR_48KHZ;
  335. break;
  336. case 48000:
  337. r06 = REG_CLOCK_GEN | CLKSEL_MCLK | MCLK_DIV1 | BCLK_DIV8 | (r06 & MS);
  338. r07 |= SR_48KHZ;
  339. break;
  340. default:
  341. return RT_ERROR;
  342. }
  343. codec_send(r06);
  344. codec_send(r07);
  345. #if !CODEC_MASTER_MODE
  346. codec_sr_new = sr;
  347. #endif
  348. return RT_EOK;
  349. }
  350. FINSH_FUNCTION_EXPORT(vol, Set volume);
  351. FINSH_FUNCTION_EXPORT(eq1, Set EQ1(Cut-off, Gain, Mode));
  352. FINSH_FUNCTION_EXPORT(eq2, Set EQ2(Center, Gain, Bandwidth));
  353. FINSH_FUNCTION_EXPORT(eq3, Set EQ3(Center, Gain, Bandwidth));
  354. FINSH_FUNCTION_EXPORT(eq4, Set EQ4(Center, Gain, Bandwidth));
  355. FINSH_FUNCTION_EXPORT(eq5, Set EQ5(Cut-off, Gain));
  356. FINSH_FUNCTION_EXPORT(eq3d, Set 3D(Depth));
  357. FINSH_FUNCTION_EXPORT(sample_rate, Set sample rate);
  358. static rt_err_t codec_open(rt_device_t dev, rt_uint16_t oflag)
  359. {
  360. #if !CODEC_MASTER_MODE
  361. /* enable I2S */
  362. I2S_Cmd(CODEC_I2S_PORT, ENABLE);
  363. #endif
  364. return RT_EOK;
  365. }
  366. static rt_err_t codec_close(rt_device_t dev)
  367. {
  368. #if CODEC_MASTER_MODE
  369. if (r06 & MS)
  370. {
  371. CODEC_I2S_DMA->CCR &= ~DMA_CCR1_EN;
  372. while ((CODEC_I2S_PORT->SR & SPI_I2S_FLAG_TXE) == 0);
  373. while ((CODEC_I2S_PORT->SR & SPI_I2S_FLAG_BSY) != 0);
  374. CODEC_I2S_PORT->I2SCFGR &= ~SPI_I2SCFGR_I2SE;
  375. r06 &= ~MS;
  376. codec_send(r06);
  377. /* remove all data node */
  378. if (codec.parent.tx_complete != RT_NULL)
  379. {
  380. rt_base_t level = rt_hw_interrupt_disable();
  381. do
  382. {
  383. codec.parent.tx_complete(&codec.parent, codec.data_list[codec.read_index].data_ptr);
  384. codec.read_index++;
  385. if (codec.read_index >= DATA_NODE_MAX)
  386. {
  387. codec.read_index = 0;
  388. }
  389. }
  390. while (codec.read_index != codec.put_index);
  391. rt_hw_interrupt_enable(level);
  392. }
  393. }
  394. #endif
  395. return RT_EOK;
  396. }
  397. static rt_err_t codec_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  398. {
  399. switch (cmd)
  400. {
  401. case CODEC_CMD_RESET:
  402. codec_init(dev);
  403. break;
  404. case CODEC_CMD_VOLUME:
  405. vol(*((uint16_t*) args));
  406. break;
  407. case CODEC_CMD_SAMPLERATE:
  408. sample_rate(*((int*) args));
  409. break;
  410. case CODEC_CMD_EQ:
  411. eq((codec_eq_args_t) args);
  412. break;
  413. case CODEC_CMD_3D:
  414. eq3d(*((uint8_t*) args));
  415. break;
  416. default:
  417. return RT_ERROR;
  418. }
  419. return RT_EOK;
  420. }
  421. static rt_size_t codec_write(rt_device_t dev, rt_off_t pos,
  422. const void* buffer, rt_size_t size)
  423. {
  424. struct codec_device* device;
  425. struct codec_data_node* node;
  426. rt_uint32_t level;
  427. rt_uint16_t next_index;
  428. device = (struct codec_device*) dev;
  429. RT_ASSERT(device != RT_NULL);
  430. next_index = device->put_index + 1;
  431. if (next_index >= DATA_NODE_MAX)
  432. next_index = 0;
  433. /* check data_list full */
  434. if (next_index == device->read_index)
  435. {
  436. rt_set_errno(-RT_EFULL);
  437. return 0;
  438. }
  439. level = rt_hw_interrupt_disable();
  440. node = &device->data_list[device->put_index];
  441. device->put_index = next_index;
  442. /* set node attribute */
  443. node->data_ptr = (rt_uint16_t*) buffer;
  444. node->data_size = size >> 1; /* size is byte unit, convert to half word unit */
  445. next_index = device->read_index + 1;
  446. if (next_index >= DATA_NODE_MAX)
  447. next_index = 0;
  448. /* check data list whether is empty */
  449. if (next_index == device->put_index)
  450. {
  451. DMA_Configuration((rt_uint32_t) node->data_ptr, node->data_size);
  452. #if CODEC_MASTER_MODE
  453. if ((r06 & MS) == 0)
  454. {
  455. CODEC_I2S_PORT->I2SCFGR |= SPI_I2SCFGR_I2SE;
  456. r06 |= MS;
  457. codec_send(r06);
  458. }
  459. #endif
  460. }
  461. rt_hw_interrupt_enable(level);
  462. return size;
  463. }
  464. rt_err_t codec_hw_init(void)
  465. {
  466. rt_device_t dev;
  467. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE);
  468. RCC_APB1PeriphClockCmd(CODEC_I2S_RCC_APB1, ENABLE);
  469. RCC_AHBPeriphClockCmd(CODEC_I2S_RCC_AHB, ENABLE);
  470. NVIC_Configuration();
  471. GPIO_Configuration();
  472. I2S_Configuration(I2S_AudioFreq_44k);
  473. dev = (rt_device_t) &codec;
  474. dev->type = RT_Device_Class_Sound;
  475. dev->rx_indicate = RT_NULL;
  476. dev->tx_complete = RT_NULL;
  477. dev->init = codec_init;
  478. dev->open = codec_open;
  479. dev->close = codec_close;
  480. dev->read = RT_NULL;
  481. dev->write = codec_write;
  482. dev->control = codec_control;
  483. dev->private = RT_NULL;
  484. /* set read_index and put index to 0 */
  485. codec.read_index = 0;
  486. codec.put_index = 0;
  487. /* unselect */
  488. codec_set_csb();
  489. /* register the device */
  490. return rt_device_register(&codec.parent, "snd", RT_DEVICE_FLAG_WRONLY | RT_DEVICE_FLAG_DMA_TX);
  491. }
  492. void codec_dma_isr(void)
  493. {
  494. /* switch to next buffer */
  495. rt_uint16_t next_index;
  496. void* data_ptr;
  497. next_index = codec.read_index + 1;
  498. if (next_index >= DATA_NODE_MAX)
  499. next_index = 0;
  500. /* save current data pointer */
  501. data_ptr = codec.data_list[codec.read_index].data_ptr;
  502. #if !CODEC_MASTER_MODE
  503. if (codec_sr_new)
  504. {
  505. I2S_Configuration(codec_sr_new);
  506. I2S_Cmd(CODEC_I2S_PORT, ENABLE);
  507. codec_sr_new = 0;
  508. }
  509. #endif
  510. codec.read_index = next_index;
  511. if (next_index != codec.put_index)
  512. {
  513. /* enable next dma request */
  514. DMA_Configuration((rt_uint32_t) codec.data_list[codec.read_index].data_ptr, codec.data_list[codec.read_index].data_size);
  515. #if CODEC_MASTER_MODE
  516. if ((r06 & MS) == 0)
  517. {
  518. CODEC_I2S_PORT->I2SCFGR |= SPI_I2SCFGR_I2SE;
  519. r06 |= MS;
  520. codec_send(r06);
  521. }
  522. #endif
  523. }
  524. else
  525. {
  526. #if CODEC_MASTER_MODE
  527. if (r06 & MS)
  528. {
  529. CODEC_I2S_DMA->CCR &= ~DMA_CCR1_EN;
  530. while ((CODEC_I2S_PORT->SR & SPI_I2S_FLAG_TXE) == 0);
  531. while ((CODEC_I2S_PORT->SR & SPI_I2S_FLAG_BSY) != 0);
  532. CODEC_I2S_PORT->I2SCFGR &= ~SPI_I2SCFGR_I2SE;
  533. r06 &= ~MS;
  534. codec_send(r06);
  535. }
  536. #endif
  537. rt_kprintf("*\n");
  538. }
  539. /* notify transmitted complete. */
  540. if (codec.parent.tx_complete != RT_NULL)
  541. {
  542. codec.parent.tx_complete(&codec.parent, data_ptr);
  543. }
  544. }