codec.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. #include <rthw.h>
  2. #include <rtthread.h>
  3. #include "stm32f10x.h"
  4. #include "codec.h"
  5. /*
  6. SCLK PA5 SPI1_SCK
  7. SDIN PA7 SPI1_MOSI
  8. CSB PC5
  9. */
  10. #define CODEC_CSB_PORT GPIOC
  11. #define CODEC_CSB_PIN GPIO_Pin_5
  12. #define codec_set_csb() do { CODEC_CSB_PORT->BSRR = CODEC_CSB_PIN; } while (0)
  13. #define codec_reset_csb() do { CODEC_CSB_PORT->BRR = CODEC_CSB_PIN; } while (0)
  14. void vol(uint16_t v);
  15. #define DATA_NODE_MAX 5
  16. /* data node for Tx Mode */
  17. struct codec_data_node
  18. {
  19. rt_uint16_t *data_ptr;
  20. rt_size_t data_size;
  21. };
  22. struct codec_device
  23. {
  24. /* inherit from rt_device */
  25. struct rt_device parent;
  26. /* pcm data list */
  27. struct codec_data_node data_list[DATA_NODE_MAX];
  28. rt_uint16_t read_index, put_index;
  29. /* transmitted offset of current data node */
  30. rt_size_t offset;
  31. };
  32. struct codec_device codec;
  33. struct pll_ratio
  34. {
  35. uint8_t n;
  36. uint8_t k1;
  37. uint16_t k2;
  38. uint16_t k3;
  39. };
  40. static void delay_ms(unsigned int dt)
  41. {
  42. volatile unsigned int u;
  43. for (u = 0; u < dt * 30; u++);
  44. }
  45. static void NVIC_Configuration(void)
  46. {
  47. NVIC_InitTypeDef NVIC_InitStructure;
  48. /* SPI2 IRQ Channel configuration */
  49. NVIC_InitStructure.NVIC_IRQChannel = SPI2_IRQn;
  50. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  51. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  52. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  53. NVIC_Init(&NVIC_InitStructure);
  54. /* DMA1 IRQ Channel configuration */
  55. NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel5_IRQn;
  56. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  57. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  58. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  59. NVIC_Init(&NVIC_InitStructure);
  60. }
  61. static void GPIO_Configuration(void)
  62. {
  63. GPIO_InitTypeDef GPIO_InitStructure;
  64. /* Disable the JTAG interface and enable the SWJ interface */
  65. GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
  66. /* PC5 CODEC CS */
  67. GPIO_InitStructure.GPIO_Pin = CODEC_CSB_PIN;
  68. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  69. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  70. GPIO_Init(CODEC_CSB_PORT, &GPIO_InitStructure);
  71. /* Configure SPI2 pins: CK, WS and SD */
  72. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_15;
  73. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  74. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  75. GPIO_Init(GPIOB, &GPIO_InitStructure);
  76. #ifdef CODEC_USE_MCO
  77. /* MCO configure */
  78. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  79. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  80. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  81. GPIO_Init(GPIOA,&GPIO_InitStructure);
  82. RCC_MCOConfig(RCC_MCO_HSE);
  83. #endif
  84. }
  85. static void DMA_Configuration(rt_uint32_t addr, rt_size_t size)
  86. {
  87. DMA_InitTypeDef DMA_InitStructure;
  88. /* DMA1 Channel2 configuration ----------------------------------------------*/
  89. DMA_Cmd(DMA1_Channel5, DISABLE);
  90. DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&(SPI2->DR));
  91. DMA_InitStructure.DMA_MemoryBaseAddr = (u32) addr;
  92. DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
  93. DMA_InitStructure.DMA_BufferSize = size;
  94. DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  95. DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  96. DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  97. DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  98. DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  99. DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  100. DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  101. DMA_Init(DMA1_Channel5, &DMA_InitStructure);
  102. /* Enable SPI2 DMA Tx request */
  103. SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx, ENABLE);
  104. DMA_ITConfig(DMA1_Channel5, DMA_IT_TC, ENABLE);
  105. DMA_Cmd(DMA1_Channel5, ENABLE);
  106. }
  107. static void I2S_Configuration(void)
  108. {
  109. I2S_InitTypeDef I2S_InitStructure;
  110. /* I2S peripheral configuration */
  111. I2S_InitStructure.I2S_Standard = I2S_Standard_Phillips;
  112. I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_16b;
  113. I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Disable;
  114. I2S_InitStructure.I2S_AudioFreq = I2S_AudioFreq_44k;
  115. I2S_InitStructure.I2S_CPOL = I2S_CPOL_High; // I2S_CPOL_Low
  116. /* I2S2 Master Transmitter to I2S3 Slave Receiver communication -----------*/
  117. /* I2S2 configuration */
  118. I2S_InitStructure.I2S_Mode = I2S_Mode_MasterTx; //I2S_Mode_MasterTx I2S_Mode_SlaveTx
  119. I2S_Init(SPI2, &I2S_InitStructure);
  120. }
  121. uint8_t SPI_WriteByte(unsigned char data)
  122. {
  123. //Wait until the transmit buffer is empty
  124. while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
  125. // Send the byte
  126. SPI_I2S_SendData(SPI1, data);
  127. //Wait until a data is received
  128. while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
  129. // Get the received data
  130. data = SPI_I2S_ReceiveData(SPI1);
  131. // Return the shifted data
  132. return data;
  133. }
  134. static void codec_send(rt_uint16_t s_data)
  135. {
  136. codec_reset_csb();
  137. SPI_WriteByte((s_data >> 8) & 0xFF);
  138. SPI_WriteByte(s_data & 0xFF);
  139. codec_set_csb();
  140. }
  141. static rt_err_t codec_init(rt_device_t dev)
  142. {
  143. codec_send(REG_SOFTWARE_RESET);
  144. // 1.5x boost power up sequence.
  145. // Mute all outputs.
  146. codec_send(REG_LOUT1_VOL | LOUT1MUTE);
  147. codec_send(REG_ROUT1_VOL | ROUT1MUTE);
  148. codec_send(REG_LOUT2_VOL | LOUT2MUTE);
  149. codec_send(REG_ROUT2_VOL | ROUT2MUTE);
  150. // Enable unused output chosen from L/ROUT2, OUT3 or OUT4.
  151. codec_send(REG_POWER_MANAGEMENT3 | OUT4EN);
  152. // Set BUFDCOPEN=1 and BUFIOEN=1 in register R1
  153. codec_send(REG_POWER_MANAGEMENT1 | BUFDCOPEN | BUFIOEN);
  154. // Set SPKBOOST=1 in register R49.
  155. codec_send(REG_OUTPUT | SPKBOOST);
  156. // Set VMIDSEL[1:0] to required value in register R1.
  157. codec_send(REG_POWER_MANAGEMENT1 | BUFDCOPEN | BUFIOEN | VMIDSEL_75K);
  158. // Wait for VMID supply to settle.
  159. delay_ms(750);
  160. // Set L/RMIXEN=1 and DACENL/R=1 in register R3.
  161. codec_send(REG_POWER_MANAGEMENT3 | LMIXEN | RMIXEN | DACENL | DACENR);
  162. // Set BIASEN=1 in register R1.
  163. codec_send(REG_POWER_MANAGEMENT1 | BUFDCOPEN | BUFIOEN | VMIDSEL_75K | BIASEN);
  164. // Set L/ROUT2EN=1 in register R3.
  165. codec_send(REG_POWER_MANAGEMENT3 | LMIXEN | RMIXEN | DACENL | DACENR | LOUT2EN | ROUT2EN);
  166. // Enable other mixers as required.
  167. // Enable other outputs as required.
  168. codec_send(REG_POWER_MANAGEMENT2 | LOUT1EN | ROUT1EN | BOOSTENL | BOOSTENR | INPPGAENL | INPPGAENR);
  169. // Digital inferface setup.
  170. codec_send(REG_AUDIO_INTERFACE | BCP_NORMAL | LRP_NORMAL | WL_16BITS | FMT_I2S);
  171. // PLL setup.
  172. // fs = 44.1KHz / 256fs = 11.2896MHz
  173. // F_PLL = 11.2896MHz * 4 * 2 = 90.3168MHz
  174. // R = 90.3168MHz / 12.288MHz = 7.35
  175. // PLL_N = 7
  176. // PLL_K = 5872026
  177. codec_send(REG_PLL_N | 7);
  178. codec_send(REG_PLL_K1 | 0x16);
  179. codec_send(REG_PLL_K2 | 0xCC);
  180. codec_send(REG_PLL_K3 | 0x19A);
  181. codec_send(REG_POWER_MANAGEMENT1 | BUFDCOPEN | BUFIOEN | VMIDSEL_75K | BIASEN | PLLEN);
  182. codec_send(REG_CLOCK_GEN | CLKSEL_PLL | MCLK_DIV2);
  183. // Enable DAC 128x oversampling.
  184. codec_send(REG_DAC | DACOSR128);
  185. // Set LOUT2/ROUT2 in BTL operation.
  186. codec_send(REG_BEEP | INVROUT2);
  187. // Set output volume to -22dB.
  188. vol(35);
  189. return RT_EOK;
  190. }
  191. // Exported functions
  192. #include <finsh.h>
  193. void vol(uint16_t v)
  194. {
  195. v = (v & VOL_MASK) << VOL_POS;
  196. codec_send(REG_LOUT1_VOL | v);
  197. codec_send(REG_ROUT1_VOL | HPVU | v);
  198. codec_send(REG_LOUT2_VOL | v);
  199. codec_send(REG_ROUT2_VOL | SPKVU | v);
  200. }
  201. void eq1(uint8_t freq, uint8_t gain, uint8_t mode)
  202. {
  203. codec_send(REG_EQ1 | ((freq & EQC_MASK) << EQC_POS) | ((gain & EQG_MASK) << EQG_POS) | (mode ? EQ3DMODE_DAC : EQ3DMODE_ADC));
  204. }
  205. void eq2(uint8_t freq, uint8_t gain, uint8_t bw)
  206. {
  207. codec_send(REG_EQ2 | ((freq & EQC_MASK) << EQC_POS) | ((gain & EQG_MASK) << EQG_POS) | (bw ? EQ2BW_WIDE : EQ2BW_NARROW));
  208. }
  209. void eq3(uint8_t freq, uint8_t gain, uint8_t bw)
  210. {
  211. codec_send(REG_EQ3 | ((freq & EQC_MASK) << EQC_POS) | ((gain & EQG_MASK) << EQG_POS) | (bw ? EQ3BW_WIDE : EQ3BW_NARROW));
  212. }
  213. void eq4(uint8_t freq, uint8_t gain, uint8_t bw)
  214. {
  215. codec_send(REG_EQ4 | ((freq & EQC_MASK) << EQC_POS) | ((gain & EQG_MASK) << EQG_POS) | (bw ? EQ4BW_WIDE : EQ4BW_NARROW));
  216. }
  217. void eq5(uint8_t freq, uint8_t gain)
  218. {
  219. codec_send(REG_EQ2 | ((freq & EQC_MASK) << EQC_POS) | ((gain & EQG_MASK) << EQG_POS));
  220. }
  221. void eq3d(uint8_t depth)
  222. {
  223. codec_send(REG_3D | ((depth & DEPTH3D_MASK) << DEPTH3D_POS));
  224. }
  225. void sample_rate(uint8_t sr)
  226. {
  227. if (sr == 44)
  228. {
  229. codec_send(REG_ADDITIONAL | SR_48KHZ);
  230. codec_send(REG_CLOCK_GEN | CLKSEL_PLL | MCLK_DIV2);
  231. }
  232. else
  233. {
  234. switch (sr)
  235. {
  236. case 8:
  237. codec_send(REG_ADDITIONAL | SR_8KHZ);
  238. break;
  239. case 12:
  240. codec_send(REG_ADDITIONAL | SR_12KHZ);
  241. break;
  242. case 16:
  243. codec_send(REG_ADDITIONAL | SR_16KHZ);
  244. break;
  245. case 24:
  246. codec_send(REG_ADDITIONAL | SR_24KHZ);
  247. break;
  248. case 32:
  249. codec_send(REG_ADDITIONAL | SR_32KHZ);
  250. break;
  251. case 48:
  252. codec_send(REG_ADDITIONAL | SR_48KHZ);
  253. break;
  254. default:
  255. return;
  256. }
  257. codec_send(REG_CLOCK_GEN | CLKSEL_MCLK | MCLK_DIV1);
  258. }
  259. }
  260. FINSH_FUNCTION_EXPORT(vol, Set volume);
  261. FINSH_FUNCTION_EXPORT(eq1, Set EQ1(Cut-off, Gain, Mode));
  262. FINSH_FUNCTION_EXPORT(eq2, Set EQ2(Center, Gain, Bandwidth));
  263. FINSH_FUNCTION_EXPORT(eq3, Set EQ3(Center, Gain, Bandwidth));
  264. FINSH_FUNCTION_EXPORT(eq4, Set EQ4(Center, Gain, Bandwidth));
  265. FINSH_FUNCTION_EXPORT(eq5, Set EQ5(Cut-off, Gain));
  266. FINSH_FUNCTION_EXPORT(eq3d, Set 3D(Depth));
  267. FINSH_FUNCTION_EXPORT(sample_rate, Set sample rate);
  268. static rt_err_t codec_open(rt_device_t dev, rt_uint16_t oflag)
  269. {
  270. /* enable I2S */
  271. I2S_Cmd(SPI2, ENABLE);
  272. return RT_EOK;
  273. }
  274. static rt_err_t codec_close(rt_device_t dev)
  275. {
  276. /* interrupt mode */
  277. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  278. {
  279. /* Disable the I2S2 */
  280. I2S_Cmd(SPI2, DISABLE);
  281. }
  282. /* remove all data node */
  283. return RT_EOK;
  284. }
  285. static rt_err_t codec_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  286. {
  287. /* rate control */
  288. return RT_EOK;
  289. }
  290. static rt_size_t codec_write(rt_device_t dev, rt_off_t pos,
  291. const void* buffer, rt_size_t size)
  292. {
  293. struct codec_device* device;
  294. struct codec_data_node* node;
  295. rt_uint32_t level;
  296. rt_uint16_t next_index;
  297. device = (struct codec_device*) dev;
  298. RT_ASSERT(device != RT_NULL);
  299. next_index = device->put_index + 1;
  300. if (next_index >= DATA_NODE_MAX)
  301. next_index = 0;
  302. /* check data_list full */
  303. if (next_index == device->read_index)
  304. {
  305. rt_set_errno(-RT_EFULL);
  306. return 0;
  307. }
  308. level = rt_hw_interrupt_disable();
  309. node = &device->data_list[device->put_index];
  310. device->put_index = next_index;
  311. // rt_kprintf("+\n");
  312. /* set node attribute */
  313. node->data_ptr = (rt_uint16_t*) buffer;
  314. node->data_size = size >> 1; /* size is byte unit, convert to half word unit */
  315. next_index = device->read_index + 1;
  316. if (next_index >= DATA_NODE_MAX)
  317. next_index = 0;
  318. /* check data list whether is empty */
  319. if (next_index == device->put_index)
  320. {
  321. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  322. {
  323. device->offset = 0;
  324. /* enable I2S interrupt */
  325. SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_TXE, ENABLE);
  326. }
  327. else if (dev->flag & RT_DEVICE_FLAG_DMA_TX)
  328. {
  329. DMA_Configuration((rt_uint32_t) node->data_ptr, node->data_size);
  330. }
  331. }
  332. rt_hw_interrupt_enable(level);
  333. return size;
  334. }
  335. rt_err_t codec_hw_init(void)
  336. {
  337. rt_device_t dev;
  338. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE);
  339. RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
  340. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
  341. NVIC_Configuration();
  342. GPIO_Configuration();
  343. I2S_Configuration();
  344. dev = (rt_device_t) &codec;
  345. dev->type = RT_Device_Class_Unknown;
  346. dev->rx_indicate = RT_NULL;
  347. dev->tx_complete = RT_NULL;
  348. dev->init = codec_init;
  349. dev->open = codec_open;
  350. dev->close = codec_close;
  351. dev->read = RT_NULL;
  352. dev->write = codec_write;
  353. dev->control = codec_control;
  354. dev->private = RT_NULL;
  355. /* set read_index and put index to 0 */
  356. codec.read_index = 0;
  357. codec.put_index = 0;
  358. /* unselect */
  359. codec_set_csb();
  360. /* register the device */
  361. return rt_device_register(&codec.parent, "snd", RT_DEVICE_FLAG_WRONLY | RT_DEVICE_FLAG_DMA_TX);
  362. }
  363. void codec_isr()
  364. {
  365. struct codec_data_node* node;
  366. node = &codec.data_list[codec.read_index]; /* get current data node */
  367. if (SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_TXE) == SET)
  368. {
  369. SPI_I2S_SendData(SPI2, node->data_ptr[codec.offset++]);
  370. }
  371. if (codec.offset == node->data_size)
  372. {
  373. /* move to next node */
  374. rt_uint16_t next_index;
  375. next_index = codec.read_index + 1;
  376. if (next_index >= DATA_NODE_MAX)
  377. next_index = 0;
  378. /* notify transmitted complete. */
  379. if (codec.parent.tx_complete != RT_NULL)
  380. {
  381. codec.parent.tx_complete(&codec.parent,
  382. codec.data_list[codec.read_index].data_ptr);
  383. rt_kprintf("-\n");
  384. }
  385. codec.offset = 0;
  386. codec.read_index = next_index;
  387. if (next_index == codec.put_index)
  388. {
  389. /* no data on the list, disable I2S interrupt */
  390. SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_TXE, DISABLE);
  391. rt_kprintf("*\n");
  392. }
  393. }
  394. }
  395. void codec_dma_isr()
  396. {
  397. /* switch to next buffer */
  398. rt_uint16_t next_index;
  399. void* data_ptr;
  400. next_index = codec.read_index + 1;
  401. if (next_index >= DATA_NODE_MAX)
  402. next_index = 0;
  403. /* save current data pointer */
  404. data_ptr = codec.data_list[codec.read_index].data_ptr;
  405. codec.read_index = next_index;
  406. if (next_index != codec.put_index)
  407. {
  408. /* enable next dma request */
  409. DMA_Configuration((rt_uint32_t) codec.data_list[codec.read_index].data_ptr, codec.data_list[codec.read_index].data_size);
  410. }
  411. else
  412. {
  413. rt_kprintf("*\n");
  414. }
  415. /* notify transmitted complete. */
  416. if (codec.parent.tx_complete != RT_NULL)
  417. {
  418. codec.parent.tx_complete(&codec.parent, data_ptr);
  419. // rt_kprintf("-\n");
  420. }
  421. }