codec.c 17 KB

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